summaryrefslogtreecommitdiffhomepage
path: root/stc/cpqueue.h
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-09-02 19:49:51 +0200
committerTyge Løvset <[email protected]>2020-09-02 19:49:51 +0200
commitaf12926547b1a09b4513ba149d13ea9f84d4c528 (patch)
tree954ab763d10a2c5558163645004b9c11ed63b5d1 /stc/cpqueue.h
parent1b46028f4636c78af40c37dbc55d76598996a5b7 (diff)
downloadSTC-modified-af12926547b1a09b4513ba149d13ea9f84d4c528.tar.gz
STC-modified-af12926547b1a09b4513ba149d13ea9f84d4c528.zip
Added cqueue.h and cstack.h adapters. Updated cpqueue.h API to be more consistent.
Diffstat (limited to 'stc/cpqueue.h')
-rw-r--r--stc/cpqueue.h56
1 files changed, 28 insertions, 28 deletions
diff --git a/stc/cpqueue.h b/stc/cpqueue.h
index 15fd980a..b2c81478 100644
--- a/stc/cpqueue.h
+++ b/stc/cpqueue.h
@@ -26,19 +26,19 @@
#include <stc/crandom.h>
#include <stc/cpqueue.h>
declare_cvec(f, float);
- declare_cpqueue(f, >, cvec); // min-heap (increasing values)
+ declare_cpqueue(f, cvec_f, >); // min-heap (increasing values)
int main() {
- crandom_eng32_t gen = crandom_eng32_init(1234);
- crandom_uniform_f32_t dist = crandom_uniform_f32_init(10.0f, 100.0f);
+ crand_rng32_t gen = crand_rng32_init(1234);
+ crand_uniform_f32_t dist = crand_uniform_f32_init(10.0f, 100.0f);
- cpqueue_f queue = cpqueue_init;
+ cpqueue_f queue = cpqueue_f_init();
// Push ten million random numbers onto the queue.
for (int i=0; i<10000000; ++i)
- cpqueue_f_push(&queue, crandom_uniform_f32(&gen, dist));
+ cpqueue_f_push(&queue, crand_uniform_f32(&gen, dist));
// Extract the 100 smallest.
for (int i=0; i<100; ++i) {
- printf("%f ", cpqueue_f_top(queue));
+ printf("%f ", *cpqueue_f_top(queue));
cpqueue_f_pop(&queue);
}
cpqueue_f_destroy(&queue);
@@ -48,53 +48,53 @@
#ifndef CPQUEUE__H__
#define CPQUEUE__H__
-#include "cdefs.h"
+#include "cvec.h"
-#define declare_cpqueue(tag, cmpOpr, type) /* cmpOpr: < or > */ \
+#define declare_cpqueue(tag, ctype, cmpOpr) /* cmpOpr: < or > */ \
\
-typedef type##_##tag cpqueue_##tag; \
-typedef type##_##tag##_value_t cpqueue_##tag##_value_t; \
-typedef type##_##tag##_rawvalue_t cpqueue_##tag##_rawvalue_t; \
-typedef type##_##tag##_input_t cpqueue_##tag##_input_t; \
+typedef ctype cpqueue_##tag; \
+typedef ctype##_value_t cpqueue_##tag##_value_t; \
+typedef ctype##_rawvalue_t cpqueue_##tag##_rawvalue_t; \
+typedef ctype##_input_t cpqueue_##tag##_input_t; \
STC_INLINE cpqueue_##tag \
-cpqueue_##tag##_init() {return type##_##tag##_init();} \
+cpqueue_##tag##_init() {return ctype##_init();} \
STC_INLINE size_t \
-cpqueue_##tag##_size(cpqueue_##tag pq) {return type##_##tag##_size(pq);} \
+cpqueue_##tag##_size(cpqueue_##tag pq) {return ctype##_size(pq);} \
STC_INLINE bool \
-cpqueue_##tag##_empty(cpqueue_##tag pq) {return type##_##tag##_empty(pq);} \
+cpqueue_##tag##_empty(cpqueue_##tag pq) {return ctype##_empty(pq);} \
STC_INLINE void \
-cpqueue_##tag##_destroy(cpqueue_##tag* self) {type##_##tag##_destroy(self);} \
+cpqueue_##tag##_destroy(cpqueue_##tag* self) {ctype##_destroy(self);} \
STC_API void \
cpqueue_##tag##_build(cpqueue_##tag* self); \
STC_API void \
cpqueue_##tag##_erase(cpqueue_##tag* self, size_t i); \
-STC_INLINE cpqueue_##tag##_value_t \
-cpqueue_##tag##_top(cpqueue_##tag pq) {return pq.data[0];} \
+STC_INLINE const cpqueue_##tag##_value_t* \
+cpqueue_##tag##_top(const cpqueue_##tag* self) {return &self->data[0];} \
STC_INLINE void \
cpqueue_##tag##_pop(cpqueue_##tag* self) {cpqueue_##tag##_erase(self, 0);} \
STC_API void \
cpqueue_##tag##_push_v(cpqueue_##tag* self, cpqueue_##tag##_value_t value); \
STC_INLINE void \
cpqueue_##tag##_push(cpqueue_##tag* self, cpqueue_##tag##_rawvalue_t rawValue) { \
- cpqueue_##tag##_push_v(self, type##_##tag##_value_from_raw(rawValue)); \
+ cpqueue_##tag##_push_v(self, ctype##_value_from_raw(rawValue)); \
} \
STC_API void \
cpqueue_##tag##_push_n(cpqueue_##tag *self, const cpqueue_##tag##_input_t in[], size_t size); \
\
-implement_cpqueue(tag, cmpOpr, type)
+implement_cpqueue(tag, ctype, cmpOpr)
/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-#define implement_cpqueue(tag, cmpOpr, type) \
+#define implement_cpqueue(tag, ctype, cmpOpr) \
\
STC_INLINE void \
_cpqueue_##tag##_sift_down(cpqueue_##tag##_value_t* arr, size_t i, size_t n) { \
size_t r = i, c = i << 1; \
while (c <= n) { \
- if (c < n && type##_##tag##_value_compare(&arr[c], &arr[c + 1]) cmpOpr 0) \
+ if (c < n && ctype##_value_compare(&arr[c], &arr[c + 1]) cmpOpr 0) \
++c; \
- if (type##_##tag##_value_compare(&arr[r], &arr[c]) cmpOpr 0) { \
+ if (ctype##_value_compare(&arr[r], &arr[c]) cmpOpr 0) { \
cpqueue_##tag##_value_t t = arr[r]; arr[r] = arr[c]; arr[r = c] = t; \
} else \
return; \
@@ -114,28 +114,28 @@ STC_API void \
cpqueue_##tag##_erase(cpqueue_##tag* self, size_t i) { \
size_t n = cpqueue_##tag##_size(*self) - 1; \
self->data[i] = self->data[n]; \
- type##_##tag##_pop_back(self); \
+ ctype##_pop_back(self); \
_cpqueue_##tag##_sift_down(self->data - 1, i + 1, n); \
} \
\
STC_API void \
cpqueue_##tag##_push_v(cpqueue_##tag* self, cpqueue_##tag##_value_t value) { \
- type##_##tag##_push_back(self, value); /* sift-up the value */ \
+ ctype##_push_back(self, value); /* sift-up the value */ \
size_t n = cpqueue_##tag##_size(*self), c = n; \
cpqueue_##tag##_value_t *arr = self->data - 1; \
- for (; c > 1 && type##_##tag##_value_compare(&arr[c >> 1], &value) cmpOpr 0; c >>= 1) \
+ for (; c > 1 && ctype##_value_compare(&arr[c >> 1], &value) cmpOpr 0; c >>= 1) \
arr[c] = arr[c >> 1]; \
if (c != n) arr[c] = value; \
} \
STC_API void \
cpqueue_##tag##_push_n(cpqueue_##tag *self, const cpqueue_##tag##_input_t in[], size_t size) { \
- type##_##tag##_reserve(self, cpqueue_##tag##_size(*self) + size); \
+ ctype##_reserve(self, cpqueue_##tag##_size(*self) + size); \
for (size_t i=0; i<size; ++i) cpqueue_##tag##_push(self, in[i]); \
} \
typedef int cpqueue_##tag##_dud
#else
-#define implement_cpqueue(tag, cmpOpr, type)
+#define implement_cpqueue(tag, ctype, cmpOpr)
#endif
#endif