summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-09-08 21:16:00 +0200
committerTyge Løvset <[email protected]>2021-09-08 21:16:00 +0200
commitc86e3d30299baea44f98f153c4b01ea5e244feaa (patch)
tree7de3a473408bdee0017d8efa452dd5a095b61177
parentb9e58749fb77715f4635a45377350a75ce7e0948 (diff)
downloadSTC-modified-c86e3d30299baea44f98f153c4b01ea5e244feaa.tar.gz
STC-modified-c86e3d30299baea44f98f153c4b01ea5e244feaa.zip
Updated most examples to newstyle. Some changes in cpque/cstack.
-rw-r--r--examples/birthday.c28
-rw-r--r--examples/complex.c53
-rw-r--r--examples/convert.c18
-rw-r--r--examples/cpque.c27
-rw-r--r--examples/csmap_erase.c10
-rw-r--r--examples/csmap_insert.c42
-rw-r--r--examples/csset_erase.c39
-rw-r--r--examples/ex_gauss1.c14
-rw-r--r--examples/ex_gauss2.c16
-rw-r--r--examples/inits.c47
-rw-r--r--examples/list.c11
-rw-r--r--examples/list_splice.c10
-rw-r--r--examples/mmap.c16
-rw-r--r--examples/priority.c12
-rw-r--r--examples/stack.c10
-rw-r--r--include/stc/cpque.h26
-rw-r--r--include/stc/cstack.h12
17 files changed, 235 insertions, 156 deletions
diff --git a/examples/birthday.c b/examples/birthday.c
index 0dbb544a..62f39fb2 100644
--- a/examples/birthday.c
+++ b/examples/birthday.c
@@ -1,11 +1,13 @@
#include <math.h>
#include <stdio.h>
#include <time.h>
-
#include <stc/crandom.h>
+
+#define i_tag ic
+#define i_key uint64_t
+#define i_val uint8_t
#include <stc/cmap.h>
-using_cmap(ic, uint64_t, uint8_t);
static uint64_t seed = 12345;
static void test_repeats(void)
@@ -16,17 +18,21 @@ static void test_repeats(void)
printf("birthday paradox: value range: 2^%d, testing repeats of 2^%d values\n", BITS, BITS_TEST);
stc64_t rng = stc64_init(seed);
- cmap_ic m = cmap_ic_init();
- cmap_ic_reserve(&m, N);
- c_forrange (i, N) {
- uint64_t k = stc64_rand(&rng) & mask;
- int v = cmap_ic_emplace(&m, k, 0).ref->second += 1;
- if (v > 1) printf("repeated value %llx (%d) at 2^%d\n", k, v, (int) log2(i));
+ c_forauto (cmap_ic, m)
+ {
+ cmap_ic_reserve(&m, N);
+ c_forrange (i, N) {
+ uint64_t k = stc64_rand(&rng) & mask;
+ int v = cmap_ic_emplace(&m, k, 0).ref->second += 1;
+ if (v > 1) printf("repeated value %llx (%d) at 2^%d\n", k, v, (int) log2(i));
+ }
}
}
-
-using_cmap(x, uint32_t, uint64_t);
+#define i_tag x
+#define i_key uint32_t
+#define i_val uint64_t
+#include <stc/cmap.h>
void test_distribution(void)
{
@@ -35,7 +41,7 @@ void test_distribution(void)
stc64_t rng = stc64_init(seed);
const size_t N = 1ull << BITS ;
- c_forvar (cmap_x map = cmap_x_init(), cmap_x_del(&map)) {
+ c_forauto (cmap_x, map) {
c_forrange (N) {
uint64_t k = stc64_rand(&rng);
cmap_x_emplace(&map, k & 0xf, 0).ref->second += 1;
diff --git a/examples/complex.c b/examples/complex.c
index c6390d52..5cbaaa40 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -1,16 +1,32 @@
#include <stc/cstr.h>
-#include <stc/cmap.h>
-#include <stc/clist.h>
-#include <stc/carray.h>
void check_del(float* v) {printf("destroy %g\n", *v);}
-using_carray2(f, float, check_del, c_default_fromraw);
-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);
+#define i_tag f
+#define i_val float
+#define i_valdel check_del
+#include <stc/cstack.h>
+
+#define i_tag arr
+#define i_val cstack_f
+#define i_cmp c_no_compare
+#define i_valdel cstack_f_del
+#include <stc/clist.h>
+
+#define i_tag lst
+#define i_key int
+#define i_val clist_arr
+#define i_valdel clist_arr_del
+#include <stc/cmap.h>
+
+#define i_tag map
+#define i_key_str
+#define i_val cmap_lst
+#define i_valdel cmap_lst_del
+#include <stc/cmap.h>
+
// c++:
-// using array2f = std::array<std::array<float, 6>, 4>>;
+// using cstack_f = std::stack<float>;
// using map_lst = std::unordered_map<int, std::forward_list<array2f>>;
// using map_map = std::unordered_map<std::string, map_lst>;
@@ -22,23 +38,24 @@ int main() {
cmap_map myMap = cmap_map_init();
cmap_lst listMap = cmap_lst_init();
clist_arr tableList = clist_arr_init();
- carray2f arr2 = carray2f_with_values(xdim, ydim, 1.f);
+ cstack_f stk = cstack_f_with_capacity(xdim * ydim);
+ memset(stk.data, 0, xdim*ydim*sizeof *stk.data);
+ stk.size = stk.capacity;
- printf("arr2 size: %zu x %zu\n", arr2.xdim, arr2.ydim);
+ printf("stk size: %zu\n", cstack_f_size(stk));
// Put in some data in 2D array
- arr2.data[x][y] = 3.1415927f;
- clist_arr_push_back(&tableList, arr2);
+ 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;
- carray2f arr = *clist_arr_back(lsta);
-
- printf("value (%d, %d) is: %f\n", x, y, arr.data[x][y]);
+ 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]);
- arr2.data[x][y] = 1.41421356f; // change the value in array
+ stk.data[x] = 1.41421356f; // change the value in array
cmap_map_del(&myMap); // free up everything!
}
diff --git a/examples/convert.c b/examples/convert.c
index 25ae9dcd..d42454ee 100644
--- a/examples/convert.c
+++ b/examples/convert.c
@@ -1,14 +1,16 @@
-#include "stc/cmap.h"
-#include "stc/csmap.h"
-#include "stc/cvec.h"
-#include "stc/clist.h"
-#include "stc/cstr.h"
+#include <stc/cstr.h>
#include <stdio.h>
-using_cmap_str();
-using_cvec_str();
-using_clist_str();
+#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()
{
diff --git a/examples/cpque.c b/examples/cpque.c
index 3c22fdfd..0e21a5f7 100644
--- a/examples/cpque.c
+++ b/examples/cpque.c
@@ -1,15 +1,20 @@
-#include <stc/cpque.h>
+// Implements c++ example: https://en.cppreference.com/w/cpp/container/priority_queue
#include <stdio.h>
-// Implements c++ example: https://en.cppreference.com/w/cpp/container/priority_queue
+#define i_tag imax
+#define i_val int
+#include <stc/cpque.h>
-using_cvec(i, int);
-#define imix_less(x, y) ((*(x) ^ 1) < (*(y) ^ 1))
-#define imix_cmp(x, y) c_less_compare(imix_less, x, y)
+#define i_tag imin
+#define i_val int
+#define i_cmp -c_default_compare
+#include <stc/cpque.h>
-using_cpque(imax, cvec_i);
-using_cpque(imin, cvec_i, -c_default_compare);
-using_cpque(imix, cvec_i, imix_cmp);
+#define imix_less(x, y) ((*(x) ^ 1) < (*(y) ^ 1))
+#define i_tag imix
+#define i_val int
+#define i_cmp(x, y) c_less_compare(imix_less, x, y)
+#include <stc/cpque.h>
#define PRINT_Q(Q) \
void print_##Q(Q q) { \
@@ -31,9 +36,9 @@ int main()
{
const int data[] = {1,8,5,6,3,4,0,9,7,2};
- c_forvar (cpque_imax q = cpque_imax_init(), cpque_imax_del(&q)) // init() and defered del()
- c_forvar (cpque_imin q2 = cpque_imin_init(), cpque_imin_del(&q2))
- c_forvar (cpque_imix q3 = cpque_imix_init(), cpque_imix_del(&q3))
+ c_forauto (cpque_imax, q) // init() and defered del()
+ c_forauto (cpque_imin, q2)
+ c_forauto (cpque_imix, q3)
{
c_forrange (n, c_arraylen(data))
cpque_imax_push(&q, n);
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c
index 6cebffb7..619efb31 100644
--- a/examples/csmap_erase.c
+++ b/examples/csmap_erase.c
@@ -1,10 +1,12 @@
// map_erase.c
// From C++ example: https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-16
-#include <stc/csmap.h>
#include <stc/cstr.h>
#include <stdio.h>
-using_csmap_strval(my, int);
+#define i_tag my
+#define i_key int
+#define i_val_str
+#include <stc/csmap.h>
void printmap(csmap_my map)
{
@@ -27,7 +29,7 @@ int main()
puts("Starting data of map m1 is:");
printmap(m1);
// The 1st member function removes an element at a given position
- csmap_my_erase_at(&m1, csmap_my_fwd(csmap_my_begin(&m1), 1));
+ csmap_my_erase_at(&m1, csmap_my_advance(csmap_my_begin(&m1), 1));
puts("After the 2nd element is deleted, the map m1 is:");
printmap(m1);
}
@@ -45,7 +47,7 @@ int main()
puts("Starting data of map m2 is:");
printmap(m2);
- csmap_my_iter_t it1 = csmap_my_fwd(csmap_my_begin(&m2), 1);
+ csmap_my_iter_t it1 = csmap_my_advance(csmap_my_begin(&m2), 1);
csmap_my_iter_t it2 = csmap_my_find(&m2, csmap_my_back(&m2)->first);
// The 2nd member function removes elements
// in the range [First, Last)
diff --git a/examples/csmap_insert.c b/examples/csmap_insert.c
index 684d499a..25f7a23f 100644
--- a/examples/csmap_insert.c
+++ b/examples/csmap_insert.c
@@ -1,21 +1,30 @@
-#include <stc/csmap.h>
-#include <stc/cvec.h>
-#include <stc/csview.h>
+#include <stc/cstr.h>
#include <stdio.h>
// This implements the std::map insert c++ example at:
// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-19
+#define i_tag ii // Map of int => int
+#define i_key int
+#define i_val int
+#include <stc/csmap.h>
-using_csmap(ii, int, int); // Map of int => int
-using_csmap_strval(istr, int); // Map of int => cstr
-using_cvec(ii, csmap_ii_rawvalue_t, c_no_compare);
+#define i_tag istr // Map of int => cstr
+#define i_key int
+#define i_val_str
+#include <stc/csmap.h>
+
+#define i_tag ii
+#define i_val csmap_ii_rawvalue_t
+#define i_cmp c_no_compare
+#include <stc/cvec.h>
void print_ii(csmap_ii map) {
c_foreach (e, csmap_ii, map)
printf("(%d, %d) ", e.ref->first, e.ref->second);
puts("");
}
+
void print_istr(csmap_istr map) {
c_foreach (e, csmap_istr, map)
printf("(%d, %s) ", e.ref->first, e.ref->second.str);
@@ -25,7 +34,7 @@ void print_istr(csmap_istr map) {
int main()
{
// insert single values
- c_forvar (csmap_ii m1 = csmap_ii_init(), csmap_ii_del(&m1)) {
+ c_forauto (csmap_ii, m1) {
csmap_ii_insert(&m1, 1, 10);
csmap_ii_insert(&m1, 2, 20);
@@ -52,13 +61,14 @@ int main()
}
// The templatized version inserting a jumbled range
- c_forvar (csmap_ii m2 = csmap_ii_init(), csmap_ii_del(&m2))
- c_forvar (cvec_ii v = cvec_ii_init(), cvec_ii_del(&v)) {
- cvec_ii_push_back(&v, (cvec_ii_value_t){43, 294});
- cvec_ii_push_back(&v, (cvec_ii_value_t){41, 262});
- cvec_ii_push_back(&v, (cvec_ii_value_t){45, 330});
- cvec_ii_push_back(&v, (cvec_ii_value_t){42, 277});
- cvec_ii_push_back(&v, (cvec_ii_value_t){44, 311});
+ c_forauto (csmap_ii, m2)
+ c_forauto (cvec_ii, v) {
+ typedef cvec_ii_value_t ipair;
+ cvec_ii_push_back(&v, (ipair){43, 294});
+ cvec_ii_push_back(&v, (ipair){41, 262});
+ cvec_ii_push_back(&v, (ipair){45, 330});
+ cvec_ii_push_back(&v, (ipair){42, 277});
+ cvec_ii_push_back(&v, (ipair){44, 311});
puts("Inserting the following vector data into m2:");
c_foreach (e, cvec_ii, v) printf("(%d, %d) ", e.ref->first, e.ref->second);
@@ -72,7 +82,7 @@ int main()
}
// The templatized versions move-constructing elements
- c_forvar (csmap_istr m3 = csmap_istr_init(), csmap_istr_del(&m3)) {
+ c_forauto (csmap_istr, m3) {
csmap_istr_value_t ip1 = {475, cstr_lit("blue")}, ip2 = {510, cstr_lit("green")};
// single element
@@ -87,7 +97,7 @@ int main()
puts("");
}
- c_forvar (csmap_ii m4 = csmap_ii_init(), csmap_ii_del(&m4)) {
+ c_forauto (csmap_ii, m4) {
// Insert the elements from an initializer_list
c_emplace(csmap_ii, m4, { { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
puts("After initializer_list insertion, m4 contains:");
diff --git a/examples/csset_erase.c b/examples/csset_erase.c
index 53398ff9..774201b2 100644
--- a/examples/csset_erase.c
+++ b/examples/csset_erase.c
@@ -1,29 +1,30 @@
-#include <stc/csset.h>
#include <stdio.h>
-using_csset(i, int);
+#define i_key int
+#include <stc/csset.h>
int main()
{
- c_var (csset_i, set, {30, 20, 80, 40, 60, 90, 10, 70, 50});
- c_foreach (k, csset_i, set) printf(" %d", *k.ref); puts("");
-
- int val = 64;
- csset_i_iter_t it;
- printf("Show values >= %d:\n", val);
- it = csset_i_lower_bound(&set, val);
- c_foreach (k, csset_i, it, csset_i_end(&set)) printf(" %d", *k.ref); puts("");
+ c_forauto (csset_int, set)
+ {
+ c_var (csset_int, set, {30, 20, 80, 40, 60, 90, 10, 70, 50});
+ c_foreach (k, csset_int, set) printf(" %d", *k.ref); puts("");
- printf("Erase values >= %d:\n", val);
- while (it.ref) it = csset_i_erase_at(&set, it);
- c_foreach (k, csset_i, set) printf(" %d", *k.ref); puts("");
+ int val = 64;
+ 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("");
- val = 40;
- printf("Erase values < %d:\n", val);
- it = csset_i_lower_bound(&set, val);
- csset_i_erase_range(&set, csset_i_begin(&set), it);
- c_foreach (k, csset_i, set) printf(" %d", *k.ref); puts("");
+ printf("Erase values >= %d:\n", val);
+ while (it.ref) it = csset_int_erase_at(&set, it);
+ c_foreach (k, csset_int, set) printf(" %d", *k.ref); puts("");
- csset_i_del(&set);
+ val = 40;
+ 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/examples/ex_gauss1.c b/examples/ex_gauss1.c
index d089de89..e345ddd1 100644
--- a/examples/ex_gauss1.c
+++ b/examples/ex_gauss1.c
@@ -3,19 +3,23 @@
#include <math.h>
#include <stc/crandom.h>
#include <stc/cstr.h>
-#include <stc/cmap.h>
-#include <stc/cvec.h>
// Declare int -> int hashmap. Uses typetag 'ii' for ints.
-using_cmap(ii, int32_t, size_t);
+#define i_tag ii
+#define i_key int32_t
+#define i_val size_t
+#include <stc/cmap.h>
// Declare int vector with map entries that can be sorted by map keys.
-typedef struct {int first; size_t second;} mapval;
+struct {int first; size_t second;} typedef mapval;
static int compare(mapval *a, mapval *b) {
return c_default_compare(&a->first, &b->first);
}
-using_cvec(pair, mapval, compare);
+#define i_tag pair
+#define i_val mapval
+#define i_cmp compare
+#include <stc/cvec.h>
int main()
{
diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c
index 553846aa..850cd2de 100644
--- a/examples/ex_gauss2.c
+++ b/examples/ex_gauss2.c
@@ -1,12 +1,12 @@
#include <stdio.h>
#include <time.h>
-
#include <stc/crandom.h>
-#include <stc/csmap.h>
#include <stc/cstr.h>
-// Declare int -> int sorted map. Uses typetag 'i' for ints.
-using_csmap(i, int, size_t);
+// Declare int -> int sorted map.
+#define i_key int
+#define i_val size_t
+#include <stc/csmap.h>
int main()
{
@@ -21,16 +21,16 @@ int main()
stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
// Create and init histogram map with defered destruct
- c_forvar (csmap_i mhist = csmap_i_init(), csmap_i_del(&mhist))
- c_forvar (cstr bar = cstr_init(), cstr_del(&bar))
+ c_forauto (csmap_int, mhist)
+ c_forauto (cstr, bar)
{
c_forrange (N) {
int index = (int) round( stc64_normalf(&rng, &dist) );
- csmap_i_emplace(&mhist, index, 0).ref->second += 1;
+ csmap_int_insert(&mhist, index, 0).ref->second += 1;
}
// Print the gaussian bar chart
- c_foreach (i, csmap_i, mhist) {
+ c_foreach (i, csmap_int, mhist) {
size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
if (n > 0) {
cstr_resize(&bar, n, '*');
diff --git a/examples/inits.c b/examples/inits.c
index 40e89139..04860258 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -1,12 +1,15 @@
#include <stdio.h>
#include <stc/cstr.h>
+
+#define i_tag id // Map of int => cstr
+#define i_key int
+#define i_val_str
#include <stc/cmap.h>
-#include <stc/cvec.h>
-#include <stc/cpque.h>
-#include <stc/clist.h>
-using_cmap_strval(id, int); // Map of int => cstr
-using_cmap_strkey(cnt, int); // Map of cstr => int
+#define i_tag cnt // Map of cstr => int
+#define i_key_str
+#define i_val int
+#include <stc/cmap.h>
typedef struct {int x, y;} ipair_t;
inline static int ipair_compare(const ipair_t* a, const ipair_t* b) {
@@ -14,19 +17,31 @@ inline static int ipair_compare(const ipair_t* a, const ipair_t* b) {
return cx == 0 ? c_default_compare(&a->y, &b->y) : cx;
}
-using_cvec(ip, ipair_t, ipair_compare);
-using_clist(ip, ipair_t, ipair_compare);
-using_cvec(f, float);
-using_cpque(f, cvec_f);
+#define i_tag ip
+#define i_val ipair_t
+#define i_cmp ipair_compare
+#include <stc/cvec.h>
+
+#define i_tag ip
+#define i_val ipair_t
+#define i_cmp ipair_compare
+#include <stc/clist.h>
+
+#define i_tag f
+#define i_val float
+#include <stc/cpque.h>
int main(void)
{
// CVEC FLOAT / PRIORITY QUEUE
- c_forvar (cvec_f floats = cvec_f_init(), cvec_f_del(&floats)) {
- c_emplace(cvec_f, floats, {4.0f, 2.0f, 5.0f, 3.0f, 1.0f});
+ c_forauto (cpque_f, floats) {
+ float nums[] = {4.0f, 2.0f, 5.0f, 3.0f, 1.0f};
- c_foreach (i, cvec_f, floats) printf("%.1f ", *i.ref);
+ c_forrange (i, c_arraylen(nums)) {
+ cpque_f_push_back(&floats, nums[i]);
+ printf("%.1f ", floats.data[i]);
+ }
puts("");
// CVEC PRIORITY QUEUE
@@ -45,7 +60,7 @@ int main(void)
// CMAP ID
int year = 2020;
- c_forvar (cmap_id idnames = cmap_id_init(), cmap_id_del(&idnames)) {
+ c_forauto (cmap_id, idnames) {
cmap_id_emplace(&idnames, 100, "Hello");
cmap_id_insert(&idnames, 110, cstr_from("World"));
cmap_id_insert(&idnames, 120, cstr_from_fmt("Howdy, -%d-", year));
@@ -57,7 +72,7 @@ int main(void)
// CMAP CNT
- c_forvar (cmap_cnt countries = cmap_cnt_init(), cmap_cnt_del(&countries)) {
+ c_forauto (cmap_cnt, countries) {
c_emplace(cmap_cnt, countries, {
{"Norway", 100},
{"Denmark", 50},
@@ -80,7 +95,7 @@ int main(void)
// CVEC PAIR
- c_forvar (cvec_ip pairs1 = cvec_ip_init(), cvec_ip_del(&pairs1)) {
+ c_forauto (cvec_ip, pairs1) {
c_emplace (cvec_ip, pairs1, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
cvec_ip_sort(&pairs1);
@@ -91,7 +106,7 @@ int main(void)
// CLIST PAIR
- c_forvar (clist_ip pairs2 = clist_ip_init(), clist_ip_del(&pairs2)) {
+ c_forauto (clist_ip, pairs2) {
c_emplace(clist_ip, pairs2, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
clist_ip_sort(&pairs2);
diff --git a/examples/list.c b/examples/list.c
index 327b477c..f08ba34d 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -1,14 +1,17 @@
#include <stdio.h>
#include <time.h>
-#include <stc/clist.h>
#include <stc/crandom.h>
-using_clist(fx, double);
+
+#define i_tag fx
+#define i_val double
+#include <stc/clist.h>
int main() {
int k;
const int n = 2000000;
- c_forvar (clist_fx list = clist_fx_init(), clist_fx_del(&list)) {
+ c_forauto (clist_fx, list)
+ {
stc64_t rng = stc64_init(1234);
stc64_uniformf_t dist = stc64_uniformf_init(100.0f, n);
int m = 0;
@@ -46,7 +49,7 @@ int main() {
c_foreach (i, clist_fx, list)
printf(" %g", *i.ref);
printf("\nSubs: ");
- c_foreach (i, clist_fx, clist_fx_fwd(it, 4), clist_fx_end(&list))
+ c_foreach (i, clist_fx, clist_fx_advance(it, 4), clist_fx_end(&list))
printf(" %g", *i.ref);
puts("");
}
diff --git a/examples/list_splice.c b/examples/list_splice.c
index 7724d683..e9139156 100644
--- a/examples/list_splice.c
+++ b/examples/list_splice.c
@@ -1,7 +1,8 @@
-#include <stc/clist.h>
#include <stdio.h>
-using_clist(i, int);
+#define i_tag i
+#define i_val int
+#include <stc/clist.h>
void print_ilist(const char* s, clist_i list)
{
@@ -14,15 +15,14 @@ void print_ilist(const char* s, clist_i list)
int main ()
{
- c_forvar (clist_i list1 = clist_i_init(), clist_i_del(&list1))
- c_forvar (clist_i list2 = clist_i_init(), clist_i_del(&list2))
+ c_forauto (clist_i, list1, list2)
{
c_emplace(clist_i, list1, {1, 2, 3, 4, 5});
c_emplace(clist_i, list2, {10, 20, 30, 40, 50});
print_ilist("list1:", list1);
print_ilist("list2:", list2);
- clist_i_iter_t it = clist_i_fwd(clist_i_begin(&list1), 2);
+ clist_i_iter_t it = clist_i_advance(clist_i_begin(&list1), 2);
it = clist_i_splice(&list1, it, &list2);
puts("After splice");
diff --git a/examples/mmap.c b/examples/mmap.c
index 9c92b19b..823d1d2b 100644
--- a/examples/mmap.c
+++ b/examples/mmap.c
@@ -1,13 +1,17 @@
-#include <stc/csmap.h>
-#include <stc/clist.h>
-#include <stc/cstr.h>
-
// This implements the multimap c++ example found at:
// https://en.cppreference.com/w/cpp/container/multimap/insert
+#include <stc/cstr.h>
// Map of int => clist_str. Note the negation of c_default_compare
-using_clist_str();
-using_csmap(mult, int, clist_str, -c_default_compare, clist_str_del);
+#define i_val_str
+#include <stc/clist.h>
+
+#define i_tag mult
+#define i_key int
+#define i_val clist_str
+#define i_valdel clist_str_del
+#define i_cmp -c_default_compare
+#include <stc/csmap.h>
void print(const csmap_mult mmap)
{
diff --git a/examples/priority.c b/examples/priority.c
index 4824a294..09ede43c 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -1,13 +1,12 @@
#include <stdio.h>
#include <time.h>
-#include <stc/cvec.h>
-#include <stc/cpque.h>
-#include <stc/cmap.h>
#include <stc/crandom.h>
-using_cvec(i, int64_t);
-using_cpque(i, cvec_i, -c_default_compare); // min-heap (increasing values)
+#define i_tag i
+#define i_val int64_t
+#define i_cmp -c_default_compare // min-heap (increasing values)
+#include <stc/cpque.h>
int main() {
size_t N = 10000000;
@@ -16,6 +15,7 @@ int main() {
c_forauto (cpque_i, heap)
{
// Push ten million random numbers to priority queue
+ printf("Push %zu numbers\n", N);
c_forrange (N)
cpque_i_push(&heap, stc64_uniform(&rng, &dist));
@@ -25,7 +25,7 @@ int main() {
c_forrange (N)
cpque_i_push(&heap, stc64_uniform(&rng, &dist));
- // Extract the hundred smallest.
+ puts("Extract the hundred smallest.");
c_forrange (100) {
printf("%zd ", *cpque_i_top(&heap));
cpque_i_pop(&heap);
diff --git a/examples/stack.c b/examples/stack.c
index 7ae53e0f..89987b7b 100644
--- a/examples/stack.c
+++ b/examples/stack.c
@@ -1,12 +1,14 @@
#include <stdio.h>
#include <stc/cstr.h>
+
+#define i_tag i
+#define i_val int
#include <stc/cstack.h>
-using_cvec(i, int);
-using_cvec(c, char);
-using_cstack(i, cvec_i);
-using_cstack(c, cvec_c);
+#define i_tag c
+#define i_val char
+#include <stc/cstack.h>
int main() {
cstack_i stack = cstack_i_init();
diff --git a/include/stc/cpque.h b/include/stc/cpque.h
index 20bfdb6e..4f7e0003 100644
--- a/include/stc/cpque.h
+++ b/include/stc/cpque.h
@@ -45,7 +45,7 @@ STC_API Self cx_memb(_clone)(Self q);
STC_INLINE Self cx_memb(_init)(void)
{ return (Self){0, 0, 0}; }
-STC_INLINE Self cx_memb(_init_with_capacity)(size_t cap) {
+STC_INLINE Self cx_memb(_with_capacity)(size_t cap) {
Self out = {(cx_value_t *) c_malloc(cap*sizeof(cx_value_t)), 0, cap};
return out;
}
@@ -84,6 +84,19 @@ STC_INLINE int cx_memb(_value_compare)(const cx_value_t* x, const cx_value_t* y)
return i_cmp(&rx, &ry);
}
+STC_INLINE void
+cx_memb(_push_back)(Self* self, cx_value_t value) {
+ if (self->size == self->capacity) {
+ self->capacity = self->size*3/2 + 4;
+ self->data = (cx_value_t *)c_realloc(self->data, self->capacity*sizeof value);
+ }
+ self->data[ self->size++ ] = value;
+}
+
+STC_INLINE void
+cx_memb(_pop_back)(Self* self)
+ { --self->size; i_valdel(&self->data[self->size]); }
+
/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp)
@@ -100,13 +113,6 @@ cx_memb(_sift_down_)(cx_value_t* arr, size_t i, size_t n) {
}
}
-STC_INLINE void
-cx_memb(_push_back_)(Self* self, cx_value_t value) {
- if (self->size == self->capacity)
- self->data = (cx_value_t *) c_realloc(self->data, (self->capacity = self->size*3/2 + 4)*sizeof value);
- self->data[ self->size++ ] = value;
-}
-
STC_DEF void
cx_memb(_make_heap)(Self* self) {
size_t n = cx_memb(_size)(*self);
@@ -125,13 +131,13 @@ STC_DEF void
cx_memb(_erase_at)(Self* self, size_t idx) {
size_t n = cx_memb(_size)(*self) - 1;
self->data[idx] = self->data[n];
- --self->size;
+ cx_memb(_pop_back)(self);
cx_memb(_sift_down_)(self->data - 1, idx + 1, n);
}
STC_DEF void
cx_memb(_push)(Self* self, cx_value_t value) {
- cx_memb(_push_back_)(self, value); /* sift-up the value */
+ cx_memb(_push_back)(self, value); /* sift-up the value */
size_t n = cx_memb(_size)(*self), c = n;
cx_value_t *arr = self->data - 1;
for (; c > 1 && i_cmp(&arr[c >> 1], &value) < 0; c >>= 1)
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index 42717ee8..984b8fad 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -32,14 +32,14 @@
#include "template.h"
#if !defined i_fwd
- cx_deftypes(_c_cstack_types, Self, i_val);
+cx_deftypes(_c_cstack_types, Self, i_val);
#endif
typedef i_valraw cx_rawvalue_t;
STC_INLINE Self cx_memb(_init)(void)
{ return (Self){0, 0, 0}; }
-STC_INLINE Self cx_memb(_init_with_capacity)(size_t cap) {
+STC_INLINE Self cx_memb(_with_capacity)(size_t cap) {
Self out = {(cx_value_t *) c_malloc(cap*sizeof(cx_value_t)), 0, cap};
return out;
}
@@ -65,11 +65,13 @@ STC_INLINE cx_value_t* cx_memb(_top)(const Self* self)
{ return &self->data[self->size - 1]; }
STC_INLINE void cx_memb(_pop)(Self* self)
- { --self->size; }
+ { --self->size; i_valdel(&self->data[self->size]); }
STC_INLINE void cx_memb(_push)(Self* self, cx_value_t value) {
- if (self->size == self->capacity)
- self->data = (cx_value_t *) c_realloc(self->data, (self->capacity = self->size*3/2 + 4)*sizeof value);
+ if (self->size == self->capacity) {
+ self->capacity = self->size*3/2 + 4;
+ self->data = (cx_value_t *)c_realloc(self->data, self->capacity*sizeof value);
+ }
self->data[ self->size++ ] = value;
}