diff options
| author | Tyge Løvset <[email protected]> | 2020-09-01 08:50:14 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-09-01 08:50:14 +0200 |
| commit | 4021220d0b4c6508ec7c500c32b133459b6ad51e (patch) | |
| tree | 60f43b7509021f25c11b8bbfb4f1e6e0003d7513 | |
| parent | 8c4daa7f91d9a6e7a30e0f6dfdafaf2664d68f2d (diff) | |
| download | STC-modified-4021220d0b4c6508ec7c500c32b133459b6ad51e.tar.gz STC-modified-4021220d0b4c6508ec7c500c32b133459b6ad51e.zip | |
Added back splice_front()/back(), updated ex_gaussian.c
| -rw-r--r-- | examples/ex_gaussian.c | 8 | ||||
| -rw-r--r-- | examples/list.c | 4 | ||||
| -rw-r--r-- | 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 <stdio.h>
#include <stc/clist.h>
@@ -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* \
@@ -142,28 +173,6 @@ 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
|
