summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-15 15:40:08 +0100
committerTyge Løvset <[email protected]>2023-02-15 15:40:08 +0100
commit290055ad96c22fd9dfb539d0217e45cd2f1cdb91 (patch)
treece6d1398ca4a08ddcb42653ad46c0ab76655ee47
parente456085a392d063df1a2495422523a2474ea6a18 (diff)
downloadSTC-modified-290055ad96c22fd9dfb539d0217e45cd2f1cdb91.tar.gz
STC-modified-290055ad96c22fd9dfb539d0217e45cd2f1cdb91.zip
Cleaned up in size-types. API always uses intptr_t as default for all containers.
-rw-r--r--docs/cmap_api.md15
-rw-r--r--docs/cset_api.md8
-rw-r--r--docs/csmap_api.md13
-rw-r--r--docs/csset_api.md7
-rw-r--r--include/stc/cbits.h77
-rw-r--r--include/stc/cmap.h34
-rw-r--r--include/stc/csmap.h31
-rw-r--r--src/utf8tabs.py43
8 files changed, 126 insertions, 102 deletions
diff --git a/docs/cmap_api.md b/docs/cmap_api.md
index 76fbda92..998146a7 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -38,7 +38,7 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain
#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)
+#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.
@@ -47,20 +47,20 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain
```c
cmap_X cmap_X_init(void);
-cmap_X cmap_X_with_capacity(i_ssize cap);
+cmap_X cmap_X_with_capacity(intptr_t cap);
cmap_X cmap_X_clone(cmap_x map);
void cmap_X_clear(cmap_X* self);
void cmap_X_copy(cmap_X* self, const cmap_X* other);
float cmap_X_max_load_factor(const cmap_X* self); // default: 0.85f
-bool cmap_X_reserve(cmap_X* self, i_ssize size);
+bool cmap_X_reserve(cmap_X* self, intptr_t size);
void cmap_X_shrink_to_fit(cmap_X* self);
void cmap_X_drop(cmap_X* self); // destructor
bool cmap_X_empty(const cmap_X* self );
-i_ssize cmap_X_size(const cmap_X* self);
-i_ssize cmap_X_capacity(const cmap_X* self); // buckets * max_load_factor
-i_ssize cmap_X_bucket_count(const cmap_X* self); // num. of allocated buckets
+intptr_t cmap_X_size(const cmap_X* self);
+intptr_t cmap_X_capacity(const cmap_X* self); // buckets * max_load_factor
+intptr_t cmap_X_bucket_count(const cmap_X* self); // num. of allocated buckets
const cmap_X_mapped* cmap_X_at(const cmap_X* self, i_keyraw rkey); // rkey must be in map
cmap_X_mapped* cmap_X_at_mut(cmap_X* self, i_keyraw rkey); // mutable at
@@ -74,7 +74,8 @@ cmap_X_result cmap_X_insert_or_assign(cmap_X* self, i_key key, i_val map
cmap_X_result cmap_X_push(cmap_X* self, cmap_X_value entry); // similar to insert
cmap_X_result cmap_X_emplace(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map
-cmap_X_result cmap_X_emplace_or_assign(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped
+cmap_X_result cmap_X_emplace_or_assign(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update
+cmap_X_result cmap_X_put(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // emplace_or_assign() alias
int cmap_X_erase(cmap_X* self, i_keyraw rkey); // return 0 or 1
cmap_X_iter cmap_X_erase_at(cmap_X* self, cmap_X_iter it); // return iter after it
diff --git a/docs/cset_api.md b/docs/cset_api.md
index 34b6f3eb..287f7636 100644
--- a/docs/cset_api.md
+++ b/docs/cset_api.md
@@ -36,14 +36,14 @@ cset_X cset_X_clone(cset_x set);
void cset_X_clear(cset_X* self);
void cset_X_copy(cset_X* self, const cset_X* other);
float cset_X_max_load_factor(const cset_X* self); // default: 0.85
-bool cset_X_reserve(cset_X* self, i_ssize size);
+bool cset_X_reserve(cset_X* self, intptr_t size);
void cset_X_shrink_to_fit(cset_X* self);
void cset_X_drop(cset_X* self); // destructor
-i_ssize cset_X_size(const cset_X* self); // num. of allocated buckets
-i_ssize cset_X_capacity(const cset_X* self); // buckets * max_load_factor
bool cset_X_empty(const cset_X* self);
-i_ssize cset_X_bucket_count(const cset_X* self);
+intptr_t cset_X_size(const cset_X* self); // num. of allocated buckets
+intptr_t cset_X_capacity(const cset_X* self); // buckets * max_load_factor
+intptr_t cset_X_bucket_count(const cset_X* self);
bool cset_X_contains(const cset_X* self, i_keyraw rkey);
const cset_X_value* cset_X_get(const cset_X* self, i_keyraw rkey); // return NULL if not found
diff --git a/docs/csmap_api.md b/docs/csmap_api.md
index 19a654d6..89948369 100644
--- a/docs/csmap_api.md
+++ b/docs/csmap_api.md
@@ -34,7 +34,7 @@ See the c++ class [std::map](https://en.cppreference.com/w/cpp/container/map) fo
#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 // defaults to int32_t
+#define i_ssize // internal size rep. defaults to int32_t
#include <stc/csmap.h>
```
`X` should be replaced by the value of `i_tag` in all of the following documentation.
@@ -43,8 +43,8 @@ See the c++ class [std::map](https://en.cppreference.com/w/cpp/container/map) fo
```c
csmap_X csmap_X_init(void);
-csset_X csmap_X_with_capacity(i_ssize cap);
-bool csmap_X_reserve(csmap_X* self, i_ssize cap);
+csset_X csmap_X_with_capacity(intptr_t cap);
+bool csmap_X_reserve(csmap_X* self, intptr_t cap);
void csmap_X_shrink_to_fit(csmap_X* self);
csmap_X csmap_X_clone(csmap_x map);
@@ -53,8 +53,8 @@ void csmap_X_copy(csmap_X* self, const csmap_X* other);
void csmap_X_drop(csmap_X* self); // destructor
bool csmap_X_empty(const csmap_X* self);
-i_ssize csmap_X_size(const csmap_X* self);
-i_ssize csmap_X_capacity(const csmap_X* self);
+intptr_t csmap_X_size(const csmap_X* self);
+intptr_t csmap_X_capacity(const csmap_X* self);
const csmap_X_mapped* csmap_X_at(const csmap_X* self, i_keyraw rkey); // rkey must be in map
csmap_X_mapped* csmap_X_at_mut(csmap_X* self, i_keyraw rkey); // mutable at
@@ -74,6 +74,7 @@ csmap_X_result csmap_X_push(csmap_X* self, csmap_X_value entry);
csmap_X_result csmap_X_emplace(csmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map
csmap_X_result csmap_X_emplace_or_assign(csmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped
+csmap_X_result csmap_X_put(csmap_X* self, i_keyraw rkey, i_valraw rmapped); // csmap_X_emplace_or_assign() alias
int csmap_X_erase(csmap_X* self, i_keyraw rkey);
csmap_X_iter csmap_X_erase_at(csmap_X* self, csmap_X_iter it); // returns iter after it
@@ -82,7 +83,7 @@ csmap_X_iter csmap_X_erase_range(csmap_X* self, csmap_X_iter it1, csmap
csmap_X_iter csmap_X_begin(const csmap_X* self);
csmap_X_iter csmap_X_end(const csmap_X* self);
void csmap_X_next(csmap_X_iter* iter);
-csmap_X_iter csmap_X_advance(csmap_X_iter it, i_ssize n);
+csmap_X_iter csmap_X_advance(csmap_X_iter it, intptr_t n);
csmap_X_value csmap_X_value_clone(csmap_X_value val);
csmap_X_raw csmap_X_value_toraw(csmap_X_value* pval);
diff --git a/docs/csset_api.md b/docs/csset_api.md
index c696eab5..80ee1844 100644
--- a/docs/csset_api.md
+++ b/docs/csset_api.md
@@ -29,8 +29,8 @@ See the c++ class [std::set](https://en.cppreference.com/w/cpp/container/set) fo
```c
csset_X csset_X_init(void);
-csset_X csset_X_with_capacity(i_ssize cap);
-bool csset_X_reserve(csset_X* self, i_ssize cap);
+csset_X csset_X_with_capacity(intptr_t cap);
+bool csset_X_reserve(csset_X* self, intptr_t cap);
void csset_X_shrink_to_fit(csset_X* self);
csset_X csset_X_clone(csset_x set);
@@ -38,8 +38,9 @@ void csset_X_clear(csset_X* self);
void csset_X_copy(csset_X* self, const csset_X* other);
void csset_X_drop(csset_X* self); // destructor
-i_ssize csset_X_size(const csset_X* self);
bool csset_X_empty(const csset_X* self);
+intptr_t csset_X_size(const csset_X* self);
+intptr_t csset_X_capacity(const csset_X* self);
const csset_X_value* csset_X_get(const csset_X* self, i_keyraw rkey); // const get
csset_X_value* csset_X_get_mut(csset_X* self, i_keyraw rkey); // return NULL if not found
diff --git a/include/stc/cbits.h b/include/stc/cbits.h
index c3167dae..e2d97f8c 100644
--- a/include/stc/cbits.h
+++ b/include/stc/cbits.h
@@ -49,14 +49,16 @@ int main() {
}
}
*/
-
#ifndef CBITS_H_INCLUDED
#include "ccommon.h"
#include <stdlib.h>
#include <string.h>
+#ifndef i_ssize
+#define i_ssize intptr_t
+#endif
#define _cbits_bit(i) ((uint64_t)1 << ((i) & 63))
-#define _cbits_words(n) (int64_t)(((n) + 63)>>6)
+#define _cbits_words(n) (i_ssize)(((n) + 63)>>6)
#define _cbits_bytes(n) (_cbits_words(n) * c_sizeof(uint64_t))
#if defined(__GNUC__) || defined(__clang__)
@@ -73,23 +75,23 @@ int main() {
}
#endif
-STC_INLINE int64_t _cbits_count(const uint64_t* set, const int64_t sz) {
- const int64_t n = sz>>6;
- int64_t count = 0;
- for (int64_t i = 0; i < n; ++i)
+STC_INLINE i_ssize _cbits_count(const uint64_t* set, const i_ssize sz) {
+ const i_ssize n = sz>>6;
+ i_ssize count = 0;
+ for (i_ssize i = 0; i < n; ++i)
count += cpopcount64(set[i]);
if (sz & 63)
count += cpopcount64(set[n] & (_cbits_bit(sz) - 1));
return count;
}
-STC_INLINE char* _cbits_to_str(const uint64_t* set, const int64_t sz,
- char* out, int64_t start, int64_t stop) {
+STC_INLINE char* _cbits_to_str(const uint64_t* set, const i_ssize sz,
+ char* out, i_ssize start, i_ssize stop) {
if (stop > sz) stop = sz;
assert(start <= stop);
c_memset(out, '0', stop - start);
- for (int64_t i = start; i < stop; ++i)
+ for (i_ssize i = start; i < stop; ++i)
if ((set[i>>6] & _cbits_bit(i)) != 0)
out[i - start] = '1';
out[stop - start] = '\0';
@@ -97,8 +99,8 @@ STC_INLINE char* _cbits_to_str(const uint64_t* set, const int64_t sz,
}
#define _cbits_OPR(OPR, VAL) \
- const int64_t n = sz>>6; \
- for (int64_t i = 0; i < n; ++i) \
+ const i_ssize n = sz>>6; \
+ for (i_ssize i = 0; i < n; ++i) \
if ((set[i] OPR other[i]) != VAL) \
return false; \
if (!(sz & 63)) \
@@ -106,10 +108,10 @@ STC_INLINE char* _cbits_to_str(const uint64_t* set, const int64_t sz,
const uint64_t i = (uint64_t)n, m = _cbits_bit(sz) - 1; \
return ((set[i] OPR other[i]) & m) == (VAL & m)
-STC_INLINE bool _cbits_subset_of(const uint64_t* set, const uint64_t* other, const int64_t sz)
+STC_INLINE bool _cbits_subset_of(const uint64_t* set, const uint64_t* other, const i_ssize sz)
{ _cbits_OPR(|, set[i]); }
-STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const int64_t sz)
+STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const i_ssize sz)
{ _cbits_OPR(&, 0); }
#endif // CBITS_H_INCLUDED
@@ -121,12 +123,12 @@ STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, cons
#define _i_assert(x) assert(x)
#define i_type cbits
-struct { uint64_t *data64; int64_t _size; } typedef i_type;
+struct { uint64_t *data64; i_ssize _size; } typedef i_type;
-STC_INLINE cbits cbits_init(void) { return c_LITERAL(cbits){NULL}; }
-STC_INLINE void cbits_create(cbits* self) { self->data64 = NULL; self->_size = 0; }
-STC_INLINE void cbits_drop(cbits* self) { c_free(self->data64); }
-STC_INLINE int64_t cbits_size(const cbits* self) { return self->_size; }
+STC_INLINE cbits cbits_init(void) { return c_LITERAL(cbits){NULL}; }
+STC_INLINE void cbits_create(cbits* self) { self->data64 = NULL; self->_size = 0; }
+STC_INLINE void cbits_drop(cbits* self) { c_free(self->data64); }
+STC_INLINE i_ssize cbits_size(const cbits* self) { return self->_size; }
STC_INLINE cbits* cbits_take(cbits* self, cbits other) {
if (self->data64 != other.data64) {
@@ -137,7 +139,7 @@ STC_INLINE cbits* cbits_take(cbits* self, cbits other) {
}
STC_INLINE cbits cbits_clone(cbits other) {
- const int64_t bytes = _cbits_bytes(other._size);
+ const i_ssize bytes = _cbits_bytes(other._size);
cbits set = {(uint64_t *)c_memcpy(c_malloc(bytes), other.data64, bytes), other._size};
return set;
}
@@ -151,8 +153,8 @@ STC_INLINE cbits* cbits_copy(cbits* self, const cbits* other) {
return self;
}
-STC_INLINE void cbits_resize(cbits* self, const int64_t size, const bool value) {
- const int64_t new_n = _cbits_words(size), osize = self->_size, old_n = _cbits_words(osize);
+STC_INLINE void cbits_resize(cbits* self, const i_ssize size, const bool value) {
+ const i_ssize new_n = _cbits_words(size), osize = self->_size, old_n = _cbits_words(osize);
self->data64 = (uint64_t *)c_realloc(self->data64, new_n*8);
self->_size = size;
if (new_n >= old_n) {
@@ -174,13 +176,13 @@ STC_INLINE cbits cbits_move(cbits* self) {
return tmp;
}
-STC_INLINE cbits cbits_with_size(const int64_t size, const bool value) {
+STC_INLINE cbits cbits_with_size(const i_ssize size, const bool value) {
cbits set = {(uint64_t *)c_malloc(_cbits_bytes(size)), size};
cbits_set_all(&set, value);
return set;
}
-STC_INLINE cbits cbits_with_pattern(const int64_t size, const uint64_t pattern) {
+STC_INLINE cbits cbits_with_pattern(const i_ssize size, const uint64_t pattern) {
cbits set = {(uint64_t *)c_malloc(_cbits_bytes(size)), size};
cbits_set_pattern(&set, pattern);
return set;
@@ -198,7 +200,7 @@ struct { uint64_t data64[(i_capacity - 1)/64 + 1]; } typedef i_type;
STC_INLINE i_type _i_memb(_init)(void) { return c_LITERAL(i_type){0}; }
STC_INLINE void _i_memb(_create)(i_type* self) {}
STC_INLINE void _i_memb(_drop)(i_type* self) {}
-STC_INLINE int64_t _i_memb(_size)(const i_type* self) { return i_capacity; }
+STC_INLINE i_ssize _i_memb(_size)(const i_type* self) { return i_capacity; }
STC_INLINE i_type _i_memb(_move)(i_type* self) { return *self; }
STC_INLINE i_type* _i_memb(_take)(i_type* self, i_type other)
@@ -213,13 +215,13 @@ STC_INLINE i_type* _i_memb(_copy)(i_type* self, const i_type* other)
STC_INLINE void _i_memb(_set_all)(i_type *self, const bool value);
STC_INLINE void _i_memb(_set_pattern)(i_type *self, const uint64_t pattern);
-STC_INLINE i_type _i_memb(_with_size)(const int64_t size, const bool value) {
+STC_INLINE i_type _i_memb(_with_size)(const i_ssize size, const bool value) {
assert(size <= i_capacity);
i_type set; _i_memb(_set_all)(&set, value);
return set;
}
-STC_INLINE i_type _i_memb(_with_pattern)(const int64_t size, const uint64_t pattern) {
+STC_INLINE i_type _i_memb(_with_pattern)(const i_ssize size, const uint64_t pattern) {
assert(size <= i_capacity);
i_type set; _i_memb(_set_pattern)(&set, pattern);
return set;
@@ -232,31 +234,31 @@ STC_INLINE void _i_memb(_set_all)(i_type *self, const bool value)
{ c_memset(self->data64, value? ~0 : 0, _cbits_bytes(_i_memb(_size)(self))); }
STC_INLINE void _i_memb(_set_pattern)(i_type *self, const uint64_t pattern) {
- int64_t n = _cbits_words(_i_memb(_size)(self));
+ i_ssize n = _cbits_words(_i_memb(_size)(self));
while (n--) self->data64[n] = pattern;
}
-STC_INLINE bool _i_memb(_test)(const i_type* self, const int64_t i)
+STC_INLINE bool _i_memb(_test)(const i_type* self, const i_ssize i)
{ return (self->data64[i>>6] & _cbits_bit(i)) != 0; }
-STC_INLINE bool _i_memb(_at)(const i_type* self, const int64_t i)
+STC_INLINE bool _i_memb(_at)(const i_type* self, const i_ssize i)
{ return (self->data64[i>>6] & _cbits_bit(i)) != 0; }
-STC_INLINE void _i_memb(_set)(i_type *self, const int64_t i)
+STC_INLINE void _i_memb(_set)(i_type *self, const i_ssize i)
{ self->data64[i>>6] |= _cbits_bit(i); }
-STC_INLINE void _i_memb(_reset)(i_type *self, const int64_t i)
+STC_INLINE void _i_memb(_reset)(i_type *self, const i_ssize i)
{ self->data64[i>>6] &= ~_cbits_bit(i); }
-STC_INLINE void _i_memb(_set_value)(i_type *self, const int64_t i, const bool b) {
+STC_INLINE void _i_memb(_set_value)(i_type *self, const i_ssize i, const bool b) {
self->data64[i>>6] ^= ((uint64_t)-(int)b ^ self->data64[i>>6]) & _cbits_bit(i);
}
-STC_INLINE void _i_memb(_flip)(i_type *self, const int64_t i)
+STC_INLINE void _i_memb(_flip)(i_type *self, const i_ssize i)
{ self->data64[i>>6] ^= _cbits_bit(i); }
STC_INLINE void _i_memb(_flip_all)(i_type *self) {
- int64_t n = _cbits_words(_i_memb(_size)(self));
+ i_ssize n = _cbits_words(_i_memb(_size)(self));
while (n--) self->data64[n] ^= ~(uint64_t)0;
}
@@ -270,19 +272,19 @@ STC_INLINE i_type _i_memb(_from)(const char* str) {
/* Intersection */
STC_INLINE void _i_memb(_intersect)(i_type *self, const i_type* other) {
_i_assert(self->_size == other->_size);
- int64_t n = _cbits_words(_i_memb(_size)(self));
+ i_ssize n = _cbits_words(_i_memb(_size)(self));
while (n--) self->data64[n] &= other->data64[n];
}
/* Union */
STC_INLINE void _i_memb(_union)(i_type *self, const i_type* other) {
_i_assert(self->_size == other->_size);
- int64_t n = _cbits_words(_i_memb(_size)(self));
+ i_ssize n = _cbits_words(_i_memb(_size)(self));
while (n--) self->data64[n] |= other->data64[n];
}
/* Exclusive disjunction */
STC_INLINE void _i_memb(_xor)(i_type *self, const i_type* other) {
_i_assert(self->_size == other->_size);
- int64_t n = _cbits_words(_i_memb(_size)(self));
+ i_ssize n = _cbits_words(_i_memb(_size)(self));
while (n--) self->data64[n] ^= other->data64[n];
}
@@ -303,6 +305,7 @@ STC_INLINE bool _i_memb(_disjoint)(const i_type* self, const i_type* other) {
}
#define CBITS_H_INCLUDED
+#undef _i_size
#undef _i_memb
#undef _i_assert
#undef i_capacity
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index 799df1ec..5e370167 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -75,9 +75,11 @@ typedef struct { int64_t idx; uint8_t hx; } chash_bucket_t;
#endif
#ifndef i_ssize
#define i_ssize int32_t
+ #define _i_size intptr_t
#define _i_expandby 1
#else
#define _i_expandby 2
+ #define _i_size i_ssize
#endif
#include "priv/template.h"
#ifndef i_hash_functor
@@ -102,13 +104,13 @@ typedef _i_SET_ONLY( i_keyraw )
i_valraw second; } )
_cx_raw;
-STC_API _cx_self _cx_memb(_with_capacity)(int64_t cap);
+STC_API _cx_self _cx_memb(_with_capacity)(_i_size cap);
#if !defined i_no_clone
STC_API _cx_self _cx_memb(_clone)(_cx_self map);
#endif
STC_API void _cx_memb(_drop)(_cx_self* self);
STC_API void _cx_memb(_clear)(_cx_self* self);
-STC_API bool _cx_memb(_reserve)(_cx_self* self, int64_t capacity);
+STC_API bool _cx_memb(_reserve)(_cx_self* self, _i_size capacity);
STC_API chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr);
STC_API _cx_result _cx_memb(_insert_entry_)(_cx_self* self, _cx_rawkey rkey);
STC_API void _cx_memb(_erase_entry)(_cx_self* self, _cx_value* val);
@@ -117,17 +119,22 @@ STC_INLINE _cx_self _cx_memb(_init)(void) { return c_LITERAL(_cx_self){0}; }
STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) { _cx_memb(_reserve)(self, self->size); }
STC_INLINE float _cx_memb(_max_load_factor)(const _cx_self* self) { return (float)(i_max_load_factor); }
STC_INLINE bool _cx_memb(_empty)(const _cx_self* map) { return !map->size; }
-STC_INLINE int64_t _cx_memb(_size)(const _cx_self* map) { return map->size; }
-STC_INLINE int64_t _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket_count; }
-STC_INLINE int64_t _cx_memb(_capacity)(const _cx_self* map)
- { return (int64_t)((float)map->bucket_count * (i_max_load_factor)); }
+STC_INLINE _i_size _cx_memb(_size)(const _cx_self* map) { return map->size; }
+STC_INLINE _i_size _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket_count; }
+STC_INLINE _i_size _cx_memb(_capacity)(const _cx_self* map)
+ { return (_i_size)((float)map->bucket_count * (i_max_load_factor)); }
STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rkey)
{ return self->size && self->_hashx[_cx_memb(_bucket_)(self, &rkey).idx]; }
#ifndef _i_isset
- STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped);
+ STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped);
#if !defined i_no_emplace
- STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped);
+ STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped);
+ STC_INLINE _cx_result _cx_memb(_put)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped)
+ { return _cx_memb(_emplace_or_assign)(self, rkey, rmapped); }
+ #else
+ STC_INLINE _cx_result _cx_memb(_put)(_cx_self* self, i_key key, i_val mapped)
+ { return _cx_memb(_insert_or_assign)(self, key, mapped); }
#endif
STC_INLINE const _cx_mapped*
@@ -201,7 +208,7 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) {
return _res;
}
-STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n) {
+STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, _i_size n) {
while (n--)
#if defined _i_isset && defined i_no_emplace
_cx_memb(_insert)(self, *raw++);
@@ -214,7 +221,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n)
#endif
}
-STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, int64_t n)
+STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, _i_size n)
{ _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) {
@@ -237,7 +244,7 @@ _cx_memb(_next)(_cx_iter* it) {
}
STC_INLINE _cx_iter
-_cx_memb(_advance)(_cx_iter it, uint64_t n) {
+_cx_memb(_advance)(_cx_iter it, size_t n) {
while (n-- && it.ref) _cx_memb(_next)(&it);
return it;
}
@@ -300,7 +307,7 @@ STC_INLINE uint64_t next_power_of_2(uint64_t n) {
#endif // CMAP_H_INCLUDED
STC_DEF _cx_self
-_cx_memb(_with_capacity)(const int64_t cap) {
+_cx_memb(_with_capacity)(const _i_size cap) {
_cx_self h = {0};
_cx_memb(_reserve)(&h, cap);
return h;
@@ -411,7 +418,7 @@ _cx_memb(_clone)(_cx_self m) {
#endif
STC_DEF bool
-_cx_memb(_reserve)(_cx_self* self, const int64_t newcap) {
+_cx_memb(_reserve)(_cx_self* self, const _i_size newcap) {
const i_ssize _oldbuckets = self->bucket_count, _newcap = (i_ssize)newcap;
if (_newcap != self->size && _newcap <= _oldbuckets)
return true;
@@ -469,6 +476,7 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) {
#undef i_max_load_factor
#undef i_hash_functor
#undef i_eq_functor
+#undef _i_size
#undef _i_isset
#undef _i_ismap
#undef _i_ishash
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index 4344ceb9..48e1eb62 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -75,6 +75,9 @@ int main(void) {
#endif
#ifndef i_ssize
#define i_ssize int32_t
+ #define _i_size intptr_t
+#else
+ #define _i_size i_ssize
#endif
#include "priv/template.h"
#ifndef i_cmp_functor
@@ -110,7 +113,7 @@ STC_API _cx_self _cx_memb(_init)(void);
STC_API _cx_result _cx_memb(_insert)(_cx_self* self, i_key key _i_MAP_ONLY(, i_val mapped));
STC_API _cx_result _cx_memb(_push)(_cx_self* self, _cx_value _val);
STC_API void _cx_memb(_drop)(_cx_self* self);
-STC_API bool _cx_memb(_reserve)(_cx_self* self, int64_t cap);
+STC_API bool _cx_memb(_reserve)(_cx_self* self, _i_size cap);
STC_API _cx_value* _cx_memb(_find_it)(const _cx_self* self, _cx_rawkey rkey, _cx_iter* out);
STC_API _cx_iter _cx_memb(_lower_bound)(const _cx_self* self, _cx_rawkey rkey);
STC_API _cx_value* _cx_memb(_front)(const _cx_self* self);
@@ -121,8 +124,8 @@ STC_API _cx_iter _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx
STC_API void _cx_memb(_next)(_cx_iter* it);
STC_INLINE bool _cx_memb(_empty)(const _cx_self* cx) { return cx->size == 0; }
-STC_INLINE int64_t _cx_memb(_size)(const _cx_self* cx) { return cx->size; }
-STC_INLINE int64_t _cx_memb(_capacity)(const _cx_self* cx) { return cx->cap; }
+STC_INLINE _i_size _cx_memb(_size)(const _cx_self* cx) { return cx->size; }
+STC_INLINE _i_size _cx_memb(_capacity)(const _cx_self* cx) { return cx->cap; }
STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey)
{ _cx_iter it; _cx_memb(_find_it)(self, rkey, &it); return it; }
STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rkey)
@@ -133,7 +136,7 @@ STC_INLINE _cx_value* _cx_memb(_get_mut)(_cx_self* self, _cx_rawkey rkey)
{ _cx_iter it; return _cx_memb(_find_it)(self, rkey, &it); }
STC_INLINE _cx_self
-_cx_memb(_with_capacity)(const int64_t cap) {
+_cx_memb(_with_capacity)(const _i_size cap) {
_cx_self tree = _cx_memb(_init)();
_cx_memb(_reserve)(&tree, cap);
return tree;
@@ -180,10 +183,15 @@ _cx_memb(_shrink_to_fit)(_cx_self *self) {
#endif // !i_no_clone
#ifndef _i_isset
+ STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped);
#if !defined i_no_emplace
- STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped);
+ STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped);
+ STC_INLINE _cx_result _cx_memb(_put)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped)
+ { return _cx_memb(_emplace_or_assign)(self, rkey, rmapped); }
+ #else
+ STC_INLINE _cx_result _cx_memb(_put)(_cx_self* self, i_key key, i_val mapped)
+ { return _cx_memb(_insert_or_assign)(self, key, mapped); }
#endif
- STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped);
STC_INLINE const _cx_mapped*
_cx_memb(_at)(const _cx_self* self, _cx_rawkey rkey)
@@ -212,7 +220,7 @@ _cx_memb(_end)(const _cx_self* self) {
}
STC_INLINE _cx_iter
-_cx_memb(_advance)(_cx_iter it, uint64_t n) {
+_cx_memb(_advance)(_cx_iter it, size_t n) {
while (n-- && it.ref)
_cx_memb(_next)(&it);
return it;
@@ -228,8 +236,8 @@ _cx_memb(_init)(void) {
}
STC_DEF bool
-_cx_memb(_reserve)(_cx_self* self, const int64_t cap) {
- if ((i_ssize)cap <= self->cap)
+_cx_memb(_reserve)(_cx_self* self, const _i_size cap) {
+ if (cap <= self->cap)
return false;
_cx_node* nodes = (_cx_node*)i_realloc(self->nodes, (cap + 1)*c_sizeof(_cx_node));
if (!nodes)
@@ -297,7 +305,7 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) {
return _res;
}
-STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n) {
+STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, _i_size n) {
while (n--)
#if defined _i_isset && defined i_no_emplace
_cx_memb(_insert)(self, *raw++);
@@ -310,7 +318,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n)
#endif
}
-STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, int64_t n)
+STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, _i_size n)
{ _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
#ifndef _i_isset
@@ -587,6 +595,7 @@ _cx_memb(_drop)(_cx_self* self) {
#endif // i_implement
#undef i_cmp_functor
+#undef _i_size
#undef _i_isset
#undef _i_ismap
#undef _i_keyref
diff --git a/src/utf8tabs.py b/src/utf8tabs.py
index 7ed5e7ae..5bcc7144 100644
--- a/src/utf8tabs.py
+++ b/src/utf8tabs.py
@@ -5,16 +5,16 @@ import numpy as np
_UNICODE_DIR = "https://www.unicode.org/Public/15.0.0/ucd"
-def read_unidata(casetype='lowcase', category='Lu', range32=False):
+def read_unidata(casetype='lowcase', category='Lu', bitrange=16):
df = pd.read_csv(_UNICODE_DIR+'/UnicodeData.txt', sep=';', converters={0: lambda x: int(x, base=16)},
names=['code', 'name', 'category', 'canclass', 'bidircat', 'chrdecomp',
'decdig', 'digval', 'numval', 'mirrored', 'uc1name', 'comment',
'upcase', 'lowcase', 'titlecase'],
usecols=['code', 'name', 'category', 'bidircat', 'upcase', 'lowcase', 'titlecase'])
- if range32:
- df = df[df['code'] >= (1<<16)]
- else:
+ if bitrange == 16:
df = df[df['code'] < (1<<16)]
+ else:
+ df = df[df['code'] >= (1<<16)]
if category:
df = df[df['category'] == category]
@@ -27,14 +27,14 @@ def read_unidata(casetype='lowcase', category='Lu', range32=False):
return df
-def read_casefold(range32=False):
+def read_casefold(bitrange):
df = pd.read_csv(_UNICODE_DIR+'/CaseFolding.txt', engine='python', sep='; #? ?', comment='#',
converters={0: lambda x: int(x, base=16)},
names=['code', 'status', 'lowcase', 'name']) # comment => 'name'
- if range32:
- df = df[df['code'] >= (1<<16)]
- else:
+ if bitrange == 16:
df = df[df['code'] < (1<<16)]
+ else:
+ df = df[df['code'] >= (1<<16)]
df = df[df.status.isin(['S', 'C'])]
df['lowcase'] = df['lowcase'].apply(int, base=16)
@@ -75,8 +75,11 @@ def make_table(caselist):
return table
-def print_table(name, table, style=1):
- print('static struct CaseMapping %s[] = {' % (name))
+def print_table(name, table, style=1, bitrange=16):
+ r32 = '32' if bitrange == 32 else ''
+ print('#include <stdint.h>\n')
+ print('struct CaseMapping%d { uint%d_t c1, c2, m2; };\n' % (bitrange, bitrange))
+ print('static struct CaseMapping%s %s%s[] = {' % (r32, name, r32))
for a,b,c,t in table:
if style == 1: # first char with name
d = b - a + 1 if abs(c - b) != 1 else (b - a)/2 + 1
@@ -100,24 +103,22 @@ def print_index_table(name, indtab):
print('\n};')
-def compile_table(casetype='lowcase', category=None, range32=False):
+def compile_table(casetype='lowcase', category=None, bitrange=16):
if category:
- df = read_unidata(casetype, category, range32)
+ df = read_unidata(casetype, category, bitrange)
else:
- df = read_casefold(range32)
+ df = read_casefold(bitrange)
caselist = make_caselist(df, casetype)
table = make_table(caselist)
return table
def main():
- print('#include <stdint.h>\n')
- print('struct CaseMapping { uint16_t c1, c2, m2; };\n')
- range32 = False
+ bitrange = 32
- casemappings = compile_table('lowcase', None, range32) # CaseFolding.txt
- upcase = compile_table('lowcase', 'Lu', range32) # UnicodeData.txt uppercase
- lowcase = compile_table('upcase', 'Ll', range32) # UnicodeData.txt lowercase
+ casemappings = compile_table('lowcase', None, bitrange) # CaseFolding.txt
+ upcase = compile_table('lowcase', 'Lu', bitrange) # UnicodeData.txt uppercase
+ lowcase = compile_table('upcase', 'Ll', bitrange) # UnicodeData.txt lowercase
casefolding_len = len(casemappings)
@@ -143,7 +144,7 @@ def main():
lowcase_ind.append(len(casemappings))
casemappings.append(v)
- print_table('casemappings', casemappings, style=1)
+ print_table('casemappings', casemappings, style=1, bitrange=bitrange)
print('enum { casefold_len = %d };' % casefolding_len)
# upcase => low
@@ -151,7 +152,7 @@ def main():
print_index_table('upcase_ind', upcase_ind)
# lowcase => up. add "missing" SHARP S caused by https://www.unicode.org/policies/stability_policy.html#Case_Pair
- if not range32:
+ if bitrange == 16:
lowcase_ind.append(next(i for i,x in enumerate(casemappings) if x[0]==ord('ẞ')))
lowcase_ind.sort(key=lambda i: casemappings[i][2] - (casemappings[i][1] - casemappings[i][0]))
print_index_table('lowcase_ind', lowcase_ind)