summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-01-19 19:27:07 +0100
committerTyge Løvset <[email protected]>2023-01-19 19:27:07 +0100
commit5aa48d538569463ffeda976d21f79edc5f276be4 (patch)
tree6bbd87bc9e5041b2c3b69e14ec175536b8418630 /include
parent0c4c4f8bba17562735b67b2923cd23c773aa53a7 (diff)
downloadSTC-modified-5aa48d538569463ffeda976d21f79edc5f276be4.tar.gz
STC-modified-5aa48d538569463ffeda976d21f79edc5f276be4.zip
Add a from_n() method to containers (and put_n() to maps), to support new initialization.
Diffstat (limited to 'include')
-rw-r--r--include/stc/ccommon.h4
-rw-r--r--include/stc/cdeq.h5
-rw-r--r--include/stc/clist.h4
-rw-r--r--include/stc/cmap.h19
-rw-r--r--include/stc/cpque.h6
-rw-r--r--include/stc/csmap.h16
-rw-r--r--include/stc/cspan.h13
-rw-r--r--include/stc/cstack.h15
-rw-r--r--include/stc/cvec.h4
9 files changed, 70 insertions, 16 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index edf4b2cb..d7b68d1f 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -113,6 +113,10 @@
#define c_no_hash (1<<6)
#define c_no_lookup (c_no_cmp|c_no_eq|c_no_hash)
+#define c_initialize(C, ...) \
+ C##_from_n((C##_raw[])__VA_ARGS__, sizeof((C##_raw[])__VA_ARGS__)/sizeof(C##_raw))
+#define c_literal(C, ...) (*(C[]){c_initialize(C, __VA_ARGS__)})
+
/* Generic algorithms */
typedef const char* crawstr;
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index 1fe52548..b555da63 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -48,7 +48,10 @@ STC_API void _cx_memb(_clear)(_cx_self* self);
STC_API void _cx_memb(_drop)(_cx_self* self);
STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key value);
STC_API void _cx_memb(_shrink_to_fit)(_cx_self *self);
-
+STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n)
+ { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); }
+STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n)
+ { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
#if !defined _i_queue
#if !defined i_no_emplace
STC_API _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos,
diff --git a/include/stc/clist.h b/include/stc/clist.h
index 37fa447a..ac687fc9 100644
--- a/include/stc/clist.h
+++ b/include/stc/clist.h
@@ -134,6 +134,10 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw)
#endif // !i_no_emplace
STC_INLINE _cx_self _cx_memb(_init)(void) { return c_INIT(_cx_self){NULL}; }
+STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n)
+ { while (n--) _cx_memb(_push_back)(self, i_keyfrom(*raw++)); }
+STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n)
+ { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { return true; }
STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return self->last == NULL; }
STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); }
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index df3ce6fc..e63adb77 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -195,8 +195,23 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) {
return _res;
}
-STC_INLINE _cx_iter
-_cx_memb(_begin)(const _cx_self* self) {
+STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) {
+ while (n--)
+#if defined _i_isset && defined i_no_emplace
+ _cx_memb(_insert)(self, *raw++);
+#elif defined _i_isset
+ _cx_memb(_emplace)(self, *raw++);
+#elif defined i_no_emplace
+ _cx_memb(_insert_or_assign)(self, raw->first, raw->second), ++raw;
+#else
+ _cx_memb(_emplace_or_assign)(self, raw->first, raw->second), ++raw;
+#endif
+}
+
+STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t 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) {
_cx_iter it = {self->table, self->table+self->bucket_count, self->_hashx};
if (it._hx)
while (*it._hx == 0)
diff --git a/include/stc/cpque.h b/include/stc/cpque.h
index 28b5eb86..f1f03f8e 100644
--- a/include/stc/cpque.h
+++ b/include/stc/cpque.h
@@ -47,6 +47,12 @@ STC_API void _cx_memb(_push)(_cx_self* self, _cx_value value);
STC_INLINE _cx_self _cx_memb(_init)(void)
{ return c_INIT(_cx_self){NULL}; }
+STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n)
+ { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); }
+
+STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n)
+ { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
+
STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, const size_t cap) {
if (cap != self->_len && cap <= self->_cap) return true;
_cx_value *d = (_cx_value *)c_REALLOC(self->data, cap*sizeof *d);
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index e5df0fe2..75f10308 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -294,6 +294,22 @@ _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, size_t n) {
+ while (n--)
+#if defined _i_isset && defined i_no_emplace
+ _cx_memb(_insert)(self, *raw++);
+#elif defined _i_isset
+ _cx_memb(_emplace)(self, *raw++);
+#elif defined i_no_emplace
+ _cx_memb(_insert_or_assign)(self, raw->first, raw->second), ++raw;
+#else
+ _cx_memb(_emplace_or_assign)(self, raw->first, raw->second), ++raw;
+#endif
+}
+
+STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n)
+ { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
+
#ifndef _i_isset
STC_DEF _cx_result
_cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped) {
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index a4c8f5cb..9969a670 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -48,7 +48,7 @@ int demo2() {
puts("");
// use a temporary Intspan object.
- c_FORFILTER (i, Intspan, cspan_literal(Intspan, {10, 20, 30, 23, 22, 21})
+ c_FORFILTER (i, Intspan, c_literal(Intspan, {10, 20, 30, 23, 22, 21})
, c_FLT_SKIPWHILE(i, *i.ref < 25)
&& (*i.ref & 1) == 0 // even only
, c_FLT_TAKE(i, 2)) // break after 2
@@ -66,6 +66,9 @@ int demo2() {
typedef struct { Self##_value *ref, *end; } Self##_iter; \
typedef struct { Self##_value *data; uint32_t dim[RANK]; } Self; \
\
+ STC_INLINE Self Self##_from_n(Self##_raw* raw, const size_t n) { \
+ return (Self){.data=raw, .dim={(uint32_t)n}}; \
+ } \
STC_INLINE Self##_iter Self##_begin(const Self* self) { \
size_t n = cspan_size(self); \
Self##_iter it = {n ? self->data : NULL, self->data + n}; \
@@ -90,17 +93,11 @@ int demo2() {
/* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */
#define cspan_from(container) \
- {.data=(container)->data, .dim={(container)->_len}}
+ {.data=(container)->data, .dim={(uint32_t)(container)->_len}}
#define cspan_from_array(array) \
{.data=(array) + c_STATIC_ASSERT(sizeof(array) != sizeof(void*)), .dim={c_ARRAYLEN(array)}}
-#define cspan_from_list(ValueType, ...) \
- {.data=(ValueType[])__VA_ARGS__, .dim={sizeof((ValueType[])__VA_ARGS__)/sizeof(ValueType)}}
-
-#define cspan_literal(SpanType, ...) \
- (c_INIT(SpanType)cspan_from_list(SpanType##_value, __VA_ARGS__))
-
#define cspan_size(self) _cspan_size((self)->dim, cspan_rank(self))
#define cspan_rank(self) c_ARRAYLEN((self)->dim)
#define cspan_index(self, ...) \
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index f4a86156..c1332769 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -44,11 +44,11 @@
typedef i_keyraw _cx_raw;
STC_INLINE _cx_self _cx_memb(_init)(void) {
- _cx_self s; s._len = 0;
+ _cx_self cx; cx._len = 0;
#ifndef i_capacity
- s._cap = 0; s.data = NULL;
+ cx._cap = 0; cx.data = NULL;
#endif
- return s;
+ return cx;
}
#ifdef i_capacity
@@ -106,8 +106,7 @@ STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) {
return false;
}
-STC_INLINE _cx_value*
-_cx_memb(_append_uninit)(_cx_self *self, size_t n) {
+STC_INLINE _cx_value* _cx_memb(_append_uninit)(_cx_self *self, size_t n) {
size_t len = self->_len;
if (!_cx_memb(_reserve)(self, len + n)) return NULL;
self->_len += n;
@@ -137,6 +136,12 @@ STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) {
STC_INLINE void _cx_memb(_pop)(_cx_self* self)
{ assert(!_cx_memb(_empty)(self)); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); }
+STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n)
+ { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); }
+
+STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n)
+ { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
+
STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx)
{ assert(idx < self->_len); return self->data + idx; }
STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, size_t idx)
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index fbacd305..7f8365d7 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -114,6 +114,10 @@ _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) {
STC_API _cx_self _cx_memb(_clone)(_cx_self cx);
STC_API _cx_iter _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos,
const _cx_value* p1, const _cx_value* p2);
+STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n)
+ { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); }
+STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n)
+ { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val)
{ return i_keyclone(val); }
STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) {