summaryrefslogtreecommitdiffhomepage
path: root/include/stc/cqueue.h
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-09-10 19:01:45 +0200
committerTyge Løvset <[email protected]>2021-09-10 19:01:45 +0200
commitad4c5377cf7e2f5812e2053e51550239fcd38d45 (patch)
treebb74419144b0326ec58458650b557e5b243ae27b /include/stc/cqueue.h
parent2dcac231b5eea841f7bc607f0ddbc729bc41a9e5 (diff)
downloadSTC-modified-ad4c5377cf7e2f5812e2053e51550239fcd38d45.tar.gz
STC-modified-ad4c5377cf7e2f5812e2053e51550239fcd38d45.zip
Added support for cqueue.h Added test.
Diffstat (limited to 'include/stc/cqueue.h')
-rw-r--r--include/stc/cqueue.h94
1 files changed, 33 insertions, 61 deletions
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 <stc/crandom.h>
+#include <stdio.h>
-/* Queue adapter, default uses clist.
+#define i_val int
+#include <stc/cqueue.h>
- #include <stc/crandom.h>
- #include <stc/cqueue.h>
- 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; i<n; ++i)
+ cqueue_int_push(&Q, stc64_uniform(&rng, &dist));
- c_forvar (cqueue_i queue = cqueue_i_init(), cqueue_i_del(&queue))
- {
- // Push ten million random numbers onto the queue.
- for (int i=0; i<n; ++i)
- cqueue_i_push(&queue, stc64_uniform(&dist));
-
- // Push or pop on the queue ten million times
- printf("%d\n", n);
- for (int i=n; i>0; --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