From 13393d3cbc81e5c5d3ea8335694280ef16a5a84f Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 2 Aug 2020 10:35:34 +0200 Subject: Add c_nullptr to cdefs.h --- examples/benchmark.c | 2 +- examples/geek5.c | 4 ++-- examples/geek6.c | 2 +- examples/geek7.c | 2 +- stc/cdefs.h | 5 +++++ stc/clist.h | 30 +++++++++++++++--------------- stc/cmap.h | 16 ++++++++-------- stc/copt.h | 2 +- stc/cstr.h | 8 ++++---- stc/cvec.h | 16 ++++++++-------- 10 files changed, 46 insertions(+), 41 deletions(-) diff --git a/examples/benchmark.c b/examples/benchmark.c index 7b707260..ae41dec6 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -35,7 +35,7 @@ crandom_eng64_t rng; ; cmap_##tag##_set_load_factors(&map, max_load_factor, 0.0) #define CMAP_PUT(tag, key, val) cmap_##tag##_put(&map, key, val)->value #define CMAP_ERASE(tag, key) cmap_##tag##_erase(&map, key) -#define CMAP_FIND(tag, key) (cmap_##tag##_find(map, key) != NULL) +#define CMAP_FIND(tag, key) (cmap_##tag##_find(map, key) != c_nullptr) #define CMAP_SIZE(tag) cmap_size(map) #define CMAP_BUCKETS(tag) (map).bucket_count #define CMAP_CLEAR(tag) cmap_##tag##_destroy(&map) diff --git a/examples/geek5.c b/examples/geek5.c index 67f5d097..cbfc40e6 100644 --- a/examples/geek5.c +++ b/examples/geek5.c @@ -37,7 +37,7 @@ int NumOccurrences(const char* arr[], int n, const char* str, int L, int R) // If current string doesn't // have an entry in the map // then create the entry - if (it == NULL) { + if (it == c_nullptr) { cvec_i A = cvec_init; cvec_i_push_back(&A, i + 1); cmap_sv_put(&M, temp, A); @@ -51,7 +51,7 @@ int NumOccurrences(const char* arr[], int n, const char* str, int L, int R) // If the given string is not // present in the array - if (it == NULL) + if (it == c_nullptr) return 0; // If the given string is present diff --git a/examples/geek6.c b/examples/geek6.c index db0b3bcf..b3991e99 100644 --- a/examples/geek6.c +++ b/examples/geek6.c @@ -52,7 +52,7 @@ int missingNumber(int a[], int n) // Return the first value starting // from 1 which does not exists in map while (1) { - if (cset_i_find(&mp, index) == NULL) { + if (cset_i_find(&mp, index) == c_nullptr) { return index; } diff --git a/examples/geek7.c b/examples/geek7.c index 9a60c0a9..0976bf80 100644 --- a/examples/geek7.c +++ b/examples/geek7.c @@ -49,7 +49,7 @@ void findElementsAfterDel(int arr[], int m, int del[], // Search if the element is present cmap_ii_entry_t *e = cmap_ii_find(&mp, arr[i]); - if (e != NULL) { + if (e != c_nullptr) { // Decrement its frequency e->value--; diff --git a/stc/cdefs.h b/stc/cdefs.h index 9328d723..3a50df0e 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -61,6 +61,11 @@ #define c_max_alloca (1000) #define c_swap(T, x, y) { T __t = x; x = y; y = __t; } +#ifdef __cplusplus +#define c_nullptr nullptr +#else +#define c_nullptr NULL +#endif #define c_default_from_raw(x) (x) #define c_default_to_raw(ptr) (*(ptr)) diff --git a/stc/clist.h b/stc/clist.h index ef2ffa85..a34c2162 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -81,10 +81,10 @@ clistnode_##tag *item, **_last; \ } clist_##tag##_iter_t -#define clist_init {NULL} +#define clist_init {c_nullptr} #define clist_front(list) (list).last->next->value #define clist_back(list) (list).last->value -#define clist_empty(list) ((list).last == NULL) +#define clist_empty(list) ((list).last == c_nullptr) #define declare_clist_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \ @@ -129,12 +129,12 @@ \ STC_INLINE clist_##tag##_iter_t \ clist_##tag##_begin(clist_##tag* self) { \ - clistnode_##tag *head = self->last ? self->last->next : NULL; \ + clistnode_##tag *head = self->last ? self->last->next : c_nullptr; \ clist_##tag##_iter_t it = {head, &self->last}; return it; \ } \ STC_INLINE clist_##tag##_iter_t \ clist_##tag##_next(clist_##tag##_iter_t it) { \ - it.item = it.item == *it._last ? NULL : it.item->next; return it; \ + it.item = it.item == *it._last ? c_nullptr : it.item->next; return it; \ } \ STC_INLINE clist_##tag##_iter_t \ clist_##tag##_last(clist_##tag* self) { \ @@ -196,7 +196,7 @@ other->last->next = next; \ if (bottom && pos.item == self->last) self->last = other->last; \ } \ - other->last = NULL; \ + other->last = c_nullptr; \ } \ STC_API void \ clist_##tag##_splice_front(clist_##tag* self, clist_##tag* other) { \ @@ -217,13 +217,13 @@ } \ prev = i; \ } \ - prev.item = NULL; return prev; \ + prev.item = c_nullptr; return prev; \ } \ \ STC_API Value* \ clist_##tag##_find(clist_##tag* self, RawValue val) { \ clist_##tag##_iter_t it = clist_##tag##_find_before(self, val); \ - return it.item ? &it.item->next->value : NULL; \ + return it.item ? &it.item->next->value : c_nullptr; \ } \ \ STC_API clist_##tag##_iter_t \ @@ -256,7 +256,7 @@ #define _clist_erase_after(self, tag, node, valueDestroy) \ clistnode_##tag* del = node->next, *next = del->next; \ node->next = next; \ - if (del == next) self->last = NULL; \ + if (del == next) self->last = c_nullptr; \ else if (self->last == del) self->last = node; \ valueDestroy(&del->value); \ free(del) @@ -270,12 +270,12 @@ static inline clistnode__base * _clist_mergesort(clistnode__base *list, int (*cmp)(const void*, const void*)) { clistnode__base *p, *q, *e, *tail, *oldhead; int insize = 1, nmerges, psize, qsize, i; - if (!list) return NULL; + if (!list) return c_nullptr; while (1) { p = list; oldhead = list; - list = tail = NULL; + list = tail = c_nullptr; nmerges = 0; while (p) { @@ -284,7 +284,7 @@ _clist_mergesort(clistnode__base *list, int (*cmp)(const void*, const void*)) { psize = 0; for (i = 0; i < insize; ++i) { ++psize; - q = (q->next == oldhead ? NULL : q->next); + q = (q->next == oldhead ? c_nullptr : q->next); if (!q) break; } qsize = insize; @@ -292,16 +292,16 @@ _clist_mergesort(clistnode__base *list, int (*cmp)(const void*, const void*)) { while (psize > 0 || (qsize > 0 && q)) { if (psize == 0) { e = q; q = q->next; --qsize; - if (q == oldhead) q = NULL; + if (q == oldhead) q = c_nullptr; } else if (qsize == 0 || !q) { e = p; p = p->next; --psize; - if (p == oldhead) p = NULL; + if (p == oldhead) p = c_nullptr; } else if (cmp(p, q) <= 0) { e = p; p = p->next; --psize; - if (p == oldhead) p = NULL; + if (p == oldhead) p = c_nullptr; } else { e = q; q = q->next; --qsize; - if (q == oldhead) q = NULL; + if (q == oldhead) q = c_nullptr; } if (tail) tail->next = e; diff --git a/stc/cmap.h b/stc/cmap.h index fc9209eb..bfb6336d 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -38,7 +38,7 @@ int main(void) { cmap_mx_put(&m, 5, 'a'); cmap_mx_put(&m, 8, 'b'); cmap_mx_put(&m, 12, 'c'); - cmap_mx_entry_t *e = cmap_mx_find(&m, 10); // = NULL + cmap_mx_entry_t *e = cmap_mx_find(&m, 10); // = c_nullptr char val = cmap_mx_find(&m, 5)->value; cmap_mx_put(&m, 5, 'd'); // update cmap_mx_erase(&m, 8); @@ -53,7 +53,7 @@ int main(void) { #include #include "cdefs.h" -#define cmap_init {NULL, NULL, 0, 0, 0.85f, 0.15f} +#define cmap_init {c_nullptr, c_nullptr, 0, 0, 0.85f, 0.15f} #define cmap_size(m) ((size_t) (m).size) #define cset_init cmap_init #define cset_size(s) cmap_size(s) @@ -123,8 +123,8 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80}; cstr_destroy, const char*, cstr_to_raw, cstr_make) #define OPT_1_cset(x) -#define OPT_2_cset(x, y) x #define OPT_1_cmap(x) x +#define OPT_2_cset(x, y) x #define OPT_2_cmap(x, y) x, y /* CHASH full: use 'void' for Value if ctype is cset */ @@ -255,10 +255,10 @@ ctype##_##tag##_bucket(const ctype##_##tag* self, const ctype##_##tag##_rawkey_t \ STC_API ctype##_##tag##_entry_t* \ ctype##_##tag##_find(const ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey) { \ - if (self->size == 0) return NULL; \ + if (self->size == 0) return c_nullptr; \ uint32_t hx; \ size_t idx = ctype##_##tag##_bucket(self, &rawKey, &hx); \ - return self->_hashx[idx] ? &self->table[idx] : NULL; \ + return self->_hashx[idx] ? &self->table[idx] : c_nullptr; \ } \ \ STC_INLINE void ctype##_##tag##_reserve_expand(ctype##_##tag* self) { \ @@ -310,7 +310,7 @@ ctype##_##tag##_reserve(ctype##_##tag* self, size_t newcap) { \ self->max_load_factor, self->shrink_limit_factor \ }; \ /* Rehash: */ \ - ctype##_##tag##_swap(self, &tmp); \ + c_swap(ctype##_##tag, *self, tmp); \ ctype##_##tag##_entry_t* e = tmp.table, *slot = self->table; \ uint8_t* hashx = self->_hashx; \ uint32_t hx; \ @@ -366,13 +366,13 @@ ctype##_##tag##_begin(ctype##_##tag* map) { \ uint8_t* hx = map->_hashx; \ ctype##_##tag##_entry_t* e = map->table, *end = e + map->bucket_count; \ while (e != end && !*hx) ++e, ++hx; \ - ctype##_##tag##_iter_t it = {e == end ? NULL : e, end, hx}; return it; \ + ctype##_##tag##_iter_t it = {e == end ? c_nullptr : e, end, hx}; return it; \ } \ \ STC_API ctype##_##tag##_iter_t \ ctype##_##tag##_next(ctype##_##tag##_iter_t it) { \ do { ++it.item, ++it._hx; } while (it.item != it._end && !*it._hx); \ - if (it.item == it._end) it.item = NULL; \ + if (it.item == it._end) it.item = c_nullptr; \ return it; \ } diff --git a/stc/copt.h b/stc/copt.h index 49c895e0..26a7e94d 100644 --- a/stc/copt.h +++ b/stc/copt.h @@ -83,7 +83,7 @@ typedef struct { int val; } copt_long_t; -static const copt_t copt_init = {1, 0, NULL, NULL, -1, 1, 0, 0, {'-', '?', '\0'}}; +static const copt_t copt_init = {1, 0, c_nullptr, c_nullptr, -1, 1, 0, 0, {'-', '?', '\0'}}; static void _copt_permute(char *argv[], int j, int n) { /* move argv[j] over n elements to the left */ int k; diff --git a/stc/cstr.h b/stc/cstr.h index 42411980..47353651 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -203,7 +203,7 @@ STC_API void cstr_reserve(cstr_t* self, size_t cap) { size_t len = cstr_size(*self), oldcap = cstr_capacity(*self); if (cap > oldcap) { - size_t* rep = (size_t *) realloc(oldcap ? _cstr_rep(self) : NULL, _cstr_mem(cap)); + size_t* rep = (size_t *) realloc(oldcap ? _cstr_rep(self) : c_nullptr, _cstr_mem(cap)); self->str = (char *) (rep + 2); self->str[rep[0] = len] = '\0'; rep[1] = cap; @@ -242,7 +242,7 @@ cstr_from(const char* fmt, ...) { cstr_t tmp = cstr_init; va_list args; va_start(args, fmt); - int len = vsnprintf(NULL, (size_t)0, fmt, args); + int len = vsnprintf(c_nullptr, (size_t)0, fmt, args); if (len > 0) { tmp = cstr_make_reserved(len); vsprintf(tmp.str, fmt, args); @@ -317,7 +317,7 @@ cstr_strnstr(cstr_t s, size_t pos, const char* needle, size_t n) { char *x = s.str + pos, /* haystack */ *z = s.str + cstr_size(s) - n + 1; if (x >= z) - return NULL; + return c_nullptr; ptrdiff_t sum = 0; const char *y = x, *p = needle, *q = needle + n; while (p != q) @@ -327,7 +327,7 @@ cstr_strnstr(cstr_t s, size_t pos, const char* needle, size_t n) { return x; sum += *y++ - *x++; } - return NULL; + return c_nullptr; } #endif diff --git a/stc/cvec.h b/stc/cvec.h index 96d990b4..01cc718d 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -20,14 +20,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef CVECTOR__H__ -#define CVECTOR__H__ +#ifndef CVEC__H__ +#define CVEC__H__ #include #include #include "cdefs.h" -#define cvec_init {NULL} +#define cvec_init {c_nullptr} #define cvec_size(cv) _cvec_safe_size((cv).data) #define cvec_capacity(cv) _cvec_safe_capacity((cv).data) #define cvec_empty(cv) (_cvec_safe_size((cv).data) == 0) @@ -96,12 +96,12 @@ typedef struct { \ STC_INLINE cvec_##tag##_iter_t \ cvec_##tag##_begin(cvec_##tag* vec) { \ const size_t n = cvec_size(*vec); \ - cvec_##tag##_iter_t it = {n ? vec->data : NULL, vec->data + n}; \ + cvec_##tag##_iter_t it = {n ? vec->data : c_nullptr, vec->data + n}; \ return it; \ } \ STC_INLINE cvec_##tag##_iter_t \ cvec_##tag##_next(cvec_##tag##_iter_t it) { \ - if (++it.item == it.end) it.item = NULL; \ + if (++it.item == it.end) it.item = c_nullptr; \ return it; \ } \ \ @@ -159,7 +159,7 @@ STC_API void \ cvec_##tag##_push_back(cvec_##tag* self, Value value) { \ size_t len = cvec_size(*self); \ if (len == cvec_capacity(*self)) \ - cvec_##tag##_reserve(self, 7 + len * 5 / 3); \ + cvec_##tag##_reserve(self, 5 + len * 5 / 3); \ self->data[cvec_size(*self)] = value; \ ++_cvec_size(*self); \ } \ @@ -168,7 +168,7 @@ STC_API void \ cvec_##tag##_insert(cvec_##tag* self, size_t pos, Value value) { \ size_t len = cvec_size(*self); \ if (len == cvec_capacity(*self)) \ - cvec_##tag##_reserve(self, 7 + len * 5 / 3); \ + cvec_##tag##_reserve(self, 5 + len * 5 / 3); \ memmove(&self->data[pos + 1], &self->data[pos], (len - pos) * sizeof(Value)); \ self->data[pos] = value; \ ++_cvec_size(*self); \ @@ -222,7 +222,7 @@ cvec_##tag##_sort(cvec_##tag* self) { \ #define _cvec_capacity(cv) ((size_t *)(cv).data)[-1] STC_INLINE size_t* _cvec_alloced(void* data) { - return data ? ((size_t *) data) - 2 : NULL; + return data ? ((size_t *) data) - 2 : c_nullptr; } STC_INLINE size_t _cvec_safe_size(const void* data) { return data ? ((const size_t *) data)[-2] : 0; -- cgit v1.2.3