NCBI C++ ToolKit
stress32.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1  /*
2 Copyright(c) 2002-2018 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16 For more information please visit: http://bitmagic.io
17 */
18 
19 
20 //#define BMSSE2OPT
21 //#define BMSSE42OPT
22 //#define BMAVX2OPT
23 //#define BM_USE_EXPLICIT_TEMP
24 //#define BM_USE_GCC_BUILD
25 
26 #define BMXORCOMP
27 #define BM_NONSTANDARD_EXTENTIONS
28 
29 #include <ncbi_pch.hpp>
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #undef NDEBUG
34 #include <time.h>
35 #include <math.h>
36 #include <string.h>
37 
38 #include <iostream>
39 #include <iomanip>
40 #include <utility>
41 #include <memory>
42 #include <random>
43 #include <algorithm>
44 #include <stdarg.h>
45 #include <mutex>
46 
49 #include <util/bitset/bmxor.h>
51 #include <util/bitset/bmutil.h>
52 #include <util/bitset/bmserial.h>
53 #include <util/bitset/bmbvimport.h>
54 #include <util/bitset/bmrandom.h>
55 #include <util/bitset/bmvmin.h>
56 #include <util/bitset/bmbmatrix.h>
65 #include <util/bitset/bm3vl.h>
66 #include <util/bitset/bmtimer.h>
67 #include <util/bitset/bmtask.h>
70 
71 #include <common/test_assert.h>
72 
73 using namespace bm;
74 using namespace std;
75 
76 #include "rlebtv.h"
77 #include <util/bitset/encoding.h>
78 #include <limits.h>
79 
80 #include <util/bitset/bmdbg.h>
81 #include <vector>
82 
83 bool is_silent = false;
84 
85 #if defined(BMSSE2OPT) || defined(BMSSE42OPT) || defined(BMAVX2OPT) || defined(BMAVX512OPT) || defined(__ARM_NEON__)
86 #else
87 # define MEM_DEBUG
88 #endif
89 
90 #ifdef MEM_DEBUG
91 
92 #include "stacktrace_dbg.h"
93 #include <unordered_map>
94 
95 
96 
97 // -------------------------------
98 // memory profiler: very slow if defined
99 //
100 //#define BM_STACK_COLL
101 
102 // NOTE: ARM NEON compilation causes SIGBUS failure (Pi 32-bit)
103 // (disabled as non-essential)
104 
105 #ifdef BM_STACK_COLL
106 std::unordered_map<void*, std::string> g_alloc_trace_map;
107 #endif
108 std::mutex g_trace_lock;
109 
111 {
112 public:
113  static inline size_t na_ = 0;
114  static inline size_t nf_ = 0;
115 
116  static bm::word_t* allocate(size_t n, const void *)
117  {
118  #ifdef BM_STACK_COLL
119  std::string stack_str;
120  stack_str.reserve(2048);
121  get_stacktrace(stack_str);
122  #endif
123  g_trace_lock.lock();
124  ++na_;
125  assert(n);
126  bm::word_t* p =
127  (bm::word_t*) ::malloc((n+1) * sizeof(bm::word_t));
128  if (!p)
129  {
130  std::cerr << "ERROR Failed allocation!" << endl;
131  assert(0);exit(1);
132  }
133  *p = (bm::word_t)n;
134 
135  #ifdef BM_STACK_COLL
136  g_alloc_trace_map.emplace(p, std::move(stack_str));
137  #endif
138  g_trace_lock.unlock();
139  return ++p;
140  }
141 
142  static void deallocate(bm::word_t* p, size_t n)
143  {
144  g_trace_lock.lock();
145  ++nf_;
146  --p;
147  if (*p != n)
148  {
149  printf("Block memory deallocation ERROR! n = %i (expected %i)\n", (int)n, (int)*p);
150  assert(0);exit(1);
151  }
152  ::free(p);
153  #ifdef BM_STACK_COLL
154  g_alloc_trace_map.erase(p);
155  #endif
156  g_trace_lock.unlock();
157  }
158 
159  static size_t balance() BMNOEXCEPT { return na_ - nf_; }
160 };
161 
163 {
164 public:
165  static inline size_t na_ = 0;
166  static inline size_t nf_ = 0;
167 
168  static void* allocate(size_t n, const void *)
169  {
170  #ifdef BM_STACK_COLL
171  std::string stack_str;
172  stack_str.reserve(2048);
173  get_stacktrace(stack_str);
174  #endif
175 
176  g_trace_lock.lock();
177  ++na_;
178  assert(sizeof(size_t) == sizeof(void*));
179  void* p = ::malloc((n+1) * sizeof(void*));
180  if (!p)
181  {
182  std::cerr << "ERROR! Failed allocation!" << endl;
183  exit(1);
184  }
185  size_t* s = (size_t*) p;
186  *s = n;
187 
188  #ifdef BM_STACK_COLL
189  g_alloc_trace_map.emplace(p, std::move(stack_str));
190  #endif
191  g_trace_lock.unlock();
192 
193  return (void*)++s;
194  }
195 
196  static void deallocate(void* p, size_t n)
197  {
198  g_trace_lock.lock();
199  ++nf_;
200  size_t* s = (size_t*) p;
201  --s;
202  if(*s != n)
203  {
204  printf("Ptr memory deallocation ERROR!\n");
205  assert(0);
206  exit(1);
207  }
208  ::free(s);
209 
210  #ifdef BM_STACK_COLL
211  g_alloc_trace_map.erase(s);
212  #endif
213  g_trace_lock.unlock();
214  }
215 
216  static size_t balance() BMNOEXCEPT { return nf_ - na_; }
217 
218 };
219 
220 
221 
223 
227 
228 #else
229 
230 typedef bm::bvector<> bvect;
232 typedef bm::rs_index<> rs_ind;
233 
234 
235 #endif
236 
237 typedef std::vector<unsigned> ref_vect_type;
238 
247 
248 //const unsigned BITVECT_SIZE = 100000000 * 8;
249 
250 // This this setting program will consume around 150M of RAM
251 const unsigned BITVECT_SIZE = 100000000 * 2;
252 
253 const unsigned ITERATIONS = 180000;
254 //const unsigned PROGRESS_PRINT = 2000000;
255 
256 /// Reference (naive) inetrval detector based on population counting
257 /// and boundaries tests
258 ///
259 template<typename BV>
260 bool test_interval(const BV& bv,
261  typename BV::size_type left, typename BV::size_type right)
262 {
263  if (left > right)
264  bm::xor_swap(left, right); // make sure left <= right
265  bool is_left(0), is_right(0);
266  if (left) // check left-1 bit (if exists)
267  is_left = bv.test(left - 1);
268  if ((is_left == false) && (right < bm::id_max - 1))
269  is_right = bv.test(right + 1); // check [...right] range condition
270  if (is_left == false && is_right == false)
271  {
272  typename BV::size_type cnt = bv.count_range(left, right);
273  if (cnt == (1 + right - left))
274  return true;
275  }
276  return false;
277 }
278 
279 
280 template<class BV>
281 void DetailedCompareBVectors(const BV& bv1, const BV& bv2)
282 {
283  bvect::counted_enumerator en1 = bv1.first();
284  bvect::counted_enumerator en2 = bv2.first();
285 
286  for (; en1.valid(); ++en1)
287  {
288  assert(en2.valid());
289 
290  bm::id_t i1 = *en1;
291  bm::id_t i2 = *en2;
292 
293  if (i1 != i2)
294  {
295  unsigned nb1 = unsigned(i1 >> bm::set_block_shift);
296  unsigned nb2 = unsigned(i2 >> bm::set_block_shift);
297  unsigned ii1 = nb1 >> bm::set_array_shift;
298  unsigned jj1 = nb1 & bm::set_array_mask;
299  unsigned ii2 = nb2 >> bm::set_array_shift;
300  unsigned jj2 = nb2 & bm::set_array_mask;
301 
302  std::cerr << "Difference detected at: position="
303  << i1 << " nb=" << nb1
304  << "[" << ii1 << ", " << jj1 << "]"
305  " other position = " << i2 <<
306  " nb=" << nb2
307  << "[" << ii2 << ", " << jj2 << "]"
308  << std::endl;
309  std::cerr << " en1.count()=" << en1.count() << " en2.count()=" << en2.count()
310  << std::endl;
311 
312  exit(1);
313  return;
314  }
315  ++en2;
316  } // for
317 
318  bool eq = bv1.equal(bv2);
319  if (!eq)
320  {
321  cerr << "EQ (1-2) discrepancy! " << endl;
322  exit(1);
323  }
324  int cmp = bv1.compare(bv2);
325  if (cmp != 0)
326  {
327  cerr << "Compare (1-2) discrepancy! " << cmp << endl;
328  exit(1);
329  }
330  cmp = bv2.compare(bv1);
331  if (cmp != 0)
332  {
333  cerr << "Compare (2-1) discrepancy! " << cmp << endl;
334  exit(1);
335  }
336 
337  cout << "Detailed compare OK (no difference)." << endl;
338 }
339 
340 void CheckVectors(bvect_mini &bvect_min,
341  bvect &bvect_full,
342  unsigned size,
343  bool detailed = true);
344 
345 
346 extern "C" {
347  static
348  int bit_decode_func(void* handle_ptr, bm::id_t bit_idx)
349  {
350  std::vector<bm::id_t>* vp = (std::vector<bm::id_t>*)handle_ptr;
351  vp->push_back(bit_idx);
352  return 0;
353  }
354  /*
355  static
356  int bit_decode_func2(void* handle_ptr, bm::id_t bit_idx)
357  {
358  if (bit_idx > (65536 * 256))
359  {
360  throw 1;
361  }
362  std::vector<bm::id_t>* vp = (std::vector<bm::id_t>*)handle_ptr;
363  vp->push_back(bit_idx);
364  return 0;
365  }
366  */
367 } // extern C
368 
369 
370 template<class BV>
371 void VisitorAllRangeTest(const BV& bv, typename BV::size_type step = 1)
372 {
373  typename BV::size_type left, right, next, i_end;
374  bool non_empty = bv.find_range(left, right);
375  if (!non_empty)
376  return;
377  if (left < 65536)
378  left = 0;
379  if (right < 65536)
380  right = 65535;
381 
382  auto drange = right - left;
383  if (!drange)
384  drange = 256;
385  if (!step)
386  step = drange / 100;
387  if (!step)
388  step = 1;
389 
390  cout << "... VisitorAllRangeTest() step=" << step << endl;
391 
392  auto pcnt = 256;
393  for (auto i = left; i <= right; i+=step)
394  {
395  {
396  bvect bv_control;
397  bm::bit_vistor_copy_functor<bvect> func(bv_control);
398  bm::for_each_bit_range(bv, i, right, func);
399 
400  bvect bv2;
401  bv2.copy_range(bv, i, right);
402 
403  bool eq = bv2.equal(bv_control);
404  assert(eq);
405  }
406  next = bv.get_next(i);
407  if (next)
408  {
409  auto delta = next - i;
410  if (delta > 32)
411  {
412  i += delta / 2;
413  }
414  else
415  if (delta == 1)
416  {
417  bool f = bm::find_interval_end(bv, next, i_end);
418  if (f)
419  {
420  delta = i_end - i;
421  if (delta > 4)
422  i += delta / 2;
423  }
424  else
425  {
426  assert(!bv.test(i));
427  }
428  }
429  }
430  if (!pcnt)
431  {
432  if (!is_silent)
433  cout << "\r" << i << " / " << right << flush;
434  pcnt = 128;
435  }
436  --pcnt;
437 
438  } // for i
439  cout << endl;
440 
441  pcnt = 256;
442  for (; left <= right; left+=step, --right)
443  {
444  {
445  bvect bv_control;
446  bm::bit_vistor_copy_functor<bvect> func(bv_control);
447  bm::for_each_bit_range(bv, left, right, func);
448 
449  bvect bv2;
450  bv2.copy_range(bv, left, right);
451 
452  bool eq = bv2.equal(bv_control);
453  assert(eq);
454  }
455  next = bv.get_next(left);
456  if (next)
457  {
458  auto delta = next - left;
459  if (delta > 128)
460  {
461  left += delta / 2;
462  }
463  else
464  if (delta == 1)
465  {
466  bool f = bm::find_interval_end(bv, left, i_end);
467  if (f)
468  {
469  delta = i_end - left;
470  if (delta > 4)
471  left += delta / 2;
472  }
473  }
474  }
475  if (!pcnt)
476  {
477  if (!is_silent)
478  cout << "\r" << left << " / " << right << flush;
479  pcnt = 128;
480  }
481  --pcnt;
482 
483  } // for i
484  cout << endl;
485 }
486 
487 
488 void generate_bvector(bvect& bv, unsigned vector_max = 40000000, bool optimize=true);
489 
490 static
491 unsigned random_minmax(unsigned min, unsigned max)
492 {
493  unsigned r = (unsigned(rand()) << 16u) | unsigned(rand());
494  return r % (max-min) + min;
495 }
496 
497 template<typename BV>
498 void TestFindDiff(const BV& bv1, BV& bv2)
499 {
500  bool f;
501  typename BV::size_type pos, pos_c, pos_l;
502  f = bv1.find_first_mismatch(bv2, pos);
503  bvect bv_x;
504  bv_x.bit_xor(bv1, bv2, bvect::opt_compress);
505  if (!f)
506  {
507  auto a = bv_x.any();
508  assert(!a);
509  return;
510  }
511  else // found
512  {
513  bool f2 = bv1.find_first_mismatch(bv2, pos_l, pos);
514  assert(f2 == f);
515  assert(pos_l == pos);
516  if (pos)
517  {
518  f2 = bv1.find_first_mismatch(bv2, pos_l, pos-1);
519  assert(!f2);
520  }
521  }
522  bool cf = bv_x.find(pos_c);
523  assert(f == cf);
524  assert(pos == pos_c);
525 
526  f = bv2.find_first_mismatch(bv1, pos);
527  assert(f == cf);
528  assert(pos == pos_c);
529 }
530 
531 
532 static
533 void FillSets(bvect_mini* bvect_min,
534  bvect* bvect_full,
535  unsigned min,
536  unsigned max,
537  unsigned fill_factor)
538 {
539  unsigned i;
540  unsigned id;
541 
542  //Random filling
543  if(fill_factor == 0)
544  {
545  unsigned n_id = (max - min) / 100;
546  printf("random filling : %i\n", n_id);
547  for (i = 0; i < n_id; i++)
548  {
549  id = random_minmax(min, max);
550  bvect_min->set_bit(id);
551  bvect_full->set_bit(id);
552  }
553  std::cout << endl;
554  }
555  else
556  {
557  printf("fill_factor random filling : factor = %i\n", fill_factor);
558 
559  for(i = 0; i < fill_factor; i++)
560  {
561  unsigned k = unsigned(rand()) % 10;
562  if (k == 0)
563  k+=2;
564 
565  //Calculate start
566  unsigned start = min + (max - min) / (fill_factor * k);
567 
568  //Randomize start
569  start += random_minmax(1, (max - min) / (fill_factor * 10));
570 
571  if (start > max)
572  {
573  start = min;
574  }
575 
576  //Calculate end
577  unsigned end = start + (max - start) / (fill_factor *2);
578 
579  //Randomize end
580  end -= random_minmax(1, (max - start) / (fill_factor * 10));
581 
582  if (end > max )
583  {
584  end = max;
585  }
586 
587  bvect::bulk_insert_iterator iit = bvect_full->inserter();
588 
589  if (fill_factor > 1)
590  {
591  for(; start < end;)
592  {
593  unsigned r = unsigned(rand()) % 8;
594 
595  if (r > 7)
596  {
597  unsigned inc = unsigned(rand()) % 3;
598  ++inc;
599  unsigned end2 = start + (unsigned)rand() % 1000;
600  if (end2 > end)
601  end2 = end;
602  while (start < end2)
603  {
604  bvect_min->set_bit(start);
605  iit = start;
606  start += inc;
607  }
608  continue;
609  }
610 
611  if (r)
612  {
613  bvect_min->set_bit(start);
614  iit = start;
615  ++start;
616  }
617  else
618  {
619  start+=r;
620  bvect_min->set_bit(start);
621  iit = start;
622  }
623  }
624  }
625  else
626  {
627  unsigned c = unsigned(rand()) % 15;
628  if (c == 0)
629  ++c;
630  for(; start < end; ++start)
631  {
632  bvect_min->set_bit(start);
633  iit = start;
634  if (start % c)
635  {
636  start += c;
637  }
638  }
639  }
640  cout << endl;
641 
642  }
643  }
644 }
645 
646 //
647 // Interval filling.
648 // 111........111111........111111..........11111111.......1111111...
649 //
650 
651 static
652 void FillSetsIntervals(bvect_mini* bvect_min,
653  bvect& bvect_full,
654  unsigned min,
655  unsigned max,
656  unsigned fill_factor,
657  bool set_flag=true)
658 {
659 
660  while(fill_factor==0)
661  {
662  fill_factor=(unsigned)rand()%10;
663  }
664  bvect_full.init();
665 
666  cout << "Intervals filling. Factor="
667  << fill_factor << endl << endl;
668 
669  unsigned i, j;
670  unsigned factor = 70 * fill_factor;
671  for (i = min; i < max; ++i)
672  {
673  unsigned len, end;
674 
675  do
676  {
677  len = unsigned(rand()) % factor;
678  end = i+len;
679 
680  } while (end >= max);
681  if (i < end)
682  {
683  bvect_full.set_range(i, end-1, set_flag);
684  bool all_one_range = bvect_full.is_all_one_range(i, end - 1);
685  assert(all_one_range == set_flag);
686  bool any_one = bvect_full.any_range(i, end - 1);
687  assert(any_one == set_flag);
688  bool is_int = bm::is_interval(bvect_full, i, end-1);
689  bool is_int_c = test_interval(bvect_full, i, end-1);
690  assert(is_int == is_int_c);
691  if (is_int)
692  {
693  bvect::size_type pos;
694  bool b = bm::find_interval_end(bvect_full, i, pos);
695  assert(b && pos == end-1);
696  }
697  }
698 
699  for (j = i; j < end; ++j)
700  {
701  if (set_flag)
702  {
703  if (bvect_min)
704  bvect_min->set_bit(j);
705  //bvect_full.set_bit(j);
706  }
707  else
708  {
709  if (bvect_min)
710  bvect_min->clear_bit(j);
711  //bvect_full.clear_bit(j);
712  }
713 
714 
715  } // j
716 
717  i = end;
718  len = unsigned(rand()) % (factor* 10 * bm::gap_max_bits);
719  if (len % 2)
720  {
721  len *= unsigned(rand()) % (factor * 10);
722  }
723 
724  i+=len;
725 
726  if ( (len % 6) == 0)
727  {
728  for(unsigned k=0; k < 1000 && i < max; k+=3,i+=3)
729  {
730  if (set_flag)
731  {
732  if (bvect_min)
733  bvect_min->set_bit(i);
734  bvect_full.set_bit_no_check(i);
735  }
736  else
737  {
738  if (bvect_min)
739  bvect_min->clear_bit(j);
740  bvect_full.clear_bit(j);
741  }
742  }
743  }
744  } // for i
745 
746 }
747 
748 static
750  bvect* bvect_full,
751  unsigned min,
752  unsigned max,
753  unsigned fill_factor)
754 {
755  FillSetsIntervals(bvect_min, *bvect_full, min, max, fill_factor, true);
756  FillSetsIntervals(bvect_min, *bvect_full, min, max, fill_factor, false);
757 }
758 
759 static
760 void FillSetsRandomOne(bvect_mini* bvect_min,
761  bvect* bvect_full,
762  unsigned min,
763  unsigned max)
764 {
765  unsigned range = max - min;
766  unsigned bit_idx = unsigned(rand()) % range;
767  bvect_min->set_bit(bit_idx);
768  bvect_full->set_bit(bit_idx);
769  std::cout << "Bit_idx=" << bit_idx << endl;
770 }
771 
772 static
773 void FillSetsRandom(bvect_mini* bvect_min,
774  bvect* bvect_full,
775  unsigned min,
776  unsigned max,
777  unsigned fill_factor)
778 {
779  bvect_full->init();
780  unsigned diap = max - min;
781  unsigned count;
782 
783  switch (fill_factor)
784  {
785  case 0:
786  count = diap / 1000;
787  break;
788  case 1:
789  count = diap / 255;
790  break;
791  default:
792  count = diap / 10;
793  break;
794  }
795 
796  for (unsigned i = 0; i < count; ++i)
797  {
798  unsigned bn = unsigned(rand()) % count;
799  bn += min;
800 
801  if (bn > max)
802  {
803  bn = max;
804  }
805  bvect_min->set_bit(bn);
806  bvect_full->set_bit_no_check(bn);
807  }
808  cout << "Ok" << endl;
809 
810 }
811 
812 static
813 void FillSetsRegular(bvect_mini* bvect_min,
814  bvect* bvect_full,
815  unsigned /*min*/,
816  unsigned max,
817  unsigned /*fill_factor*/)
818 {
819  bvect::bulk_insert_iterator iit = bvect_full->inserter();
820 
821  unsigned step = (unsigned)rand() % 4;
822  if (step < 2) ++step;
823  for (unsigned i = 0; i < max; i+=step)
824  {
825  bvect_min->set_bit(i);
826  iit = i;
827  //bvect_full->set_bit_no_check(i);
828  }
829  cout << "Ok" << endl;
830 }
831 
832 
833 
834 
835 //
836 // Quasi random filling with choosing randomizing method.
837 //
838 //
839 static
841  bvect* bvect_full,
842  unsigned min,
843  unsigned max,
844  int optimize = 0,
845  int method = -1)
846 {
847  if (method == -1)
848  {
849  method = rand() % 7;
850  }
851 //method = 2;
852  unsigned factor;
853  switch (method)
854  {
855 
856  case 0:
857  cout << "Random filling: method - FillSets - factor(0)" << endl;
858  FillSets(bvect_min, bvect_full, min, max, 0);
859  break;
860 
861  case 1:
862  cout << "Random filling: method - FillSets - factor(random)" << endl;
863  factor = (unsigned)rand()%3;
864  FillSets(bvect_min, bvect_full, min, max, factor?factor:1);
865  break;
866 
867  case 2:
868  cout << "Random filling: method - Set-Clear Intervals - factor(random)" << endl;
869  factor = (unsigned)rand()%10;
870  FillSetClearIntervals(bvect_min, bvect_full, min, max, factor);
871  break;
872  case 3:
873  cout << "Random filling: method - FillRandom - factor(random)" << endl;
874  factor = (unsigned)rand()%3;
875  FillSetsRandom(bvect_min, bvect_full, min, max, factor?factor:1);
876  break;
877  case 4:
878  cout << "Random set one bit" << endl;
879  FillSetsRandomOne(bvect_min, bvect_full, min, max);
880  break;
881  case 5:
882  cout << "Regular pattern filling" << endl;
883  FillSetsRegular(bvect_min, bvect_full, min, max, 2);
884  break;
885  default:
886  cout << "Random filling: method - Set Intervals - factor(random)" << endl;
887  factor = (unsigned)rand()%10;
888  FillSetsIntervals(bvect_min, *bvect_full, min, max, factor);
889  break;
890 
891  } // switch
892 
893  if (optimize && (method <= 1))
894  {
896  bvect_full->optimize(tb);
897  }
898 }
899 
900 static
901 void print_bv(const bvect& bv)
902 {
903  std::cout << bv.count() << ": ";
904  bvect::enumerator en = bv.first();
905  for (; en.valid(); ++en)
906  std::cout << *en << ", ";
907  std::cout << std::endl;
908 }
909 
910 // reference SHIFT right
911 static
912 void ShiftRight(bvect* bv, unsigned shift)
913 {
914  bvect bv_tmp;
915  {
917  bvect::enumerator en = bv->first();
918  for (; en.valid(); ++en)
919  {
920  unsigned v = *en;
921  unsigned new_v = v + shift;
922  if (new_v < v || new_v == bm::id_max) // check overflow
923  {}
924  else
925  {
926  bi = new_v;
927  }
928  }
929  }
930  bv->swap(bv_tmp);
931 }
932 
933 // Reference bit insert
934 static
935 void BVectorInsert(bvect* bv, unsigned pos, bool value)
936 {
937  bvect bv_tmp;
938  if (pos)
939  bv_tmp.copy_range(*bv, 0, pos-1);
940 
941  {
943  bvect::enumerator en = bv->first();
944  for (; en.valid(); ++en)
945  {
946  unsigned v = *en;
947  if (v < pos)
948  continue;
949  unsigned new_v = v + 1;
950  if (new_v < v || new_v == bm::id_max) // check overflow
951  {}
952  else
953  {
954  bi = new_v;
955  }
956  }
957  }
958  bv->swap(bv_tmp);
959  bv->set(pos, value);
960 }
961 
962 // Reference bit erase
963 static
964 void BVectorErase(bvect* bv, unsigned pos)
965 {
966  bvect bv_tmp;
967  if (pos)
968  bv_tmp.copy_range(*bv, 0, pos-1);
969 
970  {
972  bvect::enumerator en = bv->first();
973  en.go_to(pos+1);
974  for (; en.valid(); ++en)
975  {
976  unsigned v = *en;
977  assert(v > pos);
978  bi = v -1;
979  }
980  }
981  bv->swap(bv_tmp);
982 }
983 
984 
985 
986 // do logical operation through serialization
987 static
988 unsigned SerializationOperation(bvect* bv_target,
989  /*const*/ bvect& bv1,
990  /*const*/ bvect& bv2,
991  set_operation op,
992  bool check_reverse=false)
993 {
994  bvect bv_tmp;
995  if (!bv_target)
996  {
997  bv_target = &bv_tmp;
998  }
999 
1000  if (op == set_COUNT_SUB_AB ||
1001  op == set_COUNT_SUB_BA)
1002  {
1003  check_reverse = false;
1004  }
1005 
1006  // serialize input vectors
1007  bvect::statistics st1_op, st2_op;
1008 
1010  if (!bv1.is_ro())
1011  bv1.optimize(tb, bvect::opt_compress, &st1_op);
1012  else
1013  bv1.calc_stat(&st1_op);
1014  if (!bv2.is_ro())
1015  bv2.optimize(tb, bvect::opt_compress, &st2_op);
1016  else
1017  bv2.calc_stat(&st2_op);
1018 
1019 
1020  struct bvect::statistics st1, st2;
1021  bv1.calc_stat(&st1);
1022  bv2.calc_stat(&st2);
1023 
1024 
1025  if (st1.max_serialize_mem > st1_op.max_serialize_mem)
1026  {
1027  cout << "Error: Optimize failed to compute max_serialize_mem" << endl;
1028  cout << "calc_stat=" << st1.max_serialize_mem << endl;
1029  cout << "optimize=" << st1_op.max_serialize_mem << endl;
1030  assert(0);
1031  exit(1);
1032  }
1033  if (st2.max_serialize_mem > st2_op.max_serialize_mem)
1034  {
1035  cout << "Error:Optimize failed to compute max_serialize_mem" << endl;
1036  cout << "calc_stat=" << st2.max_serialize_mem << endl;
1037  cout << "optimize=" << st2_op.max_serialize_mem << endl;
1038  assert(0); exit(1);
1039  }
1040 
1041  unsigned char* smem1 = new unsigned char[st1.max_serialize_mem];
1042  unsigned char* smem2 = new unsigned char[st2.max_serialize_mem];
1043 
1044  size_t slen1 = bm::serialize(bv1, smem1, tb);
1045  size_t slen2 = bm::serialize(bv2, smem2, tb);
1046 
1047  if (slen1 > st1.max_serialize_mem || slen2 > st2.max_serialize_mem)
1048  {
1049  cout << "Serialization override detected!" << endl;
1050  exit(1);
1051  }
1052 
1054 
1055  auto count = od.deserialize(*bv_target, smem1, nullptr, set_ASSIGN);
1056  cout << slen1 << " " << slen2 << endl;
1057  int res = bv1.compare(*bv_target);
1058  if (res != 0)
1059  {
1060  cout << "---------------------------------- " << endl;
1061  cout << "bv1.count()=" << bv1.count() << endl;
1062  print_stat(cout, bv1);
1063  cout << "---------------------------------- " << endl;
1064  cout << "bv_target.count()=" << bv_target->count() << endl;
1065  print_stat(cout, *bv_target);
1066 
1067  bv_target->bit_xor(bv1);
1068  cout << "First diff=" << bv_target->get_first() << endl;
1069  cout << "set_ASSIGN 1 failed!" << endl;
1070  exit (1);
1071  }
1072  cout << "Deserialization ASSIGN into bv1 OK" << endl;
1073 
1074  {
1075  bvect* bv_tmp2 = new bvect();
1076  bm::deserialize(*bv_tmp2, smem1);
1077  if (*bv_tmp2 != bv1)
1078  {
1079  cout << "Deserialize NOT equal to Operation deserialize!" << endl;
1080  assert(0); exit(1);
1081  }
1082  delete bv_tmp2;
1083  }
1084 
1085 
1086  cout << "Operation deserialization... " << op << endl;
1087 
1088  count = od.deserialize(*bv_target, smem2, 0, op);
1089  cout << "OK" << endl;
1090 
1091  // check if operation was ok
1092  {
1093  bvect bv_agg, bv_agg2;
1094 
1096  agg.set_optimization();
1097 
1098  const bvect* agg_list[10];
1099  const bvect* agg_list2[10];
1100  agg_list[0] = &bv1;
1101  agg_list[1] = &bv2;
1102  agg_list2[0] = &bv2;
1103 
1104 
1105  bool agg_check = false;
1106 
1108  switch(op)
1109  {
1110  case bm::set_OR:
1111  {
1113  bvc |= bv2;
1114  bvect bv_merge1(bv1, bm::finalization::READWRITE);
1115  bvect bv_merge2(bv2, bm::finalization::READWRITE);
1116  bv_merge1.merge(bv_merge2);
1117 
1118  if (bv_merge1 != bvc)
1119  {
1120  cerr << "Merge(OR) check error!" << endl;
1121  exit(1);
1122  }
1123  // 2-way
1124  {
1125  bvect bvt1;
1126  bvt1.bit_or(bv1, bv2, bvect::opt_none);
1127  if (bvt1 != bvc)
1128  {
1129  cerr << "1. OR 2-way check error!" << endl;
1130  exit(1);
1131  }
1132  bvect bvt2;
1133  bvt2.bit_or(bv2, bv1, bvect::opt_compress);
1134  if (bvt2 != bvc)
1135  {
1136  cerr << "2. OR 2-way check error!" << endl;
1137  exit(1);
1138  }
1139  }
1140  }
1141  bvt |= bv2;
1142  agg.combine_or(bv_agg, agg_list, 2);
1143  agg_check = true;
1144  break;
1145  case bm::set_XOR:
1146  bvt ^= bv2;
1147  // 2-way
1148  {
1150  bvc ^= bv2;
1151 
1152  bvect bvt1;
1153  bvt1.bit_xor(bv1, bv2, bvect::opt_none);
1154  if (bvt1 != bvc)
1155  {
1156  cerr << "1. XOR 2-way check error!" << endl;
1157  cerr << "Run detailed check (1)..." << endl;
1158  DetailedCompareBVectors(bvt1, bvc);
1159 
1160  bvect bvt2;
1161  bvt2.bit_xor(bv2, bv1, bvect::opt_compress);
1162  if (bvt2 != bvc)
1163  {
1164  cerr << "Reverse XOR 2-way check failed too!" << endl;
1165  }
1166 
1167  cerr << "Run detailed check (2)..." << endl;
1168  DetailedCompareBVectors(bvt2, bvc);
1169 
1170  exit(1);
1171  }
1172  bvect bvt2;
1173  bvt2.bit_xor(bv2, bv1, bvect::opt_compress);
1174  if (bvt2 != bvc)
1175  {
1176  cerr << "2. XOR 2-way check error!" << endl;
1177  exit(1);
1178  }
1179  }
1180  break;
1181  case bm::set_AND:
1182  bvt &= bv2;
1183  agg.combine_and(bv_agg, agg_list, 2);
1184  agg.combine_and_sub(bv_agg2, agg_list, 2, 0, 0, false);
1185  {
1186  if (bv_agg != bv_agg2)
1187  {
1188  cerr << "Error: Aggregator AND - AND-SUB(0) comparison failed!" << endl;
1189  exit(1);
1190  }
1191  }
1192  agg_check = true;
1193 
1194  // 2-way
1195  {
1197  bvc &= bv2;
1198 
1199  bvect bv_ro1(bv1, bm::finalization::READONLY);
1200  bvect bv_ro2(bv2, bm::finalization::READONLY);
1201 
1202  bvect bvt1;
1203  bvt1.bit_and(bv1, bv2, bvect::opt_none);
1204  if (bvt1 != bvc)
1205  {
1206  cerr << "1. AND 2-way check error!" << endl;
1207  //print_bv(bvt1);
1208  //cout << "control:" << endl;
1209  //print_bv(bvc);
1210  assert(0); exit(1);
1211  }
1212  bvect bvt11;
1213  bvt11.bit_and(bv_ro1, bv_ro2, bvect::opt_none);
1214  if (bvt11 != bvc)
1215  {
1216  cerr << "1.1 AND 2-way check error!" << endl;
1217  assert(0); exit(1);
1218  }
1219 
1220  bvect bvt2;
1221  bvt2.bit_and(bv2, bv1, bvect::opt_compress);
1222  if (bvt2 != bvc)
1223  {
1224  cerr << "2. AND 2-way check error!" << endl;
1225  assert(0);exit(1);
1226  }
1227  bvect bvt12;
1228  bvt12.bit_and(bv_ro1, bv_ro2, bvect::opt_none);
1229  if (bvt12 != bvc)
1230  {
1231  cerr << "2.1 AND 2-way check error!" << endl;
1232  assert(0); exit(1);
1233  }
1234  }
1235  break;
1236  case bm::set_SUB:
1237  bvt -= bv2;
1238  agg.combine_and_sub(bv_agg, agg_list, 1, agg_list2, 1, false);
1239  {
1240  bvect bv_h;
1241  agg.combine_and_sub_horizontal(bv_h, agg_list, 1, agg_list2, 1);
1242  if (bv_agg != bv_h)
1243  {
1244  cerr << "Error: Aggregator Horz-AND-SUB comparison failed!" << endl;
1245  exit(1);
1246  }
1247  }
1248  agg_check = true;
1249  // 2-way
1250  {
1253  bvc1 -= bv2;
1254  bvc2 -= bv1;
1255 
1256  bvect bvt1;
1257  bvt1.bit_sub(bv1, bv2, bvect::opt_compress);
1258  if (bvt1 != bvc1)
1259  {
1260  DetailedCompareBVectors(bvt1, bvc1);
1261  cerr << "1. SUB 2-way check error!" << endl;
1262  exit(1);
1263  }
1264  bvect bvt2;
1265  bvt2.bit_sub(bv2, bv1, bvect::opt_compress);
1266  if (bvt2 != bvc2)
1267  {
1268  cerr << "2. SUB 2-way check error!" << endl;
1269  exit(1);
1270  }
1271  }
1272 
1273  break;
1274  default:
1275  goto no_compare;
1276  }
1277  if (bvt.compare(*bv_target) != 0)
1278  {
1279  cout << "Direct Serial operation comparison failed!" << endl;
1280  exit(1);
1281  }
1282  if (agg_check && bvt.compare(bv_agg) != 0)
1283  {
1284  cerr << "Error: Aggregator operation comparison failed!" << endl;
1285  exit(1);
1286  }
1287 
1288  no_compare:
1289  ;
1290 
1291  }
1292 /*
1293  if (op == bm::set_AND || op == bm::set_OR || op == bm::set_XOR || op == bm::set_SUB)
1294  {
1295  cout << "3 way operation check... " << op << endl;
1296  operation_deserializer<bvect>::deserialize(*bv_target,
1297  bv1,
1298  smem2,
1299  0,
1300  op);
1301  cout << "OK" << endl;
1302 
1303  bvect bvt(bv1);
1304  switch(op)
1305  {
1306  case bm::set_OR:
1307  bvt |= bv2;
1308  break;
1309  case bm::set_XOR:
1310  bvt ^= bv2;
1311  break;
1312  case bm::set_AND:
1313  bvt &= bv2;
1314  break;
1315  case bm::set_SUB:
1316  bvt -= bv2;
1317  break;
1318  default:
1319  goto no_compare2;
1320  }
1321  if (bvt.compare(*bv_target) != 0)
1322  {
1323  cout << "3-way Serial operation comparison failed!" << endl;
1324  exit(1);
1325  }
1326  no_compare2:
1327  ;
1328  }
1329 */
1330 
1331  if (check_reverse)
1332  {
1333  cout << "Reverse check... " << endl;
1334  bvect bv_tmp2(BM_GAP);
1335  od.deserialize(bv_tmp2, smem2, 0, set_ASSIGN);
1336  res = bv_tmp2.compare(bv2);
1337  if (res != 0)
1338  {
1339  cout << "set_ASSIGN failed 2! " << endl;
1340  exit(1);
1341  }
1342  if (bv_tmp2 != bv2)
1343  {
1344  cout << "set_ASSIGN failed 2-1! " << endl;
1345  exit(1);
1346  }
1347  cout << "Deserialization assign to bv_tmp2 OK" << endl;
1348  unsigned count_rev =
1349  od.deserialize(bv_tmp2, smem1, 0, op);
1350  if (count != count_rev)
1351  {
1352 // print_stat(bv1);
1353 /*
1354  unsigned c = count_or(bv1, bv2);
1355  cout << "Correct count=" << c << endl;
1356 
1357  c = count_or(bv2, bv1);
1358  cout << "Correct count=" << c << endl;
1359 
1360  bv1 |= bv2;
1361  cout << "Count3 = " << bv1.count() << endl;;
1362 */
1363  //SaveBVector("err1.bv", bv1);
1364  //SaveBVector("err2.bv", bv2);
1365 
1366 
1367 
1368  cout << "Operation=" << op << endl;
1369 
1370  cout << "Serialization operation reverse check failed"
1371  << " count = " << count
1372  << " count rev= " << count_rev
1373  << endl;
1374  cout << "See bvector dumps: err1.bv, err2.bv" << endl;
1375 
1376  exit(1);
1377  }
1378 
1379  }
1380 
1381  delete [] smem1;
1382  delete [] smem2;
1383 
1384  return count;
1385 }
1386 
1387 static
1389  bvect& bv1,
1390  bvect& bv2,
1391  unsigned predicted_count,
1392  set_operation op_count,
1393  set_operation op_combine)
1394 {
1395  bv_target->clear(true);
1396 
1397  bvect bv_ro1(bv1, bm::finalization::READONLY);
1398  bvect bv_ro2(bv2, bm::finalization::READONLY);
1399 
1400  cout << "Serialization operation count..." << endl;
1401 
1402  unsigned scount1 = SerializationOperation(0,
1403  bv1,
1404  bv2,
1405  op_count,
1406  true //reverse check
1407  );
1408  cout << "Serialization operation count OK." << endl;
1409 
1410 
1411 
1412  cout << "Serialization operation. " << endl;
1413  unsigned scount2 = SerializationOperation(bv_target,
1414  bv1,
1415  bv2,
1416  op_combine);
1417  scount2 = bv_target->count();
1418  if (predicted_count != scount2 || scount1 != scount2)
1419  {
1420  cout << "Serialization count != predicted" << endl
1421  << " predicted=" << predicted_count
1422  << " scount1=" << scount1
1423  << " scount2=" << scount2
1424  << endl;
1425 
1426  cout << endl << "target:" << endl;
1427  print_stat(cout, *bv_target);
1428  cout << endl << endl << "Reference" << endl;
1429  if (op_combine == set_OR)
1430  {
1431  bv1 |= bv2;
1432  if (bv1 != *bv_target)
1433  {
1434  cout << "Comparison OR error!" << endl;
1435  }
1436  cout << "OR operation count=" << bv1.count() << endl;
1437  print_stat(cout, bv1);
1438  }
1439  else
1440  if (op_combine == set_AND)
1441  {
1442  bv1 &= bv2;
1443  print_stat(cout,bv1);
1444  }
1445 
1446  exit(1);
1447  }
1448 
1449  bvect bv_target2;
1450  unsigned scount3 = SerializationOperation(&bv_target2,
1451  bv_ro1,
1452  bv_ro2,
1453  op_combine);
1454  scount3 = bv_target2.count();
1455  assert(scount3 == scount2);
1456 
1457  bool eq = bv_target->equal(bv_target2);
1458  assert(eq);
1459 
1460  cout << "OK" << endl;
1461 }
1462 
1463 static
1464 void print_mv(const bvect_mini &bvect_min, unsigned size)
1465 {
1466  unsigned i;
1467  for (i = 0; i < size; ++i)
1468  {
1469  bool bflag = bvect_min.is_bit_true(i) != 0;
1470 
1471  if (bflag)
1472  printf("1");
1473  else
1474  printf("0");
1475  if ((i % 31) == 0 && (i != 0))
1476  printf(".");
1477  }
1478 
1479  printf("\n");
1480 }
1481 
1482 static
1483 void print_gap(const gap_vector& gap_vect, unsigned /*size*/)
1484 {
1485  const gap_word_t *buf = gap_vect.get_buf();
1486  unsigned len = gap_length(buf);
1487  printf("[%i:]", *buf++ & 1);
1488 
1489  for (unsigned i = 1; i < len; ++i)
1490  {
1491  printf("%i,", *buf++);
1492  }
1493 
1494  printf("\n");
1495 }
1496 
1497 
1498 static
1499 void CheckGAPMin(const gap_vector& gapv, const bvect_mini& bvect_min, unsigned len)
1500 {
1501  int last_bit = -1;
1502  for (unsigned i = 0; i < len; ++i)
1503  {
1504  int bit1 = (gapv.is_bit_true(i) == 1);
1505  int bit2 = (bvect_min.is_bit_true(i) != 0);
1506  if (bit1 != bit2)
1507  {
1508  cout << "Bit comparison failed. " << "Bit N=" << i << endl;
1509  assert(0);
1510  exit(1);
1511  }
1512  if (bvect_min.is_bit_true(i))
1513  {
1514  last_bit = (int)i;
1515  }
1516  }
1517  unsigned glast;
1518  bool found = gapv.get_last(&glast);
1519  if (!found && last_bit != -1)
1520  {
1521  cout << "Gap last search failed. " << "Bit=" << last_bit << endl;
1522  assert(0);
1523  exit(1);
1524  }
1525 
1526  if (found && last_bit == -1)
1527  {
1528  cout << "Gap last search ok but should failed. " << "Bit=" <<
1529  glast << endl;
1530  assert(0);
1531  exit(1);
1532  }
1533 
1534  if (last_bit != (int)glast)
1535  {
1536  if (!found && last_bit == -1)
1537  {}
1538  else
1539  {
1540  cout << "Gap last search discrepancy:" << " found Bit=" <<
1541  glast << " last=" << last_bit << endl;
1542  assert(0);
1543  exit(1);
1544  }
1545  }
1546 }
1547 
1548 static
1549 void CheckIntervals(const bvect& bv, unsigned /*max_bit*/)
1550 {
1551  unsigned cnt0 = count_intervals(bv);
1552  unsigned cnt1 = 1;
1553  //bool bit_prev = bv.test(0);
1554 
1555  unsigned cnt2 = 0;
1556  bvect::enumerator en = bv.first();
1557  if (!en.valid())
1558  {
1559  cnt2 = 1;
1560  }
1561  else
1562  {
1563  if (*en > 0)
1564  ++cnt2;
1565  unsigned prev = *en;
1566  for (++en; en.valid(); ++en)
1567  {
1568  if (++prev == *en)
1569  {
1570  }
1571  else
1572  {
1573  cnt2 += 2;
1574  prev = *en;
1575  }
1576  }
1577  cnt2 += 2;
1578  }
1579 /*
1580  for (unsigned i = 1; i < max_bit; ++i)
1581  {
1582  bool bit = bv.test(i);
1583  cnt1 += bit_prev ^ bit;
1584  bit_prev = bit;
1585  }
1586 */
1587  if (cnt0 != cnt2)
1588  {
1589  cout << "CheckIntervals error. " << "bm count=" << cnt0
1590  << " Control = " << cnt1 << endl;
1591  exit(1);
1592  }
1593 }
1594 
1595 template<class T> void CheckCountGapRange(const T& vect,
1596  unsigned left,
1597  unsigned right,
1598  unsigned* block_count_arr=0)
1599 {
1600  unsigned cnt1 = vect.count_range(left, right, block_count_arr);
1601  unsigned cnt2 = 0;
1602  for (unsigned i = left; i <= right; ++i)
1603  {
1604  if (vect.test(i))
1605  {
1606  ++cnt2;
1607  }
1608  }
1609  if (cnt1 != cnt2)
1610  {
1611  cout << "Bitcount range failed!" << "left=" << left
1612  << " right=" << right << endl
1613  << "count_range()=" << cnt1
1614  << " check=" << cnt2;
1615  exit(1);
1616  }
1617 }
1618 
1619 template<typename T>
1620 bool FindRank(const T& bv, bm::id_t rank, bm::id_t from, bm::id_t& pos)
1621 {
1622  assert(rank);
1623  bool res = false;
1624 
1625  typename T::enumerator en = bv.get_enumerator(from);
1626 
1627  typename T::enumerator en2 = bv.get_enumerator(from);
1628  if (rank > 1)
1629  en2.skip_to_rank(rank);
1630  if (!en.valid())
1631  return false;
1632  for (; en.valid(); ++en)
1633  {
1634  rank -= en.valid();
1635  if (rank == 0)
1636  {
1637  pos = *en;
1638  res = true;
1639  break;
1640  }
1641  } // for en
1642 
1643  bm::id_t pos2 = *en2;
1644  if (pos != pos2)
1645  {
1646  cerr << "FindRank enumerator::skip() failed: "
1647  << "pos=" << pos << " skip()pos=" << pos2
1648  << " from=" << from
1649  << endl;
1650  assert(0);
1651  exit(1);
1652  }
1653 
1654  return res;
1655 }
1656 
1657 inline
1658 void CheckRangeCopy(const bvect& bv, unsigned from, unsigned to)
1659 {
1660  bm::id_t f1, l1, f2, l2;
1661 
1662  bvect bv_cp(bv, from, to);
1663  bvect bv_cp2(bv, to, from); // swapped interval copy is legal
1664 
1665  bvect bv_control;
1666  bv_control.set_range(from, to);
1667 
1668  bv_control &= bv;
1669 
1670  int res = bv_control.compare(bv_cp);
1671  if (res != 0)
1672  {
1673  bool found1 = bv_cp.find_range(f1, l1);
1674  bool found2 = bv_control.find_range(f2, l2);
1675 
1676  bvect bv_cp3(bv, from, to);
1677 
1678  cerr << "Error: bvector<>::range_copy() failed. from=" << from << " to=" << to << endl;
1679  if (found1)
1680  cerr << " range copy from=" << f1 << " to=" << l1 << endl;
1681  if (found2)
1682  cerr << " control from=" << f2 << " to=" << l2 << endl;
1683  exit(1);
1684  }
1685 
1686  int res2 = bv_control.compare(bv_cp2);
1687  if (res2 != 0)
1688  {
1689  bool found1 = bv_cp2.find_range(f1, l1);
1690  bool found2 = bv_control.find_range(f2, l2);
1691 
1692  cerr << "Error: reversed bvector<>::range_copy() failed. from=" << from << " to=" << to << endl;
1693  if (found1)
1694  cerr << " range copy from=" << f1 << " to=" << l1 << endl;
1695  if (found2)
1696  cerr << " control from=" << f2 << " to=" << l2 << endl;
1697  exit(1);
1698  }
1699 
1700  bool found1 = bv_cp.find_range(f1, l1);
1701  bool found2 = bv_control.find_range(f2, l2);
1702  if (found1 != found2)
1703  {
1704  cerr << "Error: Dynamic range integrity check." << endl;
1705  exit(1);
1706  }
1707  if (found1)
1708  {
1709  if (f1 != f2 || l1 != l2)
1710  {
1711  cerr << "Error: bvector<>::range_copy() failed (dynamic range check). from=" << from << " to=" << to << endl;
1712  cerr << " range copy from=" << f1 << " to=" << l1 << endl;
1713  cerr << " control from=" << f2 << " to=" << l2 << endl;
1714  exit(1);
1715  }
1716  }
1717 
1718 }
1719 
1720 
1721 template<class T>
1722 void CheckCountRange(const T& vect, const bvect::rs_index_type& bc_arr,
1723  unsigned left,
1724  unsigned right)
1725 {
1726  unsigned cnt1 = vect.count_range(left, right);
1727  unsigned cnt2 = 0;
1728 
1729  typename T::enumerator en = vect.get_enumerator(left);
1730  for (; en.valid(); ++en)
1731  {
1732  if (*en > right)
1733  break;
1734  cnt2 += en.valid();
1735  }
1736  if (cnt1 != cnt2)
1737  {
1738  cout << "2. Bitcount range failed!" << "left=" << left
1739  << " right=" << right << endl
1740  << "count_range()=" << cnt1
1741  << " check=" << cnt2;
1742  exit(1);
1743  }
1744 
1745  CheckRangeCopy(vect, left, right);
1746 
1747 // bvect::rs_index_type bc_arr;
1748 // vect.build_rs_index(&bc_arr);
1749 
1750  // run a cycle to check count_to()
1751  //
1752  //for (unsigned i = 0; i <= right; ++i)
1753  {
1754  if (left > right)
1755  swap(left, right);
1756 
1757  unsigned cnt_to_r = vect.count_to(right, bc_arr);
1758  cnt1 = vect.count_range(left, right, bc_arr);
1759  unsigned cnt_to_l = left ? vect.count_to(left - 1, bc_arr) : 0;
1760  cnt2 = cnt_to_r - cnt_to_l;
1761  if (cnt1 != cnt2)
1762  {
1763  cout << "Bitcount range TO failed!" << " left=" << left
1764  << " right=" << right << endl
1765  << " count_range()=" << cnt1
1766  << " check=" << cnt2;
1767  assert(0); exit(1);
1768  }
1769 
1770  bm::id_t range = 1 + right - left;
1771  if (cnt1 > range)
1772  {
1773  cerr << "Impossible count_range detected!" << endl;
1774  cerr << " range = " << range << endl;
1775  cerr << " cnt1 = " << cnt1 << endl;
1776  cerr << " left = " << left << endl;
1777  cerr << " right = " << right << endl;
1778  cnt1 = vect.count_range(left, right, bc_arr);
1779  cerr << " cnt1 = " << cnt1 << endl;
1780  assert(0); exit(1);
1781  }
1782 
1783 
1784  if (cnt1) // check if we can reverse the search (rank)
1785  {
1786  unsigned pos, pos1;
1787  pos = pos1 = 0;
1788  bool rf = vect.find_rank(cnt1, left, pos);
1789  bool rf1 = vect.find_rank(cnt1, left, pos1, bc_arr);
1790  if (!rf || !rf1)
1791  {
1792  cerr << "1. find_rank() failed!" << " left=" << left
1793  << " right=" << right
1794  << " count_range()=" << cnt1
1795  << " pos=" << pos
1796  << " pos1=" << pos1
1797  << " range=" << range
1798  << endl;
1799 
1800  unsigned pos2;
1801  bool rf2 = FindRank(vect, cnt1, left, pos2);
1802  if (!rf2)
1803  {
1804  cerr << "Debug FindRank failed!" << endl;
1805  }
1806  else
1807  {
1808  cerr << " rank=" << pos2 << endl;
1809  }
1810 
1811  cerr << "detailed bug search..." << endl;
1812  for (unsigned k = 1; k <= cnt1; ++k)
1813  {
1814  rf = vect.find_rank(k, left, pos);
1815  rf2 = FindRank(vect, k, left, pos2);
1816  if (rf != rf2 || pos != pos2)
1817  {
1818  rf = vect.find_rank(k, left, pos);
1819 
1820  cerr << "Failed for rank=" << k << endl;
1821  cerr << rf << " " << rf2 << endl;
1822  cerr << "pos = " << pos << " pos2 = " << pos2 << endl;
1823  assert(0); exit(1);
1824  }
1825  }
1826  assert(0); exit(1);
1827  }
1828  assert(pos == pos1);
1829 
1830  if (left > 0)
1831  {
1832  unsigned pos3;
1833  T bv1(vect, left, bm::id_max-1);
1834  std::unique_ptr<bvect::rs_index_type> bc_arr2(new bvect::rs_index_type);
1835  bv1.build_rs_index(bc_arr2.get());
1836 
1837  bool rf3 = bv1.select(cnt1, pos3, *bc_arr2);
1838  assert(rf3 == rf);
1839  assert(pos == pos3);
1840  }
1841 
1842  if (right != pos)
1843  {
1844  unsigned pos2;
1845  bool rf2 = FindRank(vect, cnt1, left, pos2);
1846  assert(rf2);
1847  // check if we found zero-tail
1848  //auto cnt3 = vect.count_range(pos+1, right, block_count_arr);
1849  if (pos2 != pos)
1850  {
1851  rf = vect.find_rank(cnt1, left, pos);
1852  cout << "2. find_rank() check failed! \n" << "left=" << left
1853  << " right=" << right
1854  << " count_range()=" << cnt1
1855  << " pos=" << pos
1856  << " rank = " << pos2
1857  << endl;
1858  assert(0); exit(1);
1859  }
1860  }
1861  }
1862  }
1863 }
1864 
1865 static
1866 unsigned BitCountChange(unsigned word)
1867 {
1868  unsigned count = 1;
1869  unsigned bit_prev = word & 1;
1870  word >>= 1;
1871  for (unsigned i = 1; i < 32; ++i)
1872  {
1873  unsigned bit = word & 1;
1874  count += bit ^ bit_prev;
1875  bit_prev = bit;
1876  word >>= 1;
1877  }
1878  return count;
1879 }
1880 
1881 static
1882 void DetailedCheckVectors(const bvect &bv1,
1883  const bvect &bv2)
1884 {
1885  bvect::enumerator en1 = bv1.first();
1886  bvect::enumerator en2 = bv2.first();
1887 
1888  while (en1.valid())
1889  {
1890  if (!en2.valid())
1891  {
1892  cout << "Second vector - invalid enumerator at:" << *en1;
1893  return;
1894  }
1895  if (*en1 != *en2)
1896  {
1897  cout << "Discrepancy at bit position: " << *en1;
1898  cout << " second vector is at:" << *en2;
1899  return;
1900  }
1901  ++en1;
1902  ++en2;
1903  }
1904  cout << "Detailed check OK" << endl; // Hmmm... Why?
1905 }
1906 
1907 static
1908 void DetailedCheckVectors(const bvect_mini &bvect_min,
1909  const bvect &bvect_full,
1910  unsigned size)
1911 {
1912  cout << "Detailed check" << endl;
1913 
1914  //bvect_full.stat();
1915 
1916  // detailed bit by bit comparison. Paranoia check.
1917 
1918  unsigned i;
1919  for (i = 0; i < size; ++i)
1920  {
1921  bool bv_m_flag = bvect_min.is_bit_true(i) != 0;
1922  bool bv_f_flag = bvect_full.get_bit(i) != 0;
1923 
1924  if (bv_m_flag != bv_f_flag)
1925  {
1926  printf("Bit %u is non conformant. vect_min=%i vect_full=%i\n",
1927  i, (int)bv_m_flag, (int)bv_f_flag);
1928 
1929  cout << "Non-conformant block number is: " << unsigned(i >> bm::set_block_shift) << endl;
1930  assert(0); exit(1);
1931  }
1932  }
1933 
1934  printf("\n detailed check ok.\n");
1935 
1936 }
1937 
1938 static
1940 {
1941  if (!en1.valid() && !en2.valid())
1942  return;
1943  bool fsm_equal = en1.compare_state(en2);
1944  if (!fsm_equal)
1945  {
1946  cerr << "Enumerators FSM comparison failed" << endl;
1947  exit(1);
1948  }
1949 }
1950 
1951 // find last set bit by scan (not optimal)
1952 //
1953 static
1954 bool FindLastBit(const bvect& bv, bm::id_t& last_pos)
1955 {
1956  bvect::enumerator en = bv.first();
1957  if (!en.valid())
1958  return false;
1959  for (; en.valid(); ++en)
1960  {
1961  last_pos = *en;
1962  }
1963  return true;
1964 }
1965 
1966 
1967 
1968 template<class BV>
1969 void IntervalsCheck(const BV& bv)
1970 {
1971  cout << " ... IntervalsCheck" << endl;
1972  BV bv_inv(bv);
1973  bv_inv.invert();
1974 
1975  typename BV::size_type intervals = bm::count_intervals(bv);
1976  typename BV::size_type intervals_c = 1;
1977 
1978  typename BV::enumerator en1 = bv.get_enumerator(0);
1979  typename BV::enumerator en2 = bv_inv.get_enumerator(0);
1980 
1981  while (en1.valid())
1982  {
1983  typename BV::size_type from = *en1;
1984  typename BV::size_type to = *en2;
1985  assert(from != to);
1986 
1987  bool all_one, any_one;
1988  bool is_int, is_int_c;
1989  if (to == bm::id_max)
1990  {
1991  all_one = bv.is_all_one_range(from, to-1);
1992  assert(all_one);
1993  any_one = bv.any_range(from, to-1);
1994  assert(any_one);
1995  is_int = bm::is_interval(bv, from, to-1);
1996  is_int_c = test_interval(bv, from, to-1);
1997  assert(is_int == is_int_c);
1998  {
1999  bvect::size_type pos;
2000  bool b = bm::find_interval_end(bv, from, pos);
2001  assert(b == is_int);
2002  if (b)
2003  {
2004  assert(pos == to-1);
2005  }
2006  }
2007  if (is_int)
2008  {
2009  bvect::size_type pos;
2010  bool b = bm::find_interval_start(bv, from, pos);
2011  assert(b && pos == from);
2012  b = bm::find_interval_start(bv, to-1, pos);
2013  assert(b && pos == from);
2014  }
2015 
2016  break;
2017  }
2018  else
2019  {
2020  all_one = bv.is_all_one_range(from, to);
2021  assert(!all_one);
2022  any_one = bv.any_range(from, to);
2023  auto cnt = bv.count_range(from, to);
2024  if (any_one)
2025  {
2026  assert(cnt);
2027  }
2028  else
2029  {
2030  assert(!cnt);
2031  }
2032  is_int = bm::is_interval(bv, from, to);
2033  is_int_c = test_interval(bv, from, to);
2034  assert(is_int == is_int_c);
2035  if (is_int)
2036  {
2037  bvect::size_type pos;
2038  bool b = bm::find_interval_end(bv, from, pos);
2039  assert(b == is_int);
2040  if (b)
2041  {
2042  assert(pos == to);
2043  }
2044  b = bm::find_interval_start(bv, from, pos);
2045  assert(b && pos == from);
2046  pos = 0xDEADBEEF;
2047  b = bm::find_interval_start(bv, to, pos);
2048  assert(b && pos == from);
2049  }
2050 
2051  }
2052 
2053 
2054  if (to == bm::id_max)
2055  {}
2056  else
2057  {
2058  ++intervals_c;
2059  }
2060  if (to < from)
2061  {
2062  --from;
2063  assert(!bv.test(to));
2064  all_one = bv.is_all_one_range(from, to);
2065  assert(!all_one);
2066  any_one = bv.any_range(from, to);
2067  assert(!any_one);
2068  typename BV::size_type cnt = bv.count_range(to, from);
2069  assert(!cnt);
2070  is_int = bm::is_interval(bv, from, to);
2071  is_int_c = test_interval(bv, from, to);
2072  assert(is_int == is_int_c);
2073  if (is_int)
2074  {
2075  bvect::size_type pos;
2076  bool b = bm::find_interval_end(bv, from, pos);
2077  assert(b == is_int);
2078  if (b)
2079  {
2080  assert(pos == to);
2081  }
2082  b = bm::find_interval_start(bv, from, pos);
2083  assert(b && pos == from);
2084  pos = 0xDEADBEEF;
2085  b = bm::find_interval_start(bv, to, pos);
2086  assert(b && pos == from);
2087  }
2088 
2089  en2.go_to(from+1);
2090  if (!en2.valid())
2091  break;
2092  }
2093  else
2094  {
2095  --to;
2096  assert(bv.test(to));
2097  all_one = bv.is_all_one_range(from, to);
2098  assert(all_one);
2099  any_one = bv.any_range(from, to);
2100  assert(any_one);
2101  typename BV::size_type cnt = bv.count_range(from, to);
2102  assert(cnt == (to - from + 1));
2103  is_int = bm::is_interval(bv, from, to);
2104  is_int_c = test_interval(bv, from, to);
2105  assert(is_int == is_int_c);
2106  if (is_int)
2107  {
2108  bvect::size_type pos;
2109  bool b = bm::find_interval_end(bv, from, pos);
2110  assert(b == is_int);
2111  if (b)
2112  {
2113  assert(pos == to);
2114  }
2115  b = bm::find_interval_start(bv, from, pos);
2116  assert(b && pos == from);
2117  pos = 0xDEADBEEF;
2118  b = bm::find_interval_start(bv, to, pos);
2119  assert(b && pos == from);
2120  }
2121 
2122  en1.go_to(to+1);
2123  }
2124 
2125  } // while
2126  if (intervals != intervals_c)
2127  {
2128  typename BV::size_type diff;
2129  diff = std::max(intervals, intervals_c) - std::min(intervals, intervals_c);
2130  if (diff > 1)
2131  {
2132  cerr << "Intervals difference:" << diff << endl;
2133  assert(0);
2134  exit(1);
2135  }
2136  }
2137 }
2138 
2139 template<class BV>
2140 void interval_copy_range(BV& bv, const BV& bv_src,
2141  typename BV::size_type from, typename BV::size_type to)
2142 {
2143  bv.clear();
2144 
2145  if (from > to)
2146  bm::xor_swap(from, to);
2147 
2148  bm::interval_enumerator<bvect> ien(bv_src, from, false);
2149  while (ien.valid())
2150  {
2151  auto st = ien.start();
2152  assert(st >= from);
2153  if (st > to)
2154  break;
2155  auto end = ien.end();
2156  if (end > to)
2157  end = to;
2158 
2159  assert(st <= end);
2160  bv.set_range(st, end);
2161  if (!ien.advance())
2162  break;
2163  } // while
2164 }
2165 
2166 template<typename BV>
2167 void IntervalsEnumeratorCheck(const BV& bv, bool report)
2168 {
2169  if (report)
2170  cout << "..IntervalsEnumeratorCheck()" << flush;
2172 
2173  typename BV::size_type f, l, m;
2174  auto b = bv.find_range(f, l);
2175  if (!b)
2176  {
2177  assert(bv.count() == 0);
2178  return;
2179  }
2180  m = l - f;
2181  if (!m)
2182  m = l;
2183 
2184  bool eq;
2185  if (report)
2186  cout << "1 " << flush;
2187  // Full vector
2188  {
2189  bvect bv2; bvect bv2_c;
2190  bvect::mem_pool_guard g1(pool, bv2);
2191  bvect::mem_pool_guard g2(pool, bv2_c);
2192 
2193  bv2_c.copy_range(bv, 0, bm::id_max-1);
2194 
2195  interval_copy_range(bv2, bv, 0, bm::id_max - 1);
2196  eq = bv2.equal(bv2_c);
2197  assert(eq);
2198  }
2199  if (report)
2200  cout << "2 " << flush;
2201  // 0 -> frist
2202  {
2203  bvect bv2; bvect bv2_c;
2204  bvect::mem_pool_guard g1(pool, bv2);
2205  bvect::mem_pool_guard g2(pool, bv2_c);
2206 
2207  bv2_c.copy_range(bv, 0, l);
2208 
2209  interval_copy_range(bv2, bv, 0, l);
2210  eq = bv2.equal(bv2_c);
2211  assert(eq);
2212  }
2213 
2214  if (report)
2215  cout << "3 " << flush;
2216  // [first..last]
2217  {
2218  bvect bv2; bvect bv2_c;
2219  bvect::mem_pool_guard g1(pool, bv2);
2220  bvect::mem_pool_guard g2(pool, bv2_c);
2221 
2222  bv2_c.copy_range(bv, f, l);
2223 
2224  interval_copy_range(bv2, bv, f, l);
2225  eq = bv2.equal(bv2_c);
2226  assert(eq);
2227  }
2228  if (report)
2229  cout << "4 " << flush;
2230  // [last..]
2231  {
2232  bvect bv2; bvect bv2_c;
2233  bvect::mem_pool_guard g1(pool, bv2);
2234  bvect::mem_pool_guard g2(pool, bv2_c);
2235 
2236  bv2_c.copy_range(bv, l, bm::id_max-1);
2237 
2238  interval_copy_range(bv2, bv, l, bm::id_max - 1);
2239  eq = bv2.equal(bv2_c);
2240  assert(eq);
2241  }
2242  if (report)
2243  cout << "5 " << flush;
2244  // [mid..last]
2245  {
2246  bvect bv2; bvect bv2_c;
2247  bvect::mem_pool_guard g1(pool, bv2);
2248  bvect::mem_pool_guard g2(pool, bv2_c);
2249 
2250  bv2_c.copy_range(bv, m, l);
2251 
2252  interval_copy_range(bv2, bv, m, l);
2253  eq = bv2.equal(bv2_c);
2254  assert(eq);
2255  }
2256  if (report)
2257  cout << "6 " << flush;
2258  // [first..mid]
2259  {
2260  bvect bv2; bvect bv2_c;
2261  bvect::mem_pool_guard g1(pool, bv2);
2262  bvect::mem_pool_guard g2(pool, bv2_c);
2263 
2264  bv2_c.copy_range(bv, f, m);
2265 
2266  interval_copy_range(bv2, bv, f, m);
2267  eq = bv2.equal(bv2_c);
2268  assert(eq);
2269  }
2270  if (report)
2271  cout << endl;
2272 }
2273 
2274 
2275 
2276 
2277 // vectors comparison check
2278 
2279 void CheckVectors(bvect_mini &bvect_min,
2280  bvect &bvect_full,
2281  unsigned size,
2282  bool detailed)
2283 {
2284  cout << "\nVectors checking...bits to compare = " << size << endl;
2285 
2286  cout << "Bitcount summary : ";
2287  unsigned min_count = bvect_min.bit_count();
2288  cout << " minvector count = " << min_count;
2289  unsigned count = bvect_full.count();
2290  unsigned full_count = bvect_full.recalc_count();
2291  cout << " fullvector re-count = " << full_count << endl;
2292 
2293  if (min_count != full_count)
2294  {
2295  cout << "fullvector count = " << count << endl;
2296  cout << "Count comparison failed !!!!" << endl;
2297  print_stat(cout, bvect_full);
2298  DetailedCheckVectors(bvect_min, bvect_full, size);
2299  assert(0);
2300  exit(1);
2301  }
2302 
2303  if (full_count)
2304  {
2305  bool any = bvect_full.any();
2306  if (!any)
2307  {
2308  cout << "Anycheck failed!" << endl;
2309  exit(1);
2310  }
2311  }
2312 
2313  // find_last check
2314  {
2315  bm::id_t pos1 = 0;
2316  bm::id_t pos2 = 0;
2317  bool last_found1 = FindLastBit(bvect_full, pos1);
2318  bool last_found2 = bvect_full.find_reverse(pos2);
2319 
2320  assert(last_found1 == last_found2);
2321  if (last_found1)
2322  {
2323  assert(pos1 == pos2);
2324  }
2325  }
2326 
2327  IntervalsCheck(bvect_full);
2328  IntervalsEnumeratorCheck(bvect_full, true);
2329 
2330  if (!detailed)
2331  return;
2332 
2333  // get_next comparison
2334  cout << "Positive bits comparison..." << flush;
2335  unsigned nb_min = bvect_min.get_first();
2336  unsigned nb_ful = bvect_full.get_first();
2337 
2338  bvect::counted_enumerator en = bvect_full.first();
2339  unsigned nb_en = *en;
2340  bvect::enumerator en1 = bvect_full.get_enumerator(*en);
2341  if (nb_min != nb_ful)
2342  {
2343  cout << "!!!! First bit comparison failed. Full id = "
2344  << nb_ful << " Min id = " << nb_min
2345  << endl;
2346 
2347  bool bit_f = bvect_full.get_bit(nb_ful);
2348  cout << "Full vector'd bit #" << nb_ful << "is:"
2349  << bit_f << endl;
2350 
2351  bool bit_m = (bvect_min.is_bit_true(nb_min) == 1);
2352  cout << "Min vector'd bit #" << nb_min << "is:"
2353  << bit_m << endl;
2354 
2355 
2356  print_stat(cout,bvect_full);
2357 
2358  DetailedCheckVectors(bvect_min, bvect_full, size);
2359 
2360  exit(1);
2361  }
2362  CompareEnumerators(en, en1);
2363 
2364  if (full_count)
2365  {
2366  unsigned bit_count = 1;
2367  unsigned en_prev = nb_en;
2368 
2369  do
2370  {
2371  nb_min = bvect_min.get_next(nb_min);
2372  if (nb_min == 0)
2373  {
2374  break;
2375  }
2376 
2377  en_prev = nb_en;
2378  ++en;
2379  ++en1;
2380  CompareEnumerators(en, en1);
2381 
2382  if ((bit_count % 10 == 0) || (bit_count % 128 == 0))
2383  {
2384  bvect::enumerator en2 = bvect_full.get_enumerator(*en);
2385  CompareEnumerators(en, en2);
2386  }
2387 
2388  nb_en = *en;
2389  ++bit_count;
2390 
2391  if (nb_en != nb_min)
2392  {
2393  nb_ful = bvect_full.get_next(en_prev);
2394  cout << "!!!!! next bit comparison failed. Full id = "
2395  << nb_ful << " Min id = " << nb_min
2396  << " Enumerator = " << nb_en
2397  << endl;
2398 
2399  // bvect_full.stat();
2400 
2401  // DetailedCheckVectors(bvect_min, bvect_full, size);
2402 
2403  exit(1);
2404  }
2405  } while (en.valid());
2406  if (bit_count != min_count)
2407  {
2408  cout << " Bit count failed."
2409  << " min = " << min_count
2410  << " bit = " << bit_count
2411  << endl;
2412  exit(1);
2413  }
2414  }
2415 
2416  cout << "OK" << endl;
2417 
2418  return;
2419 }
2420 
2421 static
2423 {
2424  cout << "---------------------------- DynamicMatrixTest() test" << endl;
2425 
2426  {
2428  matr.resize(3, bm::set_sub_array_size);
2429  matr.set_zero();
2430 
2431  {
2432  unsigned* r = matr.row(1);
2433  for (unsigned i = 0; i < matr.cols(); ++i)
2434  {
2435  r[i] = i;
2436  }
2437  }
2438 
2439  matr.resize(matr.rows()+1, matr.cols());
2440  {
2441  unsigned* r = matr.row(3);
2442  for (unsigned i = 0; i < matr.cols(); ++i)
2443  {
2444  r[i] = i;
2445  }
2446  }
2447 
2448  {
2449  const unsigned* r = matr.row(1);
2450  for (unsigned i = 0; i < matr.cols(); ++i)
2451  {
2452  assert(r[i] == i);
2453  }
2454  }
2455  }
2456  {
2458  matr.init();
2459  matr.set_zero();
2460 
2461  matr.set(0, 0, 10);
2462  matr.set(1, 1, 100);
2463  matr.set(2, 2, 200);
2464 
2465  matr.set(1, 0, 1);
2466  matr.set(2, 0, 2);
2467  matr.set(2, 1, 21);
2468 
2469  assert(matr.get(0, 0) == 10);
2470  assert(matr.get(1, 1) == 100);
2471  assert(matr.get(2, 2) == 200);
2472 
2473 
2474  matr.replicate_triange();
2475 
2476  assert(matr.get(0, 1) == 1);
2477  assert(matr.get(0, 2) == 2);
2478  assert(matr.get(1, 2) == 21);
2479 
2480 
2481 
2482  bm::id64_t s;
2483  matr.sum(s, 0);
2484  assert(s == 13);
2485 
2486  }
2487 
2488 
2489  cout << "---------------------------- DynamicMatrixTest() test OK" << endl;
2490 }
2491 
2492 static
2494 {
2495  cout << "---------------------------- RSIndexTest() test" << endl;
2496 
2497  {
2498  rs_ind rsi;
2499 
2502 
2503  rsi.set_super_block(0, 1);
2504  rsi.set_super_block(1, 2);
2505  rsi.set_super_block(2, 3);
2506  rsi.set_null_super_block(3);
2507  rsi.set_full_super_block(4);
2508 
2509  auto sb_size = rsi.super_block_size();
2510  assert(sb_size == 5);
2511 
2512  auto rc = rsi.get_super_block_count(0);
2513  assert(rc == 1);
2514  rc = rsi.get_super_block_count(1);
2515  assert(rc == 2);
2516  rc = rsi.get_super_block_count(2);
2517  assert(rc == 3);
2518  rc = rsi.get_super_block_count(256);
2519  assert(rc == 0);
2520 
2521 
2522  auto bc = rsi.get_super_block_rcount(0);
2523  assert(bc == 1);
2524  bc = rsi.get_super_block_rcount(1);
2525  assert(bc == 3);
2526  bc = rsi.get_super_block_rcount(2);
2527  assert(bc == 6);
2528 
2529  unsigned i = rsi.find_super_block(1);
2530  assert(i == 0);
2531  i = rsi.find_super_block(2);
2532  assert(i == 1);
2533  i = rsi.find_super_block(3);
2534  assert(i == 1);
2535  i = rsi.find_super_block(4);
2536  assert(i == 2);
2537  i = rsi.find_super_block(200);
2538  assert(i == 4);
2539 /*
2540  i = rsi.find_super_block(bm::id_max);
2541  assert(i == 5);
2542 */
2543  }
2544 
2545  {
2546  unsigned bcount[bm::set_sub_array_size];
2547  bm::id64_t sub_count1[bm::set_sub_array_size];
2548  bm::id64_t sub_count2[bm::set_sub_array_size];
2549  for (unsigned i = 0; i < bm::set_sub_array_size; ++i)
2550  {
2551  bcount[i] = 0; sub_count1[i] = sub_count2[i] = 0;
2552  } // for
2553  bcount[0] = 1;
2554  bcount[255] = 2;
2555 
2556  sub_count1[0] = 1; // sub-range 0
2557  sub_count1[255] = 0; // sub-3
2558  sub_count2[0] = 1 << 16; // sub-2
2559  sub_count2[255] = 1 << 16; // sub 2,3
2560 
2561 
2562  rs_ind rsi;
2563  // -----------------------------------------
2567 
2568 
2569  rsi.set_null_super_block(0);
2570  auto tcnt = rsi.count();
2571  assert(tcnt == 0);
2572  rsi.register_super_block(1, &bcount[0], &sub_count1[0]);
2573  tcnt = rsi.count();
2574  assert(tcnt == 3);
2575  rsi.register_super_block(2, &bcount[0], &sub_count2[0]);
2576  tcnt = rsi.count();
2577  assert(tcnt == 6);
2578  rsi.set_full_super_block(3);
2579  tcnt = rsi.count();
2580  assert(tcnt == 6 + bm::set_sub_array_size * 65536);
2581 
2582  unsigned i = rsi.find_super_block(1);
2583  assert(i == 1);
2584  i = rsi.find_super_block(3);
2585  assert(i == 1);
2586  i = rsi.find_super_block(4);
2587  assert(i == 2);
2588  i = rsi.find_super_block(400);
2589  assert(i == 3);
2590 // i = rsi.find_super_block(bm::id_max);
2591 // assert(i == rsi.super_block_size());
2592 
2593  unsigned bc;
2594  rs_ind::size_type rbc;
2595  for (unsigned nb = 0; nb < bm::set_sub_array_size; ++nb)
2596  {
2597  bc = rsi.count(nb);
2598  assert(bc == 0);
2599  rbc = rsi.rcount(nb);
2600  assert(!rbc);
2601  }
2602  bc = rsi.count(bm::set_sub_array_size);
2603  assert(bc == 1);
2604  rbc = rsi.rcount(bm::set_sub_array_size);
2605  assert(rbc == 1);
2606 
2607  bc = rsi.count(bm::set_sub_array_size+1);
2608  assert(bc == 0);
2609  rbc = rsi.rcount(bm::set_sub_array_size+1);
2610  assert(rbc == 1);
2611 
2612  bc = rsi.count(bm::set_sub_array_size + 255);
2613  assert(bc == 2);
2614  rbc = rsi.rcount(bm::set_sub_array_size + 255);
2615  assert(rbc == 3);
2616 
2617  bc = rsi.count(bm::set_sub_array_size*3);
2618  assert(bc == 65536);
2619  rbc = rsi.rcount(bm::set_sub_array_size*3);
2620  assert(rbc == 65536+6);
2621  rbc = rsi.rcount(bm::set_sub_array_size*3 + 1);
2622  assert(rbc == 65536+6 + 65536);
2623 
2624 
2625  // ==========================
2626  {
2627  auto nb = rsi.find(1);
2628  assert(nb == 256);
2629 
2630  nb = rsi.find(2);
2631  assert(nb == bm::set_sub_array_size+255);
2632  nb = rsi.find(3);
2633  assert(nb == bm::set_sub_array_size+255);
2634 
2635  nb = rsi.find(4);
2636  assert(nb == bm::set_sub_array_size+255+1);
2637 
2638  nb = rsi.find(65536);
2639  assert(nb == 3*bm::set_sub_array_size+0);
2640  nb = rsi.find(65536*2);
2641  assert(nb == 3*bm::set_sub_array_size+1);
2642  nb = rsi.find(65536*3);
2643  assert(nb == 3*bm::set_sub_array_size+2);
2644  }
2645  // ==========================
2646 
2647  {
2648  bool b;
2651  bm::gap_word_t sub_range;
2652 
2653  rank = 1;
2654  b = rsi.find(&rank, &nb, &sub_range);
2655  assert(b);
2656  assert(nb == 256);
2657  assert(sub_range == 0);
2658  assert(rank == 1);
2659 
2660  rank = 2;
2661  b = rsi.find(&rank, &nb, &sub_range);
2662  assert(b);
2663  assert(nb == bm::set_sub_array_size+255);
2664  assert(sub_range == bm::rs3_border1 + 1);
2665  assert(rank == 1);
2666 
2667  rank = 3;
2668  b = rsi.find(&rank, &nb, &sub_range);
2669  assert(b);
2670  assert(nb == bm::set_sub_array_size+255);
2671  assert(sub_range == bm::rs3_border1 + 1);
2672  assert(rank == 2);
2673 
2674  rank = 4;
2675  b = rsi.find(&rank, &nb, &sub_range);
2676  assert(b);
2677  assert(nb == bm::set_sub_array_size+255+1);
2678  assert(sub_range == bm::rs3_border0 + 1);
2679  assert(rank == 1);
2680 
2681  rank = 5;
2682  b = rsi.find(&rank, &nb, &sub_range);
2683  assert(b);
2684  assert(nb == bm::set_sub_array_size+256+255);
2685  assert(sub_range == bm::rs3_border0 + 1);
2686  assert(rank == 1);
2687 
2688  rank = 6;
2689  b = rsi.find(&rank, &nb, &sub_range);
2690  assert(b);
2691  assert(nb == bm::set_sub_array_size+256+255);
2692  assert(sub_range == bm::rs3_border1 + 1);
2693  assert(rank == 1);
2694 
2695  rank = 65536;
2696  b = rsi.find(&rank, &nb, &sub_range);
2697  assert(b);
2698  assert(nb == 3*bm::set_sub_array_size+0);
2699  assert(sub_range == bm::rs3_border1 + 1);
2700  assert(rank == 65536 - 6 - bm::rs3_border1 - 1);
2701 
2702  rank = 65536 + 7;
2703  b = rsi.find(&rank, &nb, &sub_range);
2704  assert(b);
2705  assert(nb == 3*bm::set_sub_array_size+1);
2706  assert(sub_range == 0);
2707  assert(rank == 1);
2708 
2709  rank = bm::id_max;
2710  b = rsi.find(&rank, &nb, &sub_range);
2711  assert(!b);
2712 
2713 
2714  }
2715 
2716  }
2717 
2718 
2719  cout << "---------------------------- RSIndexTest() test OK" << endl;
2720 }
2721 
2722 
2723 static
2725 {
2726  bvect bvect_full;
2727 
2728  for (unsigned i = 0; i < 100000; ++i)
2729  {
2730  bvect_full.set_bit(i);
2731  }
2733  bvect_full.optimize(tb);
2734  bvect_full.clear();
2735 
2736  print_stat(cout, bvect_full);
2737 
2738  unsigned count = bvect_full.count();
2739  assert(count == 0);
2740  print_stat(cout, bvect_full);
2741 }
2742 
2743 static
2745 {
2746  cout << "---------------------------- WordCmp test" << endl;
2747 
2748  for (int i = 0; i < 10000000; ++i)
2749  {
2750  unsigned w1 = unsigned(rand());
2751  unsigned w2 = unsigned(rand());
2752  int res = wordcmp0(w1, w2);
2753  int res2 = wordcmp(w1, w2);
2754  if (res != res2)
2755  {
2756  printf("WordCmp failed !\n");
2757  exit(1);
2758  }
2759 
2760  res = wordcmp0((unsigned)0U, (unsigned)w2);
2761  res2 = wordcmp((unsigned)0U, (unsigned)w2);
2762 
2763  if (res != res2)
2764  {
2765  printf("WordCmp 0 test failed !\n");
2766  exit(1);
2767  }
2768 
2769  res = wordcmp0((unsigned)~0U, (unsigned)w2);
2770  res2 = wordcmp((unsigned)~0U, (unsigned)w2);
2771 
2772  if (res != res2)
2773  {
2774  printf("WordCmp ~0 test failed !\n");
2775  exit(1);
2776  }
2777 
2778  res = wordcmp0((unsigned)w2, (unsigned)0);
2779  res2 = wordcmp((unsigned)w2, (unsigned)0);
2780 
2781  if (res != res2)
2782  {
2783  printf("WordCmp 0-2 test failed !\n");
2784  exit(1);
2785  }
2786 
2787  }
2788 
2789  cout << "Ok." << endl;
2790 }
2791 
2792 
2793 static
2795 {
2796  cout << "---------------------------- CountChange test" << endl;
2797 
2798 #ifdef VECT_BLOCK_CHANGE
2799 
2800  unsigned i, c, cc;
2802 
2803  for (i = 0; i < bm::set_block_size; ++i)
2804  blk[i] = 0;
2805 
2806  c = VECT_BLOCK_CHANGE(blk, sizeof(blk)/sizeof(blk[0]));
2807  cc = bm::bit_block_change32(blk, sizeof(blk)/sizeof(blk[0]));
2808  assert(c == 1);
2809  assert(c == cc);
2810 
2811  blk[0] = 1;
2812  c = VECT_BLOCK_CHANGE(blk, sizeof(blk)/sizeof(blk[0]));
2813  cc = bm::bit_block_change32(blk, sizeof(blk)/sizeof(blk[0]));
2814  assert(c == 2);
2815  assert(c == cc);
2816 
2817  blk[0] = 0xFF;
2818  c = VECT_BLOCK_CHANGE(blk, sizeof(blk)/sizeof(blk[0]));
2819  cc = bm::bit_block_change32(blk, sizeof(blk)/sizeof(blk[0]));
2820  assert(c == 2);
2821  assert(c == cc);
2822 
2823  blk[0] = ~0u;
2824  c = VECT_BLOCK_CHANGE(blk, sizeof(blk)/sizeof(blk[0]));
2825  cc = bm::bit_block_change32(blk, sizeof(blk)/sizeof(blk[0]));
2826  assert(c == 2);
2827  assert(c == cc);
2828 
2829  blk[0] = blk[1] = blk[2] = blk[3] = 2;
2830  c = VECT_BLOCK_CHANGE(blk, sizeof(blk)/sizeof(blk[0]));
2831  cc = bm::bit_block_change32(blk, sizeof(blk)/sizeof(blk[0]));
2832  assert(c == cc);
2833 
2834  blk[4] = blk[5] = blk[6] = blk[7] = 2;
2835  c = VECT_BLOCK_CHANGE(blk, sizeof(blk)/sizeof(blk[0]));
2836  cc = bm::bit_block_change32(blk, sizeof(blk)/sizeof(blk[0]));
2837  assert(c == cc);
2838 
2839  {
2840  for (i = 0; i < bm::set_block_size; ++i)
2841  blk[i] = 2;
2842 
2843  c = VECT_BLOCK_CHANGE(blk, sizeof(blk)/sizeof(blk[0]));
2844  cc = bm::bit_block_change32(blk, sizeof(blk)/sizeof(blk[0]));
2845  assert(c == cc);
2846  }
2847 
2848  {
2849  for (i = 0; i < bm::set_block_size; ++i)
2850  blk[i] = 1u << 31;
2851 
2852  c = VECT_BLOCK_CHANGE(blk, sizeof(blk)/sizeof(blk[0]));
2853  cc = bm::bit_block_change32(blk, sizeof(blk)/sizeof(blk[0]));
2854  assert(c == cc);
2855  }
2856 
2857  {
2858  for (i = 0; i < bm::set_block_size; ++i)
2859  blk[i] = ~0u << 30;
2860 
2861  c = VECT_BLOCK_CHANGE(blk, sizeof(blk)/sizeof(blk[0]));
2862  cc = bm::bit_block_change32(blk, sizeof(blk)/sizeof(blk[0]));
2863  assert(c == cc);
2864  }
2865 
2866  cout << "Block change stress..." << endl;
2867  {
2868  std::chrono::time_point<std::chrono::steady_clock> s;
2869  std::chrono::time_point<std::chrono::steady_clock> f;
2870  s = std::chrono::steady_clock::now();
2871 
2872 
2873  unsigned k_max = (1u << 31) / 4;
2874  for (unsigned k = 0; k <= k_max; ++k)
2875  {
2876  for (i = 0; i < bm::set_block_size; ++i)
2877  blk[i] = k;
2878  c = VECT_BLOCK_CHANGE(blk, sizeof(blk)/sizeof(blk[0]));
2879  cc = bm::bit_block_change32(blk, sizeof(blk)/sizeof(blk[0]));
2880  assert(c == cc);
2881 
2882  if (k % 100000 == 0)
2883  {
2884  f = std::chrono::steady_clock::now();
2885  auto diff = f - s;
2886  auto d = std::chrono::duration <double, std::milli> (diff).count();
2887 
2888  if (!is_silent)
2889  cout << "\r" << k << " / " << k_max << " (" << d << "ms)" << flush;
2890 
2891  s = std::chrono::steady_clock::now();
2892  }
2893  } // for k
2894  }
2895  cout << endl;
2896 
2897 #endif
2898 
2899 
2900  cout << "---------------------------- CountChange test OK" << endl;
2901 }
2902 
2903 
2904 inline
2906  const bm::word_t* block,
2907  const bm::word_t* xor_block,
2908  block_waves_xor_descr& x_descr,
2909  bm::xor_complement_match& match_type)
2910 {
2911  unsigned gc, bc;
2912  unsigned c_gc, c_bc;
2913  bm::compute_s_block_descr(block, x_descr, &gc, &bc);
2914  bm::bit_block_change_bc(block, &c_gc, &c_bc);
2915  assert(gc == c_gc);
2916  assert(bc == c_bc);
2917 
2919  bm::id64_t d64 = bm::calc_block_digest0(block);
2920 
2921  xor_scanner<bvect> xs;
2922  xs.compute_s_block_stats(block);
2923  xs.compute_xor_complexity_descr(block, d64, xor_block, x_descr, xmd);
2924  match_type = xmd.match_type;
2925  return xmd.xor_d64;
2926 }
2927 
2928 static
2929 void Check_XOR_Product(const bm::word_t* block,
2930  const bm::word_t* xor_block,
2931  bm::id64_t digest)
2932 {
2933  assert(digest);
2934 
2937 
2938  bm::bit_block_xor(t_blk1, block, xor_block, digest);
2939  bm::bit_block_xor(t_blk2, t_blk1, xor_block, digest);
2940 
2941  unsigned cnt = bm::bit_block_xor_count(block, t_blk2);
2942  assert(cnt == 0); // identically restored
2943 }
2944 
2945 
2946 static
2948 {
2949  cout << "---------------------------- TestBlockCountXORChange() test" << endl;
2950  unsigned i;
2951  bm::id64_t d64;
2952  bm::block_waves_xor_descr x_descr;
2953  unsigned gc, bc;
2954 
2955  {
2957  bmc.nb=100;
2958  bmc.chain_size = 56;
2959  bmc.ref_idx[0]=1024;
2960  bmc.xor_d64[0]=~0ULL;
2961 
2963  bmc2 = bmc;
2964  assert(bmc2.nb==100);
2965  assert(bmc2.chain_size == 56);
2966  assert(bmc2.ref_idx[0]==1024);
2967  }
2968 
2969  {
2971  bm::compute_s_block_descr(blk, x_descr, &gc, &bc);
2972  for (unsigned k = 0; k < bm::block_waves; ++k)
2973  {
2974  assert(x_descr.sb_bc[k] == 0);
2975  } // for
2976  assert(bc == 0);
2977  assert(gc == 1);
2978 
2980  bm::compute_s_block_descr(blk, x_descr, &gc, &bc);
2981  assert(x_descr.sb_bc[0] == 0);
2982  assert(x_descr.sb_bc[1] == 1);
2983  assert(x_descr.sb_gc[0] == 1);
2984  assert(x_descr.sb_gc[1] == 2);
2985  assert(bc == 1);
2986  assert(gc == 3);
2987 
2988  blk[bm::set_block_digest_wave_size-1] = 1u << 31;
2989  bm::compute_s_block_descr(blk, x_descr, &gc, &bc);
2990  assert(x_descr.sb_bc[0] == 1);
2991  assert(x_descr.sb_bc[1] == 1);
2992  assert(x_descr.sb_gc[0] == 2);
2993  assert(x_descr.sb_gc[1] == 2 || x_descr.sb_gc[1] == 1);
2994  assert(bc == 2);
2995  assert(gc == 3);
2996 
2997 
2998  blk[bm::set_block_digest_wave_size-1] = 0;
3000 
3001 
3002 
3003  blk[0] = 1;
3004  bm::compute_s_block_descr(blk, x_descr, &gc, &bc);
3005  assert(x_descr.sb_bc[0] == 1);
3006  assert(x_descr.sb_gc[0] == 2);
3007  for (unsigned k = 1; k < bm::block_waves; ++k)
3008  {
3009  assert(x_descr.sb_bc[k] == 0);
3010  } // for
3011  assert(bc == 1);
3012  assert(gc == 2);
3013 
3014 
3015 
3016  blk[0] = 1 | (1 << 1);
3017  bm::compute_s_block_descr(blk, x_descr, &gc, &bc);
3018  assert(x_descr.sb_bc[0] == 2);
3019  assert(x_descr.sb_gc[0] == 2);
3020  for (unsigned k = 1; k < bm::block_waves; ++k)
3021  {
3022  assert(x_descr.sb_bc[k] == 0);
3023  assert(x_descr.sb_gc[k] == 0);
3024  } // for
3025  assert(bc == 2);
3026  assert(gc == 2);
3027 
3028  blk[bm::set_block_size-1] = 1 | (1 << 1);
3029  bm::compute_s_block_descr(blk, x_descr, &gc, &bc);
3030  assert(x_descr.sb_bc[0] == 2);
3031  assert(x_descr.sb_gc[0] == 2);
3032  assert(x_descr.sb_bc[bm::block_waves-1] == 2);
3033  assert(x_descr.sb_gc[bm::block_waves-1] == 3 || x_descr.sb_gc[bm::block_waves-1] == 2);
3034  for (unsigned k = 1; k < bm::block_waves-1; ++k)
3035  {
3036  assert(x_descr.sb_bc[k] == 0);
3037  assert(x_descr.sb_gc[k] == 0);
3038  } // for
3039 
3040  blk[0] = 0;
3041  bm::compute_s_block_descr(blk, x_descr, &gc, &bc);
3042  assert(x_descr.sb_bc[bm::block_waves-1] == 2);
3043  assert(x_descr.sb_gc[bm::block_waves-1] == 3 || x_descr.sb_gc[bm::block_waves-1] == 2);
3044  for (unsigned k = 0; k < bm::block_waves-1; ++k)
3045  {
3046  assert(x_descr.sb_bc[k] == 0);
3047  if (k == 0)
3048  {
3049  assert(x_descr.sb_gc[k] == 1);
3050  }
3051  } // for
3052 
3053  }
3054 
3055  // test match vector search/refine
3056  //
3057  {
3058  typedef
3060  xor_matches_vector_type;
3061  typedef
3063  m_pairs_vector_type;
3064  xor_matches_vector_type xm_vect;
3065  m_pairs_vector_type pm_vect;
3066  {
3068  xmd.match_type = e_xor_match_BC; xmd.ref_idx = 10; xmd.xor_d64 = 1ull;
3069  xm_vect.push_back(xmd);
3070  }
3071  auto cnt = bm::greedy_refine_match_vector(pm_vect, xm_vect, 10, (1ull << 1), e_xor_match_BC);
3072  assert(!cnt);
3073  assert(pm_vect.size()==0);
3074  cnt = bm::greedy_refine_match_vector(pm_vect, xm_vect, 11, (1ull << 1), e_xor_match_BC);
3075  assert(cnt == 1);
3076  assert(pm_vect.size()==1);
3077  bm::match_pair mp = pm_vect[0];
3078  assert(mp.ref_idx == 10);
3079  assert(pm_vect[0].xor_d64 == 1ull);
3080  cnt = bm::greedy_refine_match_vector(pm_vect, xm_vect, 11, (1ull << 1), e_xor_match_iBC);
3081  assert(cnt == 0);
3082 /*
3083  int brate = bm::check_pair_vect_vbr(pm_vect, 255);
3084  assert(brate == 1);
3085  brate = bm::check_pair_vect_vbr(pm_vect, 65535);
3086  assert(brate == 2);
3087  brate = bm::check_pair_vect_vbr(pm_vect, 65536);
3088  assert(brate == 0);
3089 */
3090 
3091  {
3093  xmd.match_type = e_xor_match_BC; xmd.ref_idx = 20; xmd.xor_d64 = 3ull;
3094  xm_vect.push_back(xmd);
3095  }
3096  cnt = bm::greedy_refine_match_vector(pm_vect, xm_vect, 11, (1ull << 1), e_xor_match_BC);
3097  assert(cnt == 1);
3098  mp = pm_vect[0];
3099  assert(pm_vect[0].ref_idx == 20);
3100  assert(mp.xor_d64 == 1ull);
3101 
3102  {
3103  xm_vect.resize(0);
3105  xmd.match_type = e_xor_match_BC; xmd.ref_idx = 20; xmd.xor_d64 = 3ull;
3106  xm_vect.push_back(xmd);
3107  xmd.match_type = e_xor_match_BC; xmd.ref_idx = 25; xmd.xor_d64 = 3ull << 60;
3108  xm_vect.push_back(xmd);
3109  }
3110  cnt = bm::greedy_refine_match_vector(pm_vect, xm_vect, 11, (1ull << 1), e_xor_match_BC);
3111  assert(cnt == 2);
3112  mp = pm_vect[0];
3113  assert(pm_vect[0].ref_idx == 20);
3114  assert(mp.xor_d64 == 1ull);
3115  mp = pm_vect[1];
3116  assert(mp.ref_idx == 25);
3117  assert(mp.xor_d64 == 3ull << 60);
3118 
3119  }
3120 
3121  {
3122  bm::xor_complement_match match_type;
3125 
3126  for (i = 0; i < bm::set_block_size; ++i)
3127  blk[i] = blk_xor[i] = 0;
3128 
3129  d64 = bit_block_calc_xor_change_digest(blk, blk_xor, x_descr, match_type);
3130  assert(d64 == ~0ull);
3131  assert(match_type == bm::e_xor_match_GC);
3132  for (unsigned k = 0; k < bm::block_waves; ++k)
3133  {
3134  assert(x_descr.sb_gc[k] == 1 || (k && x_descr.sb_gc[k] == 0));
3135  assert(x_descr.sb_xor_gc[k] == 1 || (k && x_descr.sb_xor_gc[k] == 0));
3136  } // for k
3137 
3138  blk[0] = 1;
3139  d64 = bit_block_calc_xor_change_digest(blk, blk_xor, x_descr, match_type);
3140  assert(!d64);
3141  assert(match_type == bm::e_no_xor_match);
3142 
3143  assert(x_descr.sb_gc[0] == 2);
3144  assert(x_descr.sb_xor_gc[0] == 2);
3145  for (unsigned k = 1; k < bm::block_waves; ++k)
3146  {
3147  assert(x_descr.sb_gc[k] == 1 || (k && x_descr.sb_gc[k] == 0));
3148  assert(x_descr.sb_xor_gc[k] == 1 || (k && x_descr.sb_xor_gc[k] == 0));
3149  } // for k
3150 
3151  // ----------------------------
3152  blk[0] = 1 | (1<<1) | (1<<2); blk_xor[0] = (1 << 1);
3153  d64 = bit_block_calc_xor_change_digest(blk, blk_xor, x_descr, match_type);
3154  assert(d64);
3155  assert(x_descr.sb_bc[0] == 3);
3156  assert(x_descr.sb_xor_bc[0] == 2);
3157  assert(match_type == bm::e_xor_match_BC);
3158 
3159 
3160  // ----------------------------
3161  blk[0] = 1; blk_xor[0] = 1;
3162  d64 = bit_block_calc_xor_change_digest(blk, blk_xor, x_descr, match_type);
3163  cout << x_descr.sb_xor_gc[0] << endl;
3164  assert(x_descr.sb_gc[0] == 2);
3165  assert(match_type == bm::e_xor_match_BC);
3166  // next assert hides non-critical discrepancy between SIMD versions
3167  assert(x_descr.sb_xor_gc[0] == 1 || x_descr.sb_xor_gc[0] == 0);
3168  assert(d64 == 1ull);
3169  for (unsigned k = 1; k < bm::block_waves; ++k)
3170  {
3171  assert(x_descr.sb_gc[k] == 1 || (k && x_descr.sb_gc[k] == 0));
3172  assert(x_descr.sb_xor_gc[k] == 1 || (k && x_descr.sb_xor_gc[k] == 0));
3173  } // for k
3174 
3175  Check_XOR_Product(blk, blk_xor, d64);
3176 
3177  blk[0] = (1 << 10) | (1 << 12) | (1 << 14) | ( 1 << 16) | (1 << 18) | (1<<19);
3178  blk_xor[0] = (1 << 11) | (1 << 13) | (1 << 15) | (1 << 17);
3179  unsigned off = (60 * bm::set_block_digest_wave_size);
3180  blk[off] = (1 << 10) | (1 << 12);
3181 
3182  d64 = bit_block_calc_xor_change_digest(blk, blk_xor, x_descr, match_type);
3183  assert(x_descr.sb_gc[0] == 11);
3184  assert(x_descr.sb_xor_gc[0] == 3);
3185  assert((d64 & 1));
3186  assert(match_type == bm::e_xor_match_GC);
3187 
3188  Check_XOR_Product(blk, blk_xor, d64);
3189 
3190  for (unsigned k = 1; k < bm::block_waves; ++k)
3191  {
3192  if (k!= 60)
3193  {
3194  assert(x_descr.sb_gc[k] == 0);
3195  assert(x_descr.sb_xor_gc[k] == 0);
3196  }
3197  else
3198  {
3199  assert(x_descr.sb_gc[60] == 4);
3200  assert(x_descr.sb_xor_gc[60] == 4);
3201  }
3202  } // for k
3203 
3204  blk_xor[off] = (1 << 10) | (1 << 11) | (1 << 12);
3205  d64 = bit_block_calc_xor_change_digest(blk, blk_xor, x_descr, match_type);
3206  assert(x_descr.sb_gc[0] == 11);
3207  assert(x_descr.sb_xor_gc[0] == 3);
3208  assert((d64 & 1) && (d64 & (1ull << 60)));
3209  assert(match_type == bm::e_xor_match_GC);
3210 
3211  for (unsigned k = 1; k < bm::block_waves; ++k)
3212  {
3213  if (k!= 60)
3214  {
3215  assert(x_descr.sb_gc[k] == 0);
3216  assert(x_descr.sb_xor_gc[k] == 0);
3217  }
3218  else
3219  {
3220  assert(x_descr.sb_gc[60] == 4);
3221  assert(x_descr.sb_xor_gc[60] == 2);
3222  }
3223  } // for k
3224 
3225  Check_XOR_Product(blk, blk_xor, d64);
3226 
3227  }
3228 
3229  cout << "---------------------------- TestBlockCountXORChange() test OK" << endl;
3230 }
3231 
3232 
3233 /*!
3234  \brief Converts bit block to GAP.
3235  \param dest - Destinatio GAP buffer.
3236  \param src - Source bitblock buffer.
3237  \param bits - Number of bits to convert.
3238  \param dest_len - length of the dest. buffer.
3239  \return New length of GAP block or 0 if conversion failed
3240  (insufficicent space).
3241 
3242  @ingroup gapfunc
3243 */
3244 template<typename T>
3245 unsigned bit_convert_to_gap(T* dest,
3246  const unsigned* src,
3247  bm::id_t bits,
3248  unsigned dest_len)
3249 {
3250  T* pcurr = dest;
3251  T* end = dest + dest_len;
3252  unsigned bitval = (*src) & 1u;
3253  *pcurr = (T)bitval;
3254 
3255  ++pcurr;
3256  *pcurr = 0;
3257  unsigned bit_idx = 0;
3258  unsigned bitval_next;
3259 
3260  unsigned val = *src;
3261 
3262  do
3263  {
3264  // We can fast pace if *src == 0 or *src = 0xffffffff
3265 
3266  while (val == 0 || val == 0xffffffff)
3267  {
3268  bitval_next = val ? 1 : 0;
3269  if (bitval != bitval_next)
3270  {
3271  *pcurr++ = (T)(bit_idx-1);
3272  assert((pcurr-1) == (dest+1) || *(pcurr-1) > *(pcurr-2));
3273  if (pcurr >= end)
3274  {
3275  return 0; // OUT of memory
3276  }
3277  bitval = bitval_next;
3278  }
3279  bit_idx += unsigned(sizeof(*src) * 8);
3280  if (bit_idx >= bits)
3281  {
3282  goto complete;
3283  }
3284  ++src;
3285  val = *src;
3286  }
3287 
3288  unsigned mask = 1;
3289  while (mask)
3290  {
3291  // Now plane bitshifting. TODO: Optimization wanted.
3292 
3293  bitval_next = val & mask ? 1 : 0;
3294  if (bitval != bitval_next)
3295  {
3296  *pcurr++ = (T)(bit_idx-1);
3297  assert((pcurr-1) == (dest+1) || *(pcurr-1) > *(pcurr-2));
3298  bitval = bitval_next;
3299  if (pcurr >= end)
3300  return 0; // OUT of memory
3301  }
3302  mask <<= 1;
3303  ++bit_idx;
3304  } // while mask
3305 
3306  if (bit_idx >= bits)
3307  goto complete;
3308 
3309  ++src;
3310  val = *src;
3311 
3312  } while(1);
3313 
3314 complete:
3315  *pcurr = (T)(bit_idx-1);
3316  unsigned len = (unsigned)(pcurr - dest);
3317  *dest = (T)((*dest & 7) + (len << 3));
3318  return len;
3319 }
3320 
3321 /*
3322 static
3323 void TestGAP_XOR()
3324 {
3325  cout << "---------------------------- TestGAP_XOR()" << endl;
3326 
3327  unsigned gc, bc;
3328  {
3329  gap_vector gapv1(0);
3330  gap_vector gapv2(0);
3331  gap_word_t* gap_buf1 = gapv1.get_buf();
3332  gap_word_t* gap_buf2 = gapv2.get_buf();
3333 
3334 
3335  gap_operation_dry_xor(gap_buf1, gap_buf2, gc, bc);
3336  assert(bc == 0);
3337  assert(gc == 1);
3338 
3339  gapv1.set_bit(256);
3340  gapv2.set_bit(256);
3341 
3342  gap_operation_dry_xor(gap_buf1, gap_buf2, gc, bc);
3343  assert(bc == 0);
3344  assert(gc == 1);
3345 
3346  gapv1.set_bit(258);
3347  gapv2.set_bit(257);
3348  gapv1.set_bit(259);
3349  gapv1.set_bit(260);
3350 
3351  gap_operation_dry_xor(gap_buf1, gap_buf2, gc, bc);
3352  assert(bc == 4);
3353  assert(gc == 3);
3354 
3355  }
3356 
3357  cout << "---------------------------- TestGAP_XOR() - end" << endl;
3358 }*/
3359 
3360 static
3362 {
3363  cout << "---------------------------- TestBlockToGAP" << endl;
3364 
3365  unsigned i;
3367 
3368  for (i = 0; i < bm::set_block_size; ++i)
3369  blk[i] = 0;
3370 
3371  {
3372  gap_vector gapv1(0);
3373  gap_vector gapv2(0);
3374  gap_word_t* gap_buf1 = gapv1.get_buf();
3375  *gap_buf1 = 0;
3376  unsigned len1 = bit_convert_to_gap(gap_buf1, blk, bm::gap_max_bits, bm::gap_max_buff_len);
3377  gap_word_t* gap_buf2 = gapv2.get_buf();
3378  unsigned len2 = bm::bit_to_gap(gap_buf2, blk, bm::gap_max_buff_len);
3379  print_gap(gapv2, 100);
3380  assert(len1 == len2);
3381  int cmp = bm::gapcmp(gap_buf1, gap_buf2);
3382  assert(cmp == 0);
3383 
3384  unsigned pos;
3385  bool f = bm::gap_find_first_diff(gap_buf1, gap_buf2, &pos);
3386  assert(!f);
3387  }
3388 
3389  unsigned test_arr[] = { 1, 2, 3, (~0u << 4), ~0u, (~0u >> 1), (~0u >> 2) };
3390  unsigned arr_size = sizeof(test_arr)/sizeof(test_arr[0]);
3391  for (unsigned k = 0; k < bm::set_block_size; ++k)
3392  {
3393  for (i = 0; i < bm::set_block_size; ++i)
3394  blk[i] = 0;
3395  for (i = 0; i < arr_size; ++i)
3396  {
3397  blk[k] = test_arr[i];
3398 
3399  gap_vector gapv1(0);
3400  gap_vector gapv2(0);
3401  gap_word_t* gap_buf1 = gapv1.get_buf();
3402  *gap_buf1 = 0;
3403  unsigned len1 = bit_convert_to_gap(gap_buf1, blk, bm::gap_max_bits, bm::gap_max_buff_len);
3404  print_gap(gapv1, 100);
3405  gap_word_t* gap_buf2 = gapv2.get_buf();
3406  unsigned len2 = bm::bit_to_gap(gap_buf2, blk, bm::gap_max_buff_len);
3407  assert(len1 == len2);
3408  assert(len1);
3409  print_gap(gapv2, 100);
3410  int cmp = bm::gapcmp(gap_buf1, gap_buf2);
3411  assert(cmp == 0);
3412 
3413  unsigned pos;
3414  bool f = bm::gap_find_first_diff(gap_buf1, gap_buf2, &pos);
3415  assert(!f);
3416  }
3417  } // for k
3418 
3419  cout << "Test arr - ok" << endl;
3420 
3421  {
3422  gap_vector gapv1(0);
3423  gap_vector gapv2(0);
3424  gap_word_t* gap_buf1 = gapv1.get_buf();
3425  *gap_buf1 = 0;
3426  gap_word_t* gap_buf2 = gapv2.get_buf();
3427  *gap_buf2 = 0;
3428 
3429  unsigned mask = 1u;
3430  for (unsigned k = 0; k < bm::set_block_size; ++k)
3431  {
3432  for (i = 0; i < bm::set_block_size; ++i)
3433  blk[i] = 0;
3434 
3435  blk[k] = mask;
3436  mask <<= 1;
3437  if (!mask)
3438  mask = 1u;
3439 
3440  *gap_buf1 = 0;
3441  *gap_buf2 = 0;
3442  unsigned len1 = bit_convert_to_gap(gap_buf1, blk, bm::gap_max_bits, bm::gap_max_buff_len);
3443  unsigned len2 = bm::bit_to_gap(gap_buf2, blk, bm::gap_max_buff_len);
3444  assert(len1);
3445  assert(len1 == len2);
3446  if (len1)
3447  {
3448  int cmp = bm::gapcmp(gap_buf1, gap_buf2);
3449  assert(cmp == 0);
3450 
3451  unsigned pos;
3452  bool f = bm::gap_find_first_diff(gap_buf1, gap_buf2, &pos);
3453  assert(!f);
3454  }
3455  }
3456  }
3457 
3458  cout << "mask shift - ok" << endl;
3459 
3460  {
3461  gap_vector gapv1(0);
3462  gap_vector gapv2(0);
3463  gap_word_t* gap_buf1 = gapv1.get_buf();
3464  *gap_buf1 = 0;
3465  gap_word_t* gap_buf2 = gapv2.get_buf();
3466  *gap_buf2 = 0;
3467 
3468  unsigned max_try = 1000000;
3469  for (unsigned k = 0; k < max_try; ++k)
3470  {
3471  for (i = 0; i < bm::set_block_size; ++i)
3472  blk[i] = 0;
3473 
3474  unsigned idx = (unsigned)rand() % bm::set_block_size;
3475  blk[idx] |= (unsigned)rand();
3476  idx = (unsigned)rand() % bm::set_block_size;
3477  blk[idx] |= (unsigned)rand();
3478  idx = (unsigned)rand() % bm::set_block_size;
3479  blk[idx] |= (unsigned)rand();
3480  idx = (unsigned)rand() % bm::set_block_size;
3481  blk[idx] |= (unsigned)rand();
3482 
3483  idx = (unsigned)rand() % bm::set_block_size;
3484  blk[idx] |= ~0u;
3485  idx = (unsigned)rand() % bm::set_block_size;
3486  blk[idx] |= (~0u << (rand()%31));
3487 
3488  *gap_buf1 = 0;
3489  *gap_buf2 = 0;
3490  unsigned len1 = bit_convert_to_gap(gap_buf1, blk, bm::gap_max_bits, bm::gap_max_buff_len);
3491  unsigned len2 = bm::bit_to_gap(gap_buf2, blk, bm::gap_max_buff_len);
3492  assert(len1);
3493  assert(len1 == len2);
3494  if (len1)
3495  {
3496  int cmp = bm::gapcmp(gap_buf1, gap_buf2);
3497  assert(cmp == 0);
3498  unsigned pos;
3499  bool f = bm::gap_find_first_diff(gap_buf1, gap_buf2, &pos);
3500  assert(!f);
3501  }
3502  }
3503  }
3504 
3505 
3506  cout << "---------------------------- TestBlockToGAP OK" << endl;
3507 
3508 }
3509 
3510 
3511 static
3513 {
3514  cout << "---------------------------- ShiftRotate test" << endl;
3515 
3518  unsigned i;
3519 
3520  for (i = 0; i < bm::set_block_size; ++i)
3521  {
3522  blk0[i] = blk1[i] = 1;
3523  }
3524 
3527 
3528  for (i = 0; i < bm::set_block_size; ++i)
3529  {
3530  if (blk0[i] != 2 || blk0[i] != blk1[i])
3531  {
3532  cerr << "Cyclic rotate check failed" << endl;
3533  exit(1);
3534  }
3535  }
3536 
3539 
3540  for (i = 0; i < bm::set_block_size; ++i)
3541  {
3542  if (blk0[i] != 4 || blk0[i] != blk1[i])
3543  {
3544  cerr << "Cyclic rotate check failed" << endl;
3545  exit(1);
3546  }
3547  }
3548 
3549  for (i = 0; i < bm::set_block_size; ++i)
3550  {
3551  blk0[i] = blk1[i] = 1u << 31;
3552  }
3555 
3556  for (i = 0; i < bm::set_block_size; ++i)
3557  {
3558  if (blk0[i] != 1 || blk0[i] != blk1[i])
3559  {
3560  cerr << "Cyclic rotate check failed" << endl;
3561  exit(1);
3562  }
3563  }
3564 
3565  for (i = 0; i < bm::set_block_size; ++i)
3566  {
3567  blk0[i] = blk1[i] = unsigned(rand());
3568  }
3569 
3570  for (unsigned j = 0; j < bm::set_block_size * 32; ++j)
3571  {
3574 
3575  for (i = 0; i < bm::set_block_size; ++i)
3576  {
3577  if (blk0[i] != blk1[i])
3578  {
3579  cerr << "Stress Cyclic rotate check failed" << endl;
3580  exit(1);
3581  }
3582  }
3583  }
3584 
3585  // SHIFT-R tests
3586  //
3587 
3588  unsigned acc0, acc1;
3589 
3590  for (i = 0; i < bm::set_block_size; ++i)
3591  {
3592  blk0[i] = blk1[i] = 1;
3593  }
3594 
3595  bm::bit_block_shift_r1(blk0, &acc0, 0);
3596  bm::bit_block_shift_r1_unr(blk1, &acc1, 0);
3597 
3598  for (i = 0; i < bm::set_block_size; ++i)
3599  {
3600  if (blk0[i] != blk1[i])
3601  {
3602  cerr << "1. SHIFT-r check failed" << endl;
3603  assert(0); exit(1);
3604  }
3605  assert(blk0[i] == 2);
3606  }
3607 
3608 
3609  for (i = 0; i < bm::set_block_size; ++i)
3610  {
3611  blk0[i] = blk1[i] = (1u << 31);
3612  }
3613 
3614  bm::bit_block_shift_r1(blk0, &acc0, 0);
3615  bm::bit_block_shift_r1_unr(blk1, &acc1, 0);
3616 
3617  for (i = 0; i < bm::set_block_size; ++i)
3618  {
3619  if (blk0[i] != blk1[i])
3620  {
3621  cerr << "2. SHIFT-r check failed" << endl;
3622  exit(1);
3623  }
3624  }
3625 
3626  for (i = 0; i < bm::set_block_size; ++i)
3627  {
3628  blk0[i] = blk1[i] = unsigned(rand());
3629  }
3630 
3631  for (unsigned j = 0; j < bm::set_block_size * 32; ++j)
3632  {
3633  bm::bit_block_shift_r1(blk0, &acc0, 0);
3634  bm::bit_block_shift_r1_unr(blk1, &acc1, 0);
3635 
3636  assert(bool(acc0) == bool(acc1));
3637 
3638  for (i = 0; i < bm::set_block_size; ++i)
3639  {
3640  if (blk0[i] != blk1[i])
3641  {
3642  cerr << "Stress SHIFT-r check failed" << endl;
3643  exit(1);
3644  }
3645  }
3646  }
3647 
3648  cout << "---------------------------- ShiftRotate test OK" << endl;
3649 }
3650 
3651 static
3653 {
3654  cout << "---------------------------- BlockBitInsertTest test" << endl;
3655 
3658 
3659  unsigned i;
3660  for (i = 0; i < bm::set_block_size; ++i)
3661  blk0[i] = blk1[i] = 0;
3662 
3663  {
3664  bm::word_t co = bm::bit_block_insert(blk0, 0, 1);
3665  assert(co == 0);
3666  assert(blk0[0]==1u);
3667  co = bm::bit_block_insert(blk0, 0, 1);
3668  assert(co == 0);
3669  assert(blk0[0]==3u);
3670 
3671  blk0[bm::set_block_size-1] = ~0u;
3672  co = bm::bit_block_insert(blk0, 0, 0);
3673  assert(co == 1);
3674  assert(blk0[0]==(3u << 1));
3675  assert(blk0[bm::set_block_size-1] == (~0u << 1));
3676 
3677  blk0[0] = ~0u;
3678  co = bm::bit_block_insert(blk0, 1, 0);
3679  assert(co == 1);
3680  assert(blk0[0]==(~0u & ~(1u << 1)));
3681  assert(blk0[bm::set_block_size-1] == (~0u << 2));
3682 
3683  blk0[0] = ~0u;
3684  co = bm::bit_block_insert(blk0, 31, 0);
3685  assert(co == 1);
3686  assert(blk0[0]==(~0u >> 1));
3687  assert(blk0[1]==3);
3688  assert(blk0[bm::set_block_size-1] == (~0u << 3));
3689 
3690  blk0[0] = 0u;
3691  co = bm::bit_block_insert(blk0, 31, 1);
3692  assert(co == 1);
3693  assert(blk0[0]==(1u << 31));
3694  assert(blk0[1]==(3u << 1));
3695  assert(blk0[bm::set_block_size-1] == (~0u << 4));
3696  }
3697 
3698  cout << "bit-insert stress 0..." << endl;
3699  {
3700  for (i = 0; i < bm::set_block_size; ++i)
3701  blk0[i] = blk1[i] = 0;
3702 
3703  blk0[0] = 1;
3704  for (i = 0; i < 65536; ++i)
3705  {
3706  bm::word_t co = bm::bit_block_insert(blk0, i, 0);
3707  assert(co == 0 || i == 65535);
3708  if (i < 65535)
3709  {
3710  unsigned t = bm::test_bit(blk0, i+1);
3711  assert(t);
3712  }
3713  for (unsigned k = 0; k < 65536; ++k)
3714  {
3715  if (k != i+1)
3716  {
3717  unsigned t = bm::test_bit(blk0, k);
3718  assert(!t);
3719  }
3720  } // for k
3721  } // for i
3722  for (i = 0; i < bm::set_block_size; ++i)
3723  {
3724  if (blk0[i] != blk1[i])
3725  {
3726  cerr << "Stress insert(0) failed" << endl;
3727  exit(1);
3728  }
3729  }
3730  }
3731  cout << "OK" << endl;
3732 
3733  cout << "bit-insert stress 1..." << endl;
3734  {
3735  for (i = 0; i < bm::set_block_size; ++i)
3736  blk0[i] = ~0u;
3737 
3738  for (i = 0; i < 65536; ++i)
3739  {
3740  bm::word_t co = bm::bit_block_insert(blk0, i, 0);
3741  assert(co == 1 || i == 65535);
3742  for (unsigned k = 0; k < 65536; ++k)
3743  {
3744  unsigned t = bm::test_bit(blk0, k);
3745  if (k <= i)
3746  {
3747  assert(!t);
3748  }
3749  else
3750  {
3751  assert(t);
3752  }
3753  } // for k
3754  } // for i
3755  for (i = 0; i < bm::set_block_size; ++i)
3756  {
3757  if (blk0[i] != blk1[i])
3758  {
3759  cerr << "Stress insert(1) failed" << endl;
3760  exit(1);
3761  }
3762  }
3763  }
3764  cout << "OK" << endl;
3765 
3766 
3767  cout << "---------------------------- BlockBitInsertTest test OK" << endl;
3768 }
3769 
3770 
3771 static
3773 {
3774  cout << "---------------------------- BlockBitEraseTest test" << endl;
3777 
3778  bm::bit_block_set(blk0, 0);
3779  bm::bit_block_set(blk1, 0);
3780 
3781  {
3782  blk0[0] = 1;
3783  bm::bit_block_erase(blk0, 0, true);
3784  assert(blk0[0] == 0);
3785  assert(blk0[bm::set_block_size-1] == (1u << 31u));
3786 
3787  blk0[0] = ~0u;
3788  blk0[1] = 1u;
3789  blk0[bm::set_block_size-1] = (1u << 31u);
3790  bm::bit_block_erase(blk0, 0, false);
3791  assert(blk0[0] == ~0u);
3792  assert(blk0[1] == 0);
3793  assert(blk0[bm::set_block_size-1] == (1u << 30u));
3794 
3795  bm::bit_block_set(blk0, 0);
3796  blk0[1] = 15u; // ..01111
3797  bm::bit_block_erase(blk0, 31, false);
3798  assert(blk0[1] == 7u); // ..0111
3799  assert(blk0[0] == 1u << 31u);
3800 
3801  bm::bit_block_erase(blk0, 32, false);
3802  assert(blk0[1] == 3u); // ..011
3803 
3804  blk0[1] = 15u; // ..01111
3805  bm::bit_block_erase(blk0, 33, false);
3806  assert(blk0[1] == 7); //0b111
3807  }
3808 
3809  {
3810  bm::bit_block_set(blk0, ~0u);
3811  bm::word_t acc;
3812  bm::bit_block_shift_l1_unr(blk0, &acc, true);
3813  for (unsigned i = 0; i < bm::set_block_size; ++i)
3814  {
3815  assert(blk0[i] == ~0u);
3816  }
3817  bm::bit_block_erase(blk0, 0, false);
3818  for (unsigned i = 0; i < bm::set_block_size-1; ++i)
3819  {
3820  assert(blk0[i] == ~0u);
3821  }
3822  assert(blk0[bm::set_block_size-1] == ((~0u) >> 1) );
3823 
3824  bm::bit_block_shift_l1_unr(blk0, &acc, false);
3825  for (unsigned i = 0; i < bm::set_block_size-1; ++i)
3826  {
3827  assert(blk0[i] == ~0u);
3828  }
3829  assert(blk0[bm::set_block_size-1] == ((~0u) >> 2) );
3830 
3831  bm::bit_block_erase(blk0, 0, true);
3832  for (unsigned i = 0; i < bm::set_block_size-1; ++i)
3833  {
3834  assert(blk0[i] == ~0u);
3835  }
3836  assert(blk0[bm::set_block_size-1] == ((~0u >> 3) | (1u << 31)));
3837  }
3838 
3839  cout << "bit-insert-erase stress 0..." << endl;
3840  unsigned c = 0;
3841  {
3842  bm::bit_block_set(blk0, 0);
3843  bm::bit_block_set(blk1, 0);
3844 
3845  blk0[bm::set_block_size-1] = (1u << 31);
3846  for (unsigned i = 65535; i != 0; --i)
3847  {
3848  unsigned t = bm::test_bit(blk0, i);
3849  assert(t);
3850  bm::bit_block_erase(blk0, 0, false);
3851  unsigned cnt = bm::bit_block_count(blk0);
3852  {
3853  auto cnt2 = bm::bit_block_count(blk0, ~0ull);
3854  assert(cnt2 == cnt);
3855  auto d = calc_block_digest0(blk0);
3856  cnt2 = bm::bit_block_count(blk0, d);
3857  assert(cnt2 == cnt);
3858  }
3859 
3860  c += cnt;
3861  if (cnt != 1)
3862  {
3863  unsigned control = 0;
3864  for (unsigned k = 0; k < 65536; ++k)
3865  {
3866  t = bm::test_bit(blk0, k);
3867  control += t;
3868  }
3869 
3870  t = bm::test_bit(blk0, i-1);
3871  assert(t);
3872  cerr << t << " " << control << endl;
3873  cerr << "i=" << i << " cnt=" << cnt;
3874  cerr << " CNT==1 failed!" << endl;
3875  assert(cnt == 1);
3876  exit(1);
3877  }
3878  } // for i
3879 
3880  std::cout << c << endl;
3881 
3882  bm::bit_block_set(blk0, 0);
3883 
3884  {
3885  blk0[bm::set_block_size-1] = (1u << 31);
3886  unsigned j = 0;
3887  for (unsigned i = 65535; i != 0; --i, ++j)
3888  {
3889  unsigned t = bm::test_bit(blk0, i);
3890  assert(t);
3891  bm::bit_block_erase(blk0, j, false);
3892  if (i <= j)
3893  {
3894  t = bm::test_bit(blk0, j);
3895  assert(!t);
3896  t = bm::test_bit(blk0, i);
3897  assert(t);
3898 
3899  auto cnt = bm::bit_block_count(blk0);
3900  assert(cnt);
3901 
3902  bm::bit_block_erase(blk0, i, false);
3903  t = bm::test_bit(blk0, i);
3904  assert(!t);
3905  cnt = bm::bit_block_count(blk0);
3906  assert(!cnt);
3907  {
3908  auto cnt2 = bm::bit_block_count(blk0, ~0ull);
3909  assert(cnt == cnt2);
3910  auto d = calc_block_digest0(blk0);
3911  cnt2 = bm::bit_block_count(blk0, d);
3912  assert(cnt2 == cnt);
3913  }
3914  break;
3915  }
3916  auto cnt = bm::bit_block_count(blk0);
3917  assert(cnt == 1);
3918  } // for i
3919  }
3920 
3921  {
3922  bm::bit_block_set(blk0, 0);
3923  for (unsigned i = 0; i < 65535; ++i)
3924  {
3925  for(unsigned j = i; j < 65535; ++j)
3926  {
3927  bm::bit_block_set(blk0, 0);
3928  unsigned bitcount = j - i + 1;
3929  assert(i + bitcount < 65536);
3930  bm::or_bit_block(blk0, i, bitcount);
3931  if (bitcount == 1)
3932  {
3933  break;
3934  }
3935  unsigned bc = bm::bit_block_count(blk0);
3936  for (unsigned k = i + bitcount/2; k < i+bitcount; ++k)
3937  {
3938  auto t = bm::test_bit(blk0, k);
3939  assert(t);
3940 
3941  bm::bit_block_erase(blk0, k, false);
3942 
3943  unsigned bc2 = bm::bit_block_count(blk0);
3944  assert(bc2 == bc-1);
3945  {
3946  auto cnt2 = bm::bit_block_count(blk0, ~0ull);
3947  assert(cnt2 == bc2);
3948  auto d = calc_block_digest0(blk0);
3949  cnt2 = bm::bit_block_count(blk0, d);
3950  assert(cnt2 == bc2);
3951  }
3952  --bc;
3953  } // for k
3954 
3955  } // for j
3956  } // for i
3957  }
3958 
3959 
3960  }
3961  cout << "ok" << endl;
3962 
3963  cout << "---------------------------- BlockBitEraseTest test OK" << endl;
3964 }
3965 
3966 static
3968 {
3969  cout << "---------------------------- Empty bvector test" << endl;
3970 
3971  {
3972  bvect bv1;
3973  bvect bv2;
3974 
3975  bvect bv3(bv1 & bv2);
3976  bvect bv4 = (bv1 & bv2);
3977 
3978  std::vector< bvect > v;
3979  v.push_back(bvect());
3980  }
3981 
3982  {
3983  bvect bv1;
3984  bm::id_t cnt = bv1.count_range(0, 10);
3985  if (cnt)
3986  {
3987  cerr << "Failed count_range()" << endl;
3988  exit(1);
3989  }
3990  bool b = bv1.test(0);
3991  if (b)
3992  {
3993  cerr << "Failed test" << endl;
3994  exit(1);
3995  }
3996 
3997  b = bv1.any();
3998  if (b)
3999  {
4000  cerr << "Failed any" << endl;
4001  exit(1);
4002  }
4003 
4004  bv1.set_bit(0);
4005  if (!bv1.any())
4006  {
4007  cerr << "Failed set_bit" << endl;
4008  exit(1);
4009  }
4010  }
4011  {
4012  bvect bv1;
4013  bool b = bv1.set_bit_and(0, false);
4014  if (bv1.any() || b)
4015  {
4016  cerr << "Failed set_bit" << endl;
4017  exit(1);
4018  }
4019  }
4020  {
4021  bvect bv1;
4022  bv1.set_range(0, 1, false);
4023  if (bv1.any())
4024  {
4025  cerr << "Failed set_range" << endl;
4026  exit(1);
4027  }
4028  bv1.set_range(0, 1, true);
4029  if (bv1.count()!=2)
4030  {
4031  cerr << "Failed set_range(0,1)" << endl;
4032  exit(1);
4033  }
4034  }
4035  {
4036  bvect bv1;
4037  bv1.clear_bit(0);
4038  if (bv1.any())
4039  {
4040  cerr << "Failed clear_bit" << endl;
4041  exit(1);
4042  }
4043  }
4044  {
4045  bvect bv1;
4046  bv1.clear();
4047  if (bv1.any())
4048  {
4049  cerr << "Failed clear()" << endl;
4050  exit(1);
4051  }
4052  }
4053  {
4054  bvect bv1;
4055  bv1.invert();
4056  if (!bv1.any())
4057  {
4058  cerr << "Failed invert()" << endl;
4059  exit(1);
4060  }
4061  }
4062  {
4063  bvect bv1;
4064  bvect bv2;
4065  bv1.swap(bv2);
4066  if (bv1.any() || bv2.any())
4067  {
4068  cerr << "Failed swap()" << endl;
4069  exit(1);
4070  }
4071  }
4072  {
4073  bvect bv1;
4074  if (bv1.get_first() != 0)
4075  {
4076  cerr << "Failed get_first()" << endl;
4077  exit(1);
4078  }
4079  }
4080  {
4081  bvect bv1;
4082  if (bv1.extract_next(0) != 0)
4083  {
4084  cerr << "Failed extract_next()" << endl;
4085  exit(1);
4086  }
4087  }
4088  {
4089  bvect bv1;
4090  bvect::statistics st;
4091  bv1.calc_stat(&st);
4092  if (st.memory_used == 0)
4093  {
4094  cerr << "Failed calc_stat()" << endl;
4095  exit(1);
4096  }
4097  }
4098  {
4099  bvect bv1;
4100  bvect bv2;
4101  bvect bv3;
4102  bv1.bit_or(bv2);
4103  if (bv1.any())
4104  {
4105  cerr << "Failed bit_or()" << endl;
4106  exit(1);
4107  }
4108  bv2.set_bit(100000000);
4109  bv1.bit_or(bv2);
4110  if (!bv1.any())
4111  {
4112  cerr << "Failed bit_or()" << endl;
4113  exit(1);
4114  }
4115  bv1.bit_or(bv3);
4116  if (bv1.count()!=1)
4117  {
4118  cerr << "Failed bit_or()" << endl;
4119  exit(1);
4120  }
4121  }
4122 
4123  {
4124  bvect bv1;
4125  bvect bv2;
4126  bv1.bit_and(bv2);
4127  if (bv1.any())
4128  {
4129  cerr << "Failed bit_and()" << endl;
4130  exit(1);
4131  }
4132  bv2.set_bit(100000000);
4133  bv1.bit_and(bv2);
4134  if (bv1.any())
4135  {
4136  cerr << "Failed bit_and()" << endl;
4137  exit(1);
4138  }
4139  bv2.bit_and(bv1);
4140  if (bv2.count()!=0)
4141  {
4142  cerr << "Failed bit_and()" << endl;
4143  exit(1);
4144  }
4145  }
4146 
4147  {
4148  bvect bv1;
4149  bvect::statistics st1;
4150  bv1.optimize(0, bvect::opt_compress, &st1);
4151  if (st1.memory_used == 0)
4152  {
4153  cerr << "Failed calc_stat()" << endl;
4154  exit(1);
4155  }
4156  bv1.optimize_gap_size();
4157  }
4158 
4159  {
4160  bvect bv1;
4161  bvect bv2;
4162 
4163  int r = bv1.compare(bv2);
4164  if (r != 0)
4165  {
4166  cerr << "Failed compare()" << endl;
4167  exit(1);
4168 
4169  }
4170  bv2.set_bit(1000);
4171  r = bv1.compare(bv2);
4172  if (r == 0)
4173  {
4174  cerr << "Failed compare()" << endl;
4175  exit(1);
4176 
4177  }
4178  r = bv2.compare(bv1);
4179  if (r == 0)
4180  {
4181  cerr << "Failed compare()" << endl;
4182  exit(1);
4183  }
4184  }
4185 
4186  {
4187  bvect bv1;
4188  bvect::enumerator en1 = bv1.first();
4189  bvect::enumerator en2 = bv1.get_enumerator(65535);
4190  CompareEnumerators(en1, en2);
4191  if (en1.valid() || en2.valid())
4192  {
4193  cerr << "failed first enumerator" << endl;
4194  exit(1);
4195  }
4196  }
4197 
4198  {
4199  bvect bv1;
4200  bv1.resize(0);
4201 
4202  bv1.invert();
4203  assert(!bv1.any());
4204  assert(bv1.size()==0);
4205  }
4206 
4207  cout << "---------------------------- Empty bvector test OK" << endl;
4208 
4209 }
4210 
4211 
4212 
4213 
4214 static
4216 {
4217  cout << "---------------------------- Basic functinality test" << endl;
4218 
4220 
4221  bvect_mini bvect_min(BITVECT_SIZE);
4222  bvect bvect_full;
4223  bvect bvect_full1;
4224  bvect::rs_index_type bc_arr;
4225  bvect::rs_index_type bc_arr1;
4226 
4227  printf("\nBasic functionality test.\n");
4228 
4229  {
4230  bvect::rs_index_type rs_idx;
4231  for (unsigned i = 0; i < ITERATIONS; ++i)
4232  {
4233  rs_idx.resize(i);
4234  }
4235  }
4236 
4237 
4238  // clear_range with memory de-allocation
4239  {
4240  bvect::statistics st;
4241  bvect bv { 65536, 65538};
4242  bv.calc_stat(&st);
4243  assert(st.bit_blocks == 1);
4244 
4245  bv.clear_range(65536, 65536+65535);
4246  bv.calc_stat(&st);
4247  assert(st.bit_blocks == 0);
4248 
4249  }
4250 
4251 
4252  {
4253  bvect::statistics st;
4254  bvect bv;
4255  bv.init(256, false/* NOT allocate secondary structures*/);
4256  bv.calc_stat(&st);
4257  assert(st.ptr_sub_blocks == 0);
4258  }
4259  {
4260  bvect::statistics st;
4261  bvect bv;
4262  bv.init(256, true/*allocate secondary structures*/);
4263  bv.calc_stat(&st);
4264  assert(st.ptr_sub_blocks == 256);
4265  }
4266 
4267  // filling vectors with regular values
4268 
4269  cout << "test data generation... " << endl;
4270  unsigned i;
4271  for (i = 0; i < ITERATIONS; ++i)
4272  {
4273  bvect_min.set_bit(i);
4274  bvect_full.set_bit(i);
4275 
4276  bvect_full.build_rs_index(&bc_arr);
4277 
4278  bm::id_t pos1, pos2, pos3, pos4, pos5;
4279  auto rf1 = FindRank(bvect_full, i+1, 0, pos1);
4280  auto rf2 = bvect_full.find_rank(i+1, 0, pos2);
4281  auto rf3 = bvect_full.find_rank(i+1, 0, pos3);
4282  auto rf4 = bvect_full.select(i+1, pos4, bc_arr);
4283 
4284  assert(rf1);
4285  assert(rf2);
4286  assert(rf3);
4287  assert(rf4);
4288  if (pos1 != pos2 || pos1 != pos3 || pos1 != pos4)
4289  {
4290  rf2 = bvect_full.find_rank(i+1, 0, pos2);
4291  cerr << "1.Rank check error!\n"
4292  << " pos1 = " << pos1
4293  << " pos2 = " << pos2
4294  << " pos3 = " << pos3
4295  << " pos4 = " << pos4
4296  << endl;
4297  ;
4298  exit(1);
4299  }
4300 
4301  {
4302  bvect bv_opt(bvect_full);
4303  bv_opt.optimize();
4304  bv_opt.build_rs_index(&bc_arr);
4305  auto rf5 = bv_opt.select(i+1, pos5, bc_arr);
4306  assert(rf5);
4307  assert(pos1 == pos5);
4308  }
4309 
4310  }
4311 
4312  bvect_full1.set_range(0, ITERATIONS-1);
4313 
4314  bvect_full1.build_rs_index(&bc_arr1);
4315 
4316  cout << "Rank check 2" << endl;
4317 
4318  for (i = 0; i < ITERATIONS; ++i)
4319  {
4320  bm::id_t pos1, pos2, pos3, pos4;
4321  auto rf1 = FindRank(bvect_full1, i+1, 0, pos1);
4322  auto rf2 = bvect_full1.find_rank(i+1, 0, pos2);
4323  auto rf3 = bvect_full1.find_rank(i+1, 0, pos3);
4324  auto rf4 = bvect_full1.select(i+1, pos4, bc_arr1);
4325  assert(rf1);
4326  assert(rf2);
4327  assert(rf3);
4328  assert(rf4);
4329  if (pos1 != pos2 || pos1 != pos3 || pos1 != pos4)
4330  {
4331  rf2 = bvect_full1.find_rank(i+1, 0, pos2);
4332  cerr << "2.Rank check error!\n"
4333  << " pos1 = " << pos1
4334  << " pos2 = " << pos2
4335  << " pos3 = " << pos3
4336  << " pos4 = " << pos4
4337  << endl;
4338  ;
4339  exit(1);
4340  }
4341  if (i % 1000 == 0)
4342  {
4343  if (!is_silent)
4344  cout << "\r" << i << " / " << ITERATIONS << flush;
4345  }
4346  }
4347  cout << endl;
4348 
4349  bvect::rs_index_type rs_idx_full;
4350  bvect_full.build_rs_index(&rs_idx_full);
4351 
4352  bvect::rs_index_type rs_idx_full1;
4353  bvect_full1.build_rs_index(&rs_idx_full1);
4354 
4355  CheckCountRange(bvect_full, rs_idx_full, 0, ITERATIONS);
4356  CheckCountRange(bvect_full, rs_idx_full, 10, ITERATIONS+10);
4357  CheckCountRange(bvect_full1, rs_idx_full1, 0, ITERATIONS);
4358  CheckCountRange(bvect_full1, rs_idx_full1, ITERATIONS-10, ITERATIONS+10);
4359  CheckCountRange(bvect_full1, rs_idx_full1, 10, ITERATIONS+10);
4360 
4361  if (bvect_full1 != bvect_full)
4362  {
4363  cout << "set_range failed!" << endl;
4364  print_stat(cout,bvect_full1);
4365  exit(1);
4366  }
4367 
4368  print_stat(cout,bvect_full);
4369  print_stat(cout,bvect_full1);
4370 
4371  // checking the results
4372  unsigned count_min = 0;
4373 
4374  for (i = 0; i < ITERATIONS; ++i)
4375  {
4376  if (bvect_min.is_bit_true(i))
4377  ++count_min;
4378  }
4379 
4380  cout << "Rank check 3" << endl;
4381  for (i = 0; i < ITERATIONS; ++i)
4382  {
4383  CheckCountRange(bvect_full, rs_idx_full, i, ITERATIONS);
4384  CheckCountRange(bvect_full1, rs_idx_full1, i, ITERATIONS);
4385  }
4386 
4387  cout << "Rank check 4" << endl;
4388  for (i = ITERATIONS; i > 0; --i)
4389  {
4390  CheckCountRange(bvect_full, rs_idx_full, 0, i);
4391  CheckCountRange(bvect_full1, rs_idx_full1, 0, i);
4392  }
4393 
4394  unsigned count_full = bvect_full.count();
4395 
4396  if (count_min == count_full)
4397  {
4398  printf("simple count test ok.\n");
4399  }
4400  else
4401  {
4402  printf("simple count test failed count_min = %i count_full = %i\n",
4403  count_min, count_full);
4404  exit(1);
4405  }
4406 
4407 
4408  // detailed vectors verification
4409 
4410  CheckVectors(bvect_min, bvect_full, ITERATIONS);
4411 
4412  // now clearning
4413 
4414  for (i = 0; i < ITERATIONS; i+=2)
4415  {
4416  bvect_min.clear_bit(i);
4417  bvect_full.clear_bit(i);
4418  bvect_full1.set_range(i, i, false);
4419  }
4420 
4421  CheckVectors(bvect_min, bvect_full, ITERATIONS);
4422  CheckVectors(bvect_min, bvect_full1, ITERATIONS);
4423 
4424  for (i = 0; i < ITERATIONS; ++i)
4425  {
4426  bvect_min.clear_bit(i);
4427  }
4428  bvect_full.clear();
4429 
4430  CheckVectors(bvect_min, bvect_full, ITERATIONS);
4431 
4432  cout << "Random step filling" << endl;
4433 
4434  for (i = (unsigned)rand()%10; i < ITERATIONS; i+=(unsigned)rand()%10)
4435  {
4436  bvect_min.clear_bit(i);
4437  bvect_full.clear_bit(i);
4438  }
4439 
4440  CheckVectors(bvect_min, bvect_full, ITERATIONS);
4441 
4442  bvect bv1;
4443  bvect bv2;
4444 
4445  bv1[10] = true;
4446  bv1[1000] = true;
4447 
4448  bv2[200] = bv2[700] = bv2[500] = true;
4449 
4450  bv1.swap(bv2);
4451 
4452  if (bv1.count() != 3)
4453  {
4454  cout << "Swap test failed!" << endl;
4455  exit(1);
4456  }
4457 
4458  if (bv2.count() != 2)
4459  {
4460  cout << "Swap test failed!" << endl;
4461  exit(1);
4462  }
4463 
4464  {
4465  //bm::standard_alloc_pool pool;
4467  bvect bv3, bv4;
4468  bv3.set_allocator_pool(&pool);
4469  bv3.set(10, true);
4470  bv4.set(10, true);
4471  bv4.set(10, false);
4472  bv3 &= bv4;
4473  }
4474  {
4475  bvect bv(100);
4476  bv.set_range(150, 151);
4477  assert(bv.size() == 152);
4478  bv.set_bit(160);
4479  assert(bv.size() == 161);
4480  }
4481 }
4482 
4483 
4484 static
4485 void generate_test_vectors(std::vector<bm::id_t> &v1,
4486  std::vector<bm::id_t> &v2,
4487  std::vector<bm::id_t> &v3,
4488  unsigned vector_max)
4489 {
4490  bm::id_t j;
4491  for (j = 0; j < vector_max; j += 2)
4492  v1.push_back(j);
4493  for (j = 0; j < vector_max; j += 5)
4494  v2.push_back(j);
4495  for (j = 0; j < vector_max; j += 120)
4496  v3.push_back(j);
4497 }
4498 
4499 
4500 static
4502 {
4503  cout << "---------------------------- Bvector BULK set test" << endl;
4504 
4505 
4506  {
4507  unsigned ids[] = { 0 };
4508 
4509  bvect bv1, bv2, bv3;
4510  {
4512  for (unsigned i = 0; i < sizeof(ids)/sizeof(ids[0]); ++i)
4513  {
4514  bv1.set(ids[i]);
4515  iit = ids[i];
4516  }
4517  }
4518  bv2.set(&ids[0], sizeof(ids)/sizeof(ids[0]));
4519 
4520  int cmp = bv1.compare(bv2);
4521  assert(cmp==0);
4522  cmp = bv1.compare(bv3);
4523  assert(cmp==0);
4524 
4525  bv2.set(0);
4526  bv2.keep(&ids[0], sizeof(ids)/sizeof(ids[0]));
4527  assert(bv2.count()==1);
4528  assert(bv2.test(0));
4529  }
4530 
4531  {
4532  unsigned ids[] = {65535, bm::id_max };
4533  unsigned cnt;
4534  bvect bv2;
4535  bv2.set(&ids[0], sizeof(ids)/sizeof(ids[0]));
4536 
4537  cnt = bv2.count();
4538  cout << cnt << endl;
4539 
4540  assert(cnt == 1);
4541  assert(bv2.test(ids[0]));
4542  }
4543 
4544  // set bits in FULL vector
4545  {
4546  unsigned ids[] = {0, 10, 65535, bm::id_max-1, bm::id_max };
4547  unsigned cnt;
4548  bvect bv2;
4549  bv2.invert();
4550  struct bvect::statistics st1, st2;
4551  bv2.calc_stat(&st1);
4552 
4553  bv2.set(&ids[0], sizeof(ids)/sizeof(ids[0]));
4554 
4555  bv2.calc_stat(&st2);
4556  assert(st1.bit_blocks == st2.bit_blocks);
4557  assert(st1.gap_blocks == st2.gap_blocks);
4558 
4559  cnt = bv2.count();
4560  cout << cnt << endl;
4561 
4562  assert(cnt == bm::id_max);
4563  }
4564 
4565  // test correct sizing
4566  {
4567  unsigned ids[] = {65536 };
4568  bvect bv1;
4569  bv1.resize(10);
4570 
4571  bv1.set(&ids[0], sizeof(ids)/sizeof(ids[0]));
4572  assert(bv1.size()==65536+1);
4573  bv1.keep(&ids[0], sizeof(ids)/sizeof(ids[0]));
4574  cout << bv1.size() << endl;
4575  assert(bv1.size()==65536+1);
4576  }
4577 
4578  {
4579  unsigned ids[] = {65536, 1280000, 65535 };
4580  bvect bv1, bv2;
4581 
4582  for (unsigned i = 0; i < sizeof(ids)/sizeof(ids[0]); ++i)
4583  bv1.set(ids[i]);
4584 
4585  bv2.set(&ids[0], sizeof(ids)/sizeof(ids[0]));
4586  int cmp = bv1.compare(bv2);
4587  assert(cmp==0);
4588 
4589  bv2.keep(&ids[0], sizeof(ids)/sizeof(ids[0]));
4590  cmp = bv1.compare(bv2);
4591  assert(cmp==0);
4592  }
4593 
4594 
4595  {
4596  unsigned ids[] = { 0, 1, 2, 3, 4, 5, 256, 1024, 1028, 256000 };
4597 
4598  bvect bv1, bv2;
4599  for (unsigned i = 0; i < sizeof(ids)/sizeof(ids[0]); ++i)
4600  bv1.set(ids[i]);
4601  bv2.set(&ids[0], sizeof(ids)/sizeof(ids[0]));
4602 
4603  DetailedCompareBVectors(bv1, bv2);
4604 
4605  int cmp = bv1.compare(bv2);
4606  assert(cmp==0);
4607 
4608  {
4609  bvect bv_inv;
4610  bv_inv.flip();
4611  unsigned keep_cnt = sizeof(ids)/sizeof(ids[0]);
4612  bv_inv.keep(&ids[0], keep_cnt, bm::BM_SORTED);
4613  unsigned cnt_inv2 = bv_inv.count();
4614  assert(cnt_inv2 == keep_cnt);
4615  for (unsigned i = 0; i < sizeof(ids)/sizeof(ids[0]); ++i)
4616  {
4617  assert(bv_inv.test(ids[i]));
4618  }
4619  }
4620 
4621  bvect bv3, bv4, bv5;
4622  bv3.invert();
4623  bv4.invert();
4624  bv5.invert();
4625 
4626  {
4628  for (unsigned i = 0; i < sizeof(ids)/sizeof(ids[0]); ++i)
4629  {
4630  bv3.set(ids[i]);
4631  iit = ids[i];
4632  }
4633  }
4634  bv4.set(&ids[0], sizeof(ids)/sizeof(ids[0]));
4635  cmp = bv3.compare(bv4);
4636  assert(cmp==0);
4637  cmp = bv4.compare(bv3);
4638  assert(cmp==0);
4639 
4640  cmp = bv3.compare(bv5);
4641  assert(cmp==0);
4642  }
4643 
4644  {
4645  unsigned vector_max = 4000000;
4646  std::vector<bm::id_t> v1, v2, v3;
4647  generate_test_vectors(v1, v2, v3, vector_max);
4648 
4649  for (unsigned k = 0; k < 2; ++ k)
4650  {
4651  bvect bvu, bvuc;
4652  bvect bv1, bv2, bv3, bv11;
4653  bvect bv1c, bv2c, bv3c;
4654 
4655  {
4656  bvect::bulk_insert_iterator iit(bv11);
4657  for (unsigned i = 0; i < v1.size(); ++i)
4658  {
4659  bv1c.set(v1[i]);
4660  iit = v1[i];
4661  }
4662  iit.flush();
4663  }
4664 
4665  for (unsigned i = 0; i < v2.size(); ++i)
4666  bv2c.set(v2[i]);
4667  for (unsigned i = 0; i < v3.size(); ++i)
4668  bv3c.set(v3[i]);
4669 
4670  // union of 3 vectors
4671  bvuc = bv1c;
4672  bvuc |= bv2c;
4673  bvuc |= bv3c;
4674 
4675  bv1.set(&v1[0], unsigned(v1.size()));
4676  bv2.set(&v2[0], unsigned(v2.size()));
4677  bv3.set(&v3[0], unsigned(v3.size()));
4678 
4679  // imported union of 3 vectors
4680  bvu.set(&v1[0], unsigned(v1.size()));
4681  bvu.set(&v2[0], unsigned(v2.size()));
4682  bvu.set(&v3[0], unsigned(v3.size()));
4683 
4684  cout << bv1.count() << " " << bv1c.count() << endl;
4685 
4686  int cmp;
4687  cmp = bv1c.compare(bv1);
4688  if (cmp != 0)
4689  {
4690  DetailedCompareBVectors(bv1, bv1c);
4691  }
4692  assert(cmp==0);
4693  cmp = bv2c.compare(bv2);
4694  assert(cmp==0);
4695  cmp = bv3c.compare(bv3);
4696  assert(cmp==0);
4697  cmp = bv1.compare(bv11);
4698  assert(cmp==0);
4699 
4700  cmp = bvuc.compare(bvu);
4701  assert(cmp == 0);
4702 
4703  {
4704  std::random_device rd;
4705  std::mt19937 g(rd());
4706 
4707  std::shuffle(v1.begin(), v1.end(), g);
4708  std::shuffle(v2.begin(), v2.end(), g);
4709  std::shuffle(v3.begin(), v3.end(), g);
4710  }
4711  }
4712 
4713 
4714  }
4715 
4716  cout << "Bulk bvector<>::set() stress.." << endl;
4717  {
4718  unsigned vector_max =40000000;
4719  unsigned delta_max = 65537;
4720 
4721  bvect bv1, bv2;
4722  bvect bv1c;
4723  std::vector<bm::id_t> v1;
4724 
4725  for (unsigned delta = 1; delta < delta_max; ++delta)
4726  {
4727  v1.resize(0);
4728  bvect::bulk_insert_iterator iit(bv2);
4729  for (unsigned i = 0; i < vector_max; i+=delta)
4730  {
4731  v1.push_back(i);
4732  iit = i;
4733  }
4734  iit.flush();
4735 
4736  bv1.set(&v1[0], unsigned(v1.size()));
4737  bm::combine_or(bv1c, v1.begin(), v1.end());
4738 
4739  int cmp = bv1.compare(bv1c);
4740  if (cmp!=0)
4741  {
4742  cerr << "1.Failed bulk set test at delta=" << delta << endl;
4743  exit(1);
4744  }
4745  cmp = bv1.compare(bv2);
4746  if (cmp!=0)
4747  {
4748  cerr << "2.Failed bulk set test at delta=" << delta << endl;
4749  exit(1);
4750  }
4751 
4752  // test AND/keep
4753  {
4754  bvect bv3(bv1);
4755  bvect bv4;
4756  bv3.keep(&v1[0], unsigned(v1.size()));
4757  bv4.set(&v1[0], unsigned(v1.size()));
4758  cmp = bv3.compare(bv4);
4759  if (cmp!=0)
4760  {
4761  cerr << "3.Failed keep() test at delta=" << delta << endl;
4762  exit(1);
4763  }
4764  if (v1.size())
4765  {
4766  bv3.keep(&v1[0], 1);
4767  assert(bv3.count()==1);
4768  assert(bv3.test(v1[0]));
4769  }
4770  }
4771 
4772  bv1.clear();
4773  bv1c.clear();
4774  bv2.clear();
4775 
4776  if (delta % 256 == 0)
4777  {
4778  if (!is_silent)
4779  cout << "\r" << delta << "/" << delta_max << flush;
4780  }
4781 
4782  } // for delta
4783  cout << endl;
4784  }
4785 
4786 
4787  cout << "---------------------------- Bvector BULK set test OK\n" << endl;
4788 }
4789 
4790 
4791 
4792 static
4794 {
4795  cout << "---------------------------- Find Rank test" << endl;
4796 
4797  {
4798  bvect bv1;
4799  bv1[30] = true;
4800  bv1[65534] = true;
4801 
4802  bvect::rs_index_type bc_arr1;
4803  bv1.build_rs_index(&bc_arr1);
4804 
4805  bool rf1, rf2, rf3;
4806  bm::id_t pos, pos1;
4807  rf1 = bv1.find_rank(1, 20, pos);
4808  rf3 = bv1.find_rank(1, 20, pos1, bc_arr1);
4809  assert(rf1);
4810  assert(rf3);
4811  assert(pos == 30);
4812  assert(pos1 == 30);
4813 
4814  rf2 = bv1.find_rank(2, 30, pos);
4815  rf3 = bv1.find_rank(2, 30, pos1, bc_arr1);
4816  assert(rf2);
4817  assert(rf3);
4818  assert(pos == 65534);
4819  assert(pos1 == 65534);
4820  }
4821 
4822 
4823  cout << "Test bvector<>::select()" << endl;
4824  {
4825  bvect::size_type r(65536*256-1), pos;
4826 
4827  bvect bv;
4828  bv.set_range(0, r);
4829  bv.optimize();
4830 
4831  bvect::rs_index_type rs_idx;
4832  bv.build_rs_index(&rs_idx);
4833 
4834  struct bvect::statistics st;
4835  bv.calc_stat(&st);
4836  assert(st.bit_blocks == 0);
4837  assert(st.gap_blocks == 0);
4838 
4840  for (i = 0; i <=r; ++i)
4841  {
4842  bvect::size_type ri = bv.rank(i, rs_idx);
4843  assert(ri == (i+1));
4844  bool f = bv.select(ri, pos, rs_idx);
4845  assert(f);
4846  assert(pos == i);
4847  }
4848  bool f = bv.select(i+1, pos, rs_idx);
4849  assert(!f);
4850  }
4851 
4852  cout << "Find Rank test stress 1\n" << endl;
4853 
4854  {
4855  const unsigned max_size = 2000000;
4856  bvect bv1;
4857  for (unsigned i = 0; i < max_size;)
4858  {
4859  bv1.set(i);
4860  i += (unsigned)rand()%5;
4861  }
4862  bvect::rs_index_type bc_arr1;
4863  bv1.build_rs_index(&bc_arr1);
4864 
4865 
4866  for (unsigned i = 0; i < max_size; ++i)
4867  {
4868  bool rf1, rf3;
4869  bm::id_t pos, pos1;
4870 
4871  rf1 = bv1.find_rank(0, i, pos);
4872  rf3 = bv1.find_rank(0, i, pos1, bc_arr1);
4873  assert (rf1 == rf3);
4874  if (rf1)
4875  {
4876  if (pos != pos1)
4877  {
4878  cerr << "Rank cmp test failed! i=" << i
4879  << " pos=" << pos
4880  << " pos1=" << pos1
4881  << endl;
4882  exit(1);
4883  }
4884  }
4885 
4886  rf1 = bv1.find_rank(i, max_size-i, pos);
4887  rf3 = bv1.find_rank(i, max_size-i, pos1, bc_arr1);
4888  assert (rf1 == rf3);
4889  if (rf1)
4890  {
4891  if (pos != pos1)
4892  {
4893  cerr << "Rank cmp test failed! i=" << i
4894  << " pos=" << pos
4895  << " pos1=" << pos1
4896  << endl;
4897  exit(1);
4898  }
4899  }
4900  if (i % 100 == 0)
4901  if (!is_silent)
4902  cout << "\r" << i << "/" << max_size << flush;
4903  } // for
4904  cout << endl;
4905  }
4906 
4907  cout << "---------------------------- Find Rank test OK" << endl;
4908 }
4909 
4910 static
4912 {
4913  cout << "---------------------------- Bvector inc test" << endl;
4914 
4915  {
4916  bvect bv1;
4917  bool b;
4918 
4919  b = bv1.inc(0);
4920  assert(!b);
4921  b = bv1.inc(0);
4922  assert(b);
4923 
4924  b = bv1.inc(10);
4925  assert(!b);
4926  b = bv1.inc(10);
4927  assert(b);
4928  }
4929 
4930  {
4931  bvect bv1(BM_GAP);
4932  bool b;
4933 
4934  assert(bv1.count()==0);
4935 
4936  b = bv1.inc(0);
4937  assert(!b);
4938  cout << bv1.count() << endl;
4939  assert(bv1.count()==1);
4940  b = bv1.inc(0);
4941  assert(b);
4942  assert(bv1.count()==0);
4943 
4944  b = bv1.inc(10);
4945  assert(!b);
4946  b = bv1.inc(10);
4947  assert(b);
4948  }
4949 
4950  {
4951  bvect bv1(BM_GAP);
4952  bool b;
4953 
4954  bv1.flip();
4955 
4956  b = bv1.inc(0);
4957  assert(b);
4958  b = bv1.inc(0);
4959  assert(!b);
4960 
4961  b = bv1.inc(10);
4962  assert(b);
4963  b = bv1.inc(10);
4964  assert(!b);
4965  }
4966 
4967  cout << "---------------------------- Bvector inc test OK" << endl;
4968 }
4969 
4970 // -----------------------------------------------------------------------
4971 
4972 static
4973 void optimize_fill(bvect& bv, bvect::size_type base, unsigned inc,
4975  bool value = true)
4976 {
4977  for (bvect::size_type i = 0; i < bm::set_sub_array_size; ++i)
4978  {
4979  bvect::size_type base_idx = i * bm::gap_max_bits;
4980  for (bvect::size_type j = base; j < max_bits; j += inc)
4981  {
4982  bv.set(base_idx + j, value);
4983  } // for j
4984  } // for i
4985 }
4986 
4987 static
4989 {
4990  cout << "---------------------------- Bvector Optimize test" << endl;
4992 
4993  {
4994  bvect bv;
4995  optimize_fill(bv, 0, 1, bm::gap_max_bits, true);
4996 
4997  bvect::statistics st1;
4998  bv.calc_stat(&st1);
4999 
5001  assert(st1.gap_blocks == 0);
5002  assert(st1.ptr_sub_blocks == 1);
5003 
5004  bv.optimize(tb, bvect::opt_compress, &st1);
5005 
5006  assert(st1.bit_blocks == 0);
5007  assert(st1.gap_blocks == 0);
5008  assert(st1.ptr_sub_blocks == 0);
5009 
5010  bv.calc_stat(&st1);
5011 
5012  assert(st1.bit_blocks == 0);
5013  assert(st1.gap_blocks == 0);
5014  assert(st1.ptr_sub_blocks == 0);
5015  }
5016 
5017  {
5018  bvect::statistics st;
5019  bvect bv;
5020  bv.set(10);
5021  bv.set(65536);
5022  bv.optimize_range(0, 0, tb, bvect::opt_compress);
5023  bv.calc_stat(&st);
5024  assert(st.bit_blocks == 1);
5025  assert(st.gap_blocks == 1);
5027  bv.calc_stat(&st);
5028  assert(st.bit_blocks == 0);
5029  assert(st.gap_blocks == 2);
5030  }
5031 
5032 
5033  {
5034  bvect::statistics st;
5035 
5036  bvect bv;
5037  bv.optimize_range(0, 0, tb, bvect::opt_compress);
5038  bv.invert();
5039 
5040  bv.optimize_range(0, 65536, tb, bvect::opt_compress);
5041  bv.calc_stat(&st);
5042  assert(st.bit_blocks == 1);
5043  assert(st.gap_blocks == 0);
5044 
5045  bv.optimize_range(65536, bm::id_max-1, tb, bvect::opt_compress);
5046  bv.calc_stat(&st);
5047  assert(st.bit_blocks == 0);
5048  assert(st.gap_blocks == 1);
5049 
5050  }
5051 
5052 
5053  {
5054  bvect bv;
5055  optimize_fill(bv, 0, 100, bm::gap_max_bits, true);
5056  optimize_fill(bv, 0, 100, bm::gap_max_bits, false);
5057 
5058  bvect::statistics st1;
5059  bv.calc_stat(&st1);
5060 
5062  assert(st1.gap_blocks == 0);
5063  assert(st1.ptr_sub_blocks == 1);
5064 
5065  bv.optimize(tb, bvect::opt_compress, &st1);
5066 
5067  assert(st1.bit_blocks == 0);
5068  assert(st1.gap_blocks == 0);
5069  assert(st1.ptr_sub_blocks == 0);
5070  }
5071 
5072 
5073  {
5074  bvect bv(BM_GAP);
5075  optimize_fill(bv, 0, 1, bm::gap_max_bits, true);
5076 
5077  bvect::statistics st1;
5078  bv.calc_stat(&st1);
5079 
5080  assert(st1.bit_blocks == 0);
5082  assert(st1.ptr_sub_blocks == 1);
5083 
5084  bv.optimize(tb, bvect::opt_compress, &st1);
5085 
5086  assert(st1.bit_blocks == 0);
5087  assert(st1.gap_blocks == 0);
5088  assert(st1.ptr_sub_blocks == 0);
5089 
5090  bv.calc_stat(&st1);
5091 
5092  assert(st1.bit_blocks == 0);
5093  assert(st1.gap_blocks == 0);
5094  assert(st1.ptr_sub_blocks == 0);
5095  }
5096 
5097  {
5098  bvect bv(BM_GAP);
5099  optimize_fill(bv, 0, 1000, bm::gap_max_bits, true);
5100  optimize_fill(bv, 0, 1000, bm::gap_max_bits, false);
5101 
5102  bvect::statistics st1;
5103  bv.calc_stat(&st1);
5104 
5105  assert(st1.bit_blocks == 0);
5107  assert(st1.gaps_by_level[0] == 0);
5109  assert(st1.ptr_sub_blocks == 1);
5110 
5111  bv.optimize(tb, bvect::opt_compress, &st1);
5112 
5113  assert(st1.bit_blocks == 0);
5114  assert(st1.gap_blocks == 0);
5115  assert(st1.ptr_sub_blocks == 0);
5116  }
5117 
5118  {
5119  bvect bv(BM_GAP);
5120  optimize_fill(bv, 0, 1000, bm::gap_max_bits, true);
5121  optimize_fill(bv, 1, 1, bm::gap_max_bits, false);
5122 
5123  bvect::statistics st1;
5124  bv.calc_stat(&st1);
5125 
5126  assert(st1.bit_blocks == 0);
5128  assert(st1.gaps_by_level[0] == 0);
5130  assert(st1.ptr_sub_blocks == 1);
5131 
5132  bv.optimize(tb, bvect::opt_compress, &st1);
5133 
5134  assert(st1.bit_blocks == 0);
5136  assert(st1.ptr_sub_blocks == 1);
5138  assert(st1.gaps_by_level[1] == 0);
5139  }
5140 
5141 
5142  cout << "---------------------------- Bvector Optimize test OK" << endl;
5143 }
5144 
5145 
5146 // -----------------------------------------------------------------------
5147 
5148 static
5150  unsigned min = 0,
5151  unsigned max = 40000000,
5152  unsigned fill_factor = 65536)
5153 {
5155  unsigned ff = fill_factor / 10;
5156  for (unsigned i = min; i < max; i+= ff)
5157  {
5158  //bv.set(i);
5159  iit = i;
5160  ff += ff / 2;
5161  if (ff > fill_factor)
5162  ff = fill_factor / 10;
5163  }
5164  iit.flush();
5165 }
5166 
5167 
5168 static
5169 void GenerateShiftTestCollection(std::vector<bvect>* target,
5170  unsigned count = 30,
5171  unsigned vector_max = 40000000,
5172  bool optimize = true)
5173 {
5174  assert(target);
5175  bvect bv_common; // sub-vector common for all collection
5176  generate_sparse_bvector(bv_common, vector_max/10, vector_max, 250000);
5177 
5178  unsigned cnt1 = (count / 2);
5179 
5180  unsigned i = 0;
5181 
5182  for (i = 0; i < cnt1; ++i)
5183  {
5184  std::unique_ptr<bvect> bv (new bvect);
5185  generate_bvector(*bv, vector_max, optimize);
5186  *bv |= bv_common;
5187  if (optimize)
5188  bv->optimize();
5189  target->push_back(std::move(*bv));
5190  } // for
5191 
5192  unsigned fill_factor = 10;
5193  for (; i < count; ++i)
5194  {
5195  std::unique_ptr<bvect> bv (new bvect);
5196 
5197  FillSetsIntervals(0, *bv, vector_max/ 10, vector_max, fill_factor);
5198  *bv |= bv_common;
5199 
5200  target->push_back(std::move(*bv));
5201  } // for
5202 }
5203 
5204 
5205 
5206 static
5208 {
5209  cout << "---------------------------- Bvector SHIFT test" << endl;
5210 
5211 
5212  {
5213  bvect bv;
5214 
5215  bv.set(bm::id_max-1);
5216  bv.shift_right();
5217  print_bv(bv);
5218  assert(bv.count()==0);
5219  }
5220 
5221  {
5222  bvect bv(BM_GAP);
5223 
5224  bv.set(bm::id_max-1);
5225  bv.optimize();
5226  bv.shift_right();
5227  assert(bv.count()==0);
5228  }
5229 
5230  {
5231  bvect bv;
5232 
5233  bv.set();
5234  auto cnt1 = bv.count();
5235  bv.shift_right();
5236  auto cnt2 = bv.count();
5237  assert(cnt1-1 == cnt2);
5238  bool b = bv.test(0);
5239  assert(!b);
5240  }
5241  {
5242  bvect bv;
5243  bv.set();
5245 
5246  auto cnt1 = bv.count();
5247  bv.shift_left();
5248  auto cnt2 = bv.count();
5249  assert(cnt1-1 == cnt2);
5250  bool b = bv.test(bm::id_max-1);
5251  assert(!b);
5252  b = bv.test(bm::id_max-2);
5253  assert(b);
5255  assert(b);
5257  assert(!b);
5258  }
5259 
5260  {
5261  bvect bv;
5262 
5263  bv.set();
5264  auto cnt1 = bv.count();
5265  bv.shift_left();
5266  auto cnt2 = bv.count();
5267  assert(cnt1-1 == cnt2);
5268  bool b = bv.test(bm::id_max-1);
5269  assert(!b);
5270  }
5271 
5272 
5273 
5274  {
5275  bvect bv;
5276 
5277  bv.set(0);
5278  bv.set(65535);
5279  bv.set(bm::id_max-1);
5280  bvect bv1(bv);
5281 
5282  bvect bv2(bm::BM_GAP);
5283  bv2 = bv;
5284  bv2.optimize();
5285 
5286  ShiftRight(&bv, 1);
5287  assert(bv.count() == 2);
5288  assert(bv.test(1));
5289  assert(bv.test(65536));
5290 
5291  bv1.shift_right();
5292  print_bv(bv1);
5293  int cmp = bv.compare(bv1);
5294  assert(cmp == 0);
5295 
5296  bv2.shift_right();
5297  print_bv(bv2);
5298  cmp = bv.compare(bv2);
5299  assert(cmp == 0);
5300  struct bvect::statistics st;
5301  bv2.calc_stat(&st);
5302  assert(st.gap_blocks >= 2);
5303 
5304  }
5305 
5306  {
5307  bvect bv(BM_GAP);
5308  struct bvect::statistics st;
5309 
5310  for (unsigned i = 0; i < 65536; ++i)
5311  {
5312  bv.set(i);
5313  }
5314  bv.calc_stat(&st);
5315  assert(st.gap_blocks == 1);
5316 
5317  auto cnt = bv.count();
5318  bv.shift_right();
5319  assert(bv.test(0)==0);
5320  assert(bv.count() == cnt);
5321 
5322  bv.calc_stat(&st);
5323  auto bcnt = st.bit_blocks + st.gap_blocks;
5324  assert(bcnt == 2);
5325  assert(st.gap_blocks);
5326  for (unsigned i = 0+1; i < 65536+1; ++i)
5327  {
5328  assert(bv.test(i));
5329  }
5330  }
5331 
5332 
5333  {
5334  cout << " inverted test" << endl;
5335  bvect bv;
5336  bv.invert();
5337  unsigned cnt = bv.count();
5338  bool carry_over = bv.shift_right();
5339  assert(carry_over);
5340  unsigned cnt1 = bv.count();
5341  assert(cnt1 == cnt - 1);
5342  assert(bv.test(0)==0);
5343  assert(bv.test(1)==1);
5344 
5345  struct bvect::statistics st;
5346  bv.calc_stat(&st);
5347  auto bcnt = st.bit_blocks + st.gap_blocks;
5348  assert(bcnt == 2);
5349 
5350  }
5351 
5352  {
5353  cout << " 3-bit optimized test" << endl;
5354  bvect bv;
5355 
5356  bv.set(0);
5357  bv.set(65535);
5358  bv.set(66000);
5359  bv.optimize();
5360  bvect bv1(bv);
5361  ShiftRight(&bv, 1);
5362  bv1.shift_right();
5363  int cmp = bv.compare(bv1);
5364 
5365  assert(cmp == 0);
5366  }
5367 
5368 
5369  {
5370  cout << " carry-over test" << endl;
5371  bvect bv { 1 };
5372  bool carry_over = bv.shift_left();
5373  assert(!carry_over);
5374  unsigned idx = bv.get_first();
5375  assert(idx == 0);
5376  carry_over = bv.shift_left();
5377  assert(carry_over);
5378  idx = bv.get_first();
5379  std::cout << idx << endl;
5380  assert(idx == 0);
5381  assert(bv.count()==0);
5382  }
5383 
5384  {
5385  cout << " 4278190080 test" << endl;
5386 
5387  bvect bv { 4278190080 };
5388  bv.shift_left();
5389  unsigned idx = bv.get_first();
5390  assert(idx == 4278190080-1);
5391  bv.shift_left();
5392  idx = bv.get_first();
5393  assert(idx == 4278190080-2);
5394  }
5395 
5396  {
5397  cout << " 4278190080 (optimized) test" << endl;
5398  bvect bv { 4278190080 };
5399  bv.optimize();
5400  bv.shift_left();
5401  unsigned idx = bv.get_first();
5402  assert(idx == 4278190080-1);
5403  bv.shift_left();
5404  idx = bv.get_first();
5405  assert(idx == 4278190080-2);
5406  }
5407 
5408  {
5409  std::cout << "\nShift-L stress (1 bit shift)..\n" << endl;;
5410  unsigned start = bm::id_max-1;
5411  bvect bv;
5412  bv.set(start);
5413 
5414  struct bvect::statistics st;
5415  bv.calc_stat(&st);
5416  auto bcnt = st.bit_blocks + st.gap_blocks;
5417  assert(bcnt == 1);
5418 
5419  std::chrono::time_point<std::chrono::steady_clock> s;
5420  std::chrono::time_point<std::chrono::steady_clock> f;
5421 
5422  s = std::chrono::steady_clock::now();
5423 
5424  for( ; start; --start)
5425  {
5426  bool carry_over = bv.shift_left();
5427  if (carry_over)
5428  {
5429  cout << "CO at " << start << endl;
5430  assert(bv.count()==0);
5431  assert(start == bm::id_max-1);
5432  break;
5433  }
5434  /*
5435  unsigned idx = bv.get_first();
5436  if(idx != start-1)
5437  {
5438  cerr << bv.count() << endl;
5439  cerr << "Shift-L Failed at idx=" << idx << " != " << start << endl;
5440  exit(1);
5441  }
5442  */
5443 
5444  if ((start % (1024 * 1024)) == 0)
5445  {
5446  f = std::chrono::steady_clock::now();
5447  auto diff = f - s;
5448  auto d = std::chrono::duration <double, std::milli> (diff).count();
5449  cout << "\r" << start << " (" << d << ") " << flush;
5450 
5451  unsigned idx = bv.get_first();
5452  assert(idx == start-1);
5453 
5454  bv.optimize();
5455 
5456  bv.calc_stat(&st);
5457  bcnt = st.bit_blocks + st.gap_blocks;
5458  assert(bcnt == 1);
5459 
5460  s = std::chrono::steady_clock::now();
5461  }
5462  }
5463  cout << "ok.\n";
5464  }
5465 
5466  {
5467  std::cout << "\nShift-R stress (1 bit shift)..\n" << endl;
5468  unsigned start = 0;
5469  bvect bv, bv1(BM_GAP);
5470  bv.set(start);
5471  bv1.set(start);
5472 
5473  struct bvect::statistics st;
5474  bv.calc_stat(&st);
5475  auto bcnt = st.bit_blocks + st.gap_blocks;
5476  assert(bcnt == 1);
5477 
5478  std::chrono::time_point<std::chrono::steady_clock> s;
5479  std::chrono::time_point<std::chrono::steady_clock> f;
5480 
5481  s = std::chrono::steady_clock::now();
5482 
5483  while(1)
5484  {
5485  bool carry_over = bv.shift_right();
5486  if (carry_over)
5487  {
5488  cout << "CO at " << start << endl;
5489  assert(bv.count()==0);
5490  assert(start == bm::id_max-1);
5491  break;
5492  }
5493 
5494  {
5495  bv.calc_stat(&st);
5496  bcnt = st.bit_blocks + st.gap_blocks;
5497  assert(bcnt == 1);
5498  }
5499  carry_over = bv1.shift_right();
5500  if (carry_over)
5501  {
5502  cout << "CO at " << start << endl;
5503  assert(bv1.count()==0);
5504  assert(start == bm::id_max-1);
5505  break;
5506  }
5507  // optmization of GAP blocks is not implemented yet
5508  #if 0
5509  {
5510  bv1.calc_stat(&st);
5511  bcnt = st.bit_blocks + st.gap_blocks;
5512  assert(bcnt == 1);
5513  }
5514  #endif
5515 
5516 
5517  if ((start % (1024 * 1024)) == 0)
5518  {
5519  bool eq = bv.equal(bv1);
5520  assert(eq);
5521 
5522  bv1.optimize();
5523 
5524  f = std::chrono::steady_clock::now();
5525  auto diff = f - s;
5526  auto d = std::chrono::duration <double, std::milli> (diff).count();
5527 
5528  cout << "\r" << start << " (" << d << ") " << flush;
5529 
5530  unsigned idx = bv.get_first();
5531  assert(idx-1 == start);
5532 
5533  bv.calc_stat(&st);
5534  bcnt = st.bit_blocks + st.gap_blocks;
5535  assert(bcnt == 1);
5536 
5537  s = std::chrono::steady_clock::now();
5538  }
5539  ++start;
5540  }
5541  cout << "ok.\n";
5542  }
5543 
5544  {
5545  std::cout << "\nShift-R stress (large vector shift)..\n" << endl;
5546  bvect bv;
5547  generate_bvector(bv);
5548  bvect bv_control(bv);
5549 
5550  unsigned max_shifts = 10000;
5551  for (unsigned i = 0; i < max_shifts; ++i)
5552  {
5553  ShiftRight(&bv_control, 1);
5554  bv.shift_right();
5555  int cmp = bv.compare(bv_control);
5556  assert(cmp==0);
5557  if ((i % 16) == 0)
5558  {
5559  if (!is_silent)
5560  cout << "\r" << i << "/" << max_shifts << flush;
5561  }
5562  }
5563  }
5564  cout << "ok.\n";
5565 
5566 
5567  // stress test for shifting aggregator
5568  //
5569  cout << "\nAggregator based SHIT-R tests..." << endl;
5570  {
5571  const unsigned int REPEATS = 300;
5572 
5573  bvect mask_bv; // mask vector
5574  mask_bv.init();
5575  generate_bvector(mask_bv, 75000000, false); // mask is shorter on both ends
5576 
5577  std::vector<bvect> bv_coll1;
5578  GenerateShiftTestCollection(&bv_coll1, 25, 80000000);
5579 
5580  {
5582  agg.add(&mask_bv);
5583  for (unsigned k = 0; k < bv_coll1.size(); ++k)
5584  {
5585  agg.add(&bv_coll1[k]);
5586  }
5587 
5588  for (unsigned i = 0; i < REPEATS; ++i)
5589  {
5590  bvect bv1(mask_bv);
5591  for (unsigned k = 0; k < bv_coll1.size(); ++k)
5592  {
5593  bv1.shift_right();
5594  bv1 &= bv_coll1[k];
5595  } // for
5596 
5597  bvect bv2;
5598  agg.set_compute_count(false);
5599  agg.combine_shift_right_and(bv2);
5600  int cmp = bv1.compare(bv2);
5601  if (cmp != 0)
5602  {
5603  cerr << "Shift-R compare failure!" << endl;
5604  exit(1);
5605  }
5606  bvect bv3;
5607  agg.set_compute_count(true);
5608  agg.combine_shift_right_and(bv3);
5609  assert(!bv3.any());
5610  auto cnt = agg.count();
5611  auto cnt_c = bv1.count();
5612  assert(cnt == cnt_c);
5613 
5614  } // for
5615  }
5616  }
5617 
5618 
5619  cout << "\n---------------------------- Bvector SHIFT test OK" << endl;
5620 }
5621 
5622 static
5624 {
5625  cout << "\n---------------------------- Bvector INSERT test" << endl;
5626 
5627  {
5628  bvect bv { 1, 2, 3 };
5629  bvect bv_c { 2, 3, 4 };
5630  bvect bv1(bv);
5631  bvect bv2(bv); bv2.optimize();
5632 
5633  BVectorInsert(&bv, 0, false);
5634  int cmp = bv.compare(bv_c);
5635  assert(cmp == 0);
5636 
5637  bv1.insert(0, false);
5638  cmp = bv1.compare(bv_c);
5639  assert(cmp == 0);
5640 
5641  bv2.insert(0, false);
5642  cmp = bv2.compare(bv_c);
5643  assert(cmp == 0);
5644 
5645  struct bvect::statistics st2;
5646  bv2.calc_stat(&st2);
5647  assert(st2.gap_blocks==1);
5648  assert(st2.bit_blocks==0);
5649  }
5650 
5651  {
5652  bvect bv { 1, 2, 3 };
5653  bvect bv_c { 0, 2, 3, 4 };
5654  bvect bv1(bv);
5655  bvect bv2(bv); bv2.optimize();
5656 
5657  BVectorInsert(&bv, 0, true);
5658  int cmp = bv.compare(bv_c);
5659  assert(cmp == 0);
5660 
5661  bv1.insert(0, true);
5662  cmp = bv1.compare(bv_c);
5663  assert(cmp == 0);
5664 
5665  bv2.insert(0, true);
5666 // print_bv(bv2);
5667 // print_bv(bv_c);
5668  cmp = bv2.compare(bv_c);
5669  assert(cmp == 0);
5670 
5671  struct bvect::statistics st2;
5672  bv2.calc_stat(&st2);
5673  assert(st2.gap_blocks==1);
5674  assert(st2.bit_blocks==0);
5675  }
5676 
5677  {
5678  bvect bv;
5679  bv.set_range(0, 65535);
5680  bv.optimize();
5681  bvect bv_c;
5682  bv_c.set_range(0, 65536);
5683 
5684  bv.insert(1, true);
5685  int cmp = bv.compare(bv_c);
5686  assert(cmp == 0);
5687 
5688  struct bvect::statistics st;
5689  bv.calc_stat(&st);
5690  assert(st.gap_blocks==0);
5691  assert(st.bit_blocks==1);
5692 
5693  }
5694 
5695  {
5696  bvect bv { 1, 20, 65535 };
5697  bvect bv_c { 1, 20, 65535, 65536 };
5698  bvect bv1(bv);
5699  bvect bv2(bv); bv2.optimize();
5700 
5701  BVectorInsert(&bv, 65535, true);
5702  int cmp = bv.compare(bv_c);
5703  assert(cmp == 0);
5704 
5705  bv1.insert(65535, true);
5706  print_bv(bv1);
5707  cmp = bv1.compare(bv_c);
5708  assert(cmp == 0);
5709 
5710  bv2.insert(65535, true);
5711  cmp = bv2.compare(bv_c);
5712  assert(cmp == 0);
5713 
5714  struct bvect::statistics st2;
5715  bv2.calc_stat(&st2);
5716  assert(st2.gap_blocks==1);
5717  assert(st2.bit_blocks==1);
5718 
5719  }
5720 
5721  // bit-vector insert checks
5722  {
5723  bvect bv;
5724  bv.resize(10);
5725  bv.insert(120303030, true);
5726  assert(bv.test(120303030));
5727  assert(bv.count()==1);
5728  assert(bv.size() == 120303030+1);
5729  }
5730 
5731  {
5732  bvect bv { 120303030u, 120303031u };
5733  bvect bv1(bv);
5734  BVectorInsert(&bv, 120303031u, true);
5735  bv1.insert(120303031u, true);
5736  int cmp = bv1.compare(bv);
5737  assert(cmp==0);
5738  bv.optimize();
5739  bv1.optimize();
5740  BVectorInsert(&bv, 120303031u, true);
5741  bv1.insert(120303031u, true);
5742  cmp = bv1.compare(bv);
5743  assert(cmp==0);
5744  }
5745 
5746  {
5747  bvect bv, bv1;
5748  bv.set(10);
5749  bv.set_range(1203030u, 1203030u+65535u*10u);
5750  bv1 = bv;
5751  BVectorInsert(&bv, 1203030u+10, false);
5752  bv1.insert(1203030u+10, false);
5753  int cmp = bv1.compare(bv);
5754  assert(cmp==0);
5755  }
5756 
5757  {
5758  std::cout << "INSERT GAP (random)..\n";
5759 
5760  bvect bv(BM_GAP), bv1;
5761  bv.set(10);
5762  bv1 = bv;
5763  bv.optimize();
5764 
5765  for (unsigned i = 0; i < 1000; ++i)
5766  {
5767  unsigned idx = (unsigned)rand() % 65535;
5768  unsigned val = (unsigned)rand() & 1;
5769  bv.insert(idx, val);
5770  bv1.insert(idx, val);
5771  int cmp = bv1.compare(bv);
5772  assert(cmp==0);
5773 
5774  struct bvect::statistics st;
5775  bv.calc_stat(&st);
5776  assert(st.gap_blocks >= 1);
5777  assert(st.bit_blocks == 0);
5778 
5779  bv.insert(idx, val);
5780  bv1.insert(idx, val);
5781  cmp = bv1.compare(bv);
5782  assert(cmp==0);
5783 
5784  bv.calc_stat(&st);
5785  assert(st.gap_blocks >= 1);
5786  assert(st.bit_blocks == 0);
5787  }
5788  }
5789 
5790 
5791  {
5792  std::cout << "INSERT stress (large vector insert)..\n";
5793  bvect bv;
5794  generate_bvector(bv, 40000000);
5795  bvect bv_control(bv);
5796 
5797  unsigned max_shifts = 10000;
5798  for (unsigned i = 0; i < max_shifts; ++i)
5799  {
5800  bvect bv2(bm::BM_GAP);
5801  bv2 = bv_control;
5802  bv2.optimize();
5803 
5804  unsigned i_pos = (unsigned)rand()%40000000;
5805 
5806  BVectorInsert(&bv_control, i_pos, i & 1u);
5807  bv.insert(i_pos, i & 1u);
5808  int cmp = bv.compare(bv_control);
5809  if (cmp != 0)
5810  {
5811  DetailedCompareBVectors(bv, bv_control);
5812  }
5813  assert(cmp==0);
5814 
5815  bv2.insert(i_pos, i & 1u);
5816  cmp = bv2.compare(bv_control);
5817  assert(cmp==0);
5818 
5819  if ((i % 16) == 0)
5820  {
5821  if (!is_silent)
5822  cout << "\r" << i << "/" << max_shifts << flush;
5823  }
5824  } // for i
5825  }
5826  cout << "ok.\n";
5827 
5828 
5829  cout << "---------------------------- Bvector INSERT test OK" << endl;
5830 }
5831 
5832 static
5834 {
5835  cout << "---------------------------- Bvector ERASE test" << endl;
5836 
5837  {
5838  bvect bv;
5839  bv.erase(100);
5840  assert(!bv.any());
5841  }
5842 
5843  {
5844  bvect bv { 1, 2, 3 };
5845  bvect bv_c { 1, 2 };
5846  bv.erase(1);
5847  print_bv(bv);
5848  int cmp = bv.compare(bv_c);
5849  assert(cmp == 0);
5850  }
5851 
5852  {
5853  bvect bv {100, 65536 };
5854  bvect bv_c(bv);
5855  bv.optimize();
5856  bv.erase(99);
5857  print_bv(bv);
5858  BVectorErase(&bv_c, 99);
5859 
5860  assert(bv.test(99));
5861  assert(bv.test(65535));
5862  assert(bv.count()==2);
5863  int cmp = bv.compare(bv_c);
5864  assert(!cmp);
5865  }
5866 
5867  {
5868  bvect bv;
5869  bv.set_range(65536, 65536 + 65536);
5870  bvect bv_c(bv);
5871 
5872  unsigned cnt1 = bv.count();
5873  bv.optimize();
5874  bv.erase(0);
5875  BVectorErase(&bv_c, 0);
5876 
5877  unsigned cnt2 = bv.count();
5878  assert(cnt1 == cnt2);
5879  unsigned cnt3 = bv.count_range(65535, 65535 + 65536);
5880  assert(cnt3 == cnt1);
5881 
5882  struct bvect::statistics st;
5883  bv.calc_stat(&st);
5884  assert(st.bit_blocks == 1);
5885  int cmp = bv.compare(bv_c);
5886  assert(!cmp);
5887  }
5888 
5889  {
5890  bvect bv;
5891  bv.set_range(65536, 65536 + 65535);
5892  unsigned cnt1 = bv.count();
5893  bv.optimize();
5894  assert(cnt1 == bv.count());
5895  bv.erase(0);
5896  unsigned cnt2 = bv.count();
5897  assert(cnt1 == cnt2);
5898  unsigned cnt3 = bv.count_range(65535, 65535 + 65535);
5899  assert(cnt3 == cnt1);
5900  }
5901 
5902  {
5903  bvect bv;
5904  bv.invert();
5905  unsigned cnt1 = bv.count();
5906  bv.erase(65536);
5907  unsigned cnt2 = bv.count();
5908  cout << cnt1 << " " << cnt2 << endl;
5909  assert(cnt1 == (cnt2 + 1));
5910  assert(!bv.test(bm::id_max-1));
5911 
5912  struct bvect::statistics st;
5913  bv.calc_stat(&st);
5914  assert(st.bit_blocks == 2);
5915 
5916  }
5917 
5918  // test how emty blocks get deallocated on left shift
5919  {
5920  unsigned start = 1000000;
5921  bvect bv;
5922  bv.set(start);
5923  unsigned finish = 10;
5924  for(;true;)
5925  {
5926  bv.erase(finish);
5927  --start;
5928  if (start == finish)
5929  {
5930  assert(bv.test(start));
5931  bv.erase(finish);
5932  assert(!bv.test(start));
5933 
5934  struct bvect::statistics st;
5935  bv.calc_stat(&st);
5936  assert(st.bit_blocks == 1);
5937 
5938  break;
5939  }
5940  assert(bv.test(start));
5941  unsigned cnt = bv.count();
5942  assert(cnt == 1);
5943 
5944  struct bvect::statistics st;
5945  bv.calc_stat(&st);
5946  assert(st.bit_blocks == 1);
5947  } // for
5948  }
5949 
5950  cout << "bit erase stress test" << endl;
5951  {
5952  std::random_device rd;
5953 
5954  bvect bv;
5955  generate_bvector(bv, 750000000, false);
5956  bvect bv2(bv);
5957 
5958  bvect bv_c(bv);
5959 
5960  unsigned max_erase = 256;
5961 
5962  for(unsigned k = 0; k < max_erase; ++k)
5963  {
5964  bm::id_t pos;
5965  unsigned from = rd();
5966  bool b = bv.find(from, pos);
5967  if (!b)
5968  {
5969  bool any = bv.any();
5970  if (!any)
5971  break;
5972  pos = 0;
5973  }
5974  bv.erase(pos);
5975  bv2.erase(pos);
5976  BVectorErase(&bv_c, pos);
5977 
5978  int cmp = bv.compare(bv_c);
5979  if (cmp != 0)
5980  {
5981  cerr << "Erase test failed! at pos=" << pos << endl;
5982  exit(1);
5983  }
5984  cmp = bv2.compare(bv_c);
5985  if (cmp != 0)
5986  {
5987  cerr << "2. Erase test failed! at pos=" << pos << endl;
5988  exit(1);
5989  }
5990 
5991 
5992  if ((k % 4) == 0)
5993  {
5994  if (!is_silent)
5995  cout << "\r" << k << "/" << max_erase << flush;
5996  bv.optimize();
5997  }
5998 
5999  } // for
6000  }
6001  cout << "\nOK" << endl;
6002 
6003  cout << "---------------------------- Bvector ERASE test OK" << endl;
6004 }
6005 
6006 
6007 // -----------------------------------------------------------------------
6008 
6009 static
6011 {
6012  bvect bv_subset;
6013  bvect::size_type bcnt = bv.count();
6014 
6015  bvect::size_type samples[] =
6016  { 0, 1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, bcnt / 5, bcnt / 4, bcnt / 3, bcnt / 2, (bcnt * 2)/3, bcnt };
6017  bvect::size_type samples_size = sizeof(samples)/sizeof(*samples);
6018 
6019  printf("Taking random sub-sets: ");
6020 
6021  for (unsigned i = 0; i < samples_size; ++i)
6022  {
6023  unsigned sample_count = samples[i];
6024  printf(" %u, ", sample_count);
6025  rsub.sample(bv_subset, bv, sample_count);
6026  if (sample_count > bcnt)
6027  sample_count = bcnt;
6028 
6029  if (sample_count != bv_subset.count())
6030  {
6031  printf("\nRandom subset failed! sample_count = %u result_count=%u\n",
6032  sample_count,
6033  bv_subset.count());
6034  exit(1);
6035  }
6036  {
6037  bvect bv_subset_copy(bv_subset);
6038  bvect bv_set_copy(bv);
6039  bv_set_copy.invert();
6040  bv_subset_copy -= bv_set_copy;
6041  int res = bv_subset_copy.compare(bv_subset);
6042  if (res != 0)
6043  {
6044  printf("\nRandom subset failed! inverted set MINUS error! \n");
6045  exit(1);
6046  }
6047  }
6048 
6049  bv_subset -= bv;
6050  if (bv_subset.count() != 0)
6051  {
6052  printf("\nRandom subset failed! Extra bits set! \n");
6053  exit(1);
6054  }
6055 
6056 
6057  }
6058  printf("\n");
6059 }
6060 
6061 static
6063 {
6065 
6067 
6068  cout << "\n-------------------------- SimpleRandomFillTest()" << endl;
6069 
6070  printf("Test for Random inverted subset.\n");
6071  {
6072  bvect bv;
6073  bv.invert();
6074  TestRandomSubset(bv, rsub);
6075  }
6076 
6077  {
6078  printf("Simple random fill test 1.\n");
6079  bvect_mini bvect_min(BITVECT_SIZE);
6080  bvect bvect_full;
6081  bvect_full.set_new_blocks_strat(bm::BM_BIT);
6082 
6083 
6084  unsigned iter = ITERATIONS / 5;
6085 
6086  printf("\nSimple Random fill test ITERATIONS = %i\n", iter);
6087 
6088  bvect::rs_index_type rs_idx_full;
6089  bvect_full.build_rs_index(&rs_idx_full);
6090 
6091  bvect_min.set_bit(0);
6092  bvect_full.set_bit(0);
6093  unsigned i;
6094  for (i = 0; i < iter; ++i)
6095  {
6096  unsigned num = unsigned(::rand()) % iter;
6097  bvect_min.set_bit(num);
6098  bvect_full.set_bit(num);
6099 
6100  bvect_full.build_rs_index(&rs_idx_full);
6101 
6102  CheckCountRange(bvect_full, rs_idx_full, 0, num);
6103  CheckCountRange(bvect_full, rs_idx_full, num, num+iter);
6104 
6105  if ((i % 1000) == 0) cout << "." << flush;
6106  }
6107 
6108  CheckVectors(bvect_min, bvect_full, iter);
6109 
6110 
6111  CheckCountRange(bvect_full, rs_idx_full, 0, iter);
6112 
6113  TestRandomSubset(bvect_full, rsub);
6114 
6115  printf("Simple random fill test 2.");
6116 
6117  for(i = 0; i < iter; ++i)
6118  {
6119  unsigned num = unsigned(::rand()) % iter;
6120  bvect_min.clear_bit(num);
6121  bvect_full.clear_bit(num);
6122  }
6123 
6124  CheckVectors(bvect_min, bvect_full, iter);
6125  }
6126 
6127 
6128  {
6129  printf("\nSimple random fill test 3.\n");
6130  bvect_mini bvect_min(BITVECT_SIZE);
6131  bvect bvect_full(bm::BM_GAP);
6132 
6133 
6134  unsigned iter = ITERATIONS;
6135 
6136  printf("\nSimple Random fill test ITERATIONS = %i\n", iter);
6137  bvect::rs_index_type rs_idx_full;
6138  bvect_full.build_rs_index(&rs_idx_full);
6139 
6140  unsigned i;
6141  for(i = 0; i < iter; ++i)
6142  {
6143  unsigned num = unsigned(::rand()) % iter;
6144  bvect_min.set_bit(num);
6145  bvect_full.set_bit(num);
6146 
6147  bvect_full.build_rs_index(&rs_idx_full);
6148 
6149  CheckCountRange(bvect_full, rs_idx_full, 0, 65535);
6150  CheckCountRange(bvect_full, rs_idx_full, 0, num);
6151  CheckCountRange(bvect_full, rs_idx_full, num, num+iter);
6152  }
6153 
6154  CheckVectors(bvect_min, bvect_full, iter);
6155 
6156  TestRandomSubset(bvect_full, rsub);
6157 
6158  printf("Simple random fill test 4.");
6159 
6160  for(i = 0; i < iter; ++i)
6161  {
6162  unsigned num = unsigned(rand()) % iter;
6163  bvect_min.clear_bit(num);
6164  bvect_full.clear_bit(num);
6165 
6166  bvect_full.build_rs_index(&rs_idx_full);
6167 
6168  CheckCountRange(bvect_full, rs_idx_full, 0, num);
6169  CheckCountRange(bvect_full, rs_idx_full, num, num+iter);
6170  }
6171 
6172  CheckVectors(bvect_min, bvect_full, iter);
6173  CheckCountRange(bvect_full, rs_idx_full, 0, iter);
6174 
6175  TestRandomSubset(bvect_full, rsub);
6176  }
6177 
6178 }
6179 
6180 
6181 
6182 static
6184 {
6186 
6187  cout << "----------------------------------- RangeRandomFillTest" << endl;
6188 
6189  {
6190  bvect_mini bvect_min(BITVECT_SIZE);
6191  bvect bvect_full;
6192 
6193  printf("Range Random fill test\n");
6194 
6197  if (max > BITVECT_SIZE)
6198  max = BITVECT_SIZE - 1;
6199 
6200  FillSets(&bvect_min, &bvect_full, min, max, 0);
6201 
6202  CheckVectors(bvect_min, bvect_full, BITVECT_SIZE);
6203 
6204  bvect::rs_index_type rs_idx_full;
6205  bvect_full.build_rs_index(&rs_idx_full);
6206 
6207  CheckCountRange(bvect_full, rs_idx_full, min, max);
6208 
6209  }
6210 
6211 
6212  {
6213  bvect_mini bvect_min(BITVECT_SIZE);
6214  bvect bvect_full;
6215 
6216  printf("Range Random fill test\n");
6217 
6220  if (max > BITVECT_SIZE)
6221  max = BITVECT_SIZE - 1;
6222 
6223  FillSetsIntervals(&bvect_min, bvect_full, min, max, 4);
6224 
6225  CheckVectors(bvect_min, bvect_full, BITVECT_SIZE);
6226  bvect::rs_index_type rs_idx_full;
6227  bvect_full.build_rs_index(&rs_idx_full);
6228 
6229  CheckCountRange(bvect_full, rs_idx_full, min, max);
6230  }
6231 }
6232 
6233 static
6235 {
6236  cout << "----------------------------------- RangeCopyTest" << endl;
6237  {
6238  const unsigned to_max = 65536 * bm::set_sub_array_size + 10;
6239  cout << "Basic range-copy test" << endl;
6240  bvect bvect1
6241  { 10, 20, 21, 100, 65535, 65536, 100000, to_max/2, to_max-1, to_max };
6242 
6243 
6244  CheckRangeCopy(bvect1, 0, 0);
6245  CheckRangeCopy(bvect1, 10, 10);
6246  CheckRangeCopy(bvect1, 15, 15);
6247  CheckRangeCopy(bvect1, 65535, 65535);
6248  CheckRangeCopy(bvect1, 65536, 65536);
6249  CheckRangeCopy(bvect1, 65535, 65536);
6250 
6251  for (unsigned k = 0; k < 2; ++k)
6252  {
6253  cout << "Pass " << k << "-0" << endl;
6254  for (unsigned i = 0; i < to_max; ++i)
6255  {
6256  CheckRangeCopy(bvect1, i, to_max);
6257  }
6258  cout << "Pass " << k << "-1" << endl;
6259  for (unsigned i = to_max-1; i > 0; --i)
6260  {
6261  CheckRangeCopy(bvect1, 0, i);
6262  }
6263  cout << "Pass " << k << "-2" << endl;
6264  auto to = to_max;
6265  for (unsigned i = 0; i != to_max; ++i, --to)
6266  {
6267  CheckRangeCopy(bvect1, i, to_max);
6268  }
6269  bvect1.optimize();
6270  } // for k
6271  cout << "OK" << endl;
6272  }
6273 
6274  {
6275  cout << "Inverted vector stress test" << endl;
6276  bvect bvect1;
6277  bvect1.invert();
6278 
6279  {
6281 
6282  cout << "T1" << endl;
6283  auto to = to_max;
6284 
6285  for (unsigned i = 0; i < to; ++i)
6286  {
6287  CheckRangeCopy(bvect1, i, to);
6288  }
6289 
6290  cout << "T2" << endl;
6291  to = to_max;
6292  for (unsigned i = to; i > 0; --i)
6293  {
6294  CheckRangeCopy(bvect1, 0, i);
6295  }
6296 
6297  cout << "T3" << endl;
6298  to = to_max;
6299  for (unsigned i = 0; i != to; ++i, --to)
6300  {
6301  CheckRangeCopy(bvect1, i, to);
6302  }
6303  cout << "T4" << endl;
6304  to = bm::id_max-1 - to_max - 100;
6305  for (unsigned i = to; i < bm::id_max; ++i)
6306  {
6307  CheckRangeCopy(bvect1, i, bm::id_max);
6308  if ((i & 0xFFFF) == 0)
6309  if (!is_silent)
6310  cout << "\r" << i << flush;
6311  }
6312  cout << endl;
6313  to = bm::id_max-1 - to_max - 100;
6314  for (unsigned i = to; i < bm::id_max-(65536 * 3); i+=65536 * 2)
6315  {
6316  bvect1.set(i, false);
6317  }
6318  for (unsigned k = 0; k < 2; ++k)
6319  {
6320  cout << "T5 pass=" << k << endl;
6321  to = bm::id_max-1 - to_max - (bm::gap_max_bits/2);
6322  for (unsigned i = to; i < bm::id_max; ++i)
6323  {
6324  CheckRangeCopy(bvect1, i, bm::id_max);
6325  if ((i & 0xFFFF) == 0)
6326  if (!is_silent)
6327  cout << "\r" << i << flush;
6328  }
6329  bvect1.optimize();
6330  }
6331  }
6332  }
6333 
6334 
6335 
6336  cout << "----------------------------------- RangeCopyTest() OK\n" << endl;
6337 }
6338 
6339 
6340 
6341 static
6342 void AndOperationsTest(bool detailed)
6343 {
6345 
6346  cout << "----------------------------------- AndOperationTest" << endl;
6347 
6348  {
6349 
6350  bvect_mini bvect_min1(256);
6351  bvect_mini bvect_min2(256);
6352  bvect bvect_full1;
6353  bvect bvect_full2;
6354 
6355  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
6356  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
6357 
6358 
6359 
6360  printf("AND test\n");
6361 
6362  bvect_min1.set_bit(1);
6363  bvect_min1.set_bit(12);
6364  bvect_min1.set_bit(13);
6365 
6366  bvect_min2.set_bit(12);
6367  bvect_min2.set_bit(13);
6368 
6369  bvect_min1.combine_and(bvect_min2);
6370 
6371  bvect_full1.set_bit(1);
6372  bvect_full1.set_bit(12);
6373  bvect_full1.set_bit(13);
6374 
6375  bvect_full2.set_bit(12);
6376  bvect_full2.set_bit(13);
6377 
6378  bm::id_t predicted_count = bm::count_and(bvect_full1, bvect_full2);
6379 
6380  bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
6381  if (predicted_any == 0 && predicted_count != 0)
6382  {
6383  cout << "Predicted any error!" << endl;
6384  exit(1);
6385  }
6386 
6387  {
6388  bvect bv_ro1(bvect_full1, bm::finalization::READONLY);
6389  bm::id_t pcount1 = bm::count_and(bv_ro1, bvect_full2);
6390  assert(pcount1 == predicted_count);
6391 
6392  bvect bv_ro2(bvect_full2, bm::finalization::READONLY);
6393  bm::id_t pcount2 = bm::count_and(bvect_full1, bv_ro2);
6394  assert(pcount2 == predicted_count);
6395 
6396  bm::id_t pcount3 = bm::count_and(bv_ro1, bv_ro2);
6397  assert(pcount3 == predicted_count);
6398  }
6399 
6400  bvect bv_target_s;
6401  SerializationOperation2Test(&bv_target_s,
6402  bvect_full1,
6403  bvect_full2,
6404  predicted_count,
6405  set_COUNT_AND,
6406  set_AND);
6407 
6408 
6409  bvect_full1.bit_and(bvect_full2);
6410 
6411  bm::id_t count = bvect_full1.count();
6412  if (count != predicted_count)
6413  {
6414  cout << "Predicted count error!" << endl;
6415  exit(1);
6416  }
6417 
6418  CheckVectors(bvect_min1, bvect_full1, 256, detailed);
6419  CheckVectors(bvect_min1, bv_target_s, 256, detailed);
6420 
6421  {
6422  bvect::rs_index_type rs_idx_full1;
6423  bvect_full1.build_rs_index(&rs_idx_full1);
6424  CheckCountRange(bvect_full1, rs_idx_full1, 0, 256);
6425  }
6426 
6427  {
6428  bvect bv_ro1(bvect_full1, bm::finalization::READONLY);
6429  bvect::rs_index_type rs_idx_ro1;
6430  bv_ro1.build_rs_index(&rs_idx_ro1);
6431  CheckCountRange(bv_ro1, rs_idx_ro1, 0, 256);
6432  }
6433 
6434  }
6435 
6436  {
6437  bvect bvect1;
6438  bvect bvect2 { 256, 165535 };
6439  bvect bvect_control { 256 };
6440  bvect_control.freeze();
6441 
6442  bvect1.set_range(0, 100000);
6443  bvect2.optimize();
6444 
6445  bvect1 &= bvect2;
6446  int res = bvect1.compare(bvect_control);
6447  assert(res==0);
6448  }
6449 
6450  {
6451  bvect bvect1 { 1, 2, 3};
6452  bvect bvect2 { 256, 165535 };
6453  bvect bvect_control;
6454  bvect1.optimize();
6455 
6456  bvect1 &= bvect2;
6457  int res = bvect1.compare(bvect_control);
6458  assert(res==0);
6459 
6460  bvect::statistics st1;
6461  bvect1.calc_stat(&st1);
6462 
6463  assert(st1.bit_blocks == 0);
6464 
6465  }
6466 
6467  {
6468  bvect bvect1 { 1, 2, 3};
6469  bvect bvect2 { 2, 3 };
6470  bvect bvect3 { 1 };
6471  bvect1 -= bvect2;
6472  bvect::statistics st1;
6473  bvect1.calc_stat(&st1);
6474  assert(st1.bit_blocks == 1);
6475  bvect1 -= bvect3;
6476  bvect1.calc_stat(&st1);
6477  assert(st1.bit_blocks == 0);
6478 
6479  }
6480 
6481 
6482  {
6483 
6484  bvect_mini bvect_min1(BITVECT_SIZE);
6485  bvect_mini bvect_min2(BITVECT_SIZE);
6486  bvect bvect_full1;
6487  bvect bvect_full2;
6488 
6489 
6490  printf("AND test stage 1.\n");
6491 
6492  for (unsigned i = 0; i < 112; ++i)
6493  {
6494  bvect_min1.set_bit(i);
6495  bvect_full1.set_bit(i);
6496 
6497  bvect_min2.set_bit(i);
6498  bvect_full2.set_bit(i);
6499  }
6500 
6501  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10, detailed);
6502  bvect::rs_index_type rs_idx_full1;
6503  bvect_full1.build_rs_index(&rs_idx_full1);
6504  CheckCountRange(bvect_full1, rs_idx_full1, 0, BITVECT_SIZE/10+10);
6505 
6506 // FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
6507 // FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
6508 
6509  bvect_min1.combine_and(bvect_min2);
6510 
6511  bm::id_t predicted_count = bm::count_and(bvect_full1,bvect_full2);
6512  bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
6513  if (predicted_any == 0 && predicted_count != 0)
6514  {
6515  cout << "Predicted any error!" << endl;
6516  exit(1);
6517  }
6518 
6519  bvect bv_target_s;
6520  SerializationOperation2Test(&bv_target_s,
6521  bvect_full1,
6522  bvect_full2,
6523  predicted_count,
6524  set_COUNT_AND,
6525  set_AND);
6526 
6527  bvect_full1.bit_and(bvect_full2);
6528 
6529  bm::id_t count = bvect_full1.count();
6530  if (count != predicted_count)
6531  {
6532  cout << "Predicted count error!" << endl;
6533  exit(1);
6534  }
6535 
6536  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10, detailed);
6537  CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10, detailed);
6538 
6539 // bvect::rs_index_type rs_idx_full1;
6540  bvect_full1.build_rs_index(&rs_idx_full1);
6541 
6542  CheckCountRange(bvect_full1, rs_idx_full1, 0, BITVECT_SIZE/10+10);
6543 
6544  }
6545 
6546 
6547  {
6548 
6549  bvect_mini bvect_min1(BITVECT_SIZE);
6550  bvect_mini bvect_min2(BITVECT_SIZE);
6551  bvect bvect_full1;
6552  bvect bvect_full2;
6553 
6554  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
6555  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
6556 
6557  printf("AND test stage 2.\n");
6558 
6559 
6560  FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
6561  FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
6562 
6563  bm::id_t predicted_count = bm::count_and(bvect_full1,bvect_full2);
6564  bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
6565  if (predicted_any == 0 && predicted_count != 0)
6566  {
6567  cout << "Predicted any error!" << endl;
6568  exit(1);
6569  }
6570 
6571  bvect bv_target_s;
6572  SerializationOperation2Test(&bv_target_s,
6573  bvect_full1,
6574  bvect_full2,
6575  predicted_count,
6576  set_COUNT_AND,
6577  set_AND);
6578 
6579  bvect_min1.combine_and(bvect_min2);
6580 
6581  bvect_full1.bit_and(bvect_full2);
6582 
6583  bm::id_t count = bvect_full1.count();
6584  if (count != predicted_count)
6585  {
6586  cout << "Predicted count error!" << endl;
6587  print_stat(cout,bvect_full1);
6588  exit(1);
6589  }
6590 
6591  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10, detailed);
6592  CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10, detailed);
6593 
6594  bvect::rs_index_type rs_idx1;
6595  bvect_full1.build_rs_index(&rs_idx1);
6596  CheckCountRange(bvect_full1, rs_idx1, 0, BITVECT_SIZE/10+10);
6597 
6598  }
6599 
6600  {
6601 
6602  bvect_mini bvect_min1(BITVECT_SIZE);
6603  bvect_mini bvect_min2(BITVECT_SIZE);
6604  bvect bvect_full1;
6605  bvect bvect_full2;
6606 
6607  bvect_full1.set_new_blocks_strat(bm::BM_BIT);
6608  bvect_full2.set_new_blocks_strat(bm::BM_BIT);
6609 
6610  cout << "------------------------------" << endl;
6611  printf("AND test stage 3.\n");
6612 
6613 
6614  FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
6615  FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
6616 
6617  bvect_min1.combine_and(bvect_min2);
6618 
6619  bm::id_t predicted_count = bm::count_and(bvect_full1, bvect_full2);
6620  bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
6621  if (predicted_any == 0 && predicted_count != 0)
6622  {
6623  cout << "Predicted any error!" << endl;
6624  exit(1);
6625  }
6626 
6627  bvect bv_target_s;
6628  SerializationOperation2Test(&bv_target_s,
6629  bvect_full1,
6630  bvect_full2,
6631  predicted_count,
6632  set_COUNT_AND,
6633  set_AND);
6634 
6635  bvect_full1.bit_and(bvect_full2);
6636 
6637  bm::id_t count = bvect_full1.count();
6638  if (count != predicted_count)
6639  {
6640  cout << "Predicted count error!" << endl;
6641  exit(1);
6642  }
6643 
6644  {
6645  bvect::size_type pos;
6646  bool f = bvect_full1.find_first_mismatch(bv_target_s, pos);
6647  if (f)
6648  {
6649  cerr << "Mismatch at position = " << pos << endl;
6650  assert(0); exit(1);
6651  }
6652  }
6653 
6654  CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE, detailed);
6655  bv_target_s.freeze();
6656  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE, detailed);
6657 
6658  bvect::rs_index_type rs_idx1;
6659  bvect_full1.build_rs_index(&rs_idx1);
6660  CheckCountRange(bvect_full1, rs_idx1, 0, BITVECT_SIZE);
6661 
6663  bvect_full1.optimize(tb);
6664 
6665  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE, detailed);
6666 
6667 // bvect::rs_index_type rs_idx1;
6668  bvect_full1.build_rs_index(&rs_idx1);
6669  CheckCountRange(bvect_full1, rs_idx1, 0, BITVECT_SIZE);
6670  CheckCountRange(bvect_full1, rs_idx1, BITVECT_SIZE/2, BITVECT_SIZE);
6671 
6672  }
6673 
6674  printf("AND test stage 4. combine_and_sorted\n");
6675  {
6676  unsigned ids[] = {0, 1, 2, 3, 10, 65535, 65536, 65535*2, 65535*3};
6677  unsigned to_add = sizeof(ids)/sizeof(unsigned);
6678  bvect bvect_full1;
6679  bvect bvect_full2;
6680  bvect_mini bvect_min1(BITVECT_SIZE);
6681  bvect_mini bvect_min2(BITVECT_SIZE);
6682 
6683  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
6684  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
6685 
6686  for (unsigned i = 2; i < to_add; ++i)
6687  {
6688  bvect_full1.set(ids[i]);
6689  bvect_min1.set_bit(ids[i]);
6690  bvect_full2.set(ids[i]);
6691  bvect_min2.set_bit(ids[i]);
6692  }
6693 
6694  unsigned* first = ids;
6695  unsigned* last = ids + to_add;
6696 
6697  bvect_min1.combine_and(bvect_min2);
6698 
6699  bm::combine_and_sorted(bvect_full1, first, last);
6700  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE, detailed);
6701  }
6702 
6703  {
6704  bvect bvect1 { 1, 10, 12 };
6705  bvect bvect2 { 2, 15, 165535 };
6706  bvect2.freeze();
6707 
6708  bvect1 &= bvect2;
6709 
6710  bvect::statistics st;
6711  bvect1.calc_stat(&st);
6712  if (st.bit_blocks != 0 || st.gap_blocks != 0)
6713  {
6714  cerr << "Error: AND-optimization reduction failed!" << endl;
6715  exit(1);
6716  }
6717  }
6718 
6719 
6720  // ------------------------------------------
6721  // 2-way AND
6722  //
6723  {
6724  bvect bv1 { 0, 1 };
6725  bvect bv2 ;
6726  bv2.bit_and(bv1, bv2, bvect::opt_compress);
6727  int cmp = bv2.any();
6728  assert(cmp == 0);
6729  }
6730 
6731  {
6732  bvect bv1 { 0, 1 };
6733  bvect bv2 { 1, 3 };
6734  bvect bv1c(bv1);
6735  bv1c.bit_and(bv2);
6736 
6737  bvect bv;
6738  bv.bit_and(bv1, bv2, bvect::opt_compress);
6739  int cmp = bv.compare(bv1c);
6740  assert(cmp == 0);
6741  struct bvect::statistics st1;
6742  bv.calc_stat(&st1);
6743  assert(!st1.bit_blocks);
6744  assert(st1.gap_blocks == 1);
6745  }
6746 
6747  {
6748  bvect bv1 { 0, 1 };
6749  bvect bv2;
6750  for (unsigned i = 2; i < 65536; ++i)
6751  bv2.set(i);
6752 
6753  bvect bv1c(bv1);
6754  bv1c.bit_and(bv2);
6755 
6756  bvect bv;
6757  bv.bit_and(bv1, bv2, bvect::opt_none); // should detect 0 automatically
6758  int cmp = bv.compare(bv1c);
6759  assert(cmp == 0);
6760  struct bvect::statistics st1;
6761  bv.calc_stat(&st1);
6762  assert(!st1.bit_blocks);
6763  assert(!st1.gap_blocks);
6764  }
6765 
6766  {
6767  bvect bv1 { 0, 1 };
6768  bvect bv2 { 1 };
6769  bv1.clear(0); bv1.clear(1);
6770  bv2.clear(1);
6771 
6772  bvect bv;
6773  bv.bit_and(bv1, bv2, bvect::opt_none); // should detect empty automatically
6774 
6775  struct bvect::statistics st1;
6776  bv.calc_stat(&st1);
6777  assert(!st1.bit_blocks);
6778  assert(!st1.gap_blocks);
6779  assert(!st1.ptr_sub_blocks);
6780  }
6781 
6782 
6783  {
6784  bvect bvect_full1;
6785  bvect bvect_full2;
6786  bvect_full1.invert();
6787  bvect_full2.set();
6788 
6789  {
6790  bvect bv_target_s;
6791  bv_target_s.bit_and(bvect_full1, bvect_full2, bvect::opt_none);
6792  int cmp = bv_target_s.compare(bvect_full1);
6793  assert(cmp == 0);
6794  }
6795  }
6796  {
6797  bvect bv1;
6798  bvect bv2;
6799  bv1.invert();
6800  bv2.set();
6801  bv2.set(bm::id_max/2, false);
6802 
6803  {
6804  bvect bv_s;
6805  bv_s.bit_and(bv1, bv2, bvect::opt_none);
6806  int cmp = bv_s.compare(bv2);
6807  assert(cmp == 0);
6808  }
6809  {
6810  bvect bv_s;
6811  bv_s.bit_and(bv2, bv1, bvect::opt_none);
6812  int cmp = bv_s.compare(bv2);
6813  assert(cmp == 0);
6814  }
6815  bv1 &= bv2;
6816  int cmp = bv1.compare(bv2);
6817  assert(cmp == 0);
6818  }
6819 
6820  {
6821  bvect bv1 { 0, 1 };
6822  bvect bv2 { 1, 3 };
6823  bv2.optimize();
6824  bvect bv1c(bv1);
6825  bv1c.bit_and(bv2);
6826 
6827  {
6828  bvect bv;
6829  bv.bit_and(bv1, bv2, bvect::opt_compress);
6830  int cmp = bv.compare(bv1c);
6831  assert(cmp == 0);
6832  }
6833  bv1.optimize();
6834  {
6835  bvect bv;
6836  bv.bit_and(bv1, bv2, bvect::opt_compress);
6837  int cmp = bv.compare(bv1c);
6838  assert(cmp == 0);
6839  }
6840  bv2.clear();
6841  bv2.invert();
6842  {
6843  bvect bv;
6844  bv.bit_and(bv1, bv2, bvect::opt_compress);
6845  int cmp = bv.compare(bv1);
6846  assert(cmp == 0);
6847  }
6848  }
6849 
6850  cout << "----------------------------------- AndOperationTest OK" << endl;
6851 
6852 }
6853 
6854 template<typename BV>
6855 void CheckBV_AND_OR(BV& bv_target, const BV& bv1, const BV& bv2)
6856 {
6857  BV bv_control(bv_target);
6858  BV bv_t_copy(bv_target);
6859  BV bv_t_copy1(bv_target);
6860 
6861  BV bv_ro1(bv1, bm::finalization::READONLY);
6862  BV bv_ro2(bv2, bm::finalization::READONLY);
6863 
6864  {
6865  BV bv_and;
6866  bv_and.bit_and(bv1, bv2, bvect::opt_compress);
6867  bv_t_copy |= bv_and;
6868  }
6869 
6870  bv_target.bit_or_and(bv1, bv2, bvect::opt_compress);
6871  bool f;
6872  typename BV::size_type pos;
6873  f = bv_target.find_first_mismatch(bv_t_copy, pos);
6874  if (f)
6875  {
6876  cerr << "AND-OR Mismatch at:" << pos << endl;
6877  unsigned nb = (pos >> bm::set_block_shift);
6878  unsigned i,j;
6879  bm::get_block_coord(nb, i, j);
6880  cout << "nb=" << nb << " i=" << i << " j=" << j << endl;
6881 
6882  bool v1 = bv_target.test(pos);
6883  bool vC = bv_t_copy.test(pos);
6884  cout << "v1=" << v1 << " control=" << vC << endl;
6885 
6886  bv_control.bit_or_and(bv1, bv2, bvect::opt_compress);
6887 
6888  assert(0);
6889  }
6890 
6891  bv_t_copy1.bit_or_and(bv_ro1, bv_ro2, bvect::opt_compress);
6892  bv_t_copy1.freeze();
6893 
6894  bool eq = bv_target.equal(bv_t_copy1);
6895  assert(eq);
6896 }
6897 
6898 static
6899 void AndOrOperationsTest(bool detailed)
6900 {
6901  (void)detailed;
6902  cout << "----------------------------------- AndOrOperationTest()" << endl;
6903 
6904  {
6905  bvect bvtarget;
6906  bvect bv1 { 0, 1 }, bv2 { 1, 3 };
6907  CheckBV_AND_OR(bvtarget, bv1, bv2);
6908  assert(bvtarget.count() == 1);
6909  }
6910 
6911  {
6912  bvect bvtarget;
6913  bvect bv1, bv2;
6914  bv1.invert();
6915  bv2.invert();
6916  CheckBV_AND_OR(bvtarget, bv1, bv2);
6917  }
6918  {
6919  bvect bvtarget { 1, 10, 65536 };
6920  bvect bv1, bv2;
6921  bv1.invert();
6922  bv2.invert();
6923  CheckBV_AND_OR(bvtarget, bv1, bv2);
6924  }
6925  {
6926  bvect bvtarget {1, 256, 65536 } ;
6927  bvect bv1 { 0, 1 }, bv2 { 1, 3 };
6928  CheckBV_AND_OR(bvtarget, bv1, bv2);
6929  auto cnt = bvtarget.count();
6930  assert(cnt == 3);
6931  }
6932  {
6933  bvect bvtarget {1, 256, 65536 } ;
6934  bvect bv1 { 0, 1 }, bv2 { 1, 3 };
6935  bvtarget.optimize();
6936  CheckBV_AND_OR(bvtarget, bv1, bv2);
6937  auto cnt = bvtarget.count();
6938  assert(cnt == 3);
6939  }
6940  {
6941  bvect bvtarget {1, 256, 65536 } ;
6942  bvect bv1 { 0, 1 }, bv2 { 1, 3 };
6943  bv1.optimize();
6944  CheckBV_AND_OR(bvtarget, bv1, bv2);
6945  auto cnt = bvtarget.count();
6946  assert(cnt == 3);
6947  }
6948  {
6949  bvect bvtarget {1, 256, 65536 } ;
6950  bvect bv1 { 0, 1 }, bv2 { 1, 3 };
6951  bv2.optimize();
6952  CheckBV_AND_OR(bvtarget, bv1, bv2);
6953  auto cnt = bvtarget.count();
6954  assert(cnt == 3);
6955  }
6956  {
6957  bvect bvtarget {1, 256, 65536 } ;
6958  bvect bv1 { 0, 1 }, bv2 { 1, 3 };
6959  bv1.optimize();
6960  bv2.optimize();
6961  CheckBV_AND_OR(bvtarget, bv1, bv2);
6962  auto cnt = bvtarget.count();
6963  assert(cnt == 3);
6964  }
6965  {
6966  bvect bvtarget;
6967  bvect bv1 { 0, 1 }, bv2 { 2, 3 };
6968  CheckBV_AND_OR(bvtarget, bv1, bv2);
6969  auto cnt = bvtarget.count();
6970  assert(cnt == 0);
6971  }
6972  {
6973  bvect bvtarget;
6974  bvect bv1, bv2 { 2, 3, bm::id_max/2 };
6975  bv1.invert();
6976  CheckBV_AND_OR(bvtarget, bv1, bv2);
6977  auto cnt = bvtarget.count();
6978  assert(cnt == 3);
6979  }
6980  {
6981  bvect bvtarget;
6982  bvect bv2, bv1 { 2, 3, bm::id_max/2 };
6983  bv2.invert();
6984  CheckBV_AND_OR(bvtarget, bv1, bv2);
6985  auto cnt = bvtarget.count();
6986  assert(cnt == 3);
6987  }
6988 
6989 
6990  {
6991  bvect bvtarget {1, 256, 65536 } ;
6992  bvect bv1 { 0, 1 }, bv2 { 1, 3 };
6993  bvtarget.optimize();
6994  bv1.optimize();
6995  bv2.optimize();
6996  CheckBV_AND_OR(bvtarget, bv1, bv2);
6997  auto cnt = bvtarget.count();
6998  assert(cnt == 3);
6999  }
7000 
7001  {
7002  bvect bvtarget;
7003  bvect bv1, bv2;
7004  bvtarget.set_range(2, 65535);
7005  bv1.set_range(0,1);
7006  bv2.set_range(0,1);
7007  CheckBV_AND_OR(bvtarget, bv1, bv2);
7008  auto cnt = bvtarget.count();
7009  assert(cnt == 65536);
7010  bvect::statistics st;
7011  bvtarget.calc_stat(&st);
7012  assert(st.gap_blocks==0 && st.bit_blocks==0);
7013  }
7014 
7015  // check automatic optimization to FULL and empty blocks
7016  bvect::statistics st;
7017 
7018  {
7019  bvect bvtarget;
7020  bvect bv1{1}, bv2{2};
7021  bvtarget.bit_or_and(bv1, bv2);
7022 
7023  bvtarget.calc_stat(&st);
7024  assert(st.bit_blocks == 0 && st.gap_blocks == 0);
7025 
7026  bv1.optimize();
7027  bvtarget.bit_or_and(bv1, bv2);
7028  bvtarget.calc_stat(&st);
7029  assert(st.bit_blocks == 0 && st.gap_blocks == 0);
7030 
7031  bv2.optimize();
7032  bvtarget.bit_or_and(bv1, bv2);
7033  bvtarget.calc_stat(&st);
7034  assert(st.bit_blocks == 0 && st.gap_blocks == 0);
7035  }
7036 
7037  {
7038  bvect bvtarget {0};
7039  bvect bv1{0}, bv2{0};
7040  bv1.set(0, false);
7041  bv2.set(0, false);
7042  bv1.set_range(1, 65535);
7043  bv2.set_range(1, 65535);
7044  bvtarget.bit_or_and(bv1, bv2);
7045 
7046  bvtarget.calc_stat(&st);
7047  assert(st.bit_blocks == 0 && st.gap_blocks == 0);
7048  }
7049 
7050 
7051  {
7052  bvect bvtarget {0};
7053  bvect bv1{0}, bv2{0};
7054  bv1.set(0, false);
7055  bv2.set(0, false);
7056  bv1.set_range(1, 65535);
7057  bv2.set_range(1, 65535);
7058  bv1.optimize();
7059  bv2.optimize();
7060  bvtarget.bit_or_and(bv1, bv2);
7061 
7062  bvtarget.calc_stat(&st);
7063  assert(st.bit_blocks == 0 && st.gap_blocks == 0);
7064  }
7065 
7066  {
7067  bvect bvtarget {0};
7068  bvect bv1{0}, bv2{0};
7069  bv1.set(0, false);
7070  bv2.set(0, false);
7071  bv1.set_range(1, 65535);
7072  bv2.set_range(1, 65535);
7073  bv2.optimize();
7074  bvtarget.bit_or_and(bv1, bv2);
7075 
7076  bvtarget.calc_stat(&st);
7077  assert(st.bit_blocks == 0 && st.gap_blocks == 0);
7078  }
7079 
7080  cout << "----------------------------------- AndOrOperationTest OK" << endl;
7081 }
7082 
7083 
7084 
7085 static
7086 void OrOperationsTest(bool detailed)
7087 {
7089 
7090  cout << "----------------------------------- OrOperationTest" << endl;
7091 
7092  {
7093 
7094  bvect_mini bvect_min1(256);
7095  bvect_mini bvect_min2(256);
7096  bvect bvect_full1;
7097  bvect bvect_full2;
7098 
7099  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
7100  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
7101 
7102 
7103 
7104  printf("OR test\n");
7105 
7106  bvect_min1.set_bit(1);
7107  bvect_min1.set_bit(12);
7108  bvect_min1.set_bit(13);
7109 
7110  bvect_min2.set_bit(12);
7111  bvect_min2.set_bit(13);
7112 
7113  bvect_min1.combine_or(bvect_min2);
7114 
7115  bvect_full1.set_bit(1);
7116  bvect_full1.set_bit(12);
7117  bvect_full1.set_bit(13);
7118 
7119  bvect_full2.set_bit(12);
7120  bvect_full2.set_bit(13);
7121 
7122  bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);
7123  bm::id_t predicted_any = bm::any_or(bvect_full1, bvect_full2);
7124  if (predicted_any == 0 && predicted_count != 0)
7125  {
7126  cout << "Predicted any error!" << endl;
7127  exit(1);
7128  }
7129 
7130  bvect bv_target_s;
7131  SerializationOperation2Test(&bv_target_s,
7132  bvect_full1,
7133  bvect_full2,
7134  predicted_count,
7135  set_COUNT_OR,
7136  set_OR);
7137 
7138 
7139  bvect_full1.bit_or(bvect_full2);
7140 
7141  bm::id_t count = bvect_full1.count();
7142  if (count != predicted_count)
7143  {
7144  cout << "Predicted count error!" << endl;
7145  cout << predicted_count << " " << count << endl;
7146  print_stat(cout, bvect_full1);
7147  exit(1);
7148  }
7149 
7150  {
7151  bvect::size_type pos;
7152  bool f = bvect_full1.find_first_mismatch(bv_target_s, pos);
7153  if (f)
7154  {
7155  cerr << "Mismatch found pos=" << pos << endl;
7156  assert(0); exit(1);
7157  }
7158  }
7159 
7160 
7161  CheckVectors(bvect_min1, bvect_full1, 256, detailed);
7162  CheckVectors(bvect_min1, bv_target_s, 256, detailed);
7163 
7164 
7165  bvect::rs_index_type rs_idx1;
7166  bvect_full1.build_rs_index(&rs_idx1);
7167 
7168  CheckCountRange(bvect_full1, rs_idx1, 0, 256);
7169  CheckCountRange(bvect_full1, rs_idx1, 128, 256);
7170  }
7171 
7172  {
7173 
7174  bvect_mini bvect_min1(BITVECT_SIZE);
7175  bvect_mini bvect_min2(BITVECT_SIZE);
7176  bvect bvect_full1;
7177  bvect bvect_full2;
7178 
7179  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
7180  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
7181 
7182  printf("OR test stage 2.\n");
7183 
7184 
7185  FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
7186  FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
7187 
7188  bvect_min1.combine_or(bvect_min2);
7189 
7190  bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);
7191  bm::id_t predicted_any = bm::any_or(bvect_full1, bvect_full2);
7192  if (predicted_any == 0 && predicted_count != 0)
7193  {
7194  cout << "Predicted any error!" << endl;
7195  exit(1);
7196  }
7197 
7198  bvect bv_target_s;
7199  SerializationOperation2Test(&bv_target_s,
7200  bvect_full1,
7201  bvect_full2,
7202  predicted_count,
7203  set_COUNT_OR,
7204  set_OR);
7205 
7206 
7207  bvect_full1.bit_or(bvect_full2);
7208 
7209  bm::id_t count = bvect_full1.count();
7210  if (count != predicted_count)
7211  {
7212  cout << "Predicted count error!" << endl;
7213  exit(1);
7214  }
7215 
7216  {
7217  bvect::size_type pos;
7218  bool f = bvect_full1.find_first_mismatch(bv_target_s, pos);
7219  if (f)
7220  {
7221  cerr << "Mismatch found pos=" << pos << endl;
7222  assert(0); exit(1);
7223  }
7224  }
7225 
7226  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10, detailed);
7227  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10, detailed);
7228 
7229  bvect::rs_index_type rs_idx1;
7230  bvect_full1.build_rs_index(&rs_idx1);
7231  CheckCountRange(bvect_full1, rs_idx1, 0, BITVECT_SIZE/10+10);
7232 
7233  }
7234 
7235  {
7236  bvect bv1;
7237  bvect bv2;
7238  bv1.flip(); bv2.flip();
7239  unsigned cnt1 = bv1.count();
7240  bv1.bit_or(bv2);
7241  unsigned cnt2 = bv1.count();
7242  assert(cnt1 == cnt2);
7243  struct bvect::statistics st;
7244  bv1.calc_stat(&st);
7245  auto bcnt = st.bit_blocks + st.gap_blocks;
7246  assert(bcnt == 1);
7247  }
7248 
7249  {
7250 
7251  bvect_mini bvect_min1(BITVECT_SIZE);
7252  bvect_mini bvect_min2(BITVECT_SIZE);
7253  bvect bvect_full1;
7254  bvect bvect_full2;
7255 
7256  bvect_full1.set_new_blocks_strat(bm::BM_BIT);
7257  bvect_full2.set_new_blocks_strat(bm::BM_BIT);
7258 
7259  cout << "------------------------------" << endl;
7260  printf("OR test stage 3.\n");
7261 
7262 
7263  FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
7264  FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
7265 
7266  bvect_min1.combine_or(bvect_min2);
7267  unsigned mcnt = bvect_min1.bit_count();
7268 
7269  cout << mcnt << endl;
7270 
7271  bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);
7272  cout << predicted_count << endl;
7273  bm::id_t predicted_any = bm::any_or(bvect_full1, bvect_full2);
7274  if (predicted_any == 0 && predicted_count != 0)
7275  {
7276  cout << "Predicted any error!" << endl;
7277  exit(1);
7278  }
7279 
7280  bvect bv_target_s;
7281  SerializationOperation2Test(&bv_target_s,
7282  bvect_full1,
7283  bvect_full2,
7284  predicted_count,
7285  set_COUNT_OR,
7286  set_OR);
7287 
7288  bvect_full1.bit_or(bvect_full2);
7289 
7290  bm::id_t count = bvect_full1.count();
7291  if (count != predicted_count)
7292  {
7293  cout << "Predicted count error!" << endl;
7294  exit(1);
7295  }
7296 
7297  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
7298 
7300  bvect_full1.optimize(tb);
7301 
7302  {
7303  bvect::size_type pos;
7304  bool f = bvect_full1.find_first_mismatch(bv_target_s, pos);
7305  if (f)
7306  {
7307  cerr << "Mismatch found pos=" << pos << endl;
7308  assert(0); exit(1);
7309  }
7310  }
7311 
7312  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE, detailed);
7313  CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE, detailed);
7314  bvect::rs_index_type rs_idx1;
7315  bvect_full1.build_rs_index(&rs_idx1);
7316  CheckCountRange(bvect_full1, rs_idx1, 0, BITVECT_SIZE);
7317 
7318 
7319  }
7320 
7321  cout << "Testing combine_or" << endl;
7322 
7323  {
7324 
7325  bvect bvect_full1;
7326  bvect bvect_full2;
7327  bvect_mini bvect_min1(BITVECT_SIZE);
7328 
7329  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
7330  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
7331 
7332  unsigned ids[10000];
7333  unsigned to_add = 10000;
7334 
7335  unsigned bn = 0;
7336  for (unsigned i = 0; i < to_add; ++i)
7337  {
7338  ids[i] = bn;
7339  bvect_full2.set(bn);
7340  bvect_min1.set_bit(bn);
7341  bn += 15;
7342  }
7343 
7344  unsigned* first = ids;
7345  unsigned* last = ids + to_add;
7346 
7347  bm::combine_or(bvect_full1, first, last);
7348 
7349  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
7350 
7351  bm::combine_or(bvect_full1, first, last);
7352  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
7353 
7354  }
7355 
7356 
7357  {
7358  unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
7359  unsigned to_add = sizeof(ids)/sizeof(unsigned);
7360  bvect bvect_full1;
7361  bvect bvect_full2;
7362  bvect_mini bvect_min1(BITVECT_SIZE);
7363 
7364  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
7365  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
7366 
7367  unsigned bn = 0;
7368  for (unsigned i = 0; i < to_add; ++i)
7369  {
7370  ids[i] = bn;
7371  bvect_full2.set(bn);
7372  bvect_min1.set_bit(bn);
7373  bn += 15;
7374  }
7375 
7376  unsigned* first = ids;
7377  unsigned* last = ids + to_add;
7378 
7379  bm::combine_or(bvect_full1, first, last);
7380  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
7381 
7382  bm::combine_or(bvect_full1, first, last);
7383  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
7384  }
7385 
7386  {
7387  bvect bv0;
7388  bvect bv1 { 0, 36500 };
7389  bvect bv2 { 128000, bm::id_max-1 };
7390 
7391  bvect bvc(bv1);
7392  bvc |= bv2;
7393 
7394  bv1.merge(bv2);
7395  int cmp = bv1.compare(bvc);
7396  assert(cmp==0);
7397 
7398  struct bvect::statistics st2;
7399  bv2.calc_stat(&st2);
7400  auto bcnt = st2.bit_blocks + st2.gap_blocks;
7401  assert(bcnt == 0);
7402 
7403 
7404  bv0.merge(bv1);
7405  struct bvect::statistics st1;
7406  bv1.calc_stat(&st1);
7407  bcnt = st1.bit_blocks + st1.gap_blocks;
7408  assert(bcnt == 0);
7409 
7410  }
7411 
7412  // ------------------------------------------
7413  // 2-way OR
7414  //
7415  {
7416  bvect bv1 { 0, 1 };
7417  bvect bv2;
7418  bv2.bit_or(bv1, bv2, bvect::opt_compress);
7419  int cmp = bv1.compare(bv2);
7420  assert(cmp == 0);
7421  }
7422 
7423  {
7424  bvect bv1 { 0, 1 };
7425  bvect bv2 { 2, 3 };
7426  bvect bv1c(bv1);
7427  bv1c.bit_or(bv2);
7428 
7429  bvect bv;
7430  bv.bit_or(bv1, bv2, bvect::opt_compress);
7431  int cmp = bv.compare(bv1c);
7432  assert(cmp == 0);
7433  struct bvect::statistics st1;
7434  bv.calc_stat(&st1);
7435  assert(!st1.bit_blocks);
7436  assert(st1.gap_blocks == 1);
7437  }
7438 
7439  {
7440  bvect bv1 { 0, 1 };
7441  bvect bv2;
7442  for (unsigned i = 2; i < 65536; ++i)
7443  bv2.set(i);
7444 
7445  bvect bv1c(bv1);
7446  bv1c.bit_or(bv2);
7447 
7448  bvect bv;
7449  bv.bit_or(bv1, bv2, bvect::opt_none); // should detect FULL automatically
7450  int cmp = bv.compare(bv1c);
7451  assert(cmp == 0);
7452  struct bvect::statistics st1;
7453  bv.calc_stat(&st1);
7454  assert(!st1.bit_blocks);
7455  assert(!st1.gap_blocks);
7456  }
7457 
7458  {
7459  bvect bv1 { 0, 1 };
7460  bvect bv2 { 1 };
7461  bv1.clear(0); bv1.clear(1);
7462  bv2.clear(1);
7463 
7464  bvect bv;
7465  bv.bit_or(bv1, bv2, bvect::opt_none); // should detect FULL automatically
7466 
7467  struct bvect::statistics st1;
7468  bv.calc_stat(&st1);
7469  assert(!st1.bit_blocks);
7470  assert(!st1.gap_blocks);
7471  assert(!st1.ptr_sub_blocks);
7472  }
7473 
7474  {
7475  bvect bv1;
7476  bvect bv2;
7477  bv1.invert(); bv2.set();
7478  bv1 |= bv2;
7479  bvect bv;
7480  bv.bit_or(bv1, bv2, bvect::opt_compress);
7481  int cmp = bv.compare(bv2);
7482  assert(cmp == 0);
7483  cmp = bv.compare(bv1);
7484  assert(cmp == 0);
7485  bvect bv3 { 10, bm::id_max - 1 };
7486  bv1 |= bv3;
7487  cmp = bv.compare(bv1);
7488  assert(cmp == 0);
7489  }
7490  {
7491  bvect bv1;
7492  bvect bv2;
7493  bv1.invert();
7494  bv2.set();
7495  bv2.set(bm::id_max/2, false);
7496 
7497  {
7498  bvect bv_s;
7499  bv_s.bit_or(bv1, bv2, bvect::opt_none);
7500  int cmp = bv_s.compare(bv1);
7501  assert(cmp == 0);
7502  }
7503  {
7504  bvect bv_s;
7505  bv_s.bit_or(bv2, bv1, bvect::opt_none);
7506  auto cnt = bv_s.count();
7507  assert(cnt == bm::id_max);
7508  bool b = bv_s.test(bm::id_max/2);
7509  assert(b);
7510  int cmp = bv_s.compare(bv1);
7511  assert(cmp == 0);
7512  }
7513  bv2 |= bv1;
7514  int cmp = bv1.compare(bv2);
7515  assert(cmp == 0);
7516  }
7517 
7518  {
7519  bvect bv1 { 0, 1 };
7520  bvect bv2 { 2, 3 };
7521  bv2.optimize();
7522  bvect bv1c(bv1);
7523  bv1c.bit_or(bv2);
7524 
7525  {
7526  bvect bv;
7527  bv.bit_or(bv1, bv2, bvect::opt_compress);
7528  int cmp = bv.compare(bv1c);
7529  assert(cmp == 0);
7530  }
7531  bv1.optimize();
7532  {
7533  bvect bv;
7534  bv.bit_or(bv1, bv2, bvect::opt_compress);
7535  int cmp = bv.compare(bv1c);
7536  assert(cmp == 0);
7537  }
7538  bv2.clear();
7539  bv2.invert();
7540  {
7541  bvect bv;
7542  bv |= bv2;
7543  int cmp = bv.compare(bv2);
7544  assert(cmp == 0);
7545  bv.bit_or(bv1, bv2, bvect::opt_compress);
7546  cmp = bv.compare(bv2);
7547  assert(cmp == 0);
7548  }
7549  }
7550 
7551 
7552 
7553  cout << "----------------------------------- OrOperationTest OK" << endl;
7554 
7555 }
7556 
7557 
7558 static
7559 void SubOperationsTest(bool detailed)
7560 {
7562 
7563  cout << "----------------------------------- SubOperationTest" << endl;
7564 
7565  {
7566 
7567  bvect_mini bvect_min1(256);
7568  bvect_mini bvect_min2(256);
7569  bvect bvect_full1;
7570  bvect bvect_full2;
7571 
7572  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
7573  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
7574 
7575 
7576 
7577  printf("SUB test\n");
7578 
7579  bvect_min1.set_bit(1);
7580  bvect_min1.set_bit(12);
7581  bvect_min1.set_bit(13);
7582 
7583  bvect_min2.set_bit(12);
7584  bvect_min2.set_bit(13);
7585 
7586  bvect_min1.combine_sub(bvect_min2);
7587 
7588  bvect_full1.set_bit(1);
7589  bvect_full1.set_bit(12);
7590  bvect_full1.set_bit(13);
7591 
7592  bvect_full2.set_bit(12);
7593  bvect_full2.set_bit(13);
7594 
7595  bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
7596  bm::id_t predicted_any = bm::any_sub(bvect_full1, bvect_full2);
7597  if (predicted_any == 0 && predicted_count != 0)
7598  {
7599  cout << "Predicted any error!" << endl;
7600  exit(1);
7601  }
7602 
7603  bvect bv_target_s;
7604  SerializationOperation2Test(&bv_target_s,
7605  bvect_full1,
7606  bvect_full2,
7607  predicted_count,
7609  set_SUB);
7610 
7611 
7612  bvect_full1.bit_sub(bvect_full2);
7613 
7614  bm::id_t count = bvect_full1.count();
7615  if (count != predicted_count)
7616  {
7617  cout << "Predicted count error!" << endl;
7618  exit(1);
7619  }
7620 
7621  {
7622  bvect::size_type pos;
7623  bool f = bvect_full1.find_first_mismatch(bv_target_s, pos);
7624  if (f)
7625  {
7626  cerr << "Mismatch found pos=" << pos << endl;
7627  assert(0); exit(1);
7628  }
7629  }
7630 
7631  CheckVectors(bvect_min1, bvect_full1, 256, detailed);
7632  CheckVectors(bvect_min1, bv_target_s, 256, detailed);
7633 
7634  bvect::rs_index_type rs_idx1;
7635  bvect_full1.build_rs_index(&rs_idx1);
7636  CheckCountRange(bvect_full1, rs_idx1, 0, 256);
7637 
7638  }
7639 
7640  {
7641 
7642  bvect_mini bvect_min1(BITVECT_SIZE);
7643  bvect_mini bvect_min2(BITVECT_SIZE);
7644  bvect bvect_full1;
7645  bvect bvect_full2;
7646 
7647  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
7648  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
7649 
7650  printf("SUB test stage 2.\n");
7651 
7652 
7653  FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
7654  FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
7655 
7656  bvect_min1.combine_sub(bvect_min2);
7657 
7658  bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
7659  bm::id_t predicted_any = bm::any_sub(bvect_full1, bvect_full2);
7660  if (predicted_any == 0 && predicted_count != 0)
7661  {
7662  cout << "Predicted any error!" << endl;
7663  exit(1);
7664  }
7665 
7666  bvect bv_target_s;
7667  SerializationOperation2Test(&bv_target_s,
7668  bvect_full1,
7669  bvect_full2,
7670  predicted_count,
7672  set_SUB);
7673 
7674  bvect_full1.bit_sub(bvect_full2);
7675 
7676  bm::id_t count = bvect_full1.count();
7677  if (count != predicted_count)
7678  {
7679  cout << "Predicted count error!" << endl;
7680  cout << predicted_count << " " << count << endl;
7681  print_stat(cout,bvect_full1);
7682 
7683  exit(1);
7684  }
7685 
7686  {
7687  bvect::size_type pos;
7688  bool f = bvect_full1.find_first_mismatch(bv_target_s, pos);
7689  if (f)
7690  {
7691  cerr << "Mismatch found pos=" << pos << endl;
7692  assert(0); exit(1);
7693  }
7694  }
7695 
7696  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10, detailed);
7697  CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10, detailed);
7698 
7699  bvect::rs_index_type rs_idx1;
7700  bvect_full1.build_rs_index(&rs_idx1);
7701  CheckCountRange(bvect_full1, rs_idx1, 0, BITVECT_SIZE/10+10);
7702 
7703  }
7704 
7705  {
7706 
7707  bvect_mini bvect_min1(BITVECT_SIZE);
7708  bvect_mini bvect_min2(BITVECT_SIZE);
7709  bvect bvect_full1;
7710  bvect bvect_full2;
7711 
7712  bvect_full1.set_new_blocks_strat(bm::BM_BIT);
7713  bvect_full2.set_new_blocks_strat(bm::BM_BIT);
7714 
7715  cout << "------------------------------" << endl;
7716  printf("SUB test stage 3.\n");
7717 
7718 
7719  FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
7720  FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
7721 
7722  bvect_min1.combine_sub(bvect_min2);
7723 
7724  bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
7725  bm::id_t predicted_any = bm::any_sub(bvect_full1, bvect_full2);
7726  if (predicted_any == 0 && predicted_count != 0)
7727  {
7728  cout << "Predicted any error!" << endl;
7729  exit(1);
7730  }
7731 
7732  bvect bv_target_s;
7733  SerializationOperation2Test(&bv_target_s,
7734  bvect_full1,
7735  bvect_full2,
7736  predicted_count,
7738  set_SUB);
7739 
7740  bvect_full1.bit_sub(bvect_full2);
7741 
7742  bm::id_t count = bvect_full1.count();
7743  if (count != predicted_count)
7744  {
7745  cout << "Predicted count error!" << endl;
7746  exit(1);
7747  }
7748 
7749 
7750  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
7751 
7753  bvect_full1.optimize(tb);
7754  {
7755  bvect::size_type pos;
7756  bool f = bvect_full1.find_first_mismatch(bv_target_s, pos);
7757  if (f)
7758  {
7759  cerr << "Mismatch found pos=" << pos << endl;
7760  assert(0); exit(1);
7761  }
7762  }
7763 
7764  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE, detailed);
7765  CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE, detailed);
7766 
7767  bvect::rs_index_type rs_idx1;
7768  bvect_full1.build_rs_index(&rs_idx1);
7769  CheckCountRange(bvect_full1, rs_idx1, 0, BITVECT_SIZE);
7770 
7771  }
7772 
7773 
7774  // ------------------------------------------
7775  // 2-way SUB
7776  //
7777 
7778  {
7779  bvect bv1 { 0, 1 };
7780  bvect bv2 { 1, 3 };
7781  bvect bv1c(bv1);
7782  bv1c.bit_sub(bv2);
7783 
7784  bvect bv;
7785  bv.bit_sub(bv1, bv2, bvect::opt_compress);
7786  int cmp = bv.compare(bv1c);
7787  assert(cmp == 0);
7788  struct bvect::statistics st1;
7789  bv.calc_stat(&st1);
7790  assert(!st1.bit_blocks);
7791  assert(st1.gap_blocks == 1);
7792  }
7793 
7794  {
7795  bvect bv1 { 0, 1 };
7796  bvect bv2;
7797  for (unsigned i = 0; i < 65536; ++i)
7798  bv2.set(i);
7799 
7800  bvect bv1c(bv1);
7801  bv1c.bit_sub(bv2);
7802 
7803  bvect bv;
7804  bv.bit_sub(bv1, bv2, bvect::opt_none); // should detect 0 automatically
7805  int cmp = bv.compare(bv1c);
7806  assert(cmp == 0);
7807  struct bvect::statistics st1;
7808  bv.calc_stat(&st1);
7809  assert(!st1.bit_blocks);
7810  assert(!st1.gap_blocks);
7811  }
7812 
7813  {
7814  bvect bv1 { 0, 1 };
7815  bvect bv2 { 1 };
7816  bv1.clear(0); bv1.clear(1);
7817  bv2.clear(1);
7818 
7819  bvect bv;
7820  bv.bit_or(bv1, bv2, bvect::opt_none); // should detect 0 automatically
7821 
7822  struct bvect::statistics st1;
7823  bv.calc_stat(&st1);
7824  assert(!st1.bit_blocks);
7825  assert(!st1.gap_blocks);
7826  assert(!st1.ptr_sub_blocks);
7827  }
7828 
7829 
7830 
7831  {
7832  bvect bv1 { 0, 1 };
7833  bvect bv2 { 1, 3 };
7834  bv1.optimize();
7835  bvect bv1c(bv1);
7836  bv1c.bit_sub(bv2);
7837 
7838  {
7839  bvect bv;
7840  bv.bit_sub(bv1, bv2, bvect::opt_compress);
7841  int cmp = bv.compare(bv1c);
7842  assert(cmp == 0);
7843  }
7844  bv2.optimize();
7845  {
7846  bvect bv;
7847  bv.bit_sub(bv1, bv2, bvect::opt_compress);
7848  int cmp = bv.compare(bv1c);
7849  assert(cmp == 0);
7850  }
7851  bv2.clear();
7852  bv2.invert();
7853  {
7854  bvect bv;
7855  bv.bit_sub(bv1, bv2, bvect::opt_compress);
7856  assert(!bv.any());
7857  }
7858  }
7859 
7860  {
7861  bvect bvect_full1;
7862  bvect bvect_full2;
7863  bvect_full1.invert();
7864  bvect_full2.set();
7865 
7866  {
7867  bvect bv_target_s;
7868  bv_target_s.bit_sub(bvect_full1, bvect_full2, bvect::opt_none);
7869  auto b = bv_target_s.none();
7870  assert(b);
7871  }
7872  }
7873 
7874  {
7875  bvect bv1;
7876  bvect bv2;
7877  bv1.invert();
7878  bv2.set();
7879  bv2.set(0, false);
7880 
7881  {
7882  bvect bv_s;
7883  bv_s.bit_sub(bv1, bv2, bvect::opt_none);
7884  auto cnt = bv_s.count();
7885  assert(cnt == 1);
7886  assert(bv1.test(bm::id_max/2));
7887  }
7888  {
7889  bvect bv_s;
7890  bv_s.bit_sub(bv2, bv1, bvect::opt_none);
7891  auto cnt = bv_s.count();
7892  assert(cnt == 0);
7893  }
7894  bv1 -= bv2;
7895  auto cnt = bv1.count();
7896  assert(cnt == 1);
7897  assert(bv1.test(0));
7898  }
7899 
7900  cout << "----------------------------------- SubOperationTest OK" << endl;
7901 }
7902 
7903 
7904 static
7905 void XorOperationsTest(bool detailed)
7906 {
7908 
7909  cout << "----------------------------------- XorOperationTest" << endl;
7910  {
7911 
7912  bvect_mini bvect_min1(256);
7913  bvect_mini bvect_min2(256);
7914  bvect bvect_full1;
7915  bvect bvect_full2;
7916 
7917  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
7918  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
7919 
7920 
7921 
7922  printf("XOR test\n");
7923 
7924  bvect_min1.set_bit(1);
7925  bvect_min1.set_bit(12);
7926  bvect_min1.set_bit(13);
7927 
7928  bvect_min2.set_bit(12);
7929  bvect_min2.set_bit(13);
7930 
7931  bvect_min1.combine_xor(bvect_min2);
7932 
7933  bvect_full1.set_bit(1);
7934  bvect_full1.set_bit(12);
7935  bvect_full1.set_bit(13);
7936 
7937  bvect_full2.set_bit(12);
7938  bvect_full2.set_bit(13);
7939 
7940  bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
7941  bm::id_t predicted_any = bm::any_xor(bvect_full1, bvect_full2);
7942  if (predicted_any == 0 && predicted_count != 0)
7943  {
7944  cout << "Predicted any error!" << endl;
7945  exit(1);
7946  }
7947 
7948  bvect bv_target_s;
7949  SerializationOperation2Test(&bv_target_s,
7950  bvect_full1,
7951  bvect_full2,
7952  predicted_count,
7953  set_COUNT_XOR,
7954  set_XOR);
7955 
7956 
7957  bvect_full1.bit_xor(bvect_full2);
7958 
7959  bm::id_t count = bvect_full1.count();
7960  if (count != predicted_count)
7961  {
7962  cout << "1.Predicted count error!" << endl;
7963  exit(1);
7964  }
7965 
7966  {
7967  bvect::size_type pos;
7968  bool f = bvect_full1.find_first_mismatch(bv_target_s, pos);
7969  if (f)
7970  {
7971  cerr << "Mismatch found pos=" << pos << endl;
7972  assert(0); exit(1);
7973  }
7974  }
7975  CheckVectors(bvect_min1, bvect_full1, 256, detailed);
7976  CheckVectors(bvect_min1, bv_target_s, 256, detailed);
7977 
7978  bvect::rs_index_type rs_idx1;
7979  bvect_full1.build_rs_index(&rs_idx1);
7980 
7981  CheckCountRange(bvect_full1, rs_idx1, 0, 256);
7982  CheckCountRange(bvect_full1, rs_idx1, 128, 256);
7983 
7984  }
7985  {
7986  bvect bvect1;
7987  bvect_mini bvect_min1(BITVECT_SIZE);
7988 
7989  bvect bvect2;
7990  bvect_mini bvect_min2(BITVECT_SIZE);
7991 
7992 
7993  for (unsigned i = 0; i < 150000; ++i)
7994  {
7995  bvect2.set_bit(i);
7996  bvect_min2.set_bit(i);
7997  }
7998 
8000  bvect2.optimize(tb);
8001 
8002  bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
8003  bm::id_t predicted_any = bm::any_xor(bvect1, bvect2);
8004  if (predicted_any == 0 && predicted_count != 0)
8005  {
8006  cout << "Predicted any error!" << endl;
8007  exit(1);
8008  }
8009 
8010  bvect bv_target_s;
8011  SerializationOperation2Test(&bv_target_s,
8012  bvect1,
8013  bvect2,
8014  predicted_count,
8015  set_COUNT_XOR,
8016  set_XOR);
8017 
8018  bvect1.bit_xor(bvect2);
8019 
8020  bm::id_t count = bvect1.count();
8021  if (count != predicted_count)
8022  {
8023  cout << "2.Predicted count error!" << endl;
8024  exit(1);
8025  }
8026 
8027  bvect_min1.combine_xor(bvect_min2);
8028 
8029  {
8030  bvect::size_type pos;
8031  bool f = bvect1.find_first_mismatch(bv_target_s, pos);
8032  if (f)
8033  {
8034  cerr << "Mismatch found pos=" << pos << endl;
8035  assert(0); exit(1);
8036  }
8037  }
8038 
8039  CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, detailed);
8040  CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE, detailed);
8041 
8042  bvect::rs_index_type rs_idx1;
8043  bvect1.build_rs_index(&rs_idx1);
8044  CheckCountRange(bvect1, rs_idx1, 0, BITVECT_SIZE);
8045  }
8046 
8047  {
8048  bvect bv1;
8049  bvect bv2;
8050  bv1.flip();
8051  bv2.flip();
8052  bv1.bit_xor(bv2);
8053  unsigned cnt2 = bv1.count();
8054  assert(0 == cnt2);
8055  struct bvect::statistics st;
8056  bv1.calc_stat(&st);
8057  auto bcnt = st.bit_blocks + st.gap_blocks;
8058  assert(bcnt == 0);
8059  }
8060 
8061 
8062  {
8063  bvect bvect1;
8064  bvect_mini bvect_min1(BITVECT_SIZE);
8065 
8066  bvect bvect2;
8067  bvect_mini bvect_min2(BITVECT_SIZE);
8068 
8069 
8070  for (unsigned i = 0; i < 150000; ++i)
8071  {
8072  bvect1.set_bit(i);
8073  bvect_min1.set_bit(i);
8074  }
8075 
8077  bvect1.optimize(tb);
8078 
8079  bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
8080  bm::id_t predicted_any = bm::any_xor(bvect1, bvect2);
8081  if (predicted_any == 0 && predicted_count != 0)
8082  {
8083  cout << "Predicted any error!" << endl;
8084  exit(1);
8085  }
8086 
8087  bvect bv_target_s;
8088  SerializationOperation2Test(&bv_target_s,
8089  bvect1,
8090  bvect2,
8091  predicted_count,
8092  set_COUNT_XOR,
8093  set_XOR);
8094 
8095  bvect1.bit_xor(bvect2);
8096 
8097  bm::id_t count = bvect1.count();
8098  if (count != predicted_count)
8099  {
8100  cout << "3.Predicted count error!" << endl;
8101  exit(1);
8102  }
8103 
8104  bvect_min1.combine_xor(bvect_min2);
8105  {
8106  bvect::size_type pos;
8107  bool f = bvect1.find_first_mismatch(bv_target_s, pos);
8108  if (f)
8109  {
8110  cerr << "Mismatch found pos=" << pos << endl;
8111  assert(0); exit(1);
8112  }
8113  }
8114 
8115  CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, detailed);
8116  CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE, detailed);
8117  }
8118 
8119 
8120  {
8121  bvect bvect1;
8122  bvect_mini bvect_min1(BITVECT_SIZE);
8123 
8124  bvect bvect2;
8125  bvect_mini bvect_min2(BITVECT_SIZE);
8126 
8127 
8128  for (unsigned i = 0; i < 150000; ++i)
8129  {
8130  bvect1.set_bit(i);
8131  bvect_min1.set_bit(i);
8132  bvect2.set_bit(i);
8133  bvect_min2.set_bit(i);
8134  }
8135 
8137  bvect1.optimize(tb);
8138 
8139  bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
8140  bm::id_t predicted_any = bm::any_xor(bvect1, bvect2);
8141  if (predicted_any == 0 && predicted_count != 0)
8142  {
8143  cout << "Predicted any error!" << endl;
8144  exit(1);
8145  }
8146 
8147  bvect bv_target_s;
8148  SerializationOperation2Test(&bv_target_s,
8149  bvect1,
8150  bvect2,
8151  predicted_count,
8152  set_COUNT_XOR,
8153  set_XOR);
8154 
8155  bvect1.bit_xor(bvect2);
8156 
8157  bm::id_t count = bvect1.count();
8158  if (count != predicted_count)
8159  {
8160  cout << "4.Predicted count error!" << endl;
8161  cout << count << " " << predicted_count << endl;
8162 
8163  exit(1);
8164  }
8165 
8166  bvect_min1.combine_xor(bvect_min2);
8167  CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, true);
8168  }
8169 
8170 
8171 
8172  {
8173 
8174  bvect_mini bvect_min1(BITVECT_SIZE);
8175  bvect_mini bvect_min2(BITVECT_SIZE);
8176  bvect bvect_full1;
8177  bvect bvect_full2;
8178 
8179  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
8180  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
8181 
8182  printf("XOR test stage 2.\n");
8183 
8184  FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
8185  FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
8186 
8187  bvect_min1.combine_xor(bvect_min2);
8188 
8189  bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
8190  bm::id_t predicted_any = bm::any_xor(bvect_full1, bvect_full2);
8191  if (predicted_any == 0 && predicted_count != 0)
8192  {
8193  cout << "Predicted any error!" << endl;
8194  exit(1);
8195  }
8196 
8197  bvect bv_target_s;
8198  SerializationOperation2Test(&bv_target_s,
8199  bvect_full1,
8200  bvect_full2,
8201  predicted_count,
8202  set_COUNT_XOR,
8203  set_XOR);
8204 
8205 
8206  bvect_full1.bit_xor(bvect_full2);
8207 
8208  bm::id_t count = bvect_full1.count();
8209  if (count != predicted_count)
8210  {
8211  cout << "5.Predicted count error!" << endl;
8212  cout << count << " " << predicted_count << endl;
8213  print_stat(cout,bvect_full1);
8214  exit(1);
8215  }
8216 
8217  {
8218  bvect::size_type pos;
8219  bool f = bvect_full1.find_first_mismatch(bv_target_s, pos);
8220  if (f)
8221  {
8222  cerr << "Mismatch found pos=" << pos << endl;
8223  assert(0); exit(1);
8224  }
8225  }
8226 
8227  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10, detailed);
8228  CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10, detailed);
8229 
8230  bvect::rs_index_type rs_idx1;
8231  bvect_full1.build_rs_index(&rs_idx1);
8232  CheckCountRange(bvect_full1, rs_idx1, 0, BITVECT_SIZE/10+10);
8233 
8234  }
8235 
8236  {
8237 
8238  bvect_mini bvect_min1(BITVECT_SIZE);
8239  bvect_mini bvect_min2(BITVECT_SIZE);
8240  bvect bvect_full1;
8241  bvect bvect_full2;
8242 
8243  bvect_full1.set_new_blocks_strat(bm::BM_BIT);
8244  bvect_full2.set_new_blocks_strat(bm::BM_BIT);
8245 
8246  cout << "------------------------------" << endl;
8247  printf("XOR test stage 3.\n");
8248 
8249 
8250  FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
8251  FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
8252 
8253  bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
8254  bm::id_t predicted_any = bm::any_xor(bvect_full1, bvect_full2);
8255  if (predicted_any == 0 && predicted_count != 0)
8256  {
8257  cout << "Predicted any error!" << endl;
8258  exit(1);
8259  }
8260 
8261  bvect bv_target_s;
8262  SerializationOperation2Test(&bv_target_s,
8263  bvect_full1,
8264  bvect_full2,
8265  predicted_count,
8266  set_COUNT_XOR,
8267  set_XOR);
8268 
8269  bvect_min1.combine_xor(bvect_min2);
8270 
8271  bvect_full1.bit_xor(bvect_full2);
8272 
8273  bm::id_t count = bvect_full1.count();
8274  if (count != predicted_count)
8275  {
8276  cout << "6.Predicted count error!" << endl;
8277  exit(1);
8278  }
8279 
8280 
8281  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
8282 
8284  bvect_full1.optimize(tb);
8285  {
8286  bvect::size_type pos;
8287  bool f = bvect_full1.find_first_mismatch(bv_target_s, pos);
8288  if (f)
8289  {
8290  cerr << "Mismatch found pos=" << pos << endl;
8291  assert(0); exit(1);
8292  }
8293  }
8294 
8295  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE, detailed);
8296  CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE, detailed);
8297 
8298  bvect::rs_index_type rs_idx1;
8299  bvect_full1.build_rs_index(&rs_idx1);
8300  CheckCountRange(bvect_full1, rs_idx1, 0, BITVECT_SIZE);
8301 
8302  }
8303 
8304 
8305  cout << "Testing combine_xor" << endl;
8306 
8307  {
8308 
8309  bvect bvect_full1;
8310  bvect bvect_full2;
8311  bvect_mini bvect_min1(BITVECT_SIZE);
8312 
8313  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
8314  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
8315 
8316  unsigned ids[10000];
8317  unsigned to_add = 10000;
8318 
8319  unsigned bn = 0;
8320  for (unsigned i = 0; i < to_add; ++i)
8321  {
8322  ids[i] = bn;
8323  bvect_full2.set(bn);
8324  bvect_min1.set_bit(bn);
8325  bn += 15;
8326  }
8327 
8328  unsigned* first = ids;
8329  unsigned* last = ids + to_add;
8330 
8331  bm::combine_xor(bvect_full1, first, last);
8332 
8333  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
8334 
8335  bm::combine_xor(bvect_full1, first, last);
8336  if (bvect_full1.count())
8337  {
8338  cout << "combine_xor count failed!" << endl;
8339  exit(1);
8340  }
8341 
8342  }
8343 
8344  {
8345 
8346  bvect bvect_full1;
8347  bvect bvect_full2;
8348  bvect_mini bvect_min1(BITVECT_SIZE);
8349 
8350  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
8351  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
8352 
8353  unsigned ids[10000]={0,};
8354  unsigned to_add = 10000;
8355 
8356  for (unsigned i = 0; i < to_add; i+=100)
8357  {
8358  ids[i] = i;
8359  bvect_full2.set(i);
8360  bvect_min1.set_bit(i);
8361  }
8362  unsigned* first = ids;
8363  unsigned* last = ids + to_add;
8364 
8365  bm::combine_xor(bvect_full1, first, last);
8366 
8367  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
8368 
8369  bm::combine_xor(bvect_full1, first, last);
8370  if (bvect_full1.count())
8371  {
8372  cout << "combine_xor count failed!" << endl;
8373  exit(1);
8374  }
8375 
8376  }
8377 
8378 
8379  {
8380  unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
8381  unsigned to_add = sizeof(ids)/sizeof(unsigned);
8382  bvect bvect_full1;
8383  bvect bvect_full2;
8384  bvect_mini bvect_min1(BITVECT_SIZE);
8385 
8386  bvect_full1.set_new_blocks_strat(bm::BM_BIT);
8387  bvect_full2.set_new_blocks_strat(bm::BM_BIT);
8388 
8389  unsigned bn = 0;
8390  for (unsigned i = 0; i < to_add; ++i)
8391  {
8392  ids[i] = bn;
8393  bvect_full2.set(bn);
8394  bvect_min1.set_bit(bn);
8395  bn += 15;
8396  }
8397 
8398  unsigned* first = ids;
8399  unsigned* last = ids + to_add;
8400 
8401  bm::combine_xor(bvect_full1, first, last);
8402  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
8403 
8404  bm::combine_xor(bvect_full1, first, last);
8405  if (bvect_full1.count())
8406  {
8407  cout << "combine_xor count failed!" << endl;
8408  exit(1);
8409  }
8410  }
8411 
8412 
8413  {
8414  unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
8415  unsigned to_add = sizeof(ids)/sizeof(unsigned);
8416  bvect bvect_full1;
8417  bvect bvect_full2;
8418  bvect_mini bvect_min1(BITVECT_SIZE);
8419 
8420  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
8421  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
8422 
8423  unsigned bn = 0;
8424  for (unsigned i = 0; i < to_add; ++i)
8425  {
8426  ids[i] = bn;
8427  bvect_full2.set(bn);
8428  bvect_min1.set_bit(bn);
8429  bn += 15;
8430  }
8431 
8432  unsigned* first = ids;
8433  unsigned* last = ids + to_add;
8434 
8435  bm::combine_xor(bvect_full1, first, last);
8436  CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
8437 
8438  bm::combine_xor(bvect_full1, first, last);
8439  if (bvect_full1.count())
8440  {
8441  cout << "combine_xor count failed!" << endl;
8442  exit(1);
8443  }
8444  }
8445 
8446 
8447  // ------------------------------------------
8448  // 2-way XOR
8449  //
8450  {
8451  bvect bv1 { 0, 1 };
8452  bvect bv2;
8453  bv2.bit_xor(bv1, bv2, bvect::opt_compress);
8454  int cmp = bv1.compare(bv2);
8455  assert(cmp == 0);
8456  }
8457 
8458  {
8459  bvect bv1 { 0, 1 };
8460  bvect bv2 { 2, 3 };
8461  bvect bv1c(bv1);
8462  bv1c.bit_xor(bv2);
8463 
8464  bvect bv;
8465  bv.bit_xor(bv1, bv2, bvect::opt_compress);
8466  int cmp = bv.compare(bv1c);
8467  assert(cmp == 0);
8468  struct bvect::statistics st1;
8469  bv.calc_stat(&st1);
8470  assert(!st1.bit_blocks);
8471  assert(st1.gap_blocks == 1);
8472  }
8473 
8474  {
8475  bvect bv1;
8476  bvect bv2;
8477  for (unsigned i = 2; i < 65536; ++i)
8478  {
8479  bv1.set(i);
8480  bv2.set(i);
8481  }
8482 
8483  bvect bv1c(bv1);
8484  bv1c.bit_xor(bv2);
8485 
8486  bvect bv;
8487  bv.bit_xor(bv1, bv2, bvect::opt_none); // should detect 0 automatically
8488  int cmp = bv.compare(bv1c);
8489  assert(cmp == 0);
8490  struct bvect::statistics st1;
8491  bv.calc_stat(&st1);
8492  assert(!st1.bit_blocks);
8493  assert(!st1.gap_blocks);
8494  }
8495 
8496  {
8497  bvect bv1 { 0, 1 };
8498  bvect bv2 { 1 };
8499  bv1.clear(0); bv1.clear(1);
8500  bv2.clear(1);
8501 
8502  bvect bv;
8503  bv.bit_xor(bv1, bv2, bvect::opt_none); // should detect FULL automatically
8504 
8505  struct bvect::statistics st1;
8506  bv.calc_stat(&st1);
8507  assert(!st1.bit_blocks);
8508  assert(!st1.gap_blocks);
8509  assert(!st1.ptr_sub_blocks);
8510  }
8511 
8512 
8513 
8514  {
8515  bvect bv1 { 0, 1 };
8516  bvect bv2 { 2, 3 };
8517  bv2.optimize();
8518  bvect bv1c(bv1);
8519  bv1c.bit_xor(bv2);
8520 
8521  {
8522  bvect bv;
8523  bv.bit_xor(bv1, bv2, bvect::opt_compress);
8524  int cmp = bv.compare(bv1c);
8525  if (cmp != 0)
8526  {
8527  DetailedCompareBVectors(bv, bv1c);
8528  }
8529  assert(cmp == 0);
8530  }
8531  bv1.optimize();
8532  {
8533  bvect bv;
8534  bv.bit_xor(bv1, bv2, bvect::opt_compress);
8535  int cmp = bv.compare(bv1c);
8536  assert(cmp == 0);
8537  }
8538  bv1.clear();
8539  bv2.clear();
8540  bv2.invert();
8541  {
8542  bvect bv;
8543  bv.bit_xor(bv1, bv2, bvect::opt_compress);
8544  int cmp = bv.compare(bv2);
8545  assert(cmp == 0);
8546  }
8547  }
8548 
8549  {
8550  bvect bvect_full1;
8551  bvect bvect_full2;
8552  bvect_full1.invert();
8553  bvect_full2.set();
8554 
8555  {
8556  bvect bv_target_s;
8557  bv_target_s.bit_xor(bvect_full1, bvect_full2, bvect::opt_none);
8558  auto b = bv_target_s.none();
8559  assert(b);
8560  }
8561  }
8562 
8563  {
8564  bvect bv1;
8565  bvect bv2;
8566  bv1.invert();
8567  bv2.set();
8568  bv2.set(bm::id_max/2, false);
8569 
8570  {
8571  bvect bv_s;
8572  bv_s.bit_xor(bv1, bv2, bvect::opt_none);
8573  auto cnt = bv_s.count();
8574  assert(cnt == 1);
8575  assert(bv_s.test(bm::id_max/2));
8576  }
8577  {
8578  bvect bv_s;
8579  bv_s.bit_xor(bv2, bv1, bvect::opt_none);
8580  auto cnt = bv_s.count();
8581  assert(cnt == 1);
8582  assert(bv_s.test(bm::id_max/2));
8583  }
8584  bv1.bit_xor(bv2);
8585  auto cnt = bv1.count();
8586  assert(cnt == 1);
8587  assert(bv1.test(bm::id_max/2));
8588  }
8589 
8590 }
8591 
8592 
8593 static
8594 void GenerateRandomKleenVect(bvect& bv_v, bvect& bv_null,
8596  size_t sparse_factor = 0)
8597 {
8598  int v = -1;
8599  ++sparse_factor;
8600  for (bvect::size_type i = 0; i < size; i += bvect::size_type(sparse_factor))
8601  {
8602  bm::set_value_kleene(bv_v, bv_null, i , v);
8603  if (rand()&1)
8604  if (++v > 1) v = -1;
8605  } // for
8606  if (sparse_factor > 1)
8607  {
8608  bv_v.optimize();
8609  bv_null.optimize();
8610  }
8611 }
8612 
8613 
8614 
8615 static
8616 void KleeneLogicAndStressTest(unsigned repeats = 150, unsigned size = 1000000)
8617 {
8618  cout << "-------------------------------------- KleenLogicAndStressTest()" << endl;
8619 
8620  for (unsigned i = 0; i < repeats; ++i)
8621  {
8622  size_t sparse_factor = i & 1 ? 1024 : 0;
8623 
8624  {
8625  bvect bv_v1, bv_null1;
8626  bvect bv_v2, bv_null2;
8627  GenerateRandomKleenVect(bv_v1, bv_null1, size, sparse_factor);
8628  GenerateRandomKleenVect(bv_v2, bv_null2, size, sparse_factor);
8629  bvect bv_v3(bv_v1), bv_null3(bv_null1);
8630 
8631 
8632  bvect bv_v_t, bv_null_t;
8633  bm::and_kleene(bv_v_t, bv_null_t, bv_v1, bv_null1, bv_v2, bv_null2);
8634 
8635  bm::and_kleene(bv_v1, bv_null1, bv_v2, bv_null2);
8636 
8637  for (unsigned k = 0; k < size; ++k)
8638  {
8639  int v, v_t;
8640  v = bm::get_value_kleene(bv_v1, bv_null1, k);
8641  v_t = bm::get_value_kleene(bv_v_t, bv_null_t, k);
8642  assert(v == v_t);
8643 
8644  int a, b;
8645  a = bm::get_value_kleene(bv_v3, bv_null3, k);
8646  b = bm::get_value_kleene(bv_v2, bv_null2, k);
8647 
8648  int control = bm::and_values_kleene(a, b);
8649  assert(control == v);
8650 
8651  } // for
8652  if (i % 16 == 0)
8653  {
8654  if (!is_silent)
8655  cout << "\r" << i << " of " << repeats << flush;
8656  }
8657  }
8658  } // for
8659  cout << "\n-------------------------------------- KleenLogicAndStressTest() OK" << endl;
8660 }
8661 
8662 static
8663 void KleeneLogicOrStressTest(unsigned repeats = 150, unsigned size = 1000000)
8664 {
8665  cout << "-------------------------------------- KleeneLogicOrStressTest()" << endl;
8666 
8667  for (unsigned i = 0; i < repeats; ++i)
8668  {
8669  size_t sparse_factor = i & 1 ? 1024 : 0;
8670  {
8671  bvect bv_v1, bv_null1;
8672  bvect bv_v2, bv_null2;
8673  GenerateRandomKleenVect(bv_v1, bv_null1, size, sparse_factor);
8674  GenerateRandomKleenVect(bv_v2, bv_null2, size, sparse_factor);
8675  bvect bv_v3(bv_v1), bv_null3(bv_null1);
8676 
8677  bvect bv_v_t, bv_null_t;
8678  bm::or_kleene(bv_v_t, bv_null_t, bv_v1, bv_null1, bv_v2, bv_null2);
8679 
8680  bm::or_kleene(bv_v1, bv_null1, bv_v2, bv_null2);
8681 
8682  for (unsigned k = 0; k < size; ++k)
8683  {
8684  int v;
8685  v = bm::get_value_kleene(bv_v1, bv_null1, k);
8686  int v_t;
8687  v_t = bm::get_value_kleene(bv_v_t, bv_null_t, k);
8688 
8689  int a, b;
8690  a = bm::get_value_kleene(bv_v3, bv_null3, k);
8691  b = bm::get_value_kleene(bv_v2, bv_null2, k);
8692 
8693  int control = bm::or_values_kleene(a, b);
8694  assert(control == v);
8695  assert(v == v_t);
8696 
8697  } // for
8698  if (!is_silent)
8699  if (i % 16 == 0)
8700  {
8701  cout << "\r" << i << " of " << repeats << flush;
8702  }
8703  }
8704  } // for
8705  cout << "\n-------------------------------------- KleeneLogicOrStressTest() OK" << endl;
8706 }
8707 
8708 
8709 static
8711 {
8712  cout << "-------------------------------------- KleeneLogicTest()" << endl;
8713 
8714  cout << " basic" << endl;
8715  bool b;
8716  {
8717  bvect bv_v { 10, 20, 30, bm::id_max/2, bm::id_max-1 };
8718  bvect bv_nnull { 10, 20, 25, bm::id_max/2 };
8719 
8720  bm::init_kleene(bv_v, bv_nnull);
8721 
8722 
8723  b = bv_v.test(30);
8724  assert(!b);
8725  b = bv_v.test(bm::id_max-1);
8726  assert(!b);
8727  auto cnt = bv_v.count();
8728  assert(cnt == 3);
8729 
8730  int v;
8731  v = bm::get_value_kleene(bv_v, bv_nnull, 0);
8732  assert(v == 0);
8733 
8734 
8735  bm::invert_kleene(bv_v, bv_nnull);
8736  cnt = bv_v.count();
8737  assert(cnt == 1);
8738  b = bv_v.test(25);
8739  assert(b);
8740  }
8741 
8742  cout << " set/get value kleene" << endl;
8743  {
8744  bvect bv_v;
8745  bvect bv_nnull;
8746 
8747  int v = 0;
8748  for (unsigned i = 0; i < 128000; ++i)
8749  {
8750  bm::set_value_kleene(bv_v, bv_nnull, i, v);
8751  auto v1 = bm::get_value_kleene(bv_v, bv_nnull, i);
8752  assert(v == v1);
8753  v += 1;
8754  if (v > 1)
8755  v = -1;
8756  }
8757  }
8758 
8759  cout << " OR kleene" << endl;
8760  {
8761  bvect bv_v1 { 10, 20, 30, bm::id_max/2, bm::id_max-1 };
8762  bvect bv_nnull1 { 10, 20, 25, bm::id_max/2 };
8763 
8764  bm::init_kleene(bv_v1, bv_nnull1);
8765 
8766  bvect bv_v2 { 11, 20, 30, bm::id_max/2, bm::id_max-1 };
8767  bvect bv_nnull2 { 11, 20, 25, bm::id_max/2 };
8768 
8769  bm::init_kleene(bv_v2, bv_nnull2);
8770  int v;
8771  v = bm::get_value_kleene(bv_v1, bv_nnull1, 11);
8772  assert(v==0);
8773 
8774  bvect bv_v_t, bv_null_t;
8775  bm::or_kleene(bv_v_t, bv_null_t, bv_v1, bv_nnull1, bv_v2, bv_nnull2);
8776 
8777  bm::or_kleene(bv_v1, bv_nnull1, bv_v2, bv_nnull2);
8778 
8779  {
8780  b = bv_v1.equal(bv_v_t);
8781  assert(b);
8782  b = bv_nnull1.equal(bv_null_t);
8783  assert(b);
8784  }
8785 
8786  auto cnt = bv_v1.count();
8787  assert(cnt == 4);
8788 
8789  v = bm::get_value_kleene(bv_v1, bv_nnull1, 11);
8790  assert(v == 1);
8791  v = bm::get_value_kleene(bv_v1, bv_nnull1, 25);
8792  assert(v == -1);
8793  }
8794  {
8795  bvect bv_v1, bv_v2, bv_n1, bv_n2;
8796 
8797  bm::set_value_kleene(bv_v1, bv_n1, 10, -1);
8798  bm::set_value_kleene(bv_v2, bv_n2, 10, 0);
8799 
8800  bvect bv_v_t, bv_null_t;
8801  bm::or_kleene(bv_v_t, bv_null_t, bv_v1, bv_n1, bv_v2, bv_n2);
8802 
8803  bm::or_kleene(bv_v1, bv_n1, bv_v2, bv_n2);
8804 
8805  b = bv_v1.equal(bv_v_t);
8806  assert(b);
8807  b = bv_n1.equal(bv_null_t);
8808  assert(b);
8809 
8810  int v;
8811  v = bm::get_value_kleene(bv_v1, bv_n1, 10);
8812  assert(v == 0);
8813 
8814  bm::set_value_kleene(bv_v1, bv_n1, 10, 0);
8815  bm::set_value_kleene(bv_v2, bv_n2, 10, -1);
8816 
8817  bm::or_kleene(bv_v1, bv_n1, bv_v2, bv_n2);
8818 
8819  v = bm::get_value_kleene(bv_v1, bv_n1, 10);
8820  assert(v == 0);
8821  }
8822 
8823  for (unsigned r = 0; r < 2; ++r)
8824  {
8825  bvect bv_v1, bv_v2, bv_n1, bv_n2;
8826 
8827  bm::set_value_kleene(bv_v1, bv_n1, 10, 1);
8828  bm::set_value_kleene(bv_v2, bv_n2, 10, 1);
8829  bm::set_value_kleene(bv_v1, bv_n1, 10, -1);
8830  bm::set_value_kleene(bv_v2, bv_n2, 10, -1);
8831 
8832  if (r)
8833  {
8834  bv_v1.optimize(); bv_v2.optimize();
8835  bv_n1.optimize(); bv_n2.optimize();
8836  }
8837  bvect bv_v_t, bv_null_t;
8838  bm::or_kleene(bv_v_t, bv_null_t, bv_v1, bv_n1, bv_v2, bv_n2);
8839  bv_v_t.optimize();
8840  bv_null_t.optimize();
8841 
8842  bm::or_kleene(bv_v1, bv_n1, bv_v2, bv_n2);
8843 
8844  {
8845  b = bv_v1.equal(bv_v_t);
8846  assert(b);
8847  b = bv_n1.equal(bv_null_t);
8848  assert(b);
8849  }
8850 
8851  int v;
8852  v = bm::get_value_kleene(bv_v1, bv_n1, 10);
8853  assert(v == -1);
8854  }
8855 
8856 
8857  cout << " AND kleene" << endl;
8858  {
8859  assert(bm::and_values_kleene(0, 0) == 0);
8860  assert(bm::and_values_kleene(0, 1) == 0);
8861  assert(bm::and_values_kleene(1, 0) == 0);
8862 
8863  assert(bm::and_values_kleene(1, 1) == 1);
8864  assert(bm::and_values_kleene(1, 0) == 0);
8865  assert(bm::and_values_kleene(0, 1) == 0);
8866 
8867  assert(bm::and_values_kleene(-1, -1) == -1);
8868  assert(bm::and_values_kleene(-1, 0) == -1);
8869  assert(bm::and_values_kleene(0, -1) == -1);
8870 
8871  }
8872  {
8873  bvect bv_v1 { 10, 20, 30, bm::id_max/2, bm::id_max-1 };
8874  bvect bv_nnull1 { 10, 20, 25, bm::id_max/2 };
8875 
8876  bm::init_kleene(bv_v1, bv_nnull1);
8877 
8878  bvect bv_v2(bv_v1);
8879  bvect bv_nnull2(bv_nnull1);
8880 
8881  bvect bv_ref_v1(bv_v1);
8882  bvect bv_ref_nnull1(bv_nnull1);
8883 
8884  bvect bv_v_t, bv_null_t;
8885  bm::and_kleene(bv_v_t, bv_null_t, bv_v1, bv_nnull1, bv_v2, bv_nnull2);
8886 
8887  bm::and_kleene(bv_v1, bv_nnull1, bv_v2, bv_nnull2);
8888  b = bv_v1.equal(bv_ref_v1);
8889  assert(b);
8890  b = bv_nnull1.equal(bv_ref_nnull1);
8891  assert(b);
8892 
8893  b = bv_v1.equal(bv_v_t);
8894  assert(b);
8895  b = bv_nnull1.equal(bv_null_t);
8896  assert(b);
8897 
8898  }
8899 
8900  {
8901  bvect bv_v1, bv_v2, bv_n1, bv_n2;
8902 
8903  bm::set_value_kleene(bv_v1, bv_n1, 10, -1);
8904  bm::set_value_kleene(bv_v2, bv_n2, 10, -1);
8905 
8906  bm::and_kleene(bv_v1, bv_n1, bv_v2, bv_n2);
8907  int v = bm::get_value_kleene(bv_v1, bv_n1, 10);
8908  assert(v == -1);
8909  bm::set_value_kleene(bv_v1, bv_n1, 100, -1);
8910 
8911  bm::and_kleene(bv_v1, bv_n1, bv_v2, bv_n2);
8912  v = bm::get_value_kleene(bv_v1, bv_n1, 100);
8913  assert(v == -1);
8914 
8915  bm::set_value_kleene(bv_v1, bv_n1, 100, 1);
8916  bm::set_value_kleene(bv_v2, bv_n2, 101, 1);
8917 
8918  bvect bv_v_t, bv_null_t;
8919  bm::and_kleene(bv_v_t, bv_null_t, bv_v1, bv_n1, bv_v2, bv_n2);
8920 
8921  bm::and_kleene(bv_v1, bv_n1, bv_v2, bv_n2);
8922  v = bm::get_value_kleene(bv_v1, bv_n1, 100);
8923  assert(v == 0);
8924  v = bm::get_value_kleene(bv_v1, bv_n1, 101);
8925  assert(v == 0);
8926 
8927  b = bv_v1.equal(bv_v_t);
8928  assert(b);
8929  b = bv_n1.equal(bv_null_t);
8930  assert(b);
8931 
8932  }
8933 
8934  cout << "-------------------------------------- KleeneLogicTest() OK" << endl;
8935 }
8936 
8937 
8938 static
8940 {
8941  cout << "-------------------------------------- ComparisonTest" << endl;
8942 
8943  bvect_mini bvect_min1(BITVECT_SIZE);
8944  bvect_mini bvect_min2(BITVECT_SIZE);
8945  bvect bvect_full1;
8946  bvect bvect_full2;
8947  int res1, res2;
8948 
8949  bvect_full1.set_bit(31);
8950  bvect_full2.set_bit(63);
8951 
8952  res1 = bvect_full1.compare(bvect_full2);
8953  if (res1 != 1)
8954  {
8955  printf("Comparison test failed 1\n");
8956  exit(1);
8957  }
8958 
8959  bvect_full1.clear();
8960  bvect_full2.clear();
8961 
8962  bvect_min1.set_bit(10);
8963  bvect_min2.set_bit(10);
8964 
8965  bvect_full1.set_bit(10);
8966  bvect_full2.set_bit(10);
8967 
8968  res1 = bvect_min1.compare(bvect_min2);
8969  res2 = bvect_full1.compare(bvect_full2);
8970 
8971  if (res1 != res2)
8972  {
8973  printf("Comparison test failed 1\n");
8974  exit(1);
8975  }
8976 
8977  printf("Comparison 2.\n");
8978 
8979  bvect_min1.set_bit(11);
8980  bvect_full1.set_bit(11);
8981 
8982  res1 = bvect_min1.compare(bvect_min2);
8983  res2 = bvect_full1.compare(bvect_full2);
8984 
8985  if (res1 != res2 && res1 != 1)
8986  {
8987  printf("Comparison test failed 2\n");
8988  exit(1);
8989  }
8990 
8991  res1 = bvect_min2.compare(bvect_min1);
8992  res2 = bvect_full2.compare(bvect_full1);
8993 
8994  if (res1 != res2 && res1 != -1)
8995  {
8996  printf("Comparison test failed 2.1\n");
8997  exit(1);
8998  }
8999 
9000  printf("Comparison 3.\n");
9001 
9003  bvect_full1.optimize(tb);
9004 
9005  res1 = bvect_min1.compare(bvect_min2);
9006  res2 = bvect_full1.compare(bvect_full2);
9007 
9008  if (res1 != res2 && res1 != 1)
9009  {
9010  printf("Comparison test failed 3\n");
9011  exit(1);
9012  }
9013 
9014  res1 = bvect_min2.compare(bvect_min1);
9015  res2 = bvect_full2.compare(bvect_full1);
9016 
9017  if (res1 != res2 && res1 != -1)
9018  {
9019  printf("Comparison test failed 3.1\n");
9020  exit(1);
9021  }
9022 
9023  printf("Comparison 4.\n");
9024 
9025  bvect_full2.optimize();
9026 
9027  res1 = bvect_min1.compare(bvect_min2);
9028  res2 = bvect_full1.compare(bvect_full2);
9029 
9030  if (res1 != res2 && res1 != 1)
9031  {
9032  printf("Comparison test failed 4\n");
9033  exit(1);
9034  }
9035 
9036  res1 = bvect_min2.compare(bvect_min1);
9037  res2 = bvect_full2.compare(bvect_full1);
9038 
9039  if (res1 != res2 && res1 != -1)
9040  {
9041  printf("Comparison test failed 4.1\n");
9042  exit(1);
9043  }
9044 
9045  printf("Comparison 5.\n");
9046 
9047  unsigned i;
9048  for (i = 0; i < 65536; ++i)
9049  {
9050  bvect_full1.set_bit(i);
9051  }
9052 
9053  res1 = bvect_min1.compare(bvect_min2);
9054  res2 = bvect_full1.compare(bvect_full2);
9055 
9056  if (res1 != res2 && res1 != 1)
9057  {
9058  printf("Comparison test failed 5\n");
9059  exit(1);
9060  }
9061 
9062  bvect_full1.optimize();
9063 
9064  res1 = bvect_min2.compare(bvect_min1);
9065  res2 = bvect_full2.compare(bvect_full1);
9066 
9067  if (res1 != res2 && res1 != -1)
9068  {
9069  printf("Comparison test failed 5.1\n");
9070  exit(1);
9071  }
9072 
9073 }
9074 
9075 
9076 static
9078 {
9079  cout << "-------------------------------------- BvectorFindFirstDiffTest" << endl;
9080 
9081  // empty test
9082  {
9083  bvect bv1, bv2;
9084  TestFindDiff(bv1, bv2);
9085 
9086  bv1.set(0);
9087  TestFindDiff(bv1, bv2);
9088  bv2.set(0);
9089  TestFindDiff(bv1, bv2);
9090  }
9091  // test GAP bits
9092  {
9093  bvect bv1(bm::BM_GAP), bv2(bm::BM_GAP);
9094 
9095  bv1.set_range(10, 15);
9096  bv2.set_range(10, 15);
9097 
9098  TestFindDiff(bv1, bv2);
9099  }
9100  {
9101  bvect bv1(bm::BM_GAP), bv2(bm::BM_GAP);
9102 
9103  bv1.set_range(10, 15);
9104  bv2.set_range(10, 12);
9105 
9106  TestFindDiff(bv1, bv2);
9107  }
9108  {
9109  bvect bv1(bm::BM_GAP), bv2(bm::BM_GAP);
9110 
9111  bv1.set_range(bm::id_max32/2 - 10, bm::id_max32/2 + 15);
9112  bv2.set_range(bm::id_max32/2 - 10, bm::id_max32/2 + 12);
9113 
9114  TestFindDiff(bv1, bv2);
9115  }
9116  // test GAP-bit mix
9117  {
9118  bvect bv1(bm::BM_GAP), bv2;
9119 
9120  bv1.set_range(10, 15);
9121  bv2.set_range(10, 12);
9122 
9123  TestFindDiff(bv1, bv2);
9124  }
9125  {
9126  bvect bv1(bm::BM_GAP), bv2;
9127 
9128  bv1.set_range(bm::id_max32/2 - 10, bm::id_max32/2 + 15);
9129  bv2.set_range(bm::id_max32/2 - 10, bm::id_max32/2 + 12);
9130 
9131  TestFindDiff(bv1, bv2);
9132  }
9133 
9134  // test inverted
9135  {
9136  bvect bv1, bv2;
9137 
9138  bv1.invert();
9139  TestFindDiff(bv1, bv2);
9140  bv2.invert();
9141  TestFindDiff(bv1, bv2);
9142  bv2[123456] = false;
9143  TestFindDiff(bv1, bv2);
9144  }
9145 
9146 
9147  // test bits far
9148  {
9149  bvect bv1, bv2;
9150  bv1.set(bm::id_max32/2);
9151  TestFindDiff(bv1, bv2);
9152  bv2.set(bm::id_max32/2);
9153  TestFindDiff(bv1, bv2);
9154  bv2.set(bm::id_max32/2+1);
9155  TestFindDiff(bv1, bv2);
9156  bv1.optimize();
9157  TestFindDiff(bv1, bv2);
9158  bv2.optimize();
9159  TestFindDiff(bv1, bv2);
9160  }
9161  {
9162  bvect bv1, bv2;
9163  bv1.set(bm::id_max-1);
9164  TestFindDiff(bv1, bv2);
9165  bv1.optimize();
9166  TestFindDiff(bv1, bv2);
9167  bv2.set(bm::id_max-1);
9168  TestFindDiff(bv1, bv2);
9169  bv2.optimize();
9170  TestFindDiff(bv1, bv2);
9171  }
9172 
9173  // test FULL blocks
9174  {
9175  bvect bv1, bv2;
9176  bv1.set_range(0, bm::id_max32/2);
9177  TestFindDiff(bv1, bv2);
9178  bv2.set_range(0, bm::id_max32/2);
9179  TestFindDiff(bv1, bv2);
9180 
9181  bv1[bm::id_max32/2 - 100] = false;
9182  TestFindDiff(bv1, bv2);
9183  bv1.optimize();
9184  TestFindDiff(bv1, bv2);
9185 
9186  bv2[bm::id_max32/2 - 100] = false;
9187  TestFindDiff(bv1, bv2);
9188  bv2.optimize();
9189  TestFindDiff(bv1, bv2);
9190  }
9191 
9192  cout << "-------------------------------------- BvectorFindFirstDiffTest OK" << endl;
9193 }
9194 
9195 
9196 static
9198 {
9199  cout << "-------------------------------------- RankRangeSplitTest" << endl;
9200 
9201  std::vector<std::pair<bvect::size_type, bvect::size_type>> pair_vect;
9202  {
9203  bvect bv;
9204  bm::rank_range_split(bv, 2, pair_vect);
9205  assert(pair_vect.size()==0);
9206  }
9207 
9208  {
9209  bvect bv { 1, 2, 10, 100, 200 };
9210  bm::rank_range_split(bv, 2, pair_vect);
9211  assert(pair_vect.size()==3);
9212 
9213  assert(pair_vect[0].first == 1);
9214  assert(pair_vect[0].second == 2);
9215 
9216  assert(pair_vect[1].first == 3);
9217  assert(pair_vect[1].second == 100);
9218 
9219  assert(pair_vect[2].first == 101);
9220  assert(pair_vect[2].second == 200);
9221  }
9222 
9223  {
9224  bvect bv;
9225 
9226  generate_bvector(bv);
9227  bv.optimize();
9228  auto cnt = bv.count();
9229 
9231  bool b = bv.find_range(first, last);
9232  assert(b);
9233 
9234  for (bvect::size_type i = 1; i < cnt+1; ++i)
9235  {
9236  bm::rank_range_split(bv, i, pair_vect);
9237  assert(pair_vect.size());
9238  assert(pair_vect[0].first == first);
9239  assert(pair_vect[pair_vect.size()-1].second == last);
9240 
9241  bvect::size_type cnt_c = 0;
9242  for (size_t k = 0; k < pair_vect.size(); ++k)
9243  {
9244  auto& p = pair_vect[k];
9245  auto c = bv.count_range(p.first, p.second);
9246  assert(c);
9247  assert(c <= i);
9248  cnt_c += c;
9249  assert(p.first >= first);
9250  assert(p.second <= last);
9251  if (k < pair_vect.size()-1)
9252  {
9253  assert(c == i);
9254  }
9255  } // for k
9256 
9257  assert(cnt == cnt_c);
9258  if (i % 500 == 0)
9259  {
9260  if (!is_silent)
9261  cout << "\r" << i << "/" << cnt << flush;
9262  }
9263  } // for i
9264  }
9265 
9266  cout << "\r-------------------------------------- RankRangeSplitTest OK" << endl;
9267 }
9268 
9269 static
9271 {
9272  bvect bvtotal;
9273  unsigned size = BITVECT_SIZE - 10;
9275 
9276 
9277  bvect bv1;
9278  bvect bv2;
9279  unsigned i;
9280  for (i = 10; i < 165536; i+=2)
9281  {
9282  bv1.set_bit(i);
9283  }
9284 
9285  bv1.optimize(tb);
9286  print_stat(cout, bv1);
9287 
9288  struct bvect::statistics st1;
9289  bv1.calc_stat(&st1);
9290 
9291  std::vector<unsigned char> sermemv(st1.max_serialize_mem);
9292 
9293  size_t slen2 = bm::serialize(bv1, sermemv.data(), tb);
9294  assert(slen2);
9295  slen2 = 0;
9296 
9297  bm::deserialize(bvtotal, sermemv.data());
9298  bvect bv_target_s;
9300  od.deserialize(bv_target_s, sermemv.data(), 0, set_OR);
9301 
9302  bvtotal.optimize(tb);
9303  int res = bvtotal.compare(bv_target_s);
9304  if (res != 0)
9305  {
9306  cout << "Operation deserialization error 1" << endl;
9307  exit(1);
9308  }
9309 
9310  for (i = 55000; i < 165536; ++i)
9311  {
9312  bv2.set_bit(i);
9313  }
9314  bv2.optimize();
9315  print_stat(cout,bv2);
9316 
9317  struct bvect::statistics st2;
9318  bv2.calc_stat(&st2);
9319 
9320  std::vector<unsigned char> sermemv2(st2.max_serialize_mem);
9321 
9322  size_t slen = bm::serialize(bv2, sermemv2.data());
9323  assert(slen);
9324  slen = 0;
9325 
9326  bm::deserialize(bvtotal, sermemv2.data());
9327  print_stat(cout, bvtotal);
9328  od.deserialize(bv_target_s, sermemv2.data(), 0, set_OR);
9329  res = bvtotal.compare(bv_target_s);
9330  if (res != 0)
9331  {
9332  cout << "Operation deserialization error 2" << endl;
9333  assert(0);
9334  exit(1);
9335  }
9336 
9337  bm::deserialize(bvtotal, sermemv2.data());
9338  bm::deserialize(bvtotal, sermemv.data());
9339 
9340  od.deserialize(bv_target_s,
9341  sermemv2.data(),
9342  0,
9343  set_OR);
9344  od.deserialize(bv_target_s,
9345  sermemv2.data(),
9346  0,
9347  set_OR);
9348 
9349  res = bvtotal.compare(bv_target_s);
9350  if (res != 0)
9351  {
9352  cout << "Deserialization test failed! 3" << endl;
9353  exit(1);
9354  }
9355 
9356 
9357  bvtotal.clear();
9358  bv_target_s.clear(false);
9359 
9360  int clcnt = 0;
9361 
9362  unsigned repetitions = 25;
9363  for (i = 0; i < repetitions; ++i)
9364  {
9365  cout << endl << endl << "Deserialization STEP " << i << endl;
9366 
9367  bvect_mini* bvect_min1= new bvect_mini(size);
9368  bvect* bvect_full1= new bvect();
9369 
9370  FillSetsRandomMethod(bvect_min1, bvect_full1, 1, size, 1);
9371 
9372  struct bvect::statistics st;
9373  bvect_full1->calc_stat(&st);
9374 
9375  std::vector<unsigned char> sermemv1(st.max_serialize_mem);
9376  slen = bm::serialize(*bvect_full1, sermemv1.data(), tb);
9377 
9378  std::vector<unsigned char> smemv(slen);
9379  ::memcpy(smemv.data(), sermemv1.data(), slen);
9380 
9381  bm::deserialize(bvtotal, smemv.data());
9382 
9383  {
9384  bvect bv_c;
9385  bm::deserialize(bv_c, smemv.data());
9386  res = bv_c.compare(*bvect_full1);
9387  assert(res == 0);
9388 
9389  bvect bv3;
9390  od.deserialize(bv3,
9391  smemv.data(),
9392  0,
9393  set_OR);
9394  res = bv3.compare(*bvect_full1);
9395  assert(res == 0);
9396  }
9397 
9398  od.deserialize(bv_target_s,
9399  smemv.data(),
9400  0,
9401  set_OR);
9402  res = bvtotal.compare(bv_target_s);
9403  if (res != 0)
9404  {
9405  res = bvtotal.compare(bv_target_s);
9406 
9407  unsigned bit_idx = bv_target_s.get_first();
9408  cout << bit_idx << " " << bv_target_s.get_next(bit_idx) << endl;;
9409  print_stat(cout,*bvect_full1);
9410  print_stat(cout,bv_target_s);
9411  cout << "Operation deserialization error 2" << endl;
9412  assert(0); exit(1);
9413  }
9414 
9415  bvtotal.optimize(tb);
9416  bv_target_s.optimize(tb);
9417 
9418  if (++clcnt == 5)
9419  {
9420  clcnt = 0;
9421  bvtotal.clear();
9422  bv_target_s.clear();
9423  }
9424 
9425  delete bvect_min1;
9426  delete bvect_full1;
9427 
9428  } // for i
9429 
9430 }
9431 
9432 // ---------------------------------------------------------------------------
9433 
9434 static
9435 void CheckRangeDeserial(const bvect& bv,
9436  bvect::size_type from,
9437  bvect::size_type to)
9438 {
9439  static unsigned bm_distance = 4;
9440  assert(from < to);
9441 
9442  cout << " Check Range [" << from << ", " << to << "] = " << (to-from) << endl;
9443 
9444  int max_inc = 256;
9445  if (to - from > 65536)
9446  max_inc = 1024;
9447 
9448 
9449  bool eq;
9451 
9453  bvs.set_bookmarks(false);
9454  //bvs.set_bookmarks(true, bm_distance++);
9455 
9456  cout << " bookmarks OFF" << endl;
9457 
9458  for (unsigned pass = 0; pass < 2; ++pass)
9459  {
9460  cout << " pass = " << pass << endl;
9462  cout << " serialize ..." << flush;
9463  bvs.serialize(bv, buf);
9464  cout << "OK" << endl;
9465 
9466  bvect bv_r;
9467  bv_r.copy_range(bv, from, to);
9468  auto count_r = bv.count_range(from, to);
9469  auto count = bv_r.count();
9470  assert(count == count_r);
9471 
9472  {
9473  bvect bv_c;
9474  bm::deserialize(bv_c, buf.data());
9475  eq = bv.equal(bv_c);
9476  assert(eq);
9477  }
9478 
9479  {
9480  bvect bv_x;
9481  bv_x.bit_xor(bv, bv_r, bvect::opt_compress);
9482  count_r = bv_x.count_range(from, to);
9483  assert(!count_r);
9484  }
9485 
9486  const unsigned char* sdata = buf.data();
9487 
9488  {
9489 
9490  bvect bv_rd_m;
9491  bv_rd_m.set_range(from, to);
9492  od.deserialize(bv_rd_m, sdata, 0, bm::set_AND);
9493  eq = bv_r.equal(bv_rd_m);
9494  assert(eq);
9495 
9496  bvect bv_rd;
9497  od.deserialize_range(bv_rd, sdata, from, to);
9498  eq = bv_r.equal(bv_rd);
9499  assert(eq);
9500  }
9501 
9502  bvect::size_type cnt = 0;
9503 
9504  cout << " start range" << endl;
9505  {
9506  bvect::size_type target = from + 65536 * 2;
9507  if (target > to)
9508  target = to;
9509 
9510  bvect bv_rd_m;
9511  bv_rd_m.set_range(from, target);
9512  for (bvect::size_type i = from; i <= target; ++cnt)
9513  {
9514  bv_r.copy_range(bv, i, target);
9515  bvect bv_rd;
9516  od.deserialize_range(bv_rd, sdata, i, target);
9517  eq = bv_r.equal(bv_rd);
9518  assert(eq);
9519 
9520  od.deserialize(bv_rd_m, sdata, 0, bm::set_AND);
9521  eq = bv_rd.equal(bv_rd_m);
9522  assert(eq);
9523 
9524  {
9525  bvect bv_rd2;
9526  bm::deserialize_range(bv_rd2, sdata, i, target);
9527  eq = bv_rd.equal(bv_rd2);
9528  assert(eq);
9529  }
9530 
9531  auto r = target - i;
9532  if (!is_silent)
9533  cout << "\r " << r << " " << flush;
9534  if (cnt > 128 && (target - i) > 128)
9535  {
9536  {
9537  i += bvect::size_type(rand() % max_inc);
9538  target -= bvect::size_type(rand() % max_inc);
9539  }
9540  bv_rd_m.keep_range(i, target);
9541  continue;
9542  }
9543  else
9544  {
9545  bv_rd_m.set(i, false);
9546  bv_rd_m.set(target, false);
9547  }
9548  ++i; --target;
9549  } // for i
9550  }
9551 
9552  cout << "\r " << endl;
9553  cout << " whole range (randomized)" << endl;
9554 
9555  cnt = 0;
9556  bvect::size_type j = to;
9557 
9558  for (bvect::size_type i = from; i <= j; ++i, --j, ++cnt)
9559  {
9560  bv_r.copy_range(bv, i, j);
9561  bvect bv_rd;
9562  od.deserialize_range(bv_rd, buf.data(), i, j);
9563  eq = bv_r.equal(bv_rd);
9564  assert(eq);
9565 
9566  bvect bv_rd_m;
9567  bv_rd_m.set_range(i, j);
9568  od.deserialize(bv_rd_m, buf.data(), 0, bm::set_AND);
9569  eq = bv_rd.equal(bv_rd_m);
9570  assert(eq);
9571 
9572  auto r = j - i;
9573  if (!is_silent)
9574  cout << "\r " << r << " " << flush;
9575  // turn on random gallop mode
9576  if (cnt > 100)
9577  {
9578  {
9579  i = bvect::size_type(i + unsigned(rand() % max_inc));
9580  j = bvect::size_type(j - unsigned(rand() % max_inc));
9581  }
9582  }
9583  } // for i-j
9584 
9585  bvs.set_bookmarks(true, bm_distance++);
9586  cout << "\n bookmarks ON distance=" << (bm_distance-1) << endl;
9587 
9588  } // for pass (bookmarks)
9589 
9590  cout << "\r " << endl;
9591 
9592 }
9593 
9594 static
9596  bvect::size_type to,
9597  bvect::size_type step = 65536 / 10)
9598 {
9599  for (bvect::size_type i = from; true; i += step)
9600  {
9601  bv.set(i);
9602  if (to - step < i)
9603  break;
9604  }
9605 }
9606 
9607 
9608 static
9610 {
9611  cout << "\n------------------------------- RangeDeserializationTest()" << endl;
9612 
9613  cout << "============ BV sparse vector" << endl;
9614  {
9615  std::vector<std::pair<bvect::size_type, bvect::size_type> > ranges;
9616 
9617  ranges.push_back(std::make_pair(0, 65535 * 255));
9618  ranges.push_back(std::make_pair(0, 65535 * 255 * 3));
9619  ranges.push_back(std::make_pair(65535 * 5, 65535 * 255 * 2));
9620  ranges.push_back(std::make_pair(65535 * 255 / 2, 65535 * 255));
9621  ranges.push_back(std::make_pair(65535 * 255 / 2, 65535 * 255 * 2));
9622  ranges.push_back(std::make_pair(bm::id_max / 2 - 65535 * 255 / 2, bm::id_max / 2 + 65535 * 255 * 2));
9623  ranges.push_back(std::make_pair(bm::id_max / 2, bm::id_max / 2 + 65535 * 255 * 2));
9624  ranges.push_back(std::make_pair(bm::id_max - 65535 * 255 * 2, bm::id_max - 1));
9625 
9626  for (size_t k = 0; k < ranges.size(); ++k)
9627  {
9628  bvect bv; // generated random
9629 
9630  bvect::size_type from = ranges[k].first;
9631  bvect::size_type to = ranges[k].second;
9632  std::cout << "- Vector range [" << from << ", " << to << "]" << std::endl;
9633 
9634  generate_sparse_bv(bv, from, to, 65536 / 10);
9635 
9636  CheckRangeDeserial(bv, to - 65536, to);
9637  CheckRangeDeserial(bv, from, from + 65536);
9638  auto mid = (to - from) / 2;
9639  CheckRangeDeserial(bv, mid - 100, mid + 100);
9640  }
9641  }
9642 
9643 
9644  cout << "======= BV Empty " << endl;
9645  {
9646  bvect bv_e;
9647  CheckRangeDeserial(bv_e, 0, 256*65536);
9648  CheckRangeDeserial(bv_e, bm::id_max32/4-(256*65536), bm::id_max32/4);
9649  }
9650 
9651  // inverted
9652  cout << "======= BV inverted " << endl;
9653  {
9654  bvect bv_i; // inverted
9655  bv_i.invert();
9656  CheckRangeDeserial(bv_i, 0, 256*65536);
9657  CheckRangeDeserial(bv_i, bm::id_max32/4-(256*65536), bm::id_max32/4);
9658  }
9659 
9660 
9661  // generated random
9662  cout << "======= BV random generated " << endl;
9663  {
9664  bvect bv1; // generated random
9665 
9666  generate_bvector(bv1, bm::id_max32/4, false);
9667  CheckRangeDeserial(bv1, 0, 5*65536);
9668  CheckRangeDeserial(bv1, bm::id_max32/4-(8*65536), bm::id_max32/4);
9669 
9670  bv1.optimize();
9671  CheckRangeDeserial(bv1, 128*65536, 130*65536);
9672  CheckRangeDeserial(bv1, bm::id_max32/4-(2*65536), bm::id_max32/4);
9673  }
9674 
9675 
9676 
9677  cout << "\n------------------------------- RangeDeserializationTest() OK" << endl;
9678 }
9679 
9680 // ---------------------------------------------------------------------------
9681 
9682 
9683 
9684 
9685 
9686 // ------------------------------------------------------------------------
9687 
9688 static
9690  bvect& bv_target,
9691  const bvect* bv, ...)
9692 {
9693  va_list args;
9694  va_start(args, bv);
9695  agg.add(bv);
9696 
9697  for (int i = 0; true; ++i)
9698  {
9699  const bvect* bv_arg = (const bvect*)va_arg(args, void*);
9700  if (!bv_arg)
9701  break;
9702  agg.add(bv_arg);
9703  }
9704  va_end(args);
9705 
9706  agg.combine_shift_right_and(bv_target);
9707  agg.reset();
9708  return bv_target.any();
9709 }
9710 
9711 
9712 static
9714 {
9715  cout << "---------------------------- Aggregator Test" << endl;
9716 
9717  bvect* bv_arr[128] = { 0, };
9718  bvect* bv_arr2[128] = { 0, };
9719 
9720 
9721  cout << " AGG arg pipeline tests (basic)" << endl;
9722 
9723  {
9726  {
9727  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
9728  assert(args);
9729  args->arg_bv0.push_back(nullptr);
9730  args->arg_bv1.push_back(nullptr);
9731  }
9732  {
9733  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
9734  assert(args);
9735  args->arg_bv0.push_back(nullptr);
9736  }
9737  assert(!agg_pipe.is_complete());
9738  agg_pipe.complete();
9739  assert(agg_pipe.is_complete());
9740 
9741  auto& arg_vect = agg_pipe.get_args_vector();
9742  assert(arg_vect.size() == 2);
9743  assert(arg_vect[0]->arg_bv0.size() == 1);
9744  assert(arg_vect[0]->arg_bv1.size() == 1);
9745  assert(arg_vect[1]->arg_bv0.size() == 1);
9746  assert(arg_vect[1]->arg_bv1.size() == 0);
9747 
9748  }
9749 
9750  cout << "OR tests..." << endl;
9751  {
9753  agg.set_optimization();
9754 
9755  {
9756  bvect bv_target;
9757  bvect bv1 { 1, 2, 3};
9758  bvect bv2 { 0, 4, 5};
9759 
9760  agg.add(&bv1);
9761  agg.add(&bv2);
9762 
9763  agg.combine_or(bv_target);
9764  agg.reset();
9765 
9766  struct bvect::statistics st;
9767  bv_target.calc_stat(&st);
9768  assert (st.gap_blocks == 1);
9769  assert (st.bit_blocks == 0);
9770 
9771  unsigned bc = bv_target.count();
9772  assert(bc == 6);
9773  }
9774 
9775  // FULL block optimization test
9776  {
9777  bvect bv_target;
9778  bvect bv1;
9779  bvect bv2;
9780  bv1.set_range(0, 256);
9781  bv2.set_range(256, 65535);
9782 
9783  agg.reset();
9784  agg.add(&bv1);
9785  agg.add(&bv2);
9786 
9787  agg.combine_or(bv_target);
9788 
9789  struct bvect::statistics st;
9790  bv_target.calc_stat(&st);
9791  assert (st.gap_blocks == 0);
9792  assert (st.bit_blocks == 0);
9793 
9794  unsigned bc = bv_target.count();
9795  assert(bc == 65536);
9796 
9797  bv_target.set(1);
9798  agg.reset();
9799  agg.add(&bv1);
9800  agg.add(&bv2);
9801 
9802  agg.combine_and(bv_target);
9803  bv_target.calc_stat(&st);
9804  assert (st.gap_blocks == 1);
9805  assert (st.bit_blocks == 0);
9806  bc = bv_target.count();
9807  assert(bc == 1);
9808  }
9809 
9810  // 0-block optimization test
9811  {
9812  bvect bv_target;
9813  bvect bv1 { 1 };
9814  bvect bv2 { 5 };
9815 
9816  bv1.set(1, false); bv2.set(5, false);
9817 
9818  agg.reset();
9819  agg.add(&bv1);
9820  agg.add(&bv2);
9821 
9822  agg.combine_or(bv_target);
9823  agg.reset();
9824 
9825  struct bvect::statistics st;
9826  bv_target.calc_stat(&st);
9827  assert (st.gap_blocks == 0);
9828  assert (st.bit_blocks == 0);
9829 
9830  unsigned bc = bv_target.count();
9831  assert(bc == 0);
9832  }
9833  }
9834 
9836  agg.set_optimization();
9837 
9838  cout << "AND-SUB tests..." << endl;
9839  {
9840  bvect bv1, bv2, bv3;
9841  bvect bv_empty;
9842  bvect bv_control;
9843 
9844  bv_arr[0] = &bv1;
9845  agg.combine_or(bv3, bv_arr, 1);
9846  assert(bv3.count()==0);
9847 
9848  bv3[100] = true;
9849  bv1.invert();
9850  bv_control.invert();
9851  bv_arr[0] = &bv1;
9852  agg.combine_or(bv3, bv_arr, 1);
9853 
9854  int res = bv_control.compare(bv3);
9855  assert(res == 0);
9856 
9857  bv_arr[0] = &bv1;
9858  bv_arr[1] = &bv2;
9859  agg.combine_or(bv3, bv_arr, 2);
9860 
9861  res = bv_control.compare(bv3);
9862  assert(res == 0);
9863 
9864  bv2[1000000] = true;
9865  bv_arr[0] = &bv1;
9866  bv_arr[1] = &bv2;
9867  agg.combine_or(bv3, bv_arr, 2);
9868  res = bv_control.compare(bv3);
9869  assert(res == 0);
9870  }
9871 
9872  {
9873  bvect bv1, bv2, bv3;
9874  bv1.set(1);
9875  bv2.set(2);
9876  bv_arr[0] = &bv1;
9877  bv_arr[1] = &bv2;
9878  agg.combine_and(bv3, bv_arr, 2);
9879  bool b = bv3.any();
9880  assert(!b);
9881 
9882  }
9883 
9884  {
9885  bvect bv1, bv2, bv3;
9886  bvect bv_empty;
9887  bvect bv_control;
9888  int res;
9889 
9890  bv1.invert();
9891  bv_control.invert();
9892  bv_arr[0] = &bv1;
9893  agg.combine_and(bv3, bv_arr, 1);
9894  res = bv_control.compare(bv3);
9895  assert(res == 0);
9896 
9897  bv2.invert();
9898  bv2.set_range(100000, 100100);
9899  bv_arr[0] = &bv1;
9900  bv_arr[1] = &bv2;
9901  bv_control.set_range(100000, 100100);
9902  agg.combine_and(bv3, bv_arr, 2);
9903  res = bv_control.compare(bv3);
9904  assert(res == 0);
9905  agg.combine_and_sub(bv3, bv_arr, 2, 0, 0, false);
9906  res = bv_control.compare(bv3);
9907  assert(res == 0);
9908 
9909  }
9910 
9911  {
9912  bvect bv1, bv2, bv3, bv4;
9913  bvect bv_empty;
9914  bvect bv_control;
9915  int res;
9916 
9917  bv1.invert();
9918  bv2.set_range(200000, 300000);
9919  bv3.set_range(200000, 300000);
9920 
9921  bv_control.set_range(200000, 300000);
9922 
9923  bv_arr[0] = &bv1;
9924  bv_arr[1] = &bv2;
9925  bv_arr[2] = &bv3;
9926 
9927  agg.combine_and(bv4, bv_arr, 3);
9928  res = bv_control.compare(bv4);
9929  assert(res == 0);
9930  agg.combine_and_sub(bv4, bv_arr, 3, 0, 0, false);
9931  res = bv_control.compare(bv3);
9932  assert(res == 0);
9933  }
9934 
9935  {
9936  bvect bv1, bv2, bv3;
9937  bvect bv_empty;
9938  bvect bv_control;
9939  int res;
9940 
9941  bv_arr[0] = &bv1;
9942  agg.combine_and(bv3, bv_arr, 1);
9943  assert(bv3.count()==0);
9944 
9945  bv1[100] = true;
9946  bv_arr[0] = &bv1;
9947  agg.combine_and(bv3, bv_arr, 1);
9948  assert(bv3.count()==1);
9949  assert(bv3[100] == true);
9950 
9951  bv1[100] = true;
9952  bv2.invert();
9953  bv_arr[0] = &bv1;
9954  bv_arr[1] = &bv2;
9955  agg.combine_and(bv3, bv_arr, 2);
9956  assert(bv3.count()==1);
9957  assert(bv3[100] == true);
9958 
9959  bv1.clear();
9960  bv1.invert();
9961  bv_control.invert();
9962  bv_arr[0] = &bv1;
9963  bv_arr[1] = &bv2;
9964  agg.combine_and(bv3, bv_arr, 2);
9965  res = bv_control.compare(bv3);
9966  assert(res == 0);
9967  agg.combine_and_sub(bv3, bv_arr, 2, 0, 0, false);
9968  res = bv_control.compare(bv3);
9969  assert(res == 0);
9970  }
9971 
9972 
9973  // ---------------------------
9974  {
9975  bvect bv1, bv2, bv3, bv4;
9976  bvect bv_empty;
9977  bvect bv_control;
9978 
9979  bv1[100] = true;
9980  bv1[100000] = true;
9981  bv2[100] = true;
9982  bv2[100000] = true;
9983  bv3.set(100000);
9984 
9985  bv_arr[0] = &bv1;
9986  bv_arr[1] = &bv2;
9987  bv_arr2[0] = &bv3;
9988 
9989  agg.combine_and_sub(bv4, bv_arr, 2, bv_arr2, 1, false);
9990  assert(bv4.count()==1);
9991  assert(bv4.test(100));
9992 
9993  bv3.optimize();
9994  agg.combine_and_sub(bv4, bv_arr, 2, bv_arr2, 1, false);
9995  assert(bv4.count()==1);
9996  assert(bv4.test(100));
9997 
9998  bv1.optimize();
9999  agg.combine_and_sub(bv4, bv_arr, 2, bv_arr2, 1, false);
10000  auto c = bv4.count();
10001  assert(c==1);
10002  assert(bv4.test(100));
10003 
10004  bv2.optimize();
10005  agg.combine_and_sub(bv4, bv_arr, 2, bv_arr2, 1, false);
10006  assert(bv4.count()==1);
10007  assert(bv4.test(100));
10008  }
10009 
10010  {
10011  bvect bv1, bv2, bv3, bv4;
10012  bvect bv_empty;
10013  bvect bv_control;
10014 
10015  bv1[100] = true;
10016  bv1[100000] = true;
10017  bv2[100] = true;
10018  bv2[100000] = true;
10019 
10020  bv3.invert();
10021 
10022  bv_arr[0] = &bv1;
10023  bv_arr[1] = &bv2;
10024  bv_arr2[0] = &bv3;
10025 
10026  agg.combine_and_sub(bv4, bv_arr, 2, bv_arr2, 1, false);
10027  assert(bv4.count()==0);
10028  assert(!bv4.any());
10029  }
10030 
10031 
10032  cout << " AGG arg pipeline tests (AND-SUB)" << endl;
10033 
10034  {
10035  {
10036  bvect bv0{ 1, 65536 }, bv1{ 1, 65536 }, bv2 {65536};
10037 
10038  {
10040  {
10041  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10042  args->add(&bv0, 0); // AND
10043  args->add(&bv1, 0);
10044  }
10045  agg_pipe.complete();
10046  agg.combine_and_sub(agg_pipe);
10047  auto& res_vect = agg_pipe.get_bv_res_vector();
10048  assert(res_vect.size()==1);
10049  const auto& res_cnt = agg_pipe.get_bv_count_vector();
10050  assert(res_cnt.size()==0);
10051 
10052  for (size_t i = 0; i < res_vect.size(); ++i)
10053  {
10054  bvect* bv = res_vect[i];
10055  assert(bv);
10056  auto cnt = bv->count();
10057  assert(cnt == 2);
10058  assert(bv->test(1));
10059  assert(bv->test(65536));
10060  }
10061  }
10062 
10063  {
10064  bvect bv_res { 0 };
10065  bv_res.optimize();
10066 
10068  {
10069  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10070  args->add(&bv0, 0); // AND
10071  args->add(&bv1, 0);
10072 
10073  args = agg_pipe.add();
10074  args->add(&bv0, 0); // AND
10075  args->add(&bv2, 0);
10076  }
10077  agg_pipe.set_or_target(&bv_res);
10078 
10079  agg_pipe.complete();
10080 
10081  agg.combine_and_sub(agg_pipe);
10082 
10083  auto& res_vect = agg_pipe.get_bv_res_vector();
10084  assert(res_vect.size()==0);
10085  const auto& res_cnt = agg_pipe.get_bv_count_vector();
10086  assert(res_cnt.size()==0);
10087 
10088  auto cnt = bv_res.count();
10089  assert(bv_res.test(0));
10090  assert(cnt == 3);
10091 
10092  }
10093 
10094  {
10095  bvect bv_res;
10096  bv_res.invert();
10097 
10099  {
10100  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10101  args->add(&bv0, 0); // AND
10102  args->add(&bv1, 0);
10103 
10104  args = agg_pipe.add();
10105  args->add(&bv0, 0); // AND
10106  args->add(&bv2, 0);
10107  }
10108  agg_pipe.set_or_target(&bv_res);
10109 
10110  agg_pipe.complete();
10111 
10112  agg.combine_and_sub(agg_pipe);
10113 
10114  auto& res_vect = agg_pipe.get_bv_res_vector();
10115  assert(res_vect.size()==0);
10116  const auto& res_cnt = agg_pipe.get_bv_count_vector();
10117  assert(res_cnt.size()==0);
10118 
10119  bvect bv_control;
10120  bv_control.invert();
10121 
10122  bool b = bv_control.equal(bv_res);
10123  assert(b);
10124  }
10125 
10126 
10127  {
10129 
10130  {
10131  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10132  args->add(&bv0, 0); // AND
10133  args->add(&bv1, 0);
10134  args->add(&bv2, 1); // SUB
10135  }
10136  {
10137  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10138  args->add(&bv0, 0); // AND
10139  args->add(&bv1, 0);
10140  args->add(&bv2, 1); // SUB
10141  }
10142 
10143  agg_pipe.complete();
10144 
10145 
10146  agg.combine_and_sub(agg_pipe);
10147 
10148  auto& res_vect = agg_pipe.get_bv_res_vector();
10149  assert(res_vect.size()==2);
10150  const auto& res_cnt = agg_pipe.get_bv_count_vector();
10151 
10152  for (size_t i = 0; i < res_vect.size(); ++i)
10153  {
10154  bvect* bv = res_vect[i];
10155  assert(bv);
10156  auto cnt = bv->count();
10157  assert(cnt == 1);
10158  assert(bv->test(1));
10159  auto c = res_cnt[i];
10160  assert(c == cnt);
10161  }
10162  }
10163 
10164  {
10166 
10167  {
10168  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10169  args->add(&bv0, 0); // AND
10170  args->add(&bv1, 0);
10171  args->add(&bv2, 1); // SUB
10172  }
10173  {
10174  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10175  args->add(&bv0, 0); // AND
10176  args->add(&bv1, 0);
10177  args->add(&bv2, 1); // SUB
10178  }
10179 
10180  agg_pipe.complete();
10181 
10182 
10183  agg.combine_and_sub(agg_pipe);
10184 
10185  auto& res_vect = agg_pipe.get_bv_res_vector();
10186  const auto& res_cnt = agg_pipe.get_bv_count_vector();
10187 
10188  for (size_t i = 0; i < res_vect.size(); ++i)
10189  {
10190  bvect* bv = res_vect[i];
10191  assert(!bv);
10192  auto c = res_cnt[i];
10193  assert(c == 1);
10194  }
10195  }
10196 
10197 
10198  {
10199  bvect bv_full;
10200  bv_full.invert();
10202 
10203  {
10204  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10205  args->add(&bv0, 0); // AND
10206  args->add(&bv1, 0);
10207  args->add(&bv_full, 1); // SUB
10208  }
10209  {
10210  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10211  args->add(&bv0, 0); // AND
10212  args->add(&bv1, 0);
10213  args->add(&bv_full, 1); // SUB
10214  args->add(&bv2, 1); // SUB
10215  args->add(&bv0, 1); // SUB
10216 
10217  }
10218 
10219  agg_pipe.complete();
10220 
10221  agg.combine_and_sub(agg_pipe);
10222  auto& res_vect = agg_pipe.get_bv_res_vector();
10223  assert(res_vect.size()==2);
10224  const auto& res_cnt = agg_pipe.get_bv_count_vector();
10225 
10226  for (size_t i = 0; i < res_vect.size(); ++i)
10227  {
10228  bvect* bv = res_vect[i];
10229  assert(!bv);
10230  auto c = res_cnt[i];
10231  assert(c == 0);
10232  }
10233  auto gch = agg.get_cache_gap_hits(); (void) gch;
10234  //assert(gch == 0);
10235  }
10236  }
10237 
10238  // test GAP cached aggregator
10239  {
10240  bvect bv0{ 1, 65536 }, bv1{ 1, 65536 }, bv2 {65536};
10241  bv0.optimize();
10242  bv1.optimize();
10243  bv2.optimize();
10244  {
10246  {
10247  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10248  args->add(&bv0, 0); // AND
10249  args->add(&bv1, 0);
10250  args->add(&bv2, 1); // SUB
10251  }
10252  {
10253  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10254  args->add(&bv0, 0); // AND
10255  args->add(&bv1, 0);
10256  args->add(&bv2, 1); // SUB
10257  }
10258 
10259  agg_pipe.complete();
10260  auto& ivect = agg_pipe.get_all_input_vect();
10261  auto& cnt_vect = agg_pipe.get_all_input_cnt_vect();
10262  assert(ivect.size() == 3);
10263  for (size_t i = 0; i < ivect.size(); ++i)
10264  {
10265  const bvect* bv = ivect[i];
10266  assert(bv == &bv0 || bv == &bv1 || bv == &bv2);
10267  assert(cnt_vect[i] == 1);
10268  }
10269 
10270 
10271  agg.combine_and_sub(agg_pipe);
10272  auto& res_vect = agg_pipe.get_bv_res_vector();
10273  assert(res_vect.size()==2);
10274  for (size_t i = 0; i < res_vect.size(); ++i)
10275  {
10276  bvect* bv = res_vect[i];
10277  assert(bv);
10278  auto cnt = bv->count();
10279  assert(cnt == 1);
10280  assert(bv->test(1));
10281  }
10282  auto gch = agg.get_cache_gap_hits(); (void)gch;
10283  //assert(gch == 10);
10284  }
10285 
10286  }
10287 
10288  }
10289 
10290 
10291 
10292 
10293  // SHIFT-R_AND
10294 
10295  cout << "SHIFT-R-AND tests..." << endl;
10296 
10297  {
10298  bvect bv0, bv1, bv2;
10299  bv1[0] = true;
10300  bv1[65535]=true;
10301 
10302  bv2[1]=true;
10303  bv2[65536]=true;
10304 
10305  agg.add(&bv1); agg.add(&bv2);
10306  agg.set_compute_count(false);
10307  agg.combine_shift_right_and(bv0);
10308  agg.reset();
10309  bool any = bv0.any();
10310 
10311 /// bool any = agg.shift_right_and(bv1, bv2);
10312  assert(any);
10313  assert(bv0.count()==2);
10314  assert(bv0.test(1));
10315  assert(bv0.test(65536));
10316  }
10317 
10318  {
10319  bvect bv0, bv1, bv2;
10320  bv1[0] = true;
10321  bv1[65535]=true;
10322 
10323  bv2[0]=true;
10324  bv2[65535]=true;
10325 
10326  bool any = agg_shift_right_and(agg, bv0, &bv1, &bv2, 0);
10327  assert(!any);
10328  assert(bv0.count()==0);
10329  }
10330 
10331 
10332  {
10333  bvect bv0, bv1, bv2;
10334  bv1[0] = true;
10335  bv1[65535]=true;
10336  bv1.optimize();
10337 
10338  bv2[1]=true;
10339  bv2[65536]=true;
10340  bv2.optimize();
10341 
10342  agg_shift_right_and(agg, bv0, &bv1, &bv2, 0);
10343  assert(bv0.count()==2);
10344  assert(bv0.test(1));
10345  assert(bv0.test(65536));
10346  }
10347 
10348 
10349  {
10350  bvect bv0, bv1, bv2;
10351  bv1[65535]=true;
10352 
10353  bv2[65536]=true;
10354  bv2.optimize();
10355 
10356  bool any = agg_shift_right_and(agg, bv0, &bv1, &bv2, 0);
10357  assert(bv0.count()==1);
10358  assert(bv0.test(65536));
10359  assert(any);
10360  struct bvect::statistics st1;
10361  bv0.calc_stat(&st1);
10362  auto bcnt = st1.bit_blocks + st1.gap_blocks;
10363  assert(bcnt == 1);
10364  }
10365 
10366 
10367  {
10368  bvect bv0, bv1, bv2;
10369  bv1[0] = true;
10370  bv1[65535]=true;
10371 
10372  bv2.invert();
10373 
10374  agg_shift_right_and(agg, bv0, &bv1, &bv2, 0);
10375  assert(bv0.count()==2);
10376  assert(bv0.test(1));
10377  assert(bv0.test(65536));
10378  }
10379 
10380  {
10381  bvect bv0, bv1, bv2;
10382  bvect bv1c, bv2c;
10383  bv1.invert();
10384  bv2.invert();
10385  bv1c.invert();
10386  bv2c.invert();
10387 
10388  bv1c.shift_right();
10389  bv1c &= bv2c;
10390 
10391  bool any = agg_shift_right_and(agg, bv0, &bv1, &bv2, 0);
10392 
10393  assert(any);
10394  assert(!bv0.test(0));
10395  assert(!bv1c.test(0));
10396 
10397  struct bvect::statistics st1;
10398  bv0.calc_stat(&st1);
10399  auto bcnt = st1.bit_blocks + st1.gap_blocks;
10400  cout << bcnt << endl;
10401  assert(bcnt == 2);
10402 
10403  assert(bv0.count()==bv1c.count());
10404  auto cmp = bv1c.compare(bv0);
10405  assert(cmp==0);
10406  }
10407 
10408  {
10409  bvect bv0, bv1, bv2;
10410  bv1.set_range(0, 65536*4);
10411  bv2.set_range(0, 65536*4);
10412 
10413  bool any = agg_shift_right_and(agg, bv0, &bv1, &bv2, 0);
10414 
10415  assert(any);
10416  assert(!bv0.test(0));
10417  assert(bv0.count() == 65536*4);
10418 
10419  struct bvect::statistics st1;
10420  bv0.calc_stat(&st1);
10421  auto bcnt = st1.bit_blocks + st1.gap_blocks;
10422  assert(bcnt == 2); // TODO: not critical (optimization) needs a fix
10423  }
10424 
10425  {
10426  bvect bv0, bv1, bv2;
10427  bv1.set_range(0, 65536*4);
10428  bv2.set_range(0, 65536*2);
10429 
10430  bool any = agg_shift_right_and(agg, bv0, &bv1, &bv2, 0);
10431 
10432  assert(any);
10433  assert(!bv0.test(0));
10434  assert(bv0.count() == 65536*2);
10435 
10436  struct bvect::statistics st1;
10437  bv0.calc_stat(&st1);
10438  auto bcnt = st1.bit_blocks + st1.gap_blocks;
10439  assert(bcnt == 2);
10440  }
10441 
10442 
10443  {
10444  bvect bv0, bv1, bv2;
10445  bv1.set_range(0, 65536*4);
10446  bv2.set_range(65536, 65536+10);
10447 
10448  bool any = agg_shift_right_and(agg, bv0, &bv1, &bv2, 0);
10449 
10450  assert(any);
10451  assert(!bv0.test(0));
10452  cout << bv0.count() << endl;
10453  assert(bv0.count() == 11);
10454 
10455  struct bvect::statistics st1;
10456  bv0.calc_stat(&st1);
10457  auto bcnt = st1.bit_blocks + st1.gap_blocks;
10458  assert(bcnt == 1);
10459  }
10460 
10461 
10462  cout << "---------------------------- Aggregator Test OK" << endl;
10463 }
10464 
10465 
10466 static
10467 void StressTestAggregatorOR(unsigned repetitions)
10468 {
10469  cout << "---------------------------- Aggregator OR Stress Test" << endl;
10470  unsigned size = BITVECT_SIZE - 10;
10471  bvect bv_target1, bv_target2;
10472 
10473 
10474  unsigned i;
10475  for (i = 0; i < repetitions; ++i)
10476  {
10477  int opt = rand() % 2;
10478  cout << endl << " - - - - - - - - - - - - AGG OR STRESS STEP " << i << endl;;
10479 
10480  switch (rand() % 3)
10481  {
10482  case 0:
10483  size = BITVECT_SIZE / 10;
10484  break;
10485  case 1:
10486  size = BITVECT_SIZE / 2;
10487  break;
10488  default:
10489  size = BITVECT_SIZE - 10;
10490  break;
10491  } // switch
10492 
10493  unsigned start1 = 0;
10494  switch (rand() % 3)
10495  {
10496  case 1:
10497  start1 += size / 5;
10498  break;
10499  default:
10500  break;
10501  }
10502 
10503  unsigned start2 = 0;
10504  switch (rand() % 3)
10505  {
10506  case 1:
10507  start2 += size / 5;
10508  break;
10509  default:
10510  break;
10511  }
10512 
10513  bvect_mini bvect_min1(size);
10514  bvect bv0, bv1, bv2, bv3, bv4, bv5, bv6, bv7, bv8, bv9;
10515 
10516  // 0 skipped
10517  FillSetsRandomMethod(&bvect_min1, &bv1, start1, size, opt);
10518  FillSetsRandomMethod(&bvect_min1, &bv2, start2, size, opt);
10519  // 3 skipped
10520  FillSetsRandomMethod(&bvect_min1, &bv5, start1, size, opt);
10521  FillSetsRandomMethod(&bvect_min1, &bv6, start2, size, opt);
10522  FillSetsRandomMethod(&bvect_min1, &bv7, start1, size, opt);
10523  FillSetsRandomMethod(&bvect_min1, &bv8, start2, size, opt);
10524  FillSetsRandomMethod(&bvect_min1, &bv9, start2, size, opt);
10525 
10527  agg.set_optimization();
10528 
10529  bvect* agg_list[32] = {0, };
10530 
10531  agg_list[0] = &bv0;
10532  agg_list[1] = &bv1;
10533  agg_list[2] = &bv2;
10534  agg_list[3] = &bv3;
10535  agg_list[4] = &bv4;
10536  agg_list[5] = &bv5;
10537  agg_list[6] = &bv6;
10538  agg_list[7] = &bv7;
10539  agg_list[8] = &bv8;
10540  agg_list[9] = &bv9;
10541 
10542  unsigned cnt = 10;
10543  agg.combine_or(bv_target1, agg_list, cnt);
10544  agg.combine_or_horizontal(bv_target2, agg_list, cnt);
10545 
10546  int res = bv_target1.compare(bv_target2);
10547  if (res!=0)
10548  {
10549  cerr << "Error: Aggregator OR check failed!" << endl;
10550  DetailedCompareBVectors(bv_target1, bv_target2);
10551  exit(1);
10552  }
10553  for (unsigned j = 1; j < cnt; ++j)
10554  {
10555  agg.combine_or(bv_target1, agg_list, j);
10556  agg.combine_or_horizontal(bv_target2, agg_list, j);
10557  res = bv_target1.compare(bv_target2);
10558  if (res!=0)
10559  {
10560  cerr << "Error: Aggregator OR check failed! 1.laddder step = "
10561  << j << endl;
10562  exit(1);
10563  }
10564  }
10565 
10566  for (unsigned j = 0; j < cnt; ++j)
10567  {
10568  agg.combine_or(bv_target1, agg_list+j, cnt-j);
10569  agg.combine_or_horizontal(bv_target2, agg_list+j, cnt-j);
10570  res = bv_target1.compare(bv_target2);
10571  if (res!=0)
10572  {
10573  cerr << "Error: Aggregator OR check failed! 2.laddder step = "
10574  << j << endl;
10575  exit(1);
10576  }
10577  }
10578 
10579 
10580  } // for i
10581 
10582  cout << "---------------------------- Aggregator OR Stress Test OK" << endl;
10583 }
10584 
10585 static
10586 void StressTestAggregatorAND(unsigned repetitions)
10587 {
10588  cout << "---------------------------- Aggregator AND Stress Test" << endl;
10589  unsigned size = BITVECT_SIZE - 10;
10590 
10591 
10592  unsigned i;
10593  for (i = 0; i < repetitions; ++i)
10594  {
10595  int opt = rand() % 2;
10596  cout << endl << " - - - - - - - - - - - - AGG AND STRESS STEP " << i << endl;;
10597 
10598  switch (rand() % 3)
10599  {
10600  case 0:
10601  size = BITVECT_SIZE / 10;
10602  break;
10603  case 1:
10604  size = BITVECT_SIZE / 2;
10605  break;
10606  default:
10607  size = BITVECT_SIZE - 10;
10608  break;
10609  } // switch
10610 
10611  unsigned start1 = 0;
10612  switch (rand() % 3)
10613  {
10614  case 1:
10615  start1 += size / 5;
10616  break;
10617  default:
10618  break;
10619  }
10620 
10621  unsigned start2 = 0;
10622  switch (rand() % 3)
10623  {
10624  case 1:
10625  start2 += size / 5;
10626  break;
10627  default:
10628  break;
10629  }
10630 
10631  bvect_mini bvect_min1(size);
10632  bvect bv0, bv1, bv2, bv3, bv4, bv5, bv6, bv7, bv8, bv9;
10633 
10634  // 0 skipped
10635  FillSetsRandomMethod(&bvect_min1, &bv1, start1, size, opt);
10636  FillSetsRandomMethod(&bvect_min1, &bv2, start2, size, opt);
10637  // 3 skipped
10638  FillSetsRandomMethod(&bvect_min1, &bv5, start1, size, opt);
10639  FillSetsRandomMethod(&bvect_min1, &bv6, start2, size, opt);
10640  FillSetsRandomMethod(&bvect_min1, &bv7, start1, size, opt);
10641  FillSetsRandomMethod(&bvect_min1, &bv8, start2, size, opt);
10642  FillSetsRandomMethod(&bvect_min1, &bv9, start2, size, opt);
10643 
10645 
10646 
10647  {
10649  bvect bv_res;
10650  {
10651  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10652  args->add(&bv1, 0); // AND
10653  args->add(&bv2, 0);
10654 
10655  args = agg_pipe.add();
10656  args->add(&bv5, 0); // AND
10657  args->add(&bv6, 0);
10658  args->add(&bv7, 0);
10659 
10660  args = agg_pipe.add();
10661  args->add(&bv7, 0);
10662  args->add(&bv8, 0);
10663  args->add(&bv9, 0);
10664  }
10665  agg_pipe.set_or_target(&bv_res);
10666  agg_pipe.complete();
10667 
10668  agg.combine_and_sub(agg_pipe);
10669 
10670  auto& res_vect = agg_pipe.get_bv_res_vector();
10671  assert(res_vect.size()==3);
10672 
10673  bool b;
10674  {
10675  const bvect* bvIp = res_vect[0];
10676  bvect bv1c;
10677  bv1c.bit_and(bv1, bv2);
10678  if (bvIp)
10679  b = bvIp->equal(bv1c);
10680  else
10681  b = !bv1c.any();
10682  assert(b);
10683 
10684  }
10685  {
10686  const bvect* bvIp = res_vect[1];
10687  bvect bv1c;
10688  bv1c.bit_and(bv5, bv6);
10689  bv1c.bit_and(bv7);
10690 
10691  if (bvIp)
10692  b = bvIp->equal(bv1c);
10693  else
10694  b = !bv1c.any();
10695  assert(b);
10696  }
10697  {
10698  const bvect* bvIp = res_vect[2];
10699  bvect bv1c;
10700  bv1c.bit_and(bv8, bv9);
10701  bv1c.bit_and(bv7);
10702 
10703  if (bvIp)
10704  b = bvIp->equal(bv1c);
10705  else
10706  b = !bv1c.any();
10707  assert(b);
10708  }
10709  }
10710 
10711  agg.set_optimization();
10712 
10713  bvect* agg_list[32] = {0, };
10714 
10715  agg_list[0] = &bv0;
10716  agg_list[1] = &bv1;
10717  agg_list[2] = &bv2;
10718  agg_list[3] = &bv3;
10719  agg_list[4] = &bv4;
10720  agg_list[5] = &bv5;
10721  agg_list[6] = &bv6;
10722  agg_list[7] = &bv7;
10723  agg_list[8] = &bv8;
10724  agg_list[9] = &bv9;
10725 
10726  bvect bv_target1, bv_target2, bv_target3, bv_target4;
10727  bvect bv_empty;
10728 
10729  unsigned cnt = 10;
10730  agg.combine_and_sub(bv_target3, agg_list, cnt, 0, 0, false);
10731  agg.combine_and(bv_target1, agg_list, cnt);
10732  agg.combine_and_horizontal(bv_target2, agg_list, cnt);
10733  agg.combine_and_sub(bv_empty, agg_list, cnt, agg_list, cnt, false);
10734 
10735  int res = bv_target1.compare(bv_target2);
10736  if (res!=0)
10737  {
10738  cerr << "Error: Aggregator AND check failed!" << endl;
10739  assert(0);
10740  exit(1);
10741  }
10742  res = bv_target3.compare(bv_target1);
10743  if (res!=0)
10744  {
10745  cerr << "Error: Aggregator AND-SUB(0) check failed!" << endl;
10746  assert(0);exit(1);
10747  }
10748  assert(!bv_empty.any());
10749  for (unsigned j = 1; j < cnt; ++j)
10750  {
10751  agg.combine_and(bv_target1, agg_list, j);
10752  agg.combine_and_horizontal(bv_target2, agg_list, j);
10753  agg.combine_and_sub(bv_target3, agg_list, cnt, 0, 0, false);
10754  agg.combine_and_sub(bv_empty, agg_list, cnt, agg_list, cnt, false);
10755 
10756 
10757  res = bv_target1.compare(bv_target2);
10758  if (res!=0)
10759  {
10760  cerr << "Error: Aggregator AND check failed! 1.laddder step = "
10761  << j << endl;
10762  assert(0);
10763  exit(1);
10764  }
10765  res = bv_target1.compare(bv_target3);
10766  if (res!=0)
10767  {
10768  cerr << "Error: Aggregator AND-SUB(0) check failed! 1.laddder step = "
10769  << j << endl;
10770  assert(0);
10771  exit(1);
10772  }
10773  assert(!bv_empty.any());
10774  }
10775 
10776  for (unsigned j = 0; j < cnt; ++j)
10777  {
10778  if (j == 9)
10779  cerr << j << endl;
10780  agg.combine_and(bv_target1, agg_list+j, cnt-j);
10781  agg.combine_and_horizontal(bv_target2, agg_list+j, cnt-j);
10782  agg.combine_and_sub(bv_target3, agg_list+j, cnt-j, 0, 0, false);
10783  agg.combine_and_sub_horizontal(bv_target4, agg_list+j, cnt-j, 0, 0);
10784  agg.combine_and_sub(bv_empty, agg_list+j, cnt-j, agg_list+j, cnt-j, false);
10785 
10786  res = bv_target1.compare(bv_target2);
10787  if (res!=0)
10788  {
10789  cerr << "Error: Aggregator AND check failed! 2.laddder step = "
10790  << j << endl;
10791  assert(0); exit(1);
10792  }
10793  res = bv_target1.compare(bv_target4);
10794  if (res!=0)
10795  {
10796  cerr << "Error: Aggregator Horz-AND-SUB(0) check failed! 2.laddder step = "
10797  << j << endl;
10798  res = bv_target3.compare(bv_target4);
10799  if (res == 0)
10800  {
10801  cerr << "Warning. Aggregator AND-SUB ok... \n";
10802  }
10803  assert(0); exit(1);
10804  }
10805 
10806  res = bv_target1.compare(bv_target3);
10807  if (res!=0)
10808  {
10809  cerr << "Error: Aggregator AND-SUB(0) check failed! 2.laddder step = "
10810  << j << endl;
10811  assert(0); exit(1);
10812  }
10813  assert(!bv_empty.any());
10814  }
10815 
10816 
10817  } // for i
10818 
10819  cout << "---------------------------- Aggregator AND Stress Test OK" << endl;
10820 }
10821 
10822 
10823 
10824 static
10825 void StressTestAggregatorAND_SUB(unsigned repetitions)
10826 {
10827  cout << "---------------------------- Aggregator AND-SUB Stress Test" << endl;
10828  unsigned size = BITVECT_SIZE - 10;
10830 
10831  unsigned i;
10832  for (i = 0; i < repetitions; ++i)
10833  {
10834  int opt = rand() % 2;
10835  cout << endl << " - - - - - - - - - - - - AGG AND-SUB STRESS STEP " << i << endl;;
10836 
10837  switch (rand() % 3)
10838  {
10839  case 0:
10840  size = BITVECT_SIZE / 10;
10841  break;
10842  case 1:
10843  size = BITVECT_SIZE / 2;
10844  break;
10845  default:
10846  size = BITVECT_SIZE - 10;
10847  break;
10848  } // switch
10849 
10850  unsigned start1 = 0;
10851  switch (rand() % 3)
10852  {
10853  case 1:
10854  start1 += size / 5;
10855  break;
10856  default:
10857  break;
10858  }
10859 
10860  unsigned start2 = 0;
10861  switch (rand() % 3)
10862  {
10863  case 1:
10864  start2 += size / 5;
10865  break;
10866  default:
10867  break;
10868  }
10869 
10870  bvect_mini bvect_min1(size);
10871  bvect bv0, bv1, bv2, bv3, bv4, bv5, bv6, bv7, bv8, bv9;
10872 
10873  // 0 skipped
10874  FillSetsRandomMethod(&bvect_min1, &bv1, start1, size, opt);
10875  FillSetsRandomMethod(&bvect_min1, &bv2, start2, size, opt);
10876 
10877  auto cnt1 = bv1.count();
10878  auto cnt2 = bv2.count();
10879  bvect::size_type sample_size = (cnt1+cnt2)/2;
10880 
10881  rsub.sample(bv3, bv1, sample_size);
10882  bv3.optimize();
10883  rsub.sample(bv4, bv1, sample_size);
10884  rsub.sample(bv5, bv1, sample_size);
10885 
10886  rsub.sample(bv6, bv2, sample_size);
10887  rsub.sample(bv7, bv2, sample_size);
10888  bv7.optimize();
10889 
10890  bv8 = bv1;
10891  bv9 = bv2;
10892 
10893  bvect bv_full;
10894  bv_full.invert();
10895 
10897 
10898  bvect bv_target1;//, bv_target2;
10899  {
10900  bvect* agg_list_and[32] = {0, };
10901  bvect* agg_list_sub[32] = {0, };
10902  agg_list_and[0] = &bv2;
10903  agg_list_and[1] = &bv3;
10904  agg_list_and[2] = &bv_full; // irrelevant
10905 
10906  agg_list_sub[0] = &bv6;
10907  agg_list_sub[1] = &bv7;
10908 
10909  agg.combine_and_sub(bv_target1,
10910  agg_list_and, 3, agg_list_sub, 2, false);
10911  }
10912 
10913  {
10915  bvect bv_res;
10916  {
10917  bm::aggregator<bvect>::arg_groups* args = agg_pipe.add();
10918  args->add(&bv1, 0); // AND
10919  args->add(&bv3, 0);
10920  args->add(&bv8, 1); // SUB bv1
10921  args->add(&bv2, 1); // irrelevant arg
10922 
10923  args = agg_pipe.add();
10924  args->add(&bv2, 0); // AND
10925  args->add(&bv3, 0);
10926  args->add(&bv6, 1); // SUB
10927  args->add(&bv7, 1);
10928 
10929  args = agg_pipe.add();
10930  args->add(&bv7, 0);
10931  args->add(&bv8, 0);
10932  args->add(&bv9, 0); // AND bv2
10933  args->add(&bv9, 1); // SUB bv2
10934  }
10935  agg_pipe.set_or_target(&bv0);
10936  agg_pipe.complete();
10937 
10938  agg.combine_and_sub(agg_pipe);
10939 
10940  auto& res_vect = agg_pipe.get_bv_res_vector();
10941  assert(res_vect.size()==3);
10942 
10943  bool b;
10944  {
10945  const bvect* bvIp = res_vect[0];
10946  assert(bvIp == 0);
10947  }
10948  {
10949  const bvect* bvIp = res_vect[1];
10950  bvect bv1c;
10951  bv1c.bit_and(bv2, bv3);
10952  bv1c.bit_sub(bv6);
10953  bv1c.bit_sub(bv7);
10954 
10955  if (bvIp)
10956  {
10957  b = bvIp->equal(bv1c);
10958  assert(b);
10959  bool b2 = bvIp->equal(bv0);
10960  assert(b2);
10961  bool b3 = bvIp->equal(bv_target1);
10962  assert(b3);
10963  }
10964  else
10965  {
10966  b = !bv1c.any();
10967  assert(b);
10968  }
10969 
10970  }
10971  {
10972  const bvect* bvIp = res_vect[2];
10973  assert(bvIp == 0);
10974  }
10975  }
10976 
10977 
10978  } // for i
10979 
10980  cout << "---------------------------- Aggregator AND-SUB Stress Test OK" << endl;
10981 }
10982 
10983 
10984 static
10985 void GenerateTestCollection(std::vector<bvect>* target, unsigned count = 30, unsigned vector_max = 40000000)
10986 {
10987  assert(target);
10988  bvect bv_common; // sub-vector common for all collection
10989  bvect_mini bvect_min(vector_max);
10990 
10991  FillSetsRandomMethod(&bvect_min, &bv_common, 0, vector_max, 1);
10992 
10993  for (unsigned i = 0; i < count; ++i)
10994  {
10995  std::unique_ptr<bvect> bv (new bvect);
10996  FillSetsRandomMethod(&bvect_min, bv.get(), 0, vector_max, 1);
10997  *bv |= bv_common;
10998  target->push_back(std::move(*bv));
10999  } // for
11000 }
11001 
11002 
11003 static
11004 void StressTestAggregatorShiftAND(unsigned repeats)
11005 {
11006  cout << "----------------------------StressTestAggregatorShiftAND " << endl;
11007 
11008  unsigned vector_max = 400000000;
11009  unsigned coll_size = 20;
11010 
11011  for (unsigned r = 0; r < repeats; ++r)
11012  {
11013  bvect mask_bv0;
11014  bvect_mini bvect_min(vector_max);
11015  FillSetsRandomMethod(&bvect_min, &mask_bv0, 0, vector_max - (vector_max / 5), 1);
11016 
11017  std::vector<bvect> bv_coll1;
11018  GenerateTestCollection(&bv_coll1, coll_size, vector_max);
11019 
11020 
11022  agg.set_optimization();
11023 
11024  unsigned shift_repeats = 65536/3;
11025  for (unsigned i = 0; i < shift_repeats; ++i)
11026  {
11027  bvect bv_target0(mask_bv0);
11028  for (unsigned k = 0; k < bv_coll1.size(); ++k)
11029  {
11030  bv_target0.shift_right();
11031  bv_target0 &= bv_coll1[k];
11032  } // for
11033 
11034  agg.reset();
11035  agg.add(&mask_bv0);
11036  for (unsigned k = 0; k < bv_coll1.size(); ++k)
11037  {
11038  agg.add(&bv_coll1[k]);
11039  } // for
11040 
11041  bvect bv_target1;
11042  agg.set_compute_count(false);
11043  agg.combine_shift_right_and(bv_target1);
11044  auto cmp = bv_target1.compare(bv_target0);
11045  if (cmp != 0)
11046  {
11047  cerr << "Error: Mismatch! " << "STEP=" << i << endl;
11048  DetailedCheckVectors(bv_target0, bv_target1);
11049  exit(1);
11050  }
11051  if (!is_silent)
11052  if (i % 250 == 0)
11053  cout << "\r" << i << "/" << shift_repeats << flush;
11054 
11055  bvect bv_target2;
11056  agg.set_compute_count(true);
11057  agg.combine_shift_right_and(bv_target2);
11058  assert(!bv_target2.any());
11059  auto cnt = agg.count();
11060  auto cnt_c = bv_target1.count();
11061  assert(cnt == cnt_c);
11062 
11063  } // for
11064  cout << "\n\n ---------- SHIFT-AND step: " << r << endl;
11065  } // for
11066 
11067  cout << "\n----------------------------StressTestAggregatorShiftAND OK" << endl;
11068 
11069 }
11070 
11071 
11072 static
11075  const bvect& bvect_full1, const bvect& bvect_full2)
11076 {
11077  cout << "AND-OR tests..." << flush;
11078  bvect bv_sub1;
11079  auto sample_count = count / 2;
11080  if (sample_count)
11081  rsub.sample(bv_sub1, bvect_full1, sample_count);
11082  CheckBV_AND_OR(bv_sub1, bvect_full1, bvect_full2);
11083  if (sample_count)
11084  rsub.sample(bv_sub1, bvect_full2, sample_count);
11085  CheckBV_AND_OR(bv_sub1, bvect_full2, bvect_full1);
11086  bv_sub1 = bvect_full1;
11087  CheckBV_AND_OR(bv_sub1, bvect_full2, bvect_full1);
11088  bv_sub1 = bvect_full2;
11089  CheckBV_AND_OR(bv_sub1, bvect_full1, bvect_full2);
11090  cout << " OK" << endl;
11091 }
11092 
11093 
11094 static
11095 void StressTest(unsigned repetitions, int set_operation, bool detailed,
11096  int method = -1)
11097 {
11098 
11099  unsigned RatioSum = 0;
11100  unsigned SRatioSum = 0;
11101  unsigned DeltaSum = 0;
11102  unsigned SDeltaSum = 0;
11103 
11104  unsigned clear_count = 0;
11105 
11106  bvect bvtotal;
11108 
11110 
11111 
11112  cout << "----------------------------StressTest" << endl;
11113 
11114  unsigned size = BITVECT_SIZE - 10;
11115 //size = BITVECT_SIZE / 10;
11116  unsigned i;
11117  for (i = 0; i < repetitions; ++i)
11118  {
11119  cout << endl << " - - - - - - - - - - - - STRESS STEP " << i;
11120  switch (set_operation)
11121  {
11122  case 0: cout << " [OR]"; break;
11123  case 1: cout << " [SUB]";break;
11124  case 2: cout << " [XOR]";break;
11125  case 3: cout << " [AND]";break;
11126  default:
11127  cout << " [RANDOM]";
11128  }
11129  cout << endl;
11130 
11131  switch (rand() % 3)
11132  {
11133  case 0:
11134  size = BITVECT_SIZE / 10;
11135  break;
11136  case 1:
11137  size = BITVECT_SIZE / 2;
11138  break;
11139  default:
11140  size = BITVECT_SIZE - 10;
11141  break;
11142  } // switch
11143 
11144 
11145  bvect_mini* bvect_min1= new bvect_mini(size);
11146  bvect_mini* bvect_min2= new bvect_mini(size);
11147  bvect* bvect_full1= new bvect();
11148  bvect* bvect_full2= new bvect();
11149 
11150  bvect_full1->set_new_blocks_strat(i&1 ? bm::BM_GAP : bm::BM_BIT);
11151  bvect_full2->set_new_blocks_strat(i&1 ? bm::BM_GAP : bm::BM_BIT);
11152 
11153  int opt = rand() % 2;
11154 
11155  unsigned start1 = 0;
11156 
11157  switch (rand() % 3)
11158  {
11159  case 1:
11160  start1 += size / 5;
11161  break;
11162  default:
11163  break;
11164  }
11165 
11166  unsigned start2 = 0;
11167 
11168  switch (rand() % 3)
11169  {
11170  case 1:
11171  start2 += size / 5;
11172  break;
11173  default:
11174  break;
11175  }
11176 
11177  FillSetsRandomMethod(bvect_min1, bvect_full1, start1, size, opt, method);
11178  FillSetsRandomMethod(bvect_min2, bvect_full2, start2, size, opt, method);
11179 
11180  unsigned arr[bm::set_total_blocks]={0,};
11181  bm::id_t cnt = bvect_full1->count();
11182  unsigned last_block = bvect_full1->count_blocks(arr);
11183  unsigned sum = (unsigned)bm::sum_arr(&arr[0], &arr[last_block+1]);
11184 
11185  if (sum != cnt)
11186  {
11187  cout << "Error in function count_blocks." << endl;
11188  cout << "Array sum = " << sum << endl;
11189  cout << "BitCount = " << cnt << endl;
11190  cnt = bvect_full1->count();
11191  for ( i = 0; i <= last_block; ++i)
11192  {
11193  if (arr[i])
11194  {
11195  cout << "[" << i << ":" << arr[i] << "]";
11196  }
11197  }
11198  cout << endl;
11199  cout << "================" << endl;
11200  print_stat(cout, *bvect_full1);
11201 
11202 
11203  exit(1);
11204  }
11205 
11206  bvect::rs_index_type rs_idx1;
11207  bvect_full1->build_rs_index(&rs_idx1);
11208  bvect::rs_index_type rs_idx2;
11209  bvect_full2->build_rs_index(&rs_idx2);
11210 
11211  CheckCountRange(*bvect_full1, rs_idx1, start1, BITVECT_SIZE);
11212  CheckIntervals(*bvect_full1, BITVECT_SIZE);
11213 
11214 
11215  CheckCountRange(*bvect_full2, rs_idx2, start2, BITVECT_SIZE);
11216 
11217  CheckCountRange(*bvect_full1, rs_idx1, 0, start1);
11218  CheckCountRange(*bvect_full2, rs_idx2, 0, start2);
11219 
11220 
11221  TestRandomSubset(*bvect_full1, rsub);
11222  TestRandomSubset(*bvect_full2, rsub);
11223 
11224  // test find first difference
11225  //
11226  TestFindDiff(*bvect_full1, *bvect_full1);
11227 
11228 
11229 
11230 #if(0)
11231  cout << "!!!!!!!!!!!!!!!" << endl;
11232  CheckVectors(*bvect_min1, *bvect_full1, size);
11233  cout << "!!!!!!!!!!!!!!!" << endl;
11234  CheckVectors(*bvect_min2, *bvect_full2, size);
11235  cout << "!!!!!!!!!!!!!!!" << endl;
11236 
11237 
11238  bvect_full1->stat();
11239  cout << " --" << endl;
11240  bvect_full2->stat();
11241 #endif
11242 
11243  int operation = rand()%5;
11244  if (set_operation != -1)
11246 
11247  switch(operation)
11248  {
11249  case 0:
11250  cout << "Operation OR" << endl;
11251  bvect_min1->combine_or(*bvect_min2);
11252  break;
11253 
11254  case 1:
11255  cout << "Operation SUB" << endl;
11256  bvect_min1->combine_sub(*bvect_min2);
11257  break;
11258 
11259  case 2:
11260  cout << "Operation XOR" << endl;
11261  bvect_min1->combine_xor(*bvect_min2);
11262  break;
11263 
11264  default:
11265  cout << "Operation AND" << endl;
11266  bvect_min1->combine_and(*bvect_min2);
11267  break;
11268  }
11269 
11270  int cres1 = bvect_min1->compare(*bvect_min2);
11271 
11272  delete bvect_min2;
11273 
11274  switch(operation)
11275  {
11276  case 0:
11277  {
11278  cout << "Operation OR" << endl;
11279 
11280  bm::id_t predicted_count = bm::count_or(*bvect_full1, *bvect_full2);
11281  bm::id_t predicted_any = bm::any_or(*bvect_full1, *bvect_full2);
11282  if (predicted_any == 0 && predicted_count != 0)
11283  {
11284  cout << "Predicted any error!" << endl;
11285  exit(1);
11286  }
11287 
11288  bvect bv_target_s;
11289  SerializationOperation2Test(&bv_target_s,
11290  *bvect_full1,
11291  *bvect_full2,
11292  predicted_count,
11293  set_COUNT_OR,
11294  set_OR);
11295 
11296  bvect_full1->bit_or(*bvect_full2);
11297 
11298  bm::id_t count = bvect_full1->count();
11299 
11300  if (count != predicted_count)
11301  {
11302  cout << "Predicted count error!" << endl;
11303  cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
11304  exit(1);
11305  }
11306  int res = bvect_full1->compare(bv_target_s);
11307  if (res != 0)
11308  {
11309  cout << "Serialization operation failed!" << endl;
11310  exit(1);
11311  }
11312 
11313  TestAND_OR(rsub, predicted_count, *bvect_full1, *bvect_full2);
11314 
11315  }
11316  break;
11317 
11318  case 1:
11319  {
11320  cout << "Operation SUB" << endl;
11321 
11322  bm::id_t predicted_count = bm::count_sub(*bvect_full1, *bvect_full2);
11323  bm::id_t predicted_any = bm::any_sub(*bvect_full1, *bvect_full2);
11324  if (predicted_any == 0 && predicted_count != 0)
11325  {
11326  cout << "Predicted any error!" << endl;
11327  exit(1);
11328  }
11329 
11330  bvect bv_target_s;
11331  SerializationOperation2Test(&bv_target_s,
11332  *bvect_full1,
11333  *bvect_full2,
11334  predicted_count,
11336  set_SUB);
11337 
11338  bvect_full1->bit_sub(*bvect_full2);
11339 
11340  bm::id_t count = bvect_full1->count();
11341 
11342  if (count != predicted_count)
11343  {
11344  cout << "Predicted count error!" << endl;
11345  cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
11346  exit(1);
11347  }
11348  int res = bvect_full1->compare(bv_target_s);
11349  if (res != 0)
11350  {
11351  cout << "Serialization operation failed!" << endl;
11352  exit(1);
11353  }
11354 
11355 
11356  }
11357  break;
11358 
11359  case 2:
11360  {
11361  cout << "Operation XOR <<<" << endl;
11362 
11363  bm::id_t predicted_count = bm::count_xor(*bvect_full1, *bvect_full2);
11364  bm::id_t predicted_any = bm::any_xor(*bvect_full1, *bvect_full2);
11365  if (predicted_any == 0 && predicted_count != 0)
11366  {
11367  cout << "Predicted any error!" << endl;
11368  exit(1);
11369  }
11370 
11371  bvect bv_target_s;
11372  SerializationOperation2Test(&bv_target_s,
11373  *bvect_full1,
11374  *bvect_full2,
11375  predicted_count,
11376  set_COUNT_XOR,
11377  set_XOR);
11378 
11379  bvect_full1->bit_xor(*bvect_full2);
11380 
11381  bm::id_t count = bvect_full1->count();
11382 
11383  if (count != predicted_count)
11384  {
11385  cout << "Predicted count error!" << endl;
11386  cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
11387  exit(1);
11388  }
11389  int res = bvect_full1->compare(bv_target_s);
11390  if (res != 0)
11391  {
11392  cout << "Serialization operation failed!" << endl;
11393  exit(1);
11394  }
11395 
11396  }
11397 
11398  break;
11399 
11400  default:
11401  {
11402  cout << "Operation AND" << endl;
11403 
11404  bm::id_t predicted_count = bm::count_and(*bvect_full1, *bvect_full2);
11405 
11406  bm::id_t predicted_any = bm::any_and(*bvect_full1, *bvect_full2);
11407  if (predicted_any == 0 && predicted_count != 0)
11408  {
11409  cout << "Predicted any error!" << endl;
11410  exit(1);
11411  }
11412 
11413  bvect bv_target_s;
11414  SerializationOperation2Test(&bv_target_s,
11415  *bvect_full1,
11416  *bvect_full2,
11417  predicted_count,
11418  set_COUNT_AND,
11419  set_AND);
11420 
11421  TestRandomSubset(bv_target_s, rsub);
11422 
11423  TestAND_OR(rsub, predicted_count, *bvect_full1, *bvect_full2);
11424 
11425  bvect bv1(*bvect_full1);
11426 
11427  bvect_full1->bit_and(*bvect_full2);
11428  bm::id_t count = bvect_full1->count();
11429 
11430  int res = bvect_full1->compare(bv_target_s);
11431  if (res != 0)
11432  {
11433  //SaveBVector("bv1.bv", bv1);
11434  //SaveBVector("bv2.bv", *bvect_full2);
11435  cout << "Serialization operation failed!" << endl;
11436  exit(1);
11437  }
11438 
11439  if (count != predicted_count)
11440  {
11441  cout << "Predicted count error!" << endl;
11442  cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
11443  exit(1);
11444  }
11445 
11446  }
11447  break;
11448  }
11449 
11450 
11451 
11452  cout << "Operation comparison" << endl;
11453  CheckVectors(*bvect_min1, *bvect_full1, size, detailed);
11454 
11455  int cres2 = bvect_full1->compare(*bvect_full2);
11456 
11457  //CheckIntervals(*bvect_full1, BITVECT_SIZE);
11458 
11459  if (cres1 != cres2)
11460  {
11461  cout << cres1 << " " << cres2 << endl;
11462  cout << bvect_full1->get_first() << " " << bvect_full1->count() << endl;
11463  cout << bvect_full2->get_first() << " " << bvect_full2->count() << endl;
11464 
11465  cout << endl;
11466  printf("Bitset comparison operation failed.\n");
11467  assert(0); exit(1);
11468  }
11469  if (cres1 == 0) // re-confirm match
11470  {
11471  bvect::size_type pos;
11472  bool f = bvect_full1->find_first_mismatch(*bvect_full2, pos);
11473  if (f)
11474  {
11475  cerr << "Mismatch found pos=" << pos << endl;
11476  assert(0); exit(1);
11477  }
11478  }
11479 
11480 
11481  {
11482  bvect bv1(*bvect_full1);
11483  unsigned idx = unsigned(rand()) % size;
11484  bool b = bv1[idx];
11485  bool changed;
11486  if (b)
11487  {
11488  changed = bv1.set_bit_conditional(idx, true, false);
11489  if (changed)
11490  {
11491  cout << "Set bit conditional failed!" << endl;
11492  exit(1);
11493  }
11494  b = bv1[idx];
11495  if (!b)
11496  {
11497  cout << "Set bit conditional failed!" << endl;
11498  exit(1);
11499  }
11500 
11501  changed = bv1.set_bit_conditional(idx, false, false);
11502  if (changed)
11503  {
11504  cout << "Set bit conditional failed!" << endl;
11505  exit(1);
11506  }
11507  changed = bv1.set_bit_conditional(idx, true, true);
11508  if (changed)
11509  {
11510  cout << "Set bit conditional failed!" << endl;
11511  exit(1);
11512  }
11513  changed = bv1.set_bit_conditional(idx, false, true);
11514  if (!changed)
11515  {
11516  cout << "Set bit conditional failed!" << endl;
11517  exit(1);
11518  }
11519  b = bv1[idx];
11520  if (b)
11521  {
11522  cout << "Set bit conditional failed!" << endl;
11523  exit(1);
11524  }
11525  }
11526  else
11527  {
11528  changed = bv1.set_bit_conditional(idx, false, true);
11529  if (changed)
11530  {
11531  cout << "Set bit conditional failed!" << endl;
11532  exit(1);
11533  }
11534  changed = bv1.set_bit_conditional(idx, true, false);
11535  if (!changed)
11536  {
11537  cout << "Set bit conditional failed!" << endl;
11538  exit(1);
11539  }
11540  b = bv1[idx];
11541  if (!b)
11542  {
11543  cout << "Set bit conditional failed!" << endl;
11544  exit(1);
11545  }
11546  }
11547  }
11548 
11549  {
11550  VisitorAllRangeTest(*bvect_full2, 0); // test with automatic step
11551  }
11552 
11553  delete bvect_full2;
11554 
11555 
11556  struct bvect::statistics st1;
11557  bvect_full1->calc_stat(&st1);
11558  bvect_full1->optimize();
11559  bvect_full1->optimize_gap_size();
11560  struct bvect::statistics st2;
11561  bvect_full1->calc_stat(&st2);
11562 
11563  unsigned Ratio = unsigned((st2.memory_used * 100)/st1.memory_used);
11564  RatioSum+=Ratio;
11565  DeltaSum+=unsigned(st1.memory_used - st2.memory_used);
11566 
11567  cout << "Optimization statistics: " << endl
11568  << " MemUsedBefore=" << st1.memory_used
11569  << " MemUsed=" << st2.memory_used
11570  << " Ratio=" << Ratio << "%"
11571  << " Delta=" << st1.memory_used - st2.memory_used
11572  << endl;
11573 
11574  cout << "Optimization comparison" << endl;
11575 
11576  CheckVectors(*bvect_min1, *bvect_full1, size, detailed);
11577  {
11578  bvect bv(*bvect_full1);
11580  {
11581  bvect::size_type pos;
11582  bool f = bv.find_first_mismatch(*bvect_full1, pos);
11583  if (f)
11584  {
11585  cerr << "Mismatch found pos=" << pos << endl;
11586  assert(0); exit(1);
11587  }
11588  }
11589  }
11590  CheckVectors(*bvect_min1, *bvect_full1, size, detailed);
11591  CheckIntervals(*bvect_full1, BITVECT_SIZE);
11592 
11593 // bvect::rs_index_type rs_idx1;
11594  bvect_full1->build_rs_index(&rs_idx1);
11595  CheckCountRange(*bvect_full1, rs_idx1, 0, size);
11596 
11597 
11598  // Serialization
11599 
11600  bm::serializer<bvect> bv_ser;
11601  bv_ser.gap_length_serialization(false);
11602  bv_ser.byte_order_serialization(false);
11603 
11604  bm::serializer<bvect>::buffer sermem_buf;
11605 
11606  bv_ser.serialize(*bvect_full1, sermem_buf, 0);
11607  unsigned slen = (unsigned)sermem_buf.size();
11608 
11609  delete bvect_full1;
11610 
11611  unsigned SRatio = unsigned((slen*100)/st2.memory_used);
11612  SRatioSum+=SRatio;
11613  SDeltaSum+=unsigned(st2.memory_used) - slen;
11614 
11615 
11616  cout << "Serialized mem_max = " << st2.max_serialize_mem
11617  << " size= " << slen
11618  << " Ratio=" << SRatio << "%"
11619  << " Delta=" << st2.memory_used - slen
11620  << endl;
11621 
11622  bvect* bvect_full3= new bvect();
11623 
11624  bm::serializer<bvect>::buffer new_sermem_buf;
11625  new_sermem_buf = sermem_buf;
11626  cout << "Deserialization...";
11627 
11628  bm::deserialize(*bvect_full3, new_sermem_buf.buf());
11629 
11630  bm::deserialize(bvtotal, new_sermem_buf.buf());
11632  {
11633  int res;
11634  bvect bv_ac;
11635  bvect bv_a;
11636  bv_a.invert(); bv_ac.invert();
11637  bm::deserialize(bv_a, new_sermem_buf.buf());
11638  res = bv_a.compare(bv_ac);
11639 
11640  od.deserialize(bv_a,
11641  new_sermem_buf.buf(),
11642  0,
11643  set_OR);
11644  res = bv_a.compare(bv_ac);
11645  assert(res == 0);
11646  }
11647 
11648  bvect* bv_target_s=new bvect();
11649  od.deserialize(*bv_target_s,
11650  new_sermem_buf.buf(),
11651  0,
11652  set_OR);
11653 
11654  cout << "Ok." << endl;
11655 // delete [] new_sermem;
11656 
11657  cout << "Optimization...";
11658  bvtotal.optimize();
11659  cout << "Ok." << endl;
11660 
11661  ++clear_count;
11662 
11663  if (clear_count == 4)
11664  {
11665  bvtotal.clear();
11666  clear_count = 0;
11667  }
11668 
11669  cout << "Serialization comparison" << endl;
11670 
11671  {
11672  bvect::size_type pos;
11673  bool f = bv_target_s->find_first_mismatch(*bvect_full3, pos);
11674  if (f)
11675  {
11676  cerr << "Mismatch found pos=" << pos << endl;
11677  assert(0); exit(1);
11678  }
11679  }
11680  CheckVectors(*bvect_min1, *bvect_full3, size, detailed);
11681 
11682  int res = bv_target_s->compare(*bvect_full3);
11683  if (res != 0)
11684  {
11685  CheckVectors(*bvect_min1, *bv_target_s, size, true);
11686  }
11687 
11688  delete bv_target_s;
11689  delete bvect_min1;
11690  delete bvect_full3;
11691 
11692  }
11693 
11694  --i;
11695  cout << "Repetitions:" << i <<
11696  " AVG optimization ratio:" << RatioSum/i
11697  << " AVG Delta:" << DeltaSum/i
11698  << endl
11699  << " AVG serialization Ratio:"<< SRatioSum/i
11700  << " Delta:" << SDeltaSum/i
11701  << endl;
11702 }
11703 
11704 static
11706 {
11707  bm::gap_word_t dgap_buf[bm::gap_max_buff_len+3];
11708  bm::gap_word_t gap_buf[bm::gap_max_buff_len+3] = {0, };
11709 
11710  bm::gap_2_dgap(gapv.get_buf(), dgap_buf);
11711  bm::dgap_2_gap(dgap_buf, gap_buf);
11712 
11713  int c = bm::gapcmp(gap_buf, gapv.get_buf());
11714  if (c != 0)
11715  {
11716  cout << "Gap1: ";
11717  PrintGap(cout, gapv.get_buf());
11718  cout << "D-Gap: ";
11719  PrintGap(cout, dgap_buf);
11720  cout << "Gap2:";
11721  PrintGap(cout, gap_buf);
11722 
11723  cout << "DGap conversion failed!" << endl;
11724  exit(1);
11725  }
11726 }
11727 
11728 static
11729 void GAPCheck()
11730 {
11731  cout << "-------------------------------------------GAPCheck" << endl;
11732 
11733  {
11734 
11735  gap_vector gapv(0);
11736  bvect_mini bvect_min(bm::gap_max_bits);
11737 
11738  unsigned i;
11739  for( i = 0; i < 454; ++i)
11740  {
11741  bvect_min.set_bit(i);
11742  gapv.set_bit(i);
11743  }
11744 
11745  for(i = 0; i < 254; ++i)
11746  {
11747  bvect_min.clear_bit(i);
11748  gapv.clear_bit(i);
11749  }
11750 
11751  for(i = 5; i < 10; ++i)
11752  {
11753  bvect_min.set_bit(i);
11754  gapv.set_bit(i);
11755  }
11756 
11757  for( i = 0; i < bm::gap_max_bits; ++i)
11758  {
11759  int bit1 = (gapv.is_bit_true(i) == 1);
11760  int bit2 = (bvect_min.is_bit_true(i) != 0);
11761  int bit3 = (gapv.test(i) == 1);
11762  if (bit1 != bit2)
11763  {
11764  cout << "problem with bit comparison " << i << endl;
11765  exit(1);
11766  }
11767  if (bit1 != bit3)
11768  {
11769  cout << "problem with bit test comparison " << i << endl;
11770  exit(1);
11771  }
11772 
11773  }
11774 
11775  }
11776 
11777 
11778  {
11779  gap_vector gapv(1);
11780  int bit = gapv.is_bit_true(65535);
11781 
11782  if (bit != 1)
11783  {
11784  cout << "Bit != 1" << endl;
11785  exit(1);
11786  }
11787 
11788  unsigned i;
11789  for (i = 0; i < 65536; ++i)
11790  {
11791  bit = gapv.is_bit_true(i);
11792  if (bit != 1)
11793  {
11794  cout << "2.Bit != 1" << endl;
11795  exit(1);
11796  }
11797  }
11798  unsigned cnt = gapv.count_range(0, 65535);
11799  if (cnt != 65536)
11800  {
11801  cout << "count_range failed:" << cnt << endl;
11802  exit(1);
11803  }
11804 
11805  CheckCountGapRange(gapv, 10, 20);
11806  CheckCountGapRange(gapv, 0, 20);
11807 
11808  CheckGap2DGap(gapv);
11809 
11810  printf("gapv 1 check ok\n");
11811  }
11812 
11813  {
11814  gap_vector gapv(0);
11815 
11816 
11817  int bit = gapv.is_bit_true(65535);
11818  int bit1 = gapv.test(65535);
11819  if(bit != 0)
11820  {
11821  cout << "Bit != 0" << endl;
11822  exit(1);
11823  }
11824 
11825  unsigned i;
11826  for (i = 0; i < 65536; ++i)
11827  {
11828  bit = gapv.is_bit_true(i);
11829  bit1 = gapv.test(i);
11830  if (bit != 0)
11831  {
11832  cout << "2.Bit != 0 bit =" << i << endl;
11833  exit(1);
11834  }
11835  if (bit1 != 0)
11836  {
11837  cout << "2.Bit test != 0 bit =" << i << endl;
11838  exit(1);
11839  }
11840  }
11841  unsigned cnt = gapv.count_range(0, 65535);
11842  if (cnt != 0)
11843  {
11844  cout << "count_range failed:" << cnt << endl;
11845  exit(1);
11846  }
11847  CheckCountGapRange(gapv, 10, 20);
11848  CheckCountGapRange(gapv, 0, 20);
11849 
11850  CheckGap2DGap(gapv);
11851 
11852 
11853 
11854  printf("gapv 2 check ok\n");
11855  }
11856 
11857  {
11858  gap_vector gapv(0);
11859 
11860  gapv.set_bit(1);
11861  gapv.set_bit(0);
11862 
11863  gapv.control();
11864  CheckCountGapRange(gapv, 0, 20);
11865 
11866  int bit = gapv.is_bit_true(0);
11867 
11868  if (bit != 1)
11869  {
11870  cout << "Trouble" << endl;
11871  exit(1);
11872  }
11873 
11874  bit = gapv.is_bit_true(1);
11875  if (bit != 1)
11876  {
11877  cout << "Trouble 2." << endl;
11878  exit(1);
11879  }
11880 
11881 
11882  bit = gapv.is_bit_true(2);
11883  if(bit != 0)
11884  {
11885  cout << "Trouble 3." << endl;
11886  exit(1);
11887  }
11888 
11889  CheckGap2DGap(gapv);
11890 
11891  }
11892 
11893  {
11894  gap_vector gapv(0);
11895 
11896  gapv.set_bit(0);
11897  gapv.control();
11898  gapv.set_bit(1);
11899  gapv.control();
11900 
11901  gapv.set_bit(4);
11902  gapv.control();
11903  gapv.set_bit(5);
11904  gapv.control();
11905  CheckCountGapRange(gapv, 4, 5);
11906  CheckCountGapRange(gapv, 3, 5);
11907 
11908  gapv.set_bit(3);
11909  CheckCountGapRange(gapv, 3, 3);
11910  CheckCountGapRange(gapv, 3, 5);
11911 
11912  gapv.control();
11913 
11914  int bit = gapv.is_bit_true(0);
11915  if(bit!=1)
11916  {
11917  cout << "Bug" << endl;
11918  }
11919  bit = gapv.is_bit_true(1);
11920  if(bit!=1)
11921  {
11922  cout << "Bug2" << endl;
11923  }
11924 
11925  gapv.control();
11926  gapv.set_bit(4);
11927  gapv.control();
11928 
11929  CheckGap2DGap(gapv);
11930 
11931 
11932  printf("gapv 3 check ok\n");
11933  }
11934 
11935  {
11936  gap_vector gapv(0);
11937  bvect_mini bvect_min(bm::gap_max_bits);
11938 
11939  cout << "++++++1" << endl;
11940  print_gap(gapv, 10);
11941 
11942  gapv.set_bit(bm::gap_max_bits-1);
11943  gapv.control();
11944  print_gap(gapv, 10);
11945 
11946 
11947  bvect_min.set_bit(bm::gap_max_bits-1);
11948 
11949  cout << "++++++3" << endl;
11950 
11951  gapv.set_bit(5);
11952  print_gap(gapv,15);
11953  gapv.control();
11954  bvect_min.set_bit(5);
11955 
11956  cout << "++++++4" << endl;
11957 
11958  CheckCountGapRange(gapv, 13, 150);
11959  gapv.control();
11960 
11961  CheckGap2DGap(gapv);
11962 
11963 
11964  unsigned i;
11965  for (i = 0; i < bm::gap_max_bits; ++i)
11966  {
11967  if (i == 65535)
11968  printf("%i\n", i);
11969  int bit1 = (gapv.is_bit_true(i) == 1);
11970  int bit2 = (bvect_min.is_bit_true(i) != 0);
11971  int bit3 = (gapv.test(i) == 1);
11972  if (bit1 != bit2)
11973  {
11974  cout << "problem with bit comparison " << i << endl;
11975  }
11976  if (bit1 != bit3)
11977  {
11978  cout << "problem with bit test comparison " << i << endl;
11979  }
11980 
11981  }
11982 
11983  gapv.clear_bit(5);
11984  bvect_min.clear_bit(5);
11985  gapv.control();
11986 
11987  CheckGap2DGap(gapv);
11988 
11989 
11990  for ( i = 0; i < bm::gap_max_bits; ++i)
11991  {
11992  if (i == 65535)
11993  printf("%i\n", i);
11994  int bit1 = (gapv.is_bit_true(i) == 1);
11995  int bit2 = (bvect_min.is_bit_true(i) != 0);
11996  int bit3 = (gapv.test(i) == 1);
11997  if (bit1 != bit2)
11998  {
11999  cout << "2.problem with bit comparison " << i << endl;
12000  }
12001  if (bit1 != bit3)
12002  {
12003  cout << "2.problem with bit test comparison " << i << endl;
12004  }
12005  }
12006  printf("gapv check 4 ok.\n");
12007  }
12008 
12009  {
12010  gap_vector gapv(0);
12011  bvect_mini bvect_min(65536);
12012 
12013  unsigned i;
12014  for (i = 10; i > 0; i-=2)
12015  {
12016  bvect_min.set_bit(i);
12017  gapv.set_bit(i);
12018  gapv.control();
12019  CheckCountGapRange(gapv, 0, i);
12020 
12021 
12022  int bit1 = (gapv.is_bit_true(i) == 1);
12023  int bit2 = (bvect_min.is_bit_true(i) != 0);
12024  int bit3 = (gapv.test(i) != 0);
12025  if (bit1 != bit2)
12026  {
12027  cout << "3.problem with bit comparison " << i << endl;
12028  }
12029  if (bit1 != bit3)
12030  {
12031  cout << "3.problem with bit test comparison " << i << endl;
12032  }
12033 
12034  CheckGap2DGap(gapv);
12035 
12036 
12037  }
12038  for (i = 0; i < (int)bm::gap_max_bits; ++i)
12039  {
12040  int bit1 = (gapv.is_bit_true(i) == 1);
12041  int bit2 = (bvect_min.is_bit_true(i) != 0);
12042  int bit3 = (gapv.test(i) == 1);
12043 
12044  if (bit1 != bit2)
12045  {
12046  cout << "3.problem with bit comparison " << i << endl;
12047  }
12048  if (bit1 != bit3)
12049  {
12050  cout << "3.problem with bit test comparison " << i << endl;
12051  }
12052  }
12053  printf("gapv check 5 ok.\n");
12054  }
12055 
12056  {
12057  gap_vector gapv(0);
12058  bvect_mini bvect_min(bm::gap_max_bits);
12059 
12060  int i;
12061  for (i = 0; i < 25; ++i)
12062  {
12063  unsigned id = random_minmax(0, bm::gap_max_bits);
12064  bvect_min.set_bit(id);
12065  gapv.set_bit(id);
12066  gapv.control();
12067  CheckCountGapRange(gapv, 0, id);
12068  CheckCountGapRange(gapv, id, 65535);
12069 
12070  CheckGap2DGap(gapv);
12071 
12072  }
12073 
12074  for (i = 0; i < (int)bm::gap_max_bits; ++i)
12075  {
12076  unsigned idx = unsigned(i);
12077  int bit1 = (gapv.is_bit_true(idx) == 1);
12078  int bit2 = (bvect_min.is_bit_true(idx) != 0);
12079  if (bit1 != bit2)
12080  {
12081  cout << "4.problem with bit comparison " << i << endl;
12082  }
12083  }
12084 
12085  for (i = bm::gap_max_bits; i < 0; --i)
12086  {
12087  unsigned idx = unsigned(i);
12088  int bit1 = (gapv.is_bit_true(idx) == 1);
12089  int bit2 = (bvect_min.is_bit_true(idx) != 0);
12090  if (bit1 != bit2)
12091  {
12092  cout << "5.problem with bit comparison " << i << endl;
12093  }
12094  }
12095  printf("gapv check 6 ok.\n");
12096 
12097  }
12098 
12099  printf("gapv random bit set check ok.\n");
12100 
12101 
12102  // conversion functions test
12103 
12104  {
12105  // aligned position test
12106  bvect bvect_a;
12107 
12108  bvect_a.set_bit(1);
12109  bvect_a.set_bit(1, false);
12110  //bvect_a.clear();
12111 
12112 
12113  unsigned* buf = (unsigned*) bvect_a.get_blocks_manager().get_block_ptr(0, 0);
12114 
12115  bm::or_bit_block(buf, 0, 4);
12116  unsigned cnt = bm::bit_block_calc_count_range(buf, 0, 3);
12117  assert(cnt == 4);
12118 
12119  bool bit = bvect_a.get_bit(0);
12120  assert(bit);
12121  bit = bvect_a.get_bit(1);
12122  assert(bit);
12123  bit = bvect_a.get_bit(2);
12124  assert(bit);
12125  bit = bvect_a.get_bit(3);
12126  assert(bit);
12127  bit = bvect_a.get_bit(4);
12128  assert(bit==0);
12129 
12130  bm::or_bit_block(buf, 0, 36);
12132  assert(cnt == 36);
12133 
12134  for (unsigned i = 0; i < 36; ++i)
12135  {
12136  bit = (bvect_a.get_bit(i) != 0);
12137  assert(bit);
12138  }
12139  bit = (bvect_a.get_bit(36) != 0);
12140  assert(bit==0);
12141 
12142  unsigned count = bvect_a.recalc_count();
12143  assert(count == 36);
12144 
12145  cout << "Aligned position test ok." << endl;
12146 
12147  }
12148 
12149 
12150  {
12151  // unaligned position test
12152  bvect bvect_u;
12153 
12154  bvect_u.set_bit(0);
12155  bvect_u.set_bit(0, false);
12156 // bvect_u.clear();
12157 
12158  unsigned* buf = (unsigned*) bvect_u.get_blocks_manager().get_block_ptr(0, 0);
12159 
12160  bm::or_bit_block(buf, 5, 32);
12161  bool bit = (bvect_u.get_bit(4) != 0);
12162  assert(bit==0);
12163  unsigned cnt = bm::bit_block_calc_count_range(buf, 5, 5+32-1);
12164  assert(cnt == 32);
12166  assert(cnt == 32);
12167 
12168  unsigned i;
12169  for (i = 5; i < 4 + 32; ++i)
12170  {
12171  bit = bvect_u.get_bit(i);
12172  assert(bit);
12173  }
12174  unsigned count = bvect_u.recalc_count();
12175  assert(count==32);
12176 
12177  cout << "Unaligned position ok." << endl;
12178 
12179  }
12180 
12181  // random test
12182  {
12183  cout << "random test" << endl;
12184 
12185  bvect bvect_r;
12186 
12187  bvect_r.set_bit(0);
12188  bvect_r.set_bit(0, false);
12189  //bvect_r.clear();
12190 
12191  for (int i = 0; i < 5000; ++i)
12192  {
12193  unsigned* buf = (unsigned*) bvect_r.get_blocks_manager().get_block_ptr(0, 0);
12194  assert(buf);
12195  unsigned start = (unsigned)rand() % 65535;
12196  unsigned end = (unsigned)rand() % 65535;
12197  if (start > end)
12198  {
12199  unsigned tmp = end;
12200  end = start;
12201  start = tmp;
12202  }
12203  unsigned len = end - start;
12204  if (len)
12205  {
12206  bm::or_bit_block(buf, start, len);
12207  unsigned cnt = bm::bit_block_calc_count_range(buf, start, end);
12208  if (cnt != len)
12209  {
12210  cout << "random test: count_range comparison failed. "
12211  << " LEN = " << len << " cnt = " << cnt
12212  << endl;
12213  exit(1);
12214  }
12215 
12216  unsigned count = bvect_r.recalc_count();
12217 
12218  if (count != len)
12219  {
12220  cout << "random test: count comparison failed. "
12221  << " LEN = " << len << " count = " << count
12222  << endl;
12223  assert(0); exit(1);
12224  }
12225 
12226  for (unsigned j = start; j < end; ++j)
12227  {
12228  bool bit = bvect_r.get_bit(j);
12229  if (!bit)
12230  {
12231  cout << "random test: bit comparison failed. bit#"
12232  << i << endl;
12233  exit(1);
12234  }
12235  } // for j
12236 
12237  }
12238  bvect_r.clear();
12239  bvect_r.set_bit(0);
12240  bvect_r.set_bit(0, false);
12241 
12242  if ((i % 100)==0)
12243  {
12244  cout << "*" << flush;
12245  }
12246  } // for i
12247 
12248  cout << endl << "Random test Ok." << endl;
12249 
12250  }
12251 
12252 
12253  // conversion test
12254 
12255  cout << "Conversion test" << endl;
12256 
12257  {
12258 
12259  gap_vector gapv(0);
12260  bvect bvect_a;
12261 
12262  gapv.set_bit(0);
12263  gapv.set_bit(2);
12264  gapv.set_bit(10);
12265  gapv.set_bit(11);
12266  gapv.set_bit(12);
12267 
12268  CheckCountGapRange(gapv, 3, 15);
12269 
12270  print_gap(gapv, 100);
12271  bvect_a.set_bit(0);
12272  bvect_a.set_bit(0, false);
12273  //bvect_a.clear();
12274 
12275  unsigned* buf = (unsigned*) bvect_a.get_blocks_manager().get_block_ptr(0, 0);
12276 
12277  gapv.convert_to_bitset(buf);
12278 
12279 
12280  unsigned bitcount = bvect_a.recalc_count();
12281 
12282 
12283  if (bitcount != 5)
12284  {
12285  cout << "test failed: bitcout = " << bitcount << endl;
12286  exit(1);
12287  }
12288 
12289 
12290  gap_vector gapv1(0);
12291  gap_word_t* gap_buf = gapv1.get_buf();
12292  *gap_buf = 0;
12294  print_gap(gapv1, 100);
12295 
12296  bitcount = gapv1.bit_count();
12297  if(bitcount != 5)
12298  {
12299  cout << "2.test_failed: bitcout = " << bitcount << endl;
12300  exit(1);
12301  }
12302 
12303  printf("conversion test ok.\n");
12304 
12305  }
12306 
12307  // gap AND test
12308 
12309  {
12310  // special case 1: operand is all 1
12311  gap_vector gapv1(0);
12312  gapv1.set_bit(2);
12313  gap_vector gapv2(1);
12314 
12315  gapv1.combine_and(gapv2.get_buf());
12316  gapv1.control();
12317  print_gap(gapv1, 0);
12318 
12319  unsigned count = gapv1.bit_count();
12320  assert(count == 1);
12321  int bit = gapv1.is_bit_true(2);
12322  if(bit == 0)
12323  {
12324  cout << "Wrong bit" << endl;
12325  exit(1);
12326  }
12327  CheckCountGapRange(gapv1, 0, 17);
12328 
12329  }
12330 
12331  {
12332  // special case 2: src is all 1
12333  gap_vector gapv1(1);
12334  gap_vector gapv2(0);
12335  gapv2.set_bit(2);
12336 
12337  gapv1.combine_and(gapv2.get_buf());
12338  gapv1.control();
12339  print_gap(gapv1, 0);
12340 
12341  unsigned count = gapv1.bit_count();
12342  assert(count == 1);
12343  int bit = gapv1.is_bit_true(2);
12344  assert(bit);
12345 
12346  }
12347 
12348  {
12349  gap_vector gapv;
12350  gap_vector gapv1(0);
12351 
12352  gapv1.set_bit(3);
12353  gapv1.set_bit(4);
12354  print_gap(gapv1, 0);
12355 
12356  gap_vector gapv2(0);
12357  gapv2.set_bit(2);
12358  gapv2.set_bit(3);
12359  print_gap(gapv2, 0);
12360 
12361  unsigned dsize=0;
12362  bm::gap_buff_op<bm::gap_word_t, bm::and_func>((gap_word_t*)gapv.get_buf(),
12363  gapv1.get_buf(), 0,
12364  gapv2.get_buf(), 0,
12365  dsize);
12366  print_gap(gapv, 0);
12367  gapv.control();
12368 
12369 
12370  int bit1 = (gapv.is_bit_true(3) == 1);
12371  if(bit1 == 0)
12372  {
12373  cout << "Checking failed." << endl;
12374  exit(0);
12375  }
12376 
12377  gapv1.combine_or(gapv2);
12378  print_gap(gapv1, 0);
12379  gapv1.control();
12380 
12381  }
12382 
12383  {
12384  printf("gap AND test 1.\n");
12385  gap_vector gapv1(0);
12386  gap_vector gapv2(0);
12387  bvect_mini bvect_min1(65536);
12388  bvect_mini bvect_min2(65536);
12389 
12390  gapv1.set_bit(65535);
12391  bvect_min1.set_bit(65535);
12392  gapv1.set_bit(4);
12393  bvect_min1.set_bit(4);
12394 
12395  gapv2.set_bit(65535);
12396  bvect_min2.set_bit(65535);
12397  gapv2.set_bit(3);
12398  bvect_min2.set_bit(3);
12399  CheckCountGapRange(gapv2, 3, 65535);
12400 
12401  gapv2.control();
12402 
12403  printf("vect1:"); print_gap(gapv1, 0);
12404  printf("vect2:");print_gap(gapv2, 0);
12405 
12406  gapv1.combine_and(gapv2.get_buf());
12407  printf("vect1:");print_gap(gapv1, 0);
12408 
12409  gapv1.control();
12410  unsigned bit1 = (unsigned)gapv1.is_bit_true(65535u);
12411  assert(bit1);
12412 
12413  bvect_min1.combine_and(bvect_min2);
12414  CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
12415  }
12416 
12417  {
12418  printf("gap random AND test.\n");
12419  gap_vector gapv1(0);
12420  gap_vector gapv2(0);
12421  bvect_mini bvect_min1(65536);
12422  bvect_mini bvect_min2(65536);
12423 
12424  int i;
12425  for (i = 0; i < 25; ++i)
12426  {
12427  unsigned id = random_minmax(0, 65535);
12428  bvect_min1.set_bit(id);
12429  gapv1.set_bit(id);
12430  gapv1.control();
12431  CheckCountGapRange(gapv1, 0, id);
12432  CheckCountGapRange(gapv1, id, 65535);
12433  }
12434  for (i = 0; i < 25; ++i)
12435  {
12436  unsigned id = random_minmax(0, 65535);
12437  bvect_min2.set_bit(id);
12438  gapv2.set_bit(id);
12439  gapv2.control();
12440  }
12441 
12442  gapv1.combine_and(gapv2.get_buf());
12443  gapv1.control();
12444  gapv2.control();
12445  bvect_min1.combine_and(bvect_min2);
12446 
12447  CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
12448 
12449  printf("gap random AND test ok.\n");
12450 
12451  }
12452 
12453  {
12454  printf("gap OR test.\n");
12455 
12456  gap_vector gapv1(0);
12457  gap_vector gapv2(0);
12458 
12459  gapv1.set_bit(2);
12460  gapv2.set_bit(3);
12461 
12462  gapv1.combine_or(gapv2);
12463  gapv1.control();
12464  print_gap(gapv1, 0);
12465  int bit1 = (gapv1.is_bit_true(0) == 1);
12466  assert(bit1==0);
12467  bit1=(gapv1.is_bit_true(2) == 1);
12468  assert(bit1);
12469  bit1=(gapv1.is_bit_true(3) == 1);
12470  assert(bit1);
12471  }
12472 
12473  {
12474  printf("gap XOR test.\n");
12475 
12476  gap_vector gapv1(0);
12477  gap_vector gapv2(0);
12478 
12479  gapv1.set_bit(2);
12480  gapv2.set_bit(3);
12481  gapv1.set_bit(4);
12482  gapv2.set_bit(4);
12483  print_gap(gapv1, 0);
12484  print_gap(gapv2, 0);
12485 
12486  gapv1.combine_xor(gapv2);
12487  gapv1.control();
12488  print_gap(gapv1, 0);
12489  int bit1 = (gapv1.is_bit_true(0) == 0);
12490  assert(bit1);
12491  bit1=(gapv1.is_bit_true(2) == 1);
12492  assert(bit1);
12493  bit1=(gapv1.is_bit_true(3) == 1);
12494  assert(bit1);
12495  bit1=(gapv1.is_bit_true(4) == 0);
12496  assert(bit1);
12497 
12498  }
12499 
12500 
12501  {
12502  unsigned i;
12503  printf("gap random OR test.\n");
12504  gap_vector gapv1(0);
12505  gap_vector gapv2(0);
12506  bvect_mini bvect_min1(bm::gap_max_bits);
12507  bvect_mini bvect_min2(bm::gap_max_bits);
12508 
12509  for (i = 0; i < 10; ++i)
12510  {
12511  unsigned id = random_minmax(0, 100);
12512  bvect_min1.set_bit(id);
12513  gapv1.set_bit(id);
12514  gapv1.control();
12515  }
12516  for (i = 0; i < 10; ++i)
12517  {
12518  unsigned id = random_minmax(0, 100);
12519  bvect_min2.set_bit(id);
12520  gapv2.set_bit(id);
12521  gapv2.control();
12522  }
12523 
12524  print_mv(bvect_min1, 64);
12525  print_mv(bvect_min2, 64);
12526 
12527  gapv1.combine_or(gapv2);
12528  gapv1.control();
12529  gapv2.control();
12530  bvect_min1.combine_or(bvect_min2);
12531 
12532  print_mv(bvect_min1, 64);
12533 
12534  CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
12535 
12536  printf("gap random OR test ok.\n");
12537 
12538  }
12539 
12540 
12541  {
12542  unsigned i;
12543  printf("gap random SUB test.\n");
12544  gap_vector gapv1(0);
12545  gap_vector gapv2(0);
12546  bvect_mini bvect_min1(bm::gap_max_bits);
12547  bvect_mini bvect_min2(bm::gap_max_bits);
12548 
12549  for (i = 0; i < 25; ++i)
12550  {
12551  unsigned id = random_minmax(0, 100);
12552  bvect_min1.set_bit(id);
12553  gapv1.set_bit(id);
12554  gapv1.control();
12555  }
12556  for (i = 0; i < 25; ++i)
12557  {
12558  unsigned id = random_minmax(0, 100);
12559  bvect_min2.set_bit(id);
12560  gapv2.set_bit(id);
12561  gapv2.control();
12562  }
12563 
12564  print_mv(bvect_min1, 64);
12565  print_mv(bvect_min2, 64);
12566 
12567  gapv1.combine_sub(gapv2);
12568  gapv1.control();
12569  gapv2.control();
12570  bvect_min1.combine_sub(bvect_min2);
12571 
12572  print_mv(bvect_min1, 64);
12573 
12574  CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
12575 
12576  printf("gap random SUB test ok.\n");
12577  }
12578 
12579  {
12580  printf("GAP comparison test.\n");
12581 
12582  gap_vector gapv1(0);
12583  gap_vector gapv2(0);
12584 
12585  gapv1.set_bit(3);
12586  gapv2.set_bit(3);
12587 
12588  int res = gapv1.compare(gapv2);
12589  if (res != 0)
12590  {
12591  printf("GAP comparison failed!");
12592  exit(1);
12593  }
12594 
12595  gapv1.set_bit(4);
12596  gapv2.set_bit(4);
12597 
12598  res = gapv1.compare(gapv2);
12599  if (res != 0)
12600  {
12601  printf("GAP comparison failed!");
12602  exit(1);
12603  }
12604 
12605  gapv1.set_bit(0);
12606  gapv1.set_bit(1);
12607 
12608  res = gapv1.compare(gapv2);
12609  if (res != 1)
12610  {
12611  printf("GAP comparison failed!");
12612  exit(1);
12613  }
12614 
12615  gapv2.set_bit(0);
12616  gapv2.set_bit(1);
12617  res = gapv1.compare(gapv2);
12618  if (res != 0)
12619  {
12620  printf("GAP comparison failed!");
12621  exit(1);
12622  }
12623 
12624  gapv1.clear_bit(1);
12625 
12626  res = gapv1.compare(gapv2);
12627  if (res != -1)
12628  {
12629  printf("GAP comparison failed!");
12630  exit(1);
12631  }
12632 
12633 
12634  }
12635 }
12636 
12637 // -----------------------------------------------------------------------------
12638 static
12640  bvect& bv1,
12641  unsigned min,
12642  unsigned max,
12643  unsigned fill_factor)
12644 {
12645  bvect::bulk_insert_iterator bii0(bv0);
12646  for (unsigned i = min; i < max; i += fill_factor)
12647  {
12648  bii0 = i;
12649  bv1.set(i);
12650  } // for i
12651  bii0.flush();
12652 }
12653 
12654 static
12656 {
12657  cout << "----------------------------------- GAP test stress " << endl;
12658  const unsigned BV_SIZE = 65535 * 3;
12659 
12660  for (unsigned ff = 64; ff < 10000; ff++)
12661  {
12662  bvect bv0, bv1;
12663  SimpleGapFillSets(bv0, bv1, 0, BV_SIZE, ff);
12664  bv0.optimize();
12665  for (unsigned i = 0; i < BV_SIZE+1; ++i)
12666  {
12667  bool b0 = bv0.test(i);
12668  bool b1 = bv1.test(i);
12669  if (b0 != b1)
12670  {
12671  cerr << "GAP Test Stress failure!" << " FillFactor=" << ff << " bit=" << i << endl;
12672  exit(1);
12673  }
12674  } // for i
12675  bool b = bv0.equal(bv1);
12676  assert(b);
12677  if (ff % 100 == 0)
12678  {
12679  cout << "*" << flush;
12680  }
12681  } // for j
12682 
12683  cout << "----------------------------------- GAP test stress " << endl;
12684 }
12685 
12686 // -----------------------------------------------------------------------------
12687 static
12689 {
12690 
12691  cout << "--------------------------------- MutationTest" << endl;
12692  {
12693  bvect_mini bvect_min(BITVECT_SIZE);
12694  bvect bvect_full;
12695 
12696  printf("\nMutation test.\n");
12697 
12698  bvect_full.set_new_blocks_strat(bm::BM_GAP);
12699 
12700  bvect_full.set_bit(5);
12701  bvect_full.set_bit(5);
12702 
12703  bvect_min.set_bit(5);
12704 
12705  bvect_full.set_bit(65535);
12706  bvect_full.set_bit(65537);
12707  bvect_min.set_bit(65535);
12708  bvect_min.set_bit(65537);
12709 
12710  bvect_min.set_bit(100000);
12711  bvect_full.set_bit(100000);
12712 
12713  // detailed vectors verification
12714  ::CheckVectors(bvect_min, bvect_full, ITERATIONS, false);
12715 
12716  unsigned i;
12717  for (i = 5; i < 20000; i += 3)
12718  {
12719  bvect_min.set_bit(i);
12720  bvect_full.set_bit(i);
12721  }
12722  ::CheckVectors(bvect_min, bvect_full, ITERATIONS, false);
12723 
12724  for (i = 100000; i < 200000; i += 3)
12725  {
12726  bvect_min.set_bit(i);
12727  bvect_full.set_bit(i);
12728  }
12729 
12730  ::CheckVectors(bvect_min, bvect_full, 300000);
12731  }
12732  // set-clear functionality
12733 
12734  {
12735  printf("Set-clear functionality test.");
12736 
12737  bvect_mini bvect_min(BITVECT_SIZE);
12738  bvect bvect_full;
12739  bvect_full.set_new_blocks_strat(bm::BM_GAP);
12740 
12741  unsigned i;
12742  for (i = 100000; i < 100010; ++i)
12743  {
12744  bvect_min.set_bit(i);
12745  bvect_full.set_bit(i);
12746  }
12747  ::CheckVectors(bvect_min, bvect_full, 300000);
12748 
12749  for (i = 100000; i < 100010; ++i)
12750  {
12751  bvect_min.clear_bit(i);
12752  bvect_full.clear_bit(i);
12753  }
12754  ::CheckVectors(bvect_min, bvect_full, 300000);
12755 
12756  bvect_full.optimize();
12757  CheckVectors(bvect_min, bvect_full, 65536);//max+10);
12758  }
12759 
12760 }
12761 
12762 static
12764 {
12765 
12766  cout << "------------------------------------ MutationOperationsTest" << endl;
12767 
12768  printf("Mutation operations test 1.\n");
12769  {
12770  bvect_mini bvect_min1(BITVECT_SIZE);
12771  bvect_mini bvect_min2(BITVECT_SIZE);
12772  bvect bvect_full1;
12773  bvect bvect_full2;
12774 
12775  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
12776  bvect_full2.set_new_blocks_strat(bm::BM_BIT);
12777 
12778  bvect_full1.set_bit(100);
12779  bvect_min1.set_bit(100);
12780 
12781  unsigned i;
12782  for(i = 0; i < 10000; i+=2)
12783  {
12784  bvect_full2.set_bit(i);
12785  bvect_min2.set_bit(i);
12786  }
12787  print_stat(cout,bvect_full2);
12788  CheckVectors(bvect_min2, bvect_full2, 65536, true);
12789 
12790  bvect_min1.combine_and(bvect_min2);
12791  bvect_full1.bit_and(bvect_full2);
12792 
12793  CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
12794 
12795  }
12796 
12797  printf("Mutation operations test 2.\n");
12798  {
12799  unsigned delta = 65536;
12800  bvect_mini bvect_min1(BITVECT_SIZE);
12801  bvect_mini bvect_min2(BITVECT_SIZE);
12802  bvect bvect_full1;
12803  bvect bvect_full2;
12804 
12805  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
12806  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
12807 
12808  unsigned i;
12809  for(i = 0; i < 1000; i+=1)
12810  {
12811  bvect_full1.set_bit(delta+i);
12812  bvect_min1.set_bit(delta+i);
12813  }
12814 
12815  for(i = 0; i < 100; i+=2)
12816  {
12817  bvect_full2.set_bit(delta+i);
12818  bvect_min2.set_bit(delta+i);
12819  }
12820 // CheckVectors(bvect_min2, bvect_full2, 65536);
12821 
12822  bvect_min1.combine_and(bvect_min2);
12823  bvect_full1.bit_and(bvect_full2);
12824 
12825  CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
12826  bvect_full1.optimize();
12827  CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
12828 
12829  }
12830 
12831  {
12832  bvect_mini bvect_min1(BITVECT_SIZE);
12833  bvect bvect_full1;
12834 
12835  bvect_full1.set_bit(3);
12836  bvect_min1.set_bit(3);
12837 
12838  struct bvect::statistics st;
12839  bvect_full1.calc_stat(&st);
12840 
12841  // serialization
12842 
12844 
12845  unsigned char* sermem = new unsigned char[st.max_serialize_mem];
12846  size_t slen = bm::serialize(bvect_full1, sermem, tb);
12847  cout << "BVECTOR SERMEM=" << slen << endl;
12848 
12849 
12850  bvect bvect_full3;
12851  bm::deserialize(bvect_full3, sermem);
12852  print_stat(cout,bvect_full3);
12853  CheckVectors(bvect_min1, bvect_full3, 100, true);
12854  }
12855 
12856 
12857  printf("Mutation operations test 3.\n");
12858  {
12859  bvect_mini bvect_min1(BITVECT_SIZE);
12860  bvect_mini bvect_min2(BITVECT_SIZE);
12861  bvect bvect_full1;
12862  bvect bvect_full2;
12863 
12864  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
12865  bvect_full2.set_new_blocks_strat(bm::BM_GAP);
12866 
12867 
12868  unsigned min = BITVECT_SIZE / 2 - ITERATIONS;
12869  unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
12870  if (max > BITVECT_SIZE)
12871  max = BITVECT_SIZE - 1;
12872 
12873  unsigned len = max - min;
12874 
12875  FillSets(&bvect_min1, &bvect_full1, min, max, 0);
12876  FillSets(&bvect_min1, &bvect_full1, 0, len, 5);
12877  printf("Bvect_FULL 1 STAT\n");
12878  print_stat(cout,bvect_full1);
12879 // CheckVectors(bvect_min1, bvect_full1, max+10, false);
12880  FillSets(&bvect_min2, &bvect_full2, min, max, 0);
12881  FillSets(&bvect_min2, &bvect_full2, 0, len, 0);
12882  printf("Bvect_FULL 2 STAT\n");
12883  print_stat(cout,bvect_full2);
12884 // CheckVectors(bvect_min2, bvect_full2, max+10);
12885 
12886 
12887  bvect_min1.combine_and(bvect_min2);
12888  bvect_full1.bit_and(bvect_full2);
12889  printf("Bvect_FULL 1 STAT after AND\n");
12890  print_stat(cout,bvect_full1);
12891 
12892  CheckVectors(bvect_min1, bvect_full1, max+10, false);
12893 
12894  struct bvect::statistics st;
12895  bvect_full1.calc_stat(&st);
12896  cout << "BVECTOR: GAP=" << st.gap_blocks << " BIT=" << st.bit_blocks
12897  << " MEM=" << st.memory_used << " SERMAX=" << st.max_serialize_mem
12898  << endl;
12899  cout << "MINIVECT: " << bvect_min1.mem_used() << endl;
12900 
12901  bvect_full1.optimize();
12902  print_stat(cout,bvect_full1);
12903 
12904  CheckVectors(bvect_min1, bvect_full1, max+10, false);
12905 
12906  bvect_full1.calc_stat(&st);
12907  cout << "BVECTOR: GAP=" << st.gap_blocks << " BIT=" << st.bit_blocks
12908  << " MEM=" << st.memory_used << " SERMAX=" << st.max_serialize_mem
12909  << endl;
12910  cout << "MINIVECT: " << bvect_min1.mem_used() << endl;
12911 
12912 
12913 
12914  // serialization
12915 
12917  bm::serializer<bvect> bv_ser(tb);
12918  bm::serializer<bvect>::buffer sermem_buf;
12919 
12920  bv_ser.serialize(bvect_full1, sermem_buf, 0);
12921  unsigned slen = (unsigned)sermem_buf.size();
12922 
12923  cout << "BVECTOR SERMEM=" << slen << endl;
12924 
12925 
12926 
12927  bvect bvect_full3;
12928  bm::deserialize(bvect_full3, sermem_buf.buf());
12929  print_stat(cout,bvect_full3);
12930  CheckVectors(bvect_min1, bvect_full3, max+10, true);
12931 
12932  cout << "Copy constructor check." << endl;
12933 
12934  {
12935  bvect bvect_full4(bvect_full3);
12936  print_stat(cout,bvect_full3);
12937  CheckVectors(bvect_min1, bvect_full4, max+10, true);
12938  }
12939 
12940 
12941  }
12942 
12943 }
12944 
12945 static
12947 {
12948  cout << " ----------------------------------- Serialization Buffer test" << endl;
12949 
12950  {
12952 
12953  assert(buf1.size() == 0);
12954  assert(buf1.capacity() == 0);
12955 
12957  assert(buf2.size() == 0);
12958  assert(buf2.capacity() != 0);
12959 
12960  buf1.reserve(100);
12961  assert(buf1.capacity() == buf2.capacity());
12962 
12963  const unsigned char str[] = "abc";
12964  buf1.copy_from(str, 3);
12965  assert(buf1.size() == 3);
12966 
12967  {
12968  const unsigned char* s = buf1.buf();
12969  assert(s[0] == 'a' && s[1] == 'b' && s[2] == 'c');
12970  }
12971 
12972  buf2 = buf1;
12973  assert(buf2.size() == buf1.size());
12974 
12975  {
12976  const unsigned char* s = buf2.buf();
12977  assert(s[0] == 'a' && s[1] == 'b' && s[2] == 'c');
12978  }
12979  buf2.reserve(100000);
12980  assert(buf2.size() == buf1.size());
12981  {
12982  const unsigned char* s = buf2.buf();
12983  assert(s[0] == 'a' && s[1] == 'b' && s[2] == 'c');
12984  }
12985  size_t cap2_1 = buf2.capacity();
12986 
12987  buf2.optimize();
12988  size_t cap2_2 = buf2.capacity();
12989 
12990  assert(cap2_2 < cap2_1);
12991  assert(buf2.size() == buf1.size());
12992 
12993  {
12994  const unsigned char* s = buf2.buf();
12995  assert(s[0] == 'a' && s[1] == 'b' && s[2] == 'c');
12996  }
12997 
12998  bm::serializer<bvect>::buffer buf3(buf2);
12999  assert(buf3.size() == buf2.size());
13000  {
13001  const unsigned char* s = buf3.buf();
13002  assert(s[0] == 'a' && s[1] == 'b' && s[2] == 'c');
13003  }
13004 
13005  buf3.reinit(1000000);
13006  assert(buf3.size() == 0);
13007  }
13008 
13009 
13010  cout << " ----------------------------------- Serialization Buffer test OK" << endl;
13011 }
13012 
13013 static
13015  const bm::xor_sim_model<bvect>& sim2)
13016 {
13017  bool eq = sim1.bv_blocks.equal(sim2.bv_blocks);
13018  if (!eq)
13019  {
13020  cerr << "Sim model Blocks vectors does not match! (BV)" << std::endl;
13021  assert(0); exit(1);
13022  }
13023  eq = sim1.matr.equal(sim2.matr);
13024  if (!eq)
13025  {
13026  cerr << "Sim model Blocks vectors does not match! (Matr)" << std::endl;
13027  std::this_thread::sleep_for (std::chrono::seconds(1));
13028  eq = sim1.matr.equal(sim2.matr);
13029  if (eq)
13030  {
13031  cerr << "Race!" << endl;
13032  }
13033 
13034  auto cols = sim1.matr.cols();
13035  auto rows = sim1.matr.rows();
13036  for (unsigned i = 0; i < rows; ++i)
13037  {
13038  for (unsigned j = i+1; j < cols; ++j)
13039  {
13040  auto v1 = sim1.matr.get(i,j);
13041  auto v2 = sim2.matr.get(i, j);
13042  if (!(v1 == v2))
13043  {
13044  std::cerr << " Mismatch at: " << i << ":" << j << endl;
13045  if (v1.chain_size != v2.chain_size)
13046  cerr << "Chain size mismatch" << endl;
13047  std::cerr << "chains=" << v1.chain_size << " " << v2.chain_size << endl;
13048 
13049  if (v1.nb != v2.nb)
13050  std::cerr << "NB mismatch!" << endl;
13051  if (v1.match != v2.match)
13052  std::cerr << "XOR match type mismatch!" << endl;
13053  cerr << "Match type = " << v1.match << endl;
13054 
13055  auto chain_size = v1.chain_size;
13056  assert(chain_size < 64);
13057  for (unsigned k = 0; k < chain_size; ++k)
13058  if (v1.ref_idx[k] != v2.ref_idx[k])
13059  std::cerr << "refs " << v1.ref_idx[k] << " " << v2.ref_idx[k] << " ";
13060  std::cerr << endl;
13061  for (unsigned k = 0; k < chain_size; ++k)
13062  if (v1.xor_d64[k] != v2.xor_d64[k])
13063  std::cerr << "D64 " << v1.xor_d64[k] << " " << v2.xor_d64[k];
13064  std::cerr << endl;
13065 
13066  }
13067  } // j
13068  } // i
13069 
13070  assert(0); exit(1);
13071  }
13072 }
13073 
13074 
13075 static
13077 {
13078  cout << " ----------------------------------- SerializationCompressionLevelsTest()" << endl;
13079 
13080  // test some basics
13081  {
13082  unsigned best_metric;
13083  bm::xor_complement_match xm_type;
13084 
13085  xm_type = bm::xor_scanner<bvect>::best_metric(1, 2, &best_metric);
13086  assert(xm_type == e_xor_match_BC);
13087  assert(best_metric == 1);
13088 
13089  xm_type = bm::xor_scanner<bvect>::best_metric(2, 1, &best_metric);
13090  assert(xm_type == e_xor_match_GC);
13091  assert(best_metric == 1);
13092 
13093  xm_type = bm::xor_scanner<bvect>::best_metric(65530, 65531, &best_metric);
13094  assert(xm_type == e_xor_match_iBC);
13095  assert(best_metric == 65536 - 65530);
13096 
13097  xm_type = bm::xor_scanner<bvect>::best_metric(65530, 65530, &best_metric);
13098  assert(xm_type == e_xor_match_iBC);
13099  assert(best_metric == 65536 - 65530);
13100 
13101  xm_type = bm::xor_scanner<bvect>::best_metric(65530, 3, &best_metric);
13102  assert(xm_type == e_xor_match_GC);
13103  assert(best_metric == 3);
13104  }
13105 
13106 
13108 
13109  {
13111 
13112  bvect bv(bm::BM_GAP);
13113  bv.set_range(0, 2);
13114  bv.set_range(10, 20);
13115  bv.set_range(100, 200);
13116  bv.optimize();
13117 
13118  bm::serializer<bvect> bv_ser(tb);
13119  bv_ser.set_compression_level(4); // use elias gamma
13120  bv_ser.set_bookmarks(true);
13121 
13122  bm::serializer<bvect>::buffer sermem_buf;
13123 
13124  bv_ser.serialize(bv, sermem_buf, 0);
13125 
13126  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13127  assert(cstat[bm::set_block_gap_egamma] == 1);
13128 
13129  bvect bv2;
13130  bm::deserialize(bv2, sermem_buf.buf());
13131 
13132  int cmp = bv.compare(bv2);
13133  assert(cmp == 0);
13134 
13135  bvect bv3;
13136  od.deserialize(bv3,
13137  sermem_buf.buf(),
13138  tb,
13139  set_OR);
13140 
13141  cmp = bv3.compare(bv2);
13142  assert(cmp == 0);
13143  }
13144 
13145  {
13147 
13148  bvect bv(bm::BM_GAP);
13149  bv.set_range(0, 2);
13150  bv.set_range(10, 20);
13151  bv.set_range(100, 200);
13152  bv.set_range(250, 300);
13153  bv.optimize();
13154 
13155  bm::serializer<bvect> bv_ser(tb);
13156  bv_ser.set_compression_level(5); // use interpolative encoder
13157  bv_ser.set_bookmarks(true);
13158 
13159  bm::serializer<bvect>::buffer sermem_buf;
13160 
13161  bv_ser.serialize(bv, sermem_buf, 0);
13162 
13163  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13164  assert(cstat[bm::set_block_gap_bienc] == 1);
13165 
13166  bvect bv2;
13167  bm::deserialize(bv2, sermem_buf.buf());
13168 
13169  int cmp = bv.compare(bv2);
13170  assert(cmp == 0);
13171 
13172  bvect bv3;
13173  od.deserialize(bv3,
13174  sermem_buf.buf(),
13175  tb,
13176  set_OR);
13177 
13178  cmp = bv3.compare(bv2);
13179  assert(cmp == 0);
13180  }
13181 
13182  {
13184  bvect bv { 0, 1, 2, 10, 100, 200 };
13185  bv.optimize();
13186 
13187  bm::serializer<bvect> bv_ser(tb);
13188  bv_ser.set_compression_level(4); // use elias gamma
13189  bv_ser.set_bookmarks(true);
13190 
13191  bm::serializer<bvect>::buffer sermem_buf;
13192 
13193  bv_ser.serialize(bv, sermem_buf, 0);
13194 
13195  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13196  assert(cstat[bm::set_block_arrgap_egamma] == 1);
13197 
13198  bvect bv2;
13199  bm::deserialize(bv2, sermem_buf.buf());
13200 
13201  int cmp = bv.compare(bv2);
13202  assert(cmp == 0);
13203  bvect bv3;
13204  od.deserialize(bv3,
13205  sermem_buf.buf(),
13206  tb,
13207  set_OR);
13208  cmp = bv3.compare(bv2);
13209  assert(cmp == 0);
13210  }
13211 
13212  {
13214  bvect bv { 0, 1, 2, 10, 100, 200 };
13215  bv.optimize();
13216 
13217  bm::serializer<bvect> bv_ser(tb);
13218  bv_ser.set_compression_level(5); // binary interpolated coding
13219  bv_ser.set_bookmarks(true);
13220 
13221  bm::serializer<bvect>::buffer sermem_buf;
13222 
13223  bv_ser.serialize(bv, sermem_buf, 0);
13224 
13225  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13227 
13228  bvect bv2;
13229  bm::deserialize(bv2, sermem_buf.buf());
13230 
13231  int cmp = bv.compare(bv2);
13232  assert(cmp == 0);
13233  bvect bv3;
13234  od.deserialize(bv3,
13235  sermem_buf.buf(),
13236  tb,
13237  set_OR);
13238  cmp = bv3.compare(bv2);
13239  assert(cmp == 0);
13240  }
13241 
13242  {
13244  bvect bv;
13245  bv.set_range(0, 65535);
13246  bv.clear_bit(1);
13247  bv.clear_bit(5);
13248  bv.clear_bit(10);
13249  bv.clear_bit(100);
13250  bv.clear_bit(200);
13251  bv.clear_bit(250);
13252 
13253  bv.optimize();
13254 
13255  bm::serializer<bvect> bv_ser(tb);
13256  bv_ser.set_compression_level(5); // binary interplated coding
13257  bv_ser.set_bookmarks(true);
13258 
13259  bm::serializer<bvect>::buffer sermem_buf;
13260 
13261  bv_ser.serialize(bv, sermem_buf, 0);
13262 
13263  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13265 
13266  bvect bv2;
13267  bm::deserialize(bv2, sermem_buf.buf());
13268 
13269  int cmp = bv.compare(bv2);
13270  assert(cmp == 0);
13271  bvect bv3;
13272  od.deserialize(bv3,
13273  sermem_buf.buf(),
13274  tb,
13275  set_OR);
13276  cmp = bv3.compare(bv2);
13277  assert(cmp == 0);
13278  }
13279 
13280 
13281  {
13283 
13284  bvect bv(bm::BM_GAP);
13285  bv.set_range(0, bm::gap_max_bits-1);
13286  bv.clear_bit(1);
13287  bv.clear_bit(10);
13288  bv.clear_bit(100);
13289  bv.clear_bit(200);
13290 
13291  bv.optimize();
13292 
13293  bm::serializer<bvect> bv_ser(tb);
13294  bv_ser.set_compression_level(4); // use elias gamma
13295  bv_ser.set_bookmarks(true);
13296 
13297  bm::serializer<bvect>::buffer sermem_buf;
13298 
13299  bv_ser.serialize(bv, sermem_buf, 0);
13300 
13301  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13303 
13304  bvect bv2;
13305  bm::deserialize(bv2, sermem_buf.buf());
13306 
13307  int cmp = bv.compare(bv2);
13308  assert(cmp == 0);
13309  bvect bv3;
13310  od.deserialize(bv3,
13311  sermem_buf.buf(),
13312  tb,
13313  set_OR);
13314  cmp = bv3.compare(bv2);
13315  assert(cmp == 0);
13316  }
13317 
13318  {
13320 
13321  bvect bv(bm::BM_GAP);
13322  bv.set(100);
13323 
13324  bm::serializer<bvect> bv_ser(tb);
13325  bv_ser.set_compression_level(4); // use elias gamma
13326  bv_ser.set_bookmarks(true);
13327 
13328  bm::serializer<bvect>::buffer sermem_buf;
13329 
13330  bv_ser.serialize(bv, sermem_buf, 0);
13331 
13332  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13333  assert(cstat[bm::set_block_bit_1bit] == 1);
13334 
13335  bvect bv2;
13336  bm::deserialize(bv2, sermem_buf.buf());
13337 
13338  int cmp = bv.compare(bv2);
13339  assert(cmp == 0);
13340  bvect bv3;
13341  od.deserialize(bv3,
13342  sermem_buf.buf(),
13343  tb,
13344  set_OR);
13345  cmp = bv3.compare(bv2);
13346  assert(cmp == 0);
13347  }
13348 
13349  // --------------------------------------------------------------
13350  // Sparse super-block serialization
13351 
13352  {
13354  bvect bv(bm::BM_GAP);
13355  for (bvect::size_type i = 1; i < 65535 * 255; i += 65535)
13356  bv.set_range(i, i+10);
13357 
13358  bm::serializer<bvect> bv_ser(tb);
13359  bv_ser.set_compression_level(5);
13360 
13361  bm::serializer<bvect>::buffer sermem_buf;
13362 
13363  bv_ser.serialize(bv, sermem_buf, 0);
13364 
13365  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13366  assert(cstat[bm::set_sblock_bienc] == 0);
13367 
13368  bvect bv2;
13369  bm::deserialize(bv2, sermem_buf.buf());
13370 
13371  bool eq = bv.equal(bv2);
13372  assert(eq);
13373  }
13374 
13375  {
13377 
13378  bvect bv(bm::BM_GAP);
13379 
13380  bv.set_range(0, 10000);
13381  bv.set_range(65535, 65535+10000);
13382  bv.set_range(0, 10000, false);
13383  bv.set_range(65535, 65535+10000, false);
13384 
13385  for (bvect::size_type i = 65535*5; i < 65535*255; i+= 65536/10)
13386  bv.set(i);
13387 
13388  bm::serializer<bvect> bv_ser(tb);
13389  bv_ser.set_compression_level(5);
13390 
13391  bm::serializer<bvect>::buffer sermem_buf;
13392 
13393  bv_ser.serialize(bv, sermem_buf, 0);
13394 
13395  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13396  assert(cstat[bm::set_sblock_bienc] >= 1);
13397 
13398  bvect bv2;
13399  bm::deserialize(bv2, sermem_buf.buf());
13400 
13401  int cmp = bv.compare(bv2);
13402  assert(cmp == 0);
13403  bvect bv3;
13404  od.deserialize(bv3,
13405  sermem_buf.buf(),
13406  tb,
13407  set_OR);
13408  bool eq = bv3.equal(bv2);
13409  assert(eq);
13410  }
13411 
13412 
13413  // --------------------------------------------------------------
13414  // XOR serialization
13415 
13416  // check digest vector compute
13417  {{
13418  bvect bv1, bv2, bv3;
13419  bv1[1] = true;
13420  bv2[65536*256] = true;
13422  bv_ref.add(&bv1, 1); // idx = 0
13423  bv_ref.add(&bv2, 5); // idx = 1
13424  bv_ref.add(&bv3, 6); // idx = 1
13425 
13426  bvect bv_d;
13427  bv_ref.fill_alloc_digest(bv_d);
13428  auto c = bv_d.count();
13429  assert(c == 2);
13430  assert(bv_d.test(0));
13431  assert(bv_d.test(256));
13432 
13434  bv_ref.resize_xor_matrix(cm, c);
13435  assert(cm.rows()==3);
13436  assert(cm.cols()==c);
13437 
13438  }}
13439 
13440  {{
13441  bvect bv1, bv2;
13442  bv1[1] = true;
13443  bv2[1] = true;
13444 
13446  bv_ref.add(&bv1, 1); // idx = 0
13447  bv_ref.add(&bv2, 5); // idx = 1
13448 
13450 
13451  xor_sim_params xs_params;
13453  bms.set_ref_vectors(&bv_ref);
13454  bms.compute_sim_model(sim_model, bv_ref, xs_params);
13455  bms.set_sim_model(&sim_model);
13456 
13457 
13458  bms.set_curr_ref_idx(0);
13459  bms.set_bookmarks(true);
13460 
13462  bms.serialize(bv1, buf);
13463 
13464 // const bvect::size_type* cstat = bms.get_compression_stat();
13465 // assert(cstat[bm::set_block_ref_eq] >= 1);
13466 // assert(cstat[bm::set_block_xor_ref32] >= 1);
13467 
13468  bvect bv3;
13469  bm::deserialize(bv3, buf.buf(), 0, &bv_ref);
13470  auto eq = bv3.equal(bv2);
13471  assert(eq);
13472 
13473  bvect bv4;
13474  od.set_ref_vectors(&bv_ref);
13475  od.deserialize(bv4,
13476  buf.buf(),
13477  set_OR);
13478  eq = bv4.equal(bv2);
13479  assert(eq);
13480 
13481  bvect bv5;
13482  od.deserialize_range(bv5, buf.buf(), 0, 100);
13483  eq = bv5.equal(bv2);
13484  assert(eq);
13485 
13486  }}
13487 
13488  // --------------------------------------------------------------
13489  // XOR serialization (2)
13490  {{
13491  bvect::size_type off = 0;
13492  for (unsigned pass = 0; pass < 2; ++pass, off+=65536*256)
13493  {
13494  bvect bv1, bv2;
13495  for (unsigned i = 0; i < 100; i+=2)
13496  {
13497  bv1[off+i] = true;
13498  bv2[off+i+1] = true;
13499  }
13500 
13502  bv_ref.add(&bv1, 1); // idx = 0
13503  bv_ref.add(&bv2, 5); // idx = 1
13504 
13505 
13507  bms.set_ref_vectors(&bv_ref);
13509  bms.compute_sim_model(sim_model, bv_ref, bm::xor_sim_params());
13510  bms.set_sim_model(&sim_model);
13511 
13512 
13513  bms.set_curr_ref_idx(0);
13514  bms.set_bookmarks(true);
13515 
13516  {
13517  struct bvect::statistics st1;
13518  bv1.calc_stat(&st1);
13519  cout << st1.bit_blocks << endl;
13520  }
13521 
13523  bms.serialize(bv1, buf);
13524 
13525  const bvect::size_type* cstat = bms.get_compression_stat();
13526  assert(cstat[bm::set_block_xor_ref32] == 1);
13527 
13528  bvect bv3;
13529  bm::deserialize(bv3, buf.buf(), 0, &bv_ref);
13530  auto eq = bv3.equal(bv1);
13531  assert(eq);
13532 
13533  bvect bv4;
13534  od.set_ref_vectors(&bv_ref);
13535  od.deserialize(bv4,
13536  buf.buf(),
13537  set_OR);
13538  eq = bv4.equal(bv1);
13539  assert(eq);
13540 
13541  bvect bv5;
13542  od.deserialize_range(bv5, buf.buf(), off+0, off+100);
13543  eq = bv5.equal(bv1);
13544  assert(eq);
13545 
13546  bvect bv6;
13547  bv6.set(off+0);
13548  assert(bv6.test(off+0) == bv1.test(off+0));
13549 
13550  bm::deserialize(bv6, buf.buf(), 0, &bv_ref);
13551  eq = bv6.equal(bv1);
13552  assert(eq);
13553 
13554  bvect bv7(bm::BM_GAP);
13555  bv7.set(off+0);
13556  bm::deserialize(bv7, buf.buf(), 0, &bv_ref);
13557  eq = bv7.equal(bv1);
13558  assert(eq);
13559  struct bvect::statistics st1;
13560  bv7.calc_stat(&st1);
13561  assert(!st1.bit_blocks);
13562  assert(st1.gap_blocks == 1);
13563 
13564  } // pass
13565  }}
13566 
13567  // --------------------------------------------------------------
13568  // XOR serialization (2-1)
13569  {{
13570  bvect bv1, bv2;
13571  for (unsigned i = 4; i < 65536; i+=4)
13572  {
13573  bv2[i] = true;
13574  bv1[i-1] = true;
13575  bv1[i-2] = true;
13576  }
13577  cout << bv1.count() << endl;
13578  cout << bv2.count() << endl;
13579 
13581  bv_ref.add(&bv1, 1); // idx = 0
13582  bv_ref.add(&bv2, 5); // idx = 1
13583 
13585  bms.set_ref_vectors(&bv_ref);
13586  bms.set_curr_ref_idx(0);
13587  //bms.set_bookmarks(true);
13588 
13590 
13591  {
13594  bm::xor_sim_params xor_search_params;
13595 
13596  pbuilder.build_plan(tbatch, sim_model,
13597  bv_ref, xor_search_params);
13598 
13599  typedef
13601  pool_type tpool; // our thread pool here (no threads created yet)
13602  tpool.start(1); // start the threads
13603  {
13605  exec.run(tpool, tbatch, true);
13606  }
13607  tpool.set_stop_mode(pool_type::stop_when_done);
13608  tpool.join();
13609  {
13611  bms.compute_sim_model(sim_model_c, bv_ref, xor_search_params);
13612  Check_SimModel(sim_model_c, sim_model);
13613  }
13614  }
13615 
13616 
13617  //bms.compute_sim_model(sim_model, bv_ref, bm::xor_sim_params());
13618 
13619 
13620  bms.set_sim_model(&sim_model);
13621 
13623  bms.serialize(bv1, buf);
13624 
13625  const bvect::size_type* cstat = bms.get_compression_stat();
13626  assert(cstat[bm::set_block_xor_ref32] == 1);
13627 
13628  bvect bv3;
13629  bm::deserialize(bv3, buf.buf(), 0, &bv_ref);
13630  auto eq = bv3.equal(bv1);
13631  assert(eq);
13632 
13633  }}
13634 
13635 
13636  // --------------------------------------------------------------
13637  // XOR serialization (3) - GAPs
13638 
13639  {{
13640  bvect bv1, bv2;
13641  for (unsigned i = 0; i < 500; i+=8)
13642  {
13643  bv1.set_range(i, i+1);
13644  bv2.set(i+1);
13645  }
13646 
13647 
13648  bv1.optimize();
13649  bv2.optimize();
13650  {
13651  struct bvect::statistics st1;
13652  bv1.calc_stat(&st1);
13653  assert(!st1.bit_blocks);
13654  assert(st1.gap_blocks);
13655  struct bvect::statistics st2;
13656  bv2.calc_stat(&st2);
13657  assert(!st2.bit_blocks);
13658  assert(st2.gap_blocks);
13659  }
13660 
13662  bv_ref.add(&bv1, 1000000000); // idx = 0
13663  bv_ref.add(&bv2, 65500); // idx = 1
13664 
13666  bms.set_ref_vectors(&bv_ref);
13667  bms.set_curr_ref_idx(0);
13668 
13670 /// bms.compute_sim_model(sim_model, bv_ref, bm::xor_sim_params());
13671 
13672  {
13675  bm::xor_sim_params xor_search_params;
13676 
13677  pbuilder.build_plan(tbatch, sim_model,
13678  bv_ref, xor_search_params);
13679 
13680  typedef
13682  pool_type tpool; // our thread pool here (no threads created yet)
13683  tpool.start(3); // start the threads
13684  {
13686  exec.run(tpool, tbatch, true);
13687  }
13688  tpool.set_stop_mode(pool_type::stop_when_done);
13689  tpool.join();
13690  {
13692  bms.compute_sim_model(sim_model_c, bv_ref, xor_search_params);
13693  Check_SimModel(sim_model_c, sim_model);
13694  }
13695  }
13696 
13697 
13698 
13699  bms.set_sim_model(&sim_model);
13700 
13702  bms.serialize(bv1, buf);
13703 
13704  const bvect::size_type* cstat = bms.get_compression_stat();
13705  assert(cstat[bm::set_block_xor_ref32] == 1);
13706 
13707  //assert(cstat[bm::set_block_xor_gap_ref32] == 1);
13708 
13709  bvect bv3;
13710  bm::deserialize(bv3, buf.buf(), 0, &bv_ref);
13711  auto eq = bv3.equal(bv1);
13712  assert(eq);
13713 
13714  bvect bv4;
13715  od.set_ref_vectors(&bv_ref);
13716  od.deserialize(bv4,
13717  buf.buf(),
13718  set_OR);
13719  eq = bv4.equal(bv1);
13720  assert(eq);
13721 
13722  bvect bv5;
13723  od.deserialize_range(bv5, buf.buf(), 0, 600);
13724  eq = bv5.equal(bv1);
13725  assert(eq);
13726  }}
13727 
13728  {{
13729  bvect bv1, bv2;
13730  for (unsigned i = 0; i < 100; i+=2)
13731  {
13732  bv1[i] = true;
13733  bv2[i+1] = true;
13734  }
13735  bv1.optimize();
13736  bv2.optimize();
13737  {
13738  struct bvect::statistics st1;
13739  bv1.calc_stat(&st1);
13740  assert(!st1.bit_blocks);
13741  assert(st1.gap_blocks);
13742  struct bvect::statistics st2;
13743  bv2.calc_stat(&st2);
13744  assert(!st2.bit_blocks);
13745  assert(st2.gap_blocks);
13746  }
13747 
13749  bv_ref.add(&bv1, 1000000000); // idx = 0
13750  bv_ref.add(&bv2, 65500); // idx = 1
13751 
13753  bms.set_ref_vectors(&bv_ref);
13754  bms.set_curr_ref_idx(0);
13755  bms.set_bookmarks(true);
13756 
13758  bms.compute_sim_model(sim_model, bv_ref, bm::xor_sim_params());
13759  bms.set_sim_model(&sim_model);
13760 
13762  bms.serialize(bv1, buf);
13763 
13764  const bvect::size_type* cstat = bms.get_compression_stat();
13765  assert(cstat[bm::set_block_xor_ref32] == 1);
13766 
13767 // assert(cstat[bm::set_block_xor_gap_ref32] == 1);
13768 
13769  bvect bv3;
13770  bm::deserialize(bv3, buf.buf(), 0, &bv_ref);
13771  auto eq = bv3.equal(bv1);
13772  assert(eq);
13773 
13774  bvect bv4;
13775  od.set_ref_vectors(&bv_ref);
13776  od.deserialize(bv4,
13777  buf.buf(),
13778  set_OR);
13779  eq = bv4.equal(bv1);
13780  assert(eq);
13781 
13782  bvect bv5;
13783  od.deserialize_range(bv5, buf.buf(), 0, 100);
13784  eq = bv5.equal(bv1);
13785  assert(eq);
13786 
13787  bvect bv6;
13788  bv6.set(0);
13789  assert(bv6.test(0) == bv1.test(0));
13790 
13791  bm::deserialize(bv6, buf.buf(), 0, &bv_ref);
13792  eq = bv6.equal(bv1);
13793  assert(eq);
13794 
13795  bvect bv7(bm::BM_GAP);
13796  bv7.set(0);
13797  bm::deserialize(bv7, buf.buf(), 0, &bv_ref);
13798  eq = bv7.equal(bv1);
13799  assert(eq);
13800  struct bvect::statistics st1;
13801  bv7.calc_stat(&st1);
13802  assert(!st1.bit_blocks);
13803  assert(st1.gap_blocks == 1);
13804  }}
13805 
13806 
13807 
13808  // --------------------------------------------------------------
13809  // bit-block tests
13810  //
13811 
13812  {
13813  bvect bv { 100 };
13814 
13815  bm::serializer<bvect> bv_ser;
13816  bv_ser.set_compression_level(4);
13817  bv_ser.set_bookmarks(true);
13818 
13819  bm::serializer<bvect>::buffer sermem_buf;
13820 
13821  bv_ser.serialize(bv, sermem_buf, 0);
13822 
13823  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13824  assert(cstat[bm::set_block_bit_1bit] == 1);
13825 
13826  bvect bv2;
13827  bm::deserialize(bv2, sermem_buf.buf());
13828  int cmp = bv.compare(bv2);
13829  assert(cmp == 0);
13830  bvect bv3;
13831  od.deserialize(bv3,
13832  sermem_buf.buf(),
13833  0, set_OR);
13834  cmp = bv3.compare(bv2);
13835  assert(cmp == 0);
13836  }
13837 
13838  {
13839  bvect bv;
13840  for (bvect::size_type i = 0; i < 65536; ++i)
13841  bv.set(i);
13842  bv.set(100, false);
13843 
13844  bm::serializer<bvect> bv_ser;
13845  bv_ser.set_compression_level(4);
13846  bv_ser.set_bookmarks(true);
13847 
13848  bm::serializer<bvect>::buffer sermem_buf;
13849 
13850  bv_ser.serialize(bv, sermem_buf, 0);
13851 
13852  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13853  assert(cstat[set_block_arrbit_inv] == 1);
13854 
13855  bvect bv2;
13856  bm::deserialize(bv2, sermem_buf.buf());
13857  int cmp = bv.compare(bv2);
13858  assert(cmp == 0);
13859  bvect bv3;
13860  od.deserialize(bv3,
13861  sermem_buf.buf(),
13862  0, set_OR);
13863  cmp = bv3.compare(bv2);
13864  assert(cmp == 0);
13865  }
13866 
13867  {
13868  bvect bv;
13869  for (bvect::size_type i = 0; i < 22345; i+=2)
13870  bv.set(1000+i);
13871 
13872  bm::serializer<bvect> bv_ser;
13873  bv_ser.set_compression_level(4);
13874  bv_ser.set_bookmarks(true);
13875 
13876  bm::serializer<bvect>::buffer sermem_buf;
13877 
13878  bv_ser.serialize(bv, sermem_buf, 0);
13879 
13880  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13881  assert(cstat[bm::set_block_bit_0runs] == 1);
13882 
13883  bvect bv2;
13884  bm::deserialize(bv2, sermem_buf.buf());
13885  int cmp = bv.compare(bv2);
13886  assert(cmp == 0);
13887  bvect bv3;
13888  od.deserialize(bv3,
13889  sermem_buf.buf(),
13890  0, set_OR);
13891  cmp = bv.compare(bv2);
13892  assert(cmp == 0);
13893  }
13894 
13895 
13896  {
13897  bvect bv;
13898  for (bvect::size_type i = 0; i < 22345; i+=2)
13899  bv.set(1000+i);
13900 
13901  bm::serializer<bvect> bv_ser;
13902  bv_ser.set_compression_level(4);
13903  bv_ser.set_bookmarks(true);
13904 
13905  bm::serializer<bvect>::buffer sermem_buf;
13906 
13907  bv_ser.serialize(bv, sermem_buf, 0);
13908 
13909  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13910  assert(cstat[bm::set_block_bit_0runs] == 1);
13911 
13912  bvect bv2;
13913  bm::deserialize(bv2, sermem_buf.buf());
13914  int cmp = bv.compare(bv2);
13915  assert(cmp == 0);
13916  bvect bv3;
13917  od.deserialize(bv3,
13918  sermem_buf.buf(),
13919  0, set_OR);
13920  cmp = bv3.compare(bv2);
13921  assert(cmp == 0);
13922  }
13923 
13924  {
13925  bvect bv;
13926  for (bvect::size_type i = 0; i < 145; i+=64)
13927  bv.set(1000+i);
13928 
13929  bm::serializer<bvect> bv_ser;
13930  bv_ser.set_compression_level(3);
13931  bv_ser.set_bookmarks(true);
13932 
13933  bm::serializer<bvect>::buffer sermem_buf;
13934 
13935  bv_ser.serialize(bv, sermem_buf, 0);
13936 
13937  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13938  assert(cstat[bm::set_block_arrbit] == 1);
13939 
13940  bvect bv2;
13941  bm::deserialize(bv2, sermem_buf.buf());
13942  int cmp = bv.compare(bv2);
13943  assert(cmp == 0);
13944  bvect bv3;
13945  od.deserialize(bv3,
13946  sermem_buf.buf(),
13947  0, set_OR);
13948  cmp = bv3.compare(bv2);
13949  assert(cmp == 0);
13950  }
13951 
13952 
13953  {
13954  bvect bv;
13955  for (bvect::size_type i = 0; i < 545; i+=64)
13956  bv.set(1000+i);
13957 
13958  bm::serializer<bvect> bv_ser;
13959  bv_ser.set_compression_level(4);
13960  bv_ser.set_bookmarks(true);
13961 
13962  bm::serializer<bvect>::buffer sermem_buf;
13963 
13964  bv_ser.serialize(bv, sermem_buf, 0);
13965 
13966  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13967  assert(cstat[bm::set_block_arrgap] == 1);
13968 
13969  bvect bv2;
13970  bm::deserialize(bv2, sermem_buf.buf());
13971  int cmp = bv.compare(bv2);
13972  assert(cmp == 0);
13973  bvect bv3;
13974  od.deserialize(bv3,
13975  sermem_buf.buf(),
13976  0, set_OR);
13977  cmp = bv3.compare(bv2);
13978  assert(cmp == 0);
13979  }
13980 
13981  {
13982  bvect bv;
13983  for (bvect::size_type i = 0; i < 1045; i+=64)
13984  {
13985  auto target = i + 25;
13986  for ( ;i < target; ++i)
13987  bv.set(i);
13988  }
13989 
13990  bm::serializer<bvect> bv_ser;
13991  bv_ser.set_compression_level(4);
13992  bv_ser.set_bookmarks(true);
13993 
13994  bm::serializer<bvect>::buffer sermem_buf;
13995 
13996  bv_ser.serialize(bv, sermem_buf, 0);
13997 
13998  const bvect::size_type* cstat = bv_ser.get_compression_stat();
13999  assert(cstat[set_block_gap_egamma] == 1);
14000 
14001  bvect bv2;
14002  bm::deserialize(bv2, sermem_buf.buf());
14003  int cmp = bv.compare(bv2);
14004  assert(cmp == 0);
14005  bvect bv3;
14006  od.deserialize(bv3,
14007  sermem_buf.buf(),
14008  0, set_OR);
14009  cmp = bv3.compare(bv2);
14010  assert(cmp == 0);
14011  }
14012 
14013 
14014  {
14015  bvect bv;
14016  for (bvect::size_type i = 0; i < 65536; ++i)
14017  bv.set(i);
14018  for (bvect::size_type i = 0; i < 1045; i+=64)
14019  bv.set(1000+i, false);
14020 
14021  bm::serializer<bvect> bv_ser;
14022  bv_ser.set_compression_level(4);
14023  bv_ser.set_bookmarks(true);
14024 
14025  bm::serializer<bvect>::buffer sermem_buf;
14026 
14027  bv_ser.serialize(bv, sermem_buf, 0);
14028 
14029  const bvect::size_type* cstat = bv_ser.get_compression_stat();
14030  assert(cstat[set_block_arrgap_egamma_inv] == 1);
14031 
14032  bvect bv2;
14033  bm::deserialize(bv2, sermem_buf.buf());
14034  int cmp = bv.compare(bv2);
14035  assert(cmp == 0);
14036  bvect bv3;
14037  od.deserialize(bv3,
14038  sermem_buf.buf(),
14039  0, set_OR);
14040  cmp = bv3.compare(bv2);
14041  assert(cmp == 0);
14042  }
14043 
14044  {
14045  for (bvect::size_type k = 0; k < 4; ++k)
14046  {
14047  bvect bv;
14048  for (bvect::size_type i = 5; true; )
14049  {
14050  auto idx = (k * 1024) + i;
14051  if (idx >= 65536)
14052  break;
14053  bv.set(idx);
14054  i += unsigned(rand() % 9);
14055  if (bv.count() > 13000)
14056  break;
14057  }
14058  auto bc = bv.count();
14059 
14060  size_t l4size = 0;
14061  bvect bv_l4;
14062  {
14063  bm::serializer<bvect> bv_ser;
14064  bv_ser.set_compression_level(4);
14065  bm::serializer<bvect>::buffer sermem_buf;
14066  bv_ser.serialize(bv, sermem_buf, 0);
14067  //const bvect::size_type* cstat = bv_ser.get_compression_stat();
14068  //assert(cstat[bm::set_block_bit_0runs] == 1);
14069  l4size= sermem_buf.size();
14070  }
14071 
14072  bm::serializer<bvect> bv_ser;
14073  bv_ser.set_compression_level(5); // interpolated binary
14074  bv_ser.set_bookmarks(true);
14075 
14076 
14077  bm::serializer<bvect>::buffer sermem_buf;
14078  bv_ser.serialize(bv, sermem_buf, 0);
14079  size_t l5size = sermem_buf.size();
14080  size_t raw_int = bc * sizeof(bm::word_t);
14081  assert(raw_int > l5size);
14082  assert(l5size < l4size);
14083  cout << " offset = " << k * 1024 << endl;
14084  cout << "raw = " << raw_int << " l4 = " << l4size << " l5 = " << l5size << " Diff(5-4)="
14085  << l4size - l5size
14086  << endl;
14087 
14088  const bvect::size_type* cstat = bv_ser.get_compression_stat();
14089  assert(cstat[bm::set_block_arr_bienc] == 1);
14090 
14091  bvect bv2;
14092  bm::deserialize(bv2, sermem_buf.buf());
14093  int cmp = bv.compare(bv2);
14094  assert(cmp == 0);
14095  bvect bv3;
14096  od.deserialize(bv3,
14097  sermem_buf.buf(),
14098  0, set_OR);
14099  cmp = bv3.compare(bv2);
14100  assert(cmp == 0);
14101  }
14102  }
14103 
14104 
14105  {
14106  cout << "Generate large split in the mid-block" << endl;
14107  for (bvect::size_type k = 0; k < 4; ++k)
14108  {
14109  bvect bv;
14110  for (bvect::size_type i = 5; true; )
14111  {
14112  auto idx = i;
14113  if (idx >= 65536)
14114  break;
14115  bv.set(idx);
14116  i += unsigned(rand() % 9);
14117  if (bv.count() > 3000)
14118  break;
14119  }
14120  bvect bv_shift(bv);
14121  for (bvect::size_type i = 0; i < 10000 * 3; ++i)
14122  {
14123  bv_shift.shift_right();
14124  }
14125  bv |= bv_shift;
14126 
14127 
14128  auto bc = bv.count();
14129 
14130  size_t l4size = 0;
14131  bvect bv_l4;
14132  {
14133  bm::serializer<bvect> bv_ser;
14134  bv_ser.set_compression_level(4);
14135  bv_ser.set_bookmarks(true);
14136 
14137  bm::serializer<bvect>::buffer sermem_buf;
14138  bv_ser.serialize(bv, sermem_buf, 0);
14139  const bvect::size_type* cstat = bv_ser.get_compression_stat();
14141  l4size= sermem_buf.size();
14142  }
14143 
14144  bm::serializer<bvect> bv_ser;
14145  bv_ser.set_compression_level(5); // interpolated binary
14146  bv_ser.set_bookmarks(true);
14147 
14148 
14149  bm::serializer<bvect>::buffer sermem_buf;
14150  bv_ser.serialize(bv, sermem_buf, 0);
14151  size_t l5size = sermem_buf.size();
14152  size_t raw_int = bc * sizeof(bm::word_t);
14153  assert(raw_int >= l5size);
14154  assert(l5size <= l4size);
14155  cout << " offset = " << k * 1024 << endl;
14156  cout << "raw = " << raw_int << " l4 = " << l4size << " l5 = " << l5size << " Diff(5-4)="
14157  << l4size - l5size
14158  << endl;
14159 
14160  const bvect::size_type* cstat = bv_ser.get_compression_stat();
14161  assert(cstat[bm::set_block_arr_bienc]==1 ||
14162  cstat[bm::set_block_bit_digest0]==1 ||
14163  cstat[bm::set_block_bit_0runs]== 1);
14164 
14165  bvect bv2;
14166  bm::deserialize(bv2, sermem_buf.buf());
14167  int cmp = bv.compare(bv2);
14168  assert(cmp == 0);
14169  bvect bv3;
14170  od.deserialize(bv3,
14171  sermem_buf.buf(),
14172  0, set_OR);
14173  cmp = bv3.compare(bv2);
14174  assert(cmp == 0);
14175  }
14176  }
14177 
14178 
14179 
14180  {
14181  bvect bv;
14182  bv.set(1); bv.set(1, false);
14183  bv.set_range(10, 20);
14184  bv.set_range(100, 200);
14185  bv.set_range(1000, 2000);
14186  bv.set_range(2010, 2020);
14187  bv.set_range(3000, 4020);
14188  bv.set_range(5000, 6000);
14189  bv.set_range(6000, 7000);
14190 
14191  bm::serializer<bvect> bv_ser;
14192  bv_ser.set_compression_level(5);
14193  bv_ser.set_bookmarks(true);
14194 
14195  bm::serializer<bvect>::buffer sermem_buf;
14196 
14197  bv_ser.serialize(bv, sermem_buf, 0);
14198 
14199  const bvect::size_type* cstat = bv_ser.get_compression_stat();
14200  assert(cstat[bm::set_block_gap_bienc] == 1);
14201 
14202  bvect bv2;
14203  bm::deserialize(bv2, sermem_buf.buf());
14204  int cmp = bv.compare(bv2);
14205  assert(cmp == 0);
14206  bvect bv3;
14207  od.deserialize(bv3,
14208  sermem_buf.buf(),
14209  0, set_OR);
14210  cmp = bv3.compare(bv2);
14211  assert(cmp == 0);
14212  }
14213 
14214  {
14215  bvect bv;
14216  for (bvect::size_type i = 0; i < 65536; ++i)
14217  bv.set(i);
14218  for (bvect::size_type i = 0; i < 12045; ++i)
14219  bv.set((unsigned)rand()%65535, false);
14220 
14221  bm::serializer<bvect> bv_ser;
14222  bv_ser.set_compression_level(5);
14223  bv_ser.set_bookmarks(true);
14224 
14225  bm::serializer<bvect>::buffer sermem_buf;
14226 
14227  bv_ser.serialize(bv, sermem_buf, 0);
14228 
14229  const bvect::size_type* cstat = bv_ser.get_compression_stat();
14230  assert(cstat[bm::set_block_arr_bienc_inv] == 1);
14231 
14232  bvect bv2;
14233  bm::deserialize(bv2, sermem_buf.buf());
14234  int cmp = bv.compare(bv2);
14235  assert(cmp == 0);
14236  bvect bv3;
14237  od.deserialize(bv3,
14238  sermem_buf.buf(),
14239  0, set_OR);
14240  cmp = bv3.compare(bv2);
14241  assert(cmp == 0);
14242  }
14243 
14244  {
14245  bvect bv;
14246  bv.set(100);
14247  bvect::size_type from = 0;
14248  for (bvect::size_type i = 0; i < 3000; ++i)
14249  {
14250  auto to = from + (unsigned)rand()%15;
14251  bv.set_range(from, to);
14252  from = to + (unsigned)rand()%15;
14253  }
14254 
14255  bm::serializer<bvect> bv_ser;
14256  bv_ser.set_compression_level(5);
14257  bv_ser.set_bookmarks(true);
14258 
14259  bm::serializer<bvect>::buffer sermem_buf;
14260  bv_ser.serialize(bv, sermem_buf, 0);
14261 
14262  const bvect::size_type* cstat = bv_ser.get_compression_stat();
14263  assert(cstat[set_block_bitgap_bienc] == 1);
14264 
14265  bvect bv2;
14266  bm::deserialize(bv2, sermem_buf.buf());
14267  int cmp = bv.compare(bv2);
14268  assert(cmp == 0);
14269  bvect bv3;
14270  od.deserialize(bv3,
14271  sermem_buf.buf(),
14272  0, set_OR);
14273  cmp = bv3.compare(bv2);
14274  assert(cmp == 0);
14275  }
14276 
14277 
14278  cout << " ----------------------------------- SerializationCompressionLevelsTest() OK" << endl;
14279 }
14280 
14281 
14282 
14283 static
14285 {
14286  cout << " ----------------------------------- SparseSerializationTest()" << endl;
14289  bm::serializer<bvect> bv_ser(tb);
14290  bm::serializer<bvect>::buffer sermem_buf;
14291  bm::serializer<bvect>::buffer sermem_buf_shifted;
14292 
14293  std::vector<std::pair<bvect::size_type, bvect::size_type> > ranges;
14294 
14295  ranges.push_back(std::make_pair(0, 65535*255));
14296  ranges.push_back(std::make_pair(0, 65535*255 * 2));
14297  ranges.push_back(std::make_pair(65535*5, 65535*255 * 2));
14298  ranges.push_back(std::make_pair(65535*255/2, 65535*255));
14299  ranges.push_back(std::make_pair(65535*255/2, 65535*255 * 2));
14300  ranges.push_back(std::make_pair(bm::id_max/2 - 65535*255/2, bm::id_max/2 + 65535*255 * 2));
14301  ranges.push_back(std::make_pair(bm::id_max/2, bm::id_max/2 + 65535*255 * 2));
14302  ranges.push_back(std::make_pair(bm::id_max-65535*255*2, bm::id_max-1));
14303 
14304  for (size_t k = 0; k < ranges.size(); ++k)
14305  {
14306  bvect::size_type from = ranges[k].first;
14307  bvect::size_type to = ranges[k].second;
14308 
14309  std::cout << " range [" << from << ", " << to << "]" << std::endl;
14310 
14311  for (unsigned i = 0; i < 2; ++i)
14312  {
14313  bvect bv, bv_shifted;
14314 
14315  generate_sparse_bv(bv, from, to, 65536/10);
14316  generate_sparse_bv(bv_shifted, from+1, to+1, 65536/10);
14317 
14318  auto bv_cnt = bv.count();
14319 
14320  bv_ser.serialize(bv, sermem_buf, 0);
14321  bv_ser.serialize(bv_shifted, sermem_buf_shifted, 0);
14322 
14323  const bvect::size_type* cstat = bv_ser.get_compression_stat();
14324 
14325  {
14326  bvect::size_type sb_from = from / (65536*256);
14327  bvect::size_type sb_to = to / (65536*256);
14328  bvect::size_type sb_cnt = sb_to - sb_from + 1;
14329  assert(cstat[bm::set_sblock_bienc] == sb_cnt || cstat[bm::set_sblock_bienc] == sb_cnt-1);
14330  }
14331 
14332  bvect bv2;
14333  bm::deserialize(bv2, sermem_buf.buf());
14334  bool eq = bv.equal(bv2);
14335  assert(eq);
14336 
14337  {
14338  bvect bv3;
14339  od.deserialize(bv3, sermem_buf.buf(), tb, set_OR);
14340  eq = bv3.equal(bv2);
14341  assert(eq);
14342  }
14343  {
14344  bvect bv3;
14345  od.deserialize(bv3, sermem_buf.buf(), tb, set_XOR);
14346  eq = bv3.equal(bv);
14347  assert(eq);
14348  od.deserialize(bv3, sermem_buf.buf(), tb, set_XOR);
14349  assert(bv3.count()==0);
14350 
14351  bv3 = bv;
14352  od.deserialize(bv3, sermem_buf_shifted.buf(), tb, set_XOR);
14353  assert(bv3.count()==2*bv_cnt);
14354  }
14355  {
14356  bvect bv3;
14357  od.deserialize(bv3, sermem_buf.buf(), tb, set_XOR);
14358  eq = bv3.equal(bv);
14359  assert(eq);
14360  od.deserialize(bv3, sermem_buf.buf(), tb, set_XOR);
14361  assert(bv3.count()==0);
14362 
14363  bv3 = bv;
14364  od.deserialize(bv3, sermem_buf_shifted.buf(), tb, set_XOR);
14365  assert(bv3.count()==2*bv_cnt);
14366  }
14367  {
14368  bvect bv3(bv);
14369  od.deserialize(bv3, sermem_buf_shifted.buf(), tb, set_SUB);
14370  eq = bv3.equal(bv);
14371  assert(eq);
14372  od.deserialize(bv3, sermem_buf.buf(), tb, set_SUB);
14373  assert(bv3.count()==0);
14374  }
14375  {
14376  bvect bv3(bv);
14377  od.deserialize(bv3, sermem_buf.buf(), tb, set_AND);
14378  eq = bv3.equal(bv);
14379  assert(eq);
14380  od.deserialize(bv3, sermem_buf_shifted.buf(), tb, set_AND);
14381  assert(bv3.count()==0);
14382  }
14383 
14384 
14385  {
14386  bvect bv3(bv);
14387  auto cnt = od.deserialize(bv3, sermem_buf.buf(), tb, set_COUNT_OR);
14388  assert(cnt == bv_cnt);
14389 
14390  cnt = od.deserialize(bv3, sermem_buf_shifted.buf(), tb, set_COUNT_OR);
14391  assert(cnt == 2*bv_cnt);
14392  }
14393 
14394  {
14395  bvect bv3;
14396  auto cnt = od.deserialize(bv3, sermem_buf.buf(), tb, set_COUNT_XOR);
14397  assert(cnt == bv_cnt);
14398 
14399  cnt = od.deserialize(bv, sermem_buf.buf(), tb, set_COUNT_XOR);
14400  assert(cnt==0);
14401 
14402  bv3 = bv_shifted;
14403  cnt = od.deserialize(bv3, sermem_buf_shifted.buf(), tb, set_COUNT_XOR);
14404  assert(cnt==0);
14405  }
14406  {
14407  bvect bv3(bv);
14408  auto cnt = od.deserialize(bv3, sermem_buf.buf(), tb, set_COUNT_AND);
14409  assert(cnt == bv_cnt);
14410  cnt = od.deserialize(bv3, sermem_buf_shifted.buf(), tb, set_COUNT_AND);
14411  assert(cnt==0);
14412  }
14413 
14414 
14415  bv.optimize(tb);
14416  bv_shifted.optimize(tb);
14417  } // for
14418  }
14419 
14420  cout << " ----------------------------------- SparseSerializationTest() OK" << endl;
14421 }
14422 
14423 static
14425 {
14426 
14427  cout << " ----------------------------------- SerializationTest" << endl;
14428 
14429  cout << "Compression level test (GAP blocks)" << endl;
14430 
14431 
14432 
14433  // ------------------------------------------------------------
14434 
14435  cout << "Serialization STEP 0" << endl;
14436 
14437  {
14438  unsigned size = BITVECT_SIZE/6000;
14439 
14440 
14441  bvect_mini* bvect_min1= new bvect_mini(BITVECT_SIZE);
14442  bvect* bvect_full1= new bvect();
14443  bvect* bvect_full2= new bvect();
14444  bvect* bv_target_s= new bvect();
14445 
14446  bvect_full1->set_new_blocks_strat(bm::BM_BIT);
14447  bvect_full2->set_new_blocks_strat(bm::BM_BIT);
14448 
14449  for(unsigned i = 0; i < size; ++i)
14450  {
14451  bvect_full1->set_bit(i);
14452  bvect_min1->set_bit(i);
14453  }
14454 
14455  bvect_full1->optimize();
14456  CheckVectors(*bvect_min1, *bvect_full1, size, true);
14457 
14458 
14459 
14460  bvect::statistics st;
14461  bvect_full1->calc_stat(&st);
14463 
14464  bm::serializer<bvect> bv_ser(tb);
14465 
14466  bm::serializer<bvect>::buffer sermem_buf;
14467  bv_ser.optimize_serialize_destroy(*bvect_full1, sermem_buf);
14468  unsigned slen = (unsigned)sermem_buf.size();
14469 
14470  cout << "Serialized mem_max = " << st.max_serialize_mem
14471  << " size= " << slen
14472  << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
14473  << endl;
14474 
14475  bm::deserialize(*bvect_full2, sermem_buf.buf());
14476 
14478  od.deserialize(*bv_target_s,
14479  sermem_buf.buf(),
14480  tb,
14481  set_OR);
14482 
14483 
14484  CheckVectors(*bvect_min1, *bvect_full2, size, true);
14485  CheckVectors(*bvect_min1, *bv_target_s, size, true);
14486 
14487 
14488  delete bvect_full2;
14489  delete bvect_min1;
14490  delete bvect_full1;
14491  delete bv_target_s;
14492 
14493  }
14494 
14495 
14496  {
14497  unsigned size = BITVECT_SIZE/6000;
14498 
14499 
14500  bvect_mini* bvect_min1= new bvect_mini(BITVECT_SIZE);
14501  bvect* bvect_full1= new bvect();
14502  bvect* bvect_full2= new bvect();
14503  bvect* bv_target_s= new bvect();
14504 
14505  bvect_full1->set_new_blocks_strat(bm::BM_BIT);
14506  bvect_full2->set_new_blocks_strat(bm::BM_BIT);
14507 
14508  bvect_full1->set_bit(131072);
14509  bvect_min1->set_bit(131072);
14510 
14511 
14512  bvect_full1->optimize();
14513 
14514  bvect::statistics st;
14515  bvect_full1->calc_stat(&st);
14516  unsigned char* sermem = new unsigned char[st.max_serialize_mem];
14517  size_t slen = bm::serialize(*bvect_full1, sermem);
14518  cout << "Serialized mem_max = " << st.max_serialize_mem
14519  << " size= " << slen
14520  << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
14521  << endl;
14522 
14523  bm::deserialize(*bvect_full2, sermem);
14524 
14526  od.deserialize(*bv_target_s,
14527  sermem,
14528  0,
14529  set_OR);
14530 
14531  delete [] sermem;
14532 
14533  CheckVectors(*bvect_min1, *bvect_full2, size, true);
14534  CheckVectors(*bvect_min1, *bv_target_s, size, true);
14535 
14536  delete bvect_full2;
14537  delete bvect_min1;
14538  delete bvect_full1;
14539  delete bv_target_s;
14540 
14541  }
14542 
14543 
14544  cout << "Serialization STEP 1." << endl;
14545 
14546  {
14547  bvect_mini bvect_min1(BITVECT_SIZE);
14548  bvect bvect_full1;
14549 
14550  bvect_full1.set_new_blocks_strat(bm::BM_GAP);
14551 
14552  unsigned min = BITVECT_SIZE / 2 - ITERATIONS;
14553  unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
14554  if (max > BITVECT_SIZE)
14555  max = BITVECT_SIZE - 1;
14556 
14557  unsigned len = max - min;
14558 
14559  FillSets(&bvect_min1, &bvect_full1, min, max, 0);
14560  FillSets(&bvect_min1, &bvect_full1, 0, len, 5);
14561 
14562  // shot some random bits
14563 
14564  unsigned i;
14565  for (i = 0; i < 10000; ++i)
14566  {
14567  unsigned bit = unsigned(rand()) % BITVECT_SIZE;
14568  bvect_full1.set_bit(bit);
14569  bvect_min1.set_bit(bit);
14570  }
14571 
14572  bvect::statistics st;
14573  bvect_full1.calc_stat(&st);
14574 
14575  unsigned char* sermem = new unsigned char[st.max_serialize_mem];
14576  print_stat(cout,bvect_full1);
14577 
14578  size_t slen = bm::serialize(bvect_full1, sermem);
14579 
14580  cout << "Serialized len = " << slen << endl;
14581 
14582  bvect bvect_full3;
14583  bm::deserialize(bvect_full3, sermem);
14584  bvect* bv_target_s = new bvect();
14585 
14587  od.deserialize(*bv_target_s,
14588  sermem,
14589  0,
14590  set_OR);
14591 
14592  CheckVectors(bvect_min1, bvect_full3, max+10, true);
14593  CheckVectors(bvect_min1, *bv_target_s, max+10, true);
14594 
14595  delete [] sermem;
14596  delete bv_target_s;
14597 
14598  }
14599 
14600 
14601  cout << "Stage 2" << endl;
14602 
14603  {
14604 
14605  bvect_mini* bvect_min1= new bvect_mini(BITVECT_SIZE);
14606 // bm::bvect_mini* bvect_min2= new bm::bvect_mini(BITVECT_SIZE);
14607  bvect* bvect_full1= new bvect();
14608  bvect* bvect_full2= new bvect();
14609 
14610  bvect_full1->set_new_blocks_strat(bm::BM_GAP);
14611  bvect_full2->set_new_blocks_strat(bm::BM_GAP);
14612 
14613  FillSetsRandomMethod(bvect_min1, bvect_full1, 1, BITVECT_SIZE-10, 1);
14614 // FillSetsRandomMethod(bvect_min2, bvect_full2, 1, BITVECT_SIZE-10, 1);
14615 
14616  bvect::statistics st;
14617  bvect_full1->calc_stat(&st);
14618 
14619  bm::serializer<bvect> bv_ser;
14620  bm::serializer<bvect>::buffer sermem_buf;
14621 
14622  bv_ser.serialize(*bvect_full1, sermem_buf, &st);
14623  unsigned slen = (unsigned)sermem_buf.size();
14624 
14625  cout << "Serialized mem_max = " << st.max_serialize_mem
14626  << " size= " << slen
14627  << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
14628  << endl;
14629  bm::deserialize(*bvect_full2, sermem_buf.buf());
14630  bvect* bv_target_s=new bvect();
14631 
14632 
14634  od.deserialize(*bv_target_s,
14635  sermem_buf.buf(),
14636  0,
14637  set_OR);
14638 
14639  CheckVectors(*bvect_min1, *bvect_full2, BITVECT_SIZE, true);
14640  CheckVectors(*bvect_min1, *bv_target_s, BITVECT_SIZE, true);
14641 
14642  delete bv_target_s;
14643  delete bvect_full2;
14644  delete bvect_min1;
14645  delete bvect_full1;
14646 
14647  }
14648 
14649 
14650 
14651  cout << "Stage 3" << endl;
14652 
14653  {
14654 
14655  bvect_mini* bvect_min1= new bvect_mini(BITVECT_SIZE);
14656  bvect_mini* bvect_min2= new bvect_mini(BITVECT_SIZE);
14657  bvect* bvect_full1= new bvect();
14658  bvect* bvect_full2= new bvect();
14659 
14660  bvect_full1->set_new_blocks_strat(bm::BM_GAP);
14661  bvect_full2->set_new_blocks_strat(bm::BM_GAP);
14662 
14663 
14664  FillSetsRandomMethod(bvect_min1, bvect_full1, 1, BITVECT_SIZE, 1);
14665  FillSetsRandomMethod(bvect_min2, bvect_full2, 1, BITVECT_SIZE, 1);
14666  CheckVectors(*bvect_min1, *bvect_full1, BITVECT_SIZE, true);
14667  CheckVectors(*bvect_min2, *bvect_full2, BITVECT_SIZE, true);
14668 
14669 
14670  bvect::statistics st;
14671  bvect_full1->calc_stat(&st);
14672  unsigned char* sermem = new unsigned char[st.max_serialize_mem];
14673  size_t slen = bm::serialize(*bvect_full1, sermem);
14674 
14675  bvect bvt;
14676  bm::deserialize(bvt, sermem);
14677  if (bvt != *bvect_full1)
14678  {
14679  print_stat(cout,bvt);
14680  print_stat(cout,*bvect_full1);
14681  cout << "Error!" << endl;
14682  exit(1);
14683  }
14684 
14685  CheckVectors(*bvect_min1, *bvect_full1, BITVECT_SIZE, true);
14686  CheckVectors(*bvect_min2, *bvect_full2, BITVECT_SIZE, true);
14687 
14688  cout << "Serialized mem_max = " << st.max_serialize_mem
14689  << " size= " << slen
14690  << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
14691  << endl;
14692 
14693  bvect* bv_target_s=new bvect(*bvect_full2);
14694  print_stat(cout,*bv_target_s);
14695 
14696  print_stat(cout,*bvect_full2);
14697 
14698  bvect* bvect_full3= new bvect();
14699  *bvect_full3 = *bvect_full1;
14700  *bvect_full3 |= *bvect_full2;
14701 // CheckVectors(*bvect_min2, *bvect_full3, BITVECT_SIZE, true);
14702 
14703 
14704  bm::deserialize(*bvect_full2, sermem);
14705 
14707  od.deserialize(*bv_target_s,
14708  sermem,
14709  0,
14710  set_OR);
14711  delete [] sermem;
14712 
14713  CheckVectors(*bvect_min1, *bvect_full1, BITVECT_SIZE, true);
14714 // CheckVectors(*bvect_min1, *bvect_full3, BITVECT_SIZE, true);
14715 
14716  bvect_min2->combine_or(*bvect_min1);
14717  delete bvect_min1;
14718 
14719  if (*bvect_full2 != *bvect_full3)
14720  {
14721  print_stat(cout,*bvect_full2);
14722  print_stat(cout,*bvect_full3);
14723 
14724  cout << "Error!" << endl;
14725  exit(1);
14726  }
14727 
14728 
14729  CheckVectors(*bvect_min2, *bvect_full2, BITVECT_SIZE, true);
14730  CheckVectors(*bvect_min2, *bv_target_s, BITVECT_SIZE, true);
14731 
14732  delete bv_target_s;
14733  delete bvect_full1;
14734  delete bvect_full2;
14735  delete bvect_full3;
14736  delete bvect_min2;
14737 
14738 
14739  }
14740 
14741  cout << "Stage 4. " << endl;
14742 
14743  {
14744  unsigned size = BITVECT_SIZE/3;
14745 
14746 
14747  bvect_mini* bvect_min1= new bvect_mini(BITVECT_SIZE);
14748  bvect* bvect_full1= new bvect();
14749  bvect* bvect_full2= new bvect();
14750 
14751  bvect_full1->set_new_blocks_strat(bm::BM_BIT);
14752  bvect_full2->set_new_blocks_strat(bm::BM_BIT);
14753 
14754  unsigned i;
14755  for(i = 0; i < 65000; ++i)
14756  {
14757  bvect_full1->set_bit(i);
14758  bvect_min1->set_bit(i);
14759  }
14760 
14761  for(i = 65536; i < 65536+65000; ++i)
14762  {
14763  bvect_full1->set_bit(i);
14764  bvect_min1->set_bit(i);
14765  }
14766 
14767  for (i = 65536*2; i < size/6; ++i)
14768  {
14769  bvect_full1->set_bit(i);
14770  bvect_min1->set_bit(i);
14771  }
14772 
14773 
14774  bvect_full1->optimize();
14775 
14776  print_stat(cout,*bvect_full1);
14777 
14778  bvect::statistics st;
14779  bvect_full1->calc_stat(&st);
14780  unsigned char* sermem = new unsigned char[st.max_serialize_mem];
14781  size_t slen = bm::serialize(*bvect_full1, sermem);
14782  cout << "Serialized mem_max = " << st.max_serialize_mem
14783  << " size= " << slen
14784  << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
14785  << endl;
14786 
14787  unsigned char* new_sermem = new unsigned char[st.max_serialize_mem];
14788  ::memcpy(new_sermem, sermem, slen);
14789 
14790  bvect bv_target_s(*bvect_full2);
14791 
14792  bm::deserialize(*bvect_full2, new_sermem);
14794 
14795  od.deserialize(bv_target_s,
14796  new_sermem,
14797  0,
14798  set_OR);
14799 
14800  delete [] sermem;
14801  delete [] new_sermem;
14802 
14803  CheckVectors(*bvect_min1, *bvect_full2, size, true);
14804  CheckVectors(*bvect_min1, bv_target_s, size, true);
14805 
14806 
14807  delete bvect_full2;
14808  delete bvect_min1;
14809  delete bvect_full1;
14810 
14811  }
14812 
14813 
14814 }
14815 
14816 template<typename SV>
14818  typename SV::size_type vector_max)
14819 {
14820  typename SV::back_insert_iterator bi(sv.get_back_inserter());
14821 
14822  unsigned v = 0;
14823  for (typename SV::size_type i = 0; i < vector_max; ++i)
14824  {
14825  unsigned plato = (unsigned)rand() % 32;
14826  for (unsigned j = 0; i < vector_max && j < plato; ++i, ++j)
14827  {
14828  *bi = v;
14829  } // for j
14830  if (++v > 100000)
14831  v = 0;
14832  unsigned nulls = (unsigned)rand() % 24;
14833  if (nulls)
14834  bi.add_null(nulls);
14835  i += nulls;
14836  } // for i
14837 }
14838 
14839 static
14841 {
14842  cout << " ------------------------------ TestSparseVectorSerialization2()" << endl;
14843 
14844 
14845  const unsigned int BSIZE = 150000000;
14846 
14847  const unsigned char* buf;
14848  bool eq;
14849  size_t sz1, sz2;
14850 
14855 
14856 
14857  {
14861 
14862  {
14863  /*
14864  unsigned off = 0;
14865  for (; off < 65536*2; ++off)
14866  {
14867  unsigned i = 0;
14868  for (; i < 256; ++i)
14869  {
14870  sv1i[i+off] = 1;
14871  }
14872  for (; i < 512; ++i)
14873  {
14874  sv1i[i+off] = 2;
14875  }
14876  off += 512;
14877  } // for off
14878  */
14879 
14880  unsigned i=0;
14881  for (; i < 256; ++i)
14882  {
14883  sv1i[i] = 1;
14884  }
14885  for (; i < 512; ++i)
14886  {
14887  sv1i[i] = 2;
14888  }
14889  for (; i < 65536; ++i)
14890  {
14891  sv1i[i] = 1;
14892  }
14893  for (; i < 65536*2; i+=2)
14894  {
14895  sv1i[i] = 1;
14896  }
14897 
14898  sv1i.optimize();
14899  }
14900  sv_serializer.enable_xor_compression();
14901  sv_serializer.serialize(sv1i, sv_lay1);
14902  {
14903  const bvect::size_type* cstat = sv_serializer.get_bv_serializer().get_compression_stat();
14904  assert(cstat[bm::set_block_xor_ref32]>=1);
14905  }
14906 
14907  buf = sv_lay1.buf();
14908  sz1 = sv_lay1.size();
14909 
14910  sv_deserial.deserialize(sv1o, buf);
14911 
14912  eq = sv1i.equal(sv1o);
14913  assert(eq);
14914  if (!eq)
14915  {
14916  cerr << "Failed test (GAP SV XOR)" << endl;
14917  exit(1);
14918  }
14919  sv_serializer.disable_xor_compression();
14920  }
14921 
14922  cout << "Test data-frame XOR compression" << endl;
14923  {
14924  sparse_vector_u32 sv1i, sv3i(bm::use_null);
14925  sparse_vector_i32 sv2i;
14926  sparse_vector_u32 sv1o, sv3o(bm::use_null);
14927  sparse_vector_i32 sv2o;
14928 
14931 
14932 
14933  for (unsigned i = 0; i < 3*65536; i+=2)
14934  {
14935  sv1i[i] = 4;
14936  sv2i.set(i, -8);
14937  sv3i[i] = 0;
14938  }
14939 
14940  for (unsigned pass = 0; pass < 2; ++pass)
14941  {
14943  // add references in reverse(!) order
14944  bv_ref.add_vectors(sv3i.get_bmatrix());
14945  auto ref_sz = bv_ref.size();
14946  assert(ref_sz == 1);
14947  bv_ref.add_vectors(sv2i.get_bmatrix());
14948  ref_sz = bv_ref.size();
14949  assert(ref_sz == 5);
14950  bv_ref.add_vectors(sv1i.get_bmatrix());
14951  ref_sz = bv_ref.size();
14952  assert(ref_sz == 6);
14953 
14954  sv_serializer.set_xor_ref(&bv_ref);
14955  assert(sv_serializer.is_xor_ref());
14956 
14958 // sv_serializer.compute_sim_model(sim_model, bv_ref, bm::xor_sim_params());
14959 
14960  {
14963  bm::xor_sim_params xor_search_params;
14964 
14965  pbuilder.build_plan(tbatch, sim_model,
14966  bv_ref, xor_search_params);
14967 
14968  typedef
14970  pool_type tpool; // our thread pool here (no threads created yet)
14971  tpool.start(2); // start the threads
14972  {
14974  exec.run(tpool, tbatch, true);
14975  }
14976  tpool.set_stop_mode(pool_type::stop_when_done);
14977  tpool.join();
14978  {
14980  sv_serializer.compute_sim_model(sim_model_c, bv_ref, xor_search_params);
14981  Check_SimModel(sim_model_c, sim_model);
14982  }
14983  }
14984 
14985  sv_serializer.set_sim_model(&sim_model);
14986 
14987  sv_serializer.serialize(sv1i, sv_lay1);
14988  {
14989  const bvect::size_type* cstat = sv_serializer.get_bv_serializer().get_compression_stat();
14990  assert(cstat[bm::set_block_ref_eq]==0);
14991  }
14992  svi_serializer.serialize(sv2i, sv_lay2);
14993  {
14994  const bvect::size_type* cstat = svi_serializer.get_bv_serializer().get_compression_stat();
14995  assert(cstat[bm::set_block_ref_eq]>=1 || cstat[bm::set_block_xor_ref32] >= 1);
14996  }
14997  sv_serializer.serialize(sv3i, sv_lay3);
14998  {
14999  const bvect::size_type* cstat = sv_serializer.get_bv_serializer().get_compression_stat();
15000  assert(cstat[bm::set_block_ref_eq]>=1 || cstat[bm::set_block_xor_ref32] >= 1);
15001  }
15002 
15003  // ----------
15004 
15005 
15007 
15008  buf = sv_lay1.buf();
15009  sz2 = sv_lay1.size();
15010 
15011 
15012  sv_deserial.deserialize_structure(sv1o, sv_lay1.buf());
15013  svi_deserial.deserialize_structure(sv2o, sv_lay2.buf());
15014  sv_deserial.deserialize_structure(sv3o, sv_lay3.buf());
15015 
15016  bv_ref_d.add_vectors(sv3o.get_bmatrix());
15017  bv_ref_d.add_vectors(sv2o.get_bmatrix());
15018  bv_ref_d.add_vectors(sv1o.get_bmatrix());
15019 
15020  sv_deserial.set_xor_ref(&bv_ref_d);
15021 
15022  sv_deserial.deserialize(sv1o, buf, false);
15023  eq = sv1i.equal(sv1o);
15024  assert(eq);
15025 
15026  buf = sv_lay2.buf();
15027  sz2 = sv_lay2.size();
15028 
15029  svi_deserial.deserialize(sv2o, buf, false);
15030  eq = sv2i.equal(sv2o);
15031  assert(eq);
15032 
15033  buf = sv_lay3.buf();
15034  sz2 = sv_lay3.size();
15035 
15036  sv_deserial.deserialize(sv3o, buf, false);
15037  eq = sv3i.equal(sv3o);
15038  assert(eq);
15039 
15040  sv_deserial.set_xor_ref(0); // unset
15041 
15042  sv1i.optimize();
15043  sv2i.optimize();
15044  sv3i.optimize();
15045 
15046  } // for pass
15047  }
15048  cout << "Test data-frame XOR compression - OK" << endl;
15049 
15050  // -------------------------------------------------
15051 
15052 
15056 
15057 
15058 
15059  generate_serialization_test_set(sv1, BSIZE);
15060 
15062 
15063  for (unsigned k = 0; k < 2; ++k)
15064  {
15065  {
15066  {
15067  sv_serializer.set_xor_ref(false); // disable XOR compression
15068  sv_serializer.serialize(sv1, sv_lay);
15069  }
15070 
15071  buf = sv_lay.buf();
15072  sz1 = sv_lay.size();
15073 
15074  sv_deserial.deserialize(sv2, buf);
15075 
15076  eq = sv1.equal(sv2);
15077  if (!eq)
15078  {
15079  cerr << "Error: SparseVectorSerializationTest() integrity failure! (1)" << endl;
15081 
15082  bool f = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
15083  assert(f);
15084  cerr << "Mismatch at: " << pos << endl;
15085 
15086  sv_deserial.deserialize(sv2, buf);
15087 
15088  exit(1);
15089  }
15090  sv2.resize(0);
15091  }
15092 
15093  // check range de-serialization
15094  {
15096  sparse_vector_u32::size_type left = BSIZE / 2;
15097  sparse_vector_u32::size_type right = BSIZE;
15098 
15099  sv_deserial.deserialize_range(sv4, buf, left, right);
15100 
15102  sv_r.copy_range(sv1, left, right);
15103 
15104  eq = sv_r.equal(sv4);
15105  if (!eq)
15106  {
15108  bool f = bm::sparse_vector_find_first_mismatch(sv1, sv4, pos);
15109  assert(f);
15110  cerr << "Mismatch at: " << pos << endl;
15111  assert(eq);
15112  exit(1);
15113  }
15114  }
15115 
15116 
15117  {
15118  sv_serializer.set_xor_ref(true); // enable XOR compression
15119  sv_serializer.serialize(sv1, sv_lay);
15120  }
15121 
15122  buf = sv_lay.buf();
15123  sz2 = sv_lay.size();
15124 
15125  sv_deserial.deserialize(sv3, buf);
15126  eq = sv1.equal(sv3);
15127  if (!eq)
15128  {
15129  cerr << "Error: SparseVectorSerializationTest() integrity failure! (2)" << endl;
15131  bool f = bm::sparse_vector_find_first_mismatch(sv1, sv3, pos);
15132  assert(f);
15133  cerr << "Mismatch at: " << pos << endl;
15134 
15135  sv_deserial.deserialize(sv3, buf);
15136 
15137  exit(1);
15138  }
15139 
15140  if (sz2 > sz1)
15141  {
15142  cerr << "XOR negative compression!" << endl;
15143  assert(0);
15144  }
15145  else
15146  {
15147  cout << "sz1 = " << sz1 << " gain=" << (sz1 - sz2) << endl;
15148  }
15149 
15150  // check range deserialization
15151  {
15153  sparse_vector_u32::size_type left = BSIZE / 2;
15154  sparse_vector_u32::size_type right = BSIZE;
15155 
15156  sv_deserial.deserialize_range(sv4, buf, left, right);
15157 
15159  sv_r.copy_range(sv1, left, right);
15160 
15161  eq = sv_r.equal(sv4);
15162  if (!eq)
15163  {
15165  bool f = bm::sparse_vector_find_first_mismatch(sv_r, sv4, pos);
15166  assert(f);
15167  cerr << "Mismatch at: " << pos << endl;
15168 
15169  auto v4 = sv4[pos];
15170  auto v_r = sv_r[pos];
15171  auto v1 = sv1.get(pos);
15172  cout << "v4=" << v4 << " v_r=" << v_r << " v1=" << v1 << endl;
15173 
15174  assert(eq);
15175  exit(1);
15176  }
15177  }
15178 
15179  sv1.optimize();
15180  sv_serializer.set_bookmarks(true, (unsigned)rand()%16);
15181 
15182  } // for k
15183 
15184 
15185 
15186 
15187  cout << " ------------------------------ TestSparseVectorSerialization2() OK" << endl;
15188 
15189 }
15190 
15191 
15192 
15193 
15194 
15195 static
15197 {
15198  cout << "-------------------------------------------- GetNextTest" << endl;
15199 
15200  cout << "testing bvector<>::find() in bit-mode" << endl;
15201 
15202  {
15203  bvect bv;
15204  bv.set();
15205  bvect::size_type pos=1;
15206  bool b;
15207  b = bv.find(pos);
15208  assert(pos == 0);
15209  assert(b);
15210 
15211  b = bv.find(bm::id_max/2, pos);
15212  assert(b);
15213  assert(pos == bm::id_max/2);
15214 
15215  bv.set(bm::id_max/2, false);
15216  b = bv.find(bm::id_max/2, pos);
15217  assert(b);
15218  assert(pos == bm::id_max/2 + 1);
15219 
15220  b = bv.find_reverse(pos);
15221  assert(b);
15222  assert(pos == bm::id_max-1);
15223 
15224  bvect::size_type f, l;
15225  b = bv.find_range(f, l);
15226  assert(b);
15227  assert(f == 0);
15228  assert(l == bm::id_max-1);
15229  }
15230 
15231  {
15232  bvect bv;
15233  bool found;
15234  bm::id_t pos;
15235  found = bv.find(0, pos);
15236 
15237  if (found)
15238  {
15239  cout << "1. find() failed" << endl;
15240  exit(1);
15241  }
15242  found = bv.find_reverse(pos);
15243  assert(!found);
15244 
15245  bv[0] = true;
15246  found = bv.find(0, pos);
15247  if (!found || pos != 0)
15248  {
15249  cout << "2. find() failed " << pos << endl;
15250  exit(1);
15251  }
15252  found = bv.find_reverse(pos);
15253  assert(found && pos == 0);
15254 
15255  bv[0] = false;
15256  found = bv.find_reverse(pos);
15257  assert(!found);
15258  bv[0] = true;
15259 
15260  found = bv.find(1, pos);
15261  if (found)
15262  {
15263  cout << "3. find() failed" << endl;
15264  exit(1);
15265  }
15266 
15267  bv[100000] = true;
15268  bv[100001] = true;
15269  found = bv.find(1, pos);
15270  if (!found || pos != 100000)
15271  {
15272  cout << "4. find() failed " << pos << " " << found << endl;
15273  exit(1);
15274  }
15275  found = bv.find_reverse(pos);
15276  assert(found && pos == 100001);
15277 
15278  found = bv.find(100000, pos);
15279  if (!found || pos != 100000)
15280  {
15281  cout << "5. find() failed " << pos << " " << found << endl;
15282  exit(1);
15283  }
15284  found = bv.find(100001, pos);
15285  if (!found || pos != 100001)
15286  {
15287  cout << "6. find() failed " << pos << " " << found << endl;
15288  exit(1);
15289  }
15290  found = bv.find(100002, pos);
15291  if (found)
15292  {
15293  cout << "7. find() failed "<< endl;
15294  exit(1);
15295  }
15296  bv[100001] = false;
15297  found = bv.find_reverse(pos);
15298  assert(found && pos == 100000);
15299  }
15300 
15301  cout << "testing bvector<>::find() in GAP-mode" << endl;
15302 
15303  {
15304  bvect bv(BM_GAP);
15305  bool found;
15306  bm::id_t pos;
15307  found = bv.find(0, pos);
15308  if (found)
15309  {
15310  cout << "1. find() failed" << endl;
15311  exit(1);
15312  }
15313  found = bv.find_reverse(pos);
15314  assert(!found);
15315 
15316  bv[0] = true;
15317  found = bv.find(0, pos);
15318  if (!found || pos != 0)
15319  {
15320  cout << "2. find() failed " << pos << endl;
15321  exit(1);
15322  }
15323  found = bv.find_reverse(pos);
15324  assert(found && pos == 0);
15325 
15326  found = bv.find(1, pos);
15327  if (found)
15328  {
15329  cout << "3. find() failed" << endl;
15330  exit(1);
15331  }
15332  bv[100000] = true;
15333  bv[100001] = true;
15334  found = bv.find(1, pos);
15335  if (!found || pos != 100000)
15336  {
15337  cout << "4. find() failed " << pos << " " << found << endl;
15338  exit(1);
15339  }
15340  found = bv.find(100000, pos);
15341  if (!found || pos != 100000)
15342  {
15343  cout << "5. find() failed " << pos << " " << found << endl;
15344  exit(1);
15345  }
15346  found = bv.find(100001, pos);
15347  if (!found || pos != 100001)
15348  {
15349  cout << "6. find() failed " << pos << " " << found << endl;
15350  exit(1);
15351  }
15352  found = bv.find_reverse(pos);
15353  assert(found && pos == 100001);
15354 
15355  found = bv.find(100002, pos);
15356  if (found)
15357  {
15358  cout << "7. find() failed "<< endl;
15359  exit(1);
15360  }
15361  bv[100001] = false;
15362  found = bv.find_reverse(pos);
15363  assert(found && pos == 100000);
15364 
15365 
15366  }
15367 
15368  {
15369  bvect bv;
15370  bool found;
15371 
15372  bv.set_range(100000, 20000000);
15373  bm::id_t pos;
15374  found = bv.find_reverse(pos);
15375  assert(found && pos == 20000000);
15376 
15377  bv.optimize();
15378  found = bv.find_reverse(pos);
15379  assert(found && pos == 20000000);
15380 
15381  bv[bm::id_max-1] = true;
15382  found = bv.find_reverse(pos);
15383  assert(found && pos == bm::id_max-1);
15384 
15385  bv[bm::id_max-1] = false;
15386  found = bv.find_reverse(pos);
15387  assert(found && pos == 20000000);
15388 
15389  bv.set_range(100000, 20000000, false);
15390  found = bv.find_reverse(pos);
15391  assert(!found);
15392 
15393  found = bv.find(0, pos);
15394  assert(!found);
15395  }
15396 
15397  {
15398  bvect bv;
15399  bool found;
15400  bm::id_t pos;
15401  bv.invert();
15402 
15403  found = bv.find_reverse(pos);
15404  assert(found && pos == bm::id_max-1);
15405 
15406  bv.optimize();
15407  found = bv.find_reverse(pos);
15408  assert(found && pos == bm::id_max-1);
15409 
15410  bv.invert();
15411  found = bv.find_reverse(pos);
15412  assert(!found);
15413  found = bv.find(0, pos);
15414  assert(!found);
15415  }
15416 
15417 
15418  int i;
15419  for(i = 0; i < 2; ++i)
15420  {
15421  cout << "Strategy " << i << endl;
15422 
15423  {
15424  bvect bvect_full1;
15425  bvect_mini bvect_min1(BITVECT_SIZE);
15426 
15427  bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
15428 
15429  bvect_full1.set_bit(0);
15430  bvect_min1.set_bit(0);
15431 
15432 
15433  bvect_full1.set_bit(65536);
15434  bvect_min1.set_bit(65536);
15435 
15436  unsigned nbit1 = bvect_full1.get_first();
15437  unsigned nbit2 = bvect_min1.get_first();
15438 
15439  if (nbit1 != nbit2)
15440  {
15441  cout << "1. get_first failed() " << nbit1 << " " << nbit2 << endl;
15442  exit(1);
15443  }
15444  nbit1 = bvect_full1.get_next(nbit1);
15445  nbit2 = bvect_min1.get_next(nbit2);
15446  if ((nbit1 != nbit2) || (nbit1 != 65536))
15447  {
15448  cout << "1. get_next failed() " << nbit1 << " " << nbit2 << endl;
15449  exit(1);
15450  }
15451  }
15452 
15453 
15454 
15455  {
15456  bvect bvect_full1;
15457  bvect_mini bvect_min1(BITVECT_SIZE);
15458  bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
15459 
15460  bvect_full1.set_bit(65535);
15461  bvect_min1.set_bit(65535);
15462 
15463  unsigned nbit1 = bvect_full1.get_first();
15464  unsigned nbit2 = bvect_min1.get_first();
15465 
15466  if ((nbit1 != nbit2) || (nbit1 != 65535))
15467  {
15468  cout << "1. get_first failed() " << nbit1 << " " << nbit2 << endl;
15469  exit(1);
15470  }
15471  nbit1 = bvect_full1.get_next(nbit1);
15472  nbit2 = bvect_min1.get_next(nbit2);
15473  if (nbit1 != nbit2 )
15474  {
15475  cout << "1. get_next failed() " << nbit1 << " " << nbit2 << endl;
15476  exit(1);
15477  }
15478  }
15479 
15480  {
15481  cout << "--------------" << endl;
15482  bvect bvect_full1;
15483  bvect_mini bvect_min1(BITVECT_SIZE);
15484  bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
15485 
15486  bvect_full1.set_bit(655350);
15487  bvect_min1.set_bit(655350);
15488 
15489  unsigned nbit1 = bvect_full1.get_first();
15490  unsigned nbit2 = bvect_min1.get_first();
15491 
15492  if (nbit1 != nbit2 || nbit1 != 655350)
15493  {
15494  cout << "1. get_first failed() " << nbit1 << " " << nbit2 << endl;
15495  exit(1);
15496  }
15497 
15498  nbit1 = bvect_full1.get_next(nbit1);
15499  nbit2 = bvect_min1.get_next(nbit2);
15500  if (nbit1 != nbit2)
15501  {
15502  cout << "1. get_next failed() " << nbit1 << " " << nbit2 << endl;
15503  exit(1);
15504  }
15505  }
15506 
15507 
15508  {
15509  bvect bvect_full1;
15510  bvect_mini bvect_min1(BITVECT_SIZE);
15511 
15512  bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
15513 
15514  bvect_full1.set_bit(256);
15515  bvect_min1.set_bit(256);
15516 
15517 // bvect_full1.clear_bit(256);
15518  bvect_full1.set_bit(65536);
15519  bvect_min1.set_bit(65536);
15520 
15521  unsigned nbit1 = bvect_full1.get_first();
15522  unsigned nbit2 = bvect_min1.get_first();
15523 
15524  if (nbit1 != nbit2)
15525  {
15526  cout << "get_first failed " << nbit1 << " " << nbit2 << endl;
15527  exit(1);
15528  }
15529 
15530  unsigned last_found = 0;
15531  while (nbit1)
15532  {
15533  cout << nbit1 << endl;
15534  nbit1 = bvect_full1.get_next(nbit1);
15535  nbit2 = bvect_min1.get_next(nbit2);
15536  if (nbit1 != nbit2)
15537  {
15538  cout << "get_next failed " << nbit1 << " " << nbit2 << endl;
15539  exit(1);
15540  }
15541  if (nbit1)
15542  last_found = nbit1;
15543  } // while
15544 
15545  unsigned pos = 0;
15546  bool found = bvect_full1.find_reverse(pos);
15547  assert(found && pos == last_found);
15548 
15549  }
15550 
15551 
15552  }// for
15553 
15554 }
15555 
15556 // Test contributed by Maxim Shemanarev.
15557 static
15558 void MaxSTest()
15559 {
15560  cout << "\n ------------------------------------ MaxSTest()\n";
15561  bvect vec;
15562 
15563  int i, j;
15564  unsigned id;
15565  for(i = 0; i < 100; i++)
15566  {
15567  int n = rand() % 2000 + 1;
15568  id = 1;
15569  for(j = 0; j < n; j++)
15570  {
15571  id += (unsigned)rand() % 10 + 1;
15572  vec.set_bit(id);
15573 
15574  }
15575  vec.optimize();
15576  vec.clear();
15577  }
15578  cout << "\n ------------------------------------ MaxSTest() OK \n\n";
15579 }
15580 
15581 static
15583 {
15584  printf("BeginMask:\n");
15585 
15586  unsigned i;
15587  for (i = 0; i < 32; ++i)
15588  {
15589  {
15590  unsigned mask_r = bm::mask_r_u32(i);
15592  unsigned mask_l = bm::mask_l_u32(i);
15594  }
15595  unsigned mask = 0;
15596 
15597  for(unsigned j = i; j < 32; ++j)
15598  {
15599  //unsigned nbit = j; (void)nbit;
15600  //nbit &= bm::set_word_mask;
15601  bm::word_t mask1 = (((bm::word_t)1) << j);
15602 
15603  mask |= mask1;
15604  }
15605 
15606  printf("0x%x, ", mask);
15607 
15608  }
15609  printf("\n");
15610 }
15611 
15612 static
15614 {
15615  printf("EndMask:\n");
15616 
15617  unsigned i;
15618  for (i = 0; i < 32; ++i)
15619  {
15620  unsigned mask = 1;
15621 
15622  for(unsigned j = i; j > 0; --j)
15623  {
15624  //unsigned nbit = j;
15625  //nbit &= bm::set_word_mask;
15626  bm::word_t mask1 = (((bm::word_t)1) << j);
15627 
15628  mask |= mask1;
15629  }
15630 
15631  printf("0x%x,", mask);
15632 
15633  }
15634  printf("\n");
15635 }
15636 
15637 static
15639 {
15640  cout << "-------------------------------------------- EnumeratorTest" << endl;
15641 
15642  {
15643  bvect bvect1;
15644 
15645  bvect1.set_bit(100);
15646 
15647  {
15648  unsigned n = bvect1.get_next(101);
15649  assert(!n);
15650  }
15651 
15652  bvect::enumerator en = bvect1.first();
15653  unsigned n = bvect1.get_next(0);
15654 
15655  bvect::enumerator en1 = bvect1.get_enumerator(n);
15656  if (*en != 100 || n != 100 || *en1 != 100)
15657  {
15658  cout << "1.Enumerator error !" << endl;
15659  exit(1);
15660  }
15661  CompareEnumerators(en, en1);
15662 
15663  bvect1.clear_bit(100);
15664 
15665  bvect1.set_bit(2000000000);
15666  en.go_first();
15667  n = bvect1.get_next(0);
15668  en1.go_to(n);
15669  if (*en != 2000000000 || n != *en || *en1 != *en)
15670  {
15671  cout << "2. Enumerator error !" << endl;
15672  assert(0);
15673  exit(1);
15674  }
15675  CompareEnumerators(en, en1);
15676 
15677  bvect1.optimize();
15678  en = bvect1.first();
15679  n = bvect1.get_next(0);
15680  en1 = bvect1.first();
15681  en1.go_to(n);
15682  if (*en != 2000000000 || n != *en || *en1 != *en)
15683  {
15684  cout << "2. Enumerator error !" << endl;
15685  assert(0);
15686  exit(1);
15687  }
15688  CompareEnumerators(en, en1);
15689 
15690  }
15691 
15692  {
15693  bvect bvect1;
15694  bvect1.set_bit(0);
15695  bvect1.set_bit(10);
15696  bvect1.set_bit(35);
15697  bvect1.set_bit(1000);
15698  bvect1.set_bit(2016519);
15699  bvect1.set_bit(2034779);
15700  bvect1.set_bit(bm::id_max-1);
15701 
15702  bvect::enumerator en = bvect1.first();
15703 
15704  auto num = bvect1.get_first();
15705 
15706  bvect::enumerator end = bvect1.end();
15707  while (en < end)
15708  {
15709  cout << num << endl;
15710  bvect::enumerator en1 = bvect1.get_enumerator(num ? num-1 : num);
15711  if (*en != num || *en != *en1)
15712  {
15713  cout << "Enumeration comparison failed !" <<
15714  " enumerator = " << *en <<
15715  " get_next() = " << num <<
15716  " goto enumerator = " << *en1 <<
15717  endl;
15718  exit(1);
15719  }
15720  CompareEnumerators(en, en1);
15721 
15722  ++en;
15723  num = bvect1.get_next(num);
15724  ++en1;
15725  CompareEnumerators(en, en1);
15726  {
15727  auto num2 = num / 2;
15728  if (num2 < num)
15729  {
15730  if (num2 == 2147483647)
15731  cout << "!" << endl;
15732  auto idx0 = bvect1.get_next(num2);
15733  bvect::enumerator en3 = bvect1.get_enumerator(num2);
15734  assert(idx0 == *en3);
15735  }
15736  }
15737  }
15738  if (num != 0)
15739  {
15740  cout << "Enumeration error!" << endl;
15741  exit(1);
15742  }
15743  }
15744 
15745 
15746  {
15747  bvect bvect1;
15748 
15749  unsigned i;
15750  for(i = 0; i < 65536; ++i)
15751  {
15752  bvect1.set_bit(i);
15753  }
15754  for(i = 65536*10; i < 65536*20; i+=3)
15755  {
15756  bvect1.set_bit(i);
15757  }
15758 
15759 
15760  bvect::enumerator en = bvect1.first();
15761  unsigned num = bvect1.get_first();
15762 
15763  while (en < bvect1.end())
15764  {
15765  bvect::enumerator en1 = bvect1.get_enumerator(num);
15766  if (*en != num || *en != *en1)
15767  {
15768  cout << "Enumeration comparison failed !" <<
15769  " enumerator = " << *en <<
15770  " get_next() = " << num <<
15771  " goto enumerator = " << *en1
15772  << endl;
15773  exit(1);
15774  }
15775  ++en;
15776  num = bvect1.get_next(num);
15777  if (num == 31)
15778  {
15779  num = num + 0;
15780  }
15781  ++en1;
15782  CompareEnumerators(en, en1);
15783  }
15784  if (num != 0)
15785  {
15786  cout << "Enumeration error!" << endl;
15787  exit(1);
15788  }
15789  }
15790 
15791 
15792  {
15793  bvect bvect1;
15795  bvect1.set_bit(100);
15796 
15797  bvect::enumerator en = bvect1.first();
15798  bvect::enumerator en1 = bvect1.get_enumerator(99);
15799  if (*en != 100 || *en != *en1)
15800  {
15801  cout << "Enumerator error !" << endl;
15802  exit(1);
15803  }
15804  CompareEnumerators(en, en1);
15805 
15806  bvect1.clear_bit(100);
15807 
15808  bvect1.set_bit(2000000);
15809  en.go_first();
15810  en1.go_to(10);
15811 
15812  if (*en != 2000000 || *en != *en1)
15813  {
15814  cout << "Enumerator error !" << endl;
15815  exit(1);
15816  }
15817  CompareEnumerators(en, en1);
15818  print_stat(cout,bvect1);
15819  }
15820 
15821  {
15822  bvect bvect1;
15824  bvect1.set_bit(0);
15825  bvect1.set_bit(1);
15826  bvect1.set_bit(10);
15827  bvect1.set_bit(100);
15828  bvect1.set_bit(1000);
15829 
15830  bvect::enumerator en = bvect1.first();
15831 
15832  unsigned num = bvect1.get_first();
15833 
15834  while (en < bvect1.end())
15835  {
15836  bvect::enumerator en1 = bvect1.get_enumerator(num);
15837  if (*en != num || *en != *en1)
15838  {
15839  cout << "Enumeration comparison failed !" <<
15840  " enumerator = " << *en <<
15841  " get_next() = " << num <<
15842  " goto enumerator = " << *en1 << endl;
15843  exit(1);
15844  }
15845  CompareEnumerators(en, en1);
15846  ++en;
15847  num = bvect1.get_next(num);
15848  ++en1;
15849  CompareEnumerators(en, en1);
15850  }
15851  if (num != 0)
15852  {
15853  cout << "Enumeration error!" << endl;
15854  exit(1);
15855  }
15856  }
15857 
15858  cout << "FULL bvector enumerator stress test ..." << endl;
15859  {
15860  bvect bvect1;
15861  bvect1.set();
15862 
15863  bvect::enumerator en = bvect1.first();
15864  unsigned num = bvect1.get_first();
15865  while (en.valid())
15866  {
15867  if (*en != num)
15868  {
15869  cout << "Enumeration comparison failed !" <<
15870  " enumerator = " << *en <<
15871  " get_next() = " << num << endl;
15872  assert(0);
15873  exit(1);
15874  }
15875 
15876  ++en;
15877  num = bvect1.get_next(num);
15878  {
15879  bvect::enumerator en2(&bvect1, num);
15880  if (*en2 != num)
15881  {
15882  cout << "Enumeration comparison failed !" <<
15883  " enumerator = " << *en <<
15884  " get_next() = " << num << endl;
15885  assert(0);
15886  exit(1);
15887  }
15888  CompareEnumerators(en, en2);
15889  }
15890  if (num > (bm::set_sub_array_size * bm::gap_max_bits * 2))
15891  break;
15892  } // while
15893  }
15894  cout << "FULL bvector enumerator stress test ... OK" << endl;
15895 
15896 }
15897 
15898 
15899 static
15901 {
15902  bvect bv;
15903  bvect bv2;
15904 
15907 
15908  unsigned i;
15909  for (i = 0; i < 500; i+=1)
15910  {
15911  bv.set_bit(i);
15912  }
15913  print_stat(cout,bv);
15914 
15915  for (i = 0; i < 1000; i+=2)
15916  {
15917  bv2.set_bit(i);
15918  }
15919  print_stat(cout,bv2);
15920 
15921  struct bvect::statistics st;
15922  bv2.calc_stat(&st);
15923 
15924  unsigned char* sermem = new unsigned char[st.max_serialize_mem];
15925 
15926  size_t slen = bm::serialize(bv2, sermem);
15927  assert(slen);
15928  slen = 0;
15929 
15930  bm::deserialize(bv, sermem);
15931 
15932  print_stat(cout,bv);
15933 
15934 }
15935 
15936 static
15938 {
15939  cout << "----------------------------- BVImportTest()" << endl;
15940 
15941  {
15942  unsigned int arr[1] = {0, };
15943  arr[0] = 0;
15944  bvect bv;
15945  bm::bit_import_u32(bv, arr, sizeof(arr)/sizeof(arr[0]), false);
15946  assert(bv.count() == 0);
15947  arr[0] = 1;
15948  bm::bit_import_u32(bv, arr, sizeof(arr)/sizeof(arr[0]), false);
15949  assert(bv.count() == 1);
15950  bvect::statistics st;
15951  bv.calc_stat(&st);
15952  assert(st.bit_blocks==1);
15953  assert(st.gap_blocks==0);
15954 
15955 
15956  {
15957  bvect::enumerator en = bv.first();
15958  assert(en.valid() && *en == 0);
15959  }
15960  arr[0] = 1 | (1u << 2) | (1u << 31);
15961  bm::bit_import_u32(bv, arr, sizeof(arr)/sizeof(arr[0]), true);
15962  assert(bv.count() == 3);
15963  {
15964  bvect::enumerator en = bv.first();
15965  assert(en.valid() && *en == 0);
15966  ++en;
15967  assert(*en == 2);
15968  ++en;
15969  assert(*en == 31);
15970  bv.calc_stat(&st);
15971  assert(st.bit_blocks==0);
15972  assert(st.gap_blocks==1);
15973  }
15974  }
15975 
15976  {
15977  unsigned int arr[2048] = {0, };
15978  arr[0] = 0;
15979  bvect bv;
15980  bm::bit_import_u32(bv, arr, sizeof(arr)/sizeof(arr[0]), true);
15981  assert(bv.count() == 0);
15982  arr[0] = 1;
15983  bm::bit_import_u32(bv, arr, sizeof(arr)/sizeof(arr[0]), false);
15984  assert(bv.count() == 1);
15985  assert(bv.test(0));
15986 
15987  arr[2047] = 1u << 31;
15988  bm::bit_import_u32(bv, arr, sizeof(arr)/sizeof(arr[0]), true);
15989  assert(bv.count() == 2);
15990  print_bv(bv);
15991  assert(bv.test(0));
15992  assert(bv.test(65535));
15993  }
15994 
15995  {
15996  unsigned int arr[2048 + 10] = {0, };
15997  arr[0] = 0;
15998  bvect bv;
15999  bm::bit_import_u32(bv, arr, sizeof(arr)/sizeof(arr[0]), false);
16000  assert(bv.count() == 0);
16001  arr[0] = 1 << 16;
16002  bm::bit_import_u32(bv, arr, sizeof(arr)/sizeof(arr[0]), false);
16003  assert(bv.count() == 1);
16004  assert(bv.test(16));
16005 
16006  arr[2047] = 1u << 31;
16007  arr[2048] = 1u << 7;
16008  bm::bit_import_u32(bv, arr, sizeof(arr)/sizeof(arr[0]), false);
16009  assert(bv.count() == 3);
16010  print_bv(bv);
16011  assert(bv.test(16));
16012  assert(bv.test(65535));
16013  assert(bv.test(65536 + 7));
16014  }
16015 
16016 
16017  cout << "... import stress test" << endl;
16018  {
16019  unsigned max = 250000;
16020  for (unsigned size = 1; size < max; ++size)
16021  {
16022  std::vector<unsigned> vect;
16023  vect.resize(size);
16024  bvect bv_control;
16025  {
16026  bvect::bulk_insert_iterator iit(bv_control);
16027  for (size_t i = 0; i < vect.size(); ++i)
16028  {
16029  unsigned w = (i & 1) ? (unsigned)rand() : 0;
16030  vect[i] = w;
16031  bvect::size_type base_idx = bvect::size_type(i * 32);
16032  for (unsigned k = 0; (k < 32) && w; ++k)
16033  {
16034  unsigned mask = (1u << k);
16035  if (w & mask) {
16036  iit = (base_idx + k);
16037  }
16038  w &= ~mask;
16039  } // for k
16040  }
16041  iit.flush();
16042  }
16043 
16044  bvect bv;
16045  bm::bit_import_u32(bv, vect.data(), (unsigned)vect.size(), true);
16046  bool eq = bv.equal(bv_control);
16047  assert(eq);
16048 
16049  if (!is_silent)
16050  if (size % 256 == 0)
16051  cout << "\r" << size << " of " << max << " " << flush;
16052  } // for size
16053  }
16054 
16055 
16056  cout << "\n----------------------------- BVImportTest() OK" << endl;
16057 }
16058 
16059 
16060 
16061 static
16063 {
16064  cout << "----------------------------- IntervalEnumeratorTest()" << endl;
16065 
16066  bool valid;
16067  cout << "empty bvector tests" << endl;
16068  {
16070  valid = ien.valid();
16071  assert(!valid);
16072  }
16073 
16074  {
16075  bvect bv;
16077 
16078  valid = ien.valid();
16079  assert(!valid);
16080  }
16081 
16082  {
16085 
16086  assert(ien1 == ien2);
16087  }
16088 
16089  cout << "inverted bvector tests" << endl;
16090  {
16091  bvect bv;
16092  bv.invert();
16093 
16094  {
16096 
16097  valid = ien.valid();
16098  assert(valid);
16099  assert(ien.start() == 0);
16100  assert(ien.end() == bm::id_max-1);
16101  IntervalsEnumeratorCheck(bv, false);
16102  }
16103 
16104  bm::interval_enumerator<bvect> ien(bv, 1, false);
16105  valid = ien.valid();
16106  assert(valid);
16107  assert(ien.start() == 1);
16108  assert(ien.end() == bm::id_max-1);
16109 
16110  bm::interval_enumerator<bvect> ien2(bv, 10, false);
16111  valid = ien2.valid();
16112  assert(valid);
16113  assert(ien2.start() == 10);
16114  assert(ien2.end() == bm::id_max-1);
16115 
16116  ien.swap(ien2);
16117  assert(ien.start() == 10);
16118  assert(ien2.start() == 1);
16119 
16120  bm::interval_enumerator<bvect> ien3(std::move(ien2));
16121  assert(ien3.start() == 1);
16122  }
16123 
16124 
16125 
16126  cout << "GAP bvector tests" << endl;
16127  {
16128  bvect bv;
16129  bv.set_range(0, 33);
16130 
16132  valid = ien.valid();
16133  assert(valid);
16134  assert(ien.start() == 0);
16135  assert(ien.end() == 33);
16136 
16137  bv.set_range(bm::id_max/2, bm::id_max/2 + 2);
16138  ien.go_to(100);
16139  valid = ien.valid();
16140  assert(valid);
16141  assert(ien.start() == bm::id_max/2);
16142  assert(ien.end() == bm::id_max/2 + 2);
16143 
16144  bv.set_range(bm::id_max-1, bm::id_max-1);
16145  ien.go_to(bm::id_max-2);
16146  valid = ien.valid();
16147  assert(valid);
16148  assert(ien.start() == bm::id_max-1);
16149  assert(ien.end() == bm::id_max-1);
16150 
16151  ien.go_to(0);
16152  valid = ien.valid();
16153  assert(valid);
16154  assert(ien.start() == 0);
16155  assert(ien.end() == 33);
16156 
16157  valid = ien.advance();
16158  assert(valid);
16159  assert(ien.start() == bm::id_max/2);
16160  assert(ien.end() == bm::id_max/2 + 2);
16161 
16162  valid = ien.advance();
16163  assert(valid);
16164  assert(ien.start() == bm::id_max-1);
16165  assert(ien.end() == bm::id_max-1);
16166 
16167  valid = ien.advance();
16168  assert(!valid);
16169  IntervalsEnumeratorCheck(bv, false);
16170  }
16171 
16172  {
16173  bvect bv { 0 };
16174  {
16176 
16177  valid = ien.valid();
16178  assert(valid);
16179  assert(ien.start() == 0);
16180  assert(ien.end() == 0);
16181  }
16182 
16183  bv.set(100);
16184  bv.set(101);
16185 
16186  {
16187  bm::interval_enumerator<bvect> ien(bv, 1, false);
16188 
16189  valid = ien.valid();
16190  assert(valid);
16191  assert(ien.start() == 100);
16192  assert(ien.end() == 101);
16193  }
16194 
16196  bm::interval_enumerator<bvect> ien2(bv, 1, false);
16197  assert(ien1 != ien2);
16198  assert(ien1 < ien2);
16199  assert(ien1 <= ien2);
16200  assert(ien2 > ien1);
16201  assert(ien2 >= ien1);
16202 
16203  IntervalsEnumeratorCheck(bv, false);
16204  }
16205 
16206 
16207 
16208  {
16209  bvect bv { bm::id_max-1};
16211 
16212  valid = ien.valid();
16213  assert(valid);
16214  assert(ien.start() == bm::id_max-1);
16215  assert(ien.end() == bm::id_max-1);
16216 
16217  IntervalsEnumeratorCheck(bv, false);
16218  }
16219 
16220  {
16221  bvect bv { 0, 100, bm::id_max-1 };
16222  for (unsigned pass = 0; pass < 2; ++pass)
16223  {
16225 
16226  valid = ien.valid();
16227  assert(valid);
16228  assert(ien.start() == 0);
16229  assert(ien.end() == 0);
16230  assert((*ien).first == 0);
16231  assert(ien.get().second == 0);
16232 
16233  valid = ien.advance();
16234  assert(valid);
16235  assert(ien.start() == 100);
16236  assert(ien.end() == 100);
16237 
16238  ++ien;
16239  valid = ien.valid();
16240  assert(valid);
16241  assert(ien.start() == bm::id_max-1);
16242  assert(ien.end() == bm::id_max-1);
16243 
16244  ien++;
16245  valid = ien.valid();
16246  assert(!valid);
16247  bv.optimize();
16248 
16249  } // for pass
16250  }
16251 
16252  cout << "interval_enumerator +N stress test" << endl;
16253  {
16256 
16257  unsigned delta_max = 65536*2;
16258  double duration = 0;
16259  for (unsigned inc = 1; inc < delta_max;
16260  inc += (inc < 64) ? 1 : rand()&0xF)
16261  {
16262  clock_t start = clock();
16263  //cout << "\rinc = " << inc << " delta_max= " << delta_max << " (" << duration << ")" << flush;
16264  bvect bv;
16265  bvect bv_c;
16266  bvect::mem_pool_guard g1(pool, bv);
16267  bvect::mem_pool_guard g2(pool, bv_c);
16268 
16269  bvect::size_type test_max = 65535 * 256;
16270 
16271  for (bvect::size_type i = 0; i < test_max; i+=inc)
16272  bv.set(i);
16273 
16274  for (unsigned pass = 0; pass < 2; ++pass)
16275  {
16276  IntervalsEnumeratorCheck(bv, false);
16278  while (ien.valid())
16279  {
16280  auto from = ien.start();
16281  auto to = ien.end();
16282  bv_c.set_range(from, to);
16283  if (!ien.advance())
16284  break;
16285  }
16286  bool eq = bv.equal(bv_c);
16287  assert(eq);
16288 
16289  {
16290  bvect::size_type copy_to = test_max/100;
16291  for (bvect::size_type k = 1; k < copy_to; ++k)
16292  {
16293  bvect bv2; bvect bv2_c;
16294  bv2_c.copy_range(bv, k, copy_to);
16295 
16296  interval_copy_range(bv2, bv, k, copy_to);
16297  eq = bv2.equal(bv2_c);
16298  assert(eq);
16299  copy_to -= (unsigned) rand()%256;
16300  }
16301  }
16302 
16303  bv.optimize(tb);
16304  bv_c.clear();
16305  } // for pass
16306 
16307  clock_t finish = clock();
16308  clock_t elapsed_clocks = finish - start;
16309  duration = (double)(elapsed_clocks) / CLOCKS_PER_SEC;
16310 
16311  cout << "\rinc = " << inc << " delta_max= " << delta_max << " (" << duration << ")" << flush;
16312 
16313  if (inc > 65536)
16314  inc += (unsigned) rand() % 128; // fast pace randomiser
16315  } // for inc
16316 
16317  }
16318 
16319 
16320 
16321 
16322  cout << "\n----------------------------- IntervalEnumeratorTest() OK" << endl;
16323 }
16324 
16325 
16326 /*
16327 __int64 CalcBitCount64(__int64 b)
16328 {
16329  b = (b & 0x5555555555555555) + (b >> 1 & 0x5555555555555555);
16330  b = (b & 0x3333333333333333) + (b >> 2 & 0x3333333333333333);
16331  b = b + (b >> 4) & 0x0F0F0F0F0F0F0F0F;
16332  b = b + (b >> 8);
16333  b = b + (b >> 16);
16334  b = b + (b >> 32) & 0x0000007F;
16335  return b;
16336 }
16337 
16338 
16339 */
16340 
16341 static
16343 {
16344  cout << "----------------------------- FindNotNullPtrTest()" << endl;
16345  bm::word_t*** arr = 0;
16346  unsigned arr_size = 895;
16347  arr = (bm::word_t***)::malloc(sizeof(void*) * arr_size);
16348  if (!arr)
16349  return;
16350 
16351  for (unsigned i = 0; i < arr_size; ++i)
16352  {
16353  arr[i] = 0;
16354  }
16355  bool found;
16356  unsigned pos;
16357  found = bm::find_not_null_ptr(arr, 0u, arr_size, &pos);
16358  assert(!found);
16359 
16360  for (unsigned i = arr_size-1; i > 0; --i)
16361  {
16362  arr[i] = (bm::word_t**)~0;
16363  for (unsigned j = 0; j < i; ++j)
16364  {
16365  found = bm::find_not_null_ptr(arr, j, arr_size, &pos);
16366  assert(found);
16367  assert(pos == i);
16368  }
16369  } // for i
16370 
16371  ::free(arr);
16372  cout << "----------------------------- FindNotNullPtrTest() OK" << endl;
16373 }
16374 
16375 // function to return bvector by value to test move semantics
16376 //
16377 static
16379 {
16380  bvect bv1;
16381  bvect bv2;
16382 
16383  bv1[100] = true;
16384  bv1[1000] = true;
16385  bv2[100] = true;
16386  bv2[10001] = true;
16387 
16388  if (rand()%2)
16389  {
16390  return bv1;
16391  }
16392  return (bv1 & bv2);
16393 }
16394 
16395 
16396 
16397 static
16399 {
16400  cout << "----------------------------- SyntaxTest()" << endl;
16401 
16402  {
16403  bvect bv1;
16404  bv1.set_bit(100);
16405  bv1.set_bit(100 + 10 *65535 * 256);
16406  {
16407  bvect bv2(bv1);
16408  int res = bv2.compare(bv1);
16409  assert(res == 0);
16410  }
16411  }
16412  {
16413  bvect bv1;
16414 
16415  //bvect::allocator_type a = bv1.get_allocator();
16416 
16417  bvect bv2(bv1);
16418  bvect bv3;
16419  bv3.swap(bv1);
16420 
16421  bv1[100] = true;
16422  bool v = bv1[100];
16423  assert(v);
16424  v = false;
16425 
16426  bv1[100] = false;
16427 
16428  bv2 |= bv1;
16429  bv2 &= bv1;
16430  bv2 ^= bv1;
16431  bv2 -= bv1;
16432 
16433  bv3 = bv1 | bv2;
16434 
16435  if (bv1 < bv2)
16436  {
16437  }
16438 
16439  bv3 = bv1 & bv2;
16440  bv3 = bv1 ^ bv2;
16441 
16442  bvect::reference ref = bv1[10];
16443  bool bn = !ref;
16444  bool bn2 = ~ref;
16445  bv1[10] = bn2;
16446  bv1[10] = bn;
16447 
16448  bn = bn2 = false;
16449 
16450  ref.flip();
16451 
16452  bvect bvn = ~bv1;
16453 
16454  // this should trigger move
16455  bvect bv4 = bvect_test_return();
16456  bvect bv41 = bvect_test_return() | bv2;
16457  bvect bv5(bvect_test_return());
16458 
16459  cout << bv4.count() << " " << bv41.count() << " " << bv5.count() << endl;
16460  }
16461 
16462 
16463  {
16464  bvect bv0;
16465  bvect bv1, bv2, bv3;
16466  bv0 = bv1 | bv2 | bv3;
16467  assert(bv0.count() == 0);
16468  }
16469  {
16470  bvect bv0;
16471  bvect bv1, bv2, bv3;
16472  bv0 = bv1 & bv2 & bv3;
16473  assert(bv0.count() == 0);
16474  }
16475  {
16476  bvect bv0;
16477  bvect bv1, bv2, bv3;
16478  bv0 = bv1 ^ bv2 ^ bv3;
16479  assert(bv0.count() == 0);
16480  }
16481  {
16482  bvect bv0;
16483  bvect bv1, bv2, bv3;
16484  bv0 = bv1 - bv2 - bv3;
16485  assert(bv0.count() == 0);
16486  }
16487 
16488 
16489  cout << "----------------------------- SyntaxTest() OK\n" << endl;
16490 }
16491 
16492 static
16494 {
16495  cout << "----------------------------- FreezeTest()" << endl;
16496 
16497  bool eq;
16498 
16499  {
16500  bvect bv;
16501  bvect bv_ro(bv, bm::finalization::READONLY);
16502  assert(!bv.is_ro());
16503  assert(!bv_ro.is_ro());
16504  }
16505 
16506  // swap test
16507  {
16508  bvect bv {0};
16509  bvect bv_ro(bv, bm::finalization::READONLY);
16510 
16511  assert(!bv.is_ro());
16512  assert(bv_ro.is_ro());
16513 
16514  eq = bv.equal(bv_ro);
16515  assert(eq);
16516 
16517  bv.swap(bv_ro);
16518 
16519  assert(bv.is_ro());
16520  assert(!bv_ro.is_ro());
16521 
16522  eq = bv.equal(bv_ro);
16523  assert(eq);
16524 
16525  bv_ro.clear(true);
16526  assert(!bv_ro.is_ro());
16527  assert(bv_ro.count() == 0);
16528  }
16529 
16530  // merge
16531  {
16532  bvect bv1 {0}, bv2 {65536};
16533  bv2.freeze();
16534 
16535  bvect bv3;
16536  bv3.bit_or(bv2, bv1);
16537  print_bv(bv3);
16538 
16539  bv1.merge(bv2);
16540  print_bv(bv1);
16541 
16542  eq = bv3.equal(bv1);
16543  assert(eq);
16544  }
16545  // move
16546  {
16547  bvect bv1 {0}, bv2 {65536, bm::id_max-1};
16548  bv2.freeze();
16549 
16550  bv1.move_from(bv2);
16551  assert(bv1.is_ro());
16552  print_bv(bv1);
16553  bvect bvc {65536, bm::id_max-1};
16554  bvc.freeze();
16555  eq = bvc.equal(bv1);
16556  assert(eq);
16557  }
16558 
16559 
16560  // calc_stat
16561  {
16562  bvect bv1 {1, bm::id_max-1};
16563  bv1.optimize();
16564  bvect::statistics st1, st2;
16565  bv1.calc_stat(&st1);
16566  {
16567  bvect bv2(bv1);
16568  bv2.freeze();
16569 
16570  bv2.calc_stat(&st2);
16571  }
16572  assert(st2.memory_used < st1.memory_used);
16573 
16574  }
16575 
16576 
16577  {
16578  bvect bv;
16579  bv.invert();
16580 
16581  {
16582  bvect bv_ro(bv, bm::finalization::READONLY);
16583 
16584  assert(!bv.is_ro());
16585  assert(bv_ro.is_ro());
16586 
16587  eq = bv.equal(bv_ro);
16588  assert(eq);
16589  }
16590  bv.optimize();
16591  {
16592  bvect bv_ro(bv, bm::finalization::READONLY);
16593 
16594  assert(!bv.is_ro());
16595  assert(bv_ro.is_ro());
16596 
16597  eq = bv.equal(bv_ro);
16598  assert(eq);
16599  }
16600  }
16601 
16602  {
16603  bvect bv;
16604  bv.set_range(256*65536, bm::id_max/2 + 10);
16605 
16606  {
16607  bvect bv_ro(bv, bm::finalization::READONLY);
16608 
16609  assert(!bv.is_ro());
16610  assert(bv_ro.is_ro());
16611 
16612  eq = bv.equal(bv_ro);
16613  assert(eq); // copy-ctor
16614  {
16615  bvect bv_ro2(bv_ro, bm::finalization::READONLY);
16616  assert(bv_ro2.is_ro());
16617  eq = bv.equal(bv_ro2);
16618  assert(eq);
16619  }
16620 
16621  { // assignment
16622  bvect bv_ro2 { 126 };
16623  bv_ro2 = bv_ro;
16624  assert(bv_ro2.is_ro());
16625  eq = bv.equal(bv_ro2);
16626  assert(eq);
16627  }
16628 
16629  { // move ctor/assignment
16630  bvect bv_ro2 = std::move(bv_ro);
16631  assert(bv_ro2.is_ro());
16632  eq = bv.equal(bv_ro2);
16633  assert(eq);
16634  bvect bv_ro3;
16635  bv_ro3 = std::move(bv_ro2);
16636  assert(bv_ro3.is_ro());
16637  eq = bv.equal(bv_ro3);
16638  assert(eq);
16639 
16640  }
16641  }
16642  }
16643 
16644 
16645 
16646  {
16647  std::vector<bvect::size_type> v1 { 0, 65536, 65536 * 256, bm::id_max/2, bm::id_max-1 };
16648  for (size_t i = 0; i < v1.size(); ++i)
16649  {
16650  auto idx = v1[i];
16651  bvect bv; bv.set(idx);
16652  {
16653  for (int pass = 0; pass < 2; ++pass)
16654  {
16655  bvect bv_ro(bv, bm::finalization::READONLY);
16656  assert(!bv.is_ro());
16657  assert(bv_ro.is_ro());
16658  eq = bv.equal(bv_ro);
16659  assert(eq);
16661  bool found = bv_ro.find(pos);
16662  assert(found);
16663  assert(pos == idx);
16664 
16665  { // freezing copyctor
16666  bvect bv_ro2(bv_ro, bm::finalization::READONLY);
16667  assert(bv_ro2.is_ro());
16668  eq = bv.equal(bv_ro2);
16669  assert(eq);
16670  }
16671 
16672  { // copyctor
16673  bvect bv_ro2(bv_ro);
16674  assert(bv_ro2.is_ro());
16675  eq = bv.equal(bv_ro2);
16676  assert(eq);
16677  }
16678  { // assignment tests
16679  bvect bv_ro2 { 126 };
16680  bv_ro2 = bv_ro;
16681  assert(bv_ro2.is_ro());
16682  eq = bv.equal(bv_ro2);
16683  assert(eq);
16684  }
16685 
16686  { // move ctor/assignment
16687  bvect bv_ro2 = std::move(bv_ro);
16688  assert(bv_ro2.is_ro());
16689  eq = bv.equal(bv_ro2);
16690  assert(eq);
16691  bvect bv_ro3;
16692  bv_ro3 = std::move(bv_ro2);
16693  assert(bv_ro3.is_ro());
16694  eq = bv.equal(bv_ro3);
16695  assert(eq);
16696  }
16697 
16698  bv.optimize();
16699  } // for pass
16700  }
16701  } // for i
16702  }
16703 
16704  {
16705  bvect bv { 0 };
16706  {
16707  for (bvect::size_type i = 0; i < 65535; i+=3)
16708  bv.set(i);
16709  bv.set(bm::id_max/2);
16710  bv.optimize();
16711 
16712  bvect bv_ro(bv, bm::finalization::READONLY);
16713  assert(!bv.is_ro());
16714  assert(bv_ro.is_ro());
16715  eq = bv.equal(bv_ro);
16716  assert(eq);
16717 
16718  bvect bv_ro2(bv_ro);
16719  assert(bv_ro2.is_ro());
16720  eq = bv_ro.equal(bv_ro2);
16721  assert(eq);
16722  eq = bv.equal(bv_ro2);
16723  assert(eq);
16724  }
16725  }
16726 
16727  cout << "----------------------------- FreezeTest() ON\n" << endl;
16728 }
16729 
16730 
16731 static
16733 {
16734  cout << "----------------------------- BlockDigestTest()" << endl;
16735 
16736  {
16737  bvect bv_d;
16738  bvect bv;
16739  bv.fill_alloc_digest(bv_d);
16740  auto c = bv_d.count();
16741  assert(!c);
16742 
16743  bv.set(0);
16744  bv.set(1);
16745  bv.set(65536);
16746  bv.set(65536+256);
16747  bv.set(65536*256);
16748 
16749  bv.fill_alloc_digest(bv_d);
16750  c = bv_d.count();
16751  assert(c == 3);
16752  assert(bv_d.test(0));
16753  assert(bv_d.test(1));
16754  assert(bv_d.test(256));
16755 
16756  bv.set(bm::id_max32-1);
16757  bv.fill_alloc_digest(bv_d);
16758  c = bv_d.count();
16759  assert(c == 4);
16760  }
16761 
16762  cout << "----------------------------- BlockDigestTest() OK" << endl;
16763 }
16764 
16765 static
16767 {
16768  cout << "----------------------------- ArenaTest() " << endl;
16769 
16770  {
16772  bvect bv;
16773 
16775  assert(st.gap_blocks_sz == 0);
16776  assert(st.ptr_sub_blocks_sz == 0);
16777  assert(st.bit_blocks_sz == 0);
16778 
16779  bv.set(0);
16780 
16782  assert(st.gap_blocks_sz == 0);
16785 
16786  bv.set(bm::id_max/2);
16787 
16789  assert(st.gap_blocks_sz == 0);
16792  }
16793 
16794  {
16796  bvect bv(bm::BM_GAP);
16797 
16798  bv.set(1);
16799  bv.set(2);
16800 
16802  assert(st.gap_blocks_sz == 4);
16804  assert(st.bit_blocks_sz == 0);
16805 
16806  bv.set(1+bm::id_max/2);
16807 
16809  assert(st.gap_blocks_sz == 7);
16811  assert(st.bit_blocks_sz == 0);
16812  }
16813 
16814  {
16816  bvect bv;
16817 
16818  bv.set(0);
16819  bv.set(1);
16820  bv.optimize();
16821 
16822  bv.set(bm::id_max/2);
16823 
16825  assert(st.gap_blocks_sz == 3);
16828 
16829 
16831  bvect::blocks_manager_type::arena ar;
16832 
16833  bman.alloc_arena(&ar, st, bman.get_allocator());
16834  bman.free_arena(&ar, bman.get_allocator());
16835 
16836  }
16837 
16838 
16839  cout << "----------------------------- ArenaTest() OK" << endl;
16840 }
16841 
16842 static
16843 void SetTest()
16844 {
16845  {
16846  bvect bv{ 0, 10, 65536, 10000, bm::id_max-1 };
16847  unsigned cnt = bv.count();
16848  if (cnt != 5)
16849  {
16850  cout << "Brace initialization test failed!." << endl;
16851  exit(1);
16852  }
16853  bvect bv2;
16854  bv2.set(0).set(10).set(65536).set(10000).set(bm::id_max-1);
16855 
16856  if (bv != bv2)
16857  {
16858  cout << "Brace initialization comparison test failed!." << endl;
16859  exit(1);
16860  }
16861  }
16862  {
16863  unsigned cnt;
16864  bvect bv;
16865  bv.set();
16866 
16867  cnt = bv.count();
16868  if (cnt != bm::id_max)
16869  {
16870  cout << "Set test failed!." << endl;
16871  assert(0);
16872  exit(1);
16873  }
16874 
16875  bv.invert();
16876  cnt = bv.count();
16877  if (cnt != 0)
16878  {
16879  cout << "Set invert test failed!." << endl;
16880  exit(1);
16881  }
16882 
16883  bv.set(0);
16884  bv.set(bm::id_max - 1);
16885  cnt = bv.count();
16886 
16887  assert(cnt == 2);
16888 
16889  bv.invert();
16890  print_stat(cout,bv);
16891  cnt = bv.count();
16892 
16893  if (cnt != bm::id_max - 2)
16894  {
16895  cout << "Set invert test failed!." << endl;
16896  exit(1);
16897  }
16898 
16899  bv.clear();
16900  bv[1] &= true;
16901  bool v = bv[1];
16902  if (v)
16903  {
16904  cout << "Set &= test failed!" << endl;
16905  exit(1);
16906  }
16907 
16908 
16909  bv[1] = true;
16910  bv[1] &= true;
16911  v = bv[1];
16912  if (!v)
16913  {
16914  cout << "Set &= test failed (2)!" << endl;
16915  exit(1);
16916  }
16917  bv.clear(true);
16918  bv.invert();
16919  bv[1] &= true;
16920  v = bv[1];
16921  if (!v)
16922  {
16923  cout << "Set &= test failed (2)!" << endl;
16924  exit(1);
16925  }
16926  }
16927 
16928  {
16929  bvect bvc;
16930  bvect bv(bm::BM_GAP);
16931  bv.set(10);
16932  bvc.set(10);
16933  bv.set(11);
16934  bvc.set(11);
16935 
16936  for (unsigned i = 100; i < 110; i+= 2)
16937  {
16938  bool b1 = bv.set_bit_no_check(i, true);
16939  assert(b1);
16940  bool b2 = bvc.set_bit_no_check(i, true);
16941  assert(b2);
16942  }
16943  assert(bv.equal(bvc));
16944  }
16945 
16946  {
16947  bvect bv_full;
16948  bv_full.invert();
16949  assert(bv_full.test(bm::id_max/2));
16950  }
16951 
16952  {
16953  bvect bv1;
16954  bv1.set(0);
16955  bv1.set();
16956  auto cnt1 = bv1.count();
16957  assert (cnt1 == bm::id_max);
16958  }
16959 
16960  {
16961  bvect bv1, bv2(BM_GAP);
16962  bv1.set(0); bv2.set(0);
16963  bv1.set(bm::id_max-1);bv2.set(bm::id_max-1);
16964  bv1.set((bm::id_max-1)/2);bv2.set((bm::id_max-1)/2);
16965  for (unsigned i = 0; i < 2; ++i)
16966  {
16967  bv1.set();
16968  bv2.set();
16969  auto cnt1 = bv1.count();
16970  auto cnt2 = bv2.count();
16971  assert (cnt1 == bm::id_max);
16972  assert (cnt2 == bm::id_max);
16973  }
16974  }
16975 
16976 
16977  bvect bv2;
16978  bv2[1] = true;
16979  bv2[1] = false;
16980  bvect::statistics stat1;
16981  bv2.calc_stat(&stat1);
16982 
16983  bv2.optimize();
16984 
16985  bvect::statistics stat2;
16986  bv2.calc_stat(&stat2);
16987 
16988  if (stat2.bit_blocks != 0 ||
16989  stat2.gap_blocks != 0 ||
16990  stat1.memory_used <= stat2.memory_used)
16991  {
16992  cout << "Optimization memory free test failed (2)!" << endl;
16993  exit(1);
16994  }
16995 
16996 
16997  {
16998  bvect bv3;
16999  bool changed;
17000  changed = bv3.set_bit_conditional(10, true, true);
17001  bool v = bv3[10];
17002  if (v || changed) {
17003  cout << "Conditional bit set failed." << endl;
17004  exit(1);
17005  }
17006  changed = bv3.set_bit_conditional(10, true, false);
17007  v = bv3[10];
17008  if (!v || !changed) {
17009  cout << "Conditional bit set failed." << endl;
17010  exit(1);
17011  }
17012  changed = bv3.set_bit_conditional(10, false, false);
17013  v = bv3[10];
17014  if (!v || changed) {
17015  cout << "Conditional bit set failed." << endl;
17016  exit(1);
17017  }
17018  changed = bv3.set_bit_conditional(10, false, true);
17019  v = bv3[10];
17020  if (v || !changed) {
17021  cout << "Conditional bit set failed." << endl;
17022  exit(1);
17023  }
17024  }
17025 
17026  {
17027  bvect bv3(bm::BM_GAP);
17028  bool changed;
17029  changed = bv3.set_bit_conditional(10, true, true);
17030  bool v = bv3[10];
17031  if (v || changed) {
17032  cout << "Conditional bit set failed." << endl;
17033  exit(1);
17034  }
17035  changed = bv3.set_bit_conditional(10, true, false);
17036  v = bv3[10];
17037  if (!v || !changed) {
17038  cout << "Conditional bit set failed." << endl;
17039  exit(1);
17040  }
17041  changed = bv3.set_bit_conditional(10, false, false);
17042  v = bv3[10];
17043  if (!v || changed) {
17044  cout << "Conditional bit set failed." << endl;
17045  exit(1);
17046  }
17047  changed = bv3.set_bit_conditional(10, false, true);
17048  v = bv3[10];
17049  if (v || !changed) {
17050  cout << "Conditional bit set failed." << endl;
17051  exit(1);
17052  }
17053  }
17054 
17055 
17056  {
17057  bvect bv3(bm::BM_GAP);
17058  bv3.invert();
17059 
17060  bool changed = bv3.set_bit_conditional(10, false, true);
17061  bool v = bv3[10];
17062  if (v || !changed) {
17063  cout << "Conditional bit set failed." << endl;
17064  exit(1);
17065  }
17066  }
17067 
17068  {
17069  bvect bv3(bm::BM_GAP);
17070  bv3.invert();
17071  bv3.optimize();
17072  bool changed;
17073  changed = bv3.set_bit_conditional(10, true, true);
17074  bool v = bv3[10];
17075  if (!v || changed) {
17076  cout << "Conditional bit set failed." << endl;
17077  exit(1);
17078  }
17079  changed = bv3.set_bit_conditional(10, true, false);
17080  v = bv3[10];
17081  if (!v || changed) {
17082  cout << "Conditional bit set failed." << endl;
17083  exit(1);
17084  }
17085  changed = bv3.set_bit_conditional(10, false, false);
17086  v = bv3[10];
17087  if (!v || changed) {
17088  cout << "Conditional bit set failed." << endl;
17089  exit(1);
17090  }
17091  changed = bv3.set_bit_conditional(10, false, true);
17092  v = bv3[10];
17093  if (v || !changed) {
17094  cout << "Conditional bit set failed." << endl;
17095  exit(1);
17096  }
17097  changed = bv3.set_bit_conditional(10, true, false);
17098  v = bv3[10];
17099  if (!v || !changed) {
17100  cout << "Conditional bit set failed." << endl;
17101  exit(1);
17102  }
17103  }
17104 
17105 
17106  {
17107  bvect bv(0);
17108  bv.resize(100);
17109  bv[10] = true;
17110  bv.resize(1000001);
17111  bv[100000] = 1;
17112 
17113  if (bv.size() != 1000001)
17114  {
17115  cout << "Resize failed" << endl;
17116  exit(1);
17117  }
17118  if (bv.count() != 2)
17119  {
17120  cout << "Resize count failed" << endl;
17121  exit(1);
17122  }
17123 
17124  bv.resize(100);
17125  if (bv.size() != 100)
17126  {
17127  cout << "Resize failed" << endl;
17128  exit(1);
17129  }
17130  if (bv.count() != 1)
17131  {
17132  cout << "Resize count failed" << endl;
17133  exit(1);
17134  }
17135 
17136  bv.resize(60000100);
17137  bv.invert();
17138  bv.clear(true);
17139 
17140 
17141  if (bv.size() != 60000100)
17142  {
17143  cout << "Resize failed" << endl;
17144  exit(1);
17145  }
17146  if (bv.count() != 0)
17147  {
17148  cout << "Resize count failed" << endl;
17149  exit(1);
17150  }
17151  }
17152 
17153  {
17154  bvect bv(100);
17155  assert(bv.size()==100);
17156  bv[10000000] = true;
17157  assert(bv.size() == 10000001);
17158  bv.set_bit(10000001);
17159  assert(bv.size() == 10000002);
17160  }
17161 
17162 }
17163 
17164 template<class BV>
17165 void swap_bits(BV& bv, typename BV::size_type i1, typename BV::size_type i2)
17166 {
17167  auto b1 = bv.test(i1);
17168  auto b2 = bv.test(i2);
17169  bv.set(i1, b2);
17170  bv.set(i2, b1);
17171 }
17172 
17173 template<class BV>
17174 void swap_bits_check(BV& bv1, BV& bv2,
17175  typename BV::size_type i1, typename BV::size_type i2)
17176 {
17177  auto b1 = bv1.test(i1);
17178  auto b2 = bv1.test(i2);
17179 
17180  bv1.swap(i1, i2);
17181 
17182  auto b12 = bv1.test(i1);
17183  auto b22 = bv1.test(i2);
17184  assert(b2 == b12);
17185  assert(b1 == b22);
17186 
17187  swap_bits(bv2, i1, i2);
17188 
17189  bool eq = bv1.equal(bv2);
17190  assert(eq);
17191 }
17192 
17193 
17194 static
17195 void SwapTest()
17196 {
17197  cout << "----------------------- SwapTest()" << endl;
17198 
17199  {
17200  bvect bv1, bv2;
17201  swap_bits_check(bv1, bv2, 0, 0);
17202  bv1.set(10);
17203  bv2.set(10);
17204  swap_bits_check(bv1, bv2, 0, 11);
17205  swap_bits_check(bv1, bv2, 10, 65535);
17206  bv1.optimize();
17207  swap_bits_check(bv1, bv2, 10, 65535);
17208  assert(bv1.count()==1);
17209  assert(bv1.test(10)==1);
17210  }
17211 
17212  {
17213  bvect bv1, bv2;
17214  bv1.invert();
17215  bv2.invert();
17216  swap_bits_check(bv1, bv2, 0, 11);
17217  swap_bits_check(bv1, bv2, 10, 65535);
17218  swap_bits_check(bv1, bv2, 0, bm::id_max-1);
17219  swap_bits_check(bv1, bv2, 10, 65535*2);
17220  }
17221 
17222  // gap one block tests
17223  {
17224  bvect bv1, bv2;
17225  bv1.set_range(200, 250);
17226  bv2.set_range(200, 250);
17227  bv1.optimize();
17228  swap_bits_check(bv1, bv2, 11, 1);
17229  swap_bits_check(bv1, bv2, 199, 198);
17230  swap_bits_check(bv1, bv2, 200, 199);
17231  swap_bits_check(bv1, bv2, 200, 199);
17232  swap_bits_check(bv1, bv2, 251, 65535);
17233  swap_bits_check(bv1, bv2, 250, 251);
17234  swap_bits_check(bv1, bv2, 220, 65536);
17235  swap_bits_check(bv1, bv2, 221, 65536*2);
17236  }
17237 
17238  cout << "Stress test 1" << endl;
17239  bvect::size_type max = 10000000;
17240  {
17241  bvect bv1, bv2;
17242  generate_bvector(bv1, max, false);
17243  bv2 = bv1;
17244  bvect::size_type j = max;
17245  bvect::size_type cnt = 0;
17246  for (bvect::size_type i = 0; i <= j; i+=unsigned(rand()%16), j--)
17247  {
17248  swap_bits_check(bv1, bv2, i, j);
17249 
17250  if (!is_silent)
17251  if ((++cnt & 0xFFFF) == 0)
17252  cout << "\r" << i << "/" << j << flush;
17253  } // for
17254  }
17255 
17256  cout << "\nStress test 2" << endl;
17257  {
17258  bvect bv1, bv2;
17259  generate_bvector(bv1, max, true);
17260  bv2 = bv1;
17261  bvect::size_type j = max;
17262  bvect::size_type cnt = 0;
17263  for (bvect::size_type i = 0; i <= j; i+=unsigned(rand()%22), j--)
17264  {
17265  swap_bits_check(bv1, bv2, i, j);
17266  if (!is_silent)
17267  if ((++cnt & 0xFFFF) == 0)
17268  cout << "\r" << i << "/" << j << flush;
17269 
17270  } // for
17271  }
17272 
17273 
17274 
17275  cout << "\n----------------------- SwapTest() OK" << endl;
17276 }
17277 
17278 template<class A, class B> void CompareMiniSet(const A& ms,
17279  const B& bvm)
17280 {
17281  for (unsigned i = 0; i < bm::set_total_blocks; ++i)
17282  {
17283  bool ms_val = ms.test(i)!=0;
17284  bool bvm_val = bvm.is_bit_true(i)!=0;
17285  if (ms_val != bvm_val)
17286  {
17287  printf("MiniSet comparison error: %u\n",i);
17288  exit(1);
17289  }
17290  }
17291 }
17292 
17293 static
17295 {
17296  cout << "----------------------- MiniSetTest" << endl;
17297  {
17300 
17301 
17302  CompareMiniSet(ms, bvm);
17303 
17304 
17305  ms.set(1);
17306  bvm.set_bit(1);
17307 
17308  CompareMiniSet(ms, bvm);
17309 
17310  unsigned i;
17311 
17312  for (i = 1; i < 10; i++)
17313  {
17314  ms.set(i);
17315  bvm.set_bit(i);
17316  }
17317  CompareMiniSet(ms, bvm);
17318 
17319  for (i = 1; i < 10; i++)
17320  {
17321  ms.set(i, false);
17322  bvm.clear_bit(i);
17323  }
17324  CompareMiniSet(ms, bvm);
17325 
17326 
17327  for (i = 1; i < 10; i+=3)
17328  {
17329  ms.set(i);
17330  bvm.set_bit(i);
17331  }
17332  CompareMiniSet(ms, bvm);
17333 
17334  for (i = 1; i < 5; i+=3)
17335  {
17336  ms.set(i, false);
17337  bvm.clear_bit(i);
17338  }
17339  CompareMiniSet(ms, bvm);
17340  }
17341 
17342 
17343  {
17346 
17347 
17348  ms.set(1);
17349  bvm.set_bit(1);
17350 
17351  CompareMiniSet(ms, bvm);
17352 
17353  unsigned i;
17354  for (i = 1; i < bm::set_total_blocks; i+=3)
17355  {
17356  ms.set(i);
17357  bvm.set_bit(i);
17358  }
17359  CompareMiniSet(ms, bvm);
17360 
17361  for (i = 1; i < bm::set_total_blocks/2; i+=3)
17362  {
17363  ms.set(i, false);
17364  bvm.clear_bit(i);
17365  }
17366  CompareMiniSet(ms, bvm);
17367  }
17368 
17369 
17370  {
17373 
17374 
17375  CompareMiniSet(ms, bvm);
17376 
17377 
17378  ms.set(1);
17379  bvm.set_bit(1);
17380 
17381  CompareMiniSet(ms, bvm);
17382 
17383  unsigned i;
17384 
17385  for (i = 1; i < 10; i++)
17386  {
17387  ms.set(i);
17388  bvm.set_bit(i);
17389  }
17390  CompareMiniSet(ms, bvm);
17391 
17392  for (i = 1; i < 10; i++)
17393  {
17394  ms.set(i, false);
17395  bvm.clear_bit(i);
17396  }
17397  CompareMiniSet(ms, bvm);
17398 
17399 
17400  for (i = 1; i < bm::set_total_blocks; i+=3)
17401  {
17402  ms.set(i);
17403  bvm.set_bit(i);
17404  }
17405  CompareMiniSet(ms, bvm);
17406 
17407  for (i = 1; i < bm::set_total_blocks/2; i+=3)
17408  {
17409  ms.set(i, false);
17410  bvm.clear_bit(i);
17411  }
17412  CompareMiniSet(ms, bvm);
17413  }
17414 
17415 
17416  {
17419 
17420 
17421  ms.set(1);
17422  bvm.set_bit(1);
17423 
17424  CompareMiniSet(ms, bvm);
17425 
17426  unsigned i;
17427  for (i = 1; i < 15; i+=3)
17428  {
17429  ms.set(i);
17430  bvm.set_bit(i);
17431  }
17432  CompareMiniSet(ms, bvm);
17433 
17434  for (i = 1; i < 7; i+=3)
17435  {
17436  ms.set(i, false);
17437  bvm.clear_bit(i);
17438  }
17439  CompareMiniSet(ms, bvm);
17440  }
17441 
17442 
17443  cout << "----------------------- MiniSetTest ok" << endl;
17444 }
17445 
17446 inline
17447 unsigned CalcBitCount32(unsigned b)
17448 {
17449  b = (b & 0x55555555) + (b >> 1 & 0x55555555);
17450  b = (b & 0x33333333) + (b >> 2 & 0x33333333);
17451  b = b + ((b >> 4) & 0x0F0F0F0F);
17452  b = b + (b >> 8);
17453  b = b + ((b >> 16) & 0x0000003F);
17454  return b;
17455 }
17456 
17457 static
17458 void PrintGapLevels(const gap_word_t* glevel)
17459 {
17460  cout << "Gap levels:" << endl;
17461  unsigned i;
17462  for (i = 0; i < bm::gap_levels; ++i)
17463  {
17464  cout << glevel[i] << ",";
17465  }
17466  cout << endl;
17467 }
17468 
17469 static
17471 {
17472  gap_word_t glevel[bm::gap_levels];
17473  ::memcpy(glevel, gap_len_table<true>::_len, bm::gap_levels * sizeof(gap_word_t));
17474 
17475  {
17476  gap_word_t length[] = { 2, 2, 5, 5, 10, 11, 12 };
17477  unsigned lsize = sizeof(length) / sizeof(gap_word_t);
17478 
17479  bm::improve_gap_levels(length, length + lsize, glevel);
17480 
17481  PrintGapLevels(glevel);
17482  }
17483 
17484  {
17485  gap_word_t length[] = { 3, 5, 15, 15, 100, 110, 120 };
17486  unsigned lsize = sizeof(length) / sizeof(gap_word_t);
17487 
17488  bm::improve_gap_levels(length, length + lsize, glevel);
17489  PrintGapLevels(glevel);
17490  }
17491 
17492  {
17493  gap_word_t length[] = { 15, 80, 5, 3, 100, 110, 95 };
17494  unsigned lsize = sizeof(length) / sizeof(gap_word_t);
17495 
17496  bm::improve_gap_levels(length, length + lsize, glevel);
17497  PrintGapLevels(glevel);
17498  }
17499 
17500  {
17501  gap_word_t length[] =
17502  { 16,30,14,24,14,30,18,14,12,16,8,38,28,4,20,18,28,22,32,14,12,16,10,8,14,18,14,8,
17503  16,30,8,8,58,28,18,4,26,14,52,12,18,10,14,18,22,18,20,70,12,6,26,6,8,22,12,4,8,8,
17504  8,54,18,6,8,4,4,10,4,4,4,4,4,6,22,14,38,40,56,50,6,10,8,18,82,16,6,18,20,12,12,
17505  16,8,14,14,10,16,12,10,16,14,12,18,14,18,34,14,12,18,18,10,20,10,18,8,14,14,22,16,
17506  10,10,18,8,20,14,10,14,12,12,14,16,16,6,10,14,6,10,10,10,10,12,4,8,8,8,10,10,8,
17507  8,12,10,10,14,14,14,8,4,4,10,10,4,10,4,8,6,52,104,584,218
17508  };
17509  unsigned lsize = sizeof(length) / sizeof(gap_word_t);
17510 
17511  bm::improve_gap_levels(length, length + lsize, glevel);
17512  PrintGapLevels(glevel);
17513  }
17514 
17515  {
17516  gap_word_t length[] = {
17517  30,46,26,4,4,68,72,6,10,4,6,14,6,42,198,22,12,4,6,24,12,8,18,4,6,10,6,4,6,6,12,6
17518  ,6,4,4,78,38,8,52,4,8,10,6,8,8,6,10,4,6,6,4,10,6,8,16,22,28,14,10,10,16,10,20,10
17519  ,14,12,8,18,4,8,10,6,10,4,6,12,16,12,6,4,8,4,14,14,6,8,4,10,10,8,8,6,8,6,8,4,8,4
17520  ,8,10,6,4,6
17521  };
17522  unsigned lsize = sizeof(length) / sizeof(gap_word_t);
17523 
17524  bm::improve_gap_levels(length, length + lsize, glevel);
17525  PrintGapLevels(glevel);
17526 
17527  }
17528 
17529 }
17530 
17531 
17532 static
17534 {
17535  cout << "---------------------------- BitCountChangeTest " << endl;
17536 
17537  unsigned i;
17538  for (i = 0xFFFFFFFF; i; i <<= 1)
17539  {
17540  unsigned a0 = bm::bit_count_change(i);
17541  unsigned a1 = BitCountChange(i);
17542 
17543  if (a0 != a1)
17544  {
17545  cout << hex
17546  << "Bit count change test failed!"
17547  << " i = " << i << " return = "
17548  << a0 << " check = " << a1
17549  << endl;
17550  exit(1);
17551  }
17552  }
17553 
17554  cout << "---------------------------- STEP 2 " << endl;
17555 
17556  for (i = 0xFFFFFFFF; i; i >>= 1)
17557  {
17558  unsigned a0 = bm::bit_count_change(i);
17559  unsigned a1 = BitCountChange(i);
17560 
17561  if (a0 != a1)
17562  {
17563  cout << "Bit count change test failed!"
17564  << " i = " << i << " return = "
17565  << a0 << " check = " << a1
17566  << endl;
17567  exit(1);
17568  }
17569  }
17570 
17571  cout << "---------------------------- STEP 3 " << endl;
17572 
17573  for (i = 0; i < 0xFFFFFFF; ++i)
17574  {
17575  unsigned a0 = bm::bit_count_change(i);
17576  unsigned a1 = BitCountChange(i);
17577 
17578  if (a0 != a1)
17579  {
17580  cout << "Bit count change test failed!"
17581  << " i = " << i << " return = "
17582  << a0 << " check = " << a1
17583  << endl;
17584  exit(1);
17585  }
17586  }
17587  cout << "!" << endl;
17588 
17589  bm::word_t BM_VECT_ALIGN arr0[32] BM_VECT_ALIGN_ATTR = { 0, };
17590  arr0[0] = (bm::word_t)(1 << 31);
17591  arr0[1] = 1; //(bm::word_t)(1 << 31);
17592 
17593  bm::id_t cnt;
17594 
17595  cnt = bm::bit_count_change(arr0[1]);
17596  cout << cnt << endl;
17597  if (cnt != 2)
17598  {
17599  cout << "0.count_change() failed " << cnt << endl;
17600  exit(1);
17601  }
17602 
17603  // check solid block
17604  {
17605  BM_DECLARE_TEMP_BLOCK(tb1);
17606  for (i = 0; i < bm::set_block_size; ++i)
17607  {
17608  tb1.b_.w32[i] = 0;
17609  }
17610  unsigned gap_count = bm::bit_block_calc_change(tb1);
17611  assert(gap_count == 1);
17612  for (i = 0; i < bm::set_block_size; ++i)
17613  {
17614  tb1.b_.w32[i] = ~0u;
17615  }
17616  gap_count = bm::bit_block_calc_change(tb1);
17617  assert(gap_count == 1);
17618  }
17619 
17620  cout << "---------------------------- STEP 4 " << endl;
17621 
17622  bvect bv1;
17623  cnt = bm::count_intervals(bv1);
17624 
17625  if (cnt != 1)
17626  {
17627  cout << "1.count_intervals() failed " << cnt << endl;
17628  exit(1);
17629  }
17630  CheckIntervals(bv1, 65536);
17631 
17632  bv1.invert();
17633 
17634  cnt = count_intervals(bv1);
17635  cout << "Inverted cnt=" << cnt << endl;
17636 
17637  if (cnt != 1)
17638  {
17639  cout << "2.inverted count_intervals() failed " << cnt << endl;
17640  assert(0); exit(1);
17641  }
17642 
17643  bv1.invert();
17644 
17645  for (i = 10; i < 100000; ++i)
17646  {
17647  bv1.set(i);
17648  }
17649 
17650  cnt = count_intervals(bv1);
17651 
17652  if (cnt != 3)
17653  {
17654  cout << "3.count_intervals() failed " << cnt << endl;
17655  exit(1);
17656  }
17657  cout << "-----" << endl;
17658  CheckIntervals(bv1, 65536 * 2);
17659  cout << "Optmization..." << endl;
17660  bv1.optimize();
17661  cnt = count_intervals(bv1);
17662 
17663  if (cnt != 3)
17664  {
17665  cout << "4.count_intervals() failed " << cnt << endl;
17666  exit(1);
17667  }
17668 
17669  CheckIntervals(bv1, 65536 * 2);
17670 
17671  cout << "---------------------------- array GAP test " << endl;
17672 
17673  {
17674  bm::gap_word_t arr[] = { 0 };
17675 
17676  unsigned gap_count;
17677 
17678  gap_count = bit_array_compute_gaps(arr, sizeof(arr)/sizeof(arr[0]));
17679  if (gap_count != 1)
17680  {
17681  cout << "Array gap test failed. 1. " << endl;
17682  exit(1);
17683  }
17684 
17685  bm::gap_word_t gap[20] = {0};
17686  bm::gap_word_t gap_cntrl[20] = {0};
17687 
17688  gap_set_all(gap_cntrl, bm::gap_max_bits, 0);
17689  for (i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
17690  {
17691  unsigned is_set;
17692  gap_set_value(1, gap_cntrl, arr[i], &is_set);
17693  }
17694  unsigned gap_l_cntrl = gap_length(gap_cntrl);
17695  unsigned gap_len = gap_set_array(&gap[0], arr, sizeof(arr)/sizeof(arr[0]));
17696  unsigned gap_len1 = gap_length(gap);
17697 
17698  if (gap_len != gap_l_cntrl || gap_len1 != gap_l_cntrl)
17699  {
17700  cout << "Array gap test failed. 1. " << endl;
17701  exit(1);
17702  }
17703  int cmpres = gapcmp(gap, gap_cntrl);
17704  if (cmpres != 0)
17705  {
17706  cout << "Array gap cmp test failed. 1. " << endl;
17707  exit(1);
17708  }
17709  }
17710 
17711  {
17712  bm::gap_word_t arr[] = { 65535 };
17713 
17714  unsigned gap_count;
17715 
17716  gap_count = bit_array_compute_gaps(arr, sizeof(arr)/sizeof(arr[0]));
17717  if (gap_count != 2)
17718  {
17719  cout << "Array gap test failed. 1.1 " << endl;
17720  exit(1);
17721  }
17722 
17723  bm::gap_word_t gap[20] = {0};
17724  bm::gap_word_t gap_cntrl[20] = {0};
17725 
17726  gap_set_all(gap_cntrl, bm::gap_max_bits, 0);
17727  for (i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
17728  {
17729  unsigned is_set;
17730  gap_set_value(1, gap_cntrl, arr[i], &is_set);
17731  }
17732  unsigned gap_l_cntrl = gap_length(gap_cntrl);
17733 
17734  unsigned gap_len = gap_set_array(&gap[0], arr, sizeof(arr)/sizeof(arr[0]));
17735  unsigned gap_len1 = gap_length(gap);
17736 
17737  if (gap_len != gap_l_cntrl || gap_len1 != gap_l_cntrl)
17738  {
17739  cout << "Array gap test failed. 1.1 " << endl;
17740  exit(1);
17741  }
17742  int cmpres = gapcmp(gap, gap_cntrl);
17743  if (cmpres != 0)
17744  {
17745  cout << "Array gap cmp test failed. 1. " << endl;
17746  exit(1);
17747  }
17748  }
17749 
17750  {
17751  bm::gap_word_t arr[] = { 0, 65535 };
17752 
17753  unsigned gap_count;
17754 
17755  gap_count = bit_array_compute_gaps(arr, sizeof(arr)/sizeof(arr[0]));
17756  if (gap_count != 3)
17757  {
17758  cout << "Array gap test failed. 1.2 " << endl;
17759  exit(1);
17760  }
17761 
17762  bm::gap_word_t gap[20] = {0};
17763  bm::gap_word_t gap_cntrl[20] = {0};
17764 
17765  gap_set_all(gap_cntrl, bm::gap_max_bits, 0);
17766  for (i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
17767  {
17768  unsigned is_set;
17769  gap_set_value(1, gap_cntrl, arr[i], &is_set);
17770  }
17771  unsigned gap_l_cntrl = gap_length(gap_cntrl);
17772 
17773  unsigned gap_len = gap_set_array(&gap[0], arr, sizeof(arr)/sizeof(arr[0]));
17774  unsigned gap_len1 = gap_length(gap);
17775 
17776  if (gap_len != gap_l_cntrl || gap_len1 != gap_l_cntrl)
17777  {
17778  cout << "Array gap test failed. 1.2 " << endl;
17779  exit(1);
17780  }
17781  int cmpres = gapcmp(gap, gap_cntrl);
17782  if (cmpres != 0)
17783  {
17784  cout << "Array gap cmp test failed. 1.2 " << endl;
17785  exit(1);
17786  }
17787  }
17788 
17789  {
17790  bm::gap_word_t arr[] = { 0, 1, 2, 65534, 65535 };
17791 
17792  unsigned gap_count;
17793 
17794  gap_count = bit_array_compute_gaps(arr, sizeof(arr)/sizeof(arr[0]));
17795  if (gap_count != 3)
17796  {
17797  cout << "Array gap test failed. 1.3 " << endl;
17798  exit(1);
17799  }
17800 
17801  bm::gap_word_t gap[20] = {0};
17802  bm::gap_word_t gap_cntrl[20] = {0};
17803 
17804  gap_set_all(gap_cntrl, bm::gap_max_bits, 0);
17805  for (i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
17806  {
17807  unsigned is_set;
17808  gap_set_value(1, gap_cntrl, arr[i], &is_set);
17809  }
17810  unsigned gap_l_cntrl = gap_length(gap_cntrl);
17811 
17812  unsigned gap_len = gap_set_array(&gap[0], arr, sizeof(arr)/sizeof(arr[0]));
17813  unsigned gap_len1 = gap_length(gap);
17814 
17815  if (gap_len != gap_l_cntrl || gap_len1 != gap_l_cntrl)
17816  {
17817  cout << "Array gap test failed. 1.3 " << endl;
17818  exit(1);
17819  }
17820  int cmpres = gapcmp(gap, gap_cntrl);
17821  if (cmpres != 0)
17822  {
17823  cout << "Array gap cmp test failed. 1.3 " << endl;
17824  exit(1);
17825  }
17826  }
17827 
17828  {
17829  bm::gap_word_t arr[] = { 0, 1, 2 };
17830  unsigned gap_count;
17831 
17832  gap_count = bit_array_compute_gaps(arr, sizeof(arr)/sizeof(arr[0]));
17833  if (gap_count != 1)
17834  {
17835  cout << "Array gap test failed. 2. " << endl;
17836  exit(1);
17837  }
17838  bm::gap_word_t gap[20] = {0};
17839  bm::gap_word_t gap_cntrl[20] = {0};
17840 
17841  gap_set_all(gap_cntrl, bm::gap_max_bits, 0);
17842  for (i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
17843  {
17844  unsigned is_set;
17845  gap_set_value(1, gap_cntrl, arr[i], &is_set);
17846  }
17847  unsigned gap_l_cntrl = gap_length(gap_cntrl);
17848 
17849  unsigned gap_len = gap_set_array(&gap[0], arr, sizeof(arr)/sizeof(arr[0]));
17850  unsigned gap_len1 = gap_length(gap);
17851 
17852  if (gap_len != gap_l_cntrl || gap_len1 != gap_l_cntrl)
17853  {
17854  cout << "Array gap test failed. 2 " << endl;
17855  exit(1);
17856  }
17857  int cmpres = gapcmp(gap, gap_cntrl);
17858  if (cmpres != 0)
17859  {
17860  cout << "Array gap cmp test failed. 2 " << endl;
17861  exit(1);
17862  }
17863 
17864  }
17865 
17866  {
17867  bm::gap_word_t arr[] = { 1, 2 };
17868  unsigned gap_count;
17869 
17870  gap_count = bit_array_compute_gaps(arr, sizeof(arr)/sizeof(arr[0]));
17871  if (gap_count != 2)
17872  {
17873  cout << "Array gap test failed. 3. " << endl;
17874  exit(1);
17875  }
17876  bm::gap_word_t gap[20] = {0};
17877  bm::gap_word_t gap_cntrl[20] = {0};
17878 
17879  gap_set_all(gap_cntrl, bm::gap_max_bits, 0);
17880  for (i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
17881  {
17882  unsigned is_set;
17883  gap_set_value(1, gap_cntrl, arr[i], &is_set);
17884  }
17885  unsigned gap_l_cntrl = gap_length(gap_cntrl);
17886 
17887  unsigned gap_len = gap_set_array(&gap[0], arr, sizeof(arr)/sizeof(arr[0]));
17888  unsigned gap_len1 = gap_length(gap);
17889 
17890  if (gap_len != gap_l_cntrl || gap_len1 != gap_l_cntrl)
17891  {
17892  cout << "Array gap test failed. 3 " << endl;
17893  exit(1);
17894  }
17895  int cmpres = gapcmp(gap, gap_cntrl);
17896  if (cmpres != 0)
17897  {
17898  cout << "Array gap cmp test failed. 3 " << endl;
17899  exit(1);
17900  }
17901  }
17902 
17903  {
17904  bm::gap_word_t arr[] = { 1, 2, 10 };
17905  unsigned gap_count;
17906 
17907  gap_count = bit_array_compute_gaps(arr, sizeof(arr)/sizeof(arr[0]));
17908  if (gap_count != 4)
17909  {
17910  cout << "Array gap test failed. 4. " << endl;
17911  exit(1);
17912  }
17913  bm::gap_word_t gap[20] = {0};
17914  bm::gap_word_t gap_cntrl[20] = {0};
17915 
17916  gap_set_all(gap_cntrl, bm::gap_max_bits, 0);
17917  for ( i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
17918  {
17919  unsigned is_set;
17920  gap_set_value(1, gap_cntrl, arr[i], &is_set);
17921  }
17922  unsigned gap_l_cntrl = gap_length(gap_cntrl);
17923 
17924  unsigned gap_len = gap_set_array(&gap[0], arr, sizeof(arr)/sizeof(arr[0]));
17925  unsigned gap_len1 = gap_length(gap);
17926 
17927  if (gap_len != gap_l_cntrl || gap_len1 != gap_l_cntrl)
17928  {
17929  cout << "Array gap test failed. 4 " << endl;
17930  exit(1);
17931  }
17932  int cmpres = gapcmp(gap, gap_cntrl);
17933  if (cmpres != 0)
17934  {
17935  cout << "Array gap cmp test failed. 4 " << endl;
17936  exit(1);
17937  }
17938  }
17939 
17940  {
17941  bm::gap_word_t arr[] = { 1, 2, 10, 11 };
17942  unsigned gap_count;
17943 
17944  gap_count = bit_array_compute_gaps(arr, sizeof(arr)/sizeof(arr[0]));
17945  if (gap_count != 4)
17946  {
17947  cout << "Array gap test failed. 5. " << endl;
17948  exit(1);
17949  }
17950  bm::gap_word_t gap[20] = {0};
17951  bm::gap_word_t gap_cntrl[20] = {0};
17952 
17953  gap_set_all(gap_cntrl, bm::gap_max_bits, 0);
17954  for ( i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
17955  {
17956  unsigned is_set;
17957  gap_set_value(1, gap_cntrl, arr[i], &is_set);
17958  }
17959  unsigned gap_l_cntrl = gap_length(gap_cntrl);
17960 
17961  unsigned gap_len = gap_set_array(&gap[0], arr, sizeof(arr)/sizeof(arr[0]));
17962  unsigned gap_len1 = gap_length(gap);
17963 
17964  if (gap_len != gap_l_cntrl || gap_len1 != gap_l_cntrl)
17965  {
17966  cout << "Array gap test failed. 5 " << endl;
17967  exit(1);
17968  }
17969  int cmpres = gapcmp(gap, gap_cntrl);
17970  if (cmpres != 0)
17971  {
17972  cout << "Array gap cmp test failed. 5 " << endl;
17973  exit(1);
17974  }
17975 
17976  }
17977 
17978  {
17979  bm::gap_word_t arr[] = { 1, 2, 10, 11, 256 };
17980  unsigned gap_count;
17981 
17982  gap_count = bit_array_compute_gaps(arr, sizeof(arr)/sizeof(arr[0]));
17983  if (gap_count != 6)
17984  {
17985  cout << "Array gap test failed. 6. " << endl;
17986  exit(1);
17987  }
17988  bm::gap_word_t gap[20] = {0};
17989  bm::gap_word_t gap_cntrl[20] = {0};
17990 
17991  gap_set_all(gap_cntrl, bm::gap_max_bits, 0);
17992  for ( i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
17993  {
17994  unsigned is_set;
17995  gap_set_value(1, gap_cntrl, arr[i], &is_set);
17996  }
17997  unsigned gap_l_cntrl = gap_length(gap_cntrl);
17998 
17999  unsigned gap_len = gap_set_array(&gap[0], arr, sizeof(arr)/sizeof(arr[0]));
18000  unsigned gap_len1 = gap_length(gap);
18001 
18002  if (gap_len != gap_l_cntrl || gap_len1 != gap_l_cntrl)
18003  {
18004  cout << "Array gap test failed. 6 " << endl;
18005  exit(1);
18006  }
18007  int cmpres = gapcmp(gap, gap_cntrl);
18008  if (cmpres != 0)
18009  {
18010  cout << "Array gap cmp test failed. 6 " << endl;
18011  exit(1);
18012  }
18013 
18014  }
18015 
18016 
18017  cout << "---------------------------- BitCountChangeTest Ok." << endl;
18018 }
18019 
18020 static
18022 {
18023  cout << "---------------------------- BitRangeAllSetTest()" << endl;
18024 
18025  BM_DECLARE_TEMP_BLOCK(tb1);
18026  BM_DECLARE_TEMP_BLOCK(tb0);
18027  bm::bit_block_set(tb1, ~0u);
18028  bm::bit_block_set(tb0, ~0xBEEFu);
18029  bool b;
18030 
18031  {
18032  b = bm::bit_block_is_all_one_range(tb1, 0, 65535);
18033  assert(b);
18034  tb1[2047] &= ~(1<<31);
18035 
18036  bool all_one = bm::check_block_one(tb1, true);
18037  assert(!all_one);
18038 
18039  auto cnt = bit_block_calc_count_range(tb1, 0, 65535);
18040  assert(cnt == 65535);
18041 
18042  b = bm::bit_block_is_all_one_range(tb1, 0, 65535);
18043  assert(!b);
18044  }
18045 
18046 
18047  {
18048  bm::bit_block_set(tb1, ~0u);
18049 
18050  unsigned i(0), j(65535);
18051  for (; i < j; ++i, --j)
18052  {
18053  b = bm::bit_block_is_all_one_range(tb1, i, j);
18054  assert(b);
18056  assert(b);
18057  b = bm::bit_block_is_all_one_range(tb1, i, i+63);
18058  assert(b);
18059 
18060  auto cnt = bm::bit_block_calc_count_range(tb1, i, i);
18061  assert(cnt == 1);
18063  assert(cnt == j-i+1);
18064  cnt = bm::bit_block_calc_count_range(tb1, i, i+63);
18065  assert(cnt == 64);
18066  }
18067  }
18068 
18069  {
18070  bm::bit_block_set(tb1, 0u);
18071 
18072  unsigned i(0), j(65535);
18073  for (; i < j; ++i, --j)
18074  {
18076  assert(!b);
18077  b = bm::bit_block_is_all_one_range(tb1, i, j);
18078  assert(!b);
18079  b = bm::bit_block_is_all_one_range(tb1, i, i+63);
18080  assert(!b);
18081  auto cnt = bm::bit_block_calc_count_range(tb1, i, j);
18082  assert(!cnt);
18083  cnt = bm::bit_block_calc_count_range(tb1, i, i+63);
18084  assert(!cnt);
18085  }
18086  }
18087 
18088  cout << "---------------------------- BitRangeAllSetTest() Ok." << endl;
18089 }
18090 
18091 
18092 
18094 {
18095  typedef std::vector<bvect::size_type> decode_vector;
18097 
18098 
18100  : dvect_(dvect)
18101  {
18102  dvect_.resize(0);
18103  }
18104 
18105  int add_bits(size_type offset, const unsigned char* bits, unsigned size)
18106  {
18107  for (size_type i = 0; i < size; ++i)
18108  dvect_.push_back(offset + bits[i]);
18109  return 0;
18110  }
18112  {
18113  for (size_type i = 0; i < size; ++i)
18114  dvect_.push_back(offset + i);
18115  return 0;
18116  }
18117 
18119 };
18120 
18121 static
18123 {
18124  cout << "---------------------------- BitForEachRangeFuncTest()" << endl;
18125 
18126  std::vector<bvect::size_type> dvect; // decode vector
18127  BM_DECLARE_TEMP_BLOCK(tb1);
18128 
18129  bm::bit_block_set(tb1, 0u);
18130 
18131  {
18132  TestDecodeFunctor func(dvect);
18133  bm::for_each_bit_blk(FULL_BLOCK_FAKE_ADDR, 0u, 0u, 0u, func);
18134  assert(dvect.size()==1);
18135  assert(dvect[0] == 0);
18136 
18137  dvect.resize(0);
18138  bm::for_each_bit_blk(FULL_BLOCK_FAKE_ADDR, 0u, 0u, 10u, func);
18139 
18140  assert(dvect.size()==11);
18141  for (size_t i = 0; i < dvect.size(); ++i)
18142  {
18143  auto v = dvect[i];
18144  assert(v == i);
18145  } // for i
18146  }
18147 
18148  {
18149  TestDecodeFunctor func(dvect);
18150  bm::for_each_bit_blk(tb1, 0u, 0u, 0u, func);
18151  assert(dvect.size()==0);
18152 
18153  tb1[0] = 1;
18154  bm::for_each_bit_blk(tb1, 0u, 0u, 0u, func);
18155  assert(dvect.size()==1);
18156  assert(dvect[0] == 0);
18157  dvect.resize(0);
18158 
18159  for (unsigned k = 0; k < 65535; ++k)
18160  {
18161  bm::for_each_bit_blk(tb1, k, 0, k, func);
18162  assert(dvect.size()==1);
18163  assert(dvect[0] == k);
18164  dvect.resize(0);
18165  }
18166 
18167  tb1[0] = 3;
18168 
18169  bm::for_each_bit_blk(tb1, 0u, 0u, 31u, func);
18170  assert(dvect.size()==2);
18171  assert(dvect[0] == 0);
18172  assert(dvect[1] == 1);
18173  dvect.resize(0);
18174 
18175  bm::for_each_bit_blk(tb1, 0u, 1u, 31u, func);
18176  assert(dvect.size()==1);
18177  assert(dvect[0] == 1);
18178  dvect.resize(0);
18179 
18180  tb1[0] = (1u<<5) | (1u << 7) | (1u<<31);
18181  bm::for_each_bit_blk(tb1, 0u, 4u, 31u, func);
18182  assert(dvect.size()==3);
18183  assert(dvect[0] == 5);
18184  assert(dvect[1] == 7);
18185  assert(dvect[2] == 31);
18186  dvect.resize(0);
18187 
18188  tb1[1] = 1;
18189  bm::for_each_bit_blk(tb1, 0u, 0u, 37u, func);
18190  assert(dvect.size()==4);
18191  assert(dvect[0] == 5);
18192  assert(dvect[1] == 7);
18193  assert(dvect[2] == 31);
18194  assert(dvect[3] == 32);
18195  dvect.resize(0);
18196 
18197 
18198  bm::for_each_bit_blk(tb1, 0u, 4u, 37u, func);
18199  assert(dvect.size()==4);
18200  assert(dvect[0] == 5);
18201  assert(dvect[1] == 7);
18202  assert(dvect[2] == 31);
18203  assert(dvect[3] == 32);
18204  dvect.resize(0);
18205 
18206  bm::for_each_bit_blk(tb1, 120u, 31u, 32u, func);
18207  assert(dvect.size()==2);
18208  assert(dvect[0] == 31+120);
18209  assert(dvect[1] == 32+120);
18210  dvect.resize(0);
18211 
18212  }
18213 
18214  {
18215  bm::bit_block_set(tb1, 0u);
18216  tb1[0] = 1;
18217  tb1[2047] = 1u << 31;
18218 
18219  TestDecodeFunctor func(dvect);
18220  tb1[1] = 1;
18221  bm::for_each_bit_blk(tb1, 0u, 0u, 63u, func);
18222  assert(dvect.size()==2);
18223  assert(dvect[0] == 0);
18224  assert(dvect[1] == 32);
18225  dvect.resize(0);
18226 
18227  tb1[1] = 0;
18228 
18229  bm::for_each_bit_blk(tb1, 0u, 0u, 65535u, func);
18230  assert(dvect.size()==2);
18231  assert(dvect[0] == 0);
18232  assert(dvect[1] == 65535);
18233  dvect.resize(0);
18234 
18235  tb1[0] = 1<<5;
18236 
18237  bm::for_each_bit_blk(tb1, 0u, 3u, 65535u, func);
18238  assert(dvect.size()==2);
18239  assert(dvect[0] == 5);
18240  assert(dvect[1] == 65535);
18241  dvect.resize(0);
18242 
18243  for (unsigned k = 0; k < 128; ++k)
18244  {
18245  bm::for_each_bit_blk(tb1, 0u, 65535u-k, 65535u, func);
18246  assert(dvect.size()==1);
18247  assert(dvect[0] == 65535);
18248  dvect.resize(0);
18249  }
18250  }
18251 
18252 
18253 
18254  cout << " for_each_bit_blk() stress 1 ..." << endl;
18255  {
18256  bm::bit_block_set(tb1, ~0u);
18257 
18258  unsigned off = 1234567;
18259  unsigned j = 65535;
18260  for (unsigned i0 = 0; i0 <= j; ++i0)
18261  {
18262  TestDecodeFunctor func(dvect);
18263 
18264  bm::for_each_bit_blk(FULL_BLOCK_FAKE_ADDR, off, i0, j, func);
18265  for (size_t i = 0; i < dvect.size(); ++i)
18266  {
18267  auto v = dvect[i];
18268  assert(v == (i0+off+i));
18269  } // for i
18270  dvect.resize(0);
18271  bm::for_each_bit_blk(tb1, off, i0, j, func);
18272  for (size_t i = 0; i < dvect.size(); ++i)
18273  {
18274  auto v = dvect[i];
18275  assert(v == (i0+off+i));
18276  } // for i
18277 
18278  }
18279  }
18280 
18281  cout << " for_each_bit_blk() stress 2 ..." << endl;
18282  {
18283  std::vector<unsigned> svect;
18284  svect.push_back(0);
18285  svect.push_back(~0u);
18286  svect.push_back(1u);
18287  svect.push_back(8u);
18288  svect.push_back(16u);
18289  svect.push_back(1u << 31);
18290  svect.push_back(1u << 24);
18291 
18292  for (unsigned pass = 0; pass < svect.size(); ++pass)
18293  {
18294  auto testv = svect[pass];
18295  bm::bit_block_set(tb1, testv);
18296 
18297  unsigned off = 1234567;
18298  unsigned j = 65535;
18299  for (unsigned i0 = 0; i0 <= j; ++i0, --j)
18300  {
18301  TestDecodeFunctor func(dvect);
18302 
18303  bm::for_each_bit_blk(FULL_BLOCK_FAKE_ADDR, off, i0, j, func);
18304  assert(dvect.size() == j-i0+1);
18305  for (size_t i = 0; i < dvect.size(); i+=2)
18306  {
18307  auto v = dvect[i];
18308  assert(v == (i0+off+i));
18309  } // for i
18310  dvect.resize(0);
18311 
18312  auto cnt = bm::bit_block_calc_count_range(tb1, i0, j);
18313  bool all_one = bm::bit_block_is_all_one_range(tb1, i0, j);
18314  if (testv == 0)
18315  {
18316  assert(!cnt);
18317  assert(!all_one);
18318  }
18319  if (testv == ~0u)
18320  {
18321  assert(cnt == (j-i0+1));
18322  assert(all_one);
18323  }
18324 
18325  bm::for_each_bit_blk(tb1, off, i0, j, func);
18326  if (testv == ~0u)
18327  {
18328  assert(dvect.size() == j-i0+1);
18329  }
18330  assert(dvect.size() == cnt);
18331 
18332  if (testv == ~0u)
18333  {
18334  for (size_t i = 0; i < dvect.size(); i+=2)
18335  {
18336  auto v = dvect[i];
18337  assert(v == (i0+off+i));
18338  } // for i
18339  }
18340  else
18341  {
18342  // TODO: cover all cases via alternative implementation
18343  }
18344  }
18345  } // for pass
18346  }
18347 
18348 
18349  cout << "---------------------------- BitForEachRangeFuncTest() Ok." << endl;
18350 }
18351 
18352 
18353 static
18355 {
18356  const char seeds[] =
18357  { 'A', 'C', 'G', 'T', 'A', 'C', 'G', 'A', 'N', 'A', 'C', 'G' };
18358 
18359  const unsigned arr_size = bm::set_block_size*4;
18360  const unsigned arr_plane_size = arr_size / 8;
18361 
18362  unsigned char BM_VECT_ALIGN block1[arr_size] BM_VECT_ALIGN_ATTR = {0,};
18363 
18364  unsigned char BM_VECT_ALIGN tmatrix1[8][arr_plane_size] BM_VECT_ALIGN_ATTR;
18365  unsigned BM_VECT_ALIGN distance1[8][8] BM_VECT_ALIGN_ATTR;
18366  unsigned char pc_vector1[8] = {0,};
18367  unsigned pc_vector_stat1[bm::ibpc_end];
18368 /*
18369  unsigned BM_ALIGN16 tmatrix2[32][bm::set_block_plane_size] BM_ALIGN16ATTR;
18370  unsigned
18371  BM_ALIGN16 distance2[bm::set_block_plane_cnt][bm::set_block_plane_cnt] BM_ALIGN16ATTR;
18372  unsigned char pc_vector2[32] = {0,};
18373 
18374 
18375  unsigned BM_ALIGN16 tmatrix3[32][bm::set_block_plane_size] BM_ALIGN16ATTR;
18376  unsigned
18377  BM_ALIGN16 distance3[bm::set_block_plane_cnt][bm::set_block_plane_cnt] BM_ALIGN16ATTR;
18378  unsigned char pc_vector3[32] = {0,};
18379 */
18380 
18381  // generate pseudo-random DNA sequence
18382  for (unsigned i = 0; i < arr_size; ++i)
18383  {
18384  unsigned letter_idx = unsigned(rand()) % unsigned(sizeof(seeds));
18385  unsigned char l = (unsigned char)seeds[letter_idx];
18386  unsigned char c = 0;
18387  switch (l)
18388  {
18389  case 'A':
18390  c = 0; break;
18391  case 'C':
18392  c = 1; break;
18393  case 'G':
18394  c = 2; break;
18395  case 'T':
18396  c = 3; break;
18397  case 'N':
18398  c = 4; break;
18399  default:
18400  cout << "Alphabet error!" << endl;
18401  exit(1);
18402  };
18403  block1[i] = c;
18404  //cout << block1[i];
18405  }
18406  cout << endl;
18407 
18408  bm::vect_bit_transpose<unsigned char,
18409  8,
18410  arr_plane_size>
18411  (block1, arr_size, tmatrix1);
18412 
18413  bm::tmatrix_distance<unsigned char,
18414  8,
18415  arr_plane_size>
18416  (tmatrix1, distance1);
18417 
18418  cout << "ALL count=" << sizeof(char)*8*arr_plane_size << endl;
18419  bm::bit_iblock_make_pcv<unsigned char, 8, arr_plane_size>(distance1, pc_vector1);
18420 
18421  bm::bit_iblock_pcv_stat(pc_vector1, pc_vector1 + 8, pc_vector_stat1);
18422 
18423  for (unsigned s = 0; s < bm::ibpc_end; ++s)
18424  {
18425  switch(s)
18426  {
18427  case bm::ibpc_uncompr:
18428  cout << "Uncompressed: ";
18429  break;
18430  case bm::ibpc_all_zero:
18431  cout << " All ZERO: ";
18432  break;
18433  case bm::ibpc_all_one:
18434  cout << " All ONE: ";
18435  break;
18436  case bm::ibpc_equiv:
18437  cout << " Equiv: ";
18438  break;
18439  case bm::ibpc_close:
18440  cout << " Similar: ";
18441  break;
18442  default:
18443  //cout << "Oops!" << s << " ";
18444  break;
18445  }
18446  cout << pc_vector_stat1[s] << endl;
18447  } // for
18448 
18449 
18450  // print out the pc_vector
18451  for (unsigned j = 0; j < 8; ++j)
18452  {
18453  unsigned ibpc = pc_vector1[j] & 7;
18454  unsigned n_row = (pc_vector1[j] >> 3);
18455  cout << j << ":" << "->" << n_row << " ";
18456 
18457  switch(ibpc)
18458  {
18459  case bm::ibpc_uncompr:
18460  cout << "Uncompressed: ";
18461  cout << " popcnt=" << distance1[j][j];
18462  break;
18463  case bm::ibpc_all_zero:
18464  cout << "ZERO";
18465  break;
18466  case bm::ibpc_all_one:
18467  cout << "ONE: ";
18468  break;
18469  case bm::ibpc_equiv:
18470  cout << "Equiv: ";
18471  break;
18472  case bm::ibpc_close:
18473  cout << " Similar: ";
18474  cout << " popcnt=" << distance1[j][j]
18475  << " Humming=" << distance1[j][n_row];
18476  break;
18477  default:
18478  assert(0);
18479  }
18480  cout << endl;
18481  }
18482 /*
18483  cout << endl << "Second round." << endl << endl;
18484 
18485  bm::bit_iblock_reduce(tmatrix1, pc_vector1, pc_vector1+32, tmatrix2);
18486  bm::tmatrix_distance<unsigned,
18487  bm::set_block_plane_cnt,
18488  bm::set_block_plane_size>
18489  (tmatrix2, distance2);
18490 
18491  bm::bit_iblock_make_pcv(distance2, pc_vector2);
18492 
18493  // print out the pc_vector
18494  for (unsigned j = 0; j < 32; ++j)
18495  {
18496  unsigned ibpc = pc_vector2[j] & 7;
18497  unsigned n_row = (pc_vector2[j] >> 3);
18498  cout << j << ":" << "->" << n_row << " ";
18499 
18500  switch(ibpc)
18501  {
18502  case bm::ibpc_uncompr:
18503  cout << "Uncompressed: ";
18504  cout << " popcnt=" << distance2[j][j];
18505  break;
18506  case bm::ibpc_all_zero:
18507  cout << "ZERO";
18508  break;
18509  case bm::ibpc_all_one:
18510  cout << "ONE: ";
18511  break;
18512  case bm::ibpc_equiv:
18513  cout << "Equiv: ";
18514  break;
18515  case bm::ibpc_close:
18516  cout << " Similar: ";
18517  cout << " popcnt=" << distance2[j][j]
18518  << " Humming=" << distance2[j][n_row] << endl;
18519  {
18520  const unsigned* r1 = tmatrix2[j];
18521  for (unsigned i = 0; i < bm::set_block_plane_size; ++i)
18522  {
18523  cout << hex << r1[i] << " ";
18524  }
18525  cout << dec << endl << endl;
18526  }
18527  break;
18528  }
18529  cout << endl;
18530  }
18531 
18532 
18533  cout << endl << "3rd round." << endl << endl;
18534 
18535  bm::bit_iblock_reduce(tmatrix2, pc_vector2, pc_vector2+32, tmatrix3);
18536 
18537  bm::tmatrix_distance<unsigned,
18538  bm::set_block_plane_cnt,
18539  bm::set_block_plane_size>
18540  (tmatrix3, distance3);
18541 
18542  bm::bit_iblock_make_pcv(distance3, pc_vector3);
18543 
18544  // print out the pc_vector
18545  for (unsigned j = 0; j < 32; ++j)
18546  {
18547  unsigned ibpc = pc_vector3[j] & 7;
18548  unsigned n_row = (pc_vector3[j] >> 3);
18549  cout << j << ":" << "->" << n_row << " ";
18550 
18551  switch(ibpc)
18552  {
18553  case bm::ibpc_uncompr:
18554  cout << "Uncompressed: ";
18555  cout << " popcnt=" << distance3[j][j];
18556  break;
18557  case bm::ibpc_all_zero:
18558  cout << "ZERO";
18559  break;
18560  case bm::ibpc_all_one:
18561  cout << "ONE: ";
18562  break;
18563  case bm::ibpc_equiv:
18564  cout << "Equiv: ";
18565  break;
18566  case bm::ibpc_close:
18567  cout << " Similar: ";
18568  cout << " popcnt=" << distance3[j][j]
18569  << " Humming=" << distance3[j][n_row] << endl;
18570  {
18571  const unsigned* r1 = tmatrix3[j];
18572  for (unsigned i = 0; i < bm::set_block_plane_size; ++i)
18573  {
18574  cout << hex << r1[i] << " ";
18575  }
18576  cout << dec << endl << endl;
18577  }
18578  break;
18579  }
18580  cout << endl;
18581  }
18582 */
18583 
18584 }
18585 
18586 void BitBlockTransposeTest();
18587 
18589 {
18591 
18592 
18595  unsigned BM_ALIGN16 tmatrix1[32][bm::set_block_plane_size] BM_ALIGN16ATTR;
18596 
18597 
18598  cout << "---------------------------- BitTransposeTest" << endl;
18599 
18600  cout << "Transpose 1" << endl;
18601 
18602  for (unsigned i = 0; i < bm::set_block_size; ++i)
18603  {
18604  block1[i] = 1;
18605  }
18606 
18607  bm::vect_bit_transpose<unsigned,
18610  (block1, bm::set_block_size, tmatrix1);
18611 
18612  bm::vect_bit_trestore<unsigned,
18615  (tmatrix1, block2);
18616 
18617  for (unsigned i = 0; i < bm::set_block_size; ++i)
18618  {
18619  if (block1[i] != block2[i])
18620  {
18621  cout << "Bit transpose error! " << i << endl; exit(1);
18622  }
18623  }
18624 
18625  {
18627  bm::tmatrix_distance<unsigned,
18630  (tmatrix1, distance);
18631 
18632  PrintDistanceMatrix(cout,distance);
18633 
18634  // distance matrix verification:
18635  {
18636  for (unsigned i = 0; i < bm::set_block_plane_cnt; ++i)
18637  {
18638  const unsigned* row = distance[i];
18639  for (unsigned j = i; j < bm::set_block_plane_cnt; ++j)
18640  {
18641  if (i == j)
18642  {
18643  if (distance[0][0] != 2048)
18644  {
18645  cout << "Self distance(bitcount) is incorrect!" << endl;
18646  exit(1);
18647  }
18648  }
18649  else
18650  {
18651  if (i == 0)
18652  {
18653  if (row[j] != 2048) // max distance
18654  {
18655  cout << "Incorrect max distance!" << endl; exit(1);
18656  }
18657  }
18658  else
18659  {
18660  if (row[j] != 0) // max distance
18661  {
18662  cout << "Incorrect min distance!" << endl; exit(1);
18663  }
18664  }
18665  }
18666  }
18667  }
18668  }
18669 
18670  }
18671 
18672  cout << "Transpose 2" << endl;
18673 
18674  for (unsigned i = 0; i < bm::set_block_size; ++i)
18675  {
18676  block1[i] = 1 | (1 << 17);
18677  }
18678 
18679  bm::vect_bit_transpose<unsigned,
18682  (block1, bm::set_block_size, tmatrix1);
18683  bm::vect_bit_trestore<unsigned,
18686  (tmatrix1, block2);
18687 
18688 
18689  for (unsigned i = 0; i < bm::set_block_size; ++i)
18690  {
18691  if (block1[i] != block2[i])
18692  {
18693  cout << "Bit transpose error! " << i << endl; exit(1);
18694  }
18695  }
18696 
18697  cout << "Transpose 3" << endl;
18698 
18699  for (unsigned i = 0; i < bm::set_block_size; ++i)
18700  {
18701  block1[i] = ~1u;
18702  }
18703 
18704  bm::vect_bit_transpose<unsigned,
18707  (block1, bm::set_block_size, tmatrix1);
18708  bm::vect_bit_trestore<unsigned,
18711  (tmatrix1, block2);
18712 
18713  for (unsigned i = 0; i < bm::set_block_size; ++i)
18714  {
18715  if (block1[i] != block2[i])
18716  {
18717  cout << "Bit transpose error! " << i << endl; exit(1);
18718  }
18719  }
18720 
18721  cout << "Transpose 4" << endl;
18722 
18723  for (unsigned i = 0; i < bm::set_block_size; ++i)
18724  {
18725  block1[i] = i;
18726  }
18727 
18728  bm::vect_bit_transpose<unsigned,
18731  (block1, bm::set_block_size, tmatrix1);
18732  bm::vect_bit_trestore<unsigned,
18735  (tmatrix1, block2);
18736 
18737  for (unsigned i = 0; i < bm::set_block_size; ++i)
18738  {
18739  if (block1[i] != block2[i])
18740  {
18741  cout << "Bit transpose error! " << i << endl; exit(1);
18742  }
18743  }
18744 /*
18745  cout << "Transpose 5 - random" << endl;
18746 
18747  for (unsigned c = 0; c < 10000; ++c)
18748  {
18749  if ((c % 100) == 0) cout << ".";
18750 
18751  for (unsigned i = 0; i < bm::set_block_size; ++i)
18752  {
18753  block1[i] = rand();
18754  }
18755 
18756  bm::vect_bit_transpose<unsigned,
18757  bm::set_block_plane_cnt,
18758  bm::set_block_plane_size>
18759  (block1, bm::set_block_size, tmatrix1);
18760 
18761  bm::vect_bit_trestore<unsigned,
18762  bm::set_block_plane_cnt,
18763  bm::set_block_plane_size>
18764  (tmatrix1, block2);
18765 
18766 
18767  for (unsigned i = 0; i < bm::set_block_size; ++i)
18768  {
18769  if (block1[i] != block2[i])
18770  {
18771  cout << "Bit transpose error! " << i << endl; exit(1);
18772  }
18773  }
18774  }
18775  */
18776 
18777  cout << "Transpose GAP block 1" << endl;
18778 
18779  {
18780  gap_vector gapv(0);
18781  gap_vector gapv1(9);
18782  gapv.set_bit(1);
18783  gapv.set_bit(2);
18784  gapv.set_bit(10);
18785  gapv.set_bit(65000);
18786 
18787 
18789 
18790  if ( bm::conditional<sizeof(gte.tmatrix_) != (2048 * sizeof(bm::word_t))>::test())
18791  {
18792  cout << "TMatrix recalculation error!" << sizeof(gte.tmatrix_) << endl;
18793  exit(1);
18794  }
18795 // gte.transpose(gapv.get_buf());//, block1);
18796 
18797 
18799  gte.reduce();
18800  gte.restore();
18801 
18802  unsigned glen = *(gapv.get_buf()) >> 3;
18803  PrintGap(cout,gapv.get_buf());
18804  PrintDGap(cout,(gap_word_t*) block1, glen-1);
18805  PrintDGapGamma(cout,(gap_word_t*) block1, glen-1);
18806 
18807  PrintTMatrix(cout,gte.tmatrix_, gte.eff_cols_, true);
18808 
18809  //bm::gap_word_t gap_head = *gapv.get_buf();
18810 // gte.trestore(gap_head, gapv1.get_buf());//, block2);
18811 /*
18812  if (gapv.compare(gapv1))
18813  {
18814  cout << "GAP block transpose error!" << endl;
18815  PrintGap(gapv.get_buf());
18816  PrintGap(gapv1.get_buf());
18817  exit(1);
18818  }
18819 */
18820  }
18821 
18822  cout << "Transpose GAP block 2" << endl;
18823 
18824  {
18825  gap_vector gapv(0);
18826  gap_vector gapv1(0);
18827 
18828  unsigned gcnt = 5;
18829  for (unsigned i = 0; i < 65500; i+= 50)
18830  {
18831  for (unsigned j = 0; j < gcnt ; ++j)
18832  {
18833  gapv.set_bit(i);
18834 
18835  if (++i > 65500)
18836  break;
18837  }
18838  gcnt += 2;
18839  }
18840 
18842 // gte.transpose(gapv.get_buf());
18843 
18844 
18846  gte.reduce();
18847  gte.restore();
18848 
18849  unsigned glen = *(gapv.get_buf()) >> 3;
18850  cout << glen << endl;
18851 
18852  // bm::gap_word_t gap_head = *gapv.get_buf();
18853 // gte.trestore(gap_head, gapv1.get_buf());
18854 
18855 /*
18856  if (gapv.compare(gapv1))
18857  {
18858  cout << "GAP block transpose error!" << endl;
18859  PrintGap(gapv.get_buf());
18860  PrintGap(gapv1.get_buf());
18861  exit(1);
18862  }
18863 */
18864 
18865  }
18866 
18867 
18868  cout << endl << "---------------------------- BitTransposeTest ok" << endl;
18869 }
18870 
18871 /*
18872 #define POWER_CHECK(w, mask) \
18873  (bm::bit_count_table<true>::_count[(w&mask) ^ ((w&mask)-1)])
18874 
18875 void BitListTest()
18876 {
18877  unsigned bits[64] = {0,};
18878 
18879  unsigned w = 0;
18880 
18881  w = (1 << 3) | 1;
18882 
18883 
18884  int bn3 = POWER_CHECK(w, 1 << 3) - 1;
18885  int bn2 = POWER_CHECK(w, 1 << 2) - 1;
18886  int bn0 = POWER_CHECK(w, 1 << 0) - 1;
18887 
18888  bit_list(w, bits+1);
18889 
18890 }
18891 */
18892 
18893 static
18895 {
18896  {{
18897  bvect bv(0);
18898  assert(bv.any() == false);
18899  assert(bv.count() == 0);
18900  }}
18901 
18902  {{
18903  bvect bv1(10);
18904  bvect bv2(bv1);
18905  assert(bv1.size() == bv2.size());
18906  }}
18907 
18908  {{
18909  bvect bv;
18910  bv.invert();
18911  bvect::size_type cnt = bv.count();
18912  assert(cnt == bm::id_max);
18913  assert(bv.test(bm::id_max-1));
18914  }}
18915 
18916  {{
18917  bvect bv(10);
18918  assert(bv.any() == false);
18919  assert(bv.count() == 0);
18920  bv.invert();
18921  unsigned cnt = bv.count();
18922  assert(cnt == 10);
18923  }}
18924 
18925  {{
18926  bvect bv1(10);
18927  bv1.set(1);
18928  bvect bv2(0);
18929 
18930  assert(bv1.size() == 10);
18931  assert(bv2.size() == 0);
18932  assert(bv1.count() == 1);
18933  assert(bv2.count() == 0);
18934 
18935  bv1.swap(bv2);
18936 
18937  assert(bv2.size() == 10);
18938  assert(bv2.count() == 1);
18939  assert(bv1.size() == 0);
18940  assert(bv1.count() == 0);
18941  }}
18942 
18943  {{
18944  bvect bv1;
18945  bv1.set(65536);
18946  bv1.set(100);
18947  assert(bv1.size() == bm::id_max);
18948  assert(bv1.count() == 2);
18949  bv1.resize(101);
18950  assert(bv1.size() == 101);
18951  assert(bv1.count() == 1);
18952  {{
18953  auto f = bv1.get_first();
18954  assert(f == 100);
18955  f = bv1.get_next(f);
18956  assert(f == 0);
18957  }}
18958 
18959  bv1.resize(10);
18960  assert(bv1.size() == 10);
18961  assert(bv1.count() == 0);
18962  auto f = bv1.get_first();
18963  assert(f == 0);
18964  }}
18965 
18966  {{
18967  bvect bv;
18968  print_stat(cout,bv);
18969  bv.set(100);
18970  bv.set(65536 + 10);
18971  print_stat(cout,bv);
18972  bv.set_range(0, 65536*10, false);
18973  print_stat(cout,bv);
18974  }}
18975 
18976  // test logical operations
18977 
18978  {{
18979  bvect bv1(65536 * 10);
18980  bvect bv2(65536 * 100);
18981  bv1.set(5);
18982  bv2.set(5);
18983  bv2.set(65536 * 2);
18984  bv2 &= bv1;
18985  assert(bv2.size() == 65536 * 100);
18986  assert(bv2.count() == 1);
18987  assert(bv2.get_first() == 5);
18988  }}
18989 
18990  {{
18991  bvect bv1(10);
18992  bvect bv2;
18993  bv1.set(5);
18994  bv2.set(5);
18995  bv2.set(65536 * 2);
18996  bv1 &= bv2;
18997  assert(bv1.size() == bv2.size());
18998  assert(bv1.count() == 1);
18999  assert(bv1.get_first() == 5);
19000  }}
19001 
19002  {{
19003  bvect bv1(10);
19004  bvect bv2;
19005  bv1.set(5);
19006  bv2.set(6);
19007  bv2.set(65536 * 2);
19008  bv1 |= bv2;
19009  assert(bv1.size() == bv2.size());
19010  assert(bv1.count() == 3);
19011  }}
19012 
19013  // comparison test
19014 
19015  {{
19016  int cmp;
19017  bvect bv1(10);
19018  bvect bv2;
19019  bv2.set(65536 * 2);
19020 
19021  cmp = bv1.compare(bv2);
19022  assert(cmp < 0);
19023 
19024  bv1.set(5);
19025  assert(cmp < 0);
19026  cmp = bv1.compare(bv2);
19027  assert(cmp > 0);
19028  cmp = bv2.compare(bv1);
19029  assert(cmp < 0);
19030  }}
19031 
19032  // inserter
19033 
19034  {{
19035  bvect bv1(10);
19036  {
19037  bvect::insert_iterator it(bv1);
19038  *it = 100 * 65536;
19039  }
19040  assert(bv1.size() == 100 * 65536 + 1);
19041  }}
19042 
19043  // serialization
19044 
19045  {{
19046  bvect bv1(10);
19047  bv1.set(5);
19048  struct bvect::statistics st1;
19049  bv1.calc_stat(&st1);
19050 
19051  unsigned char* sermem = new unsigned char[st1.max_serialize_mem];
19052  size_t slen2 = bm::serialize(bv1, sermem);
19053  cout << slen2 << endl;
19054 
19055  bvect bv2(0);
19056  bm::deserialize(bv2, sermem);
19057  delete [] sermem;
19058 
19059  assert(bv2.size() == 10);
19060  assert(bv2.count() == 1);
19061  assert(bv2.get_first() == 5);
19062  }}
19063 
19064  {{
19065  bvect bv1(10);
19066  bv1.set(5);
19067  unsigned int arg[] = { 10, 65536, 65537, 65538 * 10000 };
19068  unsigned* it1 = arg;
19069  unsigned* it2 = arg + 4;
19070  combine_or(bv1, it1, it2);
19071  assert(bv1.size() == 65538 * 10000 + 1);
19072  bvect::enumerator en = bv1.first();
19073  while (en.valid())
19074  {
19075  cout << *en << " ";
19076  ++en;
19077  }
19078  }}
19079 }
19080 
19081 static
19082 void VerifyCountRange(const bvect& bv,
19083  const bvect::rs_index_type& bc_arr,
19084  bvect::size_type from,
19085  bvect::size_type to)
19086 {
19087  for (bvect::size_type i = from; i < to; ++i)
19088  {
19089  bvect::size_type cnt1 = bv.count_range(0, i);
19090  bvect::size_type cnt2 = bv.count_to(i, bc_arr);
19091  auto cnt3 = bv.count_to_test(i, bc_arr);
19092 
19093  assert(cnt1 == cnt2);
19094  if (cnt1 != cnt2)
19095  {
19096  cerr << "VerifyCountRange failed!" << " count_range()=" << cnt1
19097  << " count_to()=" << cnt2 << endl;
19098  }
19099  if (cnt3 != cnt1)
19100  {
19101  bool b = bv.test(i);
19102  if (b)
19103  {
19104  cerr << "VerifyCountRange failed! count_to_test()" << cnt3 << " count_range()=" << cnt1
19105  << endl;
19106  }
19107  }
19108 
19109  bvect::size_type cnt4 = bv.count_range(i, to);
19110  bvect::size_type cnt5 = bv.count_range(i, to, bc_arr);
19111  if (cnt4 != cnt5)
19112  {
19113  cnt5 = bv.count_range(i, to, bc_arr);
19114  assert(cnt4 == cnt5);
19115  exit(1);
19116  }
19117  } // for
19118 }
19119 
19120 static
19122 {
19123  cout << "---------------------------- CountRangeTest..." << endl;
19124 
19125  {{
19126  bvect bv1 { 0, 1 };
19127  bv1.set(0);
19128  bv1.set(1);
19129 
19130  bvect::rs_index_type bc_arr;
19131  bv1.build_rs_index(&bc_arr);
19132  assert(bc_arr.count() == 2);
19133 
19134  assert(bc_arr.count(0) == 2);
19135  for (bvect::size_type i = 1; i < bm::set_total_blocks; ++i)
19136  {
19137  assert(bc_arr.count(i) == 0);
19138  } // for
19139 
19140  VerifyCountRange(bv1, bc_arr, 0, 200000);
19141 
19142  bv1.optimize();
19143  bvect::rs_index_type bc_arr1;
19144  bv1.build_rs_index(&bc_arr1);
19145 
19146  assert(bc_arr.count(0) == 2);
19147  for (bvect::size_type i = 1; i < bm::set_total_blocks; ++i)
19148  {
19149  assert(bc_arr.count(i) == 0);
19150  } // for
19151 
19152  VerifyCountRange(bv1, bc_arr1, 0, 200000);
19153  }}
19154 
19155  {{
19156  bvect bv1 { bm::id_max - 100, bm::id_max - 1 };
19157 
19158  bvect::rs_index_type bc_arr;
19159  bv1.build_rs_index(&bc_arr);
19160  assert(bc_arr.count() == 2);
19161 
19162  assert(bc_arr.rcount(bm::set_total_blocks-1) == 2);
19163  for (bvect::size_type i = 0; i < bm::set_total_blocks-1; ++i)
19164  {
19165  assert(bc_arr.rcount(i) == 0);
19166  } // for
19167 
19168  VerifyCountRange(bv1, bc_arr, 0, 200000);
19169 
19170  bv1.optimize();
19171  bvect::rs_index_type bc_arr1;
19172  bv1.build_rs_index(&bc_arr1);
19173 
19174  assert(bc_arr.rcount(bm::set_total_blocks-1) == 2);
19175  for (bvect::size_type i = 0; i < bm::set_total_blocks-1; ++i)
19176  {
19177  assert(bc_arr1.rcount(i) == 0);
19178  } // for
19179 
19180  VerifyCountRange(bv1, bc_arr1, 0, 200000);
19181  VerifyCountRange(bv1, bc_arr1, bm::id_max-200000, bm::id_max-1);
19182  }}
19183 
19184  {{
19185  bvect bv1 { 0, 1, 65535+10, 65535+20, 65535+21, bm::id_max-100};
19186 
19187 
19188  bvect::rs_index_type bc_arr;
19189  bv1.build_rs_index(&bc_arr);
19190  {
19191  auto cnt1 = bv1.count();
19192  auto cnt2 = bc_arr.count();
19193  assert(cnt1 == cnt2);
19194  }
19195 
19196  assert(bc_arr.rcount(0) == 2);
19197  assert(bc_arr.rcount(1) == 5);
19198  auto cnt = bc_arr.rcount(768);
19199  assert(cnt == 5);
19200  for (bvect::size_type i = 2; i < bm::set_total_blocks; ++i)
19201  {
19202  assert(bc_arr.rcount(i) == 5 || (bc_arr.rcount(i) == 6 && i == bm::set_total_blocks-1));
19203  } // for
19204 
19205  VerifyCountRange(bv1, bc_arr, bm::id_max-1, bm::id_max-1);
19206  for (unsigned i = 0; i < 2; ++i)
19207  {
19208  VerifyCountRange(bv1, bc_arr, 0, 200000);
19209  VerifyCountRange(bv1, bc_arr, bm::id_max-200000, bm::id_max-1);
19210 
19211  // check within empty region
19212  VerifyCountRange(bv1, bc_arr, bm::id_max/2-200000, bm::id_max/2+200000);
19213 
19214  bv1.optimize();
19215  bv1.build_rs_index(&bc_arr);
19216  }
19217  }}
19218 
19219  cout << "check 11111-filled bvector" << endl;
19220  {{
19221  bvect bv1;
19222  for (unsigned i = 0; i <= 200000; ++i)
19223  bv1.set(i, true);
19224 
19225  for (unsigned i = 0; i < 2; ++i)
19226  {
19227  bvect::rs_index_type rs_idx;
19228  bv1.build_rs_index(&rs_idx);
19229 
19230  VerifyCountRange(bv1, rs_idx, 0, 200000);
19231 
19232  bv1.optimize();
19233  }
19234  }}
19235 
19236  cout << "check inverted bvector" << endl;
19237  {{
19238  bvect bv1;
19239 
19240  bv1.invert();
19241 
19242  bvect::rs_index_type bc_arr;
19243  bv1.build_rs_index(&bc_arr);
19244  auto cnt1 = bv1.count();
19245  auto cnt2 = bc_arr.count();
19246  assert(cnt1 == cnt2);
19247 
19248  VerifyCountRange(bv1, bc_arr, bm::id_max-1, bm::id_max-1);
19249 
19250  VerifyCountRange(bv1, bc_arr, 0, 200000);
19251  VerifyCountRange(bv1, bc_arr, bm::id_max-200000, bm::id_max-1);
19252  VerifyCountRange(bv1, bc_arr, bm::id_max/2-200000, bm::id_max/2+200000);
19253  }}
19254 
19255  cout << "---------------------------- CountRangeTest OK" << endl;
19256 }
19257 
19258 // -----------------------------------------------------------------------
19259 
19261 from_arr[] = { 0, 0, 0, 0, 7, 1, 0, 65535, 65535, 0, 0, bm::id_max/2-2001, bm::id_max-2000, bm::id_max-1};
19263 to_arr[] = { 0, 1, 16, 32, 31, 255, 65535, 65536, 65536*2, 65537, bm::id_max/2-2000, bm::id_max/2+2000, bm::id_max-1, bm::id_max-1};
19264 
19265 static
19266 void verify_all_one_ranges(const bvect& bv, bool all_one)
19267 {
19268  size_t fr_size = sizeof(from_arr) / sizeof(from_arr[0]);
19269  size_t to_size = sizeof(to_arr) / sizeof(to_arr[0]);
19270 
19271  assert(fr_size == to_size);
19272 
19273  for (unsigned t = 0; t < fr_size; ++t)
19274  {
19275  bool one_test, one_test_cnt, any_one_test;
19276  bool is_int, is_int_c;
19277  bvect::size_type from(from_arr[t]), to(to_arr[t]);
19278 
19279  one_test = bv.is_all_one_range(from, to);
19280  if (all_one)
19281  {
19282  assert(one_test);
19283  any_one_test = bv.any_range(from, to);
19284  assert(any_one_test);
19285  is_int = bm::is_interval(bv, from, to);
19286  is_int_c = test_interval(bv, from, to);
19287  assert(is_int == is_int_c);
19288  assert(!is_int);
19289  if (is_int)
19290  {
19291  if (to < from)
19292  bm::xor_swap(to, from);
19293  bvect::size_type pos;
19294  bool b = bm::find_interval_end(bv, from, pos);
19295  assert(b && pos == to);
19296  b = bm::find_interval_start(bv, to, pos);
19297  assert(b && pos == from);
19298  }
19299  }
19300  else
19301  {
19302  auto cnt = bv.count_range(from, to);
19303  one_test_cnt = (cnt == to - from + 1);
19304  assert(one_test_cnt == one_test);
19305  any_one_test = bv.any_range(from, to);
19306  if (cnt)
19307  {
19308  assert(any_one_test);
19309  }
19310  else
19311  {
19312  assert(!any_one_test);
19313  }
19314  is_int = bm::is_interval(bv, from, to);
19315  is_int_c = test_interval(bv, from, to);
19316  assert(is_int == is_int_c);
19317  if (is_int)
19318  {
19319  if (to < from)
19320  bm::xor_swap(to, from);
19321  bvect::size_type pos;
19322  bool b = bm::find_interval_end(bv, from, pos);
19323  assert(b && pos == to);
19324  b = bm::find_interval_start(bv, to, pos);
19325  assert(b && pos == from);
19326  }
19327 
19328  }
19329  // [from-1, to] range check
19330  //
19331  if (from)
19332  {
19333  --from;
19334  one_test = bv.is_all_one_range(from, to);
19335  any_one_test = bv.any_range(from, to);
19336 
19337  is_int = bm::is_interval(bv, from, to);
19338  is_int_c = test_interval(bv, from, to);
19339  assert(is_int == is_int_c);
19340 
19341  if (all_one)
19342  {
19343  assert(one_test);
19344  assert(any_one_test);
19345  }
19346  else
19347  {
19348  auto cnt = bv.count_range(from, to);
19349  one_test_cnt = (cnt == to - from + 1);
19350  assert(one_test_cnt == one_test);
19351  if (cnt)
19352  {
19353  assert(any_one_test);
19354  }
19355  else
19356  {
19357  assert(!any_one_test);
19358  }
19359  }
19360  ++from;
19361  }
19362  // [from, to+1] range check
19363  //
19364  if (to < bm::id_max-1)
19365  {
19366  ++to;
19367  one_test = bv.is_all_one_range(from, to);
19368  any_one_test = bv.any_range(from, to);
19369 
19370  is_int = bm::is_interval(bv, from, to);
19371  is_int_c = test_interval(bv, from, to);
19372  assert(is_int == is_int_c);
19373 
19374  if (all_one)
19375  {
19376  assert(one_test);
19377  assert(any_one_test);
19378  }
19379  else
19380  {
19381  auto cnt = bv.count_range(from, to);
19382  one_test_cnt = (cnt == to - from + 1);
19383  assert(one_test_cnt == one_test);
19384  if (cnt)
19385  {
19386  assert(any_one_test);
19387  }
19388  else
19389  {
19390  assert(!any_one_test);
19391  }
19392  }
19393  --to;
19394  }
19395 
19396  } // for t
19397 }
19398 
19399 static
19401 {
19402  cout << "---------------------------- BvectorFindReverseTest()" << endl;
19403 
19404  bool b;
19405  bvect::size_type pos;
19406 
19407 
19408  cout << "Check inverted bvector..." << endl;
19409  {
19410  bvect bv;
19411  bv.flip();
19412  b = bv.find_reverse(0, pos);
19413  assert(b);
19414  assert(pos == 0);
19415  b = bv.find_reverse(65535, pos);
19416  assert(b);
19417  assert(pos == 65535);
19418 
19419  b = bv.find_reverse(bm::id_max-1, pos);
19420  assert(b);
19421  assert(pos == bm::id_max-1);
19422  b = bv.find_reverse(bm::id_max, pos);
19423  assert(b);
19424  assert(pos == bm::id_max-1);
19425  }
19426 
19427  cout << "Check bit bvector..." << endl;
19428  {
19429  bvect bv;
19430  bv[100] = true;
19431  b = bv.find_reverse(100, pos);
19432  assert(b);
19433  assert(pos == 100);
19434 
19435  b = bv.find_reverse(256, pos);
19436  assert(b);
19437  assert(pos == 100);
19438 
19439  bv[101] = true;
19440  b = bv.find_reverse(256, pos);
19441  assert(b);
19442  assert(pos == 101);
19443 
19444  bv[65355] = true;
19445 
19446  b = bv.find_reverse(256, pos);
19447  assert(b);
19448  assert(pos == 101);
19449 
19450  bv[100] = false;
19451  bv[101] = false;
19452  b = bv.find_reverse(256, pos);
19453  assert(!b);
19454 
19455  b = bv.find_reverse(bm::id_max/2, pos);
19456  assert(b);
19457  assert(pos == 65355);
19458 
19459  bv[65355*4] = true;
19460  b = bv.find_reverse(65355*2, pos);
19461  assert(b);
19462  assert(pos == 65355);
19463 
19464 
19465  }
19466  cout << "Check GAP bvector..." << endl;
19467  {
19468  bvect bv(bm::BM_GAP);
19469  bv[100] = true;
19470  b = bv.find_reverse(100, pos);
19471  assert(b);
19472  assert(pos == 100);
19473 
19474  b = bv.find_reverse(256, pos);
19475  assert(b);
19476  assert(pos == 100);
19477 
19478  bv[101] = true;
19479  b = bv.find_reverse(256, pos);
19480  assert(b);
19481  assert(pos == 101);
19482 
19483  bv[65355] = true;
19484 
19485  bv[100] = false;
19486  bv[101] = false;
19487  b = bv.find_reverse(256, pos);
19488  assert(!b);
19489 
19490  b = bv.find_reverse(bm::id_max/2, pos);
19491  assert(b);
19492  assert(pos == 65355);
19493 
19494  bv[65355*4] = true;
19495  b = bv.find_reverse(65355*2, pos);
19496  assert(b);
19497  assert(pos == 65355);
19498  }
19499 
19500 
19501 
19502  cout << "---------------------------- BvectorFindReverseTest() OK" << endl;
19503 }
19504 
19505 static
19507 {
19508  cout << "---------------------------- Intervals_RangesTest()" << endl;
19509 
19510  bool b;
19511  bvect::size_type pos;
19512  cout << "Check empty bvector" << endl;
19513  {{
19514  bvect bv1;
19515  verify_all_one_ranges(bv1, false);
19516 
19517  b = bm::find_interval_end(bv1, 0, pos);
19518  assert(!b);
19519  b = bm::find_interval_start(bv1, 0, pos);
19520  assert(!b);
19521 
19522  bv1.set(0);
19523  bv1.clear(0);
19524  verify_all_one_ranges(bv1, false);
19525  IntervalsCheck(bv1);
19526  }}
19527 
19528  cout << "Check inverted bvector..." << endl;
19529  {{
19530  bvect bv1;
19531  bv1.invert();
19532 
19533  verify_all_one_ranges(bv1, true);
19534  IntervalsCheck(bv1);
19535 
19536  b = bm::find_interval_end(bv1, 65536, pos);
19537  assert(b && pos == bm::id_max-1);
19538  b = bm::find_interval_end(bv1, 65536*4, pos);
19539  assert(b && pos == bm::id_max-1);
19540  b = bm::find_interval_end(bv1, bm::id_max/2, pos);
19541  assert(b && pos == bm::id_max-1);
19542  b = bm::find_interval_end(bv1, bm::id_max-2, pos);
19543  assert(b && pos == bm::id_max-1);
19544 
19545  b = bm::find_interval_start(bv1, 0, pos);
19546  assert(b && pos == 0);
19547  b = bm::find_interval_start(bv1, 65535, pos);
19548  assert(b && pos == 0);
19549  b = bm::find_interval_start(bv1, 65535*4, pos);
19550  assert(b && pos == 0);
19551  b = bm::find_interval_start(bv1, bm::id_max-1, pos);
19552  assert(b && pos == 0);
19553 
19554  for (bvect::size_type i = 0; i < bm::id_max/4; i+=(unsigned)rand()%256)
19555  {
19556  b = bm::find_interval_end(bv1, i, pos);
19557  assert(b && pos == bm::id_max-1);
19558 
19559  b = bm::find_interval_start(bv1, i, pos);
19560  assert(b && pos == 0);
19561  } // for i
19562 
19563 
19564 
19565  bv1.optimize();
19566  b = bm::find_interval_end(bv1, 1, pos);
19567  assert(b && pos == bm::id_max-1);
19568 
19569  }}
19570 
19571 
19572  {{
19573  bvect bv1;
19574  bv1.set(5);
19575  bv1.set(31);
19576 
19577  b = bm::find_interval_end(bv1, 5, pos);
19578  assert(b && pos == 5);
19579  b = bm::find_interval_end(bv1, 31, pos);
19580  assert(b && pos == 31);
19581 
19582  b = bm::find_interval_start(bv1, 5, pos);
19583  assert(b && pos == 5);
19584  b = bm::find_interval_start(bv1, 31, pos);
19585  assert(b && pos == 31);
19586 
19587  bv1.set(6);
19588 
19589  b = bm::find_interval_end(bv1, 5, pos);
19590  assert(b && pos == 6);
19591  b = bm::find_interval_end(bv1, 6, pos);
19592  assert(b && pos == 6);
19593 
19594  b = bm::find_interval_start(bv1, 5, pos);
19595  assert(b && pos == 5);
19596  b = bm::find_interval_start(bv1, 6, pos);
19597  assert(b && pos == 5);
19598 
19599  }}
19600 
19601  {{
19602  bvect::size_type base = 0;
19603 
19604  for (; base < bm::id_max/2; base += 65536)
19605  {
19606  bvect bv1 { 31+base, 32+base, 33+base, 34+base };
19607  b = bm::find_interval_end(bv1, 31+base, pos);
19608  assert(b && pos == 34+base);
19609  b = bm::find_interval_start(bv1, 31+base, pos);
19610  assert(b && pos == 31+base);
19611  b = bm::find_interval_start(bv1, 32+base, pos);
19612  assert(b && pos == 31+base);
19613  b = bm::find_interval_start(bv1, 34+base, pos);
19614  assert(b && pos == 31+base);
19615 
19616  b = bm::find_interval_start(bv1, 35+base, pos);
19617  assert(!b);
19618  } // for base
19619  }}
19620 
19621  {{
19622  bvect bv1;
19623  bv1.invert();
19624  bv1.set(0, false);
19625 
19626  //for (unsigned pass = 0; pass < 2; ++pass)
19627  {
19628  b = bm::find_interval_start(bv1, 65538, pos);
19629  assert(b && pos == 1);
19630  b = bm::find_interval_start(bv1, 65536*300, pos);
19631  assert(b && pos == 1);
19632  b = bm::find_interval_start(bv1, bm::id_max-1, pos);
19633  assert(b && pos == 1);
19634 
19635  bv1.set(31, false);
19636 
19637  b = bm::find_interval_start(bv1, bm::id_max-1, pos);
19638  assert(b && pos == 32);
19639 
19640  bv1.set(65535, false);
19641  b = bm::find_interval_start(bv1, 65536, pos);
19642  assert(b && pos == 65536);
19643  b = bm::find_interval_start(bv1, 65536*300, pos);
19644  assert(b && pos == 65536);
19645  b = bm::find_interval_start(bv1, bm::id_max-1, pos);
19646  assert(b && pos == 65536);
19647 
19648  bv1.set(65535*256-1, false);
19649  b = bm::find_interval_start(bv1, 65535*256, pos);
19650  assert(b && pos == 65535*256-1+1);
19651 
19652  b = bm::find_interval_start(bv1, bm::id_max-1, pos);
19653  assert(b && pos == 65535*256-1+1);
19654  bv1.set(65535*256+1, false);
19655  b = bm::find_interval_start(bv1, bm::id_max-1, pos);
19656  assert(b && pos == 65535*256+1+1);
19657 
19658  } // for pass
19659 
19660  }}
19661 
19662 
19663 
19664  {{
19665  bvect bv1;
19666  bv1.set(65536);
19667 
19668  for (unsigned pass = 0; pass<2; ++pass)
19669  {
19670  b = bm::find_interval_start(bv1, 65536, pos);
19671  assert(b && pos == 65536);
19672 
19673  b = bm::find_interval_end(bv1, 65536, pos);
19674  assert(b && pos == 65536);
19675 
19676  bv1.optimize();
19677  b = bm::find_interval_start(bv1, 65536, pos);
19678  assert(b && pos == 65536);
19679 
19680  b = bm::find_interval_end(bv1, 65536, pos);
19681  assert(b && pos == 65536);
19682 
19683  bv1.set(0);
19684  bv1.set(1);
19685 
19686  b = bm::find_interval_start(bv1, 0, pos);
19687  assert(b && pos == 0);
19688  pos = 0xDEADBEEF;
19689  b = bm::find_interval_start(bv1, 1, pos);
19690  assert(b && pos == 0);
19691 
19692  bv1.optimize();
19693 
19694  } // for unsigned
19695  }}
19696 
19697 
19698 
19699  {{
19700  bvect bv1;
19701  bv1.set_range(0, 65535);
19702 
19703  b = bm::find_interval_end(bv1, 65536, pos);
19704  assert(!b);
19705  b = bm::find_interval_end(bv1, 0, pos);
19706  assert(b);
19707  assert(pos == 65535);
19708  b = bm::find_interval_start(bv1, pos, pos);
19709  assert(b && pos == 0);
19710 
19711  b = bm::find_interval_end(bv1, 1234, pos);
19712  assert(b);
19713  assert(pos == 65535);
19714 
19715  bv1[65536]=true;
19716  bv1[65536]=false;
19717 
19718  b = bm::find_interval_end(bv1, 65536, pos);
19719  assert(!b);
19720  b = bm::find_interval_end(bv1, 0, pos);
19721  assert(b);
19722  assert(pos == 65535);
19723  b = bm::find_interval_end(bv1, 1234, pos);
19724  assert(b);
19725  assert(pos == 65535);
19726 
19727  bv1[65536]=true;
19728  b = bm::find_interval_end(bv1, 65536, pos);
19729  assert(b);
19730  assert(pos == 65536);
19731  b = bm::find_interval_end(bv1, 1234, pos);
19732  assert(b);
19733  assert(pos == 65536);
19734  }}
19735 
19736 
19737  cout << "find_interval_start/end()... bit stress" << endl;
19738  {{
19739  bvect bv1;
19740  bvect::size_type to = 65536*3 + 12;
19741  for (bvect::size_type i = 0; i < to; ++i)
19742  bv1.set(i);
19743  for (bvect::size_type i = 0; i < to; ++i)
19744  {
19745  b = bm::find_interval_end(bv1, i, pos);
19746  assert(b && pos == to-1);
19747 
19748  b = bm::find_interval_start(bv1, i, pos);
19749  assert(b && pos == 0);
19750  } // for i
19751  for (bvect::size_type j = to-1; j > 0; --j)
19752  {
19753  b = bm::find_interval_end(bv1, j, pos);
19754  assert(b && pos == to-1);
19755  b = bm::find_interval_start(bv1, j, pos);
19756  assert(b && pos == 0);
19757  } // for
19758 
19759  --to;
19760  for (bvect::size_type i = 0; i <= to; ++i, --to)
19761  {
19762  b = bm::find_interval_end(bv1, i, pos);
19763  assert(b && pos == to);
19764 
19765  b = bm::is_interval(bv1, i, to);
19766  assert(b);
19767 
19768  b = bm::find_interval_start(bv1, i, pos);
19769  assert(b && pos == i);
19770  b = bm::find_interval_start(bv1, to, pos);
19771  assert(b && pos == i);
19772 
19773  bv1.set(i, false);
19774  bv1.set(to, false);
19775 
19776  b = bm::find_interval_start(bv1, i, pos);
19777  assert(!b);
19778  b = bm::find_interval_start(bv1, to, pos);
19779  assert(!b);
19780 
19781  } // for i
19782 
19783  }}
19784 
19785  cout << "Check GAP ranges bvector" << endl;
19786  {{
19787  bvect bv1(bm::BM_GAP);
19788  bv1.set_range(0,1);
19789  bool one_test, any_one, is_int;
19790  one_test = bv1.is_all_one_range(0, 0);
19791  assert(one_test);
19792  any_one = bv1.any_range(0, 0);
19793  assert(any_one);
19794  is_int = bm::is_interval(bv1, 0, 0);
19795  assert(!is_int);
19796 
19797 
19798  one_test = bv1.is_all_one_range(0, 1);
19799  assert(one_test);
19800  any_one = bv1.any_range(0, 0);
19801  assert(any_one);
19802  is_int = bm::is_interval(bv1, 0, 1);
19803  assert(is_int);
19804 
19805  one_test = bv1.is_all_one_range(1, 1);
19806  assert(one_test);
19807  any_one = bv1.any_range(1, 1);
19808  assert(any_one);
19809  one_test = bv1.is_all_one_range(1, 2);
19810  assert(!one_test);
19811  any_one = bv1.any_range(1, 2);
19812  assert(any_one);
19813  is_int = bm::is_interval(bv1, 1, 2);
19814  assert(!is_int);
19815 
19816  is_int = bm::is_interval(bv1, 256, 65536);
19817  assert(!is_int);
19818 
19819  bv1.set_range(256, 65536);
19820  bv1.optimize();
19821  one_test = bv1.is_all_one_range(256, 65535);
19822  assert(one_test);
19823  any_one = bv1.any_range(256, 65535);
19824  assert(any_one);
19825  is_int = bm::is_interval(bv1, 256, 65536);
19826  assert(is_int);
19827  is_int = bm::is_interval(bv1, 255, 65536);
19828  assert(!is_int);
19829  is_int = bm::is_interval(bv1, 254, 65536);
19830  assert(!is_int);
19831  is_int = bm::is_interval(bv1, 257, 65536);
19832  assert(!is_int);
19833  is_int = bm::is_interval(bv1, 257, 65535);
19834  assert(!is_int);
19835 
19836 
19837  one_test = bv1.is_all_one_range(65535, 65535);
19838  assert(one_test);
19839  one_test = bv1.is_all_one_range(65535, 65536);
19840  assert(one_test);
19841 
19842  one_test = bv1.is_all_one_range(65535, 65537);
19843  assert(!one_test);
19844  any_one = bv1.any_range(65535, 65537);
19845  assert(any_one);
19846  any_one = bv1.any_range(65538, 65537);
19847  assert(!any_one);
19848  any_one = bv1.any_range(65538, bm::id_max-1);
19849  assert(!any_one);
19850 
19851  }}
19852 
19853  cout << "Check bit ranges bvector" << endl;
19854  {{
19855  bvect bv1;
19856  bv1[1] = true; bv1[2] = true;
19857  bool one_test, any_one;
19858  one_test = bv1.is_all_one_range(0, 0);
19859  assert(!one_test);
19860  any_one = bv1.any_range(0, 0);
19861  assert(!any_one);
19862  any_one = bv1.any_range(0, 1);
19863  assert(any_one);
19864 
19865  one_test = bv1.is_all_one_range(0, 1);
19866  assert(!one_test);
19867  one_test = bv1.is_all_one_range(2, 1);
19868  assert(one_test);
19869  any_one = bv1.any_range(2, 1);
19870  assert(any_one);
19871 
19872  one_test = bv1.is_all_one_range(258, 65530);
19873  assert(!one_test);
19874 
19875  for (bvect::size_type i = 256; i <= 65536; ++i)
19876  {
19877  bv1.set(i);
19878  }
19879  one_test = bv1.is_all_one_range(258, 65530);
19880  assert(one_test);
19881  any_one = bv1.any_range(258, 65530);
19882  assert(any_one);
19883  one_test = bv1.is_all_one_range(256, 65535);
19884  assert(one_test);
19885  one_test = bv1.is_all_one_range(65535, 65535);
19886  assert(one_test);
19887  one_test = bv1.is_all_one_range(65535, 65536);
19888  assert(one_test);
19889  one_test = bv1.is_all_one_range(65536, 65537);
19890  assert(!one_test);
19891  any_one = bv1.any_range(65535, 65537);
19892  assert(any_one);
19893  any_one = bv1.any_range(65538, 65537);
19894  assert(!any_one);
19895  any_one = bv1.any_range(65538, bm::id_max-1);
19896  assert(!any_one);
19897 
19898  }}
19899 
19900  cout << "Check set ranges" << endl;
19901  {{
19902  size_t fr_size = sizeof(from_arr) / sizeof(from_arr[0]);
19903  size_t to_size = sizeof(to_arr) / sizeof(to_arr[0]);
19904  assert(fr_size == to_size);
19905 
19906  for (unsigned t = 0; t < fr_size; ++t)
19907  {
19908  bvect::size_type from(from_arr[t]), to(to_arr[t]);
19909 
19910  {
19911  bvect bv1;
19912  bvect bv2(bm::BM_GAP);
19913 
19914  if (to - from < 65536*10)
19915  {
19916  for (bvect::size_type i = from; i <= to; ++i)
19917  bv1.set(i);
19918  }
19919  else
19920  {
19921  bv1.set_range(from, to);
19922  }
19923  bv2.set_range(from, to);
19924 
19925  bool one_test1 = bv1.is_all_one_range(from, to);
19926  bool one_test2 = bv2.is_all_one_range(from, to);
19927  assert(one_test1 == one_test2);
19928 
19929  bool any_one1 = bv1.any_range(from, to);
19930  bool any_one2 = bv2.any_range(from, to);
19931  assert(any_one1 == any_one2);
19932 
19933  if (from)
19934  {
19935  any_one1 = bv1.any_range(from-1, from);
19936  assert(any_one1 == any_one2);
19937  any_one2 = bv2.any_range(from-1, from);
19938  assert(any_one1 == any_one2);
19939  }
19940 
19941  if (to < bm::id_max-1)
19942  {
19943  any_one1 = bv1.any_range(to, to+1);
19944  assert(any_one1 == any_one2);
19945  any_one2 = bv2.any_range(to, to+1);
19946  assert(any_one1 == any_one2);
19947  }
19948 
19949  verify_all_one_ranges(bv1, false);
19950  verify_all_one_ranges(bv2, false);
19951 
19952  IntervalsCheck(bv1);
19953  IntervalsCheck(bv2);
19954  }
19955  } // for t
19956 
19957  }}
19958 
19959  cout << "---------------------------- Intervals_RangesTest() OK\n" << endl;
19960 }
19961 
19962 
19963 static
19965 {
19966  std::cout << "--------------------- KeepRangeTest()" << endl;
19967 
19968  {
19969  bvect bv;
19970  bv.keep_range(10, 100);
19971  assert(!bv.any());
19972  bv.keep_range(0, 0);
19973  assert(!bv.any());
19974  }
19975 
19976  {
19977  bvect bv;
19978  bv.invert();
19979  bv.keep_range(10, 20);
19980 
19981  assert(bv.count() == 11);
19982  assert(bv.count_range(10, 20) == 11);
19983  bv.keep_range(20, 10);
19984  assert(bv.count() == 11);
19985  assert(bv.count_range(10, 20) == 11);
19986 
19987  bv.keep_range(10, 10);
19988  assert(bv.test(10));
19989  assert(bv.count() == 1);
19990  assert(bv.count_range(10, 10) == 1);
19991  }
19992 
19993  {
19994  bvect bv{ 10, 256, bm::id_max / 2, bm::id_max - 1 };
19995  bv.optimize();
19996  bv.keep_range(bm::id_max / 2 - 100, bm::id_max / 2);
19997  assert(bv.count() == 1);
19998  assert(bv.test(bm::id_max / 2));
19999  }
20000 
20001 
20002  std::cout << "--------------------- KeepRangeTest() OK" << endl;
20003 }
20004 
20005 static
20007 {
20008  cout << "---------------------------- ExportTest..." << endl;
20009 
20010  {
20011  char buf[20] = {0,};
20012 
20013  buf[0] = 1;
20014  buf[1] = 1;
20015  buf[2]= (char)(1 << 1);
20016 
20017  bvect bv1;
20018  export_array(bv1, buf + 0, buf + 20);
20019 
20020  auto cnt = bv1.count();
20021  assert(cnt == 3);
20022  assert(bv1.test(0));
20023  assert(bv1.test(8));
20024  assert(bv1.test(17));
20025  }
20026 
20027  {
20028  char buf[65536*10] = {0,};
20029 
20030  buf[0] = 1;
20031  buf[1] = 1;
20032  buf[2]= (char)(1 << 1);
20033 
20034  bvect bv1;
20035  export_array(bv1, buf + 0, buf + 65536*10);
20036 
20037  assert(bv1.count() == 3);
20038  assert(bv1.test(0));
20039  assert(bv1.test(8));
20040  assert(bv1.test(17));
20041  }
20042 
20043  {
20044  short buf[20] = {0,};
20045 
20046  buf[0] = 1;
20047  buf[1] = 1;
20048  buf[2]= (char)(1 << 1);
20049 
20050  bvect bv1;
20051  export_array(bv1, buf + 0, buf + 20);
20052 
20053  assert(bv1.count() == 3);
20054  assert(bv1.test(0));
20055  assert(bv1.test(16));
20056  assert(bv1.test(33));
20057  }
20058 
20059  {
20060  int buf[20] = {0,};
20061 
20062  buf[0] = 1;
20063  buf[1] = 1;
20064  buf[2]= (char)(1 << 1);
20065 
20066  bvect bv1;
20067  export_array(bv1, buf + 0, buf + 20);
20068 
20069  assert(bv1.count() == 3);
20070  assert(bv1.test(0));
20071  assert(bv1.test(32));
20072  assert(bv1.test(65));
20073  }
20074 
20075 
20076  cout << "---------------------------- ExportTest Ok." << endl;
20077 }
20078 
20079 
20080 
20081 static
20083 {
20084  cout << "---------------------------- TestNibbleArr()" << endl;
20085 
20086  {
20087  unsigned char arr[256] = {0,};
20088  unsigned char v = 0;
20089  for (unsigned i = 0; i < 512; ++i, ++v)
20090  {
20091  if (v > 0xF)
20092  v = 0;
20093  bm::set_nibble(arr, i, v);
20094  auto vc = bm::get_nibble(arr, i);
20095  assert(vc == v);
20096  } // for
20097  v = 0;
20098  for (unsigned i = 0; i < 512; ++i, ++v)
20099  {
20100  if (v > 0xF)
20101  v = 0;
20102  auto vc = bm::get_nibble(arr, i);
20103  assert(vc == v);
20104  } // for
20105  }
20106 
20107  cout << "---------------------------- TestNibbleArr() OK" << endl;
20108 }
20109 
20110 static
20112 {
20113  cout << "---------------------------- TestHasZeroByte()" << endl;
20114 
20115  {
20116  bool b;
20117  b = bm::has_zero_byte_u64(0ULL);
20118  assert(b);
20119  b = bm::has_zero_byte_u64(1ULL);
20120  assert(b);
20121  for (unsigned i = 8; i < 64; i+=8)
20122  {
20123  b = bm::has_zero_byte_u64(1ULL << i);
20124  assert(b);
20125  }
20126  b = bm::has_zero_byte_u64(~0ULL);
20127  assert(!b);
20128  b = bm::has_zero_byte_u64(0x0101010101010101ULL);
20129  assert(!b);
20130  b = bm::has_zero_byte_u64(0x0101000101010101ULL);
20131  assert(b);
20132  b = bm::has_zero_byte_u64(0x8080808080808080ULL);
20133  assert(!b);
20134 
20135  b = bm::has_zero_byte_u64(~0ULL ^ 0xFFULL);
20136  assert(b);
20137  for (unsigned i = 8; i < 64; i+=8)
20138  {
20139  auto v = ~0ULL ^ (0xFFUL << i);
20140  cout << i << endl;
20141  cout << hex << v << endl;
20142  b = bm::has_zero_byte_u64(v);
20143  assert(b || v == ~0ULL);
20144  }
20145  }
20146 
20147  cout << "---------------------------- TestHasZeroByte() OK" << endl;
20148 }
20149 
20150 
20151 
20152 static
20154 {
20155  bm::word_t b1[bm::set_block_size]= {0,};
20156  bm::word_t b2[bm::set_block_size]= {0,};
20157  bm::word_t br[bm::set_block_size]= {0,};
20158 
20159  b1[0] = 1;
20160  b1[1] = 1;
20161  b2[0] = 1;
20162 
20163  bitblock_get_adapter bga1(b1);
20164  bitblock_get_adapter bga2(b2);
20165  bitblock_store_adapter bbsa(br);
20171  (bga1, bga2,and_func, bbsa);
20172 /*
20173  bit_recomb(bitblock_get_adapter(b1),
20174  bitblock_get_adapter(b2),
20175  bit_AND<bm::word_t>(),
20176  bitblock_store_adapter(br)
20177  );
20178 
20179  assert(br[0] == 1);
20180  for (int i = 1; i < bm::set_block_size; ++i)
20181  {
20182  assert(br[i] == 0);
20183  }
20184 
20185  bitblock_sum_adapter sa;
20186  bit_recomb(bitblock_get_adapter(b1),
20187  bitblock_get_adapter(b2),
20188  bit_COUNT_AND<bm::word_t>(),
20189  sa
20190  );
20191  assert(sa.sum() == 1);
20192 */
20193 }
20194 
20195 static
20196 void CheckBitList(const unsigned* bl1, unsigned bl1_cnt,
20197  const unsigned* bl2, unsigned bl2_cnt)
20198 {
20199  assert(bl1_cnt == bl2_cnt);
20200  for (unsigned i = 0; i < bl1_cnt; ++i)
20201  {
20202  assert(bl1[i] == bl2[i]);
20203  if (bl1[i] != bl2[i])
20204  {
20205  cerr << "BitList check failed!" << endl;
20206  exit(1);
20207  }
20208  }
20209 }
20210 
20211 static
20213 {
20214  cout << "---------------------------- BitForEachTest..." << endl;
20215 
20216  cout << "Testing BITSCAN variants: bit_list_4(), bitscan_popcnt().." << endl;
20217  {
20218  unsigned bit_list1[32];
20219  unsigned bit_list2[32];
20220  unsigned bit_list3[32];
20221  unsigned bit_list4[32];
20222  unsigned bit_list5[32];
20223  unsigned bit_list6[32];
20224 
20225 #ifdef __GNUC__
20226 #define BITSCAN_NIBGCC bm::bitscan_nibble_gcc
20227 #else
20228 #define BITSCAN_NIBGCC bm::bitscan_nibble
20229 #endif
20230  for (unsigned i = 0; i < 65536*10000; ++i)
20231  {
20232  unsigned bits1 = bm::bit_list(i, bit_list1);
20233  unsigned bits2 = bm::bit_list_4(i, bit_list2);
20234  unsigned bits3 = bm::bitscan_popcnt(i, bit_list3);
20235  unsigned bits4 = bm::bitscan_nibble(i, bit_list4);
20236  unsigned bits5 = BITSCAN_NIBGCC(i, bit_list5);
20237  unsigned bits6 = bm::bitscan_bsf(i, bit_list6);
20238  if (bits1 != bits2 || bits1 != bits3)
20239  {
20240  cout << "Bit for each test failed bit_cnt criteria!" << endl;
20241  exit(1);
20242  }
20243  assert (bits1 == bits4);
20244  assert (bits1 == bits5);
20245  assert (bits1 == bits6);
20246  for (unsigned j = 0; j < bits1; ++j)
20247  {
20248  if (bit_list1[j] != bit_list2[j] ||
20249  bit_list1[j] != bit_list3[j] ||
20250  bit_list1[j] != bit_list4[j] ||
20251  bit_list1[j] != bit_list5[j] ||
20252  bit_list1[j] != bit_list6[j]
20253  )
20254  {
20255  cout << "Bit for each check failed for w=" << i
20256  << " bit=" << j << endl;
20257  assert(0); exit(1);
20258  }
20259  }
20260  } // for
20261  }
20262 
20263  {
20264  cout << "testing bitscan_popcnt64()..." << endl;
20265 
20266  unsigned char bit_list[64];
20267  bm::id64_t w = 0;
20268  unsigned cnt;
20269 
20271  if (cnt)
20272  {
20273  cout << "bitscan_popcnt64 cnt for 0x00 failed " << cnt << endl;
20274  exit(1);
20275  }
20276 
20277  w = ~w; // 0xFFFFF...
20278 
20280  if (cnt != 64)
20281  {
20282  cout << "bitscan_popcnt64 cnt for 0xFFF failed " << cnt << endl;
20283  exit(1);
20284  }
20285  for (unsigned i = 0; i < cnt; ++i)
20286  {
20287  if (bit_list[i] != i)
20288  {
20289  cout << "bitscan_popcnt64 cnt at " << i << " != " << bit_list[i] << endl;
20290  exit(1);
20291  }
20292  } // for
20293 
20294  for (unsigned k = 63; k != 0; --k)
20295  {
20296  w <<= 1;
20298  if (cnt != k)
20299  {
20300  cout << "bitscan_popcnt64 cnt for " << w << " cnt=" << cnt << endl;
20301  exit(1);
20302  }
20303  cout << "[" << cnt << "]:";
20304  for (unsigned i = 0; i < cnt; ++i)
20305  {
20306  cout << (unsigned)bit_list[i] << ", ";
20307  } // for
20308  cout << endl;
20309  } // for
20310  }
20311 
20312  cout << "Stress 64-bit bitscan..." << endl;
20313  {
20314  unsigned bit_list3[64];
20315  unsigned bit_list6[64];
20316  for (bm::id64_t i = 0; i < 65536*10000; ++i)
20317  {
20318  bm::id64_t w = i | (i << 32);
20319  unsigned bits3 = bm::bitscan_popcnt64(w, bit_list3);
20320  unsigned bits6 = bm::bitscan_bsf64(w, bit_list6);
20321  CheckBitList(bit_list3, bits3, bit_list6, bits6);
20322  w = i;
20323  bits3 = bm::bitscan_popcnt64(w, bit_list3);
20324  bits6 = bm::bitscan_bsf64(w, bit_list6);
20325  CheckBitList(bit_list3, bits3, bit_list6, bits6);
20326  w = (i << 32);
20327  bits3 = bm::bitscan_popcnt64(w, bit_list3);
20328  bits6 = bm::bitscan_bsf64(w, bit_list6);
20329  CheckBitList(bit_list3, bits3, bit_list6, bits6);
20330  }
20331  }
20332 
20333 
20334  cout << "---------------------------- BitForEachTest Ok." << endl;
20335 }
20336 
20337 static
20339 {
20340  cout << "---------------------------- HMaskTest()..." << endl;
20341  {
20342  unsigned m;
20343  m = bm::compute_h64_mask(1 << 0);
20344  assert(m == 1);
20345  m = bm::compute_h64_mask(1 << 1);
20346  assert(m == 1);
20347  m = bm::compute_h64_mask(1 << 8);
20348  assert(m == (1<<1));
20349  m = bm::compute_h64_mask(1 << 16);
20350  assert(m == (1<<2));
20351  m = bm::compute_h64_mask(1ull << 24);
20352  assert(m == (1<<3));
20353  m = bm::compute_h64_mask(1ull << 32);
20354  assert(m == (1<<4));
20355  m = bm::compute_h64_mask(1ull << 40);
20356  assert(m == (1<<5));
20357  m = bm::compute_h64_mask(1ull << 48);
20358  assert(m == (1<<6));
20359  m = bm::compute_h64_mask(1ull << 56);
20360  assert(m == (1<<7));
20361  m = bm::compute_h64_mask((1ull << 48) | (1ull << 56));
20362  assert(m == ((1<<6)|(1<<7)));
20363  }
20364  cout << "---------------------------- HMaskTest()... OK" << endl;
20365 }
20366 
20367 
20368 static
20369 void Log2Test()
20370 {
20371  cout << "---------------------------- Log2 Test..." << endl;
20372 
20373  {
20374  unsigned l = bm::bit_scan_reverse32(~0u);
20375  cout << l << endl;
20376  assert(l == 31);
20377  l = bm::bit_scan_reverse(~0u);
20378  cout << l << endl;
20379  assert(l == 31);
20380  l = bm::bit_scan_reverse(~0ull);
20381  cout << l << endl;
20382  assert(l == 63);
20383  }
20384 
20385  {
20386  bm::id64_t v8 = 0x8000000000000000U;
20387  unsigned l = bm::bit_scan_reverse(v8);
20388  assert(l == 63);
20389  v8 = 0x4000000000000000U;
20390  l = bm::bit_scan_reverse(v8);
20391  assert(l == 62);
20392  }
20393 
20394  cout << "Stage 1" << endl;
20395  for (unsigned i = 1; i <= 65535; ++i)
20396  {
20397  unsigned l1 = bm::ilog2<unsigned short>((unsigned short)i);
20398  unsigned l2 = iLog2(i);
20399  unsigned l3 = ilog2_LUT<unsigned short>((unsigned short)i);
20400  unsigned l4 = bm::bit_scan_reverse(i);
20401  if (l1 != l2 || l2 != l3 || l2 != l4)
20402  {
20403  cout << "Log2 error for " << i << endl;
20404  cout << l2 << " " << l3 << endl;;
20405  exit(1);
20406  }
20407  }
20408  cout << "Stage 2" << endl;
20409  for (unsigned i = 1; i <= 10000*65535; ++i)
20410  {
20411  unsigned l1 = bm::ilog2<unsigned>(i);
20412  unsigned l2 = iLog2(i);
20413  unsigned l3 = ilog2_LUT<unsigned>(i);
20414  unsigned l4 = bm::bit_scan_reverse(i);
20415  if (l1 != l2 || l2 != l3 || l2 != l4)
20416  {
20417  cout << "Log2 error for " << i << endl;
20418  cout << l2 << " " << l3 << endl;;
20419  exit(1);
20420  }
20421  }
20422  cout << "Stage 3" << endl;
20423  unsigned v = 1;
20424  for (unsigned i = 1; i <= 31; ++i)
20425  {
20426  v |= 1;
20427  unsigned l1 = bm::ilog2<unsigned>(v);
20428  unsigned l2 = iLog2(v);
20429  unsigned l3 = ilog2_LUT<unsigned>(v);
20430  unsigned l4 = bm::bit_scan_reverse(v);
20431  if (l1 != l2 || l2 != l3 || l2 != l4)
20432  {
20433  cout << "Log2 error for " << i << endl;
20434  cout << l2 << " " << l3 << endl;;
20435  exit(1);
20436  }
20437 
20438  bm::id64_t v8 = v;
20439  v8 <<= 32;
20440  unsigned l5 = bm::bit_scan_reverse(v8);
20441  if ((l4 + 32) != l5)
20442  {
20443  cout << "Log2 error for " << v8 << " " << v << endl;
20444  cout << i << " " <<" " << l4 << " " << (l4+32) << " " << l5 << endl;;
20445  exit(1);
20446  }
20447 
20448  v <<= 1;
20449  }
20450  cout << "---------------------------- Log2 Test Ok." << endl;
20451 }
20452 
20453 
20454 static
20456 {
20457  cout << "---------------------------- LZCNT Test..." << endl;
20458 
20459  unsigned bsr;
20460  unsigned l = bm::count_leading_zeros(0);
20461  assert(l == 32);
20462 
20463  unsigned t = bm::count_trailing_zeros(0);
20464  assert(t == 32);
20465 
20466  l = bm::count_leading_zeros(2);
20467  unsigned bsf = bm::bit_scan_forward32(2);
20468  assert(bsf == 1);
20470  assert(t == 1);
20471 
20472  l = bm::count_leading_zeros(~0u);
20473  assert(l == 0);
20474  l = bm::count_leading_zeros(~0u >> 1u);
20475  assert(l == 1);
20476 
20477  unsigned clz = bm::count_leading_zeros_u32(~0u);
20478  assert(clz == 0);
20479  clz = bm::count_leading_zeros_u32(~0u >> 1);
20480  assert(clz == 1);
20481 
20482 
20483 
20484  unsigned mask = ~0u;
20485  for (unsigned i = 1; i; i <<= 1)
20486  {
20489  bsr = bm::bit_scan_reverse32(i);
20490  bsf = bm::bit_scan_forward32(i);
20492  assert(bsf == bsr);
20493  assert(l == 31 - bsf);
20494  assert(t == bsf);
20495  assert(clz == l);
20496 
20500  assert(l == 31 - bsr);
20501  mask >>= 1;
20502  }
20503 
20504  {
20505  bm::id64_t w = ~0ull;
20506  for (unsigned i = 0; i < 63; ++i)
20507  {
20508  unsigned lz = bm::count_leading_zeros_u64(w);
20509  assert(lz == i);
20510  w >>= 1;
20511  }
20512  }
20513 
20514  cout << "---------------------------- LZCNT Test..." << endl;
20515 }
20516 
20517 inline
20519 {
20520 #ifdef BMBMI1OPT
20521  return bmi1_select64_lz(val, rank);
20522 #else
20524 #endif
20525 }
20526 
20527 inline
20529 {
20530 #ifdef BMBMI1OPT
20531  return bmi1_select64_tz(val, rank);
20532 #else
20534 #endif
20535 }
20536 
20537 
20538 inline
20540 {
20541 #ifdef BMBMI2OPT
20542  return bmi2_select64_pdep(val, rank);
20543 #else
20545 #endif
20546 }
20547 
20548 
20549 // Returns the position of the rank'th 1. (rank = 0 returns the 1st 1)
20550 // Returns 64 if there are fewer than rank+1 1s.
20551 /*
20552 inline
20553 unsigned select64_pdep_tzcnt(bm::id64_t val, unsigned rank) {
20554  uint64_t i = 1ull << rank;
20555  asm("pdep %[val], %[mask], %[val]"
20556  : [val] "+r" (val)
20557  : [mask] "r" (i));
20558  asm("tzcnt %[bit], %[index]"
20559  : [index] "=r" (i)
20560  : [bit] "g" (val)
20561  : "cc");
20562  return unsigned(i);
20563 }
20564 */
20565 
20566 
20567 
20568 static
20570 {
20571  cout << "---------------------------- SELECT Test" << endl;
20572 
20573  {
20574  bm::id64_t w64 = 1;
20575  unsigned idx = bm::word_select64_linear(w64, 1);
20576  unsigned idx0 = bm::word_select64_bitscan_popcnt(w64, 1);
20577  unsigned idx1, idx4, idx3, idx5;
20578  assert(idx == 0);
20579  assert(idx0 == idx);
20580  idx4 = proxy_bmi1_select64_lz(w64, 1);
20581  assert(idx4 == idx);
20582  idx5 = proxy_bmi1_select64_tz(w64, 1);
20583  assert(idx5 == idx);
20584 
20585 
20586  idx3 = proxy_bmi2_select64_pdep(w64, 1);
20587  std::cerr << idx3 << " " << idx << endl;
20588  assert(idx3 == idx);
20589 
20590 // idx4 = word_select64_part(w64, 1);
20591 // assert(idx4 == idx);
20592 
20593 /*
20594  w64 = 1 | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 7);
20595  w64 = (1ull << 63) | 1;
20596  idx3 = avx2_select64(w64, 2);
20597  w64 = (1ull << 63) | 8 | 2;
20598  idx3 = avx2_select64(w64, 2);
20599 
20600  exit(1);
20601 
20602  w64 = ~0u;
20603  idx3 = avx2_select64(w64, 1);
20604  idx3 = avx2_select64(w64, 2);
20605  w64 = 16 | 2 | 1;
20606  idx3 = avx2_select64(w64, 1);
20607  idx3 = avx2_select64(w64, 2);
20608  idx3 = avx2_select64(w64, 3);
20609  w64 = w64 << 1;
20610  idx3 = avx2_select64(w64, 1);
20611  idx3 = avx2_select64(w64, 2);
20612  idx3 = avx2_select64(w64, 3);
20613  idx3 = avx2_select64(w64, 4);
20614  idx3 = avx2_select64(w64, 5);
20615  exit(1);
20616 */
20617 
20618  for (unsigned sel = 1; sel <= 64; ++sel)
20619  {
20620  idx = bm::word_select64_linear(~0ull, sel);
20621  assert(idx == sel-1);
20622  idx0 = word_select64_bitscan_popcnt(~0ull, sel);
20623  assert(idx0 == idx);
20624  idx4 = proxy_bmi1_select64_lz(~0ull, sel);
20625  assert(idx4 == idx);
20626  idx5 = proxy_bmi1_select64_tz(~0ull, sel);
20627  assert(idx5 == idx);
20628  idx3 = proxy_bmi2_select64_pdep(~0ull, sel);
20629  assert(idx3 == idx);
20630  }
20631  for (unsigned sel = 1; sel <= 32; ++sel)
20632  {
20633  idx = bm::word_select64_linear(~0u, sel);
20634  idx0 = word_select32_bitscan_popcnt(~0u, sel);
20635  assert(idx == idx0);
20636  unsigned idx_tz = bm::word_select32_bitscan_tz(~0u, sel);
20637  assert(idx_tz == idx);
20638  }
20639 
20640  for (idx = 0; w64; w64 <<= 1)
20641  {
20642  idx0 = bm::word_select64_linear(w64, 1);
20643  assert(idx0 == idx);
20644  idx1 = word_select64_bitscan_popcnt(w64, 1);
20645  assert(idx1 == idx0);
20646  idx4 = proxy_bmi1_select64_lz(w64, 1);
20647  assert(idx4 == idx);
20648  idx5 = proxy_bmi1_select64_tz(w64, 1);
20649  assert(idx5 == idx);
20650  idx3 = proxy_bmi2_select64_pdep(w64, 1);
20651  assert(idx3 == idx);
20652  unsigned idx_tz = bm::word_select64_bitscan_tz(w64, 1);
20653  assert(idx_tz == idx);
20654 
20655  ++idx;
20656  }
20657  }
20658 
20659  {
20660  cout << "\nSELECT stress test." << std::endl;
20661  const unsigned test_size = 1000000 * 10;
20662  for (unsigned i = 1; i < test_size; ++i)
20663  {
20664  bm::id64_t w64 = i;
20665  bm::id64_t w64_1 = (w64 << 32) | w64;
20666 
20667  unsigned count = bm::word_bitcount64(w64);
20668  for (unsigned j = 1; j <= count; ++j)
20669  {
20670  unsigned idx0 = bm::word_select64_linear(w64, j);
20671  unsigned idx1 = word_select64_bitscan_popcnt(w64, j);
20672  assert(idx0 == idx1);
20673  unsigned idx4 = proxy_bmi1_select64_lz(w64, j);
20674  assert(idx4 == idx1);
20675  unsigned idx5 = proxy_bmi1_select64_tz(w64, j);
20676  assert(idx5 == idx1);
20677  unsigned idx3 = proxy_bmi2_select64_pdep(w64, j);
20678  assert(idx3 == idx1);
20679  unsigned idx_tz = bm::word_select64_bitscan_tz(w64, j);
20680  assert(idx_tz == idx1);
20681  }
20682 
20684  for (unsigned j = 1; j <= count; ++j)
20685  {
20686  unsigned idx1 = word_select64_bitscan_popcnt(i, j);
20687  unsigned idx2 = word_select32_bitscan_popcnt(i, j);
20688  assert(idx1 == idx2);
20689  unsigned idx_tz = bm::word_select32_bitscan_tz(i, j);
20690  assert(idx_tz == idx1);
20691  }
20692 
20693  count = bm::word_bitcount64(w64_1);
20694  for (unsigned j = 1; j <= count; ++j)
20695  {
20696  unsigned idx0 = bm::word_select64_linear(w64_1, j);
20697  unsigned idx1 = bm::word_select64_bitscan_popcnt(w64_1, j);
20698  assert(idx0 == idx1);
20699  unsigned idx4 = proxy_bmi1_select64_lz(w64_1, j);
20700  assert(idx4 == idx1);
20701  unsigned idx5 = proxy_bmi1_select64_tz(w64_1, j);
20702  assert(idx5 == idx1);
20703  unsigned idx3 = proxy_bmi2_select64_pdep(w64_1, j);
20704  assert(idx3 == idx1);
20705  unsigned idx_tz = bm::word_select64_bitscan_tz(w64_1, j);
20706  assert(idx_tz == idx1);
20707  }
20708 
20709  if (!is_silent)
20710  if (i % 1000000 == 0)
20711  cout << "\r" << i << " - " << test_size << std::flush;
20712  }
20713  }
20714 
20715  {
20716  cout << "\nSELECT bit-block test." << std::endl;
20717  unsigned cnt;
20718 
20719  BM_DECLARE_TEMP_BLOCK(tb1);
20720  for (unsigned i = 0; i < bm::set_block_size; ++i)
20721  {
20722  tb1.b_.w32[i] = ~0u;
20723  }
20724  unsigned test_size = 65535;
20725  for (unsigned i = 1; i <= 65535; ++i)
20726  {
20727  unsigned idx;
20728  unsigned rank = bm::bit_find_rank(tb1, i, 0, idx);
20729  assert(rank == 0);
20730  cnt = bm::bit_block_calc_count_to(tb1, i-1);
20731  assert(idx == cnt-1);
20732 
20733  for (unsigned j = 65535; j > i; --j)
20734  {
20736  if (cnt)
20737  {
20738  rank = bm::bit_find_rank(tb1, cnt, i, idx);
20739  assert(rank == 0);
20740  assert(idx == j);
20741  }
20742  }
20743 
20744  if (!is_silent)
20745  if (i % 128 == 0)
20746  cout << "\r" << i << "-" << test_size << std::flush;
20747  }
20748  }
20749 
20750  cout << "\n---------------------------- SELECT Test OK" << endl;
20751 }
20752 
20753 
20754 static
20756 {
20757  cout << "---------------------------- BitEncoderTest" << endl;
20758 
20759  unsigned char buf[1024 * 200] = {0, };
20760 
20761  {
20762  bm::encoder enc(buf, sizeof(buf));
20763  bm::bit_out<bm::encoder> bout(enc);
20764 
20765  unsigned value = 1024 + 3;
20766  bout.put_bits(value, 32);
20767  value = 1024 + 5;
20768  bout.put_bits(value, 32);
20769 
20770  bout.flush();
20771 
20772  bm::decoder dec(buf);
20773  bm::bit_in<bm::decoder> bin(dec);
20774  value = bin.get_bits(32);
20775  assert(value == 1024 + 3);
20776  value = bin.get_bits(32);
20777  assert(value == 1024 + 5);
20778  }
20779 
20780  {
20781  unsigned bits = 1;
20782  for (unsigned i = 1; i < (1u << 31u); i <<= 1, bits++)
20783  {
20784  bm::encoder enc(buf, sizeof(buf));
20785  bm::bit_out<bm::encoder> bout(enc);
20786 
20787  for (unsigned j = 0; j < 160; ++j)
20788  {
20789  bout.put_bits(i, bits);
20790  }
20791  bout.flush();
20792 
20793  bm::decoder dec(buf);
20794  bm::bit_in<bm::decoder> bin(dec);
20795  for (unsigned j = 0; j < 160; ++j)
20796  {
20797  unsigned value = bin.get_bits(bits);
20798  if (value != i)
20799  {
20800  cerr << "Invalid encoding for i=" << i
20801  << " value=" << value << " bits=" << bits << endl;
20802  exit(1);
20803  }
20804  }
20805 
20806  } // for
20807  }
20808 
20809  {
20810  bm::encoder enc(buf, sizeof(buf));
20811  bm::bit_out<bm::encoder> bout(enc);
20812 
20813  for (unsigned i = 1; i < 65536; ++i)
20814  {
20815  unsigned bits = bm::bit_scan_reverse(i)+1;
20816  bout.put_bits(i, bits);
20817  } // for
20818  bout.flush();
20819 
20820  bm::decoder dec(buf);
20821  bm::bit_in<bm::decoder> bin(dec);
20822  for (unsigned i = 1; i < 65536; ++i)
20823  {
20824  unsigned bits = bm::bit_scan_reverse(i)+1;
20825  unsigned value = bin.get_bits(bits);
20826  if (value != i)
20827  {
20828  cerr << "2. Invalid encoding for i=" << i
20829  << " value=" << value << " bits=" << bits << endl;
20830  exit(1);
20831  }
20832 
20833  } // for
20834 
20835  }
20836 
20837  // test for 24-48-bit encode
20838  {
20839  bm::encoder enc(buf, sizeof(buf));
20840  enc.put_24(0xFFFFFF);
20841  enc.put_24(0xFEAAFE);
20842  enc.put_48(0xF0FAFFBEEFFFUL);
20843  enc.put_8(0);
20844 
20845  assert(enc.size() == 6+6+1);
20846 
20847  bm::decoder dec(buf);
20848  auto v1 = dec.get_24();
20849  assert(v1 == 0xFFFFFF);
20850  auto v2 = dec.get_24();
20851  assert(v2 == 0xFEAAFE);
20852  auto v3 = dec.get_48();
20853  assert(v3 == 0xF0FAFFBEEFFFUL);
20854 
20855  unsigned char e = dec.get_8();
20856  assert(!e);
20857  }
20858 
20859  // test h64
20860  {
20861  bm::encoder enc(buf, sizeof(buf));
20862  enc.put_h64(0xFF);
20863  enc.put_h64(0xF0FAFFBEEFFFUL);
20864 
20865  bm::decoder dec(buf);
20866  auto v1 = dec.get_h64();
20867  assert(v1 == 0xFF);
20868  auto v2 = dec.get_h64();
20869  assert(v2 == 0xF0FAFFBEEFFFUL);
20870  }
20871 
20872 
20873 
20874  cout << "---------------------------- BitEncoderTest" << endl;
20875 }
20876 
20877 /// Random numbers test
20878 template<typename V>
20879 unsigned generate_inter_test_linear(V* arr, unsigned inc, unsigned target_size)
20880 {
20881  V maskFF = (V)~V(0u);
20882 
20883 
20884  if (inc < 2 || inc > 65535)
20885  inc = 1;
20886 
20887  unsigned start = 1;
20888  unsigned sz = 0;
20889  while (sz < target_size)
20890  {
20891  arr[sz++] = V(start);
20892  if (inc + start >= maskFF)
20893  {
20894  arr[sz++] = maskFF;
20895  break;
20896  }
20897  start += inc;
20898  if (start < arr[sz-1])
20899  break;
20900 
20901  } // while
20902  return sz;
20903 }
20904 
20905 
20906 /// Random numbers test
20907 template<typename V>
20908 unsigned generate_inter_test(V* arr, unsigned inc_factor, unsigned target_size)
20909 {
20910  V maskFF = (V)~V(0u);
20911 
20912  if (inc_factor < 2)
20913  inc_factor = 65535;
20914 
20915  unsigned start = (unsigned) rand() % 256;
20916  if (!start)
20917  start = 1;
20918  unsigned sz = 0;
20919  while (sz < target_size)
20920  {
20921  arr[sz++] = V(start);
20922 
20923  unsigned inc = unsigned(rand()) % inc_factor;
20924  if (!inc)
20925  inc = 1;
20926  start += inc;
20927  if (start >= maskFF)
20928  break;
20929  } // while
20930 
20931  for(unsigned i = 1; i < sz; ++i)
20932  {
20933  if (arr[i-1] >= arr[i])
20934  {
20935  return i;
20936  }
20937  }
20938  return sz;
20939 }
20940 
20941 
20942 static
20944 {
20945  cout << "---------------------------- InterpolativeCodingTest() " << endl;
20946 
20947  unsigned char buf[1024 * 200] = {0, };
20948  unsigned char buf2[1024 * 200] = {0, };
20949  const bm::gap_word_t arr1[] = { 3, 4, 7, 13, 14, 15, 21, 25, 36, 38, 54, 62 };
20950  const bm::word_t arr2[] = { 30, 44, 78, 130, 140, 150, 210, 250, 3600, 3800, 540001, 620000258 };
20951 
20952  unsigned sz, sz2;
20953  {
20954  bm::encoder enc(buf, sizeof(buf));
20955  bm::bit_out<bm::encoder> bout(enc);
20956 
20957  sz = sizeof(arr1)/sizeof(arr1[0])-1;
20958  bout.bic_encode_u16_rg(arr1, sz, 0, 62);
20959 
20960  bout.flush();
20961  }
20962  {
20963  bm::encoder enc(buf2, sizeof(buf2));
20964  bm::bit_out<bm::encoder> bout(enc);
20965 
20966  sz2 = sizeof(arr2)/sizeof(arr2[0])-1;
20967  bout.bic_encode_u32_cm(arr2, sz2, 0, 620000258);
20968  bout.flush();
20969  }
20970 
20971  {
20972  decoder dec(buf);
20973  bm::bit_in<decoder> bin(dec);
20974 
20975  bm::gap_word_t arr2c[256] = {0, };
20976  bin.bic_decode_u16_rg(&arr2c[0], sz, 0, 62);
20977  for (unsigned i = 0; i < sz; ++i)
20978  {
20979  assert(arr1[i] == arr2c[i]);
20980  }
20981  }
20982  {
20983  decoder dec(buf2);
20984  bm::bit_in<decoder> bin(dec);
20985 
20986  bm::word_t arr2c[256] = {0, };
20987  bin.bic_decode_u32_cm(&arr2c[0], sz2, 0, 620000258);
20988  for (unsigned i = 0; i < sz2; ++i)
20989  {
20990  assert(arr2[i] == arr2c[i]);
20991  }
20992  }
20993 
20994  // -------------------------------------------------------
20995  cout << "BIC encode-decode U16 unit test" << endl;
20996  {
20997  bm::encoder enc(buf, sizeof(buf));
20998  bm::bit_out<bm::encoder> bout(enc);
20999 
21000  sz = sizeof(arr1)/sizeof(arr1[0])-1;
21001  bout.bic_encode_u16_cm(arr1, sz, 0, 62);
21002 
21003  bout.flush();
21004  }
21005  {
21006  decoder dec(buf);
21007  bm::bit_in<decoder> bin(dec);
21008 
21009  bm::gap_word_t arr2c[256] = {0, };
21010  bin.bic_decode_u16_cm(&arr2c[0], sz, 0, 62);
21011  for (unsigned i = 0; i < sz; ++i)
21012  {
21013  assert(arr1[i] == arr2c[i]);
21014  }
21015  }
21016 
21017  // -------------------------------------------------------
21018 
21019  cout << "\nu16 interpolated cm encoding Stress..." << endl;
21020  {
21021  const unsigned code_repeats = 1000000;
21022  const unsigned test_size = 12000;
21023  vector<gap_word_t> sa; sa.resize(test_size);
21024  vector<gap_word_t> da; da.resize(test_size);
21025 
21026  bm::gap_word_t* src_arr=&sa[0];
21027  bm::gap_word_t* dst_arr = &da[0];
21028 
21029  std::chrono::time_point<std::chrono::steady_clock> s;
21030  std::chrono::time_point<std::chrono::steady_clock> f;
21031  s = std::chrono::steady_clock::now();
21032 
21033  cout << " linear pattern" << endl;
21034  for (unsigned k = 0; k < code_repeats; ++k)
21035  {
21036  unsigned inc = (unsigned)rand()%(65536*256);
21037  if (k == 0)
21038  inc = 1;
21039  sz = generate_inter_test_linear(src_arr, inc, test_size);
21040  assert(sz);
21041  assert(src_arr[0]);
21042  {
21043  bm::encoder enc(buf, sizeof(buf));
21044  bm::bit_out<bm::encoder> bout(enc);
21045 
21046  bout.bic_encode_u16_cm(src_arr, sz-1, 0, src_arr[sz-1]);
21047  bout.flush();
21048  auto ssz = enc.size();
21049  assert(ssz < sizeof(buf));
21050  }
21051  {
21052  decoder dec(buf);
21053  bm::bit_in<decoder> bin(dec);
21054 
21055  bin.bic_decode_u16_cm(&dst_arr[0], sz-1, 0, src_arr[sz-1]);
21056  dst_arr[sz-1]=src_arr[sz-1];
21057  for (unsigned i = 0; i < sz; ++i)
21058  {
21059  assert(src_arr[i] == dst_arr[i]);
21060  if (i)
21061  {
21062  assert(src_arr[i-1] < src_arr[i]);
21063  }
21064  }
21065  }
21066  if ((k & 0xFFFF) == 0)
21067  {
21068  f = std::chrono::steady_clock::now();
21069  auto diff = f - s;
21070  auto d = std::chrono::duration <double, std::milli> (diff).count();
21071 
21072  if (!is_silent)
21073  cout << "\r" << k << "-" << code_repeats << " (" << d << "ms)" << flush;
21074  s = std::chrono::steady_clock::now();
21075  }
21076  }
21077 
21078  cout << "\n random pattern" << endl;
21079  for (unsigned k = 0; k < code_repeats; ++k)
21080  {
21081  sz = generate_inter_test(src_arr, k, test_size);
21082  if (sz < 3)
21083  continue;
21084 
21085  assert(sz);
21086  assert(src_arr[0]);
21087  {
21088  bm::encoder enc(buf, sizeof(buf));
21089  bm::bit_out<bm::encoder> bout(enc);
21090 
21091  bout.bic_encode_u16_cm(src_arr, sz, 0, src_arr[sz-1]);
21092  bout.flush();
21093  }
21094  {
21095  decoder dec(buf);
21096  bm::bit_in<decoder> bin(dec);
21097 
21098  bin.bic_decode_u16_cm(&dst_arr[0], sz, 0, src_arr[sz-1]);
21099  //dst_arr[sz-1]=src_arr[sz-1];
21100  for (unsigned i = 0; i < sz; ++i)
21101  {
21102  if (i)
21103  {
21104  assert(src_arr[i-1] < src_arr[i]);
21105  }
21106  assert(src_arr[i] == dst_arr[i]);
21107  }
21108  }
21109  if ((k & 0xFFF) == 0)
21110  {
21111  f = std::chrono::steady_clock::now();
21112  auto diff = f - s;
21113  auto d = std::chrono::duration <double, std::milli> (diff).count();
21114 
21115  if (!is_silent)
21116  cout << "\r" << k << "-" << code_repeats << " (" << d << "ms)" << flush;
21117  s = std::chrono::steady_clock::now();
21118  }
21119  } // for k
21120 
21121  }
21122 
21123  // -------------------------------------------------------
21124 
21125  cout << "\nu32 interpolated cm encoding Stress..." << endl;
21126  {
21127  const unsigned code_repeats = 1000000;
21128  const unsigned test_size = 12000;
21129  vector<unsigned> sa; sa.resize(test_size);
21130  vector<unsigned> da; da.resize(test_size);
21131 
21132  bm::word_t* src_arr=&sa[0];
21133  bm::word_t* dst_arr = &da[0];
21134 
21135  std::chrono::time_point<std::chrono::steady_clock> s;
21136  std::chrono::time_point<std::chrono::steady_clock> f;
21137  s = std::chrono::steady_clock::now();
21138 
21139  cout << " linear pattern" << endl;
21140  for (unsigned k = 0; k < code_repeats; ++k)
21141  {
21142  unsigned inc = (unsigned)rand()%(65536*256);
21143  if (k == 0)
21144  inc = 1;
21145  sz = generate_inter_test_linear(src_arr, inc, test_size);
21146  assert(sz);
21147  assert(src_arr[0]);
21148  {
21149  bm::encoder enc(buf, sizeof(buf));
21150  bm::bit_out<bm::encoder> bout(enc);
21151 
21152  bout.bic_encode_u32_cm(src_arr, sz-1, 0, src_arr[sz-1]);
21153  bout.flush();
21154  auto ssz = enc.size();
21155  assert(ssz < sizeof(buf));
21156  }
21157  {
21158  decoder dec(buf);
21159  bm::bit_in<decoder> bin(dec);
21160 
21161  bin.bic_decode_u32_cm(&dst_arr[0], sz-1, 0, src_arr[sz-1]);
21162  dst_arr[sz-1]=src_arr[sz-1];
21163  for (unsigned i = 0; i < sz; ++i)
21164  {
21165  assert(src_arr[i] == dst_arr[i]);
21166  if (i)
21167  {
21168  assert(src_arr[i-1] < src_arr[i]);
21169  }
21170  }
21171  }
21172  if ((k & 0xFFFF) == 0)
21173  {
21174  f = std::chrono::steady_clock::now();
21175  auto diff = f - s;
21176  auto d = std::chrono::duration <double, std::milli> (diff).count();
21177 
21178  if (!is_silent)
21179  cout << "\r" << k << "-" << code_repeats << " (" << d << "ms)" << flush;
21180  s = std::chrono::steady_clock::now();
21181  }
21182  }
21183 
21184  cout << "\n random pattern" << endl;
21185  for (unsigned k = 0; k < code_repeats; ++k)
21186  {
21187  sz = generate_inter_test(src_arr, k, test_size);
21188  if (sz < 3)
21189  continue;
21190 
21191  assert(sz);
21192  assert(src_arr[0]);
21193  {
21194  bm::encoder enc(buf, sizeof(buf));
21195  bm::bit_out<bm::encoder> bout(enc);
21196 
21197  bout.bic_encode_u32_cm(src_arr, sz, 0, src_arr[sz-1]);
21198  bout.flush();
21199  }
21200  {
21201  decoder dec(buf);
21202  bm::bit_in<decoder> bin(dec);
21203 
21204  bin.bic_decode_u32_cm(&dst_arr[0], sz, 0, src_arr[sz-1]);
21205  //dst_arr[sz-1]=src_arr[sz-1];
21206  for (unsigned i = 0; i < sz; ++i)
21207  {
21208  if (i)
21209  {
21210  assert(src_arr[i-1] < src_arr[i]);
21211  }
21212  assert(src_arr[i] == dst_arr[i]);
21213  }
21214  }
21215  if ((k & 0xFFF) == 0)
21216  {
21217  f = std::chrono::steady_clock::now();
21218  auto diff = f - s;
21219  auto d = std::chrono::duration <double, std::milli> (diff).count();
21220 
21221  if (!is_silent)
21222  cout << "\r" << k << "-" << code_repeats << " (" << d << "ms)" << flush;
21223  s = std::chrono::steady_clock::now();
21224  }
21225  } // for k
21226 
21227  }
21228 
21229  cout << "\nu16 interpolated encoding Stress..." << endl;
21230  {
21231  const unsigned code_repeats = 1000000;
21232  const unsigned test_size = 65536;
21233  vector<bm::gap_word_t> sa; sa.resize(test_size);
21234  vector<bm::gap_word_t> da; da.resize(test_size);
21235 
21236  bm::gap_word_t* src_arr= &sa[0];
21237  bm::gap_word_t* dst_arr= &da[0];
21238 
21239  std::chrono::time_point<std::chrono::steady_clock> s;
21240  std::chrono::time_point<std::chrono::steady_clock> f;
21241  s = std::chrono::steady_clock::now();
21242 
21243  cout << " linear pattern" << endl;
21244 // unsigned inc = rand()%128;
21245 // sz = generate_inter_test_linear(src_arr, inc);
21246  for (unsigned k = 0; k < code_repeats; ++k)
21247  {
21248  unsigned inc = (unsigned)rand()%128;
21249  if (k == 0)
21250  inc = 1;
21251 
21252  sz = generate_inter_test_linear(src_arr, inc, 65536);
21253  assert(sz);
21254  assert(src_arr[0]);
21255  if(src_arr[sz-1]<65535)
21256  src_arr[sz-1]=65535;
21257  {
21258  bm::encoder enc(buf, sizeof(buf));
21259  bm::bit_out<bm::encoder> bout(enc);
21260 
21261  bout.bic_encode_u16_rg(src_arr, sz, 0, 65535);
21262  bout.flush();
21263  }
21264  {
21265  decoder dec(buf);
21266  bm::bit_in<decoder> bin(dec);
21267 
21268  bin.bic_decode_u16_rg(&dst_arr[0], sz, 0, 65535);
21269  //dst_arr[sz-1]=65535;
21270  for (unsigned i = 0; i < sz; ++i)
21271  {
21272  assert(src_arr[i] == dst_arr[i]);
21273  }
21274  }
21275  if ((k & 0xFFFF) == 0)
21276  {
21277  f = std::chrono::steady_clock::now();
21278  auto diff = f - s;
21279  auto d = std::chrono::duration <double, std::milli> (diff).count();
21280 
21281  if (!is_silent)
21282  cout << "\r" << k << "-" << code_repeats << " (" << d << "ms)" << flush;
21283  s = std::chrono::steady_clock::now();
21284  }
21285  }
21286  cout << " random pattern" << endl;
21287 
21288  for (unsigned k = 0; k < code_repeats; ++k)
21289  {
21290  sz = generate_inter_test(src_arr, k, 65536);
21291  if (sz < 3)
21292  continue;
21293  assert(sz);
21294  assert(src_arr[0]);
21295  assert(src_arr[sz-1]<=65535);
21296  {
21297  bm::encoder enc(buf, sizeof(buf));
21298  bm::bit_out<bm::encoder> bout(enc);
21299 
21300  bout.bic_encode_u16_rg(src_arr, sz, 0, 65535);
21301  bout.flush();
21302  }
21303  {
21304  decoder dec(buf);
21305  bm::bit_in<decoder> bin(dec);
21306 
21307  bin.bic_decode_u16_rg(&dst_arr[0], sz, 0, 65535);
21308  //dst_arr[sz-1]=65535;
21309  for (unsigned i = 0; i < sz; ++i)
21310  {
21311  assert(src_arr[i] == dst_arr[i]);
21312  }
21313  }
21314  if (!is_silent)
21315  if ((k & 0xFFFF) == 0)
21316  cout << "\r" << k << "-" << code_repeats << flush;
21317  } // for k
21318 
21319  }
21320 
21321 
21322  cout << "---------------------------- InterpolativeCodingTest() OK " << endl;
21323 }
21324 
21325 static
21327 {
21328  cout << "---------------------------- GammaEncoderTest" << endl;
21329 
21330 
21331  unsigned char buf1[2048 * 4] = {0, };
21332 
21333  cout << "Stage 1" << endl;
21334 
21335  {
21336  encoder enc(buf1, sizeof(buf1));
21337  typedef bit_out<encoder> TBitIO;
21338  bit_out<encoder> bout(enc);
21340  gamma(65534);
21341  }
21342 
21343  {
21344  decoder dec(buf1);
21345  typedef bit_in<decoder> TBitIO;
21346  bit_in<decoder> bin(dec);
21348 
21349  gap_word_t value = gamma();
21350  if (value != 65534)
21351  {
21352  cout << "Gamma decoder error for value=" << value << endl;
21353  exit(1);
21354  }
21355  }
21356 
21357 
21358  {
21359  encoder enc(buf1, sizeof(buf1));
21360  typedef bit_out<encoder> TBitIO;
21361  bit_out<encoder> bout(enc);
21363 
21364  for (gap_word_t i = 1; i < 15; ++i)
21365  {
21366  gamma(i);
21367  }
21368  }
21369 
21370  {
21371  decoder dec(buf1);
21372  typedef bit_in<decoder> TBitIO;
21373  bit_in<decoder> bin(dec);
21375 
21376  for (gap_word_t i = 1; i < 15; ++i)
21377  {
21378  gap_word_t value = gamma();
21379  if (value != i)
21380  {
21381  cout << "Gamma decoder error for " << i << " value=" << value << endl;
21382  exit(1);
21383  }
21384  }
21385 
21386  }
21387 
21388  cout << "Stage 2" << endl;
21389 
21390  for (unsigned i = 0; i < 256; ++i)
21391  {
21392  gap_word_t short_block[64] = {0,};
21393 
21394  {
21395  encoder enc(buf1, sizeof(buf1));
21396  typedef bit_out<encoder> TBitIO;
21397  bit_out<encoder> bout(enc);
21399 
21400 
21401  for (unsigned j = 0; j < 64; ++j)
21402  {
21403  gap_word_t a = gap_word_t(rand() % 65535);
21404  if (!a) a = 65535; // 0 is illegal
21405  gap_word_t value = short_block[j] = a;
21406  gamma(value);
21407  } // for
21408  }
21409 
21410  {
21411  decoder dec(buf1);
21412  typedef bit_in<decoder> TBitIO;
21413  bit_in<decoder> bin(dec);
21415 
21416  for (unsigned j = 0; j < 64; ++j)
21417  {
21418  gap_word_t value = short_block[j];
21419  gap_word_t r = gamma();
21420  if (r != value)
21421  {
21422  cout << "Gamma encoding failure for value=" << value << " gamma=" << r << endl;
21423  exit(1);
21424  }
21425  } // for
21426  }
21427  }
21428 
21429 
21430  cout << "Stage 3" << endl;
21431 
21432  unsigned code_value = 65535;
21433  for (unsigned i = 0; i < 10000; ++i)
21434  {
21435  gap_word_t short_block[1000] = {0,};
21436 
21437  {
21438  encoder enc(buf1, sizeof(buf1));
21439  typedef bit_out<encoder> TBitIO;
21440  bit_out<encoder> bout(enc);
21442 
21443  for (unsigned j = 0; j < 1000; ++j)
21444  {
21445  gap_word_t a = (unsigned short)code_value;
21446  if (!a)
21447  {
21448  code_value = a = 65535;
21449  }
21450 
21451  gap_word_t value = short_block[j] = a;
21452  gamma(value);
21453  --code_value;
21454  } // for
21455  }
21456 
21457  {
21458  decoder dec(buf1);
21459  typedef bit_in<decoder> TBitIO;
21460  bit_in<decoder> bin(dec);
21462 
21463  for (unsigned j = 0; j < 1000; ++j)
21464  {
21465  gap_word_t value = short_block[j];
21466  gap_word_t r = gamma();
21467  if (r != value)
21468  {
21469  cout << "Gamma encoding failure for value=" << value << " gamma=" << r << endl;
21470  exit(1);
21471  }
21472  } // for
21473  }
21474  }
21475 
21476 
21477  cout << "---------------------------- GammaEncoderTest Ok." << endl;
21478 
21479 }
21480 
21481 template<class SV, class Vect>
21482 bool CompareSparseVector(const SV& sv, const Vect& vect,
21483  bool interval_filled = false,
21484  bool detailed = true)
21485 {
21486 
21487  if (vect.size() != sv.size())
21488  {
21489  cerr << "Sparse vector size test failed!" << vect.size() << "!=" << sv.size() << endl;
21490  assert(0);
21491  return false;
21492  }
21493 
21494  if (sv.is_nullable())
21495  {
21496  const typename SV::bvector_type* bv_null = sv.get_null_bvector();
21497  assert(bv_null);
21498  unsigned non_null_cnt = bv_null->count();
21499  if (vect.size() != non_null_cnt)
21500  {
21501  if (!interval_filled)
21502  {
21503  cerr << "NULL vector count failed." << non_null_cnt << " size=" << vect.size() << endl;
21504  assert(0);
21505  exit(1);
21506  }
21507  }
21508  }
21509 
21510  if (detailed)
21511  {
21512  typename SV::const_iterator it = sv.begin();
21513  typename SV::const_iterator it_end = sv.end();
21514 
21515  for (unsigned i = 0; i < vect.size(); ++i)
21516  {
21517  typename Vect::value_type v1 = vect[i];
21518  typename SV::value_type v2 = sv[i];
21519  typename SV::value_type v3 = *it;
21520 
21521  int cmp = sv.compare(i, v1);
21522  assert(cmp == 0);
21523  if (v1 > 0)
21524  {
21525  cmp = sv.compare(i, v1-1);
21526  assert(cmp > 0);
21527  }
21528 
21529  if (v1 != v2)
21530  {
21531  cerr << "SV discrepancy:" << "sv[" << i << "]=" << v2
21532  << " vect[" << i << "]=" << v1
21533  << endl;
21534  assert(0);return false;
21535  }
21536  if (v1 != v3)
21537  {
21538  cerr << "SV discrepancy:" << "sv[" << i << "]=" << v2
21539  << " *it" << v3
21540  << endl;
21541  assert(0);
21542  return false;
21543  }
21544  assert(it < it_end);
21545  ++it;
21546  } // for
21547  if (it != it_end)
21548  {
21549  cerr << "sv const_iterator discrepancy!" << endl;
21550  assert(0);
21551  return false;
21552  }
21553  }
21554 
21555  // extraction comparison
21556  if (detailed)
21557  {
21558  std::vector<typename SV::value_type> v1(sv.size());
21559  std::vector<typename SV::value_type> v1r(sv.size());
21560  sv.extract(&v1[0], sv.size(), 0);
21561  sv.extract_range(&v1r[0], sv.size(), 0);
21562  for (unsigned i = 0; i < sv.size(); ++i)
21563  {
21564  if (v1r[i] != v1[i] || v1[i] != vect[i])
21565  {
21566  cerr << "TestEqualSparseVectors Extract 1 failed at:" << i
21567  << " v1[i]=" << v1[i] << " v1r[i]=" << v1r[i]
21568  << endl;
21569  assert(0);
21570  exit(1);
21571  }
21572  } // for
21573  }
21574 
21575  // serialization comparison
21578  bm::sparse_vector_serialize<SV>(sv, sv_lay, tb);
21579  SV sv2;
21580  const unsigned char* buf = sv_lay.buf();
21581  int res = bm::sparse_vector_deserialize(sv2, buf, tb);
21582  if (res != 0)
21583  {
21584  cerr << "De-Serialization error" << endl;
21585  assert(0);
21586  exit(1);
21587  }
21588  if (sv.is_nullable() != sv2.is_nullable())
21589  {
21590  SV sv2_1;
21591  buf = sv_lay.buf();
21592  res = bm::sparse_vector_deserialize(sv2_1, buf, tb);
21593  cerr << "Serialization comparison of two svectors failed (NULL vector)" << endl;
21594  assert(0);
21595  exit(1);
21596  }
21597  const typename SV::bvector_type* bv_null = sv.get_null_bvector();
21598  const typename SV::bvector_type* bv_null2 = sv.get_null_bvector();
21599 
21600  if (bv_null != bv_null2 && (bv_null == 0 || bv_null2 == 0))
21601  {
21602  cerr << "Serialization comparison (NUUL vector missing)!" << endl;
21603  assert(0);
21604  exit(1);
21605  }
21606  if (bv_null)
21607  {
21608  if (bv_null->compare(*bv_null2) != 0)
21609  {
21610  cerr << "Serialization comparison of two svectors (NUUL vectors unmatch)!" << endl;
21611  assert(0);
21612  exit(1);
21613  }
21614  }
21615 
21616  if (!sv.equal(sv2) )
21617  {
21618  cerr << "Error: Serialization comparison of two svectors failed!" << endl;
21619  typename SV::size_type pos;
21620  bool b = bm::sparse_vector_find_first_mismatch(sv, sv2, pos);
21621  assert(b);
21622  cerr << "Mismatch at: " << pos << endl;
21623 
21625  bm::sparse_vector_serialize<SV>(sv, sv_lay1, tb);
21626  SV sv3;
21628 
21629  assert(0);
21630  exit(1);
21631  }
21632 
21633  return true;
21634 }
21635 
21636 template<class SV>
21637 bool TestEqualSparseVectors(const SV& sv1, const SV& sv2, bool detailed = true)
21638 {
21639  if (sv1.size() != sv2.size())
21640  {
21641  cerr << "TestEqualSparseVectors failed incorrect size" << endl;
21642  exit(1);
21643  }
21644 
21645  if (sv1.is_nullable() == sv2.is_nullable())
21646  {
21647  bool b = sv1.equal(sv2);
21648  if (!b)
21649  {
21650  cerr << "sv1.equal(sv2) failed" << endl;
21651  return b;
21652  }
21653  const typename SV::bvector_type* bv_null1 = sv1.get_null_bvector();
21654  const typename SV::bvector_type* bv_null2 = sv2.get_null_bvector();
21655 
21656  if (bv_null1 != bv_null2)
21657  {
21658  int r = bv_null1->compare(*bv_null2);
21659  if (r != 0)
21660  {
21661  cerr << "sparse NULL-vectors comparison failed" << endl;
21662  exit(1);
21663  }
21664  }
21665  }
21666  else // NULLable does not match
21667  {
21668  detailed = true; // simple check not possible, use slow, detailed
21669  }
21670 
21671 
21672  // test non-offset extraction
21673  //
21674  {
21675  std::vector<unsigned> v1(sv1.size());
21676  std::vector<unsigned> v1r(sv1.size());
21677  std::vector<unsigned> v1p(sv1.size());
21678  for (size_t i = 0; i < sv1.size(); ++i)
21679  {
21680  v1[i] = v1r[i] = v1p[i] = (unsigned)rand();
21681  }
21682 
21683  auto sz1 = sv1.extract(&v1[0], sv1.size(), 0);
21684  assert(sz1 == sv1.size());
21685  auto sz1r = sv1.extract_range(&v1r[0], sv1.size(), 0);
21686  auto sz1p = sv1.extract_planes(&v1p[0], sv1.size(), 0);
21687  (void)sz1; (void)sz1r; (void)sz1p;
21688 
21689  for (unsigned i = 0; i < sv1.size(); ++i)
21690  {
21691  if (v1r[i] != v1[i] || v1p[i] != v1[i])
21692  {
21693  unsigned v = sv1.get(i);
21694  cout << "XOR diff = " << (v ^ v1[i]) << endl;
21695  cerr << "TestEqualSparseVectors Extract 1 failed at:" << i
21696  << " v[i]=" << v << endl
21697  << " v1[i]=" << v1[i] << " v1r[i]=" << v1r[i] << " v1p[i]=" << v1p[i]
21698  << endl;
21699 
21700  std::vector<unsigned> v2(sz1);
21701  sv1.extract(&v2[0], sz1, i);
21702 
21703  v = v2[0];
21704  cerr << v << endl;
21705 
21706  assert(0); exit(1);
21707  }
21708  } // for
21709  }
21710 
21711  // test offset extraction
21712  //
21713  {
21714  std::vector<unsigned> v1(sv1.size());
21715  std::vector<unsigned> v1r(sv1.size());
21716  std::vector<unsigned> v1p(sv1.size());
21717 
21718  unsigned pos = sv1.size() / 2;
21719 
21720  sv1.extract(&v1[0], sv1.size(), pos);
21721  sv1.extract_range(&v1r[0], sv1.size(), pos);
21722  sv1.extract_planes(&v1p[0], sv1.size(), pos);
21723 
21724  for (unsigned i = 0; i < sv1.size(); ++i)
21725  {
21726  if (v1r[i] != v1[i] || v1p[i] != v1[i])
21727  {
21728  cerr << "TestEqualSparseVectors Extract 1 failed at:" << i
21729  << " v1[i]=" << v1[i] << " v1r[i]=" << v1r[i] << " v1p[i]=" << v1p[i]
21730  << endl;
21731  assert(0); exit(1);
21732  }
21733  } // for
21734  }
21735 
21736  {
21737  SV svv1(sv1);
21738  SV svv2(sv2);
21739 
21740  bm::null_support is_null = (sv1.is_nullable() == sv2.is_nullable()) ? bm::use_null : bm::no_null;
21741 
21742  bool b = sv1.equal(sv2, is_null);
21743  if (!b)
21744  {
21745  b = sv1.equal(sv2, is_null);
21746  assert(b);
21747  }
21748 
21749 
21750  b = svv1.equal(svv2, is_null);
21751  if (!b)
21752  {
21753  b = svv1.equal(svv2, is_null);
21754  cerr << "Equal, copyctor comparison failed" << endl;
21755  assert(0);return b;
21756  }
21757 
21758  svv1.swap(svv2);
21759  b = svv1.equal(svv2, is_null);
21760  if (!b)
21761  {
21762  cerr << "Equal, copyctor-swap comparison failed" << endl;
21763  assert(0);return b;
21764  }
21765  }
21766 
21767  // comparison using elements assignment via reference
21768  if (detailed)
21769  {
21770  SV sv3;
21771  sv3.resize(sv1.size());
21772  for (unsigned i = 0; i < sv1.size(); ++i)
21773  {
21774  sv3[i] = sv1[i];
21775  unsigned v1 = sv1[i];
21776  unsigned v2 = sv3[i];
21777  if (v1 != v2)
21778  {
21779  cerr << "1. sparse_vector reference assignment validation failed" << endl;
21780  assert(0);return false;
21781  }
21782  }
21783  bm::null_support is_null = (sv1.is_nullable() == sv3.is_nullable()) ? bm::use_null : bm::no_null;
21784  bool b = sv1.equal(sv3, is_null);
21785  if (!b)
21786  {
21787  cerr << "2. sparse_vector reference assignment validation failed" << endl;
21788  assert(0);return b;
21789  }
21790  }
21791 
21792  // comparison via const_iterators
21793  //
21794  {{
21795  typename SV::const_iterator it1 = sv1.begin();
21796  typename SV::const_iterator it2 = sv2.begin();
21797  typename SV::const_iterator it1_end = sv1.end();
21798 
21799  for (; it1 < it1_end; ++it1, ++it2)
21800  {
21801  if (*it1 != *it2)
21802  {
21803  cerr << "1. sparse_vector::const_iterator validation failed" << endl;
21804  assert(0);return false;
21805  }
21806  }
21807  }}
21808 
21809  // comparison through serialization
21810  //
21811  {{
21812  int res;
21814  bm::sparse_vector_serialize(sv1, sv_lay);
21815 
21816  // copy buffer to check if serialization size is actually correct
21817  const unsigned char* buf = sv_lay.buf();
21818  size_t buf_size = sv_lay.size();
21819 
21820  vector<unsigned char> tmp_buf(buf_size);
21821  ::memcpy(&tmp_buf[0], buf, buf_size);
21822 
21823  SV sv3;
21824  res = bm::sparse_vector_deserialize(sv3, &tmp_buf[0]);
21825  if (res != 0)
21826  {
21827  cerr << "De-Serialization error in TestEqualSparseVectors()" << endl;
21828  assert(0);exit(1);
21829  }
21830 
21831  const typename SV::bvector_type* bv_null1 = sv1.get_null_bvector();
21832  const typename SV::bvector_type* bv_null2 = sv2.get_null_bvector();
21833  const typename SV::bvector_type* bv_null3 = sv3.get_null_bvector();
21834 
21835  if (bv_null1 && bv_null3)
21836  {
21837  int r = bv_null1->compare(*bv_null3);
21838  if (r != 0)
21839  {
21840  cerr << "2. NULL bvectors comparison failed" << endl;
21841  assert(0);exit(1);
21842  }
21843  }
21844  if (bv_null1 && bv_null2)
21845  {
21846  int r = bv_null1->compare(*bv_null2);
21847  if (r != 0)
21848  {
21849  cerr << "3. NULL bvectors comparison failed" << endl;
21850  assert(0); exit(1);
21851  }
21852  }
21853 
21854  bm::null_support is_null = (sv1.is_nullable() == sv3.is_nullable()) ? bm::use_null : bm::no_null;
21855  if (!sv1.equal(sv3, is_null) )
21856  {
21857  cerr << "Serialization comparison of two svectors failed (1)" << endl;
21858  exit(1);
21859  }
21860  is_null = (sv2.is_nullable() == sv3.is_nullable()) ? bm::use_null : bm::no_null;
21861  if (!sv2.equal(sv3, is_null) )
21862  {
21863  cerr << "Serialization comparison of two svectors failed (2)" << endl;
21864  assert(0); exit(1);
21865  }
21866 
21867 
21868  }}
21869  return true;
21870 }
21871 
21872 static
21874 {
21875  cout << "---------------------------- Basic bit-matrix test" << endl;
21876 
21877  // construction-destruction
21878  {
21879  bm::basic_bmatrix<bvect> bmtr(10);
21880  bvect* bv = bmtr.construct_row(0);
21881  assert(bv);
21882 
21883  bv->set(10);
21884 
21885  // copy content
21886  //
21887  bm::basic_bmatrix<bvect> bmtr2(bmtr);
21888  bvect* bv2 = bmtr2.construct_row(0);
21889  assert(bv2);
21890 
21891  bool b = bv2->test(10);
21892  assert(b);
21893 
21894 
21895  bm::basic_bmatrix<bvect> bmtr4(10);
21896  {
21897  bm::basic_bmatrix<bvect> bmtr3(10);
21898  bmtr3.construct_row(0)->set(110);
21899  bmtr4 = bmtr3;
21900  bv = bmtr4.construct_row(0);
21901  b = bv->test(10);
21902  assert(!b);
21903  b = bv->test(110);
21904  assert(b);
21905  }
21906  bmtr4 = bmtr2;
21907  bv = bmtr4.construct_row(0);
21908  b = bv->test(10);
21909  assert(b);
21910  b = bv->test(110);
21911  assert(!b);
21912 
21913  bm::basic_bmatrix<bvect> bmtr5(2);
21914  bmtr5 = bmtr2;
21915  assert(bmtr5.rows()==10);
21916 
21917  bm::basic_bmatrix<bvect> bmtr6(11);
21918  bmtr6.construct_row(0)->set(210);
21919  bmtr6.swap(bmtr5);
21920  assert(bmtr6.rows()==10);
21921  assert(bmtr5.rows()==11);
21922 
21923  bv = bmtr6.construct_row(0);
21924  b = bv->test(210);
21925  assert(!b);
21926  bv = bmtr5.construct_row(0);
21927  b = bv->test(210);
21928  assert(b);
21929  }
21930 
21931 
21932  // octet assignment logic
21933  {
21934  bm::basic_bmatrix<bvect> bmtr(32);
21935  bmtr.set_octet(0, 0, '3');
21936  bmtr.set_octet(1, 0, 1);
21937  unsigned char ch;
21938  ch = bmtr.get_octet(0, 0);
21939  assert(ch == '3');
21940  ch = bmtr.get_octet(1, 0);
21941  assert(ch == 1);
21942 
21943  bmtr.optimize();
21944 
21945  ch = bmtr.get_octet(0, 0);
21946  assert(ch == '3');
21947  ch = bmtr.get_octet(1, 0);
21948  assert(ch == 1);
21949 
21950  }
21951  {
21952  bm::basic_bmatrix<bvect> bmtr(32);
21953  bmtr.set_octet(0, 0, 1);
21954  bmtr.set_octet(0, 1, 2);
21955  bmtr.set_octet(0, 2, 'G');
21956  bmtr.set_octet(0, 3, 'C');
21957  unsigned char ch;
21958  ch = bmtr.get_octet(0, 0);
21959  assert(ch == 1);
21960  ch = bmtr.get_octet(0, 1);
21961  assert(ch == 2);
21962  ch = bmtr.get_octet(0, 2);
21963  assert(ch == 'G');
21964  ch = bmtr.get_octet(0, 3);
21965  assert(ch == 'C');
21966  ch = bmtr.get_octet(0, 0);
21967  assert(ch == 1);
21968 
21969  bmtr.optimize();
21970  ch = bmtr.get_octet(0, 0);
21971  assert(ch == 1);
21972  ch = bmtr.get_octet(0, 1);
21973  assert(ch == 2);
21974  ch = bmtr.get_octet(0, 2);
21975  assert(ch == 'G');
21976  ch = bmtr.get_octet(0, 3);
21977  assert(ch == 'C');
21978  }
21979 
21980 
21981  cout << "---------------------------- Basic bit-matrix test OK" << endl;
21982 }
21983 
21984 
21985 
21986 static
21988 {
21989  cout << "---------------------------- Bit-plane sparse vector test" << endl;
21991 
21992 
21995 
21996  // basic construction (NULL-able vector)
21997  {{
21999 
22000  static_assert(sv1.is_signed() == false, "BM:Must be unsigned integral");
22001 
22002  bool n = sv1.is_nullable();
22003  assert(!n);
22004  const bvect* bvp = sv1.get_null_bvector();
22005  assert(bvp==0);
22006 
22008  n = sv2.is_nullable();
22009  assert(n);
22010 
22011  sv1 = sv2;
22012  assert(sv1.is_nullable());
22013 
22015  assert(sv3.is_nullable());
22016 
22018  sv3.swap(sv4);
22019  assert(sv4.is_nullable());
22020  assert(!sv3.is_nullable());
22021  bvp = sv4.get_null_bvector();
22022  assert(bvp);
22023 
22024  assert(!sv1.is_ro());
22025  sv1.freeze();
22026  assert(sv1.is_ro());
22027  }}
22028 
22029  // global set_null
22030  {{
22032 
22033  sv1.push_back(0);
22034  sv1.push_back(-5);
22035  sv1.push_back_null();
22036  sv1.push_back(3);
22037 
22039  sv1.set_null(bv);
22040 
22041  assert(sv1.is_null(0));
22042  assert(!sv1.is_null(1));
22043  assert(sv1.is_null(3));
22044  assert(sv1.is_null(5));
22045 
22046  assert(sv1.get(3) ==0);
22047  assert(sv1.get(1) ==-5);
22048  assert(sv1.get(0) ==0);
22049 
22050  int v;
22051  bool found;
22052  found = sv1.try_get(0, v);
22053  assert(!found);
22054  found = sv1.try_get(1, v);
22055  assert(found);
22056  assert(v == -5);
22057 
22058 
22060 
22061  sv2.push_back(0);
22062  sv2.push_back(1);
22063  sv2.push_back(2);
22064  sv2.push_back(3);
22065 
22066  sv2.set_null(bv);
22067 
22068  assert(sv2.is_null(0));
22069  assert(!sv2.is_null(1));
22070  assert(sv2.is_null(3));
22071  assert(!sv2.is_null(2));
22072  assert(!sv2.get(3));
22073  }}
22074 
22075 
22076  // global clear
22077  {{
22079 
22080  sv1.push_back(0);
22081  sv1.push_back(1);
22082  sv1.push_back_null();
22083  sv1.push_back(3);
22084 
22086  sv1.clear(bv);
22087 
22088  assert(!sv1.is_null(0));
22089  assert(!sv1.is_null(1));
22090  assert(!sv1.is_null(3));
22091  assert(sv1.is_null(5));
22092 
22093  assert(sv1.get(3) ==0);
22094  assert(sv1.get(1) ==1);
22095  assert(sv1.get(0) ==0);
22096  }}
22097 
22098 
22099  // bug fix for not clearing value on set_null
22100  {
22101  bool b;
22103  sv1.set_null(1);
22104  sv1.push_back(16);
22105 
22106  b = sv1.is_null(0);
22107  assert(b);
22108  b = sv1.is_null(1);
22109  assert(b);
22110  b = sv1.is_null(2);
22111  assert(!b);
22112 
22113  assert(sv1.size() == 3);
22114 
22115  sv1.set(0, 10);
22116  b = sv1.is_null(0);
22117  assert(!b);
22118  b = sv1.is_null(1);
22119  assert(b);
22120 
22121  sv1.set(0, 12);
22122  b = sv1.is_null(0);
22123  assert(!b);
22124 
22125  sv1.set_null(0);
22126  b = sv1.is_null(0);
22127  assert(b);
22128  auto it = sv1.begin();
22129  auto v = *it;
22130  assert(!v);
22131 
22132  sv1[0] = -8;
22133  b = sv1.is_null(0);
22134  assert(!b);
22135 
22136  v = sv1[0];
22137  assert(v == -8);
22138 
22139  }
22140 
22141  // swap test
22142  {
22144  sv.push_back(1);
22145  sv.push_back(8);
22146  sv.swap(1, 0);
22147  auto v1 = sv.get(0);
22148  auto v2 = sv.get(1);
22149  assert(v1 == 8);
22150  assert(v2 == 1);
22151  sv.optimize();
22152  sv.swap(0, 1);
22153  v1 = sv.get(0);
22154  v2 = sv.get(1);
22155  assert(v1 == 1);
22156  assert(v2 == 8);
22157 
22158  sv.set(128000, 5);
22159  sv.swap(128000, 0);
22160  v1 = sv.get(0);
22161  v2 = sv.get(1);
22162  assert(v1 == 5);
22163  assert(v2 == 8);
22164  v2 = sv.get(128000);
22165  assert(v2 == 1);
22166 
22167  }
22168 
22169 
22170  // reference vector construction for XOR serialization
22171  {{
22173  sv.push_back(1);
22174  sv.push_back(8);
22175 
22177  ref_vect.build(sv.get_bmatrix());
22178 
22179  assert(ref_vect.size() == 2);
22180 
22181  auto idx = ref_vect.find(unsigned(0));
22182  assert(idx == 0);
22183  idx = ref_vect.find(1);
22184  assert(idx == ref_vect.not_found());
22185  idx = ref_vect.find(2);
22186  assert(idx == ref_vect.not_found());
22187 
22188  // test for 8 which is 1 << 3
22189  idx = ref_vect.find(3);
22190  assert(idx == 1);
22191 
22192  // test XOR scanner
22193  //
22194 
22195  bm::xor_scanner<bvect> xscan;
22197  xscan.set_ref_vector(&r_vect);
22198  r_vect.build(sv.get_bmatrix());
22199 
22200  // test the distance matrix
22201  {
22202  bm::xor_sim_model<bvect> sim_model;
22203  r_vect.build_nb_digest_and_xor_matrix(sim_model.matr, sim_model.bv_blocks);
22204  assert(sim_model.matr.rows()==2);
22205  assert(sim_model.matr.cols()==1);
22206 
22207  xscan.compute_sim_model(sim_model, bm::xor_sim_params());
22208  }
22209 
22210  {
22211  bm::xor_sim_model<bvect> sim_model;
22212  xscan.compute_sim_model(sim_model, bm::xor_sim_params());
22213  assert(sim_model.matr.rows()==2);
22214  assert(sim_model.matr.cols()==1);
22215 
22216  auto mc_00 = sim_model.matr.get(0, 0);
22217 
22218  assert(mc_00.match == e_no_xor_match);
22219  }
22220 
22221 
22222  const bvect* bv_x = sv.get_slice(0);
22223  const bvect::blocks_manager_type& bman_x = bv_x->get_blocks_manager();
22224  const bm::word_t* block_x = bman_x.get_block_ptr(0, 0);
22225 
22226  xscan.compute_s_block_stats(block_x);
22227  assert(xscan.get_s_bc() == 1);
22228  assert(xscan.get_s_gc() == 2);
22229  assert(xscan.get_s_block_best() == 1);
22230 
22231  idx = xscan.get_ref_vector().find(unsigned(0));
22232  assert(idx == 0);
22233 
22234  bool f = xscan.search_best_xor_mask(block_x,
22235  idx,
22236  1, xscan.get_ref_vector().size(),
22237  0, 0, tb, bm::xor_sim_params());
22238  assert(!f);
22239  }}
22240 
22241  // cleaning
22242  {{
22244  const unsigned to = 256000;
22245  unsigned vset = to;
22246  for (unsigned i = 0; i < to; ++i, --vset)
22247  {
22248  sv1[i] = vset;
22249  }
22250  vset = to;
22251  for (unsigned i = 0; i < to; ++i, --vset)
22252  {
22253  auto v = sv1[i];
22254  assert(v == vset);
22255  }
22256  vset = to;
22257  for (unsigned i = 0; i < to; ++i, --vset)
22258  {
22259  sv1[i] = 8;
22260  }
22261  vset = to;
22262  for (unsigned i = 0; i < to; ++i, --vset)
22263  {
22264  auto v = sv1[i];
22265  assert(v == 8);
22266  }
22267 
22268  }}
22269 
22270  // basic const_iterator construction
22271  {{
22273  svector::const_iterator it_end;
22274  svector::const_iterator it = sv1.begin();
22275 
22276  assert(!it.valid());
22277  assert(!it_end.valid());
22278  assert(it != it_end);
22279  it.invalidate();
22280  assert(!it.valid());
22281 
22282  it.go_to(1);
22283  assert(!it.valid());
22284 
22285  svector::const_iterator it_end2 = sv1.end();
22286  assert(!it_end2.valid());
22287  }}
22288 
22289  // const_iterator::is_null()
22290  {{
22292  sv.push_back((1u << 31) | 1u);
22293  sv.push_back(20);
22294 
22295  sv.erase(0);
22296  sv.insert(0, 0);
22297  bvect* bv_null = sv.get_null_bvect();
22298  bv_null->clear_range(0, 0);
22299  bool b1 = bv_null->test(0);
22300  assert(!b1);
22301  b1 = bv_null->test(1);
22302  assert(b1);
22303 
22304  sv.set(0, 10);
22305  b1 = bv_null->test(0);
22306  assert(b1);
22307  b1 = bv_null->test(1);
22308  assert(b1);
22309 
22311  bool b = it.is_null();
22312  assert(!b);
22313  it.advance();
22314  b = it.is_null();
22315  assert(!b);
22316  }}
22317 
22318  // XOR scanner EQ test
22319  {{
22321  for (unsigned i = 0; i < 5; ++i)
22322  {
22323  sv.push_back(9);
22324  sv.push_back(0);
22325  }
22326 
22327  bm::xor_scanner<bvect> xscan;
22329  r_vect.build(sv.get_bmatrix());
22330  xscan.set_ref_vector(&r_vect);
22331 
22332  const bvect* bv_x = sv.get_slice(0);
22333  const bvect::blocks_manager_type& bman_x = bv_x->get_blocks_manager();
22334  const bm::word_t* block_x = bman_x.get_block_ptr(0, 0);
22335 
22336  xscan.compute_s_block_stats(block_x);
22337  assert(xscan.get_s_bc() == 5);
22338  assert(xscan.get_s_gc() == 10);
22339  assert(xscan.get_s_block_best() == 5);
22340 
22341  auto idx = xscan.get_ref_vector().find(unsigned(0));
22342  assert(idx == 0);
22343 
22344  bool f = xscan.search_best_xor_mask(block_x, idx,
22345  1, xscan.get_ref_vector().size(),
22346  0, 0, tb, bm::xor_sim_params());
22347  assert(f);
22348  idx = xscan.found_ridx();
22349  assert(idx == 1);
22350  //assert(xscan.get_x_best_metric() != 0); // not EQ as not a full block
22351  //assert(!xscan.is_eq_found());
22352  idx = xscan.get_ref_vector().get_row_idx(idx);
22353  assert(idx == 3); // matrix row 3
22354 
22355 
22356  {
22357  bm::xor_sim_model<bvect> sim_model;
22358 
22359  xscan.compute_sim_model(sim_model, bm::xor_sim_params());
22360  assert(sim_model.matr.rows()==2);
22361  assert(sim_model.matr.cols()==1);
22362 
22363  auto mc_00 = sim_model.matr.get(0, 0);
22364 
22365  assert(mc_00.match == e_xor_match_EQ);
22366  assert(mc_00.chain_size == 1);
22367  assert(mc_00.ref_idx[0] == 1);
22368 
22369 
22370  auto mc_10 = sim_model.matr.get(1, 0);
22371  assert(!mc_10.match);
22372  }
22373 
22374  }}
22375 
22376 
22377  {{
22379 
22380  for (unsigned i = 0; i < 10; ++i)
22381  sv[i] = 9;
22382  for (unsigned i = 55; i < 60; ++i)
22383  sv[i] = 9;
22384  for (unsigned i = 65; i < 70; ++i)
22385  sv[i] = 9;
22386  sv[65536*256] = 9;
22387  sv[65536*256+1] = 9;
22388  sv[65536*256+3] = 9;
22389  sv[65536*256+6] = 9;
22390 
22391  for (unsigned pass = 0; pass < 2; ++pass)
22392  {
22393  bm::xor_scanner<bvect> xscan;
22395  r_vect.build(sv.get_bmatrix());
22396  xscan.set_ref_vector(&r_vect);
22397  {
22398  bm::xor_sim_model<bvect> sim_model;
22399 
22400  xscan.compute_sim_model(sim_model, bm::xor_sim_params());
22401 
22402  assert(sim_model.matr.rows()==2);
22403  assert(sim_model.matr.cols()==2);
22404 
22405  auto mc_00 = sim_model.matr.get(0, 0);
22406 
22407  assert(mc_00.match == e_xor_match_EQ);
22408  assert(mc_00.chain_size == 1);
22409  assert(mc_00.ref_idx[0] == 1);
22410  assert(mc_00.nb == 0);
22411 
22412 
22413  auto mc_10 = sim_model.matr.get(1, 0);
22414  assert(!mc_10.match);
22415 
22416  if (pass == 0)
22417  {
22418  auto mc_01 = sim_model.matr.get(0, 1);
22419  assert(mc_01.match == e_xor_match_EQ);
22420  assert(mc_01.chain_size == 1);
22421  assert(mc_01.ref_idx[0] == 1);
22422  assert(mc_01.nb == 256);
22423  }
22424  }
22425  if (!sv.is_ro())
22426  {
22427  sv.optimize();
22428  sv.freeze();
22429  }
22430  } // for
22431 
22432  }}
22433 
22434  // test empty vector serialization
22435  {{
22436  int res;
22437 
22441  bm::sparse_vector_serialize(sv1, sv_layout);
22442 
22443  const unsigned char* buf = sv_layout.buf();
22444  res = bm::sparse_vector_deserialize(sv2, buf);
22445  if (res != 0)
22446  {
22447  cerr << "De-Serialization error" << endl;
22448  exit(1);
22449  }
22450  if (!sv1.equal(sv2) )
22451  {
22452  cerr << "Serialization comparison of empty vectors failed" << endl;
22453  exit(1);
22454  }
22455  }}
22456 
22457  // test move construction
22458  {{
22459  vector<svector> v;
22460  v.push_back(svector());
22461  v.push_back(svector());
22462  v[0] = svector();
22463  }}
22464 
22465  // test NULL operations
22466  {{
22469  sv1.resize(10);
22470  sv2.resize(10);
22471  for (unsigned i = 0; i < sv1.size(); ++i)
22472  {
22473  assert(!sv1.is_null(i));
22474  assert(sv2.is_null(i));
22475  }
22476  unsigned arr[3] = {1,2,3};
22477  sv1.import(arr, 3);
22478  sv2.import(arr, 3);
22479  assert(!sv1.is_null(0));
22480  assert(!sv2.is_null(0));
22481  assert(!sv2.is_null(1));
22482  assert(!sv2.is_null(2));
22483  assert(sv2.is_null(3));
22484 
22485  assert(sv2.is_null(5));
22486  sv2.set(5, 123);
22487  assert(!sv2.is_null(5));
22488 
22489  sv2.set_null(5);
22490  assert(sv2.is_null(5));
22491  assert(sv2[5].is_null());
22492 
22493 
22495  assert(sv3.is_nullable());
22496 
22497  assert(!sv3.is_null(0));
22498  assert(!sv3.is_null(1));
22499  assert(!sv3.is_null(2));
22500 
22501  sv3.clear_range(0, 1, true);
22502  assert(sv3.is_null(0));
22503  assert(sv3.is_null(1));
22504  assert(!sv3.is_null(2));
22505 
22506 
22507  sv3 = sv1;
22508  assert(sv3.is_nullable());
22509 
22510  sv1.clear();
22511  assert(!sv1.is_nullable());
22512  sv2.clear_all(true, 0);
22513  assert(sv2.is_nullable());
22514  }}
22515 
22516  {{
22518  unsigned arr[3] = {1,2,3};
22519  sv.import(arr, 3);
22520  cout << "sv.size() = " << sv.size() << endl;
22521  cout << "sv[]:";
22522  for (unsigned i = 0; i < sv.size(); ++i)
22523  {
22524  auto v = sv.at(i);
22525  cout << v << ",";
22526  unsigned u = sv.get_unsigned_bits(i, 2);
22527  assert(v == u);
22528  }
22529  cout << endl;
22530 
22532  bvect bv;
22533  scanner.find_nonzero(sv, bv);
22534  if (bv.count() != sv.size())
22535  {
22536  cerr << "compute_nonzero_bvector test failed" << endl;
22537  exit(1);
22538  }
22539  }}
22540 
22541  {{
22543  sv.push_back(1);
22544  sv.push_back(1);
22545  sv.push_back(1);
22546  unsigned arr[1024];
22547 
22548  unsigned esize = sv.extract(&arr[0], 1024, 0);
22549  assert(esize == 3);
22550  assert(arr[0] == 1);
22551  assert(arr[1] == 1);
22552  assert(arr[2] == 1);
22553  }}
22554 
22555  cout << "sv::push_back_null()" << endl;
22556  {{
22558  sv.push_back_null(10);
22559  auto sz = sv.size();
22560  assert(sz==10);
22561  sv.push_back(1);
22562  sz = sv.size();
22563  assert(sz==11);
22564  assert(sv[10] == 1);
22565  for (bvect::size_type i = 0; i < 10; ++i)
22566  {
22567  auto v = sv[i];
22568  assert(v == 0);
22569  }
22570  sv.optimize();
22571  assert(sv[10] == 1);
22572  for (bvect::size_type i = 0; i < 10; ++i)
22573  {
22574  auto v = sv[i];
22575  assert(v == 0);
22576  }
22577  }}
22578 
22579  // ---------------------------------------------------------
22580  // keep range tests
22581  {{
22583  sv.push_back(1);
22584  sv.push_back(1);
22585  sv.push_back(1);
22586 
22587  sv.keep_range(sv.size()+1, sv.size() + 2);
22588  assert(sv.get(0) ==0);
22589  assert(sv.get(1) ==0);
22590  assert(sv.get(2) ==0);
22591  }}
22592  {{
22594  sv.push_back(1);
22595  sv.push_back(1);
22596  sv.push_back(1);
22597 
22598  sv.keep_range(0, 0);
22599  assert(sv.get(0) ==1);
22600  assert(sv.get(1) ==0);
22601  assert(sv.get(2) ==0);
22602  }}
22603  {{
22605  sv.push_back(1);
22606  sv.push_back(1);
22607  sv.push_back(1);
22608 
22609  sv.keep_range(1, 2);
22610  assert(sv.get(0) ==0);
22611  assert(sv.get(1) ==1);
22612  assert(sv.get(2) ==1);
22613  }}
22614 
22615  {{
22617  sv.push_back(1);
22618  sv.push_back(2);
22619  sv.push_back(4);
22621 
22622  {
22625 
22627  st.reset();
22628  opt_builder.build_plan(tbatch, sv,
22630  auto sz = tbatch.size();
22631  assert(sz == 4);
22632 
22633  bm::run_task_batch(tbatch);
22634  assert(st.gap_blocks == 4);
22635  }
22636 
22639 
22641  st.reset();
22642  opt_builder.build_plan(tbatch, sv1,
22644 
22645  typedef
22647  pool_type tpool; // our thread pool here (no threads created yet)
22648  tpool.start(4); // start the threads
22649 
22650 
22651  {
22653  exec.run(tpool, tbatch, true);
22654  }
22655 
22656  tpool.set_stop_mode(pool_type::stop_when_done);
22657  tpool.join();
22658 
22659  assert(st.gap_blocks == 4);
22660  bool eq = sv.equal(sv1);
22661  assert(eq);
22662 
22663  }}
22664 
22665  cout << "svector back inserter..." << endl;
22666  {{
22668 
22670  bi = -10;
22671  bi.add_null();
22672  bi = 256;
22673 
22674  bi.flush();
22675 
22676  assert(sv.get(0) == -10);
22677  assert(sv.get(1) == 0);
22678  assert(sv.is_null(1) == true);
22679  assert(sv.get(2) == 256);
22680 
22681 
22682  }}
22683 
22684  cout << "svector Import test..." << endl;
22685 
22686  {{
22687  std::vector<unsigned> vect;
22688  for (unsigned i = 0; i < 128000; ++i)
22689  {
22690  vect.push_back(i);
22691  }
22692 
22693  svector sv;
22694  sv.import(&vect[0], (unsigned)vect.size());
22695  bool res = CompareSparseVector(sv, vect);
22696  if (!res)
22697  {
22698  cerr << "0.Bit plane import test failed" << endl;
22699  exit(1);
22700  }
22701  sv.optimize();
22702  print_svector_stat(cout,sv);
22703  res = CompareSparseVector(sv, vect);
22704  if (!res)
22705  {
22706  cerr << "optimized Bit plane import test failed" << endl;
22707  exit(1);
22708  }
22709 
22711  std::copy(vect.begin(), vect.end(), std::back_inserter(sv_1));
22712  res = CompareSparseVector(sv_1, vect);
22713  if (!res)
22714  {
22715  cerr << "Bit plane push_back test failed" << endl;
22716  exit(1);
22717  }
22718 
22719 
22721  sv.calc_stat(&st);
22722 
22724  res = CompareSparseVector(sv2, vect);
22725  if (!res)
22726  {
22727  cerr << "Bit plane copy-ctor test failed" << endl;
22728  exit(1);
22729  }
22730 
22731  sv2.clear();
22732  sv2.import(&vect[0], (unsigned)vect.size());
22733  res = CompareSparseVector(sv2, vect);
22734  if (!res)
22735  {
22736  cerr << "Bit plane copy-ctor test failed" << endl;
22737  exit(1);
22738  }
22739 
22741  sv3.set(65536, 10); // set some bit to initiate it
22742  sv3 = sv;
22743  res = CompareSparseVector(sv3, vect);
22744  if (!res)
22745  {
22746  cerr << "Bit plane assignmnet test failed" << endl;
22747  exit(1);
22748  }
22749 
22750  sv3.clear();
22751  sv3.import(&vect[0], (unsigned)vect.size());
22752  res = CompareSparseVector(sv3, vect);
22753  if (!res)
22754  {
22755  cerr << "Bit plane assignment test failed" << endl;
22756  exit(1);
22757  }
22758  }}
22759 
22760  cout << "Import test 64..." << endl;
22761 
22762  {{
22763  std::vector<unsigned long long> vect;
22764  for (unsigned long long i = 0; i < 128000; ++i)
22765  {
22766  vect.push_back(i << 33);
22767  }
22768 
22769  {{
22770  svector64 sv;
22771  unsigned long long v = 3;
22772  v = v << 33;
22773  sv.set(10, v);
22774  unsigned long long v1;
22775  v1 = sv.get(10);
22776  if (v != v1)
22777  {
22778  cerr << "SV 64-bit set failed" << endl;
22779  exit(1);
22780  }
22781  }}
22782 
22783  svector64 sv;
22784  sv.import(&vect[0], (unsigned)vect.size());
22785  bool res = CompareSparseVector(sv, vect);
22786  if (!res)
22787  {
22788  cerr << "0.Bit plane import test failed" << endl;
22789  exit(1);
22790  }
22791  sv.optimize();
22792  print_svector_stat(cout,sv);
22793  res = CompareSparseVector(sv, vect);
22794  if (!res)
22795  {
22796  cerr << "optimized Bit plane import test failed" << endl;
22797  exit(1);
22798  }
22799 
22800  svector64 sv_1;
22801  std::copy(vect.begin(), vect.end(), std::back_inserter(sv_1));
22802  res = CompareSparseVector(sv_1, vect);
22803  if (!res)
22804  {
22805  cerr << "Bit plane push_back test failed" << endl;
22806  exit(1);
22807  }
22808 
22809 
22810  svector64::statistics st;
22811  sv.calc_stat(&st);
22812 
22813  svector64 sv2(sv);
22814  res = CompareSparseVector(sv2, vect);
22815  if (!res)
22816  {
22817  cerr << "Bit plane copy-ctor test failed" << endl;
22818  exit(1);
22819  }
22820 
22821  sv2.clear_all(true, 0);
22822  sv2.import(&vect[0], (unsigned)vect.size());
22823  res = CompareSparseVector(sv2, vect);
22824  if (!res)
22825  {
22826  cerr << "Bit plane copy-ctor test failed" << endl;
22827  exit(1);
22828  }
22829 
22830  svector64 sv3;
22831  sv3.set(65536, 10); // set some bit to initiate it
22832  sv3 = sv;
22833  res = CompareSparseVector(sv3, vect);
22834  if (!res)
22835  {
22836  cerr << "Bit plane assignmnet test failed" << endl;
22837  exit(1);
22838  }
22839 
22840  sv3.clear();
22841  sv3.import(&vect[0], (unsigned)vect.size());
22842  res = CompareSparseVector(sv3, vect);
22843  if (!res)
22844  {
22845  cerr << "Bit plane assignment test failed" << endl;
22846  exit(1);
22847  }
22848  }}
22849 
22850  cout << "Same value assignment test.." << endl;
22851 
22852  {
22854  const unsigned max_assign =
22856  {
22858  bi(sv.get_back_inserter());
22859  for (unsigned i = 0; i < max_assign; ++i)
22860  {
22861  *bi = 1;
22862  } // for
22863  bi.flush();
22864  }
22866  sv.optimize(0, bvect::opt_compress, &st);
22867  assert(st.gap_blocks == 1);
22868  assert(st.bit_blocks == 0);
22869  for (unsigned i = 0; i < max_assign; ++i)
22870  {
22871  auto v = sv[i];
22872  assert(v == 1);
22873  } // for
22874  sv[65536] = 0;
22875  }
22876 
22877 
22878  cout << "Linear assignment test" << endl;
22879  {{
22880  std::vector<unsigned> vect(128000);
22884 
22885  {
22887  for (unsigned i = 0; i < 128000; ++i)
22888  {
22889  vect[i] = i;
22890  sv.set(i, i);
22891  sv1.set(i, i);
22892  *bi = i;
22893  }
22894  bi.flush();
22895  }
22896 
22897 
22898  bool res = CompareSparseVector(sv, vect);
22899  if (!res)
22900  {
22901  cerr << "linear assignment test failed" << endl;
22902  exit(1);
22903  }
22904  res = CompareSparseVector(sv1, vect);
22905  if (!res)
22906  {
22907  cerr << "linear assignment test failed (2)" << endl;
22908  exit(1);
22909  }
22910  res = CompareSparseVector(sv2, vect);
22911  if (!res)
22912  {
22913  cerr << "linear assignment test failed (3 - back_inserter)" << endl;
22914  exit(1);
22915  }
22916  }}
22917 
22918  cout << "Extract test" << endl;
22919 
22920  {{
22922  for (unsigned i = 0; i < 16; ++i)
22923  {
22924  sv.set(i, 8);
22925  }
22926  for (unsigned i =32; i < 48; ++i)
22927  {
22928  sv.set(i, 255);
22929  }
22930 
22931  std::vector<unsigned> v1(16);
22932  std::vector<unsigned> v1r(16);
22933  std::vector<unsigned> v1p(16);
22934 
22935  sv.extract(&v1[0], 16, 0);
22936  sv.extract_range(&v1r[0], 16, 0);
22937  sv.extract_planes(&v1p[0], 16, 0);
22938  for (unsigned i = 0; i < 16; ++i)
22939  {
22940  if (v1[i] != 8 || v1r[i] != v1[i] || v1p[i] != v1[i])
22941  {
22942  cerr << "Extract 1 failed at:" << i << endl;
22943  exit(1);
22944  }
22945  } // for
22946 
22947  std::vector<unsigned> v2(10);
22948  std::vector<unsigned> v2r(10);
22949  std::vector<unsigned> v2p(10);
22950 
22951  sv.extract(&v2[0], 10, 32);
22952  sv.extract_range(&v2r[0], 10, 32);
22953  sv.extract_planes(&v2p[0], 10, 32);
22954 
22955  for (unsigned i = 0; i < 10; ++i)
22956  {
22957  if (v2[i] != 255 || v2r[i] != v2[i] || v2p[i] != v2[i])
22958  {
22959  cerr << "Extract 2 failed at:" << i << "=" << v2[i] << " r=" << v2r[i] << " sv=" << sv[i+32] << endl;
22960  exit(1);
22961  }
22962  } // for
22963 
22964  }}
22965 
22966  //
22967  {
22969  {
22970  bm::sparse_vector<unsigned, bm::bvector<> >::back_insert_iterator
22971  bi = sv.get_back_inserter();
22972  for (unsigned i = 0; i < 100000; i+=2)
22973  {
22974  bi.add_null();
22975  bi = i;
22976  } // for
22977  bi.flush();
22978  }
22979  sv.optimize(tb);
22980 
22981  for (unsigned i = 0; i < 100000; i+=2)
22982  {
22983  bool b = sv.is_null(i);
22984  assert(b);
22985  auto v = sv.get(i+1);
22986  b = sv.is_null(i+1);
22987  assert(!b);
22988  assert(v == i);
22989  } // for
22990  }
22991 
22992  {
22994  {
22995  bm::sparse_vector<unsigned, bm::bvector<> >::back_insert_iterator
22996  bi = sv.get_back_inserter();
22997  for (unsigned i = 0; i < 100000; i+=2)
22998  {
22999  bi = i;
23000  bi.add_null();
23001  } // for
23002  bi.flush();
23003  }
23004  sv.optimize(tb);
23005  sv.freeze();
23006 
23007  for (unsigned i = 0; i < 100000; i+=2)
23008  {
23009  bool b = sv.is_null(i);
23010  assert(!b);
23011  auto v = sv.get(i);
23012  b = sv.is_null(i+1);
23013  assert(b);
23014  assert(v == i);
23015 
23016 
23017  unsigned N_bits = (unsigned)(rand()%31);
23018  if (N_bits)
23019  {
23020  auto u = sv.get_unsigned_bits(i, N_bits);
23021  unsigned mask1 = ~0u >> (32 - N_bits);
23022  auto v_masked = v & mask1;
23023  assert(u == v_masked);
23024  }
23025  } // for
23026  }
23027 
23028 
23029 
23030  // test automatic optimization with back_insert iterator
23031 
23032  {
23034  {
23035  bm::sparse_vector<unsigned, bm::bvector<>>::back_insert_iterator
23036  bi = sv.get_back_inserter();
23037  for (unsigned i = 0; i < 65536; ++i)
23038  {
23039  bi = 15;
23040  } // for
23041  }
23042 
23044 
23045  sv.calc_stat(&st);
23046  assert(st.bit_blocks == 0);
23047  assert(st.gap_blocks == 0);
23048 
23049  {
23050  bm::sparse_vector<unsigned, bm::bvector<>>::back_insert_iterator
23051  bi = sv.get_back_inserter();
23052  for (unsigned i = 0; i < 65536; ++i)
23053  {
23054  bi = 15;
23055  }
23056  }
23057 
23058  sv.calc_stat(&st);
23059  assert(st.bit_blocks == 0);
23060  assert(st.gap_blocks == 0);
23061  }
23062 
23063 
23064 
23065  // test insert() / erase()
23066  {
23068  sv1.push_back(1);
23069  sv1.push_back(2);
23070  sv1.insert(0, 17);
23071  sv1.insert(4, 18);
23072 
23073  assert(sv1.size() == 5);
23074  assert(sv1[0] == 17);
23075  assert(sv1[1] == 1);
23076  assert(sv1[2] == 2);
23077  assert(sv1[4] == 18);
23078 
23079  sv1.erase(1);
23080 
23081  assert(sv1.size() == 4);
23082  assert(sv1[0] == 17);
23083  assert(sv1[1] == 2);
23084  assert(sv1[3] == 18);
23085  }
23086 
23087  // test lower bound search
23088  cout << "sparse vector (unsigned) lower_bound()" << endl;
23089 
23090  {
23092  sv1.push_back(1);
23093  sv1.push_back(2);
23094  sv1.push_back(2);
23095  sv1.push_back(2);
23096  sv1.push_back(20);
23097  sv1.push_back(2000);
23098 
23099  bvect::size_type pos;
23100  bool found;
23101 
23103  found = scanner.bfind(sv1, 0u, pos);
23104  assert(!found);
23105 
23106  found = scanner.bfind(sv1, 1u, pos);
23107  assert(found);
23108  assert(pos == 0);
23109 
23110  found = scanner.bfind(sv1, 2u, pos);
23111  assert(found);
23112  assert(pos == 1);
23113 
23114  found = scanner.bfind(sv1, 3u, pos);
23115  assert(!found);
23116 
23117  found = scanner.bfind(sv1, 20u, pos);
23118  assert(found);
23119 
23120  found = scanner.bfind(sv1, 2000u, pos);
23121  assert(found);
23122  }
23123 
23124 
23125 
23126  {{
23127  cout << "sparse vector inc test" << endl;
23129 
23130  for (unsigned i = 1; i < 65536; ++i)
23131  {
23132  for (unsigned j = 0; j < 200000; ++j)
23133  {
23134  sv.inc(j);
23135  unsigned v = sv.get(j);
23136  assert(v == i);
23137  } // for j
23138  if (!is_silent)
23139  if ((i % 200) == 0)
23140  cout << "\r" << i << " / " << 65536 << flush;
23141  } // for i
23142 
23143  cout << "\nOk" << endl;
23144  }}
23145 
23146 
23147 
23148  {{
23149  cout << "Dynamic range clipping test 1" << endl;
23151 
23152  unsigned i;
23153  for (i = 0; i < 16; ++i)
23154  {
23155  sv.set(i, i);
23156  }
23158 
23159  for (i = 0; i < 16; ++i)
23160  {
23161  unsigned v = sv[i];
23162  if (v > 15)
23163  {
23164  cerr << "Clipped Value cmpr failed at:" << i << "=" << v << endl;
23165  exit(1);
23166  }
23167 
23168  } // for i
23169  cout << "Ok" << endl;
23170 
23171  }}
23172 
23173 
23174  {{
23175  cout << "Resize test" << endl;
23178  unsigned i;
23179  for (i = 0; i < 16; ++i)
23180  {
23181  sv.set(i, i);
23182  sv1.set(i, i);
23183  }
23184  if (sv.size()!= 16)
23185  {
23186  cerr << "1.Incorrect sparse vector size:" << sv.size() << endl;
23187  exit(1);
23188  }
23189 
23190 
23191  const bvect* bv_null1 = sv1.get_null_bvector();
23192  assert(bv_null1);
23193  if (bv_null1->count() != sv1.size())
23194  {
23195  cerr << "1.1. Incorrect sparse vector size() - NOT NULL comparison" << sv1.size() << " " << bv_null1->count() << endl;
23196  }
23197 
23198  sv.resize(10);
23199  sv1.resize(10);
23200  if (sv.size()!= 10 || sv1.size() != 10)
23201  {
23202  cerr << "2.Incorrect sparse vector size:" << sv.size() << endl;
23203  exit(1);
23204  }
23205  if (bv_null1->count() != sv1.size())
23206  {
23207  cerr << "2.1. Incorrect sparse vector size() - NOT NULL comparison" << sv1.size() << " " << bv_null1->count() << endl;
23208  }
23209 
23210 
23211  cout << "check values for size()=" << sv.size() << endl;
23212  for (i = 0; i < sv.size(); ++i)
23213  {
23214  unsigned v = sv[i];
23215  if (v != i)
23216  {
23217  cerr << "Wrong sparse vector value: at[" << i << "]=" << v << endl;
23218  exit(1);
23219  }
23220  assert(!sv1[i].is_null());
23221  v = sv1[i];
23222  if (v != i)
23223  {
23224  cerr << "Wrong null sparse vector value: at[" << i << "]=" << v << endl;
23225  exit(1);
23226  }
23227  }
23228 
23229  sv.resize(20);
23230  sv1.resize(20);
23231  if (sv.size() != 20 || sv1.size() != 20)
23232  {
23233  cerr << "3.Incorrect sparse vector size:" << sv.size() << endl;
23234  exit(1);
23235  }
23236  cout << "check values for size()=" << sv.size() << endl;
23237  for (i = 0; i < sv.size(); ++i)
23238  {
23239  unsigned v = sv[i];
23240  unsigned v1 = sv1[i];
23241 
23242  bool b_null = sv[i].is_null();
23243  bool b1_null = sv1[i].is_null();
23244 
23245  if (i < 10)
23246  {
23247  if (v != i || v1 != i)
23248  {
23249  cerr << "Wrong sparse vector value: at[" << i << "]=" << v << endl;
23250  exit(1);
23251  }
23252  assert(!b_null);
23253  assert(!b1_null);
23254  }
23255  else
23256  {
23257  if (v != 0 || v1 != 0)
23258  {
23259  cerr << "Wrong sparse (non-zero) vector value " << v << endl;
23260  exit(1);
23261  }
23262  assert(!b_null);
23263  assert(b1_null);
23264  }
23265  } // for i
23266 
23267  sv.resize(0);
23268  sv1.resize(0);
23269  if (sv.size()!= 0 || sv1.size() != 0)
23270  {
23271  cerr << "2.Incorrect sparse vector size:" << sv.size() << endl;
23272  exit(1);
23273  }
23274  if (bv_null1->count() != 0)
23275  {
23276  cerr << "3. Incorrect sparse vector size() - NOT NULL comparison" << sv1.size() << " " << bv_null1->count() << endl;
23277  }
23278 
23279 
23280  sv.resize(65536);
23281  sv1.resize(65536);
23282  if (bv_null1->count() != 0)
23283  {
23284  cerr << "4. Incorrect sparse vector size() - NOT NULL comparison" << sv1.size() << " " << bv_null1->count() << endl;
23285  }
23286 
23287  for (i = 0; i < sv.size(); ++i)
23288  {
23289  unsigned v = sv[i];
23290  unsigned v1 = sv1[i];
23291  if (v || v1)
23292  {
23293  if (v != 0)
23294  {
23295  cerr << "Wrong sparse (non-zero) vector value: " << v << endl;
23296  exit(1);
23297  }
23298  }
23299  assert(sv1[i].is_null());
23300  }
23301 
23302  }}
23303 
23304  // back insert test
23305  {{
23307  {
23308  auto bit = sv1.get_back_inserter();
23309  bit = 10;
23310  bit = 11;
23311  bit.add_null();
23312  bit = 13;
23313  }
23314  assert(sv1.size() == 4);
23315 
23316  assert(sv1.is_null(2));
23317  assert(!sv1.is_null(0));
23318  assert(!sv1.is_null(1));
23319  assert(!sv1.is_null(3));
23320 
23321  }}
23322 
23323  {{
23324  cout << "Dynamic range clipping test 2" << endl;
23326 
23327  unsigned i;
23328  for (i = 128000; i < 128000 * 3; ++i)
23329  {
23330  sv.set(i, 7 + (unsigned)rand() % 256);
23331  }
23333 
23334  for (i = 0; i < 128000 * 3; ++i)
23335  {
23336  unsigned v = sv[i];
23337  if (i < 128000)
23338  {
23339  if (v != 0)
23340  {
23341  cerr << "Value cmpr failed at:" << i << "=" << v << endl;
23342  exit(1);
23343  }
23344  }
23345  else
23346  {
23347  if (v > 15)
23348  {
23349  cerr << "Clipped Value cmpr failed at:" << i << "=" << v << endl;
23350  exit(1);
23351  }
23352  }
23353 
23354  } // for i
23355  cout << "Ok" << endl;
23356 
23357  }}
23358 
23359 
23360  {{
23361  cout << "Dynamic range clipping test 3" << endl;
23363 
23364  unsigned i;
23365  for (i = 0; i <= 16; ++i)
23366  {
23367  sv.set(i, 1);
23368  }
23369  for (i = 17; i < 32; ++i)
23370  {
23371  sv.set(i, 3);
23372  }
23373 
23374 
23376  for (i = 0; i < 32; ++i)
23377  {
23378  unsigned v = sv[i];
23379  if (v != 8)
23380  {
23381  cerr << "Low Clipped Value cmpr failed at:" << i << "=" << v << endl;
23382  exit(1);
23383  }
23384 
23385  } // for i
23386  cout << "Ok" << endl;
23387 
23388  }}
23389 
23390 
23391  cout << "Test Sparse vector join" << endl;
23392  {
23395 
23396  sv1.set(0, 0);
23397  sv1.set(1, 1);
23398  sv1.set(2, 2);
23399 
23400  sv2.set(3, 3);
23401  sv2.set(4, 4);
23402  sv2.set(5, 5);
23403 
23404  sv1.join(sv2);
23405 
23406  if (sv1.size()!=6)
23407  {
23408  cerr << "Sparse join size failed:" << sv1.size() << endl;
23409  exit(1);
23410 
23411  }
23412  for (unsigned i = 0; i < sv1.size(); ++i)
23413  {
23414  unsigned v1 = sv1[i];
23415  if (v1 != i)
23416  {
23417  cerr << "Sparse join cmp failed:" << sv1.size() << endl;
23418  exit(1);
23419  }
23420  }
23421  }
23422 
23423  cout << "Test Sparse vector merge" << endl;
23424  {
23427 
23428  sv1.set(0, 0);
23429  sv1.set(1, 1);
23430  sv1.set(2, 2);
23431 
23432  sv2.set(3, 3);
23433  sv2.set(4, 4);
23434  sv2.set(5, 5);
23435 
23436  sv1.merge(sv2);
23437 
23438  if (sv1.size()!=6)
23439  {
23440  cerr << "Sparse merge size failed:" << sv1.size() << endl;
23441  exit(1);
23442 
23443  }
23444  for (unsigned i = 0; i < sv1.size(); ++i)
23445  {
23446  unsigned v1 = sv1[i];
23447  if (v1 != i)
23448  {
23449  cerr << "Sparse join cmp failed:" << sv1.size() << endl;
23450  exit(1);
23451  }
23452  }
23453  }
23454 
23455 
23456  cout << "Test Sparse vector join with NULL-able" << endl;
23457  {
23460 
23461  assert(!sv1.is_nullable());
23462 
23463  sv1.set(0, 0);
23464  sv1.set(1, 1);
23465  sv1.set(2, 2);
23466 
23467  sv2.set(3, 3);
23468  sv2.set(4, 4);
23469  sv2.set(5, 5);
23470 
23471  sv1.join(sv2);
23472  assert(sv1.is_nullable());
23473 
23474  if (sv1.size()!=6)
23475  {
23476  cerr << "Sparse join size failed:" << sv1.size() << endl;
23477  exit(1);
23478 
23479  }
23480  for (unsigned i = 0; i < sv1.size(); ++i)
23481  {
23482  unsigned v1 = sv1[i];
23483  if (v1 != i)
23484  {
23485  cerr << "Sparse join cmp failed:" << sv1.size() << endl;
23486  exit(1);
23487  }
23488  //assert(!sv1[i].is_null());
23489  }
23490  }
23491 
23492  cout << "Test Sparse vector join NULL-able with not NULL-able" << endl;
23493  {
23496 
23497  assert(sv1.is_nullable());
23498 
23499  //sv1.set(0, 0);
23500  sv1.set(1, 1);
23501  sv1.set(2, 2);
23502 
23503  sv2.set(3, 3);
23504  sv2.set(4, 4);
23505  sv2.set(5, 5);
23506 
23507  sv1.join(sv2);
23508  assert(sv1.is_nullable());
23509 
23510  if (sv1.size()!=6)
23511  {
23512  cerr << "Sparse join size failed:" << sv1.size() << endl;
23513  exit(1);
23514  }
23515  for (unsigned i = 0; i < sv1.size(); ++i)
23516  {
23517  unsigned v1 = sv1[i];
23518  if (v1 != i)
23519  {
23520  cerr << "Sparse join cmp failed:" << i << endl;
23521  exit(1);
23522  }
23523  assert(!sv1[i].is_null());
23524  }
23525  }
23526 
23527  cout << "Test Sparse vector join NULL-able with NULL-able" << endl;
23528  {
23532 
23533  assert(sv1.is_nullable());
23534  assert(sv2.is_nullable());
23535 
23536  //sv1.set(0, 0);
23537  sv1.set(1, 1);
23538  sv1.set(2, 2);
23539 
23540  //sv2.set(3, 3);
23541  sv2.set(4, 4);
23542  sv2.set(5, 5);
23543 
23544  sv1.join(sv2);
23545  assert(sv1.is_nullable());
23546 
23547  if (sv1.size()!=6)
23548  {
23549  cerr << "Sparse join size failed:" << sv1.size() << endl;
23550  exit(1);
23551  }
23552  auto sz = sv1.size();
23553  for (unsigned i = 0; i < sz; ++i)
23554  {
23555  unsigned v1 = sv1[i];
23556  if (v1 != i)
23557  {
23558  if (i == 0 || i == 3) // legitimate test-case exceptions
23559  {
23560  }
23561  else
23562  {
23563  cerr << "Sparse join cmp failed:" << i << endl;
23564  exit(1);
23565  }
23566  }
23567  if (sv1[i].is_null())
23568  {
23569  assert(i == 0 || i == 3);
23570  }
23571  }
23572  }
23573 
23574  cout << "check if optimize keeps the NULL vector" << std::endl;
23575  {
23577  assert(sv.is_nullable());
23578  sv.optimize();
23579  assert(sv.is_nullable());
23580  }
23581 
23582 
23583  {
23587 
23588  unsigned i;
23589  for (i = 65536; i < 256000; ++i)
23590  {
23591  sv1.set(i, 256);
23592  sv3.set(i, 256);
23593  }
23594  for (i = 312000; i < 365636; ++i)
23595  {
23596  sv2.set(i, 65536);
23597  sv3.set(i, 65536);
23598  }
23599 
23600  sv1.optimize();
23601  sv1.join(sv2);
23602 
23603  if (sv1.size() != sv3.size())
23604  {
23605  cerr << "Sparse join size failed (2):" << sv1.size() << endl;
23606  exit(1);
23607  }
23608 
23609  for (i = 0; i < sv1.size(); ++i)
23610  {
23611  unsigned v1 = sv1[i];
23612  unsigned v3 = sv3[i];
23613  if (v1 != v3)
23614  {
23615  cerr << "Sparse join cmp failed (2):" << v1 << "!=" << v3 << endl;
23616  exit(1);
23617  }
23618  } // for i
23619  }
23620  cout << "Sparse vector join ok" << endl;
23621 
23622  cout << "---------------------------- Bit-plane sparse vector test OK" << endl;
23623 }
23624 
23626 {
23627  cout << " -------------------------- TestSignedSparseVector()" << endl;
23628 
23629  {{
23631  sv.push_back(-1);
23632  sv.push_back(1);
23633  sv.push_back(INT_MAX);
23634  sv.push_back(INT_MIN);
23635 
23636  int v0 = sv.get(0);
23637  assert(v0 == -1);
23638  int v1 = sv[1];
23639  assert(v1 == 1);
23640  int v2 = sv[2];
23641  assert(v2 == INT_MAX);
23642  int v3 = sv.get(3);
23643  assert(v3 == INT_MIN);
23644 
23645  int arr[1024];
23646 
23647  auto esize = sv.extract(&arr[0], 1024, 0);
23648  assert(esize == 4);
23649  assert(arr[0] == -1);
23650  assert(arr[1] == 1);
23651  assert(arr[2] == INT_MAX);
23652  assert(arr[3] == INT_MIN);
23653 
23654  std::vector<int> target_v;
23655  std::vector<bm::sparse_vector<int, bvect>::size_type> idx_v;
23656  target_v.resize(4);
23657  for (bm::sparse_vector<int, bvect>::size_type i = 0; i < 5; ++i)
23658  idx_v.push_back(i);
23659  sv.gather(target_v.data(), idx_v.data(), 4, BM_SORTED);
23660  assert(target_v[0] == -1);
23661  assert(target_v[1] == 1);
23662  assert(target_v[2] == INT_MAX);
23663  assert(target_v[3] == INT_MIN);
23664 
23665  sv.inc(0);
23666  sv.inc(1);
23667  sv.inc(2);
23668  sv.inc(3);
23669 
23670  assert(sv.get(0) == 0);
23671  assert(sv.get(1) == 2);
23672  v2 = sv.get(2);
23673  cout << v2 << endl;
23674  assert(sv.get(2) == 0);
23675  assert(sv.get(3) == INT_MIN+1);
23676  }}
23677 
23678  // import back
23679  {{
23680  std::vector<int> vect {0, 1, -1, INT_MIN, INT_MAX, 0 };
23682  sv.import_back(vect.data(), bm::sparse_vector<int, bvect>::size_type(vect.size()), false);
23683 
23684  assert(sv.size() == vect.size());
23685  for (size_t i = 0; i < vect.size(); ++i)
23686  {
23687  auto vc = vect.at(i);
23689  assert(v == vc);
23690  } // for
23691 
23692  sv.resize(0);
23693  vect.resize(0);
23694  for (size_t i = 0; i < 65536*256; ++i)
23695  vect.push_back(int(-i));
23696 
23697  sv.import_back(vect.data(), bm::sparse_vector<int, bvect>::size_type(vect.size()), false);
23698 
23699  assert(sv.size() == vect.size());
23700  for (size_t i = 0; i < vect.size(); ++i)
23701  {
23702  auto vc = vect.at(i);
23704  assert(v == vc);
23705  } // for
23706 
23707  }}
23708 
23709  {{
23711  {
23712  auto bi(sv.get_back_inserter());
23713  *bi = (-1);
23714  *bi = (1);
23715  *bi = (INT_MAX);
23716  *bi = (INT_MIN);
23717  *bi = 0;
23718  bi.flush();
23719  }
23720 
23721  int arr[1024];
23722  auto esize = sv.extract(&arr[0], 1024, 0);
23723  assert(esize == 5);
23724  assert(arr[0] == -1);
23725  assert(arr[1] == 1);
23726  assert(arr[2] == INT_MAX);
23727  assert(arr[3] == INT_MIN);
23728  assert(arr[4] == 0);
23729 
23730  }}
23731 
23732  cout << "svector Import test..." << endl;
23733 
23734  {{
23735  sparse_vector_i32 sv;
23736  int arr[3] = {1,-8,3};
23737  sv.import(arr, 3); // import from a C-style array (fastest way to populate)
23738  sv.optimize();
23739  print_svector_stat(cout,sv);
23740 
23742  sv.calc_stat(&st);
23743  assert(st.gap_blocks == 4);
23744  }}
23745 
23746  {{
23747  std::vector<int> vect;
23748  for (int i = 0; i < 128000; ++i)
23749  vect.push_back(-i);
23750 
23751  sparse_vector_i32 sv;
23752  sv.import(&vect[0], (bvect::size_type)vect.size());
23753  bool res = CompareSparseVector(sv, vect);
23754  if (!res)
23755  {
23756  cerr << "0.Bit plane import test failed" << endl;
23757  assert(0);exit(1);
23758  }
23759  sv.optimize();
23760  print_svector_stat(cout,sv);
23761  res = CompareSparseVector(sv, vect);
23762  if (!res)
23763  {
23764  cerr << "optimized Bit plane import test failed" << endl;
23765  assert(0);exit(1);
23766  }
23767 
23768  sparse_vector_i32 sv_1;
23769  std::copy(vect.begin(), vect.end(), std::back_inserter(sv_1));
23770  res = CompareSparseVector(sv_1, vect);
23771  if (!res)
23772  {
23773  cerr << "Bit plane push_back test failed" << endl;
23774  assert(0);exit(1);
23775  }
23776 
23778  sv.calc_stat(&st);
23779 
23780  sparse_vector_i32 sv2(sv);
23781  res = CompareSparseVector(sv2, vect);
23782  if (!res)
23783  {
23784  cerr << "Bit plane copy-ctor test failed" << endl;
23785  assert(0);exit(1);
23786  }
23787 
23788  sv2.clear();
23789  sv2.import(&vect[0], (bvect::size_type)vect.size());
23790  res = CompareSparseVector(sv2, vect);
23791  if (!res)
23792  {
23793  cerr << "Bit plane copy-ctor test failed" << endl;
23794  assert(0);exit(1);
23795  }
23796 
23797  sparse_vector_i32 sv3;
23798  sv3.set(65536, 10); // set some bit to initiate it
23799  sv3 = sv;
23800  res = CompareSparseVector(sv3, vect);
23801  if (!res)
23802  {
23803  cerr << "Bit plane assignmnet test failed" << endl;
23804  exit(1);
23805  }
23806 
23807  sv3.clear();
23808  sv3.import(&vect[0], (bvect::size_type)vect.size());
23809  res = CompareSparseVector(sv3, vect);
23810  if (!res)
23811  {
23812  cerr << "Bit plane assignment test failed" << endl;
23813  exit(1);
23814  }
23815  }}
23816 
23817 
23818  cout << "Same value assignment test.." << endl;
23819  {
23821  const unsigned max_assign =
23823  {
23824  auto bi(sv.get_back_inserter());
23825  for (unsigned i = 0; i < max_assign; ++i)
23826  {
23827  *bi = -1;
23828  } // for
23829  bi.flush();
23830  }
23832  sv.optimize(0, bvect::opt_compress, &st);
23833  assert(st.gap_blocks == 1);
23834  assert(st.bit_blocks == 0);
23835 
23836  for (unsigned i = 0; i < max_assign; ++i)
23837  {
23838  auto v = sv[i];
23839  assert(v == -1);
23840  } // for
23841 
23842  for (auto it = sv.begin(); it < sv.end(); ++it)
23843  {
23844  auto v = *it;
23845  assert(v == -1);
23846  }
23847  sv[65536] = 0;
23848  }
23849 
23850 
23851  cout << "Linear assignment test" << endl;
23852  {{
23853  std::vector<int> vect(128000);
23854  typedef std::vector<int>::size_type vect_sz_type;
23855  typedef bm::sparse_vector<int, bvect >::size_type sv_sz_type;
23859 
23860  {
23862  for (int i = 0; i < 128000; ++i)
23863  {
23864  vect[vect_sz_type(i)] = -i;
23865  sv.set(sv_sz_type(i), -i);
23866  sv1.set(sv_sz_type(i), -i);
23867  *bi = -i;
23868  }
23869  bi.flush();
23870  }
23871 
23872 
23873  bool res = CompareSparseVector(sv, vect);
23874  if (!res)
23875  {
23876  cerr << "linear assignment test failed" << endl;
23877  exit(1);
23878  }
23879  res = CompareSparseVector(sv1, vect);
23880  if (!res)
23881  {
23882  cerr << "linear assignment test failed (2)" << endl;
23883  exit(1);
23884  }
23885  res = CompareSparseVector(sv2, vect);
23886  if (!res)
23887  {
23888  cerr << "linear assignment test failed (3 - back_inserter)" << endl;
23889  exit(1);
23890  }
23891  }}
23892 
23893  cout << " -------------------------- TestSignedSparseVector() OK" << endl;
23894 }
23895 
23896 
23897 
23898 static
23900 {
23901  cout << " -------------------------- TestSparseVectorAlgo()" << endl;
23902 
23903  {
23906  sv1.push_back(1);
23907  sv1.push_back(1);
23908  sv1.push_back(1);
23909 
23910  sv2 = sv1;
23911 
23913  bool f;
23914  f = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
23915  assert(!f);
23916  f = bm::sparse_vector_find_first_mismatch(sv2, sv1, pos);
23917  assert(!f);
23918  sv2.push_back(4);
23919  f = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
23920  assert(f);
23921  assert(pos == 3);
23922  f = bm::sparse_vector_find_first_mismatch(sv2, sv1, pos);
23923  assert(f);
23924  assert(pos == 3);
23925 
23926  sv1.optimize();
23927  f = bm::sparse_vector_find_first_mismatch(sv2, sv1, pos);
23928  assert(f);
23929  assert(pos == 3);
23930 
23931  sv2.optimize();
23932  f = bm::sparse_vector_find_first_mismatch(sv2, sv1, pos);
23933  assert(f);
23934  assert(pos == 3);
23935  }
23936 
23937  // sparse with NULLs test
23938  {
23941  sv1[100] = 1;
23942  sv1[1000] = 1;
23943  sv1[bm::id_max32/2 + 1000] = 1;
23944 
23945  sv2 = sv1;
23946 
23948  bool f;
23949  f = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
23950  assert(!f);
23951  f = bm::sparse_vector_find_first_mismatch(sv2, sv1, pos);
23952  assert(!f);
23953  sv2.push_back(4);
23954  f = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
23955  assert(f);
23956  assert(pos == bm::id_max32/2 + 1000+1);
23957  f = bm::sparse_vector_find_first_mismatch(sv2, sv1, pos);
23958  assert(f);
23959  assert(pos == bm::id_max32/2 + 1000+1);
23960 
23961  sv1.optimize();
23962  f = bm::sparse_vector_find_first_mismatch(sv2, sv1, pos);
23963  assert(f);
23964  assert(pos == bm::id_max32/2 + 1000+1);
23965 
23966  sv2.optimize();
23967  f = bm::sparse_vector_find_first_mismatch(sv2, sv1, pos);
23968  assert(f);
23969  assert(pos == bm::id_max32/2 + 1000+1);
23970  }
23971 
23972  {
23976 
23977  sv1[1] = 1;
23978  sv1[2] = 2;
23979  sv1.set_null(3); // set element 3 to NULL
23980  sv1[4] = 0;
23981 
23982  sv2 = sv1;
23983 
23984  bool found = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
23985  assert(!found);
23986 
23987  sv2[4] = 10;
23988  found = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
23989  assert(found);
23990  assert(pos == 4);
23991 
23992  sv2[3] = 0;
23993  found = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
23994  assert(found);
23995  assert(pos == 3);
23996  }
23997 
23998  {
24000 
24003 
24004  bool found = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
24005  assert(!found);
24006 
24007  sv1[0] = 0;
24008  sv1[10] = 1;
24009  sv1[20] = 2;
24010  sv1.set_null(30); // set element 3 to NULL
24011  sv1[40] = 0;
24012 
24013  sv2[0] = 0;
24014  sv2[10] = 1;
24015  sv2[20] = 2;
24016  sv2[40] = 0;
24017 
24018  found = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
24019  assert(found);
24020  assert(pos == 1);
24021 
24022  found = bm::sparse_vector_find_first_mismatch(sv2, sv1, pos);
24023  assert(found);
24024  assert(pos == 1);
24025 
24026  }
24027 
24028  cout << " ----- Find mismatches " << endl;
24029 
24030  {
24031  bvect bv_m; // mismatch vector
24034 
24036  assert(!bv_m.any());
24037 
24038  sv1[0] = 0;
24039  sv1[10] = 15;
24040  sv1[20] = 23;
24041 
24042  sv2[0] = 0;
24043  sv2[10] = 15;
24044  sv2[20] = 23;
24045 
24047  assert(!bv_m.any());
24048 
24049  sv2[0] = 32;
24051  assert(bv_m.count()==1);
24052  {
24053  bvect bv_c { 0 };
24054  bool f = bv_m.equal(bv_c);
24055  assert(f);
24056  }
24057 
24058  sv1[22] = 255;
24060  cout << bv_m.count() << endl;
24061  assert(bv_m.count()==3);
24062  {
24063  bvect bv_c { 0, 21, 22 };
24064  bool f = bv_m.equal(bv_c);
24065  assert(f);
24066  }
24067  }
24068 
24069  {
24070  bvect bv_m; // mismatch vector
24073 
24074  sv1[0] = 0;
24075  sv1[10] = 15;
24076  sv1[20] = 23;
24077 
24078  sv2[0] = 0;
24079  sv2[10] = 15;
24080  sv2[20] = 23;
24081 
24083  assert(!bv_m.any());
24085  assert(!bv_m.any());
24086 
24087  sv2[id_max/2] = 0;
24088  sv2[id_max/2+1] = 256;
24089 
24091  cout << bv_m.count() << endl;
24092  assert(bv_m.count()==2);
24093  {
24094  bvect bv_c { id_max/2, id_max/2+1 };
24095  DetailedCompareBVectors(bv_c, bv_m);
24096  bool f = bv_m.equal(bv_c);
24097  assert(f);
24098  }
24100  cout << bv_m.count() << endl;
24101  assert(bv_m.count()==2);
24102  {
24103  bvect bv_c { id_max/2, id_max/2+1 };
24104  DetailedCompareBVectors(bv_c, bv_m);
24105  bool f = bv_m.equal(bv_c);
24106  assert(f);
24107  }
24108  }
24109 
24110 
24111  {
24112  bvect bv_m; // mismatch vector
24115 
24116  sv1[0] = 0;
24117  sv1[1] = 15;
24118  sv1[2] = 23;
24119 
24120  sv2[0] = 0;
24121  sv2[1] = 15;
24122  sv2[2] = 23;
24123 
24125  assert(!bv_m.any());
24127  cout << bv_m.count() << endl;
24128  assert(!bv_m.any());
24130  cout << bv_m.count() << endl;
24131  assert(!bv_m.any());
24132 
24133  sv2[4] = 10;
24135  cout << bv_m.count() << endl;
24136  {
24137  bvect bv_c { 3, 4 };
24138  DetailedCompareBVectors(bv_c, bv_m);
24139  bool f = bv_m.equal(bv_c);
24140  assert(f);
24141  }
24143  cout << bv_m.count() << endl;
24144  {
24145  bvect bv_c { 3, 4 };
24146  DetailedCompareBVectors(bv_c, bv_m);
24147  bool f = bv_m.equal(bv_c);
24148  assert(f);
24149  }
24150 
24151  }
24152 
24153  {
24154  bvect bv_m; // mismatch vector
24157 
24158  sv1[0] = 0;
24159  sv1[1] = 1;
24160  sv1[2] = 2;
24161 
24162  sv2[0] = 0;
24163  sv2[1] = 1;
24164  sv2[2] = 2;
24165 
24166  sv2[3] = 0;
24168  cout << bv_m.count() << endl;
24169  {
24170  bvect bv_c { 3 };
24171  DetailedCompareBVectors(bv_c, bv_m);
24172  bool f = bv_m.equal(bv_c);
24173  assert(f);
24174  }
24176  cout << bv_m.count() << endl;
24177  {
24178  bvect bv_c { 3 };
24179  DetailedCompareBVectors(bv_c, bv_m);
24180  bool f = bv_m.equal(bv_c);
24181  assert(f);
24182  }
24183 
24184 
24185  sv1[5] = 0;
24187  cout << bv_m.count() << endl;
24188  {
24189  bvect bv_c { 4, 5 };
24190  DetailedCompareBVectors(bv_c, bv_m);
24191  bool f = bv_m.equal(bv_c);
24192  assert(f);
24193  }
24195  cout << bv_m.count() << endl;
24196  assert(!bv_m.any());
24197 
24199  cout << bv_m.count() << endl;
24200  assert(!bv_m.any());
24201  }
24202 
24203 
24204 
24205  cout << " -------------------------- TestSparseVectorAlgo() OK" << endl;
24206 }
24207 
24208 // Debugging function to validate match findings integrity
24209 //
24210 /*
24211 static
24212 void validate_match_pairs(
24213  const bm::xor_scanner<bvect>::match_pairs_vector_type& mp_vect,
24214  unsigned xor_idx)
24215 {
24216  bvect bv_acc;
24217  auto sz = mp_vect.size();
24218  if (!sz)
24219  return;
24220  for (unsigned i = 0; i < sz; ++i)
24221  {
24222  const bm::match_pair& mp = mp_vect[i];
24223  assert(xor_idx != mp.ref_idx);
24224  if (bv_acc.test(mp.ref_idx))
24225  {
24226  assert(0); // double searched element
24227  }
24228  bv_acc.set(mp.ref_idx);
24229  for (unsigned j = 0; j < sz; ++j)
24230  {
24231  if (i == j)
24232  continue;
24233  const bm::match_pair& mp_j = mp_vect[j];
24234  assert(0 == (mp.xor_d64 & mp_j.xor_d64)); // never inetersect
24235  } // for j
24236  } // for i
24237 }
24238 */
24239 
24240 static
24242 {
24243  cout << " -------------------------- TestSparseVector_XOR_Scanner()" << endl;
24245 
24246  // XOR scanner EQ test
24247  {{
24249  for (unsigned i = 0; i < 3; ++i)
24250  {
24251  sv.push_back(9);
24252  sv.push_back(9);
24253  sv.push_back(0);
24254  }
24255 
24256 
24257  bm::xor_scanner<bvect> xscan;
24259  r_vect.build(sv.get_bmatrix());
24260  xscan.set_ref_vector(&r_vect);
24261 
24262  const bvect* bv_x = sv.get_slice(0);
24263  const bvect::blocks_manager_type& bman_x = bv_x->get_blocks_manager();
24264  const bm::word_t* block_x = bman_x.get_block_ptr(0, 0);
24265 
24266  xscan.compute_s_block_stats(block_x);
24267 
24268  auto idx = xscan.get_ref_vector().find(unsigned(0));
24269  assert(idx == 0);
24270 
24271  bool f = xscan.search_best_xor_mask(block_x, idx,
24272  1, xscan.get_ref_vector().size(),
24273  0, 0, tb, bm::xor_sim_params());
24274  assert(f);
24275  idx = xscan.found_ridx();
24276  assert(idx == 1);
24277  assert(xscan.get_x_best_metric() == 0); // EQ
24278  //assert(xscan.is_eq_found());
24279  idx = xscan.get_ref_vector().get_row_idx(idx);
24280  assert(idx == 3); // matrix row 3
24281 
24282  // parallel prog
24283  {
24286  bm::xor_sim_model<bvect> sim_model;
24287  bm::xor_sim_params xor_search_params;
24288 
24289  pbuilder.build_plan(tbatch, sim_model,
24290  r_vect, xor_search_params);
24291 
24292  typedef
24294  pool_type tpool; // our thread pool here (no threads created yet)
24295  tpool.start(1); // start the threads
24296  {
24298  exec.run(tpool, tbatch, true);
24299  }
24300  tpool.set_stop_mode(pool_type::stop_when_done);
24301  tpool.join();
24302 
24303  assert(sim_model.bv_blocks.test(0));
24304  assert(sim_model.matr.cols() == 1);
24305  assert(sim_model.matr.rows() == 2);
24306  auto mc_00 = sim_model.matr.get(0, 0);
24307  assert(mc_00.match == e_xor_match_EQ);
24308 
24309  }
24310 
24311 
24312 
24313  }}
24314 
24315  {{
24317  for (unsigned i = 0; i < 65536; ++i)
24318  sv.push_back(9);
24319 
24320  bm::xor_scanner<bvect> xscan;
24322  r_vect.build(sv.get_bmatrix());
24323  xscan.set_ref_vector(&r_vect);
24324 
24325  const bvect* bv_x = sv.get_slice(0);
24326  const bvect::blocks_manager_type& bman_x = bv_x->get_blocks_manager();
24327  const bm::word_t* block_x = bman_x.get_block_ptr(0, 0);
24328 
24329  xscan.compute_s_block_stats(block_x);
24330 
24331  auto idx = xscan.get_ref_vector().find(unsigned(0));
24332  assert(idx == 0);
24333  auto sz = xscan.get_ref_vector().size();
24334  bool f = xscan.search_best_xor_mask(block_x, idx,
24335  1, sz,
24336  0, 0, tb, bm::xor_sim_params());
24337  assert(f);
24338  idx = xscan.found_ridx();
24339  assert(idx == 1);
24340  assert(xscan.get_x_best_metric() == 0); // EQ
24341  //assert(xscan.is_eq_found());
24342  idx = xscan.get_ref_vector().get_row_idx(idx);
24343  assert(idx == 3); // matrix row 3
24344 
24345  }}
24346 
24347  {{
24349  for (unsigned i = 0; i < 65536; i+=2)
24350  {
24351  sv.push_back(1);
24352  sv.push_back(8);
24353  }
24354  bm::xor_scanner<bvect> xscan;
24356  r_vect.build(sv.get_bmatrix());
24357  xscan.set_ref_vector(&r_vect);
24358 
24359  const bvect* bv_x = sv.get_slice(0);
24360  const bvect::blocks_manager_type& bman_x = bv_x->get_blocks_manager();
24361  const bm::word_t* block_x = bman_x.get_block_ptr(0, 0);
24362 
24363  xscan.compute_s_block_stats(block_x);
24364 
24365  auto idx = xscan.get_ref_vector().find(unsigned(0));
24366  assert(idx == 0);
24367  auto sz = xscan.get_ref_vector().size();
24368  bool f = xscan.search_best_xor_mask(block_x, idx,
24369  1, sz,
24370  0, 0, tb, bm::xor_sim_params());
24371  assert(f);
24372  idx = xscan.found_ridx();
24373  assert(idx == 1);
24374  assert(xscan.get_x_best_metric() == 1);
24375  //assert(!xscan.is_eq_found());
24376  idx = xscan.get_ref_vector().get_row_idx(idx);
24377  bm::id64_t d64 = xscan.get_xor_digest();
24378  assert(d64 == ~bm::id64_t(0));
24379  assert(idx == 3); // matrix row 3
24380  }}
24381 
24382 
24383  // test chain compression search
24384  //
24385  // case 1: main (base) reference vector is found + a chain of improves
24386  cout << " case 1: main (base) reference vector is found + a chain of improves" << endl;
24387  {{
24388  const unsigned stride_len = 1024;
24390 
24391  bvect::size_type from = 0;
24392  unsigned mask = (1u << 1);
24393 
24394  for (unsigned k = 0; k < 2; ++k)
24395  {
24396  unsigned v = 1u | mask;
24397  for (unsigned i = 0; i < stride_len; i+=2)
24398  {
24399  bvect::size_type idx = from + i;
24400  sv.set(idx, v);
24401  } // for i
24402  from += stride_len;
24403  mask <<= 1;
24404  if (!mask)
24405  break;
24406  } // for k
24407 
24408  for (unsigned pass = 0; pass < 2; ++pass)
24409  {
24410  bm::xor_scanner<bvect> xscan;
24412  r_vect.build(sv.get_bmatrix());
24413  xscan.set_ref_vector(&r_vect);
24414 
24415  bm::xor_sim_params xs_params;
24416  bm::xor_sim_model<bvect> sim_model;
24417  xscan.compute_sim_model(sim_model, r_vect, xs_params);
24418 
24419  auto mchain = sim_model.matr.get(0, 0);
24420 
24421  assert(mchain.chain_size == 2);
24422  assert(mchain.ref_idx[0] == 1);
24423  assert(mchain.ref_idx[1] == 2);
24424  assert(mchain.xor_d64[0] == 1);
24425  assert(mchain.xor_d64[1] == 2);
24426 
24427  int brate = bm::check_pair_vect_vbr(mchain, r_vect);
24428  assert(brate == 1);
24429 
24430  sv.optimize();
24431  } // for
24432 
24433  }}
24434 
24435 
24436  // case 2: main (base) reference vector is found + a chain of improves
24437 
24438  cout << " case 2: main (base) reference vector is found + a chain of improves" << endl;
24439  {{
24440  const unsigned stride_len = 1024;
24441 
24442  for (unsigned pass = 2; pass < 63; ++pass)
24443  {
24445  bvect::size_type from = 0;
24446 
24447  unsigned mask = (1u << 1);
24448  for (unsigned k = 0; k < pass; ++k)
24449  {
24450  unsigned v = 1u | mask;
24451  for (unsigned i = 0; i < stride_len; i+=2)
24452  {
24453  bvect::size_type idx = from + i;
24454  sv.set(idx, v);
24455  } // for i
24456  from += stride_len;
24457  mask <<= 1;
24458  if (!mask)
24459  mask = 1u << 1;
24460  } // for k
24461 
24462  bm::xor_scanner<bvect> xscan;
24464  r_vect.build(sv.get_bmatrix());
24465  xscan.set_ref_vector(&r_vect);
24466 
24467  bm::xor_sim_params xs_params;
24468  bm::xor_sim_model<bvect> sim_model;
24469  xscan.compute_sim_model(sim_model, r_vect, xs_params);
24470 
24471 
24472  auto idx = xscan.get_ref_vector().find(unsigned(0));
24473  assert(idx == 0);
24474 
24475  auto mchain = sim_model.matr.get(0, 0);
24476  //cout << mchain.chain_size << endl;
24477  if (pass < 32)
24478  {
24479  assert(mchain.chain_size == pass);
24480  }
24481  else
24482  {
24483  assert(mchain.chain_size >= 30);
24484  }
24485 
24486  } // for pass
24487  }}
24488 
24489 
24490 // TODO: find a way to solve this case
24491 /*
24492 
24493  // case 3: main (base) reference vector is found + a chain of improves
24494  // (inverted)
24495  cout << "\n case 3: main (base) reference vector is found + a chain of improves (inverted)" << endl;
24496  {{
24497  const unsigned stride_len = 1024;
24498 
24499 // for (unsigned pass = 62; pass < 63; ++pass)
24500  unsigned pass = 63;
24501  {
24502  bm::sparse_vector<unsigned, bvect> sv;
24503  bvect::size_type from = 0;
24504 
24505  unsigned mask = (1u << 1);
24506  for (unsigned k = 0; k < pass; ++k)
24507  {
24508  unsigned v = 1u;
24509  for (unsigned i = 0; i < stride_len; i+=2)
24510  {
24511  bvect::size_type idx = from + i;
24512  sv.set(idx, v);
24513  sv.set(idx+1, mask); // mask is 1 shifted
24514  } // for i
24515  from += stride_len;
24516  mask <<= 1;
24517  if (!mask)
24518  mask = 1u << 1;
24519  } // for k
24520 
24521  bm::xor_scanner<bvect> xscan;
24522  bm::xor_scanner<bvect>::bv_ref_vector_type r_vect;
24523  r_vect.build(sv.get_bmatrix());
24524  xscan.set_ref_vector(&r_vect);
24525 
24526  bm::xor_sim_params xs_params;
24527  bm::xor_sim_model<bvect> sim_model;
24528  xscan.compute_sim_model(sim_model, r_vect, xs_params);
24529 
24530  auto mchain = sim_model.matr.get(0, 0);
24531  cout << mchain.chain_size << endl;
24532  if (pass < 32)
24533  {
24534  assert(mchain.chain_size == pass);
24535  }
24536  else
24537  {
24538  assert(mchain.chain_size >= 30);
24539  }
24540  } // for pass
24541  }}
24542 
24543 */
24544 
24545 
24546 
24547 
24548  cout << " -------------------------- TestSparseVector_XOR_Scanner() OK" << endl;
24549 }
24550 
24551 
24552 
24553 static
24555 {
24556  cout << "---------------------------- TestSparseVectorSerial()" << endl;
24557 
24558  cout << " test chain XOR serialization.. " << endl;
24563 
24564  {
24566 
24567  sv_ser.set_xor_ref(true);
24568 
24569  const unsigned stride_len = 1024;
24570 
24571  for (unsigned pass = 2; pass < 128; ++pass)
24572  {
24573  sparse_vector_u32 sv;
24574  bvect::size_type from = 0;
24575 
24576  unsigned mask = (1u << 1);
24577  for (unsigned k = 0; k < pass; ++k)
24578  {
24579  unsigned v = 1u | mask;
24580  for (unsigned i = 0; i < stride_len; i+=2)
24581  {
24582  bvect::size_type idx = from + i;
24583  sv.set(idx, v);
24584  } // for i
24585  from += stride_len;
24586  mask <<= 1;
24587  if (!mask)
24588  mask = 1u << 1;
24589  } // for k
24590 
24591  sv_ser.serialize(sv, sv_lay);
24592  const bvect::size_type* cstat = sv_ser.get_bv_serializer().get_compression_stat();
24593  assert(cstat[bm::set_block_xor_chain]>=1);
24594  {
24595  const unsigned char* buf = sv_lay.buf();
24596  sparse_vector_u32 sv2;
24597  sv_deserial.deserialize(sv2, buf);
24598 
24599  bool eq = sv.equal(sv2);
24600  assert(eq);
24601  }
24602 
24603  } // for pass
24604  }
24605 
24606 // bm::sparse_vector_serializer<sparse_vector_u32> sv_ser;
24607  sv_ser.set_xor_ref(false);
24608 //sv_ser.set_xor_ref(true);
24609 // bm::sparse_vector_deserializer<sparse_vector_u32> sv_deserial;
24610 
24611  for (unsigned pass = 0; pass < 2; ++pass)
24612  {
24613  // simple test gather for non-NULL vector
24614 
24615  {
24616  sparse_vector_u32 sv1, sv2, sv3;
24617 
24618  for (sparse_vector_u32::size_type i = 0; i < 10; ++i)
24619  sv1.push_back(i + 1);
24621  sv_ser.serialize(sv1, sv_lay);
24622  const unsigned char* buf = sv_lay.buf();
24623 
24625  bv_mask.set(0);
24626  bv_mask.set(2);
24627  sv_deserial.deserialize(sv2, buf, bv_mask);
24628 
24629  assert(sv2.size() == sv1.size());
24630  assert(sv2.get(0) == 1);
24631  cout << sv2.get(1) << endl;
24632  assert(sv2.get(1) == 0);
24633  assert(sv2.get(2) == 3);
24634 
24636  sv2.calc_stat(&st);
24637  assert(!st.bit_blocks);
24638  assert(st.gap_blocks);
24639 
24640  sv_deserial_ro.deserialize(sv3, buf, bv_mask);
24641  assert(sv3.is_ro());
24642  bool eq = sv2.equal(sv3);
24643  assert(eq);
24644 
24645  }
24646 
24647 
24648  // simple test gather for NULL-able vector
24649 
24650  {
24652  sparse_vector_u32 sv2(sv1);
24653  sparse_vector_u32 sv3;
24654 
24655  for (sparse_vector_u32::size_type i = 0; i < 100; i += 2)
24656  {
24657  sv1[i] = i + 1;
24658  }
24660  sv_ser.serialize(sv1, sv_lay);
24661  const unsigned char* buf = sv_lay.buf();
24662 
24663  {
24665  bv_mask.set(0);
24666  bv_mask.set(2);
24667  bv_mask.set(1024); // out of range mask
24668  sv_deserial.deserialize(sv2, buf, bv_mask);
24669 
24670 
24671  assert(sv2.get(0) == 1);
24672  assert(sv2.get(1) == 0);
24673  assert(sv2.get(2) == 3);
24674 
24675  const sparse_vector_u32::bvector_type* bv_null = sv2.get_null_bvector();
24676  auto cnt = bv_null->count();
24677  auto cnt1 = sv1.get_null_bvector()->count();
24678  assert(cnt == 2);
24679  assert(cnt != cnt1);
24680 
24682  sv2.calc_stat(&st);
24683  //assert(!st.bit_blocks);
24684  assert(st.gap_blocks);
24685 
24686  sv_deserial_ro.deserialize(sv3, buf, bv_mask);
24687  assert(sv3.is_ro());
24688  bool eq = sv2.equal(sv3);
24689  assert(eq);
24690 
24691  }
24692  {
24694  sv_deserial.deserialize(sv2, buf, bv_mask);
24695  assert(sv2.size() == sv1.size());
24696  const sparse_vector_u32::bvector_type* bv_null = sv2.get_null_bvector();
24697  auto cnt = bv_null->count();
24698  assert(cnt == sv2.get_null_bvector()->count());
24699  assert(sv2.get(0) == 0);
24700  assert(sv2.get(1) == 0);
24701  assert(sv2.get(2) == 0);
24702  }
24703  }
24704 
24705 
24706  // stress test gather deserialization
24707 
24708  cout << "Gather deserialization stress test..." << endl;
24709  {
24712  sparse_vector_u32 sv2(sv1);
24713  sparse_vector_u32 sv3(sv1);
24714 
24715  from = bm::id_max / 2;
24716  to = from + 75538;
24717 
24718  unsigned cnt = 0;
24719  for (sparse_vector_u32::size_type i = from; i < to; ++i, ++cnt)
24720  {
24721  if (cnt % 10 == 0)
24722  sv1.set_null(i);
24723  else
24724  sv1.set(i, cnt);
24725  } // for i
24726  sv1.sync_size();
24727 
24729 
24730  sv_ser.serialize(sv1, sv_lay);
24731  const unsigned char* buf = sv_lay.buf();
24732 
24733  {
24734 
24735  //bm::sparse_vector_deserializer<sparse_vector_u32> sv_deserial;
24737  sv_deserial.deserialize(sv4, buf);
24738  {
24740  bool is_eq = sv1.equal(sv4);
24741  if (!is_eq)
24742  {
24743  bool b = bm::sparse_vector_find_first_mismatch(sv1, sv4, midx);
24744  assert(b);
24745  cout << "Mismatch at pos = " << midx << endl;
24746  auto v1 = sv1[midx];
24747  auto v4 = sv4[midx];
24748  cout << "v1=" << v1 << " v4=" << v4 << " xor(v1, v4)=" << (v1 ^ v4) << endl;
24749  }
24750  assert(is_eq);
24751  }
24752 
24753 
24754  auto i = from;
24755  auto j = to;
24756  bool is_eq;
24758  bool found;
24759  //i = 59982;
24760  //i = 2147491602; j = 2147551230;
24761 
24762  for (i = from; i < j; ++i, --j)
24763  {
24765  bv_mask.set_range(i, j);
24766 
24767  sparse_vector_u32 sv_filt(sv1);
24768  sv_filt.filter(bv_mask);
24769  sparse_vector_u32 sv_range(bm::use_null);
24770  sv_range.copy_range(sv1, i, j);
24771 
24772  is_eq = sv_filt.equal(sv_range);
24773  assert(is_eq);
24774 
24775  sv_deserial.deserialize(sv2, buf, bv_mask);
24776 
24777  assert(sv2.size() == sv1.size());
24778  is_eq = sv2.equal(sv_range);
24779  if (!is_eq)
24780  {
24781  found = bm::sparse_vector_find_first_mismatch(sv2, sv_range, pos, bm::no_null);
24782  if (found)
24783  {
24784  auto vf = sv_filt.get(pos);
24785  auto v2 = sv2.get(pos);
24786  auto v3 = sv_range.get(pos);
24787 
24788  cerr << "Mismatch at:" << pos << endl;
24789  cerr << vf << "!=" << v2 << "!=" << v3 << endl;
24790  cerr << "[i, j] = " << i << ":" << j << endl;
24791 
24792  }
24793  assert(is_eq);
24794  }
24795 
24796  sv_deserial.deserialize(sv3, buf, i, j);
24797  {
24798  bool b = bm::sparse_vector_find_first_mismatch(sv3, sv_range, pos);
24799  if (b)
24800  {
24801  auto vf = sv_filt.get(pos);
24802  auto v2 = sv3.get(pos);
24803  auto v3 = sv_range.get(pos);
24804  auto xd = v2 ^ v3;
24805 
24806  cerr << "Mismatch at:" << pos << " xor diff=" << xd << endl;
24807  cerr << vf << "!=" << v2 << "!=" << v3 << endl;
24808  cerr << "[i, j] = " << i << ":" << j << endl;
24809  }
24810  assert(!b);
24811  }
24812  is_eq = sv2.equal(sv3);
24813  if (!is_eq)
24814  {
24815  cerr << "Error: Range deserialization equality failed!" << endl;
24816  assert(0); exit(1);
24817  }
24818 
24819  {
24821  sv_deserial_ro.deserialize(sv5, buf, i, j);
24822  is_eq = sv2.equal(sv5);
24823  if (!is_eq)
24824  {
24825  cerr << "Error: Range deserialization equality failed!(2)" << endl;
24826  assert(0); exit(1);
24827  }
24828  }
24829 
24830 
24831 
24832  //sv3.filter(bv_mask);
24833 
24834  found = bm::sparse_vector_find_first_mismatch(sv_filt, sv3, pos, bm::no_null);
24835  if (found)
24836  {
24837  found = bm::sparse_vector_find_first_mismatch(sv_filt, sv3, pos, bm::no_null);
24838 
24839  auto vf = sv_filt.get(pos);
24840  auto v1 = sv1.get(pos);
24841  auto v3 = sv3.get(pos);
24842 
24843  cerr << vf << "!=" << v3 << "!=" << v1 << endl;
24844  cerr << "Filter Range deserialization mismatch found! at pos=" << pos << endl;
24845  cerr << "[" << i << ".." << j << "]" << endl;
24846  assert(0); exit(1);
24847  }
24848 
24849  found = bm::sparse_vector_find_first_mismatch(sv_range, sv2, pos, bm::no_null);
24850  if (found)
24851  {
24852  cerr << "Range deserialization mismatch found! at pos=" << pos << endl;
24853  cerr << "[" << i << ".." << j << "]" << endl;
24854  assert(0); exit(1);
24855  }
24856  /*
24857  for (auto k = i; k < j; ++k)
24858  {
24859  auto v1 = sv1.get(k);
24860  auto v2 = sv2.get(k);
24861  if (v1 != v2)
24862  {
24863  cerr << "Error:Range deserialization discrepancy!" << endl;
24864  assert(0); exit(1);
24865  }
24866  auto n1 = sv1.is_null(k);
24867  auto n2 = sv2.is_null(k);
24868  if (n1 != n2)
24869  {
24870  cerr << "Error:Range NULL deserialization discrepancy!" << endl;
24871  assert(0); exit(1);
24872  }
24873  } // for k
24874  */
24875  if (i % 0xFF == 0)
24876  {
24877  if (!is_silent)
24878  std::cout << "\r" << j - i << flush;
24879  }
24880 
24881  } // for i
24882  }
24883  cout << "\nOK\n" << endl;
24884  }
24885 
24886  sv_ser.set_xor_ref(true);
24887  } // for pass
24888 
24889  cout << "---------------------------- Test sparse vector serializer OK" << endl;
24890 }
24891 
24892 static
24894 {
24895  cout << "---------------------------- TestSignedSparseVectorSerial()" << endl;
24896 
24897  cout << " test chain XOR serialization.. " << endl;
24900 /*
24901  {
24902  sparse_vector_serial_layout<sparse_vector_i32> sv_lay;
24903 
24904  sv_ser.set_xor_ref(true);
24905 
24906  const unsigned stride_len = 1024;
24907 
24908  for (unsigned pass = 2; pass < 128; ++pass)
24909  {
24910  sparse_vector_i32 sv;
24911  bvect::size_type from = 0;
24912 
24913  unsigned mask = (1u << 1);
24914  for (unsigned k = 0; k < pass; ++k)
24915  {
24916  sparse_vector_i32::value_type v = -int(1u | mask);
24917  for (unsigned i = 0; i < stride_len; i+=2)
24918  {
24919  bvect::size_type idx = from + i;
24920  sv.set(idx, v);
24921  } // for i
24922  from += stride_len;
24923  mask <<= 1;
24924  if (!mask)
24925  mask = 1u << 1;
24926  } // for k
24927 
24928  sv_ser.serialize(sv, sv_lay);
24929  const bvect::size_type* cstat = sv_ser.get_bv_serializer().get_compression_stat();
24930  assert(cstat[bm::set_block_xor_chain]>=1);
24931  {
24932  const unsigned char* buf = sv_lay.buf();
24933  sparse_vector_i32 sv2;
24934  sv_deserial.deserialize(sv2, buf);
24935 
24936  bool eq = sv.equal(sv2);
24937  assert(eq);
24938  }
24939 
24940  } // for pass
24941  }
24942 */
24943 // bm::sparse_vector_serializer<sparse_vector_u32> sv_ser;
24944 // bm::sparse_vector_deserializer<sparse_vector_u32> sv_deserial;
24945 
24946  for (unsigned pass = 0; pass < 2; ++pass)
24947  {
24948  if (!pass)
24949  {
24950  cout << " XOR ref compression is ON" << endl;
24951  sv_ser.set_xor_ref(true);
24952  }
24953  else
24954  {
24955  cout << " XOR ref compression is OFF" << endl;
24956  sv_ser.set_xor_ref(false);
24957  }
24958  // simple test gather for non-NULL vector
24959 
24960  {
24961  sparse_vector_i32 sv1;
24962  sparse_vector_i32 sv2;
24963 
24964  for (sparse_vector_i32::value_type i = 0; i < 10; ++i)
24965  sv1.push_back(0-(i + 1));
24967  sv_ser.serialize(sv1, sv_lay);
24968  const unsigned char* buf = sv_lay.buf();
24969 
24971  bv_mask.set(0);
24972  bv_mask.set(2);
24973  sv_deserial.deserialize(sv2, buf, bv_mask);
24974 
24975  assert(sv2.size() == sv1.size());
24976  assert(sv2.get(0) == -1);
24977  cout << sv2.get(1) << endl;
24978  assert(sv2.get(1) == 0);
24979  assert(sv2.get(2) == -3);
24980 
24982  sv2.calc_stat(&st);
24983  assert(!st.bit_blocks);
24984  assert(st.gap_blocks);
24985  }
24986 
24987 
24988  // simple test gather for NULL-able vector
24989 
24990  {
24992  sparse_vector_i32 sv2(sv1);
24993 
24994  for (sparse_vector_u32::value_type i = 0; i < 100; i += 2)
24995  {
24996  sv1[i] = -int(i + 1);
24997  }
24999  sv_ser.serialize(sv1, sv_lay);
25000  const unsigned char* buf = sv_lay.buf();
25001 
25002  //bm::sparse_vector_deserializer<sparse_vector_u32> sv_deserial;
25003 
25004  {
25006  bv_mask.set(0);
25007  bv_mask.set(2);
25008  bv_mask.set(1024); // out of range mask
25009  sv_deserial.deserialize(sv2, buf, bv_mask);
25010 
25011 
25012  assert(sv2.get(0) == -1);
25013  assert(sv2.get(1) == 0);
25014  assert(sv2.get(2) == -3);
25015 
25016  const sparse_vector_u32::bvector_type* bv_null = sv2.get_null_bvector();
25017  auto cnt = bv_null->count();
25018  auto cnt1 = sv1.get_null_bvector()->count();
25019  assert(cnt == 2);
25020  assert(cnt != cnt1);
25021 
25023  sv2.calc_stat(&st);
25024  //assert(!st.bit_blocks);
25025  assert(st.gap_blocks);
25026  }
25027  {
25029  sv_deserial.deserialize(sv2, buf, bv_mask);
25030  assert(sv2.size() == sv1.size());
25031  const sparse_vector_u32::bvector_type* bv_null = sv2.get_null_bvector();
25032  auto cnt = bv_null->count();
25033  assert(cnt == sv2.get_null_bvector()->count());
25034  assert(sv2.get(0) == 0);
25035  assert(sv2.get(1) == 0);
25036  assert(sv2.get(2) == 0);
25037  }
25038  }
25039 
25040 
25041  // stress test gather deserialization
25042 
25043  cout << "Gather deserialization stress test..." << endl;
25044  {
25047  sparse_vector_i32 sv2(sv1);
25048  sparse_vector_i32 sv3(sv1);
25049 
25050  from = bm::id_max / 2;
25051  to = from + 75538;
25052 
25054  for (sparse_vector_u32::size_type i = from; i < to; ++i, ++cnt)
25055  {
25056  if (cnt % 10 == 0)
25057  sv1.set_null(i);
25058  else
25060  } // for i
25061  sv1.sync_size();
25062 
25064 
25065  sv_ser.serialize(sv1, sv_lay);
25066  const unsigned char* buf = sv_lay.buf();
25067 
25068  {
25069 
25070  //bm::sparse_vector_deserializer<sparse_vector_u32> sv_deserial;
25072  sv_deserial.deserialize(sv4, buf);
25073  {
25075  bool is_eq = sv1.equal(sv4);
25076  if (!is_eq)
25077  {
25078  bool b = bm::sparse_vector_find_first_mismatch(sv1, sv4, midx);
25079  assert(b);
25080  cout << "Mismatch at pos = " << midx << endl;
25081  auto v1 = sv1[midx];
25082  auto v4 = sv4[midx];
25083  cout << "v1=" << v1 << " v4=" << v4 << " xor(v1, v4)=" << (v1 ^ v4) << endl;
25084  }
25085  assert(is_eq);
25086  }
25087 
25088  auto i = from;
25089  auto j = to;
25090  bool is_eq;
25092  bool found;
25093  //i = 59982;
25094  //i = 2147491602; j = 2147551230;
25095 
25096  for (i = from; i < j; ++i, --j)
25097  {
25099  bv_mask.set_range(i, j);
25100 
25101  sparse_vector_i32 sv_filt(sv1);
25102  sv_filt.filter(bv_mask);
25103  sparse_vector_i32 sv_range(bm::use_null);
25104  sv_range.copy_range(sv1, i, j);
25105 
25106  is_eq = sv_filt.equal(sv_range);
25107  assert(is_eq);
25108 
25109  sv_deserial.deserialize(sv2, buf, bv_mask);
25110 
25111  assert(sv2.size() == sv1.size());
25112  is_eq = sv2.equal(sv_range);
25113  if (!is_eq)
25114  {
25115  found = bm::sparse_vector_find_first_mismatch(sv2, sv_range, pos, bm::no_null);
25116  if (found)
25117  {
25118  auto vf = sv_filt.get(pos);
25119  auto v2 = sv2.get(pos);
25120  auto v3 = sv_range.get(pos);
25121 
25122  cerr << "Mismatch at:" << pos << endl;
25123  cerr << vf << "!=" << v2 << "!=" << v3 << endl;
25124  cerr << "[i, j] = " << i << ":" << j << endl;
25125 
25126  }
25127  assert(is_eq);
25128  }
25129 
25130  sv_deserial.deserialize(sv3, buf, i, j);
25131  {
25132  bool b = bm::sparse_vector_find_first_mismatch(sv3, sv_range, pos);
25133  if (b)
25134  {
25135  auto vf = sv_filt.get(pos);
25136  auto v2 = sv3.get(pos);
25137  auto v3 = sv_range.get(pos);
25138  auto xd = v2 ^ v3;
25139 
25140  cerr << "Mismatch at:" << pos << " xor diff=" << xd << endl;
25141  cerr << vf << "!=" << v2 << "!=" << v3 << endl;
25142  cerr << "[i, j] = " << i << ":" << j << endl;
25143  }
25144  assert(!b);
25145  }
25146  is_eq = sv2.equal(sv3);
25147  if (!is_eq)
25148  {
25149  cerr << "Error: Range deserialization equality failed!" << endl;
25150  assert(0); exit(1);
25151  }
25152 
25153  //sv3.filter(bv_mask);
25154 
25155  found = bm::sparse_vector_find_first_mismatch(sv_filt, sv3, pos, bm::no_null);
25156  if (found)
25157  {
25158  found = bm::sparse_vector_find_first_mismatch(sv_filt, sv3, pos, bm::no_null);
25159 
25160  auto vf = sv_filt.get(pos);
25161  auto v1 = sv1.get(pos);
25162  auto v3 = sv3.get(pos);
25163 
25164  cerr << vf << "!=" << v3 << "!=" << v1 << endl;
25165  cerr << "Filter Range deserialization mismatch found! at pos=" << pos << endl;
25166  cerr << "[" << i << ".." << j << "]" << endl;
25167  assert(0); exit(1);
25168  }
25169 
25170  found = bm::sparse_vector_find_first_mismatch(sv_range, sv2, pos, bm::no_null);
25171  if (found)
25172  {
25173  cerr << "Range deserialization mismatch found! at pos=" << pos << endl;
25174  cerr << "[" << i << ".." << j << "]" << endl;
25175  assert(0); exit(1);
25176  }
25177  /*
25178  for (auto k = i; k < j; ++k)
25179  {
25180  auto v1 = sv1.get(k);
25181  auto v2 = sv2.get(k);
25182  if (v1 != v2)
25183  {
25184  cerr << "Error:Range deserialization discrepancy!" << endl;
25185  assert(0); exit(1);
25186  }
25187  auto n1 = sv1.is_null(k);
25188  auto n2 = sv2.is_null(k);
25189  if (n1 != n2)
25190  {
25191  cerr << "Error:Range NULL deserialization discrepancy!" << endl;
25192  assert(0); exit(1);
25193  }
25194  } // for k
25195  */
25196  if (i % 0xFF == 0)
25197  {
25198  if (!is_silent)
25199  std::cout << "\r" << j - i << flush;
25200  }
25201 
25202  } // for i
25203  }
25204  cout << "\nOK\n" << endl;
25205  }
25206 
25207  //sv_ser.set_xor_ref(true);
25208  } // for pass
25209 
25210  cout << "---------------------------- TestSignedSparseVectorSerial()" << endl;
25211 }
25212 
25213 
25214 static
25216 {
25217  cout << "---------------------------- Test sparse vector inserter" << endl;
25218 
25219  {
25225 
25226  assert(bi2.empty());
25227  assert(bi3.empty());
25228 
25229  bi4 = bi2;
25230  assert(bi4.empty());
25231 
25232  for (unsigned i = 0; i < 1280000; ++i)
25233  {
25234  if (i % 100 == 0)
25235  {
25236  sv1.set_null(i);
25237  bi2.add_null();
25238  }
25239  else
25240  {
25241  sv1.set(i, i);
25242  *bi2 = i;
25243  }
25244  assert(!bi2.empty());
25245  }
25246  bi2.flush();
25247 
25248  if (!sv1.equal(sv2))
25249  {
25250  cout << "ERROR! sparse_vector back_insert_iterator mismatch." << endl;
25251  exit(1);
25252  }
25253  }
25254 
25255  {
25259 
25260  for (unsigned i = 0; i < 1280000; ++i)
25261  {
25262  if (i % 100 == 0)
25263  {
25264  sv1.set_null(i);
25265  ++i;
25266  sv1.set_null(i);
25267  bi2.add_null(2);
25268  }
25269  else
25270  {
25271  sv1.set(i, i);
25272  *bi2 = i;
25273  }
25274  if (i % 10000 == 0)
25275  {
25276  bi2.flush();
25277  }
25278 
25279  }
25280  bi2.flush();
25281 
25282  if (!sv1.equal(sv2))
25283  {
25284  cout << "ERROR! (2)sparse_vector back_insert_iterator mismatch." << endl;
25285  exit(1);
25286  }
25287  }
25288 
25289 
25290  cout << "---------------------------- Bit-plane sparse vector inserter OK" << endl;
25291 }
25292 
25293 template<class SV>
25294 void CheckSparseVectorGather(const SV& sv,
25295  unsigned from, unsigned to, typename SV::value_type control_value = 0)
25296 {
25297  assert(sv.size());
25298  assert (to >= from);
25299  typedef typename SV::value_type sv_value_type;
25300 
25301  unsigned gather_size = to - from + 1;
25302  std::vector<sv_value_type> target_v;
25303  std::vector<sv_value_type> target_v_control;
25304  std::vector<unsigned> idx_v;
25305  target_v.resize(gather_size);
25306  target_v_control.resize(gather_size);
25307  idx_v.reserve(gather_size);
25308 
25309  for (unsigned i = from; i <= to; ++i)
25310  {
25311  idx_v.push_back(i);
25312  }
25313  sv.decode(target_v_control.data(), from, gather_size);
25314 
25315 
25316  sv.gather(target_v.data(), idx_v.data(), gather_size, BM_SORTED);
25317  for (unsigned i = 0; i < gather_size; ++i)
25318  {
25319  sv_value_type vg = target_v[i];
25320  sv_value_type vc = target_v_control[i];
25321  if (vg != vc)
25322  {
25323  cerr << "Error! gather/decode control mismatch " << vc << " " << vg
25324  << " at=" << i << endl;
25325  cerr << control_value << endl;
25326  assert(0);exit(1);
25327  }
25328  }
25329 
25330  sv.gather(target_v.data(), idx_v.data(), gather_size, BM_UNSORTED);
25331  for (unsigned i = 0; i < gather_size; ++i)
25332  {
25333  sv_value_type vg = target_v[i];
25334  sv_value_type vc = target_v_control[i];
25335  if (vg != vc)
25336  {
25337  cerr << "Error! gather/decode control mismatch " << vc << " " << vg
25338  << " at=" << i << endl;
25339  cerr << control_value << endl;
25340  assert(0);exit(1);
25341  }
25342  }
25343 
25344  sv.gather(target_v.data(), idx_v.data(), gather_size, BM_UNKNOWN);
25345  for (unsigned i = 0; i < gather_size; ++i)
25346  {
25347  sv_value_type vg = target_v[i];
25348  sv_value_type vc = target_v_control[i];
25349  if (vg != vc)
25350  {
25351  cerr << "Error! gather/decode control mismatch " << vc << " " << vg
25352  << " at=" << i << endl;
25353  cerr << control_value << endl;
25354  assert(0); exit(1);
25355  }
25356  }
25357 
25358 
25359 #if 0
25360  // detailed check (very slow)
25361  unsigned k = 0;
25362  for (unsigned i = from; i <= to; ++i, ++k)
25363  {
25364  unsigned v1 = i;
25365  sv_value_type v2 = target_v[k];
25366  if (v1 != v2)
25367  {
25368  if (control_value)
25369  {
25370  if (v2 != control_value)
25371  {
25372  cerr << "Error! gather control mismatch " << control_value << " " << v2
25373  << " at=" << i << endl;
25374  exit(1);
25375  }
25376  }
25377  else
25378  {
25379  v1 = sv.get(i);
25380  if (v1 != v2)
25381  {
25382  cerr << "Error! gather mismatch " << v1 << " " << v2
25383  << " at=" << i << endl;
25384  exit(1);
25385  }
25386  }
25387  }
25388  } // for
25389 #endif
25390 }
25391 
25392 static
25394  unsigned gather_size)
25395 {
25396  assert(sv.size());
25397 
25398  if (gather_size == 0)
25399  gather_size = 1;
25400 
25401  std::vector<unsigned> target_v;
25402  std::vector<unsigned> idx_v;
25403  target_v.resize(gather_size);
25404  idx_v.reserve(gather_size);
25405 
25406  for (unsigned i = 0; i < gather_size; ++i)
25407  {
25408  unsigned r_idx = unsigned(rand()) % (sv.size()-1);
25409  idx_v.push_back(r_idx);
25410  }
25411 
25412  sv.gather(target_v.data(), idx_v.data(), gather_size, BM_UNSORTED);
25413 
25414  unsigned k = 0;
25415  for (unsigned i = 0; i < gather_size; ++i, ++k)
25416  {
25417  unsigned v1 = sv.get(idx_v[k]);
25418  unsigned v2 = target_v[k];
25419  if (v1 != v2)
25420  {
25421  {
25422  cerr << "Error! random gather mismatch " << v1 << " " << v2
25423  << " at=" << i << endl;
25424  exit(1);
25425  }
25426  }
25427  } // for
25428 }
25429 
25430 
25431 static
25433 {
25434  cout << "---------------------------- Test sparse vector gather decode" << endl;
25435 
25436 
25437  {
25438  unsigned base = 0;
25439  sparse_vector_u32 sv0;
25440  sparse_vector_i32 sv1;
25441 
25442  sv0[base+0] = 0;
25443  sv0[base+1] = 1;
25444  sv0[base+2] = 1;
25445  sv0[base+3] = 1;
25446 
25447  sv1[base+0] = 0;
25448  sv1[base+1] = -1;
25449  sv1[base+2] = 1;
25450  sv1[base+3] = INT32_MIN;
25451 
25452  for (unsigned pass = 0; pass < 2; ++pass)
25453  {
25454  {
25455  unsigned d[32] = { 25, };
25456  auto sz = sv0.decode(&d[0], base + 1, 2);
25457  assert(sz == 2);
25458  assert(d[0] == 1);
25459  assert(d[1] == 1);
25460  sz = sv0.decode(&d[0], base + 2, 2);
25461  assert(sz == 2);
25462  assert(d[0] == 1);
25463  assert(d[1] == 1);
25464  }
25465 
25466  {
25467  int d1[32] = { 25, };
25468  auto sz = sv1.decode(&d1[0], base + 1, 2);
25469  assert(sz == 2);
25470  assert(d1[0] == -1);
25471  assert(d1[1] == 1);
25472  sz = sv1.decode(&d1[0], base + 2, 2);
25473  assert(sz == 2);
25474  assert(d1[0] == 1);
25475  assert(d1[1] == INT32_MIN);
25476  }
25477 
25478  sv0.optimize();
25479  }
25480  }
25481 
25482  sparse_vector_u32 sv;
25483  sparse_vector_u32 sv2;
25484  sparse_vector_u32 sv3;
25485  sparse_vector_i32 sv4;
25486  sparse_vector_i32 sv5;
25487  const unsigned control_value = 9;
25488  {
25489  cout << " Filling..." << flush;
25493 
25494  unsigned max_size = 1280000;
25495  for (unsigned i = 0; i < max_size; ++i)
25496  {
25497  *bi = i;
25498  *bi2 = control_value;
25499  *bi4 = -int(i);
25500  }
25501  bi.flush(); bi2.flush(); bi4.flush();
25502  sv2.optimize(); sv4.optimize();
25503 
25504  for (unsigned i = 0; i < max_size; i+=200)
25505  {
25506  sv3[i] = i;
25507  sv5[i] = -int(i);
25508  }
25509  sv3.optimize(); sv5.optimize();
25510 
25511  cout << "ok" << endl;
25512  }
25513 
25514  {
25515  cout << "Test 1 (regular pattern)" << endl;
25516  unsigned probe_to = 100000;
25517  time_t start_time = time(0);
25518  time_t finish_time;
25519 
25520  for (unsigned i = 0; i < probe_to; ++i)
25521  {
25522  CheckSparseVectorGather(sv, i, i);
25523  CheckSparseVectorGather(sv2, i, i, control_value);
25524  CheckSparseVectorGather(sv3, i, i);
25525  CheckSparseVectorGather(sv4, i, i);
25526  unsigned depth = (unsigned)rand() % 30000;
25528  CheckSparseVectorGather(sv2, i, i+depth, control_value);
25530  if (i % 500 == 0)
25531  {
25532  finish_time = time(0);
25533  if (!is_silent)
25534  cout << "\r" << i << "/" << probe_to
25535  << " [" << (finish_time - start_time) << "]" << flush;
25536  start_time = time(0);
25537  }
25538  }
25539  cout << endl;
25540  }
25541 
25542  {
25543  cout << "Test 2 (random pattern)" << endl;
25544  unsigned probe_to = 100000;
25545  time_t start_time = time(0);
25546  time_t finish_time;
25547 
25548  for (unsigned i = 0; i < probe_to; ++i)
25549  {
25550  unsigned gsize = (unsigned)rand()%2024;
25551  CheckSparseVectorGatherRandom(sv, gsize);
25552  CheckSparseVectorGatherRandom(sv2, gsize);
25553  CheckSparseVectorGatherRandom(sv3, gsize);
25554  if (i % 500 == 0)
25555  {
25556  finish_time = time(0);
25557  if (!is_silent)
25558  cout << "\r" << i << "/" << probe_to
25559  << " [" << (finish_time - start_time) << "]" << flush;
25560  start_time = time(0);
25561  }
25562  }
25563  cout << endl;
25564  }
25565 
25566 
25567  cout << "---------------------------- Test sparse vector gather decode OK" << endl;
25568 }
25569 
25570 
25571 
25572 template<class SV>
25573 void bvector_transform_11(typename SV::bvector_type& bvect_in,
25574  const SV& sv_brel,
25575  typename SV::bvector_type& bvect_out)
25576 {
25577  bm::set2set_11_transform<SV> bin_trans;
25578  bin_trans.run(bvect_in, sv_brel, bvect_out);
25579 }
25580 
25581 template<class SV>
25582 void CheckSparseVectorRange(const SV& sv,
25583  unsigned left, unsigned right)
25584 {
25585  using value_type = typename SV::value_type;
25586  SV sv1(bm::use_null);
25587  SV sv2(sv);
25588  sv1.copy_range(sv, left, right);
25589 
25590  if (right >= sv.size())
25591  {
25592  right = sv.size()-1;
25593  }
25594 
25595  if (left == right)
25596  {
25597  value_type v1 = sv.get(left);
25598  value_type v2 = sv1[right];
25599  assert(v1 == v2);
25600  return;
25601  }
25602 
25603  if (left)
25604  {
25605  sv2.clear_range(0, left-1, true);
25606  }
25607  if (right < sv2.size()-1)
25608  {
25609  sv2.clear_range(right+1, sv2.size()-1, true);
25610  }
25611 
25612  bool same = sv2.equal(sv1);
25613  if (!same)
25614  {
25615  cerr << "Hmmm... Range comaprison failed, detailed check..." << endl;
25616  cerr << "[" << left << ".." << right << "]" << endl;
25617  for (unsigned i = left; i <= right; ++i)
25618  {
25619  value_type v1 = sv.get(i);
25620  value_type v2 = sv1[i];
25621  if (v1 != v2)
25622  {
25623  cerr << "Error! Copy range check failed at:" << i << endl;
25624  exit(1);
25625  }
25626  } // for
25627  cerr << "detailed check did not find issues. error in test?" << endl;
25628  exit(1);
25629  }
25630 }
25631 
25632 static
25634 {
25635  cout << " ---------------- Sparse vector Range partitioning test" << endl;
25636 
25637  cout << "Basic check" << endl;
25638  {
25640  sv.set(2, 25);
25641  sv.set(3, 35);
25642  sv.set(7, 75);
25643  sv.set(10, 2);
25644  sv.set(21, 201);
25645 
25646  CheckSparseVectorRange(sv, 0, 0);
25647  CheckSparseVectorRange(sv, 2, 2);
25648  CheckSparseVectorRange(sv, 7, 10);
25649  }
25650  {
25652  sv.set(2, 25);
25653  sv.set(3, 35);
25654  sv.set(7, 75);
25655  sv.set(10, 2);
25656  sv.set(21, 201);
25657 
25658  CheckSparseVectorRange(sv, 0, 0);
25659  CheckSparseVectorRange(sv, 2, 2);
25660  CheckSparseVectorRange(sv, 7, 10);
25661  }
25662 
25663  cout << "Stress check 1 (constant)" << endl;
25664  {
25666  const unsigned sv_max = 120000;
25667  cout << "Filling the vector" << endl;
25668  for (unsigned i = 0; i < sv_max; ++i)
25669  {
25670  sv.push_back(9);
25671  }
25672 
25673  cout << "Phase 1.." << endl;
25674  for (unsigned i = 0; i < sv_max; ++i)
25675  {
25676  CheckSparseVectorRange(sv, 0, i);
25677  CheckSparseVectorRange(sv, i, sv_max+10);
25678  if (!is_silent)
25679  cout << "\r" << i << "/" << sv_max << flush;
25680  }
25681  cout << endl;
25682 
25683  cout << "\nPhase 2.." << endl;
25684  unsigned k = sv_max;
25685  for (unsigned i = 0; i < k; ++i, --k)
25686  {
25687  CheckSparseVectorRange(sv, i, k);
25688  }
25689 
25690  sv.optimize();
25691 
25692  cout << "Phase 3.." << endl;
25693  for (unsigned i = 0; i < sv_max; ++i)
25694  {
25695  CheckSparseVectorRange(sv, 0, i);
25696  CheckSparseVectorRange(sv, i, sv_max+10);
25697  }
25698 
25699  cout << "Phase 4.." << endl;
25700  k = sv_max;
25701  for (unsigned i = 0; i < k; ++i, --k)
25702  {
25703  CheckSparseVectorRange(sv, i, k);
25704  }
25705 
25706  }
25707 
25708  cout << "\nStress check 2 (liner function)" << endl;
25709  {
25711  const unsigned sv_max = 250000;
25712  cout << "Filling the vector" << endl;
25713  for (unsigned i = 0; i < sv_max; ++i)
25714  {
25715  sv.push_back(i);
25716  }
25717 
25718  cout << "Phase 2-1.." << endl;
25719  for (unsigned i = 0; i < sv_max; ++i)
25720  {
25721  CheckSparseVectorRange(sv, i, i);
25722  CheckSparseVectorRange(sv, 0, i);
25723  CheckSparseVectorRange(sv, i, sv_max+10);
25724  if (!is_silent)
25725  if (i % 256 == 0)
25726  cout << "\r" << i << "/" << sv_max << flush;
25727  }
25728 
25729  cout << "\nPhase 2-2.." << endl;
25730  unsigned k = sv_max;
25731  for (unsigned i = 0; i < k; ++i, --k)
25732  {
25733  CheckSparseVectorRange(sv, i, k);
25734  }
25735  }
25736 
25737  cout << " ---------------- Sparse vector Range partitioning test OK\n" << endl;
25738 }
25739 
25740 template<class SV>
25741 void CheckSparseVectorFilter(const SV& sv, unsigned factor)
25742 {
25743  using value_type = typename SV::value_type;
25744  sparse_vector_u32 sv1(sv);
25746  for (unsigned i = 0; i < sv.size(); ++i)
25747  {
25748  if (i % factor == 0)
25749  bv_mask.set(i);
25750  }
25751  sv1.filter(bv_mask);
25752  for (unsigned i = 0; i < sv.size(); ++i)
25753  {
25754  value_type v = sv.get(i);
25755  bool is_null = sv.is_null(i);
25756  value_type v1 = sv1.get(i);
25757  bool is_null1 = sv1.is_null(i);
25758 
25759  if (i % factor == 0)
25760  {
25761  if (v != v1 || is_null != is_null1)
25762  {
25763  cerr << "Error! (1)sparse_vector<>::filter() failed at:" << i << endl;
25764  exit(1);
25765  }
25766  }
25767  else
25768  {
25769  if (v == v1 || is_null == is_null1)
25770  {
25771  cerr << "Error! (2)sparse_vector<>::filter() failed at:" << i << endl;
25772  exit(1);
25773  }
25774  }
25775  }
25776 }
25777 
25778 static
25780 {
25781  cout << " ---------------- Sparse vector Filter test" << endl;
25782  cout << "Basic check" << endl;
25783  {
25785  sv.set(2, 25);
25786  sv.set(3, 35);
25787  sv.set(7, 75);
25788  sv.set(10, 2);
25789  sv.set(21, 201);
25790 
25791  sparse_vector_u32::bvector_type bv_mask { 2, 7 };
25792 
25793  sv.filter(bv_mask);
25794 
25795  for (unsigned i = 0; i < sv.size(); ++i)
25796  {
25797  unsigned v = sv.get(i);
25798  bool is_null = sv.is_null(i);
25799  if (i == 2 || i == 7)
25800  {
25801  assert(v != 0);
25802  assert(!is_null);
25803  }
25804  else
25805  {
25806  assert(v == 0);
25807  assert(is_null);
25808  }
25809  }
25810  }
25811 
25812  {
25814  sv.set(2, 25);
25815  sv.set(3, 35);
25816  sv.set(7, 75);
25817  sv.set(10, 2);
25818  sv.set(21, 201);
25819 
25820  sparse_vector_i32::bvector_type bv_mask { 2, 7 };
25821 
25822  sv.filter(bv_mask);
25823 
25824  for (unsigned i = 0; i < sv.size(); ++i)
25825  {
25827  bool is_null = sv.is_null(i);
25828  if (i == 2 || i == 7)
25829  {
25830  assert(v != 0);
25831  assert(!is_null);
25832  }
25833  else
25834  {
25835  assert(v == 0);
25836  assert(is_null);
25837  }
25838  } // for
25839  }
25840 
25841 
25842  cout << "Stress check 1" << endl;
25843 
25844  {
25846  const unsigned sv_max = 250000;
25847  cout << "Filling the vector ... " << flush;
25848  for (unsigned i = 0; i < sv_max; ++i)
25849  {
25850  sv.push_back(i);
25851  }
25852  sv[0] = 113213;
25853 
25854 
25856  for (unsigned i = 0; i < sv_max; ++i)
25857  {
25858  if (i % 2 == 0)
25859  bv_mask.set(i);
25860  }
25861  cout << "done." << endl;
25862 
25863  sv.filter(bv_mask);
25864  for (unsigned i = 0; i < sv_max; ++i)
25865  {
25866  unsigned v = sv.get(i);
25867  bool is_null = sv.is_null(i);
25868  if (i % 2 == 0)
25869  {
25870  assert(v == i || (i == 0 && v == 113213));
25871  assert(!is_null);
25872  }
25873  else
25874  {
25875  assert(v == 0);
25876  assert(is_null);
25877  }
25878  }
25879  }
25880 
25881  cout << "Stress check 2" << endl;
25882 
25883  {
25885  const unsigned sv_max = 250000;
25886  cout << "Filling the vector ... " << flush;
25887  for (unsigned i = 0; i < sv_max; ++i)
25888  {
25889  sv.push_back(i);
25890  }
25891  cout << "done" << endl;
25892 
25893  const unsigned max_factor = 10000;
25894  for (unsigned i = 2; i < max_factor; ++i)
25895  {
25897  if (!is_silent)
25898  if (i % 256 == 0)
25899  cout << "\r" << i << "/" << max_factor << flush;
25900  }
25901  cout << endl;
25902  }
25903 
25904  cout << " ---------------- Sparse vector Filter test OK" << endl;
25905 }
25906 
25907 
25908 
25909 static
25911 {
25912  cout << " ---------------- Test set transformation with sparse vector" << endl;
25913 
25914  {
25916  bvect bv_in { 1, 2, 3, 10, 20 };
25917  bvect bv_out;
25918 
25919  bvector_transform_11(bv_in, sv, bv_out);
25920  assert(bv_out.count() == 0);
25921  cout << "Transform11 with empty sv - ok" << endl;
25922 
25924  unsigned to;
25925  bool found = set2set.remap(0, sv, to);
25926  assert(!found);
25927  found = set2set.remap(3, sv, to);
25928  assert(!found);
25929 
25930  }
25931 
25932  {
25934 
25935  sv.set(2, 25);
25936  sv.set(3, 35);
25937  sv.set(7, 75);
25938  sv.set(10, 2);
25939  sv.set(21, 201);
25940 
25942  unsigned to;
25943  bool found = set2set.remap(0, sv, to);
25944  assert(!found);
25945  found = set2set.remap(3, sv, to);
25946  assert(found);
25947  assert(to == 35);
25948 
25949  bvect bv_in { 1, 2, 3, 10, 20 };
25950  bvect bv_control {25, 35, 2 };
25951 
25952  {
25953  bvect bv_out;
25954  bvector_transform_11(bv_in, sv, bv_out);
25955  int cmp = bv_control.compare(bv_out);
25956  if (cmp != 0)
25957  {
25958  cerr << "Transform11 (1) control comparison failed" << endl;
25959  exit(1);
25960  }
25961 
25962  sv.optimize();
25963  bv_out.clear();
25964 
25965  bvector_transform_11(bv_in, sv, bv_out);
25966  cmp = bv_control.compare(bv_out);
25967  if (cmp != 0)
25968  {
25969  cerr << "Transform11 (1, 1) control comparison failed" << endl;
25970  exit(1);
25971  }
25972  }
25973 
25974  cout << "Transform11 (1) - ok" << endl;
25975  }
25976  {
25977  sparse_vector_u32 sv;
25978 
25979  sv.set(2, 25);
25980  sv.set(3, 35);
25981  sv.set(7, 75);
25982  sv.set(10, 2);
25983  sv.set(21, 201);
25984 
25986  unsigned to;
25987  bool found = set2set.remap(0, sv, to);
25988  assert(found);
25989  assert(to == 0);
25990  found = set2set.remap(8, sv, to);
25991  assert(found);
25992  assert(to == 0);
25993  found = set2set.remap(3, sv, to);
25994  assert(found);
25995  assert(to == 35);
25996 
25997 
25998  bvect bv_in { 0, 2, 3, 10};
25999  bvect bv_control {0, 25, 35, 2 };
26000 
26001  {
26002  bvect bv_out;
26003  bvector_transform_11(bv_in, sv, bv_out);
26004  int cmp = bv_control.compare(bv_out);
26005  if (cmp != 0)
26006  {
26007  cerr << "Transform11 (1-1) control comparison failed" << endl;
26008  exit(1);
26009  }
26010 
26011  sv.optimize();
26012  bv_out.clear();
26013 
26014  bvector_transform_11(bv_in, sv, bv_out);
26015  cmp = bv_control.compare(bv_out);
26016  if (cmp != 0)
26017  {
26018  cerr << "Transform11 (1-1, 1) control comparison failed" << endl;
26019  exit(1);
26020  }
26021  }
26022 
26023  cout << "Transform11 (1-1) - ok" << endl;
26024  }
26025 
26026  {
26027  bvect bv_in, bv_out;
26029 
26030  generate_bvector(bv_in);
26031 
26032  {
26033  bvect::enumerator en = bv_in.first();
26034  for (;en.valid(); ++en)
26035  {
26036  bm::id_t idx = *en;
26037  sv.set(idx, idx); // 1 to 1 direct
26038  }
26039  }
26040  bvector_transform_11(bv_in, sv, bv_out);
26041  int cmp = bv_in.compare(bv_out);
26042  if (cmp != 0)
26043  {
26044  cerr << "Transform11 (2) control comparison failed" << endl;
26045  exit(1);
26046  }
26047 
26048  sv.optimize();
26049 
26050  bvector_transform_11(bv_in, sv, bv_out);
26051  cmp = bv_in.compare(bv_out);
26052  if (cmp != 0)
26053  {
26054  cerr << "Transform11 (2, 2) control comparison failed" << endl;
26055  exit(1);
26056  }
26057 
26058  cout << "Transform11 (2) - ok" << endl;
26059  }
26060 
26061  {
26062  bvect bv_in, bv_out;
26064 
26065  generate_bvector(bv_in);
26066 
26067  bvect bv_control;
26068  {
26069  bvect::enumerator en = bv_in.first();
26070  for (;en.valid(); ++en)
26071  {
26072  bm::id_t idx = *en;
26073  bv_control.set(idx + 50000000);
26074  }
26075  }
26076 
26077  {
26078  bvect::enumerator en = bv_in.first();
26079  for (;en.valid(); ++en)
26080  {
26081  bm::id_t idx = *en;
26082  sv.set(idx, idx + 50000000); // 1 to 1 direct with a base shift
26083  }
26084  }
26085  bvector_transform_11(bv_in, sv, bv_out);
26086 
26087  int cmp = bv_control.compare(bv_out);
26088  if (cmp != 0)
26089  {
26090  cerr << "Transform11 (3) control comparison failed" << endl;
26091  exit(1);
26092  }
26093 
26094  sv.optimize();
26095 
26096  cmp = bv_control.compare(bv_out);
26097  if (cmp != 0)
26098  {
26099  cerr << "Transform11 (3, 2) control comparison failed" << endl;
26100  exit(1);
26101  }
26102  cout << "Transform11 (3) - ok" << endl;
26103  }
26104 
26105 
26106  {
26107  bvect bv_in, bv_out;
26109 
26110  generate_bvector(bv_in);
26111 
26112  bvect bv_control;
26113  bv_control.set(50000000);
26114 
26115  {
26116  bvect::enumerator en = bv_in.first();
26117  for (;en.valid(); ++en)
26118  {
26119  bm::id_t idx = *en;
26120  sv.set(idx, 50000000); // M:1
26121  }
26122  }
26123  bvector_transform_11(bv_in, sv, bv_out);
26124 
26125  int cmp = bv_control.compare(bv_out);
26126  if (cmp != 0)
26127  {
26128  cerr << "Transform11 (4) control comparison failed" << endl;
26129  exit(1);
26130  }
26131 
26132  sv.optimize();
26133  bvector_transform_11(bv_in, sv, bv_out);
26134 
26135  cmp = bv_control.compare(bv_out);
26136  if (cmp != 0)
26137  {
26138  cerr << "Transform11 (4, 2) control comparison failed" << endl;
26139  exit(1);
26140  }
26141  cout << "Transform11 (4) - ok" << endl;
26142  }
26143 
26144 
26145  {
26146  bvect bv_in, bv_out;
26148 
26149  generate_bvector(bv_in);
26150  bvect bv_control(bv_in);
26151  {
26152  bvect::enumerator en = bv_in.first();
26153  for (;en.valid(); ++en)
26154  {
26155  bm::id_t idx = *en;
26156  sv.set(idx, idx); // same:same
26157  }
26158  }
26159  bvector_transform_11(bv_in, sv, bv_out);
26160 
26161  int cmp = bv_control.compare(bv_out);
26162  if (cmp != 0)
26163  {
26164  cerr << "Transform11 (5) control comparison failed" << endl;
26165  exit(1);
26166  }
26167 
26168  sv.optimize();
26169  bvector_transform_11(bv_in, sv, bv_out);
26170 
26171  cmp = bv_control.compare(bv_out);
26172  if (cmp != 0)
26173  {
26174  cerr << "Transform11 (5, 2) control comparison failed" << endl;
26175  exit(1);
26176  }
26177  cout << "Transform11 (5) - ok" << endl;
26178  }
26179 
26180 
26181  cout << " --------------- Test set transformation with sparse vector OK" << endl;
26182 }
26183 
26184 template<class SV>
26185 void CheckGTSearch(const SV& sv, typename SV::value_type v,
26187 {
26188  bvect bv_res, bv_gt, bv_control;
26189  bvect bv_ge, bv_ge_control;
26190  bvect bv_lt, bv_lt_control;
26191  bvect bv_le, bv_le_control;
26192  bvect bv_r_0v, bv_r_0v_control;
26193 
26194  scanner.find_gt_horizontal(sv, v, bv_res);
26195  scanner.find_gt(sv, v, bv_gt);
26196  scanner.find_ge(sv, v, bv_ge);
26197  scanner.find_lt(sv, v, bv_lt);
26198  scanner.find_le(sv, v, bv_le);
26199  scanner.find_range(sv, 0, v, bv_r_0v);
26200 
26201  {
26202  bvect bv_r_vv, bv_r_vv_control;
26203  scanner.find_range(sv, v, v, bv_r_vv);
26204  scanner.find_eq(sv, v, bv_r_vv_control);
26205  bool eq = bv_r_vv.equal(bv_r_vv_control);
26206  if (!eq)
26207  {
26208  print_bv(bv_r_vv);
26209  print_bv(bv_r_vv_control);
26210  bv_r_vv ^= bv_r_vv_control;
26211  cout << "diff=" << endl;
26212  //print_bv(bv_r_vv);
26213  assert(eq);exit(1);
26214  }
26215  }
26216 
26217  auto it = sv.begin();
26218  auto it_end = sv.end();
26219  for (typename SV::size_type i(0); it != it_end; ++it, ++i)
26220  {
26221  if (!it.is_null())
26222  {
26223  auto v1 = *it;
26224  if (v1 > v)
26225  bv_control.set(i);
26226  if (v1 >= v)
26227  bv_ge_control.set(i);
26228  if (v1 < v)
26229  bv_lt_control.set(i);
26230  if (v1 <= v)
26231  bv_le_control.set(i);
26232  if (v < 0)
26233  {
26234  if (v1 <= 0 && v1 >= v)
26235  bv_r_0v_control.set(i);
26236  }
26237  else
26238  {
26239  if (v1 >= 0 && v1 <= v)
26240  bv_r_0v_control.set(i);
26241  }
26242  }
26243  } // for
26244  bool eq = bv_res.equal(bv_control);
26245  if (!eq)
26246  {
26247  cout << "1. result for v >=" << v << " :" << endl;
26248  print_bv(bv_res);
26249  bv_res ^= bv_control;
26250  cout << "diff=" << endl;
26251  print_bv(bv_res);
26252  assert(eq);exit(1);
26253  }
26254  eq = bv_res.equal(bv_gt);
26255  if (!eq)
26256  {
26257  cout << "2. result for v >=" << v << " :" << endl;
26258  print_bv(bv_gt);
26259  bv_gt ^= bv_control;
26260  cout << "diff=" << endl;
26261  print_bv(bv_gt);
26262  assert(eq);exit(1);
26263  }
26264  eq = bv_ge.equal(bv_ge_control);
26265  if (!eq)
26266  {
26267  cout << "3. result for v >=" << v << " :" << endl;
26268  print_bv(bv_ge);
26269  bv_ge ^= bv_ge_control;
26270  cout << "diff=" << endl;
26271  print_bv(bv_ge);
26272  assert(eq);exit(1);
26273  }
26274  eq = bv_lt.equal(bv_lt_control);
26275  if (!eq)
26276  {
26277  cout << "4. result for v <" << v << " :" << endl;
26278  print_bv(bv_lt);
26279  bv_lt ^= bv_lt_control;
26280  cout << "diff=" << endl;
26281  print_bv(bv_lt);
26282  assert(eq);exit(1);
26283  }
26284  eq = bv_le.equal(bv_le_control);
26285  if (!eq)
26286  {
26287  cout << "5. result for v <=" << v << " :" << endl;
26288  print_bv(bv_le);
26289  bv_le ^= bv_le_control;
26290  cout << "diff=" << endl;
26291  print_bv(bv_le);
26292  assert(eq);exit(1);
26293  }
26294  eq = bv_r_0v.equal(bv_r_0v_control);
26295  if (!eq)
26296  {
26297  cout << "6. result for [0, v] " << v << " :" << endl;
26298  print_bv(bv_r_0v);
26299  bv_r_0v ^= bv_r_0v_control;
26300  cout << "diff=" << endl;
26301  print_bv(bv_r_0v);
26302  assert(eq);exit(1);
26303  }
26304 }
26305 
26306 
26307 static
26309 {
26310  cout << " --------------- Test sparse_vector<> scan algo TestSparseVectorScan()" << endl;
26311 
26316 
26317  {
26319  bvect bv_control;
26320  scanner.find_eq(sv, 25, bv_control);
26321  assert(!bv_control.any());
26322  scanner.invert(sv, bv_control);
26323  assert(!bv_control.any());
26324  }
26325 
26326  {
26328  bvect bv_control;
26329  for (unsigned i = 0; i < 20; ++i)
26330  {
26331  sv.set(i, 0);
26332  }
26333  sv.set(bm::id_max/2, 25);
26334  sv.set(bm::id_max-1, 25);
26335 
26336  scanner.find_eq(sv, 0, bv_control);
26337  unsigned found = bv_control.count();
26338  assert(found == 20);
26339  scanner.invert(sv, bv_control);
26340  found = bv_control.count();
26341  assert(found==2);
26342 
26343  scanner.find_eq(sv, 25, bv_control);
26344  found = bv_control.count();
26345  assert(found == 2);
26346 
26347  {
26348  std::vector<unsigned> v_control;
26349  {
26350  auto bi = std::back_inserter(v_control);
26351  scanner.find_eq(sv, 25, bi);
26352  }
26353  auto sz = v_control.size();
26354  assert(sz == 2);
26355  assert(v_control[0] == bm::id_max/2);
26356  assert(v_control[1] == bm::id_max-1);
26357  }
26358 
26359  }
26360 
26361  {
26363  bvect bv_control;
26364  for (unsigned i = 0; i < 20; ++i)
26365  {
26366  sv.set(i, 0);
26367  }
26368  sv.set(bm::id_max/2, -25);
26369  sv.set(bm::id_max-1, -25);
26370 
26371  iscanner.find_eq(sv, 0, bv_control);
26372  unsigned found = bv_control.count();
26373  assert(found == 20);
26374  iscanner.invert(sv, bv_control);
26375  found = bv_control.count();
26376  assert(found==2);
26377 
26378  iscanner.find_eq(sv, -25, bv_control);
26379  found = bv_control.count();
26380  assert(found == 2);
26381 
26382  {
26383  std::vector<unsigned> v_control;
26384  {
26385  auto bi = std::back_inserter(v_control);
26386  iscanner.find_eq(sv, -25, bi);
26387  }
26388  auto sz = v_control.size();
26389  assert(sz == 2);
26390  assert(v_control[0] == bm::id_max/2);
26391  assert(v_control[1] == bm::id_max-1);
26392  }
26393 
26394  }
26395 
26396 
26397  {
26399  bvect bv_control;
26400  for (unsigned i = 65536; i < 65536*2; ++i)
26401  sv.set(i, 3);
26402  sv.optimize();
26403 
26404  std::vector<unsigned> v_control;
26405  {
26406  auto bi = std::back_inserter(v_control);
26407  scanner.find_eq(sv, 3, bi);
26408  }
26409  auto sz = v_control.size();
26410  assert(sz == 65536);
26411  unsigned idx(0);
26412  for (unsigned i = 65536; i < 65536*2; ++i, ++idx)
26413  {
26414  auto v = v_control[idx];
26415  assert(v == i);
26416  }
26417  }
26418 
26419 
26420  {
26421  sparse_vector_u32 sv;
26422  bvect bv_control;
26423  for (unsigned i = 0; i < 20; ++i)
26424  {
26425  sv.set(i, 0);
26426  }
26427  scanner.find_eq(sv, 0, bv_control);
26428  unsigned found = bv_control.count();
26429  assert(found == 20);
26430  scanner.invert(sv, bv_control);
26431  found = bv_control.count();
26432  assert(!found);
26433  }
26434 
26435 
26436  {
26437  cout << endl << "Unique search check" << endl;
26440 
26441  bvect bv_control, bv_control2, bv_control3;
26443  bvect::mem_pool_guard g1(pool, bv_control);
26444  bvect::mem_pool_guard g2(pool, bv_control2);
26445  bvect::mem_pool_guard g3(pool, bv_control3);
26446 
26447  unsigned sv_size = 1256000;
26448  {
26450  for (unsigned j = 0; j < sv_size; ++j)
26451  {
26452  *bi = j;
26453  }
26454  }
26455  csv.load_from(sv);
26456 
26457  {
26458  chrono_taker<std::ostream> ct(cerr, "sparse_vector<> search");
26459 
26460  for (unsigned j = 0; j < sv_size; ++j)
26461  {
26462  scanner.find_eq(sv, j, bv_control);
26463  if (bv_control.count()!= 1)
26464  {
26465  cerr << "1. Unique search discrepancy at value=" << j
26466  << " count = " << bv_control.count() << endl;
26467  exit(1);
26468  }
26469 
26470  bv_control3.clear();
26471  scanner.find_eq(sv, j, bvect::insert_iterator(bv_control3));
26472  if (bv_control3.count()!= 1)
26473  {
26474  cerr << "1.1 Unique search discrepancy at value=" << j
26475  << " count = " << bv_control3.count() << endl;
26476  exit(1);
26477  }
26478 
26479  {
26480  bool eq = bv_control.equal(bv_control3);
26481  assert(eq);
26482  }
26483 
26484 
26485  unsigned v1, v2;
26486  bool b = bv_control.find_range(v1, v2);
26487  assert(b);
26488  if (v1 != v2)
26489  {
26490  cerr << "2. Unique search discrepancy at value=" << j
26491  << " count = " << bv_control.count() << endl;
26492  exit(1);
26493  }
26494 
26495  bm::id_t pos;
26496  bool found = scanner.find_eq(sv, j, pos);
26497  if (!found)
26498  {
26499  cerr << "3. Unique search failure at value=" << j
26500  << endl;
26501  exit(1);
26502  }
26503  if (v1 != pos)
26504  {
26505  cerr << "4. Unique search discrepancy at value=" << j
26506  << " found = " << pos << endl;
26507  exit(1);
26508  }
26509 
26510  rsc_scanner.find_eq(csv, j, bv_control2);
26511  int res = bv_control.compare(bv_control2);
26512  if (res != 0)
26513  {
26514  cerr << "RSC scan comparison failed at value =" << j
26515  << endl;
26516  exit(1);
26517  }
26518 
26519 
26520  if (!is_silent)
26521  if (j % 1000 == 0)
26522  cout << "\r" << j << "/" << sv_size << " " << flush;
26523  } // for
26524  cout << endl;
26525  }
26526 
26527  cout << "Unique search OK" << endl;
26528  }
26529 
26530  {
26531  cout << "Find EQ test on flat data" << endl;
26533  unsigned max_value = 128000;
26534  for (unsigned value = 0; value < max_value; ++value)
26535  {
26539 
26540  bvect bv_control, bv_control2;
26541  bvect::mem_pool_guard g0(pool, bv_control);
26542 
26543  unsigned sv_size = 67000;
26544 
26545  {
26548  for (unsigned j = 0; j < 67000; ++j)
26549  {
26550  *bi = value;
26551  bm::id64_t v64 = value;
26552  v64 <<= 32;
26553  *bi_64 = v64;
26554  }
26555  }
26556  csv.load_from(sv);
26557 
26558  scanner.find_eq(sv, value, bv_control);
26559  unsigned found = bv_control.count();
26560 
26561  if (found != sv_size)
26562  {
26563  cerr << "1. sparse_vector<>::find_eq() discrepancy for value=" << value
26564  << " count = " << found << endl;
26565  exit(1);
26566  }
26567 
26568  rsc_scanner.find_eq(csv, value, bv_control2);
26569  int res = bv_control.compare(bv_control2);
26570  if (res != 0)
26571  {
26572  cerr << "RSC scan comparison failed at value =" << value
26573  << endl;
26574  exit(1);
26575  }
26576 
26577 
26578  {
26579  bm::id64_t v64 = value;
26580  v64 <<= 32;
26581 
26582  scanner_64.find_eq(sv_64, v64, bv_control);
26583  found = bv_control.count();
26584 
26585  if (found != sv_size)
26586  {
26587  cerr << "1. (64) sparse_vector<>::find_eq() discrepancy for value=" << value
26588  << " count = " << found << endl;
26589  exit(1);
26590  }
26591  }
26592 
26593  // not found check
26594  scanner.find_eq(sv, value+1, bv_control);
26595  if (bv_control.any())
26596  {
26597  cerr << "1. sparse_vector<>::find_eq() (any) discrepancy for value=" << value+1
26598  << " count = " << bv_control.count() << endl;
26599  exit(1);
26600  }
26601  rsc_scanner.find_eq(csv, value+1, bv_control2);
26602  res = bv_control.compare(bv_control2);
26603  if (res != 0)
26604  {
26605  cerr << "1. RSC scan comparison failed at value =" << value+1
26606  << endl;
26607  exit(1);
26608  }
26609 
26610  {
26612  sv.optimize(tb);
26613  }
26614 
26615  bv_control.clear();
26616 
26617  scanner.find_eq(sv, value, bv_control);
26618  found = bv_control.count();
26619 
26620  if (found != sv_size)
26621  {
26622  cerr << "2. sparse_vector<>::find_eq() discrepancy for value=" << value
26623  << " count = " << found << endl;
26624  exit(1);
26625  }
26626 
26627  // not found check
26628  scanner.find_eq(sv, value+1, bv_control);
26629  if (bv_control.any())
26630  {
26631  cerr << "2. sparse_vector<>::find_eq() (any) discrepancy for value=" << value+1
26632  << " count = " << bv_control.count() << endl;
26633  exit(1);
26634  }
26635 
26636 
26637  if (!is_silent)
26638  if (value % 256 == 0)
26639  cout << "\r" << value << "/" << max_value << " " << flush;
26640  }
26641 
26642  cout << endl << "Flat EQ ok" << endl;
26643  }
26644 
26645 
26646 
26647  cout << " \n--------------- Test sparse_vector<> scan algo OK" << endl;
26648 }
26649 
26650 
26651 static
26653 {
26654  cout << " --------------- Test sparse_vector<> scan algo TestSparseVectorScanGT()" << endl;
26655 
26657  {
26659  sv.push_back(1);
26660  sv.push_back(8);
26661  sv.push_back(8+7);
26662  sv.push_back(8+1);
26663  sv.push_back(16);
26664 
26665  bvect bv_res;
26666  bool b;
26667  scanner.find_gt_horizontal(sv, 0, bv_res);
26668  b = bv_res.empty();
26669  assert(!b);
26670  CheckGTSearch(sv, 0, scanner);
26671 
26672  sv.push_back(0);
26673 
26674  for (int pass = 0; pass < 2; ++pass)
26675  {
26676  scanner.find_gt_horizontal(sv, 0, bv_res);
26677  auto cnt = bv_res.count();
26678  assert(cnt == 5);
26679  assert(!bv_res.test(5));
26680 
26681  scanner.find_gt_horizontal(sv, 256, bv_res);
26682  cnt = bv_res.count();
26683  assert(cnt == 0);
26684 
26685  scanner.find_gt_horizontal(sv, 255, bv_res);
26686  cnt = bv_res.count();
26687  assert(cnt == 0);
26688 
26689 
26690  auto it = sv.begin();
26691  auto it_end = sv.end();
26692  for (; it != it_end; ++it)
26693  {
26694  if (!it.is_null())
26695  {
26696  auto v1 = *it;
26697  CheckGTSearch(sv, v1, scanner);
26698  }
26699  } // for
26700  sv.optimize();
26701  } // pass
26702 
26703  }
26704 
26705 
26706  {
26708  sv.push_back_null(); // 0
26709  sv.push_back(1);
26710  sv.push_back(8); // 2
26711  sv.push_back(8+7);
26712  sv.push_back(8+1); // 4
26713  sv.push_back_null(); // 5
26714  sv.push_back(16);
26715  sv.push_back(0); // 7
26716  sv.push_back_null(); // 8
26717 
26718  for (int pass = 0; pass < 2; ++pass)
26719  {
26720  auto it = sv.begin();
26721  auto it_end = sv.end();
26722  for (; it != it_end; ++it)
26723  {
26724  if (!it.is_null())
26725  {
26726  auto v1 = *it;
26727  CheckGTSearch(sv, v1, scanner);
26728  }
26729  } // for
26730  sv.optimize();
26731  } // pass
26732 
26733  }
26734 
26735 
26736  {
26737  cout << "GT-Unique search check" << endl;
26738 
26740  unsigned sv_size = 1256000;
26741  {
26743  for (unsigned j = 0; j < sv_size; ++j)
26744  *bi = j;
26745  bi.flush();
26746  }
26747 
26748  // not found tests
26749  for (bvect::size_type j = sv_size; j < sv_size+1024*10; ++j)
26750  {
26751  bvect bv_res;
26752  scanner.find_gt_horizontal(sv, j, bv_res);
26753  bool b = bv_res.empty();
26754  assert(b);
26755  } // for
26756 
26757  for (bvect::size_type j = 0; j < sv_size; ++j)
26758  {
26759  {
26760  bvect bv_res, bv_control;
26761  scanner.find_gt_horizontal(sv, j, bv_res);
26762  if (j == sv_size-1)
26763  {
26764  bool b = bv_res.empty();
26765  assert(b);
26766  continue;
26767  }
26768  bv_control.set_range(j+1, sv_size-1);
26769  bool b = bv_res.equal(bv_control);
26770  assert(b);
26771  }
26772  if ((j % 16) == 0)
26773  CheckGTSearch(sv, j, scanner);
26774 
26775  if (j % 1024 == 0)
26776  if (!is_silent)
26777  cout << "\r" << j << "/" << sv_size << " " << flush;
26778  } // for
26779 
26780 
26781  }
26782  cout << endl;
26783 
26784 
26785  cout << " --------------- Test sparse_vector<> scan algo TestSparseVectorScanGT() OK" << endl;
26786 }
26787 
26788 static
26790 {
26791  cout << " --------------- Test sparse_vector<> scan algo TestSignedSparseVectorScanGT()" << endl;
26792 
26795 
26796  cout << "...positives only" << endl;
26797  {
26798  sparse_vector_i32 sv;
26799  sv.push_back(1); // 0
26800  sv.push_back(8);
26801  sv.push_back(8+7); // 2
26802  sv.push_back(8+1);
26803  sv.push_back(16); // 4
26804  sv.push_back(INT_MAX);
26805  sv.push_back(0); // 6
26806 
26807  for (int pass = 0; pass < 2; ++pass)
26808  {
26809  auto it = sv.begin();
26810  auto it_end = sv.end();
26811  for (; it != it_end; ++it)
26812  {
26813  if (!it.is_null())
26814  {
26815  auto v1 = *it;
26816  CheckGTSearch(sv, v1, scanner);
26817 
26818  CheckGTSearch(sv, -v1, scanner);
26819  }
26820  } // for
26821  sv.optimize();
26822  } // pass
26823  }
26824 
26825  cout << "...positives AND negatives mix" << endl;
26826  {
26827  sparse_vector_i32 sv;
26828  sv.push_back(1); // 0
26829  sv.push_back(8);
26830  sv.push_back(8+7); // 2
26831  sv.push_back(8+1);
26832  sv.push_back(16); // 4
26833  sv.push_back(0);
26834  sv.push_back(-1); // 6
26835  sv.push_back(-2);
26836  sv.push_back(-INT_MAX); // 8
26837  sv.push_back(-7);
26838  sv.push_back(-17); // 10
26839  sv.push_back(0);
26840  sv.push_back(INT_MAX); // 12
26841 
26842  for (int pass = 0; pass < 2; ++pass)
26843  {
26844  auto it = sv.begin();
26845  auto it_end = sv.end();
26846  for (; it != it_end; ++it)
26847  {
26848  if (!it.is_null())
26849  {
26850  auto v1 = *it;
26851  CheckGTSearch(sv, v1, scanner);
26852  }
26853  } // for
26854  sv.optimize();
26855  } // pass
26856  }
26857 
26858  cout << "...positives AND negatives mix (with NULLs)" << endl;
26859  {
26861  sv.push_back(1); // 0
26862  sv.push_back_null();
26863  sv.push_back(8);
26864  sv.push_back(8+7);
26865  sv.push_back(8+1);
26866  sv.push_back(16);
26867  sv.push_back(0);
26868  sv.push_back(-1);
26869  sv.push_back_null();
26870  sv.push_back(-2);
26871  sv.push_back(-INT_MAX);
26872  sv.push_back(-7);
26873  sv.push_back_null();
26874  sv.push_back(-17);
26875  sv.push_back(0);
26876  sv.push_back(INT_MAX);
26877  sv.push_back_null();
26878 
26879 // CheckGTSearch(sv, 0, scanner);
26880 // CheckGTSearch(sv, -INT_MAX, scanner);
26881 
26882  for (int pass = 0; pass < 2; ++pass)
26883  {
26884  auto it = sv.begin();
26885  auto it_end = sv.end();
26886  for (; it != it_end; ++it)
26887  {
26888  if (!it.is_null())
26889  {
26890  auto v1 = *it;
26891  CheckGTSearch(sv, v1, scanner);
26892  }
26893  } // for
26894  sv.optimize();
26895  } // pass
26896  }
26897 
26898 
26899  {
26900  cout << "GT-Unique search check" << endl;
26901 
26903  int sv_size = 1256000;
26904  {
26906  for (int j = -sv_size; j < sv_size; ++j)
26907  *bi = j;
26908  bi.flush();
26909  }
26910 
26911  // not found tests
26912  for (int j = sv_size; j < sv_size+1024*10; ++j)
26913  {
26914  bvect bv_res;
26915  scanner64.find_gt_horizontal(sv, j, bv_res);
26916  bool b = bv_res.empty();
26917  assert(b);
26918  } // for
26919  {
26920  bvect bv_res;
26921  scanner64.find_gt_horizontal(sv, -sv_size-1, bv_res);
26922  auto cnt = bv_res.count();
26923  assert(cnt == sv.size()); // all found
26924  }
26925 
26926  unsigned i = 0;
26927  for (int j = -sv_size; j < sv_size; ++j, ++i)
26928  {
26929  {
26930  bvect bv_res, bv_control;
26931  scanner64.find_gt_horizontal(sv, j, bv_res);
26932  if (j == sv_size-1)
26933  {
26934  bool b = bv_res.empty();
26935  assert(b);
26936  continue;
26937  }
26938  bv_control.set_range(i+1, sv.size()-1);
26939  bool b = bv_res.equal(bv_control);
26940  assert(b);
26941  }
26942  if ((j % 16) == 0)
26943  CheckGTSearch(sv, j, scanner64);
26944 
26945  if (j % 1024 == 0)
26946  if (!is_silent)
26947  cout << "\r" << i << "/" << sv.size() << " " << flush;
26948  } // for
26949 
26950 
26951  }
26952 
26953 
26954  cout << endl;
26955 
26956  cout << " --------------- Test sparse_vector<> scan algo TestSignedSparseVectorScanGT() OK" << endl;
26957 }
26958 
26959 
26960 static
26962 {
26963  cout << " --------------- TestSignedSparseVectorScan()" << endl;
26964 
26968 
26969  {
26971  bvect bv_control;
26972  scanner.find_eq(sv, 25, bv_control);
26973  assert(!bv_control.any());
26974  scanner.invert(sv, bv_control);
26975  assert(!bv_control.any());
26976  }
26977 
26978  {
26980  bvect bv_control;
26981  for (unsigned i = 0; i < 20; ++i)
26982  {
26983  sv.set(i, 0);
26984  }
26985  scanner.find_eq(sv, 0, bv_control);
26986  unsigned found = bv_control.count();
26987  assert(found == 20);
26988  scanner.invert(sv, bv_control);
26989  found = bv_control.count();
26990  assert(!found);
26991  }
26992 
26993  {
26994  cout << endl << "Unique search check" << endl;
26997 
26998  bvect bv_control, bv_control2;
27000  bvect::mem_pool_guard g1(pool, bv_control);
27001  bvect::mem_pool_guard g2(pool, bv_control2);
27002 
27003  unsigned sv_size = 1256000;
27004  {
27006  for (unsigned j = 0; j < sv_size; ++j)
27007  {
27008  if (j & 1)
27009  *bi = int(j);
27010  else
27011  *bi = -int(j);
27012  }
27013  }
27014  csv.load_from(sv);
27015 
27016  {
27017  chrono_taker<std::ostream> ct(cout, "sparse_vector<> search");
27018 
27019  for (unsigned j = 0; j < sv_size; ++j)
27020  {
27021  int search_value;
27022  if (j & 1)
27023  search_value = int(j);
27024  else
27025  search_value = -int(j);
27026  scanner.find_eq(sv, search_value, bv_control);
27027 
27028  if (bv_control.count()!= 1)
27029  {
27030  cerr << "1. Unique search discrepancy at value=" << j
27031  << " count = " << bv_control.count() << endl;
27032  assert(0);exit(1);
27033  }
27034  unsigned v1, v2;
27035  bool b = bv_control.find_range(v1, v2);
27036  assert(b);
27037  if (v1 != v2)
27038  {
27039  cerr << "2. Unique search discrepancy at value=" << j
27040  << " count = " << bv_control.count() << endl;
27041  exit(1);
27042  }
27043 
27044  bvect::size_type pos;
27045  bool found = scanner.find_eq(sv, search_value, pos);
27046  if (!found)
27047  {
27048  cerr << "3. Unique search failure at value=" << j
27049  << endl;
27050  exit(1);
27051  }
27052  if (v1 != pos)
27053  {
27054  cerr << "4. Unique search discrepancy at value=" << j
27055  << " found = " << pos << endl;
27056  exit(1);
27057  }
27058 
27059  rsc_scanner.find_eq(csv, search_value, bv_control2);
27060  int res = bv_control.compare(bv_control2);
27061  if (res != 0)
27062  {
27063  cerr << "RSC scan comparison failed at value =" << j
27064  << endl;
27065  exit(1);
27066  }
27067 
27068 
27069  if (j % 1000 == 0)
27070  if (!is_silent)
27071  cout << "\r" << j << "/" << sv_size << " " << flush;
27072  } // for
27073  cout << endl;
27074  }
27075 
27076  cout << "Unique search OK" << endl;
27077  }
27078 
27079  {
27080  cout << "Find EQ test on flat data" << endl;
27082  int max_value = 128000;
27083  for (int value = 0; value < max_value; ++value)
27084  {
27088 
27089  bvect bv_control, bv_control2;
27090  bvect::mem_pool_guard g0(pool, bv_control);
27091 
27092  unsigned sv_size = 67000;
27093 
27094  {
27097  for (unsigned j = 0; j < 67000; ++j)
27098  {
27099  *bi = -value;
27100  bm::id64_t v64 = (unsigned)value;
27101  v64 <<= 32;
27102  *bi_64 = -(signed long long)v64;
27103  }
27104  }
27105  csv.load_from(sv);
27106 
27107  scanner.find_eq(sv, -value, bv_control);
27108  unsigned found = bv_control.count();
27109 
27110  if (found != sv_size)
27111  {
27112  cerr << "1. sparse_vector<>::find_eq() discrepancy for value=" << value
27113  << " count = " << found << endl;
27114  exit(1);
27115  }
27116 
27117  rsc_scanner.find_eq(csv, -value, bv_control2);
27118  int res = bv_control.compare(bv_control2);
27119  if (res != 0)
27120  {
27121  cerr << "RSC scan comparison failed at value =" << value
27122  << endl;
27123  exit(1);
27124  }
27125 
27126 
27127  {
27128  bm::id64_t v64 = unsigned(value);
27129  v64 <<= 32;
27130  signed long long v64s = -(signed long long)v64;
27131  scanner_64.find_eq(sv_64, v64s, bv_control);
27132  found = bv_control.count();
27133 
27134  if (found != sv_size)
27135  {
27136  cerr << "1. (64) sparse_vector<>::find_eq() discrepancy for value=" << value
27137  << " count = " << found << endl;
27138  exit(1);
27139  }
27140  }
27141 
27142  // not found check
27143  scanner.find_eq(sv, value+1, bv_control);
27144  if (bv_control.any())
27145  {
27146  cerr << "1. sparse_vector<>::find_eq() (any) discrepancy for value=" << value+1
27147  << " count = " << bv_control.count() << endl;
27148  exit(1);
27149  }
27150  rsc_scanner.find_eq(csv, value+1, bv_control2);
27151  res = bv_control.compare(bv_control2);
27152  if (res != 0)
27153  {
27154  cerr << "1. RSC scan comparison failed at value =" << value+1
27155  << endl;
27156  exit(1);
27157  }
27158 
27159  {
27161  sv.optimize(tb);
27162  }
27163 
27164  bv_control.clear();
27165 
27166  scanner.find_eq(sv, -value, bv_control);
27167  found = bv_control.count();
27168  if (found != sv_size)
27169  {
27170  cerr << "2. sparse_vector<>::find_eq() discrepancy for value=" << value
27171  << " count = " << found << endl;
27172  exit(1);
27173  }
27174 
27175  // not found check
27176  scanner.find_eq(sv, value+1, bv_control);
27177  if (bv_control.any())
27178  {
27179  cerr << "2. sparse_vector<>::find_eq() (any) discrepancy for value=" << value+1
27180  << " count = " << bv_control.count() << endl;
27181  exit(1);
27182  }
27183 
27184 
27185  if (value % 256 == 0)
27186  if (!is_silent)
27187  cout << "\r" << value << "/" << max_value << " " << flush;
27188  }
27189 
27190  cout << endl << "Flat EQ ok" << endl;
27191  }
27192 
27193 
27194 
27195  cout << " \n--------------- TestSignedSparseVectorScan() OK" << endl;
27196 }
27197 
27198 
27199 static
27201 {
27202  cout << "\n--------------- TestCompressSparseGather()" << endl;
27203 
27204  // DEBUGGING code, reading from a saved memory dump
27205 #if 0
27206  {
27208  std::vector<rsc_sparse_vector_i32::size_type> idx;
27209 
27210  int res = bm::load_vector(idx, "/Users/anatoliykuznetsov/dev/git/BitMagic/tests/stress/gath_idx.vect");
27211  assert(res == 0);
27212  res = bm::file_load_svector(csv1, "/Users/anatoliykuznetsov/dev/git/BitMagic/tests/stress/gath_data.csv");
27213  assert(res==0);
27214 
27215 // unsigned i=33595; unsigned get_idx = 139324;
27216  unsigned i=469; unsigned get_idx = 24969598;
27217 
27218 
27219  {
27220 // assert(idx[i] == get_idx);
27222  bool b = csv1.resolve(get_idx, &rank);
27223  bool b1 = !csv1.is_null(get_idx);
27224  assert(b == b1);
27225  }
27226 
27227 
27228  assert(idx[i]==get_idx);
27229  auto vc = csv1.get(get_idx);
27230 
27231 
27232  std::vector<rsc_sparse_vector_i32::size_type> idx_buf;
27233  std::vector<rsc_sparse_vector_i32::value_type> vbuf;
27234 
27235  assert(idx.size());
27236  idx_buf.resize(idx.size());
27237  vbuf.resize(idx.size());
27238 
27239 // for (unsigned k = 0; k < i; ++k)
27240  unsigned k = 33594;
27241  {
27242  int* arr = vbuf.data() + k;
27243  const rsc_sparse_vector_i32::size_type* iarr = idx.data()+k;
27244  rsc_sparse_vector_i32::size_type* iarr_tmp = idx_buf.data() + k;
27245  size_t gath_sz = idx.size()-k;
27246 /*
27247  auto sz = csv1.gather(arr, iarr, iarr_tmp,
27248  (rsc_sparse_vector_i32::size_type)gath_sz,
27249  bm::BM_UNSORTED);
27250 */
27251  //for (size_t gs = 2; gs <= gath_sz; ++gs)
27252  {
27253  auto sz = csv1.gather(arr, iarr, iarr_tmp,
27254  102, //gath_sz,
27255  bm::BM_UNSORTED);
27256  auto v0 = vbuf.at(k);
27257  auto get_idx0 = idx[k];
27258  auto v0c = csv1.at(get_idx0);
27259  auto t = idx_buf[k];
27260  if (t != bm::id_max)
27261  {
27262  assert(v0 == v0c);
27263  }
27264  auto v = vbuf.at(i);
27265  cout << sz << " " << flush;
27266  assert(v == vc);
27267  //cout << k << " " << flush;
27268  // if (v == vc)
27269  // break;
27270  }
27271  }
27272  auto v = vbuf.at(i);
27273  cout << vc << "=" << v << endl;
27274  assert(v == vc);
27275  return;
27276  }
27277 #endif
27278 
27279  unsigned max_passes = 1024;
27280  unsigned sampling_delta = 3;
27281 
27282  for (unsigned pass = 0; pass < max_passes; ++pass)
27283  {
27285  std::vector<int> vect_c;
27286 
27287  unsigned test_size = 65536 * 256 * 2;
27288  unsigned test_start = pass * 65536;
27289  if (test_start > bm::id_max/4)
27290  test_start = (unsigned)((pass % 256) * (unsigned(rand()) % 65536));
27291 
27292  {
27293  auto iit = csv1.get_back_inserter();
27294  iit.add_null(test_start);
27295 
27296  for (unsigned i = 0; i < test_size; i+=3)
27297  {
27298  if (bm::id64_t(csv1.size()) + pass + 1ull > bm::id64_t(bm::id_max))
27299  break;
27300  int v = rand() % 65539;
27301  if ((v > 0) && ((v % 5) == 0)) v = -v;
27302  iit = v;
27303  iit.add_null(pass);
27304 
27305  vect_c.push_back(v);
27306  } // for i
27307  iit.flush();
27308  }
27309 
27310  csv1.optimize();
27311  csv1.sync();
27312 
27313  if (pass < 10)
27314  {
27316  for (rsc_sparse_vector_i32::size_type i = 0; i < csv1.size(); ++i)
27317  {
27318  int v;
27319  bool b = csv1.try_get(i, v);
27320  if (b)
27321  {
27322  auto vc = vect_c[rank++];
27323  assert(v == vc);
27324  }
27325  }
27326  }
27327 
27328  std::vector<rsc_sparse_vector_i32::size_type> idx;
27329  std::vector<rsc_sparse_vector_i32::size_type> idx_buf;
27330  std::vector<rsc_sparse_vector_i32::value_type> vbuf;
27331 
27332  rsc_sparse_vector_i32::size_type gather_size = 65536 * 3;
27333 
27334  for (rsc_sparse_vector_i32::size_type i = 0; i < gather_size; i += sampling_delta)
27335  {
27336  rsc_sparse_vector_i32::size_type get_idx = test_start - 1025 + i;
27337  if (get_idx > bm::id_max)
27338  get_idx = bm::id_max - 1;
27339  idx.push_back(get_idx);
27340  }
27341 
27342  assert(idx.size());
27343  idx_buf.resize(idx.size());
27344  vbuf.resize(idx.size());
27345 
27346  auto sort_opt = bm::BM_SORTED;
27347  for (unsigned gpass = 0; gpass < 5; ++gpass)
27348  {
27349  auto sz = csv1.gather(vbuf.data(), idx.data(), idx_buf.data(),
27351  sort_opt);
27352  assert(sz == idx.size());
27353  for (rsc_sparse_vector_i32::size_type i = 0; i < sz; ++i)
27354  {
27355  auto get_idx = idx[i];
27356  auto v = vbuf[i];
27357  auto t = idx_buf[i];
27358  if (t == bm::id_max)
27359  {
27360  if (!csv1.is_null(get_idx))
27361  {
27362  cout << " i=" << i << " get_idx = " << get_idx
27363  << " sz = " << sz
27364  << endl;
27366  bool b = csv1.resolve(get_idx, &rank);
27367  cout << " is_not_NULL=" << b << " rank="
27368  << rank-1 << endl;
27369 
27370  int res = bm::save_vector(idx, "gath_idx.vect");
27371  assert(res==0);
27372  res = bm::file_save_svector(csv1, "gath_data.csv");
27373  assert(res==0);
27374  cout << "DBG/Dump creates: " << "gath_data.csv" << "gath_idx.vect" << endl;
27375 
27376  bool bc = csv1.is_null(get_idx);
27377  assert(!b);
27378  assert(bc);
27379  assert(0);
27380  }
27381  }
27382  else
27383  {
27384  auto vc = csv1.at(get_idx);
27385  auto vc0 = csv1.get(get_idx);
27386  assert(vc == vc0);
27387 
27388  if (v != vc)
27389  {
27391  bool b = csv1.resolve(get_idx, &rank);
27392  assert(b);
27393  auto vvc = vect_c.at(rank-1);
27394  assert(vc == vvc);
27395 
27396  cout << " i=" << i << " get_idx = " << get_idx
27397  << " sz = " << sz << " vc = " << vc << " v=" << v
27398  << endl;
27399 
27400  int res = bm::save_vector(idx, "gath_idx.vect");
27401  assert(res==0);
27402  res = bm::file_save_svector(csv1, "gath_data.csv");
27403  assert(res==0);
27404  cout << "DBG/Dump creates: " << "gath_data.csv" << "gath_idx.vect" << endl;
27405  }
27406  assert(v == vc);
27407  }
27408  } // for i
27409 
27410  // shuffle the indexes
27411  {
27412  std::random_device rd;
27413  std::mt19937 g(rd());
27414  std::shuffle(idx.begin(), idx.end(), g);
27415  }
27416  sort_opt = bm::BM_UNSORTED;
27417  } // for gpass
27418 
27419 
27420  if (!is_silent)
27421  cout << "\r" << pass << "/" << max_passes << flush;
27422  } // pass
27423  cout << endl;
27424 
27425  cout << "--------------- TestCompressSparseGather() OK\n" << endl;
27426 }
27427 
27428 
27429 static
27431 {
27432  cout << " --------------- TestCompressedSparseVectorAlgo()" << endl;
27433 
27434  {
27437 
27439  bool f;
27440  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27441  assert(!f);
27442 
27443  csv1.push_back(10, 10);
27444  csv1.push_back(11, 10);
27445  csv1.push_back(200, 0);
27446  csv1.push_back(300, 0);
27447 
27448 
27449  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27450  assert(f);
27451  assert(pos == 10);
27452 
27453  csv1.sync();
27454  csv2.sync();
27455 
27456  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27457  assert(f);
27458  assert(pos == 10);
27459  }
27460 
27461  {
27464 
27465  csv1.push_back(200, 0);
27466  csv1.push_back(300, 0);
27467 
27468  csv2.push_back(200, 0);
27469  csv2.push_back(300, 0);
27470 
27472  bool f;
27473  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27474  assert(!f);
27475 
27476  csv2.push_back(400, 0);
27477  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27478  assert(f);
27479  assert(pos == 400);
27480  }
27481 
27482  {
27485 
27487  bool f;
27488 
27489  csv1.push_back(10, 10);
27490  csv1.push_back(11, 10);
27491  csv1.push_back(200, 0);
27492  csv1.push_back(300, 0);
27493 
27494  csv2 = csv1;
27495  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27496  assert(!f);
27497  csv2.push_back(400, 256);
27498 
27499  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27500  assert(f);
27501  assert(pos == 400);
27502 
27503  csv1.optimize();
27504  csv2.optimize();
27505 
27506  csv1.sync();
27507  csv2.sync();
27508 
27509  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27510  assert(f);
27511  assert(pos == 400);
27512 
27513  }
27514 
27515  // ----------------------------------------------------------
27516 
27517  {
27518  rsc_sparse_vector_u32 csv1;
27519  rsc_sparse_vector_u32 csv2;
27521  bool f;
27522 
27523  for (unsigned i = 0; i < 1022; ++i)
27524  {
27525  csv1.set(i, 65536);
27526  csv2.set(i, 65536);
27527  }
27528  csv1.set(1023, 4);
27529  csv2.set(1023, 8);
27530 
27531  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27532  assert(f);
27533  assert(pos == 1023);
27534 
27535  csv1.sync(); csv2.sync();
27536  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27537  assert(f);
27538  assert(pos == 1023);
27539  }
27540 
27541  {
27542  cout << endl << "Unique mismatch check" << endl;
27546 
27547 
27548  unsigned sv_size = 525600;
27549  {
27550  sparse_vector_u32::back_insert_iterator bi(sv1.get_back_inserter());
27551  unsigned v = 0; unsigned cnt = 0;
27552  for (unsigned j = 0; j < sv_size; ++j)
27553  {
27554  *bi = v;
27555  if (++cnt > 256)
27556  {
27557  cnt = 0; ++v;
27558  }
27559  }
27560  }
27561  csv1.load_from(sv1);
27562 
27563  sv2 = sv1;
27564  csv2 = csv1;
27565 
27567  bool f;
27568 
27569  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27570  assert(!f);
27571  f = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
27572  assert(!f);
27573 
27574  for (unsigned k = 0; k < 4; ++k)
27575  {
27576  cout << "PASS = " << k << endl;
27577  chrono_taker<std::ostream> ct(cout, "sparse_vector<> unique value mismatch search");
27578 
27579  for (sparse_vector_u32::size_type j = 0; j < sv_size; ++j)
27580  {
27581  std::chrono::time_point<std::chrono::steady_clock> st;
27582  st = std::chrono::steady_clock::now();
27583 
27585  v2 = ~v2;
27586  sv2[j] = v2;
27587  f = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
27588  assert(f);
27589  assert(pos == j);
27590 
27591  sv2[j] = ~v2; // restore
27592  f = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
27593  assert(!f);
27594 
27595  v2 = csv2[j];
27596  v2 = ~v2;
27597  csv2.set(j, v2);
27598  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27599  assert(f);
27600  assert(pos == j);
27601 
27602  csv2.set(j, ~v2); // restore
27603  f = bm::sparse_vector_find_first_mismatch(csv1, csv2, pos);
27604  assert(!f);
27605 
27606 
27607  if (j % 10000 == 0)
27608  {
27609  sv2.optimize();
27610  csv2.optimize();
27611  csv2.sync();
27612 
27613  std::chrono::time_point<std::chrono::steady_clock> f1 = std::chrono::steady_clock::now();
27614  auto diff = f1 - st;
27615  //auto d = std::chrono::duration <double, std::milli> (diff).count();
27616 
27617  if (!is_silent)
27618  cout << "\r" << j << "/" << sv_size << " " <<
27619  " (" << diff.count() << ")" << flush;
27620  }
27621  } // for
27622  cout << endl;
27623 
27624  switch(k)
27625  {
27626  case 0:
27627  sv1.optimize();
27628  break;
27629  case 1:
27630  sv1.optimize();
27631  csv1.optimize();
27632  break;
27633  case 2:
27634  sv1.optimize();
27635  csv1.optimize();
27636  sv2.optimize();
27637  break;
27638  case 3:
27639  sv1.optimize();
27640  csv1.optimize();
27641  sv2.optimize();
27642  csv2.optimize();
27643  break;
27644  default:
27645  assert(0);
27646  }
27647  } // for k
27648 
27649  cout << "Unique search OK" << endl;
27650  }
27651 
27652 
27653 
27654  cout << " --------------- TestCompressedSparseVectorAlgo() OK" << endl;
27655 }
27656 
27657 
27658 static
27660 {
27661  cout << " --------------- Test rsc_sparse_vector<> scan algo" << endl;
27662 
27664 
27665  {
27667  bvect bv_control;
27668  scanner.find_eq(csv, 25, bv_control);
27669  assert(!bv_control.any());
27670  scanner.invert(csv, bv_control);
27671  assert(!bv_control.any());
27672  }
27673 
27674  {
27676  bvect bv_res;
27677 
27678  csv.push_back(10, 10);
27679  csv.push_back(11, 10);
27680  csv.push_back(200, 0);
27681  csv.push_back(300, 0);
27682 
27683  csv.sync();
27684 
27685  bm::id_t idx;
27686  for (unsigned i = 0; i < 2; ++i)
27687  {
27688  bool found = scanner.find_eq(csv, 10, idx);
27689  assert(found);
27690  assert(idx == 10);
27691 
27692  scanner.find_eq(csv, 10, bv_res);
27693  assert(bv_res.count()==2);
27694  assert(bv_res.test(10));
27695  assert(bv_res.test(11));
27696 
27697  scanner.find_eq(csv, 0, bv_res);
27698  assert(bv_res.count()==2);
27699  assert(bv_res.test(200));
27700  assert(bv_res.test(300));
27701 
27702  found = scanner.find_eq(csv, 0, idx);
27703  assert(found);
27704  assert(idx == 200);
27705 
27706  csv.optimize();
27707  } // for
27708  }
27709 
27710  cout << " --------------- Test rsc_sparse_vector<> scan algo OK" << endl;
27711 
27712 }
27713 
27714 static
27716 {
27717  cout << " --------------- Test rsc_sparse_vector<> TestSparseVectorScanGT()" << endl;
27718 
27721  {
27724 
27725  sv.push_back_null(); // 0
27726  sv.push_back(1);
27727  sv.push_back(8); // 2
27728  sv.push_back(8+7);
27729  sv.push_back(8+1); // 4
27730  sv.push_back_null(); // 5
27731  sv.push_back(16);
27732  sv.push_back(0); // 7
27733  sv.push_back_null(); // 8
27734  sv.push_back(1023); // 9
27735 
27736  csv.load_from(sv);
27737 
27738  for (int pass = 0; pass < 2; ++pass)
27739  {
27740  auto it = sv.begin();
27741  auto it_end = sv.end();
27742  for (; it != it_end; ++it)
27743  {
27744  if (!it.is_null())
27745  {
27746  auto v1 = *it;
27747  CheckGTSearch(sv, v1, scanner_sv);
27748  CheckGTSearch(csv, v1, scanner_csv);
27749  }
27750  } // for
27751  sv.optimize();
27752  } // pass
27753  }
27754 
27755  cout << " stress test GT on random data " << endl;
27756  cout << " data generation..." << flush;
27757  {
27758  const rsc_sparse_vector_u32::size_type total_size = 25 * 1024 * 1024;
27759  const unsigned sample_size = 1024;
27760 
27761 
27763  {
27764  auto bit = csv.get_back_inserter();
27765  for (rsc_sparse_vector_u32::size_type i = 0; i < total_size; )
27766  {
27767  unsigned v = unsigned(rand()) & 0xFFF;
27768  unsigned len = unsigned(rand()) % 256;
27769  for (rsc_sparse_vector_u32::size_type j = 0; j < len; ++j)
27770  bit = v;
27771  i += len;
27772  len = len & 0xF;
27773  if (len)
27774  bit.add_null(len);
27775  i += len;
27776  } // for i
27777  bit.add_null(65536 * 4);
27778  bit = ~0u;
27779 
27780  bit.flush();
27781 
27782  for (rsc_sparse_vector_u32::size_type i = bm::id_max/2; i < bm::id_max/2+ 65536*4; ++i )
27783  {
27784  csv.push_back(i, 253); //set(i, 253);
27785  }
27786 
27787 
27788  csv.sync();
27789  cout << " Ok" << endl;
27790  }
27791 
27793  bvect bv_subset;
27794  const bvect* bv_null = csv.get_null_bvector();
27795  rsub.sample(bv_subset, *bv_null, sample_size);
27796  bv_subset.set(csv.size()-1);
27797 
27798  for (unsigned pass = 0; pass < 2; ++pass)
27799  {
27800  if (pass)
27801  csv.optimize();
27802  cout << " PASS = " << pass << " sample count = " << bv_subset.count() << endl;
27803  auto en = bv_subset.get_enumerator(0);
27804  for (unsigned cnt = 0;en.valid(); ++en, ++cnt)
27805  {
27806  auto i = *en;
27807  assert (!csv.is_null(i));
27808  auto v1 = csv.get(i);
27809  CheckGTSearch(csv, v1, scanner_csv);
27810  if (cnt & 0xF)
27811  if (!is_silent)
27812  cout << "\r" << cnt << "/" << sample_size << flush;
27813  }
27814 
27815  cout << endl;
27816  } // for
27817 
27818  }
27819 
27820  cout << " --------------- Test rsc_sparse_vector<> TestSparseVectorScanGT() OK " << endl;
27821 }
27822 
27823 
27824 // fill pseudo-random plato pattern into two vectors
27825 //
27826 template<class SV>
27827 void FillSparseIntervals(std::vector<unsigned>& vect,
27828  SV& svect,
27829  unsigned min,
27830  unsigned max,
27831  unsigned fill_factor)
27832 {
27833  unsigned diap = max - min;
27834 
27835  unsigned count;
27836 
27837 
27838  switch (fill_factor)
27839  {
27840  case 0:
27841  count = diap / 1000;
27842  break;
27843  case 1:
27844  count = diap / 100;
27845  break;
27846  default:
27847  count = diap / 10;
27848  break;
27849 
27850  }
27851 
27852  if (vect.size() < max)
27853  {
27854  vect.resize(max + 1);
27855  }
27856  if (svect.size() < max)
27857  {
27858  svect.resize(max + 1);
27859  }
27860 
27861  unsigned val = 0;
27862 
27863  for ( ;min < max; )
27864  {
27865  // hi-band interval
27866  val = (unsigned)rand() % (65535 * 2);
27867  unsigned i;
27868  for (i = 0; i < count; ++i)
27869  {
27870  vect[min] = val;
27871  svect.set(min, val);
27872  ++min;
27873  if (min > max)
27874  break;
27875  } // for i
27876 
27877  // gap with all zeroes
27878  unsigned inc = (unsigned)rand() % 2048;
27879  min += inc;
27880  if (min > max)
27881  break;
27882 
27883  // low band plato
27884  val = (unsigned)rand() % 8;
27885  for (i = 0; i < count; ++i)
27886  {
27887  vect[min] = val;
27888  svect.set(min, val);
27889  ++min;
27890  if (min > max)
27891  break;
27892  } // for i
27893 
27894  } // for min
27895 }
27896 
27897 static
27899 {
27900 
27901  cout << "---------------------------- Bit-plane sparse vector stress" << endl;
27902 
27903  cout << "Interval shift check.\n";
27904  // interval shift check
27905  bool detailed_check=true;
27906  for (unsigned i = 0; i < count; ++i)
27907  {
27908  unsigned fill_factor = 0;
27909  for (unsigned min = 0; min < 10000000; min+= (unsigned)rand()%100000)
27910  {
27911  unsigned max = min + (65535 * 10);
27912  {{
27913  bm::null_support null_able =
27914  (min % 2 == 0) ? bm::no_null : bm::use_null;
27915 
27916  std::vector<unsigned> vect;
27918 
27919  FillSparseIntervals(vect, sv, min, max, fill_factor);
27920 
27921  bool res = CompareSparseVector(sv, vect, true, detailed_check);
27922  if (!res)
27923  {
27924  cerr << "sparse-dense vector comparison failed" << endl;
27925  exit(1);
27926  }
27927 
27928  sv.optimize();
27929  res = CompareSparseVector(sv, vect, true);
27930  if (!res)
27931  {
27932  cerr << "sparse-dense vector comparison failed" << endl;
27933  exit(1);
27934  }
27935  if (!is_silent)
27936  cout << "\r min=" << min << " max=" << max << " ff= "
27937  << fill_factor << flush;
27938  }}
27939  if (++fill_factor > 2) fill_factor = 0;
27940  } // for min
27941  cout << "\n." << flush;
27942  detailed_check = false; // all other passes go faster
27943  } // for i
27944  cout << endl;
27945 
27946  cout << "--------------------------- Interval shift check Ok" << endl;
27947 
27948  cout << "Join check" << endl;
27949  for (unsigned i = 0; i < 1; ++i)
27950  {
27951  unsigned fill_factor = 0;
27952  for (unsigned min = 0; min < 10000000; min+= (unsigned)rand()%100000)
27953  {
27954  unsigned max = min + (65535 * 10);
27955  unsigned min2 = max + (unsigned)rand() % 65536;
27956  unsigned max2 = min2 + (65535 * 10);
27957 
27958  bm::null_support null_able1 =
27959  (min % 2 == 0) ? bm::no_null : bm::use_null;
27960 
27961  {{
27962  std::vector<unsigned> vect1;
27963  std::vector<unsigned> vect2;
27964  bm::sparse_vector<unsigned, bvect > sv1(null_able1);
27965  bm::sparse_vector<unsigned, bvect > sv2(null_able1);
27966 
27967  FillSparseIntervals(vect1, sv1, min, max, fill_factor);
27968  FillSparseIntervals(vect2, sv2, min2, max2, fill_factor);
27969 
27970  if (rand()%2)
27971  {
27972  sv1.optimize();
27973  }
27974  if (rand()%3 == 0)
27975  {
27976  sv2.optimize();
27977  }
27980 
27981  sv1.join(sv2);
27982  sv3.join(sv1);
27983  sv3.join(sv2);
27984  sv4.join(sv1);
27985 
27986  if (sv1.size() != sv3.size() || sv2.size() != sv3.size() || sv3.size() != sv4.size())
27987  {
27988  cerr << "Sparse join size error:" << sv1.size() << endl;
27989  assert(0); exit(1);
27990  }
27991 
27992 
27993  for ( i = 0; i < sv1.size(); ++i)
27994  {
27995  unsigned v1 = sv1[i];
27996  unsigned v3 = sv3[i];
27997  unsigned v4 = sv4[i];
27998  if (v1 != v3 || v1 != v4)
27999  {
28000  cerr << "Sparse join cmp failed:" << v1 << "!=" << v3 << "!=" << v4 << endl;
28001  assert(0);exit(1);
28002  }
28003  } // for i
28004 
28005  bool b1 = TestEqualSparseVectors(sv1, sv3, false);
28006  if (!b1)
28007  {
28008  cerr << "Equal 1 comparison failed" << endl;
28009  assert(0);exit(1);
28010  }
28011  bool b2 = TestEqualSparseVectors(sv1, sv4, false);
28012  if (!b2)
28013  {
28014  cerr << "Equal 2 comparison failed" << endl;
28015  assert(0); exit(1);
28016  }
28017 
28018  bool b3 = TestEqualSparseVectors(sv3, sv4, false);
28019  if (!b3)
28020  {
28021  cerr << "Equal 3 comparison failed" << endl;
28022  assert(0); exit(1);
28023  }
28024 
28025 
28026 
28027  cout << "+" << flush;
28028 
28029  }}
28030  ++fill_factor;
28031  if (fill_factor > 2) fill_factor = 0;
28032  } // for min
28033 
28034  cout << "." << flush;
28035 
28036  } // for i
28037 
28038  cout << "--------------------------- Join check Ok" << endl;
28039 
28040 
28041 
28042  cout << "---------------------------- Bit-plane sparse vector stress OK" << endl;
28043 }
28044 
28045 template<class STR_SV>
28046 void CheckStrSVCompare(const STR_SV& str_sv,
28047  typename STR_SV::size_type limit = 0)
28048 {
28049  char emptyStr[] = "";
28050  if (!limit)
28051  limit = str_sv.size();
28052 
28053  typename STR_SV::size_type i, j;
28054  i = j = 0;
28055  auto it1 = str_sv.begin();
28056  auto it_end = str_sv.end();
28057  for (; it1 != it_end; ++it1, ++i)
28058  {
28059  const char* s1 = *it1;
28060  if (!s1)
28061  {
28062  assert(it1.is_null());
28063  s1 = emptyStr;
28064  }
28065 
28066  auto it2 = str_sv.begin();
28067  it2.go_to(i);
28068  for (j = i; j < limit; ++it2, ++j)
28069  {
28070  assert(it2 != it_end);
28071  const char* s2 = *it2;
28072  if (!s2)
28073  {
28074  assert(it2.is_null());
28075  s2 = emptyStr;
28076  }
28077  int r2 = ::strcmp(s1, s2);
28078  int r1 = str_sv.compare(i, j);
28079  assert (r1 == r2 || (r1 < 0 && r2 < 0) || (r1 > 0 && r2 > 0));
28080  } // for j
28081  if ((!is_silent) && ((i & 0xFF) == 0))
28082  cout << "\r " << i << " / " << (limit ? limit : str_sv.size()) << flush;
28083  } // for i
28084  cout << endl << endl;
28085 }
28086 
28087 
28088 static
28090 {
28091  cout << "---------------------------- TestStrSparseVector()" << endl;
28092 
28094 
28095  {
28097  str_sparse_vector<char, bvect, 32> str_sv1(str_sv0);
28099  str_sv2 = str_sv1;
28100 
28101  assert(str_sv1.size() == 0);
28102  str_sparse_vector<char, bvect, 32> str_sv3(std::move(str_sv0));
28103  assert(!str_sv3.is_remap());
28104  assert(str_sv0.get_null_support() == bm::no_null);
28105  }
28106 
28107  // test automatic resize
28108  {
28109  char str[256];
28110  const char* s0 = "A";
28111  const char* s1 = "jKl";
28112  {
28114  str_sv0.set(0, s0);
28115  str_sv0.set(2, s1);
28116  auto esize = str_sv0.effective_max_str();
28117  assert(esize >= 4);
28118 
28119  str_sv0.get(0, str, sizeof(str));
28120  int cmp = ::strcmp(str, s0);
28121  assert(cmp == 0);
28122 
28123  str_sv0.get(2, str, sizeof(str));
28124  cmp = ::strcmp(str, s1);
28125  assert(cmp == 0);
28126 
28127  bool b = str_sv0.is_null(1);
28128  assert(b);
28129  b = str_sv0.is_null(0);
28130  assert(!b);
28131  b = str_sv0.is_null(2);
28132  assert(!b);
28133 
28134  std::string s;
28135  b = str_sv0.try_get(1, s);
28136  assert(!b);
28137  b = str_sv0.try_get(2, s);
28138  assert(b);
28139  assert(s == s1);
28140  b = str_sv0.try_get(0, s);
28141  assert(b);
28142  assert(s == s0);
28143  }
28144 
28145  {
28147  const auto& bmatr = str_sv0.get_bmatrix();
28148 
28149  assert(bmatr.get_null_idx()==16);
28150  str_sv0.set(0, s0);
28151  assert(bmatr.get_null_idx()==16);
28152  str_sv0.set(2, s1);
28153  assert(bmatr.get_null_idx()==32);
28154 
28155 
28156 
28157  str_sv0.get(0, str, sizeof(str));
28158  int cmp = ::strcmp(str, s0);
28159  assert(cmp == 0);
28160 
28161  str_sv0.get(2, str, sizeof(str));
28162  cmp = ::strcmp(str, s1);
28163  assert(cmp == 0);
28164 
28165  bool b = str_sv0.is_null(1);
28166  assert(b);
28167  b = str_sv0.is_null(0);
28168  assert(!b);
28169  b = str_sv0.is_null(2);
28170  assert(!b);
28171  }
28172 
28173  }
28174 
28175  // remap of null-able vector bug
28176  {
28177  using TSparseOptVector = bm::str_sparse_vector<char, bm::bvector<>, 2>;
28178  TSparseOptVector ssv1(bm::use_null);
28179  ssv1.set(0, "s1");
28180  bool b = ssv1.is_null(0);
28181  assert(!b);
28182 
28183  string s;
28184  ssv1.get(0, s);
28185  assert(s == "s1");
28186  {
28187  TSparseOptVector::const_iterator it(&ssv1);
28188  assert(it.valid());
28189  const char* str = *it;
28190  assert(strcmp(str, "s1")==0);
28191  }
28192 
28193  ssv1.remap();
28194  b = ssv1.is_null(0);
28195  assert(!b);
28196  {
28197  TSparseOptVector::const_iterator it(&ssv1);
28198  assert(it.valid());
28199  const char* str = *it;
28200  assert(strcmp(str, "s1")==0);
28201  }
28202 
28203  ssv1.get(0, s);
28204  assert(s == "s1");
28205  }
28206 
28207  // use of remap back iserter
28208  {
28209  using TSparseOptVector = bm::str_sparse_vector<char, bm::bvector<>, 2>;
28210  TSparseOptVector str_sv1, str_sv0;
28211  auto iit1 = str_sv1.get_back_inserter();
28212  iit1.set_remap(true);
28213 
28214  auto iit0 = str_sv0.get_back_inserter();
28215 
28216  assert(1 == iit1.get_remap());
28217 
28218  *iit1 = "1";
28219  *iit1 = "10";
28220  *iit1 = "10a";
28221  *iit1 = "2100";
28222 
28223  {
28224  auto omatr = iit1.get_octet_matrix();
28225  const auto* r0 = omatr.row(0);
28226  const auto* r1 = omatr.row(1);
28227  const auto* r2 = omatr.row(2);
28228 
28229  assert(r0[int('1')] == 3);
28230  assert(r0[int('0')] == 0);
28231  assert(r0[int('2')] == 1);
28232 
28233  assert(r1[int('1')] == 1);
28234  assert(r1[int('0')] == 2);
28235 
28236  assert(r2[int('a')] == 1);
28237  assert(r2[int('0')] == 1);
28238  }
28239 
28240  iit1.flush();
28241 
28242  *iit0 = "1";
28243  *iit0 = "10";
28244  *iit0 = "10a";
28245  *iit0 = "2100";
28246 
28247  assert(0 == iit0.get_remap());
28248 
28249  iit0.flush();
28250 
28251  assert(str_sv1.is_remap());
28252  str_sv0.remap();
28253  assert(str_sv0.is_remap());
28254 
28255  bool b = str_sv0.equal(str_sv1);
28256  assert(b);
28257 
28258  // derive remapping
28259  {
28260  TSparseOptVector str_sv2(str_sv0, bm::remap_setup::COPY_RTABLES);
28261  assert(str_sv2.size()==0);
28262  assert(str_sv2.is_remap());
28263 
28264  auto iit2 = str_sv2.get_back_inserter();
28265  *iit2 = "1";
28266  *iit2 = "10";
28267  *iit2 = "10a";
28268  *iit2 = "2100";
28269 
28270  iit2.flush();
28271 
28272  b = str_sv0.equal(str_sv2);
28273  assert(b);
28274  }
28275  }
28276 
28277 
28278  {
28279  using TSparseOptVector = bm::str_sparse_vector<char, bm::bvector<>, 2>;
28280  TSparseOptVector str_sv1, str_sv0;
28281  {
28282  auto iit1 = str_sv1.get_back_inserter();
28283  iit1.set_remap(true);
28284  //iit1.set_optimize(bvect::opt_compress);
28285 
28286  for (unsigned i = 0; i < 100000; ++i)
28287  {
28288  iit1 = "1";
28289  iit1 = "723";
28290  }
28291 
28292  iit1 = "abcd";
28293  iit1.flush();
28294  }
28295 
28296  {
28297  auto iit0 = str_sv0.get_back_inserter();
28298  for (unsigned i = 0; i < 100000; ++i)
28299  {
28300  iit0 = "1";
28301  iit0 = "723";
28302  }
28303 
28304  iit0 = "abcd";
28305  iit0.flush();
28306  }
28307  str_sv0.remap();
28308  bool b = str_sv0.equal(str_sv1);
28309  assert(b);
28310  }
28311 
28312  // samplex index tests
28313  {
28314  using TSparseOptVector = bm::str_sparse_vector<char, bm::bvector<>, 2>;
28315  TSparseOptVector::size_type l, r;
28316  TSparseOptVector str_sv1, str_sv0;
28317  {
28319  s_idx.construct(str_sv1, 64);
28320  assert(s_idx.size() == 0);
28321  }
28322  str_sv1.push_back("1");
28323  {
28324  bm::sv_sample_index<TSparseOptVector> s_idx(str_sv1, 64);
28325  assert(s_idx.size() == 1);
28326  assert(s_idx.sv_size() == 1);
28327  assert(s_idx.is_unique());
28328 
28329  bool found = s_idx.bfind_range("0", 1, l, r);
28330  assert(!found);
28331  found = s_idx.bfind_range("1", 1, l, r);
28332  assert(found);
28333  assert(l == 0);
28334  assert(r == 0);
28335 
28336  }
28337  str_sv1.push_back("2");
28338  {
28339  bm::sv_sample_index<TSparseOptVector> s_idx(str_sv1, 64);
28340  assert(s_idx.size() == 2);
28341  assert(s_idx.sv_size() == 2);
28342  assert(s_idx.is_unique());
28343 
28344  }
28345 
28346 
28347  for (unsigned i = 0; i < 65536*16; ++i)
28348  {
28349  {
28350  bm::sv_sample_index<TSparseOptVector> s_idx(str_sv0, 64);
28351  assert(s_idx.sv_size() == i);
28352  if (i <= 1)
28353  {
28354  assert(s_idx.is_unique());
28355  }
28356  else
28357  {
28358  assert(!s_idx.is_unique());
28359  }
28360 
28361  if (i)
28362  {
28363  //assert(s_idx.size() == (i / 64) || (s_idx.size() == (i / 64)+1) || (s_idx.size() == (i / 64)+2));
28364  }
28365  bool found = s_idx.bfind_range("0", 1, l, r);
28366  assert(!found);
28367  found = s_idx.bfind_range("129", 3, l, r);
28368  assert(!found);
28369  if (i)
28370  {
28371  found = s_idx.bfind_range("123", 3, l, r);
28372  assert(found);
28373  assert(l >= 0);
28374  assert(r <= s_idx.size()-1);
28375 
28376  s_idx.recalc_range("123", l, r);
28377  assert(l >= 0);
28378  assert(r <= str_sv0.size()-1);
28379  assert(l <= r);
28380  }
28381 
28382  }
28383 
28384  str_sv0.push_back("123");
28385 
28386  if (((i & 0xFFFF) == 0) && (!is_silent))
28387  cout << "\r" << i << flush;
28388  } // for i
28389 
28390  }
28391 
28392 
28393 
28394  // test from Andrea Asztalos
28395  {
28396  using TSparseOptVector = bm::str_sparse_vector<char, bm::bvector<>, 2>;
28397  TSparseOptVector str_vector(bm::use_null);
28398  auto inserter = str_vector.get_back_inserter();
28399  *inserter = "rs_id1";
28400  *inserter = "rs_is2";
28401  *inserter = "rs_id3";
28402  *inserter = "VÉGE";
28403  inserter.flush();
28404 
28405  auto esize = str_vector.effective_max_str();
28406  assert(esize >= 7);
28407 
28408  char str[256];
28409  str_vector.get(0, str, sizeof(str));
28410  int cmp = ::strcmp(str, "rs_id1");
28411  assert(cmp == 0);
28412  str_vector.get(3, str, sizeof(str));
28413  cmp = ::strcmp(str, "VÉGE");
28414  assert(cmp == 0);
28415 
28417  str_vector.remap();
28418  str_vector.optimize(tb);
28419  str_vector.freeze();
28420  assert(str_vector.is_ro());
28421 
28422  str_vector.get(3, str, sizeof(str));
28423  cmp = ::strcmp(str, "VÉGE");
28424  assert(cmp == 0);
28425 
28426  CheckStrSVCompare(str_vector);
28427  }
28428 
28429  // test from Andrea Asztalos
28430  {
28431  using bvector_type = bm::bvector<>;
28432  using TSparseStrVector = bm::str_sparse_vector<char, bvector_type, 390>;
28433 
28434  TSparseStrVector vec_A(bm::use_null);
28435  auto iter_1 = vec_A.get_back_inserter();
28436  iter_1.add_null(10);
28437  iter_1.flush();
28438 
28440  vec_A.remap();
28441  vec_A.optimize(tb);
28442  vec_A.freeze();
28443 
28445  auto layout_a = make_unique<TLayout>();
28446 
28448 
28449  str_serializer.set_bookmarks(true, 16);
28450  str_serializer.enable_xor_compression();
28451  assert(str_serializer.is_xor_ref());
28452 
28453  str_serializer.serialize(vec_A, *layout_a.get());
28454 
28455  vector<unsigned char>* buffer = new vector<unsigned char>(layout_a->size());
28456 
28457  {
28458  // store the serialized data
28459  unsigned char* buf_ptr = buffer->data();
28460  memcpy(buf_ptr, layout_a->data(), layout_a->size());
28461  }
28462 
28463  // deserialize it
28464  unsigned char* data_ptr = buffer->data();
28465 
28467 
28468  TSparseStrVector des_vec_A(bm::use_null);
28469 
28470  deserializer.deserialize(des_vec_A, data_ptr); //<--- runs into assertion
28471  }
28472 
28473  // bug fix for not clearing value on set_null
28474  {
28475  using TSparseOptVector = bm::str_sparse_vector<char, bm::bvector<>, 2>;
28476  TSparseOptVector ssv1(bm::use_null);
28477  ssv1.set_null(1);
28478  assert(ssv1.size() == 2);
28479 
28480  ssv1.set(0, "test data");
28481  bool b;
28482  b = ssv1.is_null(0);
28483  assert(!b);
28484  b = ssv1.is_null(1);
28485  assert(b);
28486 
28487  ssv1.set(0, "test data 1");
28488  b = ssv1.is_null(0);
28489  assert(!b);
28490 
28491  ssv1.set(0, "test d");
28492  b = ssv1.is_null(0);
28493  assert(!b);
28494 
28495  ssv1.set_null(0);
28496  b = ssv1.is_null(0);
28497  assert(b);
28498  auto it = ssv1.begin();
28499  const char* ch = *it;
28500  assert(!ch);
28501  }
28502 
28503 
28504  // bulk set_null
28505  {
28506  using TSparseOptVector = bm::str_sparse_vector<char, bm::bvector<>, 2>;
28507  TSparseOptVector ssv1(bm::use_null);
28508 
28509  ssv1.set(0, "s1");
28510  ssv1.set(1, "s1");
28511  ssv1.set(4, "s4");
28512 
28513  TSparseOptVector::bvector_type bv {0, 2, 4, 5};
28514  ssv1.set_null(bv);
28515  bool b;
28516  b = ssv1.is_null(0);
28517  assert(b);
28518  b = ssv1.is_null(1);
28519  assert(!b);
28520  b = ssv1.is_null(2);
28521  assert(b);
28522  b = ssv1.is_null(4);
28523  assert(b);
28524 
28525  auto it = ssv1.begin();
28526  const char* ch = *it;
28527  assert(!ch);
28528  }
28529 
28530 
28531  // bulk clear
28532  {
28533  using TSparseOptVector = bm::str_sparse_vector<char, bm::bvector<>, 2>;
28534  TSparseOptVector ssv1(bm::use_null);
28535 
28536  ssv1.set(0, "s1");
28537  ssv1.set(1, "z1");
28538  ssv1.set(4, "s4");
28539 
28540  ssv1.remap();
28541  ssv1.optimize();
28542 
28543  TSparseOptVector::bvector_type bv {0, 2, 4, 5};
28544  ssv1.clear(bv);
28545 
28546  for (unsigned i = 0; i < ssv1.size(); ++i)
28547  {
28548  string s;
28549  ssv1.get(i, s);
28550  cout << s << ", ";
28551  }
28552  cout << endl;
28553 
28554  bool b;
28555  b = ssv1.is_null(0);
28556  assert(!b);
28557  b = ssv1.is_null(1);
28558  assert(!b);
28559  b = ssv1.is_null(2);
28560  assert(b);
28561  b = ssv1.is_null(4);
28562  assert(!b);
28563 
28564  auto it = ssv1.begin();
28565  const char* ch = *it;
28566  assert(!ch[0]);
28567  ++it;
28568  ch = *it;
28569  assert(ch[0] == 'z');
28570  ++it; ++it;
28571  ch = *it;
28572  assert(!ch);
28573  }
28574 
28575  // swap test
28576  {
28577  using TSparseOptVector = bm::str_sparse_vector<char, bm::bvector<>, 2>;
28578  TSparseOptVector ssv1(bm::use_null);
28579 
28580  ssv1.set(0, "s1");
28581  ssv1.set(1, "z1");
28582  ssv1.set(4, "s4");
28583 
28584  ssv1.remap();
28585  ssv1.optimize();
28586 
28587  ssv1.swap(1, 2);
28588  bool b;
28589  b = ssv1.is_null(1);
28590  assert(b);
28591  b = ssv1.is_null(2);
28592  assert(!b);
28593 
28594  int cmp;
28595  char str[256];
28596  ssv1.get(2, str, sizeof(str));
28597  cmp = ::strcmp(str, "z1");
28598  assert(cmp==0);
28599  }
28600 
28601 
28602  {
28603  const char* s0 = "AbC";
28604  const char* s1 = "jKl";
28605  char str[256];
28607  int cmp;
28608 
28609  assert(str_sv0.size()==0);
28610  str_sv0.set(0, s0);
28611  str_sv0.get(0, str, sizeof(str));
28612  cmp = ::strcmp(str, s0);
28613  assert(cmp == 0);
28614  assert(str_sv0.size()==1);
28615 
28616  str_sv0.set(1, s1);
28617  str_sv0.get(0, str, sizeof(str));
28618  cmp = ::strcmp(str, s0);
28619  assert(cmp == 0);
28620 
28621  str_sv0.get(1, str, sizeof(str));
28622  cmp = ::strcmp(str, s1);
28623  assert(cmp == 0);
28624 
28625  str_sv0.optimize();
28626 
28627  str_sv0.get(0, str, sizeof(str));
28628  cmp = ::strcmp(str, s0);
28629  assert(cmp == 0);
28630 
28631  str_sv0.get(1, str, sizeof(str));
28632  cmp = ::strcmp(str, s1);
28633  assert(cmp == 0);
28634 
28635  string str0 = "AtGc";
28636  str_sv0.assign(3, str0);
28637  str_sv0.get(3, str, sizeof(str));
28638  cmp = ::strcmp(str, str0.c_str());
28639  assert(cmp == 0);
28640  //auto sz=str_sv0.size();
28641  assert(str_sv0.size()==4);
28642 
28643  string str1;
28644  str_sv0.get(3, str1);
28645  assert(str0 == str1);
28646  {
28647  str0 = "TTF";
28648  str_sv0.assign(3, str0);
28649  str_sv0.get(3, str, sizeof(str));
28650  cmp = ::strcmp(str, str0.c_str());
28651  assert(cmp==0);
28652 
28653  str0.clear();
28654  str_sv0.assign(3, str0);
28655  str_sv0.get(3, str, sizeof(str));
28656  cmp = ::strcmp(str, str0.c_str());
28657  assert(cmp==0);
28658  }
28659 
28660  // test string insert
28661  {
28663  const char* cs0 = "10";
28664  const char* cs2 = "30";
28665  const char* cs1 = "200";
28666 
28667  str_sv10.push_back(cs0);
28668  auto esize = str_sv10.effective_max_str();
28669  assert(esize >= 3);
28670  str_sv10.push_back(cs1);
28671  str_sv10.insert(1, cs2);
28672  esize = str_sv10.effective_max_str();
28673  assert(esize >= 4);
28674 
28675  str_sv10.get(0, str, sizeof(str));
28676  cmp = ::strcmp(str, cs0);
28677  assert(cmp == 0);
28678 
28679  str_sv10.get(1, str, sizeof(str));
28680  cmp = ::strcmp(str, cs2);
28681  assert(cmp == 0);
28682 
28683  str_sv10.get(2, str, sizeof(str));
28684  cmp = ::strcmp(str, cs1);
28685  assert(cmp == 0);
28686 
28687  str_sv10.clear();
28688  assert(str_sv10.size() == 0);
28689  }
28690 
28691  // test erase
28692  {
28694  const char* cs0 = "10";
28695  const char* cs1 = "200";
28696  const char* cs2 = "30";
28697 
28698  str_sv10.push_back(cs0);
28699  str_sv10.push_back(cs1);
28700  str_sv10.push_back(cs2);
28701 
28702  str_sv10.erase(1);
28703  assert(str_sv10.size() == 2);
28704 
28705  str_sv10.get(0, str, sizeof(str));
28706  cmp = ::strcmp(str, cs0);
28707  assert(cmp == 0);
28708  str_sv10.get(1, str, sizeof(str));
28709  cmp = ::strcmp(str, cs2);
28710  assert(cmp == 0);
28711 
28712  str_sv10.erase(0);
28713  assert(str_sv10.size() == 1);
28714  }
28715 
28716  // test merge
28717  {
28720 
28721  str_sv00.push_back("0");
28722  str_sv00.push_back("1");
28723  str_sv00.push_back("");
28724  str_sv00.push_back("3");
28725 
28726  str_sv1.push_back("");
28727  str_sv1.push_back("");
28728  str_sv1.push_back("2");
28729  str_sv1.push_back("8");
28730 
28731  str_sv1.keep_range(2, 2);
28732  {
28733  str_sv1.get(3, str, sizeof(str));
28734  cmp = ::strcmp(str, "");
28735  assert(cmp == 0);
28736 
28737  }
28738 
28739  str_sv00.merge(str_sv1);
28740 
28741  str_sv00.get(2, str, sizeof(str));
28742  cmp = ::strcmp(str, "2");
28743  assert(cmp == 0);
28744 
28745  str_sv0.remap();
28746  {
28748  str_sv.copy_range(str_sv00, 0, 1);
28749 
28751  str_sv2.copy_range(str_sv00, 2, 3);
28752  str_sv.merge(str_sv2);
28753 
28754  bool b = str_sv.equal(str_sv00);
28755  assert(b);
28756 
28757 
28758  }
28759 
28760  {
28762  str_sv.merge(str_sv00);
28763  bool b = str_sv.equal(str_sv00);
28764  assert(!b); // destucted vector
28765  }
28766  }
28767 
28768 
28769  // test merge 2
28770  {
28774 
28775  str_svC.push_back("0");
28776  str_svC[2] = "12345";
28777 
28778  str_sv00.push_back("0");
28779  str_sv1[2] = "12345";
28780 
28781 
28782  str_sv00.merge(str_sv1);
28783 
28784  bool b = str_svC.equal(str_sv00);
28785  assert(b); // destucted vector
28786  }
28787 
28788  // test merge 3
28789  {
28793 
28794  str_svC.push_back("0");
28795  str_svC[2] = "12345";
28796 
28797  str_sv00.push_back("0");
28798  str_sv1[2] = "12345";
28799 
28800 
28801  str_sv1.merge(str_sv00);
28802 
28803  bool b = str_svC.equal(str_sv1);
28804  assert(b); // destucted vector
28805  }
28806 
28807 
28808 
28809 
28810  // test decode
28811  {
28813  const char* cs0 = "10";
28814  const char* cs1 = "200";
28815  const char* cs2 = "30";
28816  str_sv10.push_back(cs0);
28817  str_sv10.push_back(cs1);
28818  str_sv10.push_back(cs2);
28819 
28820  CheckStrSVCompare(str_sv10);
28821 
28823 
28824  unsigned d = 0;
28825  char *s;
28826 
28827  for (unsigned pass = 0; pass < 2; ++pass)
28828  {
28829  d = str_sv10.decode(hmatr, 0, 1);
28830  s = hmatr.row(0);
28831  cmp = ::strcmp(s, cs0);
28832  assert(cmp == 0);
28833  assert(d == 1);
28834 
28835  d = str_sv10.decode(hmatr, 1, 1);
28836  s = hmatr.row(0);
28837  cmp = ::strcmp(s, cs1);
28838  assert(cmp == 0);
28839  assert(d == 1);
28840 
28841  d = str_sv10.decode(hmatr, 2, 1);
28842  s = hmatr.row(0);
28843  cmp = ::strcmp(s, cs2);
28844  assert(cmp == 0);
28845  assert(d == 1);
28846 
28847  // decode beyond limit
28848  d = str_sv10.decode(hmatr, 3, 1);
28849  s = hmatr.row(0);
28850  assert(*s == 0);
28851  assert(d == 0);
28852 
28853  d = str_sv10.decode(hmatr, 0, 100000);
28854  assert(d == str_sv10.size());
28855  s = hmatr.row(0);
28856  cmp = ::strcmp(s, cs0);
28857  assert(cmp == 0);
28858  s = hmatr.row(1);
28859  cmp = ::strcmp(s, cs1);
28860  assert(cmp == 0);
28861  s = hmatr.row(2);
28862  cmp = ::strcmp(s, cs2);
28863  assert(cmp == 0);
28864 
28865  d = str_sv10.decode(hmatr, 1, 100000);
28866  assert(d == str_sv10.size()-1);
28867  s = hmatr.row(0);
28868  cmp = ::strcmp(s, cs1);
28869  assert(cmp == 0);
28870  s = hmatr.row(1);
28871  cmp = ::strcmp(s, cs2);
28872  assert(cmp == 0);
28873 
28874 
28875  str_sv10.optimize();
28876  }
28877 
28878  }
28879  // test decode sub-string
28880  //
28881  {
28883  const char* cs0 = "10";
28884  const char* cs1 = "200";
28885  const char* cs2 = "30";
28886  str_sv10.push_back(cs0);
28887  str_sv10.push_back(cs1);
28888  str_sv10.push_back(cs2);
28889 
28891  unsigned d = 0;
28892  char *s;
28893  for (unsigned pass = 0; pass < 2; ++pass)
28894  {
28895  d = str_sv10.decode_substr(hmatr, 0, 1, 0, 10);
28896  s = hmatr.row(0);
28897  cmp = ::strcmp(s, cs0);
28898  assert(cmp == 0);
28899  assert(d == 1);
28900 
28901  d = str_sv10.decode_substr(hmatr, 0, 1, 0, 1);
28902  s = hmatr.row(0);
28903  cmp = ::strcmp(s, cs0);
28904  assert(cmp == 0);
28905  assert(d == 1);
28906 
28907  d = str_sv10.decode_substr(hmatr, 1, 1, 0, 1);
28908  s = hmatr.row(0);
28909  assert(s[0] == cs1[0] && s[1] == cs1[1] && s[2]==0);
28910  assert(d == 1);
28911 
28912  d = str_sv10.decode_substr(hmatr, 0, 1, 1, 1);
28913  s = hmatr.row(0);
28914  assert(s[0] == cs0[1] && s[1]==0);
28915 
28916 
28917  d = str_sv10.decode_substr(hmatr, 1, 1, 1, 2);
28918  s = hmatr.row(0);
28919  assert(s[0] == cs1[1] && s[1] == cs1[2] && s[2]==0);
28920  assert(d == 1);
28921 
28922  d = str_sv10.decode_substr(hmatr, 1, 2, 0, 0);
28923  assert(d == 2);
28924  s = hmatr.row(0);
28925  assert(s[0] == cs1[0] && s[1]==0);
28926  s = hmatr.row(1);
28927  assert(s[0] == cs2[0] && s[1]==0);
28928 
28929  d = str_sv10.decode_substr(hmatr, 1, 2, 1, 2);
28930  assert(d == 2);
28931  s = hmatr.row(0);
28932  assert(s[0] == cs1[1] && s[1]==cs1[2] && s[2]==0);
28933  s = hmatr.row(1);
28934  assert(s[0] == cs2[1] && s[1]==0);
28935 
28936 
28937  str_sv10.optimize();
28938  } // for pass
28939 
28940  }
28941 
28942  // test import
28943  {
28945 
28946  const char* cs0 = "@";
28947  const char* cs1 = " 2";
28948  const char* cs2 = "034";
28949 
28951  #if defined(_MSC_VER)
28952  ::strncpy_s(hmatr.row(0), hmatr.cols(), cs0, hmatr.cols());
28953  ::strncpy_s(hmatr.row(1), hmatr.cols(), cs1, hmatr.cols());
28954  ::strncpy_s(hmatr.row(2), hmatr.cols(), cs2, hmatr.cols());
28955  #else
28956  ::strncpy(hmatr.row(0), cs0, hmatr.cols());
28957  ::strncpy(hmatr.row(1), cs1, hmatr.cols());
28958  ::strncpy(hmatr.row(2), cs2, hmatr.cols());
28959  #endif
28960 
28961  for (unsigned i = 0; i < 3; ++i)
28962  {
28963  const char* s = hmatr.row(i);
28964  cout << s << endl;
28965  }
28966 
28967  str_sv10.import(hmatr, 0, 3);
28968  assert(str_sv10.size() == 3);
28969 
28970  str_sv10.get(0, str, sizeof(str));
28971  cmp = ::strcmp(str, cs0);
28972  assert(cmp == 0);
28973 
28974  str_sv10.get(1, str, sizeof(str));
28975  cmp = ::strcmp(str, cs1);
28976  assert(cmp == 0);
28977 
28978  str_sv10.get(2, str, sizeof(str));
28979  cmp = ::strcmp(str, cs2);
28980  assert(cmp == 0);
28981  }
28982  }
28983 
28984  // test from A.Shkeda
28985  {
28986  typedef bm::str_sparse_vector<char, bm::bvector<>, 64 > str_sparse_vect64;
28987  str_sparse_vect64 m_acc_vec;
28988  str_sparse_vect64::back_insert_iterator m_acc_vec_bi;
28989  m_acc_vec_bi = m_acc_vec.get_back_inserter();
28990  string s = "hello!";
28991  m_acc_vec_bi = s;
28992 
28993  }
28994 
28995 
28996  // reference test / serialization test
28997  {
28999  char str[256];
29000  string str0;
29001 
29002  auto r = str_sv0[3];
29003  const char* s = r.get();
29004  int cmp = ::strcmp(s, str0.c_str());
29005  assert(cmp == 0);
29006  str_sv0[3] = "333";
29007  str_sv0.get(3, str, sizeof(str));
29008 
29009  cmp = ::strcmp(str_sv0[3].get(), "333");
29010  assert(cmp == 0);
29011 
29012  {
29013  const str_sparse_vector<char, bvect, 2>& ssv = str_sv0;
29015  s = ref3;
29016  cmp = ::strcmp(s, "333");
29017  assert(cmp == 0);
29018  }
29019 
29022  bm::sparse_vector_serialize<str_svect_type>(str_sv0, sv_lay, tb);
29023 
29025 
29026  const unsigned char* buf = sv_lay.buf();
29027  int res = bm::sparse_vector_deserialize(str_sv2, buf, tb);
29028  if (res != 0)
29029  {
29030  cerr << "De-Serialization error" << endl;
29031  exit(1);
29032  }
29033 
29034  bool eq = str_sv0.equal(str_sv2);
29035  assert(eq);
29036 
29037  str_sparse_vector<char, bvect, 2> str_sv3; // size increase test
29038  buf = sv_lay.buf();
29039  res = bm::sparse_vector_deserialize(str_sv3, buf, tb);
29040  if (res != 0)
29041  {
29042  cerr << "De-Serialization error" << endl;
29043  exit(1);
29044  }
29045  }
29046 
29047  {
29049  unsigned str_max = str_sv0.effective_max_str();
29050  assert(str_max > 0);
29051  str_sv0[0] = "1";
29052  str_max = str_sv0.effective_max_str();
29053  assert(str_max >= 1);
29054  str_sv0[1] = "11";
29055  str_max = str_sv0.effective_max_str();
29056  assert(str_max >= 2);
29057  str_sv0[2] = "123";
29058  str_max = str_sv0.effective_max_str();
29059  assert(str_max >= 3);
29060 
29061  str_sv0.clear_range(1, 234567);
29062 
29063  char str[256];
29064  str_sv0.get(1, str, sizeof(str));
29065  assert(str[0]==0);
29066  str_sv0.get(2, str, sizeof(str));
29067  assert(str[0]==0);
29068  }
29069 
29070  {
29072  str_sv0[0] = "1";
29073  str_sv0[1] = "11";
29074  str_sv0[2] = "123";
29075 
29076  CheckStrSVCompare(str_sv0);
29077 
29079  for (unsigned pass = 0; pass < 2; ++pass)
29080  {
29081  unsigned pos;
29082 
29083  bool found = scanner.find_eq_str(str_sv0, "1", pos);
29084  assert(found);
29085  assert(pos == 0);
29086 
29087  found = scanner.find_eq_str(str_sv0, "11", pos);
29088  assert(found);
29089  assert(pos == 1);
29090 
29091  found = scanner.find_eq_str(str_sv0, "123", pos);
29092  assert(found);
29093  assert(pos == 2);
29094 
29095  found = scanner.find_eq_str(str_sv0, "1234", pos);
29096  assert(!found);
29097 
29098  found = scanner.find_eq_str(str_sv0, "", pos);
29099  assert(!found);
29100 
29101  str_sv0.optimize();
29102  }
29103  }
29104 
29105  // test basic remappings functions
29106  {
29108  str_sv0[0] = "1";
29109  str_sv0[1] = "11";
29110  str_sv0[2] = "123";
29111  str_sv0[3] = "021";
29112 
29113  CheckStrSVCompare(str_sv0);
29114 
29117 
29118  {
29120  str_sv0.calc_octet_stat(occ_matrix);
29121 
29122  {
29123  const auto* r0 = occ_matrix.row(0);
29124  const auto* r1 = occ_matrix.row(1);
29125  const auto* r2 = occ_matrix.row(2);
29126  assert(r0[int('1')] == 3);
29127  assert(r0[int('0')] == 1);
29128 
29129  assert(r1[int('1')] == 1);
29130  assert(r1[int('2')] == 2);
29131 
29132  assert(r2[int('2')] == 0);
29133  assert(r2[int('3')] == 1);
29134  assert(r2[int('1')] == 1);
29135  }
29136 
29137  str_sv0.build_octet_remap(remap_matrix1, remap_matrix2, occ_matrix);
29138  {
29139  {
29140  const auto* r1_0 = remap_matrix1.row(0);
29141  const auto* r2_0 = remap_matrix2.row(0);
29142  assert(r1_0[1] == '1');
29143  assert(r1_0[2] == '0');
29144  assert(r1_0[3] == 0);
29145 
29146  assert(r2_0[int('1')] == 1);
29147  assert(r2_0[int('0')] == 2);
29148  assert(r2_0[int('9')] == 0);
29149  }
29150 
29151  {
29152  const auto* r1_1 = remap_matrix1.row(1);
29153  const auto* r2_1 = remap_matrix2.row(1);
29154  assert(r1_1[1] == '2');
29155  assert(r1_1[2] == '1');
29156  assert(r1_1[3] == 0);
29157 
29158  assert(r2_1[int('1')] == 2);
29159  assert(r2_1[int('2')] == 1);
29160  assert(r2_1[int('9')] == 0);
29161  }
29162 
29163  {
29164  const auto* r1_2 = remap_matrix1.row(2);
29165  const auto* r2_2 = remap_matrix2.row(2);
29166  assert(r1_2[1] == '1');
29167  assert(r1_2[2] == '3');
29168  assert(r1_2[3] == 0);
29169 
29170  assert(r2_2[int('3')] == 2);
29171  assert(r2_2[int('1')] == 1);
29172  assert(r2_2[int('0')] == 0);
29173 
29174  }
29175  }
29176  }
29177 
29178  bool res;
29179  int cmp;
29180  char str0[64];
29181  char str1[64];
29182 
29183  res = str_sv0.remap_tosv(&str0[0], 64, "1", remap_matrix2);
29184  assert(res);
29185 
29186  res = str_sv0.remap_fromsv(&str1[0], 64, &str0[0], remap_matrix1);
29187  assert(res);
29188  cmp = str_sv0.compare(0, &str1[0]);
29189  assert(cmp == 0);
29190 
29191  res = str_sv0.remap_tosv(&str0[0], 64, "2", remap_matrix2); // impossible case
29192  assert(!res);
29193 
29194 
29195  res = str_sv0.remap_tosv(&str0[0], 64, "11", remap_matrix2);
29196  assert(res);
29197 
29198  res = str_sv0.remap_fromsv(&str1[0], 64, &str0[0], remap_matrix1);
29199  assert(res);
29200  cmp = str_sv0.compare(1, &str1[0]);
29201  assert(cmp == 0);
29202 
29203  res = str_sv0.remap_tosv(&str0[0], 64, "123", remap_matrix2);
29204  assert(res);
29205 
29206  res = str_sv0.remap_fromsv(&str1[0], 64, &str0[0], remap_matrix1);
29207  assert(res);
29208  cout << str1 << endl;
29209  cmp = str_sv0.compare(2, &str1[0]);
29210  assert(cmp == 0);
29211 
29212 
29213  res = str_sv0.remap_tosv(&str0[0], 64, "133", remap_matrix2); // impossible case
29214  assert(!res);
29215  res = str_sv0.remap_tosv(&str0[0], 64, "1231", remap_matrix2); // impossible case
29216  assert(!res);
29217  }
29218 
29219  // build-vefify remapped sparse-vector
29220  {
29223 
29224  str_sv1.remap_from(str_sv0);
29225  assert(!str_sv1.is_remap());
29226 
29227  str_sv0[0] = "1";
29228  str_sv0[1] = "11";
29229  str_sv0[2] = "123";
29230 
29231  assert(!str_sv1.is_remap());
29232 
29233  str_sv1.remap_from(str_sv0);
29234  str_sv1.recalc_remap_matrix2();
29235 
29236  assert(str_sv1.is_remap());
29237  assert(str_sv1.size() == str_sv0.size());
29238 
29239  char str[256];
29240  int cmp;
29241 
29242  str_sv1.get(0, str, sizeof(str));
29243  cmp = ::strcmp(str, "1");
29244  assert(cmp==0);
29245  cmp = str_sv1.compare(0, "1");
29246  assert(cmp==0);
29247 
29249 
29250  // test remap decoder
29251  {
29252  unsigned d = 0;
29253  char *s;
29254 
29255  d = str_sv1.decode(hmatr, 0, 1);
29256  s = hmatr.row(0);
29257  cmp = ::strcmp(s, "1");
29258  assert(cmp == 0);
29259  assert(d == 1);
29260  }
29261 
29262  str_sv1.get(1, str, sizeof(str));
29263  cmp = ::strcmp(str, "11");
29264  assert(cmp==0);
29265  cmp = str_sv1.compare(1, "11");
29266  assert(cmp==0);
29267 
29268  str_sv1.get(2, str, sizeof(str));
29269  cmp = ::strcmp(str, "123");
29270  assert(cmp==0);
29271 
29272  // test remap decoder
29273  {
29274  unsigned d = 0;
29275  char *s;
29276 
29277  d = str_sv1.decode(hmatr, 2, 1);
29278  s = hmatr.row(0);
29279  cmp = ::strcmp(s, "123");
29280  assert(cmp == 0);
29281  assert(d == 1);
29282  }
29283 
29284  string s;
29285  str_sv1.get(2, s);
29286  cmp = ::strcmp(s.c_str(), "123");
29287  assert(cmp==0);
29288 
29289  {
29290  string s0 = "113";
29291  str_sv1.assign(4, s0);
29292  str_sv1.get(4, s);
29293  assert(s == s0);
29294  cout << s << endl;
29295  cmp = str_sv1.compare(4, "113");
29296  assert(cmp==0);
29297  }
29298 
29299  {
29300  bool equal = str_sv1.equal(str_sv0);
29301  assert(!equal);
29302 
29303  str_sparse_vector<char, bvect, 2> str_sv2(str_sv1);
29304  equal = str_sv1.equal(str_sv2);
29305  assert(equal);
29306  }
29307  {
29308  str_sparse_vector<char, bvect, 2> str_sv2(str_sv0);
29309  str_sv2 = str_sv1;
29310  bool equal = str_sv1.equal(str_sv2);
29311  assert(equal);
29312  }
29313  }
29314 
29315  // scanner search on remapped str vector
29316  {
29319  str_sv0[0] = "1";
29320  str_sv0[1] = "11";
29321  str_sv0[2] = "123";
29322 
29323  str_sv1.remap_from(str_sv0);
29324  CheckStrSVCompare(str_sv0);
29325  CheckStrSVCompare(str_sv1);
29326 
29327  unsigned pos, pos1;
29330  scanner.bind(str_sv1, true);
29331 
29332  for (unsigned pass = 0; pass < 2; ++pass)
29333  {
29334 
29335  bool found = scanner.find_eq_str("1", pos);
29336  assert(found);
29337  assert(pos == 0);
29338  found = scanner1.lower_bound_str(str_sv1,"1", pos1);
29339  assert(found);
29340  assert(pos == pos1);
29341 
29342  found = scanner.find_eq_str("11", pos);
29343  assert(found);
29344  assert(pos == 1);
29345  found = scanner.bfind_eq_str("11", pos);
29346  assert(found);
29347  assert(pos == 1);
29348 
29349  {
29350  pos = 0;
29351  const char test_ch [] = "113";
29352  found = scanner.bfind_eq_str(test_ch, 2, pos);
29353  assert(found);
29354  assert(pos == 1);
29355 
29356  }
29357 
29358  found = scanner1.lower_bound_str(str_sv1, "11", pos1);
29359  assert(found);
29360  assert(pos == pos1);
29361 
29362  found = scanner.find_eq_str("123", pos);
29363  assert(found);
29364  assert(pos == 2);
29365  found = scanner.bfind_eq_str("123", pos);
29366  assert(found);
29367  assert(pos == 2);
29368  found = scanner1.lower_bound_str(str_sv1, "123", pos1);
29369  assert(found);
29370  assert(pos == pos1);
29371 
29372 
29373  found = scanner.find_eq_str("1234", pos);
29374  assert(!found);
29375  found = scanner.bfind_eq_str("1234", pos);
29376  assert(!found);
29377  // commented out because lower_bound throws exceptions on impossible strings
29378  /*
29379  found = scanner1.lower_bound_str(str_sv1,"1234", pos1);
29380  assert(!found);
29381  assert(pos1 == str_sv1.size());
29382  */
29383 
29384  found = scanner.find_eq_str("", pos);
29385  assert(!found);
29386 
29387  str_sv1.optimize();
29388  }
29389  }
29390 
29391  // serialization of remap string vector
29392  {
29396  str_sv0[0] = "1";
29397  str_sv0[1] = "11";
29398  str_sv0[2] = "123";
29399 
29400  str_sv1.remap_from(str_sv0);
29401 
29404  bm::sparse_vector_serialize<str_svect_type>(str_sv1, sv_lay, tb);
29405 
29406  const unsigned char* buf = sv_lay.buf();
29407  int res = bm::sparse_vector_deserialize(str_sv2, buf, tb);
29408  if (res != 0)
29409  {
29410  cerr << "De-Serialization error!" << endl;
29411  exit(1);
29412  }
29413 
29414  bool equal = str_sv1.equal(str_sv2);
29415  assert(equal);
29416  /*
29417  // TODO: cannnot assign extra striungs into remapped marix that easily (it asserts)
29418  str_sv2[3] = "1234";
29419 
29420  char str[256];
29421  str_sv2.get(3, str, sizeof(str));
29422  int cmp = ::strcmp(str, "1234");
29423  assert(cmp==0);
29424  */
29425  }
29426 
29427 
29428  // back insert iterator
29429  //
29430  {
29435  bit = bit3;
29436  }
29437 
29438  {
29440  assert(str_sv0.get_null_support() == bm::use_null);
29441 
29442  str_sv0.resize(10);
29443  {
29444  auto bi = str_sv0.get_back_inserter();
29445  bi = "1";
29446  bi.add_null();
29447  bi = "2";
29448  bi.flush();
29449  }
29450  bool b = str_sv0.is_null(10);
29451  assert(!b);
29452  char str[256];
29453  str_sv0.get(10, str, sizeof(str));
29454  int cmp = strcmp("1", str);
29455  assert(cmp == 0);
29456  b = str_sv0.is_null(11);
29457  assert(b);
29458  b = str_sv0.is_null(12);
29459  assert(!b);
29460  str_sv0.get(12, str, sizeof(str));
29461  cmp = strcmp("2", str);
29462  assert(cmp == 0);
29463  }
29464 
29465  {
29467  {
29468  auto bi = str_sv0.get_back_inserter();
29469  for (unsigned i = 0; i < 100000; i+=2)
29470  {
29471  bi.add_null();
29472  string s(std::to_string(i));
29473  bi = s;
29474  bool b = str_sv0.is_null(i+1);
29475  assert(!b);
29476  }
29477  bi.flush();
29478  }
29479 
29480  char str[256];
29481  for (unsigned i = 0; i < 100000; i+=2)
29482  {
29483  bool b = str_sv0.is_null(i);
29484  assert(b);
29485  b = str_sv0.is_null(i+1);
29486  assert(!b);
29487  str_sv0.get(i+1, str, sizeof(str));
29488  std::string s = std::to_string(i);
29489  int cmp = strcmp(s.c_str(), str);
29490  assert(cmp == 0);
29491  }
29492 
29493  cout << " Testing compare(i, j)..." << endl;
29494  CheckStrSVCompare(str_sv0);
29495  cout << " OK" << endl;
29496 
29497  }
29498 
29499 
29500  {
29502  {
29503  auto bi = str_sv0.get_back_inserter();
29504  for (unsigned i = 0; i < 100000; i+=2)
29505  {
29506  bi = std::to_string(i);
29507  bool b = str_sv0.is_null(i);
29508  assert(!b);
29509  bi.add_null();
29510  }
29511  bi.flush();
29512  }
29513 
29514  char str[256];
29515  for (unsigned i = 0; i < 100000; i+=2)
29516  {
29517  bool b = str_sv0.is_null(i);
29518  assert(!b);
29519  b = str_sv0.is_null(i+1);
29520  assert(b);
29521  str_sv0.get(i, str, sizeof(str));
29522  std::string s = std::to_string(i);
29523  int cmp = strcmp(s.c_str(), str);
29524  assert(cmp == 0);
29525  }
29526  CheckStrSVCompare(str_sv0);
29527 
29528  }
29529 
29530 
29531 
29532  {
29534  auto bi = str_sv0.get_back_inserter();
29535  bi = (const char*)0;
29536  bool b = str_sv0.is_null(0);
29537  assert(b);
29538  bi = (const char*)nullptr;
29539  bi = "123";
29540  bi.add_null();
29541  bi.flush();
29542 
29543 
29544  assert(str_sv0.size() == 4);
29545  b = str_sv0.is_null(0);
29546  assert(b);
29547  assert(str_sv0[0].is_null());
29548  assert(str_sv0.is_null(1));
29549  assert(!str_sv0.is_null(2));
29550 
29551  auto sz = str_sv0.size();
29552  str_sv0.set_null(sz);
29553  assert(sz + 1 == str_sv0.size() );
29554  assert(str_sv0.is_null(sz));
29555 
29556  }
29557 
29558  // test automatic optimization with back_insert iterator
29559  {
29561  {
29562  auto bi = str_sv0.get_back_inserter();
29564  for (unsigned i = 0; i < 65536; ++i)
29565  {
29566  bi = "123";
29567  } // for
29568  }
29569 
29571 
29572  str_sv0.calc_stat(&st);
29573  assert(st.bit_blocks == 0);
29574  assert(st.gap_blocks == 0);
29575 
29576  {
29578  bi = str_sv0.get_back_inserter();
29579  for (unsigned i = 0; i < 65536; ++i)
29580  {
29581  bi = "123";
29582  }
29583  }
29584 
29585  str_sv0.calc_stat(&st);
29586  assert(st.bit_blocks == 0);
29587  assert(st.gap_blocks == 0);
29588  }
29589 
29590 
29591 
29592  // const_iterator / back_inserter
29593  //
29594  {
29596  {
29598  assert(!it2.valid());
29600  assert(!it2c.valid());
29601  }
29602  {
29604  bi = "1";
29605  bi = "11";
29606  bi = "123";
29607 
29608  bi.flush();
29609  }
29610 
29611 
29612  {
29614  assert(!it_end.valid());
29616  assert(it2.valid());
29617  const char* s = it2.value();
29618  int cmp = ::strcmp(s, "1");
29619  assert(cmp == 0);
29620  assert(!it2.is_null());
29621 
29623  assert(it3.valid());
29624  s = it3.value();
29625  cmp = ::strcmp(s, "11");
29626  assert(cmp == 0);
29627 
29629  assert(!it4.valid());
29630  it4.go_to(2);
29631  assert(it4.valid());
29632  s = it4.value();
29633  cmp = ::strcmp(s, "123");
29634  assert(cmp == 0);
29635  }
29636  }
29637 
29638  // substring const-iterator
29639  {
29640  size_t max_str = 0;
29642  {
29644  string str;
29645  string istr;
29646  for (unsigned i = 0; i < 1250000; ++i)
29647  {
29648  str = "rs";
29649  istr = std::to_string(i);
29650  str.append(istr);
29651  bi = str;
29652  if (str.size() > max_str)
29653  max_str = str.size();
29654  }
29655  bi.flush();
29656  }
29657 
29658  auto max_str_size = str_sv0.effective_max_str();
29659 
29660  for (unsigned pass = 0; pass < 2; ++pass)
29661  {
29662  string str;
29663  string istr;
29664  for (size_t i = 0; i < max_str_size; ++i)
29665  {
29667  it1.set_substr(unsigned(i));
29668  for (unsigned v = 0; it1.valid(); ++it1, ++v)
29669  {
29670  str = "rs";
29671  istr = std::to_string(v);
29672  str.append(istr);
29673 
29674  const auto* c = it1.value();
29675  string ss(c);
29676  string ss_c = (i >= str.size()) ? "" : str.substr(i);
29677  assert(ss == ss_c);
29678  } // for it
29679  } // for i
29680  str_sv0.optimize();
29681  } // for pass
29682 
29683 
29684  }
29685 
29686 
29687  cout << "---------------------------- TestStrSparseVector() OK" << endl;
29688 }
29689 
29690 static
29692 {
29693  cout << "------------------------------ TestStrSparseVectorAlgo()" << endl;
29694 
29695  {
29698 
29699  {
29701  bi = "123";
29702  bi = "123";
29703  bi = "123";
29704 
29705  bi.flush();
29706  }
29707  str_sv2 = str_sv1;
29708 
29710  bool f;
29711  f = bm::sparse_vector_find_first_mismatch(str_sv1, str_sv2, pos);
29712  assert(!f);
29713 
29714  str_sv2.push_back("8");
29715  f = bm::sparse_vector_find_first_mismatch(str_sv1, str_sv2, pos);
29716  assert(f);
29717  assert(pos == 3);
29718  f = bm::sparse_vector_find_first_mismatch(str_sv2, str_sv1, pos);
29719  assert(f);
29720  assert(pos == 3);
29721  str_sv1.optimize();
29722  f = bm::sparse_vector_find_first_mismatch(str_sv2, str_sv1, pos);
29723  assert(f);
29724  assert(pos == 3);
29725  str_sv2.optimize();
29726  f = bm::sparse_vector_find_first_mismatch(str_sv2, str_sv1, pos);
29727  assert(f);
29728  assert(pos == 3);
29729  }
29730 
29731  {
29734 
29736  bool f;
29737  f = bm::sparse_vector_find_first_mismatch(str_sv1, str_sv2, pos);
29738  assert(!f);
29739 
29740  str_sv1[1000] = "123";
29741  str_sv1[10000] = "123";
29742  str_sv1[100000] = "123";
29743  str_sv1[1000000] = "123";
29744 
29745  str_sv2 = str_sv1;
29746  f = bm::sparse_vector_find_first_mismatch(str_sv1, str_sv2, pos);
29747  assert(!f);
29748  str_sv1.optimize();
29749  str_sv2.optimize();
29750 
29751  f = bm::sparse_vector_find_first_mismatch(str_sv1, str_sv2, pos);
29752  assert(!f);
29753 
29754  str_sv1[10000000] = "9";
29755  f = bm::sparse_vector_find_first_mismatch(str_sv1, str_sv2, pos);
29756  assert(f);
29757  assert(pos == 10000000);
29758 
29759  str_sv1[0] = "A";
29760  f = bm::sparse_vector_find_first_mismatch(str_sv1, str_sv2, pos);
29761  assert(f);
29762  assert(pos == 0);
29763  }
29764 
29765  cout << "------------------------------ TestStrSparseVectorAlgo() OK" << endl;
29766 }
29767 
29768 static
29770 {
29771  cout << "------------------------------- TestStrSparseVector_FindEq()" << endl;
29772  using bvector_type = bm::bvector<>;
29773  using TSparseStrVector = bm::str_sparse_vector<char, bvector_type, 3>;
29774 
29775  {
29776  TSparseStrVector str_vector;
29777  {
29778  auto in_iter = str_vector.get_back_inserter();
29779 
29780  in_iter = "nssv16159936";
29781  in_iter = "nssv16168081";
29782  in_iter = "nssv16161387";
29783  in_iter = "rs4567789";
29784  in_iter = ".";
29785  in_iter = "nssv16175917";
29786  in_iter = "nssv16177038";
29787  in_iter = "nssv16177460";
29788  in_iter = ".";
29789  in_iter = "nssv16161309";
29790  in_iter.flush();
29791  }
29792 
29793  str_vector.remap();
29794  str_vector.optimize();
29795 
29796  // print them out:
29797  auto it = str_vector.begin();
29798  auto it_end = str_vector.end();
29799  for (; it != it_end; ++it)
29800  {
29801  cout << *it << endl;
29802  }
29803 
29805  TSparseStrVector::bvector_type result;
29806  scanner.find_eq_str(str_vector, ".", result);
29807 
29808 
29809  auto cnt = result.count();
29810  cout << "Number of hits: " << cnt << endl;
29811  assert(cnt == 2);
29812  {
29813  auto en = result.first();
29814  auto en_end = result.end();
29815  for (; en < en_end; ++en)
29816  {
29817  cout << *en << endl;
29818  }
29819  }
29820 
29821  scanner.find_eq_str_prefix(str_vector, "rs", result);
29822  cnt = result.count();
29823  assert(cnt == 1);
29824  {
29825  auto en = result.get_enumerator(0);
29826  assert(*en == 3);
29827  }
29828 
29829  scanner.find_eq_str_prefix(str_vector, "ns", result);
29830  cnt = result.count();
29831  assert(cnt == 7);
29832 
29833  bool f = scanner.find_eq_str_prefix(str_vector, "sn", result);
29834  assert(!f);
29835  cnt = result.count();
29836  assert(cnt == 0);
29837 
29838 
29839  // simple pipeline search
29840  {
29842  pipe.add("nssv16175917");
29843  pipe.add("nssv16175917");
29844  pipe.add("xyz"); // impossible case
29845  pipe.add("rs4567789");
29846 
29847  pipe.complete(); // finish the pipeline construction with this call
29848 
29849  assert(pipe.is_complete());
29850 
29851  scanner.find_eq_str(pipe); // run the search pipeline
29852 
29853  // iterate all the results
29854  //
29855  auto& res_vect = pipe.get_bv_res_vector();
29856  assert(res_vect.size()==4);
29857  for (size_t i = 0; i < res_vect.size(); ++i)
29858  {
29860  assert(bv || i == 2);
29861  if (bv)
29862  {
29863  cnt = bv->count();
29864  assert(cnt == 1);
29865  bm::id_t pos;
29866  bool found = bv->find(pos);
29867  assert(found);
29868  switch (i)
29869  {
29870  case 0:
29871  case 1:
29872  assert(pos == 5);
29873  break;
29874  case 2:
29875  assert(0);
29876  break;
29877  case 3:
29878  assert(pos == 3);
29879  break;
29880  default:
29881  break;
29882  } // switch
29883  }
29884  } // for i
29885 
29886 
29887  }
29888 
29889  }
29890 
29891 
29892  {
29893  TSparseStrVector str_vector(bm::use_null);
29894  TSparseStrVector str_vector1(bm::use_null);
29895  {
29896  auto in_iter = str_vector.get_back_inserter();
29897  in_iter = "nssv16159936";
29898  in_iter = "nssv16168081";
29899  in_iter = "nssv16161387";
29900  in_iter = "rs4567789";
29901  in_iter.add_null();
29902  in_iter = "nssv16175917";
29903  in_iter = "nssv16177038";
29904  in_iter = "nssv16177460";
29905  in_iter.add_null();
29906  in_iter = "nssv16161309";
29907  in_iter.add_null();
29908  in_iter = "";
29909  in_iter.add_null(10000000);
29910  in_iter.flush();
29911  }
29912 
29913  str_vector.remap();
29914  str_vector.optimize();
29915 
29916  assert(!str_vector.is_null(0));
29917  assert(str_vector.is_null(4));
29918  assert(str_vector.is_null(8));
29919 
29920 
29922  std::vector<unsigned char> buf_v;
29923  {
29925  bm::sparse_vector_serialize<TSparseStrVector >(str_vector, sv_lay, tb);
29926 
29927  buf_v.resize(sv_lay.size());
29928  {
29929  const unsigned char* buf = sv_lay.buf();
29930  ::memcpy(buf_v.data(), buf, sv_lay.size());
29931  }
29932  }
29933 
29935  sv_deserial.deserialize(str_vector1, buf_v.data());
29936 
29937 
29938  bvect::size_type pos = 0;
29939  bool b = bm::sparse_vector_find_first_mismatch(str_vector, str_vector1, pos);
29940  assert(!b);
29941  b = bm::sparse_vector_find_first_mismatch(str_vector, str_vector1, pos, bm::no_null);
29942  assert(!b);
29943 
29944  assert(str_vector.is_null(4));
29945  assert(str_vector.is_null(8));
29946 
29947 
29948  }
29949 
29950  {
29951  TSparseStrVector str_vector(bm::use_null);
29952  {
29953  TSparseStrVector str_vector0(bm::use_null);
29954  auto in_iter = str_vector0.get_back_inserter();
29955 
29956  in_iter = "rs123456";
29957  in_iter = "rs23456";
29958  in_iter.add_null();
29959  in_iter = "esv4567";
29960  in_iter = "esv89000";
29961  in_iter.add_null();
29962  in_iter = "esv4444";
29963  in_iter = "rs22222";
29964  in_iter = "rs";
29965 
29966  in_iter.flush();
29967 
29968  str_vector0.remap();
29969  str_vector = std::move(str_vector0);
29970  }
29971 
29973  str_vector.optimize(tb);
29974 
29975  size_t nr_nulls = 0;
29976 
29977  const auto* bv = str_vector.get_null_bvector();
29978  assert(bv);
29979  nr_nulls = bv->count();
29980  nr_nulls = str_vector.size() - nr_nulls;
29981  assert(nr_nulls == 2);
29982  nr_nulls = 0;
29983 
29984  auto it = str_vector.begin();
29985  auto it_end = str_vector.end();
29986  for (; it != it_end; ++it)
29987  {
29988  if (it.is_null()) {
29989  nr_nulls++;
29990  }
29991  }
29992  assert(nr_nulls == 2);
29993  }
29994 
29995  {
29996  TSparseStrVector str_vector(bm::use_null);
29997  {
29998  auto in_iter = str_vector.get_back_inserter();
29999 
30000  in_iter = "rs123456";
30001  in_iter = "rs23456";
30002  in_iter = "es24567";
30003  in_iter = "es189000";
30004  in_iter = "rs12222";
30005  in_iter = "rs1";
30006 
30007  in_iter.flush();
30008 
30009  str_vector.remap();
30010  }
30012  TSparseStrVector::size_type pos;
30013  bool b = scanner.find_eq_str(str_vector, "rs", pos);
30014  assert(!b);
30015  TSparseStrVector::bvector_type bv_res;
30016  bool found = scanner.find_eq_str(str_vector, "rs", bv_res);
30017  assert(!found);
30018  }
30019 
30020 #if 0
30021  {
30022  TSparseStrVector str_vector(bm::use_null);
30023  int res = bm::file_load_svector(str_vector, "/Volumes/WD-MacOS/VCF/ALL.chr1.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz_1_id.bin");
30024  if (res != 0)
30025  {
30026  cerr << "Cannot load the file " << endl;
30027  assert(0);
30028  }
30029  cout << str_vector.size() << endl;
30030 // str_vector[1779648] = "rs57745065";
30031 // str_vector[1779649] = "rs57745065";
30032 
30033  TSparseStrVector str_sv_remap(bm::use_null);
30034  str_sv_remap.remap_from(str_vector);
30035 
30037 /*
30038  {
30039  TSparseStrVector::bvector_type bv_res;
30040  bool found = scanner.find_eq_str(str_vector, "rs5774506", bv_res);
30041  assert(found);
30042  cout << "1. Number of hits: " << bv_res.count() << endl;
30043  }
30044 */
30045  {
30046  TSparseStrVector::size_type pos;
30047  bool b = scanner.find_eq_str(str_sv_remap, "rs5774506", pos);
30048  if (b)
30049  {
30050  cout << str_sv_remap[pos] << endl;
30051  }
30052 
30053  TSparseStrVector::bvector_type bv_res;
30054  bool found = scanner.find_eq_str(str_sv_remap, "rs5774506", bv_res);
30055  assert(found);
30056  cout << "2. Number of hits: " << bv_res.count() << endl;
30057  auto en = bv_res.get_enumerator(0);
30058  for (;en.valid(); ++en)
30059  {
30060  auto idx = *en;
30061  cout << idx << ": " << str_vector[idx] << " remap=>" << str_sv_remap[idx] << endl;
30062  } // for
30063  cout << endl;
30064 
30065  }
30066  }
30067 #endif
30068 
30069 
30070 
30071  cout << "------------------------------- TestStrSparseVector_FindEq()" << endl;
30072 }
30073 
30074 
30075 
30076 static
30078 {
30079  cout << "---------------------------- TestStrSparseVectorSerial()" << endl;
30080 
30081  {
30083  str_sparse_vector<char, bvect, 3> str_sv2, str_sv3;
30084 
30085  {
30087  bi = "1";
30088  bi = "11";
30089  bi = "123";
30090 
30091  bi.flush();
30092  }
30095  bm::sparse_vector_serialize<str_sparse_vector<char, bvect, 3> >(str_sv1, sv_lay, tb);
30096 
30097  std::vector<unsigned char> buf_v;
30098  {
30099  buf_v.resize(sv_lay.size());
30100  const unsigned char* buf = sv_lay.buf();
30101  ::memcpy(buf_v.data(), buf, sv_lay.size());
30102  }
30103 
30104  {
30106  bm::sparse_vector_serialize<str_sparse_vector<char, bvect, 3> >(str_sv1, sv_lay2, tb);
30107  const unsigned char* buf1 = sv_lay.buf();
30108  const unsigned char* buf2 = sv_lay2.buf();
30109  auto sz1 = sv_lay.size();
30110  auto sz2 = sv_lay2.size();
30111  assert(sz1 == sz2);
30112  int cmp = ::memcmp(buf1, buf2, sz1);
30113  assert(cmp == 0);
30114  }
30115 
30116 
30118  bv_mask.set(1);
30119  bv_mask.set(2);
30120  bv_mask.set(100);
30122  sv_deserial.deserialize(str_sv2, buf_v.data(), bv_mask);
30123 
30124  assert(str_sv1.size() == str_sv2.size());
30125  char str[256];
30126  int cmp;
30127 
30128  str_sv2.get(1, str, sizeof(str));
30129  cmp = ::strcmp(str, "11");
30130  assert(cmp==0);
30131  cmp = str_sv2.compare(1, "11");
30132  assert(cmp==0);
30133  cmp = str_sv2.compare(2, "123");
30134  assert(cmp==0);
30135  cmp = str_sv2.compare(0, "");
30136  assert(cmp==0);
30137 
30139  sv_deserial.deserialize(str_sv3, buf_v.data(), bv_mask);
30140  assert(str_sv3.is_ro());
30141  bool eq = str_sv2.equal(str_sv3);
30142  assert(eq);
30143  }
30144 
30145  {
30148 
30149  {
30151  bi = "1";
30152  bi.add_null();
30153  bi = "12";
30154  bi = "12";
30155 
30156  bi.flush();
30157  }
30158 
30161  bm::sparse_vector_serialize<str_sparse_vector<char, bvect, 3> >(str_sv1, sv_lay, tb);
30162 
30163  std::vector<unsigned char> buf_v;
30164  {
30165  buf_v.resize(sv_lay.size());
30166  const unsigned char* buf = sv_lay.buf();
30167  ::memcpy(buf_v.data(), buf, sv_lay.size());
30168  }
30169 
30170  {
30172  bm::sparse_vector_serialize<str_sparse_vector<char, bvect, 3> >(str_sv1, sv_lay2, tb);
30173  const unsigned char* buf1 = sv_lay.buf();
30174  const unsigned char* buf2 = sv_lay2.buf();
30175  auto sz1 = sv_lay.size();
30176  auto sz2 = sv_lay2.size();
30177  assert(sz1 == sz2);
30178  cout << sz1 << endl;
30179  int cmp = ::memcmp(buf1, buf2, sz1);
30180  if (cmp)
30181  {
30182  for (size_t i = 0; i < sz1; ++i)
30183  {
30184  unsigned char ch1 = buf1[i];
30185  unsigned char ch2 = buf2[i];
30186  if (ch1 != ch2)
30187  {
30188  cerr << "Buffer diff at pos=" << i << endl;
30189  exit(1);
30190  }
30191  } // for
30192  }
30193  assert(cmp == 0);
30194  }
30195 
30196 
30198  sv_deserial.deserialize(str_sv2, buf_v.data());
30199 
30200  auto* bv = str_sv2.get_null_bvector();
30201  assert(bv);
30202 
30203  bool b = str_sv1.equal(str_sv2);
30204  assert(b);
30205 
30207  sv_deserial.deserialize(str_sv2, buf_v.data());
30208  assert(str_sv2.is_ro());
30209  bool eq = str_sv1.equal(str_sv2);
30210  assert(eq);
30211  }
30212 
30213 
30214  // corner case from Andrea Asztalos
30215  {
30218 
30220  for (unsigned i = 0; i < 65536;++i)
30221  {
30222  bi = "1";
30223  }
30224  bi.flush();
30225 
30228  bm::sparse_vector_serialize<str_sparse_vector<char, bvect, 3> >(str_sv1, sv_lay, tb);
30229 
30230  {
30232  bm::sparse_vector_serialize<str_sparse_vector<char, bvect, 3> >(str_sv1, sv_lay2, tb);
30233  const unsigned char* buf1 = sv_lay.buf();
30234  const unsigned char* buf2 = sv_lay2.buf();
30235  auto sz1 = sv_lay.size();
30236  auto sz2 = sv_lay2.size();
30237  assert(sz1 == sz2);
30238  int cmp = ::memcmp(buf1, buf2, sz1);
30239  assert(cmp == 0);
30240  }
30241 
30242  std::vector<unsigned char> buf_v;
30243  {
30244  buf_v.resize(sv_lay.size());
30245  const unsigned char* buf = sv_lay.buf();
30246  ::memcpy(buf_v.data(), buf, sv_lay.size());
30247  }
30248 
30250  sv_deserial.deserialize(str_sv2, buf_v.data());
30251 
30252  auto* bv = str_sv2.get_null_bvector();
30253  assert(bv);
30254 
30255  bool b = str_sv1.equal(str_sv2);
30256  assert(b);
30257  }
30258 
30259  // crash report from Andrea Asztalos
30260  {
30261  using bvector_type = bm::bvector<>;
30262  using TSparseStrVector = bm::str_sparse_vector<char, bvector_type, 390>;
30263 
30264  TSparseStrVector vec_A, vec_B;
30265 
30266  auto iter_1 = vec_A.get_back_inserter();
30267 
30268  iter_1 = "rs123456";
30269  iter_1 = "rs23456";
30270  iter_1 = ".";
30271  iter_1 = "esv4567";
30272  iter_1 = "esv89000";
30273  iter_1 = ".";
30274  iter_1 = "esv4444";
30275  iter_1 = "rs22222";
30276  iter_1.flush();
30277 
30278  auto iter_2 = vec_B.get_back_inserter();
30279  iter_2.add_null(10);
30280  iter_2.flush();
30281 
30283  vec_A.remap();
30284  vec_A.optimize(tb);
30285 
30286  vec_B.remap();
30287  vec_B.optimize(tb);
30288 
30290  auto layout_a = make_unique<TLayout>();
30291  auto layout_b = make_unique<TLayout>();
30292 
30294  str_serializer.set_bookmarks(true, 16);
30295  str_serializer.enable_xor_compression();
30296  assert(str_serializer.is_xor_ref());
30297 
30298  str_serializer.serialize(vec_A, *layout_a.get());
30299  str_serializer.serialize(vec_B, *layout_b.get()); //<-- runs into assertion
30300  }
30301 
30302  {
30305 
30307  for (unsigned i = 0; i < 65536;++i)
30308  {
30309  bi = "1";
30310  bi = "1234";
30311  }
30312  bi.flush();
30313 
30314  str_sv1.remap();
30315  {
30316  auto* bv1 = str_sv1.get_null_bvector();
30317  cout << bv1->count() << endl;
30318  }
30319 
30322  bm::sparse_vector_serialize<str_sparse_vector<char, bvect, 3> >(str_sv1, sv_lay, tb);
30323 
30324  std::vector<unsigned char> buf_v_prev;
30325  for (unsigned pass = 0; pass < 10; ++pass)
30326  {
30328  bm::sparse_vector_serialize<str_sparse_vector<char, bvect, 3> >(str_sv1, sv_lay2, tb);
30329  const unsigned char* buf1 = sv_lay.buf();
30330  const unsigned char* buf2 = sv_lay2.buf();
30331  auto sz1 = sv_lay.size();
30332  auto sz2 = sv_lay2.size();
30333  assert(sz1 == sz2);
30334  int cmp = ::memcmp(buf1, buf2, sz1);
30335  assert(cmp == 0);
30336  if (pass)
30337  {
30338  const unsigned char* buf_prev = buf_v_prev.data();
30339  cmp = ::memcmp(buf_prev, buf2, sz1);
30340  assert(cmp == 0);
30341  }
30342  buf_v_prev.resize(sv_lay2.size());
30343  ::memcpy((void*)buf_v_prev.data(), buf2, sv_lay2.size());
30344  }
30345 
30346  std::vector<unsigned char> buf_v;
30347  {
30348  buf_v.resize(sv_lay.size());
30349  const unsigned char* buf = sv_lay.buf();
30350  ::memcpy(buf_v.data(), buf, sv_lay.size());
30351  }
30352 
30354  sv_deserial.deserialize(str_sv2, buf_v.data());
30355 
30356  auto* bv = str_sv2.get_null_bvector();
30357  assert(bv);
30358  bool bbv = bv->test(0);
30359  assert(bbv);
30360  auto* bv1 = str_sv1.get_null_bvector();
30361  assert(bv1);
30362  bool bn = bv->equal(*bv1);
30363  assert(bn);
30364 
30365  bool b = str_sv1.equal(str_sv2);
30366  assert(b);
30367  }
30368 
30369 
30370 
30371 
30372  cout << "Stress deserialization (AND mask) and range[..]" << endl;
30373  {
30379 
30380  {
30382  for (unsigned i = 0; i < 100000; ++i)
30383  {
30384  bi = "ATGC";
30385  bi = "GCTA";
30386  bi = "GCAA";
30387  bi = "TATA";
30388  }
30389  }
30390 
30391  str_sv1.remap_from(str_sv0);
30392  assert(str_sv1.is_remap());
30393  str_sv1.optimize();
30394 
30397  bm::sparse_vector_serialize<str_sparse_vector<char, bvect, 3> >(str_sv1, sv_lay, tb);
30398 
30399  std::vector<unsigned char> buf_v;
30400  {
30401  buf_v.resize(sv_lay.size());
30402  const unsigned char* buf = sv_lay.buf();
30403  ::memcpy(buf_v.data(), buf, sv_lay.size());
30404  }
30405  {
30407  bm::sparse_vector_serialize<str_sparse_vector<char, bvect, 3> >(str_sv1, sv_lay2, tb);
30408  const unsigned char* buf1 = sv_lay.buf();
30409  const unsigned char* buf2 = sv_lay2.buf();
30410  auto sz1 = sv_lay.size();
30411  auto sz2 = sv_lay2.size();
30412  assert(sz1 == sz2);
30413  int cmp = ::memcmp(buf1, buf2, sz1);
30414  assert(cmp == 0);
30415  }
30416 
30420  char s1[256];
30421  char s2[256];
30422  int cmp;
30423 
30424  bvect::size_type from = 100000-1;
30425  bvect::size_type to = from + 65536;
30426 
30427  cout << "\n Single element deserializations... \n";
30428  for (unsigned i = from; i < to; ++i)
30429  {
30430  bvect bv_mask;
30431  bv_mask.set_range(i, i);
30432  sv_deserial.deserialize(str_sv2, buf_v.data(), bv_mask);
30433 
30434  sv_deserial.deserialize_range(str_sv3, buf_v.data(), i, i);
30435 
30436  str_sv2.get(1, s2, sizeof(s2));
30437  assert(s2[0] == 0);
30438 
30439  bool eq = str_sv2.equal(str_sv3);
30440  assert(eq);
30441 
30442  str_r.copy_range(str_sv1, i, i);
30443  eq = str_r.equal(str_sv3);
30444  assert(eq);
30445 
30446  for (unsigned j = i; j < i+1; ++j)
30447  {
30448  str_sv1.get(j, s1, sizeof(s1));
30449  str_sv2.get(j, s2, sizeof(s2));
30450  cmp = ::strcmp(s1, s2);
30451  assert(cmp==0);
30452 
30453  } // for j
30454 
30455  if ((i & 0xFF) == 0)
30456  if (!is_silent)
30457  cout << "\r" << i << "/" << to << flush;
30458 
30459  } // for
30460 
30461  cout << "\n Ranges deserializations... \n";
30462  for (unsigned i = from; i < to; ++i)
30463  {
30464  bvect bv_mask;
30465  bv_mask.set_range(i, to);
30466  sv_deserial.deserialize(str_sv2, buf_v.data(), bv_mask);
30467 
30468  sv_deserial.deserialize_range(str_sv3, buf_v.data(), i, to);
30469 
30470  str_sv2.get(1, s2, sizeof(s2));
30471  assert(s2[0] == 0);
30472 
30473  bool eq = str_sv2.equal(str_sv3);
30474  assert(eq);
30475 
30476  str_r.copy_range(str_sv1, i, to);
30477  eq = str_r.equal(str_sv3);
30478  assert(eq);
30479 
30480  for (unsigned j = i; j < to; ++j)
30481  {
30482  str_sv1.get(j, s1, sizeof(s1));
30483  str_sv2.get(j, s2, sizeof(s2));
30484  cmp = ::strcmp(s1, s2);
30485  assert(cmp==0);
30486 
30487  } // for j
30488 
30489  sv_deserial_ro.deserialize_range(str_sv3, buf_v.data(), i, to);
30490  assert(str_sv3.is_ro());
30491  eq = str_r.equal(str_sv3);
30492  assert(eq);
30493 
30494 
30495 
30496  if ((i & 0xF) == 0)
30497  if (!is_silent)
30498  cout << "\r" << i << "/" << to << flush;
30499 
30500  } // for
30501 
30502  }
30503  cout << "\n ok" << endl;
30504 
30505 
30506  cout << "Stress deserialization (AND mask) (use NULL) and Range[..]" << endl;
30507  {
30508  typedef bm::str_sparse_vector<char, bvect, 3> str_sv_type;
30509 
30515 
30516  {
30518  for (unsigned i = 0; i < 1000000; ++i)
30519  {
30520  bi = "ATGC";
30521  bi = "GCTA";
30522  bi = "GCAA";
30523  bi = "TATA";
30524  bi.add_null();
30525  }
30526  bi.flush();
30527 
30529  for (unsigned i = 0; i < 1000000; ++i)
30530  {
30531  assert(it.valid());
30532  const char* s;
30533  int cmp;
30534 
30535  s = it.value();
30536  cmp = ::strcmp(s, "ATGC");
30537  assert(cmp==0);
30538  ++it;
30539  s = it.value();
30540  cmp = ::strcmp(s, "GCTA");
30541  assert(cmp==0);
30542  ++it;
30543  s = it.value();
30544  cmp = ::strcmp(s, "GCAA");
30545  assert(cmp==0);
30546  ++it;
30547  s = it.value();
30548  cmp = ::strcmp(s, "TATA");
30549  assert(cmp==0);
30550  ++it;
30551  s = it.value();
30552  assert(!s); // NULL value
30553  ++it;
30554 
30555  }
30556 
30557  }
30558 
30559  str_sv1.remap_from(str_sv0);
30560  assert(str_sv1.is_remap());
30561  str_sv1.optimize();
30562 
30564 
30566  sv_serializer.set_bookmarks(true, 6);
30567 
30568  sv_serializer.serialize(str_sv1, sv_lay);
30569 
30570  std::vector<unsigned char> buf_v;
30571  {
30572  buf_v.resize(sv_lay.size());
30573  const unsigned char* buf = sv_lay.buf();
30574  ::memcpy(buf_v.data(), buf, sv_lay.size());
30575  }
30576 
30578 
30581 
30582  char s1[256];
30583  char s2[256];
30584  char s3[256];
30585  int cmp;
30586 
30587  bvect::size_type from = 100000-1;
30588  bvect::size_type to = from + 65536;
30589  for (auto i = from; i < to; ++i)
30590  {
30591  bvect bv_mask;
30592  bv_mask.set_range(i, to);
30593  sv_deserial.deserialize(str_sv2, buf_v.data(), bv_mask);
30594 
30595  sv_deserial.deserialize_range(str_sv3, buf_v.data(), i, to);
30596 
30597  // check empty
30598  str_sv2.get(1, s2, sizeof(s2));
30599  assert(s2[0] == 0);
30600  str_sv3.get(1, s3, sizeof(s2));
30601  assert(s3[0] == 0);
30602 
30603  bool eq = str_sv2.equal(str_sv3);
30604  assert(eq);
30605 
30606  str_r.copy_range(str_sv1, i, to);
30607  eq = str_r.equal(str_sv3);
30608  assert(eq);
30609 
30610  // run detailed check
30611  for (auto j = i; j < to; ++j)
30612  {
30613  str_sv1.get(j, s1, sizeof(s1));
30614  str_sv2.get(j, s2, sizeof(s2));
30615  cmp = ::strcmp(s1, s2);
30616  assert(cmp==0);
30617  assert(str_sv1.is_null(j) == str_sv2.is_null(j));
30618  } // for j
30619 
30620  sv_deserial_ro.deserialize_range(str_sv3, buf_v.data(), i, to);
30621  assert(str_sv3.is_ro());
30622  eq = str_r.equal(str_sv3);
30623  assert(eq);
30624 
30625  if ((i & 0xF) == 0)
30626  if (!is_silent)
30627  cout << "\r" << i << "/" << to << flush;
30628 
30629  } // for
30630 
30631  }
30632  cout << " ok" << endl;
30633 
30634 
30635 
30636 
30637  cout << "Test data-frame XOR compression" << endl;
30638  {
30639  typedef str_sparse_vector<char, bvect, 3> str_sv_type;
30640 
30643 
30644  str_sv_type sv1i, sv2i, sv3i(bm::use_null);
30645  str_sv_type sv1o, sv2o, sv3o(bm::use_null);
30646 
30647  bm::sparse_vector_serial_layout<str_sv_type> sv_lay1, sv_lay2, sv_lay3;
30648 
30649  for (unsigned i = 0; i < 65536; i+=2)
30650  {
30651  sv1i[i] = "4";
30652  sv2i[i] = "8";
30653  sv3i[i] = "";
30654  }
30655 
30657  // add references in reverse(!) order
30658  bv_ref.add_vectors(sv3i.get_bmatrix());
30659  bv_ref.add_vectors(sv2i.get_bmatrix());
30660  bv_ref.add_vectors(sv1i.get_bmatrix());
30661 
30663  xor_sim_params xs_params;
30664  //sv_serializer.compute_sim_model(sim_model, bv_ref, xs_params);
30665 
30666  // parallel sim-model compute
30667  {
30670  bm::xor_sim_params xor_search_params;
30671 
30672  pbuilder.build_plan(tbatch, sim_model,
30673  bv_ref, xor_search_params);
30674 
30675  typedef
30677  pool_type tpool; // our thread pool here (no threads created yet)
30678  tpool.start(4); // start the threads
30679  {
30681  exec.run(tpool, tbatch, true);
30682  }
30683  tpool.set_stop_mode(pool_type::stop_when_done);
30684  tpool.join();
30685  {
30687  sv_serializer.compute_sim_model(sim_model_c, bv_ref, xs_params);
30688  Check_SimModel(sim_model_c, sim_model);
30689  }
30690  }
30691 
30692  sv_serializer.set_xor_ref(&bv_ref);
30693  sv_serializer.set_sim_model(&sim_model);
30694 
30695  assert(sv_serializer.is_xor_ref());
30696 
30697  sv_serializer.serialize(sv1i, sv_lay1);
30698  {
30699  const bvect::size_type* cstat = sv_serializer.get_bv_serializer().get_compression_stat();
30700  //assert(cstat[bm::set_block_ref_eq]);
30701 // assert(cstat[bm::set_block_xor_ref32] >= 1);
30702  assert(cstat[bm::set_block_ref_eq]>=1 || cstat[bm::set_block_xor_ref32] >= 1);
30703 
30704  }
30705  sv_serializer.serialize(sv2i, sv_lay2);
30706  {
30707  const bvect::size_type* cstat = sv_serializer.get_bv_serializer().get_compression_stat();
30708  //assert(cstat[bm::set_block_ref_eq]>=1);
30709 // assert(cstat[bm::set_block_xor_ref32] >= 1);
30710  assert(cstat[bm::set_block_ref_eq]>=1 || cstat[bm::set_block_xor_ref32] >= 1);
30711  }
30712  sv_serializer.serialize(sv3i, sv_lay3);
30713  {
30714  const bvect::size_type* cstat = sv_serializer.get_bv_serializer().get_compression_stat();
30715  //assert(cstat[bm::set_block_ref_eq]>=1);
30716  assert(cstat[bm::set_block_ref_eq]>=1 || cstat[bm::set_block_xor_ref32] >= 1);
30717 // assert(cstat[bm::set_block_xor_ref32] >= 1);
30718  }
30719 
30720  // ----------
30721 
30722 
30724 
30725  const unsigned char* buf = sv_lay1.buf();
30726  auto sz2 = sv_lay1.size();
30727  (void)sz2;
30728 
30729  bool same_struct;
30730  sv_deserial.deserialize_structure(sv1o, sv_lay1.buf());
30731  same_struct = sv1o.get_bmatrix().is_same_structure(sv1i.get_bmatrix());
30732  assert(same_struct);
30733 
30734  sv_deserial.deserialize_structure(sv2o, sv_lay2.buf());
30735  same_struct = sv2o.get_bmatrix().is_same_structure(sv2i.get_bmatrix());
30736  assert(same_struct);
30737 
30738  {
30739  auto buff = sv_lay3.buf();
30740  sv_deserial.deserialize_structure(sv3o, buff);
30741  }
30742  same_struct = sv3o.get_bmatrix().is_same_structure(sv3i.get_bmatrix());
30743  assert(same_struct);
30744 
30745  bv_ref_d.add_vectors(sv3o.get_bmatrix());
30746  bv_ref_d.add_vectors(sv2o.get_bmatrix());
30747  bv_ref_d.add_vectors(sv1o.get_bmatrix());
30748 
30749  sv_deserial.set_xor_ref(&bv_ref_d);
30750 
30751  sv_deserial.deserialize(sv1o, buf, false);
30752  bool eq = sv1i.equal(sv1o);
30753  assert(eq);
30754 
30755  buf = sv_lay2.buf();
30756  sz2 = sv_lay2.size();
30757 
30758  sv_deserial.deserialize(sv2o, buf, false);
30759  eq = sv2i.equal(sv2o);
30760  assert(eq);
30761 
30762  buf = sv_lay3.buf();
30763  sz2 = sv_lay3.size();
30764 
30765  sv_deserial.deserialize(sv3o, buf, false);
30766  eq = sv3i.equal(sv3o);
30767  assert(eq);
30768 
30769 
30770  sv_deserial.set_xor_ref(0); // unset
30771  }
30772  cout << "Test data-frame XOR compression - OK" << endl;
30773 
30774  // -------------------------------------------------
30775 
30776 
30777  cout << "---------------------------- TestStrSparseVectorSerial() OK" << endl;
30778 }
30779 
30780 
30782 
30783 static
30785  const vector<string>& str_coll)
30786 {
30787  assert(str_sv.size() == str_coll.size());
30788 
30789 
30790  string str_h = "z";
30791  string str_l = "A";
30792 
30794 
30795  str_svect_type::const_iterator it = str_sv.begin();
30796  string str;
30797  for (unsigned i = 0; i < str_sv.size(); ++i, ++it)
30798  {
30799  assert (it.valid());
30800  assert (it != str_sv.end());
30801 
30802  str_sv.get(i, str);
30803  const string& str_control = str_coll[i];
30804  if (str != str_control)
30805  {
30806  std::cerr << "String mis-match at:" << i << std::endl;
30807  exit(1);
30808  }
30809  {
30810  const char* s = *it;
30811  int cmp = ::strcmp(s, str_control.c_str());
30812  if (cmp != 0)
30813  {
30814  cerr << "Iterator comparison failed! " << s << " != " << str_control
30815  << endl;
30816  exit(1);
30817  }
30819  assert(it == it2);
30820  s = *it2;
30821  cmp = ::strcmp(s, str_control.c_str());
30822  if (cmp != 0)
30823  {
30824  cerr << "2. Iterator comparison failed! " << s << " != " << str_control
30825  << endl;
30826  exit(1);
30827  }
30828  }
30829  int cmp = str_sv.compare(i, str_control.c_str());
30830  if (cmp != 0)
30831  {
30832  std::cerr << "String comparison failure at:" << i << std::endl;
30833  exit(1);
30834  }
30835  if (!str_sv.is_remap()) // re-mapped vectors can give incorrect compare
30836  {
30837  cmp = str_sv.compare(i, str_h.c_str());
30838  if (cmp < 0)
30839  {
30840  assert(str < str_h);
30841  }
30842  if (cmp > 0)
30843  {
30844  assert(str > str_h);
30845  }
30846 
30847  cmp = str_sv.compare(i, str_l.c_str());
30848  if (cmp < 0)
30849  {
30850  assert(str < str_l);
30851  }
30852  if (cmp > 0)
30853  {
30854  assert(str > str_l);
30855  }
30856  }
30857 
30858  unsigned pos;
30859  bool found = scanner.find_eq_str(str_sv, str_control.c_str(), pos);
30860  if (!found)
30861  {
30862  cerr << "Scanner search failed! " << str_control << endl;
30863  exit(1);
30864  }
30865  assert(pos == i);
30866  {
30867  bvect bv_result;
30868  found = scanner.find_eq_str(str_sv, str_control.c_str(), bv_result);
30869  if (!found)
30870  {
30871  cerr << "Scanner bvector search failed! " << str_control << endl;
30872  exit(1);
30873  }
30874  auto c = bv_result.count();
30875  assert(c);
30876  found = bv_result.find(pos);
30877  assert(found);
30878  assert(pos == i);
30879  }
30880 
30881  if (i % 100000 == 0)
30882  {
30883  if (!is_silent)
30884  cout << "\r" << i << " / " << str_sv.size() << flush;
30885  }
30886 
30887  } // for
30888  cout << endl;
30889 }
30890 
30891 static
30892 void GenerateTestStrCollection(std::vector<string>& str_coll, unsigned max_coll)
30893 {
30894  string prefix = "az";
30895  string str;
30896  for (unsigned i = 0; i < max_coll; ++i)
30897  {
30898  str = prefix;
30899  str.append(to_string(i));
30900  str_coll.emplace_back(str);
30901 
30902  if (i % 1024 == 0) // generate new prefix
30903  {
30904  prefix.clear();
30905  unsigned prefix_len = (unsigned)rand() % 5;
30906  for (unsigned j = 0; j < prefix_len; ++j)
30907  {
30908  char cch = char('a' + (unsigned)rand() % 26);
30909  prefix.push_back(cch);
30910  } // for j
30911  }
30912  } // for i
30913 }
30914 
30915 static
30917 {
30918  std::string s_next, s_curr;
30919  while (str_sv.size())
30920  {
30921  unsigned idx = str_sv.size() / 2;
30922  unsigned sz = str_sv.size();
30923 
30924  if (idx+1 < sz)
30925  {
30926  str_sv.get(idx+1, s_next);
30927  }
30928  str_sv.erase(idx);
30929  assert(str_sv.size() == sz-1);
30930  if (idx+1 < sz)
30931  {
30932  str_sv.get(idx, s_curr);
30933  assert(s_next == s_curr);
30934  }
30935 
30936  }
30937 }
30938 
30939 template<typename SV>
30940 void EraseSVCollection(SV& sv)
30941 {
30942  typename SV::value_type v_next, v_curr;
30943  v_next = 0;
30944  while (sv.size())
30945  {
30946  auto idx = sv.size() / 2;
30947  auto sz = sv.size();
30948 
30949  if (idx+1 < sz)
30950  {
30951  v_next = sv.get(idx+1);
30952  }
30953  sv.erase(idx);
30954  assert(sv.size() == sz-1);
30955  if (idx+1 < sz)
30956  {
30957  v_curr = sv.get(idx);
30958  assert(v_next == v_curr);
30959  }
30960  }
30961 }
30962 
30963 
30964 static
30966 {
30967  cout << "---------------------------- Bit-plane STR sparse vector stress test" << endl;
30968 
30969  const unsigned max_coll = 3000000;
30970  std::vector<string> str_coll;
30971  str_svect_type str_sv;
30972 
30973  GenerateTestStrCollection(str_coll, max_coll);
30974 
30975  cout << "Loading test sparse vector..." << endl;
30976  {
30978  for (auto str : str_coll)
30979  bi = str;
30980  bi.flush();
30981  }
30982 
30983  cout << " Testing compare(i, j)..." << endl;
30984  CheckStrSVCompare(str_sv, max_coll / 100);
30985  cout << " OK" << endl;
30986 
30987 
30988  // -----------------------------------------------------------
30989  // create sorted collections
30990  cout << "Sorting str sparse vectors..." << endl;
30991  vector<string> str_coll_sorted(str_coll);
30992  str_svect_type str_sv_sorted;
30993 
30994  std::sort(str_coll_sorted.begin(), str_coll_sorted.end());
30995  string str_prev;
30996  for (const string& s : str_coll_sorted)
30997  {
30998  if (s != str_prev)
30999  str_sv_sorted.push_back(s);
31000  str_prev = s;
31001  }
31002 
31003  cout << "Build re-mapped vector..." << endl;
31004  str_svect_type str_sv_remap;
31005  str_sv_remap.remap_from(str_sv_sorted);
31006  cout << "Build re-mapped vector... OK" << endl;
31007 
31008 
31009  // -----------------------------------------------------------
31010 
31011  //print_svector_stat(str_sv);
31012  cout << "Memory optimization" << endl;
31013  str_sv.optimize();
31014  str_sv_remap.optimize();
31015 
31016  cout << "ok. \n Verification..." << endl;
31017 
31018  CompareStrSparseVector(str_sv, str_coll);
31019 
31020  //print_svector_stat(cout,str_sv, true);
31021 
31022  cout << "ok. \n Verification..." << endl;
31023 
31024  CompareStrSparseVector(str_sv, str_coll);
31025 
31026  cout << "ok. \n Verification of remap vector..." << endl;
31027 
31028  CompareStrSparseVector(str_sv_remap, str_coll_sorted);
31029 
31030 
31031 
31032  // serialization check
31033  //
31034  cout << "Validate serialization of str-sparse vector..." << endl;
31035  {
31038  bm::sparse_vector_serialize<str_svect_type>(str_sv, sv_lay, tb);
31039 
31041  const unsigned char* buf = sv_lay.buf();
31042  int res = bm::sparse_vector_deserialize(str_sv2, buf, tb);
31043  if (res != 0)
31044  {
31045  cerr << "De-Serialization error" << endl;
31046  exit(1);
31047  }
31048  CompareStrSparseVector(str_sv2, str_coll);
31049  bool equal = str_sv.equal(str_sv2);
31050  assert(equal);
31051  }
31052  cout << "Validate serialization of str-sparse vector... OK" << endl;
31053 
31054  cout << "Validate serialization of REMAP str-sparse vector..." << endl;
31055  {
31058  bm::sparse_vector_serialize<str_svect_type>(str_sv_remap, sv_lay, tb);
31059 
31061  const unsigned char* buf = sv_lay.buf();
31062  int res = bm::sparse_vector_deserialize(str_sv2, buf, tb);
31063  if (res != 0)
31064  {
31065  cerr << "De-Serialization error" << endl;
31066  exit(1);
31067  }
31068  CompareStrSparseVector(str_sv2, str_coll_sorted);
31069  bool equal = str_sv_remap.equal(str_sv2);
31070  assert(equal);
31071  }
31072  cout << "Validate serialization of REMAP str-sparse vector...OK" << endl;
31073 
31074  // ----------------------------------------------
31075 
31076  cout << "Test common prefix..." << endl;
31077  {
31078  const unsigned str_size = 64;
31079  char str1[str_size];
31080  char str2[str_size];
31081 
31082  unsigned test_size = unsigned(str_coll_sorted.size());
31083  if (test_size > 20000)
31084  test_size = 20000;
31085 
31086  for (unsigned i = 0; i < test_size; ++i)
31087  {
31088  str_sv_sorted.get(i, &str1[0], str_size);
31089  for (unsigned j = 0; j < test_size; ++j)
31090  {
31091  str_sv_sorted.get(j, &str2[0], str_size);
31092  unsigned octet_idx = 0;
31093  for (;true; ++octet_idx)
31094  {
31095  if (str1[octet_idx] != str2[octet_idx])
31096  break;
31097  if (!str1[octet_idx] || !str2[octet_idx])
31098  break;
31099  }
31100  if (octet_idx)
31101  {
31102  for (unsigned i0 = 0; i0 < octet_idx; ++i0)
31103  {
31104  assert(str1[i0] == str2[i0]);
31105  }
31106  }
31107  unsigned common_prefix = str_sv_sorted.common_prefix_length(i, j);
31108  if (common_prefix != octet_idx)
31109  {
31110  cerr << "Common prefix length mismatch!" <<
31111  common_prefix << " != " << octet_idx <<
31112  " [" << str1 << "]-[" << str2 << "]" << endl;
31113  common_prefix = str_sv_sorted.common_prefix_length(i, j);
31114  assert(common_prefix == octet_idx);
31115  exit(1);
31116  }
31117  } // for j
31118 
31119  if (i % 512 == 0)
31120  {
31121  if (!is_silent)
31122  cout << "\r" << i << " / " << test_size << flush;
31123  }
31124  } // for i
31125 
31126  }
31127  cout << "\nTest common prefix...ok." << endl;
31128 
31129 
31130  // ----------------------------------------------
31131 
31132  cout << "Test sorted search..." << endl;
31133 
31134  for (unsigned k = 0; k < 2; ++k)
31135  {
31138  scanner4.bind(str_sv_remap, true); // bind sorted vector
31139 
31141  scanner8.bind(str_sv_remap, true); // bind sorted vector
31142 
31144  scanner16.bind(str_sv_remap, true); // bind sorted vector
31145 
31147  scanner32.bind(str_sv_remap, true); // bind sorted vector
31148 
31150  scanner64.bind(str_sv_remap, true); // bind sorted vector
31151 
31152 
31153  for (unsigned i = 0; i < unsigned(str_coll_sorted.size()); ++i)
31154  {
31155  const string& s = str_coll_sorted[i];
31156  unsigned pos1, pos2, pos3, pos4;
31157 
31158  // validate the compare function
31159  if (i)
31160  {
31161  int res0 = str_sv_remap.compare(0, s.c_str());
31162  int res1 = str_sv_sorted.compare(0, s.c_str());
31163  assert(res0 == res1 && res1 < 0);
31164  res0 = str_sv_remap.compare(i-1, s.c_str());
31165  res1 = str_sv_sorted.compare(i-1, s.c_str());
31166  assert(res0 == res1 && res1 < 0);
31167 
31168  if ( i+1 < unsigned(str_coll_sorted.size()))
31169  {
31170  res0 = str_sv_remap.compare(i+1, s.c_str());
31171  res1 = str_sv_sorted.compare(i+1, s.c_str());
31172  assert(res0 == res1 && res1 > 0);
31173  }
31174  }
31175 
31176  bool found1 = scanner.find_eq_str(str_sv_sorted, s.c_str(), pos1);
31177  if (!found1)
31178  {
31179  cerr << "Sorted scan failed at: " << i << " " << s << endl;
31180  found1 = scanner.find_eq_str(str_sv_sorted, s.c_str(), pos1);
31181  assert(0); exit(1);
31182  }
31183  if (pos1 != i)
31184  {
31185  cerr << "Sorted scan position failed at: " << i << "!=" << pos1
31186  << " " << s << endl;
31187  assert(0);exit(1);
31188  }
31189 
31190  bool found2 = scanner.bfind_eq_str(str_sv_sorted, s.c_str(), pos2);
31191  if (!found2)
31192  {
31193  cerr << "Error! Sorted binary search failed at: " << i << " value='" << s << "'" << endl;
31194  //cerr << "Dump file test.sv created." << endl;
31195  //file_save_svector(str_sv_sorted, "test.sv");
31196  assert(0);exit(1);
31197  }
31198  if (pos2 != i)
31199  {
31200  cerr << "Error! Sorted binary search position mismatch at: " << i << "!=" << pos2
31201  << " " << s << endl;
31202  assert(0);exit(1);
31203  }
31204 
31205 
31206  bool found4 = scanner.lower_bound_str(str_sv_sorted, s.c_str(), pos4);
31207  assert(found4);
31208  assert(pos2 == pos4);
31209 
31210 
31211  bool found3 = scanner4.bfind_eq_str(s.c_str(), pos3);
31212  if (!found3)
31213  {
31214  cerr << "Error! Sorted-remap binary search failed at: " << i << " value='" << s << "'" << endl;
31215  assert(0);exit(1);
31216  }
31217  if (pos3 != i)
31218  {
31219  cerr << "Error! Sorted-remap binary search position mismatch at: " << i << "!=" << pos2
31220  << " value='" << s << "'" << endl;
31221  assert(0);exit(1);
31222  }
31223 
31224  // -----------------------------------------------
31225  {
31226  unsigned pos_x;
31227  bool found_x = scanner8.bfind_eq_str(s.c_str(), pos_x);
31228  if (!found_x)
31229  {
31230  cerr << "Error! Sorted-remap binary search 8 failed at: " << i << " value='" << s << "'" << endl;
31231  assert(0);exit(1);
31232  }
31233  if (pos_x != i)
31234  {
31235  cerr << "Error! Sorted-remap binary search 8 position mismatch at: " << i << "!=" << pos2
31236  << " value='" << s << "'" << endl;
31237  assert(0);exit(1);
31238  }
31239  }
31240  // -----------------------------------------------
31241  {
31242  unsigned pos_x;
31243  bool found_x = scanner16.bfind_eq_str(s.c_str(), pos_x);
31244  if (!found_x)
31245  {
31246  cerr << "Error! Sorted-remap binary search 16 failed at: " << i << " value='" << s << "'" << endl;
31247  assert(0);exit(1);
31248  }
31249  if (pos_x != i)
31250  {
31251  cerr << "Error! Sorted-remap binary search 16 position mismatch at: " << i << "!=" << pos2
31252  << " value='" << s << "'" << endl;
31253  assert(0);exit(1);
31254  }
31255  }
31256  // -----------------------------------------------
31257  {
31258  unsigned pos_x;
31259  bool found_x = scanner32.bfind_eq_str(s.c_str(), pos_x);
31260  if (!found_x)
31261  {
31262  cerr << "Error! Sorted-remap binary search 32 failed at: " << i << " value='" << s << "'" << endl;
31263  assert(0);exit(1);
31264  }
31265  if (pos_x != i)
31266  {
31267  cerr << "Error! Sorted-remap binary search 32 position mismatch at: " << i << "!=" << pos2
31268  << " value='" << s << "'" << endl;
31269  assert(0);exit(1);
31270  }
31271  }
31272  // -----------------------------------------------
31273  {
31274  unsigned pos_x;
31275  bool found_x = scanner64.bfind_eq_str(s.c_str(), pos_x);
31276  if (!found_x)
31277  {
31278  cerr << "Error! Sorted-remap binary search 64 failed at: " << i << " value='" << s << "'" << endl;
31279  assert(0);exit(1);
31280  }
31281  if (pos_x != i)
31282  {
31283  cerr << "Error! Sorted-remap binary search 64 position mismatch at: " << i << "!=" << pos2
31284  << " value='" << s << "'" << endl;
31285  assert(0);exit(1);
31286  }
31287  }
31288  // ---------------------------------------------------
31289 
31290  {
31291  bvect bv_result;
31292  bool found5 = scanner.find_eq_str(str_sv_sorted, s.c_str(), bv_result);
31293  if (!found5)
31294  {
31295  cerr << "Error! Scanner bvector search failed! at: " << i << endl;
31296  exit(1);
31297  auto c = bv_result.count();
31298  assert(c);
31299  unsigned pos5;
31300  found5 = bv_result.find(pos5);
31301  assert(found5);
31302  assert(pos5 == i);
31303  }
31304  }
31305  {
31306  bvect bv_result;
31307  bool found6 = scanner4.find_eq_str(s.c_str(), bv_result);
31308  if (!found6)
31309  {
31310  cerr << "Error! Scanner bvector search failed! at: " << i << endl;
31311  exit(1);
31312  auto c = bv_result.count();
31313  assert(c);
31314  unsigned pos6;
31315  found6 = bv_result.find(pos6);
31316  assert(found6);
31317  assert(pos6 == i);
31318  }
31319  }
31320 
31321  if (i % 65535 == 0)
31322  {
31323  if (!is_silent)
31324  cout << "\r" << (str_sv_sorted.size()-i) << flush;
31325  }
31326 
31327  } // for
31328 
31329  str_sv_sorted.optimize();
31330  cout << "\nPass 2." << endl;
31331  } // for k
31332 
31333  cout << "\nTest sorted search...OK" << endl;
31334 
31335  EraseStrCollection(str_sv_sorted);
31336  EraseStrCollection(str_sv_remap);
31337 
31338  cout << "---------------------------- Bit-plane STR sparse vector stress test OK" << endl;
31339  cout << endl;
31340 }
31341 
31342 inline
31343 void GeneratePipelineTestData(std::vector<string>& str_coll,
31344  str_svect_type& str_sv,
31345  unsigned max_coll = 8000000,
31346  unsigned repeat_factor=10)
31347 {
31348  auto bi(str_sv.get_back_inserter());
31349  string str;
31350  for (unsigned i = 10; i < max_coll; i+= (rand()&0xF))
31351  {
31352  switch (i & 0xF)
31353  {
31354  case 0: str = "AB"; break;
31355  case 1: str = "GTx"; break;
31356  case 2: str = "cnv"; break;
31357  default: str = "AbY11"; break;
31358  }
31359  str.append(to_string(i));
31360 
31361  for (unsigned k = 0; k < repeat_factor; ++k)
31362  {
31363  str_coll.emplace_back(str);
31364  bi = str;
31365  }
31366  } // for i
31367  bi.flush();
31368 }
31369 
31370 
31371 static
31373 {
31374  cout << "---------------------------- TestSparseFindEqStrPipeline()" << endl;
31375  const unsigned max_coll = 8000000;
31376  std::vector<string> str_coll;
31377  str_svect_type str_sv;
31378 
31379 
31380  {
31381  str_svect_type str_sv1(bm::use_null);
31382  str_sv1.push_back("str1");
31383  str_sv1.push_back("str2");
31384 
31386  {
31387  typedef bm::agg_run_options<true, false, true> scanner_custom_mask_opt;
31388  bm::sparse_vector_scanner<str_svect_type>::pipeline<scanner_custom_mask_opt> pipe(str_sv1);
31389  bvect bv_mask { 1 };
31390  pipe.set_search_mask(&bv_mask);
31391 
31392  pipe.add("str1"); // not found because of the mask
31393  pipe.add("z2"); // not found
31394  pipe.add("str2");
31395  pipe.complete(); // finish the pipeline construction with this call
31396 
31397  scanner.find_eq_str(pipe); // run the search pipeline
31398 
31399  auto& res_vec = pipe.get_bv_res_vector();
31400  assert(res_vec.size()==3);
31401 
31402  const bvect* bv0 = res_vec[0];
31403  assert(!bv0);
31404  const bvect* bv1 = res_vec[1];
31405  assert(!bv1);
31406  const bvect* bv2 = res_vec[2];
31407  assert(bv2);
31408  assert(bv2->count()==1);
31409  assert(bv2->test(1));
31410  }
31411 
31412  for (int pass = 0; pass < 2; ++pass)
31413  {
31415 
31416  pipe.add("str1");
31417  pipe.add("z2"); // not found
31418  pipe.add("str2");
31419 
31420  pipe.complete(); // finish the pipeline construction with this call
31421 
31422  scanner.find_eq_str(pipe); // run the search pipeline
31423 
31424  auto& res_vec = pipe.get_bv_res_vector();
31425  assert(res_vec.size()==3);
31426 
31427  const bvect* bv0 = res_vec[0];
31428  assert(bv0);
31429  assert(bv0->count()==1);
31430  assert(bv0->test(0));
31431 
31432  {
31434  scanner.find_eq_str(str_sv1, "str1", bv_res);
31435  bool eq = bv0->equal(bv_res);
31436  assert(eq);
31437  }
31438 
31439  const bvect* bv1 = res_vec[1];
31440  assert(!bv1);
31441  const bvect* bv2 = res_vec[2];
31442  assert(bv2);
31443  assert(bv2->count()==1);
31444  assert(bv2->test(1));
31445 
31446 
31447  str_sv1.optimize();
31448  }
31449 
31450  }
31451 
31452  cout << " generate test set..." << flush;
31453 
31454  GeneratePipelineTestData(str_coll, str_sv, max_coll, 10);
31455 
31456  cout << "remap..." << flush;
31457 
31458  str_sv.remap();
31459  str_sv.optimize();
31460 
31461  cout << "OK" << endl;
31462 
31463  //bm::print_svector_stat(cout,str_sv);
31464 
31465  unsigned test_runs = 10000;
31466  std::vector<string> str_test_coll;
31467  for (bvect::size_type i = 0; i < test_runs; ++i)
31468  {
31469  bvect::size_type idx = (unsigned) rand() % test_runs;
31470  if (idx >= test_runs)
31471  idx = test_runs/2;
31472  str_test_coll.push_back(str_coll[idx]);
31473  }
31474  assert(str_test_coll.size() == test_runs);
31475 
31476  std::this_thread::sleep_for (std::chrono::seconds(4));
31477 
31478 
31479  std::vector<unique_ptr<bvect> > res_vec1;
31481 
31482  {
31483  std::chrono::time_point<std::chrono::steady_clock> s;
31484  std::chrono::time_point<std::chrono::steady_clock> f;
31485  s = std::chrono::steady_clock::now();
31486 
31487  for (bvect::size_type i = 0; i < test_runs; ++i)
31488  {
31489  const string& str = str_test_coll[i];
31490 
31491  str_svect_type::bvector_type* bv_res(new bvect);
31492  scanner.find_eq_str(str_sv, str.c_str(), *bv_res);
31493  res_vec1.emplace_back(unique_ptr<bvect>(bv_res));
31494  } // for
31495  f = std::chrono::steady_clock::now();
31496  auto diff = f - s;
31497  auto d = std::chrono::duration <double, std::milli> (diff).count();
31498 
31499  cout << "scanner::find_eq_str() " << d << "ms" << endl;
31500  }
31501 
31503  {
31504  std::chrono::time_point<std::chrono::steady_clock> s;
31505  std::chrono::time_point<std::chrono::steady_clock> f;
31506  s = std::chrono::steady_clock::now();
31507 
31508  for (bvect::size_type i = 0; i < test_runs; ++i)
31509  {
31510  const string& str = str_test_coll[i];
31511  pipe.add(str.c_str());
31512  }
31513  pipe.complete(); // finish the pipeline construction with this call
31514 
31515  scanner.find_eq_str(pipe); // run the search pipeline
31516 
31517  f = std::chrono::steady_clock::now();
31518  auto diff = f - s;
31519  auto d = std::chrono::duration <double, std::milli> (diff).count();
31520  cout << "scanner::pipeline: " << d << "ms" << endl;
31521  }
31522 
31524  {
31525  std::chrono::time_point<std::chrono::steady_clock> s;
31526  std::chrono::time_point<std::chrono::steady_clock> f;
31527  s = std::chrono::steady_clock::now();
31528 
31529  for (bvect::size_type i = 0; i < test_runs; ++i)
31530  {
31531  const string& str = str_test_coll[i];
31532  pipe2.add(str.c_str());
31533  }
31534  pipe2.complete(); // finish the pipeline construction with this call
31535 
31536  scanner.find_eq_str(pipe2); // run the search pipeline
31537 
31538  f = std::chrono::steady_clock::now();
31539  auto diff = f - s;
31540  auto d = std::chrono::duration <double, std::milli> (diff).count();
31541  cout << "scanner::pipeline::count(): " << d << "ms" << endl;
31542  }
31543 
31544 
31545  cout << " validation..." << flush;
31546  {
31547  auto& res_vect = pipe.get_bv_res_vector();
31548  auto& cnt_vect = pipe2.get_bv_count_vector();
31549 
31550  for (size_t i = 0; i < res_vect.size(); ++i)
31551  {
31552  const bvect* bv1 = res_vec1[i].get();
31553  const auto* bv = res_vect[i];
31554  assert(bv);
31555  bool match = bv1->equal(*bv);
31556  assert(match);
31557  auto c = cnt_vect[i];
31558  auto cnt = bv->count();
31559  assert(cnt == c);
31560  }
31561  }
31562  cout << "OK" << endl;
31563 
31564 
31565  cout << "---------------------------- TestSparseFindEqStrPipeline() OK" << endl;
31566 }
31567 
31568 
31569 void quicksort2(str_svect_type& strsv, int first, int last)
31570 {
31571  using stype = str_svect_type::size_type;
31572  int i, j, pivot;
31573 
31574  // fixed size for simplicity (in prod code needs dynamic buffer handling)
31575  static str_svect_type::value_type pivot_buf[128];
31576  while (first < last)
31577  {
31578  pivot = i = first;
31579  j = last;
31580 
31581  // save the pivor to re-use it in strsv.compare(..)
31582  strsv.get(stype(pivot), pivot_buf, sizeof(pivot_buf));
31583 
31584  while (i < j)
31585  {
31586  while((i < last) && (strsv.compare(stype(i), pivot_buf) <= 0))
31587  ++i;
31588  while(strsv.compare(stype(j), pivot_buf) > 0)
31589  --j;
31590  if (i < j)
31591  strsv.swap(stype(i), stype(j));
31592  } // while
31593  strsv.swap(stype(pivot), stype(j));
31594 
31595  quicksort2(strsv, first, j-1);
31596  first = j+1; // tail recursion
31597  } // while
31598 }
31599 
31600 static
31601 void generate_string_set(vector<string>& str_vec,
31602  const unsigned max_coll = 150000,
31603  unsigned repeat = 220,
31604  bool shuffle = true)
31605 {
31606  str_vec.resize(0);
31607  string str;
31608  for (unsigned i = 10; i < max_coll; i += unsigned(rand() % 3))
31609  {
31610  switch (rand()%8)
31611  {
31612  case 0: str = "xnssv"; break;
31613  default: str = "xrs"; break;
31614  }
31615  str.append(to_string(i));
31616  str_vec.emplace_back(str);
31617 
31618  for (unsigned k = 0; k < repeat; ++k, ++i) // add more of the same string
31619  str_vec.emplace_back(str);
31620 
31621  } // for i
31622 
31623  if (shuffle)
31624  {
31625  std::random_device rd;
31626  std::mt19937 g(rd());
31627  std::shuffle(str_vec.begin() + str_vec.size()/2, str_vec.end(), g);
31628  }
31629 }
31630 
31631 static
31633 {
31634  cout << "---------------------------- TestStrSparseQuickSort()" << endl;
31635 
31636  unsigned max_pass = 250;
31637  bool shuffle = true;
31638  for (unsigned pass = 0; pass < max_pass; pass += (unsigned)rand()%25, shuffle ^= true)
31639  {
31640  vector<string> str_vec;
31641  generate_string_set(str_vec, 350000, pass, shuffle);
31642 
31643  str_svect_type str_sv, str_sv2, str_sv_ref;
31644  {
31645  auto bi = str_sv.get_back_inserter();
31646  for (const string& term : str_vec)
31647  bi = term;
31648  bi.flush();
31649  }
31650  str_sv.remap();
31651  str_sv.optimize();
31652 
31653  quicksort2(str_sv, 0, (int)str_sv.size()-1);
31654 
31655  std::sort(str_vec.begin(), str_vec.end());
31656  {
31657  auto bi = str_sv_ref.get_back_inserter();
31658  for (const string& term : str_vec)
31659  bi = term;
31660  bi.flush();
31661  }
31662  str_sv_ref.remap();
31663  str_sv_ref.optimize();
31664 
31665  bool b = str_sv_ref.equal(str_sv);
31666  if (!b)
31667  {
31668  cerr << "vector mismatch detected!" << endl;
31669 
31670  vector<string>::const_iterator sit = str_vec.begin();
31671  str_svect_type::const_iterator it = str_sv.begin();
31672  str_svect_type::const_iterator it_end = str_sv.end();
31673  for (; it != it_end; ++it, ++sit)
31674  {
31675  string s = *it;
31676  if (*sit != s)
31677  {
31678  cerr << "Mismatch at:" << s << "!=" << *sit << endl;
31679  assert(0);
31680  exit(1);
31681  }
31682  } // for
31683  assert(0);
31684  }
31685 
31686  str_sv2 = str_sv;
31687  str_sv.optimize();
31688 /*cout << "qsort 2 " << flush;
31689 
31690  quicksort2(str_sv, 0, (int)str_sv.size()-1);
31691 
31692  bool eq = str_sv2.equal(str_sv);
31693  assert(eq);
31694 */
31695  if (!is_silent)
31696  cout << "\r " << pass << "/" << max_pass << flush;
31697 
31698 
31699 
31700  } // for pass
31701 
31702  cout << "\n---------------------------- TestStrSparseQuickSort() OK" << endl;
31703 }
31704 
31705 static
31707 {
31708  cout << "---------------------------- Bit-plane STR sparse vector SORT test" << endl;
31709  const unsigned max_coll = 560000;
31710  {
31711  std::vector<string> str_coll;
31712  str_svect_type str_sv_sorted;
31713 
31714  // generate sorted vector
31715  string str;
31716  for (unsigned i = 10; i < max_coll; i+=10)
31717  {
31718  str = to_string(i);
31719  str_coll.emplace_back(str);
31720  } // for i
31721  std::sort(str_coll.begin(), str_coll.end());
31722  for (const string& s : str_coll)
31723  {
31724  str_sv_sorted.push_back(s);
31725  } // for s
31726  str_sv_sorted.optimize();
31727  CheckStrSVCompare(str_sv_sorted, str_sv_sorted.size()/10);
31728 
31729  // run lower bound tests
31731 
31732  for (unsigned i = 0; i < max_coll; ++i)
31733  {
31734  str = to_string(i);
31735 
31736  unsigned pos;
31737  bool found = scanner.lower_bound_str(str_sv_sorted, str.c_str(), pos);
31738  string s1;
31739  if (found)
31740  {
31741  str_sv_sorted.get(pos, s1);
31742  assert(s1 == str);
31743  }
31744 
31745  auto it = std::lower_bound(str_coll.begin(), str_coll.end(), str);
31746  if (it != str_coll.end())
31747  {
31748  unsigned idx = unsigned(it - str_coll.begin());
31749  const string& s0 = str_coll[idx];
31750 
31751  if (s0 == str)
31752  {
31753  assert(found);
31754  assert(pos == idx);
31755  }
31756  else
31757  {
31758  assert(!found);
31759  str_sv_sorted.get(pos, s1);
31760 
31761  assert(pos == idx);
31762  }
31763  }
31764  if (i % 4096 == 0)
31765  if (!is_silent)
31766  cout << "\r" << i << "/" << max_coll << flush;
31767 
31768  } // for
31769  cout << "\n";
31770  }
31771 
31772  cout << "sort test data generation.." << endl;
31773  // insertion sort stress test
31774  {
31775  std::vector<string> str_coll;
31776  // generate test values vector
31777  string str;
31778  for (unsigned i = 0; i < max_coll; )
31779  {
31780  str = to_string(i);
31781  str_coll.emplace_back(str);
31782  i += (unsigned)rand() % 3;
31783  } // for i
31784 
31785  // shuffle the data set
31786  {
31787  std::random_device rd;
31788  std::mt19937 g(rd());
31789  std::shuffle(str_coll.begin(), str_coll.end(), g);
31790  }
31791 
31792 
31793 
31794 
31795  // insertion sort
31796  str_svect_type str_sv_sorted;
31797 
31798  cout << "\ninsertion sort..." << endl;
31799  {
31800  std::chrono::time_point<std::chrono::steady_clock> st;
31801  st = std::chrono::steady_clock::now();
31802 
31803  unsigned i = 0;
31805  for (const string& s : str_coll)
31806  {
31807  unsigned pos;
31808  bool found = scanner.lower_bound_str(str_sv_sorted, s.c_str(), pos);
31809 
31810  auto sz1 = str_sv_sorted.size();
31811 
31812  str_sv_sorted.insert(pos, s.c_str());
31813 
31814  auto sz2 = str_sv_sorted.size();
31815  assert(sz1 + 1 == sz2);
31816 
31817  {
31818  string str_sv;
31819  str_sv_sorted.get(pos, str_sv);
31820  assert(s == str_sv);
31821  }
31822 
31823  if (pos)
31824  {
31825  string str_prev;
31826  str_sv_sorted.get(pos-1, str_prev);
31827  if (str_prev >= s)
31828  {
31829  cerr << "insertion sort sort order check failed! "
31830  << " i = " << i
31831  << "s=" << s << " prev=" << str_prev
31832  << endl;
31833 
31834  exit(1);
31835  }
31836  }
31837 
31838  {
31839  unsigned pos2;
31840  found = scanner.lower_bound_str(str_sv_sorted, s.c_str(), pos2);
31841  if (!found)
31842  {
31843  cerr << "control loss at " << i << " " << s << endl;
31844  exit(1);
31845  }
31846  assert(pos == pos2);
31847  }
31848 
31849 
31850  if (!is_silent)
31851  if (i % 8096 == 0)
31852  {
31853  std::chrono::time_point<std::chrono::steady_clock> f = std::chrono::steady_clock::now();
31854  auto diff = f - st;
31855  auto d = std::chrono::duration <double, std::milli> (diff).count();
31856 
31857  cout << "\r" << i << "/" << max_coll << " (" << d << "ms)" << flush;
31858 
31859  str_sv_sorted.optimize();
31860 
31861  st = std::chrono::steady_clock::now();
31862  }
31863  ++i;
31864  } // for s
31865  }
31866  cout << endl;
31867 
31868  cout << "sort validation.." << endl;
31869  std::sort(str_coll.begin(), str_coll.end());
31870  unsigned i = 0;
31871  string str_prev;
31872  for (const string& s : str_coll)
31873  {
31874  string sv_str;
31875  str_sv_sorted.get(i, sv_str);
31876  if (i)
31877  {
31878  if (str_prev > sv_str)
31879  {
31880  cerr << "Sort order violation!" << endl;
31881  exit(1);
31882  }
31883  }
31884  //cout << s << " = " << sv_str << endl;
31885  if (s != sv_str)
31886  {
31887  cerr << "Sort comparison failed at i=" << i << " s=" << s
31888  << " sv_str = " << sv_str << endl;
31889 
31891  unsigned pos;
31892  bool found = scanner.lower_bound_str(str_sv_sorted, s.c_str(), pos);
31893 
31894  if (!found)
31895  {
31896  cerr << s << " not found in target." << endl;
31897  }
31898  else
31899  {
31900  cerr << s << " is at idx=" << pos << endl;
31901  }
31902 
31903  exit(1);
31904  }
31905  str_prev = sv_str;
31906  ++i;
31907  } // for s
31908 
31909  EraseStrCollection(str_sv_sorted);
31910 
31911  }
31912 
31913 
31914 
31915  cout << "---------------------------- Bit-plane STR sparse vector SORT test OK" << endl;
31916 
31917 }
31918 
31919 static
31921 {
31922  std::cout << "---------------------------- sparse vector SORT test" << endl;
31923  const unsigned max_coll = 560000;
31924  typedef bm::sparse_vector<unsigned, bvect > u_svect_type;
31925 
31926  {
31927  std::vector<unsigned> u_coll;
31928  u_svect_type u_sv_sorted;
31929 
31930  // generate sorted vector
31931  string str;
31932  for (unsigned i = 10; i < max_coll; i+=10)
31933  {
31934  u_coll.emplace_back(i);
31935  } // for i
31936  std::sort(u_coll.begin(), u_coll.end());
31937  for (const unsigned u : u_coll)
31938  {
31939  u_sv_sorted.push_back(u);
31940  } // for s
31941  u_sv_sorted.optimize();
31942 
31943  // run lower bound tests
31945 
31946  for (unsigned i = 0; i < max_coll; ++i)
31947  {
31948  bvect::size_type pos;
31949  bool found = scanner.bfind(u_sv_sorted, i, pos);
31950  unsigned u1;
31951  if (found)
31952  {
31953  u1 = u_sv_sorted[pos];
31954  assert(u1 == i);
31955  }
31956 
31957  auto it = std::lower_bound(u_coll.begin(), u_coll.end(), i);
31958  if (it != u_coll.end())
31959  {
31960  unsigned idx = unsigned(it - u_coll.begin());
31961  unsigned u0 = u_coll[idx];
31962 
31963  if (u0 == i)
31964  {
31965  assert(found);
31966  assert(pos == idx);
31967  }
31968  else
31969  {
31970  assert(!found);
31971  u1 = u_sv_sorted[pos];
31972 
31973  assert(pos == idx);
31974  }
31975  }
31976  if (i % 4096 == 0)
31977  if (!is_silent)
31978  cout << "\r" << i << "/" << max_coll << flush;
31979 
31980  } // for
31981  cout << "\n";
31982 
31983  }
31984 
31985 
31986  cout << "insertion sort test data generation.." << endl;
31987  // insertion sort stress test
31988  {
31989  std::vector<unsigned> u_coll;
31990  // generate test values vector
31991  for (unsigned i = 0; i < max_coll; )
31992  {
31993  u_coll.emplace_back(i);
31994  i += (unsigned)rand() % 3;
31995  } // for i
31996 
31997  // shuffle the data set
31998  {
31999  std::random_device rd;
32000  std::mt19937 g(rd());
32001  std::shuffle(u_coll.begin(), u_coll.end(), g);
32002  }
32003 
32004  // insertion sort
32005  u_svect_type u_sv_sorted;
32006 
32007  cout << "\ninsertion sort..." << endl;
32008  {
32009  std::chrono::time_point<std::chrono::steady_clock> st;
32010  st = std::chrono::steady_clock::now();
32011 
32012  unsigned i = 0;
32014  for (const unsigned u : u_coll)
32015  {
32016  bvect::size_type pos;
32017  bool found = scanner.bfind(u_sv_sorted, u, pos);
32018 
32019  auto sz1 = u_sv_sorted.size();
32020 
32021  u_sv_sorted.insert(pos, u);
32022 
32023  auto sz2 = u_sv_sorted.size();
32024  assert(sz1 + 1 == sz2);
32025 
32026  {
32027  unsigned u_sv = u_sv_sorted.get(pos);
32028  assert(u == u_sv);
32029  }
32030 
32031  if (pos)
32032  {
32033  unsigned u_prev;
32034  u_prev = u_sv_sorted.get(pos-1);
32035  if (u_prev >= u)
32036  {
32037  cerr << "insertion sort sort order check failed! "
32038  << " i = " << i
32039  << "s=" << u << " prev=" << u_prev
32040  << endl;
32041  assert(0); exit(1);
32042  }
32043  }
32044 
32045  {
32046  bvect::size_type pos2;
32047  found = scanner.bfind(u_sv_sorted, u, pos2);
32048  if (!found)
32049  {
32050  cerr << "control loss at " << i << " " << u << endl;
32051  assert(0); exit(1);
32052  }
32053  assert(pos == pos2);
32054  }
32055 
32056 
32057  if (!is_silent)
32058  if (i % 8096 == 0)
32059  {
32060  std::chrono::time_point<std::chrono::steady_clock> f = std::chrono::steady_clock::now();
32061  auto diff = f - st;
32062  auto d = std::chrono::duration <double, std::milli> (diff).count();
32063  cout << "\r" << i << "/" << max_coll << " (" << d << "ms)" << flush;
32064 
32065  u_sv_sorted.optimize();
32066 
32067  st = std::chrono::steady_clock::now();
32068  }
32069  ++i;
32070  } // for s
32071  }
32072  cout << endl;
32073 
32074  cout << "sort validation.." << endl;
32075  std::sort(u_coll.begin(), u_coll.end());
32076  unsigned i = 0;
32077  unsigned u_prev = 0;
32078  for (unsigned u : u_coll)
32079  {
32080  unsigned sv_u;
32081  sv_u = u_sv_sorted.get(i);
32082  if (i)
32083  {
32084  if (u_prev > sv_u)
32085  {
32086  cerr << "Sort order violation!" << endl;
32087  assert(0);exit(1);
32088  }
32089  }
32090  //cout << s << " = " << sv_str << endl;
32091  if (u != sv_u)
32092  {
32093  cerr << "Sort comparison failed at i=" << i << " u=" << u
32094  << " sv_u = " << sv_u << endl;
32095 
32097  bvect::size_type pos;
32098  bool found = scanner.bfind(u_sv_sorted, u, pos);
32099 
32100  if (!found)
32101  {
32102  cerr << u << " not found in target." << endl;
32103  }
32104  else
32105  {
32106  cerr << u << " is at idx=" << pos << endl;
32107  }
32108 
32109  exit(1);
32110  }
32111  u_prev = sv_u;
32112  ++i;
32113  } // for u
32114 
32115  EraseSVCollection(u_sv_sorted);
32116  }
32117 
32118 
32119 
32120  cout << "---------------------------- sparse vector SORT test OK" << endl;
32121 
32122 }
32123 
32124 static
32126 {
32127  std::cout << "---------------------------- TestSignedSparseSort()" << endl;
32128  const int max_coll = 560000;
32129  typedef bm::sparse_vector<int, bvect > i_svect_type;
32130 
32131  {
32132  std::vector<int> i_coll;
32133  i_svect_type i_sv_sorted;
32134 
32135  // generate sorted vector
32136  for (int i = 10; i < max_coll; i+=10)
32137  {
32138  i_coll.emplace_back(-i);
32139  }
32140 
32141  std::sort(i_coll.begin(), i_coll.end());
32142 
32143  for (const auto u : i_coll)
32144  i_sv_sorted.push_back(u);
32145  i_sv_sorted.optimize();
32146 
32147  // run lower bound tests
32149 
32150  for (int i = 0; i < max_coll; ++i)
32151  {
32152  bvect::size_type pos;
32153  bool found = scanner.bfind(i_sv_sorted, -i, pos);
32154  int u1;
32155  if (found)
32156  {
32157  u1 = i_sv_sorted[pos];
32158  assert(u1 == -i);
32159  }
32160 
32161  auto it = std::lower_bound(i_coll.begin(), i_coll.end(), -i);
32162  if (it != i_coll.end())
32163  {
32164  auto v = *it;
32165  if (v == -i)
32166  {
32167  unsigned idx = unsigned(it - i_coll.begin());
32168  int u0 = i_coll[idx];
32169 
32170  if (u0 == -i)
32171  {
32172  assert(found);
32173  assert(pos == idx);
32174  }
32175  else
32176  {
32177  assert(!found);
32178  u1 = i_sv_sorted[pos];
32179  assert(pos == idx);
32180  }
32181  }
32182  }
32183  if (!is_silent)
32184  if (i % 4096 == 0)
32185  cout << "\r" << i << "/" << max_coll << flush;
32186 
32187  } // for
32188  cout << "\n";
32189 
32190  }
32191 
32192 
32193  cout << "insertion sort test data generation.." << endl;
32194  // insertion sort stress test
32195  {
32196  std::vector<int> u_coll;
32197  // generate test values vector
32198  for (int i = 0; i < max_coll; )
32199  {
32200  if (i & 1)
32201  u_coll.emplace_back(-i);
32202  else
32203  u_coll.emplace_back(i);
32204  i += rand() % 3;
32205  } // for i
32206 
32207  // shuffle the data set
32208  {
32209  std::random_device rd;
32210  std::mt19937 g(rd());
32211  std::shuffle(u_coll.begin(), u_coll.end(), g);
32212  }
32213 
32214  // insertion sort
32215  i_svect_type i_sv_sorted;
32216 
32217  cout << "\ninsertion sort..." << endl;
32218  {
32219  std::chrono::time_point<std::chrono::steady_clock> st;
32220  st = std::chrono::steady_clock::now();
32221 
32222  unsigned i = 0;
32224  for (const int u : u_coll)
32225  {
32226  bvect::size_type pos;
32227  bool found = scanner.bfind(i_sv_sorted, u, pos);
32228 
32229  auto sz1 = i_sv_sorted.size();
32230 
32231  i_sv_sorted.insert(pos, u);
32232 
32233  auto sz2 = i_sv_sorted.size();
32234  assert(sz1 + 1 == sz2);
32235 
32236  {
32237  int u_sv = i_sv_sorted.get(pos);
32238  assert(u == u_sv);
32239  }
32240 
32241  if (pos)
32242  {
32243  int u_prev;
32244  u_prev = i_sv_sorted.get(pos-1);
32245  if (u_prev >= u)
32246  {
32247  cerr << "insertion sort sort order check failed! "
32248  << " i = " << i
32249  << "s=" << u << " prev=" << u_prev
32250  << endl;
32251  assert(0); exit(1);
32252  }
32253  }
32254 
32255  {
32256  bvect::size_type pos2;
32257  found = scanner.bfind(i_sv_sorted, u, pos2);
32258  if (!found)
32259  {
32260  cerr << "control loss at " << i << " " << u << endl;
32261  assert(0); exit(1);
32262  }
32263  assert(pos == pos2);
32264  }
32265 
32266 
32267  if (!is_silent)
32268  if (i % 8096 == 0)
32269  {
32270  std::chrono::time_point<std::chrono::steady_clock> f = std::chrono::steady_clock::now();
32271  auto diff = f - st;
32272  auto d = std::chrono::duration <double, std::milli> (diff).count();
32273  cout << "\r" << i << "/" << max_coll << " (" << d << "ms)" << flush;
32274 
32275  i_sv_sorted.optimize();
32276 
32277  st = std::chrono::steady_clock::now();
32278  }
32279  ++i;
32280  } // for s
32281  }
32282  cout << endl;
32283 
32284  cout << "sort validation.." << endl;
32285  std::sort(u_coll.begin(), u_coll.end());
32286  int i = 0;
32287  int u_prev = 0;
32288  for (int u : u_coll)
32289  {
32290  int sv_u;
32291  sv_u = i_sv_sorted.get(unsigned(i));
32292  if (i)
32293  {
32294  if (u_prev > sv_u)
32295  {
32296  cerr << "Sort order violation!" << endl;
32297  assert(0);exit(1);
32298  }
32299  }
32300  //cout << s << " = " << sv_str << endl;
32301  if (u != sv_u)
32302  {
32303  cerr << "Sort comparison failed at i=" << i << " u=" << u
32304  << " sv_u = " << sv_u << endl;
32305 
32307  bvect::size_type pos;
32308  bool found = scanner.bfind(i_sv_sorted, u, pos);
32309 
32310  if (!found)
32311  {
32312  cerr << u << " not found in target." << endl;
32313  }
32314  else
32315  {
32316  cerr << u << " is at idx=" << pos << endl;
32317  }
32318 
32319  exit(1);
32320  }
32321  u_prev = sv_u;
32322  ++i;
32323  } // for u
32324 
32325  EraseSVCollection(i_sv_sorted);
32326  }
32327 
32328 
32329  cout << "---------------------------- TestSignedSparseSort() OK" << endl;
32330 
32331 }
32332 
32333 
32334 
32335 
32336 
32337 inline
32338 void LoadBVDump(const char* filename, const char* filename_out=0, bool validate=false)
32339 {
32340  ifstream bv_file (filename, ios::in | ios::binary);
32341  if (!bv_file.good())
32342  {
32343  cout << "Cannot open file: " << filename << endl;
32344  exit(1);
32345  }
32346 
32347  ofstream* bv_file_out = 0;
32348 
32349  if (filename_out)
32350  {
32351  bv_file_out = new ofstream(filename_out, ios::out | ios::binary);
32352  if (!bv_file_out->good())
32353  {
32354  cout << "Cannot create file: " << filename_out << endl;
32355  exit(1);
32356  }
32357  }
32358 
32359 
32360  unsigned buffer_size = 1024*1024;
32361  unsigned char* buffer = new unsigned char[buffer_size];
32362 
32363  unsigned count = 0;
32364  clock_t start = clock();
32365  size_t total_out_size = 0;
32366 
32367  for (;1; ++count)
32368  {
32369  unsigned bv_size;
32370  bv_file.read((char*)&bv_size, sizeof(bv_size));
32371  if (!bv_file.good())
32372  break;
32373  if (bv_size == 0)
32374  {
32375  cout << "Warning:Zero vector in dump..." << endl;
32376  continue;
32377  }
32378  if (buffer_size < bv_size)
32379  {
32380  delete [] buffer;
32381  buffer_size = bv_size;
32382  buffer = new unsigned char[buffer_size];
32383  }
32384  bv_file.read((char*)buffer, bv_size);
32385  {
32386  bvect bv;
32387  bm::deserialize(bv, (unsigned char*)buffer);
32388 
32389  bvect::statistics st1;
32390  bv.calc_stat(&st1);
32391 
32392  if (st1.max_serialize_mem > buffer_size)
32393  {
32394  delete [] buffer;
32395  buffer_size = (unsigned)st1.max_serialize_mem;
32396  buffer = new unsigned char[buffer_size];
32397  }
32398 
32399  size_t blob_size = bm::serialize(bv, buffer, BM_NO_GAP_LENGTH|BM_NO_BYTE_ORDER);
32400  total_out_size += blob_size;
32401 
32402  if (blob_size > bv_size)
32403  {
32404  //print_stat(bv);
32405  //cout << count << ". -" << blob_size-bv_size << endl;
32406  //exit(1);
32407  }
32408 
32409  if (validate)
32410  {
32411  bvect bv_control;
32412  bm::deserialize(bv_control, (unsigned char*)buffer);
32413  if (bv_control != bv)
32414  {
32415  cout << "Serialization error!" << endl;
32416  exit(1);
32417  }
32418  }
32419 
32420  if (bv_file_out)
32421  {
32422  bv_file_out->write((char*)&blob_size, sizeof(blob_size));
32423  bv_file_out->write((char*)buffer, (unsigned)blob_size);
32424  }
32425 
32426  }
32427  if (count % 1000 == 0)
32428  {
32429  cout << count << " out=" << total_out_size << endl;
32430  }
32431  //cout << count << ": size=" << bv_size << endl;
32432  }
32433 
32434  delete [] buffer;
32435  cout << "Total vectors:" << count << endl;
32436  cout << "Total out size:" << total_out_size << endl;
32437 
32438  clock_t finish = clock();
32439  clock_t elapsed_clocks = finish - start;
32440  double duration = (double)(elapsed_clocks) / CLOCKS_PER_SEC;
32441 
32442  cout << endl
32443  << "Serialization duration = " << duration
32444  << endl;
32445 
32446  bv_file_out->close();
32447  delete bv_file_out;
32448 
32449 }
32450 
32451 inline
32452 void GroupByTest(const char* filename, const char* query_filename)
32453 {
32454  bvect bv_query;
32455 
32456  unsigned count = 0;
32457  unsigned group_by_count = 0;
32458 
32459  clock_t start = clock();
32460 
32461  // load the query vector
32462  {
32463  ifstream bv_file (query_filename, ios::in | ios::binary);
32464  if (!bv_file.good())
32465  {
32466  cout << "Cannot open file: " << query_filename << endl;
32467  exit(1);
32468  }
32469  unsigned buffer_size = 400*1024*1024;
32470  unsigned char* buffer = new unsigned char[buffer_size];
32471 
32472  unsigned bv_size=0;
32473  bv_file.read((char*)&bv_size, sizeof(bv_size));
32474  if (bv_size == 0)
32475  {
32476  cout << "Warning:Zero vector in query dump..." << endl;
32477  return;
32478  }
32479  bv_file.read((char*)buffer, bv_size);
32480  bm::deserialize(bv_query, (unsigned char*)buffer);
32481 
32482  delete [] buffer;
32483 
32484  }
32485 
32486 
32487  ifstream bv_file (filename, ios::in | ios::binary);
32488  if (!bv_file.good())
32489  {
32490  cout << "Cannot open file: " << filename << endl;
32491  exit(1);
32492  }
32493 
32494 
32495  unsigned buffer_size = 100*1024*1024;
32496  unsigned char* buffer = new unsigned char[buffer_size];
32497 
32498  for (;1; ++count)
32499  {
32500  unsigned bv_size;
32501  bv_file.read((char*)&bv_size, sizeof(bv_size));
32502  if (!bv_file.good())
32503  break;
32504  if (bv_size == 0)
32505  {
32506  cout << "Warning:Zero vector in dump..." << endl;
32507  continue;
32508  }
32509  if (buffer_size < bv_size)
32510  {
32511  delete [] buffer;
32512  buffer_size = bv_size;
32513  buffer = new unsigned char[buffer_size];
32514  }
32515  bv_file.read((char*)buffer, bv_size);
32516  bvect bv;
32517  if (1)
32518  {
32519  bv.clear(true);
32520  bm::deserialize(bv, (unsigned char*)buffer);
32521 
32522  unsigned bc = bm::count_and(bv, bv_query);
32523  if (bc)
32524  {
32525  ++group_by_count;
32526  }
32527 
32528 /*
32529  bv &= bv_query;
32530  if (bv.any())
32531  {
32532  ++group_by_count;
32533  }
32534 */
32535 
32536  }
32537 
32538 
32539 #if 0
32540 //print_stat(bv_query);
32541 //exit(1);
32542  {
32543  bvect bv(BM_GAP);
32545  bv_query,
32546  (unsigned char*)buffer,
32547  0,
32548  bm::set_AND);
32549  // control
32550  if (0)
32551  {
32552  bvect bv_control(BM_GAP);
32553  bm::deserialize(bv_control, (unsigned char*)buffer);
32554  bv_control &= bv_query;
32555  if (bv_control != bv)
32556  {
32557  cerr << "Group by control failure" << endl;
32558  cerr << bv_control.count() << endl;
32559  cerr << bv.count() << endl;
32560  exit(1);
32561  }
32562  }
32563 
32564 
32565  if (bv.any())
32566  {
32567  ++group_by_count;
32568  }
32569  }
32570 #endif
32571 
32572  if (count % 1000 == 0)
32573  {
32574  cout << count << endl;
32575  }
32576  }
32577 
32578  delete [] buffer;
32579  cout << "Total vectors:" << count << endl;
32580  cout << "Group by vectors:" << group_by_count << endl;
32581 
32582  clock_t finish = clock();
32583  clock_t elapsed_clocks = finish - start;
32584  double duration = (double)(elapsed_clocks) / CLOCKS_PER_SEC;
32585 
32586  cout << endl
32587  << "Test duration = " << duration
32588  << endl;
32589 }
32590 
32591 
32592 inline
32593 void LoadVectors(const char* dir_name, unsigned from, unsigned to)
32594 {
32595  vector<bvect*> bv_list;
32596  vector<unsigned> sz_list;
32597 
32598  size_t total_size = 0;
32599  size_t total_new_size = 0;
32600 
32601  for(; from <= to; ++from)
32602  {
32603  std::stringstream fname_str;
32604  fname_str << dir_name << "/" << from;
32605  std::string s = fname_str.str();
32606  const char* fname = s.c_str();
32607 
32608  bvect* bv = new bvect;
32609 
32610  unsigned fsize = 0;
32611  LoadBVector(fname, *bv, &fsize);
32612  //bv->optimize();
32613  //print_stat(*bv);
32614 
32615 
32616  // get new size
32617  size_t blob_size = 0;
32618  {
32619  bvect::statistics st1;
32620  bv->calc_stat(&st1);
32621 
32622  unsigned char* blob = new unsigned char[st1.max_serialize_mem];
32623  blob_size = bm::serialize(*bv, blob, BM_NO_GAP_LENGTH|BM_NO_BYTE_ORDER);
32624 
32625  if (st1.max_serialize_mem < blob_size)
32626  {
32627  printf("BLOB size prediction error!\n");
32628  exit(1);
32629  }
32630 
32631  //if (from >= 27)
32632  {
32633  bvect bv_control;
32634  bm::deserialize(bv_control, (unsigned char*)blob);
32635  if (bv_control != *bv)
32636  {
32637  cout << "Serialization error!" << endl;
32638  exit(1);
32639  }
32640  }
32641 
32642  delete [] blob;
32643 
32644  }
32645 
32646  cout << fname << " "
32647  << " old=" << fsize << " new=" << blob_size
32648  << " diff=" << (int)fsize - (int) blob_size
32649  << endl;
32650 
32651  bv_list.push_back(bv);
32652  sz_list.push_back(fsize);
32653 
32654  total_size += fsize;
32655  total_new_size += blob_size;
32656  } // for
32657 
32658  cout << "Total size = " << total_size / (1024*1024) << "Mb" << endl;
32659  cout << " New size = " << total_new_size / (1024*1024) << "Mb" << endl;
32660  cout << "Total diff = " << (total_size - total_new_size) / (1024*1024) << "Mb" << endl;
32661 
32662  vector<unsigned char*> bv_blobs;
32663 
32664  cout << "Running serialization benchmark..." << endl;
32665  {
32666  clock_t start = clock();
32667 
32668  for (size_t i = 0; i < bv_list.size(); ++i)
32669  {
32670  const bvect* bv = bv_list[i];
32671  bvect::statistics st1;
32672  bv->calc_stat(&st1);
32673  unsigned char* blob = new unsigned char[st1.max_serialize_mem*2];
32674  bv_blobs.push_back(blob);
32675 
32676  for (int j = 0; j < (int)(400/(i?i:1)); ++j)
32677  {
32678  //unsigned blob_size =
32679  bm::serialize(*bv, blob);
32680  }
32681  // delete [] blob;
32682  }
32683 
32684  clock_t finish = clock();
32685  clock_t elapsed_clocks = finish - start;
32686  double duration = (double)(elapsed_clocks) / CLOCKS_PER_SEC;
32687 
32688  cout << endl
32689  << "Serialization duration = " << duration
32690  << endl;
32691  }
32692 
32693  cout << "Running de-serialization benchmark..." << endl;
32694  {
32695  clock_t start = clock();
32696 
32697  for (size_t i = 0; i < bv_blobs.size(); ++i)
32698  {
32699  const unsigned char* blob = bv_blobs[i];
32700  for (int j = 0; j < (int)(400/(i?i:1)); ++j)
32701  {
32702  bvect bv;
32703  bm::deserialize(bv, (unsigned char*)blob);
32704  }
32705  // delete [] blob;
32706  }
32707 
32708  clock_t finish = clock();
32709  clock_t elapsed_clocks = finish - start;
32710  double duration = (double)(elapsed_clocks) / CLOCKS_PER_SEC;
32711 
32712  cout << endl
32713  << "DeSerialization duration = " << duration
32714  << endl;
32715  }
32716 
32717 
32718 
32719 
32720  for (size_t i = 0; i < bv_list.size(); ++i)
32721  {
32722  delete bv_list[i];
32723  }
32724  for (size_t i = 0; i < bv_blobs.size(); ++i)
32725  {
32726  delete [] bv_blobs[i];
32727  }
32728 
32729 }
32730 
32731 
32732 static
32734 {
32735  cout << "------------------------ Test SIMD Utils" << endl;
32736 #if defined(BMSSE2OPT)
32737  unsigned idx;
32738  cout << "----------------------------> [ SSE2 ]" << endl;
32739  {
32740  unsigned short buf[127] = { 65535, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, 0, };
32741  idx = bm::sse2_gap_find(buf, 65535, 1);
32742  assert(idx == 0);
32743  idx = bm::sse2_gap_find(buf, 0, 1);
32744  assert(idx == 0);
32745  idx = bm::sse2_gap_find(buf, 10, 1);
32746  assert(idx == 0);
32747  }
32748 
32749  {
32750  unsigned short buf[16] = { 60000, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, 0, };
32751  idx = bm::sse2_gap_find(buf, 65535, 1);
32752  assert(idx == 1);
32753  idx = bm::sse2_gap_find(buf, 0, 1);
32754  assert(idx == 0);
32755  idx = bm::sse2_gap_find(buf, 10, 1);
32756  assert(idx == 0);
32757  }
32758 
32759  {
32760  unsigned short buf[16] = { 10, 65530, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
32761  const unsigned vsize = 2;
32762  idx = bm::sse2_gap_find(buf, 0, vsize);
32763  assert(idx == 0);
32764  idx = bm::sse2_gap_find(buf, 65535, vsize);
32765  assert(idx == vsize);
32766  for (unsigned i = 0; i < vsize; ++i)
32767  {
32768  unsigned short v = buf[i];
32769  idx = bm::sse2_gap_find(buf, v, vsize);
32770  assert(idx == i);
32771  idx = bm::sse2_gap_find(buf, gap_word_t(v - 1), vsize);
32772  assert(idx == i);
32773  }
32774  }
32775 
32776  {
32777  unsigned short buf[16] = { 10, 256, 65530, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
32778  const unsigned vsize = 3;
32779  idx = bm::sse2_gap_find(buf, 0, vsize);
32780  assert(idx == 0);
32781  idx = bm::sse2_gap_find(buf, 65535, vsize);
32782  assert(idx == vsize);
32783  for (unsigned i = 0; i < vsize; ++i)
32784  {
32785  unsigned short v = buf[i];
32786  idx = bm::sse2_gap_find(buf, v, vsize);
32787  assert(idx == i);
32788  idx = bm::sse2_gap_find(buf, gap_word_t(v - 1), vsize);
32789  assert(idx == i);
32790  }
32791  }
32792  {
32793  unsigned short buf[16] = { 10, 256, 258, 65500, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
32794  const unsigned vsize = 4;
32795  idx = bm::sse2_gap_find(buf, 0, vsize);
32796  assert(idx == 0);
32797  idx = bm::sse2_gap_find(buf, 65535, vsize);
32798  assert(idx == vsize);
32799  for (unsigned i = 0; i < vsize; ++i)
32800  {
32801  unsigned short v = buf[i];
32802  idx = bm::sse2_gap_find(buf, v, vsize);
32803  assert(idx == i);
32804  idx = bm::sse2_gap_find(buf, gap_word_t(v - 1), vsize);
32805  assert(idx == i);
32806  }
32807  }
32808 
32809  {
32810  // unsigned short buf[16] = { 10, 256, 258, 15525, 64500, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
32811  unsigned short buf[16] = { 10, 20, 30, 40, 50, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
32812  const unsigned vsize = 5;
32813  idx = bm::sse2_gap_find(buf, 0, vsize);
32814  assert(idx == 0);
32815  idx = bm::sse2_gap_find(buf, 65535, vsize);
32816  assert(idx == vsize);
32817 
32818  for (unsigned i = 0; i < vsize; ++i)
32819  {
32820  unsigned short v = buf[i];
32821  idx = bm::sse2_gap_find(buf, v, vsize);
32822  assert(idx == i);
32823  idx = bm::sse2_gap_find(buf, gap_word_t(v - 1), vsize);
32824  assert(idx == i);
32825  }
32826  }
32827 
32828  {
32829  unsigned short buf[127] = { 2, 10, 256, 258, 15525, 65530, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
32830  const unsigned vsize = 6;
32831  idx = bm::sse2_gap_find(buf, 0, vsize);
32832  assert(idx == 0);
32833  idx = bm::sse2_gap_find(buf, 65535, vsize);
32834  assert(idx == vsize);
32835  for (unsigned i = 0; i < vsize; ++i)
32836  {
32837  unsigned short v = buf[i];
32838  idx = bm::sse2_gap_find(buf, v, vsize);
32839  assert(idx == i);
32840  idx = bm::sse2_gap_find(buf, gap_word_t(v - 1), vsize);
32841  assert(idx == i);
32842  }
32843  }
32844 
32845  {
32846  unsigned short buf[16] = { 1, 2, 10, 256, 258, 15525, 65530, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
32847  const unsigned vsize = 7;
32848  idx = bm::sse2_gap_find(buf, 0, vsize);
32849  assert(idx == 0);
32850  idx = bm::sse2_gap_find(buf, 65535, vsize);
32851  assert(idx == vsize);
32852 
32853  for (unsigned i = 0; i < vsize; ++i)
32854  {
32855  unsigned short v = buf[i];
32856  idx = bm::sse2_gap_find(buf, v, vsize);
32857  assert(idx == i);
32858  idx = bm::sse2_gap_find(buf, gap_word_t(v - 1), vsize);
32859  assert(idx == i || (buf[i - 1] == v - 1));
32860  }
32861  }
32862  {
32863  unsigned short buf[16] = { 1, 2, 10, 256, 258, 1024, 15525, 65530, 127, 255, 256, 0xFF, };
32864  const unsigned vsize = 8;
32865  idx = bm::sse2_gap_find(buf, 0, vsize);
32866  assert(idx == 0);
32867  idx = bm::sse2_gap_find(buf, 65535, vsize);
32868  assert(idx == vsize);
32869  for (unsigned i = 0; i < vsize; ++i)
32870  {
32871  unsigned short v = buf[i];
32872  if (i == vsize - 1)
32873  {
32874  assert(v != 65535);
32875  }
32876  idx = bm::sse2_gap_find(buf, v, vsize);
32877  assert(idx == i);
32878  idx = bm::sse2_gap_find(buf, gap_word_t(v - 1), vsize);
32879  assert(idx == i || (buf[i - 1] == v - 1));
32880  }
32881  }
32882 
32883  {
32884  unsigned short buf[16] = { 6217, 6300, 6400, 6500,
32885  6600, 6700, 30584, 40255,
32886  50256, 60000, 61001, 65255, 65530, 12, 23, 0, };
32887  const unsigned vsize = 13;
32888  idx = bm::sse2_gap_find(buf, 0, vsize);
32889  assert(idx == 0);
32890  idx = bm::sse2_gap_find(buf, 65535, vsize);
32891  assert(idx == vsize);
32892 
32893  for (unsigned i = 0; i < vsize; ++i)
32894  {
32895  unsigned short v = buf[i];
32896  if (i == vsize - 1)
32897  {
32898  assert(v != 65535);
32899  }
32900  idx = bm::sse2_gap_find(buf, v, vsize);
32901  assert(idx == i);
32902  idx = bm::sse2_gap_find(buf, gap_word_t(v - 1), vsize);
32903  assert(idx == i || (buf[i - 1] == v - 1));
32904  }
32905  }
32906 
32907  {
32908  unsigned short buf[16] = { 6217, 6300, 6400, 6500,
32909  6600, 6700, 30584, 40255,
32910  50256, 60000, 61001, 65255,
32911  65256, 65257, 65300, 65530 };
32912  const unsigned vsize = 16;
32913  idx = bm::sse2_gap_find(buf, 0, vsize);
32914  assert(idx == 0);
32915  idx = bm::sse2_gap_find(buf, 65535, vsize);
32916  assert(idx == 16);
32917  for (unsigned i = 0; i < vsize; ++i)
32918  {
32919  unsigned short v = buf[i];
32920  if (i == vsize - 1)
32921  {
32922  assert(v != 65535);
32923  }
32924  idx = bm::sse2_gap_find(buf, v, vsize);
32925  assert(idx == i);
32926  idx = bm::sse2_gap_find(buf, gap_word_t(v - 1), vsize);
32927  assert(idx == i || (buf[i - 1] == v - 1));
32928  }
32929  }
32930 
32931  // SSE2 AND block check
32932 
32933 
32934 #endif
32935 
32936 #if defined(BMSSE42OPT)
32937  unsigned idx;
32938  cout << "----------------------------> [ SSE 4.2 ]" << endl;
32939 
32940  {
32942  for (unsigned i = 0; i < bm::set_block_size; ++i)
32943  {
32944  tb[i] = ~0u;
32945  }
32946  bool all_one = sse4_is_all_one((__m128i*)tb);
32947  assert(all_one);
32948 
32949  tb[256] = 1;
32950  all_one = sse4_is_all_one((__m128i*)tb);
32951  assert(!all_one);
32952  }
32953 
32954  {
32956  for (unsigned i = 0; i < bm::set_block_size; ++i)
32957  {
32958  tb[i] = 0u;
32959  }
32960  bool all_z = sse4_is_all_zero((__m128i*)tb);
32961  assert(all_z);
32962 
32963  tb[256] = 1;
32964  all_z = sse4_is_all_zero((__m128i*)tb);
32965  assert(!all_z);
32966  }
32967 
32968 
32969  {
32970  unsigned short buf[127] = { 65535, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, 0, };
32971  idx = bm::sse4_gap_find(buf, (unsigned short)65535, 1);
32972 cout << idx << endl;
32973  assert(idx == 0);
32974  idx = bm::sse4_gap_find(buf, 0, 1);
32975  assert(idx == 0);
32976  idx = bm::sse4_gap_find(buf, 10, 1);
32977  assert(idx == 0);
32978  }
32979 
32980  {
32981  unsigned short buf[16] = { 60000, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, 0, };
32982  idx = bm::sse4_gap_find(buf, 65535, 1);
32983  assert(idx == 1);
32984  idx = bm::sse4_gap_find(buf, 0, 1);
32985  assert(idx == 0);
32986  idx = bm::sse4_gap_find(buf, 10, 1);
32987  assert(idx == 0);
32988  }
32989 
32990  {
32991  unsigned short buf[16] = { 10, 65530, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
32992  const unsigned vsize = 2;
32993  idx = bm::sse4_gap_find(buf, 0, vsize);
32994  assert(idx == 0);
32995  idx = bm::sse4_gap_find(buf, 65535, vsize);
32996  assert(idx == vsize);
32997  for (unsigned i = 0; i < vsize; ++i)
32998  {
32999  unsigned short v = buf[i];
33000  idx = bm::sse4_gap_find(buf, v, vsize);
33001  assert(idx == i);
33002  idx = bm::sse4_gap_find(buf, (unsigned short)(v - 1), vsize);
33003  assert(idx == i);
33004  }
33005  }
33006 
33007  {
33008  unsigned short buf[16] = { 10, 256, 65530, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
33009  const unsigned vsize = 3;
33010  idx = bm::sse4_gap_find(buf, 0, vsize);
33011  assert(idx == 0);
33012  idx = bm::sse4_gap_find(buf, 65535, vsize);
33013  assert(idx == vsize);
33014  for (unsigned i = 0; i < vsize; ++i)
33015  {
33016  unsigned short v = buf[i];
33017  idx = bm::sse4_gap_find(buf, v, vsize);
33018  assert(idx == i);
33019  idx = bm::sse4_gap_find(buf, (unsigned short)(v - 1), vsize);
33020  assert(idx == i);
33021  }
33022  }
33023  {
33024  unsigned short buf[16] = { 10, 256, 258, 65500, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
33025  const unsigned vsize = 4;
33026  idx = bm::sse4_gap_find(buf, 0, vsize);
33027  assert(idx == 0);
33028  idx = bm::sse4_gap_find(buf, (unsigned short)(65535), vsize);
33029  assert(idx == vsize);
33030  for (unsigned i = 0; i < vsize; ++i)
33031  {
33032  unsigned short v = buf[i];
33033  idx = bm::sse4_gap_find(buf, v, vsize);
33034  assert(idx == i);
33035  idx = bm::sse4_gap_find(buf, (unsigned short)(v - 1), vsize);
33036  assert(idx == i);
33037  }
33038  }
33039 
33040  {
33041 // unsigned short buf[16] = { 10, 256, 258, 15525, 64500, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
33042  unsigned short buf[16] = { 10, 20, 30, 40, 50, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
33043  const unsigned vsize = 5;
33044  idx = bm::sse4_gap_find(buf, 0, vsize);
33045  assert(idx == 0);
33046  idx = bm::sse4_gap_find(buf, 65535, vsize);
33047  assert(idx == vsize);
33048 
33049  for (unsigned i = 0; i < vsize; ++i)
33050  {
33051  unsigned short v = buf[i];
33052  idx = bm::sse4_gap_find(buf, v, vsize);
33053  assert(idx == i);
33054  idx = bm::sse4_gap_find(buf, (unsigned short)(v - 1), vsize);
33055  assert(idx == i);
33056  }
33057  }
33058 
33059  {
33060  unsigned short buf[127] = { 2, 10, 256, 258, 15525, 65530, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
33061  const unsigned vsize = 6;
33062  idx = bm::sse4_gap_find(buf, 0, vsize);
33063  assert(idx == 0);
33064  idx = bm::sse4_gap_find(buf, 65535, vsize);
33065  assert(idx == vsize);
33066  for (unsigned i = 0; i < vsize; ++i)
33067  {
33068  unsigned short v = buf[i];
33069  idx = bm::sse4_gap_find(buf, v, vsize);
33070  assert(idx == i);
33071  idx = bm::sse4_gap_find(buf, (unsigned short)(v - 1), vsize);
33072  assert(idx == i);
33073  }
33074  }
33075 
33076  {
33077  unsigned short buf[16] = { 1, 2, 10, 256, 258, 15525, 65530, 127, 255, 256, 1000, 2000, 2001, 2005, 0xFF, };
33078  const unsigned vsize = 7;
33079  idx = bm::sse4_gap_find(buf, 0, vsize);
33080  assert(idx == 0);
33081  idx = bm::sse4_gap_find(buf, 65535, vsize);
33082  assert(idx == vsize);
33083 
33084  for (unsigned i = 0; i < vsize; ++i)
33085  {
33086  unsigned short v = buf[i];
33087  idx = bm::sse4_gap_find(buf, v, vsize);
33088  assert(idx == i);
33089  idx = bm::sse4_gap_find(buf, (unsigned short)(v - 1), vsize);
33090  assert(idx == i || (buf[i-1] == v-1));
33091  }
33092  }
33093  {
33094  unsigned short buf[16] = { 1, 2, 10, 256, 258, 1024, 15525, 65530, 127, 255, 256, 0xFF, };
33095  const unsigned vsize = 8;
33096  idx = bm::sse4_gap_find(buf, 0, vsize);
33097  assert(idx == 0);
33098  idx = bm::sse4_gap_find(buf, (unsigned short)(65535), vsize);
33099  assert(idx == vsize);
33100  for (unsigned i = 0; i < vsize; ++i)
33101  {
33102  unsigned short v = buf[i];
33103  if (i == vsize - 1)
33104  {
33105  assert(v != 65535);
33106  }
33107  idx = bm::sse4_gap_find(buf, v, vsize);
33108  assert(idx == i);
33109  idx = bm::sse4_gap_find(buf, (unsigned short)(v - 1), vsize);
33110  assert(idx == i || (buf[i - 1] == v - 1));
33111  }
33112  }
33113 
33114  {
33115  unsigned short buf[16] = { 6217, 6300, 6400, 6500,
33116  6600, 6700, 30584, 40255,
33117  50256, 60000, 61001, 65255, 65530, 12, 23, 0, };
33118  const unsigned vsize = 13;
33119  idx = bm::sse4_gap_find(buf, 0, vsize);
33120  assert(idx == 0);
33121  idx = bm::sse4_gap_find(buf, (unsigned short)(65535), vsize);
33122  assert(idx == vsize);
33123 
33124  for (unsigned i = 0; i < vsize; ++i)
33125  {
33126  unsigned short v = buf[i];
33127  if (i == vsize - 1)
33128  {
33129  assert(v != 65535);
33130  }
33131  idx = bm::sse4_gap_find(buf, v, vsize);
33132  assert(idx == i);
33133  idx = bm::sse4_gap_find(buf, (unsigned short)(v - 1), vsize);
33134  assert(idx == i || (buf[i - 1] == v - 1));
33135  }
33136  }
33137 
33138  {
33139  unsigned short buf[16] = { 6217, 6300, 6400, 6500,
33140  6600, 6700, 30584, 40255,
33141  50256, 60000, 61001, 65255,
33142  65256, 65257, 65300, 65530 };
33143  const unsigned vsize = 16;
33144  idx = bm::sse4_gap_find(buf, 0, vsize);
33145  assert(idx == 0);
33146  idx = bm::sse4_gap_find(buf, 65535, vsize);
33147  assert(idx == 16);
33148  for (unsigned i = 0; i < vsize; ++i)
33149  {
33150  unsigned short v = buf[i];
33151  if (i == vsize - 1)
33152  {
33153  assert(v != 65535);
33154  }
33155  idx = bm::sse4_gap_find(buf, v, vsize);
33156  assert(idx == i);
33157  idx = bm::sse4_gap_find(buf, (unsigned short)(v - 1), vsize);
33158  assert(idx == i || (buf[i - 1] == v - 1));
33159  }
33160  }
33161 
33162 
33163  // unsigned vector GE search
33164 
33165  {
33166  __m128i vect4 = _mm_set_epi32(-1, int(0x80000000u), 8, 0);
33167 
33168  int pos = bm::sse42_cmpge_u32(vect4, 0);
33169  assert(pos == 0);
33170 
33171  pos = bm::sse42_cmpge_u32(vect4, 7);
33172  assert(pos == 1);
33173 
33174  pos = bm::sse42_cmpge_u32(vect4, 8);
33175  assert(pos == 1);
33176 
33177  pos = bm::sse42_cmpge_u32(vect4, 0x80000000u);
33178  assert(pos == 2);
33179 
33180  pos = bm::sse42_cmpge_u32(vect4, ~0u);
33181  assert(pos == 3);
33182 
33183  pos = bm::sse42_cmpge_u32(vect4, ~0u - 1u);
33184  assert(pos == 3);
33185  }
33186 
33187  {
33188  __m128i vect4 = _mm_set_epi32(-1, -1, 8, 8);
33189 
33190  int pos = bm::sse42_cmpge_u32(vect4, 0);
33191  assert(pos == 0);
33192 
33193  pos = bm::sse42_cmpge_u32(vect4, 5);
33194  assert(pos == 0);
33195 
33196  pos = bm::sse42_cmpge_u32(vect4, 9);
33197  assert(pos == 2);
33198 
33199  pos = bm::sse42_cmpge_u32(vect4, ~0u);
33200  assert(pos == 2);
33201 
33202  pos = bm::sse42_cmpge_u32(vect4, ~0u - 1u);
33203  assert(pos == 2);
33204 
33205  }
33206 
33207  // lower bound SSE42 scan
33208 
33209  const unsigned arr_size = 50000;
33210  unsigned arr[arr_size + 10] = {0, };
33211 
33212  for (unsigned i = 0; i < arr_size; ++i)
33213  {
33214  arr[i] = 10 + i;
33215  }
33216 
33217  {
33218  unsigned target, s_idx;
33219  for (unsigned i = 0; i < arr_size-1; ++i)
33220  {
33221  target = arr[i];
33222  s_idx = bm::sse2_lower_bound_scan_u32(&arr[0], target, 0, arr_size-1);
33223  assert(s_idx == i);
33224  s_idx = bm::sse2_lower_bound_scan_u32(&arr[0], target, i, arr_size-1);
33225  assert(s_idx == i);
33226 
33227  target = 1; // not found but lower
33228  s_idx = bm::sse2_lower_bound_scan_u32(&arr[0], target, 0, arr_size-1);
33229  assert(s_idx == 0);
33230 
33231  target = arr_size * 2; // not found but higher
33232  s_idx = bm::sse2_lower_bound_scan_u32(&arr[0], target, 0, arr_size-1);
33233  assert(s_idx == arr_size);
33234  }
33235 
33236  }
33237 
33238 #endif
33239 
33240 #if defined(BMAVX2OPT)
33241  cout << "----------------------------> [ AVX2 ]" << endl;
33242 
33243  {
33245  for (unsigned i = 0; i < bm::set_block_size; ++i)
33246  {
33247  tb[i] = 0;
33248  }
33249  bool all_z = avx2_is_all_zero((__m256i*)tb);
33250  assert(all_z);
33251 
33252  tb[256] = 1;
33253  all_z = avx2_is_all_zero((__m256i*)tb);
33254  assert(!all_z);
33255  }
33256 
33257  {
33259  for (unsigned i = 0; i < bm::set_block_size; ++i)
33260  {
33261  tb[i] = ~0u;
33262  }
33263  bool all_one = avx2_is_all_one((__m256i*)tb);
33264  assert(all_one);
33265 
33266  tb[256] = 1;
33267  all_one = avx2_is_all_one((__m256i*)tb);
33268  assert(!all_one);
33269  }
33270 
33271  // AVX2 unsigned vector GE search
33272  {
33273  __m256i vect8 = _mm256_set_epi32(-1, -1, int(0x80000000u), 8, 5, 5, 4, 0);
33274 
33275  int pos = bm::avx2_cmpge_u32(vect8, 0);
33276  assert(pos == 0);
33277 
33278  pos = bm::avx2_cmpge_u32(vect8, 7);
33279  assert(pos == 4);
33280 
33281  pos = bm::avx2_cmpge_u32(vect8, 8);
33282  assert(pos == 4);
33283 
33284  pos = bm::avx2_cmpge_u32(vect8, 0x80000000u);
33285  assert(pos == 5);
33286 
33287  pos = bm::avx2_cmpge_u32(vect8, ~0u);
33288  assert(pos == 6);
33289 
33290  pos = bm::avx2_cmpge_u32(vect8, ~0u - 1u);
33291  assert(pos == 6);
33292  }
33293 
33294  cout << "avx2_cmpge_u32 stress" << endl;
33295  {
33296  for (unsigned i = 1; i < ~0u; ++i)
33297  {
33298  __m256i vect8 = _mm256_set_epi32(-1, -1, int(i), 0, 0, 0, 0, 0);
33299  int pos = bm::avx2_cmpge_u32(vect8, i);
33300  assert(pos == 5);
33301  }
33302  }
33303  cout << " - ok " << endl;
33304 
33305 
33306  {
33307  // lower bound avx2 scan
33308 
33309  const unsigned arr_size = 50000;
33310  unsigned arr[arr_size + 10] = { 0, };
33311 
33312  for (unsigned i = 0; i < arr_size; ++i)
33313  {
33314  arr[i] = 10 + i;
33315  }
33316 
33317  {
33318  unsigned target, s_idx;
33319  for (unsigned i = 0; i < arr_size - 1; ++i)
33320  {
33321  target = arr[i];
33322  s_idx = bm::avx2_lower_bound_scan_u32(&arr[0], target, 0, arr_size - 1);
33323  assert(s_idx == i);
33324  s_idx = bm::avx2_lower_bound_scan_u32(&arr[0], target, i, arr_size - 1);
33325  assert(s_idx == i);
33326 
33327  target = 1; // not found but lower
33328  s_idx = bm::avx2_lower_bound_scan_u32(&arr[0], target, 0, arr_size - 1);
33329  assert(s_idx == 0);
33330 
33331  target = arr_size * 2; // not found but higher
33332  s_idx = bm::avx2_lower_bound_scan_u32(&arr[0], target, 0, arr_size - 1);
33333  assert(s_idx == arr_size);
33334  }
33335  }
33336  }
33337 
33338 #endif
33339 
33340 
33341  cout << "------------------------ Test SIMD Utils OK" << endl;
33342 }
33343 
33344 static
33346 {
33347  bm::id_t id_to;
33348  bool found;
33349 
33350  {
33352 
33353  found = ares.resolve(10, &id_to);
33354  assert(!found);
33355  assert(id_to == 0);
33356 
33357  {
33358  bvps_addr_resolver<bvect> ares2(ares);
33359 
33360  found = ares2.resolve(10, &id_to);
33361  assert(!found);
33362  assert(id_to == 0);
33363  }
33364 
33365  found = ares.resolve(10, &id_to);
33366  assert(!found);
33367  assert(id_to == 0);
33368 
33369  }
33370 
33371  {
33373 
33374  ares.set(1000);
33375  ares.set(10000);
33376  ares.set(100000);
33377 
33378  found = ares.resolve(10, &id_to);
33379  assert(!found);
33380  assert(id_to == 0);
33381 
33382  found = ares.resolve(100000, &id_to);
33383  assert(found);
33384  assert(id_to == 3);
33385 
33386  assert(ares.in_sync() == false);
33387 
33388  ares.optimize();
33389  assert(ares.in_sync() == false);
33390 
33391  ares.sync();
33392  assert(ares.in_sync());
33393 
33394  found = ares.resolve(100000, &id_to);
33395  assert(found);
33396  assert(id_to == 3);
33397 
33398  bvps_addr_resolver<bvect> ares2(ares);
33399  bool same = ares.equal(ares2);
33400  assert(same);
33401 
33403  ares3.move_from(ares2);
33404  same = ares.equal(ares3);
33405  assert(same);
33406 
33407  }
33408 
33409  {
33411 
33412  found = ares.resolve(10, &id_to);
33413  assert(!found);
33414  assert(id_to == 0);
33415  }
33416 
33417  {
33419 
33420  ares.set(1000); // 1
33421  ares.set(10000); // 2
33422  ares.set(bm::id_max32-1); // 3
33423  ares.set(5); // 4
33424 
33425  found = ares.resolve(10, &id_to);
33426  assert(!found);
33427  assert(id_to == 0);
33428 
33429  found = ares.resolve(1000, &id_to);
33430  assert(found);
33431  assert(id_to == 1);
33432 
33433  found = ares.resolve(bm::id_max32-1, &id_to);
33434  assert(found);
33435  assert(id_to == 3);
33436 
33437  ares.optimize();
33438 
33439  found = ares.resolve(5, &id_to);
33440  assert(found);
33441  assert(id_to == 4);
33442  }
33443 
33444 
33445 }
33446 
33447 // generate pseudo-random bit-vector, mix of blocks
33448 //
33449 void generate_bvector(bvect& bv, unsigned vector_max, bool optimize)
33450 {
33451  unsigned i, j;
33452  for (i = 0; i < vector_max;)
33453  {
33454  // generate bit-blocks
33455  for (j = 0; j < 65535*8; i += 10, j++)
33456  {
33457  bv.set(i);
33458  }
33459  if (i > vector_max)
33460  break;
33461  // generate GAP (compressed) blocks
33462  for (j = 0; j < 65535; i += 120, j++)
33463  {
33464  unsigned len = (unsigned)rand() % 64;
33465  bv.set_range(i, i + len);
33466  bool all_one_range = bv.is_all_one_range(i, i + len);
33467  assert(all_one_range);
33468  if (len)
33469  {
33470  bool is_int = bm::is_interval(bv, i, i+len);
33471  assert(is_int);
33472  bvect::size_type pos;
33473  bool b = bm::find_interval_start(bv, i+len/2, pos);
33474  assert(b);
33475  assert(pos == i);
33476  b = bm::find_interval_end(bv, i+len/2, pos);
33477  assert(b);
33478  assert(pos == i+len);
33479  }
33480  i += len+1;
33481  if (i > vector_max)
33482  break;
33483  }
33484  }
33485  if (optimize)
33486  bv.optimize();
33487 }
33488 
33489 
33490 
33491 static
33493 {
33494  bvect bv, bv1;
33495  {
33496  bv.set_range(from, to);
33497 
33498  for (bvect::size_type i = from; i <= to; ++i)
33499  bv1.set(i);
33500  }
33501 
33502  std::vector<bvect::size_type> v;
33503  std::vector<bvect::size_type> v1;
33504  {
33505  bm::visit_each_bit(bv, (void*)&v, bit_decode_func);
33506  bm::visit_each_bit(bv1, (void*)&v1, bit_decode_func);
33507 
33508  assert(v.size() == bv.count());
33509  assert(v[0] == from);
33510  assert(v1.size() == bv1.count());
33511  assert(v1[0] == from);
33512 
33513  for (size_t i = 1; i < v.size(); ++i)
33514  {
33515  bvect::size_type prev = v[i-1];
33516  bvect::size_type curr = v[i];
33517  assert(prev+1 == curr);
33518  prev = v1[i-1];
33519  curr = v1[i];
33520  assert(prev+1 == curr);
33521  }
33522  {
33523  bvect bv_control;
33524  bm::combine_or(bv_control, v.begin(), v.end());
33525  bool eq = bv.equal(bv_control);
33526  assert(eq);
33527  bv_control.clear();
33528  bm::combine_or(bv_control, v1.begin(), v1.end());
33529  eq = bv.equal(bv_control);
33530  assert(eq);
33531  }
33532  }
33533 }
33534 
33535 
33536 
33537 static
33539 {
33540  cout << "------------------------ bvector BitForEach Test" << endl;
33541  int res;
33542 
33543  {
33544  cout << "test empty vector" << endl;
33545  bvect bv1;
33546  std::vector<bvect::size_type> v1, v2;
33547 
33548  bm::visit_each_bit(bv1, (void*)&v1, bit_decode_func);
33550  if (v1.size() || v2.size())
33551  {
33552  cerr << "1. Failed empty vector decode " << v1.size() << endl;
33553  exit(1);
33554  }
33555 
33556 
33557  bv1.init();
33558  bm::visit_each_bit(bv1, (void*)&v1, bit_decode_func);
33560  if (v1.size() || v2.size())
33561  {
33562  cerr << "2. Failed empty vector decode " << v1.size() << endl;
33563  exit(1);
33564  }
33565 
33566 
33567  bv1.set(100000, true);
33568  bv1.set(100000, false);
33569 
33570  bm::visit_each_bit(bv1, (void*)&v1, bit_decode_func);
33571  bm::visit_each_bit_range(bv1, 100000, 100000, (void*)&v2, bit_decode_func);
33572  if (v1.size() || v2.size())
33573  {
33574  cerr << "3. Failed empty vector decode " << v1.size() << endl;
33575  exit(1);
33576  }
33577 
33578  bv1.optimize();
33579 
33580  bm::visit_each_bit(bv1, (void*)&v1, bit_decode_func);
33581  bm::visit_each_bit_range(bv1, 100000, 100000, (void*)&v2, bit_decode_func);
33582  if (v1.size() || v2.size())
33583  {
33584  cerr << "3. Failed empty vector decode " << v1.size() << endl;
33585  exit(1);
33586  }
33587  assert(v1 == v2);
33588  }
33589 
33590  {
33591  std::vector<bvect::size_type> v1;
33592 
33593  bvect bv(BM_GAP);
33594  bv.set_range(20, 30);
33595 
33596  bm::visit_each_bit_range(bv, 20, 20, (void*)&v1, bit_decode_func);
33597  assert(v1.size()==1);
33598  assert(v1[0] == 20);
33599  v1.resize(0);
33600 
33601  bm::visit_each_bit_range(bv, 20, 21, (void*)&v1, bit_decode_func);
33602  assert(v1.size()==2);
33603  assert(v1[0] == 20);
33604  assert(v1[1] == 21);
33605  v1.resize(0);
33606 
33607  VisitorAllRangeTest(bv);
33608 
33609  bv.set(50);
33610  VisitorAllRangeTest(bv);
33611 
33612  bv.set(65535);
33613  VisitorAllRangeTest(bv);
33614 
33615  bv.set_range(200, 300);
33616  VisitorAllRangeTest(bv);
33617 
33618  bv.set_range(2000, 3000);
33619  bv.set_range(20000, 30000);
33620 
33621 
33622  bv.set_range(65535-34, 65535);
33623  VisitorAllRangeTest(bv);
33624  }
33625 
33626  {
33627  std::vector<bvect::size_type> v1;
33628 
33629  bvect bv(BM_GAP);
33630  bv.set_range(bm::id_max-100, bm::id_max-1);
33631  bv.set_range(bm::id_max-1000, bm::id_max-1000+23);
33632  bv.optimize();
33633 
33634  VisitorAllRangeTest(bv);
33635  }
33636 
33637 
33638  {
33639  bvect bv1 { 0,1,2, 10, 32, 100, 65535,
33640  65535+1, 65535+2, 65535+10, 65535+11, 65535+31,
33641  20000000 };
33642  bvect bv2;
33643  std::vector<unsigned> v1, v2;
33644 
33645  bm::visit_each_bit(bv1, (void*)&v1, bit_decode_func);
33647 
33648  {
33649  for (size_t i = 0; i < v1.size(); ++i)
33650  cout << v1[i] << ", ";
33651  cout << endl;
33652  }
33653 
33654  if (v1.size() != bv1.count())
33655  {
33656  std::cerr << "0. Bit for each failed size test. " << v1.size()
33657  << " should be " << bv1.count() << std::endl;
33658  exit(1);
33659  }
33660  if (v1 != v2)
33661  {
33662  assert(0);
33663  }
33664  bm::combine_or(bv2, v1.begin(), v1.end());
33665 
33666  res = bv2.compare(bv1);
33667  if (res != 0)
33668  {
33669  std::cerr << "0. Bit for each failed comparison test. " << endl;
33670  exit(1);
33671  }
33672 
33673  VisitorAllRangeTest(bv1);
33674 
33675  bv1.optimize();
33676  bv2.reset();
33677  v1.resize(0);
33678  bm::visit_each_bit(bv1, (void*)&v1, bit_decode_func);
33679 
33680  {
33681  for (size_t i = 0; i < v1.size(); ++i)
33682  cout << v1[i] << ", ";
33683  cout << endl;
33684  }
33685 
33686  if (v1.size() != bv1.count())
33687  {
33688  std::cerr << "1. Bit for each failed size test. " << v1.size()
33689  << " should be " << bv1.count() << std::endl;
33690  exit(1);
33691  }
33692  bm::combine_or(bv2, v1.begin(), v1.end());
33693 
33694  res = bv2.compare(bv1);
33695  if (res != 0)
33696  {
33697  std::cerr << "1. Bit for each failed comparison test. " << endl;
33698  exit(1);
33699  }
33700  VisitorAllRangeTest(bv1);
33701 
33702  }
33703 
33704 
33705  {
33706  bvect bv1, bv2;
33707  std::vector<unsigned> v1;
33708 
33709  generate_bvector(bv1);
33710  v1.reserve(bv1.count());
33711 
33712  bm::visit_each_bit(bv1, (void*)&v1, bit_decode_func);
33713  if (v1.size() != bv1.count())
33714  {
33715  std::cerr << "Bit for each failed size test. " << v1.size()
33716  << " should be " << bv1.count() << std::endl;
33717  exit(1);
33718  }
33719  bm::combine_or(bv2, v1.begin(), v1.end());
33720  res = bv2.compare(bv1);
33721  if (res != 0)
33722  {
33723  std::cerr << "Bit for each failed comparison test. " << endl;
33724  exit(1);
33725  }
33726 
33727  cout << "for each bit in optimized bit-vector..." << endl;
33728 
33729  v1.resize(0);
33730  bv2.clear(true);
33731 
33732  VisitorAllRangeTest(bv1, (bvect::size_type)v1.size()/2048);
33733 
33734  bv1.optimize();
33735 
33736  bm::visit_each_bit(bv1, (void*)&v1, bit_decode_func);
33737 
33738  if (v1.size() != bv1.count())
33739  {
33740  std::cerr << "Bit for each failed size test. " << v1.size()
33741  << " should be " << bv1.count() << std::endl;
33742  exit(1);
33743  }
33744  bm::combine_or(bv2, v1.begin(), v1.end());
33745 
33746  res = bv2.compare(bv1);
33747  if (res != 0)
33748  {
33749  std::cerr << "Bit for each failed comparison test. " << endl;
33750  exit(1);
33751  }
33752  VisitorAllRangeTest(bv1, (bvect::size_type)v1.size()/3048);
33753  }
33754 
33755  {
33756  RangeForEachTest(0, 65536);
33757  RangeForEachTest(65536, 65536+65536);
33758  RangeForEachTest(bm::id_max/2, bm::id_max/2 + (65536*256));
33759  }
33760 
33761 
33762  cout << "Inverted bvector tests..." << endl;
33763  {
33764  bvect::size_type from(200), to(65536*3+5);
33765  for (unsigned pass = 0; pass < 2; ++pass)
33766  {
33767  assert(from <=to);
33768  bvect bv;
33769  bv.invert();
33770 
33771  bvect bv1(bv);
33772  bv1.keep_range(from, to);
33773  bvect bv2;
33774  bv2.copy_range(bv, from, to);
33775 
33776 
33777  // std::vector<bvect::size_type> v1;
33778  bvect bv_c;
33779  {
33781  bm::for_each_bit_range(bv, from, to, func);
33782 
33783 // bm::visit_each_bit_range(bv, from, to, (void*)&v1, bit_decode_func);
33784 // assert(v1.size() == to-from+1);
33785 // bm::combine_or(bv_c, v1.begin(), v1.end());
33786  }
33787 // v1.resize(0);
33788 
33789  bool eq;
33790  eq = bv_c.equal(bv1);
33791  assert(eq);
33792  eq = bv_c.equal(bv2);
33793  assert(eq);
33794 
33795  bv_c.clear();
33796 /*
33797  {
33798  bm::visit_each_bit_range(bv1, from, to, (void*)&v1, bit_decode_func);
33799  assert(v1.size() == to-from+1);
33800  bm::combine_or(bv_c, v1.begin(), v1.end());
33801  }
33802 
33803  eq = bv_c.equal(bv1);
33804  assert(eq);
33805  eq = bv_c.equal(bv2);
33806  assert(eq);
33807 */
33808  bv_c.clear();
33809 
33810  to = bm::id_max/2;
33811  ++from;
33812  }
33813  }
33814 
33815  // dsabled for now as it produces excessive memory consumption
33816  #if 0
33817  {
33818  bvect bv;
33819  bv.set();
33820 
33821  std::vector<bvect::size_type> v1;
33822  try
33823  {
33824  bm::visit_each_bit(bv, (void*)&v1, bit_decode_func2);
33825  } catch (...)
33826  {
33827  }
33828  }
33829  #endif
33830  cout << "OK" << endl;
33831 
33832 
33833  cout << "------------------------ bvector BitForEach Test OK" << endl;
33834 }
33835 
33836 static
33838 {
33839  unsigned sz_factor = (unsigned)rand() % 10;
33840  if (!sz_factor)
33841  sz_factor = 1;
33842  unsigned size = 65000 + (unsigned)rand() % 65;// (128000 / sz_factor);
33843 
33844  buf.resize(size);
33845  unsigned char* data = buf.data();
33846 
33847  for (unsigned i = 0; i < size; ++i)
33848  {
33849  data[i] = (unsigned char)i;
33850  }
33851 }
33852 
33853 static
33855 {
33856  unsigned sz = (unsigned)rand() % 10000;
33857  unsigned key = 0;
33858  unsigned key_factor = (unsigned)rand() % 128;
33859  if (!key_factor)
33860  key_factor = 1;
33861  for (unsigned i = 0; i < sz; ++i)
33862  {
33863  {
33866  cbc.move_buffer(key, buf);
33867  }
33868  key += key_factor;
33869  } // for
33870  cbc.sync();
33871 }
33872 
33873 static
33875 {
33876  cout << "------------------------ Compressed collection Test" << endl;
33877 
33878  {
33880  bool added;
33881  unsigned v;
33882 
33883  added = coll.push_back(0, 100);
33884  assert(added);
33885  added = coll.push_back(10, 5);
33886  assert(added);
33887  added = coll.push_back(150000, 500);
33888  assert(added);
33889 
33890  coll.sync();
33891 
33892  bool found;
33893  bm::id_t idx;
33894 
33895  found = coll.resolve(0, &idx);
33896  assert(found);
33897  v = coll.get(idx);
33898  assert(v == 100);
33899 
33900  found = coll.resolve(10, &idx);
33901  assert(found);
33902  v = coll.get(idx);
33903  assert(v == 5);
33904 
33905  found = coll.resolve(150000, &idx);
33906  assert(found);
33907  v = coll.get(idx);
33908  assert(v == 500);
33909 
33910  found = coll.resolve(256, &idx);
33911  assert(!found);
33912 
33913  coll.optimize();
33914 
33915  found = coll.resolve(0, &idx);
33916  assert(found);
33917  v = coll.get(idx);
33918  assert(v == 100);
33919 
33920  found = coll.resolve(10, &idx);
33921  assert(found);
33922  v = coll.get(idx);
33923  assert(v == 5);
33924 
33925  found = coll.resolve(150000, &idx);
33926  assert(found);
33927  v = coll.get(idx);
33928  assert(v == 500);
33929 
33930  found = coll.resolve(256, &idx);
33931  assert(!found);
33932 
33933  }
33934 
33935  {
33937  {
33939  buf.copy_from((unsigned char*)"ABC", 3);
33940  cbc.move_buffer(10, buf);
33941  }
33942  {
33944  buf.copy_from((unsigned char*)"1234", 4);
33945  cbc.move_buffer(15, buf);
33946  }
33947  cbc.sync();
33948 
33949  assert(cbc.size() == 2);
33951  cbc.calc_stat(&st);
33952 
33955 
33956  cbcs.serialize(cbc, sbuf);
33957  assert(sbuf.size() > 0);
33958 
33962  cbcd.deserialize(cbc2, sbuf2.buf());
33963 
33964  if (!cbc2.equal(cbc))
33965  {
33966  std::cerr << "Compressed collection serialization error" << endl;
33967  exit(1);
33968  }
33969 
33970  }
33971 
33972  {
33973  cout << "Compressed buffer collection stress." << endl;
33974  const unsigned test_count = 60;
33975  for (unsigned i = 0; i < test_count; ++i)
33976  {
33979 
33981 
33983  cbc1.calc_stat(&st);
33984 
33987 
33988  cbcs.serialize(cbc1, sbuf);
33989 
33991  sbuf.release();
33992 
33994  cbcd.deserialize(cbc2, sbuf2.buf());
33995 
33996  if (!cbc2.equal(cbc1))
33997  {
33998  std::cerr << "Compressed collection serialization error at step " << i << endl;
33999  exit(1);
34000  }
34001 
34002 
34003  if (!is_silent)
34004  cout << "\r" << i << " of " << test_count << flush;
34005  } // for
34006 
34007  cout << endl;
34008  }
34009 
34010 
34011  cout << "------------------------ Compressed collection Test OK" << endl;
34012 }
34013 
34014 static
34016 {
34017  cout << " ------------------------------ Test bit-block LAST find" << endl;
34018 
34019  {
34020  bool found;
34021  unsigned last;
34022 
34024  for (unsigned i = 0; i < bm::set_block_size; ++i)
34025  {
34026  tb.b_.w32[i] = 0u;
34027  }
34028  found = bm::bit_find_last(tb, &last);
34029  assert(!found);
34030 
34031  tb.b_.w32[0] = 1u;
34032  found = bm::bit_find_last(tb, &last);
34033  assert(found);
34034  assert(last == 0);
34035 
34036  for (unsigned j = 0; j < 31; ++j)
34037  {
34038  tb.b_.w32[0] = 1u << j;
34039  found = bm::bit_find_last(tb, &last);
34040  assert(found);
34041  assert(last == j);
34042  }
34043  tb.b_.w32[0] = 0;
34044  for (unsigned j = 0; j < 31; ++j)
34045  {
34046  tb.b_.w32[0] |= 1u << j;
34047  found = bm::bit_find_last(tb, &last);
34048  //cout << "last = " << last << " j = " << j << endl;
34049  assert(found);
34050  assert(last == j);
34051  }
34052 
34053  tb.b_.w32[1] = 1u;
34054  found = bm::bit_find_last(tb, &last);
34055  cout << "last = " << last << endl;
34056  assert(found);
34057  assert(last == 32);
34058 
34059  tb.b_.w32[1] = 1u << 1;
34060  found = bm::bit_find_last(tb, &last);
34061  cout << "last = " << last << endl;
34062  assert(found);
34063  assert(last == 33);
34064 
34065 
34066  tb.b_.w32[bm::set_block_size-1] = 1u << 31;
34067  found = bm::bit_find_last(tb, &last);
34068  cout << "last = " << last << endl;
34069  assert(found);
34070  assert(last == 65535);
34071 
34072  tb.b_.w32[bm::set_block_size-1] = 1u << 30;
34073  found = bm::bit_find_last(tb, &last);
34074  //cout << "last = " << last << " j = " << j << endl;
34075  assert(found);
34076  assert(last == 65534);
34077  }
34078  cout << "Unit 1 ok." << endl;
34079 
34080  {
34081  bool found;
34082  unsigned last;
34083 
34085  for (unsigned i = 0; i < bm::set_block_size; ++i)
34086  {
34087  tb.b_.w32[i] = 0u;
34088  }
34089  for (unsigned i = 0; i < bm::set_block_size; ++i)
34090  {
34091  tb.b_.w32[i] = 1u;
34092  found = bm::bit_find_last(tb, &last);
34093  assert(found);
34094  assert(last == (i * 32));
34095  }
34096  for (unsigned i = 0; i < bm::set_block_size; ++i)
34097  {
34098  tb.b_.w32[i] = 0u;
34099  }
34100  for (unsigned i = 0; i < bm::set_block_size; ++i)
34101  {
34102  tb.b_.w32[i] = 2;
34103  found = bm::bit_find_last(tb, &last);
34104  assert(found);
34105  assert(last == (i * 32)+1);
34106  }
34107  }
34108  cout << "Unit 2 ok." << endl;
34109 
34110  cout << " ------------------------------ Test bit-block LAST find OK" << endl;
34111 }
34112 
34113 
34114 static
34116 {
34117  cout << " ------------------------------ Test bit-block ZERO" << endl;
34118  {
34119  BM_DECLARE_TEMP_BLOCK(tb1);
34120 
34121  unsigned pad = 0xDEAD;
34122  for (unsigned i = 0; i < bm::set_block_size; ++i)
34123  {
34124  tb1.b_.w32[i] = 0;
34125  }
34126 
34127  auto zero = bm::bit_is_all_zero(tb1);
34128  assert(zero);
34129  cout << zero << endl;
34130 
34131  for (unsigned i = 0; i < bm::set_block_size; ++i)
34132  {
34133  ::memset(tb1, 0, sizeof(tb1));
34134  tb1.b_.w32[i] = 1;
34135  zero = bm::bit_is_all_zero(tb1);
34136  assert(!zero);
34137  cout << zero;
34138  }
34139  cout << pad << endl;
34140  }
34141  cout << "\n ------------------------------ Test bit-block ZERO OK" << endl;
34142 
34143 }
34144 
34145 static
34147 {
34148  cout << " ------------------------------ TestFindFirst()" << endl;
34149 
34150  unsigned pos;
34151  bool f;
34152  BM_DECLARE_TEMP_BLOCK(tb1);
34153 
34154  for (unsigned i = 0; i < bm::set_block_size; ++i)
34155  tb1.b_.w32[i] = 0;
34156  f = bm::bit_find_first(tb1, &pos);
34157  assert(!f);
34158 
34159  for (unsigned w = 0; w < bm::set_block_size; ++w)
34160  {
34161  for (unsigned b = 0; b < 32; ++b)
34162  {
34163  unsigned mask1 = 1u << b;
34164  tb1.b_.w32[w] |= mask1;
34165 
34166  f = bm::bit_find_first(tb1, &pos);
34167  assert(f);
34168 
34169  unsigned pos_c = w*32 + b;
34170  assert(pos == pos_c);
34171  tb1.b_.w32[w] = 0;
34172  }
34173 
34174  } // for w
34175 
34176 
34177  cout << " ------------------------------ TestFindFirst()" << endl;
34178 }
34179 
34180 
34181 static
34183 {
34184  cout << " ------------------------------ Test bit_find_first_diff()" << endl;
34185  {
34186  unsigned pos;
34187  bool f;
34188  BM_DECLARE_TEMP_BLOCK(tb1);
34189  BM_DECLARE_TEMP_BLOCK(tb2);
34190 
34191  for (unsigned i = 0; i < bm::set_block_size; ++i)
34192  {
34193  tb1.b_.w32[i] = 0; tb2.b_.w32[i] = 0;
34194  }
34195 
34196  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34197  assert(f == false);
34198 
34199  f = bm::bit_find_first(tb1, &pos);
34200  assert(f == false);
34201 
34202  tb2.b_.w32[0] = 1;
34203  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34204  assert(f);
34205  assert(pos == 0);
34206  f = bm::bit_find_first(tb2, &pos);
34207  assert(f);
34208  assert(pos == 0);
34209 
34210 
34211  tb2.b_.w32[0] = (1 << 1);
34212  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34213  assert(f);
34214  assert(pos == 1);
34215  f = bm::bit_find_first(tb2, &pos);
34216  assert(f);
34217  assert(pos == 1);
34218 
34219  tb1.b_.w32[0] = (1 << 1);
34220  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34221  assert(!f);
34222 
34223  for (unsigned i = 0; i < bm::set_block_size; ++i)
34224  {
34225  tb1.b_.w32[i] = 0; tb2.b_.w32[i] = 0;
34226  }
34227  tb1.b_.w32[0] = (1<<12); tb2.b_.w32[1] = 18;
34228  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34229  assert(f);
34230  assert(pos == 12);
34231  f = bm::bit_find_first(tb1, &pos);
34232  assert(f);
34233  assert(pos == 12);
34234 
34235  for (unsigned i = 0; i < bm::set_block_size; ++i)
34236  {
34237  tb1.b_.w32[i] = 0; tb2.b_.w32[i] = 0;
34238  }
34239 
34240  bm::set_bit(tb1, 12345);
34241  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34242  assert(f);
34243  assert(pos == 12345);
34244  f = bm::bit_find_first(tb1, &pos);
34245  assert(f);
34246  assert(pos == 12345);
34247 
34248  bm::set_bit(tb2, 12345);
34249  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34250  assert(!f);
34251 
34252  for (unsigned i = 0; i < bm::set_block_size; ++i)
34253  {
34254  tb1.b_.w32[i] = 0; tb2.b_.w32[i] = 0;
34255  }
34256 
34257  for (unsigned k = 0; k < 65535; ++k)
34258  {
34259  bm::set_bit(tb1, k);
34260  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34261  assert(f);
34262  assert(pos == k);
34263 
34264 
34265  for (unsigned j = k+1; j < 65535; ++j)
34266  {
34267  bm::set_bit(tb1, j);
34268  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34269  assert(f);
34270  assert(pos == k);
34271  bm::set_bit(tb2, j);
34272  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34273  assert(f);
34274  assert(pos == k);
34275  bm::clear_bit(tb1, j);
34276  bm::clear_bit(tb2, j);
34277  }
34278 
34279  bm::set_bit(tb2, k);
34280  f = bm::bit_find_first_diff(tb1, tb2, &pos);
34281  assert(!f);
34282 
34283  if (!is_silent)
34284  if ((k & 0xFF) == 0)
34285  cout << "\r" << k << flush;
34286  } // for
34287 
34288  }
34289  cout << "\n ------------------------------ Test bit_find_first_diff() OK" << endl;
34290 
34291 }
34292 
34293 static
34295 {
34296  cout << " ------------------------------ TestBlockExpandCompact()" << endl;
34297 
34298  unsigned pos;
34299  BM_DECLARE_TEMP_BLOCK(tb1);
34300  BM_DECLARE_TEMP_BLOCK(tb2);
34301  BM_DECLARE_TEMP_BLOCK(tb3);
34302 
34303  bm::id64_t d64;
34304 
34305  bm::bit_block_set(tb1, 0);
34306  //bm::bit_block_set(tb2, 0);
34307 
34308 
34309  tb1.b_.w32[1] = 1;
34310 
34311  d64 = bm::calc_block_digest0(tb1);
34312  assert(d64 == 1ULL);
34313 
34314  bm::block_compact_by_digest(tb2, tb1, d64, true);
34315  bool f = bm::bit_find_first_diff(tb1, tb2, &pos);
34316  assert(!f);
34317 
34318  bm::block_expand_by_digest(tb3, tb2, d64, true);
34319  f = bm::bit_find_first_diff(tb3, tb1, &pos);
34320  assert(!f);
34321 
34322 
34323 
34324  for (unsigned k = 0; k < 65535; ++k)
34325  {
34326  bm::bit_block_set(tb1, 0);
34327  bm::set_bit(tb1, k);
34328 
34329  d64 = bm::calc_block_digest0(tb1);
34330  bm::block_compact_by_digest(tb2, tb1, d64, true);
34331 
34332  bm::block_expand_by_digest(tb3, tb2, d64, true);
34333  f = bm::bit_find_first_diff(tb3, tb1, &pos);
34334  assert(!f);
34335 
34336  bm::set_bit(tb1, 0);
34337 
34338  d64 = bm::calc_block_digest0(tb1);
34339  bm::block_compact_by_digest(tb2, tb1, d64, true);
34340 
34341  bm::block_expand_by_digest(tb3, tb2, d64, true);
34342  f = bm::bit_find_first_diff(tb3, tb1, &pos);
34343  assert(!f);
34344 
34345  } // for k
34346 
34347 
34348  for (unsigned k = 0; k < 65535; ++k)
34349  {
34350  bm::bit_block_set(tb1, 0);
34351  bm::set_bit(tb1, k);
34352 
34353  for (unsigned j = 0; j < k; j+=(unsigned)rand()%512)
34354  {
34355  bm::set_bit(tb1, j);
34356  d64 = bm::calc_block_digest0(tb1);
34357  bm::block_compact_by_digest(tb2, tb1, d64, true);
34358 
34359  bm::block_expand_by_digest(tb3, tb2, d64, true);
34360  f = bm::bit_find_first_diff(tb3, tb1, &pos);
34361  assert(!f);
34362  } // for j
34363 
34364  } // for k
34365 
34366  for (unsigned k = 0; k < 65535; ++k)
34367  {
34368  bm::bit_block_set(tb1, 0);
34369  bm::set_bit(tb1, k);
34370 
34371  for (int j = 65535; j > int(k); j-=(unsigned)rand()%512)
34372  {
34373  bm::set_bit(tb1, (unsigned)j);
34374  d64 = bm::calc_block_digest0(tb1);
34375  bm::block_compact_by_digest(tb2, tb1, d64, true);
34376 
34377  bm::block_expand_by_digest(tb3, tb2, d64, true);
34378  f = bm::bit_find_first_diff(tb3, tb1, &pos);
34379  assert(!f);
34380  } // for j
34381 
34382  } // for k
34383 
34384 
34385 
34386 
34387 
34388  cout << " ------------------------------ TestBlockExpandCompact() OK" << endl;
34389 }
34390 
34391 
34392 static
34394 {
34395  cout << " ------------------------------ Test bit-block DIGEST" << endl;
34396 
34397  BM_DECLARE_TEMP_BLOCK(tb1);
34398 
34399  {
34400  bm::id64_t digest0 = 1;
34401  bool all_zero;
34402  all_zero = bm::check_zero_digest(digest0, 0, 0);
34403  assert(!all_zero);
34404  for (unsigned i = 0; i < bm::set_block_digest_wave_size * 32; ++i)
34405  {
34406  all_zero = bm::check_zero_digest(digest0, 0, i);
34407  assert(!all_zero);
34408  }
34409  for (unsigned i = bm::set_block_digest_wave_size * 32+1; i < 65535; ++i)
34410  {
34411  all_zero = bm::check_zero_digest(digest0,
34413  i);
34414  assert(all_zero);
34415 
34416  } // for
34417  }
34418 
34419  for (unsigned k = 0; k < bm::set_block_size; ++k)
34420  {
34421  bm::bit_block_set(tb1, 0);
34422 
34423  tb1.b_.w32[k] = 1;
34425  bm::id64_t mask2 = bm::calc_block_digest0(tb1);
34426  assert(mask1 == mask2);
34427  bm::id64_t mask3 = bm::update_block_digest0(tb1, mask1);
34428  assert(mask1 == mask3);
34429 
34430  assert(mask1);
34431  unsigned bc = bm::word_bitcount64(mask1);
34432  assert(bc == 1);
34433 
34434  unsigned first_bit, ffbc;
34435  bool single_bit_found = bm::bit_find_first_if_1(tb1, &first_bit, mask1);
34436  assert(single_bit_found);
34437  unsigned found = bm::bit_find_first(tb1, &ffbc);
34438  assert(found);
34439  assert(first_bit == ffbc);
34440 
34441  bm::bit_block_set(tb1, 0);
34442  mask3 = bm::update_block_digest0(tb1, mask1);
34443  assert(!mask3);
34444 
34445  single_bit_found = bm::bit_find_first_if_1(tb1, &first_bit, mask1);
34446  assert(!single_bit_found);
34447 
34448  if (bm::word_bitcount64(mask3)==1)
34449  single_bit_found = bm::bit_find_first_if_1(tb1, &first_bit, mask3);
34450  else
34451  single_bit_found = false;
34452  assert(!single_bit_found);
34453 
34454  tb1.b_.w32[k] = 3;
34455  mask3 = bm::calc_block_digest0(tb1);
34456 
34457  if (bm::word_bitcount64(mask3)==1)
34458  single_bit_found = bm::bit_find_first_if_1(tb1, &first_bit, mask3);
34459  else
34460  single_bit_found = false;
34461 
34462  assert(!single_bit_found);
34463 
34464  tb1.b_.w32[0] = 1;
34465  tb1.b_.w32[bm::set_block_size/2] = 2;
34466 
34467  mask3 = bm::calc_block_digest0(tb1);
34468 
34469  if (bm::word_bitcount64(mask3)==1)
34470  single_bit_found = bm::bit_find_first_if_1(tb1, &first_bit, mask3);
34471  else
34472  single_bit_found = false;
34473  assert(!single_bit_found);
34474  }
34475 
34476  unsigned start = 0;
34477  unsigned end = bm::set_block_size-1;
34478 
34479  while(start <= end)
34480  {
34481  bm::bit_block_set(tb1, 0);
34482 
34483  tb1.b_.w32[start] = 1;
34484  tb1.b_.w32[end] = 1;
34485 
34486  bm::id64_t mask_s1 = bm::widx_to_digest_mask(start);
34488  bm::id64_t mask1 = mask_s1 | mask_e1;
34489  bm::id64_t mask2 = bm::calc_block_digest0(tb1);
34490  assert(mask1 == mask2);
34491  bm::id64_t mask3 = bm::update_block_digest0(tb1, mask1);
34492  assert(mask1 == mask3);
34493 
34494  if (mask_s1 != mask_e1)
34495  {
34496  unsigned bc = bm::word_bitcount64(mask1);
34497  assert(bc == 2);
34498  }
34499  else
34500  {
34501  unsigned bc = bm::word_bitcount64(mask1);
34502  assert(bc == 1);
34503  }
34504  bm::bit_block_set(tb1, 0);
34505  mask3 = bm::update_block_digest0(tb1, mask1);
34506  assert(!mask3);
34507 
34508 
34509  ++start; --end;
34510  } // while
34511 
34512  cout << "DIGEST masks tests..." << endl;
34513  {
34514  bm::id64_t d0;
34515  d0 = bm::digest_mask(0, 65535);
34516  assert(d0 == ~0ull);
34517  d0 = bm::digest_mask(1, 65535);
34518  assert(d0 == ~0ull);
34519  d0 = bm::digest_mask(0, 0);
34520  assert(d0 == 1ull);
34521  d0 = bm::digest_mask(1, 1);
34522  assert(d0 == 1ull);
34523  d0 = bm::digest_mask(1023, 1023);
34524  assert(d0 == 1ull);
34525 
34526  d0 = bm::digest_mask(1024, 1024+1024-1);
34527  assert(d0 == (1ull << 1));
34528  d0 = bm::digest_mask(0, 1024+1024-1);
34529  assert(d0 == ((1ull << 1)|1ull));
34530  d0 = bm::digest_mask(100, 1024+1024-1);
34531  assert(d0 == ((1ull << 1)|1ull));
34532 
34533  for (unsigned i = 0; i < 64; ++i)
34534  {
34535  bm::id64_t d0_c;
34536  d0 = bm::digest_mask(0, i*1024-(i>0));
34537  d0_c = bm::dm_control(0, i*1024-(i>0));
34538  assert(d0 == d0_c);
34539  for (unsigned j = 1; j <= i; ++j)
34540  {
34541  d0 = bm::digest_mask(j*1024-(j>0), i*1024-(i>0));
34542  d0_c = bm::dm_control(j*1024-(j>0), i*1024-(i>0));
34543  assert(d0_c == d0);
34544  } // for j
34545  } // for i
34546 
34547  cout << "DIGEST mask stress..." << endl;
34548  // uncomment this to fully re-check DIGEST mask (takes a very long time
34549  //unsigned test_to = 65536;
34550  unsigned test_to = 15000;
34551  for (unsigned i = 0; i < test_to; ++i)
34552  {
34553  bm::id64_t d0_c;
34554  d0 = bm::digest_mask(0, i);
34555  d0_c = bm::dm_control(0, i);
34556  assert(d0 == d0_c);
34557 
34558  for (unsigned j = test_to; j > i; --j)
34559  {
34560  d0 = bm::digest_mask(i, j);
34561  d0_c = bm::dm_control(i, j);
34562  assert(d0 == d0_c);
34563  } // for j
34564  if (!is_silent)
34565  if ((i & 0xFF) == 0)
34566  cout << "\r" << i << " | " << test_to << " " << flush;
34567  } // for i
34568 }
34569 
34570 
34571  cout << " ------------------------------ Test bit-block DIGEST OK" << endl;
34572 }
34573 
34574 
34575 static
34577 {
34578  cout << " ------------------------------ Test bit-block AND" << endl;
34579  {
34580  BM_DECLARE_TEMP_BLOCK(tb2);
34581  BM_DECLARE_TEMP_BLOCK(tb1);
34582  BM_DECLARE_TEMP_BLOCK(tb0);
34583 
34584  unsigned pad = 0xDEAD;
34585  for (unsigned i = 0; i < bm::set_block_size; ++i)
34586  {
34587  tb1.b_.w32[i] = tb2.b_.w32[i] = 0;
34588  }
34589 
34590  auto any = bm::bit_block_and(tb1, tb2);
34591  assert(any == 0);
34592  tb1.b_.w32[1] = 1;
34593  any = bm::bit_block_and(tb1, tb2);
34594  assert(any == 0);
34595  assert(tb1.b_.w32[1] == 0);
34596 
34597 
34598  tb1.b_.w32[1] = tb2.b_.w32[1] = 1;
34599  any = bm::bit_block_and(tb1, tb2);
34600 
34601  cout << tb1.b_.w32[1] << endl;
34602  assert(tb1.b_.w32[1] == 1);
34603  assert(any);
34604  for (unsigned j = 0; j < 32; ++j)
34605  {
34606  tb1.b_.w32[10] = tb2.b_.w32[10] = (1 << j);
34607  if (tb1.b_.w32[10])
34608  {
34609  any = bm::bit_block_and(tb1, tb2);
34610  assert(any);
34611  }
34612  }
34613 
34614  for (unsigned i = 0; i < bm::set_block_size; ++i)
34615  {
34616  tb1.b_.w32[i] = tb2.b_.w32[i] = 8;
34617  }
34618  any = bm::bit_block_and(tb1, tb2);
34619  assert(any);
34620  {
34621 // bm::bit_decode_cache dcache;
34622  bm::id64_t d1 = ~0ull;
34623  d1 = bm::bit_block_and(tb1,
34624  tb2,
34625  d1);
34627  assert(d1 == dc);
34628  unsigned bc = bm::word_bitcount64(d1);
34629  assert(bc == 64);
34630  }
34631  for (unsigned i = 0; i < bm::set_block_size; ++i)
34632  {
34633  assert(tb1.b_.w32[i] == tb2.b_.w32[i]);
34634  }
34635 
34636 
34637  for (unsigned i = 0; i < bm::set_block_size; ++i)
34638  {
34639  tb1.b_.w32[i] = tb2.b_.w32[i] = 0;
34640  }
34641 
34642  unsigned i, j;
34643  for (i = 0; i < bm::set_block_size; ++i)
34644  {
34645  for (j = 0; j < 32; ++j)
34646  {
34647  unsigned v = (1u << j);
34648  ::memset(tb1, 0, sizeof(tb1));
34649  ::memset(tb2, 0, sizeof(tb1));
34650  tb1.b_.w32[i] = tb2.b_.w32[i] = v;
34651  if (tb1[i])
34652  {
34653  auto any1 = bm::bit_block_and(tb1, tb2);
34654  auto all_zero = bm::bit_is_all_zero(tb1.begin());
34655 
34656  //cout << any1 <<" j=" << j << " i=" << i << " " << tb1[i] << " " << tb2[i] << endl;
34657  assert(pad == 0xDEAD);
34658  assert(tb1.b_.w32[i] == v);
34659  assert((unsigned)(all_zero) != any1);
34660  assert(any1);
34661  {
34662  bm::id64_t d1 = ~0ull;
34663  d1 = bm::bit_block_and(tb1,
34664  tb2,
34665  d1);
34667  assert(d1 == dc);
34668  unsigned bc = bm::word_bitcount64(d1);
34669  assert(bc == 1);
34670  }
34671  }
34672  }
34673  tb1.b_.w32[i] = tb2.b_.w32[i] = 0;
34674  }
34675  cout << tb1.b_.w32[0] << pad << endl;
34676 
34677 
34678  for (i = 0; i < bm::set_block_size; ++i)
34679  {
34680  ::memset(tb1, 0, sizeof(tb1));
34681  ::memset(tb2, 0, sizeof(tb1));
34682 
34683  tb1.b_.w32[i] = tb2.b_.w32[i] = 8u;
34684 
34685  auto any1 = bm::bit_block_and(tb1, tb2);
34686  assert(tb1.b_.w32[i] == 8u);
34687  assert(any1);
34688 
34689  ::memset(tb1, 0, sizeof(tb1));
34690  ::memset(tb2, 0, sizeof(tb1));
34691 
34692  tb1.b_.w32[i] = tb2.b_.w32[i] = 8u;
34693 
34694  bm::id64_t d1 = ~0ull;
34695  d1 = bm::bit_block_and(tb1,
34696  tb2,
34697  d1);
34699  assert(d1 == dc);
34700  unsigned bc = bm::word_bitcount64(d1);
34701  assert(bc == 1);
34702 
34703  d1 = ~0ull;
34704  d1 = bm::bit_block_and_2way(tb0, tb1, tb2, d1);
34705  assert(d1 == dc);
34706  }
34707 
34708 
34709 
34710  }
34711  cout << " ------------------------------ Test bit-block AND OK" << endl;
34712 
34713 }
34714 
34715 static
34717 {
34718  BM_DECLARE_TEMP_BLOCK(tb3);
34719  BM_DECLARE_TEMP_BLOCK(tb2);
34720  BM_DECLARE_TEMP_BLOCK(tb1);
34721 
34722  bool all_one;
34723 
34724  cout << " ------------------------------ Test bit-block OR" << endl;
34725 
34726  {
34727  for (unsigned i = 0; i < bm::set_block_size; ++i)
34728  {
34729  tb1.b_.w32[i] = tb3.b_.w32[i] = 0;
34730  tb2.b_.w32[i] = 8;
34731  }
34732 
34733  all_one = bm::bit_block_or(tb1, tb2);
34734  assert(!all_one);
34735  all_one = bm::bit_block_or_3way(tb3, tb2, tb1);
34736  assert(!all_one);
34737 
34738 
34739  for (unsigned i = 0; i < bm::set_block_size; ++i)
34740  {
34741  assert(tb1.b_.w32[i] == 8);
34742  if (tb1.b_.w32[i] != 8 || tb3.b_.w32[i] != 8)
34743  {
34744  cerr << "TestOR failed!" << endl;
34745  exit(1);
34746  }
34747  }
34748  }
34749 
34750  {
34751  for (unsigned i = 0; i < bm::set_block_size; ++i)
34752  {
34753  tb1.b_.w32[i] = ~3u;
34754  tb2.b_.w32[i] = 3u;
34755  tb3.b_.w32[i] = 0;
34756  }
34757 
34758  all_one = bm::bit_block_or(tb1, tb2);
34759  assert(all_one);
34760  all_one = bm::bit_block_or_3way(tb3, tb2, tb1);
34761  assert(all_one);
34762 
34763 
34764  for (unsigned i = 0; i < bm::set_block_size; ++i)
34765  {
34766  assert(tb1.b_.w32[i] == ~0u);
34767  assert(tb3.b_.w32[i] == ~0u);
34768  }
34769  }
34770 
34771  {
34772  for (unsigned i = 0; i < bm::set_block_size; ++i)
34773  {
34774  tb1.b_.w32[i] = 0;
34775  tb2.b_.w32[i] = 0;
34776  tb3.b_.w32[i] = 0;
34777  }
34778  for (unsigned i = 0; i < 100; ++i)
34779  {
34780  tb1.b_.w32[i] = ~0u;
34781  tb2.b_.w32[i] = 0;
34782  tb3.b_.w32[i] = 0;
34783  }
34784  for (unsigned i = 100; i < bm::set_block_size; ++i)
34785  {
34786  tb1.b_.w32[i] = 0;
34787  tb2.b_.w32[i] = ~0u;
34788  tb3.b_.w32[i] = 0;
34789  }
34790 
34791 
34792  all_one = bm::bit_block_or(tb1, tb2);
34793  assert(all_one);
34794  all_one = bm::bit_block_or_3way(tb3, tb2, tb1);
34795  assert(all_one);
34796 
34797 
34798  for (unsigned i = 0; i < bm::set_block_size; ++i)
34799  {
34800  assert(tb1.b_.w32[i] == ~0u);
34801  assert(tb3.b_.w32[i] == ~0u);
34802  }
34803  }
34804 
34805 
34806  cout << " ------------------------------ Test bit-block OR OK" << endl;
34807 
34808 }
34809 
34810 static
34812 {
34813  cout << " ------------------------------ Test bit-block SUB" << endl;
34814  {
34815  BM_DECLARE_TEMP_BLOCK(tb2);
34816  BM_DECLARE_TEMP_BLOCK(tb1);
34817 
34818  unsigned pad = 0xDEAD;
34819  for (unsigned i = 0; i < bm::set_block_size; ++i)
34820  {
34821  tb1.b_.w32[i] = tb2.b_.w32[i] = 0;
34822  }
34823 
34824  auto any = bm::bit_block_sub(tb1, tb2);
34825  assert(any == 0);
34826  tb1.b_.w32[1] = 1;
34827  any = bm::bit_block_sub(tb1, tb2);
34828  assert(any);
34829  assert(tb1.b_.w32[1] == 1);
34830 
34831 
34832  tb1.b_.w32[1] = tb2.b_.w32[1] = 1;
34833  any = bm::bit_block_sub(tb1, tb2);
34834 
34835  cout << tb1.b_.w32[1] << endl;
34836  assert(tb1.b_.w32[1] == 0);
34837  assert(!any);
34838  for (unsigned j = 0; j < 32; ++j)
34839  {
34840  tb1.b_.w32[10] = tb2.b_.w32[10] = (1 << j);
34841  if (tb1.b_.w32[10])
34842  {
34843  any = bm::bit_block_sub(tb1, tb2);
34844  assert(!any);
34845  }
34846  }
34847 
34848  for (unsigned i = 0; i < bm::set_block_size; ++i)
34849  {
34850  tb1.b_.w32[i] = tb2.b_.w32[i] = 8;
34851  }
34852  any = bm::bit_block_sub(tb1, tb2);
34853  assert(!any);
34854  for (unsigned i = 0; i < bm::set_block_size; ++i)
34855  {
34856  assert(tb1.b_.w32[i] != tb2.b_.w32[i]);
34857  }
34858 
34859  for (unsigned i = 0; i < bm::set_block_size; ++i)
34860  {
34861  tb1.b_.w32[i] = tb2.b_.w32[i] = 0;
34862  }
34863 
34864  unsigned i, j;
34865  for (i = 0; i < bm::set_block_size; ++i)
34866  {
34867  for (j = 0; j < 32; ++j)
34868  {
34869  unsigned v = (1u << j);
34870  ::memset(tb1, 0, sizeof(tb1));
34871  ::memset(tb2, 0, sizeof(tb1));
34872  tb1.b_.w32[i] = tb2.b_.w32[i] = v;
34873  if (tb1[i])
34874  {
34875  auto any1 = bm::bit_block_sub(tb1, tb2);
34876  auto all_zero = bm::bit_is_all_zero(tb1.begin());
34877  assert(all_zero);
34878 
34879  //cout << any1 <<" j=" << j << " i=" << i << " " << tb1[i] << " " << tb2[i] << endl;
34880  assert(pad == 0xDEAD);
34881  assert(tb1.b_.w32[i] == 0);
34882  assert(!any1);
34883  }
34884  }
34885  tb1.b_.w32[i] = tb2.b_.w32[i] = 0;
34886  }
34887  cout << tb1.b_.w32[0] << pad << endl;
34888 
34889 
34890  for (i = 0; i < bm::set_block_size; ++i)
34891  {
34892  ::memset(tb1, 0, sizeof(tb1));
34893  ::memset(tb2, 0, sizeof(tb1));
34894 
34895  tb1.b_.w32[i] = tb2.b_.w32[i] = 8u;
34896 
34897  auto any1 = bm::bit_block_sub(tb1, tb2);
34898  assert(tb1.b_.w32[i] == 0);
34899  assert(!any1);
34900  }
34901 
34902  }
34903  cout << " ------------------------------ Test bit-block SUB OK" << endl;
34904 
34905 }
34906 
34907 
34908 static
34910 {
34911  cout << " ------------------------------ Test Rank Compressor " << endl;
34912 
34913  int cmp;
34914 
34915  cout << "Step 1" << endl;
34916  {
34917  bvect bv1, bv2;
34918  bvect bv_s { 0, 1, 16 };
34919  bvect bv_i { 0, 1, 2, 10, 16 };
34920  bvect bv_sr; // restored vector
34921 
34922  bvect bv_ref { 0, 1, 4 };
34924 
34926  bv_i.build_rs_index(&bc);
34927 
34928  for (unsigned i = 0; i < 2; ++i)
34929  {
34930  rc.compress(bv1, bv_i, bv_s);
34931  assert(bv1.count() == bv_s.count());
34932 
34933  cmp = bv1.compare(bv_ref);
34934  assert(cmp == 0);
34935 
34936  rc.decompress(bv_sr, bv_i, bv1);
34937  cmp = bv_sr.compare(bv_s);
34938  assert(cmp == 0);
34939 
34940 
34941  rc.compress_by_source(bv2, bv_i, bc, bv_s);
34942  assert(bv2.count() == bv_s.count());
34943 
34944  cmp = bv2.compare(bv_ref);
34945  assert(cmp == 0);
34946 
34947 
34948  bv_i.optimize();
34949  bv_s.optimize();
34950  bv_i.build_rs_index(&bc);
34951  }
34952  }
34953  cout << "Step 1 - OK" << endl;
34954 
34955  {
34956  bvect bv1, bv2;
34957  bvect bv_s { 0, 100000, 100001, 1600000, 1600001 };
34958  bvect bv_i { 0, 100000, 100001, 200000, 1000000, 1600000, 1600001 };
34959  bvect bv_sr;
34960 
34962 
34964  bv_i.build_rs_index(&bc);
34965 
34966  for (unsigned i = 0; i < 2; ++i)
34967  {
34968  rc.compress(bv1, bv_i, bv_s);
34969  assert(bv1.count() == bv_s.count());
34970 
34971  rc.decompress(bv_sr, bv_i, bv1);
34972  cmp = bv_sr.compare(bv_s);
34973  if (cmp != 0)
34974  {
34975  DetailedCompareBVectors(bv_sr, bv_s);
34976  }
34977  assert(cmp == 0);
34978 
34979  rc.compress_by_source(bv2, bv_i, bc, bv_s);
34980  assert(bv2.count() == bv_s.count());
34981 
34982  cmp = bv2.compare(bv1);
34983  assert(cmp == 0);
34984 
34985  rc.decompress(bv_sr, bv_i, bv2);
34986  cmp = bv_sr.compare(bv_s);
34987  assert(cmp == 0);
34988 
34989 
34990  bv_i.optimize();
34991  bv_s.optimize();
34992  bv_i.build_rs_index(&bc);
34993  }
34994  }
34995  std::cout << "basic test OK..." << std::endl;
34996 
34997 
34998  {
34999  std::cout << "Stress rank compression..." << std::endl;
35001  unsigned test_count = 10;
35002  unsigned bv_size = 1000000;
35003  for (unsigned i = 0; i < test_count; ++i)
35004  {
35005  if (bv_size > 40000000)
35006  break;
35007  cout << "target size = " << bv_size << " " << endl;
35008  bvect bv_i, bv_s, bv_sr;
35009  generate_bvector(bv_i, bv_size);
35010  generate_bvector(bv_s, bv_size);
35011  bv_i |= bv_s;
35012 
35013  assert(bv_i.count() >= bv_s.count());
35014 
35016  bv_i.build_rs_index(&bc);
35017 
35018  bvect bv1, bv2;
35019 
35020  for (unsigned j = 0; j < 2; ++ j)
35021  {
35022  {
35023  chrono_taker<std::ostream> ct(cout, "c1");
35024  rc.compress(bv1, bv_i, bv_s);
35025  rc.decompress(bv_sr, bv_i, bv1);
35026  }
35027  assert(bv1.count() == bv_s.count());
35028  cmp = bv_sr.compare(bv_s);
35029  assert(cmp == 0);
35030 
35031  {
35032  chrono_taker<std::ostream> ct(cout, "c2");
35033  rc.compress_by_source(bv2, bv_i, bc, bv_s);
35034  rc.decompress(bv_sr, bv_i, bv2);
35035  }
35036  assert(bv2.count() == bv_s.count());
35037  cmp = bv_sr.compare(bv_s);
35038  assert(cmp == 0);
35039 
35040  cmp = bv2.compare(bv1);
35041  if (cmp!=0)
35042  {
35043  DetailedCompareBVectors(bv1, bv2);
35044  exit(1);
35045  }
35046  assert(cmp == 0);
35047 
35048  {
35050  bvect bv_subset;
35051  rsub.sample(bv_subset, bv_s, 100);
35052  {
35053  chrono_taker<std::ostream> ct(cout, "c1-1");
35054  rc.compress(bv1, bv_i, bv_subset);
35055  rc.decompress(bv_sr, bv_i, bv1);
35056  }
35057  assert(bv1.count() == bv_subset.count());
35058  cmp = bv_sr.compare(bv_subset);
35059  assert(cmp == 0);
35060 
35061  {
35062  chrono_taker<std::ostream> ct(cout, "c2-2");
35063  rc.compress_by_source(bv2, bv_i, bc, bv_subset);
35064  rc.decompress(bv_sr, bv_i, bv2);
35065  }
35066  assert(bv2.count() == bv_subset.count());
35067 
35068  cmp = bv2.compare(bv1);
35069  assert(cmp == 0);
35070 
35071  cmp = bv_sr.compare(bv_subset);
35072  assert(cmp == 0);
35073  }
35074 
35075 
35076  bv_i.optimize();
35077  bv_s.optimize();
35078  bv_i.build_rs_index(&bc);
35079 
35080  } // for j
35081  cout << "\n" << i << " of " << test_count << " " << endl;
35082 
35083  bv_size += bv_size;
35084  } // for i
35085  std::cout << endl << "Stress rank compression... OK" << std::endl;
35086  }
35087 
35088 
35089  cout << " ------------------------------ Test Rank Compressor OK " << endl;
35090 }
35091 
35092 
35093 template<typename SV>
35094 void GenerateSV(SV& sv, unsigned strategy = 0)
35095 {
35096  using value_type = typename SV::value_type;
35097 
35098  unsigned max_idx_value = 1000000;
35099  switch (strategy)
35100  {
35101  case 0:
35102  {
35103  cout << "SV Ultra sparse generation" << endl;
35104  for (unsigned i = 0; i < max_idx_value;)
35105  {
35106  value_type v = (unsigned)(rand() * rand()) % 650000;
35108  {
35109  sv[i] = v;
35110  }
35111  else
35112  {
35113  if (v & 1)
35114  sv[i] = - v;
35115  else
35116  sv[i] = v;
35117  }
35118  i += 10000 + (unsigned)rand() % 65535;
35119  }
35120  break;
35121  }
35122  case 1:
35123  {
35124  cout << "SV Dense intervals generation 1" << endl;
35125  for (unsigned i = 0; i < max_idx_value;)
35126  {
35127  value_type v = (unsigned)(rand() * rand()) % 650000;
35128  for (unsigned j = 0; i < max_idx_value; ++i, ++j)
35129  {
35131  {
35132  sv[i] = v + j;
35133  }
35134  else
35135  {
35136  sv[i] = int(j)-v;
35137  }
35138  if (j > 256)
35139  break;
35140  }
35141  i += 20000 + (unsigned)rand() % 65535;
35142  }
35143  break;
35144  }
35145  case 2:
35146  {
35147  cout << "SV Dense intervals generation 2" << endl;
35148  value_type v = (unsigned)(rand() * rand()) % 650000;
35149  for (unsigned i = 0; i < max_idx_value/4; ++i)
35150  {
35151  sv[i] = v;
35152  }
35153 
35154  for (unsigned i = 0; i < max_idx_value;)
35155  {
35156  v = unsigned(rand() * rand()) % 650000;
35157  for (unsigned j = 0; i < max_idx_value; ++i, ++j)
35158  {
35160  sv[i] = v + i;
35161  else
35162  sv[i] = - int(unsigned(v) + i);
35163  if (j > 256)
35164  break;
35165  }
35166  i += 30000 + unsigned(rand()) % 65535;
35167  }
35168  break;
35169  }
35170  case 3:
35171  {
35172  cout << "SV random generation" << endl;
35173  unsigned rand_max = (unsigned)rand() % 300000;
35174  for (unsigned i = 0; i < rand_max; ++i)
35175  {
35176  value_type v = value_type(rand() * rand());
35177  unsigned idx = unsigned(rand()) % max_idx_value;
35179  sv[idx] = v;
35180  else
35181  sv[idx] = -v;
35182 
35183  if (i % 2 == 0)
35184  {
35185  sv.clear(idx, true);
35186  }
35187  }
35188  break;
35189  }
35190  case 4:
35191  {
35192  cout << "SV empty generation" << endl;
35193  unsigned idx = unsigned(rand()) % max_idx_value;
35194  sv[idx] = 25557890;
35195  sv.clear(idx, true);
35196  }
35197  break;
35198  case 5:
35199  {
35200  cout << "SV uniform power 2 value generation" << endl;
35201  value_type v = 8;//unsigned(rand()) % 64;
35202  for (unsigned i = 0; i < max_idx_value; ++i)
35203  {
35204  sv[i] = 0-v;
35205  }
35206  }
35207  break;
35208  case 6:
35209  {
35210  cout << "SV uniform power 2+1 value generation" << endl;
35211  value_type v = 16+1;
35212  for (unsigned i = 0; i < max_idx_value; ++i)
35213  {
35215  sv[i] = v;
35216  else
35217  sv[i] = 0-v;
35218 
35219  }
35220  }
35221  break;
35222  case 7:
35223  {
35224  cout << "SV liner growth value generation" << endl;
35225  for (unsigned i = 0; i < max_idx_value; ++i)
35226  {
35228  sv[i] = i;
35229  else
35230  sv[i] = -int(i);
35231  }
35232  }
35233  break;
35234  default:
35235  break;
35236  } // switch
35237  sv.optimize();
35238 }
35239 
35240 template<class CSV, class SV>
35241 void DetailedCompareSparseVectors(const CSV& csv,
35242  const SV& sv)
35243 {
35244  SV sv_s(bm::use_null); // sparse vector decompressed
35245 
35246  // de-compression test
35247  csv.load_to(sv_s);
35248  /*
35249  if (!sv.equal(sv_s))
35250  {
35251  cerr << "compressed vector load_to (decompression) failed!" << endl;
35252  exit(1);
35253  }
35254  */
35255 
35256 
35257  size_t csv_size = csv.size();
35258  size_t sv_size = sv.size();
35259  size_t sv_s_size = sv_s.size();
35260 
35261  using bvector_type = typename SV::bvector_type;
35262  using value_type = typename SV::value_type;
35263  using size_type = typename SV::size_type;
35264 
35265  const bvector_type* bv_null_sv = sv.get_null_bvector();
35266  const bvector_type* bv_null_sv_s = sv_s.get_null_bvector();
35267  const bvector_type* bv_null_csv = csv.get_null_bvector();
35268 
35269  if (csv_size != sv_size || sv_s_size != sv_size)
35270  {
35271  assert(bv_null_sv != bv_null_csv);
35272 
35273  auto cnt_sv = bv_null_sv->count();
35274  auto cnt_sv_s = bv_null_sv_s->count();
35275  auto cnt_csv = bv_null_csv->count();
35276 
35277  if (cnt_sv != cnt_csv)
35278  {
35279  cerr << "Sparse compressed vector comparison failed (size check):"
35280  << "csv.size()=" << csv_size
35281  << "sv.size()=" << sv_size
35282  << "cnt sv = " << cnt_sv
35283  << "cnt csv = " << cnt_csv
35284  << endl;
35285  assert(0); exit(1);
35286  }
35287  if (cnt_sv_s != cnt_csv)
35288  {
35289  cerr << "Restored Sparse vector comparison failed (size check):"
35290  << "csv.size()=" << csv_size
35291  << "sv_s.size()=" << sv_s_size
35292  << "cnt sv = " << cnt_sv
35293  << "cnt csv = " << cnt_csv
35294  << endl;
35295  assert(0); exit(1);
35296  }
35297  }
35298 
35299  for (unsigned i = 0; i < sv_size; ++i)
35300  {
35301  bool is_null_sv = sv.is_null(i);
35302  bool is_null_sv_s = sv_s.is_null(i);
35303  bool is_null_csv = csv.is_null(i);
35304  if (is_null_sv != is_null_csv || is_null_sv != is_null_sv_s)
35305  {
35306  cerr << "Detailed csv check failed (null mismatch) at i=" << i
35307  << " sv=" << is_null_sv
35308  << " sv_s=" << is_null_sv_s
35309  << " csv=" << is_null_csv
35310  << endl;
35311  int cmp = bv_null_sv->compare(*bv_null_csv);
35312  if (cmp != 0)
35313  {
35314  cerr << "1. cmp=" << cmp << endl;
35315  assert(0);exit(1);
35316  }
35317  cmp = bv_null_sv->compare(*bv_null_sv_s);
35318  if (cmp != 0)
35319  {
35320  cerr << "2. cmp=" << cmp << endl;
35321  assert(0);exit(1);
35322  }
35323  assert(0); exit(1);
35324  }
35325  value_type v1c{0};
35326  bool found = csv.try_get(i, v1c);
35327  assert(is_null_csv == !found);
35328 
35329  if (!is_null_sv)
35330  {
35331  value_type v1 = sv[i];
35332  value_type v1_s = sv_s[i];
35333  value_type v2 = csv[i];
35334 
35335  if (v1 != v2 || v1_s != v1 || v1c != v1)
35336  {
35337  cerr << "Detailed csv check failed (value mismatch) at i=" << i
35338  << " v1=" << v1
35339  << " v1_s=" << v1_s
35340  << " v2=" << v2
35341  << " v1c=" << v1c
35342  << endl;
35343  assert(0); exit(1);
35344  }
35345  }
35346  }
35347 
35348  {
35351  bm::sparse_vector_serialize<CSV>(csv, sv_lay, tb);
35352  CSV csv1;
35353  const unsigned char* buf = sv_lay.buf();
35355 
35356  if (!csv.equal(csv1))
35357  {
35358  cerr << "Compressed sparse vector serialization comparison failed!" << endl;
35359 
35360  size_type pos;
35361  bool b = bm::sparse_vector_find_first_mismatch(csv, csv1, pos);
35362  assert(b);
35363  cerr << "Mismatch at: " << pos << endl;
35364 
35366  bm::sparse_vector_serialize<CSV>(csv, sv_lay1);
35367 
35369 
35370  exit(1);
35371  }
35372  }
35373 
35374 }
35375 
35376 template<typename CSV>
35377 void CheckCompressedDecode(const CSV& csv,
35378  unsigned from, unsigned size)
35379 {
35380  using value_type = typename CSV::value_type;
35381  std::vector<value_type> vect, vect2, vect_tmp;
35382  vect.resize(size);
35383  vect2.resize(size);
35384  vect_tmp.resize(size);
35385 
35386  unsigned sz = csv.decode(&vect[0], from, size);
35387  unsigned sz2 = csv.decode_buf(&vect2[0], &vect_tmp[0], from, size);
35388  assert(sz == sz2);
35389 
35390  {
35391  typename CSV::const_iterator it = csv.get_const_iterator(from);
35392 
35393  unsigned ex_idx = 0;
35394  for (unsigned i = from; i < from + sz; ++i)
35395  {
35396  value_type v = csv.get(i);
35397  value_type vx = vect[ex_idx];
35398  value_type vx2 = vect[ex_idx];
35399  auto vx_it = *it;
35400 
35401  //rsc_sparse_vector_u32::const_iterator it2 = csv.get_const_iterator(i);
35402  //auto vx_it2 = it2.value();
35403 
35404  if (v != vx || v != vx2 || v != vx_it /*|| vx_it != vx_it2*/)
35405  {
35406  cerr << "compressed vector decode mismatch from="
35407  << from << " i=" << i
35408  << " v=" << v << " vx=" << vx << " vx2=" << vx2
35409  << " vx_it = " << vx_it
35410  << " ex_idx=" << ex_idx
35411  << endl;
35412  /*
35413  vx_it = *it;
35414  rsc_sparse_vector_u32::const_iterator it2 = csv.get_const_iterator(i);
35415  vx_it = it2.value();
35416 
35417  std::vector<unsigned> vect3;
35418  vect3.resize(1024);
35419  csv.decode(&vect3[0], i , 1024);
35420  assert(vect3[0] == v);
35421  assert(v == vx_it); */
35422  assert(0); exit(1);
35423  }
35424  ++ex_idx;
35425  ++it;
35426  }
35427  }
35428 }
35429 
35430 template<typename CSV>
35431 void DetailedCheckCompressedDecode(const CSV& csv)
35432 {
35433  auto size = csv.size();
35434  cout << endl;
35435 
35436  {
35437  unsigned size1 = 100;
35438  for (unsigned i = 0; i < size1; )
35439  {
35440  CheckCompressedDecode(csv, i, size);
35441  if (!is_silent)
35442  if (i % 128 ==0)
35443  cout << "\r" << i << "/" << size1 << flush;
35444  i++;
35445  }
35446  }
35447  cout << endl;
35448 
35449  {
35450  unsigned size1 = 100000;
35451  for (unsigned i = 0; i < size1; )
35452  {
35453  CheckCompressedDecode(csv, i, size1);
35454  if (!is_silent)
35455  cout << "\r" << i << "/" << size1 << flush;
35456  i+=(unsigned)rand()%3;
35457  size1 -= (unsigned)rand()%5;
35458  }
35459  }
35460  cout << endl;
35461 
35462  {
35463  unsigned size1 = size;
35464  for (unsigned i = size-size/2; i < size1; )
35465  {
35466  CheckCompressedDecode(csv, i, size1);
35467  if (!is_silent)
35468  cout << "\r" << i << "/" << size1 << flush;
35469  i+=(1+i);
35470  }
35471  }
35472  cout << endl;
35473 
35474  for (unsigned i = size-size/2; i < size; )
35475  {
35476  CheckCompressedDecode(csv, i, size);
35477  if (!is_silent)
35478  cout << "\r" << i << "/" << size << flush;
35479  i += (unsigned)rand() % 25000;
35480  }
35481  cout << endl;
35482 
35483  for (unsigned i = size-size/2; i < size; )
35484  {
35485  if (size <= i)
35486  break;
35487  CheckCompressedDecode(csv, i, size);
35488  if (!is_silent)
35489  cout << "\r" << i << "/" << size << flush;
35490  i += (unsigned)rand() % 25000;
35491  size -= (unsigned)rand() % 25000;;
35492  }
35493  cout << endl;
35494 
35495 }
35496 
35497 static
35499 {
35500  cout << " ------------------------------ Test buffers " << endl;
35501 
35504 
35505  {
35506  hmatrix hm;
35507  hm.init();
35508  hm.set_zero();
35509 
35510  for (unsigned i = 0; i < hm.rows(); ++i)
35511  {
35512  const unsigned* r = hm.row(i);
35513  for (unsigned j = 0; j < hm.cols(); ++j)
35514  {
35515  assert(r[j] == 0);
35516  }
35517  }
35518  }
35519 
35520  {
35521  hmatrix hm(true);
35522 
35523  for (unsigned i = 0; i < hm.rows(); ++i)
35524  {
35525  unsigned* r = hm.row(i);
35526  for (unsigned j = 0; j < hm.cols(); ++j)
35527  {
35528  r[j] = i;
35529  }
35530  }
35531 
35532  for (unsigned i = 0; i < hm.rows(); ++i)
35533  {
35534  const unsigned* r = hm.row(i);
35535  for (unsigned j = 0; j < hm.cols(); ++j)
35536  {
35537  assert(r[j] == i);
35538  }
35539  }
35540  }
35541 
35542  {
35543  hmatrix_str hm(true);
35544  hm.set_zero();
35545 
35546  for (unsigned i = 0; i < hm.rows(); ++i)
35547  {
35548  char* r = hm.row(i);
35549  #if defined(_MSC_VER)
35550  ::strncpy_s(r, hm.cols(), "abcd", hm.cols());
35551  #else
35552  ::strncpy(r, "abcd", hm.cols());
35553  #endif
35554  }
35555  for (unsigned i = 0; i < hm.rows(); ++i)
35556  {
35557  const char* r = hm.row(i);
35558  int c = ::strcmp(r, "abcd");
35559  assert(c == 0);
35560  }
35561 
35562  }
35563 
35564 
35565  cout << " ------------------------------ Test buffers OK" << endl;
35566 }
35567 
35568 static
35570 {
35571  cout << " ------------------------------ Test Compressed Sparse Vector " << endl;
35572 
35573  {
35574  rsc_sparse_vector_u32 csv1;
35575  assert(csv1.size() == 0);
35576  assert(csv1.equal(csv1));
35577  rsc_sparse_vector_u32 csv2;
35578  assert(csv1.equal(csv2));
35579  rsc_sparse_vector_u32 csv3(csv1);
35580  assert(csv3.equal(csv2));
35581  }
35582 
35583 
35584  // test contributed by A.Shkeda
35585  {
35587  unsigned long long v0 = 93396ull | (237ull << 32);
35588  ins.set(2882526, v0);
35589 
35590  bool ex_flag = false;
35591  try {
35592  auto it = ins.begin(); // throws an exeption on missing RSC index
35593  auto it_end = ins.end();
35594  while (it != it_end) {
35595  if (it.is_null() == false) {
35596  auto v = it.value(); // CRASH! BOOM!
35597  assert(v == v0);
35598  }
35599  it.advance();
35600  }
35601  }
35602  catch (std::domain_error& )
35603  {
35604  ex_flag = true;
35605  }
35606  assert(ex_flag);
35607  }
35608 
35609  cout << " set test " << endl;
35610  {
35611  unsigned v;
35613 
35614  bool exists = csv.try_get(1000, v);
35615  assert(!exists);
35616 
35617  csv.set(1, 1);
35618  exists = csv.try_get(1000, v);
35619  assert(!exists);
35620 
35621  assert(csv.is_null(0));
35622  assert(!csv.is_null(1));
35623  assert((v=csv.get(1)) == 1);
35624  exists = csv.try_get(1, v);
35625  assert(exists && v == 1);
35626 
35627 
35628 
35629  csv.push_back(10, 11);
35630  csv.set(11, 12);
35631  assert(csv.get(11) == 12);
35632  exists = csv.try_get(11, v);
35633  assert(exists && v == 12);
35634 
35635  csv.set(5, 55);
35636  csv.set(5, 56);
35637 
35638  assert(csv.size() == 12);
35639  assert(csv.get(1) == 1);
35640  exists = csv.try_get(1, v);
35641  assert(exists && v == 1);
35642  assert(csv.get(10) == 11);
35643  exists = csv.try_get(10, v);
35644  assert(exists && v == 11);
35645  assert(csv.get(11) == 12);
35646  exists = csv.try_get(11, v);
35647  assert(exists && v == 12);
35648  assert(csv.get(5) == 56);
35649  exists = csv.try_get(5, v);
35650  assert(exists && v == 56);
35651 
35652 
35653  auto sz1 = csv.size();
35654  csv.set_null(5);
35655  assert(csv.is_null(5));
35656  assert(!csv.is_null(1));
35657  auto sz2 = csv.size();
35658  assert(sz1 == sz2);
35659  assert(csv.get(5) == 0);
35660 
35661 
35662  assert(csv.get(1) == 1);
35663  assert(csv.get(10) == 11);
35664  assert(csv.get(11) == 12);
35665  assert(csv.get(5) == 0);
35666 
35667  exists = csv.try_get(1000, v);
35668  assert(!exists);
35669 
35670  csv.optimize();
35671  csv.freeze();
35672  assert(csv.is_ro());
35673 
35674  csv.sync();
35675  exists = csv.try_get(1000, v);
35676  assert(!exists);
35677  exists = csv.try_get(11, v);
35678  assert(exists && v == 12);
35679  exists = csv.try_get(11, v);
35680  assert(exists && v == 12);
35681 
35682  }
35683 
35684  {
35685  cout << "push_back() test" << endl;
35686  unsigned v, v1;
35687 
35688  rsc_sparse_vector_u32 csv1;
35690 
35691  csv1.push_back(10, 100);
35692  assert(csv1.size() == 11);
35693  csv1.push_back(20, 200);
35694  csv1.push_back(21, 201);
35695 
35696  csv1.load_to(sv1);
35697 
35698  v = csv1.at(10);
35699  assert(v == 100);
35700  v1 = sv1.at(10);
35701  assert(v1 == 100);
35702 
35703  v = csv1.at(20);
35704  assert(v == 200);
35705  v1 = sv1.at(20);
35706  assert(v1 == 200);
35707 
35708  v = csv1.at(21);
35709  assert(v == 201);
35710  v1 = sv1.at(21);
35711  assert(v1 == 201);
35712 
35713  csv1.sync();
35714 
35715  DetailedCompareSparseVectors(csv1, sv1);
35716 
35717  v = csv1.at(10);
35718  assert(v == 100);
35719  v = csv1.at(20);
35720  assert(v == 200);
35721  v = csv1.at(21);
35722  assert(v == 201);
35723 
35724  csv1.optimize();
35725  v = csv1.at(10);
35726  assert(v == 100);
35727  v = csv1.at(20);
35728  assert(v == 200);
35729  v = csv1.at(21);
35730  assert(v == 201);
35731 
35732  rsc_sparse_vector_u32 csv2(csv1);
35733  bool same = csv2.equal(csv1);
35734  assert(same);
35735 
35736  rsc_sparse_vector_u32 csv3;
35737  csv3 = ::move(csv2);
35738  same = csv3.equal(csv1);
35739  assert(same);
35740 
35742  bm::id_t pos;
35743  bool found = scanner.find_eq(csv1, 201, pos);
35744  assert(found);
35745  assert(pos == 21);
35746 
35747  }
35748 
35749  // trailing NULLs and size() check
35750  {
35751  rsc_sparse_vector_u64 csv1;
35752  csv1.push_back(10, 10);
35753  assert(csv1.size() == 11);
35754  csv1.set_null(11);
35755  assert(csv1.size() == 12);
35756  csv1.optimize();
35757  csv1.sync();
35758  assert(csv1.size() == 12);
35759 
35760  auto sz0 = csv1.size();
35761  {
35762  auto bi = csv1.get_back_inserter();
35763  bi = 12;
35764  bi = 13;
35765  bi.flush();
35766  }
35767  auto sz = csv1.size();
35768  assert(sz == sz0+2);
35769  auto v = csv1.get(13);
35770  assert(v == 13);
35771  v = csv1.get(12);
35772  assert(v == 12);
35773 
35774  csv1.resize(0);
35775  sz = csv1.size();
35776  assert(sz == 0);
35777  bool b = csv1.is_null(12);
35778  assert(b);
35779  v = csv1.get(12);
35780  assert(v == 0);
35781 
35782  sz = csv1.size();
35783  assert(sz == 0);
35784 
35785  // test down resize
35786  csv1.push_back_null();
35787  sz = csv1.size(); assert(sz == 1);
35788  csv1.push_back(1);
35789  csv1.push_back(2);
35790  sz = csv1.size(); assert(sz == 3);
35791  csv1.push_back_null();
35792  sz = csv1.size(); assert(sz == 4);
35793  csv1.push_back(4);
35794  sz = csv1.size(); assert(sz == 5);
35795  csv1.push_back_null(2); // 5, 6
35796 
35797  sz = csv1.size();
35798  assert(sz == 7);
35799  csv1.resize(6);
35800  sz = csv1.size();assert(sz == 6);
35801 
35802  csv1.resize(4);
35803  sz = csv1.size(); assert(sz == 4);
35804  v = csv1.get(4);
35805  assert(v == 0);
35806 
35807  csv1.push_back(40);
35808  sz = csv1.size();assert(sz == 5);
35809  v = csv1.get(4);
35810  assert(v == 40);
35811  csv1.push_back(5);
35812  sz = csv1.size();assert(sz == 6);
35813  v = csv1.get(5);
35814  assert(v == 5);
35815 
35816  csv1.resize(4);
35817  sz = csv1.size();
35818  assert(sz == 4);
35819  v = csv1.get(4);
35820  assert(v == 0);
35821 
35822  }
35823 
35824  // resize tests
35825  {
35826  rsc_sparse_vector_u64 csv1;
35827  assert(csv1.size()==0);
35828  csv1.resize(0);
35829  assert(csv1.size()==0);
35830  csv1.push_back_null();
35831  assert(csv1.size()==1);
35832  csv1.resize(2);
35833  assert(csv1.size()==2);
35834  csv1.resize(5);
35835  assert(csv1.size()==5);
35836 
35837  csv1.push_back(5);
35838  assert(csv1.size()==6);
35839  assert(csv1.get(5) == 5);
35840  csv1.push_back_null(10);
35841  assert(csv1.size()==16);
35842 
35843  csv1.resize(6); // trim the NULL tail
35844  assert(csv1.size()==6);
35845  assert(csv1.get(5) == 5);
35846  }
35847 
35848  // bulk clear tests
35849  {
35851  csv1.push_back(1, 1);
35852  csv1.push_back(20, 200);
35853  csv1.push_back(21, 201);
35854 
35855  bvect bv { 1, 20, 21};
35856  csv1.clear(bv);
35857 
35858  const bvect* bv_null = csv1.get_null_bvector();
35859  auto c = bm::count_and(*bv_null, bv);
35860  assert(c==3);
35861  assert(!csv1.is_null(1));
35862 
35863  assert(csv1.get(1)==0);
35864  assert(csv1.get(20)==0);
35865  assert(csv1.get(21)==0);
35866  }
35867  {
35869  csv1.push_back(1, 1);
35870  csv1.push_back(20, 200);
35871  csv1.push_back(21, 201);
35872  csv1.push_back(51, 65535);
35873 
35874  bvect bv { 1, 2, 21};
35875  csv1.clear(bv);
35876 
35877  const bvect* bv_null = csv1.get_null_bvector();
35878  auto c = bm::count_and(*bv_null, bv);
35879  assert(c==2);
35880  c = bv_null->count();
35881  assert(c == 4);
35882  assert(!csv1.is_null(1));
35883 
35884  assert(csv1.get(1)==0);
35885  assert(csv1.get(20)==200);
35886  assert(csv1.get(21)==0);
35887  assert(csv1.get(51)==65535);
35888 
35889  // gather
35890 
35891  bvect::size_type idx[5] = {51, 0, 20, }, buf[5];
35892  unsigned v[5];
35893  csv1.gather(&v[0], &idx[0], &buf[0], 3, bm::BM_UNKNOWN);
35894  assert(v[0] == 65535);
35895  assert(v[1] == 0);
35896  assert(v[2] == 200);
35897  assert(buf[0] != bm::id_max);
35898  assert(buf[1] == bm::id_max);
35899  assert(buf[2] != bm::id_max);
35900 
35901  }
35902 
35903  // bulk set_null
35904  {
35906  csv1.push_back(1, 1);
35907  csv1.push_back(20, 200);
35908  csv1.push_back(21, 201);
35909 
35910  bvect bv0 { 0, 2, 3, 23};
35911  csv1.set_null(bv0);
35912 
35913  const bvect* bv_null = csv1.get_null_bvector();
35914  auto c = bv_null->count();
35915  assert(c==3);
35916  assert(csv1.get(20)==200);
35917  assert(csv1.get(21)==201);
35918 
35919  bvect bv { 0, 1, 20, 21};
35920  csv1.set_null(bv);
35921 
35922  c = bv_null->count();
35923  assert(c==0);
35924  }
35925 
35926  {
35928  csv1.push_back(1, 1);
35929  csv1.push_back(20, 200);
35930  csv1.push_back(21, 201);
35931  csv1.optimize();
35932 
35933  const bvect* bv_null = csv1.get_null_bvector();
35934 
35935  bvect bv { 0, 10, 20};
35936  csv1.set_null(bv);
35937 
35938  auto c = bv_null->count();
35939  assert(c==2);
35940 
35941  assert(csv1.get(1)==1);
35942  assert(csv1.get(20)==0);
35943  assert(csv1.get(21)==201);
35944 
35945  }
35946 
35947  cout << " testing count_range_notnull()..." << endl;
35948  {
35949  rsc_sparse_vector_u32 csv1;
35950  auto cnt = csv1.count_range_notnull(0, 10);
35951  assert(!cnt);
35952 
35953  for (unsigned i=0; i<1000; ++i)
35954  {
35955  csv1.push_back(i, 3);
35956  }
35957  for (unsigned i=1000; i<100000000; i+=7)
35958  {
35959  csv1.push_back(i, 3);
35960  }
35961 //csv1.optimize();
35962 //csv1.sync();
35963 
35964  for (unsigned pass = 0; pass < 2; ++pass)
35965  {
35966  cout << "\nPASS=" << pass << endl;
35967  cnt = csv1.count_range_notnull(0, 0);
35968  assert(cnt==1);
35969  cnt = csv1.count_range_notnull(0, 9);
35970  assert(cnt==10);
35971 
35972  auto sz = csv1.size();
35973  auto last_r = sz;
35974  for (unsigned j = 0; j < sz; ++j, --sz)
35975  {
35976  cnt = csv1.count_range_notnull(j, sz);
35977  const bvect* bv = csv1.get_null_bvector();
35978  auto c = bv->count_range(j, sz);
35979  assert(cnt == c);
35980 
35981  const auto* rs_idx = csv1.get_RS();
35982  if (rs_idx)
35983  {
35984  auto c2 = bv->count_range(j, sz, *rs_idx);
35985  assert(c2 == cnt);
35986  }
35987 
35988  auto r = sz - j;
35989  if ((last_r - r) > 1024)
35990  {
35991  if (!is_silent)
35992  cout << "\r" << r << " " << flush;
35993  last_r = r;
35994  }
35995 
35996  if (r > 65536 && j > 65536)
35997  {
35998  j+= (unsigned)rand()%256;
35999  sz -= (unsigned)rand()%256;
36000  }
36001  }
36002  csv1.optimize();
36003  csv1.sync();
36004  } // for pass
36005  cout << endl;
36006  }
36007  cout << "OK" << endl;
36008 
36009  cout << " rsc_sparse_vector<>::const_iterator tests.." << endl;
36010  {
36011  {
36012  rsc_sparse_vector_u32 csv1;
36013  {
36015  assert(!it.valid());
36016  }
36017  csv1.push_back(0, 100);
36018  csv1.push_back(2, 200);
36019 
36020  csv1.sync();
36021  {
36024  assert(it2.valid());
36025 
36026  assert(it.valid());
36027  assert(*it == 100);
36028  bool b = it.advance();
36029  assert(b);
36030  assert(*it == 0);
36031  assert(it.is_null());
36032  ++it;
36033  assert(it.valid());
36034  assert(it.value() == 200);
36035  }
36036  }
36037  }
36038 
36039 
36040  cout << " back inserter tests" << endl;
36041  {
36042  rsc_sparse_vector_u32 csv1;
36043  {
36045  rs_bi.add_null();
36046  rs_bi.add(1);
36047  rs_bi.add(2);
36048  rs_bi.flush();
36049  }
36050  assert(csv1.size() == 3);
36051  auto v = csv1.get(0);
36052  assert(v == 0);
36053  assert(csv1.is_null(0));
36054  v = csv1.at(1);
36055  assert(v == 1);
36056  v = csv1.at(2);
36057  assert(v == 2);
36058  }
36059 
36060  {
36061  rsc_sparse_vector_u32 csv1;
36062  {
36064  rs_bi.add(1);
36065  rs_bi.add(2);
36066  rs_bi.add_null();
36067  rs_bi.add(3);
36068  rs_bi.flush();
36069  }
36070  assert(csv1.size() == 4);
36071  auto v = csv1.at(0);
36072  assert(v == 1);
36073  v = csv1.at(1);
36074  assert(v == 2);
36075  v = csv1.get(2);
36076  assert(v == 0);
36077  assert(csv1.is_null(2));
36078  v = csv1.at(3);
36079  assert(v == 3);
36080 
36081  // test copy-range
36082  {
36083  rsc_sparse_vector_u32 csv2;
36084  csv2.copy_range(csv1, 4, 5);
36085  assert(csv2.size() == 0);
36086 
36087  csv2.copy_range(csv1, 0, 0);
36088  assert(csv2.size() == 4);
36089  v = csv2.at(0);
36090  assert(v == 1);
36091 
36092  csv2.copy_range(csv1, 1, 2);
36093  assert(csv2.size() == 4);
36094  v = csv2[0];
36095  assert(v == 0);
36096 
36097  v = csv2.at(1);
36098  assert(v == 2);
36099  v = csv2.get(2);
36100  assert(v == 0);
36101  assert(csv2.is_null(2));
36102  }
36103 
36104  }
36105 
36106 
36107  {
36108  rsc_sparse_vector_u32 csv1;
36109  {
36111  for (unsigned i = 0; i < 100000; i++)
36112  {
36113  if (i&1)
36114  {
36115  rs_bi.add_null();
36116  }
36117  else
36118  {
36119  rs_bi.add(i);
36120  }
36121  }
36122  rs_bi.flush();
36123  }
36124  csv1.optimize();
36125 
36126  // validation
36127  for (unsigned i = 0; i < 100000; i++)
36128  {
36129  if (i&1)
36130  {
36131  assert(csv1.is_null(i));
36132  }
36133  else
36134  {
36135  assert(!csv1.is_null(i));
36136  auto v = csv1[i];
36137  assert(v == i);
36138  }
36139  }
36140  }
36141 
36142 
36143  {
36144  cout << "decode() tests" << endl;
36145 
36146  {
36147  unsigned arr[10];
36148  unsigned arr1[10];
36149  unsigned arr2[10];
36150  rsc_sparse_vector_u32 csv1;
36151 
36152  csv1.push_back(5, 1);
36153  csv1.push_back(6, 1);
36154  csv1.push_back(8, 2);
36155 
36156  csv1.push_back(100, 4);
36157  csv1.sync();
36158 
36159  auto sz = csv1.decode(&arr[0], 100, 1);
36160  assert(sz==1);
36161  assert(arr[0] == 4);
36162 
36163  auto sz2 = csv1.decode_buf(&arr1[0], &arr2[0], 100, 1);
36164  assert(sz2==1);
36165  assert(arr1[0] == 4);
36166 
36167 
36168  csv1.set_null(100);
36169  csv1.sync(true);
36170 
36171  sz = csv1.decode(&arr[0], 100, 1);
36172  assert(sz == 0);
36173 
36174  sz2 = 2;
36175  sz2 = csv1.decode_buf(&arr1[0], &arr2[0], 100, 1);
36176  if (sz2)
36177  {
36178  cout << sz2 << endl;
36179  }
36180  assert(sz2==0);
36181  }
36182 
36183  cout << "inc() and merge_not_null() tests" << endl;
36184  {
36185  rsc_sparse_vector_u32::bvector_type bv { 1, 2, 10, 200, bm::id_max/2, bm::id_max-1 };
36186  rsc_sparse_vector_u32 csv1(bv);
36187  rsc_sparse_vector_u32 csv2(bv);
36188 
36189  csv1.sync(); csv2.sync();
36190 
36191  csv1.inc(1);
36192  csv1.inc(2, 10);
36193 
36194  csv2.set(200, 7);
36195  csv2.inc(bm::id_max/2);
36196  csv2.inc(bm::id_max/2, 1);
36197  csv2.inc(bm::id_max-1, 255);
36198 
36199  csv1.merge_not_null(csv2);
36200 
36201  assert(csv1.in_sync());
36202 
36203  assert(csv1.get(1) == 1);
36204  assert(csv1.get(2) == 10);
36205  assert(csv1.get(200) == 7);
36206  assert(csv1.get(bm::id_max/2) == 2);
36207  assert(csv1.get(bm::id_max-1) == 255);
36208  }
36209 
36210  {
36212  unsigned to = 65536*2;
36213  bv.set_range(1, to);
36214 
36215  for (unsigned i = 1; i < to; ++i)
36216  {
36217  rsc_sparse_vector_u32 csv1(bv);
36218  rsc_sparse_vector_u32 csv2(bv);
36219  csv1.sync(); csv2.sync();
36220 
36221  for (unsigned i0 = 1; i0 < i; ++i0)
36222  {
36223  csv1.set(i0, i0);
36224  csv1.inc(i0, i0);
36225  }
36226  for (unsigned i1 = i+1; i1 < 65536*2; ++i1)
36227  {
36228  csv2.set(i1, i1);
36229  csv2.inc(i1, i1);
36230  }
36231  csv1.merge_not_null(csv2);
36232  assert(csv1.in_sync());
36233 
36234  for (unsigned i0 = 1; i0 < i; ++i0)
36235  {
36236  unsigned v = csv1.get(i0);
36237  assert(csv1.get(i0) == i0*2);
36238 
36239  unsigned N_bits = (unsigned) rand()%31;
36240  if (N_bits)
36241  {
36242  auto u = csv1.get_unsigned_bits(i0, N_bits);
36243  unsigned mask1 = ~0u >> (32 - N_bits);
36244  auto v_masked = v & mask1;
36245  assert(u == v_masked);
36246  }
36247 
36248  }
36249  for (unsigned i1 = i+1; i1 < 65536*2; ++i1)
36250  {
36251  assert(csv1.get(i1) == i1*2);
36252  }
36253  assert(csv1.get(i) == 0);
36254 
36255  if (!is_silent)
36256  if (i % 100 == 0)
36257  cout << "\r" << i << " / " << to << flush;
36258 
36259  } // for
36260  cout << endl;
36261  }
36262 
36263  cout << "random assignmnet in sync() mode...." << endl;
36264  {
36265  bvect bv { 10, 20, 100, 200, bm::id_max/2, bm::id_max-1 };
36266 
36267  bvect::size_type first, last, mid;
36268  bv.find_range(first, last);
36269  mid = first + ((last - first) / 4);
36270 
36271  rsc_sparse_vector_u32 csv1;
36272  rsc_sparse_vector_u32 csv2(bv);
36273  {
36274  bvect::enumerator en = bv.get_enumerator(mid);
36275  for (;en.valid(); ++en)
36276  {
36277  auto idx = *en;
36278  csv1.set(idx, idx);
36279  csv1.inc(idx);
36280  }
36281  en.go_to(0);
36282  for (;en.valid(); ++en)
36283  {
36284  auto idx = *en;
36285  if (idx >= mid)
36286  break;
36287  csv1.set(idx, idx);
36288  csv1.inc(idx);
36289  }
36290  assert(!csv1.in_sync());
36291  }
36292  {
36293  csv2.sync();
36294  bvect::enumerator en = bv.get_enumerator(mid);
36295  for (;en.valid(); ++en)
36296  {
36297  auto idx = *en;
36298  csv2.set(idx, idx);
36299  csv2.inc(idx);
36300  }
36301 
36302  en.go_to(0);
36303  for (;en.valid(); ++en)
36304  {
36305  auto idx = *en;
36306  if (idx >= mid)
36307  break;
36308  csv2.set(idx, idx);
36309  csv2.inc(idx);
36310  }
36311  assert(csv2.in_sync());
36312 
36313  }
36314  bool eq = csv1.equal(csv2);
36315  if (!eq)
36316  {
36317  cerr << "Error: rsc_sparse_vector() add values check failed" << endl;
36318  assert(0); exit(1);
36319  }
36320  }
36321 
36322  cout << "random assignment in sync() mode.... [stress]" << endl;
36323  {
36324  bvect bv;
36325  generate_bvector(bv);
36326  bv.optimize();
36327 
36328  bvect::size_type first, last, mid;
36329  bv.find_range(first, last);
36330  mid = first + ((last - first) / 4);
36331 
36332  rsc_sparse_vector_u32 csv1;
36333  rsc_sparse_vector_u32 csv2(bv);
36334  {
36335  bvect::enumerator en = bv.get_enumerator(mid);
36336  for (;en.valid(); ++en)
36337  {
36338  auto idx = *en;
36339  csv1.set(idx, idx & 0xFF);
36340  csv1.inc(idx);
36341 
36342  }
36343  csv1.optimize();
36344  en.go_to(0);
36345  for (;en.valid(); ++en)
36346  {
36347  auto idx = *en;
36348  if (idx >= mid)
36349  break;
36350  csv1.set(idx, idx & 0xFF);
36351  csv1.inc(idx);
36352  }
36353  csv1.optimize();
36354  }
36355  // sync mode
36356  {
36357  csv2.sync();
36358  bvect::enumerator en = bv.get_enumerator(mid);
36359  for (;en.valid(); ++en)
36360  {
36361  auto idx = *en;
36362  csv2.set(idx, idx & 0xFF);
36363  csv2.inc(idx);
36364  }
36365  assert(csv2.in_sync());
36366  csv2.optimize();
36367 
36368  en.go_to(0);
36369  for (;en.valid(); ++en)
36370  {
36371  auto idx = *en;
36372  if (idx >= mid)
36373  break;
36374  csv2.set(idx, idx & 0xFF);
36375  csv2.inc(idx);
36376  }
36377  assert(csv2.in_sync());
36378  csv2.optimize();
36379 
36380  }
36381  bool eq = csv1.equal(csv2);
36382  if (!eq)
36383  {
36384  cerr << "Error: rsc_sparse_vector() add values check failed" << endl;
36385  assert(0); exit(1);
36386  }
36387  }
36388 
36389 
36390 
36391  {
36392  rsc_sparse_vector_u32 csv1;
36393 
36394  csv1.push_back(5, 1);
36395  csv1.push_back(6, 1);
36396  csv1.push_back(8, 2);
36397  csv1.push_back(255, 4);
36398 
36399  csv1.sync();
36400 
36401  for (unsigned k = 0; k < 2; ++k)
36402  {
36403  CheckCompressedDecode(csv1, 0, 1);
36404  CheckCompressedDecode(csv1, 0, 2);
36405  CheckCompressedDecode(csv1, 1, 1);
36406 
36407  CheckCompressedDecode(csv1, 0, 5);
36408  CheckCompressedDecode(csv1, 0, 6);
36409 
36410  CheckCompressedDecode(csv1, 256, 1);
36411 
36412  for (unsigned i = 0; i < csv1.size(); ++i)
36413  {
36414  CheckCompressedDecode(csv1, i, 1);
36415  CheckCompressedDecode(csv1, i, csv1.size());
36416  }
36417  unsigned j = csv1.size();
36418  for (unsigned i = 0; i < csv1.size(); ++i, --j)
36419  {
36420  unsigned size = j - i;
36421  if (!size)
36422  break;
36423  CheckCompressedDecode(csv1, i, size);
36424  }
36425 
36426  csv1.optimize();
36427  }
36428  }
36429 
36430  {
36431  rsc_sparse_vector_u32 csv1;
36432 
36433  csv1.push_back(bm::id_max-1, 10);
36434  csv1.sync();
36435 
36436  for (unsigned k = 0; k < 2; ++k)
36437  {
36438  for (unsigned i = bm::id_max-20; i < csv1.size(); ++i)
36439  {
36440  CheckCompressedDecode(csv1, i, 1);
36441  CheckCompressedDecode(csv1, i, csv1.size()-i+10);
36442  }
36443  csv1.optimize();
36444  }
36445 
36446  }
36447  }
36448 
36449 
36450  // set stress test
36451  {
36452  cout << "RSC set stress..." << endl;
36453  std::vector<std::pair<unsigned, unsigned> > vect;
36455 
36456  const unsigned max_size = 2000000;
36457 
36458  cout << "Test set generation." << endl;
36459  for (unsigned i = 0; i < max_size; i+=2)
36460  {
36461  std::pair<unsigned, unsigned> pr(i, i+10);
36462  vect.push_back(pr);
36463  } // for
36464 
36465  {
36466  std::random_device rd;
36467  std::mt19937 g(rd());
36468  std::shuffle(vect.begin(), vect.end(), g);
36469  }
36470 
36471  cout << "RSC set() " << endl;
36472  unsigned i = 0;
36473  for (auto rit = vect.rbegin(); rit != vect.rend(); ++rit)
36474  {
36475  std::pair<unsigned, unsigned> pr = *rit;
36476  csv.set(pr.first, pr.second);
36477  unsigned v = csv[pr.first];
36478  assert(v == pr.second);
36479 
36480  if (i % 4096 == 0)
36481  {
36482  if (!is_silent)
36483  cout << "\r" << pr.first << "/" << max_size << flush;
36484  csv.optimize();
36485  }
36486 
36487  ++i;
36488  } // for
36489 
36490  cout << "\nRSC verification..." << endl;
36491 
36492  csv.optimize();
36493  csv.sync();
36494  i = 0;
36495  for (i = 0; i < vect.size(); ++i)
36496  {
36497  const std::pair<unsigned, unsigned>& pr = vect[i];
36498  unsigned v = csv[pr.first];
36499  assert(v == pr.second);
36500  if (!is_silent)
36501  if (i % 4096 == 0)
36502  cout << "\r" << pr.first << "/" << max_size << flush;
36503  } // for
36504 
36505  cout << "\nRSC set null..." << endl;
36506 
36507  i = 0;
36508  for (auto rit = vect.rbegin(); rit != vect.rend(); ++rit)
36509  {
36510  std::pair<unsigned, unsigned> pr = *rit;
36511  csv.set_null(pr.first);
36512  assert(csv.is_null(pr.first));
36513  if (i % 4096 == 0)
36514  {
36515  if (!is_silent)
36516  cout << "\r" << i << "/" << max_size << flush;
36517  csv.optimize();
36518  }
36519  ++i;
36520  } // for
36521 
36522  cout << "\nOK" << endl;
36523  }
36524 
36525  {
36526  cout << "load() test" << endl;
36527  unsigned v;
36529  rsc_sparse_vector_u32 csv1;
36530  rsc_sparse_vector_u32 csv2;
36531 
36532  sv1.set(10, 9);
36533  sv1.set(20, 200);
36534  sv1.set(21, 201);
36535  sv1.set(100, 65535);
36536  sv1.clear(100, true);
36537 
36538  csv1.load_from(sv1);
36539  assert(csv1.size() == 22);
36540  csv1.sync();
36541 
36542  csv2.push_back(10, 9);
36543  csv2.push_back(20, 200);
36544  csv2.push_back(21, 201);
36545  csv2.sync();
36546 
36547 
36548  v = csv1.at(10);
36549  assert(v == 9);
36550  v = csv1.at(20);
36551  assert(v == 200);
36552  v = csv1.at(21);
36553  assert(v == 201);
36554 
36555  bool same = csv1.equal(csv2);
36556  assert(same);
36557 
36558  DetailedCompareSparseVectors(csv1, sv1);
36559 
36560  rsc_sparse_vector_u32 csv4;
36561  csv4 = std::move(csv1);
36562  v = csv4.at(10);
36563  assert(v == 9);
36564  DetailedCompareSparseVectors(csv4, sv1);
36565 
36566  rsc_sparse_vector_u32 csv5(std::move(csv4));
36567  v = csv5.at(10);
36568  assert(v == 9);
36569  DetailedCompareSparseVectors(csv5, sv1);
36570 
36571  }
36572 
36573  {
36574  cout << "------ Compressed load() stress test" << endl;
36576  for (unsigned i = 0; i < 10; ++i)
36577  {
36578  cout << "\nPass " << i << endl;
36579 
36581  rsc_sparse_vector_u32 csv1;
36582 
36583  GenerateSV(sv, i);
36584 
36585 
36586  csv1.load_from(sv);
36587  csv1.sync();
36588 
36589  cout << "cmp 1...";
36590  DetailedCompareSparseVectors(csv1, sv);
36592  cout << "ok" << endl;
36593 
36594  cout << "cmp 2...";
36595  csv1.optimize(tb);
36596  DetailedCompareSparseVectors(csv1, sv);
36598  cout << "ok" << endl;
36599 
36600  cout << "cmp 3...";
36601  csv1.clear();
36602 
36603  sv.optimize(tb);
36604  rsc_sparse_vector_u32 csv2;
36605  csv2.load_from(sv);
36606  DetailedCompareSparseVectors(csv2, sv);
36607  csv1.sync();
36609 
36610  csv2.optimize(tb);
36611  csv2.sync();
36612 
36613  DetailedCompareSparseVectors(csv2, sv);
36615  cout << "ok" << endl;
36616 
36617  cout << "cmp 4...";
36618  {
36619  rsc_sparse_vector_u32 csv3(csv2);
36620  DetailedCompareSparseVectors(csv3, sv);
36621  }
36622  cout << "ok" << endl;
36623 
36624  cout << "cmp 5...";
36625  {
36626  rsc_sparse_vector_u32 csv4;
36627  csv4 = std::move(csv2);
36628  DetailedCompareSparseVectors(csv4, sv);
36629 
36630  rsc_sparse_vector_u32 csv5(std::move(csv4));
36631  DetailedCompareSparseVectors(csv5, sv);
36632  }
36633  cout << "ok" << endl;
36634  } // for
36635  cout << "Compressed load() stress test OK" << endl;
36636 
36637 
36638  }
36639 
36640 
36641  cout << " ------------------------------ Test Compressed Sparse Vector OK" << endl;
36642 }
36643 
36644 static
36646 {
36647  cout << " ------------------------------ Test Compressed Sparse Vector " << endl;
36648 
36649  {
36650  rsc_sparse_vector_i32 csv1;
36651  assert(csv1.size() == 0);
36652  assert(csv1.equal(csv1));
36653  rsc_sparse_vector_i32 csv2;
36654  assert(csv1.equal(csv2));
36655  rsc_sparse_vector_i32 csv3(csv1);
36656  assert(csv3.equal(csv2));
36657  }
36658 
36659 
36660  cout << " set test " << endl;
36661  {
36663  csv.set(1, -1);
36664  assert(csv.is_null(0));
36665  assert(!csv.is_null(1));
36666  auto v = csv.get(1);
36667  assert(v == -1);
36668 
36669  csv.push_back(10, -11);
36670  csv.set(11, -12);
36671  v = csv.get(10);
36672  assert(v == -11);
36673  v = csv.get(11);
36674  assert(v == -12);
36675 
36676  csv.set(5, 55);
36677  csv.set(5, -56);
36678 
36679  assert(csv.size() == 12);
36680  assert(csv.get(1) == -1);
36681  assert(csv.get(10) == -11);
36682  assert(csv.get(11) == -12);
36683  assert(csv.get(5) == -56);
36684 
36685  csv.set_null(5);
36686  assert(csv.is_null(5));
36687  assert(csv.get(1) == -1);
36688  assert(csv.get(10) == -11);
36689  assert(csv.get(11) == -12);
36690  assert(csv.get(5) == 0);
36691  }
36692 
36693  {
36694  cout << "push_back() test" << endl;
36695  int v, v1;
36696 
36697  rsc_sparse_vector_i32 csv1;
36699 
36700  csv1.push_back(10, -100);
36701  assert(csv1.size() == 11);
36702  csv1.push_back(20, -200);
36703  csv1.push_back(21, 201);
36704 
36705  csv1.load_to(sv1);
36706 
36707  v = csv1.at(10);
36708  assert(v == -100);
36709  v1 = sv1.at(10);
36710  assert(v1 == -100);
36711 
36712  v = csv1.at(20);
36713  assert(v == -200);
36714  v1 = sv1.at(20);
36715  assert(v1 == -200);
36716 
36717  v = csv1.at(21);
36718  assert(v == 201);
36719  v1 = sv1.at(21);
36720  assert(v1 == 201);
36721 
36722  csv1.sync();
36723 
36724  DetailedCompareSparseVectors(csv1, sv1);
36725 
36726  v = csv1.at(10);
36727  assert(v == -100);
36728  v = csv1.at(20);
36729  assert(v == -200);
36730  v = csv1.at(21);
36731  assert(v == 201);
36732 
36733  csv1.optimize();
36734  v = csv1.at(10);
36735  assert(v == -100);
36736  v = csv1.at(20);
36737  assert(v == -200);
36738  v = csv1.at(21);
36739  assert(v == 201);
36740 
36741  rsc_sparse_vector_i32 csv2(csv1);
36742  bool same = csv2.equal(csv1);
36743  assert(same);
36744 
36745  rsc_sparse_vector_i32 csv3;
36746  csv3 = ::move(csv2);
36747  same = csv3.equal(csv1);
36748  assert(same);
36749 
36751  bm::id_t pos;
36752  bool found = scanner.find_eq(csv1, 201, pos);
36753  assert(found);
36754  assert(pos == 21);
36755  }
36756 
36757 
36758  cout << "rsc_sparse_vector<>::const_iterator tests" << endl;
36759  {
36760  {
36761  rsc_sparse_vector_i32 csv1;
36762  {
36764  assert(!it.valid());
36765  }
36766  csv1.push_back(0, 100);
36767  csv1.push_back(2, -200);
36768 
36769  csv1.sync();
36770  {
36773  assert(it2.valid());
36774 
36775  assert(it.valid());
36776  assert(*it == 100);
36777  bool b = it.advance();
36778  assert(b);
36779  assert(*it == 0);
36780  assert(it.is_null());
36781  ++it;
36782  assert(it.valid());
36783  assert(it.value() == -200);
36784  }
36785  }
36786  }
36787 
36788 
36789  cout << " back inserter tests" << endl;
36790  {
36791  rsc_sparse_vector_i32 csv1;
36792  {
36794  rs_bi.add_null();
36795  rs_bi.add(1);
36796  rs_bi.add(-2);
36797  rs_bi.flush();
36798  }
36799  assert(csv1.size() == 3);
36800  auto v = csv1.get(0);
36801  assert(v == 0);
36802  assert(csv1.is_null(0));
36803  v = csv1.at(1);
36804  assert(v == 1);
36805  v = csv1.at(2);
36806  assert(v == -2);
36807  }
36808 
36809  {
36810  rsc_sparse_vector_i32 csv1;
36811  {
36813  rs_bi.add(-1);
36814  rs_bi.add(2);
36815  rs_bi.add_null();
36816  rs_bi.add(-3);
36817  rs_bi.flush();
36818  }
36819  assert(csv1.size() == 4);
36820  auto v = csv1.at(0);
36821  assert(v == -1);
36822  v = csv1.at(1);
36823  assert(v == 2);
36824  v = csv1.get(2);
36825  assert(v == 0);
36826  assert(csv1.is_null(2));
36827  v = csv1.at(3);
36828  assert(v == -3);
36829 
36830  // test copy-range
36831  {
36832  rsc_sparse_vector_i32 csv2;
36833  csv2.copy_range(csv1, 4, 5);
36834  assert(csv2.size() == 0);
36835 
36836  csv2.copy_range(csv1, 0, 0);
36837  assert(csv2.size() == 4);
36838  v = csv2.at(0);
36839  assert(v == -1);
36840 
36841  csv2.copy_range(csv1, 1, 2);
36842  assert(csv2.size() == 4);
36843  v = csv2[0];
36844  assert(v == 0);
36845 
36846  v = csv2.at(1);
36847  assert(v == 2);
36848  v = csv2.get(2);
36849  assert(v == 0);
36850  assert(csv2.is_null(2));
36851  }
36852  }
36853 
36854 
36855  {
36856  rsc_sparse_vector_i32 csv1;
36857  {
36859  for (int i = 0; i < 100000; i++)
36860  {
36861  if (i&1)
36862  {
36863  rs_bi.add_null();
36864  }
36865  else
36866  {
36867  if (i & 1)
36868  rs_bi.add(-i);
36869  else
36870  rs_bi.add(i);
36871  }
36872  }
36873  rs_bi.flush();
36874  }
36875  csv1.optimize();
36876 
36877  // validation
36878  for (int i = 0; i < 100000; i++)
36879  {
36880  if (i&1)
36881  {
36882  assert(csv1.is_null(unsigned(i)));
36883  }
36884  else
36885  {
36886  assert(!csv1.is_null(unsigned(i)));
36887  auto v = csv1[unsigned(i)];
36888  if (i & 1)
36889  {
36890  assert(v == -i);
36891  }
36892  else
36893  {
36894  assert(v == i);
36895  }
36896  }
36897  }
36898  }
36899 
36900 
36901  {
36902  cout << "decode() tests" << endl;
36903 
36904  {
36905  int arr[10];
36906  int arr1[10];
36907  int arr2[10];
36908  rsc_sparse_vector_i32 csv1;
36909 
36910  csv1.push_back(5, 1);
36911  csv1.push_back(6, -1);
36912  csv1.push_back(8, 2);
36913 
36914  csv1.push_back(100, -4);
36915  csv1.sync();
36916 
36917  auto sz = csv1.decode(&arr[0], 100, 1);
36918  assert(sz==1);
36919  assert(arr[0] == -4);
36920 
36921  auto sz2 = csv1.decode_buf(&arr1[0], &arr2[0], 100, 1);
36922  assert(sz2==1);
36923  assert(arr1[0] == -4);
36924 
36925 
36926  csv1.set_null(100);
36927  csv1.sync(true);
36928 
36929  sz = csv1.decode(&arr[0], 100, 1);
36930  assert(sz == 0);
36931 
36932  sz2 = 2;
36933  sz2 = csv1.decode_buf(&arr1[0], &arr2[0], 100, 1);
36934  if (sz2)
36935  {
36936  cout << sz2 << endl;
36937  }
36938  assert(sz2==0);
36939  }
36940 
36941  cout << "inc() and merge_not_null() tests" << endl;
36942  {
36943  rsc_sparse_vector_i32::bvector_type bv { 1, 2, 10, 200, bm::id_max/2, bm::id_max-1 };
36944  rsc_sparse_vector_i32 csv1(bv);
36945  rsc_sparse_vector_i32 csv2(bv);
36946 
36947  csv1.sync(); csv2.sync();
36948 
36949  csv1.inc(1);
36950  csv1.inc(2, -10);
36951 
36952 
36953  csv2.set(200, -7);
36954  csv2.inc(200);
36955  csv2.inc(bm::id_max/2);
36956  csv2.inc(bm::id_max/2, 1);
36957  csv2.inc(bm::id_max-1, 255);
36958 
36959  csv1.merge_not_null(csv2);
36960 
36961  assert(csv1.in_sync());
36962 
36963  assert(csv1.get(1) == 1);
36964  assert(csv1.get(2) == -10);
36965  int v = csv1.get(200);
36966  assert(v == -6);
36967  assert(csv1.get(bm::id_max/2) == 2);
36968  assert(csv1.get(bm::id_max-1) == 255);
36969 
36970  csv1.inc(200, INT_MAX);
36971  v = csv1.get(200);
36972  assert(v == INT_MAX-6);
36973  }
36974 
36975 
36976  cout << "random assignmnet in sync() mode...." << endl;
36977  {
36978  bvect bv { 10, 20, 100, 200, bm::id_max/4 };
36979 
36980  bvect::size_type first, last, mid;
36981  bv.find_range(first, last);
36982  mid = first + ((last - first) / 4);
36983 
36984  rsc_sparse_vector_i32 csv1;
36985  rsc_sparse_vector_i32 csv2(bv);
36986  {
36987  bvect::enumerator en = bv.get_enumerator(mid);
36988  for (;en.valid(); ++en)
36989  {
36990  auto idx = *en;
36991  csv1.set(idx, -(int)idx);
36992  csv1.inc(idx);
36993  }
36994  en.go_to(0);
36995  for (;en.valid(); ++en)
36996  {
36997  auto idx = *en;
36998  if (idx >= mid)
36999  break;
37000  csv1.set(idx, -int(idx));
37001  csv1.inc(idx);
37002  }
37003  assert(!csv1.in_sync());
37004  }
37005  {
37006  csv2.sync();
37007  bvect::enumerator en = bv.get_enumerator(mid);
37008  for (;en.valid(); ++en)
37009  {
37010  auto idx = *en;
37011  csv2.set(idx, -int(idx));
37012  csv2.inc(idx);
37013  }
37014 
37015  en.go_to(0);
37016  for (;en.valid(); ++en)
37017  {
37018  auto idx = *en;
37019  if (idx >= mid)
37020  break;
37021  csv2.set(idx, -int(idx));
37022  csv2.inc(idx);
37023  }
37024  assert(csv2.in_sync());
37025 
37026  }
37027  bool eq = csv1.equal(csv2);
37028  if (!eq)
37029  {
37030  cerr << "Error: rsc_sparse_vector() add values check failed" << endl;
37031  assert(0); exit(1);
37032  }
37033  }
37034 
37035  cout << "random assignment in sync() mode.... [stress]" << endl;
37036  {
37037  bvect bv;
37038  generate_bvector(bv, 1000000);
37039  bv.optimize();
37040 
37041  bvect::size_type first, last, mid;
37042  bv.find_range(first, last);
37043  mid = first + ((last - first) / 4);
37044 
37045  rsc_sparse_vector_i32 csv1;
37046  rsc_sparse_vector_i32 csv2(bv);
37047  {
37048  bvect::enumerator en = bv.get_enumerator(mid);
37049  for (;en.valid(); ++en)
37050  {
37051  auto idx = *en;
37052  csv1.set(idx, -int(idx & 0xFF));
37053  csv1.inc(idx);
37054 
37055  }
37056  csv1.optimize();
37057  en.go_to(0);
37058  for (;en.valid(); ++en)
37059  {
37060  auto idx = *en;
37061  if (idx >= mid)
37062  break;
37063  csv1.set(idx, -int(idx & 0xFF));
37064  csv1.inc(idx);
37065  }
37066  csv1.optimize();
37067  }
37068  // sync mode
37069  {
37070  csv2.sync();
37071  bvect::enumerator en = bv.get_enumerator(mid);
37072  for (;en.valid(); ++en)
37073  {
37074  auto idx = *en;
37075  csv2.set(idx, -int(idx & 0xFF));
37076  csv2.inc(idx);
37077  }
37078  assert(csv2.in_sync());
37079  csv2.optimize();
37080 
37081  en.go_to(0);
37082  for (;en.valid(); ++en)
37083  {
37084  auto idx = *en;
37085  if (idx >= mid)
37086  break;
37087  csv2.set(idx, -int(idx & 0xFF));
37088  csv2.inc(idx);
37089  }
37090  assert(csv2.in_sync());
37091  csv2.optimize();
37092 
37093  }
37094  bool eq = csv1.equal(csv2);
37095  if (!eq)
37096  {
37097  cerr << "Error: rsc_sparse_vector() add values check failed" << endl;
37098  assert(0); exit(1);
37099  }
37100  }
37101 
37102  {
37103  rsc_sparse_vector_i32 csv1;
37104 
37105  csv1.push_back(5, 1);
37106  csv1.push_back(6, -1);
37107  csv1.push_back(8, -2);
37108  csv1.push_back(255, 4);
37109 
37110  csv1.sync();
37111 
37112  for (unsigned k = 0; k < 2; ++k)
37113  {
37114  CheckCompressedDecode(csv1, 0, 1);
37115  CheckCompressedDecode(csv1, 0, 2);
37116  CheckCompressedDecode(csv1, 1, 1);
37117 
37118  CheckCompressedDecode(csv1, 0, 5);
37119  CheckCompressedDecode(csv1, 0, 6);
37120 
37121  CheckCompressedDecode(csv1, 256, 1);
37122 
37123  for (unsigned i = 0; i < csv1.size(); ++i)
37124  {
37125  CheckCompressedDecode(csv1, i, 1);
37126  CheckCompressedDecode(csv1, i, csv1.size());
37127  }
37128  unsigned j = csv1.size();
37129  for (unsigned i = 0; i < csv1.size(); ++i, --j)
37130  {
37131  unsigned size = j - i;
37132  if (!size)
37133  break;
37134  CheckCompressedDecode(csv1, i, size);
37135  }
37136 
37137  csv1.optimize();
37138  }
37139  }
37140 
37141  {
37142  rsc_sparse_vector_i32 csv1;
37143  csv1.push_back(bm::id_max/3-1, -10);
37144  csv1.sync();
37145 
37146  for (unsigned k = 0; k < 2; ++k)
37147  {
37148  for (unsigned i = bm::id_max/3-20; i < csv1.size(); ++i)
37149  {
37150  CheckCompressedDecode(csv1, i, 1);
37151  CheckCompressedDecode(csv1, i, csv1.size()-i+10);
37152  }
37153  csv1.optimize();
37154  }
37155 
37156  }
37157  }
37158 
37159 
37160  // set stress test
37161  {
37162  cout << "RSC set stress..." << endl;
37163  std::vector<std::pair<unsigned, unsigned> > vect;
37165 
37166  const unsigned max_size = 2000000;
37167 
37168  cout << "Test set generation." << endl;
37169  for (unsigned i = 0; i < max_size; i+=2)
37170  {
37171  std::pair<unsigned, unsigned> pr(i, i+10);
37172  vect.push_back(pr);
37173  } // for
37174 
37175  {
37176  std::random_device rd;
37177  std::mt19937 g(rd());
37178  std::shuffle(vect.begin(), vect.end(), g);
37179  }
37180 
37181  cout << "RSC set() " << endl;
37182  unsigned i = 0;
37183  for (auto rit = vect.rbegin(); rit != vect.rend(); ++rit)
37184  {
37185  std::pair<unsigned, unsigned> pr = *rit;
37186  csv.set(pr.first, pr.second);
37187  unsigned v = csv[pr.first];
37188  assert(v == pr.second);
37189 
37190  if (i % 4096 == 0)
37191  {
37192  cout << "\r" << pr.first << "/" << max_size << flush;
37193  csv.optimize();
37194  }
37195 
37196  ++i;
37197  } // for
37198 
37199  cout << "\nRSC verification..." << endl;
37200 
37201  csv.optimize();
37202  csv.sync();
37203  i = 0;
37204  for (i = 0; i < vect.size(); ++i)
37205  {
37206  const std::pair<unsigned, unsigned>& pr = vect[i];
37207  unsigned v = csv[pr.first];
37208  assert(v == pr.second);
37209  if (i % 4096 == 0)
37210  cout << "\r" << pr.first << "/" << max_size << flush;
37211  } // for
37212 
37213  cout << "\nRSC set null..." << endl;
37214 
37215  i = 0;
37216  for (auto rit = vect.rbegin(); rit != vect.rend(); ++rit)
37217  {
37218  std::pair<unsigned, unsigned> pr = *rit;
37219  csv.set_null(pr.first);
37220  assert(csv.is_null(pr.first));
37221  if (i % 4096 == 0)
37222  {
37223  cout << "\r" << i << "/" << max_size << flush;
37224  csv.optimize();
37225  }
37226  ++i;
37227  } // for
37228 
37229 
37230 
37231  cout << "\nOK" << endl;
37232  }
37233 
37234  {
37235  cout << "load() test" << endl;
37236  unsigned v;
37238  rsc_sparse_vector_u32 csv1;
37239  rsc_sparse_vector_u32 csv2;
37240 
37241  sv1.set(10, 9);
37242  sv1.set(20, 200);
37243  sv1.set(21, 201);
37244  sv1.set(100, 65535);
37245  sv1.clear(100, true);
37246 
37247  csv1.load_from(sv1);
37248  assert(csv1.size() == 22);
37249  csv1.sync();
37250 
37251  csv2.push_back(10, 9);
37252  csv2.push_back(20, 200);
37253  csv2.push_back(21, 201);
37254  csv2.sync();
37255 
37256 
37257  v = csv1.at(10);
37258  assert(v == 9);
37259  v = csv1.at(20);
37260  assert(v == 200);
37261  v = csv1.at(21);
37262  assert(v == 201);
37263 
37264  bool same = csv1.equal(csv2);
37265  assert(same);
37266 
37267  DetailedCompareSparseVectors(csv1, sv1);
37268 
37269  rsc_sparse_vector_u32 csv4;
37270  csv4 = std::move(csv1);
37271  v = csv4.at(10);
37272  assert(v == 9);
37273  DetailedCompareSparseVectors(csv4, sv1);
37274 
37275  rsc_sparse_vector_u32 csv5(std::move(csv4));
37276  v = csv5.at(10);
37277  assert(v == 9);
37278  DetailedCompareSparseVectors(csv5, sv1);
37279 
37280  }
37281 
37282  {
37283  cout << "------ Compressed load() stress test" << endl;
37285  for (unsigned i = 0; i < 10; ++i)
37286  {
37287  cout << "\nPass " << i << endl;
37288 
37290  rsc_sparse_vector_i32 csv1;
37291 
37292  GenerateSV(sv, i);
37293 
37294 
37295  csv1.load_from(sv);
37296  csv1.sync();
37297 
37298  cout << "cmp 1...";
37299  DetailedCompareSparseVectors(csv1, sv);
37301  cout << "ok" << endl;
37302 
37303  cout << "cmp 2...";
37304  csv1.optimize(tb);
37305  DetailedCompareSparseVectors(csv1, sv);
37307  cout << "ok" << endl;
37308 
37309  cout << "cmp 3...";
37310  csv1.clear();
37311 
37312  sv.optimize(tb);
37313  rsc_sparse_vector_i32 csv2;
37314  csv2.load_from(sv);
37315  DetailedCompareSparseVectors(csv2, sv);
37316  csv1.sync();
37318 
37319  csv2.optimize(tb);
37320  csv2.sync();
37321 
37322  DetailedCompareSparseVectors(csv2, sv);
37324  cout << "ok" << endl;
37325 
37326  cout << "cmp 4...";
37327  {
37328  rsc_sparse_vector_i32 csv3(csv2);
37329  DetailedCompareSparseVectors(csv3, sv);
37330  }
37331  cout << "ok" << endl;
37332 
37333  cout << "cmp 5...";
37334  {
37335  rsc_sparse_vector_i32 csv4;
37336  csv4 = std::move(csv2);
37337  DetailedCompareSparseVectors(csv4, sv);
37338 
37339  rsc_sparse_vector_i32 csv5(std::move(csv4));
37340  DetailedCompareSparseVectors(csv5, sv);
37341  }
37342  cout << "ok" << endl;
37343  } // for
37344  cout << "Compressed load() stress test OK" << endl;
37345 
37346 
37347  }
37348 
37349 
37350  cout << " ------------------------------ Test Compressed Sparse Signed Vector OK" << endl;
37351 }
37352 
37353 
37354 
37355 static
37357 {
37358  cout << " ------------------------------ TestCompressSparseVectorSerial()" << endl;
37359 
37360  // empty test
37361  {
37362  rsc_sparse_vector_u32 csv1;
37363  rsc_sparse_vector_u32 csv2;
37366  bm::sparse_vector_serialize<rsc_sparse_vector_u32>(csv1, sv_lay, tb);
37367  const unsigned char* buf = sv_lay.buf();
37368  auto sz = sv_lay.size();
37369  assert(sz == 2);
37370 
37372  sv_deserial.deserialize(csv2, buf);
37373  assert(csv2.size() == csv1.size());
37374  }
37375 
37376  {
37377  rsc_sparse_vector_u32 csv1;
37378  rsc_sparse_vector_u32 csv2;
37379  rsc_sparse_vector_u32 csv3;
37380  {
37382  rs_bi.add_null();
37383  rs_bi.add(1);
37384  rs_bi.add(2);
37385  rs_bi.add_null();
37386  rs_bi.add(4);
37387  rs_bi.add_null(2);
37388 
37389  rs_bi.flush();
37390  }
37391  {
37392  auto sz = csv1.size();
37393  csv1.sync();
37394  auto sz2 = csv1.size();
37395  assert(sz == sz2);
37396  csv1.sync_size();
37397  auto sz3 = csv1.size();
37398  assert(sz3 < sz);
37399  }
37400 
37403  bm::sparse_vector_serialize<rsc_sparse_vector_u32>(csv1, sv_lay, tb);
37404  const unsigned char* buf = sv_lay.buf();
37405 
37407  bv_mask.set(1);
37408  bv_mask.set(2);
37409  bv_mask.set(100);
37411  sv_deserial.deserialize(csv2, buf, bv_mask);
37412 
37413  assert(csv2.size() == csv1.size());
37414  assert(csv2.get(1) == 1);
37415  assert(csv2.get(2) == 2);
37416 
37417  {
37419  sv_deserial.deserialize(csv3, buf, bv_mask);
37420  assert(csv3.is_ro());
37421  bool eq = csv2.equal(csv3);
37422  assert(eq);
37424  }
37425 
37426  sv_deserial.deserialize(csv2, buf, 1, 2);
37427  assert(csv2.size() == csv1.size());
37428  assert(csv2.get(1) == 1);
37429  assert(csv2.get(2) == 2);
37430 
37431  {
37433  sv_deserial.deserialize(csv3, buf, 1, 2);
37434  assert(csv3.is_ro());
37435  bool eq = csv2.equal(csv3);
37436  assert(eq);
37438  }
37439 
37440 
37441  }
37442 
37443  cout << "\nRSC gather stress test ..." << endl;
37444  {
37445  rsc_sparse_vector_u32 csv1;
37446  rsc_sparse_vector_u32 csv2;
37447  rsc_sparse_vector_u32 csv3;
37448 
37450  rsc_sparse_vector_u32::size_type to = from + 257 * 65536;
37451 
37452  cout << " generation... " << endl;
37453  {
37455  rs_bi.add_null();
37456  rs_bi.add(1);
37457  rs_bi.add(2);
37458  rs_bi.add_null();
37459  rs_bi.add(4);
37460  rs_bi.add_null(from); // add many NULLs
37461 
37462  for (auto i = from; i < to; ++i)
37463  {
37464  rs_bi.add(i);
37465  rs_bi.add_null();
37466  }
37467  rs_bi.flush();
37468  }
37469  csv1.sync_size();
37470  /*
37471  {
37472  auto sz = csv1.size();
37473  csv1.sync();
37474  auto sz2 = csv1.size();
37475  assert(sz == sz2);
37476  csv1.sync_size();
37477  }
37478  */
37479  cout << " generation OK" << endl;
37480  {
37481  rsc_sparse_vector_u32 csv_range;
37482  csv_range.set(1, 10);
37483  csv_range.copy_range(csv1, from - 10, from-1);
37484  assert(csv_range.get(1) == 0);
37485 
37486  }
37488 
37490  sv_serializer.set_bookmarks(false);
37492 
37495 
37496  for (unsigned pass = 0; pass < 2; ++pass)
37497  {
37498  cout << "\nPASS=" << pass << endl;
37499  sv_serializer.serialize(csv1, sv_lay);
37500  const unsigned char* buf = sv_lay.buf();
37501 
37502  {
37503  rsc_sparse_vector_u32 csv_c;
37504  sv_deserial.deserialize(csv_c, buf);
37505  bool eq = csv1.equal(csv_c);
37506  assert(eq);
37507 
37508  sv_deserial_ro.deserialize(csv_c, buf);
37509  assert(csv_c.is_ro());
37510  eq = csv1.equal(csv_c);
37511  assert(eq);
37512  }
37513 
37514  size_t cnt = 0;
37515  auto j = to;
37516  for (auto i = from; i < j; ++cnt, ++i, --j)
37517  {
37518  bool eq;
37519 
37521  bv_mask.set_range(i, j);
37522 
37523  sv_deserial.deserialize(csv2, buf, bv_mask);
37524  assert(csv2.get(j + 1) == 0);
37525  csv2.sync();
37526 
37527  sv_deserial.deserialize_range(csv3, buf, i, j);
37528  eq = csv2.equal(csv3);
37529  assert(eq);
37530 
37531  sv_deserial_ro.deserialize(csv3, buf, i, j);
37532  assert(csv3.is_ro());
37533  eq = csv2.equal(csv3);
37534  assert(eq);
37535 
37536 
37537  {
37538  rsc_sparse_vector_u32 csv_range;
37539  csv_range.copy_range(csv1, i, j);
37540 
37543  csv_range, csv2, pos, bm::no_null);
37544  if (f)
37545  {
37546  cout << "Range = [" << i << ".." << j << "]" << endl;
37547  auto v2 = csv2.get(pos);
37548  auto rv1 = csv_range.get(pos);
37549  auto v = csv1.get(pos);
37550  auto v3 = csv3.get(pos);
37551 
37552  std::cerr << "Discrepancy at idx=" << pos << endl;
37553  std::cerr << v2 << "!=" << rv1 << endl;
37554  cerr << "v=" << v << endl;
37555  cerr << "v3=" << v3 << endl;
37556  //csv_range.copy_range(csv1, i, j);
37557  //sv_deserial.deserialize(csv2, buf, bv_mask);
37558 
37559  assert(0);
37560  }
37561 /*
37562  if (!cnt || (j - i) < 65536)
37563  {
37564  for (auto i0 = i; i0 < j; ++i0)
37565  {
37566  auto v1 = csv1[i0];
37567  auto v2 = csv2[i0];
37568  assert(v1 == v2);
37569  assert(csv1.is_null(i0) == csv2.is_null(i0));
37570  auto rv1 = csv_range[i0];
37571  assert(rv1 == v1);
37572  assert(csv_range.is_null(i0) == csv2.is_null(i0));
37573  } // for i0
37574  }
37575 */
37576 
37577  }
37578 // eq = csv2.equal(csv3);
37579 // assert(eq);
37580 
37581 
37582  if (!is_silent)
37583  cout << "\r " << (j-i) << " " << std::flush;
37584 
37585  csv1.sync();
37586 
37587  if (cnt < 512 || (j - i) < 65536/2)
37588  {
37589  continue;
37590  }
37591 
37592  // gallop to the end
37593  i += (unsigned)rand() % 65536;
37594  j -= (unsigned)rand() % 65536;
37595  } // for i
37596 
37597  cout << "\n bookmarks ON" << endl;
37598  sv_serializer.set_bookmarks(true, unsigned(rand()%64));
37599  } // for pass
37600 
37601 
37602  }
37603  cout << "\nOK" << endl;
37604 
37605 
37606  cout << " ------------------------------ TestCompressSparseVectorSerial() OK" << endl;
37607 }
37608 
37609 static
37611 {
37612  cout << " ------------------------------ TestHeapVector()" << endl;
37613 
37614  {
37616  hv.add() = ~10ull;
37617  hv.push_back(~0ull);
37618  assert(hv[0] == ~10ull);
37619  assert(hv[1] == ~0ull);
37620  }
37621 
37622  {
37624  for (unsigned i = 0; i < 65535; ++i)
37625  hv.push_back(i);
37626  for (unsigned i = 0; i < 65535; ++i)
37627  {
37628  assert(hv[i] == i);
37629  }
37630  }
37631 
37632  {
37634 
37635  bvect& bv0 = hv.add();
37636  bv0.set(1);
37637  bvect& bv1 = hv.add();
37638  bv1.set(2);
37639  assert(hv.size() == 2);
37640  assert(hv[1].test(2));
37641 
37642  hv.resize(1);
37643  assert(hv.size() == 1);
37644  assert(hv[0].test(1));
37645 
37646  hv.resize(2);
37647  assert(hv.size() == 2);
37648  assert(!hv[1].any());
37649 
37651  assert(hv2.size() == 2);
37652  assert(hv2[0].test(1));
37653  assert(!hv2[1].any());
37654 
37656  hv3.reserve(10);
37657  hv3 = hv;
37658  hv3[1].set(0);
37659  assert(hv3.size() == 2);
37660  assert(hv3.at(0).test(1));
37661  assert(hv3.at(1).any());
37662 
37664  hv4.swap(hv3);
37665  assert(hv3.size() == 0);
37666  }
37667 
37668  cout << " ------------------------------ TestHeapVector() OK" << endl;
37669 
37670 }
37671 
37672 static
37674 {
37675  cout << " ------------------------------ TestSQueue()" << endl;
37676 
37678 
37679  {
37680  squeue_type sq;
37681  assert(sq.empty());
37682  sq.reserve(1);
37683  assert(sq.empty());
37684 
37685  bool b;
37686  b = sq.try_push(100);
37687  assert(b);
37688  assert(!sq.empty());
37689 
37690  unsigned v;
37691  v = sq.front();
37692  assert(v == 100);
37693  sq.pop();
37694  assert(sq.empty());
37695 
37696  }
37697 
37698  {
37699  std::vector<unsigned> vect { 0, 1, 20, 23, 1, 10, 12 };
37700 
37701  squeue_type sq;
37702  sq.reserve(vect.size()/2);
37703  for (size_t i = 0; i < vect.size(); ++i)
37704  {
37705  bool b = sq.try_push(vect[i]);
37706  if (b)
37707  {
37708  auto v = sq.front();
37709  assert(v == vect[0]);
37710  }
37711  else
37712  {
37713  auto sz = sq.size();
37714  sq.reserve(sz + 1);
37715  --i; // retry
37716  }
37717  } // for
37718 
37719  for (size_t i = 0; i < vect.size(); ++i)
37720  {
37721  auto vv = vect.at(i);
37722  auto vq = sq.front();
37723  assert(vv == vq);
37724  sq.pop();
37725  }
37726  }
37727 
37728 
37729  cout << " ------------------------------ TestSQueue() OK" << endl;
37730 }
37731 
37732 static
37734 {
37735  cout << " ------------------------------ TestXOR_RefVector()" << endl;
37736 
37737  {
37739  assert(ref_vect.size() == 0);
37740 
37741  bvect bv1, bv2;
37742  ref_vect.add(&bv1, 10);
37743  ref_vect.add(&bv2, 15);
37744 
37745  assert(ref_vect.size() == 2);
37746  assert(ref_vect.get_bv(0) == &bv1);
37747  assert(ref_vect.get_bv(1) == &bv2);
37748  assert(ref_vect.get_row_idx(0) == 10);
37749  assert(ref_vect.get_row_idx(1) == 15);
37750 
37751  size_t idx = ref_vect.find(15);
37752  assert(idx == 1);
37753  idx = ref_vect.find(10);
37754  assert(idx == 0);
37755 
37756  idx = ref_vect.find(100);
37757  assert(idx == ref_vect.not_found());
37758 
37759  ref_vect.reset();
37760  assert(ref_vect.size() == 0);
37761  }
37762 
37763  cout << " ------------------------------ TestXOR_RefVector() OK" << endl;
37764 }
37765 
37766 // Test builder
37767 template<typename BV>
37769 {
37770  static int task_run(void* argp)
37771  {
37772  if (!argp)
37773  return 0;
37774  BV* bv = static_cast<BV*>(argp);
37775  unsigned t = bv->test(0);
37776  void* r(0);
37777  memcpy(&r, &t, sizeof(t));
37778  return 1;
37779  }
37780 
37782  {
37783  auto& tv = batch.get_task_vector();
37784  tv.push_back(bm::task_descr(task_run));
37785  tv.push_back(bm::task_descr(task_run));
37786  tv.push_back(bm::task_descr(task_run));
37787  }
37788 };
37789 
37790 static
37792 {
37793  std::cout << " ----------------------------- TestTasks() " << endl;
37794  {
37796  assert(tbatch.size() == 0);
37798  tbatch.get_task_vector();
37799  assert(tvect.empty());
37800  }
37801 
37802  {
37803  bvect bv; bv.set(0);
37805  (void)ttb;
37807  int ret = tdescr.func(tdescr.argp);
37808  assert(ret==1);
37809  }
37810 
37811  {
37814  assert(tbatch.size() == 0);
37815  ttb.build_batch(tbatch);
37816  auto sz = tbatch.size();
37817  assert(sz == 3);
37818  }
37819 
37820 
37821 
37822 
37823  std::cout << " ----------------------------- TestTasks() OK " << endl;
37824 }
37825 
37826 
37827 static
37829 {
37830  std::cerr
37831  << "BitMagic C++ stress test." << endl
37832  << "-h - help" << endl
37833  << "-llevel (or -ll) - low level tests" << endl
37834  << "-support (or -s) - support containers " << endl
37835  << "-bvbasic (or -bvb) - bit-vector basic " << endl
37836  << "-bvser - bit-vector serialization " << endl
37837  << "-bvops (-bvo, -bvl) - bit-vector logical operations" << endl
37838  << "-bvshift (or -bvs) - bit-vector shifts " << endl
37839  << "-rankc (or -rc) - rank-compress " << endl
37840  << "-agg (or -aggregator) - bm::aggregator " << endl
37841  << "-sv - test sparse vectors" << endl
37842  << "-csv - test compressed sparse vectors" << endl
37843  << "-strsv - test sparse vectors" << endl
37844  << "-cc - test compresses collections" << endl
37845  << "-ser - test all serialization" << endl
37846  << "-allsvser - test serailization of sparse vectors (all)" << endl
37847  << "-sort - sparse vector sort tests" << endl
37848  << endl
37849  << "-silent -run without excessive progress report prints"
37850  << endl;
37851  ;
37852 }
37853 
37854 bool is_all = true;
37855 bool is_low_level = false;
37856 bool is_support = false;
37857 bool is_bvbasic = false;
37858 bool is_bvb0 = false;
37859 bool is_bvb1 = false;
37860 bool is_bvser = false;
37861 bool is_bvops = false;
37862 bool is_bvops0 = false;
37863 bool is_bvops1 = false;
37864 bool is_bvops2 = false;
37865 
37866 bool is_bvshift = false;
37867 bool is_rankc = false;
37868 bool is_agg = false;
37869 bool is_sv = false;
37870 bool is_sv0 = false;
37871 bool is_sv1 = false;
37872 bool is_csv = false;
37873 bool is_csv0 = false;
37874 bool is_csv1 = false;
37875 
37876 bool is_str_sv = false;
37877 bool is_c_coll = false;
37878 bool is_ser = false;
37879 bool is_allsvser = false;
37880 bool is_sv_sort = false;
37881 
37882 static
37883 int parse_args(int argc, char *argv[])
37884 {
37885  for (int i = 1; i < argc; ++i)
37886  {
37887  std::string arg = argv[i];
37888  if ((arg == "-h"))
37889  {
37890  show_help();
37891  return 1;
37892  }
37893  if (arg == "-ll" || arg == "-llevel")
37894  {
37895  is_all = false;
37896  is_low_level = true;
37897  continue;
37898  }
37899  if (arg == "-s" || arg == "-support")
37900  {
37901  is_all = false;
37902  is_support = true;
37903  continue;
37904  }
37905  if (arg == "-bvb" || arg == "-bvbasic")
37906  {
37907  is_all = false;
37908  is_bvbasic = true;
37909  continue;
37910  }
37911  if (arg == "-bvb0" )
37912  {
37913  is_all = false;
37914  is_bvb0 = true;
37915  continue;
37916  }
37917  if (arg == "-bvb1" )
37918  {
37919  is_all = false;
37920  is_bvb1 = true;
37921  continue;
37922  }
37923  if (arg == "-ser")
37924  {
37925  is_all = false;
37926  is_ser = true;
37927  continue;
37928  }
37929  if (arg == "-allsvser")
37930  {
37931  is_all = false;
37932  is_allsvser = true;
37933  continue;
37934  }
37935  if (arg == "-bvser")
37936  {
37937  is_all = false;
37938  is_bvser = true;
37939  continue;
37940  }
37941  if (arg == "-bvo" || arg == "-bvops" || arg == "-bvl")
37942  {
37943  is_all = false;
37944  is_bvops = true;
37945  continue;
37946  }
37947  if (arg == "-bvl0" || arg == "-bvops0")
37948  {
37949  is_all = false;
37950  is_bvops0 = true;
37951  continue;
37952  }
37953  if (arg == "-bvl1" || arg == "-bvops1")
37954  {
37955  is_all = false;
37956  is_bvops1 = true;
37957  continue;
37958  }
37959  if (arg == "-bvl2" || arg == "-bvops2")
37960  {
37961  is_all = false;
37962  is_bvops2 = true;
37963  continue;
37964  }
37965 
37966  if (arg == "-bvs" || arg == "-bvshift")
37967  {
37968  is_all = false;
37969  is_bvshift = true;
37970  continue;
37971  }
37972  if (arg == "-rc" || arg == "-rankc")
37973  {
37974  is_all = false;
37975  is_rankc = true;
37976  continue;
37977  }
37978  if (arg == "-agg" || arg == "-aggregator")
37979  {
37980  is_all = false;
37981  is_agg = true;
37982  continue;
37983  }
37984 
37985  if (arg == "-sv")
37986  {
37987  is_all = false;
37988  is_sv = true;
37989  continue;
37990  }
37991  if (arg == "-sv0")
37992  {
37993  is_all = false;
37994  is_sv0 = true;
37995  continue;
37996  }
37997  if (arg == "-sv1")
37998  {
37999  is_all = false;
38000  is_sv1 = true;
38001  continue;
38002  }
38003 
38004  if (arg == "-csv" || arg == "-rsc")
38005  {
38006  is_all = false;
38007  is_csv = true;
38008  continue;
38009  }
38010  if (arg == "-csv0")
38011  {
38012  is_all = false;
38013  is_csv0 = true;
38014  continue;
38015  }
38016  if (arg == "-csv1")
38017  {
38018  is_all = false;
38019  is_csv1 = true;
38020  continue;
38021  }
38022 
38023  if (arg == "-strsv" || arg == "-svstr")
38024  {
38025  is_all = false;
38026  is_str_sv = true;
38027  continue;
38028  }
38029  if (arg == "-cc")
38030  {
38031  is_all = false;
38032  is_c_coll = true;
38033  continue;
38034  }
38035  if (arg == "-sort" || arg == "--sort")
38036  {
38037  is_all = false;
38038  is_sv_sort = true;
38039  continue;
38040  }
38041  if (arg == "-silent" || arg == "--silent")
38042  {
38043  is_silent = true;
38044  continue;
38045  }
38046 
38047  } // for i
38048  return 0;
38049 }
38050 
38051 
38052 inline
38053 void PrintStacks(unsigned max_cnt = 10)
38054 {
38055 (void)max_cnt;
38056 #ifdef MEM_DEBUG
38057  #ifdef BM_STACK_COLL
38058  unsigned cnt(0);
38059  for (auto it = g_alloc_trace_map.begin();
38060  it != g_alloc_trace_map.end() && cnt < max_cnt; ++it)
38061  {
38062  cout << "\n--------------------STACK_TRACE: " << cnt++ << endl;
38063  cout << it->second << endl;
38064  }
38065  #endif
38066 #else
38067  cout << "Stack tracing not enabled (use #define BM_STACK_COLL)" << endl;
38068 #endif
38069 }
38070 
38071 static
38072 bool CheckAllocLeaks(bool details = false, bool abort = true)
38073 {
38074 (void)details; (void)abort;
38075 #ifdef MEM_DEBUG
38076  if (details)
38077  {
38078  cout << "[-------------- Allocation digest -------------------]" << endl;
38079  cout << "Number of BLOCK allocations = " << dbg_block_allocator::na_ << endl;
38080  cout << "Number of PTR allocations = " << dbg_ptr_allocator::na_ << endl << endl;
38081  }
38082 
38083  if (dbg_block_allocator::balance() != 0)
38084  {
38085  cout << "ERROR! Block memory leak! " << endl;
38086  cout << "leaked blocks: " << dbg_block_allocator::balance() << endl;
38087  PrintStacks();
38088  if (!abort)
38089  return true;
38090 
38091  assert(0);exit(1);
38092  }
38093 
38094  if (dbg_ptr_allocator::balance() != 0)
38095  {
38096  cout << "ERROR! Ptr memory leak! " << endl;
38097  cout << "leaked blocks: " << dbg_ptr_allocator::balance() << endl;
38098  PrintStacks();
38099  if (!abort)
38100  return true;
38101  assert(0);exit(1);
38102  }
38103  cout << "[------------ Debug Allocation balance OK ----------]" << endl;
38104 #endif
38105  return false;
38106 }
38107 
38108 template<class STR_SV, class HASH_SV>
38109 void ReadTestData(STR_SV& str_sv,
38110  HASH_SV& sv_hash,
38111  unsigned hash_bitcnt,
38112  const string& fname)
38113 {
38114  std::ifstream fin(fname.c_str(), std::ios::in);
38115  if (!fin.good())
38116  return;
38117  unsigned shift = sizeof(unsigned) * 8 - hash_bitcnt;
38118 
38119  auto bi(str_sv.get_back_inserter());
38120 // bi.set_remap(true);
38121  bi.set_optimize(bvect::opt_free_01); // minimal optimization
38122 // bi.set_optimize(bvect::opt_none); // no optimization
38123 
38124  auto bi_h(sv_hash.get_back_inserter());
38125 
38126  std::string line;
38127  for (unsigned i = 0; std::getline(fin, line); ++i)
38128  {
38129  if (line.empty())
38130  continue;
38131  bi = line;
38132  unsigned hash = 0;
38133  hash >>= shift; // reduce hash size
38134  bi_h = hash;
38135 
38136  if (i%1000000 == 0)
38137  cout << "\r" << i/1000000 << "M" << flush;
38138 if (i > 1000000 * 80)
38139  break;
38140 
38141  } // for
38142 
38143  bi.flush();
38144  bi_h.flush();
38145 }
38146 
38147 /*
38148 size_t NcbiStreamToString(string* str, ifstream& is, size_t pos=0)
38149  {
38150  if (!is.good()) {
38151  // Can't extract anything
38152  if (str)
38153  str->resize(pos);
38154 // is.setstate(NcbiFailbit);
38155  return 0;
38156  }
38157 
38158  char buf[5120];
38159  size_t buf_size = sizeof(buf);
38160  size_t str_size;
38161 
38162  if (str) {
38163  str_size = pos;
38164  if (str->size() < str_size + buf_size)
38165  str->resize(str_size + buf_size);
38166  } else
38167  str_size = pos = 0;
38168 
38169  do {
38170  try {
38171  is.read(str ? &(*str)[str_size] : buf, buf_size);
38172  } catch (...) {
38173  if (str)
38174  str->resize(str_size);
38175  throw;
38176  }
38177  streamsize count = is.gcount();
38178  str_size += (size_t) count;
38179  if (str) {
38180  if ((size_t) count == buf_size) {
38181  if (buf_size < (1UL << 20))
38182  buf_size <<= 1;
38183  str->resize(str_size + buf_size);
38184  } else
38185  assert(!is.good());
38186  }
38187  } while (is.good());
38188 
38189  assert(str_size >= pos);
38190  if (str)
38191  str->resize(str_size);
38192 
38193  if (!(str_size -= pos)) {
38194  // Nothing extracted
38195  //is.setstate(NcbiFailbit);
38196  assert(0);
38197  return 0;
38198  }
38199 
38200  // NB: istream::read() sets both bits at EOF (27.6.1.3.28)
38201 #if 0
38202  IOS_BASE::iostate iostate = is.rdstate();
38203  if (iostate != (NcbiFailbit | NcbiEofbit))
38204  return 0;
38205  is.clear(iostate & ~NcbiFailbit);
38206 #endif
38207  return str_size;
38208  }
38209 */
38210 template<typename T>
38211 static void s_READ_NUMERIC_BUFF(const string& data, size_t& pos, T& var)
38212 {
38213  var = *(T*)&data[pos];
38214  pos += sizeof(var);
38215 }
38216 /*
38217 void LoadTestAlignData(const string& input_file)
38218 {
38219  ifstream aln_data_ifstr(input_file, ios::binary);
38220 
38221 
38222  string aln_data;
38223  size_t aln_data_size(NcbiStreamToString(&aln_data, aln_data_ifstr));
38224  cout << "Align data bytes read: " << aln_data_size << "/" << aln_data.size() << endl;
38225 
38226  using svector_u32 = bm::sparse_vector<unsigned, bm::bvector<>>;
38227  using TDataType = bm::rsc_sparse_vector<unsigned, svector_u32>;
38228  vector<TDataType> alignments;
38229 
38230  size_t pos = 0;
38231  uint32_t num_rows = 0;
38232  s_READ_NUMERIC_BUFF(aln_data, pos, num_rows);
38233  cout << "num_rows = " << num_rows << endl;
38234 
38235  vector<uint32_t> sizes(num_rows, 0);
38236  for (size_t i = 0; i < num_rows; ++i) {
38237  s_READ_NUMERIC_BUFF(aln_data, pos, sizes[i]);
38238  }
38239 
38240  size_t align_data_size = 0;
38241  size_t index_sz = 0;
38242  cout << "Printing sizes \n";
38243  for (auto it : sizes) {
38244  align_data_size += it;
38245  cout << "align_size(" << index_sz << ") = " << it << endl;
38246  ++index_sz;
38247  }
38248  cout << "Complete align_data_size: " << align_data_size << endl;
38249 
38250  typedef bm::sparse_vector_deserializer<TDataType> csv_deserializer_type;
38251  csv_deserializer_type csv_deserializer;
38252  int buff_index = pos;
38253  for (size_t i = 0; i < num_rows; ++i) {
38254  alignments.push_back(TDataType(bm::use_null));
38255  TDataType& csv = alignments.back();
38256  if (i == 3)
38257  cout << i << endl;
38258  csv_deserializer.deserialize_structure(csv, (const unsigned char*)&aln_data[buff_index]);
38259  buff_index += sizes[i];
38260  }
38261 
38262  cout << "Deserialized structure\n";
38263  csv_deserializer_type::bv_ref_vector_type bv_ref;
38264  for (int i = num_rows - 1; i >= 0; --i) {
38265  bv_ref.add_vectors(alignments[i].get_bmatrix());
38266  }
38267  csv_deserializer.set_xor_ref(&bv_ref);
38268  for (size_t i = 0; i < num_rows; ++i) {
38269  csv_deserializer.deserialize(alignments[i], (const unsigned char*)&aln_data[pos], false);
38270  pos += sizes[i];
38271  }
38272 
38273  cout << "Completely deserialized\n";
38274 }
38275 */
38276 /*
38277 inline
38278 unsigned Residue2int(char bp)
38279 {
38280  unsigned v = 0;
38281  switch (bp) {
38282  case '.': v = 0; break; //00000
38283  case 'A': v = 1; break; // 0001
38284  case 'C': v = 2; break; // 00010
38285  case 'G': v = 3; break; // 00011
38286  case 'T': v = 4; break; // 00100
38287  case 'N': v = 5; break; // 00101
38288  case 'F': v = 6; break; // 00110
38289  case 'B': v = 7; break; // 00111
38290  case 'H': v = 8; break; // 01000
38291  case 'I': v = 9; break; // 01001
38292  case 'J': v = 10; break; // 01010
38293  case 'K': v = 11; break; // 01011
38294  case 'L': v = 12; break; // 01100
38295  case 'M': v = 13; break; // 01101
38296  case 'E': v = 14; break; // 01110
38297  case 'P': v = 15; break; // 01111
38298  case 'Q': v = 16; break; // 10000
38299  case 'R': v = 17; break; // 10001
38300  case 'S': v = 18; break; // 10010
38301  case 'D': v = 19; break; // 10011
38302  case 'U': v = 20; break; // 10100
38303  case 'V': v = 21; break; // 10101
38304  case 'W': v = 22; break; // 10110
38305  case 'X': v = 23; break; // 10111
38306  case 'Y': v = 24; break; // 11000
38307  case 'Z': v = 25; break; // 11001
38308  case '-': v = 26; break; // 11010
38309  case '*': v = 27; break; // 11011
38310  case '?': v = 28; break; // 11100
38311  case 'O': v = 29; break; // 11101
38312 
38313  default:
38314  throw runtime_error("Unknown residue '" + string(1, bp) + "'");
38315  }
38316  return v;
38317 }
38318 
38319 inline
38320 char Int2Residue(unsigned v)
38321 {
38322  char bp = ' ';
38323  switch (v) {
38324  case 0: bp = '.'; break; //00000
38325  case 1: bp = 'A'; break; // 0001
38326  case 2: bp = 'C'; break; // 00010
38327  case 3: bp = 'G'; break; // 00011
38328  case 4: bp = 'T'; break; // 00100
38329  case 5: bp = 'N'; break; // 00101
38330  case 6: bp = 'F'; break; // 00110
38331  case 7: bp = 'B'; break; // 00111
38332  case 8: bp = 'H'; break; // 01000
38333  case 9: bp = 'I'; break; // 01001
38334  case 10: bp = 'J'; break; // 01010
38335  case 11: bp = 'K'; break; // 01011
38336  case 12: bp = 'L'; break; // 01100
38337  case 13: bp = 'M'; break; // 01101
38338  case 14: bp = 'E'; break; // 01110
38339  case 15: bp = 'P'; break; // 01111
38340  case 16: bp = 'Q'; break; // 10000
38341  case 17: bp = 'R'; break; // 10001
38342  case 18: bp = 'S'; break; // 10010
38343  case 19: bp = 'D'; break; // 10011
38344  case 20: bp = 'U'; break; // 10100
38345  case 21: bp = 'V'; break; // 10101
38346  case 22: bp = 'W'; break; // 10110
38347  case 23: bp = 'X'; break; // 10111
38348  case 24: bp = 'Y'; break; // 11000
38349  case 25: bp = 'Z'; break; // 11001
38350  case 26: bp = '-'; break; // 11010
38351  case 27: bp = '*'; break; // 11011
38352  case 28: bp = '?'; break; // 11100
38353  case 29: bp = 'O'; break; // 11101
38354  default:
38355  throw runtime_error("Unknown value '" + to_string(v) + "'");
38356  }
38357  return bp;
38358 }
38359 
38360 
38361 int main2 ()
38362 {
38363  string test_file {"rsc_test.dat"};
38364  typedef bm::sparse_vector<unsigned, bm::bvector<> > svector_u32;
38365  typedef bm::rsc_sparse_vector<unsigned, svector_u32 > rsc_svector_u32;
38366  bm::sparse_vector_scanner<rsc_svector_u32::sparse_vector_type> scanner;
38367 
38368  {
38369  rsc_svector_u32 row_data;
38370  auto bit = row_data.get_back_inserter();
38371 
38372  string seq1{".....C.T..---------------------T.-.---.-C-.-.-G....-----.....-T.-.A....C........-.......C.-..A..GC...G.A.......-.....G.C....A..G-CCT.G----------------...C....C....T."};
38373  {
38374  size_t matches = std::count(seq1.begin(), seq1.end(), '.');
38375  size_t gaps = std::count(seq1.begin(), seq1.end(), '-');
38376  size_t mismatches = seq1.size() - matches - gaps;
38377  cout << "Statistics calculated from string:" << endl;
38378  cout << "\tMatches: " << matches << endl;
38379  cout << "\tMismatches: " << mismatches << endl;
38380  cout << "\tGaps: " << gaps << endl;
38381  }
38382 
38383  for(char& c : seq1) {
38384  if (c == '-')
38385  bit.add_null(1);
38386  else
38387  bit = Residue2int(c);
38388  }
38389  bit.flush();
38390 
38391  cout << "Input data:" << endl;
38392  for (size_t i=0; i<row_data.size(); ++i) {
38393  if (row_data.is_null(i)) {
38394  cout << '-';
38395  } else {
38396  cout << Int2Residue(row_data[i]);
38397  }
38398  }
38399  cout << endl;
38400 
38401  typedef bm::sparse_vector_serializer<rsc_svector_u32> sv_serializer_type;
38402  sv_serializer_type sv_serializer;
38403  bm::sparse_vector_serial_layout<rsc_svector_u32> sv_lay;
38404 
38405  sv_serializer.serialize(row_data, sv_lay);
38406 
38407  std::ofstream ofs (test_file, std::ofstream::out | std::ofstream::binary);
38408  ofs.write((const char*)sv_lay.buf() , sv_lay.size());
38409  ofs.close();
38410  }
38411 
38412  {
38413  ifstream is (test_file, std::ifstream::binary);
38414  if (!is) {
38415  cerr << "Failed to open file" << endl;
38416  }
38417  // get length of file:
38418  is.seekg (0, is.end);
38419  size_t length = is.tellg();
38420  is.seekg (0, is.beg);
38421 
38422  char * buffer = new char [length];
38423 
38424  // read data as a block:
38425  is.read (buffer,length);
38426  is.close();
38427 
38428  BM_DECLARE_TEMP_BLOCK(TB)
38429  rsc_svector_u32 row_data(bm::use_null);
38430  sparse_vector_deserialize(row_data, (const unsigned char*)buffer, TB);
38431  cout << "Output data:" << endl;
38432  for (size_t i=0; i<row_data.size(); ++i) {
38433  if (row_data.is_null(i)) {
38434  cout << '-';
38435  } else {
38436  cout << Int2Residue(row_data[i]);
38437  }
38438  }
38439  cout << endl;
38440 
38441  {
38442  auto null_vec = row_data.get_null_bvector();
38443  int num_aligned = null_vec->count();
38444  //cout << "num_aligned: " << num_aligned << endl;
38445  //int alignment_length = aligned_seq_len;
38446  int num_gaps = row_data.size() - num_aligned;
38447  //cout << "num_gaps: " << num_gaps << endl;
38448  //alignment_length += num_gaps;
38449  //strm << "alignment_length: " << aligned_seq_len << endl;
38450 
38451  const auto& sv = row_data.get_sv();
38452  int num_matches = num_aligned;
38453 
38454  bm::bvector<> bv_found;
38455  auto vs = Residue2int('.');
38456  scanner.find_eq(sv, vs, bv_found);
38457  num_matches = bv_found.count();
38458  //cout << "num_matches: " << num_matches << endl;
38459  //num_matches += num_gaps;
38460  //cout << "matches+gaps: " << num_matches << endl;
38461  int num_mismatches = sv.size() - num_matches;
38462  //cout << "sv.size(): " << sv.size() << endl;
38463  cout << "Statistics calculated from sparce vector:" << endl;
38464  cout << "\tMatches: " << num_matches << endl;
38465  cout << "\tMismatches: " << num_mismatches << endl;
38466  cout << "\tGaps: " << num_gaps << endl;
38467  }
38468 
38469 
38470  delete[] buffer;
38471  }
38472 
38473  return 0;
38474 }
38475 */
38476 
38477 /*
38478 void BamLoVoTest()
38479 {
38480 
38481  typedef bm::bvector<> bvector_type;
38482  typedef bm::str_sparse_vector<char, bvector_type, 32> str_sv_type;
38483  str_sv_type data; ///<< sorted succinct spot names
38484  ///
38485  str_sv_type data2;
38486  data2.push_back("E100027113L1C009R01304370475");
38487  data2.push_back("E100027113L1C030R02304370475");
38488  auto pl1 = data2.common_prefix_length(0, 1);
38489  data2.remap();
38490  data2.optimize();
38491  auto pl2 = data2.common_prefix_length(0, 1);
38492 
38493  std::string path = "/Users/anatoliykuznetsov/dev/git/BitMagic/tests/stress/";
38494  std::string key = "E100027113L1C030R02304370475";
38495  std::string file_name = path + key + ".batch";
38496  bm::file_load_svector(data, file_name);
38497 
38498  auto pl3 = data.common_prefix_length(49938432, 49999999);
38499  string s3_l, s3_r;
38500  data.get(49938432, s3_l);
38501  data.get(49999999, s3_r);
38502 
38503  str_sv_type::size_type pos = 0;
38504  std::string found_key;
38505 
38506  bm::sparse_vector_scanner<str_sv_type, 16> scanner2;
38507 
38508  if (scanner2.find_eq_str(data, key.c_str(), pos)) {
38509  data.get(pos, found_key);
38510  if (found_key != key) {
38511  std::cerr << pos << endl << key << "\n" << found_key << std::endl;
38512  }
38513  assert(0);
38514  }
38515  if (scanner2.bfind_eq_str(data, key.c_str(), pos)) {
38516  data.get(pos, found_key);
38517  if (found_key != key) {
38518  std::cerr << pos << endl << key << "\n" << found_key << std::endl;
38519  }
38520  assert(0);
38521  }
38522 
38523  bm::sparse_vector_scanner<str_sv_type, 16> scanner;
38524  scanner.bind(data, true);
38525 
38526  if (scanner.bfind_eq_str(key.c_str(), pos)) {
38527  data.get(pos, found_key);
38528  if (found_key != key) {
38529  std::cerr << "Found " << key << " at " << pos << " but get returned " << found_key << std::endl;
38530  }
38531  }
38532  auto it = data.begin();
38533  std::string s;
38534  while (it.valid()) {
38535  auto s= it.get_string_view();
38536  if (s == key) {
38537  std::cerr << "Drat! Key " << s << " exists! (Should not happen)" << std::endl;
38538  assert(0);
38539  } else if (s == found_key) {
38540  std::cerr << "Found_key " << s << " confirmed!" << std::endl;
38541  }
38542  it.advance();
38543  }
38544  std::cerr << "the end" << std::endl;
38545 }
38546 */
38547 
38548 
38549 #if 0
38550 
38551 typedef bm::sparse_vector<uint32_t, bvect> sparse_vector_u32_t;
38552 typedef bm::rsc_sparse_vector<uint32_t, sparse_vector_u32_t> rsc_sparse_vector_u32_t;
38553 
38554 void case3_read2()
38555 {
38556  std::vector<std::pair<uint32_t, uint32_t >> alleles; // vector of pairs locus_id, allele_id
38557 
38558  std::ifstream is("/Users/anatoliykuznetsov/dev/git/BitMagic/tests/stress/case3_salmonella_3min", std::ios::binary);
38559  uint8_t flags;
38560  uint32_t file_id;
38561  size_t sz = 0;
38562  std::vector<char> buffer;
38563  bvect bvector;
38564  rsc_sparse_vector_u32_t rsv;
38565  int cnt = 0;
38566 
38568 
38569  do {
38570  bvector.clear(true);
38571 
38572  is.read((char*)&flags, sizeof(flags));
38573  if (!is.good())
38574  break;
38575  is.read((char*)&file_id, sizeof(file_id));
38576  is.read((char*)&sz, sizeof(sz));
38577  buffer.resize(sz);
38578  is.read(buffer.data(), sz);
38579  bm::deserialize(bvector, (unsigned char*)buffer.data());
38580 /*
38581  {
38582  auto cnt = bvector.count();
38583  alleles.resize(0);
38584  alleles.reserve(cnt);
38585 
38586  bm::bvector<>::enumerator en = bvector.first();
38587  for ( ;en.valid(); ++en) {
38588  auto locus_id = *en;
38589  alleles.emplace_back(locus_id, 666); // allele from reference
38590  }
38591  }
38592 */
38593  is.read((char*)&sz, sizeof(sz));
38594  buffer.resize(sz);
38595  is.read(buffer.data(), sz);
38596 
38597  sv_deserializer.deserialize(rsv, (const unsigned char*)&buffer[0]);
38598  rsv.sync();
38599 
38600  {
38601  auto it = rsv.begin();
38602  if (it.valid())
38603  do {
38604  if (!it.is_null())
38605  alleles.emplace_back(it.pos(), it.value());
38606  } while (it.advance());
38607  }
38608 
38609  if (++cnt % 10000 == 0)
38610  std::cout << "Process: " << cnt << std::endl;
38611  } while (is.good());
38612  std::cout << "Total: " << cnt << " files, " << alleles.size() << " alleles" << std::endl;
38613 }
38614 
38615 
38616 #endif
38617 
38618 // Test contributed by Andrea Asztalos
38619 //
38621 {
38622  const string filename = "/Users/anatoliykuznetsov/dev/git/BitMagic/tests/stress/sample_serialized_XOR.bin";
38623 
38624 //const string filename = "sample_serialized_XOR.bin";
38625  ifstream istr(filename, std::ios::in | std::ios::binary);
38626  if (!istr.good()) {
38627  return;
38628  }
38629 
38630  istr.seekg(0, std::ios_base::end);
38631  unsigned length = (unsigned)istr.tellg();
38632  cout << "Length: " << length << endl;
38633 
38634  istr.seekg(0, std::ios::beg);
38635  char* buffer = new char[length];
38636 
38637  istr.read(buffer, length);
38638 
38639  using bvector_type = bvect;
38640  using TSparseOptVector = bm::str_sparse_vector<char, bvector_type, 200>;
38641 
38642  // reference vector for XOR deserialization
38645 
38646  TSparseOptVector sample_data(bm::use_null);
38647  sample_deserializer.deserialize_structure(sample_data, (unsigned char*)buffer); // this will run into an assertion
38648 }
38649 
38651 {
38652  cout << "Serialize and deserialize a vector of size 11" << endl;
38653 
38654  using bvector_type = bvect;
38655  using TSparseOptVector = bm::str_sparse_vector<char, bvector_type, 8>;
38656 
38657  TSparseOptVector str_vector(bm::use_null);
38658  auto in_iter = str_vector.get_back_inserter();
38659 
38660  in_iter = "0/0:40,11,0:0.216:51:16,6,0:24,5,0:11:24,0,34,324,1277,358:204,0,255:39,0:3.437e-01,1.119e+01,4.819e+01,3.348e+02,4.500e+02,3.718e+02:0.00,34.77,37.77,34.77,69.54,37.77:17,23,7,4:19,21,6,5";
38661  in_iter = "0/1:1,3,0:0.750:4:1,2,0:0,1,0:9:46,0,9,116,39,85:107,0,107:3,0:1.159e+01,5.942e-01,1.232e+01,1.169e+02,7.413e+01,8.813e+01:0.00,34.77,37.77,34.77,69.54,37.77:0,1,0,3:0,1,1,2";
38662  in_iter = "2/2:0,0,7,0:0.000,1.000:7:0,0,3,0:0,0,4,0:18:98,233,98,21,21,0,222,233,21,98:224,21,0:0,0:5.983e+01,2.306e+02,9.789e+01,1.808e+01,5.314e+01,6.815e-02,2.188e+02,2.647e+02,5.285e+01,9.760e+01:0.00,35.00,38.00,34.77,69.77,37.77,34.77,69.54,69.54,37.77:0,0,5,2:0,0,3,4";
38663  in_iter = "0/0:89,24,0:0.212:113:51,4,0:38,20,0:6:30,0,23,578,2085,601:255,0,255:40,0:1.185e+00,6.231e+00,3.223e+01,4.500e+02,4.500e+02,4.500e+02:0.00,34.77,37.77,34.77,69.54,37.77:45,44,14,10:45,44,12,12";
38664  in_iter = "0/0:15,6,0:0.286:21:8,3,0:7,3,0:4:34,0,10,200,547,210:161,0,255:10,0:2.521e+00,3.774e+00,1.677e+01,2.040e+02,4.500e+02,2.170e+02:0.00,34.77,37.77,34.77,69.54,37.77:5,10,1,5:6,9,2,4";
38665  in_iter = "0/0:15,4,0:0.211:19:7,1,0:8,3,0:23:12,0,10,155,581,165:110,0,255:15,0:2.299e-02,2.299e+01,3.599e+01,1.780e+02,4.500e+02,1.910e+02:0.00,34.77,37.77,34.77,69.54,37.77:6,9,4,0:6,9,1,3";
38666  in_iter = "0/0:16,3,0:0.158:19:6,1,0:10,2,0:24:11,0,10,116,588,126:68,0,255:15,0:1.874e-02,2.387e+01,3.687e+01,1.401e+02,4.500e+02,1.531e+02:0.00,34.77,37.77,34.77,69.54,37.77:10,6,0,3:6,10,2,1";
38667  in_iter = "0/0:10,2,0:0.167:12:4,0,0:6,2,0:9:26,0,27,78,376,105:47,0,255:10,0:5.850e-01,9.000e+00,3.900e+01,8.652e+01,4.194e+02,1.165e+02:0.00,34.77,37.77,34.77,69.54,37.77:3,7,0,2:7,3,2,0";
38668  in_iter = "0/1:3,2,0:0.400:5:0,1,0:3,1,0:5:39,0,5,66,116,71:56,0,110:3,0:6.346e+00,1.785e+00,9.785e+00,6.732e+01,1.529e+02,7.532e+01:0.00,34.77,37.77,34.77,69.54,37.77:2,1,1,1:1,2,2,0";
38669  in_iter = "1/1:0,6,0:1.000:6:0,2,0:0,4,0:14:58,18,0,251,18,58:251,18,0:0,0:2.023e+01,1.523e+01,1.748e-01,2.478e+02,5.000e+01,5.800e+01:0.00,34.77,37.77,34.77,69.54,37.77:0,0,3,3:0,0,3,3";
38670  in_iter = "0/1:7,4,0:0.364:11:0,3,0:7,1,0:20:58,0,20,121,203,144:100,0,191:6,0:2.325e+01,4.233e-02,2.304e+01,1.211e+02,2.382e+02,1.471e+02:0.00,34.77,37.77,34.77,69.54,37.77:4,3,2,2:3,4,1,3";
38671  in_iter.flush();
38672 
38674  str_vector.remap();
38675  str_vector.optimize(tb);
38676 
38677  // serialize and store it in - bm::sparse_vector_serial_layout<TSparseOptVector> layout
38678 
38680 
38681  {
38682  using samples_serializer_type = bm::sparse_vector_serializer<TSparseOptVector>;
38683  samples_serializer_type sample_serializer;
38684 
38685  sample_serializer.set_bookmarks(true, 16);
38686 
38687  // create a reference vector and attach it to all serializers
38688  samples_serializer_type::bv_ref_vector_type bitvector_ref;
38689 
38690  // add references in reverse order - here, there is only one vector
38691  bitvector_ref.add_sparse_vector(str_vector);
38692 
38693  // compute XOR similarity matrix in parallel
38697  bm::xor_sim_params xor_search_params;
38698  pbuilder.build_plan(tbatch, sim_model, bitvector_ref, xor_search_params);
38699 
38701  pool_type tpool; // our thread pool here (no threads created yet)
38702 
38703  unsigned thread_count = thread::hardware_concurrency();
38704  if (thread_count == 0)
38705  thread_count = 1;
38706 
38707  tpool.start(thread_count); // start the threads
38708  {
38710  exec.run(tpool, tbatch, true);
38711  }
38712  tpool.set_stop_mode(pool_type::stop_when_done);
38713  tpool.join();
38714 
38715  // add similarity model to each serializer
38716  sample_serializer.set_sim_model(&sim_model);
38717 
38718  // then: serialize
38719 
38720 
38721  sample_serializer.serialize(str_vector, layout);
38722 
38723  sample_serializer.set_xor_ref(nullptr);
38724  }
38725 
38726  // deserialize - and store data in: TSparseOptVector new_vector(bm::use_null);
38727 
38728  TSparseOptVector new_vector(bm::use_null);
38729  {
38730  const unsigned char* buf_ptr = layout.buf();
38731 
38732  // reference vector for XOR deserialization
38735 
38736 
38737  // first pass: build reference vectors
38738 
38739  sample_deserializer.deserialize_structure(new_vector, buf_ptr);
38740  }
38741 }
38742 
38743 
38744 // Test contributed by Andrea Asztalos
38745 //
38747 {
38748  using bvector_type = bvect;
38749  using TSparseStrVector = bm::str_sparse_vector<char, bvector_type, 390>;
38750 
38751  // set up the vector with all null elements:
38752  TSparseStrVector str_vector(bm::use_null);
38753  auto in_iter = str_vector.get_back_inserter();
38754  for (size_t i = 0; i < 10; ++i)
38755  in_iter.add_null();
38756 
38757  in_iter.flush();
38758 
38760  str_vector.remap();
38761  str_vector.optimize(tb);
38762 
38763  // search in it for the string "CBS"
38765  TSparseStrVector::bvector_type result;
38766  scanner.find_eq_str(str_vector, "CBS", result);
38767  assert (result.empty());
38768 
38769  // serialize str_vector
38771  str_serializer.set_bookmarks(true, 16);
38772  str_serializer.enable_xor_compression();
38773 
38775  str_serializer.serialize(str_vector, layout);
38776 
38777  // deserialize the entire vector int 'new_vector'
38778  const unsigned char* buf_ptr = layout.buf();
38780 
38781  TSparseStrVector new_vector(bm::use_null);
38782  deserializer.deserialize(new_vector, buf_ptr);
38783 
38784  // repeat the same search on 'new_vector'
38786  TSparseStrVector::bvector_type result_2;
38787  scanner_2.find_eq_str(new_vector, "CBS", result_2);
38788 
38789  assert(result_2.empty());
38790 }
38791 
38792 /// submitted by Andreay (GBench)
38793 ///
38795 {
38796  using bvector_type = bvect;
38797  using TSparseOptVector = bm::str_sparse_vector<char, bvector_type, 8>;
38798 
38799  cout << "Deserialize chr21 data and then do round_trip" << endl;
38800 
38801  char* buffer = 0;
38802 
38803  {
38804  const string filename = "/Users/anatoliykuznetsov/dev/git/BitMagic/tests/stress/serialized_sampleCol_chr21.bin";
38805  ifstream istr(filename, std::ios::in | std::ios::binary);
38806  if (!istr.good()) {
38807  return;
38808  }
38809  istr.seekg(0, std::ios_base::end);
38810  unsigned length = (unsigned)istr.tellg();
38811  istr.seekg(0, std::ios::beg);
38812  buffer = new char[length];
38813  istr.read(buffer, length);
38814  }
38815 
38816 
38817  TSparseOptVector str_vector(bm::use_null);
38819  deserializer.deserialize(str_vector, (unsigned char*)buffer);
38820 
38821  cout << "Vector length:" << str_vector.size() << endl; // expected size = 1776
38822 
38823  // serialize str_vector and store it in - bm::sparse_vector_serial_layout<TSparseOptVector> layout
38824 
38826 
38827  {
38828  using samples_serializer_type = bm::sparse_vector_serializer<TSparseOptVector>;
38829  samples_serializer_type sample_serializer;
38830 
38831  sample_serializer.set_bookmarks(true, 16);
38832 
38833  // create a reference vector and attach it to all serializers
38834  samples_serializer_type::bv_ref_vector_type bitvector_ref;
38835 
38836  // add references in reverse order - here, there is only one vector
38837  bitvector_ref.add_sparse_vector(str_vector);
38838 
38839  // connect the reference vector to the serializer
38840  sample_serializer.set_xor_ref(&bitvector_ref);
38841 
38842  // compute XOR similarity matrix in parallel
38845  bm::xor_sim_model<bvect> sim_model;
38846  bm::xor_sim_params xor_search_params;
38847  pbuilder.build_plan(tbatch, sim_model, bitvector_ref, xor_search_params);
38848 
38850  pool_type tpool; // our thread pool here (no threads created yet)
38851 
38852  unsigned thread_count = thread::hardware_concurrency();
38853  if (thread_count == 0)
38854  thread_count = 1;
38855 
38856  tpool.start(thread_count); // start the threads
38857  {
38859  exec.run(tpool, tbatch, true);
38860  }
38861  tpool.set_stop_mode(pool_type::stop_when_done);
38862  tpool.join();
38863 
38864  // add similarity model to each serializer
38865  sample_serializer.set_sim_model(&sim_model);
38866 
38867  // then: serialize
38868  sample_serializer.serialize(str_vector, layout);
38869 
38870  sample_serializer.set_xor_ref(nullptr);
38871  }
38872 
38873  // -----------------------------------------------
38874  // deserialize - and store data in: TSparseOptVector new_vector(bm::use_null);
38875  //
38876 
38877  TSparseOptVector new_vector(bm::use_null);
38878  {
38879  const unsigned char* buf_ptr = layout.buf();
38880 
38881  // reference vector for XOR deserialization
38884 
38885 
38886  // first pass: build reference vectors
38887 
38888  const unsigned char* tmp_ptr = buf_ptr;
38889  sample_deserializer.deserialize_structure(new_vector, tmp_ptr);
38890 
38891  {
38892  const bvect* bv = new_vector.get_bmatrix().row(2976);
38893  assert(bv);
38894  }
38895 
38896  // construct the reference vector
38897  // Add references in reverse order
38898  bitvector_ref.add_vectors(new_vector.get_bmatrix());
38899 
38900  sample_deserializer.set_xor_ref(&bitvector_ref);
38901 
38902  // second pass: data deserialization
38903 
38904  sample_deserializer.deserialize(new_vector, buf_ptr, false);
38905 
38906  sample_deserializer.set_xor_ref(nullptr);
38907 
38908 /*
38909  CompareStrSparseVector(new_vector,
38910  const vector<string>& str_coll)
38911 */
38912 /*
38913  {
38914  TSparseOptVector::const_iterator it = new_vector.begin();
38915  TSparseOptVector::const_iterator it_end = new_vector.end();
38916  for (; it != it_end; ++it)
38917  {
38918  std::cout << *it << std::endl;
38919  }
38920  }
38921 */
38922 
38923  bm::print_svector_stat(cout, str_vector);
38924 
38925 cout << "----------------------------\n";
38926  bm::print_svector_stat(cout, new_vector);
38927 
38928 
38929  bool eq = new_vector.equal(str_vector);
38930  assert(eq);
38931 
38932  }
38933 
38934 }
38935 
38936 
38937 #define BM_EXPAND(x) x ## 1
38938 #define EXPAND(x) BM_EXPAND(x)
38939 
38940 int main(int argc, char *argv[])
38941 {
38942  time_t start_time = time(0);
38943  time_t finish_time;
38944 
38945 #if !defined(BM_ASSERT) || (EXPAND(BM_ASSERT) == 1)
38946  cerr << "Build error: Test build with undefined BM_ASSERT" << endl;
38947  exit(1);
38948 #endif
38949 
38950 // BamLoVoTest();
38951 
38952 
38953  //case3_read2();
38954  //return 0;
38955 
38956 
38957  {
38958  auto ret = parse_args(argc, argv);
38959  if (ret != 0)
38960  return ret;
38961  }
38962 
38963  cout << bm::_copyright<true>::_p << endl;
38964  cout << "SIMD code = " << bm::simd_version() << endl;
38965 #if defined(BM_ASSERT)
38966 {
38967  cout << "BM_ASSERT defined. " << endl;
38968 }
38969 #endif
38970 #ifdef MEM_DEBUG
38971  cout << "MEM_DEBUG defined. " << endl;
38972 #endif
38973 
38974 #ifdef MEM_DEBUG_x
38975  {
38976  {
38977  bvect bv1;
38978  bv1.set(1);
38979  }
38980  CheckAllocLeaks(false);
38981  {
38982  bvect* bv = new bvect();
38983  bv->set(1);
38984  bool b = CheckAllocLeaks(false, false);
38985  assert(b); // leak found
38986  delete bv;
38987  b = CheckAllocLeaks(false, false);
38988  assert(!b); // leak found
38989  if (b)
38990  cout << "LEAK DETECT (false positive) OK" << endl;
38991  else
38992  cout << "ERROR detecting leaks" << endl;
38993 
38994  }
38995  }
38996 #endif
38997 
38998 /*
38999  test_str_sv_des_fnc();
39000  test_chr21();
39001 
39002  return 0;
39003 */
39004  //VCF_des_test();
39005 
39006 
39007 /*
39008  BrennerTest("/Users/anatoliykuznetsov/Desktop/dev/git/BitMagic/tests/stress/1.i",
39009  "/Users/anatoliykuznetsov/Desktop/dev/git/BitMagic/tests/stress/1.bv",
39010  "/Users/anatoliykuznetsov/Desktop/dev/git/BitMagic/tests/stress/new.bv");
39011  return 0;
39012 */
39013 
39014 /*
39015  LoadBVDump("C:/dev/group-by-sets/sets/geo_organization.bvdump",
39016  "C:/dev/group-by-sets/sets/geo_organization.bvdump2",
39017  false); // not validate!
39018  exit(1);
39019 */
39020 
39021 /*
39022  LoadBVDump("C:/dev/group-by-sets/sets/geo_organization.dat",
39023  "C:/dev/group-by-sets/sets/geo_organization.bvdump",
39024  true); // validate!
39025 
39026  LoadBVDump("C:/dev/group-by-sets/sets/exec_time_msec_bit.dat",
39027  "C:/dev/group-by-sets/sets/exec_time_msec_bit.bvdump",
39028  true);
39029 
39030  LoadBVDump("C:/dev/group-by-sets/sets/geo_country.dat",
39031  "C:/dev/group-by-sets/sets/geo_country.bvdump",
39032  true);
39033  LoadBVDump("C:/dev/group-by-sets/sets/log_displayeduids.dat",
39034  "C:/dev/group-by-sets/sets/log_displayeduids.bvdump",
39035  true);
39036 
39037  //LoadVectors("c:/dev/bv_perf", 3, 27);
39038  exit(1);
39039 */
39040 /*
39041  {
39042 
39043  BM_DECLARE_TEMP_BLOCK(tb)
39044  std::vector<unsigned char> buf_v;
39045 
39046  sparse_vector_serial_layout<str_svect_type > sv_lay;
39047  bm::sparse_vector_serialize<str_svect_type >(sv0, sv_lay, tb);
39048  const unsigned char* buf = sv_lay.buf();
39049 
39050  str_svect_type sv1;
39051  int res = bm::sparse_vector_deserialize(sv1, buf, tb);
39052  if (res != 0)
39053  {
39054  cerr << "De-Serialization error!" << endl;
39055  exit(1);
39056  }
39057 
39058  bool equal = sv0.equal(sv1);
39059  assert(equal);
39060  }
39061 */
39062 
39063 /*
39064  {
39065  const string file_name = "/Users/anatoliykuznetsov/dev/git/BitMagic/tests/stress/HG_Ref_column.txt";
39066  ifstream instr(file_name.c_str(), ios_base::in);
39067  if (!instr.good() || !instr.is_open()) {
39068  cout << "Failed to read file" << endl;
39069  assert(0);
39070  }
39071 
39072  cout << "Reading " << file_name << endl;
39073  using bvector_type = bm::bvector<>;
39074  using TSparseStrVector = bm::str_sparse_vector<char, bvector_type, 390>;
39075  TSparseStrVector ref(bm::use_null);
39076 
39077  unsigned num_lines = 0;
39078  {
39079  auto start = chrono::steady_clock::now();
39080  auto vec_it = ref.get_back_inserter();
39081  string line;
39082 
39083  while (getline(instr, line)) {
39084  if (line.empty() || (!line.empty() && line[0] == '#')) {
39085  continue;
39086  }
39087  ++num_lines;
39088  vec_it = line;
39089  }
39090  vec_it.flush();
39091  auto diff = chrono::steady_clock::now() - start;
39092  cout << "Reading took "
39093  << chrono::duration_cast<chrono::milliseconds>(diff).count() << " ms " << endl;
39094  }
39095 
39096  cout << "Lines: " << num_lines << ", " << ref.size() << endl;
39097  {
39098  cout << "Remapping..." << endl;
39099  auto start = chrono::steady_clock::now();
39100  ref.remap();
39101  auto diff = chrono::steady_clock::now() - start;
39102  cout << "Remap took "
39103  << chrono::duration_cast<chrono::milliseconds>(diff).count() << " ms " << endl;
39104  }
39105  {
39106  cout << "Optimizing..." << endl;
39107  auto start = chrono::steady_clock::now();
39108  BM_DECLARE_TEMP_BLOCK(tb);
39109  ref.optimize(tb);
39110  auto diff = chrono::steady_clock::now() - start;
39111  cout << "Optimization took "
39112  << chrono::duration_cast<chrono::milliseconds>(diff).count() << " ms " << endl;
39113  }
39114 
39115  using TLayout = bm::sparse_vector_serial_layout<TSparseStrVector>;
39116  auto layout = make_unique<TLayout>();
39117 
39118  bm::sparse_vector_serializer<TSparseStrVector> str_serializer;
39119 
39120  str_serializer.set_bookmarks(true, 16);
39121  str_serializer.enable_xor_compression();
39122  assert(str_serializer.is_xor_ref());
39123  {
39124  cout << "Serializing..." << endl;
39125  auto start = chrono::steady_clock::now();
39126  str_serializer.serialize(ref, *layout.get());
39127  auto diff = chrono::steady_clock::now() - start;
39128  cout << "Serialization took "
39129  << chrono::duration_cast<chrono::milliseconds>(diff).count() << " ms " << endl;
39130  }
39131  }
39132 */
39133 /*
39134 {
39135 typedef bm::str_sparse_vector<char, bvect, 8> str_sv_type;
39136 typedef bm::sparse_vector<uint32_t, bvect > sv_uint_32_type;
39137 
39138  str_sv_type str_sv0; // sparse-succinct vector
39139  sv_uint_32_type sv_hash0;
39140  ReadTestData(str_sv0, sv_hash0, 22, "/Volumes/DATAFAT32/spotnames/read_names2.txt");
39141  cout << "remap..." << endl;
39142  str_sv0.remap();
39143  BM_DECLARE_TEMP_BLOCK(tb)
39144  str_sv0.optimize(tb); // optimize the succinct vector
39145  str_sv0.freeze();
39146 
39147  cout << 1 << endl;
39148 }
39149  CheckAllocLeaks(false);
39150 */
39151 
39152 
39153 //avx2_i32_shift();
39154 //return 0;
39155 
39156 //unsigned long long a = 9223372036854775807ULL;
39157 //unsigned long long a = 281474976710655ULL;
39158 //a = a / (65536 * 256);
39159 /*
39160 LoadTestAlignData("/Volumes/DATAFAT32/CGV-131/woXOR_ser_align_5736.bin");
39161 
39162 LoadTestAlignData("/Volumes/DATAFAT32/CGV-131/ser_align_5736.bin");
39163 */
39164 // return main2();
39165 
39166 /*
39167 {
39168 typedef bm::bvector<> bvector_type1;
39169 typedef bm::str_sparse_vector<char, bvector_type1, 32> str_sv_type1;
39170 typedef bm::sparse_vector_scanner<str_sv_type1, 64> scanner_t;
39171 
39172  str_sv_type1 sv;
39173  bm::file_load_svector(sv, "/Users/anatoliykuznetsov/dev/git/BitMagic/tests/stress/2.batch");
39174  scanner_t scanner;
39175  scanner.bind(sv, true);
39176  str_sv_type1::size_type pos = 0;
39177  std::string name = "E00583:350:H5MV3CCX2:4:2113:18203:62663";
39178 
39179  {
39180  scanner_t scanner;
39181  bvector_type1 bv_out;
39182  bool b = scanner.find_eq_str(sv, name.c_str(), bv_out);
39183  if (b)
39184  {
39185  std::cout << "found..." << std::endl;
39186  }
39187 
39188  }
39189 
39190  bool found = scanner.bfind_eq_str(name.c_str(), name.size(), pos);
39191  if (found)
39192  {
39193  std::string val;
39194  sv.get(pos, val);
39195  std::cout << name << std::endl;
39196  std::cout << val << std::endl;
39197  std::cout << name << " - found '" << val << "' at " << pos << std::endl;
39198  }
39199 return 0;
39200 }
39201 */
39202 /*
39203 {
39204  typedef bm::sparse_vector<uint16_t, bm::bvector<> > svector_u16;
39205  svector_u16 sv, sv_c;
39206  std::ifstream input( "/Volumes/DATAFAT32/incs.txt", ios::in);
39207  if (!input.good())
39208  {
39209  cerr << "cannot open file" << endl;
39210  return 1;
39211  }
39212  string line;
39213  size_t idx = 0;
39214  size_t o_cnt = 0;
39215 
39216  while (getline(input, line))
39217  {
39218 
39219  if (line.empty())
39220  continue;
39221  auto pos = stoi(line);
39222  auto v1 = sv.get_no_check(pos);
39223 
39224  sv.inc(pos);
39225  sv_c.inc(pos);
39226 
39227 
39228  auto v2 = sv.get_no_check(pos);
39229  if (v1 + 1 != v2) {
39230  cerr << "Mismatch!" << endl;
39231  cout << "line:" << idx << ", pos: " << pos << ", v1: " << v1 << ", v2: " << v2 << endl;
39232  auto v1_c = sv_c.get(pos);
39233  cout << "v1_c=" << v1_c << endl;
39234 
39235  return 1;
39236  }
39237  //if (o_cnt > 1) // start detailed checking
39238  if (idx < 23796594)
39239  {
39240  auto v_c = sv.get(23796594);
39241  if (v_c)
39242  {
39243  cerr << "\n** turned non-zero!" << endl;
39244  cerr << " position match=" << (pos == 23796594) << endl;
39245  cout << "line:" << idx << ", pos: " << pos << ", v1: " << v1 << ", v2: " << v2 << endl;
39246  auto v1_c = sv_c.get(23796594);
39247  cout << "v1_c=" << v1_c << endl;
39248  //return 1;
39249  }
39250  }
39251 
39252  ++idx;
39253  if (idx % 8000000 == 0)
39254  {
39255  svector_u16 sv1(sv);
39256  cerr << "O" << flush;
39257  sv.optimize();
39258  ++o_cnt;
39259  }
39260  }
39261  cout << "processed " << idx << " lines" << endl;
39262  return 0;
39263 }
39264 */
39265 
39266  if (is_all || is_low_level)
39267  {
39268  TestNibbleArr();
39269 
39270  TestHasZeroByte();
39271 
39272  TestRecomb();
39273 
39274  HMaskTest();
39275 
39276  Log2Test();
39277 
39278 
39279  OptimGAPTest();
39280 
39281  CalcBeginMask();
39282  CalcEndMask();
39283 
39284 // TestSIMDUtils();
39285 
39287 
39288  TestFindFirst();
39289 
39291 
39293 
39295 
39296  LZCNTTest();
39297 
39298  SelectTest();
39299 
39300  TestBlockZero();
39301 
39302  TestBlockAND();
39303  TestBlockSUB();
39304  TestBlockOR();
39305 
39307 
39309 
39310  //TestGAP_XOR();
39311 
39312  TestBlockToGAP();
39313 
39314  ShiftRotateTest();
39315 
39317 
39319  TestBlockLast();
39320 
39321  BitForEachTest();
39322 
39324  WordCmpTest();
39325 
39327 
39329 
39330  TestBlockDigest();
39331 
39332  //BitBlockTransposeTest();
39333  }
39334 
39335  if (is_all || is_support)
39336  {
39337 
39338  TestHeapVector();
39339  CheckAllocLeaks(false);
39340 
39341  TestSQueue();
39342  CheckAllocLeaks(false);
39343 
39345  CheckAllocLeaks(false);
39346 
39347  TestTasks();
39348  CheckAllocLeaks(false);
39349 
39350  MiniSetTest();
39351  CheckAllocLeaks(false);
39352 
39353  BitEncoderTest();
39354  CheckAllocLeaks(false);
39355 
39357  CheckAllocLeaks(false);
39358 
39359  GammaEncoderTest();
39360  CheckAllocLeaks(false);
39361 
39362  GAPCheck();
39363  CheckAllocLeaks(false);
39364 
39366  CheckAllocLeaks(false);
39367 
39368  TestBasicMatrix();
39369  CheckAllocLeaks(false);
39370 
39372  CheckAllocLeaks(false);
39373 
39374  RSIndexTest();
39375  CheckAllocLeaks(false);
39376  }
39377 
39378  if (is_all || is_bvbasic || is_bvb0 || is_bvb1)
39379  {
39380 
39381  if (is_all || is_bvbasic || is_bvb0)
39382  {
39383  ExportTest();
39384  CheckAllocLeaks(false);
39385 
39386  ResizeTest();
39387  CheckAllocLeaks(false);
39388 
39389  SyntaxTest();
39390  CheckAllocLeaks(false);
39391 
39392  SetTest();
39393  CheckAllocLeaks(false);
39394 
39395  SwapTest();
39396  CheckAllocLeaks(false);
39397 
39398  ArenaTest();
39399  CheckAllocLeaks(false);
39400 
39401  FreezeTest();
39402  CheckAllocLeaks(false);
39403 
39404  BlockDigestTest();
39405  CheckAllocLeaks(false);
39406 
39407  EmptyBVTest();
39408  CheckAllocLeaks(false);
39409 
39410  ClearAllTest();
39411  CheckAllocLeaks(false);
39412 
39413  CountRangeTest();
39414  CheckAllocLeaks(false);
39415 
39416  EnumeratorTest();
39417  CheckAllocLeaks(false);
39418 
39420  CheckAllocLeaks(false);
39421 
39423  CheckAllocLeaks(false);
39424 
39426  CheckAllocLeaks(false);
39427 
39428  KeepRangeTest();
39429  CheckAllocLeaks(false);
39430  }
39431 
39432  if (is_all || is_bvbasic || is_bvb1)
39433  {
39435  CheckAllocLeaks(false);
39436 
39437  OptimizeTest();
39438  CheckAllocLeaks(false);
39439 
39440  RankFindTest();
39441  CheckAllocLeaks(false);
39442 
39444  CheckAllocLeaks(false);
39445 
39446  GetNextTest();
39447  CheckAllocLeaks(false);
39448 
39449  BvectorIncTest();
39450  CheckAllocLeaks(false);
39451 
39453  CheckAllocLeaks(false);
39454 
39455  GAPTestStress();
39456  CheckAllocLeaks(false);
39457 
39458  MaxSTest();
39459  CheckAllocLeaks(false);
39460 
39462  CheckAllocLeaks(false);
39463 
39465  CheckAllocLeaks(false);
39466 
39467  RangeCopyTest();
39468  CheckAllocLeaks(false);
39469 
39470  ComparisonTest();
39471  CheckAllocLeaks(false);
39472 
39474  CheckAllocLeaks(false);
39475 
39477  CheckAllocLeaks(false);
39478 
39479  MutationTest();
39480  CheckAllocLeaks(false);
39482  CheckAllocLeaks(false);
39483 
39484  BlockLevelTest();
39485  CheckAllocLeaks(false);
39486 
39487  BVImportTest();
39488  CheckAllocLeaks(false);
39489  }
39490 
39491  }
39492 
39493 
39494  if (is_all || is_bvser || is_bvbasic)
39495  {
39497  CheckAllocLeaks(false);
39498 
39500  CheckAllocLeaks(false);
39501 
39503  CheckAllocLeaks(false);
39504 
39506  CheckAllocLeaks(false);
39507 
39509  CheckAllocLeaks(false);
39510  }
39511 
39512  if (is_all || is_bvshift)
39513  {
39514  BvectorShiftTest();
39515  CheckAllocLeaks(false);
39516 
39518  CheckAllocLeaks(false);
39519 
39520  BvectorEraseTest();
39521  CheckAllocLeaks(false);
39522  }
39523 
39524 
39525  if (is_all || is_rankc)
39526  {
39528  CheckAllocLeaks(false);
39529 
39530  TestRankCompress();
39531  CheckAllocLeaks(false);
39532  }
39533 
39534  if (is_bvops0)
39535  {
39536  AndOperationsTest(true); // enable detailed check
39537  CheckAllocLeaks(false);
39538 
39539  AndOrOperationsTest(true); // enable detailed check
39540  CheckAllocLeaks(false);
39541 
39542  OrOperationsTest(true);
39543  CheckAllocLeaks(false);
39544 
39545  XorOperationsTest(true);
39546  CheckAllocLeaks(false);
39547 
39548  SubOperationsTest(true);
39549  CheckAllocLeaks(false);
39550 
39551  StressTest(300, -1, true); // random OPS stress
39552  }
39553 
39554  if (is_bvops1)
39555  {
39556  StressTest(250, 0, false, 2); // OR - detailed check disabled, method = 2
39557  CheckAllocLeaks(false);
39558 
39559  StressTest(150, 0, false); // OR - detailed check disabled
39560  CheckAllocLeaks(false);
39561 
39562  StressTest(250, 3, false, 2); // AND - detailed check disabled, method = 2
39563  CheckAllocLeaks(false);
39564 
39565  StressTest(150, 3, false); // AND
39566  CheckAllocLeaks(false);
39567  }
39568 
39569  if (is_bvops2)
39570  {
39571  // -------------------------------------
39572  // placed here for performance/parallel test execution purposes
39573  //
39574  StressTest(150, 1, false); // SUB
39575  CheckAllocLeaks(false);
39576 
39577  StressTest(150, 2, false); // XOR
39578  CheckAllocLeaks(false);
39579 
39580  // -------------------------------------
39581 
39582 
39583  KleeneLogicTest();
39584  CheckAllocLeaks(false);
39585 
39587  CheckAllocLeaks(false);
39588 
39590  CheckAllocLeaks(false);
39591  }
39592 
39593  if (is_all || is_bvops)
39594  {
39595  AndOperationsTest(true); // enable detailed check
39596  CheckAllocLeaks(false);
39597 
39598  AndOrOperationsTest(true); // enable detailed check
39599  CheckAllocLeaks(false);
39600 
39601  OrOperationsTest(true);
39602  CheckAllocLeaks(false);
39603 
39604  XorOperationsTest(true);
39605  CheckAllocLeaks(false);
39606 
39607  SubOperationsTest(true);
39608  CheckAllocLeaks(false);
39609 
39610  StressTest(150, 0, false); // OR - detailed check disabled
39611  CheckAllocLeaks(false);
39612 
39613  StressTest(150, 3, false); // AND
39614  CheckAllocLeaks(false);
39615 
39616  StressTest(150, 1, false); // SUB
39617  CheckAllocLeaks(false);
39618 
39619  StressTest(150, 2, false); // XOR
39620  CheckAllocLeaks(false);
39621 
39622  KleeneLogicTest();
39623  CheckAllocLeaks(false);
39624 
39626  CheckAllocLeaks(false);
39628  CheckAllocLeaks(false);
39629 
39630  StressTest(300, -1, true); // random OPS stress test
39631 
39632  }
39633 
39634  if (is_all || is_agg)
39635  {
39636  AggregatorTest();
39637  CheckAllocLeaks(false);
39638 
39640  CheckAllocLeaks(false);
39641 
39643  CheckAllocLeaks(false);
39644 
39646  CheckAllocLeaks(false);
39647 
39649  }
39650 
39651  if (is_all || is_sv || is_sv0 || is_sv1)
39652  {
39653 
39654  if (is_all || is_sv || is_sv0)
39655  {
39656  TestSparseVector();
39657  CheckAllocLeaks(false);
39658 
39660  CheckAllocLeaks(false);
39661 
39663  CheckAllocLeaks(false);
39664 
39666  CheckAllocLeaks(false);
39667 
39669  CheckAllocLeaks(false);
39670 
39672  CheckAllocLeaks(false);
39673 
39675  CheckAllocLeaks(false);
39676 
39678  CheckAllocLeaks(false);
39679 
39681  CheckAllocLeaks(false);
39682 
39684  CheckAllocLeaks(false);
39685 
39686  }
39687 
39688  if (is_all || is_sv || is_sv1)
39689  {
39690 
39692  CheckAllocLeaks(false);
39693 
39695  CheckAllocLeaks(false);
39696 
39698  CheckAllocLeaks(false);
39699 
39701  CheckAllocLeaks(false);
39702 
39704  CheckAllocLeaks(false);
39705 
39707  CheckAllocLeaks(false);
39708 
39710  CheckAllocLeaks(false);
39711  }
39712  }
39713 
39714  if (is_sv_sort || is_sv)
39715  {
39716  TestSparseSort();
39717  CheckAllocLeaks(false);
39718 
39720  CheckAllocLeaks(false);
39721  }
39722 
39723  if (is_all || is_csv || is_csv0 || is_csv1)
39724  {
39725  if (is_all || is_csv || is_csv0)
39726  {
39728  CheckAllocLeaks(false);
39729 
39731  CheckAllocLeaks(false);
39732 
39734  CheckAllocLeaks(false);
39735 
39737  CheckAllocLeaks(false);
39738  }
39739 
39740  if (is_all || is_csv || is_csv1)
39741  {
39742 
39744  CheckAllocLeaks(false);
39745 
39747  CheckAllocLeaks(false);
39748 
39750  CheckAllocLeaks(false);
39751  }
39752  }
39753 
39754  if (is_all || is_c_coll)
39755  {
39757  CheckAllocLeaks(false);
39758  }
39759 
39760  if (is_all || is_str_sv)
39761  {
39763  CheckAllocLeaks(false);
39764 
39766  CheckAllocLeaks(false);
39767 
39769  CheckAllocLeaks(false);
39770 
39772  CheckAllocLeaks(false);
39773 
39775  CheckAllocLeaks(false);
39776 
39778  CheckAllocLeaks(false);
39779 
39781  CheckAllocLeaks(false);
39782 
39784  CheckAllocLeaks(false);
39785 
39787  CheckAllocLeaks(false);
39788  }
39789 
39790  if (is_ser || is_allsvser)
39791  {
39792 
39793  if (is_ser)
39794  {
39796  CheckAllocLeaks(false);
39797 
39799  CheckAllocLeaks(false);
39800 
39802  CheckAllocLeaks(false);
39803 
39805  CheckAllocLeaks(false);
39806 
39807  }
39809  CheckAllocLeaks(false);
39810 
39812  CheckAllocLeaks(false);
39813 
39815  CheckAllocLeaks(false);
39816 
39818  CheckAllocLeaks(false);
39819 
39821  CheckAllocLeaks(false);
39822 
39823  }
39824 
39825 
39826  finish_time = time(0);
39827 
39828 
39829  cout << "Test execution time = " << finish_time - start_time << endl;
39830 //mem_debug_label:
39831 
39832  CheckAllocLeaks(true);
39833 
39834  cout << "OK";
39835 
39836  return 0;
39837 }
39838 
39839 
ncbi::TMaskedQueryRegions mask
Three-valued logic (3VL) operations.
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
Algorithms for fast aggregation of N bvectors.
#define VECT_BLOCK_CHANGE(block, size)
Definition: bmavx2.h:3555
basic bit-matrix class and utilities
Import of bvector<> from native type bit-arrays.
Debugging functions (internal). Poorly documented, not well written.
#define BM_ALIGN16
Definition: bmdef.h:304
#define BMNOEXCEPT
Definition: bmdef.h:82
#define BM_ALIGN16ATTR
Definition: bmdef.h:305
#define FULL_BLOCK_FAKE_ADDR
Definition: bmdef.h:158
#define BM_VECT_ALIGN_ATTR
Definition: bmdef.h:328
#define BM_VECT_ALIGN
Definition: bmdef.h:327
Algorithms for bit ranges and intervals.
Generation of random subset.
Serialization / compression of bvector<>. Set theoretical operations on compressed BLOBs.
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
Algorithms for bm::sparse_vector.
Compressed sparse container rsc_sparse_vector<> for integer types.
Parallel planner for operations with sparse vectors.
Serialization for sparse_vector<>
string sparse vector based on bit-transposed matrix
Task definitions for parallel programming with BitMagic.
Timing utilities for benchmarking (internal)
Bit manipulation primitives (internal)
Mini bitset for testing and utility purposes (internal)
Functions and utilities for XOR filters (internal)
static void pad(Char *s)
Definition: bzip2.c:908
Pipeline vector for running a group of aggregation operations on a family of vectors.
Definition: bmaggregator.h:224
const bv_vector_type & get_all_input_vect() const noexcept
Definition: bmaggregator.h:295
const count_vector_type & get_all_input_cnt_vect() const noexcept
Definition: bmaggregator.h:297
bv_count_vector_type & get_bv_count_vector() noexcept
Return results vector count used for pipeline execution.
Definition: bmaggregator.h:287
bool is_complete() const noexcept
return true if pipeline is ready for execution (complete)
Definition: bmaggregator.h:269
void set_or_target(bvector_type *bv_or) noexcept
Attach OR (aggregator vector).
Definition: bmaggregator.h:251
const arg_vector_type & get_args_vector() const noexcept
Return argument vector used for pipeline execution.
Definition: bmaggregator.h:279
arg_groups * add()
Add new arguments group.
void complete()
Prepare pipeline for the execution (resize and init internal structures) Once complete,...
bvect_vector_type & get_bv_res_vector() noexcept
Return results vector used for pipeline execution.
Definition: bmaggregator.h:283
Algorithms for fast aggregation of a group of bit-vectors.
Definition: bmaggregator.h:122
void combine_shift_right_and(bvector_type &bv_target)
Aggregate added group of vectors using SHIFT-RIGHT and logical AND Operation does NOT perform an expl...
bool combine_and_sub(bvector_type &bv_target)
Aggregate added group of vectors using fused logical AND-SUB Operation does NOT perform an explicit r...
void reset()
Reset aggregate groups, forget all attached vectors.
Definition: bmaggregator.h:941
void combine_or(bvector_type &bv_target)
Aggregate added group of vectors using logical OR Operation does NOT perform an explicit reset of arg...
void combine_and(bvector_type &bv_target)
Aggregate added group of vectors using logical AND Operation does NOT perform an explicit reset of ar...
bm::id64_t get_cache_gap_hits() const noexcept
Definition: bmaggregator.h:634
size_t add(const bvector_type *bv, unsigned agr_group=0)
Attach source bit-vector to a argument group (0 or 1).
void combine_and_horizontal(bvector_type &bv_target, const bvector_type_const_ptr *bv_src, size_t src_size)
Horizontal AND aggregation (potentially slower) method.
void set_optimization(typename bvector_type::optmode opt=bvector_type::opt_compress) noexcept
set on-the-fly bit-block compression By default aggregator does not try to optimize result,...
Definition: bmaggregator.h:359
size_type count() const
Definition: bmaggregator.h:488
void set_compute_count(bool count_mode) noexcept
Definition: bmaggregator.h:363
void combine_or_horizontal(bvector_type &bv_target, const bvector_type_const_ptr *bv_src, size_t src_size)
Horizontal OR aggregation (potentially slower) method.
void combine_and_sub_horizontal(bvector_type &bv_target, const bvector_type_const_ptr *bv_src_and, size_t src_and_size, const bvector_type_const_ptr *bv_src_sub, size_t src_sub_size)
Horizontal AND-SUB aggregation (potentially slower) method.
const bmatrix_type & get_bmatrix() const noexcept
Definition: bmbmatrix.h:553
bool is_nullable() const noexcept
check if container supports NULL(unassigned) values
Definition: bmbmatrix.h:439
bool is_null(size_type idx) const noexcept
test if specified element is NULL
Definition: bmbmatrix.h:1830
bvector_type_const_ptr get_slice(unsigned i) const noexcept
get read-only access to bit-plane
Definition: bmbmatrix.h:497
static constexpr bool is_signed() noexcept
returns true if value type is signed integral type
Definition: bmbmatrix.h:433
const bvector_type * get_null_bvector() const noexcept
Get bit-vector of assigned values or NULL (if not constructed that way)
Definition: bmbmatrix.h:451
bm::null_support get_null_support() const noexcept
check if container supports NULL (unassigned) values
Definition: bmbmatrix.h:444
bvector_type * get_null_bvect() noexcept
Definition: bmbmatrix.h:525
Basic dense bit-matrix class.
Definition: bmbmatrix.h:56
unsigned char get_octet(size_type pos, size_type octet_idx) const noexcept
Definition: bmbmatrix.h:1295
void swap(basic_bmatrix< BV > &bbm) noexcept
Definition: bmbmatrix.h:1024
bvector_type_ptr construct_row(size_type row)
Definition: bmbmatrix.h:1055
void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, typename bvector_type::statistics *stat=0)
run memory optimization for all bit-vector rows
Definition: bmbmatrix.h:1547
void set_octet(size_type pos, size_type octet_idx, unsigned char octet)
Definition: bmbmatrix.h:1193
size_type rows() const noexcept
Definition: bmbmatrix.h:140
Byte based reader for un-aligned bit streaming.
Definition: encoding.h:257
void bic_decode_u32_cm(bm::word_t *arr, unsigned sz, bm::word_t lo, bm::word_t hi) noexcept
Binary Interpolative array decode (32-bit)
Definition: encoding.h:1516
void bic_decode_u16_rg(bm::gap_word_t *arr, unsigned sz, bm::gap_word_t lo, bm::gap_word_t hi) noexcept
Binary Interpolative array decode.
Definition: encoding.h:1472
void bic_decode_u16_cm(bm::gap_word_t *arr, unsigned sz, bm::gap_word_t lo, bm::gap_word_t hi) noexcept
Binary Interpolative array decode.
Definition: encoding.h:1561
unsigned get_bits(unsigned count) noexcept
read number of bits out of the stream
Definition: encoding.h:1877
Byte based writer for un-aligned bit streaming.
Definition: encoding.h:183
void bic_encode_u16_rg(const bm::gap_word_t *arr, unsigned sz, bm::gap_word_t lo, bm::gap_word_t hi) noexcept
Binary Interpolative encoding (array of 16-bit ints)
Definition: encoding.h:1259
void bic_encode_u16_cm(const bm::gap_word_t *arr, unsigned sz, bm::gap_word_t lo, bm::gap_word_t hi) noexcept
Binary Interpolative encoding (array of 16-bit ints) cm - "center-minimal".
Definition: encoding.h:1420
void bic_encode_u32_cm(const bm::word_t *arr, unsigned sz, bm::word_t lo, bm::word_t hi) noexcept
Binary Interpolative encoding (array of 32-bit ints) cm - "center-minimal".
Definition: encoding.h:1294
void flush() noexcept
Flush the incomplete 32-bit accumulator word.
Definition: encoding.h:230
void put_bits(unsigned value, unsigned count) noexcept
issue count bits out of value
Definition: encoding.h:1106
Bit-block get adapter, takes bitblock and represents it as a get_32() accessor function.
Definition: bmfunc.h:9360
Bit-block store adapter, takes bitblock and saves results into it.
Definition: bmfunc.h:9376
bitvector blocks manager Embedded class managing bit-blocks on very low level. Includes number of fun...
Definition: bmblocks.h:42
static void alloc_arena(arena *ar, const bm::bv_arena_statistics &st, allocator_type &alloc)
Allocate arena (content memory) based on arena statistics.
Definition: bmblocks.h:2580
const bm::word_t * get_block_ptr(unsigned i, unsigned j) const BMNOEXCEPT
Finds block in 2-level blocks array (unsinitized)
Definition: bmblocks.h:556
allocator_type & get_allocator() BMNOEXCEPT
Returns reference on the allocator.
Definition: bmblocks.h:1990
static void free_arena(arena *ar, allocator_type &alloc) BMNOEXCEPT
Free arena (content memory) based on arena statistics.
Definition: bmblocks.h:2629
void calc_arena_stat(bm::bv_arena_statistics *st) const BMNOEXCEPT
Calculates bitvector arena statistics.
Definition: bmblocks.h:2466
List of reference bit-vectors with their true index associations.
Definition: bmxor.h:624
void add_vectors(const BMATR &bmatr)
Append basic bit-matrix to the list of reference vectors.
Definition: bmxor.h:726
void fill_alloc_digest(bvector_type &bv_blocks) const
Fill block allocation digest for all vectors in the reference collection.
Definition: bmxor.h:699
bool build_nb_digest_and_xor_matrix(matrix_chain_type &matr, bvector_type &bv_blocks) const
Calculate blocks digest and resize XOR distance matrix based on total number of available blocks.
Definition: bmxor.h:759
size_type find(std::size_t ref_idx) const noexcept
Find vector index by the reference index.
Definition: bmxor.h:676
void add(const bvector_type *bv, size_type ref_idx)
Add reference vector.
Definition: bmxor.h:652
void build(const BMATR &bmatr)
Reset and build vector of references from a basic bit-matrix all NULL rows are skipped,...
Definition: bmxor.h:716
size_type size() const noexcept
Get reference list size.
Definition: bmxor.h:660
size_type get_row_idx(size_type idx) const noexcept
Get reference row index by the index in this ref-vector.
Definition: bmxor.h:667
void resize_xor_matrix(matrix_chain_type &matr, size_type total_blocks) const
Utility function to resize matrix based on number of vectors and blocks.
Definition: bmxor.h:746
Output iterator iterator designed to set "ON" bits based on input sequence of integers.
Definition: bm.h:465
Constant iterator designed to enumerate "ON" bits counted_enumerator keeps bitcount,...
Definition: bm.h:734
size_type count() const noexcept
Number of bits ON starting from the .
Definition: bm.h:775
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:603
void go_first() noexcept
Position enumerator to the first available bit.
Definition: bm.h:8301
bool go_to(size_type pos) noexcept
go to a specific position in the bit-vector (or next)
Definition: bm.h:8209
Output iterator iterator designed to set "ON" bits based on input sequence of integers (bit indeces).
Definition: bm.h:381
bool valid() const noexcept
Checks if iterator is still valid.
Definition: bm.h:283
bool compare_state(const iterator_base &ib) const noexcept
Compare FSMs for testing purposes.
Definition: bm.h:295
Class reference implements an object for bit assignment.
Definition: bm.h:149
reference & flip()
Definition: bm.h:222
Bitvector class with very limited functionality.
Definition: bmvmin.h:272
int is_bit_true(size_type pos) const
Checks if bit pos 1 or 0. Returns 0 if 0 and non zero otherwise.
Definition: bmvmin.h:303
unsigned mem_used() const
Definition: bmvmin.h:457
size_type get_first() const
Returns index of the first ON bit.
Definition: bmvmin.h:371
size_type get_next(size_type idx) const
Returns index of next bit, which is ON.
Definition: bmvmin.h:394
void combine_sub(const bvector_mini &bvect)
Definition: bmvmin.h:446
size_type bit_count() const
Counts number of bits ON.
Definition: bmvmin.h:327
void combine_and(const bvector_mini &bvect)
Definition: bmvmin.h:416
void combine_or(const bvector_mini &bvect)
Definition: bmvmin.h:436
void combine_xor(const bvector_mini &bvect)
Definition: bmvmin.h:426
int compare(const bvector_mini &bvect)
Comparison.
Definition: bmvmin.h:340
void clear_bit(size_type pos)
Sets bit number pos to 0.
Definition: bmvmin.h:319
void set_bit(size_type pos)
Sets bit number pos to 1.
Definition: bmvmin.h:311
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
bm::bvector< Alloc > & bit_or(const bm::bvector< Alloc > &bv1, const bm::bvector< Alloc > &bv2, typename bm::bvector< Alloc >::optmode opt_mode=opt_none)
3-operand OR : this := bv1 OR bv2
Definition: bm.h:5906
bool test(size_type n) const noexcept
returns true if bit n is set and false is bit n is 0.
Definition: bm.h:1502
@ opt_compress
compress blocks when possible (GAP/prefix sum)
Definition: bm.h:137
@ opt_free_01
Free unused 0 and 1 blocks.
Definition: bm.h:136
@ opt_none
no optimization
Definition: bm.h:134
bool select(size_type rank, size_type &pos, const rs_index_type &rs_idx) const noexcept
select bit-vector position for the specified rank(bitcount)
Definition: bm.h:5283
friend class deserializer
Definition: bm.h:796
void set_gap_levels(const gap_word_t *glevel_len)
Sets new GAP lengths table. All GAP blocks will be reallocated to match the new scheme.
Definition: bm.h:3726
bool get_bit(size_type n) const noexcept
returns true if bit n is set and false is bit n is 0.
Definition: bm.h:3602
void merge(bm::bvector< Alloc > &bvect)
Merge/move content from another vector.
Definition: bm.h:5816
bvector< Alloc > & invert()
Invert/NEG all bits It should be noted, invert is affected by size() if size is set - it only inverts...
Definition: bm.h:3550
bool set_bit_and(size_type n, bool val=true)
Sets bit n using bit AND with the provided value.
Definition: bm.h:4213
allocator_type::allocator_pool_type allocator_pool_type
Definition: bm.h:118
bool clear_bit(size_type n)
Clears bit n.
Definition: bm.h:1247
bm::bvector< Alloc > & bit_and(const bm::bvector< Alloc > &bv1, const bm::bvector< Alloc > &bv2, typename bm::bvector< Alloc >::optmode opt_mode=opt_none)
3-operand AND : this := bv1 AND bv2
Definition: bm.h:6118
bool insert(size_type n, bool value)
Insert bit into specified position All the vector content after insert position is shifted right.
Definition: bm.h:5443
size_type rank(size_type n, const rs_index_type &rs_idx) const noexcept
Returns rank of specified bit position (same as count_to())
Definition: bm.h:1433
bool any() const noexcept
Returns true if any bits in this bitset are set, and otherwise returns false.
Definition: bm.h:2451
bool find_rank(size_type rank, size_type from, size_type &pos) const noexcept
Find bit-vector position for the specified rank(bitcount)
Definition: bm.h:5159
bvector< Alloc > & set(size_type n, bool val=true)
Sets bit n if val is true, clears bit n if val is false.
Definition: bm.h:4188
void resize(size_type new_size)
Change size of the bvector.
Definition: bm.h:2463
bm::bvector< Alloc > & bit_sub(const bm::bvector< Alloc > &bv1, const bm::bvector< Alloc > &bv2, typename bm::bvector< Alloc >::optmode opt_mode=opt_none)
3-operand SUB : this := bv1 MINUS bv2 SUBtraction is also known as AND NOT
Definition: bm.h:6330
bool empty() const noexcept
Returns true if the set is empty (no bits are set, otherwise returns false) Please note that this is ...
Definition: bm.h:1562
size_type size() const noexcept
Returns bvector's capacity (number of bits it can store)
Definition: bm.h:1300
void optimize(bm::word_t *temp_block=0, optmode opt_mode=opt_compress, statistics *stat=0)
Optimize memory bitvector's memory allocation.
Definition: bm.h:3635
void set_new_blocks_strat(strategy strat)
Sets new blocks allocation strategy.
Definition: bm.h:1912
bool find_range(size_type &first, size_type &last) const noexcept
Finds dynamic range of bit-vector [first, last].
Definition: bm.h:5139
void swap(bvector< Alloc > &bvect) noexcept
Exchanges content of bv and this bvector.
Definition: bm.h:3966
void fill_alloc_digest(bvector< Alloc > &bv_blocks) const
Calculate blocks digest vector (for diagnostics purposes) 1 is added if NB is a real,...
Definition: bm.h:4058
size_type count_range(size_type left, size_type right, const rs_index_type &rs_idx) const noexcept
Returns count of 1 bits in the given range [left..right] Uses rank-select index to accelerate the sea...
Definition: bm.h:3516
bool set_bit(size_type n, bool val=true)
Sets bit n.
Definition: bm.h:4227
insert_iterator inserter()
Definition: bm.h:1287
bool find(size_type &pos) const noexcept
Finds index of first 1 bit.
Definition: bm.h:5093
void keep_range(size_type left, size_type right)
Sets all bits to zero outside of the closed interval [left,right] Expected result: 00000....
Definition: bm.h:2353
bool find_first_mismatch(const bvector< Alloc > &bvect, size_type &pos, size_type search_to=bm::id_max) const noexcept
Find index of first bit different between this and the agr vector.
Definition: bm.h:3862
bvector_size_type size_type
Definition: bm.h:121
bvector< Alloc > & flip(size_type n)
Flips bit n.
Definition: bm.h:1273
void clear_range(size_type left, size_type right)
Sets all bits to zero in the specified closed interval [left,right] Interval must be inside the bvect...
Definition: bm.h:1216
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1871
bool is_ro() const noexcept
Returns true if vector is read-only.
Definition: bm.h:1060
bool find_reverse(size_type &pos) const noexcept
Finds last index of 1 bit.
Definition: bm.h:4911
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition: bm.h:1877
bool shift_left()
Shift left by 1 bit, fill with zero return carry out.
Definition: bm.h:5432
void freeze()
Turn current vector to read-only (immutable vector).
Definition: bm.h:8058
bvector< Alloc > & reset() noexcept
Clears every bit in the bitvector.
Definition: bm.h:1267
const blocks_manager_type & get_blocks_manager() const noexcept
get access to memory manager (internal) Use only if you are BitMagic library
Definition: bm.h:2058
size_type recalc_count() noexcept
Definition: bm.h:1477
void optimize_gap_size()
Optimize sizes of GAP blocks.
Definition: bm.h:3700
void init()
Explicit post-construction initialization. Must be caled to make sure safe use of *_no_check() method...
Definition: bm.h:2280
bool shift_right()
Shift right by 1 bit, fill with zero return carry out.
Definition: bm.h:5423
block_idx_type count_blocks(unsigned *arr) const noexcept
Computes bitcount values for all bvector blocks.
Definition: bm.h:2637
bvector< Alloc > & set_range(size_type left, size_type right, bool value=true)
Sets all bits in the specified closed interval [left,right] Interval must be inside the bvector's siz...
Definition: bm.h:2368
void set_allocator_pool(allocator_pool_type *pool_ptr) noexcept
Set allocator pool for local (non-th readed) memory cyclic(lots of alloc-free ops) opertations.
Definition: bm.h:1040
void erase(size_type n)
Erase bit in the specified position All the vector content after erase position is shifted left.
Definition: bm.h:5646
size_type extract_next(size_type prev)
Finds the number of the next bit ON and sets it to 0.
Definition: bm.h:1619
size_type get_next(size_type prev) const noexcept
Finds the number of the next bit ON.
Definition: bm.h:1609
void copy_range(const bvector< Alloc > &bvect, size_type left, size_type right)
Copy all bits in the specified closed interval [left,right].
Definition: bm.h:7981
enumerator get_enumerator(size_type pos) const
Returns enumerator pointing on specified or the next available bit.
Definition: bm.h:1883
void clear(const size_type *ids, size_type ids_size, bm::sort_order so=bm::BM_UNKNOWN)
clear list of bits in this bitset
Definition: bm.h:4149
bool equal(const bvector< Alloc > &bvect) const noexcept
Equal comparison with an agr bit-vector.
Definition: bm.h:2017
void optimize_range(size_type left, size_type right, bm::word_t *temp_block, optmode opt_mode=opt_compress)
Definition: bm.h:3671
void calc_stat(struct bm::bvector< Alloc >::statistics *st) const noexcept
Calculates bitvector statistics.
Definition: bm.h:3978
size_type get_first() const noexcept
find first 1 bit in vector. Function may return 0 and this requires an extra check if bit 0 is actual...
Definition: bm.h:1600
size_type count_to_test(size_type n, const rs_index_type &rs_idx) const noexcept
popcount in [0..right] range if test(right) == true
Definition: bm.h:3141
bool set_bit_conditional(size_type n, bool val, bool condition)
Sets bit n only if current value equals the condition.
Definition: bm.h:4199
int compare(const bvector< Alloc > &bvect) const noexcept
Lexicographical comparison with a bitvector.
Definition: bm.h:3744
void build_rs_index(rs_index_type *rs_idx, bvector< Alloc > *bv_blocks=0) const
compute running total of all blocks in bit vector (rank-select index)
Definition: bm.h:2501
bool any_range(size_type left, size_type right) const noexcept
Returns true if any bits in the range are 1s (non-empty interval) Function uses closed interval [left...
Definition: bm.h:3422
bool none() const noexcept
Returns true if no bits are set, otherwise returns false.
Definition: bm.h:1556
size_type count() const noexcept
population count (count of ON bits)
Definition: bm.h:2401
void keep(const size_type *ids, size_type ids_size, bm::sort_order so=bm::BM_UNKNOWN)
Keep list of bits in this bitset, others are cleared.
Definition: bm.h:4105
bm::bvector< Alloc > & bit_or_and(const bm::bvector< Alloc > &bv1, const bm::bvector< Alloc > &bv2, typename bm::bvector< Alloc >::optmode opt_mode=opt_none)
3-operand AND where result is ORed into the terget vector : this |= bv1 AND bv2 TARGET := TARGET OR (...
Definition: bm.h:6213
bm::bvector< Alloc > & bit_xor(const bm::bvector< Alloc > &bv1, const bm::bvector< Alloc > &bv2, typename bm::bvector< Alloc >::optmode opt_mode=opt_none)
3-operand XOR : this := bv1 XOR bv2
Definition: bm.h:6005
bool is_all_one_range(size_type left, size_type right) const noexcept
Returns true if all bits in the range are 1s (saturated interval) Function uses closed interval [left...
Definition: bm.h:3338
bool inc(size_type n)
Increment the specified element.
Definition: bm.h:4751
void set_bit_no_check(size_type n)
Set bit without checking preconditions (size, etc)
Definition: bm.h:4646
size_type count_to(size_type n, const rs_index_type &rs_idx) const noexcept
Returns count of 1 bits (population) in [0..right] range.
Definition: bm.h:3088
Mini bit-vector for auxiliary purposes.
Definition: bmvmin.h:214
void set(bm::id_t n, bool val=true)
Definition: bmvmin.h:234
Bit-bector prefix sum address resolver using bit-vector prefix sum as a space compactor.
void sync(bool force=false)
Re-calculate prefix sum table (same as calc_prefix_sum)
bool in_sync() const
returns true if prefix sum table is in sync with the vector
bool equal(const bvps_addr_resolver &addr_res) const noexcept
equality comparison
void move_from(bvps_addr_resolver &addr_res) noexcept
Move content from the argument address resolver.
void optimize(bm::word_t *temp_block=0)
optimize underlying bit-vector
void set(size_type id_from)
Set id (bit) to address resolver.
bool resolve(size_type id_from, size_type *id_to) const noexcept
Resolve id to integer id (address)
size_t size() const noexcept
Get buffer size.
Definition: bmbuffer.h:54
const unsigned char * buf() const noexcept
Get read access to buffer memory.
Definition: bmbuffer.h:57
size_t capacity() const noexcept
Get buffer capacity.
Definition: bmbuffer.h:197
void optimize()
try to shrink the capacity to more optimal size
Definition: bmbuffer.h:253
void reinit(size_t new_capacity)
reserve new capacity (buffer content NOT preserved, size set to 0)
Definition: bmbuffer.h:239
void release()
Free underlying memory.
Definition: bmbuffer.h:177
void reserve(size_t new_capacity)
reserve new capacity (buffer content preserved)
Definition: bmbuffer.h:223
void copy_from(const unsigned char *in_buf, size_t in_size)
copy data from an external buffer
Definition: bmbuffer.h:185
Utility class to collect performance measurements and statistics.
Definition: bmtimer.h:41
Compressed (sparse collection of objects)
void calc_stat(statistics *st) const
compute statistics on memory consumption
bool move_buffer(typename parent_type::key_type key, buffer_type &buffer)
move external buffer into collection
Deseriaizer for compressed collections.
int deserialize(CBC &buffer_coll, const unsigned char *buf, bm::word_t *temp_block=0)
Seriaizer for compressed collections.
void serialize(const CBC &buffer_coll, buffer_type &buf, bm::word_t *temp_block=0)
Serialize compressed collection into memory buffer.
Compressed (sparse collection of objects)
bool push_back(key_type key, const value_type &val)
Add new value to compressed collection.
void optimize(bm::word_t *temp_block=0)
perform memory optimizations/compression
void sync()
Checkpoint method to prepare collection for reading.
size_t size() const
size of collection
bool equal(const compressed_collection< Value, BV > &ccoll) const
perform equality comparison with another collection
const value_type & get(address_type addr) const
Get access to associated value by resolved address.
bool resolve(key_type key, address_type *addr) const
Resolve key address (index) in the dense vector.
Parallel plan builder for the XOR filter scanner.
void build_plan(task_batch &batch, bm::xor_sim_model< BV > &sim_model, const bv_ref_vector_type &ref_vect, const bm::xor_sim_params &xs_params)
Construct task batch for parallel execution.
bm::id64_t get_h64() noexcept
Read h-64-bit.
Definition: encoding.h:691
unsigned char get_8() noexcept
Reads character from the decoding buffer.
Definition: encoding.h:93
Class for decoding data from memory buffer.
Definition: encoding.h:126
bm::id64_t get_48() noexcept
Reads 64-bit word from the decoding buffer.
Definition: encoding.h:769
bm::word_t get_24() noexcept
Reads 32-bit word from the decoding buffer.
Definition: encoding.h:738
Deserializer for bit-vector.
Definition: bmserial.h:570
size_t deserialize(bvector_type &bv, const unsigned char *buf, bm::word_t *temp_block=0)
Definition: bmserial.h:4122
Heap allocated dynamic resizable scalar-type matrix.
Definition: bmbuffer.h:720
void set(size_type row_idx, size_type col_idx, value_type v) noexcept
Definition: bmbuffer.h:835
const value_type * row(size_type row_idx) const noexcept
Definition: bmbuffer.h:802
bool equal(const dynamic_heap_matrix< Val, BVAlloc > &dhm) const noexcept
Check if two matrix objects matches on the content.
Definition: bmbuffer.h:892
value_type get(size_type row_idx, size_type col_idx) noexcept
Definition: bmbuffer.h:819
size_type rows() const noexcept
Definition: bmbuffer.h:756
size_type cols() const noexcept
Definition: bmbuffer.h:757
void set_zero() noexcept
memset all buffer to all zeroes
Definition: bmbuffer.h:844
void replicate_triange() noexcept
copy values of the left triangle elements to the right triangle (operation specific to matrices with ...
Definition: bmbuffer.h:877
void resize(size_type rows_in, size_type cols_in, bool copy_content=true)
Definition: bmbuffer.h:759
void sum(ACC &acc, size_type row_idx) const noexcept
Sum of row elements.
Definition: bmbuffer.h:955
void init(bool set_z=false)
Post construction allocation, initialization.
Definition: bmbuffer.h:740
Memory encoding.
Definition: encoding.h:50
void put_24(bm::word_t w) noexcept
Puts 24 bits word into encoding buffer.
Definition: encoding.h:555
void put_48(bm::id64_t w) noexcept
Puts 48 bits word into encoding buffer.
Definition: encoding.h:589
size_t size() const noexcept
Returns size of the current encoding stream.
Definition: encoding.h:529
void put_8(unsigned char c) noexcept
Puts one character into the encoding buffer.
Definition: encoding.h:434
void put_h64(bm::id64_t w) noexcept
Puts 64 bits word into encoding buffer with h-compression.
Definition: encoding.h:628
Elias Gamma decoder.
Definition: bmgamma.h:43
Functor for Elias Gamma encoding.
Definition: encoding.h:341
Bit-plane splicing of a GAP block.
Definition: bmtrans.h:742
void compute_distance_matrix()
Definition: bmtrans.h:801
tmatrix_type tmatrix_
Definition: bmtrans.h:851
Heap allocated scalar-type matrix.
Definition: bmbuffer.h:600
const value_type * row(size_type row_idx) const noexcept
Definition: bmbuffer.h:665
void init(bool set_z=false)
Post construction allocation, initialization.
Definition: bmbuffer.h:644
static size_t cols() noexcept
Definition: bmbuffer.h:616
Simple heap allocated vector based on bvector allocator.
Definition: bmbuffer.h:324
void resize(size_type new_size)
vector resize
Definition: bmbuffer.h:462
value_type & at(size_type pos)
Definition: bmbuffer.h:400
value_type & add()
Add element to the end of the vector, return reference.
Definition: bmbuffer.h:521
size_type size() const noexcept
Definition: bmbuffer.h:421
void push_back(const value_type &v)
push new element to the back of the vector
Definition: bmbuffer.h:534
void swap(heap_vector< Val, BVAlloc, trivial_type > &other) noexcept
Definition: bmbuffer.h:383
void reserve(size_type new_size)
Definition: bmbuffer.h:436
forward iterator class to traverse bit-vector as ranges
Definition: bmintervals.h:53
const pair_type & get() const noexcept
Get interval pair.
Definition: bmintervals.h:161
bool valid() const noexcept
Returns true if enumerator is valid (false if traversal is done)
Definition: bmintervals.h:568
size_type start() const noexcept
Return interval start/left as bit-vector coordinate 011110 [left..right].
Definition: bmintervals.h:551
size_type end() const noexcept
Return interval end/right as bit-vector coordinate 011110 [left..right].
Definition: bmintervals.h:560
void swap(interval_enumerator< BV > &ien) noexcept
swap enumerator with another one
Definition: bmintervals.h:757
bool go_to(size_type pos, bool extend_start=true)
Go to inetrval at specified position Jump to position with interval. If interval is not available at ...
Definition: bmintervals.h:584
BM style allocator adapter.
Definition: bmalloc.h:290
Template class implements memory saving set functionality.
Definition: bmvmin.h:53
void set(bm::id_t n, bool val=true)
Definition: bmvmin.h:100
Deserializer, performs logical operations between bit-vector and serialized bit-vector.
Definition: bmserial.h:927
void set_ref_vectors(const bv_ref_vector_type *ref_vect)
Attach collection of reference vectors for XOR serialization (no transfer of ownership for the pointe...
Definition: bmserial.h:1009
void deserialize_range(bvector_type &bv, const unsigned char *buf, size_type idx_from, size_type idx_to)
Definition: bmserial.h:6661
size_type deserialize(bvector_type &bv, const unsigned char *buf, set_operation op, bool exit_on_one=false)
Deserialize bvector using buffer as set operation argument.
Definition: bmserial.h:6581
Builder class to prepare a batch of tasks for parallel optimization of a sparse vector.
static void build_plan(task_batch &batch, sparse_vector_type &sv, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, typename sparse_vector_type::statistics *st=0)
Build paralell optimization batch (of tasks) for sparse vector.
void sample(BV &bv_out, const BV &bv_in, size_type sample_count)
Get random subset of input vector.
Definition: bmrandom.h:140
Algorithms for rank compression of bit-vector.
Definition: bmalgo.h:453
void decompress(BV &bv_target, const BV &bv_idx, const BV &bv_src)
Rank decompression.
Definition: bmalgo.h:570
void compress_by_source(BV &bv_target, const BV &bv_idx, const rs_index_type &bc_idx, const BV &bv_src)
Source vector priority + index based rank.
Definition: bmalgo.h:647
void compress(BV &bv_target, const BV &bv_idx, const BV &bv_src)
Rank compression algorithm based on two palallel iterators/enumerators set of source vector gets re-m...
Definition: bmalgo.h:497
Rank-Select acceleration index.
Definition: bmrs.h:41
size_type super_block_size() const BMNOEXCEPT
return number of super-blocks
Definition: bmrs.h:667
unsigned get_super_block_count(unsigned i) const BMNOEXCEPT
return bit-count in super-block
Definition: bmrs.h:625
void set_super_block(unsigned i, size_type bcount) BMNOEXCEPT
set super block count
Definition: bmrs.h:605
size_type get_super_block_rcount(unsigned i) const BMNOEXCEPT
return running bit-count in super-block
Definition: bmrs.h:640
size_type rcount(block_idx_type nb) const
return running bit-count for specified block
Definition: bmrs.h:361
void resize(block_idx_type new_size)
reserve the capacity for block count
Definition: bmrs.h:296
unsigned find_super_block(size_type rank) const BMNOEXCEPT
Find super-block with specified rank.
Definition: bmrs.h:650
bm::id_t block_idx_type
Definition: bmrs.h:51
void resize_effective_super_blocks(size_type sb_eff)
set size of true super-blocks (not NULL and not FFFFF)
Definition: bmrs.h:679
void set_full_super_block(unsigned i) BMNOEXCEPT
set FULL (all bits set super-block)
Definition: bmrs.h:597
unsigned count(block_idx_type nb) const
return bit-count for specified block
Definition: bmrs.h:324
bool find(size_type *rank, block_idx_type *nb, bm::gap_word_t *sub_range) const
find block position and sub-range for the specified rank
Definition: bmrs.h:492
void set_null_super_block(unsigned i) BMNOEXCEPT
set empty (no bits set super-block)
Definition: bmrs.h:586
bm::id_t size_type
Definition: bmrs.h:50
void set_total(size_type t)
set total blocks
Definition: bmrs.h:113
void register_super_block(unsigned i, const unsigned *bcount, const bm::id64_t *sub_count)
Add block list belonging to one super block.
Definition: bmrs.h:688
Back insert iterator implements buffered insert, faster than generic access assignment.
void flush()
flush the accumulated buffer
void add(value_type v)
add value to the container
void add_null() noexcept
add NULL (no-value) to the container
Const iterator to traverse the rsc sparse vector.
bool is_null() const noexcept
Get NULL status.
bool valid() const noexcept
Returns true if iterator is at a valid position.
bool advance() noexcept
advance iterator forward by one
value_type value() const
Get current position (value)
Rank-Select compressed sparse vector.
void load_from(const sparse_vector_type &sv_src)
Load compressed vector from a sparse vector (with NULLs)
void sync_size() noexcept
recalculate size to exclude tail NULL elements After this call size() will return the true size of th...
void merge_not_null(rsc_sparse_vector< Val, SV > &csv)
merge two vectors (argument gets destroyed) It is important that both vectors have the same NULL vect...
bool try_get(size_type idx, value_type &v) const noexcept
get specified element with NOT NULL check
size_type decode(value_type *arr, size_type idx_from, size_type size, bool zero_mem=true) const
C-style decode.
void freeze()
Turn sparse vector into immutable mode Read-only (immutable) vector uses less memory and allows faste...
void push_back(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
void clear(const bvector_type &bv_idx)
Set vector elements spcified by argument bit-vector to zero Note that set to 0 elements are NOT going...
bool equal(const rsc_sparse_vector< Val, SV > &csv) const noexcept
check if another vector has the same content
back_insert_iterator get_back_inserter()
void load_to(sparse_vector_type &sv) const
Exort compressed vector to a sparse vector (with NULLs)
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
value_type at(size_type idx) const
access specified element with bounds checking
void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, statistics *stat=0)
run memory optimization for all vector slices
const_iterator end() const noexcept
Provide const iterator access to the end.
void sync(bool force)
Re-calculate rank-select index for faster access to vector.
const bvector_type * get_null_bvector() const noexcept
Get bit-vector of assigned values (or NULL)
bool is_ro() const noexcept
Returns true if vector is read-only.
bool resolve(size_type idx, size_type *idx_to) const noexcept
Resolve logical address to access via rank compressed address.
void set_null(size_type idx)
set specified element to NULL RSC vector actually erases element when it is set to NULL (expensive).
size_type count_range_notnull(size_type left, size_type right) const noexcept
void resize(size_type new_size)
change vector size
size_type decode_buf(value_type *arr, value_type *arr_buf_tmp, size_type idx_from, size_type size, bool zero_mem=true) const noexcept
C-style decode (variant with external memory) Analog of decode, but requires two arrays.
value_type get(size_type idx) const noexcept
get specified element without bounds checking
bool is_null(size_type idx) const noexcept
test if specified element is NULL
unsigned_value_type get_unsigned_bits(size_type idx, size_type N_bits) const noexcept
Get raw unsigned value first N bits.
void push_back_null(size_type count)
push back specified amount of NULL values
void copy_range(const rsc_sparse_vector< Val, SV > &csv, size_type left, size_type right)
copy range of values from another sparse vector
void inc(size_type idx)
increment specified element by one
size_type gather(value_type *arr, const size_type *idx, size_type *idx_tmp_buf, size_type size, bm::sort_order sorted_idx) const
Gather elements to a C-style array.
size_type size() const noexcept
return size of the vector
SV::bvector_type bvector_type
bool in_sync() const noexcept
returns true if prefix sum table is in sync with the vector
const rs_index_type * get_RS() const noexcept
const_iterator begin() const
Provide const iterator access to container content.
Bit-vector serialization class.
Definition: bmserial.h:76
void set_sim_model(const xor_sim_model_type *sim_model) noexcept
Atach XOR similarity model (must be computed by the same ref vector)
Definition: bmserial.h:1321
void set_bookmarks(bool enable, unsigned bm_interval=256) noexcept
Add skip-markers to serialization BLOB for faster range decode at the expense of some BLOB size incre...
Definition: bmserial.h:1284
void byte_order_serialization(bool value) noexcept
Set byte-order serialization (for cross platform compatibility)
Definition: bmserial.h:1278
void optimize_serialize_destroy(BV &bv, typename serializer< BV >::buffer &buf)
Bitvector serialization into buffer object (resized automatically) Input bit-vector gets optimized an...
Definition: bmserial.h:2265
void set_compression_level(unsigned clevel) noexcept
Set compression level.
Definition: bmserial.h:1251
bool compute_sim_model(xor_sim_model_type &sim_model, const bv_ref_vector_type &ref_vect, const bm::xor_sim_params &params)
Calculate XOR similarity model for ref_vector refernece vector must be associated before.
Definition: bmserial.h:1313
void gap_length_serialization(bool value) noexcept
Set GAP length serialization (serializes GAP levels of the original vector)
Definition: bmserial.h:1272
const size_type * get_compression_stat() const noexcept
Return serialization counter vector.
Definition: bmserial.h:196
void set_ref_vectors(const bv_ref_vector_type *ref_vect)
Attach collection of reference vectors for XOR serialization (no transfer of ownership for the pointe...
Definition: bmserial.h:1299
void set_curr_ref_idx(size_type ref_idx) noexcept
Set current index in rer.vector collection (not a row idx or plain idx)
Definition: bmserial.h:1327
size_type serialize(const BV &bv, unsigned char *buf, size_t buf_size)
Bitvector serialization into memory block.
Definition: bmserial.h:2703
Integer set to set transformation (functional image in groups theory) https://en.wikipedia....
void remap(const bvector_type &bv_in, bvector_type &bv_out)
Perform remapping (Image function) (based on attached translation table)
void run(const bvector_type &bv_in, const SV &sv_brel, bvector_type &bv_out)
Run remap transformation.
Simple queue based on memory controlled dynamic vector.
Definition: bmbuffer.h:1007
Back insert iterator implements buffered insert, faster than generic access assignment.
Definition: bmsparsevec.h:256
void add_null()
add NULL (no-value) to the container
Definition: bmsparsevec.h:2567
bool flush()
flush the accumulated buffer
Definition: bmsparsevec.h:2602
bool empty() const
return true if insertion buffer is empty
Definition: bmsparsevec.h:2594
Const iterator to traverse the sparse vector.
Definition: bmsparsevec.h:158
bool is_null() const noexcept
Get NULL status.
Definition: bmsparsevec.h:2449
bool advance() noexcept
advance iterator forward by one
Definition: bmsparsevec.h:2380
sparse vector de-serializer
void deserialize(SV &sv, const unsigned char *buf, bool clear_sv=true)
void deserialize_structure(SV &sv, const unsigned char *buf)
void set_xor_ref(bv_ref_vector_type *bv_ref_ptr)
Set external XOR reference vectors (data frame referenece vectors)
void set_finalization(bm::finalization is_final)
Set deserialization finalization to force deserialized vectors into READONLY (or READWRITE) mode.
void deserialize_range(SV &sv, const unsigned char *buf, size_type from, size_type to, bool clear_sv=true)
Pipeline to run multiple searches against a particular SV faster using cache optimizations.
void complete()
Prepare pipeline for the execution (resize and init internal structures) Once complete,...
bvect_vector_type & get_bv_res_vector() noexcept
Return results vector used for pipeline execution.
void add(const typename SV::value_type *str)
Add new search string.
algorithms for sparse_vector scan/search
SV::bvector_type bvector_type
void find_eq(const SV &sv, value_type value, bvector_type &bv_out)
find all sparse vector elements EQ to search value
bool bfind(const SV &sv, const value_type val, size_type &pos)
binary search for position in the sorted sparse vector
void bind(const SV &sv, bool sorted)
bind sparse vector for all searches
void find_range(const SV &sv, value_type from, value_type to, bvector_type &bv_out)
find all elements sparse vector element in closed range [left..right] interval
void find_gt(const SV &sv, value_type val, bvector_type &bv_out)
find all elements sparse vector element greater (>) than value
void find_lt(const SV &sv, value_type val, bvector_type &bv_out)
find all elements sparse vector element less (<) than value
bool bfind_eq_str(const SV &sv, const value_type *str, size_type &pos)
binary find first sparse vector element (string).
void find_gt_horizontal(const SV &sv, value_type value, bvector_type &bv_out, bool null_correct=true)
For testing purposes only.
bool lower_bound_str(const SV &sv, const value_type *str, size_type &pos)
lower bound search for an array position
bool find_eq_str(const SV &sv, const value_type *str, bvector_type &bv_out)
find sparse vector elements (string)
void find_nonzero(const SV &sv, bvector_type &bv_out)
Find non-zero elements Output vector is computed as a logical OR (join) of all planes.
void invert(const SV &sv, bvector_type &bv_out)
invert search result ("EQ" to "not EQ")
bool find_eq_str_prefix(const SV &sv, const value_type *str, bvector_type &bv_out)
find sparse vector elements with a given prefix (string)
void find_le(const SV &sv, value_type val, bvector_type &bv_out)
find all elements sparse vector element less or equal (<=) than value
void find_ge(const SV &sv, value_type val, bvector_type &bv_out)
find all elements sparse vector element greater or equal (>=) than value
Serialize sparse vector into a memory buffer(s) structure.
void set_bookmarks(bool enable, unsigned bm_interval=256) noexcept
Add skip-markers for faster range deserialization.
void set_sim_model(const xor_sim_model_type *sim_model) noexcept
Attach serizalizer to a pre-computed similarity model.
bool is_xor_ref() const noexcept
Returns the XOR reference compression status (enabled/disabled)
void compute_sim_model(xor_sim_model_type &sim_model, const bv_ref_vector_type &ref_vect, const bm::xor_sim_params &params)
Calculate XOR similarity model for ref_vector refernece vector must be associated before.
void set_xor_ref(bool is_enabled) noexcept
Turn ON and OFF XOR compression of sparse vectors Enables XOR reference compression for the sparse ve...
void serialize(const SV &sv, sparse_vector_serial_layout< SV > &sv_layout)
Serialize sparse vector into a memory buffer(s) structure.
void enable_xor_compression() noexcept
Enable XOR compression on vector serialization.
void disable_xor_compression() noexcept
Disable XOR compression on serialization.
bm::serializer< bvector_type > & get_bv_serializer() noexcept
Get access to the underlying bit-vector serializer This access can be used to fine tune compression s...
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition: bmsparsevec.h:87
void inc(size_type idx)
increment specified element by one
Definition: bmsparsevec.h:2052
bvector_type::size_type size_type
Definition: bmsparsevec.h:92
value_type at(size_type idx) const
access specified element with bounds checking
Definition: bmsparsevec.h:1747
void set_null(size_type idx)
set specified element to unassigned value (NULL)
Definition: bmsparsevec.h:1164
unsigned_value_type get_unsigned_bits(size_type idx, size_type N_bits) const noexcept
Get raw unsigned value first N bits.
Definition: bmsparsevec.h:1809
const_iterator begin() const noexcept
Provide const iterator access to container content.
Definition: bmsparsevec.h:2310
void sync_size() noexcept
recalculate size to exclude tail NULL elements After this call size() will return the true size of th...
Definition: bmsparsevec.h:1300
void push_back(value_type v)
push value back into vector
Definition: bmsparsevec.h:1883
bool equal(const sparse_vector< Val, BV > &sv, bm::null_support null_able=bm::use_null) const noexcept
check if another sparse vector has the same content and size
Definition: bmsparsevec.h:2300
value_type get(size_type idx) const noexcept
get specified element without bounds checking
Definition: bmsparsevec.h:1758
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
Definition: bmsparsevec.h:1849
void resize(size_type sz)
resize vector
Definition: bmsparsevec.h:739
void clear(size_type idx, bool set_null)
clear specified element with bounds checking and automatic resize
Definition: bmsparsevec.h:1865
back_insert_iterator get_back_inserter()
Provide back insert iterator Back insert iterator implements buffered insertion, which is faster,...
Definition: bmsparsevec.h:588
void swap(size_type idx1, size_type idx2)
swap two vector elements between each other
Definition: bmsparsevec.h:1904
size_type gather(value_type *arr, const size_type *idx, size_type size, bm::sort_order sorted_idx) const
Gather elements to a C-style array.
Definition: bmsparsevec.h:1368
const_iterator end() const noexcept
Provide const iterator access to the end.
Definition: bmsparsevec.h:575
void calc_stat(struct sparse_vector< Val, BV >::statistics *st) const noexcept
Calculates memory statistics.
Definition: bmsparsevec.h:2132
void push_back_null(size_type count)
push back specified amount of NULL values
Definition: bmsparsevec.h:1892
sparse_vector< Val, BV > & merge(sparse_vector< Val, BV > &sv)
merge with another sparse vector using OR operation Merge is different from join(),...
Definition: bmsparsevec.h:2208
void freeze()
Turn sparse vector into immutable mode Read-only (immutable) vector uses less memory and allows faste...
Definition: bmsparsevec.h:825
sparse_vector< Val, BV > & join(const sparse_vector< Val, BV > &sv)
join all with another sparse vector using OR operation
Definition: bmsparsevec.h:2181
void insert(size_type idx, value_type v)
insert specified element into container
Definition: bmsparsevec.h:1916
void import_back(const value_type *arr, size_type arr_size, bool set_not_null=true)
Import list of elements from a C-style array (pushed back)
Definition: bmsparsevec.h:1312
size_type extract(value_type *arr, size_type size, size_type offset=0, bool zero_mem=true) const
Bulk export list of elements to a C-style array.
Definition: bmsparsevec.h:1660
size_type extract_range(value_type *arr, size_type size, size_type offset, bool zero_mem=true) const
extract small window without use of masking vector
Definition: bmsparsevec.h:1533
size_type extract_planes(value_type *arr, size_type size, size_type offset, bool zero_mem=true) const
extract medium window without use of masking vector
Definition: bmsparsevec.h:1618
void keep_range(size_type left, size_type right, bm::null_support slice_null=bm::use_null)
Keep only specified interval in the sparse vector, clear all other elements.
Definition: bmsparsevec.h:2263
void clear_all(bool free_mem, unsigned) noexcept
resize to zero, free memory
Definition: bmsparsevec.h:2101
bool try_get(size_type idx, value_type &v) const noexcept
get specified element with NOT NULL check
Definition: bmsparsevec.h:1836
sparse_vector< Val, BV > & clear_range(size_type left, size_type right, bool set_null=false)
clear range (assign bit 0 for all planes)
Definition: bmsparsevec.h:2120
void erase(size_type idx, bool erase_null=true)
erase specified element from container
Definition: bmsparsevec.h:1975
size_type decode(value_type *arr, size_type idx_from, size_type dec_size, bool zero_mem=true) const
Bulk export list of elements to a C-style array.
Definition: bmsparsevec.h:1356
bool is_ro() const noexcept
Returns true if vector is read-only.
Definition: bmsparsevec.h:828
void copy_range(const sparse_vector< Val, BV > &sv, size_type left, size_type right, bm::null_support slice_null=bm::use_null)
copy range of values from another sparse vector
Definition: bmsparsevec.h:2248
void import(const value_type *arr, size_type arr_size, size_type offset=0, bool set_not_null=true)
Import list of elements from a C-style array.
Definition: bmsparsevec.h:1172
void filter(const bvector_type &bv_mask)
Apply value filter, defined by mask vector.
Definition: bmsparsevec.h:2275
size_type size() const noexcept
return size of the vector
Definition: bmsparsevec.h:729
void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, typename sparse_vector< Val, BV >::statistics *stat=0)
run memory optimization for all vector planes
Definition: bmsparsevec.h:2148
Back insert iterator implements buffered insert, faster than generic access assignment.
void flush()
flush the accumulated buffer.
void set_remap(bool flag) noexcept
Method to configure back inserter to collect statistics on optimal character codes.
void add_null()
add NULL (no-value) to the container
void set_optimize(typename bvector_type::optmode opt_mode) noexcept
Set optimization on load option (deafult: false)
Const iterator to do quick traverse of the sparse vector.
const value_type * value() const
Get zero terminated string value at the current position.
bool is_null() const noexcept
Get NULL status.
void go_to(size_type pos) noexcept
re-position to a specified position
void set_substr(unsigned from, unsigned len=0) noexcept
setup iterator to retrieve a sub-string of a string
bool valid() const noexcept
Returns true if iterator is at a valid position.
Reference class to access elements via common [] operator.
succinct sparse vector for strings with compression using bit-slicing ( transposition) method
void insert(size_type idx, const value_type *str)
insert the specified element
void calc_octet_stat(octet_freq_matrix_type &octet_matrix) const
void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, typename str_sparse_vector< CharType, BV, STR_SIZE >::statistics *stat=0)
run memory optimization for all vector planes
size_type get(size_type idx, value_type *str, size_type buf_size) const noexcept
get specified element
void resize(size_type sz)
resize vector
static bool remap_tosv(value_type *sv_str, size_type buf_size, const value_type *str, const slice_octet_matrix_type &octet_remap_matrix2) noexcept
const_iterator get_const_iterator(size_type idx) const noexcept
Get const_itertor re-positioned to specific element.
bool is_ro() const noexcept
Returns true if vector is read-only.
void set_null(size_type idx)
set NULL status for the specified element Vector is resized automatically
str_sparse_vector< CharType, BV, STR_SIZE > & merge(str_sparse_vector< CharType, BV, STR_SIZE > &str_sv)
merge with another sparse vector using OR operation Merge is different from join(),...
void assign(size_type idx, const StrType &str)
set specified element with bounds checking and automatic resize
void remap_from(const str_sparse_vector &str_sv, octet_freq_matrix_type *omatrix=0)
Build remapping profile and load content from another sparse vector Remapped vector likely saves memo...
size_type size() const
return size of the vector
void build_octet_remap(slice_octet_matrix_type &octet_remap_matrix1, slice_octet_matrix_type &octet_remap_matrix2, octet_freq_matrix_type &octet_occupancy_matrix) const
bool is_remap() const noexcept
Get character remapping status (true | false)
unsigned common_prefix_length(size_type idx1, size_type idx2, value_type *prefix_buf=0) const noexcept
Find size of common prefix between two vector elements in octets.
void push_back(const StrType &str)
push back a string
void clear(const bvector_type &bv_idx)
Set vector elements spcified by argument bit-vector to empty Note that set to empty elements are NOT ...
size_type decode(CharMatrix &cmatr, size_type idx_from, size_type dec_size, bool zero_mem=true) const
Bulk export strings to a C-style matrix of chars.
bool try_get(size_type idx, StrType &str) const
get specified string element if NOT NULL Template method expects an STL-compatible type basic_string<...
void erase(size_type idx)
erase the specified element
size_type decode_substr(CharMatrix &cmatr, size_type idx_from, size_type dec_size, unsigned substr_from, unsigned substr_to, bool zero_mem=true) const
Bulk export strings to a C-style matrix of chars.
static bool remap_fromsv(value_type *str, size_type buf_size, const value_type *sv_str, const slice_octet_matrix_type &octet_remap_matrix1) noexcept
void remap()
Build remapping profile and re-load content to save memory.
back_insert_iterator get_back_inserter()
Provide back insert iterator Back insert iterator implements buffered insertion, which is faster,...
const_iterator begin() const noexcept
Provide const iterator access to container content.
void calc_stat(struct str_sparse_vector< CharType, BV, STR_SIZE >::statistics *st) const noexcept
Calculates memory statistics.
void copy_range(const str_sparse_vector< CharType, BV, STR_SIZE > &sv, size_type left, size_type right, bm::null_support slice_null=bm::use_null)
copy range of values from another sparse vector
bool equal(const str_sparse_vector< CharType, BV, STR_SIZE > &sv, bm::null_support null_able=bm::use_null) const noexcept
check if another sparse vector has the same content and size
void import(CharMatrix &cmatr, size_type idx_from, size_type imp_size)
Bulk import of strings from a C-style matrix of chars.
str_sparse_vector< CharType, BV, STR_SIZE > & clear_range(size_type left, size_type right, bool set_null=false)
clear range (assign bit 0 for all planes)
void swap(size_type idx1, size_type idx2)
swap two vector elements between each other
void set(size_type idx, const value_type *str)
set specified element with bounds checking and automatic resize
const_iterator end() const noexcept
Provide const iterator access to the end.
size_type effective_max_str() const noexcept
get effective string length used in vector Calculate and returns efficiency, how close are we to the ...
int compare(size_type idx, const value_type *str) const noexcept
Compare vector element with argument lexicographically.
void keep_range(size_type left, size_type right, bm::null_support slice_null=bm::use_null)
Keep only specified interval in the sparse vector, clear all other elements.
bvector_type::size_type size_type
sparse vector based address resolver (no space compactor, just bit-plane compressors provided by spar...
bool resolve(size_type id_from, size_type *id_to) const
Resolve id to integer id (address)
void set(size_type id_from)
Set id (bit) to address resolver.
void optimize(bm::word_t *temp_block=0)
optimize underlying sparse vectors
Index for SV sorted vectors for approximate range queries.
size_type sv_size() const noexcept
Original SV size.
size_type size() const noexcept
Index size (number of sampled elements)
void recalc_range(const value_type *search_str, size_type &l, size_type &r) const noexcept
recalculate range into SV coordinates range [from..to)
bool bfind_range(const value_type *search_str, size_t in_len, size_type &l, size_type &r) const noexcept
find range (binary)
bool is_unique() const noexcept
returns true if all index values are unique
void construct(const SV &sv, unsigned s_factor)
Build sampling index for the sorted sprase vector.
Basic implementation for collection of tasks for parallel execution.
Definition: bmtask.h:140
task_vector_type & get_task_vector() noexcept
Get access to internal task vector.
Definition: bmtask.h:168
virtual size_type size() const noexcept
task_batch_base intreface implementation
Definition: bmtask.h:158
std::vector< bm::task_descr > task_vector_type
Definition: bmtask.h:146
Utility class to submit task batch to the running thread pool and optionally wait for it getting done...
Definition: bmthreadpool.h:331
static void run(thread_pool_type &tpool, bm::task_batch_base &tasks, bool wait_for_batch)
Definition: bmthreadpool.h:494
Thread pool with custom (thread safe) queue.
Definition: bmthreadpool.h:239
void start(unsigned tcount)
Start thread pool worker threads.
Definition: bmthreadpool.h:387
XOR scanner to search for complement-similarities in collections of bit-vectors.
Definition: bmxor.h:820
bm::id64_t get_xor_digest() const noexcept
Definition: bmxor.h:952
bm::xor_complement_match search_best_xor_mask(const bm::word_t *s_block, size_type ri, size_type ridx_from, size_type ridx_to, unsigned i, unsigned j, bm::word_t *tx_block, const bm::xor_sim_params &params)
Scan for all candidate bit-blocks to find mask or match.
Definition: bmxor.h:1246
bool compute_sim_model(xor_sim_model< BV > &sim_model, const bv_ref_vector_type &ref_vect, const bm::xor_sim_params &params)
Calculate matrix of best XOR match metrics per block for the attached collection of bit-vectors.
Definition: bmxor.h:1440
size_type found_ridx() const noexcept
Definition: bmxor.h:942
void compute_xor_complexity_descr(const bm::word_t *block, bm::id64_t block_d64, const bm::word_t *xor_block, bm::block_waves_xor_descr &x_descr, bm::block_xor_match_descr &xmd) const noexcept
Compute reference complexity descriptor based on XOR vector.
Definition: bmxor.h:1070
unsigned get_s_bc() const noexcept
Definition: bmxor.h:954
void compute_s_block_stats(const bm::word_t *block) noexcept
Compute statistics for the r-(or s-) block.
Definition: bmxor.h:1057
void set_ref_vector(const bv_ref_vector_type *ref_vect) noexcept
Definition: bmxor.h:850
static bm::xor_complement_match best_metric(unsigned bc, unsigned gc, unsigned *best_metric) noexcept
Definition: bmxor.h:1641
const bv_ref_vector_type & get_ref_vector() const noexcept
Definition: bmxor.h:853
unsigned get_s_block_best() const noexcept
Definition: bmxor.h:956
unsigned get_x_best_metric() const noexcept
Definition: bmxor.h:951
unsigned get_s_gc() const noexcept
Definition: bmxor.h:955
static void deallocate(bm::word_t *p, size_t n)
Definition: stress32.cpp:142
static size_t na_
Definition: stress32.cpp:113
static bm::word_t * allocate(size_t n, const void *)
Definition: stress32.cpp:116
static size_t balance() BMNOEXCEPT
Definition: stress32.cpp:159
static void * allocate(size_t n, const void *)
Definition: stress32.cpp:168
static void deallocate(void *p, size_t n)
Definition: stress32.cpp:196
static size_t balance() BMNOEXCEPT
Definition: stress32.cpp:216
static size_t na_
Definition: stress32.cpp:165
GAP vector is designed to be very small.
Definition: rlebtv.h:31
void combine_sub(const gap_vector &vect)
Definition: rlebtv.h:132
const gap_word_t * get_buf() const
Definition: rlebtv.h:69
int is_bit_true(unsigned pos) const
Checks if bit pos 1 or 0. Returns 0 if 0 and non zero otherwise.
Definition: rlebtv.h:190
bool set_bit(unsigned pos)
Sets bit number pos to 1 returns true if operation sucessful, false if GAP threashold reached and we ...
Definition: rlebtv.h:251
void combine_xor(const gap_vector &vect)
Definition: rlebtv.h:110
int compare(const gap_vector &vect)
Definition: rlebtv.h:270
int combine_and(const gap_word_t *other)
Definition: rlebtv.h:228
void convert_to_bitset(unsigned *dest) const
Definition: rlebtv.h:265
bool clear_bit(unsigned pos)
Sets bit number pos to 0.
Definition: rlebtv.h:258
int test(unsigned pos) const
Definition: rlebtv.h:205
void combine_or(const gap_vector &vect)
Definition: rlebtv.h:90
void control() const
Definition: rlebtv.h:160
unsigned count_range(unsigned left, unsigned right, unsigned *d=0) const
Definition: rlebtv.h:216
bool get_last(unsigned *last) const
Definition: rlebtv.h:198
unsigned bit_count() const
Counts number of bits ON.
Definition: rlebtv.h:211
constexpr const tuple_element< _Index, ct_const_tuple< _Types... > >::type & get(const ct_const_tuple< _Types... > &_Tuple) noexcept
Definition: const_tuple.hpp:97
static uch flags
static unsigned char depth[2 *(256+1+29)+1]
constexpr auto end(const ct_const_array< T, N > &in) noexcept
#define T(s)
Definition: common.h:230
Encoding utilities for serialization (internal)
std::ofstream out("events_result.xml")
main entry point for tests
#define test(a, b, c, d, e)
Definition: numeric.c:170
static time_t start_time
Definition: timeout.c:14
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:51
static DLIST_TYPE *DLIST_NAME() prev(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:61
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:56
static const char * str(char *buf, int n)
Definition: stats.c:84
static char tmp[3200]
Definition: utf8.c:42
static const char * validate(DSNINFO *di)
Go looking for trouble.
Definition: winsetup.c:179
int offset
Definition: replacements.h:160
char data[12]
Definition: iconv.c:80
unsigned char uint8_t
Uint4 uint32_t
bool avx2_is_all_one(const __m256i *block)
check if block is all one bits
Definition: bmavx2.h:1767
int avx2_cmpge_u32(__m256i vect8, unsigned value)
Experimental (test) function to do SIMD vector search (lower bound) in sorted, growing array.
Definition: bmavx2.h:2875
bool avx2_is_all_zero(const __m256i *block)
check if block is all zero bits
Definition: bmavx2.h:1708
unsigned avx2_lower_bound_scan_u32(const unsigned *arr, unsigned target, unsigned from, unsigned to)
lower bound (great or equal) linear scan in ascending order sorted array
Definition: bmavx2.h:3068
void swap(NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair1, NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair2)
Definition: ncbimisc.hpp:1508
string
Definition: cgiapp.hpp:687
const CVect2< U > & v2
Definition: globals.hpp:440
unsigned sse2_lower_bound_scan_u32(const unsigned *BMRESTRICT arr, unsigned target, unsigned from, unsigned to) BMNOEXCEPT
lower bound (great or equal) linear scan in ascending order sorted array
Definition: bmsse_util.h:1099
bool sse4_is_all_zero(const __m128i *block) noexcept
check if block is all zero bits
Definition: bmsse4.h:232
unsigned sse4_gap_find(const bm::gap_word_t *pbuf, const bm::gap_word_t pos, const unsigned size) noexcept
Definition: bmsse4.h:1340
bool sse4_is_all_one(const __m128i *block) noexcept
check if block is all ONE bits
Definition: bmsse4.h:874
int sse42_cmpge_u32(__m128i vect4, unsigned value) noexcept
Experimental (test) function to do SIMD vector search (lower bound) in sorted, growing array.
Definition: bmsse4.h:1527
bm::id_t bit_block_count(const bm::word_t *block) noexcept
Bitcount for bit block.
Definition: bmfunc.h:5051
unsigned bit_find_last(const bm::word_t *block, unsigned *last) noexcept
BIT block find the last set bit (backward search)
Definition: bmfunc.h:8708
unsigned short bitscan_bsf64(bm::id64_t w, B *bits) noexcept
Unpacks word into list of ON bits (BSF/__builtin_ctz)
Definition: bmfunc.h:730
bool bit_find_first(const bm::word_t *block, unsigned *pos) noexcept
BIT block find the first set bit.
Definition: bmfunc.h:8742
void or_bit_block(unsigned *dest, unsigned bitpos, unsigned bitcount) noexcept
Sets bits to 1 in the bitblock.
Definition: bmfunc.h:3766
bool bit_block_shift_r1(bm::word_t *block, bm::word_t *empty_acc, bm::word_t co_flag) noexcept
Right bit-shift bitblock by 1 bit (reference)
Definition: bmfunc.h:5634
int wordcmp0(T w1, T w2) noexcept
Lexicographical comparison of two words as bit strings (reference) Auxiliary implementation for testi...
Definition: bmfunc.h:1503
void block_compact_by_digest(bm::word_t *t_block, const bm::word_t *block, bm::id64_t digest, bool zero_tail) noexcept
Compact sub-blocks by digest (rank compaction)
Definition: bmfunc.h:1211
bm::id64_t digest_mask(unsigned from, unsigned to) noexcept
Compute digest mask for [from..to] positions.
Definition: bmfunc.h:1025
unsigned word_bitcount64(bm::id64_t x) noexcept
Definition: bmutil.h:605
bm::id64_t bit_block_xor(bm::word_t *dst, const bm::word_t *src) noexcept
Plain bitblock XOR operation. Function does not analyse availability of source and destination blocks...
Definition: bmfunc.h:8434
unsigned word_select64_bitscan_tz(bm::id64_t w, unsigned rank) noexcept
word find index of the rank-th bit set by bit-testing
Definition: bmfunc.h:854
unsigned count_leading_zeros(unsigned x) noexcept
Portable LZCNT with (uses minimal LUT)
Definition: bmutil.h:173
bool bit_find_first_diff(const bm::word_t *blk1, const bm::word_t *blk2, unsigned *pos) noexcept
Find first bit which is different between two bit-blocks.
Definition: bmfunc.h:4725
unsigned short bitscan_popcnt64(bm::id64_t w, B *bits) noexcept
Unpacks 64-bit word into list of ON bit indexes using popcnt method.
Definition: bmfunc.h:675
bm::id_t word_bitcount(bm::id_t w) noexcept
Definition: bmutil.h:582
unsigned count_trailing_zeros(unsigned v) noexcept
Portable TZCNT with (uses 37-LUT)
Definition: bmutil.h:189
int for_each_bit_blk(const bm::word_t *block, SIZE_TYPE offset, Func &bit_functor)
for-each visitor, calls a visitor functor for each 1 bit group
Definition: bmalgo_impl.h:1597
bool bit_block_or(bm::word_t *dst, const bm::word_t *src) noexcept
Plain bitblock OR operation. Function does not analyse availability of source and destination blocks.
Definition: bmfunc.h:7835
unsigned short bitscan_bsf(unsigned w, B *bits) noexcept
Unpacks word into list of ON bits (BSF/__builtin_ctz)
Definition: bmfunc.h:697
bm::id64_t bit_block_and(bm::word_t *dst, const bm::word_t *src) noexcept
Plain bitblock AND operation. Function does not analyse availability of source and destination blocks...
Definition: bmfunc.h:6858
bm::id64_t update_block_digest0(const bm::word_t *const block, bm::id64_t digest) noexcept
Compute digest for 64 non-zero areas based on existing digest (function revalidates zero areas)
Definition: bmfunc.h:1162
bool bit_block_or_3way(bm::word_t *dst, const bm::word_t *src1, const bm::word_t *src2) noexcept
3 way (target | source1 | source2) bitblock OR operation. Function does not analyse availability of s...
Definition: bmfunc.h:7952
bm::id_t bit_block_calc_count_range(const bm::word_t *block, bm::word_t left, bm::word_t right) noexcept
Definition: bmfunc.h:5390
SIZE_TYPE bit_find_rank(const bm::word_t *const block, SIZE_TYPE rank, unsigned nbit_from, unsigned &nbit_pos) noexcept
BIT block find position for the rank.
Definition: bmfunc.h:8916
bm::id_t bit_count_change(bm::word_t w) noexcept
Definition: bmfunc.h:5145
bool bit_is_all_zero(const bm::word_t *start) noexcept
Returns "true" if all bits in the block are 0.
Definition: bmfunc.h:1550
bool bit_block_shift_r1_unr(bm::word_t *block, bm::word_t *empty_acc, bm::word_t co_flag) noexcept
Right bit-shift of bit-block by 1 bit (loop unrolled)
Definition: bmfunc.h:5711
void bit_block_set(bm::word_t *dst, bm::word_t value) noexcept
Bitblock memset operation.
Definition: bmfunc.h:4456
unsigned test_bit(const unsigned *block, unsigned bitpos) noexcept
Test 1 bit in a block.
Definition: bmfunc.h:3748
void bit_block_rotate_left_1_unr(bm::word_t *block) noexcept
Unrolled cyclic rotation of bit-block left by 1 bit.
Definition: bmfunc.h:5549
unsigned bit_list_4(T w, B *bits) noexcept
Unpacks word into list of ON bit indexes (quad-bit based)
Definition: bmfunc.h:613
bm::id_t bit_block_calc_count_to(const bm::word_t *block, bm::word_t right) noexcept
Definition: bmfunc.h:5476
unsigned bit_scan_reverse(T value) noexcept
Definition: bmutil.h:453
bm::id64_t bit_block_and_2way(bm::word_t *dst, const bm::word_t *src1, const bm::word_t *src2, bm::id64_t digest) noexcept
digest based bit-block AND
Definition: bmfunc.h:7076
bool bit_block_shift_l1_unr(bm::word_t *block, bm::word_t *empty_acc, bm::word_t co_flag) noexcept
Left bit-shift of bit-block by 1 bit (loop unrolled)
Definition: bmfunc.h:5811
unsigned word_select64_linear(bm::id64_t w, unsigned rank) noexcept
word find index of the rank-th bit set by bit-testing
Definition: bmfunc.h:799
unsigned word_select32_bitscan_tz(unsigned w, unsigned rank) noexcept
word find index of the rank-th bit set by bit-testing
Definition: bmfunc.h:912
void tmatrix_distance(const T tmatrix[BPC][BPS], unsigned distance[BPC][BPC])
Compute pairwise Row x Row Humming distances on planes(rows) of the transposed bit block.
Definition: bmtrans.h:315
unsigned word_select32_bitscan_popcnt(unsigned w, unsigned rank) noexcept
word find index of the rank-th bit set by bit-testing
Definition: bmfunc.h:883
unsigned word_select64_bitscan_popcnt(bm::id64_t w, unsigned rank) noexcept
word find index of the rank-th bit set by bit-testing
Definition: bmfunc.h:825
bm::word_t bit_block_insert(bm::word_t *block, unsigned bitpos, bool value) noexcept
insert bit into position and shift the rest right with carryover
Definition: bmfunc.h:5585
void clear_bit(unsigned *dest, unsigned bitpos) noexcept
Set 1 bit in a block.
Definition: bmfunc.h:3734
void bit_block_erase(bm::word_t *block, unsigned bitpos, bool carry_over) noexcept
erase bit from position and shift the rest right with carryover
Definition: bmfunc.h:5834
void set_bit(unsigned *dest, unsigned bitpos) noexcept
Set 1 bit in a block.
Definition: bmfunc.h:3721
bm::id64_t widx_to_digest_mask(unsigned w_idx) noexcept
Compute digest mask for word address in block.
Definition: bmfunc.h:997
unsigned bit_list(T w, B *bits) noexcept
Unpacks word into list of ON bit indexes.
Definition: bmfunc.h:595
bool bit_find_first_if_1(const bm::word_t *block, unsigned *first, bm::id64_t digest) noexcept
BIT block find the first set bit if only 1 bit is set.
Definition: bmfunc.h:8838
int wordcmp(T a, T b) noexcept
Lexicographical comparison of two words as bit strings. Auxiliary implementation for testing and refe...
Definition: bmfunc.h:1532
void block_expand_by_digest(bm::word_t *t_block, const bm::word_t *block, bm::id64_t digest, bool zero_subs) noexcept
expand sub-blocks by digest (rank expansion)
Definition: bmfunc.h:1278
unsigned bit_block_xor_count(const bm::word_t *src1, const bm::word_t *src2) noexcept
Function XORs two bitblocks and computes the bitcount. Function does not analyse availability of sour...
Definition: bmfunc.h:7350
bool bit_block_is_all_one_range(const bm::word_t *const block, bm::word_t left, bm::word_t right) noexcept
Definition: bmfunc.h:5301
unsigned bit_block_calc_change(const bm::word_t *block) noexcept
Definition: bmfunc.h:5283
bm::id64_t calc_block_digest0(const bm::word_t *const block) noexcept
Compute digest for 64 non-zero areas.
Definition: bmfunc.h:1120
void bit_block_rotate_left_1(bm::word_t *block) noexcept
Definition: bmfunc.h:5533
bool check_zero_digest(bm::id64_t digest, unsigned bitpos_from, unsigned bitpos_to) noexcept
check if all digest bits for the range [from..to] are 0
Definition: bmfunc.h:1054
bm::id64_t bit_block_sub(bm::word_t *dst, const bm::word_t *src) noexcept
Plain bitblock SUB (AND NOT) operation. Function does not analyse availability of source and destinat...
Definition: bmfunc.h:8105
unsigned short bitscan_popcnt(bm::id_t w, B *bits, unsigned short offs) noexcept
Unpacks word into list of ON bit indexes using popcnt method.
Definition: bmfunc.h:632
void invert_kleene(BV &bv_value, const BV &bv_null)
Kleene NEG operation.
Definition: bm3vl.h:135
int get_value_kleene(const BV &bv_value, const BV &bv_null, typename BV::size_type idx) noexcept
Return Kleene logic value based on value and known vectors.
Definition: bm3vl.h:70
void init_kleene(BV &bv_value, const BV &bv_null)
Initialized the value bit-vector so that it always returns 0 (false) for the unknown.
Definition: bm3vl.h:54
void and_kleene(BV &bv_value1, BV &bv_null1, const BV &bv_value2, const BV &bv_null2)
Kleene AND(vect1, vect2) (vect1 &= vect2) 0 AND Unk = 0 (known)
Definition: bm3vl.h:213
void set_value_kleene(BV &bv_value, BV &bv_null, typename BV::size_type idx, int val)
Set Kleene logic value based on value and known vectors.
Definition: bm3vl.h:96
int or_values_kleene(int a, int b) noexcept
Reference function for Kleene logic OR (for verification and testing)
Definition: bm3vl.h:311
int and_values_kleene(int a, int b) noexcept
Reference function for Kleene logic AND (for verification and testing)
Definition: bm3vl.h:271
void or_kleene(BV &bv_value1, BV &bv_null1, const BV &bv_value2, const BV &bv_null2)
Kleene OR(vect1, vect2) (vect1 |= vect2) 1 OR Unk = 1 (known)
Definition: bm3vl.h:151
int simd_version()
return SIMD optimization used for building BitMagic
Definition: bmsimd.h:75
void run_task_batch(task_batch_base &tasks)
Run task batch sequentially.
Definition: bmtask.h:194
operation
Bit operations.
Definition: bmconst.h:191
set_operation
Codes of set operations.
Definition: bmconst.h:168
null_support
NULL-able value support.
Definition: bmconst.h:229
strategy
Block allocation strategies.
Definition: bmconst.h:146
@ BM_UNSORTED
input set is NOT sorted
Definition: bmconst.h:205
@ BM_SORTED
input set is sorted (ascending order)
Definition: bmconst.h:206
@ BM_UNKNOWN
sort order unknown
Definition: bmconst.h:208
@ set_COUNT_SUB_AB
Definition: bmconst.h:178
@ set_OR
Definition: bmconst.h:170
@ set_COUNT_XOR
Definition: bmconst.h:176
@ set_COUNT_OR
Definition: bmconst.h:177
@ set_COUNT_SUB_BA
Definition: bmconst.h:179
@ set_ASSIGN
Definition: bmconst.h:173
@ set_SUB
Definition: bmconst.h:171
@ set_COUNT_AND
Definition: bmconst.h:175
@ set_AND
Definition: bmconst.h:169
@ set_XOR
Definition: bmconst.h:172
@ use_null
support "non-assigned" or "NULL" logic
Definition: bmconst.h:230
@ no_null
do not support NULL values
Definition: bmconst.h:231
@ READWRITE
mutable (read-write object)
@ READONLY
immutable (read-only object)
@ BM_BIT
No GAP compression strategy. All new blocks are bit blocks.
Definition: bmconst.h:147
@ BM_GAP
GAP compression is ON.
Definition: bmconst.h:148
void bit_import_u32(BV &bv, const unsigned int *BMRESTRICT bit_arr, typename BV::size_type bit_arr_size, bool optimize)
Import native stream of bits (represented as 32-bit unsigned ints)
Definition: bmbvimport.h:46
bool find_interval_end(const BV &bv, typename BV::size_type from, typename BV::size_type &pos) noexcept
Reverse find index of first 1 bit gap (01110) starting from position Reverse scan for the first 1 in ...
Definition: bmintervals.h:438
bool is_interval(const BV &bv, typename BV::size_type left, typename BV::size_type right) noexcept
Returns true if range is all 1s flanked with 0s Function performs the test on a closed range [left,...
Definition: bmintervals.h:248
bool find_interval_start(const BV &bv, typename BV::size_type from, typename BV::size_type &pos) noexcept
Reverse find index of first 1 bit gap (01110) starting from position Reverse scan for the first 1 in ...
Definition: bmintervals.h:315
size_t serialize(const BV &bv, unsigned char *buf, bm::word_t *temp_block=0, unsigned serialization_flags=0)
Saves bitvector into memory.
Definition: bmserial.h:3071
size_t deserialize(BV &bv, const unsigned char *buf, bm::word_t *temp_block=0, const bm::bv_ref_vector< BV > *ref_vect=0)
Bitvector deserialization from a memory BLOB.
Definition: bmserial.h:3137
void deserialize_range(BV &bv, const unsigned char *buf, typename BV::size_type from, typename BV::size_type to, const bm::bv_ref_vector< BV > *ref_vect=0)
Bitvector range deserialization from a memory BLOB.
Definition: bmserial.h:3200
@ BM_NO_GAP_LENGTH
save no GAP info (save some space)
Definition: bmserial.h:3025
@ BM_NO_BYTE_ORDER
save no byte-order info (save some space)
Definition: bmserial.h:3024
unsigned bit_convert_to_gap(T *dest, const unsigned *src, bm::id_t bits, unsigned dest_len)
Converts bit block to GAP.
Definition: stress32.cpp:3245
bool gap_find_first_diff(const T *buf1, const T *buf2, unsigned *pos) noexcept
Find first bit which is different between two GAP-blocks.
Definition: bmfunc.h:2904
unsigned gap_set_array(T *buf, const T *arr, unsigned len) noexcept
Convert array to GAP buffer.
Definition: bmfunc.h:3601
bool improve_gap_levels(const T *length, const T *length_end, T *glevel_len) noexcept
Finds optimal gap blocks lengths.
Definition: bmfunc.h:9170
unsigned gap_set_value(unsigned val, T *buf, unsigned pos, unsigned *is_set) noexcept
Sets or clears bit in the GAP buffer.
Definition: bmfunc.h:3254
int gapcmp(const T *buf1, const T *buf2) noexcept
Lexicographical comparison of GAP buffers.
Definition: bmfunc.h:2848
bm::gap_word_t gap_length(const bm::gap_word_t *buf) noexcept
Returs GAP block length.
Definition: bmfunc.h:1603
void gap_set_all(T *buf, unsigned set_max, unsigned value) noexcept
Sets all bits to 0 or 1 (GAP)
Definition: bmfunc.h:4552
unsigned bit_array_compute_gaps(const T *arr, unsigned len) noexcept
Compute number of GAPs in bit-array.
Definition: bmfunc.h:3662
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
void combine_and_sorted(BV &bv, It first, It last)
AND Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1333
void rank_range_split(const BV &bv, typename BV::size_type rank, PairVect &target_v)
Algorithm to identify bit-vector ranges (splits) for the rank.
Definition: bmalgo.h:394
void export_array(BV &bv, It first, It last)
Export bitset from an array of binary data representing the bit vector.
Definition: bmalgo_impl.h:1423
BV::size_type count_and(const BV &bv1, const BV &bv2) noexcept
Computes bitcount of AND operation of two bitsets.
Definition: bmalgo.h:49
int visit_each_bit(const BV &bv, void *handle_ptr, bit_visitor_callback_type callback_ptr)
bvector visitor scanner to traverse each 1 bit using C callback
Definition: bmalgo.h:336
int visit_each_bit_range(const BV &bv, typename BV::size_type left, typename BV::size_type right, void *handle_ptr, bit_visitor_callback_type callback_ptr)
bvector visitor scanner to traverse each bits in range (C callback)
Definition: bmalgo.h:362
bm::distance_metric_descriptor::size_type count_xor(const BV &bv1, const BV &bv2) noexcept
Computes bitcount of XOR operation of two bitsets.
Definition: bmalgo.h:81
void combine_xor(BV &bv, It first, It last)
XOR Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1161
BV::size_type count_sub(const BV &bv1, const BV &bv2) noexcept
Computes bitcount of SUB operation of two bitsets.
Definition: bmalgo.h:115
BV::size_type any_and(const BV &bv1, const BV &bv2) noexcept
Computes if there is any bit in AND operation of two bitsets.
Definition: bmalgo.h:62
void combine_or(BV &bv, It first, It last)
OR Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1080
BV::size_type count_intervals(const BV &bv)
Compute number of bit intervals (GAPs) in the bitvector.
Definition: bmalgo_impl.h:1389
int for_each_bit_range(const BV &bv, typename BV::size_type left, typename BV::size_type right, Func &bit_functor)
bit-vector range visitor to traverse each 1 bit
Definition: bmalgo.h:266
BV::size_type any_xor(const BV &bv1, const BV &bv2) noexcept
Computes if there is any bit in XOR operation of two bitsets.
Definition: bmalgo.h:97
BV::size_type any_or(const BV &bv1, const BV &bv2) noexcept
Computes if there is any bit in OR operation of two bitsets.
Definition: bmalgo.h:165
BV::size_type count_or(const BV &bv1, const BV &bv2) noexcept
Computes bitcount of OR operation of two bitsets.
Definition: bmalgo.h:149
BV::size_type any_sub(const BV &bv1, const BV &bv2) noexcept
Computes if there is any bit in SUB operation of two bitsets.
Definition: bmalgo.h:132
bool sparse_vector_find_first_mismatch(const SV &sv1, const SV &sv2, typename SV::size_type &midx, bm::null_support null_proc=bm::use_null)
Find first mismatch (element which is different) between two sparse vectors (uses linear scan in bit-...
void dynamic_range_clip_low(SV &svect, unsigned low_bit)
Clip dynamic range for signal lower than specified (boost)
void dynamic_range_clip_high(SV &svect, unsigned high_bit)
Clip dynamic range for signal higher than specified.
void sparse_vector_find_mismatch(typename SV1::bvector_type &bv, const SV1 &sv1, const SV2 &sv2, bm::null_support null_proc)
Find mismatch vector, indicating positions of mismatch between two sparse vectors (uses linear scan i...
void sparse_vector_serialize(const SV &sv, sparse_vector_serial_layout< SV > &sv_layout, bm::word_t *temp_block=0)
Serialize sparse vector into a memory buffer(s) structure.
int sparse_vector_deserialize(SV &sv, const unsigned char *buf, bm::word_t *temp_block=0)
Deserialize sparse vector.
exit(2)
char * buf
int i
yy_size_t n
int len
static void hex(unsigned char c)
Definition: mdb_dump.c:56
#include<zmmintrin.h>
Definition: bm.h:78
const unsigned char set_block_ref_eq
block is a copy of a reference block
Definition: bmserial.h:1130
const unsigned set_array_mask
Definition: bmconst.h:97
xor_complement_match
XOR complementarity type between 2 blocks.
Definition: bmxor.h:81
@ e_no_xor_match
Definition: bmxor.h:82
@ e_xor_match_GC
Definition: bmxor.h:83
@ e_xor_match_BC
Definition: bmxor.h:84
@ e_xor_match_EQ
Definition: bmxor.h:86
@ e_xor_match_iBC
Definition: bmxor.h:85
const unsigned set_block_plane_cnt
Definition: bmconst.h:64
const unsigned set_block_digest_wave_size
Definition: bmconst.h:67
void print_svector_stat(TOut &tout, const SV &svect, bool print_sim=false)
Definition: bmdbg.h:630
const unsigned char set_block_arrbit_inv
List of bits OFF.
Definition: bmserial.h:1124
void PrintDGap(TOut &tout, const bm::gap_word_t *gap_buf, unsigned gap_len=0)
Definition: bmdbg.h:66
const unsigned id_max
Definition: bmconst.h:109
void xor_swap(W &x, W &y) noexcept
XOR swap two variables.
Definition: bmutil.h:534
unsigned int word_t
Definition: bmconst.h:39
const unsigned char set_block_bit_digest0
H-compression with digest mask.
Definition: bmserial.h:1128
const unsigned char set_block_xor_ref32
..... 32-bit (should never happen)
Definition: bmserial.h:1133
const unsigned char set_block_arrgap_bienc_inv_v2
Interpolated GAP array (inverted)
Definition: bmserial.h:1141
const unsigned char set_block_bitgap_bienc
Interpolated bit-block as GAPs.
Definition: bmserial.h:1127
unsigned char check_pair_vect_vbr(const BMChain &mchain, const RVect &ref_vect)
Check effective bit-rate for the XOR encode vector.
Definition: bmxor.h:404
const unsigned char set_block_arrgap_egamma_inv
Gamma compressed inverted delta GAP array.
Definition: bmserial.h:1116
void bit_recomb(It1 &it1, It2 &it2, BinaryOp &op, Encoder &enc, unsigned block_size=bm::set_block_size) noexcept
Definition: bmfunc.h:9439
bm::id64_t dm_control(unsigned from, unsigned to) noexcept
digest mask control generation (for debug and test only)
Definition: bmfunc.h:1007
bool find_not_null_ptr(const bm::word_t *const *const *arr, N start, N size, N *pos) noexcept
Definition: bmfunc.h:1427
void compute_s_block_descr(const bm::word_t *block, block_waves_xor_descr &x_descr, unsigned *s_gc, unsigned *s_bc) noexcept
Compute reference (non-XOR) 64-dim complexity descriptor for the s-block.
Definition: bmxor.h:430
void PrintTMatrix(TOut &tout, const TM &tmatrix, unsigned cols=0, bool binary=false)
Definition: bmdbg.h:271
const unsigned char set_block_arr_bienc_inv
Interpolated inverted block int array.
Definition: bmserial.h:1126
void PrintGap(TOut &tout, const bm::gap_word_t *gap_buf)
Definition: bmdbg.h:53
const unsigned set_sub_array_size
Definition: bmconst.h:95
bm::id64_t sum_arr(const T *first, const T *last) noexcept
Definition: bmfunc.h:2221
void get_block_coord(BI_TYPE nb, unsigned &i, unsigned &j) noexcept
Recalc linear bvector block index into 2D matrix coordinates.
Definition: bmfunc.h:180
const unsigned id_max32
Definition: bmconst.h:50
unsigned bmi1_select64_tz(bm::id64_t w, unsigned rank)
word find index of the rank-th bit set by bit-testing
Definition: bmbmi1.h:80
VT::size_type greedy_refine_match_vector(PVT &match_pairs_vect, VT &match_vect, typename VT::size_type best_ref_idx, bm::id64_t d64, bm::xor_complement_match match_type)
Greedy algorithm to find additional matches improving the inital best match block on its match type.
Definition: bmxor.h:327
const unsigned set_total_blocks
Definition: bmconst.h:111
void PrintDGapGamma(TOut &tout, const bm::gap_word_t *gap_buf, unsigned gap_len=0)
Definition: bmdbg.h:113
unsigned bit_block_change32(const bm::word_t *block, unsigned size) noexcept
Definition: bmfunc.h:5161
void PrintDistanceMatrix(TOut &tout, const unsigned distance[bm::set_block_plane_cnt][bm::set_block_plane_cnt])
Definition: bmdbg.h:255
unsigned bmi1_select64_lz(bm::id64_t w, unsigned rank)
word find index of the rank-th bit set by bit-testing
Definition: bmbmi1.h:42
int load_vector(VECT &vect, const std::string &fname)
Definition: bmdbg.h:911
const unsigned char set_block_bit_0runs
Bit block with encoded zero intervals.
Definition: bmserial.h:1115
const unsigned char set_block_gap_bienc
Interpolated GAP block (legacy)
Definition: bmserial.h:1121
bool check_block_one(const bm::word_t *blk, bool deep_scan) noexcept
Checks if block has only 1 bits.
Definition: bmfunc.h:9120
const unsigned char set_block_arrbit
List of bits ON.
Definition: bmserial.h:1109
const unsigned char ibpc_equiv
!< plane ALL ONE
Definition: bmtrans.h:352
unsigned sse2_gap_find(const bm::gap_word_t *pbuf, const bm::gap_word_t pos, unsigned size)
Definition: bmsse2.h:1379
void bit_block_change_bc(const bm::word_t *block, unsigned *gc, unsigned *bc) noexcept
Definition: bmfunc.h:5252
const unsigned gap_levels
Definition: bmconst.h:85
void vect_bit_transpose(const T *arr, unsigned arr_size, T tmatrix[BPC][BPS])
Generic bit-array transposition function T - array type (any int) BPC - bit plane count BPS - bit pla...
Definition: bmtrans.h:257
unsigned bitscan_nibble(unsigned w, unsigned *bits) noexcept
portable, switch based bitscan
Definition: bmfunc.h:382
void set_nibble(unsigned char *arr, unsigned idx, unsigned char v) noexcept
set nibble in the array
Definition: bmfunc.h:10251
const unsigned char set_sblock_bienc
super-block interpolated list
Definition: bmserial.h:1154
unsigned char get_nibble(const unsigned char *arr, unsigned idx) noexcept
get nibble from the array
Definition: bmfunc.h:10280
const unsigned char set_block_xor_chain
XOR chain (composit of sub-blocks)
Definition: bmserial.h:1137
const unsigned set_block_size
Definition: bmconst.h:55
unsigned bit_to_gap(gap_word_t *dest, const unsigned *block, unsigned dest_len) noexcept
Convert bit block to GAP representation.
Definition: bmfunc.h:4870
unsigned long long int id64_t
Definition: bmconst.h:35
const unsigned block_waves
Definition: bmconst.h:66
const unsigned char set_block_arrgap_egamma
Gamma compressed delta GAP array.
Definition: bmserial.h:1114
void vect_bit_trestore(const T tmatrix[BPC][BPS], T *arr)
Restore bit array from the transposition matrix T - array type (any int) BPC - bit plane count BPS - ...
Definition: bmtrans.h:290
unsigned int id_t
Definition: bmconst.h:38
const unsigned gap_max_buff_len
Definition: bmconst.h:80
const unsigned char ibpc_close
!< plane is equal to plane M
Definition: bmtrans.h:353
const unsigned char ibpc_all_one
!< plane ALL ZERO
Definition: bmtrans.h:351
unsigned mask_r_u32(unsigned nbit) noexcept
Definition: bmutil.h:513
const unsigned char set_block_arrgap_bienc_v2
//!< Interpolated GAP array (v2)
Definition: bmserial.h:1140
const unsigned char ibpc_all_zero
!< plane uncompressed
Definition: bmtrans.h:350
const unsigned rs3_border1
Definition: bmconst.h:120
const unsigned char set_block_arrgap
List of bits ON (GAP block)
Definition: bmserial.h:1111
unsigned bit_scan_forward32(unsigned w) noexcept
Definition: bmutil.h:319
@ COPY_RTABLES
copy remap tables only (without data)
unsigned int iLog2(unsigned int value)
Definition: bmdbg.h:79
void bit_iblock_pcv_stat(const unsigned char *pc_vector, const unsigned char *pc_vector_end, unsigned *pc_vector_stat)
Compute number of ibpc codes in pc_vector.
Definition: bmtrans.h:441
const unsigned char set_block_arr_bienc
Interpolated block as int array.
Definition: bmserial.h:1125
unsigned compute_h64_mask(unsigned long long w) noexcept
Сompute mask of bytes presense in 64-bit word.
Definition: bmutil.h:556
void dgap_2_gap(const T *dgap_buf, T *gap_buf, T gap_header=0) noexcept
Convert D-GAP buffer into GAP buffer.
Definition: bmfunc.h:2806
unsigned count_leading_zeros_u32(unsigned w) noexcept
32-bit bit-scan reverse
Definition: bmutil.h:356
const unsigned set_array_shift
Definition: bmconst.h:96
void print_stat(TOut &tout, const BV &bv, typename BV::block_idx_type blocks=0)
Definition: bmdbg.h:408
const unsigned char set_block_gap_egamma
Gamma compressed GAP block.
Definition: bmserial.h:1113
unsigned short gap_word_t
Definition: bmconst.h:78
const unsigned set_block_plane_size
Definition: bmconst.h:63
void LoadBVector(const char *fname, TBV &bvector, unsigned *file_size=0)
Definition: bmdbg.h:167
const unsigned rs3_border0
Definition: bmconst.h:119
const unsigned gap_max_bits
Definition: bmconst.h:81
const unsigned char set_block_bit_1bit
Bit block with 1 bit ON.
Definition: bmserial.h:1112
const unsigned set_block_shift
Definition: bmconst.h:56
T * gap_2_dgap(const T *gap_buf, T *dgap_buf, bool copy_head=true) noexcept
Convert GAP buffer into D-GAP buffer.
Definition: bmfunc.h:2780
unsigned count_leading_zeros_u64(bm::id64_t w) noexcept
64-bit bit-scan reverse
Definition: bmutil.h:373
int file_save_svector(const SV &sv, const std::string &fname, size_t *sv_blob_size=0, bool use_xor=true)
Definition: bmdbg.h:995
bool has_zero_byte_u64(bm::id64_t v) noexcept
Returns true if INT64 contains 0 octet.
Definition: bmutil.h:571
const unsigned char ibpc_uncompr
Definition: bmtrans.h:349
const unsigned char ibpc_end
!< plane is close to plane M
Definition: bmtrans.h:355
int save_vector(const VECT &vect, const std::string &fname)
Definition: bmdbg.h:888
int file_load_svector(SV &sv, const std::string &fname)
Definition: bmdbg.h:1028
unsigned bit_scan_reverse32(unsigned w) noexcept
Definition: bmutil.h:304
unsigned mask_l_u32(unsigned nbit) noexcept
Definition: bmutil.h:522
range(_Ty, _Ty) -> range< _Ty >
constexpr auto sort(_Init &&init)
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
const struct ncbi::grid::netcache::search::fields::SIZE size
const struct ncbi::grid::netcache::search::fields::KEY key
chrono::system_clock::duration duration
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
static const BitmapCharRec ch1
Definition: ncbi_10x20.c:1827
static const BitmapCharRec ch2
Definition: ncbi_10x20.c:1819
Bitset allocator.
Bitset relates utilities.
unsigned int a
Definition: ncbi_localip.c:102
EIPRangeType t
Definition: ncbi_localip.c:101
bool eq(T x_, T y_, T round_)
Definition: njn_approx.hpp:79
T max(T x_, T y_)
T min(T x_, T y_)
std::istream & in(std::istream &in_, double &x_)
void abort()
Int4 delta(size_t dimension_, const Int4 *score_)
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
void copy(Njn::Matrix< S > *matrix_, const Njn::Matrix< T > &matrix0_)
Definition: njn_matrix.hpp:613
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
static unsigned cnt[256]
static int match(register const pcre_uchar *eptr, register const pcre_uchar *ecode, const pcre_uchar *mstart, int offset_top, match_data *md, eptrblock *eptrb, unsigned int rdepth)
Definition: pcre_exec.c:513
static const char * prefix[]
Definition: pcregrep.c:405
#define CLOCKS_PER_SEC
Definition: pcretest.c:1019
static pcre_uint8 * buffer
Definition: pcretest.c:1051
static int buffer_size
Definition: pcretest.c:1050
#define assert(x)
Definition: srv_diag.hpp:58
static __m128i _mm_set_epi32(int, int, int, int)
Definition: sse2neon.h:5070
int64x2_t __m128i
Definition: sse2neon.h:200
std::string get_stacktrace(std::string &stack_str, unsigned int max_frames=256)
Print a demangled stack backtrace of the caller function to FILE* out.
#define INT32_MIN
Definition: stdint.h:182
bm::rsc_sparse_vector< int, sparse_vector_i32 > rsc_sparse_vector_i32
Definition: stress32.cpp:244
static void KeepRangeTest()
Definition: stress32.cpp:19964
static void GenerateTestStrCollection(std::vector< string > &str_coll, unsigned max_coll)
Definition: stress32.cpp:30892
void CheckSparseVectorGather(const SV &sv, unsigned from, unsigned to, typename SV::value_type control_value=0)
Definition: stress32.cpp:25294
static unsigned BitCountChange(unsigned word)
Definition: stress32.cpp:1866
static void ComparisonTest()
Definition: stress32.cpp:8939
static void MiniSetTest()
Definition: stress32.cpp:17294
bool is_csv0
Definition: stress32.cpp:37873
static void SparseSerializationTest()
Definition: stress32.cpp:14284
static void GenerateCompressedBufferCollection(bm::compressed_buffer_collection< bvect > &cbc)
Definition: stress32.cpp:33854
static void StressTestAggregatorAND(unsigned repetitions)
Definition: stress32.cpp:10586
bool is_rankc
Definition: stress32.cpp:37867
int main(int argc, char *argv[])
Definition: stress32.cpp:38940
static void CountRangeTest()
Definition: stress32.cpp:19121
static void CheckBitList(const unsigned *bl1, unsigned bl1_cnt, const unsigned *bl2, unsigned bl2_cnt)
Definition: stress32.cpp:20196
unsigned generate_inter_test_linear(V *arr, unsigned inc, unsigned target_size)
Random numbers test.
Definition: stress32.cpp:20879
static void print_mv(const bvect_mini &bvect_min, unsigned size)
Definition: stress32.cpp:1464
static void TestSparseVector()
Definition: stress32.cpp:21987
bool is_sv_sort
Definition: stress32.cpp:37880
bm::sparse_vector< unsigned, bvect > sparse_vector_u32
Definition: stress32.cpp:239
static void ClearAllTest()
Definition: stress32.cpp:2724
static void TestRecomb()
Definition: stress32.cpp:20153
static void GammaEncoderTest()
Definition: stress32.cpp:21326
bool FindRank(const T &bv, bm::id_t rank, bm::id_t from, bm::id_t &pos)
Definition: stress32.cpp:1620
static void SerializationCompressionLevelsTest()
Definition: stress32.cpp:13076
const unsigned ITERATIONS
Definition: stress32.cpp:253
static void InterpolativeCodingTest()
Definition: stress32.cpp:20943
static int parse_args(int argc, char *argv[])
Definition: stress32.cpp:37883
static void TestBlockOR()
Definition: stress32.cpp:34716
static void StressTest(unsigned repetitions, int set_operation, bool detailed, int method=-1)
Definition: stress32.cpp:11095
static void TestCompressedSparseVectorScan()
Definition: stress32.cpp:27659
bvect::size_type from_arr[]
Definition: stress32.cpp:19261
bool is_bvops2
Definition: stress32.cpp:37864
static void TestSparseVectorSerial()
Definition: stress32.cpp:24554
static void TestCompressSparseVector()
Definition: stress32.cpp:35569
std::vector< unsigned > ref_vect_type
Definition: stress32.cpp:237
static void TestSparseFindEqStrPipeline()
Definition: stress32.cpp:31372
static void Check_XOR_Product(const bm::word_t *block, const bm::word_t *xor_block, bm::id64_t digest)
Definition: stress32.cpp:2929
static void TestCompressSparseGather()
Definition: stress32.cpp:27200
static void StressTestAggregatorShiftAND(unsigned repeats)
Definition: stress32.cpp:11004
static void MutationTest()
Definition: stress32.cpp:12688
static void Log2Test()
Definition: stress32.cpp:20369
bm::sparse_vector< unsigned long long, bvect > sparse_vector_u64
Definition: stress32.cpp:241
bm::rsc_sparse_vector< signed long long, sparse_vector_i64 > rsc_sparse_vector_i64
Definition: stress32.cpp:246
static void RankFindTest()
Definition: stress32.cpp:4793
static void TestSignedSparseVectorScanGT()
Definition: stress32.cpp:26789
static void FillSetsIntervals(bvect_mini *bvect_min, bvect &bvect_full, unsigned min, unsigned max, unsigned fill_factor, bool set_flag=true)
Definition: stress32.cpp:652
static void BvectorIncTest()
Definition: stress32.cpp:4911
static void RankRangeSplitTest()
Definition: stress32.cpp:9197
static void BVectorErase(bvect *bv, unsigned pos)
Definition: stress32.cpp:964
static void PrintGapLevels(const gap_word_t *glevel)
Definition: stress32.cpp:17458
unsigned generate_inter_test(V *arr, unsigned inc_factor, unsigned target_size)
Random numbers test.
Definition: stress32.cpp:20908
bool is_sv1
Definition: stress32.cpp:37871
static void BvectorBulkSetTest()
Definition: stress32.cpp:4501
static void BvectorInsertTest()
Definition: stress32.cpp:5623
static void ShiftRotateTest()
Definition: stress32.cpp:3512
static void ShiftRight(bvect *bv, unsigned shift)
Definition: stress32.cpp:912
static void LZCNTTest()
Definition: stress32.cpp:20455
void CheckVectors(bvect_mini &bvect_min, bvect &bvect_full, unsigned size, bool detailed=true)
Definition: stress32.cpp:2279
static void SwapTest()
Definition: stress32.cpp:17195
static bool agg_shift_right_and(bm::aggregator< bvect > &agg, bvect &bv_target, const bvect *bv,...)
Definition: stress32.cpp:9689
void CheckGTSearch(const SV &sv, typename SV::value_type v, bm::sparse_vector_scanner< SV > &scanner)
Definition: stress32.cpp:26185
static void StressTestStrSparseVector()
Definition: stress32.cpp:30965
static void TestSignedSparseVector()
Definition: stress32.cpp:23625
void DetailedCompareBVectors(const BV &bv1, const BV &bv2)
Definition: stress32.cpp:281
bool is_str_sv
Definition: stress32.cpp:37876
bvect::size_type to_arr[]
Definition: stress32.cpp:19263
static void OrOperationsTest(bool detailed)
Definition: stress32.cpp:7086
bool is_bvops
Definition: stress32.cpp:37861
void swap_bits_check(BV &bv1, BV &bv2, typename BV::size_type i1, typename BV::size_type i2)
Definition: stress32.cpp:17174
static void generate_test_vectors(std::vector< bm::id_t > &v1, std::vector< bm::id_t > &v2, std::vector< bm::id_t > &v3, unsigned vector_max)
Definition: stress32.cpp:4485
static void OptimizeTest()
Definition: stress32.cpp:4988
static void BvectorBitForEachTest()
Definition: stress32.cpp:33538
static void WordCmpTest()
Definition: stress32.cpp:2744
static void EnumeratorTest()
Definition: stress32.cpp:15638
bool is_support
Definition: stress32.cpp:37856
static bool CheckAllocLeaks(bool details=false, bool abort=true)
Definition: stress32.cpp:38072
bm::rsc_sparse_vector< unsigned long long, sparse_vector_u64 > rsc_sparse_vector_u64
Definition: stress32.cpp:245
void TestFindDiff(const BV &bv1, BV &bv2)
Definition: stress32.cpp:498
unsigned proxy_bmi1_select64_tz(bm::id64_t val, unsigned rank)
Definition: stress32.cpp:20528
static unsigned random_minmax(unsigned min, unsigned max)
Definition: stress32.cpp:491
void EraseSVCollection(SV &sv)
Definition: stress32.cpp:30940
static void TestSparseVectorFilter()
Definition: stress32.cpp:25779
static void TestSparseVectorTransform()
Definition: stress32.cpp:25910
static void SimpleGapFillSets(bvect &bv0, bvect &bv1, unsigned min, unsigned max, unsigned fill_factor)
Definition: stress32.cpp:12639
void interval_copy_range(BV &bv, const BV &bv_src, typename BV::size_type from, typename BV::size_type to)
Definition: stress32.cpp:2140
static void SimpleRandomFillTest()
Definition: stress32.cpp:6062
void LoadVectors(const char *dir_name, unsigned from, unsigned to)
Definition: stress32.cpp:32593
static void DynamicMatrixTest()
Definition: stress32.cpp:2422
static void TestArraysAndBuffers()
Definition: stress32.cpp:35498
static void TestStrSparseQuickSort()
Definition: stress32.cpp:31632
static void SelectTest()
Definition: stress32.cpp:20569
static void BvectorShiftTest()
Definition: stress32.cpp:5207
static void BVImportTest()
Definition: stress32.cpp:15937
static void TestBlockZero()
Definition: stress32.cpp:34115
static void TestHasZeroByte()
Definition: stress32.cpp:20111
void FillSparseIntervals(std::vector< unsigned > &vect, SV &svect, unsigned min, unsigned max, unsigned fill_factor)
Definition: stress32.cpp:27827
static void BasicFunctionalityTest()
Definition: stress32.cpp:4215
void VCF_des_test()
Definition: stress32.cpp:38620
static void TestXOR_RefVector()
Definition: stress32.cpp:37733
bool is_agg
Definition: stress32.cpp:37868
static void BvectorFindFirstDiffTest()
Definition: stress32.cpp:9077
static void AggregatorTest()
Definition: stress32.cpp:9713
static void OptimGAPTest()
Definition: stress32.cpp:17470
static void VerifyCountRange(const bvect &bv, const bvect::rs_index_type &bc_arr, bvect::size_type from, bvect::size_type to)
Definition: stress32.cpp:19082
bm::bvector_mini< dbg_block_allocator > bvect_mini
Definition: stress32.cpp:225
static void SerializationOperation2Test(bvect *bv_target, bvect &bv1, bvect &bv2, unsigned predicted_count, set_operation op_count, set_operation op_combine)
Definition: stress32.cpp:1388
static void TestSparseVectorAlgo()
Definition: stress32.cpp:23899
static void ArenaTest()
Definition: stress32.cpp:16766
static void FillSetsRandomOne(bvect_mini *bvect_min, bvect *bvect_full, unsigned min, unsigned max)
Definition: stress32.cpp:760
static void FillSetsRegular(bvect_mini *bvect_min, bvect *bvect_full, unsigned, unsigned max, unsigned)
Definition: stress32.cpp:813
void swap_bits(BV &bv, typename BV::size_type i1, typename BV::size_type i2)
Definition: stress32.cpp:17165
static void XorOperationsTest(bool detailed)
Definition: stress32.cpp:7905
static void FillTestBuffer(bm::compressed_buffer_collection< bvect >::buffer_type &buf)
Definition: stress32.cpp:33837
static void EmptyBVTest()
Definition: stress32.cpp:3967
void CheckBV_AND_OR(BV &bv_target, const BV &bv1, const BV &bv2)
Definition: stress32.cpp:6855
static void BvectorFindReverseTest()
Definition: stress32.cpp:19400
bool is_bvser
Definition: stress32.cpp:37860
static void GenerateShiftTestCollection(std::vector< bvect > *target, unsigned count=30, unsigned vector_max=40000000, bool optimize=true)
Definition: stress32.cpp:5169
static void MaxSTest()
Definition: stress32.cpp:15558
static void TestCompressedCollection()
Definition: stress32.cpp:33874
static void TestStrSparseVectorSerial()
Definition: stress32.cpp:30077
static void HMaskTest()
Definition: stress32.cpp:20338
static void TestBlockExpandCompact()
Definition: stress32.cpp:34294
static void SerializationTest()
Definition: stress32.cpp:14424
static void BVectorInsert(bvect *bv, unsigned pos, bool value)
Definition: stress32.cpp:935
void IntervalsCheck(const BV &bv)
Definition: stress32.cpp:1969
static void TestNibbleArr()
Definition: stress32.cpp:20082
bool is_allsvser
Definition: stress32.cpp:37879
static void TestBlockToGAP()
Definition: stress32.cpp:3361
void VisitorAllRangeTest(const BV &bv, typename BV::size_type step=1)
Definition: stress32.cpp:371
static void CheckRangeDeserial(const bvect &bv, bvect::size_type from, bvect::size_type to)
Definition: stress32.cpp:9435
static void TestSparseVectorScan()
Definition: stress32.cpp:26308
static void FillSetsRandom(bvect_mini *bvect_min, bvect *bvect_full, unsigned min, unsigned max, unsigned fill_factor)
Definition: stress32.cpp:773
static void SerializationBufferTest()
Definition: stress32.cpp:12946
static void TestStrSparseVector()
Definition: stress32.cpp:28089
static void FillSetsRandomMethod(bvect_mini *bvect_min, bvect *bvect_full, unsigned min, unsigned max, int optimize=0, int method=-1)
Definition: stress32.cpp:840
static int bit_decode_func(void *handle_ptr, bm::id_t bit_idx)
Definition: stress32.cpp:348
void GeneratePipelineTestData(std::vector< string > &str_coll, str_svect_type &str_sv, unsigned max_coll=8000000, unsigned repeat_factor=10)
Definition: stress32.cpp:31343
static void TestBlockAND()
Definition: stress32.cpp:34576
static void show_help()
Definition: stress32.cpp:37828
static void BlockDigestTest()
Definition: stress32.cpp:16732
static void CheckGap2DGap(gap_vector &gapv)
Definition: stress32.cpp:11705
static void DetailedCheckVectors(const bvect &bv1, const bvect &bv2)
Definition: stress32.cpp:1882
static void TestBasicMatrix()
Definition: stress32.cpp:21873
static void TestCompressedSparseVectorScanGT()
Definition: stress32.cpp:27715
static void SubOperationsTest(bool detailed)
Definition: stress32.cpp:7559
static void GetNextTest()
Definition: stress32.cpp:15196
static void SyntaxTest()
Definition: stress32.cpp:16398
void PrintStacks(unsigned max_cnt=10)
Definition: stress32.cpp:38053
static void TestStrSparseVector_FindEq()
Definition: stress32.cpp:29769
static void FillSets(bvect_mini *bvect_min, bvect *bvect_full, unsigned min, unsigned max, unsigned fill_factor)
Definition: stress32.cpp:533
bool CompareSparseVector(const SV &sv, const Vect &vect, bool interval_filled=false, bool detailed=true)
Definition: stress32.cpp:21482
static void generate_sparse_bv(bvect &bv, bvect::size_type from, bvect::size_type to, bvect::size_type step=65536/10)
Definition: stress32.cpp:9595
bm::sparse_vector< signed long long, bvect > sparse_vector_i64
Definition: stress32.cpp:242
static void BlockBitInsertTest()
Definition: stress32.cpp:3652
static void BitForEachRangeFuncTest()
Definition: stress32.cpp:18122
static void print_bv(const bvect &bv)
Definition: stress32.cpp:901
void IntervalsEnumeratorCheck(const BV &bv, bool report)
Definition: stress32.cpp:2167
bool is_csv
Definition: stress32.cpp:37872
void CheckStrSVCompare(const STR_SV &str_sv, typename STR_SV::size_type limit=0)
Definition: stress32.cpp:28046
static void TestHeapVector()
Definition: stress32.cpp:37610
static void KleeneLogicTest()
Definition: stress32.cpp:8710
static void ExportTest()
Definition: stress32.cpp:20006
static void TestFindFirst()
Definition: stress32.cpp:34146
static void CalcBeginMask()
Definition: stress32.cpp:15582
bool is_bvbasic
Definition: stress32.cpp:37857
static void CheckGAPMin(const gap_vector &gapv, const bvect_mini &bvect_min, unsigned len)
Definition: stress32.cpp:1499
static void RangeForEachTest(bvect::size_type from, bvect::size_type to)
Definition: stress32.cpp:33492
static void TestSignedSparseVectorScan()
Definition: stress32.cpp:26961
static void DNACompressionTest()
Definition: stress32.cpp:18354
static void Intervals_RangesTest()
Definition: stress32.cpp:19506
static void TestFindBlockDiff()
Definition: stress32.cpp:34182
bool is_bvshift
Definition: stress32.cpp:37866
void CheckCountRange(const T &vect, const bvect::rs_index_type &bc_arr, unsigned left, unsigned right)
Definition: stress32.cpp:1722
static void RangeDeserializationTest()
Definition: stress32.cpp:9609
static void TestBlockSUB()
Definition: stress32.cpp:34811
void CheckSparseVectorFilter(const SV &sv, unsigned factor)
Definition: stress32.cpp:25741
static void TestSparseVectorSerialization2()
Definition: stress32.cpp:14840
bool is_ser
Definition: stress32.cpp:37878
void CheckRangeCopy(const bvect &bv, unsigned from, unsigned to)
Definition: stress32.cpp:1658
static void TestSparseVectorScanGT()
Definition: stress32.cpp:26652
void DetailedCheckCompressedDecode(const CSV &csv)
Definition: stress32.cpp:35431
bool is_bvb0
Definition: stress32.cpp:37858
bm::rs_index< dbg_alloc > rs_ind
Definition: stress32.cpp:226
unsigned CalcBitCount32(unsigned b)
Definition: stress32.cpp:17447
static void verify_all_one_ranges(const bvect &bv, bool all_one)
Definition: stress32.cpp:19266
bool is_bvops0
Definition: stress32.cpp:37862
static void BitRangeAllSetTest()
Definition: stress32.cpp:18021
mem_alloc< dbg_block_allocator, dbg_ptr_allocator, alloc_pool< dbg_block_allocator, dbg_ptr_allocator > > dbg_alloc
Definition: stress32.cpp:222
static void TestCompressSparseVectorSerial()
Definition: stress32.cpp:37356
static void s_READ_NUMERIC_BUFF(const string &data, size_t &pos, T &var)
Definition: stress32.cpp:38211
static void FillSetClearIntervals(bvect_mini *bvect_min, bvect *bvect_full, unsigned min, unsigned max, unsigned fill_factor)
Definition: stress32.cpp:749
std::mutex g_trace_lock
Definition: stress32.cpp:108
static void BitForEachTest()
Definition: stress32.cpp:20212
static void TestSparseVector_Stress(unsigned count)
Definition: stress32.cpp:27898
void generate_bvector(bvect &bv, unsigned vector_max=40000000, bool optimize=true)
Definition: stress32.cpp:33449
static void FindNotNullPtrTest()
Definition: stress32.cpp:16342
static void BlockLevelTest()
Definition: stress32.cpp:15900
bool is_low_level
Definition: stress32.cpp:37855
static void DesrializationTest2()
Definition: stress32.cpp:9270
static void generate_sparse_bvector(bvect &bv, unsigned min=0, unsigned max=40000000, unsigned fill_factor=65536)
Definition: stress32.cpp:5149
str_sparse_vector< char, bvect, 3 > str_svect_type
Definition: stress32.cpp:30781
static void TestStrSparseSort()
Definition: stress32.cpp:31706
static void BitEncoderTest()
Definition: stress32.cpp:20755
bm::bvector< dbg_alloc > bvect
Definition: stress32.cpp:224
static void TestTasks()
Definition: stress32.cpp:37791
void ReadTestData(STR_SV &str_sv, HASH_SV &sv_hash, unsigned hash_bitcnt, const string &fname)
Definition: stress32.cpp:38109
void GroupByTest(const char *filename, const char *query_filename)
Definition: stress32.cpp:32452
static bool FindLastBit(const bvect &bv, bm::id_t &last_pos)
Definition: stress32.cpp:1954
bool is_bvb1
Definition: stress32.cpp:37859
static void TestBlockCountChange()
Definition: stress32.cpp:2794
static void TestRandomSubset(const bvect &bv, bm::random_subset< bvect > &rsub)
Definition: stress32.cpp:6010
static void CompareStrSparseVector(const str_svect_type &str_sv, const vector< string > &str_coll)
Definition: stress32.cpp:30784
void NULL_serial_search_test()
Definition: stress32.cpp:38746
static void EraseStrCollection(str_svect_type &str_sv)
Definition: stress32.cpp:30916
static void TestBlockDigest()
Definition: stress32.cpp:34393
bool is_silent
Definition: stress32.cpp:83
static void generate_string_set(vector< string > &str_vec, const unsigned max_coll=150000, unsigned repeat=220, bool shuffle=true)
Definition: stress32.cpp:31601
void CheckCountGapRange(const T &vect, unsigned left, unsigned right, unsigned *block_count_arr=0)
Definition: stress32.cpp:1595
void generate_serialization_test_set(SV &sv, typename SV::size_type vector_max)
Definition: stress32.cpp:14817
void test_str_sv_des_fnc()
Definition: stress32.cpp:38650
void LoadBVDump(const char *filename, const char *filename_out=0, bool validate=false)
Definition: stress32.cpp:32338
static void TestSparseVector_XOR_Scanner()
Definition: stress32.cpp:24241
static void optimize_fill(bvect &bv, bvect::size_type base, unsigned inc, bvect::size_type max_bits=bm::gap_max_bits, bool value=true)
Definition: stress32.cpp:4973
static void BitCountChangeTest()
Definition: stress32.cpp:17533
static void TestSQueue()
Definition: stress32.cpp:37673
void CompareMiniSet(const A &ms, const B &bvm)
Definition: stress32.cpp:17278
void quicksort2(str_svect_type &strsv, int first, int last)
Definition: stress32.cpp:31569
void BitBlockTransposeTest()
Definition: stress32.cpp:18588
static void AddressResolverTest()
Definition: stress32.cpp:33345
static void StressTestAggregatorOR(unsigned repetitions)
Definition: stress32.cpp:10467
static void TestRankCompress()
Definition: stress32.cpp:34909
static bvect bvect_test_return()
Definition: stress32.cpp:16378
static void TestSparseVectorRange()
Definition: stress32.cpp:25633
bool is_csv1
Definition: stress32.cpp:37874
unsigned proxy_bmi1_select64_lz(bm::id64_t val, unsigned rank)
Definition: stress32.cpp:20518
static void ResizeTest()
Definition: stress32.cpp:18894
static void GAPCheck()
Definition: stress32.cpp:11729
static void RSIndexTest()
Definition: stress32.cpp:2493
static void TestSparseVectorGatherDecode()
Definition: stress32.cpp:25432
void CheckCompressedDecode(const CSV &csv, unsigned from, unsigned size)
Definition: stress32.cpp:35377
static void TestAND_OR(bm::random_subset< bvect > &rsub, bvect::size_type count, const bvect &bvect_full1, const bvect &bvect_full2)
Definition: stress32.cpp:11073
static void RangeCopyTest()
Definition: stress32.cpp:6234
static void StressTestAggregatorAND_SUB(unsigned repetitions)
Definition: stress32.cpp:10825
#define BITSCAN_NIBGCC
bool is_all
Definition: stress32.cpp:37854
static void TestBlockLast()
Definition: stress32.cpp:34015
static void CheckIntervals(const bvect &bv, unsigned)
Definition: stress32.cpp:1549
static void TestBlockCountXORChange()
Definition: stress32.cpp:2947
static void TestCompressedSparseVectorAlgo()
Definition: stress32.cpp:27430
void bvector_transform_11(typename SV::bvector_type &bvect_in, const SV &sv_brel, typename SV::bvector_type &bvect_out)
Definition: stress32.cpp:25573
static void CalcEndMask()
Definition: stress32.cpp:15613
static void RangeRandomFillTest()
Definition: stress32.cpp:6183
bool is_sv0
Definition: stress32.cpp:37870
bool is_c_coll
Definition: stress32.cpp:37877
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
Definition: stress32.cpp:243
static void Check_SimModel(const bm::xor_sim_model< bvect > &sim1, const bm::xor_sim_model< bvect > &sim2)
Definition: stress32.cpp:13014
const unsigned BITVECT_SIZE
Definition: stress32.cpp:251
bool is_sv
Definition: stress32.cpp:37869
static void TestSignedSparseSort()
Definition: stress32.cpp:32125
bool TestEqualSparseVectors(const SV &sv1, const SV &sv2, bool detailed=true)
Definition: stress32.cpp:21637
static void BvectorEraseTest()
Definition: stress32.cpp:5833
static void AndOperationsTest(bool detailed)
Definition: stress32.cpp:6342
void GenerateSV(SV &sv, unsigned strategy=0)
Definition: stress32.cpp:35094
unsigned proxy_bmi2_select64_pdep(bm::id64_t val, unsigned rank)
Definition: stress32.cpp:20539
static void BlockBitEraseTest()
Definition: stress32.cpp:3772
static void GenerateRandomKleenVect(bvect &bv_v, bvect &bv_null, bvect::size_type size, size_t sparse_factor=0)
Definition: stress32.cpp:8594
static void MutationOperationsTest()
Definition: stress32.cpp:12763
static void FreezeTest()
Definition: stress32.cpp:16493
bm::id64_t bit_block_calc_xor_change_digest(const bm::word_t *block, const bm::word_t *xor_block, block_waves_xor_descr &x_descr, bm::xor_complement_match &match_type)
Definition: stress32.cpp:2905
static void print_gap(const gap_vector &gap_vect, unsigned)
Definition: stress32.cpp:1483
static void GenerateTestCollection(std::vector< bvect > *target, unsigned count=30, unsigned vector_max=40000000)
Definition: stress32.cpp:10985
bool test_interval(const BV &bv, typename BV::size_type left, typename BV::size_type right)
Reference (naive) inetrval detector based on population counting and boundaries tests.
Definition: stress32.cpp:260
static void IntervalEnumeratorTest()
Definition: stress32.cpp:16062
static void KleeneLogicAndStressTest(unsigned repeats=150, unsigned size=1000000)
Definition: stress32.cpp:8616
static void CompareEnumerators(const bvect::enumerator &en1, const bvect::enumerator &en2)
Definition: stress32.cpp:1939
bm::sparse_vector< int, bvect > sparse_vector_i32
Definition: stress32.cpp:240
static unsigned SerializationOperation(bvect *bv_target, bvect &bv1, bvect &bv2, set_operation op, bool check_reverse=false)
Definition: stress32.cpp:988
static void TestSIMDUtils()
Definition: stress32.cpp:32733
static void TestSparseSort()
Definition: stress32.cpp:31920
void test_chr21()
submitted by Andreay (GBench)
Definition: stress32.cpp:38794
static void TestCompressSparseSignedVector()
Definition: stress32.cpp:36645
bool is_bvops1
Definition: stress32.cpp:37863
static void CheckSparseVectorGatherRandom(const sparse_vector_u32 &sv, unsigned gather_size)
Definition: stress32.cpp:25393
static void AndOrOperationsTest(bool detailed)
Definition: stress32.cpp:6899
static void TestStrSparseVectorAlgo()
Definition: stress32.cpp:29691
static void SetTest()
Definition: stress32.cpp:16843
static void KleeneLogicOrStressTest(unsigned repeats=150, unsigned size=1000000)
Definition: stress32.cpp:8663
void DetailedCompareSparseVectors(const CSV &csv, const SV &sv)
Definition: stress32.cpp:35241
static void TestSparseVectorInserter()
Definition: stress32.cpp:25215
void CheckSparseVectorRange(const SV &sv, unsigned left, unsigned right)
Definition: stress32.cpp:25582
static void TestSignedSparseVectorSerial()
Definition: stress32.cpp:24893
static void GAPTestStress()
Definition: stress32.cpp:12655
static int bit_decode_func2(void *, bm::id64_t bit_idx)
Definition: stress64.cpp:3116
std::vector< bm::id64_t > ref_vect
Definition: stress64.cpp:365
#define row(bind, expected)
Definition: string_bind.c:73
std::vector< bvect::size_type > decode_vector
Definition: stress32.cpp:18095
TestDecodeFunctor(decode_vector &dvect)
Definition: stress32.cpp:18099
decode_vector & dvect_
Definition: stress32.cpp:18118
bvect::size_type size_type
Definition: stress32.cpp:18096
int add_bits(size_type offset, const unsigned char *bits, unsigned size)
Definition: stress32.cpp:18105
int add_range(size_type offset, unsigned size)
Definition: stress32.cpp:18111
void build_batch(bm::task_batch< typename BV::allocator_type > &batch)
Definition: stress32.cpp:37781
static int task_run(void *argp)
Definition: stress32.cpp:37770
Aggregation options to control execution Default settings are to support only result bit-vector filte...
Definition: bmaggregator.h:66
Aggregator arg groups.
Definition: bmaggregator.h:157
size_t add(const bvector_type *bv, unsigned agr_group)
Add bit-vector pointer to its aggregation group.
bv_vector_type arg_bv0
arg group 0
Definition: bmaggregator.h:158
bv_vector_type arg_bv1
arg group 1
Definition: bmaggregator.h:159
and functor
Definition: bmutil.h:479
Bit AND functor.
Definition: bmfunc.h:9455
Functor for bit-copy (for testing)
Definition: bmalgo.h:293
XOR match chain.
Definition: bmxor.h:290
BLOCK_IDX nb
Definition: bmxor.h:291
unsigned chain_size
Definition: bmxor.h:292
bm::id64_t xor_d64[64]
Definition: bmxor.h:294
unsigned ref_idx[64]
Definition: bmxor.h:293
Structure keeps all-left/right ON bits masks.
Definition: bmconst.h:364
Structure to compute XOR gap-count profile by sub-block waves.
Definition: bmxor.h:230
unsigned short sb_bc[bm::block_waves]
BIT counts.
Definition: bmxor.h:233
unsigned short sb_gc[bm::block_waves]
GAP counts.
Definition: bmxor.h:232
unsigned short sb_xor_gc[bm::block_waves]
XOR-mask GAP count.
Definition: bmxor.h:236
unsigned short sb_xor_bc[bm::block_waves]
XOR-mask GAP count.
Definition: bmxor.h:237
Capture the XOR filter results (xor block against ref.block)
Definition: bmxor.h:245
size_type ref_idx
reference vector index
Definition: bmxor.h:250
bm::id64_t xor_d64
recorded digest
Definition: bmxor.h:251
bm::xor_complement_match match_type
match type
Definition: bmxor.h:248
Structure with statistical information about memory allocation for arena based vectors.
Definition: bmfunc.h:121
size_t bit_blocks_sz
Total size of bit blocks.
Definition: bmfunc.h:122
size_t gap_blocks_sz
Total size of gap blocks.
Definition: bmfunc.h:123
size_t ptr_sub_blocks_sz
Total size of sub-blocks ptrs.
Definition: bmfunc.h:124
size_t ptr_sub_blocks
Number of sub-blocks.
Definition: bmfunc.h:59
unsigned long long gaps_by_level[bm::gap_levels]
number of GAP blocks at each level
Definition: bmfunc.h:65
size_t gap_blocks
Number of GAP blocks.
Definition: bmfunc.h:58
size_t bit_blocks
Number of bit blocks.
Definition: bmfunc.h:57
void reset() noexcept
Reset statisctics.
Definition: bmfunc.h:94
size_t max_serialize_mem
estimated maximum memory for serialization
Definition: bmfunc.h:61
size_t memory_used
memory usage for all blocks and service tables
Definition: bmfunc.h:62
Statistical information about bitset's memory allocation details.
Definition: bm.h:125
ad-hoc conditional expressions
Definition: bmutil.h:113
Alternative GAP lengths table. Good for for memory saver mode and very sparse bitsets.
Definition: bmconst.h:411
Default GAP lengths table.
Definition: bmconst.h:396
XOR match pair.
Definition: bmxor.h:274
bvector_size_type ref_idx
reference vector index
Definition: bmxor.h:275
bm::id64_t xor_d64
recorded digest
Definition: bmxor.h:276
Second second
Definition: bmfunc.h:156
layout class for serialization buffer structure
void resize(size_t ssize)
Set new serialized size.
const unsigned char * buf() const noexcept
Return serialization buffer pointer.
size_t size() const noexcept
return current serialized size
BitMagic task with a captured function.
Definition: bmtask.h:62
void * argp
arg pointer
Definition: bmtask.h:72
task_function_t func
captured function callback
Definition: bmtask.h:71
XOR similarity model.
Definition: bmxor.h:791
matrix_chain_type matr
model matrix
Definition: bmxor.h:804
bvector_type bv_blocks
blocks digest
Definition: bmxor.h:805
Parameters for XOR similarity search.
Definition: bmxor.h:59
Definition: _hash_fun.h:40
bm::sparse_vector< unsigned, bm::bvector<> > svector
Definition: test_bm.cpp:58
float g0(Seg_Nsm *spn, Thd_Cxe *cxe)
Definition: thrddgri.c:90
int g(Seg_Gsm *spe, Seq_Mtf *psm, Thd_Gsm *tdg)
Definition: thrddgri.c:44
else result
Definition: token2.c:20
void free(voidpf ptr)
voidp malloc(uInt size)
Modified on Wed May 01 14:25:10 2024 by modify_doxy.py rev. 669887