diff options
| author | Tyge Løvset <[email protected]> | 2022-04-27 13:23:10 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-04-27 13:23:10 +0200 |
| commit | 8178a02c4048cef03952a075f87da91b6ec2ee96 (patch) | |
| tree | bc5b9a2fc2c64ae5a1b3eb665e828e4246707247 /docs | |
| parent | e6738b3da5cbd7703cf3fbd905b3fdac19d0ae24 (diff) | |
| download | STC-modified-8178a02c4048cef03952a075f87da91b6ec2ee96.tar.gz STC-modified-8178a02c4048cef03952a075f87da91b6ec2ee96.zip | |
Finally FIXED cloning/to/from scheme to work properly. When using i_key_bind/i_val_bind, a .._clone() function *must* always be defined.
Functions .._from and .._toraw is only required when i_keyraw/i_valraw type is defined.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/cmap_api.md | 47 | ||||
| -rw-r--r-- | docs/cstack_api.md | 4 | ||||
| -rw-r--r-- | docs/cvec_api.md | 3 |
3 files changed, 32 insertions, 22 deletions
diff --git a/docs/cmap_api.md b/docs/cmap_api.md index a730bee0..51607be2 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -278,12 +278,13 @@ static inline int Viking_cmp(const Viking* a, const Viking* b) { } static inline uint32_t Viking_hash(const Viking* a) { - return c_strhash(cstr_str(&a->name)) ^ (c_strhash(cstr_str(&a->country)) >> 15); + return cstr_hash(&a->name) ^ cstr_hash(&a->country); } static inline Viking Viking_clone(Viking v) { v.name = cstr_clone(v.name); v.country = cstr_clone(v.country); + return v; } static inline void Viking_drop(Viking* vk) { @@ -294,11 +295,13 @@ static inline void Viking_drop(Viking* vk) { #define i_type Vikings #define i_key_bind Viking #define i_val int -// i_key_bind auto-binds: -// #define i_cmp Viking_cmp -// #define i_hash Viking_hash -// #define i_keyfrom Viking_clone -// #define i_keydrop Viking_drop +/* + i_key_bind auto-binds: + #define i_cmp Viking_cmp + #define i_hash Viking_hash + #define i_keyfrom Viking_clone + #define i_keydrop Viking_drop +*/ #include <stc/cmap.h> int main() @@ -354,34 +357,38 @@ typedef struct RViking { const char* country; } RViking; -static inline uint64_t RViking_hash(const RViking* raw) { - uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15); - return hash; -} - static inline int RViking_cmp(const RViking* rx, const RViking* ry) { int c = strcmp(rx->name, ry->name); return c ? c : strcmp(rx->country, ry->country); } +static inline Viking Viking_clone(RViking v) { + v.name = cstr_clone(v.name), v.country = cstr_clone(v.country); + return vk; +} + static inline Viking Viking_from(RViking raw) { return (Viking){cstr_from(raw.name), cstr_from(raw.country)}; } -static inline RViking Viking_toraw(const Viking* vk) { - return (RViking){cstr_str(&vk->name), cstr_str(&vk->country)}; +static inline RViking Viking_toraw(const Viking* vp) { + return (RViking){cstr_str(&vp->name), cstr_str(&vp->country)}; } // With this in place, we define the Viking => int hash map type: #define i_type Vikings +#define i_keyraw RViking +#define i_hash(rp) (c_strhash(rp->name) ^ c_strhash(rp->country)) #define i_key_bind Viking #define i_val int -#define i_keyraw RViking -// i_key_bind macro will make these functions auto-bind: -// #define i_hash RViking_hash -// #define i_cmp RViking_cmp -// #define i_keyfrom Viking_from // uses _from because i_keyraw is defined -// #define i_keyto Viking_toraw -// #define i_keydrop Viking_drop +/* + i_key_bind macro auto-binds these functions: + #define i_hash RViking_hash + #define i_cmp RViking_cmp + #define i_keyclone Viking_clone + #define i_keyfrom Viking_from // because i_keyraw type is defined + #define i_keyto Viking_toraw // because i_keyraw type is defined + #define i_keydrop Viking_drop +*/ #include <stc/cmap.h> int main() diff --git a/docs/cstack_api.md b/docs/cstack_api.md index 8fc5fe8b..f5962344 100644 --- a/docs/cstack_api.md +++ b/docs/cstack_api.md @@ -30,6 +30,7 @@ cstack_X cstack_X_clone(cstack_X st); void cstack_X_clear(cstack_X* self); bool cstack_X_reserve(cstack_X* self, size_t n); void cstack_X_shrink_to_fit(cstack_X* self); +i_val* cstack_X_expand_uninitialized(cstack_X* self, size_t n); void cstack_X_copy(cstack_X* self, cstack_X other); void cstack_X_drop(cstack_X* self); // destructor @@ -39,6 +40,7 @@ bool cstack_X_empty(cstack_X st); i_val* cstack_X_top(const cstack_X* self); const i_val* cstack_X_at(const cstack_X* self, size_t idx); +i_val* cstack_X_at_mut(cstack_X* self, size_t idx); i_val* cstack_X_push(cstack_X* self, i_val value); i_val* cstack_X_emplace(cstack_X* self, i_valraw raw); @@ -90,4 +92,4 @@ int main() { Output: ``` top: 81 -```
\ No newline at end of file +``` diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 1b06275e..d5d87b7b 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -37,7 +37,7 @@ void cvec_X_clear(cvec_X* self); void cvec_X_copy(cvec_X* self, cvec_X other); bool cvec_X_reserve(cvec_X* self, size_t cap); bool cvec_X_resize(cvec_X* self, size_t size, i_val null); -cvec_X_value* cvec_X_expand_uninitialized(cvec_X* self, size_t n); // return uninited data ptr +cvec_X_value* cvec_X_expand_uninitialized(cvec_X* self, size_t n); // return start of uninit void cvec_X_shrink_to_fit(cvec_X* self); void cvec_X_swap(cvec_X* a, cvec_X* b); void cvec_X_drop(cvec_X* self); // destructor @@ -48,6 +48,7 @@ size_t cvec_X_capacity(cvec_X vec); const cvec_X_value* cvec_X_at(const cvec_X* self, size_t idx); const cvec_X_value* cvec_X_get(const cvec_X* self, i_valraw raw); // return NULL if not found +cvec_X_value* cvec_X_at_mut(cvec_X* self, size_t idx); cvec_X_value* cvec_X_get_mut(cvec_X* self, i_valraw raw); // get mutable value cvec_X_iter cvec_X_find(const cvec_X* self, i_valraw raw); cvec_X_iter cvec_X_find_in(cvec_X_iter i1, cvec_X_iter i2, i_valraw raw); |
