From c47f6949cbd6d851eddb83cc1b9aa4e2cf3dbe61 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 22 Jul 2020 13:01:47 +0200 Subject: Renamed CArray to CArr. Renamed cmap_xx_get() to cmap_xx_find(). Changed _front() and _back() signatures. --- examples/advanced.c | 12 +++- examples/benchmark.c | 2 +- examples/bits.c | 2 +- examples/complex.c | 16 ++--- examples/demos.c | 34 ++++----- examples/geek1.c | 2 +- examples/geek2.c | 6 +- examples/geek3.c | 4 +- examples/geek5.c | 4 +- examples/geek6.c | 2 +- examples/geek7.c | 2 +- examples/mapmap.c | 14 ++-- stc/carr.h | 195 +++++++++++++++++++++++++++++++++++++++++++++++++++ stc/carray.h | 193 -------------------------------------------------- stc/cdefs.h | 6 +- stc/clist.h | 11 ++- stc/cmap.h | 12 ++-- stc/cvec.h | 24 ++++--- stc/cvecpq.h | 5 +- 19 files changed, 285 insertions(+), 261 deletions(-) create mode 100644 stc/carr.h delete mode 100644 stc/carray.h diff --git a/examples/advanced.c b/examples/advanced.c index 5d0bd2ed..b27f214d 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -63,13 +63,19 @@ declare_CMap(vk, Viking, int, c_defaultDestroy, vikingvw_equals, vikingvw_hash, int main() { +/* CMap_vk vikings = cmap_init; - // emplace constructs the keys cmap_vk_put(&vikings, (VikingVw) {"Einar", "Norway"}, 20); cmap_vk_put(&vikings, (VikingVw) {"Olaf", "Denmark"}, 24); cmap_vk_put(&vikings, (VikingVw) {"Harald", "Iceland"}, 12); - - CMapEntry_vk* e = cmap_vk_get(&vikings, (VikingVw) {"Einar", "Norway"}); +*/ + CMap_vk vikings = cmap_vk_from( (CMapInput_vk[]) { + {{"Einar", "Norway"}, 20}, + {{"Olaf", "Denmark"}, 24}, + {{"Harald", "Iceland"}, 12} + }, 3); + + CMapEntry_vk* e = cmap_vk_find(&vikings, (VikingVw) {"Einar", "Norway"}); e->value += 5; // update c_foreach (k, cmap_vk, vikings) { diff --git a/examples/benchmark.c b/examples/benchmark.c index 87242ec8..62a6cb52 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -35,7 +35,7 @@ sfc64_random_t rng; /* ; cmap_##tag##_setLoadFactors(&map, maxLoadFactor, 0.0)*/ #define CMAP_PUT(tag, key, val) cmap_##tag##_put(&map, key, val)->value #define CMAP_ERASE(tag, key) cmap_##tag##_erase(&map, key) -#define CMAP_FIND(tag, key) (cmap_##tag##_get(map, key) != NULL) +#define CMAP_FIND(tag, key) (cmap_##tag##_find(map, key) != NULL) #define CMAP_SIZE(tag) cmap_size(map) #define CMAP_BUCKETS(tag) cmap_bucketCount(map) #define CMAP_CLEAR(tag) cmap_##tag##_destroy(&map) diff --git a/examples/bits.c b/examples/bits.c index 9767f8a3..3a10e48c 100644 --- a/examples/bits.c +++ b/examples/bits.c @@ -1,5 +1,5 @@ #include -#include "cbitvec.h" +#include int main() { CBitVec vec = cbitvec_make(23, true); diff --git a/examples/complex.c b/examples/complex.c index 6cfed90c..d4800260 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -1,12 +1,12 @@ #include #include #include -#include +#include void check_destroy(float* v) {printf("destroy %g\n", *v);} -declare_CArray(f, float, check_destroy); // normally omit the last argument - float type need no destroy. -declare_CList(t2, CArray2_f, carray2_f_destroy, c_noCompare); +declare_CArr(f, float, check_destroy); // normally omit the last argument - float type need no destroy. +declare_CList(t2, CArr2_f, carr2_f_destroy, c_noCompare); declare_CMap(il, int, CList_t2, clist_t2_destroy); declare_CMap_str(sm, CMap_il, cmap_il_destroy); @@ -17,20 +17,20 @@ int main() { CMap_sm theMap = cmap_init; { // Construct. - CArray2_f table = carray2_f_make(ydim, xdim, 0.f); - printf("table: (%zu, %zu)\n", carray2_ydim(table), carray2_xdim(table)); + CArr2_f table = carr2_f_make(ydim, xdim, 0.f); + printf("table: (%zu, %zu)\n", carr2_ydim(table), carr2_xdim(table)); CList_t2 tableList = clist_init; CMap_il listMap = cmap_init; // Put in some data. - carray2_f_data(table, y)[x] = 3.1415927; // table[y][x] + carr2_f_data(table, y)[x] = 3.1415927; // table[y][x] clist_t2_pushBack(&tableList, table); cmap_il_put(&listMap, tableKey, tableList); cmap_sm_put(&theMap, strKey, listMap); } { // Access the data entry - CArray2_f table = clist_back(cmap_il_get(&cmap_sm_get(&theMap, strKey)->value, tableKey)->value); - printf("value (%d, %d) is: %f\n", y, x, carray2_f_value(table, y, x)); + CArr2_f table = clist_back(cmap_il_find(&cmap_sm_find(&theMap, strKey)->value, tableKey)->value); + printf("value (%d, %d) is: %f\n", y, x, carr2_f_value(table, y, x)); } cmap_sm_destroy(&theMap); // free up the whole shebang! diff --git a/examples/demos.c b/examples/demos.c index f09e0fbc..89918d33 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -123,7 +123,7 @@ void mapdemo1() cmap_ii_put(&nums, 8, 64); cmap_ii_put(&nums, 11, 121); - printf("get 8: %d\n", cmap_ii_get(&nums, 8)->value); + printf("get 8: %d\n", cmap_ii_find(&nums, 8)->value); cmap_ii_destroy(&nums); } @@ -159,7 +159,7 @@ void mapdemo3() cmap_ss_put(&table, "Map", cstr_make("test")); cmap_ss_put(&table, "Make", cstr_make("my")); cmap_ss_put(&table, "Sunny", cstr_make("day")); - CMapEntry_ss *e = cmap_ss_get(&table, "Make"); + CMapEntry_ss *e = cmap_ss_find(&table, "Make"); printf("size %zu: remove: Make: %s\n", cmap_size(table), e->value.str); cmap_ss_erase(&table, "Make"); //cmap_ss_eraseEntry(&table, e); @@ -172,28 +172,28 @@ void mapdemo3() -declare_CArray(f, float); +declare_CArr(f, float); void arraydemo1() { printf("\nARRAYDEMO1\n"); - CArray3_f a3 = carray3_f_make(30, 20, 10, 0.f); - carray3_f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3] - CArray2_f a2 = carray3_f_at(a3, 5); // sub-array reference (no data copy). + CArr3_f a3 = carr3_f_make(30, 20, 10, 0.f); + carr3_f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3] + CArr2_f a2 = carr3_f_at(a3, 5); // sub-array reference (no data copy). - printf("a3: %zu: (%zu, %zu, %zu) = %zu\n", sizeof(a3), carray3_xdim(a3), carray3_ydim(a3), carray3_zdim(a3), carray3_size(a3)); - printf("a2: %zu: (%zu, %zu) = %zu\n", sizeof(a2), carray2_xdim(a2), carray2_ydim(a2), carray2_size(a2)); + printf("a3: %zu: (%zu, %zu, %zu) = %zu\n", sizeof(a3), carr3_xdim(a3), carr3_ydim(a3), carr3_zdim(a3), carr3_size(a3)); + printf("a2: %zu: (%zu, %zu) = %zu\n", sizeof(a2), carr2_xdim(a2), carr2_ydim(a2), carr2_size(a2)); - printf("%f\n", carray2_f_value(a2, 4, 3)); // readonly lookup a2[4][3] (=10.2f) - printf("%f\n", carray2_f_data(a2, 4)[3]); // same, but this is writable. - printf("%f\n", carray2_f_at(a2, 4).data[3]); // same, via sub-array access. + printf("%f\n", carr2_f_value(a2, 4, 3)); // readonly lookup a2[4][3] (=10.2f) + printf("%f\n", carr2_f_data(a2, 4)[3]); // same, but this is writable. + printf("%f\n", carr2_f_at(a2, 4).data[3]); // same, via sub-array access. - printf("%f\n", carray3_f_value(a3, 5, 4, 3)); // same data location, via a3 array. - printf("%f\n", carray3_f_data(a3, 5, 4)[3]); - printf("%f\n", carray3_f_at2(a3, 5, 4).data[3]); + printf("%f\n", carr3_f_value(a3, 5, 4, 3)); // same data location, via a3 array. + printf("%f\n", carr3_f_data(a3, 5, 4)[3]); + printf("%f\n", carr3_f_at2(a3, 5, 4).data[3]); - carray2_f_destroy(&a2); // does nothing, since it is a sub-array. - carray3_f_destroy(&a3); // also invalidates a2. + carr2_f_destroy(&a2); // does nothing, since it is a sub-array. + carr3_f_destroy(&a3); // also invalidates a2. } diff --git a/examples/geek1.c b/examples/geek1.c index a8cf5b7b..bb4678df 100644 --- a/examples/geek1.c +++ b/examples/geek1.c @@ -38,7 +38,7 @@ int findMaximumPairs(int a[], int n, int k) int first = it.item->key; int second = k - it.item->key; - CMapEntry_ii *hf = cmap_ii_get(&hash, first), + CMapEntry_ii *hf = cmap_ii_find(&hash, first), *hs = cmap_ii_at(&hash, second, 0); // Check for minimal occurrence if (hf->value < hs->value) { diff --git a/examples/geek2.c b/examples/geek2.c index 32c0d03b..1c2f0c05 100644 --- a/examples/geek2.c +++ b/examples/geek2.c @@ -39,7 +39,7 @@ int main() // Check for a specific one. // When collections store owned values (String), they can still be // queried using references (&str). - if (! cmap_ss_get(&book_reviews, "Les Misérables")) { + if (! cmap_ss_find(&book_reviews, "Les Misérables")) { printf("We've got %zu reviews, but Les Misérables ain't one.\n", book_reviews.size); } @@ -50,13 +50,13 @@ int main() // Look up the values associated with some keys. const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland", NULL}; for (const char** book = to_find; *book; ++book) { - CMapEntry_ss* review = cmap_ss_get(&book_reviews, *book); + CMapEntry_ss* review = cmap_ss_find(&book_reviews, *book); if (review) printf("%s: %s\n", *book, review->value.str); else printf("%s is unreviewed.\n", *book); } // Look up the value for a key (will panic if the key is not found). - printf("Review for Jane: %s\n", cmap_ss_get(&book_reviews, "Pride and Prejudice")->value.str); + printf("Review for Jane: %s\n", cmap_ss_find(&book_reviews, "Pride and Prejudice")->value.str); // Iterate over everything. c_foreach (i, cmap_ss, book_reviews) { diff --git a/examples/geek3.c b/examples/geek3.c index 3ea93fe5..42e50a7c 100644 --- a/examples/geek3.c +++ b/examples/geek3.c @@ -43,8 +43,8 @@ int main () cmap_ss_put(&u, "WHITE", cstr_make("#FFFFFF")); // Output values by key - printf("The HEX of color RED is:[%s]\n", cmap_ss_get(&u, "RED")->value.str); - printf("The HEX of color BLACK is:[%s]\n", cmap_ss_get(&u, "BLACK")->value.str); + printf("The HEX of color RED is:[%s]\n", cmap_ss_find(&u, "RED")->value.str); + printf("The HEX of color BLACK is:[%s]\n", cmap_ss_find(&u, "BLACK")->value.str); cmap_ss_destroy(&u); return 0; } \ No newline at end of file diff --git a/examples/geek5.c b/examples/geek5.c index 2dbd0adb..546cb278 100644 --- a/examples/geek5.c +++ b/examples/geek5.c @@ -32,7 +32,7 @@ int NumOccurrences(const char* arr[], int n, const char* str, int L, int R) CMap_sv M = cmap_init; for (int i = 0; i < n; i++) { const char* temp = arr[i]; - CMapEntry_sv* it = cmap_sv_get(&M, temp); + CMapEntry_sv* it = cmap_sv_find(&M, temp); // If current string doesn't // have an entry in the map @@ -47,7 +47,7 @@ int NumOccurrences(const char* arr[], int n, const char* str, int L, int R) } } - CMapEntry_sv* it = cmap_sv_get(&M, str); + CMapEntry_sv* it = cmap_sv_find(&M, str); // If the given string is not // present in the array diff --git a/examples/geek6.c b/examples/geek6.c index ef86c3e8..6b70dd5e 100644 --- a/examples/geek6.c +++ b/examples/geek6.c @@ -52,7 +52,7 @@ int missingNumber(int a[], int n) // Return the first value starting // from 1 which does not exists in map while (1) { - if (cset_i_get(&mp, index) == NULL) { + if (cset_i_find(&mp, index) == NULL) { return index; } diff --git a/examples/geek7.c b/examples/geek7.c index 9ca34991..bac3f21b 100644 --- a/examples/geek7.c +++ b/examples/geek7.c @@ -48,7 +48,7 @@ void findElementsAfterDel(int arr[], int m, int del[], for (int i = 0; i < m; ++i) { // Search if the element is present - CMapEntry_ii *e = cmap_ii_get(&mp, arr[i]); + CMapEntry_ii *e = cmap_ii_find(&mp, arr[i]); if (e != NULL) { // Decrement its frequency diff --git a/examples/mapmap.c b/examples/mapmap.c index 35b5f510..30d7f0fb 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -11,10 +11,16 @@ declare_CMap(im, int, CMap_ii, cmap_ii_destroy); int main(void) { CMap_im m = cmap_init; - CMap_ii x = cmap_init; - cmap_ii_put(&cmap_im_put(&m, 100, x)->value, 200, 300); - cmap_ii_put(&cmap_im_get(&m, 100)->value, 200, 400); // update - cmap_ii_put(&cmap_im_put(&m, 110, x)->value, 200, 500); + CMap_ii ini = cmap_init; + cmap_ii_put(&cmap_im_at(&m, 100, ini)->value, 2000, 200); + cmap_ii_put(&cmap_im_at(&m, 100, ini)->value, 2001, 201); + cmap_ii_put(&cmap_im_at(&m, 100, ini)->value, 2000, 400); // update + cmap_ii_put(&cmap_im_at(&m, 110, ini)->value, 2000, 500); + cmap_ii_put(&cmap_im_at(&m, 120, ini)->value, 3000, 600); + + c_foreach (i, cmap_im, m) + c_foreach (j, cmap_ii, i.item->value) + printf("%d: %d - %d\n", i.item->key, j.item->key, j.item->value); cmap_im_destroy(&m); } \ No newline at end of file diff --git a/stc/carr.h b/stc/carr.h new file mode 100644 index 00000000..e80d0ad9 --- /dev/null +++ b/stc/carr.h @@ -0,0 +1,195 @@ +/* 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 CARR__H__ +#define CARR__H__ + +#include +#include "cdefs.h" + +/* + Multi-dimensional generic array allocated as one block of heap-memory. + // demo: +#include +declare_CArr(f, float); + +int main() +{ + CArr3_f a3 = carr3_f_make(30, 20, 10, 0.f); + carr3_f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3] + CArr2_f a2 = carr3_f_at(a3, 5); // sub-array reference (no data copy). + + printf("%f\n", carr2_f_value(a2, 4, 3)); // readonly lookup a2[4][3] (=10.2f) + printf("%f\n", carr2_f_data(a2, 4)[3]); // same, but this is writable. + printf("%f\n", carr2_f_at(a2, 4).data[3]); // same, via sub-array access. + + printf("%f\n", carr3_f_value(a3, 5, 4, 3)); // same data location, via a3 array. + printf("%f\n", carr3_f_data(a3, 5, 4)[3]); + printf("%f\n", carr3_f_at2(a3, 5, 4).data[3]); + + carr2_f_destroy(&a2); // does nothing, since it is a sub-array. + carr3_f_destroy(&a3); // also invalidates a2. +} +*/ + +#define carr1_xdim(a) ((a)._xdim & _carr_SUB) +#define carr1_size(a) carr1_xdim(a) + +#define carr2_xdim(a) carr1_xdim(a) +#define carr2_ydim(a) _carr2_ydim(&(a)._yxdim) +#define carr2_size(a) ((a)._yxdim) + +#define carr3_xdim(a) carr1_xdim(a) +#define carr3_ydim(a) carr2_ydim(a) +#define carr3_zdim(a) ((a)._zdim) +#define carr3_size(a) _carr3_size(&(a)._zdim) + +#define _carr_SUB (SIZE_MAX >> 1) +#define _carr_OWN (_carr_SUB + 1) + +static inline size_t _carr2_ydim(const size_t* yxdim) { + return yxdim[0] / (yxdim[-1] & _carr_SUB); +} +static inline size_t _carr3_size(const size_t* zdim) { + return zdim[0] * zdim[-1]; +} + + +#define declare_CArr(...) c_MACRO_OVERLOAD(declare_CArr, __VA_ARGS__) + +#define declare_CArr_2(tag, Value) \ + declare_CArr_3(tag, Value, c_defaultDestroy) + + +#define declare_CArr_3(tag, Value, valueDestroy) \ + typedef struct { \ + Value *data; \ + size_t _xdim; \ + } CArr1_##tag; \ + \ + typedef struct { \ + Value *data; \ + size_t _xdim, _yxdim; \ + } CArr2_##tag; \ + \ + typedef struct { \ + Value *data; \ + size_t _xdim, _yxdim, _zdim; \ + } CArr3_##tag; \ + \ + static inline CArr1_##tag \ + carr1_##tag##_make(size_t xdim, Value val) { \ + Value* m = c_new_N(Value, xdim); \ + for (size_t i=0; i_xdim & _carr_OWN) { \ + size_t n = carr1_size(*self); Value* a = self->data; \ + while (n--) valueDestroy(&a[n]); free(a); \ + } \ + } \ + static inline void \ + carr2_##tag##_destroy(CArr2_##tag* self) { \ + if (self->_xdim & _carr_OWN) { \ + size_t n = carr2_size(*self); Value* a = self->data; \ + while (n--) valueDestroy(&a[n]); free(a); \ + } \ + } \ + static inline void \ + carr3_##tag##_destroy(CArr3_##tag* self) { \ + if (self->_xdim & _carr_OWN) { \ + size_t n = carr3_size(*self); Value* a = self->data; \ + while (n--) valueDestroy(&a[n]); free(a); \ + } \ + } \ + \ + static inline CArr1_##tag \ + carr2_##tag##_at(CArr2_##tag a, size_t y) { \ + CArr1_##tag sub = {a.data + y*carr2_xdim(a), carr2_xdim(a)}; \ + return sub; \ + } \ + static inline Value* \ + carr2_##tag##_data(CArr2_##tag a, size_t y) { \ + return a.data + y*carr2_xdim(a); \ + } \ + static inline Value \ + carr2_##tag##_value(CArr2_##tag a, size_t y, size_t x) { \ + return a.data[ y*carr2_xdim(a) + x ]; \ + } \ + \ + static inline CArr2_##tag \ + carr3_##tag##_at(CArr3_##tag a, size_t z) { \ + CArr2_##tag sub = {a.data + z*a._yxdim, carr3_xdim(a), a._yxdim}; \ + return sub; \ + } \ + static inline CArr1_##tag \ + carr3_##tag##_at2(CArr3_##tag a, size_t z, size_t y) { \ + CArr1_##tag sub = {a.data + z*a._yxdim + y*carr3_xdim(a), carr3_xdim(a)}; \ + return sub; \ + } \ + static inline Value* \ + carr3_##tag##_data(CArr3_##tag a, size_t z, size_t y) { \ + return a.data + z*a._yxdim + y*carr3_xdim(a); \ + } \ + static inline Value \ + carr3_##tag##_value(CArr3_##tag a, size_t z, size_t y, size_t x) { \ + return a.data[ z*a._yxdim + y*carr3_xdim(a) + x ]; \ + } \ + typedef Value CArrValue_##tag + +#endif diff --git a/stc/carray.h b/stc/carray.h deleted file mode 100644 index 4b5ab171..00000000 --- a/stc/carray.h +++ /dev/null @@ -1,193 +0,0 @@ -/* 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 CARRAY__H__ -#define CARRAY__H__ - -#include -#include "cdefs.h" - -/* // demo: -#include -declare_CArray(f, float); - -int main() -{ - CArray3_f a3 = carray3_f_make(30, 20, 10, 0.f); - carray3_f_data(a3, 5, 4)[3] = 10.2f; // a3[5][4][3] - CArray2_f a2 = carray3_f_at(a3, 5); // sub-array reference (no data copy). - - printf("%f\n", carray2_f_value(a2, 4, 3)); // readonly lookup a2[4][3] (=10.2f) - printf("%f\n", carray2_f_data(a2, 4)[3]); // same, but this is writable. - printf("%f\n", carray2_f_at(a2, 4).data[3]); // same, via sub-array access. - - printf("%f\n", carray3_f_value(a3, 5, 4, 3)); // same data location, via a3 array. - printf("%f\n", carray3_f_data(a3, 5, 4)[3]); - printf("%f\n", carray3_f_at2(a3, 5, 4).data[3]); - - carray2_f_destroy(&a2); // does nothing, since it is a sub-array. - carray3_f_destroy(&a3); // also invalidates a2. -} -*/ - -#define carray1_xdim(a) ((a)._xdim & _carray_SUB) -#define carray1_size(a) carray1_xdim(a) - -#define carray2_xdim(a) carray1_xdim(a) -#define carray2_ydim(a) _carray2_ydim(&(a)._yxdim) -#define carray2_size(a) ((a)._yxdim) - -#define carray3_xdim(a) carray1_xdim(a) -#define carray3_ydim(a) carray2_ydim(a) -#define carray3_zdim(a) ((a)._zdim) -#define carray3_size(a) _carray3_size(&(a)._zdim) - -#define _carray_SUB (SIZE_MAX >> 1) -#define _carray_OWN (_carray_SUB + 1) - -static inline size_t _carray2_ydim(const size_t* yxdim) { - return yxdim[0] / (yxdim[-1] & _carray_SUB); -} -static inline size_t _carray3_size(const size_t* zdim) { - return zdim[0] * zdim[-1]; -} - - -#define declare_CArray(...) c_MACRO_OVERLOAD(declare_CArray, __VA_ARGS__) - -#define declare_CArray_2(tag, Value) \ - declare_CArray_3(tag, Value, c_defaultDestroy) - - -#define declare_CArray_3(tag, Value, valueDestroy) \ - typedef struct { \ - Value *data; \ - size_t _xdim; \ - } CArray1_##tag; \ - \ - typedef struct { \ - Value *data; \ - size_t _xdim, _yxdim; \ - } CArray2_##tag; \ - \ - typedef struct { \ - Value *data; \ - size_t _xdim, _yxdim, _zdim; \ - } CArray3_##tag; \ - \ - static inline CArray1_##tag \ - carray1_##tag##_make(size_t xdim, Value val) { \ - Value* m = c_new_N(Value, xdim); \ - for (size_t i=0; i_xdim & _carray_OWN) { \ - size_t n = carray1_size(*self); Value* a = self->data; \ - while (n--) valueDestroy(&a[n]); free(a); \ - } \ - } \ - static inline void \ - carray2_##tag##_destroy(CArray2_##tag* self) { \ - if (self->_xdim & _carray_OWN) { \ - size_t n = carray2_size(*self); Value* a = self->data; \ - while (n--) valueDestroy(&a[n]); free(a); \ - } \ - } \ - static inline void \ - carray3_##tag##_destroy(CArray3_##tag* self) { \ - if (self->_xdim & _carray_OWN) { \ - size_t n = carray3_size(*self); Value* a = self->data; \ - while (n--) valueDestroy(&a[n]); free(a); \ - } \ - } \ - \ - static inline CArray1_##tag \ - carray2_##tag##_at(CArray2_##tag a, size_t y) { \ - CArray1_##tag sub = {a.data + y*carray2_xdim(a), carray2_xdim(a)}; \ - return sub; \ - } \ - static inline Value* \ - carray2_##tag##_data(CArray2_##tag a, size_t y) { \ - return a.data + y*carray2_xdim(a); \ - } \ - static inline Value \ - carray2_##tag##_value(CArray2_##tag a, size_t y, size_t x) { \ - return a.data[ y*carray2_xdim(a) + x ]; \ - } \ - \ - static inline CArray2_##tag \ - carray3_##tag##_at(CArray3_##tag a, size_t z) { \ - CArray2_##tag sub = {a.data + z*a._yxdim, carray3_xdim(a), a._yxdim}; \ - return sub; \ - } \ - static inline CArray1_##tag \ - carray3_##tag##_at2(CArray3_##tag a, size_t z, size_t y) { \ - CArray1_##tag sub = {a.data + z*a._yxdim + y*carray3_xdim(a), carray3_xdim(a)}; \ - return sub; \ - } \ - static inline Value* \ - carray3_##tag##_data(CArray3_##tag a, size_t z, size_t y) { \ - return a.data + z*a._yxdim + y*carray3_xdim(a); \ - } \ - static inline Value \ - carray3_##tag##_value(CArray3_##tag a, size_t z, size_t y, size_t x) { \ - return a.data[ z*a._yxdim + y*carray3_xdim(a) + x ]; \ - } \ - typedef Value CArrayValue_##tag - -#endif diff --git a/stc/cdefs.h b/stc/cdefs.h index 88c160a4..9ab3ed79 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -64,10 +64,12 @@ #define c_defaultInitRaw(x) (x) #define c_defaultGetRaw(ptr) (*(ptr)) -#define c_defaultCompare(x, y) (*(x) < *(y) ? -1 : *(y) < *(x)) #define c_noCompare(x, y) (0) -#define c_defaultEquals(x, y) (*(x) == *(y)) #define c_memEquals(x, y) (memcmp(x, y, sizeof(*(y))) == 0) +#define c_defaultEquals(x, y) (*(x) == *(y)) +#define c_defaultLess(x, y) (*(x) < *(y)) +#define c_compare(less, x, y) (less(x, y) ? -1 : less(y, x)) +#define c_defaultCompare(x, y) c_compare(c_defaultLess, x, y) #define c_defaultDestroy(p) ((void)0) #define c_foreach(it, prefix, container) \ diff --git a/stc/clist.h b/stc/clist.h index 9d26f7f3..7f9be5e6 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -118,16 +118,21 @@ STC_API void \ clist_##tag##_sort(CList_##tag* self); \ \ - static inline clist_##tag##_iter_t \ + STC_INLINE Value* \ + clist_##tag##_front(CList_##tag* self) {return &self->last->next->value;} \ + STC_INLINE Value* \ + clist_##tag##_back(CList_##tag* self) {return &self->last->value;} \ + \ + STC_INLINE clist_##tag##_iter_t \ clist_##tag##_begin(CList_##tag* self) { \ CListNode_##tag *head = self->last ? self->last->next : NULL; \ clist_##tag##_iter_t it = {head, &self->last}; return it; \ } \ - static inline clist_##tag##_iter_t \ + STC_INLINE clist_##tag##_iter_t \ clist_##tag##_next(clist_##tag##_iter_t it) { \ it.item = it.item == *it._last ? NULL : it.item->next; return it; \ } \ - static inline clist_##tag##_iter_t \ + STC_INLINE clist_##tag##_iter_t \ clist_##tag##_last(CList_##tag* self) { \ clist_##tag##_iter_t it = {self->last, &self->last}; return it; \ } \ diff --git a/stc/cmap.h b/stc/cmap.h index c0a887b4..d9a3ed36 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -38,8 +38,8 @@ int main(void) { cmap_mx_put(&m, 5, 'a'); cmap_mx_put(&m, 8, 'b'); cmap_mx_put(&m, 12, 'c'); - CMapEntry_mx *e = cmap_mx_get(&m, 10); // = NULL - char val = cmap_mx_get(&m, 5)->value; + CMapEntry_mx *e = cmap_mx_find(&m, 10); // = NULL + char val = cmap_mx_find(&m, 5)->value; cmap_mx_put(&m, 5, 'd'); // update cmap_mx_erase(&m, 8); c_foreach (i, cmap_mx, m) printf("map %d: %c\n", i.item->key, i.item->value); @@ -168,10 +168,10 @@ ctype##_##tag##_clear(CType##_##tag* self); \ STC_API void \ ctype##_##tag##_setLoadFactors(CType##_##tag* self, float maxLoadFactor, float shrinkLimitFactor); \ STC_API CType##Entry_##tag* \ -ctype##_##tag##_get(const CType##_##tag* self, CType##RawKey_##tag rawKey); \ -STC_API CType##Entry_##tag* \ +ctype##_##tag##_find(const CType##_##tag* self, CType##RawKey_##tag rawKey); \ +STC_API CType##Entry_##tag* /* similar to c++ std::map.insert_or_assign(): */ \ ctype##_##tag##_put(CType##_##tag* self, OPT_2_##ctype(CType##RawKey_##tag rawKey, Value value)); \ -OPT_1_##ctype(STC_API CType##Entry_##tag* \ +OPT_1_##ctype(STC_API CType##Entry_##tag* /* similar to c++ std::map.operator[](): */ \ ctype##_##tag##_at(CType##_##tag* self, CType##RawKey_##tag rawKey, Value initValue);) \ STC_INLINE void \ ctype##_##tag##_swap(CType##_##tag* a, CType##_##tag* b) { c_swap(CType##_##tag, *a, *b); } \ @@ -252,7 +252,7 @@ ctype##_##tag##_bucket(const CType##_##tag* self, const CType##RawKey_##tag* raw } \ \ STC_API CType##Entry_##tag* \ -ctype##_##tag##_get(const CType##_##tag* self, CType##RawKey_##tag rawKey) { \ +ctype##_##tag##_find(const CType##_##tag* self, CType##RawKey_##tag rawKey) { \ if (self->size == 0) return NULL; \ uint32_t hx; \ size_t idx = ctype##_##tag##_bucket(self, &rawKey, &hx); \ diff --git a/stc/cvec.h b/stc/cvec.h index c2dfd525..35110294 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -66,10 +66,12 @@ cvec_##tag##_popBack(CVec_##tag* self) { \ valueDestroy(&self->data[_cvec_size(*self) - 1]); \ --_cvec_size(*self); \ } \ -STC_INLINE Value \ -cvec_##tag##_back(CVec_##tag cv) { \ - return cv.data[_cvec_size(cv) - 1]; \ -} \ +STC_INLINE Value* \ +cvec_##tag##_front(CVec_##tag* self) {return self->data;} \ +STC_INLINE Value* \ +cvec_##tag##_back(CVec_##tag* self) {return self->data + _cvec_size(*self) - 1;} \ +STC_INLINE Value* \ +cvec_##tag##_at(CVec_##tag* self, size_t i) {return self->data + i;} \ STC_API void \ cvec_##tag##_insert(CVec_##tag* self, size_t pos, Value value); \ STC_API void \ @@ -77,7 +79,7 @@ cvec_##tag##_erase(CVec_##tag* self, size_t pos, size_t size); \ STC_API void \ cvec_##tag##_sort(CVec_##tag* self); \ STC_API size_t \ -cvec_##tag##_find(CVec_##tag cv, RawValue rawValue); \ +cvec_##tag##_find(const CVec_##tag* self, RawValue rawValue); \ STC_INLINE void \ cvec_##tag##_swap(CVec_##tag* a, CVec_##tag* b) { \ c_swap(Value*, a->data, b->data); \ @@ -182,13 +184,13 @@ cvec_##tag##_erase(CVec_##tag* self, size_t pos, size_t size) { \ } \ \ STC_API size_t \ -cvec_##tag##_find(CVec_##tag cv, RawValue rawValue) { \ - size_t n = cvec_size(cv); \ - for (size_t i = 0; i < n; ++i) { \ - RawValue r = valueGetRaw(&cv.data[i]); \ - if (valueCompareRaw(&r, &rawValue) == 0) return i; \ +cvec_##tag##_find(const CVec_##tag* self, RawValue rawValue) { \ + const Value *p = self->data, *end = p + cvec_size(*self); \ + for (; p != end; ++p) { \ + RawValue r = valueGetRaw(p); \ + if (valueCompareRaw(&r, &rawValue) == 0) return p - self->data; \ } \ - return (size_t) (-1); /*SIZE_MAX;*/ \ + return SIZE_MAX; /*(size_t) (-1)*/ \ } \ \ STC_API int \ diff --git a/stc/cvecpq.h b/stc/cvecpq.h index 441859a5..993ca841 100644 --- a/stc/cvecpq.h +++ b/stc/cvecpq.h @@ -84,9 +84,10 @@ _cvec_##tag##_siftDown(CVecValue_##tag* arr, size_t i, size_t n) { \ \ STC_API void \ cvec_##tag##_eraseFromPriorityQ(CVec_##tag* self, size_t i) { \ - self->data[i] = cvec_##tag##_back(*self); \ + size_t n = cvec_size(*self) - 1; \ + self->data[i] = self->data[n]; \ cvec_##tag##_popBack(self); \ - _cvec_##tag##_siftDown(self->data - 1, i + 1, cvec_size(*self)); \ + _cvec_##tag##_siftDown(self->data - 1, i + 1, n); \ } \ \ STC_API void \ -- cgit v1.2.3