summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2023-03-29 08:45:28 +0200
committerTyge Lovset <[email protected]>2023-03-29 08:45:28 +0200
commit97d205f7ba096a9872afbce5e696a8806d7b72d1 (patch)
tree27d1b31ed756a8a74157e48a27bcb506d66e097a
parent4f0ca428e332761666916477b22c3301044a85c6 (diff)
downloadSTC-modified-97d205f7ba096a9872afbce5e696a8806d7b72d1.tar.gz
STC-modified-97d205f7ba096a9872afbce5e696a8806d7b72d1.zip
Removed i_less_functor, i_cmp_functor, i_eq_functor and i_hash_functor: not needed.
Simplified cvec_X_eq() and cdeq_X_eq()
-rw-r--r--docs/cmap_api.md4
-rw-r--r--docs/cpque_api.md1
-rw-r--r--docs/cset_api.md2
-rw-r--r--docs/csmap_api.md1
-rw-r--r--docs/csset_api.md1
-rw-r--r--include/stc/cdeq.h5
-rw-r--r--include/stc/cmap.h14
-rw-r--r--include/stc/cpque.h10
-rw-r--r--include/stc/csmap.h18
-rw-r--r--include/stc/cvec.h5
-rw-r--r--misc/examples/functor.c8
11 files changed, 22 insertions, 47 deletions
diff --git a/docs/cmap_api.md b/docs/cmap_api.md
index 71e00265..8749bd09 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -36,9 +36,7 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain
#define i_valto // convertion func i_val* => i_valraw
#define i_tag // alternative typename: cmap_{i_tag}. i_tag defaults to i_val
-#define i_hash_functor // advanced, see examples/functor.c for similar usage.
-#define i_eq_functor // advanced, see examples/functor.c for similar usage.
-#define i_ssize // internal; default int32_t. If defined, table expand 2x (else 1.5x)
+#define i_ssize // internal; default int32_t. If defined, table expand 2x (else 1.5x)
#include <stc/cmap.h>
```
`X` should be replaced by the value of `i_tag` in all of the following documentation.
diff --git a/docs/cpque_api.md b/docs/cpque_api.md
index 134a920f..1396dc1f 100644
--- a/docs/cpque_api.md
+++ b/docs/cpque_api.md
@@ -18,7 +18,6 @@ See the c++ class [std::priority_queue](https://en.cppreference.com/w/cpp/contai
#define i_valfrom // convertion func i_valraw => i_val
#define i_valto // convertion func i_val* => i_valraw.
-#define i_less_functor // takes self as first argument. See examples/functor.c for usage.
#define i_tag // alternative typename: cpque_{i_tag}. i_tag defaults to i_val
#include <stc/cpque.h>
```
diff --git a/docs/cset_api.md b/docs/cset_api.md
index a0af357f..db9fb802 100644
--- a/docs/cset_api.md
+++ b/docs/cset_api.md
@@ -19,8 +19,6 @@ A **cset** is an associative container that contains a set of unique objects of
#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy
#define i_tag // alternative typename: cmap_{i_tag}. i_tag defaults to i_val
-#define i_hash_functor // advanced, see examples/functor.c for similar usage.
-#define i_eq_functor // advanced, see examples/functor.c for similar usage.
#define i_ssize // default int32_t. If defined, table expand 2x (else 1.5x)
#include <stc/cset.h>
```
diff --git a/docs/csmap_api.md b/docs/csmap_api.md
index da0e6915..0eff8c26 100644
--- a/docs/csmap_api.md
+++ b/docs/csmap_api.md
@@ -33,7 +33,6 @@ See the c++ class [std::map](https://en.cppreference.com/w/cpp/container/map) fo
#define i_valto // convertion func i_val* => i_valraw
#define i_tag // alternative typename: csmap_{i_tag}. i_tag defaults to i_val
-#define i_cmp_functor // advanced, see examples/functor.c for similar usage.
#define i_ssize // internal size rep. defaults to int32_t
#include <stc/csmap.h>
```
diff --git a/docs/csset_api.md b/docs/csset_api.md
index 0989fd9b..1869327c 100644
--- a/docs/csset_api.md
+++ b/docs/csset_api.md
@@ -18,7 +18,6 @@ See the c++ class [std::set](https://en.cppreference.com/w/cpp/container/set) fo
#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy
#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy
-#define i_cmp_functor // advanced, see examples/functor.c for similar usage.
#define i_tag // alternative typename: csset_{i_tag}. i_tag defaults to i_val
#define i_ssize // defaults to int32_t
#include <stc/csset.h>
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index c4f84a1b..09c0a7f8 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -198,9 +198,8 @@ _cx_memb(_get_mut)(_cx_self* self, _cx_raw raw)
STC_INLINE bool
_cx_memb(_eq)(const _cx_self* x, const _cx_self* y) {
if (x->_len != y->_len) return false;
- _cx_iter i = _cx_memb(_begin)(x), j = _cx_memb(_begin)(y);
- for (; i.ref; _cx_memb(_next)(&i), _cx_memb(_next)(&j)) {
- const _cx_raw _rx = i_keyto(i.ref), _ry = i_keyto(j.ref);
+ for (intptr_t i = 0; i < x->_len; ++i) {
+ const _cx_raw _rx = i_keyto(x->data+i), _ry = i_keyto(y->data+i);
if (!(i_eq((&_rx), (&_ry)))) return false;
}
return true;
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index 402038cb..c840523f 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -82,12 +82,6 @@ typedef struct { int64_t idx; uint8_t hx; } chash_bucket_t;
#define _i_size i_ssize
#endif
#include "priv/template.h"
-#ifndef i_hash_functor
- #define i_hash_functor(self, x) i_hash(x)
-#endif
-#ifndef i_eq_functor
- #define i_eq_functor(self, x, y) i_eq(x, y)
-#endif
#if !c_option(c_is_forward)
_cx_deftypes(_c_chash_types, _cx_self, i_key, i_val, i_ssize, _i_MAP_ONLY, _i_SET_ONLY);
#endif
@@ -371,14 +365,14 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) {
STC_DEF chash_bucket_t
_cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr) {
- const uint64_t _hash = i_hash_functor(self, rkeyptr);
+ const uint64_t _hash = i_hash(rkeyptr);
int64_t _cap = self->bucket_count;
chash_bucket_t b = {c_PASTE(fastrange_,_i_expandby)(_hash, (uint64_t)_cap), (uint8_t)(_hash | 0x80)};
const uint8_t* _hx = self->_hashx;
while (_hx[b.idx]) {
if (_hx[b.idx] == b.hx) {
const _cx_rawkey _raw = i_keyto(_i_keyref(self->table + b.idx));
- if (i_eq_functor(self, (&_raw), rkeyptr))
+ if (i_eq((&_raw), rkeyptr))
break;
}
if (++b.idx == _cap)
@@ -469,7 +463,7 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) {
if (! _hashx[j])
break;
const _cx_rawkey _raw = i_keyto(_i_keyref(_slot + j));
- k = (i_ssize)c_PASTE(fastrange_,_i_expandby)(i_hash_functor(self, (&_raw)), (uint64_t)_cap);
+ k = (i_ssize)c_PASTE(fastrange_,_i_expandby)(i_hash((&_raw)), (uint64_t)_cap);
if ((j < i) ^ (k <= i) ^ (k > j)) /* is k outside (i, j]? */
_slot[i] = _slot[j], _hashx[i] = _hashx[j], i = j;
}
@@ -479,8 +473,6 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) {
#endif // i_implement
#undef i_max_load_factor
-#undef i_hash_functor
-#undef i_eq_functor
#undef _i_size
#undef _i_isset
#undef _i_ismap
diff --git a/include/stc/cpque.h b/include/stc/cpque.h
index 4955f2e0..00eaa49e 100644
--- a/include/stc/cpque.h
+++ b/include/stc/cpque.h
@@ -32,9 +32,6 @@
#endif
#include "priv/template.h"
-#ifndef i_less_functor
- #define i_less_functor(self, x, y) i_less(x, y)
-#endif
#if !c_option(c_is_forward)
_cx_deftypes(_c_cpque_types, _cx_self, i_key);
#endif
@@ -120,8 +117,8 @@ STC_DEF void
_cx_memb(_sift_down_)(_cx_self* self, const intptr_t idx, const intptr_t n) {
_cx_value t, *arr = self->data - 1;
for (intptr_t r = idx, c = idx*2; c <= n; c *= 2) {
- c += i_less_functor(self, (&arr[c]), (&arr[c + (c < n)]));
- if (!(i_less_functor(self, (&arr[r]), (&arr[c])))) return;
+ c += i_less((&arr[c]), (&arr[c + (c < n)]));
+ if (!(i_less((&arr[r]), (&arr[c])))) return;
t = arr[r], arr[r] = arr[c], arr[r = c] = t;
}
}
@@ -156,12 +153,11 @@ _cx_memb(_push)(_cx_self* self, _cx_value value) {
_cx_memb(_reserve)(self, self->_len*3/2 + 4);
_cx_value *arr = self->data - 1; /* base 1 */
intptr_t c = ++self->_len;
- for (; c > 1 && (i_less_functor(self, (&arr[c/2]), (&value))); c /= 2)
+ for (; c > 1 && (i_less((&arr[c/2]), (&value))); c /= 2)
arr[c] = arr[c/2];
arr[c] = value;
}
#endif
#define CPQUE_H_INCLUDED
-#undef i_less_functor
#include "priv/template.h"
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index 2b1910e9..50593ba3 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -80,9 +80,6 @@ int main(void) {
#define _i_size i_ssize
#endif
#include "priv/template.h"
-#ifndef i_cmp_functor
- #define i_cmp_functor(self, x, y) i_cmp(x, y)
-#endif
#if !c_option(c_is_forward)
_cx_deftypes(_c_aatree_types, _cx_self, i_key, i_val, i_ssize, _i_MAP_ONLY, _i_SET_ONLY);
#endif
@@ -222,12 +219,12 @@ _cx_memb(_advance)(_cx_iter it, size_t n) {
}
STC_INLINE bool
-_cx_memb(_eq)(const _cx_self* m1, const _cx_self* m2) {
- if (_cx_memb(_size)(m1) != _cx_memb(_size)(m2)) return false;
- _cx_iter i = _cx_memb(_begin)(m1), j = _cx_memb(_begin)(m2);
+_cx_memb(_eq)(const _cx_self* self, const _cx_self* other) {
+ if (_cx_memb(_size)(self) != _cx_memb(_size)(other)) return false;
+ _cx_iter i = _cx_memb(_begin)(self), j = _cx_memb(_begin)(other);
for (; i.ref; _cx_memb(_next)(&i), _cx_memb(_next)(&j)) {
const _cx_rawkey _rx = i_keyto(_i_keyref(i.ref)), _ry = i_keyto(_i_keyref(j.ref));
- if ((i_cmp_functor(m1, (&_rx), (&_ry))) != 0) return false;
+ if (!(i_eq((&_rx), (&_ry)))) return false;
}
return true;
}
@@ -357,7 +354,7 @@ _cx_memb(_find_it)(const _cx_self* self, _cx_rawkey rkey, _cx_iter* out) {
out->_top = 0;
while (tn) {
int c; const _cx_rawkey _raw = i_keyto(_i_keyref(&d[tn].value));
- if ((c = i_cmp_functor(self, (&_raw), (&rkey))) < 0)
+ if ((c = i_cmp((&_raw), (&rkey))) < 0)
tn = d[tn].link[1];
else if (c > 0)
{ out->_st[out->_top++] = tn; tn = d[tn].link[0]; }
@@ -425,7 +422,7 @@ _cx_memb(_insert_entry_i_)(_cx_self* self, i_ssize tn, const _cx_rawkey* rkey, _
while (tx) {
up[top++] = tx;
const _cx_rawkey _raw = i_keyto(_i_keyref(&d[tx].value));
- if (!(c = i_cmp_functor(self, (&_raw), rkey)))
+ if (!(c = i_cmp((&_raw), rkey)))
{ _res->ref = &d[tx].value; return tn; }
dir = (c < 0);
tx = d[tx].link[dir];
@@ -464,7 +461,7 @@ _cx_memb(_erase_r_)(_cx_self *self, i_ssize tn, const _cx_rawkey* rkey, int *era
if (tn == 0)
return 0;
_cx_rawkey raw = i_keyto(_i_keyref(&d[tn].value));
- i_ssize tx; int c = i_cmp_functor(self, (&raw), rkey);
+ i_ssize tx; int c = i_cmp((&raw), rkey);
if (c != 0)
d[tn].link[c < 0] = _cx_memb(_erase_r_)(self, d[tn].link[c < 0], rkey, erased);
else {
@@ -594,7 +591,6 @@ _cx_memb(_drop)(_cx_self* self) {
}
#endif // i_implement
-#undef i_cmp_functor
#undef _i_size
#undef _i_isset
#undef _i_ismap
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index 91cdb25c..8f35e5fc 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -236,9 +236,8 @@ _cx_memb(_get_mut)(const _cx_self* self, _cx_raw raw)
STC_INLINE bool
_cx_memb(_eq)(const _cx_self* x, const _cx_self* y) {
if (x->_len != y->_len) return false;
- _cx_iter i = _cx_memb(_begin)(x), j = _cx_memb(_begin)(y);
- for (; i.ref; _cx_memb(_next)(&i), _cx_memb(_next)(&j)) {
- const _cx_raw _rx = i_keyto(i.ref), _ry = i_keyto(j.ref);
+ for (intptr_t i = 0; i < x->_len; ++i) {
+ const _cx_raw _rx = i_keyto(x->data+i), _ry = i_keyto(y->data+i);
if (!(i_eq((&_rx), (&_ry)))) return false;
}
return true;
diff --git a/misc/examples/functor.c b/misc/examples/functor.c
index f37e41d5..f8074c3a 100644
--- a/misc/examples/functor.c
+++ b/misc/examples/functor.c
@@ -1,9 +1,9 @@
// Implements c++ example: https://en.cppreference.com/w/cpp/container/priority_queue
// Example of per-instance less-function on a single priority queue type
//
-// Note: i_less_functor: available for cpque types only
-// i_cmp_functor: available for csmap and csset types only
-// i_hash_functor/i_eq_functor: available for cmap and cset types only
+// Note: i_less: has self for cpque types only
+// i_cmp: has self for csmap and csset types only
+// i_hash/i_eq: has self for cmap and cset types only
#include <stdio.h>
#include <stdbool.h>
@@ -23,7 +23,7 @@ struct {
#define i_type ipque
#define i_val int
#define i_opt c_is_forward // needed to avoid re-type-define container type
-#define i_less_functor(self, x, y) c_container_of(self, IPQueue, Q)->less(x, y)
+#define i_less(x, y) c_container_of(self, IPQueue, Q)->less(x, y)
#include <stc/cpque.h>
void print_queue(const char* name, IPQueue q) {