From 1051f059d47d30e4b8c18608c62c62920f51bc58 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 3 Sep 2020 11:10:41 +0200 Subject: Added itval() to all containers to allow for generalized access to iterator values. --- stc/clist.h | 11 +++++++---- stc/cmap.h | 21 ++++++++++++--------- stc/cprique.h | 4 ++-- stc/cqueue.h | 9 +++++++++ stc/cstack.h | 9 +++++++++ stc/cstr.h | 10 +++++++--- stc/cvec.h | 9 ++++++--- 7 files changed, 52 insertions(+), 21 deletions(-) diff --git a/stc/clist.h b/stc/clist.h index 1415bcd5..bbf05201 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -92,6 +92,7 @@ STC_API size_t _clist_size(const clist_void* self); #define declare_clist_7(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) \ \ declare_clist_types(tag, Value); \ + typedef Value clist_##tag##_value_t; \ typedef RawValue clist_##tag##_rawvalue_t; \ typedef clist_##tag##_rawvalue_t clist_##tag##_input_t; \ \ @@ -144,6 +145,8 @@ STC_API size_t _clist_size(const clist_void* self); it->end = it->item == *it->_last ? it->item->next : NULL; \ it->item = it->item->next; \ } \ + STC_INLINE clist_##tag##_value_t* \ + clist_##tag##_itval(clist_##tag##_iter_t* it) {return &it->item->value;} \ \ STC_API clist_##tag##_iter_t \ clist_##tag##_insert_after_v(clist_##tag* self, clist_##tag##_iter_t pos, Value value); \ @@ -181,8 +184,7 @@ STC_API size_t _clist_size(const clist_void* self); STC_INLINE Value* \ clist_##tag##_back(clist_##tag* self) {return &self->last->value;} \ \ - implement_clist_7(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) \ - typedef Value clist_##tag##_value_t + implement_clist_7(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) /* -------------------------- IMPLEMENTATION ------------------------- */ @@ -265,7 +267,9 @@ STC_API size_t _clist_size(const clist_void* self); clist_##tag##_sort(clist_##tag* self) { \ clist_void_node_t* last = _clist_mergesort((clist_void_node_t *) self->last->next, clist_##tag##_sort_compare); \ self->last = (clist_##tag##_node_t *) last; \ - } + } \ + typedef int clist_##tag##_dud + #define _clist_insert_after(self, tag, node, val) \ clist_##tag##_node_t *entry = c_new (clist_##tag##_node_t), \ @@ -306,7 +310,6 @@ _clist_size(const clist_void* self) { return n; } - /* Singly linked list Mergesort implementation by Simon Tatham. O(n*log n). * https://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html */ diff --git a/stc/cmap.h b/stc/cmap.h index 6aa40565..bffa19c0 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -162,6 +162,8 @@ typedef struct { \ CMAP_ONLY_##ctype(RawValue value;) \ } ctype##_##tag##_input_t; \ \ +typedef Key ctype##_##tag##_key_t; \ +typedef Value ctype##_##tag##_value_t; \ typedef RawKey ctype##_##tag##_rawkey_t; \ typedef RawValue ctype##_##tag##_rawvalue_t; \ \ @@ -237,31 +239,31 @@ ctype##_##tag##_erase_entry(ctype##_##tag* self, ctype##_##tag##_entry_t* entry) STC_API bool \ ctype##_##tag##_erase(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey); \ \ -STC_FORCE_INLINE ctype##_##tag##_iter_t \ -ctype##_##tag##_begin(ctype##_##tag* map) { \ - ctype##_##tag##_iter_t it = {map->table, map->table + map->bucket_count, map->_hashx}; \ +STC_INLINE ctype##_##tag##_iter_t \ +ctype##_##tag##_begin(ctype##_##tag* self) { \ + ctype##_##tag##_iter_t it = {self->table, self->table + self->bucket_count, self->_hashx}; \ if (it._hx) while (*it._hx == 0) ++it.item, ++it._hx; \ return it; \ } \ -STC_FORCE_INLINE void \ +STC_INLINE void \ ctype##_##tag##_next(ctype##_##tag##_iter_t* it) { \ while ((++it->item, *++it->_hx == 0)) ; \ } \ +CMAP_ONLY_##ctype( STC_INLINE ctype##_##tag##_value_t* \ +ctype##_##tag##_itval(ctype##_##tag##_iter_t* it) {return &it->item->value;} ) \ \ STC_API uint32_t c_default_hash16(const void *data, size_t len); \ STC_API uint32_t c_default_hash32(const void* data, size_t len); \ \ implement_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ - keyDestroy, RawKey, keyToRaw, keyFromRaw, RawValue, valueFromRaw) \ -typedef Key ctype##_##tag##_key_t; \ -typedef Value ctype##_##tag##_value_t + keyDestroy, RawKey, keyToRaw, keyFromRaw, RawValue, valueFromRaw) /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) #define implement_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyToRaw, keyFromRaw, RawValue, valueFromRaw) \ - STC_API ctype##_##tag \ +STC_API ctype##_##tag \ ctype##_##tag##_with_capacity(size_t cap) { \ ctype##_##tag h = ctype##_init; \ ctype##_##tag##_reserve(&h, cap); \ @@ -396,7 +398,8 @@ ctype##_##tag##_erase(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey) { \ uint32_t hx; \ size_t i = ctype##_##tag##_bucket(self, &rawKey, &hx); \ return ctype##_##tag##_erase_entry(self, self->table + i); \ -} +} \ +typedef int ctype##_##tag##_dud /* https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/ */ diff --git a/stc/cprique.h b/stc/cprique.h index 7a303152..92a1f8c2 100644 --- a/stc/cprique.h +++ b/stc/cprique.h @@ -45,8 +45,8 @@ } */ -#ifndef CPQUEUE__H__ -#define CPQUEUE__H__ +#ifndef CPRIQUE__H__ +#define CPRIQUE__H__ #include "cvec.h" diff --git a/stc/cqueue.h b/stc/cqueue.h index 2cdfb753..b69092d6 100644 --- a/stc/cqueue.h +++ b/stc/cqueue.h @@ -90,6 +90,15 @@ STC_API void \ cqueue_##tag##_push_n(cqueue_##tag *self, const cqueue_##tag##_input_t in[], size_t size) { \ ctype##_push_n(self, in, size); \ } \ + \ +typedef ctype##_iter_t cqueue_##tag##_iter_t; \ +STC_INLINE cqueue_##tag##_iter_t \ +cqueue_##tag##_begin(cqueue_##tag* self) {return ctype##_begin(self);} \ +STC_INLINE void \ +cqueue_##tag##_next(cqueue_##tag##_iter_t* it) {ctype##_next(it);} \ +STC_INLINE cqueue_##tag##_value_t* \ +cqueue_##tag##_itval(cqueue_##tag##_iter_t* it) {return ctype##_itval(it);} \ + \ typedef int cqueue_##tag##_dud #endif diff --git a/stc/cstack.h b/stc/cstack.h index 8c806523..1f8c8a4f 100644 --- a/stc/cstack.h +++ b/stc/cstack.h @@ -77,6 +77,15 @@ STC_API void \ cstack_##tag##_push_n(cstack_##tag *self, const cstack_##tag##_input_t in[], size_t size) { \ ctype##_push_n(self, in, size); \ } \ + \ +typedef ctype##_iter_t cstack_##tag##_iter_t; \ +STC_INLINE cstack_##tag##_iter_t \ +cstack_##tag##_begin(cstack_##tag* self) {return ctype##_begin(self);} \ +STC_INLINE void \ +cstack_##tag##_next(cstack_##tag##_iter_t* it) {ctype##_next(it);} \ +STC_INLINE cstack_##tag##_value_t* \ +cstack_##tag##_itval(cstack_##tag##_iter_t* it) {return ctype##_itval(it);} \ + \ typedef int cstack_##tag##_dud #endif diff --git a/stc/cstr.h b/stc/cstr.h index bc8e485d..734087dc 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -29,13 +29,13 @@ #include /* vsnprintf */ #include "cdefs.h" - typedef struct cstr { char* str; } cstr_t; typedef struct { char *item, *end; } cstr_iter_t; +typedef char cstr_value_t, cstr_rawvalue_t, cstr_input_t; static size_t _cstr_nullrep[] = {0, 0, 0}; @@ -110,8 +110,8 @@ STC_INLINE cstr_iter_t cstr_begin(cstr_t* self) { cstr_iter_t it = {self->str, self->str + cstr_size(*self)}; return it; } -STC_INLINE void -cstr_next(cstr_iter_t* it) { ++it->item; } +STC_INLINE void cstr_next(cstr_iter_t* it) { ++it->item; } +STC_INLINE char* cstr_itval(cstr_iter_t* it) {return it->item;} STC_INLINE cstr_t* cstr_assign(cstr_t* self, const char* str) { @@ -136,6 +136,10 @@ STC_INLINE cstr_t* cstr_append(cstr_t* self, const char* str) { return cstr_append_n(self, str, strlen(str)); } +/*STC_INLINE void +cstr_push_n(cstr_t* self, const cstr_input_t[] in, size_t n) { + cstr_append_n(self, in, n); +}*/ STC_INLINE cstr_t* cstr_push_back(cstr_t* self, char value) { return cstr_append_n(self, &value, 1); diff --git a/stc/cvec.h b/stc/cvec.h index d5b00ded..c94757d8 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -50,6 +50,7 @@ typedef struct cvec_##tag { \ Value* data; \ } cvec_##tag; \ +typedef Value cvec_##tag##_value_t; \ typedef RawValue cvec_##tag##_rawvalue_t; \ typedef cvec_##tag##_rawvalue_t cvec_##tag##_input_t; \ \ @@ -145,9 +146,10 @@ cvec_##tag##_begin(cvec_##tag* vec) { \ } \ STC_INLINE void \ cvec_##tag##_next(cvec_##tag##_iter_t* it) { ++it->item; } \ +STC_INLINE cvec_##tag##_value_t* \ +cvec_##tag##_itval(cvec_##tag##_iter_t* it) {return it->item;} \ \ -implement_cvec_7(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) \ -typedef Value cvec_##tag##_value_t +implement_cvec_7(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) /* -------------------------- IMPLEMENTATION ------------------------- */ @@ -231,7 +233,8 @@ cvec_##tag##_value_compare(const Value* x, const Value* y) { \ RawValue rx = valueToRaw(x); \ RawValue ry = valueToRaw(y); \ return valueCompareRaw(&rx, &ry); \ -} +} \ +typedef int cvec_##taq##_dud #else #define implement_cvec_7(tag, Value, valueDestroy, valueCompareRaw, RawValue, valueToRaw, valueFromRaw) -- cgit v1.2.3