summaryrefslogtreecommitdiffhomepage
path: root/examples/complex.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-07-14 12:11:09 +0200
committerTyge Løvset <[email protected]>2020-07-14 12:11:09 +0200
commit5baa9566801e902afaad733b6a23e8cfb7e376ee (patch)
treee9f794c454612ee1a249ed917c6b96003ffa09e2 /examples/complex.c
parent3e0e248a229d416a9c02fbb676ec86b5e72af99f (diff)
downloadSTC-modified-5baa9566801e902afaad733b6a23e8cfb7e376ee.tar.gz
STC-modified-5baa9566801e902afaad733b6a23e8cfb7e376ee.zip
Redefined declare_CHash() interface, reverted back to original. declare_CHash(tag, Key, Value, ...).
To define sets, use declare_CHash_set(tag, Key, ...); with only two params, you also may use: declare_CHash(tag, Key). Also added method: at, which works like c++ map[key] operator: inserts a new key if not existing (with given value 0), else return existing. Useful for e.g: ++chash_ii_at(&map, key, 0)->value;
Diffstat (limited to 'examples/complex.c')
-rw-r--r--examples/complex.c72
1 files changed, 36 insertions, 36 deletions
diff --git a/examples/complex.c b/examples/complex.c
index 91a1d4c2..8c44f6cc 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -1,37 +1,37 @@
-#include "../stc/cstring.h"
-#include "../stc/chash.h"
-#include "../stc/clist.h"
-#include "../stc/carray.h"
-
-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_CHash(il, MAP, int, CList_t2, clist_t2_destroy);
-declare_CHash_string(sm, MAP, CHash_il, chash_il_destroy);
-
-int main() {
- int xdim = 4, ydim = 6;
- int x = 1, y = 5, tableKey = 42;
- const char* strKey = "first";
- CHash_sm theMap = chash_init;
-
- { // Construct.
- CArray2_f table = carray2_f_make(ydim, xdim, -0.f);
- printf("table: (%zu, %zu)\n", carray2_ydim(table), carray2_xdim(table));
- CList_t2 tableList = clist_init;
- CHash_il listMap = chash_init;
-
- // Put in some data.
- carray2_f_data(table, y)[x] = 3.1415927; // table[x][y]
- clist_t2_pushBack(&tableList, table);
- chash_il_put(&listMap, tableKey, tableList);
- chash_sm_put(&theMap, strKey, listMap);
- }
- { // Access the data entry
- CArray2_f table = clist_back(chash_il_get(&chash_sm_get(&theMap, strKey)->value, tableKey)->value);
- printf("value (%d, %d) is: %f\n", y, x, carray2_f_value(table, y, x));
- }
-
- chash_sm_destroy(&theMap); // free up the whole shebang!
+#include "../stc/cstring.h"
+#include "../stc/chash.h"
+#include "../stc/clist.h"
+#include "../stc/carray.h"
+
+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_CHash(il, int, CList_t2, clist_t2_destroy);
+declare_CHash_string(sm, CHash_il, chash_il_destroy);
+
+int main() {
+ int xdim = 4, ydim = 6;
+ int x = 1, y = 5, tableKey = 42;
+ const char* strKey = "first";
+ CHash_sm theMap = chash_init;
+
+ { // Construct.
+ CArray2_f table = carray2_f_make(ydim, xdim, 0.f);
+ printf("table: (%zu, %zu)\n", carray2_ydim(table), carray2_xdim(table));
+ CList_t2 tableList = clist_init;
+ CHash_il listMap = chash_init;
+
+ // Put in some data.
+ carray2_f_data(table, y)[x] = 3.1415927; // table[y][x]
+ clist_t2_pushBack(&tableList, table);
+ chash_il_put(&listMap, tableKey, tableList);
+ chash_sm_put(&theMap, strKey, listMap);
+ }
+ { // Access the data entry
+ CArray2_f table = clist_back(chash_il_get(&chash_sm_get(&theMap, strKey)->value, tableKey)->value);
+ printf("value (%d, %d) is: %f\n", y, x, carray2_f_value(table, y, x));
+ }
+
+ chash_sm_destroy(&theMap); // free up the whole shebang!
} \ No newline at end of file