diff options
| author | Tyge Løvset <[email protected]> | 2021-03-22 19:38:59 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-03-22 20:25:44 +0100 |
| commit | 2e71adcb742278523e14f1e9eebc9635e7510a7a (patch) | |
| tree | b2d4c8d911b015f368ba60df2cb2f1b8e1706c3a /examples | |
| parent | 15c5ad14e7f4755ecae95e3aae484bdbbea34623 (diff) | |
| download | STC-modified-2e71adcb742278523e14f1e9eebc9635e7510a7a.tar.gz STC-modified-2e71adcb742278523e14f1e9eebc9635e7510a7a.zip | |
Rewrote carray.h completely. Somewhat different API, much easier/natural usage. Uses still a contiguous block of memory.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/complex.c | 43 | ||||
| -rw-r--r-- | examples/demos.c | 23 |
2 files changed, 36 insertions, 30 deletions
diff --git a/examples/complex.c b/examples/complex.c index 4ca833d4..6095a03e 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -6,32 +6,39 @@ void check_del(float* v) {printf("destroy %g\n", *v);}
using_carray2(f, float, check_del, c_trivial_fromraw);
-using_clist(a, carray2f, c_no_compare, carray2f_del, c_no_clone);
-using_cmap(l, int, clist_a, c_default_equals, c_default_hash, clist_a_del, c_no_clone);
-using_cmap_strkey(s, cmap_l, cmap_l_del, c_no_clone);
+using_clist(arr, carray2f, c_no_compare, carray2f_del, c_no_clone);
+using_cmap(lst, int, clist_arr, c_default_equals, c_default_hash, clist_arr_del, c_no_clone);
+using_cmap_strkey(map, cmap_lst, cmap_lst_del, c_no_clone);
+// c++:
+// using array2f = std::array<std::array<float, 6>, 4>>;
+// 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, y = 5, tableKey = 42;
+ int x = 1, y = 3, tableKey = 42;
const char* strKey = "first";
- cmap_l listMap = cmap_l_init();
- cmap_s myMap = cmap_s_init();
+ cmap_map myMap = cmap_map_init();
+ cmap_lst listMap = cmap_lst_init();
+ clist_arr tableList = clist_arr_init();
+ carray2f arr2 = carray2f_init(xdim, ydim, 1.f);
- // Construct.
- carray2f arr_a = carray2f_init(ydim, xdim, 0.f);
- printf("arr_a: (%zu, %zu)\n", carray2f_ydim(arr_a), carray2f_xdim(arr_a));
+ printf("arr2 size: %zu x %zu\n", arr2.xdim, arr2.ydim);
- clist_a tableList = clist_a_init();
- // Put in some data.
- *carray2f_at(&arr_a, y, x) = 3.1415927f; // aa[y][x]
- clist_a_push_back(&tableList, arr_a);
- cmap_l_insert(&listMap, tableKey, tableList);
- cmap_s_insert(&myMap, cstr_from(strKey), listMap);
+ // Put in some data in 2D array
+ arr2.at[x][y] = 3.1415927f;
+ clist_arr_push_back(&tableList, arr2);
+ cmap_lst_insert(&listMap, tableKey, tableList);
+ cmap_map_insert(&myMap, cstr_from(strKey), listMap);
// Access the data entry
- carray2f arr_b = *clist_a_back(&cmap_l_find(&cmap_s_find(&myMap, strKey).ref->second, tableKey).ref->second);
- printf("value (%d, %d) is: %f\n", y, x, *carray2f_at(&arr_b, y, x));
+ cmap_lst* mapl = &cmap_map_find(&myMap, strKey).ref->second;
+ clist_arr* lsta = &cmap_lst_find(mapl, tableKey).ref->second;
+ carray2f arr = *clist_arr_back(lsta);
- cmap_s_del(&myMap); // free up everything!
+ printf("value (%d, %d) is: %f\n", x, y, arr.at[x][y]);
+
+ arr2.at[x][y] = 1.41421356f; // change the value in array
+ cmap_map_del(&myMap); // free up everything!
}
diff --git a/examples/demos.c b/examples/demos.c index eca5609f..9d7f894c 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -181,23 +181,22 @@ void arraydemo1() {
printf("\nARRAYDEMO1\n");
carray3f a3 = carray3f_init(30, 20, 10, 0.0f);
- *carray3f_at(&a3, 5, 4, 3) = 10.2f; // a3[5][4][3]
- carray2f a2 = carray3f_at1(&a3, 5); // sub-array reference: a2 = a3[5]
- carray1f a1 = carray3f_at2(&a3, 5, 4); // sub-array reference: a1 = a3[5][4]
+ a3.at[5][4][3] = 10.2f;
+ float **a2 = a3.at[5];
+ float *a1 = a3.at[5][4];
- printf("a3: %zu: (%zu, %zu, %zu) = %zu\n", sizeof(a3), carray3f_xdim(a3), carray3f_ydim(a3), carray3f_zdim(a3), carray3f_size(a3));
- printf("a2: %zu: (%zu, %zu) = %zu\n", sizeof(a2), carray2f_xdim(a2), carray2f_ydim(a2), carray2f_size(a2));
+ printf("a3: %zu: (%zu, %zu, %zu) = %zu\n", sizeof(a3), a3.xdim, a3.ydim, a3.zdim, carray3f_size(a3));
- printf("%f\n", a1.data[3]); // lookup a1[3] (=10.2f)
- printf("%f\n", *carray2f_at(&a2, 4, 3)); // lookup a2[4][3] (=10.2f)
- printf("%f\n", *carray3f_at(&a3, 5, 4, 3)); // lookup a3[5][4][3] (=10.2f)
+ printf("%g\n", a1[3]); // = 10.2
+ printf("%g\n", a2[4][3]); // = 10.2
+ printf("%g\n", a3.at[5][4][3]); // = 10.2
+ float x = 0.0;
c_foreach (i, carray3f, a3)
- *i.ref = 1.0f;
- printf("%f\n", *carray3f_at(&a3, 29, 19, 9));
+ *i.ref = ++x;
+ printf("%g\n", a3.at[29][19][9]); // = 6000
- carray2f_del(&a2); // does nothing, since it is a sub-array.
- carray3f_del(&a3); // also invalidates a2.
+ carray3f_del(&a3);
}
|
