update
[cgds.git] / src / Stack.c
1 /**
2 * @file Stack.c
3 */
4
5 #include "cgds/Stack.h"
6
7 void _stack_init(Stack* stack, size_t dataSize)
8 {
9 stack->dataSize = dataSize;
10 _vector_init(stack->array, dataSize);
11 }
12
13 Stack* _stack_new(size_t dataSize)
14 {
15 Stack* stack = (Stack*) safe_malloc(sizeof (Stack));
16 _stack_init(stack, dataSize);
17 return stack;
18 }
19
20 Stack* stack_copy(Stack* stack)
21 {
22 Stack* stackCopy = (Stack*) safe_malloc(sizeof (Stack));
23 stackCopy->dataSize = stack->dataSize;
24 Vector* arrayCopy = vector_copy(stack->array);
25 stackCopy->array = arrayCopy;
26 return stackCopy;
27 }
28
29 Bool stack_empty(Stack* stack)
30 {
31 return vector_empty(stack->array);
32 }
33
34 UInt stack_size(Stack* stack)
35 {
36 return vector_size(stack->array);
37 }
38
39 void _stack_push(Stack* stack, void* data)
40 {
41 _vector_push(stack->array, data);
42 }
43
44 void* _stack_top(Stack* stack)
45 {
46 return _vector_get(stack->array, vector_size(stack->array)-1);
47 }
48
49 void stack_pop(Stack* stack)
50 {
51 vector_pop(stack->array);
52 }
53
54 void stack_clear(Stack* stack)
55 {
56 vector_clear(stack->array);
57 }
58
59 void stack_destroy(Stack* stack)
60 {
61 stack_clear(stack);
62 safe_free(stack);
63 }