1 #include "../../common.h"
2
3 #include "../parameters.h"
4 #include "../arginfo.h"
5
6 #include "../objects/php_deque.h"
7 #include "../iterators/php_deque_iterator.h"
8 #include "../handlers/php_deque_handlers.h"
9
10 #include "php_collection_ce.h"
11 #include "php_sequence_ce.h"
12 #include "php_deque_ce.h"
13
14 #define METHOD(name) PHP_METHOD(Deque, name)
15
16 zend_class_entry *php_ds_deque_ce;
17
METHOD(__construct)18 METHOD(__construct)
19 {
20 PARSE_OPTIONAL_ZVAL(values);
21
22 if (values) {
23 ds_deque_push_all(THIS_DS_DEQUE(), values);
24 }
25 }
26
METHOD(join)27 METHOD(join)
28 {
29 if (ZEND_NUM_ARGS()) {
30 PARSE_STRING();
31 ds_deque_join(THIS_DS_DEQUE(), str, len, return_value);
32 } else {
33 ds_deque_join(THIS_DS_DEQUE(), NULL, 0, return_value);
34 }
35 }
36
METHOD(allocate)37 METHOD(allocate)
38 {
39 PARSE_LONG(capacity);
40 ds_deque_allocate(THIS_DS_DEQUE(), capacity);
41 }
42
METHOD(apply)43 METHOD(apply)
44 {
45 PARSE_CALLABLE();
46 ds_deque_apply(THIS_DS_DEQUE(), FCI_ARGS);
47 }
48
METHOD(capacity)49 METHOD(capacity)
50 {
51 PARSE_NONE;
52 RETURN_LONG((THIS_DS_DEQUE())->capacity);
53 }
54
METHOD(map)55 METHOD(map)
56 {
57 PARSE_CALLABLE();
58 RETURN_DS_DEQUE(ds_deque_map(THIS_DS_DEQUE(), FCI_ARGS));
59 }
60
METHOD(merge)61 METHOD(merge)
62 {
63 PARSE_ZVAL(values);
64 RETURN_DS_DEQUE(ds_deque_merge(THIS_DS_DEQUE(), values));
65 }
66
METHOD(reduce)67 METHOD(reduce)
68 {
69 PARSE_CALLABLE_AND_OPTIONAL_ZVAL(initial);
70 ds_deque_reduce(THIS_DS_DEQUE(), initial, return_value, FCI_ARGS);
71 }
72
METHOD(filter)73 METHOD(filter)
74 {
75 if (ZEND_NUM_ARGS()) {
76 PARSE_CALLABLE();
77 RETURN_DS_DEQUE(ds_deque_filter_callback(THIS_DS_DEQUE(), FCI_ARGS));
78 } else {
79 PARSE_NONE;
80 RETURN_DS_DEQUE(ds_deque_filter(THIS_DS_DEQUE()));
81 }
82 }
83
METHOD(slice)84 METHOD(slice)
85 {
86 ds_deque_t *deque = THIS_DS_DEQUE();
87
88 PARSE_LONG_AND_OPTIONAL_ZVAL(index, length);
89
90 if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) {
91 if (Z_TYPE_P(length) != IS_LONG) {
92 INTEGER_LENGTH_REQUIRED(length);
93 } else {
94 RETURN_DS_DEQUE(ds_deque_slice(deque, index, Z_LVAL_P(length)));
95 }
96 } else {
97 RETURN_DS_DEQUE(ds_deque_slice(deque, index, deque->size));
98 }
99 }
100
METHOD(sort)101 METHOD(sort)
102 {
103 ds_deque_t *sorted = THIS_DS_DEQUE();
104
105 if (ZEND_NUM_ARGS()) {
106 PARSE_COMPARE_CALLABLE();
107 ds_deque_sort_callback(sorted);
108 } else {
109 ds_deque_sort(sorted);
110 }
111 }
112
METHOD(sorted)113 METHOD(sorted)
114 {
115 ds_deque_t *sorted = ds_deque_clone(THIS_DS_DEQUE());
116
117 if (ZEND_NUM_ARGS()) {
118 PARSE_COMPARE_CALLABLE();
119 ds_deque_sort_callback(sorted);
120 } else {
121 ds_deque_sort(sorted);
122 }
123
124 RETURN_DS_DEQUE(sorted);
125 }
126
METHOD(push)127 METHOD(push)
128 {
129 PARSE_VARIADIC_ZVAL();
130 if (argc == 1) {
131 ds_deque_push(THIS_DS_DEQUE(), argv);
132 } else {
133 ds_deque_push_va(THIS_DS_DEQUE(), argc, argv);
134 }
135 }
136
METHOD(unshift)137 METHOD(unshift)
138 {
139 PARSE_VARIADIC_ZVAL();
140 ds_deque_unshift_va(THIS_DS_DEQUE(), argc, argv);
141 }
142
METHOD(pop)143 METHOD(pop)
144 {
145 PARSE_NONE;
146 ds_deque_pop_throw(THIS_DS_DEQUE(), return_value);
147 }
148
METHOD(shift)149 METHOD(shift)
150 {
151 PARSE_NONE;
152 ds_deque_shift_throw(THIS_DS_DEQUE(), return_value);
153 }
154
METHOD(first)155 METHOD(first)
156 {
157 PARSE_NONE;
158 RETURN_ZVAL_COPY(ds_deque_get_first_throw(THIS_DS_DEQUE()));
159 }
160
METHOD(last)161 METHOD(last)
162 {
163 PARSE_NONE;
164 RETURN_ZVAL_COPY(ds_deque_get_last_throw(THIS_DS_DEQUE()));
165 }
166
METHOD(count)167 METHOD(count)
168 {
169 ds_deque_t *deque = THIS_DS_DEQUE();
170 PARSE_NONE;
171 RETURN_LONG(deque->size);
172 }
173
METHOD(clear)174 METHOD(clear)
175 {
176 PARSE_NONE;
177 ds_deque_clear(THIS_DS_DEQUE());
178 }
179
METHOD(contains)180 METHOD(contains)
181 {
182 PARSE_VARIADIC_ZVAL();
183 RETURN_BOOL(ds_deque_contains_va(THIS_DS_DEQUE(), argc, argv));
184 }
185
METHOD(sum)186 METHOD(sum)
187 {
188 PARSE_NONE;
189 ds_deque_sum(THIS_DS_DEQUE(), return_value);
190 }
191
METHOD(toArray)192 METHOD(toArray)
193 {
194 PARSE_NONE;
195 ds_deque_to_array(THIS_DS_DEQUE(), return_value);
196 }
197
METHOD(get)198 METHOD(get)
199 {
200 PARSE_LONG(index);
201 RETURN_ZVAL_COPY(ds_deque_get(THIS_DS_DEQUE(), index));
202 }
203
METHOD(set)204 METHOD(set)
205 {
206 PARSE_LONG_AND_ZVAL(index, value);
207 ds_deque_set(THIS_DS_DEQUE(), index, value);
208 }
209
METHOD(find)210 METHOD(find)
211 {
212 PARSE_ZVAL(value);
213 ds_deque_find(THIS_DS_DEQUE(), value, return_value);
214 }
215
METHOD(remove)216 METHOD(remove)
217 {
218 PARSE_LONG(index);
219 ds_deque_remove(THIS_DS_DEQUE(), index, return_value);
220 }
221
METHOD(insert)222 METHOD(insert)
223 {
224 PARSE_LONG_AND_VARIADIC_ZVAL(index);
225 ds_deque_insert_va(THIS_DS_DEQUE(), index, argc, argv);
226 }
227
METHOD(reverse)228 METHOD(reverse)
229 {
230 PARSE_NONE;
231 ds_deque_reverse(THIS_DS_DEQUE());
232 }
233
METHOD(reversed)234 METHOD(reversed)
235 {
236 PARSE_NONE;
237 RETURN_DS_DEQUE(ds_deque_reversed(THIS_DS_DEQUE()));
238 }
239
METHOD(rotate)240 METHOD(rotate)
241 {
242 PARSE_LONG(rotations);
243 ds_deque_rotate(THIS_DS_DEQUE(), rotations);
244 }
245
METHOD(isEmpty)246 METHOD(isEmpty)
247 {
248 PARSE_NONE;
249 RETURN_BOOL(DS_DEQUE_IS_EMPTY(THIS_DS_DEQUE()));
250 }
251
METHOD(copy)252 METHOD(copy)
253 {
254 PARSE_NONE;
255 RETURN_OBJ(php_ds_deque_create_clone(THIS_DS_DEQUE()));
256 }
257
METHOD(jsonSerialize)258 METHOD(jsonSerialize)
259 {
260 PARSE_NONE;
261 ds_deque_to_array(THIS_DS_DEQUE(), return_value);
262 }
263
METHOD(getIterator)264 METHOD(getIterator) {
265 PARSE_NONE;
266 ZVAL_COPY(return_value, getThis());
267 }
268
METHOD(offsetExists)269 METHOD(offsetExists)
270 {
271 PARSE_LONG(index);
272 RETURN_BOOL(ds_deque_isset(THIS_DS_DEQUE(), index, false));
273 }
274
METHOD(offsetGet)275 METHOD(offsetGet)
276 {
277 PARSE_LONG(index);
278 RETURN_ZVAL_COPY(ds_deque_get(THIS_DS_DEQUE(), index));
279 }
280
METHOD(offsetSet)281 METHOD(offsetSet)
282 {
283 PARSE_ZVAL_ZVAL(offset, value);
284
285 if (Z_TYPE_P(offset) == IS_NULL) {
286 ds_deque_push(THIS_DS_DEQUE(), value);
287 } else {
288 if (Z_TYPE_P(offset) != IS_LONG) {
289 INTEGER_INDEX_REQUIRED(offset);
290 } else {
291 ds_deque_set(THIS_DS_DEQUE(), Z_LVAL_P(offset), value);
292 }
293 }
294 }
295
METHOD(offsetUnset)296 METHOD(offsetUnset)
297 {
298 PARSE_LONG(index);
299 ds_deque_remove(THIS_DS_DEQUE(), index, return_value);
300 }
301
php_ds_register_deque()302 void php_ds_register_deque()
303 {
304 zend_class_entry ce;
305
306 zend_function_entry methods[] = {
307 PHP_DS_ME(Deque, __construct)
308 PHP_DS_ME(Deque, getIterator)
309
310 PHP_DS_COLLECTION_ME_LIST(Deque)
311 PHP_DS_SEQUENCE_ME_LIST(Deque)
312 PHP_FE_END
313 };
314
315 INIT_CLASS_ENTRY(ce, PHP_DS_NS(Deque), methods);
316
317 php_ds_deque_ce = zend_register_internal_class(&ce);
318 php_ds_deque_ce->ce_flags |= ZEND_ACC_FINAL;
319 php_ds_deque_ce->create_object = php_ds_deque_create_object;
320 php_ds_deque_ce->get_iterator = php_ds_deque_get_iterator;
321 php_ds_deque_ce->serialize = php_ds_deque_serialize;
322 php_ds_deque_ce->unserialize = php_ds_deque_unserialize;
323
324 zend_declare_class_constant_long(php_ds_deque_ce, STR_AND_LEN("MIN_CAPACITY"), DS_DEQUE_MIN_CAPACITY);
325 zend_class_implements(php_ds_deque_ce, 1, sequence_ce);
326
327 php_ds_register_deque_handlers();
328 }
329