From 4e3efe66adc1048168f1ebb7faaf7e7153c4d454 Mon Sep 17 00:00:00 2001 From: Tyge Løvset <60263450+tylo-work@users.noreply.github.com> Date: Thu, 5 Mar 2020 09:14:53 +0100 Subject: Add files via upload --- cdef.h | 45 ++++++++++++++++------ cmap.h | 122 +++++++++++++++++++++++++++++++++++------------------------- cmap_test.c | 52 ++++++++++++++++++-------- cstring.h | 22 +++++++++++ cvector.h | 88 +++++++++++++++++++++++-------------------- 5 files changed, 212 insertions(+), 117 deletions(-) diff --git a/cdef.h b/cdef.h index 995d1ea3..2400182d 100644 --- a/cdef.h +++ b/cdef.h @@ -1,7 +1,28 @@ +// MIT License +// +// Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #ifndef CDEF__H__ #define CDEF__H__ - // Macro overloading feature support: https://rextester.com/ONP80107 #define cdef_CAT( A, B ) A ## B #define cdef_EXPAND(...) __VA_ARGS__ @@ -12,19 +33,21 @@ #define cdef_OVERLOAD_SELECT(NAME, NUM) cdef_CAT( NAME ## _, NUM) #define cdef_MACRO_OVERLOAD(NAME, ...) cdef_OVERLOAD_SELECT(NAME, cdef_VA_ARG_SIZE(__VA_ARGS__))(__VA_ARGS__) -// #define foo(...) cdef_MACRO_OVERLOAD(foo, __VA_ARGS__) -// #define foo_1(X) foo_2(X, 100) -// #define foo_2(X, Y) X + Y +// #define foo(...) cdef_MACRO_OVERLOAD(foo, __VA_ARGS__) +// #define foo_1(X) foo_2(X, 100) +// #define foo_2(X, Y) X + Y -#define _cdef_swap(T, x, y) { T __t = x; x = y; y = __t; } +#define _cdef_swap(T, x, y) { T __t = x; x = y; y = __t; } -#define cdef_initRaw(x) (x) -#define cdef_getRaw(x) (x) -static inline void cdef_destroy(void* value) {} +#define cdef_initRaw(x) (x) +#define cdef_getRaw(x) (x) +static inline void cdef_destroy(void* value) {} -#define cforeach(...) cdef_MACRO_OVERLOAD(cforeach, __VA_ARGS__) -#define cforeach_3(it, tagc, con) for (it = tagc##_begin(con); it.item != tagc##_end(con).item; it = tagc##_next(it)) -#define cforeach_5(it, tagc, con, cond, next) for (it = tagc##_begin(con); it.item != tagc##_end(con).item && (cond); it = tagc##_next(it), next) +#define cforeach(...) cdef_MACRO_OVERLOAD(cforeach, __VA_ARGS__) +#define cforeach_3(it, ctag, con) \ + for (ctag##_iter_t it = ctag##_begin(con); it.item != ctag##_end(con).item; it = ctag##_next(it)) +#define cforeach_5(it, ctag, con, cond, next) \ + for (ctag##_iter_t it = ctag##_begin(con); it.item != ctag##_end(con).item && (cond); it = ctag##_next(it), next) static inline uint32_t cdef_murmurHash(const void *data, size_t len) // One-at-a-time 32bit diff --git a/cmap.h b/cmap.h index 3a8b0408..7f7280d8 100644 --- a/cmap.h +++ b/cmap.h @@ -1,12 +1,30 @@ +// MIT License +// +// Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #ifndef CMAP_H_ #define CMAP_H_ #include "cvector.h" -#define CMap(tag) CMap_##tag##_t -#define CMapEntry(tag) CMapEntry_##tag##_t -#define CMapIter(tag) CMapIter_##tag##_t - #define cmap_initializer {cvector_initializer, 0} #define cmap_size(cm) ((cvector_size_t) (cm)._size) #define cmap_capacity(cm) cvector_capacity((cm)._vec) @@ -14,14 +32,18 @@ // CMapEntry: #define declare_CMapEntry(tag, Key, Value, keyDestroy, valueDestroy) \ -struct CMapEntry(tag) { Key key; Value value; short untouched, _used; }; \ +struct CMapEntry_##tag { \ + Key key; \ + Value value; \ + short _used, changed; \ +}; \ \ -static inline void cmapentry_##tag##_destroy(struct CMapEntry(tag)* p) { \ +static inline void cmapentry_##tag##_destroy(struct CMapEntry_##tag* p) { \ keyDestroy(&p->key); \ valueDestroy(&p->value); \ - p->_used = 0; \ + p->_used = p->changed = 0; \ } \ -typedef struct CMapEntry(tag) CMapEntry(tag) +typedef struct CMapEntry_##tag CMapEntry_##tag // CMap: @@ -47,43 +69,43 @@ typedef struct CMapEntry(tag) CMapEntry(tag) // CMap full: #define declare_CMap_10(tag, Key, Value, valueDestroy, KeyRaw, keyGetRaw, keyCompare, keyHasher, keyInit, keyDestroy) \ declare_CMapEntry(tag, Key, Value, keyDestroy, valueDestroy); \ - declare_CVector_3(_map##tag, CMapEntry(tag), cmapentry_##tag##_destroy); \ + declare_CVector_3(map_##tag, CMapEntry_##tag, cmapentry_##tag##_destroy); \ \ -typedef struct CMap(tag) { \ - CVector(_map##tag) _vec; \ +typedef struct CMap_##tag { \ + CVector_map_##tag _vec; \ cvector_size_t _size; \ -} CMap(tag); \ +} CMap_##tag; \ \ -typedef struct CMapIter(tag) { \ - CMapEntry(tag) *item, *_end; \ -} CMapIter(tag); \ +typedef struct cmap_##tag##_iter_t { \ + CMapEntry_##tag *item, *_end; \ +} cmap_##tag##_iter_t; \ \ -static inline CMap(tag) cmap_##tag##_init(void) { \ - CMap(tag) map = cmap_initializer; \ +static inline CMap_##tag cmap_##tag##_init(void) { \ + CMap_##tag map = cmap_initializer; \ return map; \ } \ \ -static inline void cmap_##tag##_destroy(CMap(tag)* self) { \ +static inline void cmap_##tag##_destroy(CMap_##tag* self) { \ if (self->_size) { \ cvector_size_t cap = _cvector_capacity(self->_vec); \ - CMapEntry(tag)* p = self->_vec.data, *end = p + cap; \ + CMapEntry_##tag* p = self->_vec.data, *end = p + cap; \ for (; p != end; ++p) if (p->_used) cmapentry_##tag##_destroy(p); \ } \ - cvector__map##tag##_destroy(&self->_vec); \ + cvector_map_##tag##_destroy(&self->_vec); \ } \ \ -static inline void cmap_##tag##_clear(CMap(tag)* self) { \ - CMap(tag) cm = cmap_initializer; \ +static inline void cmap_##tag##_clear(CMap_##tag* self) { \ + CMap_##tag cm = cmap_initializer; \ cmap_##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); \ +static inline void cmap_##tag##_swap(CMap_##tag* a, CMap_##tag* b) { \ + cvector_map_##tag##_swap(&a->_vec, &b->_vec); \ _cdef_swap(cvector_size_t, a->_size, b->_size); \ } \ \ -static inline cvector_size_t _cmap_##tag##_findIndex(CMap(tag) cm, KeyRaw rawKey) { \ +static inline cvector_size_t _cmap_##tag##_findIndex(CMap_##tag cm, KeyRaw rawKey) { \ cvector_size_t cap = cvector_capacity(cm._vec); \ cvector_size_t idx = keyHasher(&rawKey, sizeof(Key)) % cap, first = idx; \ FIBONACCI_DECL; \ @@ -92,22 +114,22 @@ static inline cvector_size_t _cmap_##tag##_findIndex(CMap(tag) cm, KeyRaw rawKey return idx; \ } \ \ -static inline CMapEntry(tag)* cmap_##tag##_get(CMap(tag) cm, KeyRaw rawKey) { \ +static inline CMapEntry_##tag* cmap_##tag##_get(CMap_##tag cm, KeyRaw rawKey) { \ if (cm._size == 0) return NULL; \ cvector_size_t idx = _cmap_##tag##_findIndex(cm, rawKey); \ return cm._vec.data[idx]._used ? &cm._vec.data[idx] : NULL; \ } \ \ -static inline cvector_size_t cmap_##tag##_rehash(CMap(tag)* self); /* predeclared */ \ +static inline cvector_size_t cmap_##tag##_rehash(CMap_##tag* self); /* predeclared */ \ \ -static inline CMapEntry(tag)* cmap_##tag##_put(CMap(tag)* self, KeyRaw rawKey, Value value) { \ +static inline CMapEntry_##tag* cmap_##tag##_put(CMap_##tag* self, KeyRaw rawKey, Value value) { \ cvector_size_t cap = cvector_capacity(self->_vec); \ if (self->_size >= cap * 8 / 10) \ cap = cmap_##tag##_rehash(self); \ cvector_size_t idx = _cmap_##tag##_findIndex(*self, rawKey); \ - CMapEntry(tag)* e = &self->_vec.data[idx]; \ + CMapEntry_##tag* e = &self->_vec.data[idx]; \ e->value = value; \ - e->untouched = !e->_used; \ + e->changed = e->_used; \ if (!e->_used) { \ e->key = keyInit(rawKey); \ e->_used = 1; \ @@ -116,22 +138,22 @@ static inline CMapEntry(tag)* cmap_##tag##_put(CMap(tag)* self, KeyRaw rawKey, V return e; \ } \ \ -static inline cvector_size_t cmap_##tag##_rehash(CMap(tag)* self) { \ - CVector(_map##tag) vec = cvector_initializer; \ +static inline cvector_size_t cmap_##tag##_rehash(CMap_##tag* self) { \ + CVector_map_##tag vec = cvector_initializer; \ cvector_size_t newcap = 7 + cmap_capacity(*self) * 2; \ - cvector__map##tag##_swap(&self->_vec, &vec); \ - cvector__map##tag##_reserve(&self->_vec, newcap); \ + cvector_map_##tag##_swap(&self->_vec, &vec); \ + cvector_map_##tag##_reserve(&self->_vec, newcap); \ self->_size = 0; \ - memset(self->_vec.data, 0, sizeof(CMapEntry(tag)) * newcap); \ - CMapEntry(tag)* p = vec.data; \ + memset(self->_vec.data, 0, sizeof(CMapEntry_##tag) * newcap); \ + CMapEntry_##tag* p = vec.data; \ cvector_size_t i, oldcap = cvector_capacity(vec); \ for (i = 0; i < oldcap; ++i, ++p) \ if (p->_used) cmap_##tag##_put(self, keyGetRaw(p->key), p->value); \ return newcap; \ } \ \ -static inline int cmap_##tag##_erase(CMap(tag)* self, KeyRaw rawKey) { \ - CMapEntry(tag)* entryPtr = cmap_##tag##_get(*self, rawKey); \ +static inline int cmap_##tag##_erase(CMap_##tag* self, KeyRaw rawKey) { \ + CMapEntry_##tag* entryPtr = cmap_##tag##_get(*self, rawKey); \ if (entryPtr) { \ cmapentry_##tag##_destroy(entryPtr); \ --self->_size; \ @@ -140,24 +162,24 @@ static inline int cmap_##tag##_erase(CMap(tag)* self, KeyRaw rawKey) { \ return 0; \ } \ \ -static inline CMapIter(tag) cmap_##tag##_begin(CMap(tag) map) { \ - CMapIter(tag) null = {NULL, NULL}; \ +static inline cmap_##tag##_iter_t cmap_##tag##_begin(CMap_##tag map) { \ + cmap_##tag##_iter_t null = {NULL, NULL}; \ if (map._size == 0) return null; \ - CMapEntry(tag)* p = map._vec.data, *end = p + _cvector_capacity(map._vec); \ + CMapEntry_##tag* p = map._vec.data, *end = p + _cvector_capacity(map._vec); \ while (p != end && !p->_used) ++p; \ - CMapIter(tag) it = {p, end}; return it; \ + cmap_##tag##_iter_t it = {p, end}; return it; \ } \ \ -static inline CMapIter(tag) cmap_##tag##_next(CMapIter(tag) iter) { \ - ++iter.item; \ - while (iter.item != iter._end && !iter.item->_used) ++iter.item; \ - return iter; \ +static inline cmap_##tag##_iter_t cmap_##tag##_next(cmap_##tag##_iter_t it) { \ + ++it.item; \ + while (it.item != it._end && !it.item->_used) ++it.item; \ + return it; \ } \ \ -static inline CMapIter(tag) cmap_##tag##_end(CMap(tag) map) { \ - CMapEntry(tag)* end = (map._size == 0) ? NULL : map._vec.data + _cvector_capacity(map._vec); \ - CMapIter(tag) iter = {end, end}; \ - return iter; \ +static inline cmap_##tag##_iter_t cmap_##tag##_end(CMap_##tag map) { \ + CMapEntry_##tag* end = (map._size == 0) ? NULL : map._vec.data + _cvector_capacity(map._vec); \ + cmap_##tag##_iter_t it = {end, end}; \ + return it; \ } \ typedef Key cmap_##tag##_key_t; \ typedef Value cmap_##tag##_value_t diff --git a/cmap_test.c b/cmap_test.c index 0bcbaefa..7c5e1fad 100644 --- a/cmap_test.c +++ b/cmap_test.c @@ -1,3 +1,25 @@ +// MIT License +// +// Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #include #include @@ -23,7 +45,7 @@ char *fgetstr(char *string, int n, FILE *stream) return string; } -int read_words(CMap(si)* map) +int read_words(CMap_si* map) { FILE * fp; # define bufferLength 1024 @@ -49,11 +71,12 @@ int main() { int i = 0; - CMap(si) words = cmap_initializer; + CMap_si words = cmap_initializer; + printf("read words\n"); read_words(&words); - CMapEntry(si)* num = NULL; + CMapEntry_si* num = NULL; num = cmap_si_get(words, "hello"); if (num) printf("%s: %d\n", num->key.str, num->value); @@ -63,7 +86,7 @@ int main() printf("words size: %d, capacity %d\n", cmap_size(words), cmap_capacity(words)); cmap_si_clear(&words); - CVector(s) strv = cvector_initializer; + CVector_s strv = cvector_initializer; CString hello = cstring_make("Hello"); cstring_assign(&hello, "Awesome"); @@ -72,8 +95,8 @@ int main() cvector_s_push(&strv, cstring_make("E2")); printf(" element %d: %s\n", 1, strv.data[1].str); - CVectorIter(s) ii; cforeach (ii, cvector_s, strv) { - printf(" %s\n", ii.item->str); + cforeach (i, cvector_s, strv) { + printf(" %s\n", i.item->str); } for (i = 0; i < cvector_size(strv); ++i) { @@ -81,30 +104,27 @@ int main() } cvector_s_destroy(&strv); - CMap(ss) smap = cmap_initializer; + 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_clone(hello)); - cstring_destroy(&hello); - CMapIter(ss) kk = cmap_ss_begin(smap), end2 = cmap_ss_end(smap); - for (; kk.item != end2.item; kk = cmap_ss_next(kk)) { - printf(" %s: %s\n", kk.item->key.str, kk.item->value.str); - } + cforeach (i, cmap_ss, smap) + printf(" %s: %s\n", i.item->key.str, i.item->value.str); cmap_ss_destroy(&smap); - CMap(id) mymap = cmap_initializer; + CMap_id mymap = cmap_initializer; for (i = 0; i < 600000; ++i) cmap_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); - CMapEntry(id)* me = cmap_id_get(mymap, 10000); - printf("untouched: %d %f\n", me->untouched, me->value); + CMapEntry_id* me = cmap_id_get(mymap, 10000); + printf("changed: %d %f\n", me->changed, me->value); cmap_id_put(&mymap, 10000, 101.2); - printf("untouched: %d %f\n", me->untouched, me->value); + printf("changed: %d %f\n", me->changed, me->value); cmap_id_destroy(&mymap); } diff --git a/cstring.h b/cstring.h index 5d8b8035..dcde34c7 100644 --- a/cstring.h +++ b/cstring.h @@ -1,3 +1,25 @@ +// MIT License +// +// Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #ifndef CSTRING__H__ #define CSTRING__H__ diff --git a/cvector.h b/cvector.h index 9bb983d5..6066a77a 100644 --- a/cvector.h +++ b/cvector.h @@ -1,3 +1,25 @@ +// MIT License +// +// Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #ifndef CVECTOR__H__ #define CVECTOR__H__ @@ -25,44 +47,41 @@ static inline cvector_size_t _cvector_safe_capacity(const void* data) { } -#define CVector(tag) CVector_##tag##_t -#define CVectorIter(tag) CVectorIter_##tag##_t - #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 declare_CVector(...) cdef_MACRO_OVERLOAD(declare_CVector, __VA_ARGS__) +#define declare_CVector(...) cdef_MACRO_OVERLOAD(declare_CVector, __VA_ARGS__) #define declare_CVector_2(tag, Value) declare_CVector_3(tag, Value, cdef_destroy) -#define declare_CVector_STR(tag) declare_CVector_3(tag, CString, cstring_destroy) +#define declare_CVector_STR(tag) declare_CVector_3(tag, CString, cstring_destroy) #define declare_CVector_3(tag, Value, valueDestroy) \ -typedef struct CVector(tag) { \ +typedef struct CVector_##tag { \ Value* data; \ -} CVector(tag); \ -typedef struct CVectorIter(tag) { \ +} CVector_##tag; \ +typedef struct cvector_##tag##_iter_t { \ Value* item; \ -} CVectorIter(tag); \ +} cvector_##tag##_iter_t; \ \ -static inline CVector(tag) cvector_##tag##_init(void) { \ - CVector(tag) cv = cvector_initializer; \ +static inline CVector_##tag cvector_##tag##_init(void) { \ + CVector_##tag cv = cvector_initializer; \ return cv; \ } \ \ -static inline void cvector_##tag##_swap(CVector(tag)* a, CVector(tag)* b) { \ +static inline void cvector_##tag##_swap(CVector_##tag* a, CVector_##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 cvector_##tag##_destroy(CVector_##tag* self) { \ Value* p = self->data; \ cvector_size_t i = 0, n = cvector_size(*self); \ for (; i < n; ++p, ++i) valueDestroy(p); \ free(_cvector_alloced(self->data)); \ } \ \ -static inline void cvector_##tag##_reserve(CVector(tag)* self, cvector_size_t cap) { \ +static inline void cvector_##tag##_reserve(CVector_##tag* self, cvector_size_t cap) { \ if (cap > cvector_capacity(*self)) { \ cvector_size_t len = cvector_size(*self); \ cvector_size_t* rep = (cvector_size_t *) realloc(_cvector_alloced(self->data), 2 * sizeof(cvector_size_t) + cap * sizeof(Value)); \ @@ -72,25 +91,14 @@ static inline void cvector_##tag##_reserve(CVector(tag)* self, cvector_size_t ca } \ } \ \ - \ -static inline CVector(tag) cvector_##tag##_make(const Value* data, cvector_size_t len) { \ - CVector(tag) cv = cvector_initializer; \ - if (len) { \ - cvector_##tag##_reserve(&cv, len); \ - memcpy(cv.data, data, len * sizeof(Value)); \ - _cvector_size(cv) = len; \ - } \ - return cv; \ -} \ - \ -static inline void cvector_##tag##_clear(CVector(tag)* self) { \ - CVector(tag) cv = cvector_initializer; \ +static inline void cvector_##tag##_clear(CVector_##tag* self) { \ + CVector_##tag cv = cvector_initializer; \ cvector_##tag##_destroy(self); \ *self = cv; \ } \ \ \ -static inline void cvector_##tag##_push(CVector(tag)* self, Value value) { \ +static inline void cvector_##tag##_push(CVector_##tag* self, Value value) { \ cvector_size_t newsize = cvector_size(*self) + 1; \ if (newsize > cvector_capacity(*self)) \ cvector_##tag##_reserve(self, 7 + newsize * 5 / 3); \ @@ -98,7 +106,7 @@ static inline void cvector_##tag##_push(CVector(tag)* self, Value value) { \ _cvector_size(*self) = newsize; \ } \ \ -static inline void cvector_##tag##_insert(CVector(tag)* self, cvector_size_t pos, Value value) { \ +static inline void cvector_##tag##_insert(CVector_##tag* self, cvector_size_t pos, Value value) { \ cvector_##tag##_push(self, value); \ cvector_size_t len = cvector_size(*self); \ memmove(&self->data[pos + 1], &self->data[pos], (len - pos - 1) * sizeof(Value)); \ @@ -106,30 +114,30 @@ static inline void cvector_##tag##_insert(CVector(tag)* self, cvector_size_t pos } \ \ \ -static inline Value cvector_##tag##_back(CVector(tag) cv) { \ +static inline Value cvector_##tag##_back(CVector_##tag cv) { \ return cv.data[_cvector_size(cv) - 1]; \ } \ \ \ -static inline void cvector_##tag##_pop(CVector(tag)* self) { \ +static inline void cvector_##tag##_pop(CVector_##tag* self) { \ valueDestroy(&self->data[_cvector_size(*self) - 1]); \ --_cvector_size(*self); \ } \ \ \ -static inline CVectorIter(tag) cvector_##tag##_begin(CVector(tag) vec) { \ - CVectorIter(tag) iter = {vec.data}; \ - return iter; \ +static inline cvector_##tag##_iter_t cvector_##tag##_begin(CVector_##tag vec) { \ + cvector_##tag##_iter_t it = {vec.data}; \ + return it; \ } \ \ -static inline CVectorIter(tag) cvector_##tag##_next(CVectorIter(tag) iter) { \ - ++iter.item; \ - return iter; \ +static inline cvector_##tag##_iter_t cvector_##tag##_next(cvector_##tag##_iter_t it) { \ + ++it.item; \ + return it; \ } \ \ -static inline CVectorIter(tag) cvector_##tag##_end(CVector(tag) vec) { \ - CVectorIter(tag) iter = {vec.data + cvector_size(vec)}; \ - return iter; \ +static inline cvector_##tag##_iter_t cvector_##tag##_end(CVector_##tag vec) { \ + cvector_##tag##_iter_t it = {vec.data + cvector_size(vec)}; \ + return it; \ } \ typedef Value cvector_##tag##_value_t -- cgit v1.2.3