summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortylo <[email protected]>2020-08-31 17:18:40 +0200
committertylo <[email protected]>2020-08-31 17:18:40 +0200
commit895ef5181d03a628d654aa8425aa05fa583f5bf9 (patch)
tree1ebeb03e8196da198f3ff9cd4ae0a270ff0b4725
parent83a648c1ea1ed71b767779ac604833bc2f4ef200 (diff)
downloadSTC-modified-895ef5181d03a628d654aa8425aa05fa583f5bf9.tar.gz
STC-modified-895ef5181d03a628d654aa8425aa05fa583f5bf9.zip
Updated clist iterators.
-rw-r--r--examples/list.c14
-rw-r--r--stc/clist.h58
2 files changed, 38 insertions, 34 deletions
diff --git a/examples/list.c b/examples/list.c
index c9ec4a16..0da52c9b 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -20,11 +20,19 @@ int main() {
k = 0; c_foreach (i, clist_fx, list)
if (++k <= 10) printf("%8d: %10f\n", k, i.item->value); else break;
+ puts("");
clist_fx_clear(&list);
- c_push(&list, clist_fx, c_items(10, 20, 30, 40, 50));
- c_foreach (i, clist_fx, list) printf("%.1f ", i.item->value);
+ c_push(&list, clist_fx, c_items(10, 20, 30, 40, 30, 50));
+ c_foreach (i, clist_fx, list) printf(" %g", i.item->value);
+ puts("");
+
+ int removed = clist_fx_remove(&list, 30);
+ clist_fx_insert_after(&list, clist_fx_last(&list), 1000);
+ //clist_fx_insert_after(&list, clist_fx_before_begin(&list), 5);
+ clist_fx_push_front(&list, 5);
+ c_foreach (i, clist_fx, list)
+ printf(" %g", i.item->value);
puts("");
-
clist_fx_destroy(&list);
} \ No newline at end of file
diff --git a/stc/clist.h b/stc/clist.h
index 153da566..d17ddcec 100644
--- a/stc/clist.h
+++ b/stc/clist.h
@@ -123,15 +123,15 @@
} \
STC_API clist_##tag##_iter_t \
clist_##tag##_erase_after(clist_##tag* self, clist_##tag##_iter_t pos); \
- STC_API void \
- clist_##tag##_splice_front(clist_##tag* self, clist_##tag* other); \
- STC_API void \
- clist_##tag##_splice_after(clist_##tag* self, clist_##tag##_iter_t pos, clist_##tag* other); \
+ STC_INLINE void \
+ clist_##tag##_splice_after(clist_##tag* self, clist_##tag##_iter_t pos, clist_##tag* other) { \
+ _clist_splice_after((clist_void *) self, *(clist_void_iter_t *) &pos, (clist_void *) other); \
+ } \
STC_API clist_##tag##_iter_t \
- clist_##tag##_find_before(clist_##tag* self, RawValue val); \
+ clist_##tag##_find_before(clist_##tag* self, clist_##tag##_iter_t prev, RawValue val); \
STC_API Value* \
clist_##tag##_find(clist_##tag* self, RawValue val); \
- STC_API clist_##tag##_iter_t \
+ STC_API size_t \
clist_##tag##_remove(clist_##tag* self, RawValue val); \
STC_API void \
clist_##tag##_sort(clist_##tag* self); \
@@ -148,7 +148,12 @@
} \
STC_INLINE void \
clist_##tag##_next(clist_##tag##_iter_t* it) { \
- it->item = it->item == *it->_last ? NULL : it->item->next; \
+ if (it->item == *it->_last) it->end = it->item = it->item->next; \
+ else it->item = it->item->next; \
+ } \
+ STC_INLINE clist_##tag##_iter_t \
+ clist_##tag##_before_begin(clist_##tag* self) { \
+ clist_##tag##_iter_t it = {self->last, self->last, &self->last}; return it; \
} \
STC_INLINE clist_##tag##_iter_t \
clist_##tag##_last(clist_##tag* self) { \
@@ -192,7 +197,7 @@
STC_API clist_##tag##_iter_t \
clist_##tag##_insert_after_v(clist_##tag* self, clist_##tag##_iter_t pos, Value value) { \
_clist_insert_after(self, tag, pos.item, value); \
- if (!self->last || pos.item == self->last) self->last = entry; \
+ if (pos.item != pos.end) self->last = entry; \
pos.item = entry; return pos; \
} \
STC_API clist_##tag##_iter_t \
@@ -201,41 +206,32 @@
clist_##tag##_next(&pos); return pos; \
} \
\
- STC_API void \
- clist_##tag##_splice_front(clist_##tag* self, clist_##tag* other) { \
- clist_void *s = (clist_void *) self; \
- clist_void_iter_t last = {s->last, NULL, &s->last}; \
- _clist_splice(s, last, (clist_void *)other, false); \
- } \
- STC_API void \
- clist_##tag##_splice_after(clist_##tag* self, clist_##tag##_iter_t pos, clist_##tag* other) { \
- _clist_splice((clist_void *)self, *(clist_void_iter_t *) &pos, (clist_void *)other, true); \
- } \
- \
STC_API clist_##tag##_iter_t \
- clist_##tag##_find_before(clist_##tag* self, RawValue val) { \
- clist_##tag##_iter_t prev = {self->last, NULL, &self->last}; \
- c_foreach (i, clist_##tag, *self) { \
+ clist_##tag##_find_before(clist_##tag* self, clist_##tag##_iter_t prev, RawValue val) { \
+ clist_##tag##_iter_t i = prev; \
+ if (i.item) i.item = i.item->next; \
+ for (; i.item != i.end; clist_##tag##_next(&i)) { \
RawValue r = valueToRaw(&i.item->value); \
if (valueCompareRaw(&r, &val) == 0) \
return prev; \
prev = i; \
} \
- prev.item = NULL; \
+ prev.item = prev.end = NULL; \
return prev; \
} \
\
STC_API Value* \
clist_##tag##_find(clist_##tag* self, RawValue val) { \
- clist_##tag##_iter_t it = clist_##tag##_find_before(self, val); \
+ clist_##tag##_iter_t it = clist_##tag##_find_before(self, clist_##tag##_before_begin(self), val); \
return it.item ? &it.item->next->value : NULL; \
} \
\
- STC_API clist_##tag##_iter_t \
+ STC_API size_t \
clist_##tag##_remove(clist_##tag* self, RawValue val) { \
- clist_##tag##_iter_t it = clist_##tag##_find_before(self, val); \
- if (it.item) clist_##tag##_erase_after(self, it); \
- return it; \
+ clist_##tag##_iter_t it; size_t n = 0; \
+ while ((it = clist_##tag##_find_before(self, clist_##tag##_before_begin(self), val)).item) \
+ it = clist_##tag##_erase_after(self, it), ++n; \
+ return n; \
} \
\
static inline int \
@@ -270,14 +266,14 @@
declare_clist_types(void, int);
STC_API void \
-_clist_splice(clist_void* self, clist_void_iter_t pos, clist_void* other, bool bottom) {
+_clist_splice_after(clist_void* self, clist_void_iter_t pos, clist_void* other) {
if (!pos.item)
- self->last = pos.item = other->last;
+ self->last = other->last;
else if (other->last) {
clist_void_node_t *next = pos.item->next;
pos.item->next = other->last->next;
other->last->next = next;
- if (bottom && pos.item == self->last) self->last = other->last;
+ if (pos.item == pos.end) self->last = other->last;
}
other->last = NULL;
}