diff options
| author | Tyge Løvset <[email protected]> | 2022-04-27 13:23:10 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-04-27 13:23:10 +0200 |
| commit | 8178a02c4048cef03952a075f87da91b6ec2ee96 (patch) | |
| tree | bc5b9a2fc2c64ae5a1b3eb665e828e4246707247 /examples | |
| parent | e6738b3da5cbd7703cf3fbd905b3fdac19d0ae24 (diff) | |
| download | STC-modified-8178a02c4048cef03952a075f87da91b6ec2ee96.tar.gz STC-modified-8178a02c4048cef03952a075f87da91b6ec2ee96.zip | |
Finally FIXED cloning/to/from scheme to work properly. When using i_key_bind/i_val_bind, a .._clone() function *must* always be defined.
Functions .._from and .._toraw is only required when i_keyraw/i_valraw type is defined.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/complex.c | 1 | ||||
| -rw-r--r-- | examples/lower_bound.c | 39 | ||||
| -rw-r--r-- | examples/vikings.c | 32 |
3 files changed, 47 insertions, 25 deletions
diff --git a/examples/complex.c b/examples/complex.c index ef7f711f..12b665c8 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -5,7 +5,6 @@ void check_drop(float* v) {printf("destroy %g\n", *v);} #define i_type FloatStack
#define i_val float
#define i_valdrop check_drop
-#define i_valfrom c_default_from
#include <stc/cstack.h>
#define i_type StackList
diff --git a/examples/lower_bound.c b/examples/lower_bound.c index 578209f8..1d9f5843 100644 --- a/examples/lower_bound.c +++ b/examples/lower_bound.c @@ -6,42 +6,63 @@ #define i_val int
#include <stc/csset.h>
+
+#define i_val char
+#include <stc/cvec.h>
int main()
{
- c_auto (csset_int, set)
+ // TEST SORTED VECTOR
c_auto (cvec_int, vec)
{
int key, *res;
- // TEST SORTED VECTOR
c_apply(t, cvec_int_push(&vec, t), int, {
40, 600, 1, 7000, 2, 500, 30,
});
cvec_int_sort(&vec);
+ key = 500;
+ res = cvec_int_lower_bound(&vec, key).ref;
+ if (res != cvec_int_end(&vec).ref)
+ printf("Sorted Vec %d: lower bound: %d\n", key, *res); // 600
+
key = 550;
res = cvec_int_lower_bound(&vec, key).ref;
+ if (res != cvec_int_end(&vec).ref)
+ printf("Sorted Vec %d: lower_bound: %d\n", key, *res); // 500
+ key = 500;
+ res = cvec_int_binary_search(&vec, key).ref;
if (res != cvec_int_end(&vec).ref)
- printf("Vec key %d: lower bound: %d\n", key, *res); // 600
- else
- printf("Vec element not found\n");
+ printf("Sorted Vec %d: bin. search: %d\n", key, *res); // 500
+ puts("");
+ }
+ // TEST SORTED SET
+ c_auto (csset_int, set)
+ {
+ int key, *res;
- // TEST SORTED SET
c_apply(t, csset_int_push(&set, t), int, {
40, 600, 1, 7000, 2, 500, 30,
});
+ key = 500;
+ res = csset_int_lower_bound(&set, key).ref;
+ if (res != csset_int_end(&set).ref)
+ printf("Sorted Set %d: lower bound: %d\n", key, *res); // 600
+
key = 550;
res = csset_int_lower_bound(&set, key).ref;
+ if (res != csset_int_end(&set).ref)
+ printf("Sorted Set %d: lower bound: %d\n", key, *res); // 600
+ key = 500;
+ res = csset_int_find(&set, key).ref;
if (res != csset_int_end(&set).ref)
- printf("Set key %d: lower bound: %d\n", key, *res); // 600
- else
- printf("Set element not found\n");
+ printf("Sorted Set %d: find : %d\n", key, *res); // 600
}
return 0;
}
diff --git a/examples/vikings.c b/examples/vikings.c index 51ab92f8..bd4d379f 100644 --- a/examples/vikings.c +++ b/examples/vikings.c @@ -11,17 +11,13 @@ void Viking_drop(Viking* vk) { cstr_drop(&vk->country); } -// Define Viking raw struct with hash, equals, and convertion functions between Viking and RViking structs: +// Define Viking lookup struct with hash, cmp, and convertion functions between Viking and RViking structs: typedef struct RViking { const char* name; const char* country; } RViking; -uint64_t RViking_hash(const RViking* raw) { - uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15); - return hash; -} static inline int RViking_cmp(const RViking* rx, const RViking* ry) { int c = strcmp(rx->name, ry->name); return c ? c : strcmp(rx->country, ry->country); @@ -30,20 +26,26 @@ static inline int RViking_cmp(const RViking* rx, const RViking* ry) { static inline Viking Viking_from(RViking raw) { // note: parameter is by value return c_make(Viking){cstr_from(raw.name), cstr_from(raw.country)}; } -static inline RViking Viking_toraw(const Viking* vk) { - return c_make(RViking){cstr_str(&vk->name), cstr_str(&vk->country)}; +static inline Viking Viking_clone(Viking vk) { // note: parameter is by value + vk.name = cstr_clone(vk.name), vk.country = cstr_clone(vk.country); + return vk; +} +static inline RViking Viking_toraw(const Viking* vp) { + return c_make(RViking){cstr_str(&vp->name), cstr_str(&vp->country)}; } // With this in place, we define the Viking => int hash map type: #define i_type Vikings -#define i_key_bind Viking -#define i_keyraw RViking -#define i_val int -// i_key_bind auto-binds these functions: -// i_hash => Viking_hash -// i_cmp => Viking_cmp -// i_keyfrom => Viking_from // not _clone because i_keyraw is defined -// i_keyto => Viking_toraw +#define i_key_bind Viking // key type +#define i_val int // mapped type +#define i_keyraw RViking // lookup type +#define i_hash(rp) c_strhash(rp->name) ^ c_strhash(rp->country) +// i_key_bind auto-binds these functions (unless they are defined by i_...): +// i_cmp => RViking_cmp +// i_hash => RViking_hash +// i_keyclone => Viking_clone +// i_keyfrom => Viking_from // because i_keyraw is defined +// i_keyto => Viking_toraw // because i_keyraw is defined // i_keydrop => Viking_drop #include <stc/cmap.h> |
