-
Notifications
You must be signed in to change notification settings - Fork 138
/
orderedhashiterator.pmc
253 lines (173 loc) · 5.35 KB
/
orderedhashiterator.pmc
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
/*
Copyright (C) 2001-2014, Parrot Foundation.
=head1 NAME
src/pmc/orderedhashiterator.pmc - OrderedHashIterator
=head1 DESCRIPTION
Specialized iterator for an OrderedHash PMC.
=head1 METHODS
=over 4
=cut
*/
#include "pmc/pmc_orderedhash.h"
#include "pmc/pmc_hashiteratorkey.h"
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass OrderedHashIterator extends Iterator provides iterator no_ro auto_attrs {
ATTR PMC *pmc_hash; /* the Hash which this Iterator iterates */
ATTR PMC *next_entry; /* Next entry to shift/pop */
ATTR INTVAL pos; /* */
ATTR INTVAL elements; /* How many elements left to iterate over */
ATTR INTVAL reverse; /* Direction of iteration. 1 - for reverse iteration */
/*
=item C<void init_pmc(PMC *initializer)>
Initializes the iterator with an aggregate PMC.
Defaults iteration mode to iterate from start.
=cut
*/
VTABLE void init_pmc(PMC *hash) {
Parrot_OrderedHashIterator_attributes * const attrs =
(Parrot_OrderedHashIterator_attributes *) PMC_data(SELF);
attrs->pmc_hash = hash;
attrs->pos = 0;
attrs->elements = VTABLE_elements(INTERP, hash);
attrs->next_entry = PARROT_ORDEREDHASH(hash)->first;
PMC_data(SELF) = attrs;
PObj_custom_mark_SET(SELF);
}
/*
=item C<void mark()>
Marks the hash as live.
=cut
*/
VTABLE void mark() :no_wb {
PMC * const hash = PARROT_ORDEREDHASHITERATOR(SELF)->pmc_hash;
Parrot_gc_mark_PMC_alive(INTERP, hash);
}
/*
=item C<PMC *clone()>
=cut
*/
VTABLE PMC* clone() :no_wb {
UNUSED(INTERP)
UNUSED(SELF)
return PMCNULL;
}
/*
=item C<void set_integer_native()>
=cut
*/
VTABLE void set_integer_native(INTVAL value) {
Parrot_OrderedHashIterator_attributes * const attrs =
PARROT_ORDEREDHASHITERATOR(SELF);
/* Restart iterator */
attrs->elements = VTABLE_elements(INTERP, attrs->pmc_hash);
switch (value) {
case ITERATE_FROM_START:
case ITERATE_FROM_START_KEYS:
attrs->pos = 0;
attrs->reverse = 0;
attrs->next_entry = PARROT_ORDEREDHASH(attrs->pmc_hash)->first;
break;
case ITERATE_FROM_END:
attrs->pos = attrs->elements;
attrs->reverse = 1;
attrs->next_entry = PARROT_ORDEREDHASH(attrs->pmc_hash)->last;
break;
default:
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_INVALID_OPERATION,
"HashIterator: unknown iterator type");
}
};
/*
=item C<PMC *get_pmc()>
Returns this Iterator's Hash.
=cut
*/
VTABLE PMC* get_pmc() :no_wb {
UNUSED(INTERP)
return PARROT_ORDEREDHASHITERATOR(SELF)->pmc_hash;
}
/*
=item C<INTVAL get_bool()>
Returns true if there is more elements to iterate over.
=cut
*/
VTABLE INTVAL get_bool() :no_wb {
return STATICSELF.elements() > 0;
}
/*
=item C<INTVAL elements()>
Returns the number of remaining elements in the Hash.
=cut
*/
VTABLE INTVAL elements() :no_wb {
UNUSED(INTERP)
return PARROT_ORDEREDHASHITERATOR(SELF)->elements;
}
VTABLE INTVAL get_integer() :no_wb {
return SELF.elements();
}
/*
=item C<PMC *shift_pmc()>
Returns the HashIteratorKey for the current position and advance
the next one.
=cut
*/
VTABLE PMC *shift_pmc() :manual_wb {
Parrot_OrderedHashIterator_attributes * const attrs =
PARROT_ORDEREDHASHITERATOR(SELF);
PMC *ret;
if (!attrs->elements)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_OUT_OF_BOUNDS,
"StopIteration");
/* Get bucket and move to next bucket */
ret = VTABLE_get_pmc_keyed_int(INTERP, attrs->next_entry,
ORDERED_HASH_ITEM_KEY);
attrs->next_entry = VTABLE_get_pmc_keyed_int(INTERP, attrs->next_entry,
ORDERED_HASH_ITEM_NEXT);
++attrs->pos;
--attrs->elements;
PARROT_GC_WRITE_BARRIER(INTERP, SELF);
return ret;
}
/*
=item C<PMC *pop_pmc()>
Returns the HashIteratorKey for the current position and advance
the next one for reverse iterator.
=cut
*/
VTABLE PMC *pop_pmc() :manual_wb {
Parrot_OrderedHashIterator_attributes * const attrs =
PARROT_ORDEREDHASHITERATOR(SELF);
PMC *ret;
if (!attrs->elements)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_OUT_OF_BOUNDS,
"StopIteration");
ret = VTABLE_get_pmc_keyed_int(INTERP, attrs->next_entry,
ORDERED_HASH_ITEM_KEY);
attrs->next_entry = VTABLE_get_pmc_keyed_int(INTERP, attrs->next_entry,
ORDERED_HASH_ITEM_PREV);
--attrs->pos;
--attrs->elements;
PARROT_GC_WRITE_BARRIER(INTERP, SELF);
return ret;
}
/*
*/
VTABLE STRING* shift_string() :manual_wb {
PMC * const key = SELF.shift_pmc();
PARROT_GC_WRITE_BARRIER(INTERP, SELF);
return VTABLE_get_string(INTERP, key);
}
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/