76#include <tclap/CmdLine.h>
91 cout <<
"\n" << string(60,
'=') <<
"\n";
92 cout <<
" " << title <<
"\n";
93 cout << string(60,
'=') <<
"\n\n";
98 cout <<
"\n--- " << title <<
" ---\n";
103 cout << label <<
" (size=" << ba.
size() <<
"): ";
104 size_t to_show =
min(ba.
size(), max_show);
105 for (
size_t i = 0; i < to_show; ++i)
106 cout << (ba.
read_bit(i) ?
'1' :
'0');
107 if (to_show < ba.
size())
108 cout <<
"... (truncated)";
124 cout <<
"Created BitArray with 16 bits (all zeros)" << endl;
134 cout <<
"After setting bits 0, 3, 7, 15:" << endl;
139 cout <<
"bit[0] = " << ba1.
read_bit(0) << endl;
140 cout <<
"bit[1] = " << ba1.
read_bit(1) << endl;
141 cout <<
"bit[3] = " << ba1.
read_bit(3) << endl;
142 cout <<
"bit[7] = " << ba1.
read_bit(7) << endl;
146 cout <<
"ba1[0] = " << (int)ba1[0] << endl;
147 cout <<
"ba1[3] = " << (int)ba1[3] << endl;
151 cout <<
"Before toggle: bit[3] = " << ba1.
read_bit(3) << endl;
153 cout <<
"After toggle: bit[3] = " << ba1.
read_bit(3) << endl;
160 for (
size_t i = 0; i < ba2.
size(); ++i)
164 for (
size_t i = 0; i < ba2.
size(); ++i)
190 for (
size_t i = 0; i < 8; ++i)
198 for (
size_t i = 0; i < 8; ++i)
206 for (
size_t i = 0; i < 8; ++i)
213 for (
size_t i = 0; i < 8; ++i)
220 for (
size_t i = 0; i < a.
size(); ++i)
222 cout <<
"popcount(a) = " << count_a << endl;
225 for (
size_t i = 0; i < b.
size(); ++i)
227 cout <<
"popcount(b) = " << count_b << endl;
238 cout <<
"Universal set U = {0, 1, 2, 3, 4, 5, 6, 7}" << endl;
250 auto bits_to_set = [](
const BitArray& ba) {
253 for (
size_t i = 0; i < ba.size(); ++i)
257 if (not first) cout <<
", ";
265 cout <<
"A = "; bits_to_set(setA); cout << endl;
266 cout <<
"B = "; bits_to_set(setB); cout << endl;
271 for (
size_t i = 0; i < 8; ++i)
273 cout <<
"A ∪ B = "; bits_to_set(unionAB); cout << endl;
278 for (
size_t i = 0; i < 8; ++i)
280 cout <<
"A ∩ B = "; bits_to_set(intersectAB); cout << endl;
285 for (
size_t i = 0; i < 8; ++i)
287 cout <<
"A - B = "; bits_to_set(diffAB); cout << endl;
292 for (
size_t i = 0; i < 8; ++i)
294 cout <<
"A △ B = "; bits_to_set(symDiffAB); cout << endl;
299 for (
size_t i = 0; i < 8; ++i)
301 cout <<
"A' = "; bits_to_set(compA); cout << endl;
312 cout <<
"Finding all primes up to " << n << endl << endl;
322 size_t sqrt_n =
static_cast<size_t>(
sqrt(
static_cast<double>(n)));
323 for (
size_t i = 2; i <= sqrt_n; ++i)
328 for (
size_t j = i * i; j <= n; j += i)
335 for (
size_t i = 2; i <= n; ++i)
339 cout <<
"Found " <<
primes.size() <<
" primes:\n";
343 primes.for_each([&first, &shown](
size_t p) {
346 if (not first) cout <<
", ";
353 cout <<
", ... (showing first 50)";
357 cout <<
"\nMemory used: " << (n + 1 + 7) / 8 <<
" bytes" << endl;
358 cout <<
"vs. bool array: " << (n + 1) <<
" bytes" << endl;
359 cout <<
"Space savings: " << 100 * (1 - 1.0/8) <<
"%" << endl;
368 universe.
append(
"banana");
369 universe.
append(
"cherry");
371 cout <<
"Universe: {apple, banana, cherry}" << endl;
372 cout <<
"\nAll possible subsets (2^3 = 8):\n" << endl;
375 size_t total_subsets = 1 << n;
377 for (
size_t mask = 0; mask < total_subsets; ++mask)
381 for (
size_t i = 0; i < n; ++i)
388 universe.
for_each([&](
const string& item) {
391 if (not first) cout <<
", ";
405 size_t filter_size = 32;
408 cout <<
"Bloom filter size: " << filter_size <<
" bits\n" << endl;
411 auto hash1 = [filter_size](
const string& s) {
413 for (
char c : s)
h =
h * 31 + c;
414 return h % filter_size;
417 auto hash2 = [filter_size](
const string& s) {
419 for (
char c : s)
h =
h * 37 + c;
420 return h % filter_size;
424 auto add = [&](
const string& item) {
427 cout <<
"Added \"" << item <<
"\" -> bits " << hash1(item)
428 <<
", " << hash2(item) << endl;
432 auto might_contain = [&](
const string& item) {
437 cout <<
"Adding items:\n";
445 cout <<
"\nMembership tests:" << endl;
448 tests.append(
"world");
449 tests.append(
"aleph");
450 tests.append(
"test");
454 tests.for_each([&](
const string& item) {
455 bool result = might_contain(item);
456 cout <<
" \"" << item <<
"\": "
457 << (result ?
"probably in set" :
"definitely NOT in set") << endl;
460 cout <<
"\nNote: Bloom filters may have false positives, but never false negatives." << endl;
471 cout <<
"Comparison of memory usage:\n\n";
473 cout << setw(20) <<
"Size"
474 << setw(20) <<
"BitArray (bytes)"
475 << setw(20) <<
"bool[] (bytes)"
476 << setw(15) <<
"Savings" << endl;
477 cout << string(75,
'-') << endl;
487 size_t bitarray_bytes = (n + 7) / 8;
488 size_t bool_bytes = n;
489 double savings = 100.0 * (1.0 -
static_cast<double>(bitarray_bytes) / bool_bytes);
491 cout << setw(20) << n
492 << setw(20) << bitarray_bytes
493 << setw(20) << bool_bytes
494 << setw(14) << fixed << setprecision(1) << savings <<
"%" << endl;
497 cout <<
"\nBitArray uses 8x less memory than bool arrays!" << endl;
504int main(
int argc,
char* argv[])
509 "BitArray example for Aleph-w.\n"
510 "Demonstrates bit manipulation, set operations, and practical applications.",
514 TCLAP::ValueArg<size_t> sieveArg(
516 "Size for Sieve of Eratosthenes demo (default: 100)",
517 false, 100,
"size",
cmd
520 cmd.parse(argc, argv);
522 size_t sieve_size = sieveArg.getValue();
525 cout <<
"============================================================\n";
526 cout <<
" ALEPH-W BITARRAY EXAMPLE\n";
527 cout <<
"============================================================\n";
537 cout <<
"\n" << string(60,
'=') <<
"\n";
538 cout <<
"BitArray demo completed!\n";
539 cout << string(60,
'=') <<
"\n\n";
543 catch (TCLAP::ArgException& e)
545 cerr <<
"Error: " << e.error() <<
" for argument " << e.argId() << endl;
550 cerr <<
"Error: " << e.what() << endl;
Functional programming utilities for Aleph-w containers.
Space-efficient bit array implementation.
void print_subsection(const string &title)
void demo_bitwise_operations()
void demo_sieve_of_eratosthenes(size_t n)
void demo_simple_bloom_filter()
void demo_basic_operations()
void demo_set_operations()
void print_section(const string &title)
void demo_subset_enumeration()
void print_bits(const string &label, const BitArray &ba, size_t max_show=64)
Contiguous array of bits.
int read_bit(const size_t i) const
Read bit i.
void write_bit(const size_t i, const unsigned int value)
Write bit i with the value.
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sqrt_function > > sqrt(const __gmp_expr< T, U > &expr)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.