1 #include "../common.h"
2
3 #include "../php/iterators/php_stack_iterator.h"
4 #include "../php/handlers/php_stack_handlers.h"
5 #include "../php/classes/php_stack_ce.h"
6
7 #include "ds_stack.h"
8
ds_stack_ex(ds_vector_t * vector)9 ds_stack_t *ds_stack_ex(ds_vector_t *vector)
10 {
11 ds_stack_t *stack = ecalloc(1, sizeof(ds_stack_t));
12 stack->vector = vector;
13 return stack;
14 }
15
ds_stack()16 ds_stack_t *ds_stack()
17 {
18 return ds_stack_ex(ds_vector());
19 }
20
ds_stack_clone(ds_stack_t * stack)21 ds_stack_t *ds_stack_clone(ds_stack_t *stack)
22 {
23 return ds_stack_ex(ds_vector_clone(stack->vector));
24 }
25
ds_stack_free(ds_stack_t * stack)26 void ds_stack_free(ds_stack_t *stack)
27 {
28 ds_vector_free(stack->vector);
29 efree(stack);
30 }
31
ds_stack_allocate(ds_stack_t * stack,zend_long capacity)32 void ds_stack_allocate(ds_stack_t *stack, zend_long capacity)
33 {
34 ds_vector_allocate(stack->vector, capacity);
35 }
36
ds_stack_push_va(ds_stack_t * stack,VA_PARAMS)37 void ds_stack_push_va(ds_stack_t *stack, VA_PARAMS)
38 {
39 ds_vector_push_va(stack->vector, argc, argv);
40 }
41
ds_stack_push(ds_stack_t * stack,zval * value)42 void ds_stack_push(ds_stack_t *stack, zval *value)
43 {
44 ds_vector_push(stack->vector, value);
45 }
46
ds_stack_clear(ds_stack_t * stack)47 void ds_stack_clear(ds_stack_t *stack)
48 {
49 ds_vector_clear(stack->vector);
50 }
51
ds_stack_push_all(ds_stack_t * stack,zval * value)52 void ds_stack_push_all(ds_stack_t *stack, zval *value)
53 {
54 ds_vector_push_all(stack->vector, value);
55 }
56
ds_stack_to_array(ds_stack_t * stack,zval * return_value)57 void ds_stack_to_array(ds_stack_t *stack, zval *return_value)
58 {
59 zend_long size = DS_STACK_SIZE(stack);
60
61 if (size == 0) {
62 array_init(return_value);
63
64 } else {
65 zval *value;
66 array_init_size(return_value, size);
67
68 DS_VECTOR_FOREACH_REVERSED(stack->vector, value) {
69 add_next_index_zval(return_value, value);
70 Z_TRY_ADDREF_P(value);
71 }
72 DS_VECTOR_FOREACH_END();
73 }
74 }
75
ds_stack_pop_throw(ds_stack_t * stack,zval * return_value)76 void ds_stack_pop_throw(ds_stack_t *stack, zval *return_value)
77 {
78 ds_vector_pop_throw(stack->vector, return_value);
79 }
80
ds_stack_pop(ds_stack_t * stack,zval * return_value)81 void ds_stack_pop(ds_stack_t *stack, zval *return_value)
82 {
83 ds_vector_pop(stack->vector, return_value);
84 }
85
ds_stack_peek(ds_stack_t * stack)86 zval *ds_stack_peek(ds_stack_t *stack)
87 {
88 return ds_vector_get_last(stack->vector);
89 }
90
ds_stack_peek_throw(ds_stack_t * stack)91 zval *ds_stack_peek_throw(ds_stack_t *stack)
92 {
93 return ds_vector_get_last_throw(stack->vector);
94 }
95