large_bitset.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*-----------------------------------------------------------------------------+
  2. Author: Joachim Faulhaber
  3. Copyright (c) 2009-2009: Joachim Faulhaber
  4. +------------------------------------------------------------------------------+
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENCE.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. +-----------------------------------------------------------------------------*/
  9. /** Example large_bitset.cpp \file large_bitset.cpp
  10. \brief Shows a bitset class that combines interval and bitset compression
  11. in order to represent very large bitsets efficiently.
  12. Example large_bitset.cpp demonstrates the usage of a large_bitset class
  13. template that is implemented as an interval_map of bitsets. The idea is
  14. to combine interval compression and bitset compression. Large uninterrupted
  15. runs of bits are represented by intervals (interval compression). Local
  16. nests of varying bitsequences are represented by associated bitests
  17. (bitset compression).
  18. Find a commented sample implementation in the boost book documentation
  19. <a href="http://www.joachim-faulhaber.de/boost_itl/doc/libs/icl/doc/html/boost_itl/projects.html#boost_itl.projects.large_bitset">
  20. here</a>.
  21. \include large_bitset_/large_bitset.cpp
  22. */
  23. #if defined(_MSC_VER)
  24. #pragma warning(disable:4244) // Msvc warns on some operations that are needed
  25. #pragma warning(disable:4245) // in this example - we're working on bit level.
  26. #endif // So we intentionally disable them.
  27. //[large_bitset_cpp_includes
  28. #include <limits>
  29. #include "large_bitset.hpp"
  30. using namespace std;
  31. using namespace boost;
  32. using namespace boost::icl;
  33. using namespace mini;
  34. //]
  35. //[large_bitset_test_large_set_all
  36. void test_large()
  37. {
  38. const nat64 much = 0xffffffffffffffffull;
  39. large_bitset<> venti; // ... the largest, I can think of ;)
  40. venti += discrete_interval<nat64>(0, much);
  41. cout << "----- Test function test_large() -----------------------------------------------\n";
  42. cout << "We have just turned on the awesome amount of 18,446,744,073,709,551,616 bits ;-)\n";
  43. venti.show_segments();
  44. //]
  45. //[large_bitset_test_large_erase_last
  46. cout << "---- Let's swich off the very last bit -----------------------------------------\n";
  47. venti -= much;
  48. venti.show_segments();
  49. cout << "---- Venti is plenty ... let's do something small: A tall ----------------------\n\n";
  50. }
  51. //]
  52. //[large_bitset_test_small
  53. void test_small()
  54. {
  55. large_bitset<nat32, bits8> tall; // small is tall ...
  56. // ... because even this 'small' large_bitset
  57. // can represent up to 2^32 == 4,294,967,296 bits.
  58. cout << "----- Test function test_small() -----------\n";
  59. cout << "-- Switch on all bits in range [0,64] ------\n";
  60. tall += discrete_interval<nat>(0, 64);
  61. tall.show_segments();
  62. cout << "--------------------------------------------\n";
  63. cout << "-- Turn off bits: 25,27,28 -----------------\n";
  64. (((tall -= 25) -= 27) -= 28) ;
  65. tall.show_segments();
  66. cout << "--------------------------------------------\n";
  67. cout << "-- Flip bits in range [24,30) --------------\n";
  68. tall ^= discrete_interval<nat>::right_open(24,30);
  69. tall.show_segments();
  70. cout << "--------------------------------------------\n";
  71. cout << "-- Remove the first 10 bits ----------------\n";
  72. tall -= discrete_interval<nat>::right_open(0,10);
  73. tall.show_segments();
  74. cout << "-- Remove even bits in range [0,72) --------\n";
  75. int bit;
  76. for(bit=0; bit<72; bit++) if(!(bit%2)) tall -= bit;
  77. tall.show_segments();
  78. cout << "-- Set odd bits in range [0,72) --------\n";
  79. for(bit=0; bit<72; bit++) if(bit%2) tall += bit;
  80. tall.show_segments();
  81. cout << "--------------------------------------------\n\n";
  82. }
  83. //]
  84. //[large_bitset_test_picturesque
  85. void test_picturesque()
  86. {
  87. typedef large_bitset<nat, bits8> Bit8Set;
  88. Bit8Set square, stare;
  89. square += discrete_interval<nat>(0,8);
  90. for(int i=1; i<5; i++)
  91. {
  92. square += 8*i;
  93. square += 8*i+7;
  94. }
  95. square += discrete_interval<nat>(41,47);
  96. cout << "----- Test function test_picturesque() -----\n";
  97. cout << "-------- empty face: "
  98. << square.interval_count() << " intervals -----\n";
  99. square.show_matrix(" *");
  100. stare += 18; stare += 21;
  101. stare += discrete_interval<nat>(34,38);
  102. cout << "-------- compressed smile: "
  103. << stare.interval_count() << " intervals -----\n";
  104. stare.show_matrix(" *");
  105. cout << "-------- staring bitset: "
  106. << (square + stare).interval_count() << " intervals -----\n";
  107. (square + stare).show_matrix(" *");
  108. cout << "--------------------------------------------\n";
  109. }
  110. //]
  111. template<class NatT, class BitsT>
  112. void test_set()
  113. {
  114. const NatT much = (numeric_limits<NatT>::max)();
  115. large_bitset<NatT, BitsT> venti; //the largest, I can think of
  116. venti += discrete_interval<NatT>(0, much);
  117. cout << "--------------------------------------------------------------------------------\n";
  118. venti.show_segments();
  119. venti -= much;
  120. cout << "--------------------------------------------------------------------------------\n";
  121. venti.show_segments();
  122. }
  123. int main()
  124. {
  125. cout << ">>Interval Container Library: Sample large_bitset.cpp <<\n";
  126. cout << "--------------------------------------------------------\n";
  127. test_large();
  128. test_small();
  129. test_picturesque();
  130. //test_set<nat64,bits64>();
  131. return 0;
  132. }