From 82b4f519436dbb9d61b1c02b1ca1e3122b7523b3 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 8 Aug 2020 08:21:40 +0200 Subject: Changed iter API (next now takes address, no return) --- examples/demos.c | 2 +- stc/cdefs.h | 2 +- stc/clist.h | 6 +++--- stc/cmap.h | 13 ++++++------- stc/cvec.h | 9 ++++----- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/examples/demos.c b/examples/demos.c index d7b49ca8..02ac8623 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -139,7 +139,7 @@ void mapdemo2() cmap_si_put(&nums, "Groovy", 200); // overwrite previous // iterate the map: - for (cmap_si_iter_t i = cmap_si_begin(&nums); i.item; i = cmap_si_next(i)) + for (cmap_si_iter_t i = cmap_si_begin(&nums); i.item; cmap_si_next(&i)) printf("long: %s: %d\n", i.item->key.str, i.item->value); // or rather use the short form: diff --git a/stc/cdefs.h b/stc/cdefs.h index 6389b779..2c305e41 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -79,7 +79,7 @@ #define c_default_destroy(p) ((void)0) #define c_foreach(it, prefix, container) \ - for (prefix##_iter_t it = prefix##_begin(&container); it.item; it = prefix##_next(it)) + for (prefix##_iter_t it = prefix##_begin(&container); it.item; prefix##_next(&it)) #define c_items(...) {__VA_ARGS__} #define c_push(container, prefix, items) do { \ const prefix##_input_t __arr[] = items; \ diff --git a/stc/clist.h b/stc/clist.h index 4e834d16..7cebda37 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -132,9 +132,9 @@ clist_##tag##_node_t *head = self->last ? self->last->next : NULL; \ clist_##tag##_iter_t it = {head, &self->last}; return it; \ } \ - STC_INLINE clist_##tag##_iter_t \ - clist_##tag##_next(clist_##tag##_iter_t it) { \ - it.item = it.item == *it._last ? NULL : it.item->next; return it; \ + STC_INLINE void \ + clist_##tag##_next(clist_##tag##_iter_t* it) { \ + it->item = it->item == *it->_last ? NULL : it->item->next; \ } \ STC_INLINE clist_##tag##_iter_t \ clist_##tag##_last(clist_##tag* self) { \ diff --git a/stc/cmap.h b/stc/cmap.h index 36dd241a..f7565ebe 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -194,8 +194,8 @@ STC_API bool \ ctype##_##tag##_erase(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey); \ STC_API ctype##_##tag##_iter_t \ ctype##_##tag##_begin(ctype##_##tag* map); \ -STC_API ctype##_##tag##_iter_t \ -ctype##_##tag##_next(ctype##_##tag##_iter_t it); \ +STC_API void \ +ctype##_##tag##_next(ctype##_##tag##_iter_t* it); \ \ implement_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyToRaw, keyFromRaw) \ @@ -371,11 +371,10 @@ ctype##_##tag##_begin(ctype##_##tag* map) { \ ctype##_##tag##_iter_t it = {e == end ? NULL : e, end, hx}; return it; \ } \ \ -STC_API ctype##_##tag##_iter_t \ -ctype##_##tag##_next(ctype##_##tag##_iter_t it) { \ - do { ++it.item, ++it._hx; } while (it.item != it._end && !*it._hx); \ - if (it.item == it._end) it.item = NULL; \ - return it; \ +STC_API void \ +ctype##_##tag##_next(ctype##_##tag##_iter_t* it) { \ + while (++it->item != it->_end && *++it->_hx == 0) ; \ + if (it->item == it->_end) it->item = NULL; \ } #else diff --git a/stc/cvec.h b/stc/cvec.h index 4be04eca..9bd8f168 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -107,7 +107,7 @@ cvec_##tag##_swap(cvec_##tag* a, cvec_##tag* b) { \ } \ \ typedef struct { \ - Value *item, *end; \ + Value *item, *_end; \ } cvec_##tag##_iter_t; \ \ STC_INLINE cvec_##tag##_iter_t \ @@ -116,10 +116,9 @@ cvec_##tag##_begin(cvec_##tag* vec) { \ cvec_##tag##_iter_t it = {n ? vec->data : NULL, vec->data + n}; \ return it; \ } \ -STC_INLINE cvec_##tag##_iter_t \ -cvec_##tag##_next(cvec_##tag##_iter_t it) { \ - if (++it.item == it.end) it.item = NULL; \ - return it; \ +STC_INLINE void \ +cvec_##tag##_next(cvec_##tag##_iter_t* it) { \ + if (++it->item == it->_end) it->item = NULL; \ } \ \ implement_cvec_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \ -- cgit v1.2.3