xref: /ext-ds/src/ds/ds_deque.h (revision f4fee1ad)
1 #ifndef DS_DEQUE_H
2 #define DS_DEQUE_H
3 
4 #include "../common.h"
5 
6 #define DS_DEQUE_MIN_CAPACITY 8 // Must be a power of 2
7 
8 #define DS_DEQUE_SIZE(d)      ((d)->size)
9 #define DS_DEQUE_IS_EMPTY(d)  ((d)->size == 0)
10 
11 #define DS_DEQUE_FOREACH(d, v)                              \
12 do {                                                        \
13     const ds_deque_t *_deque = d;                           \
14     const zend_long _mask    = _deque->capacity - 1;        \
15     const zend_long _size    = _deque->size;                \
16     const zend_long _head    = _deque->head;                \
17                                                             \
18     zend_long _i;                                           \
19     for (_i = 0; _i < _size; _i++) {                        \
20         v = &_deque->buffer[(_head + _i) & _mask];
21 
22 #define DS_DEQUE_FOREACH_END() \
23     } \
24 } while (0)
25 
26 typedef struct _ds_deque_t {
27     zval      *buffer;
28     zend_long  capacity;
29     zend_long  head;
30     zend_long  tail;
31     zend_long  size;
32 } ds_deque_t;
33 
34 ds_deque_t *ds_deque();
35 ds_deque_t *ds_deque_ex(zend_long capacity);
36 ds_deque_t *ds_deque_clone(ds_deque_t *src);
37 
38 void ds_deque_clear(ds_deque_t *deque);
39 void ds_deque_free(ds_deque_t *deque);
40 void ds_deque_allocate(ds_deque_t *deque, zend_long capacity);
41 void ds_deque_reset_head(ds_deque_t *deque);
42 
43 void ds_deque_push(ds_deque_t *deque, zval *value);
44 void ds_deque_push_va(ds_deque_t *deque, VA_PARAMS);
45 void ds_deque_push_all(ds_deque_t *deque, zval *values);
46 
47 void ds_deque_set(ds_deque_t *deque, zend_long index, zval *value);
48 void ds_deque_pop(ds_deque_t *deque, zval *return_value);
49 void ds_deque_pop_throw(ds_deque_t *deque, zval *return_value);
50 void ds_deque_shift(ds_deque_t *deque, zval *return_value);
51 void ds_deque_shift_throw(ds_deque_t *deque, zval *return_value);
52 void ds_deque_find(ds_deque_t *deque, zval *value, zval *return_value);
53 void ds_deque_remove(ds_deque_t *deque, zend_long index, zval *return_value);
54 void ds_deque_insert_va(ds_deque_t *deque, zend_long index, VA_PARAMS);
55 void ds_deque_unshift_va(ds_deque_t *deque, VA_PARAMS);
56 
57 zval *ds_deque_get(ds_deque_t *deque, zend_long index);
58 zval *ds_deque_get_last(ds_deque_t *deque);
59 zval *ds_deque_get_last_throw(ds_deque_t *deque);
60 zval *ds_deque_get_first(ds_deque_t *deque);
61 zval *ds_deque_get_first_throw(ds_deque_t *deque);
62 
63 bool ds_deque_contains_va(ds_deque_t *deque, VA_PARAMS);
64 bool ds_deque_isset(ds_deque_t *deque, zend_long index, int check_empty);
65 bool ds_deque_index_exists(ds_deque_t *deque, zend_long index);
66 
67 ds_deque_t *ds_deque_map(ds_deque_t *deque, FCI_PARAMS);
68 ds_deque_t *ds_deque_filter(ds_deque_t *deque);
69 ds_deque_t *ds_deque_filter_callback(ds_deque_t *deque, FCI_PARAMS);
70 ds_deque_t *ds_deque_slice(ds_deque_t *deque, zend_long index, zend_long length);
71 ds_deque_t *ds_deque_merge(ds_deque_t *deque, zval *values);
72 ds_deque_t *ds_deque_reversed(ds_deque_t *deque);
73 
74 void ds_deque_join(ds_deque_t *deque, char *str, size_t len, zval *return_value);
75 void ds_deque_reduce(ds_deque_t *deque, zval *initial, zval *return_value, FCI_PARAMS);
76 void ds_deque_rotate(ds_deque_t *deque, zend_long rotations);
77 void ds_deque_sort_callback(ds_deque_t *deque);
78 void ds_deque_sort(ds_deque_t *deque);
79 void ds_deque_reverse(ds_deque_t *deque);
80 void ds_deque_to_array(ds_deque_t *deque, zval *return_value);
81 void ds_deque_apply(ds_deque_t *deque, FCI_PARAMS);
82 void ds_deque_sum(ds_deque_t *deque, zval *return_value);
83 
84 #endif
85