xref: /ext-ds/src/ds/ds_queue.c (revision 76d81488)
1 #include "../common.h"
2 
3 #include "../php/iterators/php_queue_iterator.h"
4 #include "../php/handlers/php_queue_handlers.h"
5 #include "../php/classes/php_queue_ce.h"
6 
7 #include "ds_deque.h"
8 #include "ds_queue.h"
9 
ds_queue_ex(ds_deque_t * deque)10 ds_queue_t *ds_queue_ex(ds_deque_t *deque)
11 {
12     ds_queue_t *queue = ecalloc(1, sizeof(ds_queue_t));
13     queue->deque = deque;
14     return queue;
15 }
16 
ds_queue()17 ds_queue_t *ds_queue()
18 {
19     return ds_queue_ex(ds_deque());
20 }
21 
ds_queue_clone(ds_queue_t * queue)22 ds_queue_t *ds_queue_clone(ds_queue_t *queue)
23 {
24     return ds_queue_ex(ds_deque_clone(queue->deque));
25 }
26 
ds_queue_free(ds_queue_t * queue)27 void ds_queue_free(ds_queue_t *queue)
28 {
29     ds_deque_free(queue->deque);
30     efree(queue);
31 }
32 
ds_queue_allocate(ds_queue_t * queue,zend_long capacity)33 void ds_queue_allocate(ds_queue_t *queue, zend_long capacity)
34 {
35     ds_deque_allocate(queue->deque, capacity);
36 }
37 
ds_queue_capacity(ds_queue_t * queue)38 zend_long ds_queue_capacity(ds_queue_t *queue)
39 {
40     return queue->deque->capacity;
41 }
42 
ds_queue_push(ds_queue_t * queue,VA_PARAMS)43 void ds_queue_push(ds_queue_t *queue, VA_PARAMS)
44 {
45     ds_deque_push_va(queue->deque, argc, argv);
46 }
47 
ds_queue_push_one(ds_queue_t * queue,zval * value)48 void ds_queue_push_one(ds_queue_t *queue, zval *value)
49 {
50     ds_deque_push(queue->deque, value);
51 }
52 
ds_queue_clear(ds_queue_t * queue)53 void ds_queue_clear(ds_queue_t *queue)
54 {
55     ds_deque_clear(queue->deque);
56 }
57 
ds_queue_push_all(ds_queue_t * queue,zval * value)58 void ds_queue_push_all(ds_queue_t *queue, zval *value)
59 {
60     ds_deque_push_all(queue->deque, value);
61 }
62 
ds_queue_to_array(ds_queue_t * queue,zval * return_value)63 void ds_queue_to_array(ds_queue_t *queue, zval *return_value)
64 {
65     zend_long size = QUEUE_SIZE(queue);
66 
67     if (size == 0) {
68         array_init(return_value);
69 
70     } else {
71         zval *value;
72         array_init_size(return_value, size);
73 
74         DS_DEQUE_FOREACH(queue->deque, value) {
75             add_next_index_zval(return_value, value);
76             Z_TRY_ADDREF_P(value);
77         }
78         DS_DEQUE_FOREACH_END();
79     }
80 }
81 
ds_queue_pop_throw(ds_queue_t * queue,zval * return_value)82 void ds_queue_pop_throw(ds_queue_t *queue, zval *return_value)
83 {
84     ds_deque_shift_throw(queue->deque, return_value);
85 }
86 
ds_queue_pop(ds_queue_t * queue,zval * return_value)87 void ds_queue_pop(ds_queue_t *queue, zval *return_value)
88 {
89     ds_deque_shift(queue->deque, return_value);
90 }
91 
ds_queue_peek_throw(ds_queue_t * queue)92 zval *ds_queue_peek_throw(ds_queue_t *queue)
93 {
94     return ds_deque_get_first_throw(queue->deque);
95 }
96 
ds_queue_peek(ds_queue_t * queue)97 zval *ds_queue_peek(ds_queue_t *queue)
98 {
99     return ds_deque_get_first(queue->deque);
100 }
101