diff options
| author | Tyge Løvset <[email protected]> | 2020-09-03 12:59:41 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-09-03 12:59:41 +0200 |
| commit | a4e2ee22fd57665d2388d5debc17db896a4a389f (patch) | |
| tree | 6a3a88772cd9d3b604f9def4c0579ed941983f61 /stc | |
| parent | aaa3d7f6a107a668b1f24d8ed061fe74237d9883 (diff) | |
| download | STC-modified-a4e2ee22fd57665d2388d5debc17db896a4a389f.tar.gz STC-modified-a4e2ee22fd57665d2388d5debc17db896a4a389f.zip | |
Changed constant <container>_init to <container>_ini to avoid conflict with <container>_init() method.
Reverted name cprique back to cpqueue.
Diffstat (limited to 'stc')
| -rw-r--r-- | stc/cbitset.h | 2 | ||||
| -rw-r--r-- | stc/clist.h | 6 | ||||
| -rw-r--r-- | stc/cmap.h | 12 | ||||
| -rw-r--r-- | stc/coption.h | 6 | ||||
| -rw-r--r-- | stc/cpqueue.h (renamed from stc/cprique.h) | 94 | ||||
| -rw-r--r-- | stc/cqueue.h | 4 | ||||
| -rw-r--r-- | stc/cstack.h | 4 | ||||
| -rw-r--r-- | stc/cstr.h | 29 | ||||
| -rw-r--r-- | stc/cvec.h | 8 |
9 files changed, 85 insertions, 80 deletions
diff --git a/stc/cbitset.h b/stc/cbitset.h index 31dd3f18..32659a36 100644 --- a/stc/cbitset.h +++ b/stc/cbitset.h @@ -48,7 +48,7 @@ int main() { typedef struct cbitset { uint64_t* _arr; size_t size; } cbitset_t;
-#define cbitset_init {NULL, 0}
+#define cbitset_ini {NULL, 0}
STC_API void cbitset_resize(cbitset_t* self, size_t size, bool value);
STC_API size_t cbitset_count(cbitset_t set);
diff --git a/stc/clist.h b/stc/clist.h index 39479a09..530cd222 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -38,7 +38,7 @@ declare_clist(ix, int64_t);
int main() {
- clist_ix list = clist_init;
+ clist_ix list = clist_ini;
crand_rng32_t pcg = crand_rng32_init(12345);
int n;
for (int i=0; i<1000000; ++i) // one million
@@ -82,7 +82,7 @@ clist_##tag##_node_t *item, *end, **_last; \
} clist_##tag##_iter_t
-#define clist_init {NULL}
+#define clist_ini {NULL}
#define clist_empty(list) ((list).last == NULL)
@@ -97,7 +97,7 @@ STC_API size_t _clist_size(const clist_void* self); typedef clist_##tag##_rawvalue_t clist_##tag##_input_t; \
\
STC_INLINE clist_##tag \
- clist_##tag##_init(void) {clist_##tag x = clist_init; return x;} \
+ clist_##tag##_init(void) {clist_##tag x = clist_ini; return x;} \
STC_INLINE bool \
clist_##tag##_empty(clist_##tag ls) {return clist_empty(ls);} \
STC_INLINE size_t \
@@ -28,13 +28,13 @@ declare_cset(sx, int); // Set of int declare_cmap(mx, int, char); // Map of int -> char
int main(void) {
- cset_sx s = cset_init;
+ cset_sx s = cset_ini;
cset_sx_put(&s, 5);
cset_sx_put(&s, 8);
c_foreach (i, cset_sx, s) printf("set %d\n", i.item->key);
cset_sx_destroy(&s);
- cmap_mx m = cmap_init;
+ cmap_mx m = cmap_ini;
cmap_mx_put(&m, 5, 'a');
cmap_mx_put(&m, 8, 'b');
cmap_mx_put(&m, 12, 'c');
@@ -53,11 +53,11 @@ int main(void) { #include <string.h>
#include "cdefs.h"
-#define cmap_init {NULL, NULL, 0, 0, 0.85f, 0.15f}
+#define cmap_ini {NULL, NULL, 0, 0, 0.85f, 0.15f}
#define cmap_empty(m) ((m).size == 0)
#define cmap_size(m) ((size_t) (m).size)
#define cmap_bucket_count(m) ((size_t) (m).bucket_count)
-#define cset_init cmap_init
+#define cset_ini cmap_ini
#define cset_size(s) cmap_size(s)
#define cset_bucket_count(s) cmap_bucket_count(s)
#define cmap_try_emplace(tag, self, k, v) do { \
@@ -181,7 +181,7 @@ typedef struct { \ } ctype##_##tag##_iter_t; \
\
STC_INLINE ctype##_##tag \
-ctype##_##tag##_init(void) {ctype##_##tag m = cmap_init; return m;} \
+ctype##_##tag##_init(void) {ctype##_##tag m = cmap_ini; return m;} \
STC_INLINE bool \
ctype##_##tag##_empty(ctype##_##tag m) {return m.size == 0;} \
STC_INLINE size_t \
@@ -265,7 +265,7 @@ implement_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, keyDestroy, RawKey, keyToRaw, keyFromRaw, RawValue, valueFromRaw) \
STC_API ctype##_##tag \
ctype##_##tag##_with_capacity(size_t cap) { \
- ctype##_##tag h = ctype##_init; \
+ ctype##_##tag h = ctype##_ini; \
ctype##_##tag##_reserve(&h, cap); \
return h; \
} \
diff --git a/stc/coption.h b/stc/coption.h index e1c6e7cb..4b0712d6 100644 --- a/stc/coption.h +++ b/stc/coption.h @@ -43,7 +43,7 @@ Example: const char* optstr = "xy:z::123";
printf("program -x -y ARG -z [ARG] -1 -2 -3 --foo --bar ARG --opt [ARG] [ARGUMENTS]\n");
int c;
- coption_t opt = coption_init;
+ coption_t opt = coption_ini;
while ((c = coption_get(&opt, argc, argv, optstr, longopts)) != -1) {
switch (c) {
case '?': printf("error: unknown option: %s\n", opt.faulty); break;
@@ -83,7 +83,7 @@ typedef struct { int val;
} coption_long_t;
-static const coption_t coption_init = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}};
+static const coption_t coption_ini = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}};
static void _coption_permute(char *argv[], int j, int n) { /* move argv[j] over n elements to the left */
int k;
@@ -93,7 +93,7 @@ static void _coption_permute(char *argv[], int j, int n) { /* move argv[j] over argv[j - k] = p;
}
-/* @param opt output; must be initialized to coption_init on first call
+/* @param opt output; must be initialized to coption_ini on first call
* @return ASCII val for a short option; longopt.val for a long option;
* -1 if argv[] is fully processed; '?' for an unknown option or
* an ambiguous long option; ':' if an option argument is missing
diff --git a/stc/cprique.h b/stc/cpqueue.h index 92a1f8c2..8873b0cd 100644 --- a/stc/cprique.h +++ b/stc/cpqueue.h @@ -24,78 +24,78 @@ /* Priority Queue using heap, with adapter class (normally cvec).
#include <stc/crandom.h>
- #include <stc/cprique.h>
+ #include <stc/cpqueue.h>
declare_cvec(f, float);
- declare_cprique(f, cvec_f, >); // min-heap (increasing values)
+ declare_cpqueue(f, cvec_f, >); // min-heap (increasing values)
int main() {
crand_rng32_t gen = crand_rng32_init(1234);
crand_uniform_f32_t dist = crand_uniform_f32_init(10.0f, 100.0f);
- cprique_f queue = cprique_f_init();
+ cpqueue_f queue = cpqueue_f_init();
// Push ten million random numbers onto the queue.
for (int i=0; i<10000000; ++i)
- cprique_f_push(&queue, crand_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 ", *cprique_f_top(queue));
- cprique_f_pop(&queue);
+ printf("%f ", *cpqueue_f_top(queue));
+ cpqueue_f_pop(&queue);
}
- cprique_f_destroy(&queue);
+ cpqueue_f_destroy(&queue);
}
*/
-#ifndef CPRIQUE__H__
-#define CPRIQUE__H__
+#ifndef CPQUEUE__H__
+#define CPQUEUE__H__
#include "cvec.h"
-#define declare_cprique(tag, ctype, cmpOpr) /* cmpOpr: < or > */ \
+#define declare_cpqueue(tag, ctype, cmpOpr) /* cmpOpr: < or > */ \
\
-typedef ctype cprique_##tag; \
-typedef ctype##_value_t cprique_##tag##_value_t; \
-typedef ctype##_rawvalue_t cprique_##tag##_rawvalue_t; \
-typedef ctype##_input_t cprique_##tag##_input_t; \
-STC_INLINE cprique_##tag \
-cprique_##tag##_init() {return ctype##_init();} \
+typedef struct 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 ctype##_init();} \
STC_INLINE size_t \
-cprique_##tag##_size(cprique_##tag pq) {return ctype##_size(pq);} \
+cpqueue_##tag##_size(cpqueue_##tag pq) {return ctype##_size(pq);} \
STC_INLINE bool \
-cprique_##tag##_empty(cprique_##tag pq) {return ctype##_empty(pq);} \
+cpqueue_##tag##_empty(cpqueue_##tag pq) {return ctype##_empty(pq);} \
STC_INLINE void \
-cprique_##tag##_destroy(cprique_##tag* self) {ctype##_destroy(self);} \
+cpqueue_##tag##_destroy(cpqueue_##tag* self) {ctype##_destroy(self);} \
STC_API void \
-cprique_##tag##_build(cprique_##tag* self); \
+cpqueue_##tag##_build(cpqueue_##tag* self); \
STC_API void \
-cprique_##tag##_erase(cprique_##tag* self, size_t i); \
-STC_INLINE const cprique_##tag##_value_t* \
-cprique_##tag##_top(const cprique_##tag* self) {return &self->data[0];} \
+cpqueue_##tag##_erase(cpqueue_##tag* self, size_t i); \
+STC_INLINE const cpqueue_##tag##_value_t* \
+cpqueue_##tag##_top(const cpqueue_##tag* self) {return &self->data[0];} \
STC_INLINE void \
-cprique_##tag##_pop(cprique_##tag* self) {cprique_##tag##_erase(self, 0);} \
+cpqueue_##tag##_pop(cpqueue_##tag* self) {cpqueue_##tag##_erase(self, 0);} \
STC_API void \
-cprique_##tag##_push_v(cprique_##tag* self, cprique_##tag##_value_t value); \
+cpqueue_##tag##_push_v(cpqueue_##tag* self, cpqueue_##tag##_value_t value); \
STC_INLINE void \
-cprique_##tag##_push(cprique_##tag* self, cprique_##tag##_rawvalue_t rawValue) { \
- cprique_##tag##_push_v(self, ctype##_value_from_raw(rawValue)); \
+cpqueue_##tag##_push(cpqueue_##tag* self, cpqueue_##tag##_rawvalue_t rawValue) { \
+ cpqueue_##tag##_push_v(self, ctype##_value_from_raw(rawValue)); \
} \
STC_API void \
-cprique_##tag##_push_n(cprique_##tag *self, const cprique_##tag##_input_t in[], size_t size); \
+cpqueue_##tag##_push_n(cpqueue_##tag *self, const cpqueue_##tag##_input_t in[], size_t size); \
\
-implement_cprique(tag, ctype, cmpOpr)
+implement_cpqueue(tag, ctype, cmpOpr)
/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-#define implement_cprique(tag, ctype, cmpOpr) \
+#define implement_cpqueue(tag, ctype, cmpOpr) \
\
STC_INLINE void \
-_cprique_##tag##_sift_down(cprique_##tag##_value_t* arr, size_t i, size_t n) { \
+_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 && ctype##_value_compare(&arr[c], &arr[c + 1]) cmpOpr 0) \
++c; \
if (ctype##_value_compare(&arr[r], &arr[c]) cmpOpr 0) { \
- cprique_##tag##_value_t t = arr[r]; arr[r] = arr[c]; arr[r = c] = t; \
+ cpqueue_##tag##_value_t t = arr[r]; arr[r] = arr[c]; arr[r = c] = t; \
} else \
return; \
c <<= 1; \
@@ -103,39 +103,39 @@ _cprique_##tag##_sift_down(cprique_##tag##_value_t* arr, size_t i, size_t n) { \ } \
\
STC_API void \
-cprique_##tag##_build(cprique_##tag* self) { \
- size_t n = cprique_##tag##_size(*self); \
- cprique_##tag##_value_t *arr = self->data - 1; \
+cpqueue_##tag##_build(cpqueue_##tag* self) { \
+ size_t n = cpqueue_##tag##_size(*self); \
+ cpqueue_##tag##_value_t *arr = self->data - 1; \
for (size_t k = n >> 1; k != 0; --k) \
- _cprique_##tag##_sift_down(arr, k, n); \
+ _cpqueue_##tag##_sift_down(arr, k, n); \
} \
\
STC_API void \
-cprique_##tag##_erase(cprique_##tag* self, size_t i) { \
- size_t n = cprique_##tag##_size(*self) - 1; \
+cpqueue_##tag##_erase(cpqueue_##tag* self, size_t i) { \
+ size_t n = cpqueue_##tag##_size(*self) - 1; \
self->data[i] = self->data[n]; \
ctype##_pop_back(self); \
- _cprique_##tag##_sift_down(self->data - 1, i + 1, n); \
+ _cpqueue_##tag##_sift_down(self->data - 1, i + 1, n); \
} \
\
STC_API void \
-cprique_##tag##_push_v(cprique_##tag* self, cprique_##tag##_value_t value) { \
+cpqueue_##tag##_push_v(cpqueue_##tag* self, cpqueue_##tag##_value_t value) { \
ctype##_push_back(self, value); /* sift-up the value */ \
- size_t n = cprique_##tag##_size(*self), c = n; \
- cprique_##tag##_value_t *arr = self->data - 1; \
+ size_t n = cpqueue_##tag##_size(*self), c = n; \
+ cpqueue_##tag##_value_t *arr = self->data - 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 \
-cprique_##tag##_push_n(cprique_##tag *self, const cprique_##tag##_input_t in[], size_t size) { \
- ctype##_reserve(self, cprique_##tag##_size(*self) + size); \
- for (size_t i=0; i<size; ++i) cprique_##tag##_push(self, in[i]); \
+cpqueue_##tag##_push_n(cpqueue_##tag *self, const cpqueue_##tag##_input_t in[], size_t 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 cprique_##tag##_dud
+typedef int cpqueue_##tag##_dud
#else
-#define implement_cprique(tag, ctype, cmpOpr)
+#define implement_cpqueue(tag, ctype, cmpOpr)
#endif
#endif
diff --git a/stc/cqueue.h b/stc/cqueue.h index 824f70dc..8f9dc698 100644 --- a/stc/cqueue.h +++ b/stc/cqueue.h @@ -60,7 +60,7 @@ #define declare_cqueue(tag, ctype) \
\
-typedef ctype cqueue_##tag; \
+typedef struct ctype cqueue_##tag; \
typedef ctype##_value_t cqueue_##tag##_value_t; \
typedef ctype##_rawvalue_t cqueue_##tag##_rawvalue_t; \
typedef ctype##_input_t cqueue_##tag##_input_t; \
@@ -86,11 +86,11 @@ STC_INLINE void \ cqueue_##tag##_push(cqueue_##tag* self, cqueue_##tag##_rawvalue_t rawValue) { \
ctype##_push_back(self, rawValue); \
} \
+ \
STC_API void \
cqueue_##tag##_push_n(cqueue_##tag *self, const cqueue_##tag##_input_t in[], size_t size) { \
ctype##_push_n(self, in, size); \
} \
- \
typedef ctype##_iter_t cqueue_##tag##_iter_t; \
STC_INLINE cqueue_##tag##_iter_t \
cqueue_##tag##_begin(cqueue_##tag* self) {return ctype##_begin(self);} \
diff --git a/stc/cstack.h b/stc/cstack.h index d90eee4e..5da47719 100644 --- a/stc/cstack.h +++ b/stc/cstack.h @@ -49,7 +49,7 @@ #define declare_cstack(tag, ctype) \
\
-typedef ctype cstack_##tag; \
+typedef struct ctype cstack_##tag; \
typedef ctype##_value_t cstack_##tag##_value_t; \
typedef ctype##_rawvalue_t cstack_##tag##_rawvalue_t; \
typedef ctype##_input_t cstack_##tag##_input_t; \
@@ -73,11 +73,11 @@ STC_INLINE void \ cstack_##tag##_push(cstack_##tag* self, cstack_##tag##_rawvalue_t rawValue) { \
ctype##_push_back(self, rawValue); \
} \
+ \
STC_API void \
cstack_##tag##_push_n(cstack_##tag *self, const cstack_##tag##_input_t in[], size_t size) { \
ctype##_push_n(self, in, size); \
} \
- \
typedef ctype##_iter_t cstack_##tag##_iter_t; \
STC_INLINE cstack_##tag##_iter_t \
cstack_##tag##_begin(cstack_##tag* self) {return ctype##_begin(self);} \
@@ -39,13 +39,13 @@ typedef char cstr_value_t, cstr_rawvalue_t, cstr_input_t; static size_t _cstr_nullrep[] = {0, 0, 0};
-static cstr_t cstr_init = {(char* ) &_cstr_nullrep[2]};
+static cstr_t cstr_ini = {(char* ) &_cstr_nullrep[2]};
#define cstr_size(s) ((const size_t *) (s).str)[-2]
#define cstr_capacity(s) ((const size_t *) (s).str)[-1]
#define cstr_empty(s) (cstr_size(s) == 0)
-#define cstr_front(s) (s).str[0]
-#define cstr_back(s) (s).str[_cstr_size(s) - 1] /* may have side effect */
#define cstr_npos ((size_t) (-1))
+#define cstr_push_n cstr_append_n
+#define cstr_push_back_v cstr_push_back
STC_API cstr_t
cstr_make_n(const char* str, size_t len);
@@ -73,6 +73,9 @@ c_strnstr(const char* s, const char* needle, size_t n); /* gives true string capacity: 7, 23, 39, ... */
#define _cstr_cap(size) ((((size) + 24) >> 4) * 16 - 9)
+STC_INLINE cstr_t
+cstr_init() {return cstr_ini;}
+
STC_INLINE void
cstr_destroy(cstr_t* self) {
if (cstr_capacity(*self))
@@ -81,13 +84,13 @@ cstr_destroy(cstr_t* self) { STC_INLINE cstr_t
cstr_with_capacity(size_t cap) {
- cstr_t s = cstr_init;
+ cstr_t s = cstr_ini;
cstr_reserve(&s, cap);
return s;
}
STC_INLINE cstr_t
cstr_with_size(size_t len, char fill) {
- cstr_t s = cstr_init;
+ cstr_t s = cstr_ini;
cstr_resize(&s, len, fill);
return s;
}
@@ -106,6 +109,11 @@ cstr_clear(cstr_t* self) { self->str[_cstr_size(*self) = 0] = '\0';
}
+STC_INLINE char*
+cstr_front(cstr_t* self) {return self->str;}
+STC_INLINE char*
+cstr_back(cstr_t* self) {return self->str + _cstr_size(*self) - 1;}
+
STC_INLINE cstr_iter_t
cstr_begin(cstr_t* self) {
cstr_iter_t it = {self->str, self->str + cstr_size(*self)}; return it;
@@ -128,7 +136,7 @@ cstr_take(cstr_t* self, cstr_t s) { STC_INLINE cstr_t
cstr_move(cstr_t* self) {
cstr_t tmp = *self;
- *self = cstr_init;
+ *self = cstr_ini;
return tmp;
}
@@ -136,14 +144,11 @@ STC_INLINE cstr_t* cstr_append(cstr_t* self, const char* str) {
return cstr_append_n(self, str, strlen(str));
}
-/*STC_INLINE void
-cstr_push_n(cstr_t* self, const cstr_input_t[] in, size_t n) {
- cstr_append_n(self, in, n);
-}*/
STC_INLINE cstr_t*
cstr_push_back(cstr_t* self, char value) {
return cstr_append_n(self, &value, 1);
}
+
STC_INLINE void
cstr_pop_back(cstr_t* self) {
self->str[ --_cstr_size(*self) ] = '\0';
@@ -228,7 +233,7 @@ cstr_resize(cstr_t* self, size_t len, char fill) { STC_API cstr_t
cstr_make_n(const char* str, size_t len) {
- if (len == 0) return cstr_init;
+ if (len == 0) return cstr_ini;
size_t *rep = (size_t *) malloc(_cstr_mem(len));
cstr_t s = {(char *) memcpy(rep + 2, str, len)};
s.str[rep[0] = len] = '\0';
@@ -245,7 +250,7 @@ cstr_from(const char* fmt, ...) { # pragma warning(push)
# pragma warning(disable: 4996)
#endif
- cstr_t tmp = cstr_init;
+ cstr_t tmp = cstr_ini;
va_list args, args2;
va_start(args, fmt);
va_copy(args2, args);
@@ -27,7 +27,7 @@ #include <string.h>
#include "cdefs.h"
-#define cvec_init {NULL}
+#define cvec_ini {NULL}
#define cvec_size(v) _cvec_safe_size((v).data)
#define cvec_capacity(v) _cvec_safe_capacity((v).data)
#define cvec_empty(v) (_cvec_safe_size((v).data) == 0)
@@ -55,7 +55,7 @@ typedef RawValue cvec_##tag##_rawvalue_t; \ typedef cvec_##tag##_rawvalue_t cvec_##tag##_input_t; \
\
STC_INLINE cvec_##tag \
-cvec_##tag##_init(void) {cvec_##tag v = cvec_init; return v;} \
+cvec_##tag##_init(void) {cvec_##tag v = cvec_ini; return v;} \
STC_INLINE bool \
cvec_##tag##_empty(cvec_##tag v) {return cvec_empty(v);} \
STC_INLINE size_t \
@@ -97,13 +97,13 @@ cvec_##tag##_value_compare(const Value* x, const Value* y); \ \
STC_INLINE cvec_##tag \
cvec_##tag##_with_size(size_t size, Value null_val) { \
- cvec_##tag x = cvec_init; \
+ cvec_##tag x = cvec_ini; \
cvec_##tag##_resize(&x, size, null_val); \
return x; \
} \
STC_INLINE cvec_##tag \
cvec_##tag##_with_capacity(size_t size) { \
- cvec_##tag x = cvec_init; \
+ cvec_##tag x = cvec_ini; \
cvec_##tag##_reserve(&x, size); \
return x; \
} \
|
