From 051df16d51eb1939738b385e971432d04ab695e8 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 8 Aug 2020 10:05:51 +0200 Subject: Changed iter API again, optimized. --- examples/demos.c | 2 +- stc/cdefs.h | 2 +- stc/clist.h | 10 +++++----- stc/cmap.h | 12 +++++------- stc/cvec.h | 7 +++---- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/examples/demos.c b/examples/demos.c index 02ac8623..a3a587ec 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; cmap_si_next(&i)) + for (cmap_si_iter_t i = cmap_si_begin(&nums); i.item != i.end; 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 2c305e41..54c8863e 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; prefix##_next(&it)) + for (prefix##_iter_t it = prefix##_begin(&container); it.item != it.end; 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 7cebda37..d82a7f28 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -78,7 +78,7 @@ } clist_##tag; \ \ typedef struct { \ - clist_##tag##_node_t *item, **_last; \ + clist_##tag##_node_t *item, *end, **_last; \ } clist_##tag##_iter_t #define clist_init {NULL} @@ -130,7 +130,7 @@ STC_INLINE clist_##tag##_iter_t \ clist_##tag##_begin(clist_##tag* self) { \ clist_##tag##_node_t *head = self->last ? self->last->next : NULL; \ - clist_##tag##_iter_t it = {head, &self->last}; return it; \ + clist_##tag##_iter_t it = {head, NULL, &self->last}; return it; \ } \ STC_INLINE void \ clist_##tag##_next(clist_##tag##_iter_t* it) { \ @@ -138,7 +138,7 @@ } \ STC_INLINE clist_##tag##_iter_t \ clist_##tag##_last(clist_##tag* self) { \ - clist_##tag##_iter_t it = {self->last, &self->last}; return it; \ + clist_##tag##_iter_t it = {self->last, NULL, &self->last}; return it; \ } \ \ implement_clist_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \ @@ -189,7 +189,7 @@ STC_API void \ clist_##tag##_splice_front(clist_##tag* self, clist_##tag* other) { \ clist_void *s = (clist_void *) self; \ - clist_void_iter_t last = {s->last, &s->last}; \ + clist_void_iter_t last = {s->last, NULL, &s->last}; \ _clist_splice(s, last, (clist_void *)other, false); \ } \ STC_API void \ @@ -199,7 +199,7 @@ \ STC_API clist_##tag##_iter_t \ clist_##tag##_find_before(clist_##tag* self, RawValue val) { \ - clist_##tag##_iter_t prev = {self->last, &self->last}; \ + clist_##tag##_iter_t prev = {self->last, NULL, &self->last}; \ c_foreach (i, clist_##tag, *self) { \ RawValue r = valueGetRaw(&i.item->value); \ if (valueCompareRaw(&r, &val) == 0) \ diff --git a/stc/cmap.h b/stc/cmap.h index f7565ebe..db7cc0a9 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -156,7 +156,7 @@ typedef struct { \ } ctype##_##tag; \ \ typedef struct { \ - ctype##_##tag##_entry_t *item, *_end; \ + ctype##_##tag##_entry_t *item, *end; \ uint8_t* _hx; \ } ctype##_##tag##_iter_t; \ \ @@ -365,16 +365,14 @@ ctype##_##tag##_erase(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey) { \ \ STC_API ctype##_##tag##_iter_t \ ctype##_##tag##_begin(ctype##_##tag* map) { \ - uint8_t* hx = map->_hashx; \ - ctype##_##tag##_entry_t* e = map->table, *end = e + map->bucket_count; \ - while (e != end && !*hx) ++e, ++hx; \ - ctype##_##tag##_iter_t it = {e == end ? NULL : e, end, hx}; return it; \ + ctype##_##tag##_iter_t it = {map->table, map->table + map->bucket_count, map->_hashx}; \ + while (it.item != it.end && *it._hx == 0) ++it.item, ++it._hx; \ + 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; \ + while (++it->item != it->end && *++it->_hx == 0) ; \ } #else diff --git a/stc/cvec.h b/stc/cvec.h index 9bd8f168..60f44d35 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -107,18 +107,17 @@ 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 \ cvec_##tag##_begin(cvec_##tag* vec) { \ - const size_t n = cvec_size(*vec); \ - cvec_##tag##_iter_t it = {n ? vec->data : NULL, vec->data + n}; \ + cvec_##tag##_iter_t it = {vec->data, vec->data + cvec_size(*vec)}; \ return it; \ } \ STC_INLINE void \ cvec_##tag##_next(cvec_##tag##_iter_t* it) { \ - if (++it->item == it->_end) it->item = NULL; \ + ++it->item; \ } \ \ implement_cvec_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \ -- cgit v1.2.3