summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--docs/carc_api.md9
-rw-r--r--docs/cmap_api.md6
-rw-r--r--examples/arc_containers.c10
-rw-r--r--examples/arc_demo.c4
-rw-r--r--examples/arcvec_erase.c12
-rw-r--r--examples/box.c20
-rw-r--r--examples/csmap_find.c12
-rw-r--r--examples/csmap_insert.c10
-rw-r--r--examples/demos.c4
-rw-r--r--examples/gauss1.c4
-rw-r--r--examples/new_sptr.c80
-rw-r--r--examples/new_vec.c16
-rw-r--r--examples/person_arc.c22
-rw-r--r--examples/vikings.c2
-rw-r--r--include/stc/carc.h24
-rw-r--r--include/stc/cbox.h15
-rw-r--r--include/stc/template.h15
18 files changed, 144 insertions, 123 deletions
diff --git a/README.md b/README.md
index cda096ad..f3417c34 100644
--- a/README.md
+++ b/README.md
@@ -331,7 +331,7 @@ Specials:
- `i_key_ssv` - Define key type *cstr* and container i_tag = *ssv*. It binds type convertion from/to *csview*, and its ***cmp***, ***eq***, ***hash***, and ***keydrop*** functions.
- `i_key_arcbox TYPE` - Define container key type where TYPE is a smart pointer **carc** or **cbox**. NB: not to be used when defining carc/cbox types themselves.
- `i_key_bind TYPE` - General version of the three above - will auto-bind to standard named functions: *TYPE_clone*, *TYPE_drop*, *TYPE_cmp*, *TYPE_eq*, *TYPE_hash*. If `i_keyraw` is defined, *TYPE_toraw* function is bound to `i_keyto`. Only functions required by the particular container need to be defined. E.g., only **cmap** and **cset** and smart pointers uses *TYPE_hash* and *TYPE_eq*. **cstack** does not use *TYPE_cmp*. *TYPE_clone* is not used if *#define i_opt c_no_clone* is specified. Likewise, *TYPE_cmp* is not used if *#define i_opt c_no_cmp* is specified.
-- `i_val_str`, `i_val_bind`, `i_val_arcbox` - Similar rules as for ***key***.
+- `i_val_str`, `i_val_ssv`, `i_val_arcbox`, `i_val_bind` - Similar rules as for ***key***.
**Notes**:
- Instead of defining `i_cmp`, you may define *i_opt c_no_cmp* to disable *searching and sorting* functions.
diff --git a/docs/carc_api.md b/docs/carc_api.md
index fd20ad01..08070889 100644
--- a/docs/carc_api.md
+++ b/docs/carc_api.md
@@ -84,15 +84,12 @@ bool carc_X_value_eq(const i_val* x, const i_val* y);
#include <stc/csmap.h>
#define i_type Arc // (atomic) ref. counted pointer
-#define i_val Map
-#define i_valclone Map_clone
-// override Map_drop(p):
-#define i_valdrop(p) (printf("drop Arc:\n"), Map_drop(p))
-#define i_opt c_no_atomic // make it non-atomic sharing.
+#define i_val_bind Map // Note i_val_bind: Map is a "class", i.e. has clone, drop functions.
+#define i_valdrop(p) (printf("drop Arc:\n"), Map_drop(p)) // override Map_drop(p):
#include <stc/carc.h>
#define i_type Stack
-#define i_val_arcbox Arc // NB: define i_val_arcbox for carc or cbox value-type
+#define i_val_arcbox Arc // Note: use i_val_arcbox for carc or cbox value types
#include <stc/cstack.h>
int main()
diff --git a/docs/cmap_api.md b/docs/cmap_api.md
index 863fbaf6..10bca836 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -301,7 +301,7 @@ static inline void Viking_drop(Viking* vk) {
#define i_key_bind Viking
#define i_val int
/*
- i_key_bind makes these defines, unless they are already defined:
+ i_key_bind implies these defines, unless they are already defined:
#define i_cmp Viking_cmp
#define i_hash Viking_hash
#define i_keyclone Viking_clone
@@ -377,7 +377,7 @@ static inline RViking Viking_toraw(const Viking* vp) {
// With this in place, we define the Viking => int hash map type:
#define i_type Vikings
-#define i_key_bind Viking
+#define i_key_bind Viking
#define i_keyraw RViking
#define i_keyfrom Viking_from
#define i_opt c_no_clone // disable map cloning
@@ -385,7 +385,7 @@ static inline RViking Viking_toraw(const Viking* vp) {
#define i_val int
#include <stc/cmap.h>
/*
- i_key_bind sets up these defines, unless they are already defined:
+ i_key_bind implies these defines, unless they are already defined:
#define i_cmp RViking_cmp
//#define i_hash RViking_hash // already defined above.
//#define i_keyclone Viking_clone // not used because c_no_clone
diff --git a/examples/arc_containers.c b/examples/arc_containers.c
index 6aa80dcf..356afaf5 100644
--- a/examples/arc_containers.c
+++ b/examples/arc_containers.c
@@ -9,14 +9,14 @@
#include <stc/csmap.h>
#define i_type Arc // (atomic) ref. counted type
-#define i_val Map
+#define i_val_bind Map
#define i_valdrop(p) (printf("drop Arc:\n"), Map_drop(p))
// no need for atomic ref. count in single thread:
#define i_opt c_no_atomic
#include <stc/carc.h>
#define i_type Stack
-#define i_val_arcbox Arc // define i_val_bind for carc/cbox value (not i_val)
+#define i_val_arcbox Arc // define i_val_arcbox for carc/cbox value (not i_val)
#include <stc/cstack.h>
#define i_type List
@@ -30,18 +30,18 @@ int main()
{
// POPULATE stack with shared pointers to Maps:
Map *map;
- map = Stack_push(&stack, Arc_make(Map_init()))->get;
+ map = Stack_push(&stack, Arc_from(Map_init()))->get;
Map_emplace(map, "Joey", 1990);
Map_emplace(map, "Mary", 1995);
Map_emplace(map, "Joanna", 1992);
- map = Stack_push(&stack, Arc_make(Map_init()))->get;
+ map = Stack_push(&stack, Arc_from(Map_init()))->get;
Map_emplace(map, "Rosanna", 2001);
Map_emplace(map, "Brad", 1999);
Map_emplace(map, "Jack", 1980);
// POPULATE list:
- map = List_push_back(&list, Arc_make(Map_init()))->get;
+ map = List_push_back(&list, Arc_from(Map_init()))->get;
Map_emplace(map, "Steve", 1979);
Map_emplace(map, "Rick", 1974);
Map_emplace(map, "Tracy", 2003);
diff --git a/examples/arc_demo.c b/examples/arc_demo.c
index 345432b6..96e5dadd 100644
--- a/examples/arc_demo.c
+++ b/examples/arc_demo.c
@@ -13,7 +13,7 @@ void int_drop(int* x) {
#define i_valdrop int_drop // optional, just to display the elements destroyed
#include <stc/carc.h> // Arc
-#define i_key_arcbox Arc // note: use i_key_bind instead of i_key for carc/cbox elements
+#define i_key_arcbox Arc // note: use i_key_arcbox instead of i_key for carc/cbox elements
#include <stc/csset.h> // csset_Arc (like: std::set<std::shared_ptr<int>>)
#define i_val_arcbox Arc // note: as above.
@@ -26,7 +26,7 @@ int main()
{
const int years[] = {2021, 2012, 2022, 2015};
c_forloop (i, c_arraylen(years))
- cvec_Arc_push_back(&vec, Arc_make(years[i]));
+ cvec_Arc_push(&vec, Arc_from(years[i]));
printf("vec:");
c_foreach (i, cvec_Arc, vec) printf(" %d", *i.ref->get);
diff --git a/examples/arcvec_erase.c b/examples/arcvec_erase.c
index 63b646a3..6990c94a 100644
--- a/examples/arcvec_erase.c
+++ b/examples/arcvec_erase.c
@@ -5,9 +5,7 @@ void show_drop(int* x) { printf("drop: %d\n", *x); }
#define i_type Arc
#define i_val int
#define i_valdrop show_drop
-// carc/cbox will use pointer address comparison of i_val
-// if no 'i_cmp' is defined, otherwise 'i_cmp' is used
-// to compare object values.
+#define i_cmp c_default_cmp // enable object comparisons (default ptr compare)
#include <stc/carc.h> // Shared pointer to int
#define i_type Vec
@@ -29,9 +27,8 @@ int main()
printf("vec before erase :");
c_foreach (i, Vec, vec)
printf(" %d", *i.ref->get);
- puts("");
- printf("erase vec.data[2]; or first matching value depending on compare.\n");
+ printf("\nerase vec.data[2]; or first matching value depending on compare.\n");
Vec_iter it;
it = Vec_find(&vec, *vec.data[2].get);
if (it.ref != Vec_end(&vec).ref)
@@ -45,6 +42,11 @@ int main()
printf("vec after erase :");
c_foreach (i, Vec, vec)
printf(" %d", *i.ref->get);
+
+ Vec_sort(&vec);
+ printf("\nvec after sort :");
+ c_foreach (i, Vec, vec)
+ printf(" %d", *i.ref->get);
puts("\nDone");
}
diff --git a/examples/box.c b/examples/box.c
index b12f1f71..b0f05530 100644
--- a/examples/box.c
+++ b/examples/box.c
@@ -3,7 +3,7 @@
typedef struct { cstr name, last; } Person;
-Person Person_new(const char* name, const char* last) {
+Person Person_make(const char* name, const char* last) {
return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
@@ -33,24 +33,24 @@ void Person_drop(Person* p) {
#define i_type Persons
#define i_val_arcbox PBox // informs that PBox is a smart pointer.
-#include <stc/cvec.h>
+#include <stc/csset.h>
int main()
{
c_auto (Persons, vec)
c_auto (PBox, p, q)
{
- p = PBox_make(Person_new("Laura", "Palmer"));
+ p = PBox_from(Person_make("Laura", "Palmer"));
q = PBox_clone(p);
cstr_assign(&q.get->name, "Leland");
printf("orig: %s %s\n", cstr_str(&p.get->name), cstr_str(&p.get->last));
printf("copy: %s %s\n", cstr_str(&q.get->name), cstr_str(&q.get->last));
- Persons_push(&vec, PBox_make(Person_new("Dale", "Cooper")));
- Persons_push(&vec, PBox_make(Person_new("Audrey", "Home")));
+ Persons_emplace(&vec, Person_make("Dale", "Cooper"));
+ Persons_emplace(&vec, Person_make("Audrey", "Home"));
- // NB! Clone p and q to the vector using emplace_back()
+ // NB! Clone/share p and q in the Persons container.
Persons_push(&vec, PBox_clone(p));
Persons_push(&vec, PBox_clone(q));
@@ -59,16 +59,10 @@ int main()
puts("");
// Look-up Audrey! Create a temporary Person for lookup.
- c_with (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) {
+ c_with (Person a = Person_make("Audrey", "Home"), Person_drop(&a)) {
const PBox *v = Persons_get(&vec, a); // lookup
if (v) printf("found: %s %s\n", cstr_str(&v->get->name), cstr_str(&v->get->last));
}
puts("");
-
- // Alternative to use cbox (when not placed in container).
- Person *she = c_new(Person, Person_new("Shelly", "Johnson"));
- printf("%s %s\n", cstr_str(&she->name), cstr_str(&she->last));
- c_delete(Person, she); // drop and free
- puts("");
}
}
diff --git a/examples/csmap_find.c b/examples/csmap_find.c
index 9287a89b..98da589e 100644
--- a/examples/csmap_find.c
+++ b/examples/csmap_find.c
@@ -52,12 +52,12 @@ int main()
print_collection_csmap_istr(&m1);
typedef cvec_istr_value pair;
- cvec_istr_push_back(&v, (pair){43, "Tc"});
- cvec_istr_push_back(&v, (pair){41, "Nb"});
- cvec_istr_push_back(&v, (pair){46, "Pd"});
- cvec_istr_push_back(&v, (pair){42, "Mo"});
- cvec_istr_push_back(&v, (pair){44, "Ru"});
- cvec_istr_push_back(&v, (pair){44, "Ru"}); // attempt a duplicate
+ cvec_istr_push(&v, (pair){43, "Tc"});
+ cvec_istr_push(&v, (pair){41, "Nb"});
+ cvec_istr_push(&v, (pair){46, "Pd"});
+ cvec_istr_push(&v, (pair){42, "Mo"});
+ cvec_istr_push(&v, (pair){44, "Ru"});
+ cvec_istr_push(&v, (pair){44, "Ru"}); // attempt a duplicate
puts("Inserting the following vector data into m1:");
print_collection_cvec_istr(&v);
diff --git a/examples/csmap_insert.c b/examples/csmap_insert.c
index 4d4c3871..5b18d691 100644
--- a/examples/csmap_insert.c
+++ b/examples/csmap_insert.c
@@ -63,11 +63,11 @@ int main()
c_auto (csmap_ii, m2)
c_auto (cvec_ii, v) {
typedef cvec_ii_value 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});
+ cvec_ii_push(&v, (ipair){43, 294});
+ cvec_ii_push(&v, (ipair){41, 262});
+ cvec_ii_push(&v, (ipair){45, 330});
+ cvec_ii_push(&v, (ipair){42, 277});
+ cvec_ii_push(&v, (ipair){44, 311});
puts("Inserting the following vector data into m2:");
c_foreach (e, cvec_ii, v)
diff --git a/examples/demos.c b/examples/demos.c
index 3f0125fd..d15e5c6e 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -39,12 +39,12 @@ void vectordemo1()
{
cvec_ix_reserve(&bignums, 100);
for (size_t i = 10; i <= 100; i += 10)
- cvec_ix_push_back(&bignums, i * i);
+ cvec_ix_push(&bignums, i * i);
printf("erase - %d: %" c_zu "\n", 3, bignums.data[3]);
cvec_ix_erase_n(&bignums, 3, 1); // erase index 3
- cvec_ix_pop_back(&bignums); // erase the last
+ cvec_ix_pop(&bignums); // erase the last
cvec_ix_erase_n(&bignums, 0, 1); // erase the first
for (size_t i = 0; i < cvec_ix_size(&bignums); ++i) {
diff --git a/examples/gauss1.c b/examples/gauss1.c
index 81d57852..65a6cc07 100644
--- a/examples/gauss1.c
+++ b/examples/gauss1.c
@@ -11,7 +11,7 @@
#include <stc/cmap.h>
// Declare int vector with entries from the cmap.
-#define i_val cmap_ii_raw
+#define i_val cmap_ii_value
#define i_less(x, y) x->first < y->first
#define i_tag ii
#include <stc/cvec.h>
@@ -39,7 +39,7 @@ int main()
// Transfer map to vec and sort it by map keys.
c_foreach (i, cmap_ii, histmap)
- cvec_ii_push_back(&histvec, (cmap_ii_raw){i.ref->first, i.ref->second});
+ cvec_ii_push(&histvec, (cmap_ii_value){i.ref->first, i.ref->second});
cvec_ii_sort(&histvec);
diff --git a/examples/new_sptr.c b/examples/new_sptr.c
index e3431d8a..1c0ea89b 100644
--- a/examples/new_sptr.c
+++ b/examples/new_sptr.c
@@ -1,10 +1,41 @@
#include <stc/cstr.h>
-struct Person { cstr name, last; } typedef Person;
+typedef struct { cstr name, last; } Person;
+Person Person_make(const char* name, const char* last);
+Person Person_clone(Person p);
+void Person_drop(Person* p);
+int Person_cmp(const Person* a, const Person* b);
+uint64_t Person_hash(const Person* p);
-Person Person_from(const char* name, const char* last) {
+#define i_type PersonArc
+#define i_val_bind Person // "class" ensure Person_drop will be called
+#define i_cmp Person_cmp // enable carc object comparisons (not ptr to obj)
+#define i_hash Person_hash // enable carc object hash (not ptr to obj)
+#include <stc/carc.h>
+
+#define i_type IPtr
+#define i_val int
+#define i_valdrop(x) printf("drop: %d\n", *x)
+#include <stc/carc.h>
+
+#define i_type IPStack
+#define i_val_arcbox IPtr
+#include <stc/cstack.h>
+
+#define i_type PASet
+#define i_val_arcbox PersonArc
+#include <stc/cset.h>
+
+
+Person Person_make(const char* name, const char* last) {
return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
+int Person_cmp(const Person* a, const Person* b) {
+ return cstr_cmp(&a->name, &b->name);
+}
+uint64_t Person_hash(const Person* p) {
+ return cstr_hash(&p->name);
+}
Person Person_clone(Person p) {
p.name = cstr_clone(p.name), p.last = cstr_clone(p.last);
return p;
@@ -14,41 +45,28 @@ void Person_drop(Person* p) {
c_drop(cstr, &p->name, &p->last);
}
-#define i_val_bind Person
-#define i_tag person
-#include <stc/carc.h>
-
-// ...
-#define i_type SPtr
-#define i_val int
-#define i_valdrop(x) printf("drop: %d\n", *x)
-#include <stc/carc.h>
-
-#define i_val_arcbox SPtr
-#define i_tag iptr
-#include <stc/cstack.h>
int main(void) {
- c_auto (carc_person, p, q, r, s)
+ c_auto (PersonArc, p, q, r, s)
{
puts("Ex1");
- p = carc_person_from(Person_from("John", "Smiths"));
- q = carc_person_clone(p);
- r = carc_person_clone(p);
- s = carc_person_from(Person_clone(*p.get)); // deep copy
- printf("%s %s. uses: %lu\n", cstr_str(&r.get->name), cstr_str(&s.get->last), *p.use_count);
+ p = PersonArc_from(Person_make("John", "Smiths"));
+ q = PersonArc_clone(p); // share
+ r = PersonArc_clone(p);
+ s = PersonArc_from(Person_clone(*p.get)); // deep copy
+ printf("%s %s: refs %ld\n", cstr_str(&p.get->name), cstr_str(&p.get->last), *p.use_count);
}
-
- c_auto (cstack_iptr, stk) {
+ c_auto (IPStack, vec)
+ {
puts("Ex2");
- cstack_iptr_push(&stk, SPtr_from(10));
- cstack_iptr_push(&stk, SPtr_from(20));
- cstack_iptr_push(&stk, SPtr_from(30));
- cstack_iptr_push(&stk, SPtr_clone(*cstack_iptr_top(&stk)));
- cstack_iptr_push(&stk, SPtr_clone(*cstack_iptr_begin(&stk).ref));
-
- c_foreach (i, cstack_iptr, stk)
- printf(" (%d, uses %ld)", *i.ref->get, *i.ref->use_count);
+ IPStack_push(&vec, IPtr_from(10));
+ IPStack_push(&vec, IPtr_from(20));
+ IPStack_emplace(&vec, 30); // same as IPStack_push(&vec, IPtr_from(30));
+ IPStack_push(&vec, IPtr_clone(*IPStack_back(&vec)));
+ IPStack_push(&vec, IPtr_clone(*IPStack_front(&vec)));
+
+ c_foreach (i, IPStack, vec)
+ printf(" (%d: refs %ld)", *i.ref->get, *i.ref->use_count);
puts("");
}
}
diff --git a/examples/new_vec.c b/examples/new_vec.c
index e0fa79ae..6e6be95a 100644
--- a/examples/new_vec.c
+++ b/examples/new_vec.c
@@ -21,7 +21,8 @@ int point_cmp(const Point* a, const Point* b) {
}
#define i_val Point
-#define i_cmp point_cmp
+//#define i_cmp point_cmp
+#define i_less(a, b) a->x < b->x || (a->x == b->x && a->y < b->y)
#define i_opt c_is_fwd
#define i_tag pnt
#include <stc/cvec.h>
@@ -40,17 +41,18 @@ int main()
c_auto (cvec_pnt, pvec)
c_auto (cvec_str, svec)
{
- cvec_i32_push_back(&vec, 123);
- cvec_float_push_back(&fvec, 123.3f);
+ cvec_i32_push(&vec, 123);
+ cvec_float_push(&fvec, 123.3f);
- cvec_pnt_push_back(&pvec, (Point){42, 14});
- cvec_pnt_push_back(&pvec, (Point){32, 94});
- cvec_pnt_push_back(&pvec, (Point){62, 81});
+ cvec_pnt_push(&pvec, (Point){42, 14});
+ cvec_pnt_push(&pvec, (Point){32, 94});
+ cvec_pnt_push(&pvec, (Point){62, 81});
+ cvec_pnt_push(&pvec, (Point){32, 91});
cvec_pnt_sort(&pvec);
c_foreach (i, cvec_pnt, pvec)
printf(" (%d %d)", i.ref->x, i.ref->y);
puts("");
- cvec_str_emplace_back(&svec, "Hello, friend");
+ cvec_str_emplace(&svec, "Hello, friend");
}
}
diff --git a/examples/person_arc.c b/examples/person_arc.c
index cd8d1f87..203d362a 100644
--- a/examples/person_arc.c
+++ b/examples/person_arc.c
@@ -3,7 +3,7 @@
typedef struct { cstr name, last; } Person;
-Person Person_new(const char* name, const char* last) {
+Person Person_make(const char* name, const char* last) {
return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
@@ -28,11 +28,12 @@ void Person_drop(Person* p) {
}
#define i_type PSPtr
-#define i_val_bind Person // binds Person_cmp, ...
+#define i_val_bind Person // ensure Person_drop
+#define i_cmp Person_cmp // specify object cmp, instead of ptr cmp for arc.
#include <stc/carc.h>
#define i_type Persons
-#define i_val_arcbox PSPtr // binds PSPtr_cmp, ...
+#define i_val_arcbox PSPtr // binds PSPtr_cmp, PSPtr_drop...
#include <stc/cvec.h>
@@ -41,32 +42,33 @@ int main()
c_auto (Persons, vec)
c_auto (PSPtr, p, q)
{
- p = PSPtr_make(Person_new("Laura", "Palmer"));
+ p = PSPtr_from(Person_make("Laura", "Palmer"));
// We want a deep copy -- PSPtr_clone(p) only shares!
- q = PSPtr_make(Person_clone(*p.get));
+ q = PSPtr_from(Person_clone(*p.get));
cstr_assign(&q.get->name, "Leland");
printf("orig: %s %s\n", cstr_str(&p.get->name), cstr_str(&p.get->last));
printf("copy: %s %s\n", cstr_str(&q.get->name), cstr_str(&q.get->last));
- Persons_push_back(&vec, PSPtr_make(Person_new("Dale", "Cooper")));
- Persons_push_back(&vec, PSPtr_make(Person_new("Audrey", "Home")));
+ // Use Persons_emplace to implicitly call PSPtr_make on the argument.
+ // No need to do: Persons_push(&vec, PSPtr_make(Person_make("Audrey", "Home")));
+ Persons_emplace(&vec, Person_make("Audrey", "Home"));
+ Persons_emplace(&vec, Person_make("Dale", "Cooper"));
// Clone/share p and q to the vector
c_forlist (i, PSPtr, {p, q})
- Persons_push_back(&vec, PSPtr_clone(*i.ref));
+ Persons_push(&vec, PSPtr_clone(*i.ref));
c_foreach (i, Persons, vec)
printf("%s %s\n", cstr_str(&i.ref->get->name), cstr_str(&i.ref->get->last));
puts("");
// Look-up Audrey!
- c_with (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) {
+ c_with (Person a = Person_make("Audrey", "Home"), Person_drop(&a)) {
const PSPtr *v = Persons_get(&vec, a);
if (v) printf("found: %s %s\n", cstr_str(&v->get->name), cstr_str(&v->get->last));
}
-
puts("");
}
}
diff --git a/examples/vikings.c b/examples/vikings.c
index f291a601..528c30bf 100644
--- a/examples/vikings.c
+++ b/examples/vikings.c
@@ -40,7 +40,7 @@ static inline RViking Viking_toraw(const Viking* vp) {
#define i_val int // mapped type
#include <stc/cmap.h>
/*
- i_key_bind set up these defines, unless they are already defined:
+ i_key_bind implies these defines, unless they are already defined:
i_cmp => RViking_cmp
//i_hash => RViking_hash // already defined.
//i_keyclone => Viking_clone // not used, because of c_no_clone
diff --git a/include/stc/carc.h b/include/stc/carc.h
index f0ab651b..6148a815 100644
--- a/include/stc/carc.h
+++ b/include/stc/carc.h
@@ -26,12 +26,13 @@
typedef struct { cstr name, last; } Person;
-Person Person_new(const char* name, const char* last) {
+Person Person_make(const char* name, const char* last) {
return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
void Person_drop(Person* p) {
printf("drop: %s %s\n", cstr_str(&p->name), cstr_str(&p->last));
- c_drop(cstr, &p->name, &p->last);
+ cstr_drop(&p->name);
+ cstr_drop(&p->last);
}
#define i_type ArcPers
@@ -40,10 +41,10 @@ void Person_drop(Person* p) {
#include <stc/carc.h>
int main() {
- ArcPers p = ArcPers_from(Person_new("John", "Smiths"));
+ ArcPers p = ArcPers_from(Person_make("John", "Smiths"));
ArcPers q = ArcPers_clone(p); // share the pointer
- printf("%s %s. uses: %" c_zu "\n", cstr_str(&q.get->name), cstr_str(&q.get->last), *q.use_count);
+ printf("%s %s. uses: %ld\n", cstr_str(&q.get->name), cstr_str(&q.get->last), *q.use_count);
c_drop(ArcPers, &p, &q);
}
*/
@@ -77,9 +78,12 @@ int main() {
#ifndef _i_prefix
#define _i_prefix carc_
#endif
-#if !(defined i_cmp || defined i_less || defined i_eq || defined i_hash)
+#if !(defined i_cmp || defined i_less)
#define _i_no_cmp
#endif
+#if !(defined i_eq || defined i_hash)
+ #define _i_no_hash
+#endif
#include "template.h"
typedef i_keyraw _cx_raw;
@@ -117,9 +121,6 @@ STC_INLINE _cx_self _cx_memb(_from)(_cx_value val) {
return ptr;
}
-STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) // [deprecated]
- { return _cx_memb(_from)(val); }
-
STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self)
{ return i_keyto(self->get); }
@@ -174,8 +175,8 @@ STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self ptr) {
}
STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) {
- #if defined _i_no_cmp
- return c_default_hash(&x);
+ #if defined _i_no_hash
+ return (uint64_t)x;
#else
_cx_raw rx = i_keyto(x);
return i_hash((&rx));
@@ -192,7 +193,7 @@ STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
}
STC_INLINE bool _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) {
- #if defined _i_no_cmp
+ #if defined _i_no_hash
return x == y;
#else
_cx_raw rx = i_keyto(x), ry = i_keyto(y);
@@ -211,4 +212,5 @@ STC_INLINE bool _cx_memb(_eq)(const _cx_self* x, const _cx_self* y)
#undef _i_atomic_inc
#undef _i_atomic_dec_and_test
+#undef _i_no_hash
#include "template.h"
diff --git a/include/stc/cbox.h b/include/stc/cbox.h
index fc300de3..410656c2 100644
--- a/include/stc/cbox.h
+++ b/include/stc/cbox.h
@@ -70,9 +70,12 @@ int main() {
#ifndef _i_prefix
#define _i_prefix cbox_
#endif
-#if !(defined i_cmp || defined i_less || defined i_eq || defined i_hash)
+#if !(defined i_cmp || defined i_less)
#define _i_no_cmp
#endif
+#if !(defined i_eq || defined i_hash)
+ #define _i_no_hash
+#endif
#include "template.h"
typedef i_keyraw _cx_raw;
@@ -95,9 +98,6 @@ STC_INLINE _cx_self _cx_memb(_from)(_cx_value val) {
_cx_self ptr = {c_alloc(_cx_value)};
*ptr.get = val; return ptr;
}
-// [deprecated]
-STC_INLINE _cx_self _cx_memb(_make)(_cx_value val)
- { return _cx_memb(_from)(val); }
STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self)
{ return i_keyto(self->get); }
@@ -149,8 +149,8 @@ STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self other) {
}
STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) {
- #if defined _i_no_cmp
- return c_default_hash(&x);
+ #if defined _i_no_hash
+ return (uint64_t)x;
#else
_cx_raw rx = i_keyto(x);
return i_hash((&rx));
@@ -167,7 +167,7 @@ STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
}
STC_INLINE bool _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) {
- #if defined _i_no_cmp
+ #if defined _i_no_hash
return x == y;
#else
_cx_raw rx = i_keyto(x), ry = i_keyto(y);
@@ -184,4 +184,5 @@ STC_INLINE int _cx_memb(_cmp)(const _cx_self* x, const _cx_self* y)
STC_INLINE bool _cx_memb(_eq)(const _cx_self* x, const _cx_self* y)
{ return _cx_memb(_value_eq)(x->get, y->get); }
+#undef _i_no_hash
#include "template.h"
diff --git a/include/stc/template.h b/include/stc/template.h
index 04eea1d4..8ac14583 100644
--- a/include/stc/template.h
+++ b/include/stc/template.h
@@ -52,6 +52,12 @@
#define _i_expandby 1
#endif
+#if (defined i_valraw) ^ (defined i_valto)
+ #error "both i_valto and i_valraw must be defined, if any"
+#elif defined i_valfrom && !defined i_valraw
+ #error "i_valfrom defined without i_valraw"
+#endif
+
#if !(defined i_key || defined i_key_str || defined i_key_ssv || \
defined i_key_bind || defined i_key_arcbox)
#define _i_key_from_val
@@ -177,7 +183,9 @@
#elif !defined i_eq
#define i_eq c_default_eq
#endif
-#if !defined i_less && !defined i_cmp
+#if defined i_less && defined i_cmp
+ #error "Only one of i_less and i_cmp may be defined"
+#elif !defined i_less && !defined i_cmp
#define i_less c_default_less
#elif !defined i_less
#define i_less(x, y) (i_cmp(x, y)) < 0
@@ -224,11 +232,6 @@
#ifndef i_val
#error "i_val* must be defined for maps"
#endif
-#if defined i_valraw ^ defined i_valto
- #error "both i_valto and i_valraw must be defined, if any"
-#elif defined i_valfrom && !defined i_valraw
- #error "i_valfrom defined without i_valraw"
-#endif
#if !defined i_valclone && (defined i_valdrop || defined i_valraw)
#define _i_no_clone