Commit | Line | Data |
---|---|---|
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 | */ | |
17 | typedef struct Stack { | |
e45132ac BA |
18 | size_t dataSize; ///< Size in bytes of a stack element. |
19 | Vector* array; ///< Internal data structure: resizeable array. | |
a7868768 BA |
20 | } Stack; |
21 | ||
22 | /** | |
23 | * @brief Initialize an empty stack. | |
24 | */ | |
25 | void _stack_init( | |
e45132ac BA |
26 | Stack* stack, ///< "this" pointer. |
27 | size_t dataSize ///< Size in bytes of a stack element. | |
a7868768 BA |
28 | ); |
29 | ||
1ff641f9 | 30 | /** |
a7868768 BA |
31 | * @brief Return an allocated and initialized stack. |
32 | */ | |
33 | Stack* _stack_new( | |
e45132ac | 34 | size_t dataSize ///< Size in bytes of a stack element. |
a7868768 BA |
35 | ); |
36 | ||
1ff641f9 | 37 | /** |
a7868768 BA |
38 | * @brief Return an allocated and initialized stack. |
39 | * @param type Type of a stack element (int, char*, ...). | |
1ff641f9 | 40 | * |
a7868768 BA |
41 | * Usage: Stack* stack_new(<Type> type) |
42 | */ | |
43 | #define stack_new(type) \ | |
e45132ac BA |
44 | { \ |
45 | _stack_new(sizeof(type)); \ | |
46 | } | |
a7868768 BA |
47 | |
48 | /** | |
1ff641f9 | 49 | * @brief Copy constructor (shallow copy, ok for basic types). |
a7868768 BA |
50 | */ |
51 | Stack* stack_copy( | |
e45132ac | 52 | Stack* stack ///< "this" pointer. |
a7868768 BA |
53 | ); |
54 | ||
55 | /** | |
56 | * @brief Check if the stack is empty. | |
57 | */ | |
1ff641f9 | 58 | bool stack_empty( |
e45132ac | 59 | Stack* stack ///< "this" pointer. |
a7868768 BA |
60 | ); |
61 | ||
62 | /** | |
63 | * @brief Return size of the current stack. | |
64 | */ | |
65 | UInt stack_size( | |
e45132ac | 66 | Stack* stack ///< "this" pointer. |
a7868768 BA |
67 | ); |
68 | ||
69 | /** | |
70 | * @brief Add something on top of the stack. | |
71 | */ | |
72 | void _stack_push( | |
e45132ac BA |
73 | Stack* stack, ///< "this" pointer. |
74 | void* data ///< Data to be added. | |
a7868768 BA |
75 | ); |
76 | ||
77 | /** | |
78 | * @brief Add something on top of the stack. | |
79 | * @param stack "this" pointer. | |
80 | * @param data Data to be added. | |
1ff641f9 | 81 | * |
a7868768 BA |
82 | * Usage: void stack_push(Stack* stack, void data) |
83 | */ | |
84 | #define stack_push(stack, data) \ | |
85 | { \ | |
e45132ac BA |
86 | typeof((data)) tmp = data; \ |
87 | _stack_push(stack,&tmp); \ | |
a7868768 BA |
88 | } |
89 | ||
90 | /** | |
91 | * @brief Return what is on top of the stack. | |
92 | */ | |
93 | void* _stack_top( | |
e45132ac | 94 | Stack* stack ///< "this" pointer. |
a7868768 BA |
95 | ); |
96 | ||
97 | /** | |
98 | * @brief Return what is on top of the stack. | |
99 | * @param stack "this" pointer. | |
100 | * @param data Data to be assigned. | |
1ff641f9 | 101 | * |
a7868768 BA |
102 | * Usage: void stack_top(Stack* stack, void data) |
103 | */ | |
104 | #define stack_top(stack, data) \ | |
105 | { \ | |
e45132ac BA |
106 | void* pData = _stack_top(stack); \ |
107 | data = *((typeof(&data))pData); \ | |
a7868768 BA |
108 | } |
109 | ||
110 | /** | |
111 | * @brief Remove the top of the stack. | |
112 | */ | |
113 | void stack_pop( | |
e45132ac | 114 | Stack* stack ///< "this" pointer. |
a7868768 BA |
115 | ); |
116 | ||
117 | /** | |
118 | * @brief Clear the entire stack. | |
119 | */ | |
120 | void stack_clear( | |
e45132ac | 121 | Stack* stack ///< "this" pointer. |
a7868768 BA |
122 | ); |
123 | ||
124 | /** | |
125 | * @brief Destroy the stack: clear it, and free 'stack' pointer. | |
126 | */ | |
127 | void stack_destroy( | |
e45132ac | 128 | Stack* stack ///< "this" pointer. |
a7868768 BA |
129 | ); |
130 | ||
131 | #endif |