diff options
| author | Tyge Løvset <[email protected]> | 2021-12-19 21:39:25 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-12-19 21:39:25 +0100 |
| commit | 0c3d711c2541aebe9a44fecb732e096bed14f72c (patch) | |
| tree | 0d9c6dbe393d24fccd12107a3ec08f468656ce73 | |
| parent | 09b696eab6ae640f6c1e07178d49fcd8646e1737 (diff) | |
| download | STC-modified-0c3d711c2541aebe9a44fecb732e096bed14f72c.tar.gz STC-modified-0c3d711c2541aebe9a44fecb732e096bed14f72c.zip | |
Global rename of `_equ\b` => `_eq`. In practice `i_equ` must renamed to `i_eq` in user code if used.
| -rw-r--r-- | README.md | 33 | ||||
| -rw-r--r-- | docs/ccommon_api.md | 2 | ||||
| -rw-r--r-- | docs/cmap_api.md | 22 | ||||
| -rw-r--r-- | docs/cset_api.md | 8 | ||||
| -rw-r--r-- | docs/cstr_api.md | 4 | ||||
| -rw-r--r-- | docs/csview_api.md | 2 | ||||
| -rw-r--r-- | examples/rawptr_elements.c | 2 | ||||
| -rw-r--r-- | examples/vikings.c | 4 | ||||
| -rw-r--r-- | include/stc/alt/cstr.h | 2 | ||||
| -rw-r--r-- | include/stc/cbox.h | 2 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 12 | ||||
| -rw-r--r-- | include/stc/cmap.h | 2 | ||||
| -rw-r--r-- | include/stc/csptr.h | 2 | ||||
| -rw-r--r-- | include/stc/cstr.h | 6 | ||||
| -rw-r--r-- | include/stc/csview.h | 2 | ||||
| -rw-r--r-- | include/stc/template.h | 24 |
16 files changed, 69 insertions, 60 deletions
@@ -6,21 +6,29 @@ STC - Smart Template Containers for C News ---- ### Version 3.0 released -There are new general `i_key_bind` / `i_val_bind` template parameters which auto-binds a set of functions to the type specified, and can be used in place of `i_key` / `i_val`. Use the `_bind` variant for elements of Type which have following functions defined: *Type_cmp*, *Type_clone*, *Type_drop*, *Type_equalto*, and *Type_hash*. Only the functions required by the particular container needs to be defined, e.g. only **cmap** and **cset** require *Type_equalto* and *Type_hash* to be defined. You can override these by defining `i_cmp`, `i_drop`, etc. -It's still possible define template parameters with `i_val` / `i_key` as before, which is easier for simple element types, as defaults will be assigned to missing functions. +There are new general `i_key_bind` / `i_val_bind` template parameters which auto-binds a set of functions +to the type specified, and can be used in place of `i_key` / `i_val`. Use the `_bind` variant for elements +of Type which have following functions defined: *Type_cmp*, *Type_clone*, *Type_drop*, *Type_hash*, +and *Type_eq*. Only the functions required by the particular container needs to be defined. e.g. **cmap** +and **cset** are the only types that requires *Type_hash* and *Type_eq* to be defined. +You may override these by defining `i_cmp`, `i_drop`, etc. Template parameters with `i_val` / `i_key` may +still be defined as before, which is often easier for simple element types. Migration guide from version 2 to 3. Replace (regular expresion) in VS Code: - `_del\b` → `_drop` - `_compare\b` → `_cmp` +- `_equ\b` → `_eq` Replace (whole word + match case): - `i_keydel` → `i_keydrop` - `i_valdel` → `i_valdrop` - `i_cnt` → `i_type` -- `i_key_csptr` → `i_key_bind` -- `i_val_csptr` → `i_val_bind` - `cstr_lit` → `cstr_new` - `csptr_X_make` → `csptr_X_new` +- `i_key_csptr` → `i_key_bind` +- `i_val_csptr` → `i_val_bind` +- `i_key_ref` → `i_key_bind` +- `i_val_ref` → `i_val_bind` ### Final version 2.1 - Strings: Renamed constructor *cstr_lit()* to `cstr_new(lit)`. Renamed *cstr_assign_fmt()* to `cstr_printf()`. @@ -286,18 +294,19 @@ The list of template parameters: - `i_key` - Maps key type. **[required]** for cmap/csmap. - `i_val` - The container **[required]** element type. For cmap/csmap, it is the mapped value. -- `i_cmp` - Three-way comparison of two `i_keyraw`, **[required]** for non-integral `i_keyraw`. -- `i_equ` - Equality comparison of two `i_keyraw`- defaults to `!i_cmp`. +- `i_cmp` - Three-way comparison of two `i_keyraw *` - **[required]** for non-integral `i_keyraw`. +- `i_hash` - Hash function taking `i_keyraw *` and a size - defaults to `!i_cmp`. +- `i_eq` - Equality comparison of two `i_keyraw *` - defaults to `!i_cmp`. Companion with `i_hash`. -- `i_keydrop` - Destroy map key func - defaults to empty destructor. +- `i_keydrop` - Destroy map key func - defaults to empty destructor. - `i_keyraw` - Convertion "raw" type - defaults to `i_key` type. -- `i_keyfrom` - Convertion func `i_keyraw` => `i_key` - defaults to simple copy. **[required]** if `i_keydrop` is defined. -- `i_keyto` - Convertion func `i_key` => `i_keyraw` - defaults to simple copy. +- `i_keyfrom` - Convertion func `i_key` <= `i_keyraw` - defaults to simple copy. **[required]** if `i_keydrop` is defined. +- `i_keyto` - Convertion func `i_key *` => `i_keyraw` - defaults to simple copy. -- `i_valdrop` - Destroy mapped or value func - defaults to empty destruct. +- `i_valdrop` - Destroy mapped or value func - defaults to empty destruct. - `i_valraw` - Convertion "raw" type - defaults to `i_val` type. -- `i_valfrom` - Convertion func `i_valraw` => `i_val` - defaults to simple copy. -- `i_valto` - Convertion func `i_val` => `i_valraw` - defaults to simple copy. +- `i_valfrom` - Convertion func `i_val` <= `i_valraw` - defaults to simple copy. **[required]** if `i_valdrop` is defined. +- `i_valto` - Convertion func `i_val *` => `i_valraw` - defaults to simple copy. Instead of defining `i_cmp`, you may define `i_opt c_no_cmp` to disable methods using comparison. diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index cc021001..9318b8e3 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -170,7 +170,7 @@ Type c_default_toraw(const Type* val); // dereference val void c_default_drop(Type* val); // does nothing int c_rawstr_cmp(const char* const* a, const char* const* b); -bool c_rawstr_equalto(const char* const* a, const char* const* b); +bool c_rawstr_eq(const char* const* a, const char* const* b); ``` ### c_malloc, c_calloc, c_realloc, c_free diff --git a/docs/cmap_api.md b/docs/cmap_api.md index e2cb668f..4bb367f8 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -20,14 +20,14 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain #define i_key // key: REQUIRED #define i_val // value: REQUIRED #define i_cmp // three-way compare two i_keyraw*: REQUIRED IF i_keyraw is non-integral type -#define i_equ // equality comparison two i_keyraw*: ALTERNATIVE to i_cmp +#define i_eq // equality comparison two i_keyraw*: ALTERNATIVE to i_cmp #define i_keydrop // destroy key func - defaults to empty destruct #define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy +#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_valdrop // destroy value func - defaults to empty destruct #define i_valraw // convertion "raw" type - defaults to i_val -#define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy +#define i_valfrom // convertion func i_valraw => i_val - defaults to plain copy #define i_valto // convertion func i_val* => i_valraw - defaults to plain copy #define i_tag // defaults to i_key #include <stc/cmap.h> @@ -89,9 +89,9 @@ uint64_t c_hash64(const void* data, size_t is8); uint64_t c_rawstr_hash(const char* const* strp, size_t unused); // equalto template parameter functions: -bool c_default_equalto(const i_keyraw* a, const i_keyraw* b); // *a == *b -bool c_memcmp_equalto(const i_keyraw* a, const i_keyraw* b); // !memcmp(a, b, sizeof *a) -bool c_rawstr_equalto(const char* const* a, const char* const* b); // !strcmp(*a, *b) +bool c_default_eq(const i_keyraw* a, const i_keyraw* b); // *a == *b +bool c_memcmp_eq(const i_keyraw* a, const i_keyraw* b); // !memcmp(a, b, sizeof *a) +bool c_rawstr_eq(const char* const* a, const char* const* b); // !strcmp(*a, *b) ``` ## Types @@ -200,7 +200,7 @@ typedef struct { int x, y, z; } Vec3i; #define i_key Vec3i #define i_val int -#define i_equ c_memcmp_equalto // bitwise compare, and use c_default_hash +#define i_eq c_memcmp_eq // bitwise equal, and use c_default_hash #define i_tag vi #include <stc/cmap.h> @@ -272,7 +272,7 @@ typedef struct { #define Viking_init() ((Viking){cstr_null, cstr_null}) -static inline bool RViking_equalto(const Viking* a, const Viking* b) { +static inline bool RViking_eq(const Viking* a, const Viking* b) { return cstr_equals_s(a->name, b->name) && cstr_equals_s(a->country, b->country); } @@ -294,7 +294,7 @@ void inline Viking_drop(Viking* vk) { #define i_key_bind Viking #define i_val int // i_key_bind auto-binds: -// #define i_equ RViking_equalto +// #define i_eq RViking_eq // #define i_hash RViking_hash // #define i_keyfrom Viking_clone // #define i_drop Viking_drop @@ -357,7 +357,7 @@ static inline uint64_t RViking_hash(const RViking* raw, size_t ignore) { uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15); return hash; } -static inline bool RViking_equalto(const RViking* rx, const RViking* ry) { +static inline bool RViking_eq(const RViking* rx, const RViking* ry) { return strcmp(rx->name, ry->name) == 0 && strcmp(rx->country, ry->country) == 0; } @@ -375,7 +375,7 @@ static inline RViking Viking_toraw(const Viking* vk) { #define i_keyraw RViking // i_key_bind macro will make these functions auto-bind: // #define i_hash RViking_hash -// #define i_equ RViking_equalto +// #define i_eq RViking_eq // #define i_keyfrom Viking_from // uses _from because i_keyraw is defined // #define i_keyto Viking_toraw // #define i_keydrop Viking_drop diff --git a/docs/cset_api.md b/docs/cset_api.md index b70f5fb9..2ce43309 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -8,13 +8,13 @@ A **cset** is an associative container that contains a set of unique objects of ```c
#define i_key // key: REQUIRED
-#define i_hash // hash func: REQUIRED IF i_keyraw is a non-pod type
#define i_cmp // three-way compare two i_keyraw*: REQUIRED IF i_keyraw is a non-integral type
-#define i_equ // equality comparison two i_keyraw*: ALTERNATIVE to i_cmp
+#define i_hash // hash func: REQUIRED IF i_keyraw is a non-pod type
+#define i_eq // equality comparison two i_keyraw*: !i_cmp will be used if not defined.
#define i_drop // destroy key func - defaults to empty destruct
#define i_keyraw // convertion "raw" type - defaults to i_key
-#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_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_tag // defaults to i_key
#include <stc/cset.h>
```
diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 463a723c..31f27baf 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -86,12 +86,12 @@ Note that all methods with arguments `(..., const char* str, size_t n)`, `n` mus #### Helper methods: ```c int cstr_cmp(const cstr *s1, const cstr *s2); -bool cstr_equalto(const cstr *s1, const cstr *s2); +bool cstr_eq(const cstr *s1, const cstr *s2); bool cstr_hash(const cstr *s, ...); typedef const char* c_rawstr; int c_rawstr_cmp(const c_rawstr* x, const c_rawstr* y); -bool c_rawstr_equalto(const c_rawstr* x, const c_rawstr* y); +bool c_rawstr_eq(const c_rawstr* x, const c_rawstr* y); uint64_t c_rawstr_hash(const c_rawstr* x, size_t dummy); uint64_t c_strhash(const char* str); diff --git a/docs/csview_api.md b/docs/csview_api.md index 609c5d10..4e5d3b0e 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -78,7 +78,7 @@ bool cstr_ends_with_v(cstr s, csview sub); #### Helper methods ```c int csview_cmp(const csview* x, const csview* y); -bool csview_equalto(const csview* x, const csview* y); +bool csview_eq(const csview* x, const csview* y); uint64_t csview_hash(const csview* x, size_t dummy); ``` ## Types diff --git a/examples/rawptr_elements.c b/examples/rawptr_elements.c index 463a7f35..3ff0dc8a 100644 --- a/examples/rawptr_elements.c +++ b/examples/rawptr_elements.c @@ -9,7 +9,7 @@ struct { double x, y; } typedef Point; #define i_keydrop(x) c_free(*(x))
#define i_keyfrom(x) c_new(Point, *(x))
#define i_hash(x, n) c_default_hash(*(x), sizeof *(x))
-#define i_equ(x, y) c_memcmp_equalto(*(x), *(y))
+#define i_eq(x, y) c_memcmp_eq(*(x), *(y))
#define i_tag pnt
#include <stc/cset.h>
diff --git a/examples/vikings.c b/examples/vikings.c index 838598b5..c4e86c73 100644 --- a/examples/vikings.c +++ b/examples/vikings.c @@ -22,7 +22,7 @@ uint64_t RViking_hash(const RViking* raw, size_t ignore) { uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15); return hash; } -static inline bool RViking_equalto(const RViking* rx, const RViking* ry) { +static inline bool RViking_eq(const RViking* rx, const RViking* ry) { return strcmp(rx->name, ry->name) == 0 && strcmp(rx->country, ry->country) == 0; } @@ -40,7 +40,7 @@ static inline RViking Viking_toraw(const Viking* vk) { #define i_val int // i_key_bind auto-binds these functions: // #define i_hash Viking_hash -// #define i_equ Viking_equalto +// #define i_eq Viking_eq // #define i_keyfrom Viking_from // uses _from (not _clone) because i_keyraw is defined // #define i_keyto Viking_toraw // #define i_keydrop Viking_drop diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h index bd135b7e..1a9280e8 100644 --- a/include/stc/alt/cstr.h +++ b/include/stc/alt/cstr.h @@ -167,7 +167,7 @@ STC_INLINE bool cstr_equals_s(cstr s1, cstr s2) { return strcmp(cstr_str(&s1), cstr_str(&s2)) == 0;
}
-STC_INLINE bool cstr_equalto(const cstr* s1, const cstr* s2) {
+STC_INLINE bool cstr_eq(const cstr* s1, const cstr* s2) {
return strcmp(cstr_str(s1), cstr_str(s2)) == 0;
}
diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 51bc81a4..764761fe 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -171,7 +171,7 @@ _cx_memb(_cmp)(const _cx_self* x, const _cx_self* y) { }
STC_INLINE bool
-_cx_memb(_equalto)(const _cx_self* x, const _cx_self* y) {
+_cx_memb(_eq)(const _cx_self* x, const _cx_self* y) {
return !_cx_memb(_cmp)(x, y);
}
#include "template.h"
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index d3664077..9cbe24a4 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -100,8 +100,8 @@ typedef const char c_strlit[]; #define c_default_less(x, y) (*(x) < *(y))
#define c_default_cmp(x, y) c_less_cmp(c_default_less, x, y)
-#define c_default_equalto(x, y) (*(x) == *(y))
-#define c_memcmp_equalto(x, y) (memcmp(x, y, sizeof *(x)) == 0)
+#define c_default_eq(x, y) (*(x) == *(y))
+#define c_memcmp_eq(x, y) (memcmp(x, y, sizeof *(x)) == 0)
#define c_default_clone(x) (x)
#define c_default_fromraw(x) (x) // [deprecated]
@@ -118,11 +118,11 @@ typedef const char c_strlit[]; typedef char* c_mutstr;
typedef const char* c_rawstr;
-#define c_rawstr_cmp(x, y) strcmp(*(x), *(y))
-#define c_rawstr_equalto(x, y) (strcmp(*(x), *(y)) == 0)
-#define c_rawstr_hash(x, dummy) c_strhash(*(x))
+#define c_rawstr_cmp(xp, yp) strcmp(*(xp), *(yp))
+#define c_rawstr_eq(xp, yp) (!strcmp(*(xp), *(yp)))
+#define c_rawstr_hash(p, dummy) c_strhash(*(p))
#define c_rawstr_clone(s) strcpy((char*)c_malloc(strlen(s) + 1), s)
-#define c_rawstr_drop(x) c_free((char *) &**(x))
+#define c_rawstr_drop(p) c_free((char *) &**(p))
#define _c_ROTL(x, k) (x << (k) | x >> (8*sizeof(x) - (k)))
diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 10e9c586..c78c2472 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -293,7 +293,7 @@ _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr) { while ((_hx = _hashx[b.idx])) {
if (_hx == b.hx) {
_cx_rawkey _raw = i_keyto(_i_keyref(self->table + b.idx));
- if (i_equ(&_raw, rkeyptr)) break;
+ if (i_eq(&_raw, rkeyptr)) break;
}
if (++b.idx == _cap) b.idx = 0;
}
diff --git a/include/stc/csptr.h b/include/stc/csptr.h index b66cd4d7..cf1a5b12 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -204,7 +204,7 @@ _cx_memb(_cmp)(const _cx_self* x, const _cx_self* y) { }
STC_INLINE bool
-_cx_memb(_equalto)(const _cx_self* x, const _cx_self* y) {
+_cx_memb(_eq)(const _cx_self* x, const _cx_self* y) {
return !_cx_memb(_cmp)(x, y);
}
#undef _i_atomic_inc
diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 730f4d16..cfb2dfef 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -167,9 +167,9 @@ cstr_ends_with(cstr s, const char* sub) { }
/* container adaptor functions: */
-#define cstr_cmp(xp, yp) strcmp((xp)->str, (yp)->str)
-#define cstr_equalto(xp, yp) (strcmp((xp)->str, (yp)->str) == 0)
-#define cstr_hash(xp, dummy) c_strhash((xp)->str)
+#define cstr_cmp(xp, yp) strcmp((xp)->str, (yp)->str)
+#define cstr_eq(xp, yp) (!cstr_cmp(xp, yp))
+#define cstr_hash(xp, dummy) c_strhash((xp)->str)
/* -------------------------- IMPLEMENTATION ------------------------- */
diff --git a/include/stc/csview.h b/include/stc/csview.h index ac120e15..d0152c3d 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -120,7 +120,7 @@ STC_INLINE int csview_cmp(const csview* x, const csview* y) { return c ? c : x->size - y->size;
}
#define csview_hash(xp, dummy) c_strhash((xp)->str)
-#define csview_equalto(xp, yp) (csview_cmp(xp, yp) == 0)
+#define csview_eq(xp, yp) (!csview_cmp(xp, yp))
/* -------------------------- IMPLEMENTATION ------------------------- */
diff --git a/include/stc/template.h b/include/stc/template.h index 61befb2b..83a362d7 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -48,9 +48,9 @@ #define i_val_bind i_val_csptr
#error "i_val_sptr/ref no longer supported: use new name i_val_bind"
#endif
-#ifdef i_cnt // [deprecated]
+#if defined i_cnt || defined i_equ // [deprecated]
#define i_type i_cnt
- #error "i_cnt no longer supported: use new name i_type"
+ #error "i_cnt and i_equ no longer supported: use new name i_type / i_eq"
#endif
#ifdef i_type
@@ -88,8 +88,8 @@ #ifndef i_cmp
#define i_cmp c_PASTE(i_keyraw, _cmp)
#endif
- #ifndef i_equ
- #define i_equ c_PASTE(i_keyraw, _equalto)
+ #ifndef i_eq
+ #define i_eq c_PASTE(i_keyraw, _eq)
#endif
#ifndef i_hash
#define i_hash c_PASTE(i_keyraw, _hash)
@@ -170,10 +170,10 @@ #define i_keyraw i_key
#define i_keyto c_default_toraw
#endif
- #if !defined i_equ && defined i_cmp
- #define i_equ !i_cmp
- #elif !defined i_equ
- #define i_equ c_default_equalto
+ #if !defined i_eq && defined i_cmp
+ #define i_eq !i_cmp
+ #elif !defined i_eq
+ #define i_eq c_default_eq
#endif
#ifndef i_keydrop
#define i_keydrop c_default_drop
@@ -212,26 +212,26 @@ #undef i_imp
#undef i_opt
#undef i_cmp
-#undef i_drop
-#undef i_equ
+#undef i_eq
#undef i_hash
#undef i_from
+#undef i_drop
#undef i_val
#undef i_val_str
#undef i_val_bind
-#undef i_valdrop
#undef i_valraw
#undef i_valfrom
#undef i_valto
+#undef i_valdrop
#undef i_key
#undef i_key_str
#undef i_key_bind
-#undef i_keydrop
#undef i_keyraw
#undef i_keyfrom
#undef i_keyto
+#undef i_keydrop
#undef _i_prefix
#undef _i_has_internal_clone
|
