summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--examples/csmap_erase.c5
-rw-r--r--include/stc/ccommon.h7
-rw-r--r--include/stc/cdeq.h2
3 files changed, 9 insertions, 5 deletions
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c
index 1c533a99..cbcc2607 100644
--- a/examples/csmap_erase.c
+++ b/examples/csmap_erase.c
@@ -49,6 +49,11 @@ int main()
printmap(m2);
mymap_iter it1 = mymap_advance(mymap_begin(&m2), 1);
mymap_iter it2 = mymap_find(&m2, mymap_back(&m2)->first);
+
+ puts("to remove:");
+ c_foreach (i, mymap, it1, it2)
+ printf(" [%d, %s]", i.ref->first, cstr_str(&i.ref->second));
+ puts("");
// The 2nd member function removes elements
// in the range [First, Last)
mymap_erase_range(&m2, it1, it2);
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index e2c63b8c..7a60b045 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -157,11 +157,10 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
#define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__)
#define c_foreach3(it, C, cnt) \
- for (C##_iter it = C##_begin(&cnt), it##_end_ = C##_end(&cnt) \
- ; it.ref != it##_end_.ref; C##_next(&it))
+ for (C##_iter it = C##_begin(&cnt); it.ref; C##_next(&it))
#define c_foreach4(it, C, start, finish) \
- for (C##_iter it = start, it##_end_ = finish \
- ; it.ref != it##_end_.ref; C##_next(&it))
+ for (C##_iter it = start, *_end_ref = (C##_iter*)(finish).ref \
+ ; it.ref != (C##_value*)_end_ref; C##_next(&it))
#define c_forpair(key, val, C, cnt) /* structured binding */ \
for (struct {C##_iter _it; C##_value* _endref; const C##_key* key; C##_mapped* val;} \
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index da7f258f..d08e0155 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -93,7 +93,7 @@ STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self)
STC_INLINE void _cx_memb(_pop_front)(_cx_self* self) // == _pop() when _i_queue
{ i_keydrop(self->data); ++self->data; --cdeq_rep_(self)->size; }
STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self)
- { return c_make(_cx_iter){self->data}; }
+ { size_t n = cdeq_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, self->data + cdeq_rep_(self)->size}; }
STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->_end) it->ref = NULL; }