summaryrefslogtreecommitdiffhomepage
path: root/examples/complex.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-10-20 16:17:33 +0200
committerTyge Løvset <[email protected]>2022-10-20 16:17:33 +0200
commit14f67d1936fa76be436eaaee739861268ca534f7 (patch)
treec365789721d90cb09c311f0a65527ba31832da8b /examples/complex.c
parent79d43229e64c53cd8b358a02a58fdbe124aa5e0f (diff)
downloadSTC-modified-14f67d1936fa76be436eaaee739861268ca534f7.tar.gz
STC-modified-14f67d1936fa76be436eaaee739861268ca534f7.zip
Switch from #define i_val_bind to i_val_class and i_key_class.
i_val_bind/i_key_bind is deprecated but available for now.
Diffstat (limited to 'examples/complex.c')
-rw-r--r--examples/complex.c49
1 files changed, 22 insertions, 27 deletions
diff --git a/examples/complex.c b/examples/complex.c
index 5e3f53a9..71ee6b0b 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -1,60 +1,55 @@
+
+// Define similar c++ data types:
+//
+// using FloatStack = std::stack<float>;
+// using StackList = std::stack<FloatStack>;
+// using ListMap = std::unordered_map<int, std::forward_list<StackList>>;
+// using MapMap = std::unordered_map<std::string, ListMap>;
+
#include <stc/cstr.h>
-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_valclone(x) x // required to allow cloning when i_valdrop is defined
- // (not for carc as it does not use i_valclone to clone).
#include <stc/cstack.h>
#define i_type StackList
-#define i_val_bind FloatStack
-#define i_opt c_no_cmp
+#define i_val_class FloatStack // "class" picks up _clone, _drop
+#define i_opt c_no_cmp // no FloatStack_cmp()
#include <stc/clist.h>
#define i_type ListMap
#define i_key int
-#define i_val_bind StackList
+#define i_val_class StackList // "class" picks up _clone, _drop
#include <stc/cmap.h>
#define i_type MapMap
#define i_key_str
-#define i_val_bind ListMap
+#define i_val_class ListMap
#include <stc/cmap.h>
-// c++:
-// using FloatStack = std::stack<float>;
-// using map_lst = std::unordered_map<int, std::forward_list<array2f>>;
-// using map_map = std::unordered_map<std::string, map_lst>;
-
-int main() {
- int xdim = 4, ydim = 6;
- int x = 1, tableKey = 42;
- const char* strKey = "first";
+int main()
+{
c_auto (MapMap, mmap)
{
- FloatStack stack = FloatStack_with_size(xdim * ydim, 0);
+ FloatStack stack = FloatStack_with_size(10, 0);
- // Put in some data in stack array
- stack.data[x] = 3.1415927f;
+ // Put in some data in the structures
+ stack.data[3] = 3.1415927f;
printf("stack size: %" c_zu "\n", FloatStack_size(&stack));
StackList list = StackList_init();
StackList_push_back(&list, stack);
ListMap lmap = ListMap_init();
- ListMap_insert(&lmap, tableKey, list);
- MapMap_insert(&mmap, cstr_from(strKey), lmap);
+ ListMap_insert(&lmap, 42, list);
+ MapMap_insert(&mmap, cstr_from("first"), lmap);
// Access the data entry
- const ListMap* lmap_p = MapMap_at(&mmap, strKey);
- const StackList* list_p = ListMap_at(lmap_p, tableKey);
+ const ListMap* lmap_p = MapMap_at(&mmap, "first");
+ const StackList* list_p = ListMap_at(lmap_p, 42);
const FloatStack* stack_p = StackList_back(list_p);
- printf("value (%d) is: %f\n", x, *FloatStack_at(stack_p, x));
-
- stack.data[x] = 1.41421356f; // change the value in array
+ printf("value is: %f\n", *FloatStack_at(stack_p, 3)); // pi
}
}