summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-07-24 12:21:24 +0200
committerTyge Løvset <[email protected]>2020-07-24 12:21:24 +0200
commiteda4377e0bea66d23f584b7660a7617f8bea4bf0 (patch)
treef24af161d9efebd136e49d8e6467430f0be46927
parent1c686f72bf9857c904b1320df7248ee5144d484e (diff)
downloadSTC-modified-eda4377e0bea66d23f584b7660a7617f8bea4bf0.tar.gz
STC-modified-eda4377e0bea66d23f584b7660a7617f8bea4bf0.zip
Updated CBitset (renamed from CBitVec too). Added <container>_<tag>_init(void) functions.
-rw-r--r--examples/bits.c61
-rw-r--r--stc/carr.h6
-rw-r--r--stc/cbitset.h134
-rw-r--r--stc/cbitvec.h92
-rw-r--r--stc/clist.h15
-rw-r--r--stc/cmap.h5
-rw-r--r--stc/cvec.h2
7 files changed, 191 insertions, 124 deletions
diff --git a/examples/bits.c b/examples/bits.c
index 3a10e48c..07e17605 100644
--- a/examples/bits.c
+++ b/examples/bits.c
@@ -1,26 +1,47 @@
#include <stdio.h>
-#include <stc/cbitvec.h>
+#include <stc/cbitset.h>
int main() {
- CBitVec vec = cbitvec_make(23, true);
- cbitvec_unset(&vec, 9);
- cbitvec_resize(&vec, 43, false);
- printf("%4zu: ", vec.size);
- for (int i=0; i<vec.size; ++i)
- printf("%d", cbitvec_value(&vec, i));puts("");
+ CBitset set = cbitset_make(23, true);
+ cbitset_reset(&set, 9);
+ cbitset_resize(&set, 43, false);
+ printf("%4zu: ", set.size);
+ for (int i=0; i<set.size; ++i)
+ printf("%d", cbitset_test(set, i));
+ puts("");
- cbitvec_set(&vec, 28);
- cbitvec_resize(&vec, 77, true);
- cbitvec_resize(&vec, 93, false);
- cbitvec_resize(&vec, 102, true);
- cbitvec_setValue(&vec, 99, false);
- printf("%4zu: ", vec.size);
- for (int i=0; i<vec.size; ++i)
- printf("%d", cbitvec_value(&vec, i));puts("");
+ cbitset_set(&set, 28);
+ cbitset_resize(&set, 77, true);
+ cbitset_resize(&set, 93, false);
+ cbitset_resize(&set, 102, true);
+ cbitset_setTo(&set, 99, false);
+ printf("%4zu: ", set.size);
+ for (int i=0; i<set.size; ++i)
+ printf("%d", cbitset_test(set, i));
+ puts("");
- cbitvec_setAll(&vec, false);
- printf("%4zu: ", vec.size);
- for (int i=0; i<vec.size; ++i)
- printf("%d", cbitvec_value(&vec, i));puts("");
- cbitvec_destroy(&vec);
+ CBitset s2 = cbitset_from(set);
+ cbitset_flipAll(&s2);
+ cbitset_set(&s2, 16);
+ cbitset_set(&s2, 17);
+ cbitset_set(&s2, 18);
+ printf(" new: ");
+ for (int i=0; i<s2.size; ++i)
+ printf("%d", cbitset_test(s2, i));
+ puts("");
+
+ printf(" xor: ");
+ cbitset_setXor(&set, s2);
+ for (int i=0; i<set.size; ++i)
+ printf("%d", cbitset_test(set, i));
+ puts("");
+
+ cbitset_setAll(&set, false);
+ printf("%4zu: ", set.size);
+ for (int i=0; i<set.size; ++i)
+ printf("%d", cbitset_test(set, i));
+ puts("");
+
+ cbitset_destroy(&s2);
+ cbitset_destroy(&set);
} \ No newline at end of file
diff --git a/stc/carr.h b/stc/carr.h
index e80d0ad9..0e5a65c9 100644
--- a/stc/carr.h
+++ b/stc/carr.h
@@ -121,17 +121,17 @@ static inline size_t _carr3_size(const size_t* zdim) {
} \
\
static inline CArr1_##tag \
- carr1_##tag##_makeFrom(size_t xdim, Value* array, bool own) { \
+ carr1_##tag##_from(size_t xdim, Value* array, bool own) { \
CArr1_##tag a = {array, xdim | (own ? _carr_OWN : 0)}; \
return a; \
} \
static inline CArr2_##tag \
- carr2_##tag##_makeFrom(size_t ydim, size_t xdim, Value* array, bool own) { \
+ carr2_##tag##_from(size_t ydim, size_t xdim, Value* array, bool own) { \
CArr2_##tag a = {array, xdim | (own ? _carr_OWN : 0), ydim * xdim}; \
return a; \
} \
static inline CArr3_##tag \
- carr3_##tag##_makeFrom(size_t zdim, size_t ydim, size_t xdim, Value* array, bool own) { \
+ carr3_##tag##_from(size_t zdim, size_t ydim, size_t xdim, Value* array, bool own) { \
CArr3_##tag a = {array, xdim | (own ? _carr_OWN : 0), ydim * xdim, zdim}; \
return a; \
} \
diff --git a/stc/cbitset.h b/stc/cbitset.h
new file mode 100644
index 00000000..8ab4fe09
--- /dev/null
+++ b/stc/cbitset.h
@@ -0,0 +1,134 @@
+/* MIT License
+ *
+ * Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+/*
+Similar to boost::dynamic_bitset / std::bitset
+
+#include <stdio.h>
+#include "cbitset.h"
+
+int main() {
+ CBitset set = cbitset_make(23, true);
+ cbitset_reset(&set, 9);
+ cbitset_resize(&set, 43, false);
+ printf("%4zu: ", set.size);for (int i=0; i<set.size; ++i) printf("%d", cbitset_value(&set, i));puts("");
+ cbitset_set(&set, 28);
+ cbitset_resize(&set, 77, true);
+ cbitset_resize(&set, 93, false);
+ cbitset_resize(&set, 102, true);
+ cbitset_setTo(&set, 99, false);
+ printf("%4zu: ", set.size);for (int i=0; i<set.size; ++i) printf("%d", cbitset_value(&set, i));puts("");
+ cbitset_destroy(&set);
+}
+*/
+#ifndef CBITSET__H__
+#define CBITSET__H__
+
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+#include "cdefs.h"
+
+typedef struct { uint64_t* _arr; size_t size; } CBitset;
+
+STC_INLINE void cbitset_setAll(CBitset *self, bool value);
+
+STC_INLINE CBitset cbitset_make(size_t size, bool value) {
+ CBitset set = {(uint64_t *) malloc(((size + 63) >> 6) * 8), size};
+ cbitset_setAll(&set, value);
+ return set;
+}
+STC_INLINE CBitset cbitset_from(CBitset other) {
+ size_t n = (other.size + 63) >> 6;
+ CBitset set = {(uint64_t *) memcpy(malloc(n * 8), other._arr, n * 8), other.size};
+ return set;
+}
+STC_INLINE void cbitset_destroy(CBitset* self) {
+ free(self->_arr);
+}
+
+STC_INLINE void cbitset_resize(CBitset* self, size_t size, bool value) {
+ size_t newsz = (size + 63) >> 6, oldsz = (self->size + 63) >> 6;
+ self->_arr = (uint64_t *) realloc(self->_arr, newsz * 8);
+ memset(self->_arr + oldsz, value ? 0xff : 0x0, (newsz - oldsz) * 8);
+ if (self->size & 63) {
+ size_t idx = (self->size - 1) >> 6; uint64_t bits = (1ull << (self->size & 63)) - 1;
+ value ? (self->_arr[idx] |= ~bits) : (self->_arr[idx] &= bits);
+ }
+ self->size = size;
+}
+STC_INLINE size_t cbitset_size(CBitset set) {return set.size;}
+
+STC_INLINE void cbitset_set(CBitset *self, size_t i) {self->_arr[i >> 6] |= 1ull << (i & 63);}
+STC_INLINE void cbitset_reset(CBitset *self, size_t i) {self->_arr[i >> 6] &= ~(1ull << (i & 63));}
+STC_INLINE void cbitset_setTo(CBitset *self, size_t i, bool value) {value ? cbitset_set(self, i) : cbitset_reset(self, i);}
+STC_INLINE void cbitset_flip(CBitset *self, size_t i) {self->_arr[i >> 6] ^= 1ull << (i & 63);}
+STC_INLINE bool cbitset_test(CBitset set, size_t i) {return (set._arr[i >> 6] & (1ull << (i & 63))) != 0;}
+
+STC_INLINE void cbitset_setAll(CBitset *self, bool value) {
+ memset(self->_arr, value ? 0xff : 0x0, ((self->size + 63) >> 6) * 8);
+}
+STC_INLINE void cbitset_setAll64(CBitset *self, uint64_t pattern) {
+ size_t n = (self->size + 63) >> 6;
+ for (size_t i=0; i<n; ++i) self->_arr[i] = pattern;
+}
+STC_INLINE void cbitset_flipAll(CBitset *self) {
+ size_t n = (self->size + 63) >> 6;
+ for (size_t i=0; i<n; ++i) self->_arr[i] ^= ~0ull;
+}
+/* Intersection */
+STC_INLINE void cbitset_setAnd(CBitset *self, CBitset other) {
+ assert(self->size == other.size);
+ size_t n = (self->size + 63) >> 6;
+ for (size_t i=0; i<n; ++i) self->_arr[i] &= other._arr[i];
+}
+/* Union */
+STC_INLINE void cbitset_setOr(CBitset *self, CBitset other) {
+ assert(self->size == other.size);
+ size_t n = (self->size + 63) >> 6;
+ for (size_t i=0; i<n; ++i) self->_arr[i] |= other._arr[i];
+}
+/* Exclusive disjunction */
+STC_INLINE void cbitset_setXor(CBitset *self, CBitset other) {
+ assert(self->size == other.size);
+ size_t n = (self->size + 63) >> 6;
+ for (size_t i=0; i<n; ++i) self->_arr[i] ^= other._arr[i];
+}
+
+STC_INLINE CBitset cbitset_and(CBitset s1, CBitset s2) {
+ CBitset set = cbitset_from(s1);
+ cbitset_setAnd(&set, s2); return set;
+}
+STC_INLINE CBitset cbitset_or(CBitset s1, CBitset s2) {
+ CBitset set = cbitset_from(s1);
+ cbitset_setOr(&set, s2); return set;
+}
+STC_INLINE CBitset cbitset_xor(CBitset s1, CBitset s2) {
+ CBitset set = cbitset_from(s1);
+ cbitset_setXor(&set, s2); return set;
+}
+STC_INLINE CBitset cbitset_not(CBitset s1) {
+ CBitset set = cbitset_from(s1);
+ cbitset_flipAll(&set); return set;
+}
+
+#endif
diff --git a/stc/cbitvec.h b/stc/cbitvec.h
deleted file mode 100644
index 41029853..00000000
--- a/stc/cbitvec.h
+++ /dev/null
@@ -1,92 +0,0 @@
-/* MIT License
- *
- * Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-/*
-#include <stdio.h>
-#include "cbitvec.h"
-
-int main() {
- CBitVec vec = cbitvec_make(23, true);
- cbitvec_unset(&vec, 9);
- cbitvec_resize(&vec, 43, false);
- printf("%4zu: ", vec.size);for (int i=0; i<vec.size; ++i) printf("%d", cbitvec_value(&vec, i));puts("");
- cbitvec_set(&vec, 28);
- cbitvec_resize(&vec, 77, true);
- cbitvec_resize(&vec, 93, false);
- cbitvec_resize(&vec, 102, true);
- cbitvec_setValue(&vec, 99, false);
- printf("%4zu: ", vec.size);for (int i=0; i<vec.size; ++i) printf("%d", cbitvec_value(&vec, i));puts("");
- cbitvec_destroy(&vec);
-}
-*/
-#ifndef CBITVEC__H__
-#define CBITVEC__H__
-
-#include <string.h>
-#include <stdlib.h>
-#include "cdefs.h"
-
-typedef struct { uint64_t* _arr; size_t size; } CBitVec;
-
-STC_INLINE void cbitvec_set(CBitVec *self, size_t i) {self->_arr[i >> 6] |= 1ull << (i & 63);}
-STC_INLINE void cbitvec_unset(CBitVec *self, size_t i) {self->_arr[i >> 6] &= ~(1ull << (i & 63));}
-STC_INLINE void cbitvec_setValue(CBitVec *self, size_t i, bool value) {value ? cbitvec_set(self, i) : cbitvec_unset(self, i);}
-STC_INLINE void cbitvec_flip(CBitVec *self, size_t i) {self->_arr[i >> 6] ^= 1ull << (i & 63);}
-STC_INLINE bool cbitvec_value(const CBitVec *self, size_t i) {return (self->_arr[i >> 6] & (1ull << (i & 63))) != 0;}
-STC_INLINE size_t cbitvec_size(CBitVec bv) {return bv.size;}
-
-STC_INLINE void cbitvec_resize(CBitVec* self, size_t size, bool value) {
- size_t newsz = (size + 63) >> 6, oldsz = (self->size + 63) >> 6;
- self->_arr = (uint64_t *) realloc(self->_arr, newsz * 8);
- memset(self->_arr + oldsz, value ? 0xff : 0x0, (newsz - oldsz) * 8);
- if (self->size & 63) {
- size_t idx = (self->size - 1) >> 6; uint64_t bits = (1ull << (self->size & 63)) - 1;
- value ? (self->_arr[idx] |= ~bits) : (self->_arr[idx] &= bits);
- }
- self->size = size;
-}
-
-STC_INLINE void cbitvec_setAll(CBitVec *self, bool value) {
- memset(self->_arr, value ? 0xff : 0x0, ((self->size + 63) >> 6) * 8);
-}
-
-STC_INLINE void cbitvec_setAll64(CBitVec *self, uint64_t pattern) {
- size_t n = (self->size + 63) >> 6;
- while (n--) self->_arr[n] = pattern;
-}
-
-STC_INLINE void cbitvec_flipAll(CBitVec *self) {
- size_t n = (self->size + 63) >> 6;
- while (n--) self->_arr[n] ^= ~0ull;
-}
-
-STC_INLINE CBitVec cbitvec_make(size_t size, bool value) {
- CBitVec vec = {(uint64_t *) malloc(((size + 63) >> 6) * 8), size};
- cbitvec_setAll(&vec, value);
- return vec;
-}
-
-STC_INLINE void cbitvec_destroy(CBitVec* self) {
- free(self->_arr);
-}
-
-#endif
diff --git a/stc/clist.h b/stc/clist.h
index fbfb4817..41067286 100644
--- a/stc/clist.h
+++ b/stc/clist.h
@@ -91,8 +91,8 @@
\
declare_CListTypes(tag, Value); \
\
- STC_API void \
- clist_##tag##_pushN(CList_##tag *self, const Value in[], size_t size); \
+ STC_INLINE CList_##tag \
+ clist_##tag##_init(void) {return clist_init;} \
STC_API void \
clist_##tag##_destroy(CList_##tag* self); \
STC_API void \
@@ -100,6 +100,8 @@
STC_API void \
clist_##tag##_pushFront(CList_##tag* self, Value value); \
STC_API void \
+ clist_##tag##_pushN(CList_##tag *self, const Value in[], size_t size); \
+ STC_API void \
clist_##tag##_popFront(CList_##tag* self); \
STC_API void \
clist_##tag##_insertAfter(CList_##tag* self, clist_##tag##_iter_t pos, Value value); \
@@ -148,11 +150,6 @@
#define implement_CList_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
\
STC_API void \
- clist_##tag##_pushN(CList_##tag *self, const Value in[], size_t size) { \
- for (size_t i=0; i<size; ++i) clist_##tag##_pushBack(self, in[i]); \
- } \
- \
- STC_API void \
clist_##tag##_destroy(CList_##tag* self) { \
while (self->last) \
clist_##tag##_popFront(self); \
@@ -169,6 +166,10 @@
if (!self->last) self->last = entry; \
} \
STC_API void \
+ clist_##tag##_pushN(CList_##tag *self, const Value in[], size_t size) { \
+ for (size_t i=0; i<size; ++i) clist_##tag##_pushBack(self, in[i]); \
+ } \
+ STC_API void \
clist_##tag##_popFront(CList_##tag* self) { \
_clist_eraseAfter(self, tag, self->last, valueDestroy); \
} \
diff --git a/stc/cmap.h b/stc/cmap.h
index 4292cec1..720acad8 100644
--- a/stc/cmap.h
+++ b/stc/cmap.h
@@ -81,7 +81,7 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80};
#define declare_CMap_10(tag, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw) \
- declare_CHASH(tag, CMap, cmap, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
+ declare_CHASH(tag, CMap, cmap, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw)
/* CSet: */
@@ -158,6 +158,8 @@ typedef struct { \
uint8_t* _hx; \
} CType##Iter_##tag, ctype##_##tag##_iter_t; \
\
+STC_INLINE CType##_##tag \
+ctype##_##tag##_init(void) {return cmap_init;} \
STC_API CType##_##tag \
ctype##_##tag##_make(size_t initialSize); \
STC_API void \
@@ -199,7 +201,6 @@ typedef Value CType##Value_##tag, ctype##_##tag##_value_t
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
#define implement_CHASH(tag, CType, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw) \
- \
STC_API CType##_##tag \
ctype##_##tag##_make(size_t initialSize) { \
CType##_##tag h = ctype##_init; \
diff --git a/stc/cvec.h b/stc/cvec.h
index 852fb480..32be353a 100644
--- a/stc/cvec.h
+++ b/stc/cvec.h
@@ -49,6 +49,8 @@ typedef struct CVec_##tag { \
Value* data; \
} CVec_##tag; \
\
+STC_INLINE CVec_##tag \
+cvec_##tag##_init(void) {return cvec_init;} \
STC_API CVec_##tag \
cvec_##tag##_make(size_t size, Value null); \
STC_API void \