-
Notifications
You must be signed in to change notification settings - Fork 11
/
php_judy.c
1382 lines (1172 loc) · 33.8 KB
/
php_judy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Judy |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Nicolas Brousse <nicolas@brousse.info> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_judy.h"
#include "judy_handlers.h"
#include "judy_arrayaccess.h"
#include "judy_iterator.h"
#include "ext/spl/spl_iterators.h"
ZEND_DECLARE_MODULE_GLOBALS(judy)
/* declare judy class entry */
zend_class_entry *judy_ce;
zend_object_handlers judy_handlers;
/* {{{ php_judy_init_globals
*/
static void php_judy_init_globals(zend_judy_globals *judy_globals)
{
judy_globals->max_length = 65536;
}
/* }}} */
/* {{{ judy_free_storage
close all resources and the memory allocated for the object */
static void judy_object_free_storage(void *object TSRMLS_DC)
{
judy_object *intern = (judy_object *) object;
zend_object_std_dtor(&intern->std TSRMLS_CC);
efree(object);
}
/* }}} */
/* {{{ judy_object_new_ex
*/
zend_object_value judy_object_new_ex(zend_class_entry *ce, judy_object **ptr TSRMLS_DC)
{
zend_object_value retval;
judy_object *intern;
#if PHP_VERSION_ID < 50399
zval *tmp;
#endif
intern = ecalloc(1, sizeof(judy_object));
memset(intern, 0, sizeof(judy_object));
if (ptr) {
*ptr = intern;
}
intern->next_empty_is_valid = 1;
intern->next_empty = 0;
zend_object_std_init(&(intern->std), ce TSRMLS_CC);
#if PHP_VERSION_ID < 50399
zend_hash_copy(intern->std.properties,
&ce->default_properties, (copy_ctor_func_t) zval_add_ref,
(void *) &tmp, sizeof(zval *));
#else
object_properties_init(&intern->std, ce);
#endif
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, (zend_objects_free_object_storage_t) judy_object_free_storage, NULL TSRMLS_CC);
retval.handlers = &judy_handlers;
return retval;
}
/* }}} */
/* {{{ judy_object_new
*/
zend_object_value judy_object_new(zend_class_entry *ce TSRMLS_DC)
{
return judy_object_new_ex(ce, NULL TSRMLS_CC);
}
/* }}} */
PHP_JUDY_API zend_class_entry *php_judy_ce(void)
{
return judy_ce;
}
/* {{{ PHP INI entries
*/
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("judy.string.maxlength", "65536", PHP_INI_ALL, OnUpdateLong, max_length, zend_judy_globals, judy_globals)
PHP_INI_END()
/* }}} */
#define CHECK_ARRAY_AND_ARG_TYPE(_index_, _string_key_, _return_) \
switch (intern->type) { \
case TYPE_BITSET: \
case TYPE_INT_TO_INT: \
case TYPE_INT_TO_MIXED: \
if (Z_TYPE_P(offset) != IS_LONG) { \
zval tmp = *offset; \
zval_copy_ctor(&tmp); \
INIT_PZVAL(&tmp); \
convert_to_long(&tmp); \
_index_ = Z_LVAL(tmp); \
} else { \
_index_ = Z_LVAL_P(offset); \
} \
/* avoid zval_dtor() */ \
_string_key_ = offset; \
break; \
case TYPE_STRING_TO_INT: \
case TYPE_STRING_TO_MIXED: \
if (Z_TYPE_P(offset) != IS_STRING) { \
*_string_key_ = *offset; \
zval_copy_ctor(_string_key_); \
INIT_PZVAL(_string_key_); \
convert_to_string(_string_key_); \
} else { \
_string_key_ = offset; \
} \
break; \
default: \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid Judy Array type, please report"); \
_return_; \
}
zval *judy_object_read_dimension_helper(zval *object, zval *offset TSRMLS_DC) /* {{{ */
{
long index = 0;
Word_t j_index;
Pvoid_t *PValue = NULL;
zval *result = NULL;
zval string_key, *pstring_key = &string_key;
judy_object *intern = (judy_object *) zend_object_store_get_object(object TSRMLS_CC);
if (intern->array == NULL) {
return NULL;
}
CHECK_ARRAY_AND_ARG_TYPE(index, pstring_key, return NULL);
j_index = index;
if (intern->type == TYPE_BITSET) {
int Rc_int;
J1T(Rc_int, intern->array, j_index);
MAKE_STD_ZVAL(result);
Z_SET_REFCOUNT_P(result, 0);
Z_UNSET_ISREF_P(result);
ZVAL_BOOL(result, Rc_int);
return result;
}
switch(intern->type) {
case TYPE_INT_TO_INT:
case TYPE_INT_TO_MIXED:
JLG(PValue, intern->array, j_index);
break;
case TYPE_STRING_TO_INT:
case TYPE_STRING_TO_MIXED:
JSLG(PValue, intern->array, (uint8_t *)Z_STRVAL_P(pstring_key));
break;
}
if (PValue != NULL && PValue != PJERR) {
if (intern->type == TYPE_INT_TO_INT || intern->type == TYPE_STRING_TO_INT) {
MAKE_STD_ZVAL(result);
Z_SET_REFCOUNT_P(result, 0);
Z_UNSET_ISREF_P(result);
ZVAL_LONG(result, (long)*PValue);
} else if (intern->type == TYPE_INT_TO_MIXED || intern->type == TYPE_STRING_TO_MIXED) {
result = (zval *)*PValue;
}
if (pstring_key != offset) {
zval_dtor(pstring_key);
}
return result;
}
if (pstring_key != offset) {
zval_dtor(pstring_key);
}
return NULL;
}
/* }}} */
static zval *judy_object_read_dimension(zval *object, zval *offset, int type TSRMLS_DC) /* {{{ */
{
return judy_object_read_dimension_helper(object, offset TSRMLS_CC);
}
/* }}} */
int judy_object_write_dimension_helper(zval *object, zval *offset, zval *value TSRMLS_DC) /* {{{ */
{
long index;
zval string_key, *pstring_key = &string_key;
judy_object *intern = (judy_object *) zend_object_store_get_object(object TSRMLS_CC);
if (offset) {
CHECK_ARRAY_AND_ARG_TYPE(index, pstring_key, return FAILURE);
} else {
if (intern->type == TYPE_STRING_TO_INT || intern->type == TYPE_STRING_TO_MIXED) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Judy STRING_TO_INT and STRING_TO_MIXED values cannot be set without key specifying");
return FAILURE;
}
}
if (intern->type == TYPE_BITSET) {
int Rc_int;
if (!offset || index <= -1) {
if (intern->array) {
if (!offset && intern->next_empty_is_valid) {
index = intern->next_empty++;
} else {
index = -1;
J1L(Rc_int, intern->array, index);
if (Rc_int == 1) {
index += 1;
if (!offset) {
intern->next_empty = index + 1;
intern->next_empty_is_valid = 1;
}
} else {
return FAILURE;
}
}
} else {
if (intern->next_empty_is_valid) {
index = intern->next_empty++;
} else {
index = 0;
}
}
} else {
intern->next_empty_is_valid = 0;
}
if (zend_is_true(value)) {
J1S(Rc_int, intern->array, index);
} else {
J1U(Rc_int, intern->array, index);
}
return Rc_int ? SUCCESS : FAILURE;
} else if (intern->type == TYPE_INT_TO_INT) {
Pvoid_t *PValue;
long value_long;
if (Z_TYPE_P(value) != IS_LONG) {
zval tmp = *value;
zval_copy_ctor(&tmp);
INIT_PZVAL(&tmp);
convert_to_long(&tmp);
value_long = Z_LVAL(tmp);
} else {
value_long = Z_LVAL_P(value);
}
if (!offset || index <= -1) {
if (intern->array) {
if (!offset && intern->next_empty_is_valid) {
index = intern->next_empty++;
} else {
index = -1;
JLL(PValue, intern->array, index);
if (PValue != NULL && PValue != PJERR) {
index += 1;
if (!offset) {
intern->next_empty = index + 1;
intern->next_empty_is_valid = 1;
}
} else {
return FAILURE;
}
}
} else {
if (intern->next_empty_is_valid) {
index = intern->next_empty++;
} else {
index = 0;
}
}
} else {
intern->next_empty_is_valid = 0;
}
JLI(PValue, intern->array, index);
if (PValue != NULL && PValue != PJERR) {
*PValue = (void *)value_long;
return SUCCESS;
}
return FAILURE;
} else if (intern->type == TYPE_INT_TO_MIXED) {
Pvoid_t *PValue;
if (!offset || index <= -1) {
if (intern->array){
if (!offset && intern->next_empty_is_valid) {
index = intern->next_empty++;
} else {
index = -1;
JLL(PValue, intern->array, index);
if (PValue != NULL && PValue != PJERR) {
index += 1;
if (!offset) {
intern->next_empty = index + 1;
intern->next_empty_is_valid = 1;
}
} else {
return FAILURE;
}
}
} else {
if (intern->next_empty_is_valid) {
index = intern->next_empty++;
} else {
index = 0;
}
}
} else {
intern->next_empty_is_valid = 0;
}
JLI(PValue, intern->array, index);
if (PValue != NULL && PValue != PJERR) {
if (*PValue != NULL) {
zval *old_value = (zval *)*PValue;
zval_ptr_dtor(&old_value);
}
*PValue = value;
Z_ADDREF_P(value);
return SUCCESS;
}
return FAILURE;
} else if (intern->type == TYPE_STRING_TO_INT) {
PWord_t *PValue;
int res;
JSLI(PValue, intern->array, (uint8_t *)Z_STRVAL_P(pstring_key));
if (PValue != NULL && PValue != PJERR) {
*PValue = (void *)Z_LVAL_P(value);
intern->counter++;
res = SUCCESS;
} else {
res = FAILURE;
}
if (pstring_key != offset) {
zval_dtor(pstring_key);
}
return res;
} else if (intern->type == TYPE_STRING_TO_MIXED) {
Pvoid_t *PValue;
int res;
JSLI(PValue, intern->array, (uint8_t *)Z_STRVAL_P(pstring_key));
if (PValue != NULL && PValue != PJERR) {
if (*PValue != NULL) {
zval *old_value = (zval *)*PValue;
zval_ptr_dtor(&old_value);
} else {
intern->counter++;
}
*PValue = value;
Z_ADDREF_P(value);
res = SUCCESS;
} else {
res = FAILURE;
}
if (pstring_key != offset) {
zval_dtor(pstring_key);
}
return res;
}
return FAILURE;
}
/* }}} */
static void judy_object_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC) /* {{{ */
{
judy_object_write_dimension_helper(object, offset, value TSRMLS_CC);
}
/* }}} */
int judy_object_has_dimension_helper(zval *object, zval *offset, int check_empty TSRMLS_DC) /* {{{ */
{
long index = 0;
Word_t j_index;
Pvoid_t *PValue = NULL;
zval string_key, *pstring_key = &string_key;
judy_object *intern = (judy_object *) zend_object_store_get_object(object TSRMLS_CC);
if (intern->array == NULL) {
return 0;
}
CHECK_ARRAY_AND_ARG_TYPE(index, pstring_key, return 0);
j_index = index;
if (intern->type == TYPE_BITSET) {
int Rc_int;
J1T(Rc_int, intern->array, j_index);
return Rc_int;
}
switch(intern->type) {
case TYPE_INT_TO_INT:
case TYPE_INT_TO_MIXED:
JLG(PValue, intern->array, j_index);
break;
case TYPE_STRING_TO_INT:
case TYPE_STRING_TO_MIXED:
JSLG(PValue, intern->array, (uint8_t *)Z_STRVAL_P(pstring_key));
if (pstring_key != offset) {
zval_dtor(pstring_key);
}
break;
}
if (PValue != NULL && PValue != PJERR) {
if (!check_empty) {
return 1;
} else if (intern->type == TYPE_INT_TO_INT || intern->type == TYPE_STRING_TO_INT) {
if (*PValue) {
return 1;
}
return 0;
} else if (intern->type == TYPE_INT_TO_MIXED || intern->type == TYPE_STRING_TO_MIXED) {
if (*PValue && zend_is_true((zval *)*PValue)) {
return 1;
}
return 0;
}
}
return 0;
}
/* }}} */
static int judy_object_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC) /* {{{ */
{
return judy_object_has_dimension_helper(object, offset, check_empty TSRMLS_CC);
}
/* }}} */
int judy_object_unset_dimension_helper(zval *object, zval *offset TSRMLS_DC) /* {{{ */
{
int Rc_int = 0;
long index = 0;
Word_t j_index;
zval string_key, *pstring_key = &string_key;
judy_object *intern = (judy_object *) zend_object_store_get_object(object TSRMLS_CC);
if (intern->array == NULL) {
return FAILURE;
}
CHECK_ARRAY_AND_ARG_TYPE(index, pstring_key, return FAILURE);
j_index = index;
if (intern->type == TYPE_BITSET) {
J1U(Rc_int, intern->array, j_index);
} else if (intern->type == TYPE_INT_TO_INT || intern->type == TYPE_INT_TO_MIXED) {
if (intern->type == TYPE_INT_TO_INT) {
JLD(Rc_int, intern->array, j_index);
} else {
Pvoid_t *PValue;
JLG(PValue, intern->array, j_index);
if (PValue != NULL && PValue != PJERR) {
zval *value = (zval *)*PValue;
zval_ptr_dtor(&value);
JLD(Rc_int, intern->array, j_index);
}
}
if (Rc_int == 1) {
intern->counter--;
}
} else if (intern->type == TYPE_STRING_TO_INT || intern->type == TYPE_STRING_TO_MIXED) {
if (intern->type == TYPE_STRING_TO_INT) {
JSLD(Rc_int, intern->array, (uint8_t *)Z_STRVAL_P(pstring_key));
} else {
Pvoid_t *PValue;
JSLG(PValue, intern->array, (uint8_t *)Z_STRVAL_P(pstring_key));
if (PValue != NULL && PValue != PJERR) {
zval *value = (zval *)*PValue;
zval_ptr_dtor(&value);
JSLD(Rc_int, intern->array, (uint8_t *)Z_STRVAL_P(pstring_key));
}
}
if (Rc_int == 1) {
intern->counter--;
}
if (pstring_key != offset) {
zval_dtor(pstring_key);
}
}
return Rc_int ? SUCCESS : FAILURE;
}
/* }}} */
static void judy_object_unset_dimension(zval *object, zval *offset TSRMLS_DC) /* {{{ */
{
judy_object_unset_dimension_helper(object, offset TSRMLS_CC);
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(judy)
{
zend_class_entry ce;
ZEND_INIT_MODULE_GLOBALS(judy, php_judy_init_globals, NULL);
REGISTER_INI_ENTRIES();
/* Judy class definition */
INIT_CLASS_ENTRY(ce, "Judy", judy_class_methods);
judy_ce = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
judy_ce->create_object = judy_object_new;
memcpy(&judy_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
/* set some internal handlers */
judy_handlers.clone_obj = judy_object_clone;
judy_handlers.count_elements = judy_object_count;
judy_handlers.read_dimension = judy_object_read_dimension;
judy_handlers.write_dimension = judy_object_write_dimension;
judy_handlers.unset_dimension = judy_object_unset_dimension;
judy_handlers.has_dimension = judy_object_has_dimension;
/* implements some interface to provide access to judy object as an array */
zend_class_implements(judy_ce TSRMLS_CC, 2, zend_ce_arrayaccess, spl_ce_Countable);
judy_ce->get_iterator = judy_get_iterator;
REGISTER_JUDY_CLASS_CONST_LONG("BITSET", TYPE_BITSET);
REGISTER_JUDY_CLASS_CONST_LONG("INT_TO_INT", TYPE_INT_TO_INT);
REGISTER_JUDY_CLASS_CONST_LONG("INT_TO_MIXED", TYPE_INT_TO_MIXED);
REGISTER_JUDY_CLASS_CONST_LONG("STRING_TO_INT", TYPE_STRING_TO_INT);
REGISTER_JUDY_CLASS_CONST_LONG("STRING_TO_MIXED", TYPE_STRING_TO_MIXED);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(judy)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(judy)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(judy)
{
php_info_print_table_start();
php_info_print_table_header(2, "Judy support", "enabled");
php_info_print_table_row(2, "PHP Judy version", PHP_JUDY_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ proto Judy::__construct(long type)
Constructs a new Judy array of the given type */
PHP_METHOD(judy, __construct)
{
long type;
judy_type jtype;
JUDY_METHOD_GET_OBJECT
JUDY_METHOD_ERROR_HANDLING;
if (intern->type) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Judy Array already instantiated");
} else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type) == SUCCESS) {
JTYPE(jtype, type);
intern->counter = 0;
intern->type = jtype;
intern->array = (Pvoid_t) NULL;
}
zend_restore_error_handling(&error_handling TSRMLS_CC);
}
/* }}} */
/* {{{ proto Judy::__destruct()
Free Judy array and any other references */
PHP_METHOD(judy, __destruct)
{
zval *object = getThis();
/* calling the object's free() method */
zend_call_method_with_0_params(&object, NULL, NULL, "free", NULL);
}
/* }}} */
/* {{{ proto long Judy::free()
Free the entire Judy Array. Return the number of bytes freed */
PHP_METHOD(judy, free)
{
JUDY_METHOD_GET_OBJECT
Word_t Rc_word = 0;
Word_t index;
uint8_t kindex[PHP_JUDY_MAX_LENGTH];
Word_t *PValue;
switch (intern->type)
{
case TYPE_BITSET:
/* Free Judy1 Array */
J1FA(Rc_word, intern->array);
break;
case TYPE_INT_TO_INT:
/* Free JudyL Array */
JLFA(Rc_word, intern->array);
break;
case TYPE_INT_TO_MIXED:
index = 0;
/* Del ref to zval objects */
JLF(PValue, intern->array, index);
while(PValue != NULL && PValue != PJERR)
{
zval *value = (zval *)*PValue;
zval_ptr_dtor(&value);
JLN(PValue, intern->array, index);
}
/* Free JudyL Array */
JLFA(Rc_word, intern->array);
break;
case TYPE_STRING_TO_INT:
/* Free JudySL Array */
JSLFA(Rc_word, intern->array);
/* Reset counter */
intern->counter = 0;
break;
case TYPE_STRING_TO_MIXED:
kindex[0] = '\0';
/* Del ref to zval objects */
JSLF(PValue, intern->array, kindex);
while(PValue != NULL && PValue != PJERR)
{
zval *value = (zval *)*PValue;
zval_ptr_dtor(&value);
JSLN(PValue, intern->array, kindex);
}
/* Free JudySL Array */
JSLFA(Rc_word, intern->array);
/* Reset counter */
intern->counter = 0;
break;
}
RETURN_LONG(Rc_word);
}
/* }}} */
/* {{{ proto long Judy::memoryUsage()
Return the memory used by the Judy Array */
PHP_METHOD(judy, memoryUsage)
{
Word_t Rc_word;
JUDY_METHOD_GET_OBJECT
switch (intern->type)
{
case TYPE_BITSET:
J1MU(Rc_word, intern->array);
RETURN_LONG(Rc_word);
break;
case TYPE_INT_TO_INT:
case TYPE_INT_TO_MIXED:
JLMU(Rc_word, intern->array);
RETURN_LONG(Rc_word);
break;
default:
RETURN_NULL();
}
}
/* }}} */
/* {{{ proto long Judy::count()
Return the current size of the array. */
PHP_METHOD(judy, count)
{
JUDY_METHOD_GET_OBJECT
if (intern->type == TYPE_BITSET || intern->type == TYPE_INT_TO_INT
|| intern->type == TYPE_INT_TO_MIXED) {
Word_t idx1 = 0;
Word_t idx2 = -1;
Word_t Rc_word;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &idx1, &idx2) == FAILURE) {
RETURN_FALSE;
}
if (intern->type == TYPE_BITSET) {
J1C(Rc_word, intern->array, idx1, idx2);
} else {
JLC(Rc_word, intern->array, idx1, idx2);
}
RETURN_LONG(Rc_word);
} else if (intern->type == TYPE_STRING_TO_INT || intern->type == TYPE_STRING_TO_MIXED) {
RETURN_LONG(intern->counter);
}
}
/* }}} */
/* {{{ proto long Judy::byCount(long nth_index)
Locate the Nth index that is present in the Judy array (Nth = 1 returns the first index present).
To refer to the last index in a fully populated array (all indexes present, which is rare), use Nth = 0. */
PHP_METHOD(judy, byCount)
{
JUDY_METHOD_GET_OBJECT
if (intern->type == TYPE_BITSET || intern->type == TYPE_INT_TO_INT
|| intern->type == TYPE_INT_TO_MIXED) {
long nth_index;
Word_t index;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &nth_index) == FAILURE) {
RETURN_FALSE;
}
if (intern->type == TYPE_BITSET) {
int Rc_int;
J1BC(Rc_int, intern->array, nth_index, index);
if (Rc_int == 1)
RETURN_LONG(index);
} else {
PWord_t PValue;
JLBC(PValue, intern->array, nth_index, index);
if (PValue != NULL && PValue != PJERR)
RETURN_LONG(index);
}
}
RETURN_NULL();
}
/* }}} */
/* {{{ proto mixed Judy::first([mixed index])
Search (inclusive) for the first index present that is equal to or greater than the passed Index */
PHP_METHOD(judy, first)
{
JUDY_METHOD_GET_OBJECT
if (intern->type == TYPE_BITSET) {
Word_t index = 0;
int Rc_int;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &index) == FAILURE) {
RETURN_FALSE;
}
J1F(Rc_int, intern->array, index);
if (Rc_int == 1)
RETURN_LONG(index);
} else if (intern->type == TYPE_INT_TO_INT || intern->type == TYPE_INT_TO_MIXED) {
Word_t index = 0;
PWord_t PValue;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &index) == FAILURE) {
RETURN_FALSE;
}
JLF(PValue, intern->array, index);
if (PValue != NULL && PValue != PJERR)
RETURN_LONG(index);
} else if (intern->type == TYPE_STRING_TO_INT || intern->type == TYPE_STRING_TO_MIXED) {
char *str;
int str_length = 0;
uint8_t key[PHP_JUDY_MAX_LENGTH];
PWord_t PValue;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &str, &str_length) == FAILURE) {
RETURN_FALSE;
}
/* JudySL require null temrinated strings */
if (str_length == 0) {
key[0] = '\0';
} else {
int key_len = str_length >= PHP_JUDY_MAX_LENGTH ? PHP_JUDY_MAX_LENGTH - 1 : str_length;
memcpy(key, str, key_len);
key[key_len] = '\0';
}
JSLF(PValue, intern->array, key);
if (PValue != NULL && PValue != PJERR)
RETURN_STRING((char *)key, 1);
}
RETURN_NULL();
}
/* }}} */
/* {{{ proto mixed Judy::next(mixed index)
Search (exclusive) for the next index present that is greater than the passed Index */
PHP_METHOD(judy, next)
{
JUDY_METHOD_GET_OBJECT
if (intern->type == TYPE_BITSET) {
Word_t index;
int Rc_int;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
RETURN_FALSE;
}
J1N(Rc_int, intern->array, index);
if (Rc_int == 1)
RETURN_LONG(index);
} else if (intern->type == TYPE_INT_TO_INT || intern->type == TYPE_INT_TO_MIXED) {
Word_t index;
PWord_t PValue;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
RETURN_FALSE;
}
JLN(PValue, intern->array, index);
if (PValue != NULL && PValue != PJERR)
RETURN_LONG(index);
} else if (intern->type == TYPE_STRING_TO_INT || intern->type == TYPE_STRING_TO_MIXED) {
char *str;
int str_length;
uint8_t key[PHP_JUDY_MAX_LENGTH];
PWord_t PValue;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_length) == FAILURE) {
RETURN_FALSE;
}
/* JudySL require null temrinated strings */
if (str_length == 0) {
key[0] = '\0';
} else {
int key_len = str_length >= PHP_JUDY_MAX_LENGTH ? PHP_JUDY_MAX_LENGTH - 1 : str_length;
memcpy(key, str, key_len);
key[key_len] = '\0';
}
JSLN(PValue, intern->array, key);
if (PValue != NULL && PValue != PJERR)
RETURN_STRING((char *)key, 1);
}
RETURN_NULL();
}
/* }}} */
/* {{{ proto mixed Judy::last([mixed index])
Search (inclusive) for the last index present that is equal to or less than the passed Index */
PHP_METHOD(judy, last)
{
JUDY_METHOD_GET_OBJECT
if (intern->type == TYPE_BITSET) {
Word_t index = -1;
int Rc_int;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &index) == FAILURE) {
RETURN_FALSE;
}
J1L(Rc_int, intern->array, index);
if (Rc_int == 1)
RETURN_LONG(index);
} else if (intern->type == TYPE_INT_TO_INT || intern->type == TYPE_INT_TO_MIXED) {
Word_t index = -1;
PWord_t PValue;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &index) == FAILURE) {
RETURN_FALSE;
}
JLL(PValue, intern->array, index);
if (PValue != NULL && PValue != PJERR)
RETURN_LONG(index);
} else if (intern->type == TYPE_STRING_TO_INT || intern->type == TYPE_STRING_TO_MIXED) {
uint8_t *str;
int str_length = 0;
uint8_t key[PHP_JUDY_MAX_LENGTH];
PWord_t PValue;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &str, &str_length) == FAILURE) {
RETURN_FALSE;
}
/* JudySL require null terminated strings */
if (str_length == 0) {
memset(key, 0xff, PHP_JUDY_MAX_LENGTH);
key[PHP_JUDY_MAX_LENGTH-1] = '\0';
} else {
int key_len = str_length >= PHP_JUDY_MAX_LENGTH ? PHP_JUDY_MAX_LENGTH - 1 : str_length;
memcpy(key, str, key_len);
key[key_len] = '\0';
}
JSLL(PValue, intern->array, key);
if (PValue != NULL && PValue != PJERR)
RETURN_STRING((char *)key, 1);
}
RETURN_NULL();
}
/* }}} */
/* {{{ proto mixed Judy::prev(mixed index)
Search (exclusive) for the previous index present that is less than the passed Index */
PHP_METHOD(judy, prev)
{
JUDY_METHOD_GET_OBJECT
if (intern->type == TYPE_BITSET) {
Word_t index;
int Rc_int;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
RETURN_FALSE;
}
J1P(Rc_int, intern->array, index);
if (Rc_int == 1)
RETURN_LONG(index);
} else if (intern->type == TYPE_INT_TO_INT || intern->type == TYPE_INT_TO_MIXED) {
Word_t index;
PWord_t PValue;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
RETURN_FALSE;
}
JLP(PValue, intern->array, index);
if (PValue != NULL && PValue != PJERR)
RETURN_LONG(index);
} else if (intern->type == TYPE_STRING_TO_INT || intern->type == TYPE_STRING_TO_MIXED) {
char *str;
int str_length;