Some fixes + improvements (Vector) + code reformatting
[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 {
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 */
25void _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 */
33Stack* _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) \
eed1b5d2 44 _stack_new(sizeof(type))
a7868768
BA
45
46/**
1ff641f9 47 * @brief Copy constructor (shallow copy, ok for basic types).
a7868768
BA
48 */
49Stack* stack_copy(
e45132ac 50 Stack* stack ///< "this" pointer.
a7868768
BA
51);
52
53/**
54 * @brief Check if the stack is empty.
55 */
1ff641f9 56bool stack_empty(
e45132ac 57 Stack* stack ///< "this" pointer.
a7868768
BA
58);
59
60/**
61 * @brief Return size of the current stack.
62 */
63UInt stack_size(
e45132ac 64 Stack* stack ///< "this" pointer.
a7868768
BA
65);
66
67/**
68 * @brief Add something on top of the stack.
69 */
70void _stack_push(
e45132ac
BA
71 Stack* stack, ///< "this" pointer.
72 void* data ///< Data to be added.
a7868768
BA
73);
74
75/**
76 * @brief Add something on top of the stack.
77 * @param stack "this" pointer.
78 * @param data Data to be added.
1ff641f9 79 *
a7868768
BA
80 * Usage: void stack_push(Stack* stack, void data)
81 */
82#define stack_push(stack, data) \
83{ \
eed1b5d2
BA
84 typeof(data) tmp = data; \
85 _stack_push(stack, &tmp); \
a7868768
BA
86}
87
88/**
89 * @brief Return what is on top of the stack.
90 */
91void* _stack_top(
e45132ac 92 Stack* stack ///< "this" pointer.
a7868768
BA
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.
1ff641f9 99 *
a7868768
BA
100 * Usage: void stack_top(Stack* stack, void data)
101 */
102#define stack_top(stack, data) \
103{ \
e45132ac
BA
104 void* pData = _stack_top(stack); \
105 data = *((typeof(&data))pData); \
a7868768
BA
106}
107
108/**
109 * @brief Remove the top of the stack.
110 */
111void stack_pop(
e45132ac 112 Stack* stack ///< "this" pointer.
a7868768
BA
113);
114
115/**
116 * @brief Clear the entire stack.
117 */
118void stack_clear(
e45132ac 119 Stack* stack ///< "this" pointer.
a7868768
BA
120);
121
122/**
123 * @brief Destroy the stack: clear it, and free 'stack' pointer.
124 */
125void stack_destroy(
e45132ac 126 Stack* stack ///< "this" pointer.
a7868768
BA
127);
128
129#endif