41c9bbc4c0e9ca951654239774e6bae2405c6e5f
[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->array = _vector_new(dataSize);
17 _stack_init(stack, dataSize);
18 return stack;
19 }
20
21 Stack* stack_copy(Stack* stack)
22 {
23 Stack* stackCopy = (Stack*) safe_malloc(sizeof (Stack));
24 stackCopy->dataSize = stack->dataSize;
25 Vector* arrayCopy = vector_copy(stack->array);
26 stackCopy->array = arrayCopy;
27 return stackCopy;
28 }
29
30 bool stack_empty(Stack* stack)
31 {
32 return vector_empty(stack->array);
33 }
34
35 UInt stack_size(Stack* stack)
36 {
37 return vector_size(stack->array);
38 }
39
40 void _stack_push(Stack* stack, void* data)
41 {
42 _vector_push(stack->array, data);
43 }
44
45 void* _stack_top(Stack* stack)
46 {
47 return _vector_get(stack->array, vector_size(stack->array)-1);
48 }
49
50 void stack_pop(Stack* stack)
51 {
52 vector_pop(stack->array);
53 }
54
55 void stack_clear(Stack* stack)
56 {
57 vector_clear(stack->array);
58 }
59
60 void stack_destroy(Stack* stack)
61 {
62 vector_destroy(stack->array);
63 safe_free(stack);
64 }