summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-08-09 17:34:13 +0200
committerTyge Løvset <[email protected]>2022-08-09 17:34:13 +0200
commit90624d6d398ff1d0f79df3dd656c4ad0c9c498a9 (patch)
tree8e45f6d00189a261c6a54979010f634df2af0d49 /include
parent17f1d2ed83952df00525f4be1d30a6c12e04a0f6 (diff)
downloadSTC-modified-90624d6d398ff1d0f79df3dd656c4ad0c9c498a9.tar.gz
STC-modified-90624d6d398ff1d0f79df3dd656c4ad0c9c498a9.zip
Experiment with other type of iterator. Does not compile.
Diffstat (limited to 'include')
-rw-r--r--include/stc/cmap.h18
-rw-r--r--include/stc/cstack.h6
-rw-r--r--include/stc/cstr.h6
-rw-r--r--include/stc/csview.h4
-rw-r--r--include/stc/cvec.h29
-rw-r--r--include/stc/forward.h14
6 files changed, 37 insertions, 40 deletions
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index 84957e69..1394b3a7 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -192,8 +192,8 @@ STC_INLINE _cx_iter
_cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) {
i_size idx;
if (!(self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]))
- idx = self->bucket_count;
- return c_make(_cx_iter){self->table+idx, self->_hashx+idx};
+ return c_make(_cx_iter){NULL};
+ return c_make(_cx_iter){self->table+idx, self->table+self->bucket_count, self->_hashx+idx};
}
STC_INLINE const _cx_value*
@@ -210,25 +210,27 @@ _cx_memb(_get_mut)(_cx_self* self, _cx_rawkey rkey)
STC_INLINE _cx_iter
_cx_memb(_begin)(const _cx_self* self) {
- _cx_iter it = {self->table, self->_hashx};
+ _cx_iter it = {self->table, self->table+self->bucket_count, self->_hashx};
if (it._hx)
while (*it._hx == 0)
++it.ref, ++it._hx;
+ if (it.ref == it._end) it.ref = NULL;
return it;
}
STC_INLINE _cx_iter
_cx_memb(_end)(const _cx_self* self)
- { return c_make(_cx_iter){self->table + self->bucket_count}; }
+ { return c_make(_cx_iter){NULL}; }
STC_INLINE void
-_cx_memb(_next)(_cx_iter* it)
- { while ((++it->ref, *++it->_hx == 0)) ; }
+_cx_memb(_next)(_cx_iter* it) {
+ while ((++it->ref, *++it->_hx == 0)) ;
+ if (it->ref == it->_end) it->ref = NULL;
+}
STC_INLINE _cx_iter
_cx_memb(_advance)(_cx_iter it, size_t n) {
- // UB if n > elements left
- while (n--) _cx_memb(_next)(&it);
+ while (n-- && it.ref) _cx_memb(_next)(&it);
return it;
}
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index 46d209ca..3986e366 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -170,12 +170,12 @@ STC_INLINE i_keyraw _cx_memb(_value_toraw)(const _cx_value* val)
#endif // !_i_no_clone
STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self)
- { return c_make(_cx_iter){(_cx_value*)self->data}; }
+ { return c_make(_cx_iter){self->size ? (_cx_value*)self->data : NULL, (_cx_value*)self->data + self->size}; }
STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
- { return c_make(_cx_iter){(_cx_value*)self->data + self->size}; }
+ { return c_make(_cx_iter){NULL}; }
-STC_INLINE void _cx_memb(_next)(_cx_iter* it) { ++it->ref; }
+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, intptr_t offs)
{ it.ref += offs; return it; }
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index aae52ea0..2dbea85a 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -224,15 +224,15 @@ STC_INLINE csview cstr_u8_chr(const cstr* self, size_t u8idx) {
STC_INLINE cstr_iter cstr_begin(const cstr* self) {
const char* str = cstr_str(self);
- return c_make(cstr_iter){.chr = {str, utf8_chr_size(str)}};
+ return c_make(cstr_iter){.u8 = {{str, utf8_chr_size(str)}}};
}
STC_INLINE cstr_iter cstr_end(const cstr* self) {
csview sv = cstr_sv(self);
return c_make(cstr_iter){sv.str + sv.size};
}
STC_INLINE void cstr_next(cstr_iter* it) {
- it->ref += it->chr.size;
- it->chr.size = utf8_chr_size(it->ref);
+ it->ref += it->u8.chr.size;
+ it->u8.chr.size = utf8_chr_size(it->ref);
}
diff --git a/include/stc/csview.h b/include/stc/csview.h
index 2efeff5b..ef545bf2 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -71,13 +71,13 @@ STC_INLINE csview csview_slice(csview sv, size_t p1, size_t p2) {
/* iterator */
STC_INLINE csview_iter csview_begin(const csview* self)
- { return c_make(csview_iter){.chr = {self->str, utf8_chr_size(self->str)}}; }
+ { return c_make(csview_iter){.u8 = {{self->str, utf8_chr_size(self->str)}}}; }
STC_INLINE csview_iter csview_end(const csview* self)
{ return c_make(csview_iter){self->str + self->size}; }
STC_INLINE void csview_next(csview_iter* it)
- { it->ref += it->chr.size; it->chr.size = utf8_chr_size(it->ref); }
+ { it->ref += it->u8.chr.size; it->u8.chr.size = utf8_chr_size(it->ref); }
/* utf8 */
STC_INLINE size_t csview_u8_size(csview sv)
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index 81678293..a4308628 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -141,12 +141,11 @@ STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value)
{ return _cx_memb(_push)(self, value); }
STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); }
STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self)
- { return c_make(_cx_iter){self->data}; }
-STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
- { return c_make(_cx_iter){self->data + cvec_rep_(self)->size}; }
-STC_INLINE void _cx_memb(_next)(_cx_iter* it) { ++it->ref; }
-STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t offs)
- { it.ref += offs; return it; }
+ { size_t n = cvec_rep_(self)->size; return c_make(_cx_iter){n ? self->data : NULL, self->data + n}; }
+STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) { return c_make(_cx_iter){NULL}; }
+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, size_t offs)
+ { if ((it.ref += offs) >= it._end) it.ref = NULL; return it; }
STC_INLINE size_t _cx_memb(_index)(const _cx_self* cx, _cx_iter it) { return it.ref - cx->data; }
STC_INLINE _cx_self
@@ -217,9 +216,7 @@ _cx_memb(_find)(const _cx_self* self, _cx_raw raw) {
STC_INLINE const _cx_value*
_cx_memb(_get)(const _cx_self* self, _cx_raw raw) {
- _cx_iter end = _cx_memb(_end)(self);
- _cx_value* val = _cx_memb(_find)(self, raw).ref;
- return val == end.ref ? NULL : val;
+ return _cx_memb(_find)(self, raw).ref;
}
STC_INLINE _cx_value*
@@ -354,14 +351,12 @@ _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos,
STC_DEF _cx_iter
_cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) {
intptr_t len = p2 - p1;
- if (len > 0) {
- _cx_value* p = p1, *end = self->data + cvec_rep_(self)->size;
- for (; p != p2; ++p)
- { i_keydrop(p); }
- memmove(p1, p2, (end - p2) * sizeof(i_key));
- cvec_rep_(self)->size -= len;
- }
- return c_make(_cx_iter){.ref = p1};
+ _cx_value* p = p1, *end = self->data + cvec_rep_(self)->size;
+ for (; p != p2; ++p)
+ { i_keydrop(p); }
+ memmove(p1, p2, (end - p2) * sizeof *p1);
+ cvec_rep_(self)->size -= len;
+ return c_make(_cx_iter){.ref = p2 == end ? NULL : p1, end - len};
}
#if !defined _i_no_clone
diff --git a/include/stc/forward.h b/include/stc/forward.h
index 04566b98..1a85f1ed 100644
--- a/include/stc/forward.h
+++ b/include/stc/forward.h
@@ -57,7 +57,7 @@ typedef struct { const char* str; size_t size; } csview;
typedef char csview_value;
typedef union {
const char *ref;
- csview chr;
+ struct { csview chr; const char *_end; } u8;
} csview_iter, cstr_iter;
#define c_true(...) __VA_ARGS__
@@ -73,12 +73,12 @@ typedef union {
#define _c_carr2_types(SELF, VAL) \
typedef VAL SELF##_value; \
- typedef struct { SELF##_value *ref; } SELF##_iter; \
+ typedef struct { SELF##_value *ref, *_end; } SELF##_iter; \
typedef struct { SELF##_value **data; size_t xdim, ydim; } SELF
#define _c_carr3_types(SELF, VAL) \
typedef VAL SELF##_value; \
- typedef struct { SELF##_value *ref; } SELF##_iter; \
+ typedef struct { SELF##_value *ref, *_end; } SELF##_iter; \
typedef struct { SELF##_value ***data; size_t xdim, ydim, zdim; } SELF
#define _c_cbox_types(SELF, VAL) \
@@ -89,7 +89,7 @@ typedef union {
#define _c_cdeq_types(SELF, VAL) \
typedef VAL SELF##_value; \
- typedef struct {SELF##_value *ref; } SELF##_iter; \
+ typedef struct {SELF##_value *ref, *_end; } SELF##_iter; \
typedef struct {SELF##_value *_base, *data;} SELF
#define _c_clist_types(SELF, VAL) \
@@ -120,7 +120,7 @@ typedef union {
} SELF##_result; \
\
typedef struct { \
- SELF##_value *ref; \
+ SELF##_value *ref, *_end; \
uint8_t* _hx; \
} SELF##_iter; \
\
@@ -186,7 +186,7 @@ typedef union {
#endif
#define _c_cstack_types(SELF, VAL) \
typedef VAL SELF##_value; \
- typedef struct { SELF##_value *ref; } SELF##_iter; \
+ typedef struct { SELF##_value *ref, *_end; } SELF##_iter; \
typedef struct SELF { \
SELF##_value* data; \
size_t size, capacity; \
@@ -209,7 +209,7 @@ typedef union {
#define _c_cvec_types(SELF, VAL) \
typedef VAL SELF##_value; \
- typedef struct { SELF##_value *ref; } SELF##_iter; \
+ typedef struct { SELF##_value *ref, *_end; } SELF##_iter; \
typedef struct { SELF##_value *data; } SELF
#endif // STC_FORWARD_H_INCLUDED