From ad4c5377cf7e2f5812e2053e51550239fcd38d45 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 10 Sep 2021 19:01:45 +0200 Subject: Added support for cqueue.h Added test. --- include/stc/cqueue.h | 94 ++++++++++++++++++---------------------------------- 1 file changed, 33 insertions(+), 61 deletions(-) (limited to 'include/stc/cqueue.h') diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h index 596f50a9..454913c1 100644 --- a/include/stc/cqueue.h +++ b/include/stc/cqueue.h @@ -20,74 +20,46 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef CQUEUE_H_INCLUDED -#define CQUEUE_H_INCLUDED +// STC queue +/* +#include +#include -/* Queue adapter, default uses clist. +#define i_val int +#include - #include - #include - using_cdeq(i, int); - using_cqueue(i, cdeq_i); +int main() { + int n = 10000000; + stc64_t rng = stc64_init(1234); + stc64_uniform_t dist = stc64_uniform_init(0, n); - int main() { - int n = 10000000; - stc64_t rng = stc64_init(1234); - stc64_uniform_t dist = stc64_uniform_init(rng, 0, n); + c_forauto (cqueue_int, Q) + { + // Push ten million random numbers onto the queue. + for (int i=0; i0; --i) { - int r = stc64_uniform(&dist); - if (r & 1) - ++n, cqueue_i_push(&queue, r); - else - --n, cqueue_i_pop(&queue); - } + // Push or pop on the queue ten million times + printf("before: size, capacity: %d, %d\n", n, cqueue_int_size(Q), cqueue_int_capacity(Q)); + for (int i=n; i>0; --i) { + int r = stc64_uniform(&rng, &dist); + if (r & 1) + ++n, cqueue_int_push(&Q, r); + else + --n, cqueue_int_pop(&Q); } - printf("%d\n", n); + printf("after: size, capacity: %d, %d\n", n, cqueue_int_size(Q), cqueue_int_capacity(Q)); } +} */ -#include "cdeq.h" -#define using_cqueue(X, ctype) \ - _c_using_cqueue(cqueue_##X, ctype) +#define i_module cqueue +#define i_queue +#define _push_back _push +#define _pop_front _pop -#define _c_using_cqueue(Self, ctype) \ - typedef struct { ctype rep; size_t size; } Self; \ - typedef ctype##_value_t cx_value_t; \ - typedef ctype##_rawvalue_t cx_rawvalue_t; \ - typedef ctype##_iter_t cx_iter_t; \ -\ - STC_INLINE Self cx_memb(_init)(void) { return c_make(Self){ctype##_init(), 0}; } \ - STC_INLINE Self cx_memb(_clone)(Self q) { return c_make(Self){ctype##_clone(q.rep), q.size}; } \ - STC_INLINE cx_value_t cx_memb(_value_clone)(cx_value_t val) \ - { return ctype##_value_clone(val); } \ - STC_INLINE void cx_memb(_clear)(Self* self) {ctype##_clear(&self->rep); self->size = 0; } \ - STC_INLINE void cx_memb(_del)(Self* self) {ctype##_del(&self->rep); } \ -\ - STC_INLINE size_t cx_memb(_size)(Self q) { return q.size; } \ - STC_INLINE bool cx_memb(_empty)(Self q) { return q.size == 0; } \ - STC_INLINE cx_value_t* cx_memb(_front)(const Self* self) { return ctype##_front(&self->rep); } \ - STC_INLINE cx_value_t* cx_memb(_back)(const Self* self) { return ctype##_back(&self->rep); } \ -\ - STC_INLINE void cx_memb(_pop)(Self* self) {ctype##_pop_front(&self->rep); --self->size; } \ - STC_INLINE void cx_memb(_push)(Self* self, ctype##_value_t value) \ - {ctype##_push_back(&self->rep, value); ++self->size; } \ - STC_INLINE void cx_memb(_emplace)(Self* self, cx_rawvalue_t raw) \ - {ctype##_emplace_back(&self->rep, raw); ++self->size; } \ - STC_INLINE void cx_memb(_emplace_items)(Self *self, const cx_rawvalue_t arr[], size_t n) \ - {ctype##_emplace_items(&self->rep, arr, n); self->size += n; } \ -\ - STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) { return ctype##_begin(&self->rep); } \ - STC_INLINE cx_iter_t cx_memb(_end)(const Self* self) { return ctype##_end(&self->rep); } \ - STC_INLINE void cx_memb(_next)(cx_iter_t* it) {ctype##_next(it); } \ - struct stc_trailing_semicolon +#include "cdeq.h" -#endif +#undef _push_back +#undef _pop_front +#undef i_queue -- cgit v1.2.3