1 #include "../../common.h"
2
3 #include "../parameters.h"
4 #include "../arginfo.h"
5
6 #include "../objects/php_vector.h"
7 #include "../iterators/php_vector_iterator.h"
8 #include "../handlers/php_vector_handlers.h"
9
10 #include "php_collection_ce.h"
11 #include "php_sequence_ce.h"
12 #include "php_vector_ce.h"
13
14 #define METHOD(name) PHP_METHOD(Vector, name)
15
16 zend_class_entry *php_ds_vector_ce;
17
METHOD(__construct)18 METHOD(__construct)
19 {
20 PARSE_OPTIONAL_ZVAL(values);
21
22 if (values) {
23 ds_vector_push_all(THIS_DS_VECTOR(), values);
24 }
25 }
26
METHOD(allocate)27 METHOD(allocate)
28 {
29 PARSE_LONG(capacity);
30 ds_vector_allocate(THIS_DS_VECTOR(), capacity);
31 }
32
METHOD(apply)33 METHOD(apply)
34 {
35 PARSE_CALLABLE();
36 ds_vector_apply(THIS_DS_VECTOR(), FCI_ARGS);
37 }
38
METHOD(capacity)39 METHOD(capacity)
40 {
41 PARSE_NONE;
42 RETURN_LONG((THIS_DS_VECTOR())->capacity);
43 }
44
METHOD(clear)45 METHOD(clear)
46 {
47 PARSE_NONE;
48 ds_vector_clear(THIS_DS_VECTOR());
49 }
50
METHOD(contains)51 METHOD(contains)
52 {
53 PARSE_VARIADIC_ZVAL();
54 RETURN_BOOL(ds_vector_contains_va(THIS_DS_VECTOR(), argc, argv));
55 }
56
METHOD(copy)57 METHOD(copy)
58 {
59 PARSE_NONE;
60 RETURN_OBJ(php_ds_vector_create_clone(THIS_DS_VECTOR()));
61 }
62
METHOD(count)63 METHOD(count)
64 {
65 PARSE_NONE;
66 RETURN_LONG(DS_VECTOR_SIZE(THIS_DS_VECTOR()));
67 }
68
METHOD(filter)69 METHOD(filter)
70 {
71 if (ZEND_NUM_ARGS()) {
72 PARSE_CALLABLE();
73 RETURN_DS_VECTOR(ds_vector_filter_callback(THIS_DS_VECTOR(), FCI_ARGS));
74 } else {
75 RETURN_DS_VECTOR(ds_vector_filter(THIS_DS_VECTOR()));
76 }
77 }
78
METHOD(find)79 METHOD(find)
80 {
81 PARSE_ZVAL(value);
82 ds_vector_find(THIS_DS_VECTOR(), value, return_value);
83 }
84
METHOD(first)85 METHOD(first)
86 {
87 PARSE_NONE;
88 RETURN_ZVAL_COPY(ds_vector_get_first_throw(THIS_DS_VECTOR()));
89 }
90
METHOD(get)91 METHOD(get)
92 {
93 PARSE_LONG(index);
94 RETURN_ZVAL_COPY(ds_vector_get(THIS_DS_VECTOR(), index));
95 }
96
METHOD(insert)97 METHOD(insert)
98 {
99 PARSE_LONG_AND_VARIADIC_ZVAL(index);
100 ds_vector_insert_va(THIS_DS_VECTOR(), index, argc, argv);
101 }
102
METHOD(isEmpty)103 METHOD(isEmpty)
104 {
105 PARSE_NONE;
106 RETURN_BOOL(DS_VECTOR_IS_EMPTY(THIS_DS_VECTOR()));
107 }
108
METHOD(join)109 METHOD(join)
110 {
111 if (ZEND_NUM_ARGS()) {
112 PARSE_STRING();
113 ds_vector_join(THIS_DS_VECTOR(), str, len, return_value);
114 } else {
115 ds_vector_join(THIS_DS_VECTOR(), NULL, 0, return_value);
116 }
117 }
118
METHOD(jsonSerialize)119 METHOD(jsonSerialize)
120 {
121 PARSE_NONE;
122 ds_vector_to_array(THIS_DS_VECTOR(), return_value);
123 }
124
METHOD(last)125 METHOD(last)
126 {
127 PARSE_NONE;
128 RETURN_ZVAL_COPY(ds_vector_get_last_throw(THIS_DS_VECTOR()));
129 }
130
METHOD(map)131 METHOD(map)
132 {
133 PARSE_CALLABLE();
134 RETURN_DS_VECTOR(ds_vector_map(THIS_DS_VECTOR(), FCI_ARGS));
135 }
136
METHOD(merge)137 METHOD(merge)
138 {
139 PARSE_ZVAL(values);
140 RETURN_DS_VECTOR(ds_vector_merge(THIS_DS_VECTOR(), values));
141 }
142
METHOD(pop)143 METHOD(pop)
144 {
145 PARSE_NONE;
146 ds_vector_pop_throw(THIS_DS_VECTOR(), return_value);
147 }
148
METHOD(push)149 METHOD(push)
150 {
151 PARSE_VARIADIC_ZVAL();
152 ds_vector_push_va(THIS_DS_VECTOR(), argc, argv);
153 }
154
METHOD(push_one)155 METHOD(push_one)
156 {
157 PARSE_ZVAL(value);
158 ds_vector_push(THIS_DS_VECTOR(), value);
159 }
160
METHOD(reduce)161 METHOD(reduce)
162 {
163 PARSE_CALLABLE_AND_OPTIONAL_ZVAL(initial);
164 ds_vector_reduce(THIS_DS_VECTOR(), initial, return_value, FCI_ARGS);
165 }
166
METHOD(remove)167 METHOD(remove)
168 {
169 PARSE_LONG(index);
170 ds_vector_remove(THIS_DS_VECTOR(), index, return_value);
171 }
172
METHOD(reverse)173 METHOD(reverse)
174 {
175 PARSE_NONE;
176 ds_vector_reverse(THIS_DS_VECTOR());
177 }
178
METHOD(reversed)179 METHOD(reversed)
180 {
181 PARSE_NONE;
182 RETURN_DS_VECTOR(ds_vector_reversed(THIS_DS_VECTOR()));
183 }
184
METHOD(rotate)185 METHOD(rotate)
186 {
187 PARSE_LONG(rotations);
188 ds_vector_rotate(THIS_DS_VECTOR(), rotations);
189 }
190
METHOD(set)191 METHOD(set)
192 {
193 PARSE_LONG_AND_ZVAL(index, value);
194 ds_vector_set(THIS_DS_VECTOR(), index, value);
195 }
196
METHOD(shift)197 METHOD(shift)
198 {
199 PARSE_NONE;
200 ds_vector_shift_throw(THIS_DS_VECTOR(), return_value);
201 }
202
METHOD(slice)203 METHOD(slice)
204 {
205 ds_vector_t *vector = THIS_DS_VECTOR();
206
207 PARSE_LONG_AND_OPTIONAL_ZVAL(index, length);
208
209 if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) {
210 if (Z_TYPE_P(length) != IS_LONG) {
211 INTEGER_LENGTH_REQUIRED(length);
212 } else {
213 RETURN_DS_VECTOR(ds_vector_slice(vector, index, Z_LVAL_P(length)));
214 }
215 } else {
216 RETURN_DS_VECTOR(ds_vector_slice(vector, index, vector->size));
217 }
218 }
219
METHOD(sort)220 METHOD(sort)
221 {
222 ds_vector_t *vector = THIS_DS_VECTOR();
223
224 if (ZEND_NUM_ARGS()) {
225 PARSE_COMPARE_CALLABLE();
226 ds_vector_sort_callback(vector);
227 } else {
228 ds_vector_sort(vector);
229 }
230 }
231
METHOD(sorted)232 METHOD(sorted)
233 {
234 ds_vector_t *vector = ds_vector_clone(THIS_DS_VECTOR());
235
236 if (ZEND_NUM_ARGS()) {
237 PARSE_COMPARE_CALLABLE();
238 ds_vector_sort_callback(vector);
239 } else {
240 ds_vector_sort(vector);
241 }
242
243 RETURN_DS_VECTOR(vector);
244 }
245
METHOD(sum)246 METHOD(sum)
247 {
248 PARSE_NONE;
249 ds_vector_sum(THIS_DS_VECTOR(), return_value);
250 }
251
METHOD(toArray)252 METHOD(toArray)
253 {
254 PARSE_NONE;
255 ds_vector_to_array(THIS_DS_VECTOR(), return_value);
256 }
257
METHOD(unshift)258 METHOD(unshift)
259 {
260 PARSE_VARIADIC_ZVAL();
261 ds_vector_unshift_va(THIS_DS_VECTOR(), argc, argv);
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_vector_isset(THIS_DS_VECTOR(), index, false));
273 }
274
METHOD(offsetGet)275 METHOD(offsetGet)
276 {
277 PARSE_LONG(index);
278 RETURN_ZVAL_COPY(ds_vector_get(THIS_DS_VECTOR(), 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_vector_push(THIS_DS_VECTOR(), value);
287 } else {
288 if (Z_TYPE_P(offset) != IS_LONG) {
289 INTEGER_INDEX_REQUIRED(offset);
290 } else {
291 ds_vector_set(THIS_DS_VECTOR(), Z_LVAL_P(offset), value);
292 }
293 }
294 }
295
METHOD(offsetUnset)296 METHOD(offsetUnset)
297 {
298 PARSE_LONG(index);
299 ds_vector_remove(THIS_DS_VECTOR(), index, return_value);
300 }
301
php_ds_register_vector()302 void php_ds_register_vector()
303 {
304 zend_class_entry ce;
305
306 zend_function_entry methods[] = {
307 PHP_DS_ME(Vector, __construct)
308 PHP_DS_ME(Vector, getIterator)
309
310 PHP_DS_SEQUENCE_ME_LIST(Vector)
311 PHP_DS_COLLECTION_ME_LIST(Vector)
312 PHP_FE_END
313 };
314
315 INIT_CLASS_ENTRY(ce, PHP_DS_NS(Vector), methods);
316
317 php_ds_vector_ce = zend_register_internal_class(&ce);
318 php_ds_vector_ce->ce_flags |= ZEND_ACC_FINAL;
319 php_ds_vector_ce->create_object = php_ds_vector_create_object;
320 php_ds_vector_ce->get_iterator = php_ds_vector_get_iterator;
321 php_ds_vector_ce->serialize = php_ds_vector_serialize;
322 php_ds_vector_ce->unserialize = php_ds_vector_unserialize;
323
324 zend_declare_class_constant_long(php_ds_vector_ce, STR_AND_LEN("MIN_CAPACITY"), DS_VECTOR_MIN_CAPACITY);
325
326 zend_class_implements(php_ds_vector_ce, 1, sequence_ce);
327 php_register_vector_handlers();
328 }
329