summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-03-19 21:01:33 +0100
committerTyge Løvset <[email protected]>2021-03-19 21:01:33 +0100
commitd684194487c444231c69cc82147cc8e2ac8cccd1 (patch)
tree6c1f26fe68f55346b212c4d604d45eca09900949
parent1d18ffb5e0ddaf7430b62bfc02aa5b4da5695d26 (diff)
downloadSTC-modified-d684194487c444231c69cc82147cc8e2ac8cccd1.tar.gz
STC-modified-d684194487c444231c69cc82147cc8e2ac8cccd1.zip
Trimmed down and simplified cbits API. Fixed cbits_is_disjoint(), was wrong.
-rw-r--r--docs/cbits_api.md25
-rw-r--r--examples/bits.c8
-rw-r--r--stc/cbits.h59
3 files changed, 21 insertions, 71 deletions
diff --git a/docs/cbits_api.md b/docs/cbits_api.md
index 373b7327..85a08fb1 100644
--- a/docs/cbits_api.md
+++ b/docs/cbits_api.md
@@ -26,24 +26,18 @@ void cbits_clear(cbits* self);
void cbits_resize(cbits* self, size_t size, bool value);
void cbits_del(cbits* self);
-cbits cbits_intersect(cbits s1, cbits s2);
-cbits cbits_union(cbits s1, cbits s2);
-cbits cbits_xor(cbits s1, cbits s2);
-cbits cbits_not(cbits s1);
-
cbits* cbits_assign(cbits* self, cbits other);
cbits* cbits_take(cbits* self, cbits other);
cbits cbits_move(cbits* self);
size_t cbits_size(cbits set);
size_t cbits_count(cbits set);
-bool cbits_is_disjoint(cbits set, cbits other);
-bool cbits_is_subset(cbits set, cbits other);
-bool cbits_is_superset(cbits set, cbits other);
-char* cbits_to_str(cbits set, char* str, size_t start, intptr_t stop);
bool cbits_test(cbits set, size_t i);
-bool cbits_at(cbits set, size_t i); // same as cbits_test()
+bool cbits_at(cbits set, size_t i); // same as cbits_test()
+bool cbits_is_subset(cbits set, cbits other);
+bool cbits_is_disjoint(cbits set, cbits other);
+char* cbits_to_str(cbits set, char* str, size_t start, intptr_t stop);
void cbits_set(cbits *self, size_t i);
void cbits_reset(cbits *self, size_t i);
@@ -53,14 +47,9 @@ void cbits_set_all(cbits *self, bool value);
void cbits_set_all64(cbits *self, uint64_t pattern);
void cbits_flip_all(cbits *self);
-void cbits_intersect_with(cbits *self, cbits other);
-void cbits_union_with(cbits *self, cbits other);
-void cbits_xor_with(cbits *self, cbits other);
-
-cbits_iter_t cbits_begin(cbits* self);
-cbits_iter_t cbits_end(cbits* self);
-void cbits_next(cbits_iter_t* it);
-bool cbits_itval(cbits_iter_t it);
+void cbits_intersect(cbits *self, cbits other);
+void cbits_union(cbits *self, cbits other);
+void cbits_xor(cbits *self, cbits other);
```
## Types
diff --git a/examples/bits.c b/examples/bits.c
index fb48c864..c8b037a2 100644
--- a/examples/bits.c
+++ b/examples/bits.c
@@ -28,10 +28,10 @@ int main() {
c_forrange (i, int, set.size)
printf("%d", cbits_test(set, i));
- puts("\nIterator:");
+ puts("\nIterate:");
printf("%4zu: ", set.size);
- c_foreach (i, cbits, set)
- printf("%d", cbits_itval(i));
+ c_forrange (i, int, set.size)
+ printf("%d", cbits_test(set, i));
puts("");
cbits_t s2 = cbits_clone(set);
@@ -45,7 +45,7 @@ int main() {
puts("");
printf(" xor: ");
- cbits_xor_with(&set, s2);
+ cbits_xor(&set, s2);
c_forrange (i, int, set.size)
printf("%d", cbits_test(set, i));
puts("");
diff --git a/stc/cbits.h b/stc/cbits.h
index 3007649f..d5618bba 100644
--- a/stc/cbits.h
+++ b/stc/cbits.h
@@ -64,9 +64,8 @@ STC_API cbits_t cbits_clone(cbits_t other);
STC_API void cbits_resize(cbits_t* self, size_t size, bool value);
STC_API cbits_t* cbits_assign(cbits_t* self, cbits_t other);
STC_API size_t cbits_count(cbits_t set);
-STC_API bool cbits_is_disjoint(cbits_t set, cbits_t other);
STC_API bool cbits_is_subset(cbits_t set, cbits_t other);
-STC_API bool cbits_is_superset(cbits_t set, cbits_t other);
+STC_API bool cbits_is_disjoint(cbits_t set, cbits_t other);
STC_INLINE cbits_t cbits_init() { cbits_t set = {NULL, 0}; return set; }
STC_INLINE void cbits_clear(cbits_t* self) { self->size = 0; }
@@ -122,61 +121,24 @@ STC_INLINE void cbits_flip_all(cbits_t *self) {
}
/* Intersection */
-STC_INLINE void cbits_intersect_with(cbits_t *self, cbits_t other) {
+STC_INLINE void cbits_intersect(cbits_t *self, cbits_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 cbits_union_with(cbits_t *self, cbits_t other) {
+STC_INLINE void cbits_union(cbits_t *self, cbits_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 cbits_xor_with(cbits_t *self, cbits_t other) {
+STC_INLINE void cbits_xor(cbits_t *self, cbits_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 cbits_t cbits_intersect(cbits_t s1, cbits_t s2) {
- cbits_t set = cbits_clone(s1);
- cbits_intersect_with(&set, s2); return set;
-}
-
-STC_INLINE cbits_t cbits_union(cbits_t s1, cbits_t s2) {
- cbits_t set = cbits_clone(s1);
- cbits_union_with(&set, s2); return set;
-}
-
-STC_INLINE cbits_t cbits_xor(cbits_t s1, cbits_t s2) {
- cbits_t set = cbits_clone(s1);
- cbits_xor_with(&set, s2); return set;
-}
-
-STC_INLINE cbits_t cbits_not(cbits_t s1) {
- cbits_t set = cbits_clone(s1);
- cbits_flip_all(&set); return set;
-}
-
-typedef struct {
- cbits_t *_bs;
- size_t ref;
-} cbits_iter_t;
-
-STC_INLINE cbits_iter_t
-cbits_begin(cbits_t* self) { cbits_iter_t it = {self, 0}; return it; }
-
-STC_INLINE cbits_iter_t
-cbits_end(cbits_t* self) { cbits_iter_t it = {self, self->size}; return it; }
-
-STC_INLINE void
-cbits_next(cbits_iter_t* it) { ++it->ref; }
-
-STC_INLINE bool
-cbits_itval(cbits_iter_t it) { return cbits_test(*it._bs, it.ref); }
-
#if defined(__GNUC__) || defined(__clang__)
STC_INLINE uint64_t cpopcount64(uint64_t x) {return __builtin_popcountll(x);}
#elif defined(_MSC_VER) && defined(_WIN64)
@@ -243,19 +205,18 @@ STC_DEF size_t cbits_count(cbits_t s) {
return count;
}
-#define _cbits_SETOP(OPR) \
+#define _cbits_SETOP(OPR, x) \
assert(s.size == other.size); \
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] OPR other._arr[i]) != s._arr[i]) \
+ if ((s._arr[i] OPR other._arr[i]) != x) \
return false; \
- uint64_t m = (1ull << (s.size & 63)) - 1, last = s._arr[n] & m; \
- return (last OPR (other._arr[n] & m)) == last
+ uint64_t i = n, m = (1ull << (s.size & 63)) - 1; \
+ return ((s._arr[i] & m) OPR (other._arr[i] & m)) == (x & m)
-STC_DEF bool cbits_is_disjoint(cbits_t s, cbits_t other) { _cbits_SETOP(^); }
-STC_DEF bool cbits_is_subset(cbits_t s, cbits_t other) { _cbits_SETOP(|); }
-STC_DEF bool cbits_is_superset(cbits_t s, cbits_t other) { _cbits_SETOP(&); }
+STC_DEF bool cbits_is_subset(cbits_t s, cbits_t other) { _cbits_SETOP(|, s._arr[i]); }
+STC_DEF bool cbits_is_disjoint(cbits_t s, cbits_t other) { _cbits_SETOP(^, ~0ull); }
#endif
#endif