Skip to content

Commit 26eaf66

Browse files
author
Andrei Zmievski
committed
Implemented external list traversing.
1 parent 29c41fe commit 26eaf66

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

Diff for: Zend/zend_llist.c

+26-18
Original file line numberDiff line numberDiff line change
@@ -208,46 +208,54 @@ ZEND_API int zend_llist_count(zend_llist *l)
208208
}
209209

210210

211-
ZEND_API void *zend_llist_get_first(zend_llist *l)
211+
ZEND_API void *zend_llist_get_first_ex(zend_llist *l, zend_llist_position *pos)
212212
{
213-
l->traverse_ptr = l->head;
214-
if (l->traverse_ptr) {
215-
return l->traverse_ptr->data;
213+
zend_llist_position *current = pos ? pos : &l->traverse_ptr;
214+
215+
*current = l->head;
216+
if (*current) {
217+
return (*current)->data;
216218
} else {
217219
return NULL;
218220
}
219221
}
220222

221223

222-
ZEND_API void *zend_llist_get_last(zend_llist *l)
224+
ZEND_API void *zend_llist_get_last_ex(zend_llist *l, zend_llist_position *pos)
223225
{
224-
l->traverse_ptr = l->tail;
225-
if (l->traverse_ptr) {
226-
return l->traverse_ptr->data;
226+
zend_llist_position *current = pos ? pos : &l->traverse_ptr;
227+
228+
*current = l->tail;
229+
if (*current) {
230+
return (*current)->data;
227231
} else {
228232
return NULL;
229233
}
230234
}
231235

232236

233-
ZEND_API void *zend_llist_get_next(zend_llist *l)
237+
ZEND_API void *zend_llist_get_next_ex(zend_llist *l, zend_llist_position *pos)
234238
{
235-
if (l->traverse_ptr) {
236-
l->traverse_ptr = l->traverse_ptr->next;
237-
if (l->traverse_ptr) {
238-
return l->traverse_ptr->data;
239+
zend_llist_position *current = pos ? pos : &l->traverse_ptr;
240+
241+
if (*current) {
242+
*current = (*current)->next;
243+
if (*current) {
244+
return (*current)->data;
239245
}
240246
}
241247
return NULL;
242248
}
243249

244250

245-
ZEND_API void *zend_llist_get_prev(zend_llist *l)
251+
ZEND_API void *zend_llist_get_prev_ex(zend_llist *l, zend_llist_position *pos)
246252
{
247-
if (l->traverse_ptr) {
248-
l->traverse_ptr = l->traverse_ptr->prev;
249-
if (l->traverse_ptr) {
250-
return l->traverse_ptr->data;
253+
zend_llist_position *current = pos ? pos : &l->traverse_ptr;
254+
255+
if (*current) {
256+
*current = (*current)->prev;
257+
if (*current) {
258+
return (*current)->data;
251259
}
252260
}
253261
return NULL;

Diff for: Zend/zend_llist.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef struct _zend_llist {
3939
} zend_llist;
4040

4141
typedef int (*llist_compare_func_t) (const zend_llist_element *, const zend_llist_element *);
42+
typedef zend_llist_element* zend_llist_position;
4243

4344
BEGIN_EXTERN_C()
4445
ZEND_API void zend_llist_init(zend_llist *l, size_t size, void (*dtor)(void *data), unsigned char persistent);
@@ -55,10 +56,16 @@ ZEND_API int zend_llist_count(zend_llist *l);
5556
ZEND_API void zend_llist_sort(zend_llist *l, llist_compare_func_t comp_func);
5657

5758
/* traversal */
58-
ZEND_API void *zend_llist_get_first(zend_llist *l);
59-
ZEND_API void *zend_llist_get_last(zend_llist *l);
60-
ZEND_API void *zend_llist_get_next(zend_llist *l);
61-
ZEND_API void *zend_llist_get_prev(zend_llist *l);
59+
ZEND_API void *zend_llist_get_first_ex(zend_llist *l, zend_llist_position *pos);
60+
ZEND_API void *zend_llist_get_last_ex(zend_llist *l, zend_llist_position *pos);
61+
ZEND_API void *zend_llist_get_next_ex(zend_llist *l, zend_llist_position *pos);
62+
ZEND_API void *zend_llist_get_prev_ex(zend_llist *l, zend_llist_position *pos);
63+
64+
#define zend_llist_get_first(l) zend_llist_get_first_ex(l, NULL)
65+
#define zend_llist_get_last(l) zend_llist_get_last_ex(l, NULL)
66+
#define zend_llist_get_next(l) zend_llist_get_next_ex(l, NULL)
67+
#define zend_llist_get_prev(l) zend_llist_get_prev_ex(l, NULL)
68+
6269
END_EXTERN_C()
6370

6471
#endif /* _ZEND_LLIST_H */

0 commit comments

Comments
 (0)