summaryrefslogtreecommitdiffhomepage
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
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.
-rw-r--r--docs/cspan_api.md2
-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
-rw-r--r--misc/examples/printspan.c15
11 files changed, 82 insertions, 21 deletions
diff --git a/docs/cspan_api.md b/docs/cspan_api.md
index 2865a1a5..8cabd97e 100644
--- a/docs/cspan_api.md
+++ b/docs/cspan_api.md
@@ -24,8 +24,6 @@ on assignment, but not on initialization of a span variable. All functions are t
SpanType{N} cspan_make(ValueType* data, size_t xdim, ...); // make N-dimensional cspan
SpanType cspan_from(STCContainer* cnt); // create a 1D cspan from a compatible STC container
SpanType cspan_from_array(ValueType array[]); // create a 1D cspan from a C array
-SpanType cspan_from_list(T ValueType, {val0, val1, ...}); // create a 1D cspan from an initializer list
-SpanType& cspan_literal(T SpanType, {val0, val1, ...}); // create a 1D cspan compound literal from init list
void cspan_resize(SpanType{N}* self, size_t xdim, ...); // change the extent of each dimension
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) {
diff --git a/misc/examples/printspan.c b/misc/examples/printspan.c
index a897e669..6292cecf 100644
--- a/misc/examples/printspan.c
+++ b/misc/examples/printspan.c
@@ -1,12 +1,15 @@
// printspan.c
#include <stdio.h>
+#include <stc/cstr.h>
#define i_val int
#include <stc/cvec.h>
#define i_val int
#include <stc/cstack.h>
#define i_val int
#include <stc/cdeq.h>
+#define i_val_str
+#include <stc/cset.h>
#include <stc/cspan.h>
using_cspan(ispan, int, 1);
@@ -21,22 +24,28 @@ int main() {
c_AUTO (cvec_int, vec)
c_AUTO (cstack_int, stk)
c_AUTO (cdeq_int, deq)
+ c_AUTO (cset_str, set)
{
int arr[] = {1, 2, 3, 4, 5};
ispan sp = cspan_from_array(arr);
printMe((ispan)cspan_subspan(&sp, 1, 3));
- printMe((ispan)cspan_from_list(int, {1, 2, 3, 4}));
+ printMe(c_initialize(ispan, {1, 2, 3, 4}));
printMe((ispan)cspan_from_array(arr));
c_FORLIST (i, int, {1, 2, 3, 4, 5, 6}) cvec_int_push(&vec, *i.ref);
printMe((ispan)cspan_from(&vec));
- c_FORLIST (i, int, {1, 2, 3, 4, 5, 6, 7}) cstack_int_push(&stk, *i.ref);
+ stk = c_initialize(cstack_int, {1, 2, 3, 4, 5, 6, 7});
printMe((ispan)cspan_from(&stk));
- c_FORLIST (i, int, {1, 2, 3, 4, 5, 6, 7, 8}) cdeq_int_push_front(&deq, *i.ref);
+ deq = c_initialize(cdeq_int, {1, 2, 3, 4, 5, 6, 7, 8});
printMe((ispan)cspan_from(&deq));
+
+ set = c_initialize(cset_str, {"1", "2", "3", "4", "5", "6", "7", "8", "9"});
+ printf("%d\n", (int)cset_str_size(&set));
+ c_FOREACH (e, cset_str, set) printf("%s ", cstr_str(e.ref));
+ puts("");
}
}