summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/bitsets
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples/bitsets')
-rw-r--r--misc/examples/bitsets/bits.c66
-rw-r--r--misc/examples/bitsets/bits2.c41
-rw-r--r--misc/examples/bitsets/prime.c58
3 files changed, 165 insertions, 0 deletions
diff --git a/misc/examples/bitsets/bits.c b/misc/examples/bitsets/bits.c
new file mode 100644
index 00000000..e0a11346
--- /dev/null
+++ b/misc/examples/bitsets/bits.c
@@ -0,0 +1,66 @@
+#include <stdio.h>
+#include <stc/cbits.h>
+
+int main(void)
+{
+ cbits set = cbits_with_size(23, true);
+ cbits s2;
+ c_defer(
+ cbits_drop(&set),
+ cbits_drop(&s2)
+ ){
+ printf("count %lld, %lld\n", cbits_count(&set), cbits_size(&set));
+ cbits s1 = cbits_from("1110100110111");
+ char buf[256];
+ cbits_to_str(&s1, buf, 0, 255);
+ printf("buf: %s: %lld\n", buf, cbits_count(&s1));
+ cbits_drop(&s1);
+
+ cbits_reset(&set, 9);
+ cbits_resize(&set, 43, false);
+ printf(" str: %s\n", cbits_to_str(&set, buf, 0, 255));
+
+ printf("%4lld: ", cbits_size(&set));
+ c_forrange (i, cbits_size(&set))
+ printf("%d", cbits_test(&set, i));
+ puts("");
+
+ cbits_set(&set, 28);
+ cbits_resize(&set, 77, true);
+ cbits_resize(&set, 93, false);
+ cbits_resize(&set, 102, true);
+ cbits_set_value(&set, 99, false);
+ printf("%4lld: ", cbits_size(&set));
+ c_forrange (i, cbits_size(&set))
+ printf("%d", cbits_test(&set, i));
+
+ puts("\nIterate:");
+ printf("%4lld: ", cbits_size(&set));
+ c_forrange (i, cbits_size(&set))
+ printf("%d", cbits_test(&set, i));
+ puts("");
+
+ // Make a clone
+ s2 = cbits_clone(set);
+ cbits_flip_all(&s2);
+ cbits_set(&s2, 16);
+ cbits_set(&s2, 17);
+ cbits_set(&s2, 18);
+ printf(" new: ");
+ c_forrange (i, cbits_size(&s2))
+ printf("%d", cbits_test(&s2, i));
+ puts("");
+
+ printf(" xor: ");
+ cbits_xor(&set, &s2);
+ c_forrange (i, cbits_size(&set))
+ printf("%d", cbits_test(&set, i));
+ puts("");
+
+ cbits_set_all(&set, false);
+ printf("%4lld: ", cbits_size(&set));
+ c_forrange (i, cbits_size(&set))
+ printf("%d", cbits_test(&set, i));
+ puts("");
+ }
+}
diff --git a/misc/examples/bitsets/bits2.c b/misc/examples/bitsets/bits2.c
new file mode 100644
index 00000000..de2f16f4
--- /dev/null
+++ b/misc/examples/bitsets/bits2.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+// Example of static sized (stack allocated) bitsets
+
+#define i_type Bits
+#define i_capacity 80 // enable fixed bitset on the stack
+#include <stc/cbits.h>
+
+int main(void)
+{
+ Bits s1 = Bits_from("1110100110111");
+
+ printf("size %lld\n", Bits_size(&s1));
+ char buf[256];
+ Bits_to_str(&s1, buf, 0, 256);
+ printf("buf: %s: count=%lld\n", buf, Bits_count(&s1));
+
+ Bits_reset(&s1, 8);
+ printf(" s1: %s\n", Bits_to_str(&s1, buf, 0, 256));
+
+ Bits s2 = Bits_clone(s1);
+
+ Bits_flip_all(&s2);
+ Bits_reset(&s2, 66);
+ Bits_reset(&s2, 67);
+ printf(" s2: ");
+ c_forrange (i, Bits_size(&s2))
+ printf("%d", Bits_test(&s2, i));
+ puts("");
+
+ printf("xor: ");
+ Bits_xor(&s1, &s2);
+ c_forrange (i, Bits_size(&s1))
+ printf("%d", Bits_test(&s1, i));
+ puts("");
+
+ printf("all: ");
+ Bits_set_pattern(&s1, 0x3333333333333333);
+ c_forrange (i, Bits_size(&s1))
+ printf("%d", Bits_test(&s1, i));
+ puts("");
+}
diff --git a/misc/examples/bitsets/prime.c b/misc/examples/bitsets/prime.c
new file mode 100644
index 00000000..7e5a2b3f
--- /dev/null
+++ b/misc/examples/bitsets/prime.c
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <math.h>
+#include <time.h>
+#include <stc/cbits.h>
+#include <stc/algorithm.h>
+
+typedef long long llong;
+
+cbits sieveOfEratosthenes(llong n)
+{
+ cbits bits = cbits_with_size(n/2 + 1, true);
+ llong q = (llong)sqrt((double) n) + 1;
+ for (llong i = 3; i < q; i += 2) {
+ llong j = i;
+ for (; j < n; j += 2) {
+ if (cbits_test(&bits, j>>1)) {
+ i = j;
+ break;
+ }
+ }
+ for (llong j = i*i; j < n; j += i*2)
+ cbits_reset(&bits, j>>1);
+ }
+ return bits;
+}
+
+int main(void)
+{
+ llong n = 100000000;
+ printf("Computing prime numbers up to %lld\n", n);
+
+ clock_t t = clock();
+ cbits primes = sieveOfEratosthenes(n + 1);
+
+ llong np = cbits_count(&primes);
+ t = clock() - t;
+
+ printf("Number of primes: %lld, time: %f\n\n", np, (double)t/CLOCKS_PER_SEC);
+
+ puts("Show all the primes in the range [2, 1000):");
+ printf("2");
+ c_forrange (i, 3, 1000, 2)
+ if (cbits_test(&primes, i>>1)) printf(" %lld", i);
+ puts("\n");
+
+ puts("Show the last 50 primes using a temporary crange generator:");
+ crange range = crange_init(n - 1, 0, -2);
+
+ c_forfilter (i, crange, range,
+ cbits_test(&primes, *i.ref/2) &&
+ c_flt_take(i, 50)
+ ){
+ printf("%lld ", *i.ref);
+ if (c_flt_getcount(i) % 10 == 0) puts("");
+ }
+
+ cbits_drop(&primes);
+}