Mlxa

UltraSet

Aug 8th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. #undef _GLIBCXX_DEBUG                                                   // bitset isn't working...
  2. #define all(x) begin(x), end(x)
  3. #pragma GCC optimize("O3")
  4. #pragma GCC target("popcnt")
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. template<class A>
  9. void addlog(A a) {
  10.     cerr << a << endl;
  11. }
  12. template<class A, class... B>
  13. void addlog(A a, B... b) {
  14.     cerr << a << ' ';
  15.     addlog(b...);
  16. }
  17.  
  18. typedef uint64_t Long;
  19. constexpr uint32_t W = 64;                                          // Long width.
  20. constexpr uint32_t LOG_W = (uint32_t)log2(W);                       // Log2 of Long width.
  21. constexpr uint32_t L = 1;                                           // # of longs.
  22. constexpr uint32_t B = W * L;                                           // # of bits.
  23. constexpr Long ONE = 1;
  24.  
  25. bool test_bit(Long mask, uint32_t bit) {
  26.     return (mask >> bit) & ONE;
  27. }
  28.  
  29. __attribute__((warn_unused_result))
  30. Long set_bit(const Long mask, uint32_t bit) {
  31.     return mask | (ONE << bit);
  32. }
  33.  
  34. __attribute__((warn_unused_result))
  35. Long reset_bit(const Long mask, uint32_t bit) {
  36.     return mask & (~(ONE << bit));
  37. }
  38.  
  39. void print_bits(ostream &out, const Long mask) {
  40.     for (uint32_t i = 0; i < W; ++i) {
  41.         out << test_bit(mask, i);
  42.     } out << ' ';
  43. }
  44.  
  45. struct ultra_set {
  46.     bitset<L> not_empty;
  47.     vector<Long> longs;
  48.     bool zipped;
  49.    
  50.     vector<Long>::iterator iter;
  51.     uint32_t iter_pos;
  52.    
  53.     void clear_iter() {
  54.         iter = begin(longs);
  55.         iter_pos = 0;
  56.     }
  57.    
  58.     Long get_next_zipped() {
  59.         if (not_empty[iter_pos]) {
  60.             ++iter_pos;
  61.             return *(iter++);
  62.         } return 0;
  63.     }
  64.    
  65.     // Zero-filling constructor.
  66.     ultra_set(bool zip = true) {
  67.         not_empty.reset();
  68.         if (zip)
  69.             longs.clear();
  70.         else
  71.             longs.assign(L, 0);
  72.         zipped = zip;
  73.     }
  74.    
  75.     void unzip() {
  76.         assert(zipped == true);
  77.         vector<Long> temp;
  78.         temp.reserve(L);
  79.         clear_iter();
  80.         for (uint32_t i = 0; i < L; ++i)
  81.             temp.push_back(get_next_zipped());
  82.         swap(longs, temp);
  83.         zipped = false;
  84.     }
  85.    
  86.     void zip() {
  87.         assert(zipped == false);
  88.         vector<Long> temp;
  89.         for (auto current : longs)
  90.             if (current)
  91.                 temp.push_back(current);
  92.         swap(longs, temp);
  93.         zipped = true;
  94.     }
  95.    
  96.     bool test(uint32_t k) const {
  97.         assert(zipped == false);
  98.         assert(k < B);
  99.         uint32_t word = k >> LOG_W,  bit = k & (W - 1);
  100.         return test_bit(longs[word], bit);
  101.     }
  102.    
  103.     void set(uint32_t k) {
  104.         assert(zipped == false);
  105.         assert(k < B);
  106.         uint32_t word = k >> LOG_W,  bit = k & (W - 1);
  107.         longs[word] = set_bit(longs[word], bit);
  108.         not_empty[word] = true;
  109.     }
  110.    
  111.     void reset(uint32_t k) {
  112.         assert(zipped == false);
  113.         assert(k < B);
  114.         uint32_t word = k >> LOG_W,  bit = k & (W - 1);
  115.         longs[word] = reset_bit(longs[word], bit);
  116.         if (longs[word] == 0)
  117.             not_empty[word] = false;
  118.     }
  119.    
  120.     // From left to right.
  121.     friend ostream &operator<<(ostream &out, ultra_set &u) {
  122.         if (u.zipped) {
  123.             u.clear_iter();
  124.             for (uint32_t i = 0; i < L; ++i) {
  125.                 print_bits(out, u.get_next_zipped());
  126.             }
  127.         } else {
  128.             for (auto l : u.longs)
  129.                 print_bits(out, l);
  130.         }
  131.         return out;
  132.     }
  133. };
  134.  
  135.  
  136. // |= first k bits.
  137. void move_bits(ultra_set &from, ultra_set &to, uint32_t k) {
  138.     assert(from.zipped && to.zipped);
  139.     from.clear_iter();
  140.     to.clear_iter();
  141.     vector<Long> a_temp, b_temp;
  142.     for (uint32_t i = 0; i < L; ++i) {
  143.         Long a = to.get_next_zipped();
  144.         Long b = from.get_next_zipped();
  145.         if (k < W) {
  146.             if (a | ((b << (W - k)) >> (W - k))) {
  147.                 a_temp.push_back(a | ((b << (W - k)) >> (W - k)));
  148.                 to.not_empty.set(i);
  149.             } else {
  150.                 to.not_empty.reset(i);
  151.             }
  152.            
  153.             if ((b >> k) << k) {
  154.                 b_temp.push_back((b >> k) << k);
  155.                 from.not_empty.set(i);
  156.             } else {
  157.                 from.not_empty.reset(i);
  158.             }
  159.            
  160.             break;
  161.         } else {
  162.             if (a | b) {
  163.                 a_temp.push_back(a | b);
  164.                 to.not_empty.set(i);
  165.             }
  166.             from.not_empty.reset(i);
  167.             k -= W;
  168.         }
  169.     }
  170.     swap(to.longs, a_temp);
  171.     swap(from.longs, b_temp);
  172. }
  173.  
  174. void get_bits(ultra_set &from, vector<uint32_t> &to) {
  175.     assert(from.zipped == true);
  176.     to.clear();
  177.     from.clear_iter();
  178.     for (uint32_t i = 0; i < L; ++i) {
  179.         Long l = from.get_next_zipped();
  180.         addlog("l =", l);
  181.         if (l) {
  182.             for (uint32_t j = 0; j < W; ++j, l >>= 1) {
  183.                 if (l & ONE) {
  184.                     to.push_back(i + j); {
  185.                     }
  186.                 }
  187.             }
  188.         }
  189.     }
  190. }
  191.  
  192. int main() {
  193.     ultra_set a, b;
  194.     a.unzip();
  195.     b.unzip();
  196.     a.set(1);
  197.     a.set(3);
  198.     a.set(5);
  199.     b.set(2);
  200.     b.set(4);
  201.     a.zip();
  202.     b.zip();
  203.     addlog(a);
  204.     addlog(b);
  205.     move_bits(a, b, 5);
  206.     addlog(a);
  207.     addlog(b);
  208.     vector<uint32_t> v;
  209.     get_bits(b, v);
  210.     for (auto e : v)
  211.         cout << e << ' ';
  212.     cout << endl;
  213.     return 0;
  214. }
Add Comment
Please, Sign In to add comment