Replace Stack List internal usage by a Vector (lighter)
[cgds.git] / src / Stack.h
CommitLineData
a7868768
BA
1/**
2 * @file Stack.h
3 */
4
5#ifndef CGDS_STACK_H
6#define CGDS_STACK_H
7
8#include <stdlib.h>
9#include <string.h>
10#include "cgds/types.h"
11#include "cgds/safe_alloc.h"
10b9967a 12#include "cgds/Vector.h"
a7868768
BA
13
14/**
15 * @brief Stack containing generic data.
16 */
17typedef struct Stack {
a7868768 18 size_t dataSize; ///< Size in bytes of a stack element.
10b9967a 19 Vector* array; ///< Internal data structure: resizeable array.
a7868768
BA
20} Stack;
21
22/**
23 * @brief Initialize an empty stack.
24 */
25void _stack_init(
26 Stack* stack, ///< "this" pointer.
27 size_t dataSize ///< Size in bytes of a stack element.
28);
29
30/**
31 * @brief Return an allocated and initialized stack.
32 */
33Stack* _stack_new(
34 size_t dataSize ///< Size in bytes of a stack element.
35);
36
37/**
38 * @brief Return an allocated and initialized stack.
39 * @param type Type of a stack element (int, char*, ...).
40 *
41 * Usage: Stack* stack_new(<Type> type)
42 */
43#define stack_new(type) \
44 _stack_new(sizeof(type))
45
46/**
47 * @brief Copy constructor (works well if items do not have allocated sub-pointers).
48 */
49Stack* stack_copy(
50 Stack* stack ///< "this" pointer.
51);
52
53/**
54 * @brief Check if the stack is empty.
55 */
56Bool stack_empty(
57 Stack* stack ///< "this" pointer.
58);
59
60/**
61 * @brief Return size of the current stack.
62 */
63UInt stack_size(
64 Stack* stack ///< "this" pointer.
65);
66
67/**
68 * @brief Add something on top of the stack.
69 */
70void _stack_push(
71 Stack* stack, ///< "this" pointer.
72 void* data ///< Data to be added.
73);
74
75/**
76 * @brief Add something on top of the stack.
77 * @param stack "this" pointer.
78 * @param data Data to be added.
79 *
80 * Usage: void stack_push(Stack* stack, void data)
81 */
82#define stack_push(stack, data) \
83{ \
84 typeof((data)) tmp = data; \
85 _stack_push(stack,&tmp); \
86}
87
88/**
89 * @brief Return what is on top of the stack.
90 */
91void* _stack_top(
92 Stack* stack ///< "this" pointer.
93);
94
95/**
96 * @brief Return what is on top of the stack.
97 * @param stack "this" pointer.
98 * @param data Data to be assigned.
99 *
100 * Usage: void stack_top(Stack* stack, void data)
101 */
102#define stack_top(stack, data) \
103{ \
104 void* pData = _stack_top(stack); \
105 data = *((typeof(&data))pData); \
106}
107
108/**
109 * @brief Remove the top of the stack.
110 */
111void stack_pop(
112 Stack* stack ///< "this" pointer.
113);
114
115/**
116 * @brief Clear the entire stack.
117 */
118void stack_clear(
119 Stack* stack ///< "this" pointer.
120);
121
122/**
123 * @brief Destroy the stack: clear it, and free 'stack' pointer.
124 */
125void stack_destroy(
126 Stack* stack ///< "this" pointer.
127);
128
129#endif