diff options
| author | Tylo <[email protected]> | 2020-06-22 15:00:44 +0200 |
|---|---|---|
| committer | Tylo <[email protected]> | 2020-06-22 15:00:44 +0200 |
| commit | 6b060a2393cc3a20f19e0e482e335c959dd845bb (patch) | |
| tree | c98d864b6df792d865d717e5cad1033b85bb93d3 /examples | |
| parent | 389b09b637a00678f01affbea7491ce9dfdefd0d (diff) | |
| download | STC-modified-6b060a2393cc3a20f19e0e482e335c959dd845bb.tar.gz STC-modified-6b060a2393cc3a20f19e0e482e335c959dd845bb.zip | |
Updated CArray to support destructors, and added underscore before used defined tag - for consistency with the other containers.
Changed map/set type-tag for CHash to upper case MAP/SET. Makes it clearer it is a tag and not a type or variable.
Added complex example to README.md demonstrating capability of nested containers, using custom destructors.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/advanced.c | 2 | ||||
| -rw-r--r-- | examples/benchmark.c | 2 | ||||
| -rw-r--r-- | examples/demos.c | 33 |
3 files changed, 31 insertions, 6 deletions
diff --git a/examples/advanced.c b/examples/advanced.c index 092e3f01..37b67531 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -64,7 +64,7 @@ size_t personview_hash(const struct PersonView* pv, size_t ignore) { /*``` With this in place, we can declare the map Person -> int: ```*/ -declare_CHash(ex, map, struct Person, int, c_emptyDestroy, personview_hash, personview_compare, +declare_CHash(ex, MAP, struct Person, int, c_emptyDestroy, personview_hash, personview_compare, struct PersonView, person_destroy, person_getView, person_fromView); /*``` Note we use struct PersonView to put keys in the map, but keys are stored as struct Person with proper dynamically allocated CStrings to store name and surname. diff --git a/examples/benchmark.c b/examples/benchmark.c index 4d7a2fd1..dd070ff4 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -19,7 +19,7 @@ static inline uint32_t fibonacci_hash(const void* data, size_t len) { const uint64_t key = *(const uint64_t *) data;
return (uint32_t) (key * 11400714819323198485llu);
}
-declare_CHash(ii, map, int64_t, int64_t, c_emptyDestroy, fibonacci_hash); // c_lowbias32Hash);
+declare_CHash(ii, MAP, int64_t, int64_t, c_emptyDestroy, fibonacci_hash); // c_lowbias32Hash);
KHASH_MAP_INIT_INT64(ii, uint64_t)
diff --git a/examples/demos.c b/examples/demos.c index b2fa32ec..e05fa269 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -1,5 +1,6 @@ #include "../stc/cvector.h" #include "../stc/clist.h" +#include "../stc/carray.h" #include "../stc/chash.h" #include "../stc/cstring.h" @@ -98,7 +99,7 @@ void listdemo1() clist_ix_destroy(&nums); } -declare_CHash(i, set, int); +declare_CHash(i, SET, int); void setdemo1() { @@ -113,7 +114,7 @@ void setdemo1() } -declare_CHash(ii, map, int, int); +declare_CHash(ii, MAP, int, int); void mapdemo1() { @@ -127,7 +128,7 @@ void mapdemo1() } -declare_CHash_string(si, map, int); // Shorthand macro for the general declare_CHash expansion. +declare_CHash_string(si, MAP, int); // Shorthand macro for the general declare_CHash expansion. void mapdemo2() { @@ -149,7 +150,7 @@ void mapdemo2() } -declare_CHash_string(ss, map, CString, cstring_destroy); +declare_CHash_string(ss, MAP, CString, cstring_destroy); void mapdemo3() { @@ -169,6 +170,29 @@ void mapdemo3() +declare_CArray(f, float); + +int 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). + + 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. +} + + + int main() { stringdemo1(); @@ -179,4 +203,5 @@ int main() mapdemo1(); mapdemo2(); mapdemo3(); + arraydemo1(); } |
