summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-09-25 14:51:35 +0200
committerTyge Løvset <[email protected]>2022-09-25 15:09:23 +0200
commit2093b18689383286915df511167a12dc5d7bc75f (patch)
tree174779ae7e7b4f6d10ec8be300ea9574e2395729 /include
parent756120e349a892ef1b7655fc777f28ec13845300 (diff)
downloadSTC-modified-2093b18689383286915df511167a12dc5d7bc75f.tar.gz
STC-modified-2093b18689383286915df511167a12dc5d7bc75f.zip
Added: crange number generator type. (similar to c++ std::iota). Fixed c_forfilter again. Cleanups. Docs added.
Diffstat (limited to 'include')
-rw-r--r--include/stc/ccommon.h42
-rw-r--r--include/stc/cdeq.h2
-rw-r--r--include/stc/cstack.h2
-rw-r--r--include/stc/cstr.h4
-rw-r--r--include/stc/csview.h4
-rw-r--r--include/stc/cvec.h2
6 files changed, 31 insertions, 25 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index ac55b3e9..4ed3ebf8 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -30,14 +30,6 @@
#include <string.h>
#include <assert.h>
-#if SIZE_MAX == UINT32_MAX
- typedef int32_t isize_t;
-# define ISIZE_MAX INT32_MAX
-#elif SIZE_MAX == UINT64_MAX
- typedef int64_t isize_t;
-# define ISIZE_MAX INT64_MAX
-#endif
-
#if defined(_MSC_VER)
# pragma warning(disable: 4116 4996) // unnamed type definition in parentheses
# define STC_FORCE_INLINE static __forceinline
@@ -171,27 +163,25 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
; it.ref != (C##_value*)_endref; C##_next(&it))
#ifndef c_FLT_STACK
-#define c_FLT_STACK 4
+#define c_FLT_STACK 20
#endif
-
-#define c_flt_drop(i, n) (++(i).dropped[(i).dn++] > (n))
-#define c_flt_dropwhile(i, pred) ((i).dropwhile |= !(pred))
-#define c_flt_take(i, n) (++(i).taken[(i).tn++] <= (n))
+#define c_flt_take(i, n) (++(i).stack[(i).top++] <= (n))
+#define c_flt_drop(i, n) (++(i).stack[(i).top++] > (n))
+#define c_flt_dropwhile(i, pred) ((i).stack[(i).top++] |= !(pred))
#define c_forfilter(...) c_MACRO_OVERLOAD(c_forfilter, __VA_ARGS__)
#define c_forfilter4(it, C, cnt, filter) \
c_forfilter_s(it, C, C##_begin(&cnt), filter)
-#define c_forfilter5(it, C, cnt, filter, loopwhile) \
- c_forfilter_s(it, C, C##_begin(&cnt), filter) if (!(loopwhile)) break; else
+#define c_forfilter5(it, C, cnt, filter, takewhile) \
+ c_forfilter_s(it, C, C##_begin(&cnt), filter) if (!(takewhile)) break; else
#define c_forfilter_s(it, C, start, filter) \
c_foreach_s(it, C, start) if (!(filter)) ; else
#define c_foreach_s(i, C, start) \
for (struct {C##_iter it; C##_value *ref; \
- uint32_t index, taken[c_FLT_STACK+1], dropped[c_FLT_STACK]; \
- int8_t dn, tn; bool dropwhile;} \
+ uint32_t index, top, stack[c_FLT_STACK];} \
i = {.it=start, .ref=i.it.ref}; i.ref \
- ; C##_next(&i.it), i.ref = i.it.ref, ++i.index, i.dn=0, i.tn=0)
+ ; C##_next(&i.it), i.ref = i.it.ref, ++i.index, i.top=0)
#define c_forwhile(i, C, cnt, cond) \
for (struct {C##_iter it; C##_value *ref; size_t index;} \
@@ -213,6 +203,22 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
for (itype i=start, _inc=step, _end=(stop) - (_inc > 0) \
; (_inc > 0) ^ (i > _end); i += _inc)
+typedef intmax_t crange_value;
+struct {crange_value val, end, step; } typedef crange;
+struct {crange_value *ref, end, step; } typedef crange_iter;
+#define crange_from(...) c_MACRO_OVERLOAD(crange_from, __VA_ARGS__)
+#define crange_init() crange_from3(0, INTMAX_MAX, 1)
+#define crange_from1(start) crange_from3(start, INTMAX_MAX, 1)
+#define crange_from2(start, end) crange_from3(start, end, 1)
+STC_INLINE crange crange_from3(crange_value start, crange_value finish, crange_value step)
+ { crange r = {start, finish - (step > 0), step}; return r; }
+STC_INLINE crange_iter crange_begin(crange* self)
+ { crange_iter it = {&self->val, self->end, self->step}; return it; }
+STC_INLINE crange_iter crange_end(crange* self)
+ { crange_iter it = {NULL}; return it; }
+STC_INLINE void crange_next(crange_iter* it)
+ { *it->ref += it->step; if ((it->step > 0) == (*it->ref > it->end)) it->ref = NULL; }
+
#define c_forlist(it, T, ...) \
for (struct {T* data; T* ref; size_t index;} \
it = {.data=(T[])__VA_ARGS__, .ref=it.data} \
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index 423464ac..2b8513e1 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -110,7 +110,7 @@ STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
STC_INLINE void _cx_memb(_next)(_cx_iter* it)
{ if (++it->ref == it->end) it->ref = NULL; }
-STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, isize_t n)
+STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t n)
{ if ((it.ref += n) >= it.end) it.ref = NULL; return it; }
#if !defined _i_queue
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index 9126cdef..2f9b2626 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -180,7 +180,7 @@ STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
STC_INLINE void _cx_memb(_next)(_cx_iter* it)
{ if (++it->ref == it->end) it->ref = NULL; }
-STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, isize_t n)
+STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t n)
{ if ((it.ref += n) >= it.end) it.ref = NULL ; return it; }
#include "template.h"
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index 961ca344..7b382085 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -48,7 +48,7 @@
enum { cstr_s_cap = sizeof(cstr_buf) - 1 };
#define cstr_s_size(s) ((size_t)(cstr_s_cap - (s)->sml.last))
-#define cstr_s_set_size(s, len) ((s)->sml.last = cstr_s_cap - (len), (s)->sml.data[len] = 0)
+#define cstr_s_set_size(s, len) ((s)->sml.last = (uint8_t)(cstr_s_cap - (len)), (s)->sml.data[len] = 0)
#define cstr_s_data(s) (s)->sml.data
#define cstr_s_end(s) ((s)->sml.data + cstr_s_size(s))
@@ -237,7 +237,7 @@ STC_INLINE void cstr_next(cstr_iter* it) {
it->u8.chr.size = utf8_chr_size(it->ref);
if (!*it->ref) it->ref = NULL;
}
-STC_INLINE cstr_iter cstr_advance(cstr_iter it, isize_t pos) {
+STC_INLINE cstr_iter cstr_advance(cstr_iter it, intptr_t pos) {
int inc = -1;
if (pos > 0) pos = -pos, inc = 1;
while (pos && *it.ref) pos += (*(it.ref += inc) & 0xC0) != 0x80;
diff --git a/include/stc/csview.h b/include/stc/csview.h
index 3bb8a542..2be30cb0 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -86,7 +86,7 @@ STC_INLINE void csview_next(csview_iter* it) {
it->u8.chr.size = utf8_chr_size(it->ref);
if (it->ref == it->u8.end) it->ref = NULL;
}
-STC_INLINE csview_iter csview_advance(csview_iter it, isize_t pos) {
+STC_INLINE csview_iter csview_advance(csview_iter it, intptr_t pos) {
int inc = -1;
if (pos > 0) pos = -pos, inc = 1;
while (pos && it.ref != it.u8.end) pos += (*(it.ref += inc) & 0xC0) != 0x80;
@@ -145,7 +145,7 @@ STC_INLINE csview cstr_u8_substr(const cstr* self , size_t bytepos, size_t u8len
STC_INLINE int csview_cmp(const csview* x, const csview* y) {
size_t n = x->size < y->size ? x->size : y->size;
int c = memcmp(x->str, y->str, n);
- return c ? c : x->size - y->size;
+ return c ? c : (int)(x->size - y->size);
}
STC_INLINE int csview_icmp(const csview* x, const csview* y)
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index 4419f4a8..aeb17e1a 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -207,7 +207,7 @@ STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
STC_INLINE void _cx_memb(_next)(_cx_iter* it)
{ if (++it->ref == it->end) it->ref = NULL; }
-STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, isize_t n)
+STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t n)
{ if ((it.ref += n) >= it.end) it.ref = NULL; return it; }
STC_INLINE size_t _cx_memb(_index)(const _cx_self* cx, _cx_iter it)