summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-12-16 07:32:16 +0100
committerTyge Løvset <[email protected]>2021-12-16 07:32:16 +0100
commit183a89859ba9914ee0546e4482b40be199e52292 (patch)
treec199c6f1f5dead5e349f2b00f70bb33989ee520e
parent7f264be520123c71211967a56d2826d272694edf (diff)
downloadSTC-modified-183a89859ba9914ee0546e4482b40be199e52292.tar.gz
STC-modified-183a89859ba9914ee0546e4482b40be199e52292.zip
Changed default comparison of csbox and csptr objects to comparing addresses to containing objects. If i_cmp is defined, it is used instead.
-rw-r--r--examples/box2.c2
-rw-r--r--examples/sptr_erase.c8
-rw-r--r--examples/sptr_music.c1
-rw-r--r--examples/sptr_pthread.c1
-rw-r--r--examples/sptr_to_maps.c3
-rw-r--r--examples/words.c7
-rw-r--r--include/stc/cbox.h6
-rw-r--r--include/stc/csptr.h6
-rw-r--r--include/stc/template.h37
9 files changed, 34 insertions, 37 deletions
diff --git a/examples/box2.c b/examples/box2.c
index 86374a96..48266963 100644
--- a/examples/box2.c
+++ b/examples/box2.c
@@ -18,11 +18,9 @@ struct {
} typedef Rectangle;
#define i_val Point
-#define i_opt c_no_compare
#include <stc/cbox.h> // cbox_Point
#define i_val Rectangle
-#define i_opt c_no_compare
#include <stc/cbox.h> // cbox_Rectangle
// Box in box:
diff --git a/examples/sptr_erase.c b/examples/sptr_erase.c
index eada6fd6..4707abb1 100644
--- a/examples/sptr_erase.c
+++ b/examples/sptr_erase.c
@@ -4,10 +4,10 @@ void show_del(int* x) { printf("del: %d\n", *x); }
#define i_val int
#define i_del show_del // this "destroy" func shows which elements are destroyed
-// csptr/cbox will try to use default comparison of i_val (int) if no cmp func is specified,
-// unless 'i_opt c_no_compare' is defined, in which case pointer addresses are compared.
-// See the different results by commenting in the next line.
-//#define i_opt c_no_compare
+// csptr/cbox will use pointer address comparison of i_val if no i_cmp func is specified,
+// or 'i_opt c_no_compare' is defined, otherwise i_cmp is used to compare object values.
+// See the different results by commenting out the next line.
+#define i_cmp(x, y) (*(x) - *(y))
#include <stc/csptr.h> // csptr_int: shared pointer to int
#define i_val_ref csptr_int
diff --git a/examples/sptr_music.c b/examples/sptr_music.c
index e8e08b5f..fcb7a43f 100644
--- a/examples/sptr_music.c
+++ b/examples/sptr_music.c
@@ -20,7 +20,6 @@ void Song_del(Song* s) {
#define i_val Song
#define i_del Song_del
#define i_tag song
-#define i_opt c_no_compare
#include <stc/csptr.h> // define csptr_song
#define i_val_ref csptr_song
diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c
index d82ea938..cfb845b6 100644
--- a/examples/sptr_pthread.c
+++ b/examples/sptr_pthread.c
@@ -15,7 +15,6 @@ void Base_del(Base* b) { printf("Base::~Base()\n"); }
#define i_val Base
#define i_del Base_del
-#define i_opt c_no_compare
#define i_tag base
#include <stc/csptr.h>
diff --git a/examples/sptr_to_maps.c b/examples/sptr_to_maps.c
index 77871304..a66e5b31 100644
--- a/examples/sptr_to_maps.c
+++ b/examples/sptr_to_maps.c
@@ -9,9 +9,8 @@
#define i_type Arc // (atomic) ref. counted type
#define i_val Map
#define i_del(p) (printf("del Arc:\n"), Map_del(p))
-// no comparison of Maps needed (or available), and
// no need for atomic ref. count in single thread:
-#define i_opt c_no_compare|c_no_atomic
+#define i_opt c_no_atomic
#include <stc/csptr.h>
#define i_type Stack
diff --git a/examples/words.c b/examples/words.c
index 951c3ded..6c0636dc 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -4,7 +4,6 @@
#define i_val_str
#include <stc/cvec.h>
-#define i_tag strn
#define i_key_str
#define i_val int
#include <stc/cmap.h>
@@ -12,7 +11,7 @@
int main1()
{
c_auto (cvec_str, words)
- c_auto (cmap_strn, word_map)
+ c_auto (cmap_str, word_map)
{
c_apply(cvec_str, emplace_back, &words, {
"this", "sentence", "is", "not", "a", "sentence",
@@ -20,10 +19,10 @@ int main1()
});
c_foreach (w, cvec_str, words) {
- cmap_strn_emplace(&word_map, w.ref->str, 0).ref->second += 1;
+ cmap_str_emplace(&word_map, w.ref->str, 0).ref->second += 1;
}
- c_foreach (i, cmap_strn, word_map) {
+ c_foreach (i, cmap_str, word_map) {
printf("%d occurrences of word '%s'\n",
i.ref->second, i.ref->first.str);
}
diff --git a/include/stc/cbox.h b/include/stc/cbox.h
index 445057dd..89bd272e 100644
--- a/include/stc/cbox.h
+++ b/include/stc/cbox.h
@@ -151,9 +151,9 @@ _cx_memb(_take)(_cx_self* self, _cx_self other) {
STC_INLINE uint64_t
_cx_memb(_hash)(const _cx_self* self, size_t n) {
- #if c_option(c_no_compare) && SIZE_MAX >> 32
+ #if (c_option(c_no_compare) || defined _i_default_compare) && SIZE_MAX >> 32
return c_hash64(&self->get, 8);
- #elif c_option(c_no_compare)
+ #elif (c_option(c_no_compare) || defined _i_default_compare)
return c_hash32(&self->get, 4);
#else
i_valraw raw = i_valto(self->get);
@@ -163,7 +163,7 @@ _cx_memb(_hash)(const _cx_self* self, size_t n) {
STC_INLINE int
_cx_memb(_compare)(const _cx_self* x, const _cx_self* y) {
- #if c_option(c_no_compare)
+ #if (c_option(c_no_compare) || defined _i_default_compare)
return (int)(x->get - y->get);
#else
i_valraw rx = i_valto(x->get);
diff --git a/include/stc/csptr.h b/include/stc/csptr.h
index 06790e01..4753ef12 100644
--- a/include/stc/csptr.h
+++ b/include/stc/csptr.h
@@ -185,9 +185,9 @@ _cx_memb(_take)(_cx_self* self, _cx_self ptr) {
STC_INLINE uint64_t
_cx_memb(_hash)(const _cx_self* self, size_t n) {
- #if c_option(c_no_compare) && SIZE_MAX >> 32
+ #if (c_option(c_no_compare) || defined _i_default_compare) && SIZE_MAX >> 32
return c_hash64(&self->get, 8);
- #elif c_option(c_no_compare)
+ #elif (c_option(c_no_compare) || defined _i_default_compare)
return c_hash32(&self->get, 4);
#else
i_valraw raw = i_valto(self->get);
@@ -197,7 +197,7 @@ _cx_memb(_hash)(const _cx_self* self, size_t n) {
STC_INLINE int
_cx_memb(_compare)(const _cx_self* x, const _cx_self* y) {
- #if c_option(c_no_compare)
+ #if (c_option(c_no_compare) || defined _i_default_compare)
return (int)(x->get - y->get);
#else
i_valraw rx = i_valto(x->get);
diff --git a/include/stc/template.h b/include/stc/template.h
index e788d4eb..85c4a940 100644
--- a/include/stc/template.h
+++ b/include/stc/template.h
@@ -102,13 +102,29 @@
#endif
#endif
+/* Resolve i_del and i_from here */
+#if defined i_del && defined i_isset
+ #define i_keydel i_del
+#elif defined i_del && !defined i_key
+ #define i_valdel i_del
+#elif defined i_del
+ #error "i_del not supported for maps, define i_keydel / i_valdel instead."
+#endif
+#if defined i_from && defined i_isset
+ #define i_keyfrom i_from
+#elif defined i_from && !defined i_key
+ #define i_valfrom i_from
+#elif defined i_from
+ #error "i_from not supported for maps, define i_keyfrom / i_valfrom instead."
+#endif
+
#if defined i_val_ref
#define i_val i_val_ref
#define i_valfrom c_PASTE(i_val, _clone)
#if !defined i_cmp && !defined i_key
#define i_cmp c_PASTE(i_val, _compare)
#endif
- #if !defined i_valdel && !defined i_del
+ #if !defined i_valdel
#define i_valdel c_PASTE(i_val, _del)
#endif
@@ -124,26 +140,11 @@
#if !defined i_cmp && !defined i_key
#define i_cmp c_rawstr_compare
#endif
- #if !defined i_valdel && !defined i_del
+ #if !defined i_valdel
#define i_valdel cstr_del
#endif
#endif
-#if defined i_del && defined i_isset
- #define i_keydel i_del
-#elif defined i_del && !defined i_key
- #define i_valdel i_del
-#elif defined i_del
- #error "i_del not supported for maps, define i_keydel / i_valdel instead."
-#endif
-#if defined i_from && defined i_isset
- #define i_keyfrom i_from
-#elif defined i_from && !defined i_key
- #define i_valfrom i_from
-#elif defined i_from
- #error "i_from not supported for maps, define i_keyfrom / i_valfrom instead."
-#endif
-
#ifdef i_key
#ifdef _i_isset
#define i_val i_key
@@ -190,6 +191,7 @@
#define i_valdel c_default_del
#endif
#ifndef i_cmp
+ #define _i_default_compare
#define i_cmp c_default_compare
#endif
#ifndef i_hash
@@ -225,6 +227,7 @@
#undef i_keyto
#undef _i_prefix
+#undef _i_default_compare
#undef _i_has_internal_clone
#undef _i_template
#endif