diff options
| author | tylo <[email protected]> | 2021-10-06 09:24:37 +0200 |
|---|---|---|
| committer | tylo <[email protected]> | 2021-10-06 09:24:37 +0200 |
| commit | d4ea76c273bbebdb2b1be7302a9a6edde2198134 (patch) | |
| tree | 8100169462e7a4fbe3d17a49bc97da4ce6c187b5 | |
| parent | 6c342ffdeee5353ecf78493c41ee23ef79cbd940 (diff) | |
| download | STC-modified-d4ea76c273bbebdb2b1be7302a9a6edde2198134.tar.gz STC-modified-d4ea76c273bbebdb2b1be7302a9a6edde2198134.zip | |
Updates to previous commit on cvec and cdeq. Some example improvements.
| -rw-r--r-- | examples/complex.c | 48 | ||||
| -rw-r--r-- | examples/convert.c | 97 | ||||
| -rw-r--r-- | examples/cpque.c | 93 | ||||
| -rw-r--r-- | examples/csset_erase.c | 6 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 6 | ||||
| -rw-r--r-- | include/stc/cvec.h | 8 |
6 files changed, 132 insertions, 126 deletions
diff --git a/examples/complex.c b/examples/complex.c index c910be80..69900bba 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -35,27 +35,29 @@ int main() { int x = 1, y = 3, tableKey = 42;
const char* strKey = "first";
- cmap_map myMap = cmap_map_init();
- cmap_lst listMap = cmap_lst_init();
- clist_arr tableList = clist_arr_init();
- cstack_f stk = cstack_f_with_capacity(xdim * ydim);
- memset(stk.data, 0, xdim*ydim*sizeof *stk.data);
- stk.size = stk.capacity;
-
- printf("stk size: %zu\n", cstack_f_size(stk));
-
- // Put in some data in stack array
- stk.data[x] = 3.1415927f;
- clist_arr_push_back(&tableList, stk);
- cmap_lst_insert(&listMap, tableKey, tableList);
- cmap_map_insert(&myMap, cstr_from(strKey), listMap);
-
- // Access the data entry
- cmap_lst* mapL = &cmap_map_find(&myMap, strKey).ref->second;
- clist_arr* lstA = &cmap_lst_find(mapL, tableKey).ref->second;
- cstack_f arr = *clist_arr_back(lstA);
- printf("value (%d) is: %f\n", x, arr.data[x]);
-
- stk.data[x] = 1.41421356f; // change the value in array
- cmap_map_del(&myMap); // free up everything!
+ c_auto (cmap_map, myMap)
+ {
+ cstack_f stk = cstack_f_with_capacity(xdim * ydim);
+ memset(stk.data, 0, xdim*ydim*sizeof *stk.data);
+ stk.size = stk.capacity;
+
+ // Put in some data in stack array
+ stk.data[x] = 3.1415927f;
+ printf("stk size: %zu\n", cstack_f_size(stk));
+
+ clist_arr tableList = clist_arr_init();
+ clist_arr_push_back(&tableList, stk);
+
+ cmap_lst listMap = cmap_lst_init();
+ cmap_lst_insert(&listMap, tableKey, tableList);
+ cmap_map_insert(&myMap, cstr_from(strKey), listMap);
+
+ // Access the data entry
+ cmap_lst* mapL = &cmap_map_find(&myMap, strKey).ref->second;
+ clist_arr* lstA = &cmap_lst_find(mapL, tableKey).ref->second;
+ cstack_f arr = *clist_arr_back(lstA);
+ printf("value (%d) is: %f\n", x, arr.data[x]);
+
+ stk.data[x] = 1.41421356f; // change the value in array
+ }
}
diff --git a/examples/convert.c b/examples/convert.c index d42454ee..83dc1b37 100644 --- a/examples/convert.c +++ b/examples/convert.c @@ -1,49 +1,48 @@ -
-#include <stc/cstr.h>
-#include <stdio.h>
-
-#define i_key_str
-#define i_val_str
-#include <stc/cmap.h>
-
-#define i_val_str
-#include <stc/cvec.h>
-
-#define i_val_str
-#include <stc/clist.h>
-
-int main()
-{
- cmap_str map = cmap_str_init();
- cmap_str_emplace(&map, "green", "#00ff00");
- cmap_str_emplace(&map, "blue", "#0000ff");
- cmap_str_emplace(&map, "yellow", "#ffff00");
-
- puts("MAP:");
- c_foreach (i, cmap_str, map)
- printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
-
- puts("\nCLONE MAP:");
- cmap_str mclone = cmap_str_clone(map);
- c_foreach (i, cmap_str, mclone)
- printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
-
-
- puts("\nMAP TO VECS:");
- cvec_str vec1 = cvec_str_init(), vec2 = cvec_str_init();
- c_foreach (i, cmap_str, mclone) {
- cvec_str_emplace_back(&vec1, i.ref->first.str);
- cvec_str_emplace_back(&vec2, i.ref->second.str);
- }
- c_forrange (i, cvec_str_size(vec1))
- printf(" %s: %s\n", vec1.data[i].str, vec2.data[i].str);
-
- puts("\nVEC TO LIST:");
- clist_str list = clist_str_init();
- c_foreach (i, cvec_str, vec1) clist_str_emplace_back(&list, i.ref->str);
- c_foreach (i, clist_str, list) printf(" %s\n", i.ref->str);
-
- c_del(cmap_str, &map, &mclone);
- c_del(cvec_str, &vec1, &vec2);
- clist_str_del(&list);
-}
\ No newline at end of file +#include <stc/cstr.h> + +#define i_key_str +#define i_val_str +#include <stc/cmap.h> + +#define i_val_str +#include <stc/cvec.h> + +#define i_val_str +#include <stc/clist.h> + +int main() +{ + c_auto (cmap_str, map, mclone) + c_auto (cvec_str, keys, values) + c_auto (clist_str, list) + { + c_apply_pair(cmap_str, emplace, &map, { + {"green", "#00ff00"}, + {"blue", "#0000ff"}, + {"yellow", "#ffff00"}, + }); + puts("MAP:"); + c_foreach (i, cmap_str, map) + printf(" %s: %s\n", i.ref->first.str, i.ref->second.str); + + puts("\nCLONE MAP:"); + mclone = cmap_str_clone(map); + c_foreach (i, cmap_str, mclone) + printf(" %s: %s\n", i.ref->first.str, i.ref->second.str); + + puts("\nCOPY MAP TO VECS:"); + c_foreach (i, cmap_str, mclone) { + cvec_str_emplace_back(&keys, i.ref->first.str); + cvec_str_emplace_back(&values, i.ref->second.str); + } + c_forrange (i, cvec_str_size(keys)) + printf(" %s: %s\n", keys.data[i].str, values.data[i].str); + + puts("\nCOPY VEC TO LIST:"); + c_foreach (i, cvec_str, keys) + clist_str_emplace_back(&list, i.ref->str); + + c_foreach (i, clist_str, list) + printf(" %s\n", i.ref->str); + } +} diff --git a/examples/cpque.c b/examples/cpque.c index 541dcb6e..db7bc02c 100644 --- a/examples/cpque.c +++ b/examples/cpque.c @@ -1,46 +1,47 @@ -// Implements c++ example: https://en.cppreference.com/w/cpp/container/priority_queue
-#include <stdio.h>
-#include <stc/ccommon.h>
-
-// Example of dynamic compare function
-
-static int (*icmp_fn)(const int* x, const int* y);
-
-#define i_val int
-#define i_cmp icmp_fn
-#define i_cnt ipque
-#include <stc/cpque.h>
-
-#define imix_less(x, y) ((*(x) ^ 1) < (*(y) ^ 1))
-static int imax_cmp(const int* x, const int* y) { return *x - *y; }
-static int imin_cmp(const int* x, const int* y) { return *y - *x; }
-static int imix_cmp(const int* x, const int* y) { return c_less_compare(imix_less, x, y); }
-
-void print_pque(ipque q) {
- ipque copy = ipque_clone(q);
- while (!ipque_empty(copy)) {
- printf("%d ", *ipque_top(©));
- ipque_pop(©);
- }
- puts("");
- ipque_del(©);
-}
-
-int main()
-{
- const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data);
- c_auto (ipque, q, q2, q3) // init() and defered del()
- {
- icmp_fn = imax_cmp;
- c_apply_n(ipque, push, &q, data, n);
- print_pque(q);
-
- icmp_fn = imin_cmp;
- c_apply_n(ipque, push, &q2, data, n);
- print_pque(q2);
-
- icmp_fn = imix_cmp;
- c_apply_n(ipque, push, &q3, data, n);
- print_pque(q3);
- }
-}
\ No newline at end of file +// Implements c++ example: https://en.cppreference.com/w/cpp/container/priority_queue +#include <stdio.h> + +// Example of dynamic compare function + +static int (*icmp_fn)(const int* x, const int* y); + +#define i_val int +#define i_cmp icmp_fn +#define i_cnt ipque +#include <stc/cpque.h> + +#define imix_less(left, right) ((*(left) ^ 1) < (*(right) ^ 1)) +static int imax_cmp(const int* x, const int* y) { return *x - *y; } +static int imin_cmp(const int* x, const int* y) { return *y - *x; } +static int imix_cmp(const int* x, const int* y) { return c_less_compare(imix_less, x, y); } + +void print_queue(ipque q) { + ipque copy = ipque_clone(q); + while (!ipque_empty(copy)) { + printf("%d ", *ipque_top(©)); + ipque_pop(©); + } + puts(""); + ipque_del(©); +} + +int main() +{ + const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data); + c_auto (ipque, q, q2, q3) // init() and defered del() + { + icmp_fn = imax_cmp; + c_forrange (i, n) + ipque_push(&q, data[i]); + + print_queue(q); + + icmp_fn = imin_cmp; + c_apply_n(ipque, push, &q2, data, n); + print_queue(q2); + + icmp_fn = imix_cmp; + c_apply_n(ipque, push, &q3, data, n); + print_queue(q3); + } +} diff --git a/examples/csset_erase.c b/examples/csset_erase.c index df9b3d54..6104a2b2 100644 --- a/examples/csset_erase.c +++ b/examples/csset_erase.c @@ -16,11 +16,14 @@ int main() csset_int_iter_t it;
printf("Show values >= %d:\n", val);
it = csset_int_lower_bound(&set, val);
+
c_foreach (k, csset_int, it, csset_int_end(&set))
printf(" %d", *k.ref); puts("");
printf("Erase values >= %d:\n", val);
- while (it.ref) it = csset_int_erase_at(&set, it);
+ while (it.ref != csset_int_end(&set).ref)
+ it = csset_int_erase_at(&set, it);
+
c_foreach (k, csset_int, set)
printf(" %d", *k.ref);
puts("");
@@ -29,6 +32,7 @@ int main() printf("Erase values < %d:\n", val);
it = csset_int_lower_bound(&set, val);
csset_int_erase_range(&set, csset_int_begin(&set), it);
+
c_foreach (k, csset_int, set)
printf(" %d", *k.ref);
puts("");
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index deff8965..7a5ae690 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -310,7 +310,7 @@ cx_memb(_push_front)(Self* self, i_val value) { else
--self->data;
++cdeq_rep_(self)->size;
- *self->data = value;
+ *self->data = value;
return self->data;
}
@@ -337,7 +337,7 @@ cx_memb(_erase_range_p)(Self* self, cx_value_t* p1, cx_value_t* p2) { size_t n = p2 - p1;
if (n > 0) {
cx_value_t* p = p1, *end = self->data + cdeq_rep_(self)->size;
- while (p != p2) i_valdel(p), ++p;
+ while (p != p2) { i_valdel(p); ++p; }
if (p1 == self->data) self->data += n;
else memmove(p1, p2, (end - p2) * sizeof(i_val));
cdeq_rep_(self)->size -= n;
@@ -364,4 +364,4 @@ cx_memb(_value_compare)(const cx_value_t* x, const cx_value_t* y) { #endif // i_queue
#endif // IMPLEMENTATION
#include "template.h"
-#define CDEQ_H_INCLUDED
\ No newline at end of file +#define CDEQ_H_INCLUDED
diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 06577cde..283e769d 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -20,7 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-
+
/*
#include <stc/cstr.h>
#include <stc/forward.h>
@@ -114,7 +114,7 @@ STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) STC_INLINE cx_iter_t cx_memb(_end)(const Self* self)
{ return c_make(cx_iter_t){self->data + cvec_rep_(self)->size}; }
STC_INLINE void cx_memb(_next)(cx_iter_t* it) { ++it->ref; }
-STC_INLINE cx_iter_t cx_memb(_advance)(cx_iter_t it, intptr_t offs)
+STC_INLINE cx_iter_t cx_memb(_advance)(cx_iter_t it, intptr_t offs)
{ it.ref += offs; return it; }
STC_INLINE size_t cx_memb(_index)(Self cx, cx_iter_t it) { return it.ref - cx.data; }
@@ -328,7 +328,7 @@ cx_memb(_erase_range_p)(Self* self, cx_value_t* p1, cx_value_t* p2) { intptr_t len = p2 - p1;
if (len > 0) {
cx_value_t* p = p1, *end = self->data + cvec_rep_(self)->size;
- while (p != p2) i_valdel(p), ++p;
+ while (p != p2) { i_valdel(p); ++p; }
memmove(p1, p2, (end - p2) * sizeof(i_val));
cvec_rep_(self)->size -= len;
}
@@ -366,4 +366,4 @@ cx_memb(_value_compare)(const cx_value_t* x, const cx_value_t* y) { #endif
#include "template.h"
-#define CVEC_H_INCLUDED
\ No newline at end of file +#define CVEC_H_INCLUDED
|
