76#include <tclap/CmdLine.h>
91 cout <<
"\n" << string(60,
'=') <<
"\n";
93 cout << string(60,
'=') <<
"\n\n";
103 cout << label <<
" (size=" <<
ba.
size() <<
"): ";
105 for (
size_t i = 0; i <
to_show; ++i)
106 cout << (
ba.read_bit(i) ?
'1' :
'0');
108 cout <<
"... (truncated)";
124 cout <<
"Created BitArray with 16 bits (all zeros)" <<
endl;
132 ba1.write_bit(15, 1);
134 cout <<
"After setting bits 0, 3, 7, 15:" <<
endl;
151 cout <<
"Before toggle: bit[3] = " <<
ba1.read_bit(3) <<
endl;
152 ba1.write_bit(3,
ba1.read_bit(3) ? 0 : 1);
153 cout <<
"After toggle: bit[3] = " <<
ba1.read_bit(3) <<
endl;
154 ba1.write_bit(3,
ba1.read_bit(3) ? 0 : 1);
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)
225 for (
size_t i = 0; i < b.
size(); ++i)
238 cout <<
"Universal set U = {0, 1, 2, 3, 4, 5, 6, 7}" <<
endl;
242 setA.write_bit(0, 1);
setA.write_bit(1, 1);
243 setA.write_bit(2, 1);
setA.write_bit(3, 1);
247 setB.write_bit(2, 1);
setB.write_bit(3, 1);
248 setB.write_bit(4, 1);
setB.write_bit(5, 1);
253 for (
size_t i = 0; i <
ba.
size(); ++i)
271 for (
size_t i = 0; i < 8; ++i)
278 for (
size_t i = 0; i < 8; ++i)
285 for (
size_t i = 0; i < 8; ++i)
292 for (
size_t i = 0; i < 8; ++i)
299 for (
size_t i = 0; i < 8; ++i)
300 compA.write_bit(i,
setA.read_bit(i) ? 0 : 1);
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)
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;
371 cout <<
"Universe: {apple, banana, cherry}" <<
endl;
372 cout <<
"\nAll possible subsets (2^3 = 8):\n" <<
endl;
381 for (
size_t i = 0; i < n; ++i)
391 if (not first) cout <<
", ";
413 for (
char c : s)
h =
h * 31 + c;
419 for (
char c : s)
h =
h * 37 + c;
424 auto add = [&](
const string& item) {
427 cout <<
"Added \"" << item <<
"\" -> bits " <<
hash1(item)
437 cout <<
"Adding items:\n";
445 cout <<
"\nMembership tests:" <<
endl;
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";
474 <<
setw(20) <<
"BitArray (bytes)"
475 <<
setw(20) <<
"bool[] (bytes)"
497 cout <<
"\nBitArray uses 8x less memory than bool arrays!" <<
endl;
509 "BitArray example for Aleph-w.\n"
510 "Demonstrates bit manipulation, set operations, and practical applications.",
516 "Size for Sieve of Eratosthenes demo (default: 100)",
517 false, 100,
"size",
cmd
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.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Append a new item by copy.
size_t size() const noexcept
Count the number of elements of the list.
void for_each(Operation &operation)
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.