summaryrefslogtreecommitdiffhomepage
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
parent17f1d2ed83952df00525f4be1d30a6c12e04a0f6 (diff)
downloadSTC-modified-90624d6d398ff1d0f79df3dd656c4ad0c9c498a9.tar.gz
STC-modified-90624d6d398ff1d0f79df3dd656c4ad0c9c498a9.zip
Experiment with other type of iterator. Does not compile.
-rw-r--r--docs/csview_api.md2
-rw-r--r--examples/mmap.c29
-rw-r--r--examples/regex_replace.c2
-rw-r--r--examples/utf8replace_c.c2
-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
10 files changed, 53 insertions, 59 deletions
diff --git a/docs/csview_api.md b/docs/csview_api.md
index 09f377d1..1e2ed408 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -150,7 +150,7 @@ int main()
printf("%s\n", cstr_str(&s1));
c_foreach (i, cstr, s1)
- printf("%.*s,", c_ARGsv(i.chr));
+ printf("%.*s,", c_ARGsv(i.u8.chr));
}
}
```
diff --git a/examples/mmap.c b/examples/mmap.c
index 5ca2f92a..ed78d2af 100644
--- a/examples/mmap.c
+++ b/examples/mmap.c
@@ -4,6 +4,7 @@
// Multimap entries
#include <stc/cstr.h>
#define i_val_str
+//#define i_valdrop(x) (printf("drop %s\n", cstr_str(x)), cstr_drop(x))
#define i_extern // define _clist_mergesort() once
#include <stc/clist.h>
@@ -34,10 +35,11 @@ int main()
{
c_auto (Multimap, mmap)
{
+ typedef struct {int a; const char* b;} pair;
+
// list-initialize
- struct { int first; const char* second; } vals[] =
- {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
- c_forrange (i, c_arraylen(vals)) insert(&mmap, c_pair(&vals[i]));
+ c_forarray (pair, v, {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}})
+ insert(&mmap, v->a, v->b);
print("#1", mmap);
// insert using value_type
@@ -52,24 +54,19 @@ int main()
print("#4", mmap);
// insert using initialization_list
- insert(&mmap, 5, "one");
- insert(&mmap, 5, "two");
+ c_forarray (pair, v, {{5, "one"}, {5, "two"}})
+ insert(&mmap, v->a, v->b);
print("#5", mmap);
// FOLLOWING NOT IN ORIGINAL EXAMPLE:
-
// erase all entries with key 5
Multimap_erase(&mmap, 5);
- print("+6", mmap);
+ print("+5", mmap);
+
- // find and erase first entry containing "bar"
- clist_str_iter pos;
- c_foreach (e, Multimap, mmap) {
- if ((pos = clist_str_find(&e.ref->second, "bar")).ref != clist_str_end(&e.ref->second).ref) {
- clist_str_erase_at(&e.ref->second, pos);
- break;
- }
- }
- print("+7", mmap);
+ Multimap_clear(&mmap);
+ c_forarray (pair, v, {{1, "ä"}, {2, "ё"}, {2, "ö"}, {3, "ü"}})
+ insert(&mmap, v->a, v->b);
+ print("#6", mmap);
}
}
diff --git a/examples/regex_replace.c b/examples/regex_replace.c
index 1b140676..ccc90dba 100644
--- a/examples/regex_replace.c
+++ b/examples/regex_replace.c
@@ -36,7 +36,7 @@ int main()
/* Shows how to compile RE separately */
c_autovar (cregex re = cregex_from(pattern, 0), cregex_drop(&re)) {
if (cregex_captures(&re) == 0)
- continue;
+ c_breakauto;
/* European date format. */
cstr_take(&str, cregex_replace(input, &re, "$3.$2.$1", 0));
printf("euros: %s\n", cstr_str(&str));
diff --git a/examples/utf8replace_c.c b/examples/utf8replace_c.c
index 792654b6..1bee9b44 100644
--- a/examples/utf8replace_c.c
+++ b/examples/utf8replace_c.c
@@ -16,7 +16,7 @@ int main() {
printf("%s\n", cstr_str(&hello));
c_foreach (c, cstr, hello)
- printf("%.*s,", c_ARGsv(c.chr));
+ printf("%.*s,", c_ARGsv(c.u8.chr));
puts("");
}
}
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