summaryrefslogtreecommitdiffhomepage
path: root/misc
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2023-06-29 09:32:39 +0200
committerTyge Lovset <[email protected]>2023-06-29 10:48:03 +0200
commit764d6b5a831c4ff58fb717a1360fe80f691a424d (patch)
tree55b0353d1952f749dc34d4286b25970de4039545 /misc
parent5096c3c951f6b99e9d6ee04a21531e226153cca9 (diff)
downloadSTC-modified-764d6b5a831c4ff58fb717a1360fe80f691a424d.tar.gz
STC-modified-764d6b5a831c4ff58fb717a1360fe80f691a424d.zip
Usage change: comparison is no longer enabled when specifying i_val for cvec, cdeq and clist (like cstack and cqueue). Comparison functions are still required when specifying i_valclass. For fundamental/native types like integers, floats etc., define i_native_cmp along with i_val instead of i_less/i_cmp/i_eq.
Diffstat (limited to 'misc')
-rw-r--r--misc/examples/arc_containers.c4
-rw-r--r--misc/examples/arc_demo.c14
-rw-r--r--misc/examples/arcvec_erase.c2
-rw-r--r--misc/examples/box2.c4
-rw-r--r--misc/examples/complex.c4
-rw-r--r--misc/examples/csmap_find.c1
-rw-r--r--misc/examples/demos.c1
-rw-r--r--misc/examples/intrusive.c5
-rw-r--r--misc/examples/list.c1
-rw-r--r--misc/examples/lower_bound.c1
-rw-r--r--misc/examples/music_arc.c6
-rw-r--r--misc/examples/new_list.c5
-rw-r--r--misc/examples/new_sptr.c6
-rw-r--r--misc/examples/new_vec.c5
-rw-r--r--misc/examples/scheduler.c1
15 files changed, 31 insertions, 29 deletions
diff --git a/misc/examples/arc_containers.c b/misc/examples/arc_containers.c
index 7038734e..b05bbea6 100644
--- a/misc/examples/arc_containers.c
+++ b/misc/examples/arc_containers.c
@@ -13,17 +13,15 @@
#define i_val 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|c_no_cmp|c_no_clone
+#define i_opt c_no_atomic
#include <stc/carc.h>
#define i_type Stack
#define i_valboxed Arc // define i_valboxed for carc/cbox value (not i_val)
-#define i_opt c_no_cmp
#include <stc/cvec.h>
#define i_type List
#define i_valboxed Arc // as above
-#define i_opt c_no_cmp
#include <stc/clist.h>
int main()
diff --git a/misc/examples/arc_demo.c b/misc/examples/arc_demo.c
index 2339adbb..4cda1c8b 100644
--- a/misc/examples/arc_demo.c
+++ b/misc/examples/arc_demo.c
@@ -11,13 +11,13 @@ void int_drop(int* x) {
#define i_type Arc // set type name to be defined (instead of 'carc_int')
#define i_val int
#define i_valdrop int_drop // optional, just to display the elements destroyed
-#define i_no_clone // required because of valdrop
+#define i_native_cmp // use int comparison (x < y, x == y).
#include <stc/carc.h> // Arc
-#define i_keyboxed Arc // note: use i_keyboxed instead of i_key for carc/cbox elements
+#define i_keyboxed Arc // note: use i_keyboxed instead of i_key for carc/cbox elements
#include <stc/csset.h> // csset_Arc (like: std::set<std::shared_ptr<int>>)
-#define i_valboxed Arc // note: as above.
+#define i_valboxed Arc // note: as above.
#include <stc/cvec.h> // cvec_Arc (like: std::vector<std::shared_ptr<int>>)
int main()
@@ -25,8 +25,12 @@ int main()
const int years[] = {2021, 2012, 2022, 2015};
cvec_Arc vec = {0};
- c_forrange (i, c_arraylen(years))
- cvec_Arc_push(&vec, Arc_from(years[i]));
+ c_forrange (i, c_arraylen(years)) {
+ cvec_Arc_emplace(&vec, years[i]);
+ // cvec_Arc_push(&vec, Arc_from(years[i])); // alt.
+ }
+
+ cvec_Arc_sort(&vec);
printf("vec:");
c_foreach (i, cvec_Arc, vec)
diff --git a/misc/examples/arcvec_erase.c b/misc/examples/arcvec_erase.c
index 28160c1c..0b9252d9 100644
--- a/misc/examples/arcvec_erase.c
+++ b/misc/examples/arcvec_erase.c
@@ -5,7 +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
-#define i_no_clone // required because of valdrop
+#define i_native_cmp // enable sort/search for int type
#include <stc/carc.h> // Shared pointer to int
#define i_type Vec
diff --git a/misc/examples/box2.c b/misc/examples/box2.c
index 963a3815..d3762462 100644
--- a/misc/examples/box2.c
+++ b/misc/examples/box2.c
@@ -15,16 +15,14 @@ typedef struct {
} Rectangle;
#define i_val Point
-#define i_no_cmp
#include <stc/cbox.h> // cbox_Point
#define i_val Rectangle
-#define i_no_cmp
#include <stc/cbox.h> // cbox_Rectangle
// Box in box:
-#define i_valboxed cbox_Point // NB: use i_valboxed when value is a cbox or carc!
#define i_type BoxBoxPoint
+#define i_valboxed cbox_Point // NB: use i_valboxed when value is a cbox or carc!
#define i_no_cmp
#include <stc/cbox.h> // BoxBoxPoint
diff --git a/misc/examples/complex.c b/misc/examples/complex.c
index 2d8dbf62..b5ea847a 100644
--- a/misc/examples/complex.c
+++ b/misc/examples/complex.c
@@ -13,8 +13,8 @@
#include <stc/cstack.h>
#define i_type StackList
-#define i_valclass FloatStack // "class" picks up _clone, _drop
-#define i_opt c_no_cmp // no FloatStack_cmp()
+#define i_valclass FloatStack // "class" picks up _clone, _drop, _cmp
+#define i_opt c_no_cmp // exclude FloatStack_cmp(): not defined
#include <stc/clist.h>
#define i_type ListMap
diff --git a/misc/examples/csmap_find.c b/misc/examples/csmap_find.c
index c123e398..645828a3 100644
--- a/misc/examples/csmap_find.c
+++ b/misc/examples/csmap_find.c
@@ -9,7 +9,6 @@
#include <stc/csmap.h>
#define i_val csmap_istr_raw
-#define i_opt c_no_cmp
#define i_tag istr
#include <stc/cvec.h>
diff --git a/misc/examples/demos.c b/misc/examples/demos.c
index 8488dfb9..b2f50ebf 100644
--- a/misc/examples/demos.c
+++ b/misc/examples/demos.c
@@ -74,6 +74,7 @@ void vectordemo2()
#define i_val int
#define i_tag ix
+#define i_native_cmp
#include <stc/clist.h>
void listdemo1()
diff --git a/misc/examples/intrusive.c b/misc/examples/intrusive.c
index 0d503575..0d59c5ab 100644
--- a/misc/examples/intrusive.c
+++ b/misc/examples/intrusive.c
@@ -4,7 +4,8 @@
#define i_type List
#define i_val int
-#include <stc/clist.h>
+#define i_native_cmp
+#include <stc/clist.h>
void printList(List list) {
printf("list:");
@@ -16,7 +17,7 @@ void printList(List list) {
int main() {
List list = {0};
c_forlist (i, int, {6, 9, 3, 1, 7, 4, 5, 2, 8})
- List_push_back_node(&list, c_new(List_node, {0, *i.ref}));
+ List_push_back_node(&list, c_new(List_node, {.value=*i.ref}));
printList(list);
diff --git a/misc/examples/list.c b/misc/examples/list.c
index ed27aa50..08fe837f 100644
--- a/misc/examples/list.c
+++ b/misc/examples/list.c
@@ -5,6 +5,7 @@
#define i_type DList
#define i_val double
+#define i_native_cmp
#include <stc/clist.h>
int main() {
diff --git a/misc/examples/lower_bound.c b/misc/examples/lower_bound.c
index d146c4d9..5b395e45 100644
--- a/misc/examples/lower_bound.c
+++ b/misc/examples/lower_bound.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#define i_val int
+#define i_native_cmp
#include <stc/cvec.h>
#define i_val int
diff --git a/misc/examples/music_arc.c b/misc/examples/music_arc.c
index 18ea30c0..4efc35c8 100644
--- a/misc/examples/music_arc.c
+++ b/misc/examples/music_arc.c
@@ -23,13 +23,13 @@ void Song_drop(Song* s) {
// Define the shared pointer:
#define i_type SongArc
#define i_valclass Song
-#define i_opt c_no_hash // arc require hash fn, disable as we don't need it.
+#define i_no_hash // no hash fn for Song, fallback hash pointer to Song.
#include <stc/carc.h>
// ... and a vector of them
#define i_type SongVec
-#define i_valboxed SongArc // use i_valboxed on carc / cbox instead of i_val
-#include <stc/cstack.h>
+#define i_valboxed SongArc // use i_valboxed on carc / cbox (instead of i_val)
+#include <stc/cvec.h>
void example3()
{
diff --git a/misc/examples/new_list.c b/misc/examples/new_list.c
index 382943bb..b5ff847e 100644
--- a/misc/examples/new_list.c
+++ b/misc/examples/new_list.c
@@ -27,12 +27,13 @@ int point_cmp(const Point* a, const Point* b) {
#include <stc/clist.h>
#define i_val float
+#define i_native_cmp // use < and == operators for comparison
#include <stc/clist.h>
void MyStruct_drop(MyStruct* s);
#define i_type MyList
-#define i_valclass MyStruct // i_valclass uses MyStruct_drop
-#define i_opt c_no_clone|c_no_cmp
+#define i_valclass MyStruct // MyStruct contains "class"-types, so define as "class"
+#define i_opt c_no_clone|c_no_cmp // exclude cloning and comparison support
#include <stc/clist.h>
void MyStruct_drop(MyStruct* s) {
diff --git a/misc/examples/new_sptr.c b/misc/examples/new_sptr.c
index 36a61f9c..2eff41a5 100644
--- a/misc/examples/new_sptr.c
+++ b/misc/examples/new_sptr.c
@@ -9,15 +9,13 @@ int Person_cmp(const Person* a, const Person* b);
uint64_t Person_hash(const Person* p);
#define i_type PersonArc
-#define i_valclass 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)
+#define i_valclass Person // "class" assume _clone, _drop, _cmp, _hash is defined.
#include <stc/carc.h>
#define i_type IPtr
#define i_val int
#define i_valdrop(x) printf("drop: %d\n", *x)
-#define i_no_clone
+#define i_native_cmp
#include <stc/carc.h>
#define i_type IPStack
diff --git a/misc/examples/new_vec.c b/misc/examples/new_vec.c
index e10910d9..6329b185 100644
--- a/misc/examples/new_vec.c
+++ b/misc/examples/new_vec.c
@@ -10,16 +10,17 @@ typedef struct MyStruct {
} MyStruct;
#define i_val int
-#define i_is_forward
#define i_tag i32
+#define i_is_forward
#include <stc/cvec.h>
typedef struct Point { int x, y; } Point;
#define i_val Point
+#define i_tag pnt
#define i_less(a, b) a->x < b->x || (a->x == b->x && a->y < b->y)
+#define i_eq(a, b) a->x == b->x && a->y == b->y
#define i_is_forward
-#define i_tag pnt
#include <stc/cvec.h>
int main()
diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c
index 59101b4e..04f7ba4a 100644
--- a/misc/examples/scheduler.c
+++ b/misc/examples/scheduler.c
@@ -8,7 +8,6 @@ cco_closure(bool, Task,
#define i_type Scheduler
#define i_val struct Task
-#define i_no_cmp
#include <stc/cqueue.h>
static bool schedule(Scheduler* sched)