From 4021220d0b4c6508ec7c500c32b133459b6ad51e Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 1 Sep 2020 08:50:14 +0200 Subject: Added back splice_front()/back(), updated ex_gaussian.c --- examples/ex_gaussian.c | 8 +++---- examples/list.c | 4 ++-- stc/clist.h | 59 +++++++++++++++++++++++++++++--------------------- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c index 62ab7994..dcb78d7b 100644 --- a/examples/ex_gaussian.c +++ b/examples/ex_gaussian.c @@ -18,15 +18,15 @@ declare_cvec(e, cmap_i_entry_t, c_default_destroy, compare); int main() { - enum {Rows = 40, N = 5000000}; - const double StdDev = 6.0, Mag = 35.0, Mean = 12; + enum {N = 10000000}; + const double Mean = 12.0, StdDev = 8.0, Mag = 12000.0 / StdDev; printf("Demo of gaussian / normal distribution of %d random samples\n", N); // Setup random engine with normal distribution. uint64_t seed = time(NULL); crand_rng64_t rng = crand_rng64_init(seed); - crand_normal_f64_t dist = crand_normal_f64_init(rng, Mean, Rows / StdDev); + crand_normal_f64_t dist = crand_normal_f64_init(rng, Mean, StdDev); // Create histogram map cmap_i mhist = cmap_init; @@ -44,7 +44,7 @@ int main() // Print the gaussian bar chart cstr_t bar = cstr_init; c_foreach (i, cvec_e, vhist) { - size_t n = (size_t) (i.item->value * Mag * Rows / N); + size_t n = (size_t) (i.item->value * Mag / N); if (n > 0) { // bar string: take ownership in new str after freeing current. cstr_take(&bar, cstr_with_size(n, '*')); diff --git a/examples/list.c b/examples/list.c index fe81e5e2..3aff4482 100644 --- a/examples/list.c +++ b/examples/list.c @@ -28,8 +28,8 @@ int main() { puts(""); int removed = clist_fx_remove(&list, 30); - clist_fx_insert_after(&list, clist_fx_ahead(&list), 5); - clist_fx_insert_after(&list, clist_fx_last(&list), 500); + clist_fx_insert_after(&list, clist_fx_before_begin(&list), 5); // same as push_front() + clist_fx_insert_after(&list, clist_fx_last(&list), 500); // same as push_back() clist_fx_push_front(&list, 1964); c_foreach (i, clist_fx, list) printf(" %g", i.item->value); diff --git a/stc/clist.h b/stc/clist.h index 3a972a96..0b8a6bde 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -30,7 +30,7 @@ This implements a std::forward_list-like class in C, but because it is circular, it also support push* and splice* at both ends of the list. This makes it ideal - for being used as a queue, unlike std::forward_list. Basic usage is similar to CVec: + for being used as a queue, unlike std::forward_list. Basic usage is similar to cvec: #include #include @@ -100,6 +100,9 @@ clist_##tag##_destroy(clist_##tag* self); \ STC_INLINE void \ clist_##tag##_clear(clist_##tag* self) {clist_##tag##_destroy(self);} \ + \ + STC_API void \ + clist_##tag##_push_n(clist_##tag *self, const clist_##tag##_input_t in[], size_t size); \ STC_API void \ clist_##tag##_push_back_v(clist_##tag* self, Value value); \ STC_INLINE void \ @@ -113,9 +116,27 @@ clist_##tag##_push_front_v(self, valueFromRaw(rawValue)); \ } \ STC_API void \ - clist_##tag##_push_n(clist_##tag *self, const clist_##tag##_input_t in[], size_t size); \ - STC_API void \ clist_##tag##_pop_front(clist_##tag* self); \ + \ + 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##_begin(clist_##tag* self) { \ + clist_##tag##_node_t *head = self->last ? self->last->next : NULL; \ + clist_##tag##_iter_t it = {head, NULL, &self->last}; return it; \ + } \ + STC_INLINE clist_##tag##_iter_t \ + clist_##tag##_last(clist_##tag* self) { \ + clist_##tag##_iter_t it = {self->last, NULL, &self->last}; return it; \ + } \ + STC_INLINE void \ + clist_##tag##_next(clist_##tag##_iter_t* it) { \ + if (it->item == *it->_last) it->end = it->item = it->item->next; \ + else it->item = it->item->next; \ + } \ + \ STC_API clist_##tag##_iter_t \ clist_##tag##_insert_after_v(clist_##tag* self, clist_##tag##_iter_t pos, Value value); \ STC_INLINE clist_##tag##_iter_t \ @@ -124,10 +145,20 @@ } \ STC_API clist_##tag##_iter_t \ clist_##tag##_erase_after(clist_##tag* self, clist_##tag##_iter_t pos); \ + \ 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_INLINE void \ + clist_##tag##_splice_front(clist_##tag* self, clist_##tag* other) { \ + clist_##tag##_splice_after(self, clist_##tag##_before_begin(self), other); \ + } \ + STC_INLINE void \ + clist_##tag##_splice_back(clist_##tag* self, clist_##tag* other) { \ + clist_##tag##_splice_after(self, clist_##tag##_last(self), other); \ + } \ + \ STC_API clist_##tag##_iter_t \ clist_##tag##_find_before(clist_##tag* self, clist_##tag##_iter_t prev, RawValue val); \ STC_API Value* \ @@ -141,28 +172,6 @@ clist_##tag##_front(clist_##tag* self) {return &self->last->next->value;} \ STC_INLINE Value* \ clist_##tag##_back(clist_##tag* self) {return &self->last->value;} \ - \ - STC_INLINE clist_##tag##_iter_t \ - clist_##tag##_begin(clist_##tag* self) { \ - clist_##tag##_node_t *head = self->last ? self->last->next : NULL; \ - clist_##tag##_iter_t it = {head, NULL, &self->last}; return it; \ - } \ - 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##_ahead(clist_##tag* self) {return clist_##tag##_before_begin(self);} \ - \ - STC_INLINE clist_##tag##_iter_t \ - clist_##tag##_last(clist_##tag* self) { \ - clist_##tag##_iter_t it = {self->last, NULL, &self->last}; return it; \ - } \ - STC_INLINE void \ - clist_##tag##_next(clist_##tag##_iter_t* it) { \ - if (it->item == *it->_last) it->end = it->item = it->item->next; \ - else it->item = it->item->next; \ - } \ \ implement_clist_7(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueToRaw, valueFromRaw) \ typedef Value clist_##tag##_value_t -- cgit v1.2.3