From 08d4070fef3470e510b09d083c3fb0f21e114d36 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 23 Jul 2020 16:55:09 +0200 Subject: Added generic c_push(.., items) to push a list of items onto the containers. --- examples/inits.c | 95 +++++++++++++++++++++++++++++++++----------------------- stc/cdefs.h | 6 ++++ stc/clist.h | 14 ++++----- stc/cmap.h | 24 +++++++------- stc/cstr.h | 3 +- stc/cvec.h | 18 +++++------ 6 files changed, 89 insertions(+), 71 deletions(-) diff --git a/examples/inits.c b/examples/inits.c index eb0968b4..64e85a48 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -1,56 +1,75 @@ #include -#include #include +#include +#include +#include + +declare_CMap(id, int, CStr, cstr_destroy); // Map of int -> CStr +declare_CMap_str(cnt, int); + +typedef struct {int x, y;} ipair_t; +declare_CVec(ip, ipair_t, c_defaultDestroy, c_memCompare); +declare_CList(ip, ipair_t, c_defaultDestroy, c_memCompare); -declare_CSet(sx, int); // Set of int -declare_CMap(mx, int, char); // Map of int -> char -declare_CMap(ms, int, CStr, cstr_destroy); // Map of int -> CStr -declare_CMap_str(si, int); int main(void) { int year = 2020; - CMapInput_ms ini[] = { - 100, cstr_make("Hello"), - 110, cstr_make("World"), - 120, cstr_from("Howdy, -%d-", year), - }; - CMap_ms ms = cmap_ms_from(ini, 3); - - c_foreach (i, cmap_ms, ms) + CMap_id idnames = cmap_init; + c_push(&idnames, cmap_id, c_items( + {100, cstr_make("Hello")}, + {110, cstr_make("World")}, + {120, cstr_from("Howdy, -%d-", year)}, + )); + + c_foreach (i, cmap_id, idnames) printf("%d: %s\n", i.item->key, i.item->value.str); - cmap_ms_destroy(&ms); + cmap_id_destroy(&idnames); + // ------------------ + CMap_cnt countries = cmap_init; - CMap_si si = cmap_si_from((CMapInput_si[]) { - "Norway", 100, - "Denmark", 50, - "Iceland", 10 - }, 3); + cmap_cnt_at(&countries, "Greenland", 0)->value += 20; + c_push(&countries, cmap_cnt, c_items( + {"Norway", 100}, + {"Denmark", 50}, + {"Iceland", 10}, + )); - cmap_si_at(&si, "Sweden", 0)->value += 20; - cmap_si_at(&si, "Norway", 0)->value += 20; - cmap_si_at(&si, "Finland", 0)->value += 20; + cmap_cnt_at(&countries, "Sweden", 0)->value += 20; + cmap_cnt_at(&countries, "Norway", 0)->value += 20; + cmap_cnt_at(&countries, "Finland", 0)->value += 20; - c_foreach (i, cmap_si, si) + c_foreach (i, cmap_cnt, countries) printf("%s: %d\n", i.item->key.str, i.item->value); - cmap_si_destroy(&si); + cmap_cnt_destroy(&countries); + // ------------------ - CSet_sx s = cset_init; - cset_sx_put(&s, 5); - cset_sx_put(&s, 8); - c_foreach (i, cset_sx, s) printf("set %d\n", i.item->key); - cset_sx_destroy(&s); + CVec_ip pairs1 = cvec_init; + c_push(&pairs1, cvec_ip, c_items( + {1, 2}, + {3, 4}, + {5, 6}, + {7, 8}, + )); + + c_foreach (i, cvec_ip, pairs1) + printf("(%d %d) ", i.item->x, i.item->y); + puts(""); + cvec_ip_destroy(&pairs1); + // ------------------ - CMap_mx m = cmap_mx_from((CMapInput_mx[]) { - {5, 'a'}, {8, 'b'}, {12, 'c'} - }, 3); + CList_ip pairs2 = clist_init; + c_push(&pairs2, clist_ip, c_items( + {1, 2}, + {3, 4}, + {5, 6}, + {7, 8}, + )); - 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); - cmap_mx_destroy(&m); + c_foreach (i, clist_ip, pairs2) + printf("(%d %d) ", i.item->value.x, i.item->value.y); + puts(""); + clist_ip_destroy(&pairs2); } \ No newline at end of file diff --git a/stc/cdefs.h b/stc/cdefs.h index 9ab3ed79..9fced5ab 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -66,6 +66,7 @@ #define c_defaultGetRaw(ptr) (*(ptr)) #define c_noCompare(x, y) (0) #define c_memEquals(x, y) (memcmp(x, y, sizeof(*(y))) == 0) +#define c_memCompare(x, y) memcmp(x, y, sizeof(*(y))) #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)) @@ -74,6 +75,11 @@ #define c_foreach(it, prefix, container) \ for (prefix##_iter_t it = prefix##_begin(&container); it.item; it = prefix##_next(it)) +#define c_items(...) {__VA_ARGS__} +#define c_push(container, prefix, list) do { \ + const prefix##_input_t __arr[] = list; \ + prefix##_pushN(container, __arr, sizeof(__arr)/sizeof(prefix##_input_t)); \ +} while (0) /* One-byte-at-a-time hash based on Murmur's mix */ static inline uint32_t c_defaultHash(const void *data, size_t len) { diff --git a/stc/clist.h b/stc/clist.h index 7f9be5e6..fbfb4817 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -91,8 +91,8 @@ \ declare_CListTypes(tag, Value); \ \ - STC_API CList_##tag \ - clist_##tag##_from(const Value in[], size_t size); \ + STC_API void \ + clist_##tag##_pushN(CList_##tag *self, const Value in[], size_t size); \ STC_API void \ clist_##tag##_destroy(CList_##tag* self); \ STC_API void \ @@ -139,7 +139,7 @@ \ implement_CList_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \ typedef RawValue CListRawValue_##tag; \ - typedef Value CListValue_##tag + typedef Value CListValue_##tag, clist_##tag##_input_t /* -------------------------- IMPLEMENTATION ------------------------- */ @@ -147,11 +147,9 @@ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) #define implement_CList_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \ \ - STC_API CList_##tag \ - clist_##tag##_from(const Value in[], size_t size) { \ - CList_##tag lst = clist_init; \ - for (size_t i=0; ikey); \ OPT_1_##ctype(valueDestroy(&e->value);) \ } \ -typedef const struct { \ +typedef struct { \ RawKey key; \ OPT_1_##ctype(Value value;) \ -} CType##Input_##tag; \ +} CType##Input_##tag, ctype##_##tag##_input_t; \ \ -typedef RawKey CType##RawKey_##tag; \ +typedef RawKey CType##RawKey_##tag, ctype##_##tag##_rawkey_t; \ \ typedef struct { \ CType##Entry_##tag* table; \ @@ -160,8 +160,8 @@ typedef struct { \ \ STC_API CType##_##tag \ ctype##_##tag##_make(size_t initialSize); \ -STC_API CType##_##tag \ -ctype##_##tag##_from(const CType##Input_##tag in[], size_t size); \ +STC_API void \ +ctype##_##tag##_pushN(CType##_##tag* self, const CType##Input_##tag in[], size_t size); \ STC_API void \ ctype##_##tag##_destroy(CType##_##tag* self); \ STC_API void \ @@ -191,8 +191,8 @@ ctype##_##tag##_next(ctype##_##tag##_iter_t it); \ \ implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ keyDestroy, RawKey, keyGetRaw, keyInitRaw) \ -typedef Key CType##Key_##tag; \ -typedef Value CType##Value_##tag +typedef Key CType##Key_##tag, ctype##_##tag##_key_t; \ +typedef Value CType##Value_##tag, ctype##_##tag##_value_t /* -------------------------- IMPLEMENTATION ------------------------- */ @@ -206,11 +206,9 @@ ctype##_##tag##_make(size_t initialSize) { \ ctype##_##tag##_reserve(&h, initialSize); \ return h; \ } \ -STC_API CType##_##tag \ -ctype##_##tag##_from(const CType##Input_##tag in[], size_t size) { \ - CType##_##tag hh = ctype##_init; \ - for (size_t i=0; i 0) { tmp = cstr_makeReserved(len); vsprintf(tmp.str, fmt, args); diff --git a/stc/cvec.h b/stc/cvec.h index 35110294..852fb480 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -51,8 +51,8 @@ typedef struct CVec_##tag { \ \ STC_API CVec_##tag \ cvec_##tag##_make(size_t size, Value null); \ -STC_API CVec_##tag \ -cvec_##tag##_from(const Value in[], size_t size); \ +STC_API void \ +cvec_##tag##_pushN(CVec_##tag *self, const Value in[], size_t size); \ STC_API void \ cvec_##tag##_destroy(CVec_##tag* self); \ STC_API void \ @@ -102,7 +102,7 @@ cvec_##tag##_next(cvec_##tag##_iter_t it) { \ } \ \ implement_CVec_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \ -typedef Value CVecValue_##tag; \ +typedef Value CVecValue_##tag, cvec_##tag##_input_t; \ typedef RawValue CVecRawValue_##tag /* -------------------------- IMPLEMENTATION ------------------------- */ @@ -118,13 +118,11 @@ cvec_##tag##_make(size_t size, Value null) { \ for (size_t i=0; idata[i] = in[i]; \ } \ \ STC_API void \ -- cgit v1.2.3