forked from derickr/quickhash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqh_iterator.c
174 lines (145 loc) · 5.5 KB
/
qh_iterator.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2011 StumbleUpon Inc. |
+----------------------------------------------------------------------+
| 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php.h"
#include "zend.h"
#include "zend_API.h"
#include "qh_inthash.h"
#include "qh_intset.h"
#include "qh_iterator.h"
#include "quickhash.h"
/* define an overloaded iterator structure */
typedef struct {
zend_object_iterator intern;
zval *current_value;
qhit iterator;
zval *object_ref; /* Used so that we can protect the object from being destroyed prematurely */
} qh_intset_it;
/* iterator handlers */
static void qh_intset_it_dtor(zend_object_iterator *iter TSRMLS_DC)
{
qh_intset_it *iterator = (qh_intset_it *)iter;
Z_DELREF_P(iterator->object_ref);
qhi_iterator_deinit(&iterator->iterator);
zval_ptr_dtor(&iterator->current_value);
efree(iterator);
}
static int qh_intset_it_has_more(zend_object_iterator *iter TSRMLS_DC)
{
qh_intset_it *iterator = (qh_intset_it *)iter;
int ret;
ret = qhi_iterator_forward(&iterator->iterator) ? SUCCESS : FAILURE;
ZVAL_LONG(iterator->current_value, iterator->iterator.key);
return ret;
}
static int qh_inthash_it_has_more(zend_object_iterator *iter TSRMLS_DC)
{
qh_intset_it *iterator = (qh_intset_it *)iter;
int ret;
qhi *hash = (qhi* ) iterator->intern.data;
ret = qhi_iterator_forward(&iterator->iterator) ? SUCCESS : FAILURE;
switch (hash->value_type) {
case QHI_VALUE_TYPE_INT:
ZVAL_LONG(iterator->current_value, iterator->iterator.value.i);
break;
case QHI_VALUE_TYPE_STRING:
if (Z_TYPE_P(iterator->current_value) == IS_STRING) {
zval_dtor(iterator->current_value);
}
ZVAL_STRING(iterator->current_value, iterator->iterator.value.s, 1);
break;
}
return ret;
}
static void qh_intset_it_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC)
{
qh_intset_it *iterator = (qh_intset_it *)iter;
*data = &iterator->current_value;
}
static int qh_intset_it_current_key(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC)
{
qh_intset_it *iterator = (qh_intset_it *)iter;
qhi *hash = (qhi* ) iterator->intern.data;
if (hash->key_type == QHI_KEY_TYPE_STRING) {
*str_key = estrndup(hash->keys.values + iterator->iterator.key, strlen(hash->keys.values + iterator->iterator.key));
*str_key_len = strlen(*str_key) + 1;
return HASH_KEY_IS_STRING;
} else {
*int_key = iterator->iterator.key;
return HASH_KEY_IS_LONG;
}
}
static void qh_intset_it_move_forward(zend_object_iterator *iter TSRMLS_DC)
{
}
static void qh_intset_it_rewind(zend_object_iterator *iter TSRMLS_DC)
{
qh_intset_it *iterator = (qh_intset_it *)iter;
qhi *hash = (qhi* ) iterator->intern.data;
qhi_iterator_init(&iterator->iterator, hash);
}
/* iterator handler table for sets */
zend_object_iterator_funcs qh_intset_it_funcs = {
qh_intset_it_dtor,
qh_intset_it_has_more,
qh_intset_it_current_data,
qh_intset_it_current_key,
qh_intset_it_move_forward,
qh_intset_it_rewind,
NULL
};
zend_object_iterator *qh_intset_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC)
{
qh_intset_it *iterator = emalloc(sizeof(qh_intset_it));
php_qh_intset_obj *obj = (php_qh_intset_obj *)zend_object_store_get_object(object TSRMLS_CC);
if (by_ref) {
zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
}
iterator->intern.data = (void*) obj->hash;
iterator->intern.funcs = &qh_intset_it_funcs;
iterator->object_ref = object;
Z_ADDREF_P(iterator->object_ref);
MAKE_STD_ZVAL(iterator->current_value);
ZVAL_NULL(iterator->current_value);
return (zend_object_iterator*)iterator;
}
/* iterator handler table for hashes */
zend_object_iterator_funcs qh_inthash_it_funcs = {
qh_intset_it_dtor,
qh_inthash_it_has_more,
qh_intset_it_current_data,
qh_intset_it_current_key,
qh_intset_it_move_forward,
qh_intset_it_rewind,
NULL
};
zend_object_iterator *qh_inthash_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC)
{
qh_intset_it *iterator = emalloc(sizeof(qh_intset_it));
php_qh_intset_obj *obj = (php_qh_intset_obj *)zend_object_store_get_object(object TSRMLS_CC);
if (by_ref) {
zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
}
iterator->intern.data = (void*) obj->hash;
iterator->intern.funcs = &qh_inthash_it_funcs;
iterator->object_ref = object;
Z_ADDREF_P(iterator->object_ref);
MAKE_STD_ZVAL(iterator->current_value);
ZVAL_NULL(iterator->current_value);
return (zend_object_iterator*)iterator;
}