1 #include "../../common.h"
2
3 #include "../parameters.h"
4 #include "../arginfo.h"
5
6 #include "../objects/php_set.h"
7
8 #include "../iterators/php_set_iterator.h"
9 #include "../handlers/php_set_handlers.h"
10
11 #include "php_collection_ce.h"
12 #include "php_set_ce.h"
13
14 #define METHOD(name) PHP_METHOD(Set, name)
15
16 zend_class_entry *php_ds_set_ce;
17
METHOD(__construct)18 METHOD(__construct)
19 {
20 PARSE_OPTIONAL_ZVAL(values);
21
22 if (values) {
23 ds_set_add_all(THIS_DS_SET(), values);
24 }
25 }
26
METHOD(join)27 METHOD(join)
28 {
29 if (ZEND_NUM_ARGS()) {
30 PARSE_STRING();
31 ds_set_join(THIS_DS_SET(), str, len, return_value);
32 } else {
33 ds_set_join(THIS_DS_SET(), NULL, 0, return_value);
34 }
35 }
36
METHOD(allocate)37 METHOD(allocate)
38 {
39 PARSE_LONG(capacity);
40 ds_set_allocate(THIS_DS_SET(), capacity);
41 }
42
METHOD(capacity)43 METHOD(capacity)
44 {
45 PARSE_NONE;
46 RETURN_LONG(DS_SET_CAPACITY(THIS_DS_SET()));
47 }
48
METHOD(add)49 METHOD(add)
50 {
51 PARSE_VARIADIC_ZVAL();
52 ds_set_add_va(THIS_DS_SET(), argc, argv);
53 }
54
METHOD(remove)55 METHOD(remove)
56 {
57 PARSE_VARIADIC_ZVAL();
58 ds_set_remove_va(THIS_DS_SET(), argc, argv);
59 }
60
METHOD(get)61 METHOD(get)
62 {
63 PARSE_LONG(index);
64 RETURN_ZVAL_COPY(ds_set_get(THIS_DS_SET(), index));
65 }
66
METHOD(contains)67 METHOD(contains)
68 {
69 PARSE_VARIADIC_ZVAL();
70 RETURN_BOOL(ds_set_contains_va(THIS_DS_SET(), argc, argv));
71 }
72
METHOD(diff)73 METHOD(diff)
74 {
75 PARSE_OBJ(obj, php_ds_set_ce);
76 RETURN_DS_SET(ds_set_diff(THIS_DS_SET(), Z_DS_SET_P(obj)));
77 }
78
METHOD(intersect)79 METHOD(intersect)
80 {
81 PARSE_OBJ(obj, php_ds_set_ce);
82 RETURN_DS_SET(ds_set_intersect(THIS_DS_SET(), Z_DS_SET_P(obj)));
83 }
84
METHOD(xor)85 METHOD(xor)
86 {
87 PARSE_OBJ(obj, php_ds_set_ce);
88 RETURN_DS_SET(ds_set_xor(THIS_DS_SET(), Z_DS_SET_P(obj)));
89 }
90
METHOD(first)91 METHOD(first)
92 {
93 PARSE_NONE;
94 RETURN_ZVAL_COPY(ds_set_get_first(THIS_DS_SET()));
95 }
96
METHOD(last)97 METHOD(last)
98 {
99 PARSE_NONE;
100 RETURN_ZVAL_COPY(ds_set_get_last(THIS_DS_SET()));
101 }
102
METHOD(map)103 METHOD(map)
104 {
105 PARSE_CALLABLE();
106 RETURN_DS_SET(ds_set_map(THIS_DS_SET(), FCI_ARGS));
107 }
108
METHOD(merge)109 METHOD(merge)
110 {
111 PARSE_ZVAL(values);
112 RETURN_DS_SET(ds_set_merge(THIS_DS_SET(), values));
113 }
114
METHOD(union)115 METHOD(union)
116 {
117 PARSE_OBJ(obj, php_ds_set_ce);
118 RETURN_DS_SET(ds_set_union(THIS_DS_SET(), Z_DS_SET_P(obj)));
119 }
120
METHOD(clear)121 METHOD(clear)
122 {
123 PARSE_NONE;
124 ds_set_clear(THIS_DS_SET());
125 }
126
METHOD(toArray)127 METHOD(toArray)
128 {
129 PARSE_NONE;
130 ds_set_to_array(THIS_DS_SET(), return_value);
131 }
132
METHOD(count)133 METHOD(count)
134 {
135 PARSE_NONE;
136 RETURN_LONG(DS_SET_SIZE(THIS_DS_SET()));
137 }
138
METHOD(isEmpty)139 METHOD(isEmpty)
140 {
141 PARSE_NONE;
142 RETURN_BOOL(DS_SET_IS_EMPTY(THIS_DS_SET()));
143 }
144
METHOD(sort)145 METHOD(sort)
146 {
147 if (ZEND_NUM_ARGS()) {
148 PARSE_COMPARE_CALLABLE();
149 ds_set_sort_callback(THIS_DS_SET());
150 } else {
151 ds_set_sort(THIS_DS_SET());
152 }
153 }
154
METHOD(sorted)155 METHOD(sorted)
156 {
157 if (ZEND_NUM_ARGS()) {
158 PARSE_COMPARE_CALLABLE();
159 RETURN_DS_SET(ds_set_sorted_callback(THIS_DS_SET()));
160 } else {
161 RETURN_DS_SET(ds_set_sorted(THIS_DS_SET()));
162 }
163 }
164
METHOD(copy)165 METHOD(copy)
166 {
167 PARSE_NONE;
168 RETURN_OBJ(php_ds_set_create_clone(THIS_DS_SET()));
169 }
170
METHOD(reduce)171 METHOD(reduce)
172 {
173 PARSE_CALLABLE_AND_OPTIONAL_ZVAL(initial);
174 ds_set_reduce(THIS_DS_SET(), FCI_ARGS, initial, return_value);
175 }
176
METHOD(slice)177 METHOD(slice)
178 {
179 ds_set_t *set = THIS_DS_SET();
180
181 PARSE_LONG_AND_OPTIONAL_ZVAL(index, length);
182
183 if (ZEND_NUM_ARGS() > 1 && Z_TYPE_P(length) != IS_NULL) {
184 if (Z_TYPE_P(length) != IS_LONG) {
185 INTEGER_LENGTH_REQUIRED(length);
186 } else {
187 RETURN_DS_SET(ds_set_slice(set, index, Z_LVAL_P(length)));
188 }
189 } else {
190 RETURN_DS_SET(ds_set_slice(set, index, DS_SET_SIZE(set)));
191 }
192 }
193
METHOD(filter)194 METHOD(filter)
195 {
196 if (ZEND_NUM_ARGS()) {
197 PARSE_CALLABLE();
198 RETURN_DS_SET(ds_set_filter_callback(THIS_DS_SET(), FCI_ARGS));
199 } else {
200 RETURN_DS_SET(ds_set_filter(THIS_DS_SET()));
201 }
202 }
203
METHOD(reverse)204 METHOD(reverse)
205 {
206 PARSE_NONE;
207 ds_set_reverse(THIS_DS_SET());
208 }
209
METHOD(reversed)210 METHOD(reversed)
211 {
212 PARSE_NONE;
213 RETURN_DS_SET(ds_set_reversed(THIS_DS_SET()));
214 }
215
METHOD(sum)216 METHOD(sum)
217 {
218 PARSE_NONE;
219 ds_set_sum(THIS_DS_SET(), return_value);
220 }
221
METHOD(jsonSerialize)222 METHOD(jsonSerialize)
223 {
224 PARSE_NONE;
225 ds_set_to_array(THIS_DS_SET(), return_value);
226 }
227
METHOD(getIterator)228 METHOD(getIterator) {
229 PARSE_NONE;
230 ZVAL_COPY(return_value, getThis());
231 }
232
233
METHOD(offsetExists)234 METHOD(offsetExists)
235 {
236 ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
237 }
238
METHOD(offsetGet)239 METHOD(offsetGet)
240 {
241 PARSE_LONG(index);
242 RETURN_ZVAL_COPY(ds_set_get(THIS_DS_SET(), index));
243 }
244
METHOD(offsetSet)245 METHOD(offsetSet)
246 {
247 PARSE_ZVAL_ZVAL(offset, value);
248
249 if (Z_TYPE_P(offset) == IS_NULL) {
250 ds_set_add_va(THIS_DS_SET(), 1, value);
251 } else {
252 ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
253 }
254 }
255
METHOD(offsetUnset)256 METHOD(offsetUnset)
257 {
258 ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
259 }
260
php_ds_register_set()261 void php_ds_register_set()
262 {
263 zend_class_entry ce;
264
265 zend_function_entry methods[] = {
266 PHP_DS_ME(Set, __construct)
267 PHP_DS_ME(Set, add)
268 PHP_DS_ME(Set, allocate)
269 PHP_DS_ME(Set, capacity)
270 PHP_DS_ME(Set, contains)
271 PHP_DS_ME(Set, diff)
272 PHP_DS_ME(Set, filter)
273 PHP_DS_ME(Set, first)
274 PHP_DS_ME(Set, get)
275 PHP_DS_ME(Set, intersect)
276 PHP_DS_ME(Set, join)
277 PHP_DS_ME(Set, last)
278 PHP_DS_ME(Set, map)
279 PHP_DS_ME(Set, merge)
280 PHP_DS_ME(Set, reduce)
281 PHP_DS_ME(Set, remove)
282 PHP_DS_ME(Set, reverse)
283 PHP_DS_ME(Set, reversed)
284 PHP_DS_ME(Set, slice)
285 PHP_DS_ME(Set, sort)
286 PHP_DS_ME(Set, sorted)
287 PHP_DS_ME(Set, sum)
288 PHP_DS_ME(Set, union)
289 PHP_DS_ME(Set, xor)
290 PHP_DS_ME(Set, getIterator)
291
292 PHP_DS_ME(Set, offsetExists)
293 PHP_DS_ME(Set, offsetGet)
294 PHP_DS_ME(Set, offsetSet)
295 PHP_DS_ME(Set, offsetUnset)
296
297 PHP_DS_COLLECTION_ME_LIST(Set)
298 PHP_FE_END
299 };
300
301 INIT_CLASS_ENTRY(ce, PHP_DS_NS(Set), methods);
302
303 php_ds_set_ce = zend_register_internal_class(&ce);
304 php_ds_set_ce->ce_flags |= ZEND_ACC_FINAL;
305 php_ds_set_ce->create_object = php_ds_set_create_object;
306 php_ds_set_ce->get_iterator = php_ds_set_get_iterator;
307 php_ds_set_ce->serialize = php_ds_set_serialize;
308 php_ds_set_ce->unserialize = php_ds_set_unserialize;
309
310 zend_declare_class_constant_long(
311 php_ds_set_ce,
312 STR_AND_LEN("MIN_CAPACITY"),
313 DS_HTABLE_MIN_CAPACITY
314 );
315
316 zend_class_implements(php_ds_set_ce, 2,
317 collection_ce,
318 zend_ce_arrayaccess
319 );
320
321 php_ds_register_set_handlers();
322 }
323