From 1f3545e681a0fa68608760d5ec7b555e41d35c77 Mon Sep 17 00:00:00 2001 From: tylo Date: Tue, 8 Sep 2020 13:23:09 +0200 Subject: changed c_foreach() macro -> Removed cnt_range() methods, and simplified iters. --- examples/demos.c | 8 ++++---- stc/carray.h | 10 +++------- stc/cbitset.h | 10 +++------- stc/cdefs.h | 8 ++++---- stc/clist.h | 16 ++++++---------- stc/cmap.h | 14 +++++--------- stc/cqueue.h | 2 -- stc/cstack.h | 6 ++---- stc/cstr.h | 17 ++++------------- stc/cvec.h | 8 +------- 10 files changed, 32 insertions(+), 67 deletions(-) diff --git a/examples/demos.c b/examples/demos.c index c0c5de18..21d9efaa 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -73,7 +73,7 @@ void vectordemo2() cvec_str_destroy(&names); } -declare_clist(ix, int); +declare_clist(ix, int); void listdemo1() { @@ -140,7 +140,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.end; cmap_si_next(&i)) + for (cmap_si_iter_t i = cmap_si_begin(&nums), ie = cmap_si_end(&nums); i.item != ie.item; cmap_si_next(&i)) printf("long: %s: %d\n", i.item->key.str, i.item->value); // or rather use the short form: @@ -151,7 +151,7 @@ void mapdemo2() } -declare_cmap_str(); +declare_cmap_str(); void mapdemo3() { @@ -210,5 +210,5 @@ int main() mapdemo1(); mapdemo2(); mapdemo3(); - arraydemo1(); + arraydemo1(); } diff --git a/stc/carray.h b/stc/carray.h index 86324ff9..28993e4a 100644 --- a/stc/carray.h +++ b/stc/carray.h @@ -28,7 +28,7 @@ /* Multi-dimensional generic array allocated as one block of heap-memory. - // demo: + // demo: #include declare_carray(f, float); @@ -68,7 +68,7 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { } #define declare_carray_common(D, X, Value, valueDestroy) \ - typedef struct { Value *item, *end; } carray##D##X##_iter_t; \ + typedef struct { Value *item; } carray##D##X##_iter_t; \ \ STC_INLINE carray##D##X##_iter_t \ carray##D##X##_begin(carray##D##X* a) { \ @@ -78,10 +78,6 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { carray##D##X##_end(carray##D##X* a) { \ carray##D##X##_iter_t it = {a->data + carray##D##_size(*a)}; return it; \ } \ - STC_INLINE carray##D##X##_iter_t \ - carray##D##X##_range(carray##D##X##_iter_t start, carray##D##X##_iter_t finish) { \ - start.end = finish.item; return start; \ - } \ STC_INLINE void \ carray##D##X##_next(carray##D##X##_iter_t* it) {++it->item;} \ \ @@ -188,5 +184,5 @@ STC_INLINE size_t _carray3_size(const size_t* zdim) { } \ typedef Value carray1##X##_value_t; \ typedef carray1##X##_value_t carray2##X##_value_t, carray3##X##_value_t - + #endif diff --git a/stc/cbitset.h b/stc/cbitset.h index a3268f23..93e164bf 100644 --- a/stc/cbitset.h +++ b/stc/cbitset.h @@ -148,7 +148,7 @@ STC_INLINE cbitset_t cbitset_not(cbitset_t s1) { typedef struct { cbitset_t *_bs; - size_t item, end; + size_t item; } cbitset_iter_t; STC_INLINE cbitset_iter_t @@ -159,14 +159,10 @@ STC_INLINE cbitset_iter_t cbitset_end(cbitset_t* self) { cbitset_iter_t it = {self, self->size}; return it; } -STC_INLINE cbitset_iter_t -cbitset_range(cbitset_iter_t start, cbitset_iter_t finish) { - start.end = finish.item; return start; -} STC_INLINE void cbitset_next(cbitset_iter_t* it) {++it->item;} -STC_INLINE bool cbitset_itval(cbitset_iter_t it) { +STC_INLINE bool cbitset_itval(cbitset_iter_t it) { return cbitset_test(*it._bs, it.item); } @@ -224,4 +220,4 @@ STC_API bool cbitset_is_subset(cbitset_t s, cbitset_t other) { _cbitset_SETOP(|) STC_API bool cbitset_is_superset(cbitset_t s, cbitset_t other) { _cbitset_SETOP(&); } #endif -#endif \ No newline at end of file +#endif diff --git a/stc/cdefs.h b/stc/cdefs.h index 2414f60c..c3e24c64 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -76,14 +76,14 @@ #define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__) #define c_foreach_3(it, ctype, cnt) \ - for (ctype##_iter_t it = ctype##_range(ctype##_begin(&cnt), ctype##_end(&cnt)); it.item != it.end; ctype##_next(&it)) + for (ctype##_iter_t it = ctype##_begin(&cnt), it##_end_ = ctype##_end(&cnt); it.item != it##_end_.item; ctype##_next(&it)) #define c_foreach_4(it, ctype, start, finish) \ - for (ctype##_iter_t it = ctype##_range(start, finish); it.item != it.end; ctype##_next(&it)) + for (ctype##_iter_t it = start, it##_end_ = finish; it.item != it##_end_.item; ctype##_next(&it)) #define c_items(...) __VA_ARGS__ -#define c_push(container_ptr, ctype, items) do { \ +#define c_push(cnt_ptr, ctype, items) do { \ const ctype##_input_t __arr[] = { items }; \ - ctype##_push_n(container_ptr, __arr, sizeof(__arr)/sizeof(__arr[0])); \ + ctype##_push_n(cnt_ptr, __arr, sizeof(__arr)/sizeof(__arr[0])); \ } while (0) #define c_init(ctype, cnt, items) \ diff --git a/stc/clist.h b/stc/clist.h index 8a196b7b..b970e93d 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -27,23 +27,23 @@ #include "cdefs.h" /* Circular Singly-linked Lists. - + This implements a std::forward_list-like class in C, but because it is circular, it also support push* and splice* at both ends of the list. This makes it ideal for being used as a queue, unlike std::forward_list. Basic usage is similar to cvec: - + #include #include #include declare_clist(ix, int64_t); - + int main() { clist_ix list = clist_ini; crand_rng32_t pcg = crand_rng32_init(12345); int n; for (int i=0; i<1000000; ++i) // one million clist_ix_push_back(&list, crand_i32(&pcg)); - n = 0; + n = 0; c_foreach (i, clist_ix, list) if (++n % 10000 == 0) printf("%8d: %10zd\n", n, i.item->value); // Sort them... @@ -140,10 +140,6 @@ STC_API size_t _clist_size(const clist_void* self); clist_##X##_end(clist_##X* self) { \ clist_##X##_iter_t it = {NULL, NULL, &self->last}; return it; \ } \ - STC_INLINE clist_##X##_iter_t \ - clist_##X##_range(clist_##X##_iter_t start, clist_##X##_iter_t finish) { \ - start.end = finish.item; return start; \ - } \ STC_INLINE void \ clist_##X##_next(clist_##X##_iter_t* it) { \ it->item = (it->item == *it->_last) ? NULL : it->item->next; \ @@ -190,7 +186,7 @@ STC_API size_t _clist_size(const clist_void* self); \ implement_clist_7(X, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) - + /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) @@ -323,7 +319,7 @@ _clist_mergesort(clist_void_node_t *list, int (*cmp)(const void*, const void*)) clist_void_node_t *p, *q, *e, *tail, *oldhead; int insize = 1, nmerges, psize, qsize, i; if (!list) return NULL; - + while (1) { p = list; oldhead = list; diff --git a/stc/cmap.h b/stc/cmap.h index 1ba079c2..910a091b 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -184,7 +184,7 @@ typedef struct ctype##_##X { \ } ctype##_##X; \ \ typedef struct { \ - ctype##_##X##_entry_t *item, *end; \ + ctype##_##X##_entry_t *item; \ uint8_t* _hx; \ } ctype##_##X##_iter_t; \ \ @@ -257,21 +257,17 @@ ctype##_##X##_erase(ctype##_##X* self, ctype##_##X##_rawkey_t rawKey); \ \ STC_INLINE ctype##_##X##_iter_t \ ctype##_##X##_begin(ctype##_##X* self) { \ - ctype##_##X##_iter_t it = {self->table, self->table + self->bucket_count, self->_hashx}; \ - while (it.item != it.end && *it._hx == 0) ++it.item, ++it._hx; \ + ctype##_##X##_iter_t it = {self->table, self->_hashx}; \ + if (it._hx) while (*it._hx == 0) ++it.item, ++it._hx; \ return it; \ } \ STC_INLINE ctype##_##X##_iter_t \ ctype##_##X##_end(ctype##_##X* self) {\ - ctype##_##X##_iter_t it = {self->table + self->bucket_count, NULL, NULL}; return it; \ -} \ -STC_INLINE ctype##_##X##_iter_t \ -ctype##_##X##_range(ctype##_##X##_iter_t start, ctype##_##X##_iter_t finish) {\ - start.end = finish.item; return start; \ + ctype##_##X##_iter_t it = {self->table + self->bucket_count}; return it; \ } \ STC_INLINE void \ ctype##_##X##_next(ctype##_##X##_iter_t* it) { \ - while (++it->item != it->end && *++it->_hx == 0) ; \ + while ((++it->item, *++it->_hx == 0)) ; \ } \ CMAP_ONLY_##ctype( STC_INLINE ctype##_##X##_value_t* \ ctype##_##X##_itval(ctype##_##X##_iter_t it) {return &it.item->value;} ) \ diff --git a/stc/cqueue.h b/stc/cqueue.h index a9efa1b6..33e2da88 100644 --- a/stc/cqueue.h +++ b/stc/cqueue.h @@ -95,8 +95,6 @@ STC_INLINE cqueue_##X##_iter_t \ cqueue_##X##_begin(cqueue_##X* self) {return ctype##_begin(self);} \ STC_INLINE cqueue_##X##_iter_t \ cqueue_##X##_end(cqueue_##X* self) {return ctype##_end(self);} \ -STC_INLINE cqueue_##X##_iter_t \ -cqueue_##X##_range(cqueue_##X##_iter_t s, cqueue_##X##_iter_t f) {return ctype##_range(s, f);} \ STC_INLINE void \ cqueue_##X##_next(cqueue_##X##_iter_t* it) {ctype##_next(it);} \ STC_INLINE cqueue_##X##_value_t* \ diff --git a/stc/cstack.h b/stc/cstack.h index a56d99af..6ed6ccb3 100644 --- a/stc/cstack.h +++ b/stc/cstack.h @@ -34,10 +34,10 @@ for (int i=0; i<100; ++i) cstack_i_push(&stack, i*i); - + for (int i=0; i<90; ++i) cstack_i_pop(&stack); - + printf("top: %d\n", *cstack_i_top(&stack)); } */ @@ -82,8 +82,6 @@ STC_INLINE cstack_##X##_iter_t \ cstack_##X##_begin(cstack_##X* self) {return ctype##_begin(self);} \ STC_INLINE cstack_##X##_iter_t \ cstack_##X##_end(cstack_##X* self) {return ctype##_end(self);} \ -STC_INLINE cstack_##X##_iter_t \ -cstack_##X##_range(cstack_##X##_iter_t s, cstack_##X##_iter_t f) {return ctype##_range(s, f);} \ STC_INLINE void \ cstack_##X##_next(cstack_##X##_iter_t* it) {ctype##_next(it);} \ STC_INLINE cstack_##X##_value_t* \ diff --git a/stc/cstr.h b/stc/cstr.h index 8a484be4..944b164d 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -29,12 +29,8 @@ #include /* vsnprintf */ #include "cdefs.h" -typedef struct cstr { - char* str; -} cstr_t; -typedef struct { - char *item, *end; -} cstr_iter_t; +typedef struct cstr { char* str; } cstr_t; +typedef struct { char *item; } cstr_iter_t; typedef char cstr_value_t, cstr_rawvalue_t, cstr_input_t; static size_t _cstr_nullrep[] = {0, 0, 0}; @@ -120,11 +116,6 @@ STC_INLINE cstr_iter_t cstr_end(cstr_t* self) { cstr_iter_t it = {self->str + cstr_size(*self)}; return it; } -STC_INLINE cstr_iter_t -cstr_range(cstr_iter_t start, cstr_iter_t finish) { - start.end = finish.item; return start; -} - STC_INLINE void cstr_next(cstr_iter_t* it) { ++it->item; } STC_INLINE char* cstr_itval(cstr_iter_t it) {return it.item;} @@ -208,7 +199,7 @@ cstr_find(const cstr_t* s, const char* needle) { STC_INLINE uint32_t c_string_hash(const char* str) { uint32_t hash = 5381, c; /* djb2 */ while ((c = *str++)) hash = ((hash << 5) + hash) ^ c; - return hash; + return hash; } STC_INLINE uint32_t cstr_hash_raw(const char* const* spp, size_t ignored) { @@ -243,7 +234,7 @@ cstr_make_n(const char* str, size_t len) { if (len == 0) return cstr_ini; size_t *rep = (size_t *) malloc(_cstr_mem(len)); cstr_t s = {(char *) memcpy(rep + 2, str, len)}; - s.str[rep[0] = len] = '\0'; + s.str[rep[0] = len] = '\0'; rep[1] = _cstr_cap(len); return s; } diff --git a/stc/cvec.h b/stc/cvec.h index 78f913a0..3d35af04 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -130,9 +130,7 @@ cvec_##X##_sort_with(cvec_##X* self, int(*cmp)(const Value*, const Value*)) { \ qsort(self->data, cvec_size(*self), sizeof(Value), (_cvec_cmp) cmp); \ } \ \ -typedef struct { \ - Value *item, *end; \ -} cvec_##X##_iter_t; \ +typedef struct { Value *item; } cvec_##X##_iter_t; \ \ STC_INLINE cvec_##X##_iter_t \ cvec_##X##_begin(cvec_##X* self) { \ @@ -142,10 +140,6 @@ STC_INLINE cvec_##X##_iter_t \ cvec_##X##_end(cvec_##X* self) { \ cvec_##X##_iter_t it = {self->data + cvec_size(*self)}; return it; \ } \ -STC_INLINE cvec_##X##_iter_t \ -cvec_##X##_range(cvec_##X##_iter_t start, cvec_##X##_iter_t finish) { \ - start.end = finish.item; return start; \ -} \ STC_INLINE void \ cvec_##X##_next(cvec_##X##_iter_t* it) {++it->item;} \ STC_INLINE cvec_##X##_value_t* \ -- cgit v1.2.3