summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-08-03 23:40:58 +0200
committerTyge Løvset <[email protected]>2020-08-03 23:40:58 +0200
commit4cff2c39e1bac80d9ab95f293c144bb6c0f30961 (patch)
treecd1f8a7253179f99ccca7adbd16633799fab348a
parent25215a10006e48654a0c2202ad4e0b1a4fdd939b (diff)
downloadSTC-modified-4cff2c39e1bac80d9ab95f293c144bb6c0f30961.tar.gz
STC-modified-4cff2c39e1bac80d9ab95f293c144bb6c0f30961.zip
Improved API.
-rw-r--r--examples/bits.c6
-rw-r--r--examples/prime.c2
-rw-r--r--stc/cbitset.h69
-rw-r--r--stc/cstr.h29
-rw-r--r--stc/cvec.h64
5 files changed, 99 insertions, 71 deletions
diff --git a/examples/bits.c b/examples/bits.c
index 42f7931f..8b3ecd97 100644
--- a/examples/bits.c
+++ b/examples/bits.c
@@ -2,7 +2,7 @@
#include <stc/cbitset.h>
int main() {
- cbitset_t set = cbitset_make(23, true);
+ cbitset_t set = cbitset_with_size(23, true);
printf("count %zu, %zu\n", cbitset_count(set), set.size);
cbitset_reset(&set, 9);
cbitset_resize(&set, 43, false);
@@ -21,7 +21,7 @@ int main() {
printf("%d", cbitset_test(set, i));
puts("");
- cbitset_t s2 = cbitset_make_copy(set);
+ cbitset_t s2 = cbitset_clone(set);
cbitset_flip_all(&s2);
cbitset_set(&s2, 16);
cbitset_set(&s2, 17);
@@ -32,7 +32,7 @@ int main() {
puts("");
printf(" xor: ");
- cbitset_set_xor(&set, s2);
+ cbitset_xor_with(&set, s2);
for (int i=0; i<set.size; ++i)
printf("%d", cbitset_test(set, i));
puts("");
diff --git a/examples/prime.c b/examples/prime.c
index ef2af591..d5b0b186 100644
--- a/examples/prime.c
+++ b/examples/prime.c
@@ -3,7 +3,7 @@
static inline cbitset_t sieveOfEratosthenes(size_t n)
{
- cbitset_t pbits = cbitset_make(n + 1, true);
+ cbitset_t pbits = cbitset_with_size(n + 1, true);
cbitset_reset(&pbits, 0);
cbitset_reset(&pbits, 1);
diff --git a/stc/cbitset.h b/stc/cbitset.h
index 05557ec5..4271c101 100644
--- a/stc/cbitset.h
+++ b/stc/cbitset.h
@@ -27,7 +27,7 @@ Similar to boost::dynamic_bitset / std::bitset
#include "cbitset.h"
int main() {
- cbitset_t set = cbitset_make(23, true);
+ cbitset_t set = cbitset_with_size(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("");
@@ -54,15 +54,18 @@ typedef struct { uint64_t* _arr; size_t size; } cbitset_t;
STC_API void cbitset_resize(cbitset_t* self, size_t size, bool value);
STC_API size_t cbitset_count(cbitset_t set);
+STC_API bool cbitset_is_disjoint(cbitset_t s, cbitset_t other);
+STC_API bool cbitset_is_subset(cbitset_t s, cbitset_t other);
+STC_API bool cbitset_is_superset(cbitset_t s, cbitset_t other);
STC_INLINE void cbitset_set_all(cbitset_t *self, bool value);
-STC_INLINE cbitset_t cbitset_make(size_t size, bool value) {
+STC_INLINE cbitset_t cbitset_with_size(size_t size, bool value) {
cbitset_t set = {(uint64_t *) malloc(((size + 63) >> 6) * 8), size};
cbitset_set_all(&set, value);
return set;
}
-STC_INLINE cbitset_t cbitset_make_copy(cbitset_t other) {
+STC_INLINE cbitset_t cbitset_clone(cbitset_t other) {
size_t bytes = ((other.size + 63) >> 6) * 8;
cbitset_t set = {(uint64_t *) memcpy(malloc(bytes), other._arr, bytes), other.size};
return set;
@@ -101,38 +104,38 @@ STC_INLINE void cbitset_flip_all(cbitset_t *self) {
for (size_t i=0; i<n; ++i) self->_arr[i] ^= ~0ull;
}
/* Intersection */
-STC_INLINE void cbitset_set_and(cbitset_t *self, cbitset_t other) {
+STC_INLINE void cbitset_intersect_with(cbitset_t *self, cbitset_t 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_set_or(cbitset_t *self, cbitset_t other) {
+STC_INLINE void cbitset_union_with(cbitset_t *self, cbitset_t 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_set_xor(cbitset_t *self, cbitset_t other) {
+STC_INLINE void cbitset_xor_with(cbitset_t *self, cbitset_t 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_t cbitset_and(cbitset_t s1, cbitset_t s2) {
- cbitset_t set = cbitset_make_copy(s1);
- cbitset_set_and(&set, s2); return set;
+STC_INLINE cbitset_t cbitset_intersect(cbitset_t s1, cbitset_t s2) {
+ cbitset_t set = cbitset_clone(s1);
+ cbitset_intersect_with(&set, s2); return set;
}
-STC_INLINE cbitset_t cbitset_or(cbitset_t s1, cbitset_t s2) {
- cbitset_t set = cbitset_make_copy(s1);
- cbitset_set_or(&set, s2); return set;
+STC_INLINE cbitset_t cbitset_union(cbitset_t s1, cbitset_t s2) {
+ cbitset_t set = cbitset_clone(s1);
+ cbitset_union_with(&set, s2); return set;
}
STC_INLINE cbitset_t cbitset_xor(cbitset_t s1, cbitset_t s2) {
- cbitset_t set = cbitset_make_copy(s1);
- cbitset_set_xor(&set, s2); return set;
+ cbitset_t set = cbitset_clone(s1);
+ cbitset_xor_with(&set, s2); return set;
}
STC_INLINE cbitset_t cbitset_not(cbitset_t s1) {
- cbitset_t set = cbitset_make_copy(s1);
+ cbitset_t set = cbitset_clone(s1);
cbitset_flip_all(&set); return set;
}
@@ -145,8 +148,8 @@ STC_API void cbitset_resize(cbitset_t* self, size_t size, bool value) {
if (new_n >= old_n) {
memset(self->_arr + old_n, value ? 0xff : 0x0, (new_n - old_n) * 8);
if (old_n > 0) {
- uint64_t mask = (1ull << (osize & 63)) - 1;
- value ? (self->_arr[old_n - 1] |= ~mask) : (self->_arr[old_n - 1] &= mask);
+ uint64_t m = (1ull << (osize & 63)) - 1; /* mask */
+ value ? (self->_arr[old_n - 1] |= ~m) : (self->_arr[old_n - 1] &= m);
}
}
}
@@ -168,15 +171,37 @@ static inline uint64_t c_popcount64(uint64_t x) {
}
#endif
-STC_API size_t cbitset_count(cbitset_t set) {
- size_t count = 0, n = ((set.size + 63) >> 6) - 1;
- if (set.size > 0) {
- for (size_t i=0; i<n; ++i) count += c_popcount64(set._arr[i]);
- count += c_popcount64(set._arr[n] & ((1ull << (set.size & 63)) - 1));
+STC_API size_t cbitset_count(cbitset_t s) {
+ size_t count = 0, n = ((s.size + 63) >> 6) - 1;
+ if (s.size > 0) {
+ for (size_t i=0; i<n; ++i) count += c_popcount64(s._arr[i]);
+ count += c_popcount64(s._arr[n] & ((1ull << (s.size & 63)) - 1));
}
return count;
}
+#define _cbitset_SETOP(op) \
+ if (s.size == 0) return false; /* ? */ \
+ size_t n = ((s.size + 63) >> 6) - 1; \
+ for (size_t i=0; i<n; ++i) \
+ if ((s._arr[i] op other._arr[i]) != s._arr[i]) \
+ return false; \
+ uint64_t m = (1ull << (s.size & 63)) - 1, last = s._arr[n] & m; \
+ return (last op (other._arr[n] & m)) == last
+
+STC_API bool cbitset_is_disjoint(cbitset_t s, cbitset_t other) {
+ _cbitset_SETOP(^);
+}
+
+STC_API bool cbitset_is_subset(cbitset_t s, cbitset_t other) {
+ _cbitset_SETOP(|);
+}
+
+STC_API bool cbitset_is_superset(cbitset_t s, cbitset_t other) {
+ _cbitset_SETOP(&);
+}
+
+
#endif
#endif \ No newline at end of file
diff --git a/stc/cstr.h b/stc/cstr.h
index a75d7200..f9e2f884 100644
--- a/stc/cstr.h
+++ b/stc/cstr.h
@@ -53,8 +53,6 @@ STC_API void
cstr_reserve(cstr_t* self, size_t cap);
STC_API void
cstr_resize(cstr_t* self, size_t len, char fill);
-STC_API cstr_t
-cstr_make_reserved(size_t cap);
STC_API cstr_t*
cstr_assign_n(cstr_t* self, const char* str, size_t len);
STC_API cstr_t*
@@ -70,15 +68,21 @@ cstr_strnstr(cstr_t s, size_t pos, const char* needle, size_t n);
STC_INLINE void
cstr_destroy(cstr_t* self) {
- if (cstr_capacity(*self)) {
+ if (cstr_capacity(*self))
free(_cstr_rep(self));
- }
}
STC_INLINE cstr_t
-cstr_make_filled(size_t len, char fill) {
+cstr_with_capacity(size_t cap) {
+ cstr_t s = cstr_init;
+ cstr_reserve(&s, cap);
+ return s;
+}
+
+STC_INLINE cstr_t
+cstr_with_size(size_t len, char fill) {
cstr_t s = cstr_init;
- if (len) cstr_resize(&s, len, fill);
+ cstr_resize(&s, len, fill);
return s;
}
@@ -88,7 +92,7 @@ cstr_make(const char* str) {
}
STC_INLINE cstr_t
-cstr_make_copy(cstr_t s) {
+cstr_clone(cstr_t s) {
return cstr_make_n(s.str, cstr_size(s));
}
@@ -219,15 +223,6 @@ cstr_resize(cstr_t* self, size_t len, char fill) {
}
STC_API cstr_t
-cstr_make_reserved(size_t cap) {
- if (cap == 0) return cstr_init;
- size_t *rep = (size_t *) malloc(_cstr_mem(cap));
- cstr_t s = {(char *) (rep + 2)};
- rep[0] = 0, rep[1] = cap, s.str[0] = '\0';
- return s;
-}
-
-STC_API cstr_t
cstr_make_n(const char* str, size_t len) {
if (len == 0) return cstr_init;
size_t *rep = (size_t *) malloc(_cstr_mem(len));
@@ -244,7 +239,7 @@ cstr_from(const char* fmt, ...) {
va_start(args, fmt);
int len = vsnprintf(c_nullptr, (size_t)0, fmt, args);
if (len > 0) {
- tmp = cstr_make_reserved(len);
+ tmp = cstr_with_capacity(len);
vsprintf(tmp.str, fmt, args);
_cstr_size(tmp) = len;
}
diff --git a/stc/cvec.h b/stc/cvec.h
index 01cc718d..42d430a7 100644
--- a/stc/cvec.h
+++ b/stc/cvec.h
@@ -51,20 +51,45 @@ typedef struct cvec_##tag { \
Value* data; \
} cvec_##tag; \
\
-STC_INLINE cvec_##tag \
-cvec_##tag##_init(void) {cvec_##tag x = cvec_init; return x;} \
-STC_API cvec_##tag \
-cvec_##tag##_make(size_t size, Value null); \
-STC_API void \
-cvec_##tag##_push_n(cvec_##tag *self, const Value in[], size_t size); \
STC_API void \
cvec_##tag##_destroy(cvec_##tag* self); \
STC_API void \
cvec_##tag##_reserve(cvec_##tag* self, size_t cap); \
STC_API void \
-cvec_##tag##_clear(cvec_##tag* self); \
+cvec_##tag##_resize(cvec_##tag* self, size_t size, Value null_val); \
+STC_API void \
+cvec_##tag##_push_n(cvec_##tag *self, const Value in[], size_t size); \
STC_API void \
cvec_##tag##_push_back(cvec_##tag* self, Value value); \
+STC_API void \
+cvec_##tag##_insert(cvec_##tag* self, size_t pos, Value value); \
+STC_API void \
+cvec_##tag##_erase(cvec_##tag* self, size_t pos, size_t size); \
+STC_API void \
+cvec_##tag##_sort(cvec_##tag* self); \
+STC_API size_t \
+cvec_##tag##_find(const cvec_##tag* self, RawValue rawValue); \
+ \
+STC_INLINE cvec_##tag \
+cvec_##tag##_init(void) { \
+ cvec_##tag x = cvec_init; return x; \
+} \
+STC_INLINE cvec_##tag \
+cvec_##tag##_with_size(size_t size, Value null_val) { \
+ cvec_##tag x = cvec_init; \
+ 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##_reserve(&x, size); \
+ return x; \
+} \
+STC_INLINE void \
+cvec_##tag##_clear(cvec_##tag* self) { \
+ if (self->data) _cvec_size(*self) = 0; \
+} \
STC_INLINE void \
cvec_##tag##_pop_back(cvec_##tag* self) { \
valueDestroy(&self->data[_cvec_size(*self) - 1]); \
@@ -76,14 +101,6 @@ STC_INLINE Value* \
cvec_##tag##_back(cvec_##tag* self) {return self->data + _cvec_size(*self) - 1;} \
STC_INLINE Value* \
cvec_##tag##_at(cvec_##tag* self, size_t i) {return self->data + i;} \
-STC_API void \
-cvec_##tag##_insert(cvec_##tag* self, size_t pos, Value value); \
-STC_API void \
-cvec_##tag##_erase(cvec_##tag* self, size_t pos, size_t size); \
-STC_API void \
-cvec_##tag##_sort(cvec_##tag* self); \
-STC_API size_t \
-cvec_##tag##_find(const cvec_##tag* self, RawValue rawValue); \
STC_INLINE void \
cvec_##tag##_swap(cvec_##tag* a, cvec_##tag* b) { \
c_swap(Value*, a->data, b->data); \
@@ -114,14 +131,6 @@ typedef RawValue cvec_##tag##_rawvalue_t
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
#define implement_cvec_6(tag, Value, valueDestroy, RawValue, valueCompareRaw, valueGetRaw) \
\
-STC_API cvec_##tag \
-cvec_##tag##_make(size_t size, Value null) { \
- cvec_##tag vec = cvec_init; \
- cvec_##tag##_reserve(&vec, size); \
- _cvec_size(vec) = size; \
- for (size_t i=0; i<size; ++i) vec.data[i] = null; \
- return vec; \
-} \
STC_API void \
cvec_##tag##_push_n(cvec_##tag *self, const Value in[], size_t size) { \
cvec_##tag##_reserve(self, cvec_size(*self) + size); \
@@ -147,12 +156,11 @@ cvec_##tag##_reserve(cvec_##tag* self, size_t cap) { \
rep[1] = cap; \
} \
} \
- \
STC_API void \
-cvec_##tag##_clear(cvec_##tag* self) { \
- cvec_##tag cv = cvec_init; \
- cvec_##tag##_destroy(self); \
- *self = cv; \
+cvec_##tag##_resize(cvec_##tag* self, size_t size, Value null_val) { \
+ cvec_##tag##_reserve(self, size); \
+ for (size_t i=cvec_size(*self); i<size; ++i) self->data[i] = null_val; \
+ if (self->data) _cvec_size(*self) = size; \
} \
\
STC_API void \