From 4da2cdc45522626d005a0221a73064f0217b69fe Mon Sep 17 00:00:00 2001 From: tylo Date: Wed, 11 Mar 2020 11:50:00 +0100 Subject: New API: c_ --- benchmark.cpp | 18 +++--- cdefs.h | 36 +++++------ cmap.h | 200 +++++++++++++++++++++++++++++----------------------------- cmap_test.c | 98 ++++++++++++++-------------- cstring.h | 184 ++++++++++++++++++++++++++--------------------------- cvector.h | 112 ++++++++++++++++---------------- 6 files changed, 324 insertions(+), 324 deletions(-) diff --git a/benchmark.cpp b/benchmark.cpp index 30bf9d2c..b6a2f472 100644 --- a/benchmark.cpp +++ b/benchmark.cpp @@ -1,17 +1,17 @@ #include #include #include -#include "cstring.h" -#include "cmap.h" +#include "c_string.h" +#include "c_hashmap_.h" #include -declare_CMap(ii, int, int); -declare_CStringVector(s); +c_declare_Hashmap(ii, int, int); +declare_c_StringVector(s); int main() { - CMap_ii map = cmap_initializer; + c_Hashmap_ii map = c_hashmap_initializer; uint64_t checksum = 0; clock_t before, difference; size_t fib1, fib2, fibx; @@ -19,15 +19,15 @@ int main() const size_t N = 10000000; printf("Starting\n"); - //cmap_ii_reserve(&map, N * 1.25); + //c_hashmap_ii_reserve(&map, N * 1.25); before = clock(); fib1 = 0, fib2 = 1; for (size_t i = 0; i < N; ++i) { - checksum += ++cmap_ii_put(&map, FIBONACCI_NEXT, i)->value; + checksum += ++c_hashmap_ii_put(&map, FIBONACCI_NEXT, i)->value; } difference = clock() - before; - printf("Check: %llu, size: %llu, time: %f\n", checksum, cmap_size(map), 1.0 * difference / CLOCKS_PER_SEC); - cmap_ii_destroy(&map); + printf("Check: %llu, size: %llu, time: %f\n", checksum, c_hashmap_size(map), 1.0 * difference / CLOCKS_PER_SEC); + c_hashmap_ii_destroy(&map); std::unordered_map map2; //map2.reserve(N); diff --git a/cdefs.h b/cdefs.h index 6fb10410..3769c358 100644 --- a/cdefs.h +++ b/cdefs.h @@ -20,37 +20,37 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#ifndef CDEFS__H__ -#define CDEFS__H__ +#ifndef C_DEFS__H__ +#define C_DEFS__H__ #include #include // Macro overloading feature support: https://rextester.com/ONP80107 -#define cdefs_CAT( A, B ) A ## B -#define cdefs_EXPAND(...) __VA_ARGS__ -#define cdefs_VA_ARG_SIZE(...) cdefs_EXPAND(cdefs_APPLY_ARG_N((__VA_ARGS__, cdefs_RSEQ_N))) -#define cdefs_APPLY_ARG_N(ARGS) cdefs_EXPAND(cdefs_ARG_N ARGS) -#define cdefs_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N,...) N -#define cdefs_RSEQ_N 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 -#define cdefs_OVERLOAD_SELECT(NAME, NUM) cdefs_CAT( NAME ## _, NUM) +#define c_defs_CAT( A, B ) A ## B +#define c_defs_EXPAND(...) __VA_ARGS__ +#define c_defs_VA_ARG_SIZE(...) c_defs_EXPAND(c_defs_APPLY_ARG_N((__VA_ARGS__, c_defs_RSEQ_N))) +#define c_defs_APPLY_ARG_N(ARGS) c_defs_EXPAND(c_defs_ARG_N ARGS) +#define c_defs_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N,...) N +#define c_defs_RSEQ_N 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 +#define c_defs_OVERLOAD_SELECT(NAME, NUM) c_defs_CAT( NAME ## _, NUM) -#define cdefs_MACRO_OVERLOAD(NAME, ...) cdefs_OVERLOAD_SELECT(NAME, cdefs_VA_ARG_SIZE(__VA_ARGS__))(__VA_ARGS__) -// #define foo(...) cdefs_MACRO_OVERLOAD(foo, __VA_ARGS__) +#define c_defs_MACRO_OVERLOAD(NAME, ...) c_defs_OVERLOAD_SELECT(NAME, c_defs_VA_ARG_SIZE(__VA_ARGS__))(__VA_ARGS__) +// #define foo(...) c_defs_MACRO_OVERLOAD(foo, __VA_ARGS__) // #define foo_1(X) foo_2(X, 100) // #define foo_2(X, Y) X + Y -#define cdefs_max_alloca (1000) -#define cdefs_swap(T, x, y) { T __t = x; x = y; y = __t; } +#define c_defs_max_alloca (1000) +#define c_defs_swap(T, x, y) { T __t = x; x = y; y = __t; } -#define cdefs_initRaw(x) (x) -#define cdefs_getRaw(x) (x) -static inline void cdefs_destroy(void* value) {} +#define c_defs_initRaw(x) (x) +#define c_defs_getRaw(x) (x) +static inline void c_defs_destroy(void* value) {} -#define cforeach(it, ctag, con) \ +#define c_foreach(it, ctag, con) \ for (ctag##_iter_t it = ctag##_begin(con); it.item != ctag##_end(con).item; it = ctag##_next(it)) -static inline uint32_t cdefs_murmurHash(const void *data, size_t len) { // One-at-a-time 32bit +static inline uint32_t c_defs_murmurHash(const void *data, size_t len) { // One-at-a-time 32bit const unsigned char *key = (const unsigned char *) data; uint32_t h = 0xC613FC15; // ‭0x749E3E6989DF617‬; 64bit while (len--) { diff --git a/cmap.h b/cmap.h index 542f7c87..20804882 100644 --- a/cmap.h +++ b/cmap.h @@ -20,122 +20,122 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#ifndef CMAP_H_ -#define CMAP_H_ +#ifndef C_HASHMAP_H_ +#define C_HASHMAP_H_ -#include "cvector.h" +#include "c_vector.h" -#define cmap_initializer {cvector_initializer, 0, 0.8f} -#define cmap_size(cm) ((size_t) (cm)._size) -#define cmap_buckets(cm) cvector_capacity((cm)._vec) +#define c_hashmap_initializer {c_vector_initializer, 0, 0.8f} +#define c_hashmap_size(cm) ((size_t) (cm)._size) +#define c_hashmap_buckets(cm) c_vector_capacity((cm)._vec) -// CMapEntry: -enum { CMapEntry_VACANT = 0, - CMapEntry_INUSE = 1, - CMapEntry_ERASED = 2 +// c_HashmapEntry: +enum { c_HashmapEntry_VACANT = 0, + c_HashmapEntry_INUSE = 1, + c_HashmapEntry_ERASED = 2 }; -#define declare_CMapEntry(tag, Key, Value, keyDestroy, valueDestroy) \ -struct CMapEntry_##tag { \ +#define c_declare_HashmapEntry(tag, Key, Value, keyDestroy, valueDestroy) \ +struct c_HashmapEntry_##tag { \ Key key; \ Value value; \ uint8_t state, changed; \ }; \ \ -static inline void cmapentry_##tag##_destroy(struct CMapEntry_##tag* e) { \ +static inline void cmapentry_##tag##_destroy(struct c_HashmapEntry_##tag* e) { \ keyDestroy(&e->key); \ valueDestroy(&e->value); \ - e->state = CMapEntry_VACANT; \ + e->state = c_HashmapEntry_VACANT; \ } \ -typedef struct CMapEntry_##tag CMapEntry_##tag +typedef struct c_HashmapEntry_##tag c_HashmapEntry_##tag -// CMap: -#define declare_CMap(...) cdefs_MACRO_OVERLOAD(declare_CMap, __VA_ARGS__) +// c_Hashmap: +#define c_declare_Hashmap(...) c_defs_MACRO_OVERLOAD(c_declare_Hashmap, __VA_ARGS__) -#define declare_CMap_3(tag, Key, Value) \ - declare_CMap_4(tag, Key, Value, cdefs_destroy) +#define c_declare_Hashmap_3(tag, Key, Value) \ + c_declare_Hashmap_4(tag, Key, Value, c_defs_destroy) -#define declare_CMap_4(tag, Key, Value, valueDestroy) \ - declare_CMap_10(tag, Key, Value, valueDestroy, Key, memcmp, cdefs_murmurHash, cdefs_initRaw, cdefs_getRaw, cdefs_destroy) +#define c_declare_Hashmap_4(tag, Key, Value, valueDestroy) \ + c_declare_Hashmap_10(tag, Key, Value, valueDestroy, Key, memcmp, c_defs_murmurHash, c_defs_initRaw, c_defs_getRaw, c_defs_destroy) -// CMap: -#define declare_CMap_StringKey(...) cdefs_MACRO_OVERLOAD(declare_CMap_StringKey, __VA_ARGS__) +// c_Hashmap: +#define c_declare_Hashmap_stringkey(...) c_defs_MACRO_OVERLOAD(c_declare_Hashmap_stringkey, __VA_ARGS__) -#define declare_CMap_StringKey_2(tag, Value) \ - declare_CMap_StringKey_3(tag, Value, cdefs_destroy) +#define c_declare_Hashmap_stringkey_2(tag, Value) \ + c_declare_Hashmap_stringkey_3(tag, Value, c_defs_destroy) -#define declare_CMap_StringKey_3(tag, Value, valueDestroy) \ - declare_CMap_10(tag, CString, Value, valueDestroy, const char*, cstring_compareRaw, cstring_hashRaw, cstring_make, cstring_getRaw, cstring_destroy) +#define c_declare_Hashmap_stringkey_3(tag, Value, valueDestroy) \ + c_declare_Hashmap_10(tag, CString, Value, valueDestroy, const char*, cstring_compareRaw, cstring_hashRaw, cstring_make, cstring_getRaw, cstring_destroy) -// CMap full: -#define declare_CMap_10(tag, Key, Value, valueDestroy, KeyRaw, keyCompareRaw, keyHashRaw, keyInitRaw, keyGetRaw, keyDestroy) \ - declare_CMapEntry(tag, Key, Value, keyDestroy, valueDestroy); \ - declare_CVector_3(map_##tag, CMapEntry_##tag, cmapentry_##tag##_destroy); \ +// c_Hashmap full: +#define c_declare_Hashmap_10(tag, Key, Value, valueDestroy, KeyRaw, keyCompareRaw, keyHashRaw, keyInitRaw, keyGetRaw, keyDestroy) \ + c_declare_HashmapEntry(tag, Key, Value, keyDestroy, valueDestroy); \ + c_declare_Vector_3(map_##tag, c_HashmapEntry_##tag, cmapentry_##tag##_destroy); \ \ -typedef struct CMap_##tag { \ - CVector_map_##tag _vec; \ +typedef struct c_Hashmap_##tag { \ + c_Vector_map_##tag _vec; \ size_t _size; \ float maxLoadFactor; \ -} CMap_##tag; \ +} c_Hashmap_##tag; \ \ -typedef struct cmap_##tag##_iter_t { \ - CMapEntry_##tag *item, *_end; \ -} cmap_##tag##_iter_t; \ +typedef struct c_hashmap_##tag##_iter_t { \ + c_HashmapEntry_##tag *item, *_end; \ +} c_hashmap_##tag##_iter_t; \ \ -static inline CMap_##tag cmap_##tag##_init(void) { \ - CMap_##tag map = cmap_initializer; \ +static inline c_Hashmap_##tag c_hashmap_##tag##_init(void) { \ + c_Hashmap_##tag map = c_hashmap_initializer; \ return map; \ } \ \ -static inline void cmap_##tag##_destroy(CMap_##tag* self) { \ - if (cmap_size(*self)) { \ - size_t cap = _cvector_capacity(self->_vec); \ - CMapEntry_##tag* e = self->_vec.data, *end = e + cap; \ - for (; e != end; ++e) if (e->state == CMapEntry_INUSE) cmapentry_##tag##_destroy(e); \ +static inline void c_hashmap_##tag##_destroy(c_Hashmap_##tag* self) { \ + if (c_hashmap_size(*self)) { \ + size_t cap = _c_vector_capacity(self->_vec); \ + c_HashmapEntry_##tag* e = self->_vec.data, *end = e + cap; \ + for (; e != end; ++e) if (e->state == c_HashmapEntry_INUSE) cmapentry_##tag##_destroy(e); \ } \ - cvector_map_##tag##_destroy(&self->_vec); \ + c_vector_map_##tag##_destroy(&self->_vec); \ } \ \ -static inline size_t cmap_##tag##_reserve(CMap_##tag* self, size_t size); /* predeclared */ \ +static inline size_t c_hashmap_##tag##_reserve(c_Hashmap_##tag* self, size_t size); /* predeclared */ \ \ -static inline void cmap_##tag##_clear(CMap_##tag* self) { \ - CMap_##tag cm = cmap_initializer; \ - cmap_##tag##_destroy(self); \ +static inline void c_hashmap_##tag##_clear(c_Hashmap_##tag* self) { \ + c_Hashmap_##tag cm = c_hashmap_initializer; \ + c_hashmap_##tag##_destroy(self); \ *self = cm; \ } \ \ -static inline void cmap_##tag##_swap(CMap_##tag* a, CMap_##tag* b) { \ - cvector_map_##tag##_swap(&a->_vec, &b->_vec); \ - cdefs_swap(size_t, a->_size, b->_size); \ +static inline void c_hashmap_##tag##_swap(c_Hashmap_##tag* a, c_Hashmap_##tag* b) { \ + c_vector_map_##tag##_swap(&a->_vec, &b->_vec); \ + c_defs_swap(size_t, a->_size, b->_size); \ } \ \ -static inline void cmap_##tag##_setMaxLoadFactor(CMap_##tag* self, float fac) { \ +static inline void c_hashmap_##tag##_setMaxLoadFactor(c_Hashmap_##tag* self, float fac) { \ self->maxLoadFactor = fac; \ - if (cmap_size(*self) > cmap_buckets(*self) * fac) \ - cmap_##tag##_reserve(self, 1 + (size_t) (cmap_size(*self) / fac)); \ + if (c_hashmap_size(*self) > c_hashmap_buckets(*self) * fac) \ + c_hashmap_##tag##_reserve(self, 1 + (size_t) (c_hashmap_size(*self) / fac)); \ } \ \ -static inline size_t cmap_##tag##_bucket(CMap_##tag cm, KeyRaw rawKey) { \ - size_t cap = cvector_capacity(cm._vec); \ - size_t idx = cmap_reduce(keyHashRaw(&rawKey, sizeof(Key)), cap); \ +static inline size_t c_hashmap_##tag##_bucket(c_Hashmap_##tag cm, KeyRaw rawKey) { \ + size_t cap = c_vector_capacity(cm._vec); \ + size_t idx = c_hashmap_reduce(keyHashRaw(&rawKey, sizeof(Key)), cap); \ size_t first = idx, erased_idx = cap; \ FIBONACCI_DECL; \ do { \ switch (cm._vec.data[idx].state) { \ - case CMapEntry_VACANT: \ + case c_HashmapEntry_VACANT: \ return erased_idx != cap ? erased_idx : idx; \ - case CMapEntry_INUSE: \ + case c_HashmapEntry_INUSE: \ if (keyCompareRaw(&cm._vec.data[idx].key, &rawKey, sizeof(Key)) != 0) \ break; \ if (erased_idx != cap) { \ - cdefs_swap(CMapEntry_##tag, cm._vec.data[erased_idx], cm._vec.data[idx]); \ + c_defs_swap(c_HashmapEntry_##tag, cm._vec.data[erased_idx], cm._vec.data[idx]); \ return erased_idx; \ } \ return idx; \ - case CMapEntry_ERASED: \ + case c_HashmapEntry_ERASED: \ if (erased_idx == cap) erased_idx = idx; \ break; \ } \ @@ -144,80 +144,80 @@ static inline size_t cmap_##tag##_bucket(CMap_##tag cm, KeyRaw rawKey) { \ } while (1); \ } \ \ -static inline CMapEntry_##tag* cmap_##tag##_get(CMap_##tag cm, KeyRaw rawKey) { \ - if (cmap_size(cm) == 0) return NULL; \ - size_t idx = cmap_##tag##_bucket(cm, rawKey); \ - return cm._vec.data[idx].state == CMapEntry_INUSE ? &cm._vec.data[idx] : NULL; \ +static inline c_HashmapEntry_##tag* c_hashmap_##tag##_get(c_Hashmap_##tag cm, KeyRaw rawKey) { \ + if (c_hashmap_size(cm) == 0) return NULL; \ + size_t idx = c_hashmap_##tag##_bucket(cm, rawKey); \ + return cm._vec.data[idx].state == c_HashmapEntry_INUSE ? &cm._vec.data[idx] : NULL; \ } \ \ -static inline CMapEntry_##tag* cmap_##tag##_put(CMap_##tag* self, KeyRaw rawKey, Value value) { \ - size_t cap = cvector_capacity(self->_vec); \ - if (cmap_size(*self) >= cap * self->maxLoadFactor) \ - cap = cmap_##tag##_reserve(self, (size_t) (cap * 1.8)); \ - size_t idx = cmap_##tag##_bucket(*self, rawKey); \ - CMapEntry_##tag* e = &self->_vec.data[idx]; \ +static inline c_HashmapEntry_##tag* c_hashmap_##tag##_put(c_Hashmap_##tag* self, KeyRaw rawKey, Value value) { \ + size_t cap = c_vector_capacity(self->_vec); \ + if (c_hashmap_size(*self) >= cap * self->maxLoadFactor) \ + cap = c_hashmap_##tag##_reserve(self, (size_t) (cap * 1.8)); \ + size_t idx = c_hashmap_##tag##_bucket(*self, rawKey); \ + c_HashmapEntry_##tag* e = &self->_vec.data[idx]; \ e->value = value; \ - e->changed = (e->state == CMapEntry_INUSE); \ - if (e->state != CMapEntry_INUSE) { \ + e->changed = (e->state == c_HashmapEntry_INUSE); \ + if (e->state != c_HashmapEntry_INUSE) { \ e->key = keyInitRaw(rawKey); \ - e->state = CMapEntry_INUSE; \ + e->state = c_HashmapEntry_INUSE; \ ++self->_size; \ } \ return e; \ } \ \ -static inline size_t cmap_##tag##_reserve(CMap_##tag* self, size_t size) { \ - size_t oldcap = cvector_capacity(self->_vec), newcap = (oldcap ? 1 : 7) + (size / 2) * 2; \ +static inline size_t c_hashmap_##tag##_reserve(c_Hashmap_##tag* self, size_t size) { \ + size_t oldcap = c_vector_capacity(self->_vec), newcap = (oldcap ? 1 : 7) + (size / 2) * 2; \ if (oldcap >= newcap) return oldcap; \ - CVector_map_##tag vec = cvector_initializer; \ - cvector_map_##tag##_swap(&self->_vec, &vec); \ - cvector_map_##tag##_reserve(&self->_vec, newcap); \ + c_Vector_map_##tag vec = c_vector_initializer; \ + c_vector_map_##tag##_swap(&self->_vec, &vec); \ + c_vector_map_##tag##_reserve(&self->_vec, newcap); \ self->_size = 0; \ - memset(self->_vec.data, 0, sizeof(CMapEntry_##tag) * newcap); \ - CMapEntry_##tag* e = vec.data; \ + memset(self->_vec.data, 0, sizeof(c_HashmapEntry_##tag) * newcap); \ + c_HashmapEntry_##tag* e = vec.data; \ for (size_t i = 0; i < oldcap; ++i, ++e) \ - if (e->state == CMapEntry_INUSE) cmap_##tag##_put(self, keyGetRaw(e->key), e->value); \ + if (e->state == c_HashmapEntry_INUSE) c_hashmap_##tag##_put(self, keyGetRaw(e->key), e->value); \ return newcap; \ } \ \ -static inline bool cmap_##tag##_erase(CMap_##tag* self, KeyRaw rawKey) { \ - CMapEntry_##tag* e = cmap_##tag##_get(*self, rawKey); \ +static inline bool c_hashmap_##tag##_erase(c_Hashmap_##tag* self, KeyRaw rawKey) { \ + c_HashmapEntry_##tag* e = c_hashmap_##tag##_get(*self, rawKey); \ if (e) { \ cmapentry_##tag##_destroy(e); \ - e->state = CMapEntry_ERASED; \ + e->state = c_HashmapEntry_ERASED; \ --self->_size; \ return true; \ } \ return false; \ } \ \ -static inline cmap_##tag##_iter_t cmap_##tag##_begin(CMap_##tag map) { \ - cmap_##tag##_iter_t null = {NULL, NULL}; \ - if (cmap_size(map) == 0) return null; \ - CMapEntry_##tag* e = map._vec.data, *end = e + _cvector_capacity(map._vec); \ - while (e != end && e->state != CMapEntry_INUSE) ++e; \ - cmap_##tag##_iter_t it = {e, end}; return it; \ +static inline c_hashmap_##tag##_iter_t c_hashmap_##tag##_begin(c_Hashmap_##tag map) { \ + c_hashmap_##tag##_iter_t null = {NULL, NULL}; \ + if (c_hashmap_size(map) == 0) return null; \ + c_HashmapEntry_##tag* e = map._vec.data, *end = e + _c_vector_capacity(map._vec); \ + while (e != end && e->state != c_HashmapEntry_INUSE) ++e; \ + c_hashmap_##tag##_iter_t it = {e, end}; return it; \ } \ \ -static inline cmap_##tag##_iter_t cmap_##tag##_next(cmap_##tag##_iter_t it) { \ - do { ++it.item; } while (it.item != it._end && it.item->state != CMapEntry_INUSE); \ +static inline c_hashmap_##tag##_iter_t c_hashmap_##tag##_next(c_hashmap_##tag##_iter_t it) { \ + do { ++it.item; } while (it.item != it._end && it.item->state != c_HashmapEntry_INUSE); \ return it; \ } \ \ -static inline cmap_##tag##_iter_t cmap_##tag##_end(CMap_##tag map) { \ - CMapEntry_##tag* end = (cmap_size(map) == 0) ? NULL : map._vec.data + _cvector_capacity(map._vec); \ - cmap_##tag##_iter_t it = {end, end}; \ +static inline c_hashmap_##tag##_iter_t c_hashmap_##tag##_end(c_Hashmap_##tag map) { \ + c_HashmapEntry_##tag* end = (c_hashmap_size(map) == 0) ? NULL : map._vec.data + _c_vector_capacity(map._vec); \ + c_hashmap_##tag##_iter_t it = {end, end}; \ return it; \ } \ -typedef Key cmap_##tag##_key_t; \ -typedef Value cmap_##tag##_value_t +typedef Key c_hashmap_##tag##_key_t; \ +typedef Value c_hashmap_##tag##_value_t #define FIBONACCI_DECL size_t fib1 = 0, fib2 = 1, fibx #define FIBONACCI_NEXT (fibx = fib1 + fib2, fib1 = fib2, fib2 = fibx) // https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ -static inline uint32_t cmap_reduce(uint32_t x, uint32_t N) { +static inline uint32_t c_hashmap_reduce(uint32_t x, uint32_t N) { return ((uint64_t) x * (uint64_t) N) >> 32 ; } diff --git a/cmap_test.c b/cmap_test.c index ca773fdb..39715b3d 100644 --- a/cmap_test.c +++ b/cmap_test.c @@ -24,14 +24,14 @@ #include #include -#include "cmap.h" -#include "cstring.h" +#include "c_hashmap.h" +#include "c_string.h" -declare_CStringVector(s); -declare_CMap_StringKey(ss, CString, cstring_destroy); -declare_CMap_StringKey(si, int); -declare_CMap(id, uint64_t, double); +declare_c_StringVector(s); +c_declare_Hashmap_stringkey(ss, c_String, c_string_destroy); +c_declare_Hashmap_stringkey(si, int); +c_declare_Hashmap(id, uint64_t, double); // like fgets, but removes any newline @@ -46,7 +46,7 @@ char *fgetstr(char *string, int n, FILE *stream) return string; } -int read_words(CMap_si* map) +int read_words(c_Hashmap_si* map) { FILE * fp; # define bufferLength 1024 @@ -61,7 +61,7 @@ int read_words(CMap_si* map) while (fgetstr(line, bufferLength, fp)) { ++i; if (i < 10) printf("%zu: %s\n", i, line); - cmap_si_put(map, line, i); + c_hashmap_si_put(map, line, i); } fclose(fp); @@ -69,20 +69,20 @@ int read_words(CMap_si* map) } void stringSpeed(int limit) { - CString s = cstring_initializer; + c_String s = c_string_initializer; char ch[2] = {0, 0}; size_t x = 0; size_t p = 0; - for (int n = 0; n < limit; ++n) { ch[0] = 'A' + (rand() % 26); cstring_append(&s, ch); } + for (int n = 0; n < limit; ++n) { ch[0] = 'A' + (rand() % 26); c_string_append(&s, ch); } const char* search = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"; - cstring_append(&s, search); + c_string_append(&s, search); clock_t before = clock(); for (int n = 0; n < limit; ++n) { - p = cstring_find(s, 0, search + (n % 100)); - if (p != cstring_npos) x += p; + p = c_string_find(s, 0, search + (n % 100)); + if (p != c_string_npos) x += p; } clock_t diff = clock() - before; - printf("cstring length = %llu / %llu, sum %llu speed: %f\n", cstring_size(s), cstring_capacity(s), x, 1.0 * diff / CLOCKS_PER_SEC); + printf("cstring length = %llu / %llu, sum %llu speed: %f\n", c_string_size(s), c_string_capacity(s), x, 1.0 * diff / CLOCKS_PER_SEC); } int main() @@ -91,73 +91,73 @@ int main() stringSpeed(20000); - CString cs = cstring_make("one-nine-three-seven-five"); + c_String cs = c_string_make("one-nine-three-seven-five"); printf("%s.\n", cs.str); - cstring_insert(&cs, 3, "-two"); + c_string_insert(&cs, 3, "-two"); printf("%s.\n", cs.str); - cstring_erase(&cs, 7, 5); // -nine + c_string_erase(&cs, 7, 5); // -nine printf("%s.\n", cs.str); - cstring_replace(&cs, 0, "seven", "four"); + c_string_replace(&cs, 0, "seven", "four"); printf("%s.\n", cs.str); - printf("found: %s\n", cs.str + cstring_find(cs, 0, "four")); - cstring_assign(&cs, "one two three four five six seven"); - cstring_replace(&cs, 0, "four", "F-O-U-R"); + printf("found: %s\n", cs.str + c_string_find(cs, 0, "four")); + c_string_assign(&cs, "one two three four five six seven"); + c_string_replace(&cs, 0, "four", "F-O-U-R"); printf("replace: %s\n", cs.str); - CMap_si words = cmap_initializer; + c_Hashmap_si words = c_hashmap_initializer; printf("read words\n"); read_words(&words); - CMapEntry_si* num = NULL; - num = cmap_si_get(words, "hello"); + c_HashmapEntry_si* num = NULL; + num = c_hashmap_si_get(words, "hello"); if (num) printf("%s: %d\n", num->key.str, num->value); - num = cmap_si_get(words, "funny"); + num = c_hashmap_si_get(words, "funny"); if (num) printf("%s: %d\n", num->key.str, num->value); - printf("words size: %llu, capacity %llu\n", cmap_size(words), cmap_buckets(words)); - cmap_si_clear(&words); + printf("words size: %llu, capacity %llu\n", c_hashmap_size(words), c_hashmap_buckets(words)); + c_hashmap_si_clear(&words); - CVector_s strv = cvector_initializer; - CString myday = cstring_make("my day"); - cstring_assign(&myday, "great"); + c_Vector_s strv = c_vector_initializer; + c_String myday = c_string_make("my day"); + c_string_assign(&myday, "great"); - cvector_s_push(&strv, cstring_make("E0")); - cvector_s_push(&strv, cstring_make("E1")); - cvector_s_push(&strv, cstring_make("E2")); + c_vector_s_push(&strv, c_string_make("E0")); + c_vector_s_push(&strv, c_string_make("E1")); + c_vector_s_push(&strv, c_string_make("E2")); printf(" element %d: %s\n", 1, strv.data[1].str); - cforeach (i, cvector_s, strv) { + c_foreach (i, c_vector_s, strv) { printf(" %s\n", i.item->str); } - for (i = 0; i < cvector_size(strv); ++i) { + for (i = 0; i < c_vector_size(strv); ++i) { printf(" %s\n", strv.data[i].str); } - cvector_s_destroy(&strv); + c_vector_s_destroy(&strv); - CMap_ss smap = cmap_initializer; - cmap_ss_put(&smap, "KEY1", cstring_make("VAL1")); - cmap_ss_put(&smap, "KEY2", cstring_make("VAL2")); - cmap_ss_put(&smap, "hello", cstring_makeCopy(myday)); - cstring_destroy(&myday); + c_Hashmap_ss smap = c_hashmap_initializer; + c_hashmap_ss_put(&smap, "KEY1", c_string_make("VAL1")); + c_hashmap_ss_put(&smap, "KEY2", c_string_make("VAL2")); + c_hashmap_ss_put(&smap, "hello", c_string_makeCopy(myday)); + c_string_destroy(&myday); - cforeach (i, cmap_ss, smap) + c_foreach (i, c_hashmap_ss, smap) printf(" %s: %s\n", i.item->key.str, i.item->value.str); - cmap_ss_destroy(&smap); + c_hashmap_ss_destroy(&smap); - CMap_id mymap = cmap_initializer; + c_Hashmap_id mymap = c_hashmap_initializer; for (i = 0; i < 600000; ++i) - cmap_id_put(&mymap, i*i, i); + c_hashmap_id_put(&mymap, i*i, i); for (i = 1000; i < 1010; ++i) - printf("lookup %d: %f\n", i*i, cmap_id_get(mymap, i*i)->value); + printf("lookup %d: %f\n", i*i, c_hashmap_id_get(mymap, i*i)->value); - CMapEntry_id* me = cmap_id_get(mymap, 10000); + c_HashmapEntry_id* me = c_hashmap_id_get(mymap, 10000); printf("changed: %d %f\n", me->changed, me->value); - cmap_id_put(&mymap, 10000, 101.2); + c_hashmap_id_put(&mymap, 10000, 101.2); printf("changed: %d %f\n", me->changed, me->value); - cmap_id_destroy(&mymap); + c_hashmap_id_destroy(&mymap); } diff --git a/cstring.h b/cstring.h index bfffe2e3..e0fcd7e5 100644 --- a/cstring.h +++ b/cstring.h @@ -20,187 +20,187 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#ifndef CSTRING__H__ -#define CSTRING__H__ +#ifndef C_STRING__H__ +#define C_STRING__H__ #include #include #include #include -#include "cdefs.h" +#include "c_defs.h" -typedef struct CString { +typedef struct c_String { char* str; -} CString; +} c_String; -static size_t _cstring_null_rep[] = {0, 0, 0}; -#define _cstring_rep(cs) (((size_t *) (cs).str) - 2) +static size_t _c_string_null_rep[] = {0, 0, 0}; +#define _c_string_rep(cs) (((size_t *) (cs).str) - 2) -#define cstring_initializer {(char* ) (_cstring_null_rep + 2)} -#define cstring_size(cs) ((size_t) _cstring_rep(cs)[0]) -#define cstring_capacity(cs) ((size_t) _cstring_rep(cs)[1]) -#define cstring_npos ((size_t) -1) +#define c_string_initializer {(char* ) (_c_string_null_rep + 2)} +#define c_string_size(cs) ((size_t) _c_string_rep(cs)[0]) +#define c_string_capacity(cs) ((size_t) _c_string_rep(cs)[1]) +#define c_string_npos ((size_t) -1) -static inline void cstring_reserve(CString* self, size_t cap) { - size_t len = cstring_size(*self), oldcap = cstring_capacity(*self); +static inline void c_string_reserve(c_String* self, size_t cap) { + size_t len = c_string_size(*self), oldcap = c_string_capacity(*self); if (cap > oldcap) { - size_t* rep = (size_t *) realloc(oldcap ? _cstring_rep(*self) : NULL, sizeof(size_t) * 2 + cap + 1); + size_t* rep = (size_t *) realloc(oldcap ? _c_string_rep(*self) : NULL, sizeof(size_t) * 2 + cap + 1); self->str = (char* ) (rep + 2); self->str[ rep[0] = len ] = '\0'; rep[1] = cap; } } -static inline void cstring_destroy(CString* self) { - if (cstring_capacity(*self)) { - free(_cstring_rep(*self)); +static inline void c_string_destroy(c_String* self) { + if (c_string_capacity(*self)) { + free(_c_string_rep(*self)); } } -static inline CString cstring_init(void) { - CString cs = cstring_initializer; +static inline c_String c_string_init(void) { + c_String cs = c_string_initializer; return cs; } -static inline CString cstring_makeN(const char* str, size_t len) { - CString cs = cstring_initializer; +static inline c_String c_string_makeN(const char* str, size_t len) { + c_String cs = c_string_initializer; if (len) { - cstring_reserve(&cs, len); + c_string_reserve(&cs, len); memcpy(cs.str, str, len); - cs.str[ _cstring_rep(cs)[0] = len ] = '\0'; + cs.str[ _c_string_rep(cs)[0] = len ] = '\0'; } return cs; } -static inline CString cstring_make(const char* str) { - return cstring_makeN(str, strlen(str)); +static inline c_String c_string_make(const char* str) { + return c_string_makeN(str, strlen(str)); } -static inline CString cstring_makeCopy(CString cs) { - return cstring_makeN(cs.str, cstring_size(cs)); +static inline c_String c_string_makeCopy(c_String cs) { + return c_string_makeN(cs.str, c_string_size(cs)); } -static inline void cstring_clear(CString* self) { - CString cs = cstring_initializer; - cstring_destroy(self); +static inline void c_string_clear(c_String* self) { + c_String cs = c_string_initializer; + c_string_destroy(self); *self = cs; } -static inline CString* cstring_assignN(CString* self, const char* str, size_t len) { +static inline c_String* c_string_assignN(c_String* self, const char* str, size_t len) { if (len) { - cstring_reserve(self, len); + c_string_reserve(self, len); memmove(self->str, str, len); - self->str[_cstring_rep(*self)[0] = len] = '\0'; + self->str[_c_string_rep(*self)[0] = len] = '\0'; } return self; } -static inline CString* cstring_assign(CString* self, const char* str) { - return cstring_assignN(self, str, strlen(str)); +static inline c_String* c_string_assign(c_String* self, const char* str) { + return c_string_assignN(self, str, strlen(str)); } -static inline CString* cstring_copy(CString* self, CString cs2) { - return cstring_assignN(self, cs2.str, cstring_size(cs2)); +static inline c_String* c_string_copy(c_String* self, c_String cs2) { + return c_string_assignN(self, cs2.str, c_string_size(cs2)); } -static inline CString* cstring_appendN(CString* self, const char* str, size_t len) { +static inline c_String* c_string_appendN(c_String* self, const char* str, size_t len) { if (len) { - size_t oldlen = cstring_size(*self), newlen = oldlen + len; - if (newlen > cstring_capacity(*self)) - cstring_reserve(self, newlen * 5 / 3); + size_t oldlen = c_string_size(*self), newlen = oldlen + len; + if (newlen > c_string_capacity(*self)) + c_string_reserve(self, newlen * 5 / 3); memmove(&self->str[oldlen], str, len); - self->str[_cstring_rep(*self)[0] = newlen] = '\0'; + self->str[_c_string_rep(*self)[0] = newlen] = '\0'; } return self; } -static inline CString* cstring_append(CString* self, const char* str) { - return cstring_appendN(self, str, strlen(str)); +static inline c_String* c_string_append(c_String* self, const char* str) { + return c_string_appendN(self, str, strlen(str)); } -static inline CString* cstring_appendS(CString* self, CString cs2) { - return cstring_appendN(self, cs2.str, cstring_size(cs2)); +static inline c_String* c_string_appendS(c_String* self, c_String cs2) { + return c_string_appendN(self, cs2.str, c_string_size(cs2)); } -static inline void _cstring_internalMove(CString* self, size_t pos1, size_t pos2) { +static inline void _c_string_internalMove(c_String* self, size_t pos1, size_t pos2) { if (pos1 == pos2) return; - size_t len = cstring_size(*self), newlen = len + pos2 - pos1; - if (newlen > cstring_capacity(*self)) - cstring_reserve(self, newlen * 5 / 3); + size_t len = c_string_size(*self), newlen = len + pos2 - pos1; + if (newlen > c_string_capacity(*self)) + c_string_reserve(self, newlen * 5 / 3); memmove(&self->str[pos2], &self->str[pos1], len - pos1); - self->str[_cstring_rep(*self)[0] = newlen] = '\0'; + self->str[_c_string_rep(*self)[0] = newlen] = '\0'; } -static inline void cstring_insertN(CString* self, size_t pos, const char* str, size_t n) { - char* xstr = (char *) memcpy(n > cdefs_max_alloca ? malloc(n) : alloca(n), str, n); - _cstring_internalMove(self, pos, pos + n); +static inline void c_string_insertN(c_String* self, size_t pos, const char* str, size_t n) { + char* xstr = (char *) memcpy(n > c_defs_max_alloca ? malloc(n) : alloca(n), str, n); + _c_string_internalMove(self, pos, pos + n); memcpy(&self->str[pos], xstr, n); - if (n > cdefs_max_alloca) free(xstr); + if (n > c_defs_max_alloca) free(xstr); } -static inline void cstring_insert(CString* self, size_t pos, const char* str) { - cstring_insertN(self, pos, str, strlen(str)); +static inline void c_string_insert(c_String* self, size_t pos, const char* str) { + c_string_insertN(self, pos, str, strlen(str)); } -static inline void cstring_erase(CString* self, size_t pos, size_t n) { - size_t len = cstring_size(*self); +static inline void c_string_erase(c_String* self, size_t pos, size_t n) { + size_t len = c_string_size(*self); if (len) { memmove(&self->str[pos], &self->str[pos + n], len - (pos + n)); - self->str[_cstring_rep(*self)[0] -= n] = '\0'; + self->str[_c_string_rep(*self)[0] -= n] = '\0'; } } -static inline size_t cstring_findN(CString cs, size_t pos, const char* needle, size_t n); +static inline size_t c_string_findN(c_String cs, size_t pos, const char* needle, size_t n); -static inline size_t cstring_replaceN(CString* self, size_t pos, const char* s1, size_t n1, const char* s2, size_t n2) { - size_t pos2 = cstring_findN(*self, pos, s1, n1); - if (pos2 == cstring_npos) return cstring_npos; - char* xs2 = (char *) memcpy(n2 > cdefs_max_alloca ? malloc(n2) : alloca(n2), s2, n2); - _cstring_internalMove(self, pos2 + n1, pos2 + n2); +static inline size_t c_string_replaceN(c_String* self, size_t pos, const char* s1, size_t n1, const char* s2, size_t n2) { + size_t pos2 = c_string_findN(*self, pos, s1, n1); + if (pos2 == c_string_npos) return c_string_npos; + char* xs2 = (char *) memcpy(n2 > c_defs_max_alloca ? malloc(n2) : alloca(n2), s2, n2); + _c_string_internalMove(self, pos2 + n1, pos2 + n2); memcpy(&self->str[pos2], xs2, n2); - if (n2 > cdefs_max_alloca) free(xs2); + if (n2 > c_defs_max_alloca) free(xs2); return pos2; } -static inline size_t cstring_replace(CString* self, size_t pos, const char* s1, const char* s2) { - return cstring_replaceN(self, pos, s1, strlen(s1), s2, strlen(s2)); +static inline size_t c_string_replace(c_String* self, size_t pos, const char* s1, const char* s2) { + return c_string_replaceN(self, pos, s1, strlen(s1), s2, strlen(s2)); } -static inline char cstring_back(CString cs) { - return cs.str[cstring_size(cs) - 1]; +static inline char c_string_back(c_String cs) { + return cs.str[c_string_size(cs) - 1]; } -static inline CString* cstring_push(CString* self, char value) { - return cstring_appendN(self, &value, 1); +static inline c_String* c_string_push(c_String* self, char value) { + return c_string_appendN(self, &value, 1); } -static inline void cstring_pop(CString* self) { - --_cstring_rep(*self)[0]; +static inline void c_string_pop(c_String* self) { + --_c_string_rep(*self)[0]; } /* readonly */ -static inline bool cstring_empty(CString cs) { - return cstring_size(cs) == 0; +static inline bool c_string_empty(c_String cs) { + return c_string_size(cs) == 0; } -static inline bool cstring_equals(CString cs1, const char* str) { +static inline bool c_string_equals(c_String cs1, const char* str) { return strcmp(cs1.str, str) == 0; } -static inline bool cstring_equalsS(CString cs1, CString cs2) { +static inline bool c_string_equalsS(c_String cs1, c_String cs2) { return strcmp(cs1.str, cs2.str) == 0; } -static inline char* cstring_strnstr(CString cs, size_t pos, const char* needle, size_t n) { +static inline char* c_string_strnstr(c_String cs, size_t pos, const char* needle, size_t n) { char *x = cs.str + pos, // haystack - *z = cs.str + cstring_size(cs) - n + 1; + *z = cs.str + c_string_size(cs) - n + 1; if (x >= z) return NULL; ptrdiff_t sum = 0; @@ -215,30 +215,30 @@ static inline char* cstring_strnstr(CString cs, size_t pos, const char* needle, return NULL; } -static inline size_t cstring_findN(CString cs, size_t pos, const char* needle, size_t n) { - char* res = cstring_strnstr(cs, pos, needle, n); - return res ? res - cs.str : cstring_npos; +static inline size_t c_string_findN(c_String cs, size_t pos, const char* needle, size_t n) { + char* res = c_string_strnstr(cs, pos, needle, n); + return res ? res - cs.str : c_string_npos; } -static inline size_t cstring_find(CString cs, size_t pos, const char* needle) { +static inline size_t c_string_find(c_String cs, size_t pos, const char* needle) { char* res = strstr(cs.str + pos, needle); - return res ? res - cs.str : cstring_npos; + return res ? res - cs.str : c_string_npos; } -static inline char* cstring_splitFirst(const char* delimiters, CString cs) { +static inline char* c_string_splitFirst(const char* delimiters, c_String cs) { return strtok(cs.str, delimiters); } -static inline char* cstring_splitNext(const char* delimiters) { +static inline char* c_string_splitNext(const char* delimiters) { return strtok(NULL, delimiters); } -// CVector / CMap API functions: +// c_Vector / c_Hashmap API functions: -#define cstring_getRaw(x) ((x).str) -static inline uint32_t cstring_hashRaw(const char** str, size_t sz_ignored) { return cdefs_murmurHash(*str, strlen(*str)); } -static inline int cstring_compareRaw(CString* self, const char** str, size_t sz_ignored) { return strcmp(self->str, *str); } +#define c_string_getRaw(x) ((x).str) +static inline uint32_t c_string_hashRaw(const char** str, size_t sz_ignored) { return c_defs_murmurHash(*str, strlen(*str)); } +static inline int c_string_compareRaw(c_String* self, const char** str, size_t sz_ignored) { return strcmp(self->str, *str); } #endif diff --git a/cvector.h b/cvector.h index 02a351ed..67ce9918 100644 --- a/cvector.h +++ b/cvector.h @@ -20,127 +20,127 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#ifndef CVECTOR__H__ -#define CVECTOR__H__ +#ifndef C_VECTOR__H__ +#define C_VECTOR__H__ #include #include -#include "cdefs.h" +#include "c_defs.h" -#define cvector_initializer {NULL} -#define cvector_size(cv) _cvector_safe_size((cv).data) -#define cvector_capacity(cv) _cvector_safe_capacity((cv).data) -#define cvector_empty(cv) (_cvector_safe_size((cv).data) == 0) +#define c_vector_initializer {NULL} +#define c_vector_size(cv) _c_vector_safe_size((cv).data) +#define c_vector_capacity(cv) _c_vector_safe_capacity((cv).data) +#define c_vector_empty(cv) (_c_vector_safe_size((cv).data) == 0) -#define declare_CVector(...) cdefs_MACRO_OVERLOAD(declare_CVector, __VA_ARGS__) -#define declare_CVector_2(tag, Value) declare_CVector_3(tag, Value, cdefs_destroy) -#define declare_CStringVector(tag) declare_CVector_3(tag, CString, cstring_destroy) +#define c_declare_Vector(...) c_defs_MACRO_OVERLOAD(c_declare_Vector, __VA_ARGS__) +#define c_declare_Vector_2(tag, Value) c_declare_Vector_3(tag, Value, c_defs_destroy) +#define declare_CStringVector(tag) c_declare_Vector_3(tag, CString, cstring_destroy) -#define declare_CVector_3(tag, Value, valueDestroy) \ -typedef struct CVector_##tag { \ +#define c_declare_Vector_3(tag, Value, valueDestroy) \ +typedef struct c_Vector_##tag { \ Value* data; \ -} CVector_##tag; \ -typedef struct cvector_##tag##_iter_t { \ +} c_Vector_##tag; \ +typedef struct c_vector_##tag##_iter_t { \ Value* item; \ -} cvector_##tag##_iter_t; \ +} c_vector_##tag##_iter_t; \ \ -static inline CVector_##tag cvector_##tag##_init(void) { \ - CVector_##tag cv = cvector_initializer; \ +static inline c_Vector_##tag c_vector_##tag##_init(void) { \ + c_Vector_##tag cv = c_vector_initializer; \ return cv; \ } \ \ -static inline void cvector_##tag##_swap(CVector_##tag* a, CVector_##tag* b) { \ +static inline void c_vector_##tag##_swap(c_Vector_##tag* a, c_Vector_##tag* b) { \ Value* data = a->data; a->data = b->data; b->data = data; \ } \ \ -static inline void cvector_##tag##_destroy(CVector_##tag* self) { \ +static inline void c_vector_##tag##_destroy(c_Vector_##tag* self) { \ Value* p = self->data; \ - size_t i = 0, n = cvector_size(*self); \ + size_t i = 0, n = c_vector_size(*self); \ for (; i < n; ++p, ++i) valueDestroy(p); \ - free(_cvector_alloced(self->data)); \ + free(_c_vector_alloced(self->data)); \ } \ \ -static inline void cvector_##tag##_reserve(CVector_##tag* self, size_t cap) { \ - if (cap > cvector_capacity(*self)) { \ - size_t len = cvector_size(*self); \ - size_t* rep = (size_t *) realloc(_cvector_alloced(self->data), 2 * sizeof(size_t) + cap * sizeof(Value)); \ +static inline void c_vector_##tag##_reserve(c_Vector_##tag* self, size_t cap) { \ + if (cap > c_vector_capacity(*self)) { \ + size_t len = c_vector_size(*self); \ + size_t* rep = (size_t *) realloc(_c_vector_alloced(self->data), 2 * sizeof(size_t) + cap * sizeof(Value)); \ self->data = (Value *) (rep + 2); \ rep[0] = len; \ rep[1] = cap; \ } \ } \ \ -static inline void cvector_##tag##_clear(CVector_##tag* self) { \ - CVector_##tag cv = cvector_initializer; \ - cvector_##tag##_destroy(self); \ +static inline void c_vector_##tag##_clear(c_Vector_##tag* self) { \ + c_Vector_##tag cv = c_vector_initializer; \ + c_vector_##tag##_destroy(self); \ *self = cv; \ } \ \ \ -static inline void cvector_##tag##_push(CVector_##tag* self, Value value) { \ - size_t newsize = cvector_size(*self) + 1; \ - if (newsize > cvector_capacity(*self)) \ - cvector_##tag##_reserve(self, 7 + newsize * 5 / 3); \ - self->data[cvector_size(*self)] = value; \ - _cvector_size(*self) = newsize; \ +static inline void c_vector_##tag##_push(c_Vector_##tag* self, Value value) { \ + size_t newsize = c_vector_size(*self) + 1; \ + if (newsize > c_vector_capacity(*self)) \ + c_vector_##tag##_reserve(self, 7 + newsize * 5 / 3); \ + self->data[c_vector_size(*self)] = value; \ + _c_vector_size(*self) = newsize; \ } \ \ -static inline void cvector_##tag##_insert(CVector_##tag* self, size_t pos, Value value) { \ - cvector_##tag##_push(self, value); \ - size_t len = cvector_size(*self); \ +static inline void c_vector_##tag##_insert(c_Vector_##tag* self, size_t pos, Value value) { \ + c_vector_##tag##_push(self, value); \ + size_t len = c_vector_size(*self); \ memmove(&self->data[pos + 1], &self->data[pos], (len - pos - 1) * sizeof(Value)); \ self->data[pos] = value; \ } \ \ -static inline void cvector_##tag##_erase(CVector_##tag* self, size_t pos, size_t size) { \ - size_t len = cvector_size(*self); \ +static inline void c_vector_##tag##_erase(c_Vector_##tag* self, size_t pos, size_t size) { \ + size_t len = c_vector_size(*self); \ if (len) { \ Value* p = &self->data[pos], *start = p, *end = p + size; \ while (p != end) valueDestroy(p++); \ memmove(start, end, (len - pos - size) * sizeof(Value)); \ - _cvector_size(*self) -= size; \ + _c_vector_size(*self) -= size; \ } \ } \ \ \ -static inline Value cvector_##tag##_back(CVector_##tag cv) { \ - return cv.data[_cvector_size(cv) - 1]; \ +static inline Value c_vector_##tag##_back(c_Vector_##tag cv) { \ + return cv.data[_c_vector_size(cv) - 1]; \ } \ \ -static inline void cvector_##tag##_pop(CVector_##tag* self) { \ - valueDestroy(&self->data[_cvector_size(*self) - 1]); \ - --_cvector_size(*self); \ +static inline void c_vector_##tag##_pop(c_Vector_##tag* self) { \ + valueDestroy(&self->data[_c_vector_size(*self) - 1]); \ + --_c_vector_size(*self); \ } \ \ -static inline cvector_##tag##_iter_t cvector_##tag##_begin(CVector_##tag vec) { \ - cvector_##tag##_iter_t it = {vec.data}; \ +static inline c_vector_##tag##_iter_t c_vector_##tag##_begin(c_Vector_##tag vec) { \ + c_vector_##tag##_iter_t it = {vec.data}; \ return it; \ } \ \ -static inline cvector_##tag##_iter_t cvector_##tag##_next(cvector_##tag##_iter_t it) { \ +static inline c_vector_##tag##_iter_t c_vector_##tag##_next(c_vector_##tag##_iter_t it) { \ ++it.item; \ return it; \ } \ \ -static inline cvector_##tag##_iter_t cvector_##tag##_end(CVector_##tag vec) { \ - cvector_##tag##_iter_t it = {vec.data + cvector_size(vec)}; \ +static inline c_vector_##tag##_iter_t c_vector_##tag##_end(c_Vector_##tag vec) { \ + c_vector_##tag##_iter_t it = {vec.data + c_vector_size(vec)}; \ return it; \ } \ -typedef Value cvector_##tag##_value_t +typedef Value c_vector_##tag##_value_t -#define _cvector_size(cv) ((size_t *)(cv).data)[-2] -#define _cvector_capacity(cv) ((size_t *)(cv).data)[-1] +#define _c_vector_size(cv) ((size_t *)(cv).data)[-2] +#define _c_vector_capacity(cv) ((size_t *)(cv).data)[-1] -static inline size_t* _cvector_alloced(void* data) { +static inline size_t* _c_vector_alloced(void* data) { return data ? ((size_t *) data) - 2 : NULL; } -static inline size_t _cvector_safe_size(const void* data) { +static inline size_t _c_vector_safe_size(const void* data) { return data ? ((const size_t *) data)[-2] : 0; } -static inline size_t _cvector_safe_capacity(const void* data) { +static inline size_t _c_vector_safe_capacity(const void* data) { return data ? ((const size_t *) data)[-1] : 0; } -- cgit v1.2.3