From 8eea6dfb61e9922de5940be975f4a51dcf4a62a3 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 10 Aug 2022 13:43:25 +0200 Subject: Fixed cdeq_begin(). Optimized c_foreach: only use one iter. --- include/stc/ccommon.h | 7 +++---- include/stc/cdeq.h | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'include') 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; } -- cgit v1.2.3