~ubuntu-branches/ubuntu/trusty/bmagic/trusty

« back to all changes in this revision

Viewing changes to tests/stress/t.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Andres Salomon
  • Date: 2008-01-05 23:58:56 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080105235856-2kmxhxkz14qjy9ia
Tags: 3.5.0-1
* New upstream release.
* Add tcpp.dpatch.  This stops tests/stress/t.cpp from including
  ncbi_pch.hpp.  As far as I can tell, NCBI is not used at all, I have
  no idea where that came from..
* Silence some lintian warnings; binary-arch-rules-but-pkg-is-arch-indep
  and ancient-standards-version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (c) 2002,2003 Anatoliy Kuznetsov.
3
 
 
4
 
Permission is hereby granted, free of charge, to any person 
5
 
obtaining a copy of this software and associated documentation 
6
 
files (the "Software"), to deal in the Software without restriction, 
7
 
including without limitation the rights to use, copy, modify, merge, 
8
 
publish, distribute, sublicense, and/or sell copies of the Software, 
9
 
and to permit persons to whom the Software is furnished to do so, 
10
 
subject to the following conditions:
11
 
 
12
 
The above copyright notice and this permission notice shall be included 
13
 
in all copies or substantial portions of the Software.
14
 
 
15
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
16
 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
17
 
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
18
 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
19
 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
20
 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
21
 
OTHER DEALINGS IN THE SOFTWARE.
22
 
*/
23
 
//#define BM64OPT
24
 
//#define BM_SET_MMX_GUARD
25
 
//#define BMSSE2OPT
26
 
#define BMCOUNTOPT
27
 
 
28
 
#include <stdio.h>
29
 
#include <stdlib.h>
30
 
#include <assert.h>
31
 
#include <memory.h>
32
 
#include <iostream>
33
 
#include <time.h>
34
 
#include "bm.h"
35
 
#include "bmalgo.h"
36
 
#include "bmserial.h"
37
 
 
38
 
using namespace bm;
39
 
using namespace std;
40
 
 
41
 
#include "rlebtv.h"
42
 
#include "encoding.h"
43
 
#include <limits.h>
44
 
 
45
 
 
46
 
 
47
 
#define POOL_SIZE 5000
48
 
 
49
 
//#define MEM_POOL
50
 
 
51
 
 
52
 
template<class T> T* pool_allocate(T** pool, int& i, size_t n)
53
 
{
54
 
    return i ? pool[i--] : (T*) ::malloc(n * sizeof(T));
55
 
}
56
 
 
57
 
inline void* pool_allocate2(void** pool, int& i, size_t n)
58
 
{
59
 
    return i ? pool[i--] : malloc(n * sizeof(void*));
60
 
}
61
 
 
62
 
 
63
 
 
64
 
template<class T> void pool_free(T** pool, int& i, T* p)
65
 
{
66
 
    i < POOL_SIZE ? (free(p),(void*)0) : pool[++i]=p;
67
 
}
68
 
 
69
 
 
70
 
class pool_block_allocator
71
 
{
72
 
public:
73
 
 
74
 
    static bm::word_t* allocate(size_t n, const void *)
75
 
    {
76
 
        int *idx = 0;
77
 
        bm::word_t** pool = 0;
78
 
 
79
 
        switch (n)
80
 
        {
81
 
        case bm::set_block_size:
82
 
            idx = &bit_blocks_idx_;
83
 
            pool = free_bit_blocks_;
84
 
            break;
85
 
 
86
 
        case 64:
87
 
            idx = &gap_blocks_idx0_;
88
 
            pool = gap_bit_blocks0_;
89
 
            break;
90
 
 
91
 
        case 128:
92
 
            idx = &gap_blocks_idx1_;
93
 
            pool = gap_bit_blocks1_;
94
 
            break;
95
 
        
96
 
        case 256:
97
 
            idx = &gap_blocks_idx2_;
98
 
            pool = gap_bit_blocks2_;
99
 
            break;
100
 
 
101
 
        case 512:
102
 
            idx = &gap_blocks_idx3_;
103
 
            pool = gap_bit_blocks3_;
104
 
            break;
105
 
 
106
 
        default:
107
 
            assert(0);
108
 
        }
109
 
 
110
 
        return pool_allocate(pool, *idx, n);
111
 
    }
112
 
 
113
 
    static void deallocate(bm::word_t* p, size_t n)
114
 
    {
115
 
        int *idx = 0;
116
 
        bm::word_t** pool = 0;
117
 
 
118
 
        switch (n)
119
 
        {
120
 
        case bm::set_block_size:
121
 
            idx = &bit_blocks_idx_;
122
 
            pool = free_bit_blocks_;
123
 
            break;
124
 
 
125
 
        case 64:
126
 
            idx = &gap_blocks_idx0_;
127
 
            pool = gap_bit_blocks0_;
128
 
            break;
129
 
 
130
 
        case 128:
131
 
            idx = &gap_blocks_idx1_;
132
 
            pool = gap_bit_blocks1_;
133
 
            break;
134
 
        
135
 
        case 256:
136
 
            idx = &gap_blocks_idx2_;
137
 
            pool = gap_bit_blocks2_;
138
 
            break;
139
 
 
140
 
        case 512:
141
 
            idx = &gap_blocks_idx3_;
142
 
            pool = gap_bit_blocks3_;
143
 
            break;
144
 
 
145
 
        default:
146
 
            assert(0);
147
 
        }
148
 
 
149
 
        pool_free(pool, *idx, p);
150
 
    }
151
 
 
152
 
private:
153
 
    static bm::word_t* free_bit_blocks_[];
154
 
    static int         bit_blocks_idx_;
155
 
 
156
 
    static bm::word_t* gap_bit_blocks0_[];
157
 
    static int         gap_blocks_idx0_;
158
 
 
159
 
    static bm::word_t* gap_bit_blocks1_[];
160
 
    static int         gap_blocks_idx1_;
161
 
 
162
 
    static bm::word_t* gap_bit_blocks2_[];
163
 
    static int         gap_blocks_idx2_;
164
 
 
165
 
    static bm::word_t* gap_bit_blocks3_[];
166
 
    static int         gap_blocks_idx3_;
167
 
};
168
 
 
169
 
bm::word_t* pool_block_allocator::free_bit_blocks_[POOL_SIZE];
170
 
int pool_block_allocator::bit_blocks_idx_ = 0;
171
 
 
172
 
bm::word_t* pool_block_allocator::gap_bit_blocks0_[POOL_SIZE];
173
 
int pool_block_allocator::gap_blocks_idx0_ = 0;
174
 
 
175
 
bm::word_t* pool_block_allocator::gap_bit_blocks1_[POOL_SIZE];
176
 
int pool_block_allocator::gap_blocks_idx1_ = 0;
177
 
 
178
 
bm::word_t* pool_block_allocator::gap_bit_blocks2_[POOL_SIZE];
179
 
int pool_block_allocator::gap_blocks_idx2_ = 0;
180
 
 
181
 
bm::word_t* pool_block_allocator::gap_bit_blocks3_[POOL_SIZE];
182
 
int pool_block_allocator::gap_blocks_idx3_ = 0;
183
 
 
184
 
 
185
 
 
186
 
 
187
 
class pool_ptr_allocator
188
 
{
189
 
public:
190
 
 
191
 
    static void* allocate(size_t n, const void *)
192
 
    {
193
 
        return pool_allocate2(free_ptr_blocks_, ptr_blocks_idx_, n);
194
 
    }
195
 
 
196
 
    static void deallocate(void* p, size_t)
197
 
    {
198
 
        pool_free(free_ptr_blocks_, ptr_blocks_idx_, p);
199
 
    }
200
 
 
201
 
private:
202
 
    static void*  free_ptr_blocks_[];
203
 
    static int    ptr_blocks_idx_;
204
 
};
205
 
 
206
 
void* pool_ptr_allocator::free_ptr_blocks_[POOL_SIZE];
207
 
int pool_ptr_allocator::ptr_blocks_idx_ = 0;
208
 
 
209
 
 
210
 
//#define MEM_DEBUG
211
 
 
212
 
#ifdef MEM_DEBUG
213
 
 
214
 
 
215
 
class dbg_block_allocator
216
 
{
217
 
public:
218
 
static unsigned na_;
219
 
static unsigned nf_;
220
 
 
221
 
    static bm::word_t* allocate(size_t n, const void *)
222
 
    {
223
 
        ++na_;
224
 
        assert(n);
225
 
        bm::word_t* p =
226
 
            (bm::word_t*) ::malloc((n+1) * sizeof(bm::word_t));
227
 
        *p = n;
228
 
        return ++p;
229
 
    }
230
 
 
231
 
    static void deallocate(bm::word_t* p, size_t n)
232
 
    {
233
 
        ++nf_;
234
 
        --p;
235
 
        if(*p != n)
236
 
        {
237
 
            printf("Block memory deallocation error!\n");
238
 
            exit(1);
239
 
        }
240
 
        ::free(p);
241
 
    }
242
 
 
243
 
    static int balance()
244
 
    {
245
 
        return nf_ - na_;
246
 
    }
247
 
};
248
 
 
249
 
unsigned dbg_block_allocator::na_ = 0;
250
 
unsigned dbg_block_allocator::nf_ = 0;
251
 
 
252
 
class dbg_ptr_allocator
253
 
{
254
 
public:
255
 
static unsigned na_;
256
 
static unsigned nf_;
257
 
 
258
 
    static void* allocate(size_t n, const void *)
259
 
    {
260
 
        ++na_;
261
 
        assert(sizeof(size_t) == sizeof(void*));
262
 
        void* p = ::malloc((n+1) * sizeof(void*));
263
 
        size_t* s = (size_t*) p;
264
 
        *s = n;
265
 
        return (void*)++s;
266
 
    }
267
 
 
268
 
    static void deallocate(void* p, size_t n)
269
 
    {
270
 
        ++nf_;
271
 
        size_t* s = (size_t*) p;
272
 
        --s;
273
 
        if(*s != n)
274
 
        {
275
 
            printf("Ptr memory deallocation error!\n");
276
 
            exit(1);
277
 
        }
278
 
        ::free(s);
279
 
    }
280
 
 
281
 
    static int balance()
282
 
    {
283
 
        return nf_ - na_;
284
 
    }
285
 
 
286
 
};
287
 
 
288
 
unsigned dbg_ptr_allocator::na_ = 0;
289
 
unsigned dbg_ptr_allocator::nf_ = 0;
290
 
 
291
 
 
292
 
typedef mem_alloc<dbg_block_allocator, dbg_ptr_allocator> dbg_alloc;
293
 
 
294
 
typedef bm::bvector<dbg_alloc> bvect;
295
 
typedef bm::bvector_mini<dbg_block_allocator> bvect_mini;
296
 
 
297
 
#else
298
 
 
299
 
#ifdef MEM_POOL
300
 
 
301
 
typedef mem_alloc<pool_block_allocator, pool_ptr_allocator> pool_alloc;
302
 
typedef bm::bvector<pool_alloc> bvect;
303
 
typedef bm::bvector_mini<bm::block_allocator> bvect_mini;
304
 
 
305
 
 
306
 
#else
307
 
 
308
 
typedef bm::bvector<> bvect;
309
 
typedef bm::bvector_mini<bm::block_allocator> bvect_mini;
310
 
 
311
 
#endif
312
 
 
313
 
#endif
314
 
 
315
 
//const unsigned BITVECT_SIZE = 100000000 * 8;
316
 
 
317
 
// This this setting program will consume around 150M of RAM
318
 
const unsigned BITVECT_SIZE = 100000000 * 2;
319
 
 
320
 
const unsigned ITERATIONS = 100000;
321
 
const unsigned PROGRESS_PRINT = 2000000;
322
 
 
323
 
 
324
 
 
325
 
void CheckVectors(bvect_mini &bvect_min, 
326
 
                  bvect      &bvect_full,
327
 
                  unsigned size,
328
 
                  bool     detailed = false);
329
 
 
330
 
 
331
 
unsigned random_minmax(unsigned min, unsigned max)
332
 
{
333
 
    unsigned r = (rand() << 16) | rand();
334
 
    return r % (max-min) + min;
335
 
}
336
 
 
337
 
 
338
 
void FillSets(bvect_mini* bvect_min, 
339
 
              bvect* bvect_full,
340
 
              unsigned min, 
341
 
              unsigned max,
342
 
              unsigned fill_factor)
343
 
{
344
 
    unsigned i;
345
 
    unsigned id;
346
 
 
347
 
    //Random filling
348
 
    if(fill_factor == 0)
349
 
    {
350
 
        unsigned n_id = (max - min) / 100;
351
 
        printf("random filling : %i\n", n_id);
352
 
        for (i = 0; i < n_id; i++)
353
 
        {
354
 
            id = random_minmax(min, max);
355
 
            bvect_min->set_bit(id);
356
 
            bvect_full->set_bit(id);
357
 
            if (PROGRESS_PRINT)
358
 
            {
359
 
                if ( (i % PROGRESS_PRINT) == 0)
360
 
                {
361
 
                    cout << "+" << flush;
362
 
                }
363
 
            }
364
 
        }
365
 
        cout << endl;
366
 
    }
367
 
    else
368
 
    {
369
 
        printf("fill_factor random filling : factor = %i\n", fill_factor);
370
 
 
371
 
        for(i = 0; i < fill_factor; i++)
372
 
        {
373
 
            int k = rand() % 10;
374
 
            if (k == 0)
375
 
                k+=2;
376
 
 
377
 
            //Calculate start
378
 
            unsigned start = min + (max - min) / (fill_factor * k);
379
 
 
380
 
            //Randomize start
381
 
            start += random_minmax(1, (max - min) / (fill_factor * 10));
382
 
 
383
 
            if (start > max)
384
 
            {
385
 
                start = min;
386
 
            }
387
 
            
388
 
            //Calculate end 
389
 
            unsigned end = start + (max - start) / (fill_factor *2);
390
 
 
391
 
            //Randomize end
392
 
            end -= random_minmax(1, (max - start) / (fill_factor * 10));
393
 
 
394
 
            if (end > max )
395
 
            {
396
 
                end = max;
397
 
            }
398
 
 
399
 
            
400
 
            if (fill_factor > 1)
401
 
            {
402
 
                for(; start < end;)
403
 
                {
404
 
                    int r = rand() % 8;
405
 
 
406
 
                    if (r > 7)
407
 
                    {
408
 
                        int inc = rand() % 3;
409
 
                        ++inc;
410
 
                        unsigned end2 = start + rand() % 1000;
411
 
                        if (end2 > end)
412
 
                            end2 = end;
413
 
                        while (start < end2)
414
 
                        {
415
 
                            bvect_min->set_bit(start);
416
 
                            bvect_full->set_bit(start);  
417
 
                            start += inc;
418
 
                        }
419
 
 
420
 
                        if (PROGRESS_PRINT)
421
 
                        {
422
 
                            if ( ( ((i+1)*(end-start))  % PROGRESS_PRINT) == 0)
423
 
                            {
424
 
                                cout << "+" << flush;
425
 
                            }
426
 
                        }
427
 
 
428
 
                        continue;
429
 
                    }
430
 
 
431
 
                    if (r)
432
 
                    {
433
 
                        bvect_min->set_bit(start);
434
 
                        bvect_full->set_bit(start);
435
 
                        ++start;
436
 
                    }
437
 
                    else
438
 
                    {
439
 
                        start+=r;
440
 
                        bvect_min->set_bit(start);
441
 
                        bvect_full->set_bit(start);
442
 
                    }
443
 
 
444
 
                    if (PROGRESS_PRINT)
445
 
                    {
446
 
                        if ( ( ((i+1)*(end-start))  % PROGRESS_PRINT) == 0)
447
 
                        {
448
 
                            cout << "+" << flush;
449
 
                        }
450
 
                    }
451
 
                }
452
 
 
453
 
            }
454
 
            else
455
 
            {
456
 
                int c = rand() % 15;
457
 
                if (c == 0)
458
 
                    ++c;
459
 
                for(; start < end; ++start)
460
 
                {
461
 
                    bvect_min->set_bit(start);
462
 
                    bvect_full->set_bit(start);
463
 
 
464
 
                    if (start % c)
465
 
                    {
466
 
                        start += c;
467
 
                    }
468
 
 
469
 
                    if (PROGRESS_PRINT)
470
 
                    {
471
 
                        if ( ( ((i+1)*(end-start))  % PROGRESS_PRINT) == 0)
472
 
                        {
473
 
                            cout << "+" << flush;
474
 
                        }
475
 
                    }
476
 
 
477
 
                }
478
 
            }
479
 
            cout << endl;
480
 
 
481
 
        }
482
 
    }
483
 
}
484
 
 
485
 
//
486
 
// Interval filling.
487
 
// 111........111111........111111..........11111111.......1111111...
488
 
//
489
 
 
490
 
 
491
 
void FillSetsIntervals(bvect_mini* bvect_min, 
492
 
              bvect* bvect_full,
493
 
              unsigned min, 
494
 
              unsigned max,
495
 
              unsigned fill_factor,
496
 
              bool set_flag=true)
497
 
{
498
 
 
499
 
    while(fill_factor==0)
500
 
    {
501
 
        fill_factor=rand()%10;
502
 
    }
503
 
 
504
 
    cout << "Intervals filling. Factor=" 
505
 
         <<  fill_factor << endl << endl;
506
 
 
507
 
    unsigned i, j;
508
 
    unsigned factor = 70 * fill_factor;
509
 
    for (i = min; i < max; ++i)
510
 
    {
511
 
        unsigned len, end; 
512
 
 
513
 
        do
514
 
        {
515
 
            len = rand() % factor;
516
 
            end = i+len;
517
 
            
518
 
        } while (end >= max);
519
 
/*
520
 
        if (set_flag == false)
521
 
        {
522
 
            cout << "Cleaning: " << i << "-" << end << endl;
523
 
        }
524
 
        else
525
 
        {
526
 
            cout << "Add: " << i << "-" << end << endl;
527
 
        }
528
 
*/
529
 
        if (i < end)
530
 
        {
531
 
            bvect_full->set_range(i, end-1, set_flag);
532
 
        }
533
 
       
534
 
        for (j = i; j < end; ++j)
535
 
        {
536
 
            if (set_flag)
537
 
            {
538
 
                bvect_min->set_bit(j);
539
 
                //bvect_full->set_bit(j);
540
 
            }
541
 
            else
542
 
            {
543
 
                bvect_min->clear_bit(j);
544
 
                //bvect_full->clear_bit(j);
545
 
/*
546
 
        if (g_cnt_check)
547
 
        {
548
 
            bool b = bvect_full->count_check();
549
 
            if(!b)
550
 
            {
551
 
                cout << "Count check failed (clear)!" << endl;
552
 
                cout << "bit=" << j << endl;
553
 
                exit(1);
554
 
            }
555
 
        }
556
 
*/
557
 
            }
558
 
 
559
 
                           
560
 
        } // j
561
 
 
562
 
//cout << "Checking range filling " << from << "-" << to << endl;
563
 
//CheckVectors(*bvect_min, *bvect_full, 100000000);
564
 
 
565
 
 
566
 
        i = end;
567
 
 
568
 
 
569
 
        len = rand() % (factor* 10 * bm::gap_max_bits);
570
 
        if (len % 2)
571
 
        {
572
 
            len *= rand() % (factor * 10);
573
 
        }
574
 
 
575
 
        i+=len;
576
 
 
577
 
        if ( (len % 6) == 0)  
578
 
        {
579
 
/*
580
 
if (set_flag == false)
581
 
{
582
 
    cout << "Additional Cleaning: " << i << "-" << end << endl;
583
 
}
584
 
*/
585
 
            for(unsigned k=0; k < 1000 && i < max; k+=3,i+=3)
586
 
            {
587
 
                if (set_flag)
588
 
                {
589
 
                    bvect_min->set_bit(i);
590
 
                    bvect_full->set_bit(i);            
591
 
                }
592
 
                else
593
 
                {
594
 
                    bvect_min->clear_bit(j);
595
 
                    bvect_full->clear_bit(j);
596
 
                }
597
 
 
598
 
            }
599
 
        }
600
 
 
601
 
    } // for i
602
 
 
603
 
}
604
 
 
605
 
void FillSetClearIntervals(bvect_mini* bvect_min, 
606
 
              bvect* bvect_full,
607
 
              unsigned min, 
608
 
              unsigned max,
609
 
              unsigned fill_factor)
610
 
{
611
 
    FillSetsIntervals(bvect_min, bvect_full, min, max, fill_factor, true);
612
 
    FillSetsIntervals(bvect_min, bvect_full, min, max, fill_factor, false);
613
 
}
614
 
 
615
 
 
616
 
void FillSetsRandom(bvect_mini* bvect_min, 
617
 
              bvect* bvect_full,
618
 
              unsigned min, 
619
 
              unsigned max,
620
 
              unsigned fill_factor)
621
 
{
622
 
    unsigned diap = max - min;
623
 
 
624
 
    unsigned count;
625
 
 
626
 
 
627
 
    switch (fill_factor)
628
 
    {
629
 
    case 0:
630
 
        count = diap / 1000;
631
 
        break;
632
 
    case 1:
633
 
        count = diap / 100;
634
 
        break;
635
 
    default:
636
 
        count = diap / 10;
637
 
        break;
638
 
 
639
 
    }
640
 
 
641
 
    for (unsigned i = 0; i < count; ++i)
642
 
    {
643
 
        unsigned bn = rand() % count;
644
 
        bn += min;
645
 
 
646
 
        if (bn > max)
647
 
        {
648
 
            bn = max;
649
 
        }
650
 
        bvect_min->set_bit(bn);
651
 
        bvect_full->set_bit(bn);   
652
 
        
653
 
        if ( (i  % PROGRESS_PRINT) == 0)
654
 
        {
655
 
            cout << "+" << flush;
656
 
        }
657
 
    }
658
 
    cout << "Ok" << endl;
659
 
 
660
 
}
661
 
 
662
 
 
663
 
//
664
 
//  Quasi random filling with choosing randomizing method.
665
 
//
666
 
//
667
 
void FillSetsRandomMethod(bvect_mini* bvect_min, 
668
 
                          bvect* bvect_full,
669
 
                          unsigned min, 
670
 
                          unsigned max,
671
 
                          int optimize = 0,
672
 
                          int method = -1)
673
 
{
674
 
    if (method == -1)
675
 
    {
676
 
        method = rand() % 5;
677
 
    }
678
 
    unsigned factor;
679
 
//method = 4;
680
 
    switch (method)
681
 
    {
682
 
 
683
 
    case 0:
684
 
        cout << "Random filling: method - FillSets - factor(0)" << endl;
685
 
        FillSets(bvect_min, bvect_full, min, max, 0);
686
 
        break;
687
 
 
688
 
    case 1:
689
 
        cout << "Random filling: method - FillSets - factor(random)" << endl;
690
 
        factor = rand()%3;
691
 
        FillSets(bvect_min, bvect_full, min, max, factor?factor:1);
692
 
        break;
693
 
 
694
 
    case 2:
695
 
        cout << "Random filling: method - Set-Clear Intervals - factor(random)" << endl;
696
 
        factor = rand()%10;
697
 
        FillSetClearIntervals(bvect_min, bvect_full, min, max, factor);
698
 
        break;
699
 
    case 3:
700
 
        cout << "Random filling: method - FillRandom - factor(random)" << endl;
701
 
        factor = rand()%3;
702
 
        FillSetsRandom(bvect_min, bvect_full, min, max, factor?factor:1);
703
 
        break;
704
 
 
705
 
    default:
706
 
        cout << "Random filling: method - Set Intervals - factor(random)" << endl;
707
 
        factor = rand()%10;
708
 
        FillSetsIntervals(bvect_min, bvect_full, min, max, factor);
709
 
        break;
710
 
 
711
 
    } // switch
712
 
 
713
 
    if (optimize && (method <= 1))
714
 
    {
715
 
        cout << "Vector optimization..." << flush;
716
 
        bvect::optmode opt = bvect::opt_compress;
717
 
        if (rand() % 3 == 0) {
718
 
            opt = bvect::opt_free_01;
719
 
        }
720
 
        bvect_full->optimize(0, opt);
721
 
        cout << "OK" << endl;
722
 
    }
723
 
}
724
 
 
725
 
 
726
 
 
727
 
void print_mv(const bvect_mini &bvect_min, unsigned size)
728
 
{
729
 
    unsigned i;
730
 
    for (i = 0; i < size; ++i)
731
 
    {
732
 
        bool bflag = bvect_min.is_bit_true(i) != 0; 
733
 
 
734
 
        if (bflag)
735
 
            printf("1");
736
 
        else
737
 
            printf("0");
738
 
        if ((i % 31) == 0 && (i != 0))
739
 
            printf(".");
740
 
    }
741
 
 
742
 
    printf("\n");
743
 
}
744
 
 
745
 
void print_gap(const gap_vector& gap_vect, unsigned size)
746
 
{
747
 
    const gap_word_t *buf = gap_vect.get_buf();
748
 
    unsigned len = gap_length(buf);
749
 
    printf("[%i:]", *buf++ & 1);
750
 
 
751
 
    for (unsigned i = 1; i < len; ++i)
752
 
    {
753
 
        printf("%i,", *buf++);
754
 
    }
755
 
 
756
 
    printf("\n");
757
 
}
758
 
 
759
 
void CheckGAPMin(const gap_vector& gapv, const bvect_mini& bvect_min, unsigned len)
760
 
{
761
 
    for (unsigned i = 0; i < len; ++i)
762
 
    {
763
 
        int bit1 = (gapv.is_bit_true(i) == 1);
764
 
        int bit2 = (bvect_min.is_bit_true(i) != 0);
765
 
        if(bit1 != bit2)
766
 
        {
767
 
           cout << "Bit comparison failed. " << "Bit N=" << i << endl;
768
 
           assert(0);
769
 
           exit(1);
770
 
        }
771
 
    }
772
 
}
773
 
 
774
 
void CheckIntervals(const bvect& bv, unsigned max_bit)
775
 
{
776
 
    unsigned cnt0 = count_intervals(bv);
777
 
    unsigned cnt1 = 1;
778
 
    bool bit_prev = bv.test(0);
779
 
    for (unsigned i = 1; i < max_bit; ++i)
780
 
    {
781
 
        bool bit = bv.test(i);
782
 
        cnt1 += bit_prev ^ bit;
783
 
        bit_prev = bit;
784
 
    }
785
 
    if (cnt0 != cnt1)
786
 
    {
787
 
        cout << "CheckIntervals error. " << "bm count=" << cnt0
788
 
             << " Control = " << cnt1 << endl;
789
 
        exit(1);
790
 
    }
791
 
}
792
 
 
793
 
template<class T> void CheckCountRange(const T& vect, 
794
 
                                       unsigned left, 
795
 
                                       unsigned right,
796
 
                                       unsigned* block_count_arr=0)
797
 
{
798
 
    unsigned cnt1 = vect.count_range(left, right, block_count_arr);
799
 
    unsigned cnt2 = 0;
800
 
//cout << endl;
801
 
    for (unsigned i = left; i <= right; ++i)
802
 
    {
803
 
        if (vect.test(i))
804
 
        {
805
 
//            cout << i << " " << flush;
806
 
            ++cnt2;
807
 
        }
808
 
    }
809
 
//cout << endl;
810
 
    if (cnt1 != cnt2)
811
 
    {
812
 
        cout << "Bitcount range failed!" << "left=" << left 
813
 
             << " right=" << right << endl
814
 
             << "count_range()=" << cnt1 
815
 
             << " check=" << cnt2;
816
 
        exit(1);
817
 
    }
818
 
}
819
 
 
820
 
 
821
 
unsigned BitCountChange(unsigned word)
822
 
{
823
 
    unsigned count = 1;
824
 
    unsigned bit_prev = word & 1;
825
 
    word >>= 1;
826
 
    for (unsigned i = 1; i < 32; ++i)
827
 
    {
828
 
        unsigned bit = word & 1;
829
 
        count += bit ^ bit_prev;
830
 
        bit_prev = bit;
831
 
        word >>= 1;
832
 
    }
833
 
    return count;
834
 
}
835
 
 
836
 
 
837
 
void DetailedCheckVectors(const bvect_mini &bvect_min, 
838
 
                          const bvect      &bvect_full,
839
 
                          unsigned size)
840
 
{
841
 
    cout << "Detailed check" << endl;
842
 
 
843
 
    //bvect_full.stat();
844
 
 
845
 
    // detailed bit by bit comparison. Paranoia check.
846
 
 
847
 
    unsigned i;
848
 
    for (i = 0; i < size; ++i)
849
 
    {
850
 
        bool bv_m_flag = bvect_min.is_bit_true(i) != 0; 
851
 
        bool bv_f_flag = bvect_full.get_bit(i) != 0;
852
 
 
853
 
        if (bv_m_flag != bv_f_flag)
854
 
        {
855
 
            printf("Bit %u is non conformant. vect_min=%i vect_full=%i\n",
856
 
                i, (int)bv_m_flag, (int)bv_f_flag);
857
 
 
858
 
            cout << "Non-conformant block number is: " << unsigned(i >>  bm::set_block_shift) << endl;
859
 
//            throw 110;
860
 
            exit(1);
861
 
        }
862
 
 
863
 
        if (PROGRESS_PRINT)
864
 
        {
865
 
            if ( (i % PROGRESS_PRINT) == 0)
866
 
            {
867
 
                printf(".");
868
 
            }
869
 
        }
870
 
             
871
 
    }
872
 
    
873
 
    printf("\n detailed check ok.\n");
874
 
 
875
 
}
876
 
 
877
 
 
878
 
// vectors comparison check
879
 
 
880
 
void CheckVectors(bvect_mini &bvect_min, 
881
 
                  bvect      &bvect_full,
882
 
                  unsigned size,
883
 
                  bool     detailed)
884
 
{
885
 
    cout << "\nVectors checking...bits to compare = " << size << endl;
886
 
 
887
 
    cout << "Bitcount summary : " << endl;
888
 
    unsigned min_count = bvect_min.bit_count();
889
 
    cout << "minvector count = " << min_count << endl;
890
 
    unsigned count = bvect_full.count();
891
 
    unsigned full_count = bvect_full.recalc_count();
892
 
    cout << "fullvector re-count = " << full_count << endl;
893
 
    
894
 
    if (min_count != full_count)
895
 
    {
896
 
        cout << "fullvector count = " << count << endl;
897
 
        cout << "Count comparison failed !!!!" << endl;
898
 
        bvect_full.stat();
899
 
        DetailedCheckVectors(bvect_min, bvect_full, size);
900
 
 
901
 
        exit(1);  
902
 
    } 
903
 
 
904
 
    if (full_count)
905
 
    {
906
 
        bool any = bvect_full.any();
907
 
        if (!any)
908
 
        {
909
 
            cout << "Anycheck failed!" << endl;
910
 
            exit(1);
911
 
        }
912
 
    }
913
 
 
914
 
    // get_next comparison
915
 
 
916
 
    cout << "Positive bits comparison..." << flush;
917
 
    unsigned nb_min = bvect_min.get_first();
918
 
    unsigned nb_ful = bvect_full.get_first();
919
 
 
920
 
    bvect::counted_enumerator en = bvect_full.first();
921
 
    unsigned nb_en = *en;
922
 
    if (nb_min != nb_ful)
923
 
    {
924
 
         cout << "!!!! First bit comparison failed. Full id = " 
925
 
              << nb_ful << " Min id = " << nb_min 
926
 
              << endl;
927
 
 
928
 
         bool bit_f = bvect_full.get_bit(nb_ful);
929
 
         cout << "Full vector'd bit #" << nb_ful << "is:" 
930
 
              << bit_f << endl;
931
 
 
932
 
         bool bit_m = (bvect_min.is_bit_true(nb_min) == 1);
933
 
         cout << "Min vector'd bit #" << nb_min << "is:" 
934
 
              << bit_m << endl;
935
 
 
936
 
 
937
 
         bvect_full.stat();
938
 
 
939
 
         DetailedCheckVectors(bvect_min, bvect_full, size);
940
 
 
941
 
         exit(1);
942
 
    }
943
 
 
944
 
    if (full_count)
945
 
    {
946
 
       unsigned bit_count = 1;
947
 
       unsigned en_prev = nb_en;
948
 
 
949
 
       do
950
 
       {
951
 
           nb_min = bvect_min.get_next(nb_min);
952
 
           if (nb_min == 0)
953
 
           {
954
 
               break;
955
 
           }
956
 
 
957
 
           en_prev = nb_en;
958
 
           ++en;
959
 
 
960
 
           nb_en = *en;
961
 
//           nb_en = bvect_full.get_next(nb_en);
962
 
 
963
 
           ++bit_count;
964
 
 
965
 
           if (nb_en != nb_min)
966
 
           {
967
 
               nb_ful = bvect_full.get_next(en_prev);
968
 
               cout << "!!!!! next bit comparison failed. Full id = " 
969
 
                    << nb_ful << " Min id = " << nb_min 
970
 
                    << " Enumerator = " << nb_en
971
 
                    << endl;
972
 
 
973
 
     //          bvect_full.stat();
974
 
 
975
 
     //          DetailedCheckVectors(bvect_min, bvect_full, size);
976
 
 
977
 
               exit(1);
978
 
           }
979
 
            if ( (bit_count % PROGRESS_PRINT) == 0)
980
 
           {
981
 
                cout << "." << flush;
982
 
            }
983
 
 
984
 
       } while (en.valid());
985
 
       if (bit_count != min_count)
986
 
       {
987
 
           cout << " Bit count failed."  
988
 
                << " min = " << min_count 
989
 
                << " bit = " << bit_count 
990
 
                << endl;
991
 
           exit(1);
992
 
       }
993
 
    }
994
 
 
995
 
    cout << "OK" << endl;
996
 
 
997
 
    return;
998
 
    
999
 
 
1000
 
}
1001
 
 
1002
 
 
1003
 
void ClearAllTest()
1004
 
{
1005
 
    bvect     bvect_full;
1006
 
 
1007
 
    for (int i = 0; i < 100000; ++i)
1008
 
    {
1009
 
        bvect_full.set_bit(i);
1010
 
    }
1011
 
    bvect_full.optimize();
1012
 
    bvect_full.clear();
1013
 
 
1014
 
    bvect_full.stat();
1015
 
 
1016
 
    int count = bvect_full.count();
1017
 
    assert(count == 0);
1018
 
    bvect_full.stat();
1019
 
}
1020
 
 
1021
 
 
1022
 
void WordCmpTest()
1023
 
{
1024
 
    cout << "---------------------------- WordCmp test" << endl;
1025
 
 
1026
 
    for (int i = 0; i < 10000000; ++i)
1027
 
    {
1028
 
        unsigned w1 = rand();
1029
 
        unsigned w2 = rand();
1030
 
        int res = wordcmp0(w1, w2);
1031
 
        int res2 = wordcmp(w1, w2);
1032
 
        if (res != res2)
1033
 
        {
1034
 
            printf("WordCmp failed !\n");
1035
 
            exit(1);
1036
 
        }
1037
 
 
1038
 
        res = wordcmp0((unsigned)0U, (unsigned)w2);
1039
 
        res2 = wordcmp((unsigned)0U, (unsigned)w2);
1040
 
 
1041
 
        if (res != res2)
1042
 
        {
1043
 
            printf("WordCmp 0 test failed !\n");
1044
 
            exit(1);
1045
 
        }
1046
 
 
1047
 
        res = wordcmp0((unsigned)~0U, (unsigned)w2);
1048
 
        res2 = wordcmp((unsigned)~0U, (unsigned)w2);
1049
 
 
1050
 
        if (res != res2)
1051
 
        {
1052
 
            printf("WordCmp ~0 test failed !\n");
1053
 
            exit(1);
1054
 
        }
1055
 
 
1056
 
        res = wordcmp0((unsigned)w2, (unsigned)0);
1057
 
        res2 = wordcmp((unsigned)w2, (unsigned)0);
1058
 
 
1059
 
        if (res != res2)
1060
 
        {
1061
 
            printf("WordCmp 0-2 test failed !\n");
1062
 
            exit(1);
1063
 
        }
1064
 
 
1065
 
    }
1066
 
 
1067
 
    cout << "Ok." << endl;
1068
 
}
1069
 
 
1070
 
void BasicFunctionalityTest()
1071
 
{
1072
 
    cout << "---------------------------- Basic functinality test" << endl;
1073
 
 
1074
 
    assert(ITERATIONS < BITVECT_SIZE);
1075
 
 
1076
 
 
1077
 
    bvect_mini     bvect_min(BITVECT_SIZE);
1078
 
    bvect          bvect_full;
1079
 
    bvect          bvect_full1;
1080
 
 
1081
 
    printf("\nBasic functionality test.\n");
1082
 
    
1083
 
    // filling vectors with regular values
1084
 
 
1085
 
    unsigned i;
1086
 
    for (i = 0; i < ITERATIONS; ++i)
1087
 
    {
1088
 
        bvect_min.set_bit(i);
1089
 
        bvect_full.set_bit(i);
1090
 
    }
1091
 
    
1092
 
    bvect_full1.set_range(0, ITERATIONS-1);
1093
 
    
1094
 
    CheckCountRange(bvect_full, 0, ITERATIONS);
1095
 
    CheckCountRange(bvect_full, 10, ITERATIONS+10);
1096
 
 
1097
 
    if (bvect_full1 != bvect_full)
1098
 
    {
1099
 
        cout << "set_range failed!" << endl;
1100
 
        bvect_full1.stat();
1101
 
        exit(1);
1102
 
    }
1103
 
 
1104
 
    bvect_full.stat();
1105
 
    bvect_full1.stat();
1106
 
 
1107
 
    // checking the results
1108
 
    unsigned count_min = 0;
1109
 
    for (i = 0; i < ITERATIONS; ++i)
1110
 
    {
1111
 
        if (bvect_min.is_bit_true(i))
1112
 
            ++count_min;
1113
 
    }
1114
 
 
1115
 
 
1116
 
    
1117
 
    unsigned count_full = bvect_full.count();
1118
 
 
1119
 
    if (count_min == count_full)
1120
 
    {
1121
 
        printf("simple count test ok.\n");
1122
 
    }
1123
 
    else
1124
 
    {
1125
 
        printf("simple count test failed count_min = %i  count_full = %i\n", 
1126
 
               count_min, count_full);
1127
 
        exit(1);
1128
 
    }
1129
 
 
1130
 
 
1131
 
    // detailed vectors verification
1132
 
 
1133
 
    CheckVectors(bvect_min, bvect_full, ITERATIONS);
1134
 
 
1135
 
    // now clearning
1136
 
 
1137
 
    for (i = 0; i < ITERATIONS; i+=2)
1138
 
    {
1139
 
        bvect_min.clear_bit(i);
1140
 
        bvect_full.clear_bit(i);
1141
 
        bvect_full1.set_range(i, i, false);
1142
 
    }
1143
 
 
1144
 
    CheckVectors(bvect_min, bvect_full, ITERATIONS);
1145
 
    CheckVectors(bvect_min, bvect_full1, ITERATIONS);
1146
 
 
1147
 
    for (i = 0; i < ITERATIONS; ++i)
1148
 
    {
1149
 
        bvect_min.clear_bit(i);
1150
 
    }
1151
 
    bvect_full.clear();
1152
 
 
1153
 
    CheckVectors(bvect_min, bvect_full, ITERATIONS);
1154
 
 
1155
 
    cout << "Random step filling" << endl;
1156
 
 
1157
 
    for (i = rand()%10; i < ITERATIONS; i+=rand()%10)
1158
 
    {
1159
 
        bvect_min.clear_bit(i);
1160
 
        bvect_full.clear_bit(i);
1161
 
    }
1162
 
    
1163
 
    CheckVectors(bvect_min, bvect_full, ITERATIONS);
1164
 
 
1165
 
    bvect bv1;
1166
 
    bvect bv2;
1167
 
 
1168
 
    bv1[10] = true;
1169
 
    bv1[1000] = true;
1170
 
 
1171
 
    bv2[200] = bv2[700] = bv2[500] = true;
1172
 
 
1173
 
    bv1.swap(bv2);
1174
 
 
1175
 
    if (bv1.count() != 3)
1176
 
    {
1177
 
        cout << "Swap test failed!" << endl;
1178
 
        exit(1);
1179
 
    }
1180
 
 
1181
 
    if (bv2.count() != 2)
1182
 
    {
1183
 
        cout << "Swap test failed!" << endl;
1184
 
        exit(1);
1185
 
    }
1186
 
}
1187
 
 
1188
 
void SimpleRandomFillTest()
1189
 
{
1190
 
    assert(ITERATIONS < BITVECT_SIZE);
1191
 
 
1192
 
    cout << "-------------------------- SimpleRandomFillTest" << endl;
1193
 
 
1194
 
    {
1195
 
    printf("Simple random fill test 1.");
1196
 
    bvect_mini   bvect_min(BITVECT_SIZE);
1197
 
    bvect      bvect_full;
1198
 
    bvect_full.set_new_blocks_strat(bm::BM_BIT);
1199
 
 
1200
 
 
1201
 
    unsigned iter = ITERATIONS / 5;
1202
 
 
1203
 
    printf("\nSimple Random fill test ITERATIONS = %i\n", iter);
1204
 
 
1205
 
    bvect_min.set_bit(0);
1206
 
    bvect_full.set_bit(0);
1207
 
 
1208
 
    unsigned i;
1209
 
    for (i = 0; i < iter; ++i)
1210
 
    {
1211
 
        unsigned num = ::rand() % iter;
1212
 
        bvect_min.set_bit(num);
1213
 
        bvect_full.set_bit(num);
1214
 
        if ((i % 1000) == 0) cout << "." << flush;
1215
 
        CheckCountRange(bvect_full, 0, num);
1216
 
        CheckCountRange(bvect_full, num, num+iter);
1217
 
    }
1218
 
 
1219
 
    CheckVectors(bvect_min, bvect_full, iter);
1220
 
    CheckCountRange(bvect_full, 0, iter);
1221
 
 
1222
 
    printf("Simple random fill test 2.");
1223
 
 
1224
 
    for(i = 0; i < iter; ++i)
1225
 
    {
1226
 
        unsigned num = ::rand() % iter;
1227
 
        bvect_min.clear_bit(num);
1228
 
        bvect_full.clear_bit(num);
1229
 
    }
1230
 
 
1231
 
    CheckVectors(bvect_min, bvect_full, iter);
1232
 
    }
1233
 
 
1234
 
 
1235
 
    {
1236
 
    printf("\nSimple random fill test 3.\n");
1237
 
    bvect_mini   bvect_min(BITVECT_SIZE);
1238
 
    bvect      bvect_full(bm::BM_GAP);
1239
 
 
1240
 
 
1241
 
    unsigned iter = ITERATIONS;
1242
 
 
1243
 
    printf("\nSimple Random fill test ITERATIONS = %i\n", iter);
1244
 
 
1245
 
    unsigned i;
1246
 
    for(i = 0; i < iter; ++i)
1247
 
    {
1248
 
        unsigned num = ::rand() % iter;
1249
 
        bvect_min.set_bit(num);
1250
 
        bvect_full.set_bit(num);
1251
 
        CheckCountRange(bvect_full, 0, 65535);
1252
 
        CheckCountRange(bvect_full, 0, num);
1253
 
        CheckCountRange(bvect_full, num, num+iter);
1254
 
        if ((i % 1000) == 0) cout << "." << flush;
1255
 
    }
1256
 
 
1257
 
    CheckVectors(bvect_min, bvect_full, iter);
1258
 
 
1259
 
    printf("Simple random fill test 4.");
1260
 
 
1261
 
    for(i = 0; i < iter; ++i)
1262
 
    {
1263
 
        unsigned num = ::rand() % iter;
1264
 
        bvect_min.clear_bit(num);
1265
 
        bvect_full.clear_bit(num);
1266
 
        CheckCountRange(bvect_full, 0, num);
1267
 
        CheckCountRange(bvect_full, num, num+iter);
1268
 
        if ((i % 1000) == 0) cout << "." << flush;
1269
 
    }
1270
 
 
1271
 
    CheckVectors(bvect_min, bvect_full, iter);
1272
 
    CheckCountRange(bvect_full, 0, iter);
1273
 
    }
1274
 
 
1275
 
 
1276
 
}
1277
 
 
1278
 
 
1279
 
 
1280
 
 
1281
 
void RangeRandomFillTest()
1282
 
{
1283
 
    assert(ITERATIONS < BITVECT_SIZE);
1284
 
 
1285
 
    cout << "----------------------------------- RangeRandomFillTest" << endl;
1286
 
 
1287
 
    {
1288
 
    bvect_mini   bvect_min(BITVECT_SIZE);
1289
 
    bvect     bvect_full;
1290
 
 
1291
 
    printf("Range Random fill test\n");
1292
 
 
1293
 
    unsigned min = BITVECT_SIZE / 2;
1294
 
    unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
1295
 
    if (max > BITVECT_SIZE) 
1296
 
        max = BITVECT_SIZE - 1;
1297
 
 
1298
 
    FillSets(&bvect_min, &bvect_full, min, max, 0);
1299
 
 
1300
 
    CheckVectors(bvect_min, bvect_full, BITVECT_SIZE);
1301
 
    CheckCountRange(bvect_full, min, max);
1302
 
 
1303
 
    }
1304
 
 
1305
 
    
1306
 
    {
1307
 
    bvect_mini   bvect_min(BITVECT_SIZE);
1308
 
    bvect     bvect_full;
1309
 
 
1310
 
    printf("Range Random fill test\n");
1311
 
 
1312
 
    unsigned min = BITVECT_SIZE / 2;
1313
 
    unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
1314
 
    if (max > BITVECT_SIZE) 
1315
 
        max = BITVECT_SIZE - 1;
1316
 
 
1317
 
    FillSetsIntervals(&bvect_min, &bvect_full, min, max, 4);
1318
 
 
1319
 
    CheckVectors(bvect_min, bvect_full, BITVECT_SIZE);
1320
 
    CheckCountRange(bvect_full, min, max);
1321
 
    }
1322
 
    
1323
 
 
1324
 
}
1325
 
 
1326
 
void AndOperationsTest()
1327
 
{
1328
 
    assert(ITERATIONS < BITVECT_SIZE);
1329
 
 
1330
 
    cout << "----------------------------------- AndOperationTest" << endl;
1331
 
 
1332
 
    {
1333
 
 
1334
 
    bvect_mini   bvect_min1(256);
1335
 
    bvect_mini   bvect_min2(256);
1336
 
    bvect        bvect_full1;
1337
 
    bvect        bvect_full2;
1338
 
 
1339
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
1340
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
1341
 
 
1342
 
 
1343
 
 
1344
 
    printf("AND test\n");
1345
 
 
1346
 
    bvect_min1.set_bit(1);
1347
 
    bvect_min1.set_bit(12);
1348
 
    bvect_min1.set_bit(13);
1349
 
 
1350
 
    bvect_min2.set_bit(12);
1351
 
    bvect_min2.set_bit(13);
1352
 
 
1353
 
    bvect_min1.combine_and(bvect_min2);
1354
 
 
1355
 
    bvect_full1.set_bit(1);
1356
 
    bvect_full1.set_bit(12);
1357
 
    bvect_full1.set_bit(13);
1358
 
 
1359
 
    bvect_full2.set_bit(12);
1360
 
    bvect_full2.set_bit(13);
1361
 
 
1362
 
    bm::id_t predicted_count = bm::count_and(bvect_full1, bvect_full2);
1363
 
 
1364
 
    bvect_full1.bit_and(bvect_full2);
1365
 
 
1366
 
    bm::id_t count = bvect_full1.count();
1367
 
    if (count != predicted_count)
1368
 
    {
1369
 
        cout << "Predicted count error!" << endl;
1370
 
        exit(1);
1371
 
    }
1372
 
 
1373
 
    CheckVectors(bvect_min1, bvect_full1, 256);
1374
 
    CheckCountRange(bvect_full1, 0, 256);
1375
 
 
1376
 
    }
1377
 
 
1378
 
    {
1379
 
 
1380
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
1381
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
1382
 
    bvect        bvect_full1;
1383
 
    bvect        bvect_full2;
1384
 
 
1385
 
 
1386
 
    printf("AND test stage 1.\n");
1387
 
 
1388
 
    for (int i = 0; i < 112; ++i)
1389
 
    {
1390
 
        bvect_min1.set_bit(i);
1391
 
        bvect_full1.set_bit(i);
1392
 
 
1393
 
        bvect_min2.set_bit(i);
1394
 
        bvect_full2.set_bit(i);
1395
 
 
1396
 
    }
1397
 
 
1398
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
1399
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
1400
 
 
1401
 
//    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
1402
 
//    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
1403
 
 
1404
 
    bvect_min1.combine_and(bvect_min2);
1405
 
 
1406
 
    bm::id_t predicted_count = bm::count_and(bvect_full1,bvect_full2);
1407
 
 
1408
 
    bvect_full1.bit_and(bvect_full2);
1409
 
 
1410
 
    bm::id_t count = bvect_full1.count();
1411
 
    if (count != predicted_count)
1412
 
    {
1413
 
        cout << "Predicted count error!" << endl;
1414
 
        exit(1);
1415
 
    }
1416
 
 
1417
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
1418
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
1419
 
 
1420
 
    }
1421
 
 
1422
 
 
1423
 
    {
1424
 
 
1425
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
1426
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
1427
 
    bvect        bvect_full1;
1428
 
    bvect        bvect_full2;
1429
 
 
1430
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
1431
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
1432
 
 
1433
 
    printf("AND test stage 2.\n");
1434
 
 
1435
 
 
1436
 
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
1437
 
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
1438
 
 
1439
 
    bm::id_t predicted_count = bm::count_and(bvect_full1,bvect_full2);
1440
 
 
1441
 
    bvect_min1.combine_and(bvect_min2);
1442
 
 
1443
 
    bvect_full1.bit_and(bvect_full2);
1444
 
 
1445
 
    bm::id_t count = bvect_full1.count();
1446
 
    if (count != predicted_count)
1447
 
    {
1448
 
        cout << "Predicted count error!" << endl;
1449
 
        bvect_full1.stat();
1450
 
        exit(1);
1451
 
    }
1452
 
 
1453
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
1454
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
1455
 
 
1456
 
    }
1457
 
 
1458
 
    {
1459
 
 
1460
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
1461
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
1462
 
    bvect        bvect_full1;
1463
 
    bvect        bvect_full2;
1464
 
 
1465
 
    bvect_full1.set_new_blocks_strat(bm::BM_BIT);
1466
 
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
1467
 
 
1468
 
    cout << "------------------------------" << endl;
1469
 
    printf("AND test stage 3.\n");
1470
 
 
1471
 
 
1472
 
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
1473
 
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
1474
 
 
1475
 
    bvect_min1.combine_and(bvect_min2);
1476
 
 
1477
 
    bm::id_t predicted_count = bm::count_and(bvect_full1, bvect_full2);
1478
 
 
1479
 
    bvect_full1.bit_and(bvect_full2);
1480
 
 
1481
 
    bm::id_t count = bvect_full1.count();
1482
 
    if (count != predicted_count)
1483
 
    {
1484
 
        cout << "Predicted count error!" << endl;
1485
 
        exit(1);
1486
 
    }
1487
 
 
1488
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
1489
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
1490
 
 
1491
 
    bvect_full1.optimize();
1492
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
1493
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
1494
 
    CheckCountRange(bvect_full1, BITVECT_SIZE/2, BITVECT_SIZE);
1495
 
 
1496
 
 
1497
 
    }
1498
 
 
1499
 
 
1500
 
}
1501
 
 
1502
 
 
1503
 
void OrOperationsTest()
1504
 
{
1505
 
    assert(ITERATIONS < BITVECT_SIZE);
1506
 
 
1507
 
    cout << "----------------------------------- OrOperationTest" << endl;
1508
 
 
1509
 
    {
1510
 
 
1511
 
    bvect_mini   bvect_min1(256);
1512
 
    bvect_mini   bvect_min2(256);
1513
 
    bvect        bvect_full1;
1514
 
    bvect        bvect_full2;
1515
 
 
1516
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
1517
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
1518
 
 
1519
 
 
1520
 
 
1521
 
    printf("OR test\n");
1522
 
 
1523
 
    bvect_min1.set_bit(1);
1524
 
    bvect_min1.set_bit(12);
1525
 
    bvect_min1.set_bit(13);
1526
 
 
1527
 
    bvect_min2.set_bit(12);
1528
 
    bvect_min2.set_bit(13);
1529
 
 
1530
 
    bvect_min1.combine_or(bvect_min2);
1531
 
 
1532
 
    bvect_full1.set_bit(1);
1533
 
    bvect_full1.set_bit(12);
1534
 
    bvect_full1.set_bit(13);
1535
 
 
1536
 
    bvect_full2.set_bit(12);
1537
 
    bvect_full2.set_bit(13);
1538
 
    
1539
 
    bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);    
1540
 
 
1541
 
    bvect_full1.bit_or(bvect_full2);
1542
 
 
1543
 
    bm::id_t count = bvect_full1.count();
1544
 
    if (count != predicted_count)
1545
 
    {
1546
 
        cout << "Predicted count error!" << endl;
1547
 
        cout << predicted_count << " " << count << endl;
1548
 
        bvect_full1.stat();
1549
 
        exit(1);
1550
 
    }
1551
 
 
1552
 
 
1553
 
    CheckVectors(bvect_min1, bvect_full1, 256);
1554
 
    CheckCountRange(bvect_full1, 0, 256);
1555
 
    CheckCountRange(bvect_full1, 128, 256);
1556
 
    }
1557
 
 
1558
 
    {
1559
 
 
1560
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
1561
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
1562
 
    bvect        bvect_full1;
1563
 
    bvect        bvect_full2;
1564
 
 
1565
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
1566
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
1567
 
 
1568
 
    printf("OR test stage 2.\n");
1569
 
 
1570
 
 
1571
 
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
1572
 
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
1573
 
 
1574
 
    bvect_min1.combine_or(bvect_min2);
1575
 
 
1576
 
    bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);    
1577
 
 
1578
 
    bvect_full1.bit_or(bvect_full2);
1579
 
 
1580
 
    bm::id_t count = bvect_full1.count();
1581
 
    if (count != predicted_count)
1582
 
    {
1583
 
        cout << "Predicted count error!" << endl;
1584
 
        exit(1);
1585
 
    }
1586
 
 
1587
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
1588
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
1589
 
 
1590
 
    }
1591
 
 
1592
 
    {
1593
 
 
1594
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
1595
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
1596
 
    bvect        bvect_full1;
1597
 
    bvect        bvect_full2;
1598
 
 
1599
 
    bvect_full1.set_new_blocks_strat(bm::BM_BIT);
1600
 
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
1601
 
 
1602
 
    cout << "------------------------------" << endl;
1603
 
    printf("OR test stage 3.\n");
1604
 
 
1605
 
 
1606
 
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
1607
 
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
1608
 
 
1609
 
    bvect_min1.combine_or(bvect_min2);
1610
 
    
1611
 
    bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);    
1612
 
 
1613
 
    bvect_full1.bit_or(bvect_full2);
1614
 
 
1615
 
    bm::id_t count = bvect_full1.count();
1616
 
    if (count != predicted_count)
1617
 
    {
1618
 
        cout << "Predicted count error!" << endl;
1619
 
        exit(1);
1620
 
    }
1621
 
 
1622
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
1623
 
 
1624
 
    bvect_full1.optimize();
1625
 
 
1626
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
1627
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
1628
 
 
1629
 
 
1630
 
    }
1631
 
    
1632
 
    cout << "Testing combine_or" << endl;
1633
 
    
1634
 
    {
1635
 
    
1636
 
    bvect        bvect_full1;
1637
 
    bvect        bvect_full2;
1638
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
1639
 
    
1640
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
1641
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
1642
 
 
1643
 
    unsigned ids[10000];
1644
 
    unsigned to_add = 10000;
1645
 
    
1646
 
    unsigned bn = 0;
1647
 
    for (unsigned i = 0; i < to_add; ++i)
1648
 
    {
1649
 
        ids[i] = bn;
1650
 
        bvect_full2.set(bn);
1651
 
        bvect_min1.set_bit(bn);
1652
 
        bn += 15;
1653
 
    }
1654
 
    
1655
 
    unsigned* first = ids;
1656
 
    unsigned* last = ids + to_add;
1657
 
    
1658
 
    bm::combine_or(bvect_full1, first, last);
1659
 
 
1660
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
1661
 
    
1662
 
    bm::combine_or(bvect_full1, first, last);
1663
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
1664
 
    
1665
 
    }
1666
 
    
1667
 
    
1668
 
    {
1669
 
    unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
1670
 
    unsigned to_add = sizeof(ids)/sizeof(unsigned);
1671
 
    bvect        bvect_full1;
1672
 
    bvect        bvect_full2;    
1673
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
1674
 
 
1675
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
1676
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
1677
 
    
1678
 
    unsigned bn = 0;
1679
 
    for (unsigned i = 0; i < to_add; ++i)
1680
 
    {
1681
 
        ids[i] = bn;
1682
 
        bvect_full2.set(bn);
1683
 
        bvect_min1.set_bit(bn);
1684
 
        bn += 15;
1685
 
    }
1686
 
    
1687
 
    unsigned* first = ids;
1688
 
    unsigned* last = ids + to_add;
1689
 
    
1690
 
    bm::combine_or(bvect_full1, first, last);
1691
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
1692
 
 
1693
 
    bm::combine_or(bvect_full1, first, last);
1694
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);    
1695
 
    }
1696
 
    
1697
 
 
1698
 
}
1699
 
 
1700
 
 
1701
 
 
1702
 
void SubOperationsTest()
1703
 
{
1704
 
    assert(ITERATIONS < BITVECT_SIZE);
1705
 
 
1706
 
    cout << "----------------------------------- SubOperationTest" << endl;
1707
 
 
1708
 
    {
1709
 
 
1710
 
    bvect_mini   bvect_min1(256);
1711
 
    bvect_mini   bvect_min2(256);
1712
 
    bvect        bvect_full1;
1713
 
    bvect        bvect_full2;
1714
 
 
1715
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
1716
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
1717
 
 
1718
 
 
1719
 
 
1720
 
    printf("SUB test\n");
1721
 
 
1722
 
    bvect_min1.set_bit(1);
1723
 
    bvect_min1.set_bit(12);
1724
 
    bvect_min1.set_bit(13);
1725
 
 
1726
 
    bvect_min2.set_bit(12);
1727
 
    bvect_min2.set_bit(13);
1728
 
 
1729
 
    bvect_min1.combine_sub(bvect_min2);
1730
 
 
1731
 
    bvect_full1.set_bit(1);
1732
 
    bvect_full1.set_bit(12);
1733
 
    bvect_full1.set_bit(13);
1734
 
 
1735
 
    bvect_full2.set_bit(12);
1736
 
    bvect_full2.set_bit(13);
1737
 
 
1738
 
    bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
1739
 
 
1740
 
    bvect_full1.bit_sub(bvect_full2);
1741
 
    
1742
 
    bm::id_t count = bvect_full1.count();
1743
 
    if (count != predicted_count)
1744
 
    {
1745
 
        cout << "Predicted count error!" << endl;
1746
 
        exit(1);
1747
 
    }
1748
 
 
1749
 
    CheckVectors(bvect_min1, bvect_full1, 256);
1750
 
    CheckCountRange(bvect_full1, 0, 256);
1751
 
 
1752
 
    }
1753
 
 
1754
 
    {
1755
 
 
1756
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
1757
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
1758
 
    bvect        bvect_full1;
1759
 
    bvect        bvect_full2;
1760
 
 
1761
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
1762
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
1763
 
 
1764
 
    printf("SUB test stage 2.\n");
1765
 
 
1766
 
 
1767
 
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
1768
 
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
1769
 
 
1770
 
    bvect_min1.combine_sub(bvect_min2);
1771
 
 
1772
 
    bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
1773
 
 
1774
 
    bvect_full1.bit_sub(bvect_full2);
1775
 
    
1776
 
    bm::id_t count = bvect_full1.count();
1777
 
    if (count != predicted_count)
1778
 
    {
1779
 
        cout << "Predicted count error!" << endl;
1780
 
        cout << predicted_count << " " << count << endl;
1781
 
bvect_full1.stat();    
1782
 
        
1783
 
        exit(1);
1784
 
    }
1785
 
    
1786
 
 
1787
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
1788
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
1789
 
 
1790
 
    }
1791
 
 
1792
 
    {
1793
 
 
1794
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
1795
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
1796
 
    bvect        bvect_full1;
1797
 
    bvect        bvect_full2;
1798
 
 
1799
 
    bvect_full1.set_new_blocks_strat(bm::BM_BIT);
1800
 
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
1801
 
 
1802
 
    cout << "------------------------------" << endl;
1803
 
    printf("SUB test stage 3.\n");
1804
 
 
1805
 
 
1806
 
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
1807
 
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
1808
 
 
1809
 
    bvect_min1.combine_sub(bvect_min2);
1810
 
    
1811
 
    bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
1812
 
 
1813
 
    bvect_full1.bit_sub(bvect_full2);
1814
 
 
1815
 
    bm::id_t count = bvect_full1.count();
1816
 
    if (count != predicted_count)
1817
 
    {
1818
 
        cout << "Predicted count error!" << endl;
1819
 
        exit(1);
1820
 
    }
1821
 
 
1822
 
 
1823
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
1824
 
 
1825
 
    bvect_full1.optimize();
1826
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
1827
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
1828
 
 
1829
 
 
1830
 
    }
1831
 
 
1832
 
}
1833
 
 
1834
 
 
1835
 
 
1836
 
void XorOperationsTest()
1837
 
{
1838
 
    assert(ITERATIONS < BITVECT_SIZE);
1839
 
 
1840
 
    cout << "----------------------------------- XorOperationTest" << endl;
1841
 
 
1842
 
    {
1843
 
 
1844
 
    bvect_mini   bvect_min1(256);
1845
 
    bvect_mini   bvect_min2(256);
1846
 
    bvect        bvect_full1;
1847
 
    bvect        bvect_full2;
1848
 
 
1849
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
1850
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
1851
 
 
1852
 
 
1853
 
 
1854
 
    printf("XOR test\n");
1855
 
 
1856
 
    bvect_min1.set_bit(1);
1857
 
    bvect_min1.set_bit(12);
1858
 
    bvect_min1.set_bit(13);
1859
 
 
1860
 
    bvect_min2.set_bit(12);
1861
 
    bvect_min2.set_bit(13);
1862
 
 
1863
 
    bvect_min1.combine_xor(bvect_min2);
1864
 
 
1865
 
    bvect_full1.set_bit(1);
1866
 
    bvect_full1.set_bit(12);
1867
 
    bvect_full1.set_bit(13);
1868
 
 
1869
 
    bvect_full2.set_bit(12);
1870
 
    bvect_full2.set_bit(13);
1871
 
 
1872
 
    bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
1873
 
 
1874
 
    bvect_full1.bit_xor(bvect_full2);
1875
 
 
1876
 
    bm::id_t count = bvect_full1.count();
1877
 
    if (count != predicted_count)
1878
 
    {
1879
 
        cout << "1.Predicted count error!" << endl;
1880
 
        exit(1);
1881
 
    }
1882
 
 
1883
 
    CheckVectors(bvect_min1, bvect_full1, 256);
1884
 
    CheckCountRange(bvect_full1, 0, 256);
1885
 
    CheckCountRange(bvect_full1, 128, 256);
1886
 
 
1887
 
    }
1888
 
 
1889
 
    {
1890
 
        bvect  bvect1;
1891
 
        bvect_mini  bvect_min1(BITVECT_SIZE);
1892
 
 
1893
 
        bvect  bvect2;
1894
 
        bvect_mini  bvect_min2(BITVECT_SIZE);
1895
 
 
1896
 
 
1897
 
        for (int i = 0; i < 150000; ++i)
1898
 
        {
1899
 
            bvect2.set_bit(i);
1900
 
            bvect_min2.set_bit(i);
1901
 
        }
1902
 
 
1903
 
        bvect2.optimize();
1904
 
 
1905
 
        bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
1906
 
 
1907
 
        bvect1.bit_xor(bvect2);
1908
 
        
1909
 
        bm::id_t count = bvect1.count();
1910
 
        if (count != predicted_count)
1911
 
        {
1912
 
            cout << "2.Predicted count error!" << endl;
1913
 
            exit(1);
1914
 
        }
1915
 
        
1916
 
        bvect_min1.combine_xor(bvect_min2);
1917
 
        CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, true);
1918
 
        CheckCountRange(bvect1, 0, BITVECT_SIZE);
1919
 
    }
1920
 
 
1921
 
 
1922
 
    {
1923
 
        bvect  bvect1;
1924
 
        bvect_mini  bvect_min1(BITVECT_SIZE);
1925
 
 
1926
 
        bvect  bvect2;
1927
 
        bvect_mini  bvect_min2(BITVECT_SIZE);
1928
 
 
1929
 
 
1930
 
        for (int i = 0; i < 150000; ++i)
1931
 
        {
1932
 
            bvect1.set_bit(i);
1933
 
            bvect_min1.set_bit(i);
1934
 
        }
1935
 
 
1936
 
        bvect1.optimize();
1937
 
        
1938
 
        bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
1939
 
 
1940
 
        bvect1.bit_xor(bvect2);
1941
 
 
1942
 
        bm::id_t count = bvect1.count();
1943
 
        if (count != predicted_count)
1944
 
        {
1945
 
            cout << "3.Predicted count error!" << endl;
1946
 
            exit(1);
1947
 
        }
1948
 
        
1949
 
        bvect_min1.combine_xor(bvect_min2);
1950
 
        CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, true);
1951
 
    }
1952
 
 
1953
 
 
1954
 
    {
1955
 
        bvect  bvect1;
1956
 
        bvect_mini  bvect_min1(BITVECT_SIZE);
1957
 
 
1958
 
        bvect  bvect2;
1959
 
        bvect_mini  bvect_min2(BITVECT_SIZE);
1960
 
 
1961
 
 
1962
 
        for (int i = 0; i < 150000; ++i)
1963
 
        {
1964
 
            bvect1.set_bit(i);
1965
 
            bvect_min1.set_bit(i);
1966
 
            bvect2.set_bit(i);
1967
 
            bvect_min2.set_bit(i);
1968
 
        }
1969
 
 
1970
 
        bvect1.optimize();
1971
 
        
1972
 
        bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
1973
 
 
1974
 
        bvect1.bit_xor(bvect2);
1975
 
 
1976
 
        bm::id_t count = bvect1.count();
1977
 
        if (count != predicted_count)
1978
 
        {
1979
 
            cout << "4.Predicted count error!" << endl;
1980
 
            cout << count << " " << predicted_count << endl;
1981
 
            
1982
 
            exit(1);
1983
 
        }
1984
 
        
1985
 
        bvect_min1.combine_xor(bvect_min2);
1986
 
        CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, true);
1987
 
    }
1988
 
 
1989
 
 
1990
 
 
1991
 
    {
1992
 
 
1993
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
1994
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
1995
 
    bvect        bvect_full1;
1996
 
    bvect        bvect_full2;
1997
 
 
1998
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
1999
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
2000
 
 
2001
 
    printf("XOR test stage 2.\n");
2002
 
 
2003
 
 
2004
 
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
2005
 
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
2006
 
 
2007
 
    bvect_min1.combine_xor(bvect_min2);
2008
 
    
2009
 
    bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
2010
 
    
2011
 
    bvect_full1.bit_xor(bvect_full2);
2012
 
    
2013
 
    bm::id_t count = bvect_full1.count();
2014
 
    if (count != predicted_count)
2015
 
    {
2016
 
        cout << "5.Predicted count error!" << endl;
2017
 
        cout << count << " " << predicted_count << endl;
2018
 
        bvect_full1.stat();
2019
 
        exit(1);
2020
 
    }
2021
 
 
2022
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
2023
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
2024
 
 
2025
 
    }
2026
 
 
2027
 
    {
2028
 
 
2029
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
2030
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
2031
 
    bvect        bvect_full1;
2032
 
    bvect        bvect_full2;
2033
 
 
2034
 
    bvect_full1.set_new_blocks_strat(bm::BM_BIT);
2035
 
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
2036
 
 
2037
 
    cout << "------------------------------" << endl;
2038
 
    printf("XOR test stage 3.\n");
2039
 
 
2040
 
 
2041
 
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
2042
 
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
2043
 
 
2044
 
    bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
2045
 
 
2046
 
    bvect_min1.combine_xor(bvect_min2);
2047
 
 
2048
 
    bvect_full1.bit_xor(bvect_full2);
2049
 
 
2050
 
    bm::id_t count = bvect_full1.count();
2051
 
    if (count != predicted_count)
2052
 
    {
2053
 
        cout << "6.Predicted count error!" << endl;
2054
 
        exit(1);
2055
 
    }
2056
 
 
2057
 
 
2058
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
2059
 
 
2060
 
    bvect_full1.optimize();
2061
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
2062
 
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
2063
 
 
2064
 
 
2065
 
    }
2066
 
 
2067
 
 
2068
 
    cout << "Testing combine_xor" << endl;
2069
 
    
2070
 
    {
2071
 
    
2072
 
    bvect        bvect_full1;
2073
 
    bvect        bvect_full2;
2074
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
2075
 
    
2076
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
2077
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
2078
 
 
2079
 
    unsigned ids[10000];
2080
 
    unsigned to_add = 10000;
2081
 
    
2082
 
    unsigned bn = 0;
2083
 
    for (unsigned i = 0; i < to_add; ++i)
2084
 
    {
2085
 
        ids[i] = bn;
2086
 
        bvect_full2.set(bn);
2087
 
        bvect_min1.set_bit(bn);
2088
 
        bn += 15;
2089
 
    }
2090
 
    
2091
 
    unsigned* first = ids;
2092
 
    unsigned* last = ids + to_add;
2093
 
    
2094
 
    bm::combine_xor(bvect_full1, first, last);
2095
 
 
2096
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
2097
 
    
2098
 
    bm::combine_xor(bvect_full1, first, last);
2099
 
    if (bvect_full1.count())
2100
 
    {
2101
 
        cout << "combine_xor count failed!" << endl;
2102
 
        exit(1);
2103
 
    }
2104
 
    
2105
 
    }
2106
 
 
2107
 
    {
2108
 
    
2109
 
    bvect        bvect_full1;
2110
 
    bvect        bvect_full2;
2111
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
2112
 
    
2113
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
2114
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
2115
 
 
2116
 
    unsigned ids[10000]={0,};
2117
 
    unsigned to_add = 10000;
2118
 
    
2119
 
    for (unsigned i = 0; i < to_add; i+=100)
2120
 
    {
2121
 
        ids[i] = i;
2122
 
        bvect_full2.set(i);
2123
 
        bvect_min1.set_bit(i);
2124
 
    }
2125
 
    unsigned* first = ids;
2126
 
    unsigned* last = ids + to_add;
2127
 
    
2128
 
    bm::combine_xor(bvect_full1, first, last);
2129
 
 
2130
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
2131
 
    
2132
 
    bm::combine_xor(bvect_full1, first, last);
2133
 
    if (bvect_full1.count())
2134
 
    {
2135
 
        cout << "combine_xor count failed!" << endl;
2136
 
        exit(1);
2137
 
    }
2138
 
    
2139
 
    }
2140
 
 
2141
 
    
2142
 
    {
2143
 
    unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
2144
 
    unsigned to_add = sizeof(ids)/sizeof(unsigned);
2145
 
    bvect        bvect_full1;
2146
 
    bvect        bvect_full2;    
2147
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
2148
 
 
2149
 
    bvect_full1.set_new_blocks_strat(bm::BM_BIT);
2150
 
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
2151
 
    
2152
 
    unsigned bn = 0;
2153
 
    for (unsigned i = 0; i < to_add; ++i)
2154
 
    {
2155
 
        ids[i] = bn;
2156
 
        bvect_full2.set(bn);
2157
 
        bvect_min1.set_bit(bn);
2158
 
        bn += 15;
2159
 
    }
2160
 
    
2161
 
    unsigned* first = ids;
2162
 
    unsigned* last = ids + to_add;
2163
 
    
2164
 
    bm::combine_xor(bvect_full1, first, last);
2165
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
2166
 
 
2167
 
    bm::combine_xor(bvect_full1, first, last);
2168
 
    if (bvect_full1.count())
2169
 
    {
2170
 
        cout << "combine_xor count failed!" << endl;
2171
 
        exit(1);
2172
 
    }
2173
 
    }
2174
 
    
2175
 
    
2176
 
    {
2177
 
    unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
2178
 
    unsigned to_add = sizeof(ids)/sizeof(unsigned);
2179
 
    bvect        bvect_full1;
2180
 
    bvect        bvect_full2;    
2181
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
2182
 
 
2183
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
2184
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
2185
 
    
2186
 
    unsigned bn = 0;
2187
 
    for (unsigned i = 0; i < to_add; ++i)
2188
 
    {
2189
 
        ids[i] = bn;
2190
 
        bvect_full2.set(bn);
2191
 
        bvect_min1.set_bit(bn);
2192
 
        bn += 15;
2193
 
    }
2194
 
    
2195
 
    unsigned* first = ids;
2196
 
    unsigned* last = ids + to_add;
2197
 
    
2198
 
    bm::combine_xor(bvect_full1, first, last);
2199
 
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
2200
 
 
2201
 
    bm::combine_xor(bvect_full1, first, last);
2202
 
    if (bvect_full1.count())
2203
 
    {
2204
 
        cout << "combine_xor count failed!" << endl;
2205
 
        exit(1);
2206
 
    }
2207
 
    }
2208
 
 
2209
 
}
2210
 
 
2211
 
 
2212
 
void ComparisonTest()
2213
 
{
2214
 
    cout << "-------------------------------------- ComparisonTest" << endl;
2215
 
 
2216
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
2217
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
2218
 
    bvect        bvect_full1;
2219
 
    bvect        bvect_full2;
2220
 
    int res1, res2;
2221
 
 
2222
 
    bvect_full1.set_bit(31); 
2223
 
    bvect_full2.set_bit(63); 
2224
 
 
2225
 
    res1 = bvect_full1.compare(bvect_full2);
2226
 
    if (res1 != 1)
2227
 
    {
2228
 
        printf("Comparison test failed 1\n");
2229
 
        exit(1);
2230
 
    }
2231
 
 
2232
 
    bvect_full1.clear();
2233
 
    bvect_full2.clear();
2234
 
 
2235
 
    bvect_min1.set_bit(10);
2236
 
    bvect_min2.set_bit(10);
2237
 
 
2238
 
    bvect_full1.set_bit(10);
2239
 
    bvect_full2.set_bit(10);
2240
 
 
2241
 
    res1 = bvect_min1.compare(bvect_min2);
2242
 
    res2 = bvect_full1.compare(bvect_full2);
2243
 
 
2244
 
    if (res1 != res2)
2245
 
    {
2246
 
        printf("Comparison test failed 1\n");
2247
 
        exit(1);
2248
 
    }
2249
 
 
2250
 
    printf("Comparison 2.\n");
2251
 
 
2252
 
    bvect_min1.set_bit(11);
2253
 
    bvect_full1.set_bit(11);
2254
 
 
2255
 
    res1 = bvect_min1.compare(bvect_min2);
2256
 
    res2 = bvect_full1.compare(bvect_full2);
2257
 
 
2258
 
    if (res1 != res2 && res1 != 1)
2259
 
    {
2260
 
        printf("Comparison test failed 2\n");
2261
 
        exit(1);
2262
 
    }
2263
 
 
2264
 
    res1 = bvect_min2.compare(bvect_min1);
2265
 
    res2 = bvect_full2.compare(bvect_full1);
2266
 
 
2267
 
    if (res1 != res2 && res1 != -1)
2268
 
    {
2269
 
        printf("Comparison test failed 2.1\n");
2270
 
        exit(1);
2271
 
    }
2272
 
 
2273
 
    printf("Comparison 3.\n");
2274
 
 
2275
 
    bvect_full1.optimize();
2276
 
 
2277
 
    res1 = bvect_min1.compare(bvect_min2);
2278
 
    res2 = bvect_full1.compare(bvect_full2);
2279
 
 
2280
 
    if (res1 != res2 && res1 != 1)
2281
 
    {
2282
 
        printf("Comparison test failed 3\n");
2283
 
        exit(1);
2284
 
    }
2285
 
 
2286
 
    res1 = bvect_min2.compare(bvect_min1);
2287
 
    res2 = bvect_full2.compare(bvect_full1);
2288
 
 
2289
 
    if (res1 != res2 && res1 != -1)
2290
 
    {
2291
 
        printf("Comparison test failed 3.1\n");
2292
 
        exit(1);
2293
 
    }
2294
 
 
2295
 
    printf("Comparison 4.\n");
2296
 
 
2297
 
    bvect_full2.optimize();
2298
 
 
2299
 
    res1 = bvect_min1.compare(bvect_min2);
2300
 
    res2 = bvect_full1.compare(bvect_full2);
2301
 
 
2302
 
    if (res1 != res2 && res1 != 1)
2303
 
    {
2304
 
        printf("Comparison test failed 4\n");
2305
 
        exit(1);
2306
 
    }
2307
 
 
2308
 
    res1 = bvect_min2.compare(bvect_min1);
2309
 
    res2 = bvect_full2.compare(bvect_full1);
2310
 
 
2311
 
    if (res1 != res2 && res1 != -1)
2312
 
    {
2313
 
        printf("Comparison test failed 4.1\n");
2314
 
        exit(1);
2315
 
    }
2316
 
 
2317
 
    printf("Comparison 5.\n");
2318
 
 
2319
 
    unsigned i;
2320
 
    for (i = 0; i < 65536; ++i)
2321
 
    {
2322
 
        bvect_full1.set_bit(i);
2323
 
    }
2324
 
 
2325
 
    res1 = bvect_min1.compare(bvect_min2);
2326
 
    res2 = bvect_full1.compare(bvect_full2);
2327
 
 
2328
 
    if (res1 != res2 && res1 != 1)
2329
 
    {
2330
 
        printf("Comparison test failed 5\n");
2331
 
        exit(1);
2332
 
    }
2333
 
 
2334
 
    bvect_full1.optimize();
2335
 
 
2336
 
    res1 = bvect_min2.compare(bvect_min1);
2337
 
    res2 = bvect_full2.compare(bvect_full1);
2338
 
 
2339
 
    if (res1 != res2 && res1 != -1)
2340
 
    {
2341
 
        printf("Comparison test failed 5.1\n");
2342
 
        exit(1);
2343
 
    }
2344
 
 
2345
 
}
2346
 
 
2347
 
void DesrializationTest2()
2348
 
{
2349
 
   bvect  bvtotal;
2350
 
   unsigned size = BITVECT_SIZE - 10;
2351
 
 
2352
 
 
2353
 
   bvect  bv1;
2354
 
   bvect  bv2;
2355
 
   int i;
2356
 
   for (i = 10; i < 165536; i+=2)
2357
 
   {
2358
 
      bv1.set_bit(i);
2359
 
   }
2360
 
 
2361
 
   bv1.optimize();
2362
 
   bv1.stat();
2363
 
 
2364
 
   struct bvect::statistics st1;
2365
 
   bv1.calc_stat(&st1);
2366
 
 
2367
 
 
2368
 
   unsigned char* sermem = new unsigned char[st1.max_serialize_mem];
2369
 
 
2370
 
   unsigned slen2 = bm::serialize(bv1, sermem);
2371
 
   assert(slen2);
2372
 
   slen2 = 0;
2373
 
 
2374
 
   bm::deserialize(bvtotal, sermem);
2375
 
   bvtotal.optimize();
2376
 
 
2377
 
   for (i = 55000; i < 165536; ++i)
2378
 
   {
2379
 
      bv2.set_bit(i);
2380
 
   }
2381
 
   bv2.optimize();
2382
 
   bv2.stat();
2383
 
 
2384
 
   struct bvect::statistics st2;
2385
 
   bv2.calc_stat(&st2);
2386
 
 
2387
 
   unsigned char* sermem2 = new unsigned char[st2.max_serialize_mem];
2388
 
 
2389
 
   unsigned slen = bm::serialize(bv2, sermem2);
2390
 
   assert(slen);
2391
 
   slen = 0;
2392
 
 
2393
 
   bm::deserialize(bvtotal, sermem2);
2394
 
   bvtotal.stat();
2395
 
 
2396
 
//   bvtotal.optimize();
2397
 
 //  bvtotal.stat();
2398
 
 
2399
 
   bm::deserialize(bvtotal, sermem2);
2400
 
 
2401
 
   bm::deserialize(bvtotal, sermem);
2402
 
 
2403
 
   delete [] sermem;
2404
 
   delete [] sermem2;
2405
 
 
2406
 
 
2407
 
   bvtotal.clear();
2408
 
 
2409
 
   int clcnt = 0;
2410
 
 
2411
 
   int repetitions = 25;
2412
 
   for (i = 0; i < repetitions; ++i)
2413
 
   {
2414
 
        cout << endl << "Deserialization STEP " << i << endl;
2415
 
 
2416
 
        bvect_mini*   bvect_min1= new bvect_mini(size);
2417
 
        bvect*        bvect_full1= new bvect();
2418
 
 
2419
 
        FillSetsRandomMethod(bvect_min1, bvect_full1, 1, size, 1);
2420
 
 
2421
 
       struct bvect::statistics st;
2422
 
       bvect_full1->calc_stat(&st);
2423
 
 
2424
 
       unsigned char* sermem = new unsigned char[st.max_serialize_mem];
2425
 
 
2426
 
       unsigned slen = bm::serialize(*bvect_full1, sermem);
2427
 
 
2428
 
       unsigned char* smem = new unsigned char[slen];
2429
 
       ::memcpy(smem, sermem, slen);
2430
 
 
2431
 
//       cout << "Serialized vector" << endl;
2432
 
//       bvect_full1->stat();
2433
 
 
2434
 
//       cout << "Before deserialization" << endl;
2435
 
//       bvtotal.stat();
2436
 
       bm::deserialize(bvtotal, smem);
2437
 
 
2438
 
//       cout << "After deserialization" << endl;
2439
 
//       bvtotal.stat();
2440
 
 
2441
 
       bvtotal.optimize();
2442
 
 
2443
 
//       cout << "After optimization" << endl;
2444
 
//       bvtotal.stat();
2445
 
 
2446
 
 
2447
 
       if (++clcnt == 5)
2448
 
       {
2449
 
          clcnt = 0;
2450
 
          bvtotal.clear();
2451
 
 
2452
 
//          cout << "Post clear." << endl;
2453
 
//          bvtotal.stat();
2454
 
 
2455
 
       }
2456
 
 
2457
 
       delete [] sermem;
2458
 
       delete [] smem;
2459
 
       delete bvect_min1;
2460
 
       delete bvect_full1;
2461
 
 
2462
 
   } // for i
2463
 
 
2464
 
}
2465
 
 
2466
 
 
2467
 
void StressTest(int repetitions)
2468
 
{
2469
 
 
2470
 
   unsigned RatioSum = 0;
2471
 
   unsigned SRatioSum = 0;
2472
 
   unsigned DeltaSum = 0;
2473
 
   unsigned SDeltaSum = 0;
2474
 
 
2475
 
   unsigned clear_count = 0;
2476
 
 
2477
 
   bvect  bvtotal;
2478
 
   bvtotal.set_new_blocks_strat(bm::BM_GAP);
2479
 
 
2480
 
 
2481
 
   cout << "----------------------------StressTest" << endl;
2482
 
 
2483
 
   unsigned size = BITVECT_SIZE - 10;
2484
 
//size = BITVECT_SIZE / 10;
2485
 
   int i;
2486
 
   for (i = 0; i < repetitions; ++i)
2487
 
   {
2488
 
        cout << endl << " - - - - - - - - - - - - STRESS STEP " << i << endl;
2489
 
 
2490
 
        switch (rand() % 3)
2491
 
        {
2492
 
        case 0:
2493
 
            size = BITVECT_SIZE / 10;
2494
 
            break;
2495
 
        case 1:
2496
 
            size = BITVECT_SIZE / 2;
2497
 
            break;
2498
 
        default:
2499
 
            size = BITVECT_SIZE - 10;
2500
 
            break;
2501
 
        } // switch
2502
 
 
2503
 
 
2504
 
        bvect_mini*   bvect_min1= new bvect_mini(size);
2505
 
        bvect_mini*   bvect_min2= new bvect_mini(size);
2506
 
        bvect*        bvect_full1= new bvect();
2507
 
        bvect*        bvect_full2= new bvect();
2508
 
 
2509
 
        bvect_full1->set_new_blocks_strat(i&1 ? bm::BM_GAP : bm::BM_BIT);
2510
 
        bvect_full2->set_new_blocks_strat(i&1 ? bm::BM_GAP : bm::BM_BIT);
2511
 
 
2512
 
        int opt = rand() % 2;
2513
 
 
2514
 
        unsigned start1 = 0;
2515
 
 
2516
 
        switch (rand() % 3)
2517
 
        {
2518
 
        case 1:
2519
 
            start1 += size / 5;
2520
 
            break;
2521
 
        default:
2522
 
            break;
2523
 
        }
2524
 
 
2525
 
        unsigned start2 = 0;
2526
 
 
2527
 
        switch (rand() % 3)
2528
 
        {
2529
 
        case 1:
2530
 
            start2 += size / 5;
2531
 
            break;
2532
 
        default:
2533
 
            break;
2534
 
        }
2535
 
/*
2536
 
        if (i == 3)
2537
 
        {
2538
 
            g_cnt_check = 1;
2539
 
        }
2540
 
*/
2541
 
        FillSetsRandomMethod(bvect_min1, bvect_full1, start1, size, opt);
2542
 
        FillSetsRandomMethod(bvect_min2, bvect_full2, start2, size, opt);
2543
 
 
2544
 
 
2545
 
 
2546
 
        unsigned arr[bm::set_total_blocks]={0,};
2547
 
        bm::id_t cnt = bvect_full1->count();
2548
 
        unsigned last_block = bvect_full1->count_blocks(arr);
2549
 
        unsigned sum = bm::sum_arr(&arr[0], &arr[last_block+1]);
2550
 
 
2551
 
        if (sum != cnt)
2552
 
        {
2553
 
            cout << "Error in function count_blocks." << endl;
2554
 
            cout << "Array sum = " << sum << endl;
2555
 
            cout << "BitCount = " << cnt << endl;
2556
 
            cnt = bvect_full1->count();
2557
 
            for (unsigned i = 0; i <= last_block; ++i)
2558
 
            {
2559
 
                if (arr[i])
2560
 
                {
2561
 
                    cout << "[" << i << ":" << arr[i] << "]";
2562
 
                }
2563
 
            }
2564
 
            cout << endl;
2565
 
            cout << "================" << endl;
2566
 
            bvect_full1->stat();
2567
 
 
2568
 
 
2569
 
            exit(1);
2570
 
        }
2571
 
 
2572
 
        CheckCountRange(*bvect_full1, start1, BITVECT_SIZE, arr);
2573
 
        CheckIntervals(*bvect_full1, BITVECT_SIZE);
2574
 
 
2575
 
 
2576
 
        
2577
 
 
2578
 
        CheckCountRange(*bvect_full2, start2, BITVECT_SIZE);
2579
 
 
2580
 
        CheckCountRange(*bvect_full1, 0, start1, arr);
2581
 
        CheckCountRange(*bvect_full2, 0, start2);
2582
 
 
2583
 
 
2584
 
/*        
2585
 
        cout << "!!!!!!!!!!!!!!!" << endl;
2586
 
        CheckVectors(*bvect_min1, *bvect_full1, size);
2587
 
        cout << "!!!!!!!!!!!!!!!" << endl;
2588
 
        CheckVectors(*bvect_min2, *bvect_full2, size);
2589
 
        cout << "!!!!!!!!!!!!!!!" << endl;
2590
 
 
2591
 
        
2592
 
         bvect_full1->stat();
2593
 
         cout << " --" << endl;
2594
 
         bvect_full2->stat();
2595
 
*/
2596
 
 
2597
 
        int operation = rand()%4;
2598
 
//operation = 2;
2599
 
 
2600
 
        switch(operation)
2601
 
        {
2602
 
        case 0:
2603
 
            cout << "Operation OR" << endl;
2604
 
            bvect_min1->combine_or(*bvect_min2);
2605
 
            break;
2606
 
 
2607
 
        case 1:
2608
 
            cout << "Operation SUB" << endl;
2609
 
            bvect_min1->combine_sub(*bvect_min2);
2610
 
            break;
2611
 
 
2612
 
        case 2:
2613
 
            cout << "Operation XOR" << endl;
2614
 
            bvect_min1->combine_xor(*bvect_min2);
2615
 
            break;
2616
 
 
2617
 
        default:
2618
 
            cout << "Operation AND" << endl;
2619
 
            bvect_min1->combine_and(*bvect_min2);
2620
 
            break;
2621
 
        }
2622
 
 
2623
 
        int cres1 = bvect_min1->compare(*bvect_min2);
2624
 
 
2625
 
        delete bvect_min2;
2626
 
 
2627
 
        switch(operation)
2628
 
        {
2629
 
        case 0:
2630
 
            {
2631
 
            cout << "Operation OR" << endl;
2632
 
 
2633
 
            bm::id_t predicted_count = bm::count_or(*bvect_full1, *bvect_full2);
2634
 
            
2635
 
            bvect_full1->bit_or(*bvect_full2);
2636
 
            
2637
 
            bm::id_t count = bvect_full1->count();
2638
 
 
2639
 
            if (count != predicted_count)
2640
 
            {
2641
 
                cout << "Predicted count error!" << endl;
2642
 
                cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
2643
 
                exit(1);
2644
 
            }
2645
 
            
2646
 
            }
2647
 
            break;
2648
 
 
2649
 
        case 1:
2650
 
            {
2651
 
            cout << "Operation SUB" << endl;
2652
 
            
2653
 
            bm::id_t predicted_count = bm::count_sub(*bvect_full1, *bvect_full2);
2654
 
            
2655
 
            bvect_full1->bit_sub(*bvect_full2);
2656
 
            
2657
 
            bm::id_t count = bvect_full1->count();
2658
 
 
2659
 
            if (count != predicted_count)
2660
 
            {
2661
 
                cout << "Predicted count error!" << endl;
2662
 
                cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
2663
 
                exit(1);
2664
 
            }
2665
 
            
2666
 
            
2667
 
            }
2668
 
            break;
2669
 
 
2670
 
        case 2:
2671
 
            {
2672
 
            cout << "Operation XOR" << endl;
2673
 
           
2674
 
            bm::id_t predicted_count = bm::count_xor(*bvect_full1, *bvect_full2);
2675
 
            
2676
 
            bvect_full1->bit_xor(*bvect_full2);
2677
 
            
2678
 
            bm::id_t count = bvect_full1->count();
2679
 
 
2680
 
            if (count != predicted_count)
2681
 
            {
2682
 
                cout << "Predicted count error!" << endl;
2683
 
                cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
2684
 
                exit(1);
2685
 
            }
2686
 
            
2687
 
            }
2688
 
            
2689
 
            break;
2690
 
 
2691
 
        default:
2692
 
            {
2693
 
            cout << "Operation AND" << endl;
2694
 
 
2695
 
            bm::id_t predicted_count = bm::count_and(*bvect_full1, *bvect_full2);
2696
 
 
2697
 
            bvect_full1->bit_and(*bvect_full2);
2698
 
 
2699
 
            bm::id_t count = bvect_full1->count();
2700
 
 
2701
 
            if (count != predicted_count)
2702
 
            {
2703
 
                cout << "Predicted count error!" << endl;
2704
 
                cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
2705
 
                exit(1);
2706
 
            }
2707
 
 
2708
 
            }
2709
 
            break;
2710
 
        }
2711
 
 
2712
 
        cout << "Operation comparison" << endl;
2713
 
        CheckVectors(*bvect_min1, *bvect_full1, size);
2714
 
 
2715
 
        int cres2 = bvect_full1->compare(*bvect_full2);
2716
 
 
2717
 
        CheckIntervals(*bvect_full1, BITVECT_SIZE);
2718
 
 
2719
 
        if (cres1 != cres2)
2720
 
        {
2721
 
            cout << cres1 << " " << cres2 << endl;
2722
 
            cout << bvect_full1->get_first() << " " << bvect_full1->count() << endl;
2723
 
            cout << bvect_full2->get_first() << " " << bvect_full2->count() << endl;
2724
 
 
2725
 
           // bvect_full1->stat(1000);
2726
 
            cout << endl;
2727
 
           // bvect_full2->stat(1000);
2728
 
            printf("Bitset comparison operation failed.\n");
2729
 
            exit(1);
2730
 
        }
2731
 
 
2732
 
        delete bvect_full2;
2733
 
 
2734
 
 
2735
 
        struct bvect::statistics st1;
2736
 
        bvect_full1->calc_stat(&st1);
2737
 
        bvect_full1->optimize();
2738
 
        bvect_full1->optimize_gap_size();
2739
 
        struct bvect::statistics st2;
2740
 
        bvect_full1->calc_stat(&st2);
2741
 
 
2742
 
        unsigned Ratio = (st2.memory_used * 100)/st1.memory_used;
2743
 
        RatioSum+=Ratio;
2744
 
        DeltaSum+=st1.memory_used - st2.memory_used;
2745
 
 
2746
 
        cout << "Optimization statistics: " << endl  
2747
 
             << "   MemUsedBefore=" << st1.memory_used
2748
 
             << "   MemUsed=" << st2.memory_used 
2749
 
             << "   Ratio=" << Ratio << "%"
2750
 
             << "   Delta=" << st1.memory_used - st2.memory_used
2751
 
             << endl;
2752
 
                
2753
 
        cout << "Optimization comparison" << endl;
2754
 
 
2755
 
        CheckVectors(*bvect_min1, *bvect_full1, size);
2756
 
 
2757
 
        bvect_full1->set_gap_levels(gap_len_table_min<true>::_len);
2758
 
        CheckVectors(*bvect_min1, *bvect_full1, size);
2759
 
        CheckIntervals(*bvect_full1, BITVECT_SIZE);
2760
 
 
2761
 
        //CheckCountRange(*bvect_full1, 0, size);
2762
 
 
2763
 
 
2764
 
        // Serialization
2765
 
        bvect_full1->calc_stat(&st2);
2766
 
 
2767
 
        cout << "Memory allocation: " << st2.max_serialize_mem << endl;
2768
 
        unsigned char* sermem = new unsigned char[st2.max_serialize_mem];
2769
 
 
2770
 
//    bvect_full1->stat();
2771
 
 
2772
 
        cout << "Serialization...";
2773
 
        unsigned slen = bm::serialize(*bvect_full1, sermem);
2774
 
        cout << "Ok" << endl;
2775
 
 
2776
 
        delete bvect_full1;
2777
 
 
2778
 
        unsigned SRatio = (slen*100)/st2.memory_used;
2779
 
        SRatioSum+=SRatio;
2780
 
        SDeltaSum+=st2.memory_used - slen;
2781
 
 
2782
 
 
2783
 
        cout << "Serialized mem_max = " << st2.max_serialize_mem 
2784
 
             << " size= " << slen 
2785
 
             << " Ratio=" << SRatio << "%"
2786
 
             << " Delta=" << st2.memory_used - slen
2787
 
             << endl;
2788
 
 
2789
 
        bvect*        bvect_full3= new bvect();
2790
 
        unsigned char* new_sermem = new unsigned char[slen];
2791
 
        memcpy(new_sermem, sermem, slen);
2792
 
        delete [] sermem;
2793
 
 
2794
 
        cout << "Deserialization...";
2795
 
 
2796
 
        bm::deserialize(*bvect_full3, new_sermem);
2797
 
 
2798
 
        bm::deserialize(bvtotal, new_sermem);
2799
 
 
2800
 
        cout << "Ok." << endl;
2801
 
        delete [] new_sermem;
2802
 
 
2803
 
        cout << "Optimization...";
2804
 
        bvtotal.optimize();
2805
 
        cout << "Ok." << endl;
2806
 
 
2807
 
        ++clear_count;
2808
 
 
2809
 
        if (clear_count == 4)
2810
 
        {
2811
 
           bvtotal.clear();
2812
 
           clear_count = 0;
2813
 
        }
2814
 
 
2815
 
        cout << "Serialization comparison" << endl;
2816
 
        CheckVectors(*bvect_min1, *bvect_full3, size, true);
2817
 
 
2818
 
 
2819
 
        delete bvect_min1;
2820
 
        delete bvect_full3;
2821
 
 
2822
 
    }
2823
 
 
2824
 
    --i;
2825
 
    cout << "Repetitions:" << i <<
2826
 
            " AVG optimization ratio:" << RatioSum/i 
2827
 
         << " AVG Delta:" << DeltaSum/i
2828
 
         << endl
2829
 
         << " AVG serialization Ratio:"<< SRatioSum/i
2830
 
         << " Delta:" << SDeltaSum/i
2831
 
         << endl;
2832
 
}
2833
 
 
2834
 
void GAPCheck()
2835
 
{
2836
 
   cout << "-------------------------------------------GAPCheck" << endl;
2837
 
 
2838
 
    {
2839
 
 
2840
 
    gap_vector   gapv(0);
2841
 
    bvect_mini  bvect_min(bm::gap_max_bits);
2842
 
 
2843
 
    unsigned i;
2844
 
    for( i = 0; i < 454; ++i)
2845
 
    {
2846
 
        bvect_min.set_bit(i);
2847
 
        gapv.set_bit(i);
2848
 
    }
2849
 
 
2850
 
    for(i = 0; i < 254; ++i)
2851
 
    {
2852
 
        bvect_min.clear_bit(i);
2853
 
        gapv.clear_bit(i);
2854
 
    }
2855
 
 
2856
 
    for(i = 5; i < 10; ++i)
2857
 
    {
2858
 
        bvect_min.set_bit(i);
2859
 
        gapv.set_bit(i);
2860
 
    }
2861
 
 
2862
 
    for( i = 0; i < bm::gap_max_bits; ++i)
2863
 
    {
2864
 
        int bit1 = (gapv.is_bit_true(i) == 1);
2865
 
        int bit2 = (bvect_min.is_bit_true(i) != 0);
2866
 
        int bit3 = (gapv.test(i) == 1);
2867
 
        if (bit1 != bit2)
2868
 
        {
2869
 
            cout << "problem with bit comparison " << i << endl;
2870
 
            exit(1);
2871
 
        }
2872
 
        if (bit1 != bit3)
2873
 
        {
2874
 
            cout << "problem with bit test comparison " << i << endl;
2875
 
            exit(1);
2876
 
        }
2877
 
 
2878
 
    }
2879
 
 
2880
 
    }
2881
 
 
2882
 
 
2883
 
   {
2884
 
   gap_vector gapv(1);
2885
 
   int bit = gapv.is_bit_true(65535);
2886
 
 
2887
 
   if (bit != 1)
2888
 
   {
2889
 
      cout << "Bit != 1" << endl;
2890
 
      exit(1);
2891
 
   }
2892
 
   
2893
 
   int i;
2894
 
   for (i = 0; i < 65536; ++i)
2895
 
   {
2896
 
        bit = gapv.is_bit_true(i);
2897
 
        if (bit != 1)
2898
 
        {
2899
 
            cout << "2.Bit != 1" << endl;
2900
 
            exit(1);
2901
 
        }
2902
 
   }
2903
 
   unsigned cnt = gapv.count_range(0, 65535);
2904
 
   if (cnt != 65536)
2905
 
   {
2906
 
       cout << "count_range failed:" << cnt << endl;
2907
 
       exit(1);
2908
 
   }
2909
 
   
2910
 
   CheckCountRange(gapv, 10, 20);
2911
 
   CheckCountRange(gapv, 0, 20);
2912
 
 
2913
 
   printf("gapv 1 check ok\n");
2914
 
   }
2915
 
 
2916
 
   {
2917
 
   gap_vector gapv(0);
2918
 
 
2919
 
 
2920
 
   int bit = gapv.is_bit_true(65535);
2921
 
   int bit1 = gapv.test(65535);
2922
 
   if(bit != 0)
2923
 
   {
2924
 
      cout << "Bit != 0" << endl;
2925
 
      exit(1);
2926
 
   }
2927
 
      
2928
 
   int i;
2929
 
   for (i = 0; i < 65536; ++i)
2930
 
   {
2931
 
        bit = gapv.is_bit_true(i);
2932
 
        bit1 = gapv.test(i);
2933
 
        if (bit != 0)
2934
 
        {
2935
 
            cout << "2.Bit != 0 bit =" << i << endl;
2936
 
            exit(1);
2937
 
        }
2938
 
        if (bit1 != 0)
2939
 
        {
2940
 
            cout << "2.Bit test != 0 bit =" << i << endl;
2941
 
            exit(1);
2942
 
        }
2943
 
   }
2944
 
   unsigned cnt = gapv.count_range(0, 65535);
2945
 
   if (cnt != 0)
2946
 
   {
2947
 
       cout << "count_range failed:" << cnt << endl;
2948
 
       exit(1);
2949
 
   }
2950
 
   CheckCountRange(gapv, 10, 20);
2951
 
   CheckCountRange(gapv, 0, 20);
2952
 
 
2953
 
   printf("gapv 2 check ok\n");
2954
 
   }
2955
 
 
2956
 
   {
2957
 
   gap_vector gapv(0);
2958
 
 
2959
 
   gapv.set_bit(1);
2960
 
   gapv.set_bit(0);
2961
 
 
2962
 
   gapv.control();
2963
 
   CheckCountRange(gapv, 0, 20);
2964
 
 
2965
 
   int bit = gapv.is_bit_true(0);
2966
 
 
2967
 
   if (bit != 1)
2968
 
   {
2969
 
      cout << "Trouble" << endl;
2970
 
      exit(1);
2971
 
   }
2972
 
   
2973
 
   bit = gapv.is_bit_true(1);
2974
 
   if (bit != 1)
2975
 
   {
2976
 
      cout << "Trouble 2." << endl;
2977
 
      exit(1);
2978
 
   }
2979
 
 
2980
 
 
2981
 
   bit = gapv.is_bit_true(2);
2982
 
   if(bit != 0)
2983
 
   {
2984
 
      cout << "Trouble 3." << endl;
2985
 
      exit(1);
2986
 
   }
2987
 
 
2988
 
   }
2989
 
 
2990
 
   {
2991
 
   gap_vector gapv(0);
2992
 
 
2993
 
   gapv.set_bit(0);
2994
 
   gapv.control();
2995
 
   gapv.set_bit(1);
2996
 
   gapv.control();
2997
 
 
2998
 
   gapv.set_bit(4);
2999
 
   gapv.control();
3000
 
   gapv.set_bit(5);
3001
 
   gapv.control();
3002
 
   CheckCountRange(gapv, 4, 5);
3003
 
   CheckCountRange(gapv, 3, 5);
3004
 
 
3005
 
   gapv.set_bit(3);
3006
 
   CheckCountRange(gapv, 3, 3);
3007
 
   CheckCountRange(gapv, 3, 5);
3008
 
 
3009
 
   gapv.control();
3010
 
   
3011
 
   int bit = gapv.is_bit_true(0);
3012
 
   if(bit!=1)
3013
 
   {
3014
 
      cout << "Bug" << endl;
3015
 
   }
3016
 
   bit = gapv.is_bit_true(1);
3017
 
   if(bit!=1)
3018
 
   {
3019
 
      cout << "Bug2" << endl;
3020
 
   }
3021
 
 
3022
 
   gapv.control();
3023
 
   gapv.set_bit(4);
3024
 
   gapv.control();
3025
 
 
3026
 
 
3027
 
   printf("gapv 3 check ok\n");
3028
 
   }
3029
 
 
3030
 
   {
3031
 
        gap_vector gapv(0);
3032
 
        bvect_mini   bvect_min(bm::gap_max_bits);
3033
 
cout << "++++++1" << endl;
3034
 
print_gap(gapv, 10);
3035
 
        gapv.set_bit(bm::gap_max_bits-1);
3036
 
        gapv.control();
3037
 
        print_gap(gapv, 10);
3038
 
 
3039
 
//cout << "++++++2" << endl;
3040
 
//cout << "m_buf=" << bvect_min.m_buf << endl;
3041
 
 
3042
 
        bvect_min.set_bit(bm::gap_max_bits-1);
3043
 
cout << "++++++3" << endl;
3044
 
        gapv.set_bit(5);
3045
 
        print_gap(gapv,15);
3046
 
        gapv.control();
3047
 
        bvect_min.set_bit(5);
3048
 
cout << "++++++4" << endl;
3049
 
 
3050
 
        CheckCountRange(gapv, 13, 150);
3051
 
        gapv.control();
3052
 
 
3053
 
        unsigned i;
3054
 
        for (i = 0; i < bm::gap_max_bits; ++i)
3055
 
        {
3056
 
            if (i == 65535)
3057
 
                printf("%i\n", i);
3058
 
            int bit1 = (gapv.is_bit_true(i) == 1);
3059
 
            int bit2 = (bvect_min.is_bit_true(i) != 0);
3060
 
            int bit3 = (gapv.test(i) == 1);
3061
 
            if (bit1 != bit2)
3062
 
            {
3063
 
                cout << "problem with bit comparison " << i << endl;
3064
 
            }
3065
 
            if (bit1 != bit3)
3066
 
            {
3067
 
                cout << "problem with bit test comparison " << i << endl;
3068
 
            }
3069
 
 
3070
 
        }
3071
 
 
3072
 
        gapv.clear_bit(5);
3073
 
        bvect_min.clear_bit(5);
3074
 
        gapv.control();
3075
 
 
3076
 
        for ( i = 0; i < bm::gap_max_bits; ++i)
3077
 
        {
3078
 
            if (i == 65535)
3079
 
                printf("%i\n", i);
3080
 
            int bit1 = (gapv.is_bit_true(i) == 1);
3081
 
            int bit2 = (bvect_min.is_bit_true(i) != 0);
3082
 
            int bit3 = (gapv.test(i) == 1);
3083
 
            if (bit1 != bit2)
3084
 
            {
3085
 
                cout << "2.problem with bit comparison " << i << endl;
3086
 
            }
3087
 
            if (bit1 != bit3)
3088
 
            {
3089
 
                cout << "2.problem with bit test comparison " << i << endl;
3090
 
            }
3091
 
        }
3092
 
   printf("gapv check 4 ok.\n");
3093
 
   }
3094
 
 
3095
 
   {
3096
 
        gap_vector gapv(0);
3097
 
        bvect_mini   bvect_min(65536);
3098
 
        
3099
 
        int i;
3100
 
        for (i = 10; i > 0; i-=2)
3101
 
        {
3102
 
            bvect_min.set_bit(i);
3103
 
            gapv.set_bit(i);
3104
 
            gapv.control();
3105
 
            CheckCountRange(gapv, 0, i);
3106
 
 
3107
 
            int bit1 = (gapv.is_bit_true(i) == 1);
3108
 
            int bit2 = (bvect_min.is_bit_true(i) != 0);
3109
 
            int bit3 = (gapv.test(i) != 0);
3110
 
            if (bit1 != bit2)
3111
 
            {
3112
 
                cout << "3.problem with bit comparison " << i << endl;
3113
 
            }
3114
 
            if (bit1 != bit3)
3115
 
            {
3116
 
                cout << "3.problem with bit test comparison " << i << endl;
3117
 
            }
3118
 
 
3119
 
        }
3120
 
        for (i = 0; i < (int)bm::gap_max_bits; ++i)
3121
 
        {
3122
 
            int bit1 = (gapv.is_bit_true(i) == 1);
3123
 
            int bit2 = (bvect_min.is_bit_true(i) != 0);
3124
 
            int bit3 = (gapv.test(i) == 1);
3125
 
 
3126
 
            if (bit1 != bit2)
3127
 
            {
3128
 
                cout << "3.problem with bit comparison " << i << endl;
3129
 
            }
3130
 
            if (bit1 != bit3)
3131
 
            {
3132
 
                cout << "3.problem with bit test comparison " << i << endl;
3133
 
            }
3134
 
        }
3135
 
   printf("gapv check 5 ok.\n");
3136
 
   }
3137
 
 
3138
 
   {
3139
 
        gap_vector gapv(0);
3140
 
        bvect_mini   bvect_min(bm::gap_max_bits);
3141
 
        
3142
 
        int i;
3143
 
        for (i = 0; i < 25; ++i)
3144
 
        {
3145
 
            unsigned id = random_minmax(0, bm::gap_max_bits);
3146
 
            bvect_min.set_bit(id);
3147
 
            gapv.set_bit(id);
3148
 
            gapv.control();
3149
 
            CheckCountRange(gapv, 0, id);
3150
 
            CheckCountRange(gapv, id, 65535);
3151
 
        }
3152
 
 
3153
 
        for (i = 0; i < (int)bm::gap_max_bits; ++i)
3154
 
        {
3155
 
            int bit1 = (gapv.is_bit_true(i) == 1);
3156
 
            int bit2 = (bvect_min.is_bit_true(i) != 0);
3157
 
            if (bit1 != bit2)
3158
 
            {
3159
 
                cout << "4.problem with bit comparison " << i << endl;
3160
 
            }
3161
 
        }
3162
 
 
3163
 
        for (i = bm::gap_max_bits; i < 0; --i)
3164
 
        {
3165
 
            int bit1 = (gapv.is_bit_true(i) == 1);
3166
 
            int bit2 = (bvect_min.is_bit_true(i) != 0);
3167
 
            if (bit1 != bit2)
3168
 
            {
3169
 
                cout << "5.problem with bit comparison " << i << endl;
3170
 
            }
3171
 
        }
3172
 
   printf("gapv check 6 ok.\n");
3173
 
 
3174
 
   }
3175
 
 
3176
 
   printf("gapv random bit set check ok.\n");
3177
 
 
3178
 
 
3179
 
   // conversion functions test
3180
 
   
3181
 
   {
3182
 
   // aligned position test
3183
 
   bvect        bvect;
3184
 
 
3185
 
   bvect.set_bit(1);
3186
 
   bvect.clear();
3187
 
 
3188
 
 
3189
 
   unsigned* buf = (unsigned*) bvect.get_block(0);
3190
 
 
3191
 
   bm::or_bit_block(buf, 0, 4);
3192
 
   unsigned cnt = bm::bit_block_calc_count_range(buf, 0, 3);
3193
 
   assert(cnt == 4);
3194
 
   
3195
 
   bool bit = bvect.get_bit(0);
3196
 
   assert(bit);
3197
 
   bit = bvect.get_bit(1);
3198
 
   assert(bit);
3199
 
   bit = bvect.get_bit(2);
3200
 
   assert(bit);
3201
 
   bit = bvect.get_bit(3);
3202
 
   assert(bit);
3203
 
   bit = bvect.get_bit(4);
3204
 
   assert(bit==0);
3205
 
 
3206
 
   bm::or_bit_block(buf, 0, 36); 
3207
 
   cnt = bm::bit_block_calc_count_range(buf, 0, 35);
3208
 
   assert(cnt == 36);
3209
 
 
3210
 
   for (int i = 0; i < 36; ++i)
3211
 
   {
3212
 
        bit = (bvect.get_bit(i) != 0);
3213
 
        assert(bit);
3214
 
   }
3215
 
   bit = (bvect.get_bit(36) != 0);
3216
 
   assert(bit==0);
3217
 
 
3218
 
   unsigned count = bvect.recalc_count();
3219
 
   assert(count == 36);   
3220
 
   
3221
 
   cout << "Aligned position test ok." << endl; 
3222
 
 
3223
 
   }
3224
 
 
3225
 
 
3226
 
   {
3227
 
   // unaligned position test
3228
 
   bvect   bvect;
3229
 
 
3230
 
   bvect.set_bit(0);
3231
 
   bvect.clear();
3232
 
 
3233
 
   unsigned* buf = (unsigned*) bvect.get_block(0);
3234
 
 
3235
 
   bm::or_bit_block(buf, 5, 32);
3236
 
   bool bit = (bvect.get_bit(4) != 0);
3237
 
   assert(bit==0);
3238
 
   unsigned cnt = bm::bit_block_calc_count_range(buf, 5, 5+32-1);
3239
 
   assert(cnt == 32);
3240
 
   cnt = bm::bit_block_calc_count_range(buf, 5, 5+32);
3241
 
   assert(cnt == 32);
3242
 
 
3243
 
   int i;
3244
 
   for (i = 5; i < 4 + 32; ++i)
3245
 
   {
3246
 
        bit = bvect.get_bit(i);
3247
 
        assert(bit);
3248
 
   }
3249
 
   int count = bvect.recalc_count();
3250
 
   assert(count==32);
3251
 
 
3252
 
   cout << "Unaligned position ok." << endl;
3253
 
 
3254
 
   } 
3255
 
 
3256
 
   // random test
3257
 
   {
3258
 
   cout << "random test" << endl;
3259
 
 
3260
 
   bvect   bvect;
3261
 
 
3262
 
   bvect.set_bit(0);
3263
 
   bvect.clear();
3264
 
 
3265
 
   unsigned* buf = (unsigned*) bvect.get_block(0);
3266
 
   for (int i = 0; i < 5000; ++i)
3267
 
   {
3268
 
        unsigned start = rand() % 65535;
3269
 
        unsigned end = rand() % 65535;
3270
 
        if (start > end)
3271
 
        {
3272
 
            unsigned tmp = end;
3273
 
            end = start;
3274
 
            start = tmp;
3275
 
        }
3276
 
        unsigned len = end - start;
3277
 
        if (len)
3278
 
        {
3279
 
           bm::or_bit_block(buf, start, len);
3280
 
           unsigned cnt = bm::bit_block_calc_count_range(buf, start, end);
3281
 
           if (cnt != len)
3282
 
           {
3283
 
            cout << "random test: count_range comparison failed. " 
3284
 
                 << " LEN = " << len << " cnt = " << cnt
3285
 
                 << endl;
3286
 
                 exit(1);
3287
 
           }
3288
 
 
3289
 
           unsigned count = bvect.recalc_count();
3290
 
 
3291
 
           if (count != len)
3292
 
           {
3293
 
            cout << "random test: count comparison failed. " 
3294
 
                 << " LEN = " << len << " count = " << count
3295
 
                 << endl;
3296
 
                 exit(1);
3297
 
           }            
3298
 
 
3299
 
           for (unsigned j = start; j < end; ++j)
3300
 
           {
3301
 
                bool bit = bvect.get_bit(j);
3302
 
                if (!bit)
3303
 
                {
3304
 
                    cout << "random test: bit comparison failed. bit#" 
3305
 
                         << i << endl;
3306
 
                    exit(1);
3307
 
                } 
3308
 
           } // for j
3309
 
 
3310
 
        } 
3311
 
        bvect.clear();
3312
 
 
3313
 
        if ((i % 100)==0)
3314
 
        {
3315
 
            cout << "*" << flush;
3316
 
        }
3317
 
   } // for i
3318
 
 
3319
 
   cout << endl << "Random test Ok." << endl;
3320
 
 
3321
 
   }
3322
 
 
3323
 
 
3324
 
   // conversion test
3325
 
 
3326
 
   cout << "Conversion test" << endl;
3327
 
    
3328
 
   {
3329
 
   
3330
 
   gap_vector gapv(0);
3331
 
   bvect   bvect;
3332
 
 
3333
 
   gapv.set_bit(0);
3334
 
   gapv.set_bit(2);
3335
 
   gapv.set_bit(10);
3336
 
   gapv.set_bit(11);
3337
 
   gapv.set_bit(12);
3338
 
   
3339
 
   CheckCountRange(gapv, 3, 15);
3340
 
 
3341
 
   print_gap(gapv, 100);
3342
 
   bvect.set_bit(0);
3343
 
   bvect.clear();
3344
 
 
3345
 
   unsigned* buf = (unsigned*) bvect.get_block(0);
3346
 
 
3347
 
 
3348
 
   gapv.convert_to_bitset(buf);
3349
 
 
3350
 
 
3351
 
   unsigned bitcount = bvect.recalc_count();
3352
 
 
3353
 
 
3354
 
   if (bitcount != 5)
3355
 
   {
3356
 
      cout << "test failed: bitcout = " << bitcount << endl;
3357
 
      exit(1);
3358
 
   }
3359
 
 
3360
 
 
3361
 
   gap_vector gapv1(0);
3362
 
   gap_word_t* gap_buf = gapv1.get_buf();
3363
 
   *gap_buf = 0;
3364
 
   bit_convert_to_gap(gap_buf, buf, bm::gap_max_bits, bm::gap_max_buff_len);
3365
 
   print_gap(gapv1, 100);
3366
 
 
3367
 
   bitcount = gapv1.bit_count();
3368
 
   if(bitcount != 5)
3369
 
   {
3370
 
      cout << "2.test_failed: bitcout = " << bitcount << endl;
3371
 
      exit(1);
3372
 
   }
3373
 
 
3374
 
   printf("conversion test ok.\n");
3375
 
    
3376
 
   }
3377
 
 
3378
 
   // gap AND test
3379
 
 
3380
 
   {
3381
 
   // special case 1: operand is all 1
3382
 
   gap_vector gapv1(0);
3383
 
   gapv1.set_bit(2);
3384
 
   gap_vector gapv2(1); 
3385
 
 
3386
 
   gapv1.combine_and(gapv2.get_buf());
3387
 
   gapv1.control();
3388
 
   print_gap(gapv1, 0);
3389
 
 
3390
 
   int count = gapv1.bit_count();
3391
 
   assert(count == 1);
3392
 
   int bit = gapv1.is_bit_true(2);
3393
 
   if(bit == 0)
3394
 
   {
3395
 
      cout << "Wrong bit" << endl;
3396
 
      exit(1);
3397
 
   }
3398
 
   CheckCountRange(gapv1, 0, 17);
3399
 
 
3400
 
   }
3401
 
 
3402
 
   {
3403
 
   // special case 2: src is all 1
3404
 
   gap_vector gapv1(1);
3405
 
   gap_vector gapv2(0); 
3406
 
   gapv2.set_bit(2);
3407
 
 
3408
 
   gapv1.combine_and(gapv2.get_buf());
3409
 
   gapv1.control();
3410
 
   print_gap(gapv1, 0);
3411
 
 
3412
 
   int count = gapv1.bit_count();
3413
 
   assert(count == 1);
3414
 
   int bit = gapv1.is_bit_true(2);
3415
 
   assert(bit);
3416
 
 
3417
 
   }
3418
 
 
3419
 
   {
3420
 
   gap_vector gapv;
3421
 
   gap_vector gapv1(0);
3422
 
 
3423
 
   gapv1.set_bit(3);
3424
 
   gapv1.set_bit(4);
3425
 
   print_gap(gapv1, 0);
3426
 
 
3427
 
   gap_vector gapv2(0); 
3428
 
   gapv2.set_bit(2);
3429
 
   gapv2.set_bit(3);
3430
 
   print_gap(gapv2, 0);
3431
 
 
3432
 
   bm::gap_buff_op((gap_word_t*)gapv.get_buf(), 
3433
 
                         gapv1.get_buf(), 0,
3434
 
                         gapv2.get_buf(), 0, bm::and_op); 
3435
 
   print_gap(gapv, 0);
3436
 
   gapv.control();
3437
 
 
3438
 
 
3439
 
    int bit1 = (gapv.is_bit_true(3) == 1);
3440
 
    if(bit1 == 0)
3441
 
    {
3442
 
       cout << "Checking failed." << endl;
3443
 
       exit(0);
3444
 
    }
3445
 
 
3446
 
   gapv1.combine_or(gapv2);
3447
 
   print_gap(gapv1, 0);
3448
 
   gapv1.control();
3449
 
 
3450
 
   }
3451
 
 
3452
 
   {
3453
 
        printf("gap AND test 1.\n");
3454
 
        gap_vector gapv1(0);
3455
 
        gap_vector gapv2(0);
3456
 
        bvect_mini   bvect_min1(65536);
3457
 
        bvect_mini   bvect_min2(65536);
3458
 
 
3459
 
        gapv1.set_bit(65535);
3460
 
        bvect_min1.set_bit(65535);
3461
 
        gapv1.set_bit(4);
3462
 
        bvect_min1.set_bit(4);
3463
 
 
3464
 
        gapv2.set_bit(65535);
3465
 
        bvect_min2.set_bit(65535);
3466
 
        gapv2.set_bit(3);
3467
 
        bvect_min2.set_bit(3);
3468
 
        CheckCountRange(gapv2, 3, 65535);
3469
 
 
3470
 
        gapv2.control();
3471
 
 
3472
 
        printf("vect1:"); print_gap(gapv1, 0);
3473
 
        printf("vect2:");print_gap(gapv2, 0);
3474
 
 
3475
 
        gapv1.combine_and(gapv2.get_buf());
3476
 
        printf("vect1:");print_gap(gapv1, 0);
3477
 
 
3478
 
        gapv1.control();
3479
 
        unsigned bit1 = gapv1.is_bit_true(65535);
3480
 
        assert(bit1);
3481
 
 
3482
 
        bvect_min1.combine_and(bvect_min2);
3483
 
        CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
3484
 
   }
3485
 
 
3486
 
   {
3487
 
        printf("gap random AND test.\n");
3488
 
        gap_vector gapv1(0);
3489
 
        gap_vector gapv2(0);
3490
 
        bvect_mini   bvect_min1(65536);
3491
 
        bvect_mini   bvect_min2(65536);
3492
 
        
3493
 
        int i;
3494
 
        for (i = 0; i < 25; ++i)
3495
 
        {
3496
 
            unsigned id = random_minmax(0, 65535);
3497
 
            bvect_min1.set_bit(id);
3498
 
            gapv1.set_bit(id);
3499
 
            gapv1.control();
3500
 
            CheckCountRange(gapv1, 0, id);
3501
 
            CheckCountRange(gapv1, id, 65535);
3502
 
        }
3503
 
        for (i = 0; i < 25; ++i)
3504
 
        {
3505
 
            unsigned id = random_minmax(0, 65535);
3506
 
            bvect_min2.set_bit(id);
3507
 
            gapv2.set_bit(id);
3508
 
            gapv2.control();
3509
 
        }
3510
 
 
3511
 
        gapv1.combine_and(gapv2.get_buf());
3512
 
        gapv1.control();
3513
 
        gapv2.control();
3514
 
        bvect_min1.combine_and(bvect_min2);
3515
 
 
3516
 
        CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
3517
 
 
3518
 
        printf("gap random AND test ok.\n");
3519
 
 
3520
 
   }
3521
 
 
3522
 
   {
3523
 
        printf("gap OR test.\n");
3524
 
 
3525
 
        gap_vector gapv1(0);
3526
 
        gap_vector gapv2(0);
3527
 
 
3528
 
        gapv1.set_bit(2);
3529
 
        gapv2.set_bit(3);
3530
 
 
3531
 
        gapv1.combine_or(gapv2);
3532
 
        gapv1.control();
3533
 
        print_gap(gapv1, 0);   
3534
 
        int bit1 = (gapv1.is_bit_true(0) == 1);
3535
 
        assert(bit1==0);
3536
 
        bit1=(gapv1.is_bit_true(2) == 1);
3537
 
        assert(bit1);
3538
 
        bit1=(gapv1.is_bit_true(3) == 1);
3539
 
        assert(bit1);
3540
 
   }
3541
 
 
3542
 
   {
3543
 
        printf("gap XOR test.\n");
3544
 
 
3545
 
        gap_vector gapv1(0);
3546
 
        gap_vector gapv2(0);
3547
 
 
3548
 
        gapv1.set_bit(2);
3549
 
        gapv2.set_bit(3);
3550
 
        gapv1.set_bit(4);
3551
 
        gapv2.set_bit(4);
3552
 
        print_gap(gapv1, 0);   
3553
 
        print_gap(gapv2, 0);   
3554
 
 
3555
 
        gapv1.combine_xor(gapv2);
3556
 
        gapv1.control();
3557
 
        print_gap(gapv1, 0);   
3558
 
        int bit1 = (gapv1.is_bit_true(0) == 0);
3559
 
        assert(bit1);
3560
 
        bit1=(gapv1.is_bit_true(2) == 1);
3561
 
        assert(bit1);
3562
 
        bit1=(gapv1.is_bit_true(3) == 1);
3563
 
        assert(bit1);
3564
 
        bit1=(gapv1.is_bit_true(4) == 0);
3565
 
        assert(bit1);
3566
 
 
3567
 
   }
3568
 
 
3569
 
 
3570
 
   {
3571
 
        int i;
3572
 
        printf("gap random OR test.\n");
3573
 
        gap_vector gapv1(0);
3574
 
        gap_vector gapv2(0);
3575
 
        bvect_mini   bvect_min1(bm::gap_max_bits);
3576
 
        bvect_mini   bvect_min2(bm::gap_max_bits);
3577
 
        
3578
 
        for (i = 0; i < 10; ++i)
3579
 
        {
3580
 
            unsigned id = random_minmax(0, 100);
3581
 
            bvect_min1.set_bit(id);
3582
 
            gapv1.set_bit(id);
3583
 
            gapv1.control();
3584
 
        }
3585
 
        for (i = 0; i < 10; ++i)
3586
 
        {
3587
 
            unsigned id = random_minmax(0, 100);
3588
 
            bvect_min2.set_bit(id);
3589
 
            gapv2.set_bit(id);
3590
 
            gapv2.control();
3591
 
        }
3592
 
 
3593
 
        print_mv(bvect_min1, 64);
3594
 
        print_mv(bvect_min2, 64);
3595
 
 
3596
 
        gapv1.combine_or(gapv2);
3597
 
        gapv1.control();
3598
 
        gapv2.control();
3599
 
        bvect_min1.combine_or(bvect_min2);
3600
 
 
3601
 
        print_mv(bvect_min1, 64);
3602
 
 
3603
 
        CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
3604
 
 
3605
 
        printf("gap random OR test ok.\n");
3606
 
 
3607
 
   }
3608
 
 
3609
 
 
3610
 
   {
3611
 
        int i;
3612
 
        printf("gap random SUB test.\n");
3613
 
        gap_vector gapv1(0);
3614
 
        gap_vector gapv2(0);
3615
 
        bvect_mini   bvect_min1(bm::gap_max_bits);
3616
 
        bvect_mini   bvect_min2(bm::gap_max_bits);
3617
 
        
3618
 
        for (i = 0; i < 25; ++i)
3619
 
        {
3620
 
            unsigned id = random_minmax(0, 100);
3621
 
            bvect_min1.set_bit(id);
3622
 
            gapv1.set_bit(id);
3623
 
            gapv1.control();
3624
 
        }
3625
 
        for (i = 0; i < 25; ++i)
3626
 
        {
3627
 
            unsigned id = random_minmax(0, 100);
3628
 
            bvect_min2.set_bit(id);
3629
 
            gapv2.set_bit(id);
3630
 
            gapv2.control();
3631
 
        }
3632
 
 
3633
 
        print_mv(bvect_min1, 64);
3634
 
        print_mv(bvect_min2, 64);
3635
 
 
3636
 
        gapv1.combine_sub(gapv2);
3637
 
        gapv1.control();
3638
 
        gapv2.control();
3639
 
        bvect_min1.combine_sub(bvect_min2);
3640
 
 
3641
 
        print_mv(bvect_min1, 64);
3642
 
 
3643
 
        CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
3644
 
 
3645
 
        printf("gap random SUB test ok.\n");
3646
 
   }
3647
 
 
3648
 
   {
3649
 
       printf("GAP comparison test.\n");
3650
 
 
3651
 
       gap_vector gapv1(0);
3652
 
       gap_vector gapv2(0);
3653
 
 
3654
 
       gapv1.set_bit(3);
3655
 
       gapv2.set_bit(3);
3656
 
 
3657
 
       int res = gapv1.compare(gapv2);
3658
 
       if (res != 0)
3659
 
       {
3660
 
           printf("GAP comparison failed!");
3661
 
           exit(1);
3662
 
       }
3663
 
 
3664
 
       gapv1.set_bit(4);
3665
 
       gapv2.set_bit(4);
3666
 
 
3667
 
       res = gapv1.compare(gapv2);
3668
 
       if (res != 0)
3669
 
       {
3670
 
           printf("GAP comparison failed!");
3671
 
           exit(1);
3672
 
       }
3673
 
 
3674
 
       gapv1.set_bit(0);
3675
 
       gapv1.set_bit(1);
3676
 
 
3677
 
       res = gapv1.compare(gapv2);
3678
 
       if (res != 1)
3679
 
       {
3680
 
           printf("GAP comparison failed!");
3681
 
           exit(1);
3682
 
       }
3683
 
 
3684
 
       gapv2.set_bit(0);
3685
 
       gapv2.set_bit(1);
3686
 
       res = gapv1.compare(gapv2);
3687
 
       if (res != 0)
3688
 
       {
3689
 
           printf("GAP comparison failed!");
3690
 
           exit(1);
3691
 
       }
3692
 
 
3693
 
       gapv1.clear_bit(1);
3694
 
 
3695
 
       res = gapv1.compare(gapv2);
3696
 
       if (res != -1)
3697
 
       {
3698
 
           printf("GAP comparison failed!");
3699
 
           exit(1);
3700
 
       }
3701
 
 
3702
 
 
3703
 
   }
3704
 
 
3705
 
 
3706
 
}
3707
 
 
3708
 
// -----------------------------------------------------------------------------
3709
 
 
3710
 
void MutationTest()
3711
 
{
3712
 
 
3713
 
    cout << "--------------------------------- MutationTest" << endl;
3714
 
 
3715
 
    bvect_mini     bvect_min(BITVECT_SIZE);
3716
 
    bvect          bvect_full;
3717
 
 
3718
 
    printf("\nMutation test.\n");
3719
 
 
3720
 
    bvect_full.set_new_blocks_strat(bm::BM_GAP);
3721
 
 
3722
 
    bvect_full.set_bit(5);
3723
 
    bvect_full.set_bit(5);
3724
 
 
3725
 
    bvect_min.set_bit(5);
3726
 
 
3727
 
    bvect_full.set_bit(65535);
3728
 
    bvect_full.set_bit(65537);
3729
 
    bvect_min.set_bit(65535);
3730
 
    bvect_min.set_bit(65537);
3731
 
 
3732
 
    bvect_min.set_bit(100000);
3733
 
    bvect_full.set_bit(100000);
3734
 
 
3735
 
bvect_full.stat();
3736
 
    // detailed vectors verification
3737
 
    ::CheckVectors(bvect_min, bvect_full, ITERATIONS, false);
3738
 
 
3739
 
    int i;
3740
 
    for (i = 5; i < 20000; i+=3)
3741
 
    {
3742
 
        bvect_min.set_bit(i);
3743
 
        bvect_full.set_bit(i);
3744
 
    }
3745
 
bvect_full.stat();
3746
 
    ::CheckVectors(bvect_min, bvect_full, ITERATIONS, false);
3747
 
 
3748
 
    for (i = 100000; i < 200000; i+=3)
3749
 
    {
3750
 
        bvect_min.set_bit(i);
3751
 
        bvect_full.set_bit(i);
3752
 
    }
3753
 
 
3754
 
    ::CheckVectors(bvect_min, bvect_full, 300000);
3755
 
 
3756
 
    // set-clear functionality
3757
 
 
3758
 
    {
3759
 
        printf("Set-clear functionality test.");
3760
 
 
3761
 
        bvect_mini     bvect_min(BITVECT_SIZE);
3762
 
        bvect          bvect_full;
3763
 
        bvect_full.set_new_blocks_strat(bm::BM_GAP);
3764
 
 
3765
 
        int i;
3766
 
        for (i = 100000; i < 100010; ++i)
3767
 
        {
3768
 
            bvect_min.set_bit(i);
3769
 
            bvect_full.set_bit(i);            
3770
 
        }
3771
 
        ::CheckVectors(bvect_min, bvect_full, 300000);
3772
 
 
3773
 
        for (i = 100000; i < 100010; ++i)
3774
 
        {
3775
 
            bvect_min.clear_bit(i);
3776
 
            bvect_full.clear_bit(i);            
3777
 
        }
3778
 
        ::CheckVectors(bvect_min, bvect_full, 300000);
3779
 
        
3780
 
        bvect_full.optimize();
3781
 
        CheckVectors(bvect_min, bvect_full, 65536);//max+10);
3782
 
    }
3783
 
 
3784
 
}
3785
 
 
3786
 
void MutationOperationsTest()
3787
 
{
3788
 
 
3789
 
   cout << "------------------------------------ MutationOperationsTest" << endl;
3790
 
 
3791
 
   printf("Mutation operations test 1.\n");
3792
 
   {
3793
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
3794
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
3795
 
    bvect        bvect_full1;
3796
 
    bvect        bvect_full2;
3797
 
 
3798
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
3799
 
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
3800
 
 
3801
 
    bvect_full1.set_bit(100);
3802
 
    bvect_min1.set_bit(100);
3803
 
 
3804
 
    int i;
3805
 
    for(i = 0; i < 10000; i+=2)
3806
 
    {
3807
 
       bvect_full2.set_bit(i);
3808
 
       bvect_min2.set_bit(i);
3809
 
    }
3810
 
    bvect_full2.stat();
3811
 
    CheckVectors(bvect_min2, bvect_full2, 65536, true);
3812
 
    
3813
 
    bvect_min1.combine_and(bvect_min2);
3814
 
    bvect_full1.bit_and(bvect_full2);
3815
 
 
3816
 
    CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
3817
 
 
3818
 
   }
3819
 
 
3820
 
   printf("Mutation operations test 2.\n");
3821
 
   {
3822
 
    unsigned delta = 65536;
3823
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
3824
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
3825
 
    bvect        bvect_full1;
3826
 
    bvect        bvect_full2;
3827
 
 
3828
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
3829
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
3830
 
 
3831
 
    int i;
3832
 
    for(i = 0; i < 1000; i+=1)
3833
 
    {
3834
 
       bvect_full1.set_bit(delta+i);
3835
 
       bvect_min1.set_bit(delta+i);
3836
 
    }
3837
 
 
3838
 
    for(i = 0; i < 100; i+=2)
3839
 
    {
3840
 
       bvect_full2.set_bit(delta+i);
3841
 
       bvect_min2.set_bit(delta+i);
3842
 
    }
3843
 
//    CheckVectors(bvect_min2, bvect_full2, 65536);
3844
 
    
3845
 
    bvect_min1.combine_and(bvect_min2);
3846
 
    bvect_full1.bit_and(bvect_full2);
3847
 
 
3848
 
    CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
3849
 
    bvect_full1.optimize();
3850
 
    CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
3851
 
 
3852
 
   }
3853
 
 
3854
 
   {
3855
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
3856
 
    bvect        bvect_full1;
3857
 
 
3858
 
    bvect_full1.set_bit(3);
3859
 
    bvect_min1.set_bit(3);
3860
 
 
3861
 
    struct bvect::statistics st;
3862
 
    bvect_full1.calc_stat(&st);
3863
 
 
3864
 
    // serialization
3865
 
 
3866
 
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
3867
 
    unsigned slen = bm::serialize(bvect_full1, sermem);
3868
 
    cout << "BVECTOR SERMEM=" << slen << endl;
3869
 
 
3870
 
 
3871
 
    bvect        bvect_full3;
3872
 
    bm::deserialize(bvect_full3, sermem);
3873
 
    bvect_full3.stat();
3874
 
    CheckVectors(bvect_min1, bvect_full3, 100, true);
3875
 
   }
3876
 
 
3877
 
 
3878
 
   printf("Mutation operations test 3.\n");
3879
 
   {
3880
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
3881
 
    bvect_mini   bvect_min2(BITVECT_SIZE);
3882
 
    bvect        bvect_full1;
3883
 
    bvect        bvect_full2;
3884
 
 
3885
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
3886
 
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
3887
 
 
3888
 
   
3889
 
    unsigned min = BITVECT_SIZE / 2 - ITERATIONS;
3890
 
    unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
3891
 
    if (max > BITVECT_SIZE) 
3892
 
        max = BITVECT_SIZE - 1;
3893
 
 
3894
 
    unsigned len = max - min;
3895
 
 
3896
 
    FillSets(&bvect_min1, &bvect_full1, min, max, 0);
3897
 
    FillSets(&bvect_min1, &bvect_full1, 0, len, 5);
3898
 
    printf("Bvect_FULL 1 STAT\n");
3899
 
    bvect_full1.stat();
3900
 
//    CheckVectors(bvect_min1, bvect_full1, max+10, false);
3901
 
    FillSets(&bvect_min2, &bvect_full2, min, max, 0);
3902
 
    FillSets(&bvect_min2, &bvect_full2, 0, len, 0);
3903
 
    printf("Bvect_FULL 2 STAT\n");
3904
 
    bvect_full2.stat();
3905
 
//    CheckVectors(bvect_min2, bvect_full2, max+10);
3906
 
    
3907
 
 
3908
 
    bvect_min1.combine_and(bvect_min2);
3909
 
    bvect_full1.bit_and(bvect_full2);
3910
 
    printf("Bvect_FULL 1 STAT after AND\n");
3911
 
    bvect_full1.stat();
3912
 
 
3913
 
    CheckVectors(bvect_min1, bvect_full1, max+10, false);
3914
 
 
3915
 
    struct bvect::statistics st;
3916
 
    bvect_full1.calc_stat(&st);
3917
 
    cout << "BVECTOR: GAP=" << st.gap_blocks << " BIT=" << st.bit_blocks 
3918
 
         << " MEM=" << st.memory_used << " SERMAX=" << st.max_serialize_mem
3919
 
         << endl;
3920
 
    cout << "MINIVECT: " << bvect_min1.mem_used() << endl;
3921
 
 
3922
 
    bvect_full1.optimize();
3923
 
    bvect_full1.stat();
3924
 
 
3925
 
    CheckVectors(bvect_min1, bvect_full1, max+10, false);
3926
 
 
3927
 
    bvect_full1.calc_stat(&st);
3928
 
    cout << "BVECTOR: GAP=" << st.gap_blocks << " BIT=" << st.bit_blocks 
3929
 
         << " MEM=" << st.memory_used << " SERMAX=" << st.max_serialize_mem
3930
 
         << endl;
3931
 
    cout << "MINIVECT: " << bvect_min1.mem_used() << endl;
3932
 
 
3933
 
 
3934
 
 
3935
 
    // serialization
3936
 
 
3937
 
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
3938
 
    unsigned slen = bm::serialize(bvect_full1, sermem);
3939
 
    cout << "BVECTOR SERMEM=" << slen << endl;
3940
 
 
3941
 
 
3942
 
    
3943
 
    bvect        bvect_full3;
3944
 
    bm::deserialize(bvect_full3, sermem);
3945
 
    bvect_full3.stat();
3946
 
    CheckVectors(bvect_min1, bvect_full3, max+10, true);
3947
 
    
3948
 
    delete [] sermem;
3949
 
    
3950
 
 
3951
 
    cout << "Copy constructor check." << endl;
3952
 
 
3953
 
 
3954
 
    {
3955
 
    bvect       bvect_full4(bvect_full3);
3956
 
    bvect_full3.stat();
3957
 
    CheckVectors(bvect_min1, bvect_full4, max+10, true);
3958
 
    }
3959
 
    
3960
 
 
3961
 
   }
3962
 
 
3963
 
}
3964
 
 
3965
 
 
3966
 
void SerializationTest()
3967
 
{
3968
 
 
3969
 
   cout << " ----------------------------------- SerializationTest" << endl;
3970
 
 
3971
 
   cout << "Serialization STEP 0" << endl;
3972
 
 
3973
 
   {
3974
 
    unsigned size = BITVECT_SIZE/6000;
3975
 
 
3976
 
 
3977
 
    bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
3978
 
    bvect*        bvect_full1= new bvect();
3979
 
    bvect*        bvect_full2= new bvect();
3980
 
 
3981
 
    bvect_full1->set_new_blocks_strat(bm::BM_BIT);
3982
 
    bvect_full2->set_new_blocks_strat(bm::BM_BIT);
3983
 
 
3984
 
    for(unsigned i = 0; i < size; ++i)
3985
 
    {
3986
 
        bvect_full1->set_bit(i);
3987
 
        bvect_min1->set_bit(i);
3988
 
    }
3989
 
 
3990
 
    bvect_full1->optimize();
3991
 
    CheckVectors(*bvect_min1, *bvect_full1, size, true);
3992
 
 
3993
 
 
3994
 
 
3995
 
    bvect::statistics st;
3996
 
    bvect_full1->calc_stat(&st);
3997
 
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
3998
 
    unsigned slen = bm::serialize(*bvect_full1, sermem);
3999
 
    cout << "Serialized mem_max = " << st.max_serialize_mem 
4000
 
         << " size= " << slen 
4001
 
         << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
4002
 
         << endl;
4003
 
 
4004
 
    bm::deserialize(*bvect_full2, sermem);
4005
 
    delete [] sermem;
4006
 
 
4007
 
 
4008
 
    CheckVectors(*bvect_min1, *bvect_full2, size, true);
4009
 
 
4010
 
 
4011
 
    delete bvect_full2;
4012
 
    delete bvect_min1;
4013
 
    delete bvect_full1;
4014
 
 
4015
 
    }
4016
 
 
4017
 
 
4018
 
   {
4019
 
    unsigned size = BITVECT_SIZE/6000;
4020
 
 
4021
 
 
4022
 
    bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
4023
 
    bvect*        bvect_full1= new bvect();
4024
 
    bvect*        bvect_full2= new bvect();
4025
 
 
4026
 
    bvect_full1->set_new_blocks_strat(bm::BM_BIT);
4027
 
    bvect_full2->set_new_blocks_strat(bm::BM_BIT);
4028
 
 
4029
 
        bvect_full1->set_bit(131072);
4030
 
        bvect_min1->set_bit(131072);
4031
 
    
4032
 
 
4033
 
    bvect_full1->optimize();
4034
 
 
4035
 
    bvect::statistics st;
4036
 
    bvect_full1->calc_stat(&st);
4037
 
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
4038
 
    unsigned slen = bm::serialize(*bvect_full1, sermem);
4039
 
    cout << "Serialized mem_max = " << st.max_serialize_mem 
4040
 
         << " size= " << slen 
4041
 
         << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
4042
 
         << endl;
4043
 
 
4044
 
    bm::deserialize(*bvect_full2, sermem);
4045
 
    delete [] sermem;
4046
 
 
4047
 
    CheckVectors(*bvect_min1, *bvect_full2, size, true);
4048
 
 
4049
 
    delete bvect_full2;
4050
 
    delete bvect_min1;
4051
 
    delete bvect_full1;
4052
 
 
4053
 
    }
4054
 
 
4055
 
 
4056
 
    cout << "Serialization STEP 1." << endl;
4057
 
 
4058
 
    {
4059
 
    bvect_mini   bvect_min1(BITVECT_SIZE);
4060
 
    bvect        bvect_full1;
4061
 
 
4062
 
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
4063
 
   
4064
 
    unsigned min = BITVECT_SIZE / 2 - ITERATIONS;
4065
 
    unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
4066
 
    if (max > BITVECT_SIZE) 
4067
 
        max = BITVECT_SIZE - 1;
4068
 
 
4069
 
    unsigned len = max - min;
4070
 
 
4071
 
    FillSets(&bvect_min1, &bvect_full1, min, max, 0);
4072
 
    FillSets(&bvect_min1, &bvect_full1, 0, len, 5);
4073
 
 
4074
 
    // shot some random bits
4075
 
 
4076
 
    int i;
4077
 
    for (i = 0; i < 10000; ++i)
4078
 
    {
4079
 
        unsigned bit = rand() % BITVECT_SIZE;
4080
 
        bvect_full1.set_bit(bit);
4081
 
        bvect_min1.set_bit(bit);
4082
 
    }
4083
 
 
4084
 
    bvect::statistics st;
4085
 
    bvect_full1.calc_stat(&st);
4086
 
 
4087
 
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
4088
 
    bvect_full1.stat();
4089
 
    
4090
 
    unsigned slen = bm::serialize(bvect_full1, sermem);
4091
 
 
4092
 
    cout << "Serialized len = " << slen << endl;
4093
 
 
4094
 
    bvect        bvect_full3;
4095
 
    bm::deserialize(bvect_full3, sermem);
4096
 
    CheckVectors(bvect_min1, bvect_full3, max+10, true);
4097
 
 
4098
 
    delete [] sermem;
4099
 
 
4100
 
    }
4101
 
 
4102
 
 
4103
 
   cout << "Stage 2" << endl;
4104
 
 
4105
 
   {
4106
 
 
4107
 
    bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
4108
 
//    bm::bvect_mini*   bvect_min2= new bm::bvect_mini(BITVECT_SIZE);
4109
 
    bvect*        bvect_full1= new bvect();
4110
 
    bvect*        bvect_full2= new bvect();
4111
 
 
4112
 
    bvect_full1->set_new_blocks_strat(bm::BM_GAP);
4113
 
    bvect_full2->set_new_blocks_strat(bm::BM_GAP);
4114
 
 
4115
 
    FillSetsRandomMethod(bvect_min1, bvect_full1, 1, BITVECT_SIZE-10, 1);
4116
 
//    FillSetsRandomMethod(bvect_min2, bvect_full2, 1, BITVECT_SIZE-10, 1);
4117
 
 
4118
 
//bvect_full1->stat();
4119
 
cout << "Filling. OK." << endl;
4120
 
    bvect::statistics st;
4121
 
    bvect_full1->calc_stat(&st);
4122
 
cout << st.max_serialize_mem << endl;
4123
 
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
4124
 
cout << "Serialization" << endl;
4125
 
    unsigned slen = bm::serialize(*bvect_full1, sermem);
4126
 
 
4127
 
    cout << "Serialized mem_max = " << st.max_serialize_mem 
4128
 
         << " size= " << slen 
4129
 
         << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
4130
 
         << endl;
4131
 
cout << "Deserialization" << endl;
4132
 
    bm::deserialize(*bvect_full2, sermem);
4133
 
cout << "Deserialization ok" << endl;
4134
 
 
4135
 
    CheckVectors(*bvect_min1, *bvect_full2, BITVECT_SIZE, true);
4136
 
 
4137
 
    delete [] sermem;
4138
 
 
4139
 
 
4140
 
    delete bvect_full2;
4141
 
    delete bvect_min1;
4142
 
    delete bvect_full1;
4143
 
 
4144
 
    }
4145
 
 
4146
 
 
4147
 
 
4148
 
   cout << "Stage 3" << endl;
4149
 
 
4150
 
   {
4151
 
 
4152
 
    bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
4153
 
    bvect_mini*   bvect_min2= new bvect_mini(BITVECT_SIZE);
4154
 
    bvect*        bvect_full1= new bvect();
4155
 
    bvect*        bvect_full2= new bvect();
4156
 
 
4157
 
    bvect_full1->set_new_blocks_strat(bm::BM_GAP);
4158
 
    bvect_full2->set_new_blocks_strat(bm::BM_GAP);
4159
 
 
4160
 
 
4161
 
    FillSetsRandomMethod(bvect_min1, bvect_full1, 1, BITVECT_SIZE-10, 1);
4162
 
    FillSetsRandomMethod(bvect_min2, bvect_full2, 1, BITVECT_SIZE-10, 1);
4163
 
 
4164
 
    bvect::statistics st;
4165
 
    bvect_full1->calc_stat(&st);
4166
 
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
4167
 
    unsigned slen = bm::serialize(*bvect_full1, sermem);
4168
 
    delete bvect_full1;
4169
 
 
4170
 
    cout << "Serialized mem_max = " << st.max_serialize_mem 
4171
 
         << " size= " << slen 
4172
 
         << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
4173
 
         << endl;
4174
 
    bm::deserialize(*bvect_full2, sermem);
4175
 
    delete [] sermem;
4176
 
    
4177
 
    bvect_min2->combine_or(*bvect_min1);
4178
 
    delete bvect_min1;
4179
 
 
4180
 
    CheckVectors(*bvect_min2, *bvect_full2, BITVECT_SIZE, true);
4181
 
 
4182
 
 
4183
 
    delete bvect_full2;
4184
 
    delete bvect_min2;    
4185
 
 
4186
 
 
4187
 
    }
4188
 
 
4189
 
   cout << "Stage 4. " << endl;
4190
 
 
4191
 
   {
4192
 
    unsigned size = BITVECT_SIZE/3;
4193
 
 
4194
 
 
4195
 
    bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
4196
 
    bvect*        bvect_full1= new bvect();
4197
 
    bvect*        bvect_full2= new bvect();
4198
 
 
4199
 
    bvect_full1->set_new_blocks_strat(bm::BM_BIT);
4200
 
    bvect_full2->set_new_blocks_strat(bm::BM_BIT);
4201
 
 
4202
 
    unsigned i;
4203
 
    for(i = 0; i < 65000; ++i)
4204
 
    {
4205
 
        bvect_full1->set_bit(i);
4206
 
        bvect_min1->set_bit(i);
4207
 
    }
4208
 
 
4209
 
    for(i = 65536; i < 65536+65000; ++i)
4210
 
    {
4211
 
        bvect_full1->set_bit(i);
4212
 
        bvect_min1->set_bit(i);
4213
 
    }
4214
 
 
4215
 
    for (i = 65536*2; i < size/6; ++i)
4216
 
    {
4217
 
        bvect_full1->set_bit(i);
4218
 
        bvect_min1->set_bit(i);
4219
 
    }
4220
 
 
4221
 
 
4222
 
    bvect_full1->optimize();
4223
 
 
4224
 
    bvect_full1->stat();
4225
 
 
4226
 
 
4227
 
    bvect::statistics st;
4228
 
    bvect_full1->calc_stat(&st);
4229
 
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
4230
 
    unsigned slen = bm::serialize(*bvect_full1, sermem);
4231
 
    cout << "Serialized mem_max = " << st.max_serialize_mem 
4232
 
         << " size= " << slen 
4233
 
         << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
4234
 
         << endl;
4235
 
    
4236
 
    unsigned char* new_sermem = new unsigned char[st.max_serialize_mem];
4237
 
    ::memcpy(new_sermem, sermem, slen);
4238
 
 
4239
 
    bm::deserialize(*bvect_full2, new_sermem);
4240
 
    delete [] sermem;
4241
 
    delete [] new_sermem;
4242
 
 
4243
 
    CheckVectors(*bvect_min1, *bvect_full2, size, true);
4244
 
 
4245
 
 
4246
 
    delete bvect_full2;
4247
 
    delete bvect_min1;
4248
 
    delete bvect_full1;
4249
 
 
4250
 
    }
4251
 
 
4252
 
 
4253
 
}
4254
 
 
4255
 
void GetNextTest()
4256
 
{
4257
 
   cout << "-------------------------------------------- GetNextTest" << endl;
4258
 
 
4259
 
   int i;
4260
 
   for(i = 0; i < 2; ++i)
4261
 
   {
4262
 
      cout << "Strategy " << i << endl;
4263
 
 
4264
 
   {
4265
 
      bvect       bvect_full1;
4266
 
      bvect_mini  bvect_min1(BITVECT_SIZE);
4267
 
 
4268
 
      bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
4269
 
 
4270
 
      bvect_full1.set_bit(0);
4271
 
      bvect_min1.set_bit(0);
4272
 
 
4273
 
 
4274
 
      bvect_full1.set_bit(65536);
4275
 
      bvect_min1.set_bit(65536);
4276
 
 
4277
 
      unsigned nbit1 = bvect_full1.get_first();
4278
 
      unsigned nbit2 = bvect_min1.get_first();
4279
 
 
4280
 
      if (nbit1 != nbit2)
4281
 
      {
4282
 
         cout << "1. get_first failed() " <<  nbit1 << " " << nbit2 << endl;
4283
 
         exit(1);
4284
 
      }
4285
 
      nbit1 = bvect_full1.get_next(nbit1);
4286
 
      nbit2 = bvect_min1.get_next(nbit2);
4287
 
      if ((nbit1 != nbit2) || (nbit1 != 65536))
4288
 
      {
4289
 
         cout << "1. get_next failed() " <<  nbit1 << " " << nbit2 << endl;
4290
 
         exit(1);
4291
 
      }
4292
 
   }
4293
 
 
4294
 
 
4295
 
 
4296
 
   {
4297
 
      bvect       bvect_full1;
4298
 
      bvect_mini  bvect_min1(BITVECT_SIZE);
4299
 
      bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
4300
 
 
4301
 
      bvect_full1.set_bit(65535);
4302
 
      bvect_min1.set_bit(65535);
4303
 
 
4304
 
      unsigned nbit1 = bvect_full1.get_first();
4305
 
      unsigned nbit2 = bvect_min1.get_first();
4306
 
 
4307
 
      if ((nbit1 != nbit2) || (nbit1 != 65535))
4308
 
      {
4309
 
         cout << "1. get_first failed() " <<  nbit1 << " " << nbit2 << endl;
4310
 
         exit(1);
4311
 
      }
4312
 
      nbit1 = bvect_full1.get_next(nbit1);
4313
 
      nbit2 = bvect_min1.get_next(nbit2);
4314
 
      if (nbit1 != nbit2 )
4315
 
      {
4316
 
         cout << "1. get_next failed() " <<  nbit1 << " " << nbit2 << endl;
4317
 
         exit(1);
4318
 
      }
4319
 
   }
4320
 
 
4321
 
   {
4322
 
      cout << "--------------" << endl;
4323
 
      bvect       bvect_full1;
4324
 
      bvect_mini  bvect_min1(BITVECT_SIZE);
4325
 
      bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
4326
 
 
4327
 
      bvect_full1.set_bit(655350);
4328
 
      bvect_min1.set_bit(655350);
4329
 
 
4330
 
      unsigned nbit1 = bvect_full1.get_first();
4331
 
      unsigned nbit2 = bvect_min1.get_first();
4332
 
 
4333
 
      if (nbit1 != nbit2 || nbit1 != 655350)
4334
 
      {
4335
 
         cout << "1. get_first failed() " <<  nbit1 << " " << nbit2 << endl;
4336
 
         exit(1);
4337
 
      }
4338
 
 
4339
 
      nbit1 = bvect_full1.get_next(nbit1);
4340
 
      nbit2 = bvect_min1.get_next(nbit2);
4341
 
      if (nbit1 != nbit2)
4342
 
      {
4343
 
         cout << "1. get_next failed() " <<  nbit1 << " " << nbit2 << endl;
4344
 
         exit(1);
4345
 
      }
4346
 
   }
4347
 
 
4348
 
 
4349
 
   {
4350
 
   bvect       bvect_full1;
4351
 
   bvect_mini  bvect_min1(BITVECT_SIZE);
4352
 
 
4353
 
   bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
4354
 
 
4355
 
   bvect_full1.set_bit(256);
4356
 
   bvect_min1.set_bit(256);
4357
 
 
4358
 
//   bvect_full1.clear_bit(256);
4359
 
   bvect_full1.set_bit(65536);
4360
 
   bvect_min1.set_bit(65536);
4361
 
 
4362
 
   unsigned nbit1 = bvect_full1.get_first();
4363
 
   unsigned nbit2 = bvect_min1.get_first();
4364
 
 
4365
 
   if (nbit1 != nbit2)
4366
 
   {
4367
 
      cout << "get_first failed " <<  nbit1 << " " << nbit2 << endl;
4368
 
      exit(1);
4369
 
   }
4370
 
 
4371
 
   while (nbit1)
4372
 
   {
4373
 
      cout << nbit1 << endl;
4374
 
      nbit1 = bvect_full1.get_next(nbit1);
4375
 
      nbit2 = bvect_min1.get_next(nbit2);
4376
 
      if (nbit1 != nbit2)
4377
 
      {
4378
 
         cout << "get_next failed " <<  nbit1 << " " << nbit2 << endl;
4379
 
         exit(1);
4380
 
      }
4381
 
 
4382
 
   } // while
4383
 
 
4384
 
   }
4385
 
 
4386
 
   
4387
 
   }// for
4388
 
 
4389
 
}
4390
 
 
4391
 
// Test contributed by Maxim Shemanarev.
4392
 
 
4393
 
void MaxSTest()
4394
 
{
4395
 
   bvect vec;
4396
 
 
4397
 
   int i, j;
4398
 
   unsigned id;
4399
 
   for(i = 0; i < 100; i++)
4400
 
   {
4401
 
      int n = rand() % 2000 + 1;
4402
 
      id = 1;
4403
 
      for(j = 0; j < n; j++)
4404
 
      {
4405
 
         id += rand() % 10 + 1;
4406
 
         vec.set_bit(id);
4407
 
 
4408
 
      }
4409
 
      vec.optimize();
4410
 
      vec.clear();
4411
 
      fprintf(stderr, ".");
4412
 
   }
4413
 
}
4414
 
 
4415
 
 
4416
 
void CalcBeginMask()
4417
 
{
4418
 
    printf("BeginMask:\n");
4419
 
 
4420
 
    int i;
4421
 
    for (i = 0; i < 32; ++i)
4422
 
    {
4423
 
    unsigned mask = 0;
4424
 
 
4425
 
        for(int j = i; j < 32; ++j)
4426
 
        {
4427
 
            unsigned nbit  = j; 
4428
 
            nbit &= bm::set_word_mask;
4429
 
            bm::word_t  mask1 = (((bm::word_t)1) << j);
4430
 
 
4431
 
            mask |= mask1;
4432
 
        }
4433
 
 
4434
 
        printf("0x%x, ", mask);
4435
 
        
4436
 
    } 
4437
 
    printf("\n");
4438
 
}
4439
 
 
4440
 
void CalcEndMask()
4441
 
{
4442
 
    printf("EndMask:\n");
4443
 
 
4444
 
    int i;
4445
 
    for (i = 0; i < 32; ++i)
4446
 
    {
4447
 
    unsigned mask = 1;
4448
 
 
4449
 
        for(int j = i; j > 0; --j)
4450
 
        {
4451
 
            unsigned nbit  = j; 
4452
 
            nbit &= bm::set_word_mask;
4453
 
            bm::word_t  mask1 = (((bm::word_t)1) << j);
4454
 
 
4455
 
            mask |= mask1;
4456
 
        }
4457
 
 
4458
 
        printf("0x%x,", mask);
4459
 
        
4460
 
    } 
4461
 
    printf("\n");
4462
 
}
4463
 
 
4464
 
 
4465
 
void EnumeratorTest()
4466
 
{
4467
 
    cout << "-------------------------------------------- EnumeratorTest" << endl;
4468
 
 
4469
 
    {
4470
 
    bvect bvect1;
4471
 
 
4472
 
    bvect1.set_bit(100);
4473
 
 
4474
 
    bvect::enumerator en = bvect1.first();
4475
 
 
4476
 
    if (*en != 100)
4477
 
    {
4478
 
        cout << "Enumerator error !" << endl;
4479
 
        exit(1);
4480
 
    }
4481
 
 
4482
 
    bvect1.clear_bit(100);
4483
 
 
4484
 
    bvect1.set_bit(2000000000);
4485
 
    en.go_first();
4486
 
 
4487
 
    if (*en != 2000000000)
4488
 
    {
4489
 
        cout << "Enumerator error !" << endl;
4490
 
        exit(1);
4491
 
    }
4492
 
    }
4493
 
 
4494
 
    {
4495
 
        bvect bvect1;
4496
 
        bvect1.set_bit(0);
4497
 
        bvect1.set_bit(10);
4498
 
        bvect1.set_bit(35);
4499
 
        bvect1.set_bit(1000);
4500
 
        bvect1.set_bit(2016519);
4501
 
        bvect1.set_bit(2034779);
4502
 
        bvect1.set_bit(bm::id_max-1);
4503
 
 
4504
 
        bvect::enumerator en = bvect1.first();
4505
 
 
4506
 
        unsigned num = bvect1.get_first();
4507
 
 
4508
 
        bvect::enumerator end = bvect1.end();
4509
 
        while (en < end)
4510
 
        {
4511
 
            if (*en != num)
4512
 
            {
4513
 
                cout << "Enumeration comparison failed !" << 
4514
 
                        " enumerator = " << *en <<
4515
 
                        " get_next() = " << num << endl; 
4516
 
                exit(1);
4517
 
            }
4518
 
            ++en;
4519
 
            num = bvect1.get_next(num);
4520
 
        }
4521
 
        if (num != 0)
4522
 
        {
4523
 
            cout << "Enumeration error!" << endl;
4524
 
            exit(1);
4525
 
        }
4526
 
    }
4527
 
/*
4528
 
    {
4529
 
        bvect bvect1;
4530
 
        bvect1.set();
4531
 
 
4532
 
        bvect::enumerator en = bvect1.first();
4533
 
 
4534
 
        unsigned num = bvect1.get_first();
4535
 
 
4536
 
        while (en < bvect1.end())
4537
 
        {
4538
 
            if (*en != num)
4539
 
            {
4540
 
                cout << "Enumeration comparison failed !" << 
4541
 
                        " enumerator = " << *en <<
4542
 
                        " get_next() = " << num << endl; 
4543
 
                exit(1);
4544
 
            }
4545
 
 
4546
 
            ++en;
4547
 
            num = bvect1.get_next(num);
4548
 
        }
4549
 
        if (num != 0)
4550
 
        {
4551
 
            cout << "Enumeration error!" << endl;
4552
 
            exit(1);
4553
 
        }
4554
 
    }
4555
 
*/
4556
 
 
4557
 
    {
4558
 
        bvect bvect1;
4559
 
 
4560
 
        int i;
4561
 
        for(i = 0; i < 65536; ++i)
4562
 
        {
4563
 
            bvect1.set_bit(i);
4564
 
        }
4565
 
/*
4566
 
        for(i = 65536*10; i < 65536*20; i+=3)
4567
 
        {
4568
 
            bvect1.set_bit(i);
4569
 
        }
4570
 
*/
4571
 
 
4572
 
        bvect::enumerator en = bvect1.first();
4573
 
 
4574
 
        unsigned num = bvect1.get_first();
4575
 
 
4576
 
        while (en < bvect1.end())
4577
 
        {
4578
 
            if (*en != num)
4579
 
            {
4580
 
                cout << "Enumeration comparison failed !" << 
4581
 
                        " enumerator = " << *en <<
4582
 
                        " get_next() = " << num << endl; 
4583
 
                exit(1);
4584
 
            }
4585
 
            ++en;
4586
 
            num = bvect1.get_next(num);
4587
 
            if (num == 31)
4588
 
            {
4589
 
                num = num + 0;
4590
 
            }
4591
 
        }
4592
 
        if (num != 0)
4593
 
        {
4594
 
            cout << "Enumeration error!" << endl;
4595
 
            exit(1);
4596
 
        }
4597
 
    }
4598
 
 
4599
 
 
4600
 
    {
4601
 
    bvect bvect1;
4602
 
    bvect1.set_new_blocks_strat(bm::BM_GAP);
4603
 
    bvect1.set_bit(100);
4604
 
 
4605
 
    bvect::enumerator en = bvect1.first();
4606
 
 
4607
 
    if (*en != 100)
4608
 
    {
4609
 
        cout << "Enumerator error !" << endl;
4610
 
        exit(1);
4611
 
    }
4612
 
 
4613
 
    bvect1.clear_bit(100);
4614
 
 
4615
 
    bvect1.set_bit(2000000);
4616
 
    en.go_first();
4617
 
 
4618
 
    if (*en != 2000000)
4619
 
    {
4620
 
        cout << "Enumerator error !" << endl;
4621
 
        exit(1);
4622
 
    }
4623
 
    bvect1.stat();
4624
 
    }
4625
 
 
4626
 
    {
4627
 
        bvect bvect1;
4628
 
        bvect1.set_new_blocks_strat(bm::BM_GAP);
4629
 
        bvect1.set_bit(0);
4630
 
        bvect1.set_bit(1);
4631
 
        bvect1.set_bit(10);
4632
 
        bvect1.set_bit(100);
4633
 
        bvect1.set_bit(1000);
4634
 
 
4635
 
        bvect::enumerator en = bvect1.first();
4636
 
 
4637
 
        unsigned num = bvect1.get_first();
4638
 
 
4639
 
        while (en < bvect1.end())
4640
 
        {
4641
 
            if (*en != num)
4642
 
            {
4643
 
                cout << "Enumeration comparison failed !" << 
4644
 
                        " enumerator = " << *en <<
4645
 
                        " get_next() = " << num << endl; 
4646
 
                exit(1);
4647
 
            }
4648
 
            ++en;
4649
 
            num = bvect1.get_next(num);
4650
 
        }
4651
 
        if (num != 0)
4652
 
        {
4653
 
            cout << "Enumeration error!" << endl;
4654
 
            exit(1);
4655
 
        }
4656
 
    }
4657
 
 
4658
 
 
4659
 
}
4660
 
 
4661
 
 
4662
 
 
4663
 
void BlockLevelTest()
4664
 
{
4665
 
    bvect  bv;
4666
 
    bvect  bv2;
4667
 
 
4668
 
    bv.set_new_blocks_strat(bm::BM_GAP);
4669
 
    bv2.set_new_blocks_strat(bm::BM_GAP);
4670
 
 
4671
 
    int i;
4672
 
    for (i = 0; i < 500; i+=1)
4673
 
    {
4674
 
        bv.set_bit(i);
4675
 
    }
4676
 
    bv.stat();
4677
 
 
4678
 
    for (i = 0; i < 1000; i+=2)
4679
 
    {
4680
 
        bv2.set_bit(i);
4681
 
    }
4682
 
    bv2.stat();
4683
 
 
4684
 
    struct bvect::statistics st;
4685
 
    bv2.calc_stat(&st);
4686
 
 
4687
 
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
4688
 
 
4689
 
    unsigned slen = bm::serialize(bv2, sermem);
4690
 
    assert(slen);
4691
 
    slen = 0;
4692
 
    
4693
 
    bm::deserialize(bv, sermem);
4694
 
//    bv.optimize();
4695
 
 
4696
 
    bv.stat();
4697
 
 
4698
 
}
4699
 
 
4700
 
/*
4701
 
__int64 CalcBitCount64(__int64 b)
4702
 
{
4703
 
    b = (b & 0x5555555555555555) + (b >> 1 & 0x5555555555555555);
4704
 
    b = (b & 0x3333333333333333) + (b >> 2 & 0x3333333333333333);
4705
 
    b = b + (b >> 4) & 0x0F0F0F0F0F0F0F0F;
4706
 
    b = b + (b >> 8);
4707
 
    b = b + (b >> 16);
4708
 
    b = b + (b >> 32) & 0x0000007F;
4709
 
    return b;
4710
 
}
4711
 
 
4712
 
unsigned CalcBitCount32(unsigned b)
4713
 
{
4714
 
    b = (b & 0x55555555) + (b >> 1 & 0x55555555);
4715
 
    b = (b & 0x33333333) + (b >> 2 & 0x33333333);
4716
 
    b = b + (b >> 4) & 0x0F0F0F0F;
4717
 
    b = b + (b >> 8);
4718
 
    b = b + (b >> 16) & 0x0000003F;
4719
 
    return b;
4720
 
}
4721
 
*/
4722
 
 
4723
 
 
4724
 
void SyntaxTest()
4725
 
{
4726
 
    cout << "----------------------------- Syntax test." << endl;
4727
 
    bvect bv1;
4728
 
    
4729
 
    bvect::allocator_type a = bv1.get_allocator();
4730
 
 
4731
 
    bvect bv2(bv1);
4732
 
    bvect bv3;
4733
 
    bv3.swap(bv1);
4734
 
     
4735
 
    bv1[100] = true;
4736
 
    bool v = bv1[100];
4737
 
    assert(v);
4738
 
    v = false;
4739
 
 
4740
 
    bv1[100] = false;
4741
 
 
4742
 
    bv2 |= bv1;
4743
 
    bv2 &= bv1;
4744
 
    bv2 ^= bv1;
4745
 
    bv2 -= bv1;
4746
 
 
4747
 
    bv3 = bv1 | bv2;
4748
 
 
4749
 
    if (bv1 < bv2)
4750
 
    {
4751
 
    }
4752
 
 
4753
 
    bvect::reference ref = bv1[10];
4754
 
    bool bn = !ref;
4755
 
    bool bn2 = ~ref;
4756
 
 
4757
 
    bn = bn2 = false;
4758
 
 
4759
 
    ref.flip();
4760
 
 
4761
 
    bvect bvn = ~bv1;
4762
 
 
4763
 
    cout << "----------------------------- Syntax test ok." << endl;
4764
 
}
4765
 
 
4766
 
 
4767
 
void SetTest()
4768
 
{
4769
 
    unsigned cnt;
4770
 
    bvect bv;
4771
 
 
4772
 
    bv.set();
4773
 
 
4774
 
    cnt = bv.count();
4775
 
    if (cnt != bm::id_max)
4776
 
    {
4777
 
        cout << "Set test failed!." << endl;
4778
 
        exit(1);
4779
 
    }
4780
 
 
4781
 
    bv.invert();
4782
 
    cnt = bv.count();
4783
 
    if (cnt != 0)
4784
 
    {
4785
 
        cout << "Set invert test failed!." << endl;
4786
 
        exit(1);
4787
 
    }
4788
 
 
4789
 
    bv.set(0);
4790
 
    bv.set(bm::id_max-1);
4791
 
    cnt = bv.count();
4792
 
 
4793
 
    assert(cnt == 2);
4794
 
 
4795
 
    bv.invert();
4796
 
    bv.stat();
4797
 
    cnt = bv.count();
4798
 
 
4799
 
    if (cnt != bm::id_max-2)
4800
 
    {
4801
 
        cout << "Set invert test failed!." << endl;
4802
 
        exit(1);
4803
 
    }
4804
 
}
4805
 
 
4806
 
 
4807
 
template<class A, class B> void CompareMiniSet(const A& ms,
4808
 
                                          const B& bvm)
4809
 
{
4810
 
    for (unsigned i = 0; i < bm::set_total_blocks; ++i)
4811
 
    {
4812
 
        bool ms_val = ms.test(i)!=0;
4813
 
        bool bvm_val = bvm.is_bit_true(i)!=0;
4814
 
        if (ms_val != bvm_val)
4815
 
        {
4816
 
            printf("MiniSet comparison error: %u\n",i);
4817
 
            exit(1);
4818
 
        }
4819
 
    }
4820
 
}
4821
 
 
4822
 
void MiniSetTest()
4823
 
{
4824
 
    cout << "----------------------- MiniSetTest" << endl;
4825
 
    {
4826
 
    bm::miniset<bm::block_allocator, bm::set_total_blocks> ms;
4827
 
    bvect_mini bvm(bm::set_total_blocks);
4828
 
 
4829
 
 
4830
 
    CompareMiniSet(ms, bvm);
4831
 
 
4832
 
 
4833
 
    ms.set(1);
4834
 
    bvm.set_bit(1);
4835
 
 
4836
 
    CompareMiniSet(ms, bvm);
4837
 
 
4838
 
    unsigned i;
4839
 
 
4840
 
    for (i = 1; i < 10; i++)
4841
 
    {
4842
 
        ms.set(i);
4843
 
        bvm.set_bit(i);
4844
 
    }
4845
 
    CompareMiniSet(ms, bvm);
4846
 
 
4847
 
    for (i = 1; i < 10; i++)
4848
 
    {
4849
 
        ms.set(i, false);
4850
 
        bvm.clear_bit(i);
4851
 
    }
4852
 
    CompareMiniSet(ms, bvm);
4853
 
 
4854
 
 
4855
 
    for (i = 1; i < 10; i+=3)
4856
 
    {
4857
 
        ms.set(i);
4858
 
        bvm.set_bit(i);
4859
 
    }
4860
 
    CompareMiniSet(ms, bvm);
4861
 
 
4862
 
    for (i = 1; i < 5; i+=3)
4863
 
    {
4864
 
        ms.set(i, false);
4865
 
        bvm.clear_bit(i);
4866
 
    }
4867
 
    CompareMiniSet(ms, bvm);
4868
 
    }
4869
 
 
4870
 
 
4871
 
    {
4872
 
    bm::miniset<bm::block_allocator, bm::set_total_blocks> ms;
4873
 
    bvect_mini bvm(bm::set_total_blocks);
4874
 
 
4875
 
 
4876
 
    ms.set(1);
4877
 
    bvm.set_bit(1);
4878
 
 
4879
 
    CompareMiniSet(ms, bvm);
4880
 
 
4881
 
    unsigned i;
4882
 
    for (i = 1; i < bm::set_total_blocks; i+=3)
4883
 
    {
4884
 
        ms.set(i);
4885
 
        bvm.set_bit(i);
4886
 
    }
4887
 
    CompareMiniSet(ms, bvm);
4888
 
 
4889
 
    for (i = 1; i < bm::set_total_blocks/2; i+=3)
4890
 
    {
4891
 
        ms.set(i, false);
4892
 
        bvm.clear_bit(i);
4893
 
    }
4894
 
    CompareMiniSet(ms, bvm);
4895
 
    }
4896
 
 
4897
 
 
4898
 
    {
4899
 
    bm::bvmini<bm::set_total_blocks> ms(0);
4900
 
    bvect_mini bvm(bm::set_total_blocks);
4901
 
 
4902
 
 
4903
 
    CompareMiniSet(ms, bvm);
4904
 
 
4905
 
 
4906
 
    ms.set(1);
4907
 
    bvm.set_bit(1);
4908
 
 
4909
 
    CompareMiniSet(ms, bvm);
4910
 
 
4911
 
    unsigned i;
4912
 
 
4913
 
    for (i = 1; i < 10; i++)
4914
 
    {
4915
 
        ms.set(i);
4916
 
        bvm.set_bit(i);
4917
 
    }
4918
 
    CompareMiniSet(ms, bvm);
4919
 
 
4920
 
    for (i = 1; i < 10; i++)
4921
 
    {
4922
 
        ms.set(i, false);
4923
 
        bvm.clear_bit(i);
4924
 
    }
4925
 
    CompareMiniSet(ms, bvm);
4926
 
 
4927
 
 
4928
 
    for (i = 1; i < bm::set_total_blocks; i+=3)
4929
 
    {
4930
 
        ms.set(i);
4931
 
        bvm.set_bit(i);
4932
 
    }
4933
 
    CompareMiniSet(ms, bvm);
4934
 
 
4935
 
    for (i = 1; i < bm::set_total_blocks/2; i+=3)
4936
 
    {
4937
 
        ms.set(i, false);
4938
 
        bvm.clear_bit(i);
4939
 
    }
4940
 
    CompareMiniSet(ms, bvm);
4941
 
    }
4942
 
 
4943
 
 
4944
 
    {
4945
 
    bm::miniset<bm::block_allocator, bm::set_total_blocks> ms;
4946
 
    bvect_mini bvm(bm::set_total_blocks);
4947
 
 
4948
 
 
4949
 
    ms.set(1);
4950
 
    bvm.set_bit(1);
4951
 
 
4952
 
    CompareMiniSet(ms, bvm);
4953
 
 
4954
 
    unsigned i;
4955
 
    for (i = 1; i < 15; i+=3)
4956
 
    {
4957
 
        ms.set(i);
4958
 
        bvm.set_bit(i);
4959
 
    }
4960
 
    CompareMiniSet(ms, bvm);
4961
 
 
4962
 
    for (i = 1; i < 7; i+=3)
4963
 
    {
4964
 
        ms.set(i, false);
4965
 
        bvm.clear_bit(i);
4966
 
    }
4967
 
    CompareMiniSet(ms, bvm);
4968
 
    }
4969
 
 
4970
 
 
4971
 
    cout << "----------------------- MiniSetTest ok" << endl;
4972
 
}
4973
 
 
4974
 
 
4975
 
unsigned CalcBitCount32(unsigned b)
4976
 
{
4977
 
    b = (b & 0x55555555) + (b >> 1 & 0x55555555);
4978
 
    b = (b & 0x33333333) + (b >> 2 & 0x33333333);
4979
 
    b = b + (b >> 4) & 0x0F0F0F0F;
4980
 
    b = b + (b >> 8);
4981
 
    b = b + (b >> 16) & 0x0000003F;
4982
 
    return b;
4983
 
}
4984
 
 
4985
 
 
4986
 
void PrintGapLevels(const gap_word_t* glevel)
4987
 
{
4988
 
    cout << "Gap levels:" << endl;
4989
 
    unsigned i;
4990
 
    for (i = 0; i < bm::gap_levels; ++i)
4991
 
    {
4992
 
        cout << glevel[i] << ",";
4993
 
    }
4994
 
    cout << endl;
4995
 
}
4996
 
 
4997
 
void OptimGAPTest()
4998
 
{
4999
 
    gap_word_t    glevel[bm::gap_levels];
5000
 
    ::memcpy(glevel, gap_len_table<true>::_len, bm::gap_levels * sizeof(gap_word_t));
5001
 
 
5002
 
    {
5003
 
    gap_word_t  length[] = { 2, 2, 5, 5, 10, 11, 12 };
5004
 
    unsigned lsize = sizeof(length) / sizeof(gap_word_t);
5005
 
 
5006
 
    bm::improve_gap_levels(length, length + lsize, glevel);
5007
 
 
5008
 
    PrintGapLevels(glevel);
5009
 
    }
5010
 
 
5011
 
    {
5012
 
    gap_word_t  length[] = { 3, 5, 15, 15, 100, 110, 120 };
5013
 
    unsigned lsize = sizeof(length) / sizeof(gap_word_t);
5014
 
 
5015
 
    bm::improve_gap_levels(length, length + lsize, glevel);
5016
 
    PrintGapLevels(glevel);
5017
 
    }
5018
 
 
5019
 
    {
5020
 
    gap_word_t  length[] = { 15, 80, 5, 3, 100, 110, 95 };
5021
 
    unsigned lsize = sizeof(length) / sizeof(gap_word_t);
5022
 
 
5023
 
    bm::improve_gap_levels(length, length + lsize, glevel);
5024
 
    PrintGapLevels(glevel);
5025
 
    }
5026
 
 
5027
 
    {
5028
 
    gap_word_t  length[] = 
5029
 
    { 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,
5030
 
      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,
5031
 
      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,
5032
 
      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,
5033
 
      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,
5034
 
      8,12,10,10,14,14,14,8,4,4,10,10,4,10,4,8,6,52,104,584,218
5035
 
    };
5036
 
    unsigned lsize = sizeof(length) / sizeof(gap_word_t);
5037
 
 
5038
 
    bm::improve_gap_levels(length, length + lsize, glevel);
5039
 
    PrintGapLevels(glevel);
5040
 
    }
5041
 
 
5042
 
    {
5043
 
    gap_word_t  length[] = {
5044
 
     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
5045
 
    ,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
5046
 
    ,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
5047
 
    ,8,10,6,4,6 
5048
 
    };
5049
 
    unsigned lsize = sizeof(length) / sizeof(gap_word_t);
5050
 
 
5051
 
    bm::improve_gap_levels(length, length + lsize, glevel);
5052
 
    PrintGapLevels(glevel);
5053
 
 
5054
 
    }
5055
 
 
5056
 
}
5057
 
 
5058
 
void BitCountChangeTest()
5059
 
{
5060
 
    cout << "---------------------------- BitCountChangeTest " << endl;
5061
 
 
5062
 
    unsigned i;
5063
 
    for(i = 0xFFFFFFFF; i; i <<= 1) 
5064
 
    { 
5065
 
        unsigned a0 = bm::bit_count_change(i);
5066
 
        unsigned a1 = BitCountChange(i);
5067
 
        
5068
 
        if (a0 != a1)
5069
 
        {
5070
 
            cout << hex 
5071
 
                 << "Bit count change test failed!" 
5072
 
                 << " i = " << i << " return = " 
5073
 
                 << a0 << " check = " << a1
5074
 
                 << endl;
5075
 
            exit(1);
5076
 
        }
5077
 
    }
5078
 
 
5079
 
    cout << "---------------------------- STEP 2 " << endl;
5080
 
 
5081
 
    for(i = 0xFFFFFFFF; i; i >>= 1) 
5082
 
    { 
5083
 
        unsigned a0 = bm::bit_count_change(i);
5084
 
        unsigned a1 = BitCountChange(i);
5085
 
        
5086
 
        if (a0 != a1)
5087
 
        {
5088
 
            cout << "Bit count change test failed!" 
5089
 
                 << " i = " << i << " return = " 
5090
 
                 << a0 << " check = " << a1
5091
 
                 << endl;
5092
 
            exit(1);
5093
 
        }
5094
 
    }
5095
 
 
5096
 
    cout << "---------------------------- STEP 3 " << endl;
5097
 
 
5098
 
    for (i = 0; i < 0xFFFFFFF; ++i)
5099
 
    {
5100
 
        unsigned a0 = bm::bit_count_change(i);
5101
 
        unsigned a1 = BitCountChange(i);
5102
 
        
5103
 
        if (a0 != a1)
5104
 
        {
5105
 
            cout << "Bit count change test failed!" 
5106
 
                 << " i = " << i << " return = " 
5107
 
                 << a0 << " check = " << a1
5108
 
                 << endl;
5109
 
            exit(1);
5110
 
        }
5111
 
    }
5112
 
   
5113
 
 
5114
 
    bm::word_t   arr[16] = {0,};
5115
 
    arr[0] = (bm::word_t)(1 << 31);
5116
 
    arr[1] = 1; //(bm::word_t)(1 << 31);
5117
 
    
5118
 
    bm::id_t cnt;
5119
 
    
5120
 
    cnt = bm::bit_count_change(arr[1]);
5121
 
    cout << cnt << endl;
5122
 
    if (cnt != 2)
5123
 
    {
5124
 
        cout << "0.count_change() failed " << cnt << endl;
5125
 
        exit(1);
5126
 
    }
5127
 
    
5128
 
    cnt = bm::bit_block_calc_count_change(arr, arr+4);
5129
 
    
5130
 
    if (cnt != 3)
5131
 
    {
5132
 
        cout << "1.count_intervals() failed " << cnt << endl;
5133
 
        exit(1);
5134
 
    }
5135
 
 
5136
 
    arr[0] = arr[1] = arr[2] = 0xFFFFFFFF;
5137
 
    arr[3] = (bm::word_t)(0xFFFFFFFF >> 1);
5138
 
    
5139
 
    cnt = bm::bit_block_calc_count_change(arr, arr+4);
5140
 
    cout << cnt << endl;
5141
 
    
5142
 
    if (cnt != 2)
5143
 
    {
5144
 
        cout << "1.1 count_intervals() failed " << cnt << endl;
5145
 
        exit(1);
5146
 
    }
5147
 
    
5148
 
 
5149
 
    cout << "---------------------------- STEP 4 " << endl;
5150
 
 
5151
 
    bvect   bv1;
5152
 
    cnt = bm::count_intervals(bv1);
5153
 
    
5154
 
    if (cnt != 1)
5155
 
    {
5156
 
        cout << "1.count_intervals() failed " << cnt << endl;
5157
 
        exit(1);
5158
 
    }
5159
 
    CheckIntervals(bv1, 65536);
5160
 
    
5161
 
    bv1.invert();
5162
 
 
5163
 
    cnt = count_intervals(bv1);
5164
 
    cout << "Inverted cnt=" << cnt << endl;
5165
 
    
5166
 
    if (cnt != 2)
5167
 
    {
5168
 
        cout << "2.inverted count_intervals() failed " << cnt << endl;
5169
 
        exit(1);
5170
 
    }
5171
 
    
5172
 
    bv1.invert();
5173
 
        
5174
 
    for (i = 10; i < 100000; ++i)
5175
 
    {
5176
 
        bv1.set(i);
5177
 
    }
5178
 
    
5179
 
    cnt = count_intervals(bv1);
5180
 
    
5181
 
    if (cnt != 3)
5182
 
    {
5183
 
        cout << "3.count_intervals() failed " << cnt << endl;
5184
 
        exit(1);
5185
 
    }
5186
 
    cout << "-----" << endl;
5187
 
    CheckIntervals(bv1, 65536*2);
5188
 
    cout << "Optmization..." << endl; 
5189
 
    bv1.optimize();
5190
 
    cnt = count_intervals(bv1);
5191
 
    
5192
 
    if (cnt != 3)
5193
 
    {
5194
 
        cout << "4.count_intervals() failed " << cnt << endl;
5195
 
        exit(1);
5196
 
    }
5197
 
    
5198
 
    CheckIntervals(bv1, 65536*2);
5199
 
 
5200
 
    cout << "---------------------------- BitCountChangeTest Ok." << endl;
5201
 
}
5202
 
 
5203
 
void ResizeTest()
5204
 
{
5205
 
    {{
5206
 
    bvect bv(0);
5207
 
    assert(bv.any() == false);
5208
 
    assert(bv.count() == 0);
5209
 
    }}
5210
 
 
5211
 
    {{
5212
 
    bvect bv1(10);
5213
 
    bvect bv2(bv1);
5214
 
    assert(bv1.size() == bv2.size());
5215
 
    }}
5216
 
 
5217
 
    {{
5218
 
    bvect bv(10);
5219
 
    assert(bv.any() == false);
5220
 
    assert(bv.count() == 0);
5221
 
    bv.invert();
5222
 
    unsigned cnt = bv.count();
5223
 
    assert(cnt == 10);
5224
 
    }}
5225
 
 
5226
 
    {{
5227
 
    bvect bv1(10);
5228
 
    bv1.set(1);
5229
 
    bvect bv2(0);
5230
 
 
5231
 
    assert(bv1.size() == 10);
5232
 
    assert(bv2.size() == 0);
5233
 
    assert(bv1.count() == 1);
5234
 
    assert(bv2.count() == 0);
5235
 
    
5236
 
    bv1.swap(bv2);
5237
 
 
5238
 
    assert(bv2.size() == 10);
5239
 
    assert(bv2.count() == 1);
5240
 
    assert(bv1.size() == 0);
5241
 
    assert(bv1.count() == 0);
5242
 
 
5243
 
    }}
5244
 
 
5245
 
    {{
5246
 
    bvect bv1;
5247
 
    bv1.set(65536);
5248
 
    bv1.set(100);
5249
 
    assert(bv1.size() == bm::id_max);
5250
 
    assert(bv1.count() == 2);
5251
 
    bv1.resize(101);
5252
 
    assert(bv1.size() == 101);
5253
 
    assert(bv1.count() == 1);
5254
 
    {{
5255
 
    bm::id_t f = bv1.get_first();
5256
 
    assert(f == 100);
5257
 
    f = bv1.get_next(f);
5258
 
    assert(f == 0);
5259
 
    }}
5260
 
 
5261
 
    bv1.resize(10);
5262
 
    assert(bv1.size() == 10);
5263
 
    assert(bv1.count() == 0);
5264
 
    bm::id_t f = bv1.get_first();
5265
 
    assert(f == 0);
5266
 
    }}
5267
 
 
5268
 
    {{
5269
 
    bvect bv;
5270
 
    bv.stat();
5271
 
    bv.set(100);
5272
 
    bv.set(65536 + 10);
5273
 
    bv.stat();
5274
 
    bv.set_range(0, 65536*10, false);
5275
 
    bv.stat();
5276
 
    }}
5277
 
 
5278
 
    // test logical operations
5279
 
 
5280
 
    {{
5281
 
    bvect bv1(65536 * 10);
5282
 
    bvect bv2(65536 * 100);
5283
 
    bv1.set(5);
5284
 
    bv2.set(5);
5285
 
    bv2.set(65536 * 2);
5286
 
    bv2 &= bv1;
5287
 
    assert(bv2.size() == 65536 * 100);
5288
 
    assert(bv2.count() == 1);
5289
 
    assert(bv2.get_first() == 5);
5290
 
    }}
5291
 
 
5292
 
    {{
5293
 
    bvect bv1(10);
5294
 
    bvect bv2;
5295
 
    bv1.set(5);
5296
 
    bv2.set(5);
5297
 
    bv2.set(65536 * 2);
5298
 
    bv1 &= bv2;
5299
 
    assert(bv1.size() == bv2.size());
5300
 
    assert(bv1.count() == 1);
5301
 
    assert(bv1.get_first() == 5);
5302
 
    }}
5303
 
 
5304
 
    {{
5305
 
    bvect bv1(10);
5306
 
    bvect bv2;
5307
 
    bv1.set(5);
5308
 
    bv2.set(6);
5309
 
    bv2.set(65536 * 2);
5310
 
    bv1 |= bv2;
5311
 
    assert(bv1.size() == bv2.size());
5312
 
    assert(bv1.count() == 3);
5313
 
    }}
5314
 
 
5315
 
    // comparison test
5316
 
 
5317
 
    {{
5318
 
    int cmp;
5319
 
    bvect bv1(10);
5320
 
    bvect bv2;
5321
 
    bv2.set(65536 * 2);
5322
 
 
5323
 
    cmp = bv1.compare(bv2);
5324
 
    assert(cmp < 0);
5325
 
 
5326
 
    bv1.set(5);
5327
 
    assert(cmp < 0);
5328
 
    cmp = bv1.compare(bv2);
5329
 
    assert(cmp > 0);
5330
 
    cmp = bv2.compare(bv1);
5331
 
    assert(cmp < 0);
5332
 
 
5333
 
    }}
5334
 
 
5335
 
    // inserter
5336
 
 
5337
 
    {{
5338
 
    bvect bv1(10);
5339
 
    {
5340
 
        bvect::insert_iterator it(bv1);
5341
 
        *it = 100 * 65536;
5342
 
    }
5343
 
    assert(bv1.size() ==  100 * 65536 + 1);
5344
 
    }}
5345
 
 
5346
 
    // serialization
5347
 
 
5348
 
    {{
5349
 
    bvect bv1(10);
5350
 
    bv1.set(5);
5351
 
    struct bvect::statistics st1;
5352
 
    bv1.calc_stat(&st1);
5353
 
 
5354
 
    unsigned char* sermem = new unsigned char[st1.max_serialize_mem];
5355
 
    unsigned slen2 = bm::serialize(bv1, sermem);
5356
 
 
5357
 
    bvect bv2(0);
5358
 
    bm::deserialize(bv2, sermem);
5359
 
    delete [] sermem;
5360
 
 
5361
 
    assert(bv2.size() == 10);
5362
 
    assert(bv2.count() == 1);
5363
 
    assert(bv2.get_first() == 5);
5364
 
    
5365
 
    }}
5366
 
 
5367
 
    {{
5368
 
    bvect bv1(10);
5369
 
    bv1.set(5);
5370
 
    unsigned int arg[] = { 10, 65536, 65537, 65538 * 10000 };
5371
 
    unsigned* it1 = arg;
5372
 
    unsigned* it2 = arg + 4;
5373
 
    combine_or(bv1, it1, it2);
5374
 
    assert(bv1.size() == 65538 * 10000 + 1);
5375
 
    bvect::enumerator en = bv1.first();
5376
 
    while (en.valid())
5377
 
    {
5378
 
        cout << *en << " ";
5379
 
        ++en;
5380
 
    }
5381
 
    }}
5382
 
 
5383
 
}
5384
 
 
5385
 
 
5386
 
/*
5387
 
#define POWER_CHECK(w, mask) \
5388
 
    (bm::bit_count_table<true>::_count[(w&mask) ^ ((w&mask)-1)])
5389
 
 
5390
 
void BitListTest()
5391
 
{
5392
 
    unsigned bits[64] = {0,};
5393
 
 
5394
 
    unsigned w = 0;
5395
 
 
5396
 
    w = (1 << 3) | 1;
5397
 
 
5398
 
 
5399
 
    int bn3 = POWER_CHECK(w, 1 << 3) - 1;
5400
 
    int bn2 = POWER_CHECK(w, 1 << 2) - 1;
5401
 
    int bn0 = POWER_CHECK(w, 1 << 0) - 1;
5402
 
 
5403
 
    bit_list(w, bits+1);
5404
 
  
5405
 
}
5406
 
*/
5407
 
 
5408
 
 
5409
 
int main(void)
5410
 
{
5411
 
    time_t      start_time = time(0);
5412
 
    time_t      finish_time;
5413
 
 
5414
 
    OptimGAPTest();
5415
 
 
5416
 
    CalcBeginMask();
5417
 
    CalcEndMask();
5418
 
 
5419
 
 
5420
 
//   cout << sizeof(__int64) << endl;
5421
 
 
5422
 
//   ::srand((unsigned)::time(NULL));
5423
 
 
5424
 
     MiniSetTest();
5425
 
 
5426
 
     SyntaxTest();
5427
 
 
5428
 
     SetTest();
5429
 
 
5430
 
     ResizeTest();
5431
 
    
5432
 
     BitCountChangeTest();
5433
 
 
5434
 
     EnumeratorTest();
5435
 
 
5436
 
     BasicFunctionalityTest();
5437
 
 
5438
 
     ClearAllTest();
5439
 
 
5440
 
     GAPCheck();
5441
 
 
5442
 
     MaxSTest();
5443
 
 
5444
 
     GetNextTest();
5445
 
 
5446
 
     SimpleRandomFillTest();
5447
 
     
5448
 
     RangeRandomFillTest();
5449
 
 
5450
 
     AndOperationsTest();   
5451
 
           
5452
 
     OrOperationsTest();
5453
 
 
5454
 
     XorOperationsTest();
5455
 
 
5456
 
     SubOperationsTest();
5457
 
 
5458
 
     WordCmpTest();
5459
 
 
5460
 
     ComparisonTest();
5461
 
 
5462
 
     MutationTest();
5463
 
 
5464
 
     MutationOperationsTest();
5465
 
   
5466
 
     SerializationTest();
5467
 
 
5468
 
     DesrializationTest2();
5469
 
 
5470
 
     BlockLevelTest();
5471
 
 
5472
 
     StressTest(800);
5473
 
 
5474
 
    finish_time = time(0);
5475
 
 
5476
 
 
5477
 
    cout << "Test execution time = " << finish_time - start_time << endl;
5478
 
 
5479
 
#ifdef MEM_DEBUG
5480
 
    cout << "Number of BLOCK allocations = " <<  dbg_block_allocator::na_ << endl;
5481
 
    cout << "Number of PTR allocations = " <<  dbg_ptr_allocator::na_ << endl << endl;
5482
 
 
5483
 
    assert(dbg_block_allocator::balance() == 0);
5484
 
    assert(dbg_ptr_allocator::balance() == 0);
5485
 
#endif
5486
 
 
5487
 
    return 0;
5488
 
}
5489
 
 
5490
 
 
 
1
/*
 
2
Copyright (c) 2002,2003 Anatoliy Kuznetsov.
 
3
 
 
4
Permission is hereby granted, free of charge, to any person 
 
5
obtaining a copy of this software and associated documentation 
 
6
files (the "Software"), to deal in the Software without restriction, 
 
7
including without limitation the rights to use, copy, modify, merge, 
 
8
publish, distribute, sublicense, and/or sell copies of the Software, 
 
9
and to permit persons to whom the Software is furnished to do so, 
 
10
subject to the following conditions:
 
11
 
 
12
The above copyright notice and this permission notice shall be included 
 
13
in all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 
16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
 
17
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 
18
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 
19
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
 
20
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
 
21
OTHER DEALINGS IN THE SOFTWARE.
 
22
*/
 
23
 
 
24
 
 
25
//#define BM64OPT
 
26
//#define BM_SET_MMX_GUARD
 
27
//#define BMSSE2OPT
 
28
//#define BMCOUNTOPT
 
29
 
 
30
#include <ncbi_pch.hpp>
 
31
#include <stdio.h>
 
32
#include <stdlib.h>
 
33
#include <assert.h>
 
34
#include <memory.h>
 
35
#include <iostream>
 
36
#include <time.h>
 
37
#include <bm.h>
 
38
#include <bmalgo.h>
 
39
#include <bmserial.h>
 
40
 
 
41
using namespace bm;
 
42
using namespace std;
 
43
 
 
44
#include "rlebtv.h"
 
45
#include <encoding.h>
 
46
#include <limits.h>
 
47
 
 
48
 
 
49
 
 
50
#define POOL_SIZE 5000
 
51
 
 
52
//#define MEM_POOL
 
53
 
 
54
 
 
55
template<class T> T* pool_allocate(T** pool, int& i, size_t n)
 
56
{
 
57
    return i ? pool[i--] : (T*) ::malloc(n * sizeof(T));
 
58
}
 
59
 
 
60
inline void* pool_allocate2(void** pool, int& i, size_t n)
 
61
{
 
62
    return i ? pool[i--] : malloc(n * sizeof(void*));
 
63
}
 
64
 
 
65
 
 
66
 
 
67
template<class T> void pool_free(T** pool, int& i, T* p)
 
68
{
 
69
    i < POOL_SIZE ? (free(p),(void*)0) : pool[++i]=p;
 
70
}
 
71
 
 
72
 
 
73
class pool_block_allocator
 
74
{
 
75
public:
 
76
 
 
77
    static bm::word_t* allocate(size_t n, const void *)
 
78
    {
 
79
        int *idx = 0;
 
80
        bm::word_t** pool = 0;
 
81
 
 
82
        switch (n)
 
83
        {
 
84
        case bm::set_block_size:
 
85
            idx = &bit_blocks_idx_;
 
86
            pool = free_bit_blocks_;
 
87
            break;
 
88
 
 
89
        case 64:
 
90
            idx = &gap_blocks_idx0_;
 
91
            pool = gap_bit_blocks0_;
 
92
            break;
 
93
 
 
94
        case 128:
 
95
            idx = &gap_blocks_idx1_;
 
96
            pool = gap_bit_blocks1_;
 
97
            break;
 
98
        
 
99
        case 256:
 
100
            idx = &gap_blocks_idx2_;
 
101
            pool = gap_bit_blocks2_;
 
102
            break;
 
103
 
 
104
        case 512:
 
105
            idx = &gap_blocks_idx3_;
 
106
            pool = gap_bit_blocks3_;
 
107
            break;
 
108
 
 
109
        default:
 
110
            assert(0);
 
111
        }
 
112
 
 
113
        return pool_allocate(pool, *idx, n);
 
114
    }
 
115
 
 
116
    static void deallocate(bm::word_t* p, size_t n)
 
117
    {
 
118
        int *idx = 0;
 
119
        bm::word_t** pool = 0;
 
120
 
 
121
        switch (n)
 
122
        {
 
123
        case bm::set_block_size:
 
124
            idx = &bit_blocks_idx_;
 
125
            pool = free_bit_blocks_;
 
126
            break;
 
127
 
 
128
        case 64:
 
129
            idx = &gap_blocks_idx0_;
 
130
            pool = gap_bit_blocks0_;
 
131
            break;
 
132
 
 
133
        case 128:
 
134
            idx = &gap_blocks_idx1_;
 
135
            pool = gap_bit_blocks1_;
 
136
            break;
 
137
        
 
138
        case 256:
 
139
            idx = &gap_blocks_idx2_;
 
140
            pool = gap_bit_blocks2_;
 
141
            break;
 
142
 
 
143
        case 512:
 
144
            idx = &gap_blocks_idx3_;
 
145
            pool = gap_bit_blocks3_;
 
146
            break;
 
147
 
 
148
        default:
 
149
            assert(0);
 
150
        }
 
151
 
 
152
        pool_free(pool, *idx, p);
 
153
    }
 
154
 
 
155
private:
 
156
    static bm::word_t* free_bit_blocks_[];
 
157
    static int         bit_blocks_idx_;
 
158
 
 
159
    static bm::word_t* gap_bit_blocks0_[];
 
160
    static int         gap_blocks_idx0_;
 
161
 
 
162
    static bm::word_t* gap_bit_blocks1_[];
 
163
    static int         gap_blocks_idx1_;
 
164
 
 
165
    static bm::word_t* gap_bit_blocks2_[];
 
166
    static int         gap_blocks_idx2_;
 
167
 
 
168
    static bm::word_t* gap_bit_blocks3_[];
 
169
    static int         gap_blocks_idx3_;
 
170
};
 
171
 
 
172
bm::word_t* pool_block_allocator::free_bit_blocks_[POOL_SIZE];
 
173
int pool_block_allocator::bit_blocks_idx_ = 0;
 
174
 
 
175
bm::word_t* pool_block_allocator::gap_bit_blocks0_[POOL_SIZE];
 
176
int pool_block_allocator::gap_blocks_idx0_ = 0;
 
177
 
 
178
bm::word_t* pool_block_allocator::gap_bit_blocks1_[POOL_SIZE];
 
179
int pool_block_allocator::gap_blocks_idx1_ = 0;
 
180
 
 
181
bm::word_t* pool_block_allocator::gap_bit_blocks2_[POOL_SIZE];
 
182
int pool_block_allocator::gap_blocks_idx2_ = 0;
 
183
 
 
184
bm::word_t* pool_block_allocator::gap_bit_blocks3_[POOL_SIZE];
 
185
int pool_block_allocator::gap_blocks_idx3_ = 0;
 
186
 
 
187
 
 
188
 
 
189
 
 
190
class pool_ptr_allocator
 
191
{
 
192
public:
 
193
 
 
194
    static void* allocate(size_t n, const void *)
 
195
    {
 
196
        return pool_allocate2(free_ptr_blocks_, ptr_blocks_idx_, n);
 
197
    }
 
198
 
 
199
    static void deallocate(void* p, size_t)
 
200
    {
 
201
        pool_free(free_ptr_blocks_, ptr_blocks_idx_, p);
 
202
    }
 
203
 
 
204
private:
 
205
    static void*  free_ptr_blocks_[];
 
206
    static int    ptr_blocks_idx_;
 
207
};
 
208
 
 
209
void* pool_ptr_allocator::free_ptr_blocks_[POOL_SIZE];
 
210
int pool_ptr_allocator::ptr_blocks_idx_ = 0;
 
211
 
 
212
 
 
213
//#define MEM_DEBUG
 
214
 
 
215
#ifdef MEM_DEBUG
 
216
 
 
217
 
 
218
class dbg_block_allocator
 
219
{
 
220
public:
 
221
static unsigned na_;
 
222
static unsigned nf_;
 
223
 
 
224
    static bm::word_t* allocate(size_t n, const void *)
 
225
    {
 
226
        ++na_;
 
227
        assert(n);
 
228
        bm::word_t* p =
 
229
            (bm::word_t*) ::malloc((n+1) * sizeof(bm::word_t));
 
230
        *p = n;
 
231
        return ++p;
 
232
    }
 
233
 
 
234
    static void deallocate(bm::word_t* p, size_t n)
 
235
    {
 
236
        ++nf_;
 
237
        --p;
 
238
        if(*p != n)
 
239
        {
 
240
            printf("Block memory deallocation error!\n");
 
241
            exit(1);
 
242
        }
 
243
        ::free(p);
 
244
    }
 
245
 
 
246
    static int balance()
 
247
    {
 
248
        return nf_ - na_;
 
249
    }
 
250
};
 
251
 
 
252
unsigned dbg_block_allocator::na_ = 0;
 
253
unsigned dbg_block_allocator::nf_ = 0;
 
254
 
 
255
class dbg_ptr_allocator
 
256
{
 
257
public:
 
258
static unsigned na_;
 
259
static unsigned nf_;
 
260
 
 
261
    static void* allocate(size_t n, const void *)
 
262
    {
 
263
        ++na_;
 
264
        assert(sizeof(size_t) == sizeof(void*));
 
265
        void* p = ::malloc((n+1) * sizeof(void*));
 
266
        size_t* s = (size_t*) p;
 
267
        *s = n;
 
268
        return (void*)++s;
 
269
    }
 
270
 
 
271
    static void deallocate(void* p, size_t n)
 
272
    {
 
273
        ++nf_;
 
274
        size_t* s = (size_t*) p;
 
275
        --s;
 
276
        if(*s != n)
 
277
        {
 
278
            printf("Ptr memory deallocation error!\n");
 
279
            exit(1);
 
280
        }
 
281
        ::free(s);
 
282
    }
 
283
 
 
284
    static int balance()
 
285
    {
 
286
        return nf_ - na_;
 
287
    }
 
288
 
 
289
};
 
290
 
 
291
unsigned dbg_ptr_allocator::na_ = 0;
 
292
unsigned dbg_ptr_allocator::nf_ = 0;
 
293
 
 
294
 
 
295
typedef mem_alloc<dbg_block_allocator, dbg_ptr_allocator> dbg_alloc;
 
296
 
 
297
typedef bm::bvector<dbg_alloc> bvect;
 
298
typedef bm::bvector_mini<dbg_block_allocator> bvect_mini;
 
299
 
 
300
#else
 
301
 
 
302
#ifdef MEM_POOL
 
303
 
 
304
typedef mem_alloc<pool_block_allocator, pool_ptr_allocator> pool_alloc;
 
305
typedef bm::bvector<pool_alloc> bvect;
 
306
typedef bm::bvector_mini<bm::block_allocator> bvect_mini;
 
307
 
 
308
 
 
309
#else
 
310
 
 
311
typedef bm::bvector<> bvect;
 
312
typedef bm::bvector_mini<bm::block_allocator> bvect_mini;
 
313
 
 
314
#endif
 
315
 
 
316
#endif
 
317
 
 
318
//const unsigned BITVECT_SIZE = 100000000 * 8;
 
319
 
 
320
// This this setting program will consume around 150M of RAM
 
321
const unsigned BITVECT_SIZE = 100000000 * 2;
 
322
 
 
323
const unsigned ITERATIONS = 100000;
 
324
const unsigned PROGRESS_PRINT = 2000000;
 
325
 
 
326
 
 
327
 
 
328
void CheckVectors(bvect_mini &bvect_min, 
 
329
                  bvect      &bvect_full,
 
330
                  unsigned size,
 
331
                  bool     detailed = false);
 
332
 
 
333
 
 
334
unsigned random_minmax(unsigned min, unsigned max)
 
335
{
 
336
    unsigned r = (rand() << 16) | rand();
 
337
    return r % (max-min) + min;
 
338
}
 
339
 
 
340
 
 
341
void FillSets(bvect_mini* bvect_min, 
 
342
              bvect* bvect_full,
 
343
              unsigned min, 
 
344
              unsigned max,
 
345
              unsigned fill_factor)
 
346
{
 
347
    unsigned i;
 
348
    unsigned id;
 
349
 
 
350
    //Random filling
 
351
    if(fill_factor == 0)
 
352
    {
 
353
        unsigned n_id = (max - min) / 100;
 
354
        printf("random filling : %i\n", n_id);
 
355
        for (i = 0; i < n_id; i++)
 
356
        {
 
357
            id = random_minmax(min, max);
 
358
            bvect_min->set_bit(id);
 
359
            bvect_full->set_bit(id);
 
360
            if (PROGRESS_PRINT)
 
361
            {
 
362
                if ( (i % PROGRESS_PRINT) == 0)
 
363
                {
 
364
                    cout << "+" << flush;
 
365
                }
 
366
            }
 
367
        }
 
368
        cout << endl;
 
369
    }
 
370
    else
 
371
    {
 
372
        printf("fill_factor random filling : factor = %i\n", fill_factor);
 
373
 
 
374
        for(i = 0; i < fill_factor; i++)
 
375
        {
 
376
            int k = rand() % 10;
 
377
            if (k == 0)
 
378
                k+=2;
 
379
 
 
380
            //Calculate start
 
381
            unsigned start = min + (max - min) / (fill_factor * k);
 
382
 
 
383
            //Randomize start
 
384
            start += random_minmax(1, (max - min) / (fill_factor * 10));
 
385
 
 
386
            if (start > max)
 
387
            {
 
388
                start = min;
 
389
            }
 
390
            
 
391
            //Calculate end 
 
392
            unsigned end = start + (max - start) / (fill_factor *2);
 
393
 
 
394
            //Randomize end
 
395
            end -= random_minmax(1, (max - start) / (fill_factor * 10));
 
396
 
 
397
            if (end > max )
 
398
            {
 
399
                end = max;
 
400
            }
 
401
 
 
402
            
 
403
            if (fill_factor > 1)
 
404
            {
 
405
                for(; start < end;)
 
406
                {
 
407
                    int r = rand() % 8;
 
408
 
 
409
                    if (r > 7)
 
410
                    {
 
411
                        int inc = rand() % 3;
 
412
                        ++inc;
 
413
                        unsigned end2 = start + rand() % 1000;
 
414
                        if (end2 > end)
 
415
                            end2 = end;
 
416
                        while (start < end2)
 
417
                        {
 
418
                            bvect_min->set_bit(start);
 
419
                            bvect_full->set_bit(start);  
 
420
                            start += inc;
 
421
                        }
 
422
 
 
423
                        if (PROGRESS_PRINT)
 
424
                        {
 
425
                            if ( ( ((i+1)*(end-start))  % PROGRESS_PRINT) == 0)
 
426
                            {
 
427
                                cout << "+" << flush;
 
428
                            }
 
429
                        }
 
430
 
 
431
                        continue;
 
432
                    }
 
433
 
 
434
                    if (r)
 
435
                    {
 
436
                        bvect_min->set_bit(start);
 
437
                        bvect_full->set_bit(start);
 
438
                        ++start;
 
439
                    }
 
440
                    else
 
441
                    {
 
442
                        start+=r;
 
443
                        bvect_min->set_bit(start);
 
444
                        bvect_full->set_bit(start);
 
445
                    }
 
446
 
 
447
                    if (PROGRESS_PRINT)
 
448
                    {
 
449
                        if ( ( ((i+1)*(end-start))  % PROGRESS_PRINT) == 0)
 
450
                        {
 
451
                            cout << "+" << flush;
 
452
                        }
 
453
                    }
 
454
                }
 
455
 
 
456
            }
 
457
            else
 
458
            {
 
459
                int c = rand() % 15;
 
460
                if (c == 0)
 
461
                    ++c;
 
462
                for(; start < end; ++start)
 
463
                {
 
464
                    bvect_min->set_bit(start);
 
465
                    bvect_full->set_bit(start);
 
466
 
 
467
                    if (start % c)
 
468
                    {
 
469
                        start += c;
 
470
                    }
 
471
 
 
472
                    if (PROGRESS_PRINT)
 
473
                    {
 
474
                        if ( ( ((i+1)*(end-start))  % PROGRESS_PRINT) == 0)
 
475
                        {
 
476
                            cout << "+" << flush;
 
477
                        }
 
478
                    }
 
479
 
 
480
                }
 
481
            }
 
482
            cout << endl;
 
483
 
 
484
        }
 
485
    }
 
486
}
 
487
 
 
488
//
 
489
// Interval filling.
 
490
// 111........111111........111111..........11111111.......1111111...
 
491
//
 
492
 
 
493
 
 
494
void FillSetsIntervals(bvect_mini* bvect_min, 
 
495
              bvect* bvect_full,
 
496
              unsigned min, 
 
497
              unsigned max,
 
498
              unsigned fill_factor,
 
499
              bool set_flag=true)
 
500
{
 
501
 
 
502
    while(fill_factor==0)
 
503
    {
 
504
        fill_factor=rand()%10;
 
505
    }
 
506
 
 
507
    cout << "Intervals filling. Factor=" 
 
508
         <<  fill_factor << endl << endl;
 
509
 
 
510
    unsigned i, j;
 
511
    unsigned factor = 70 * fill_factor;
 
512
    for (i = min; i < max; ++i)
 
513
    {
 
514
        unsigned len, end; 
 
515
 
 
516
        do
 
517
        {
 
518
            len = rand() % factor;
 
519
            end = i+len;
 
520
            
 
521
        } while (end >= max);
 
522
/*
 
523
        if (set_flag == false)
 
524
        {
 
525
            cout << "Cleaning: " << i << "-" << end << endl;
 
526
        }
 
527
        else
 
528
        {
 
529
            cout << "Add: " << i << "-" << end << endl;
 
530
        }
 
531
*/
 
532
        if (i < end)
 
533
        {
 
534
            bvect_full->set_range(i, end-1, set_flag);
 
535
        }
 
536
       
 
537
        for (j = i; j < end; ++j)
 
538
        {
 
539
            if (set_flag)
 
540
            {
 
541
                bvect_min->set_bit(j);
 
542
                //bvect_full->set_bit(j);
 
543
            }
 
544
            else
 
545
            {
 
546
                bvect_min->clear_bit(j);
 
547
                //bvect_full->clear_bit(j);
 
548
/*
 
549
        if (g_cnt_check)
 
550
        {
 
551
            bool b = bvect_full->count_check();
 
552
            if(!b)
 
553
            {
 
554
                cout << "Count check failed (clear)!" << endl;
 
555
                cout << "bit=" << j << endl;
 
556
                exit(1);
 
557
            }
 
558
        }
 
559
*/
 
560
            }
 
561
 
 
562
                           
 
563
        } // j
 
564
 
 
565
//cout << "Checking range filling " << from << "-" << to << endl;
 
566
//CheckVectors(*bvect_min, *bvect_full, 100000000);
 
567
 
 
568
 
 
569
        i = end;
 
570
 
 
571
 
 
572
        len = rand() % (factor* 10 * bm::gap_max_bits);
 
573
        if (len % 2)
 
574
        {
 
575
            len *= rand() % (factor * 10);
 
576
        }
 
577
 
 
578
        i+=len;
 
579
 
 
580
        if ( (len % 6) == 0)  
 
581
        {
 
582
/*
 
583
if (set_flag == false)
 
584
{
 
585
    cout << "Additional Cleaning: " << i << "-" << end << endl;
 
586
}
 
587
*/
 
588
            for(unsigned k=0; k < 1000 && i < max; k+=3,i+=3)
 
589
            {
 
590
                if (set_flag)
 
591
                {
 
592
                    bvect_min->set_bit(i);
 
593
                    bvect_full->set_bit(i);            
 
594
                }
 
595
                else
 
596
                {
 
597
                    bvect_min->clear_bit(j);
 
598
                    bvect_full->clear_bit(j);
 
599
                }
 
600
 
 
601
            }
 
602
        }
 
603
 
 
604
    } // for i
 
605
 
 
606
}
 
607
 
 
608
void FillSetClearIntervals(bvect_mini* bvect_min, 
 
609
              bvect* bvect_full,
 
610
              unsigned min, 
 
611
              unsigned max,
 
612
              unsigned fill_factor)
 
613
{
 
614
    FillSetsIntervals(bvect_min, bvect_full, min, max, fill_factor, true);
 
615
    FillSetsIntervals(bvect_min, bvect_full, min, max, fill_factor, false);
 
616
}
 
617
 
 
618
 
 
619
void FillSetsRandom(bvect_mini* bvect_min, 
 
620
              bvect* bvect_full,
 
621
              unsigned min, 
 
622
              unsigned max,
 
623
              unsigned fill_factor)
 
624
{
 
625
    unsigned diap = max - min;
 
626
 
 
627
    unsigned count;
 
628
 
 
629
 
 
630
    switch (fill_factor)
 
631
    {
 
632
    case 0:
 
633
        count = diap / 1000;
 
634
        break;
 
635
    case 1:
 
636
        count = diap / 100;
 
637
        break;
 
638
    default:
 
639
        count = diap / 10;
 
640
        break;
 
641
 
 
642
    }
 
643
 
 
644
    for (unsigned i = 0; i < count; ++i)
 
645
    {
 
646
        unsigned bn = rand() % count;
 
647
        bn += min;
 
648
 
 
649
        if (bn > max)
 
650
        {
 
651
            bn = max;
 
652
        }
 
653
        bvect_min->set_bit(bn);
 
654
        bvect_full->set_bit(bn);   
 
655
        
 
656
        if ( (i  % PROGRESS_PRINT) == 0)
 
657
        {
 
658
            cout << "+" << flush;
 
659
        }
 
660
    }
 
661
    cout << "Ok" << endl;
 
662
 
 
663
}
 
664
 
 
665
 
 
666
//
 
667
//  Quasi random filling with choosing randomizing method.
 
668
//
 
669
//
 
670
void FillSetsRandomMethod(bvect_mini* bvect_min, 
 
671
                          bvect* bvect_full,
 
672
                          unsigned min, 
 
673
                          unsigned max,
 
674
                          int optimize = 0,
 
675
                          int method = -1)
 
676
{
 
677
    if (method == -1)
 
678
    {
 
679
        method = rand() % 5;
 
680
    }
 
681
    unsigned factor;
 
682
//method = 4;
 
683
    switch (method)
 
684
    {
 
685
 
 
686
    case 0:
 
687
        cout << "Random filling: method - FillSets - factor(0)" << endl;
 
688
        FillSets(bvect_min, bvect_full, min, max, 0);
 
689
        break;
 
690
 
 
691
    case 1:
 
692
        cout << "Random filling: method - FillSets - factor(random)" << endl;
 
693
        factor = rand()%3;
 
694
        FillSets(bvect_min, bvect_full, min, max, factor?factor:1);
 
695
        break;
 
696
 
 
697
    case 2:
 
698
        cout << "Random filling: method - Set-Clear Intervals - factor(random)" << endl;
 
699
        factor = rand()%10;
 
700
        FillSetClearIntervals(bvect_min, bvect_full, min, max, factor);
 
701
        break;
 
702
    case 3:
 
703
        cout << "Random filling: method - FillRandom - factor(random)" << endl;
 
704
        factor = rand()%3;
 
705
        FillSetsRandom(bvect_min, bvect_full, min, max, factor?factor:1);
 
706
        break;
 
707
 
 
708
    default:
 
709
        cout << "Random filling: method - Set Intervals - factor(random)" << endl;
 
710
        factor = rand()%10;
 
711
        FillSetsIntervals(bvect_min, bvect_full, min, max, factor);
 
712
        break;
 
713
 
 
714
    } // switch
 
715
 
 
716
    if (optimize && (method <= 1))
 
717
    {
 
718
        cout << "Vector optimization..." << flush;
 
719
        bvect_full->optimize();
 
720
        cout << "OK" << endl;
 
721
    }
 
722
}
 
723
 
 
724
// do logical operation through serialization
 
725
unsigned SerializationOperation(bvect*             bv_target,
 
726
                                /*const*/ bvect&   bv1,
 
727
                                /*const*/ bvect&   bv2,
 
728
                                set_operation      op,
 
729
                                bool               check_reverse=false)
 
730
{
 
731
    bvect bv_tmp;
 
732
    if (!bv_target)
 
733
    {
 
734
        bv_target = &bv_tmp;
 
735
    }
 
736
 
 
737
    if (op == set_COUNT_SUB_AB ||
 
738
        op == set_COUNT_SUB_BA)
 
739
    {
 
740
        check_reverse = false;
 
741
    }
 
742
 
 
743
    // serialize input vectors
 
744
    bvect::statistics *st1_op, *st2_op;
 
745
    st1_op = new bvect::statistics;
 
746
    st2_op = new bvect::statistics;
 
747
 
 
748
    bv1.optimize(0, bvect::opt_compress, st1_op);
 
749
    bv2.optimize(0, bvect::opt_compress, st2_op);
 
750
 
 
751
 
 
752
   struct bvect::statistics st1, st2;
 
753
   bv1.calc_stat(&st1);
 
754
   bv2.calc_stat(&st2);
 
755
 
 
756
 
 
757
   if (st1.max_serialize_mem > st1_op->max_serialize_mem)
 
758
   {
 
759
       cout << "Optimize failed to compute max_serialize_mem" << endl;
 
760
       cout << "calc_stat=" << st1.max_serialize_mem << endl;
 
761
       cout << "optimize=" << st1_op->max_serialize_mem << endl;
 
762
       exit(1);
 
763
   }
 
764
   if (st2.max_serialize_mem > st2_op->max_serialize_mem)
 
765
   {
 
766
       cout << "Optimize failed to compute max_serialize_mem" << endl;
 
767
       cout << "calc_stat=" << st2.max_serialize_mem << endl;
 
768
       cout << "optimize=" << st2_op->max_serialize_mem << endl;
 
769
       exit(1);
 
770
   }
 
771
 
 
772
   delete st1_op;
 
773
   delete st2_op;
 
774
 
 
775
   unsigned char* smem1 = new unsigned char[st1.max_serialize_mem];
 
776
   unsigned char* smem2 = new unsigned char[st2.max_serialize_mem];
 
777
 
 
778
   unsigned slen1 = bm::serialize(bv1, smem1);
 
779
   unsigned slen2 = bm::serialize(bv2, smem2);
 
780
 
 
781
   unsigned count =
 
782
       operation_deserializer<bvect>::deserialize(*bv_target,
 
783
                                                  smem1,
 
784
                                                  0,
 
785
                                                  set_ASSIGN);
 
786
   int res = bv1.compare(*bv_target);
 
787
   if (res != 0)
 
788
   {
 
789
       cout << "set_ASSIGN 1 failed!" << endl;
 
790
       exit (1);
 
791
   }
 
792
//   PrintSet(cout, *bv_target);
 
793
//bv2.stat();
 
794
 
 
795
    count=
 
796
       operation_deserializer<bvect>::deserialize(*bv_target,
 
797
                                                  smem2,
 
798
                                                  0,
 
799
                                                  op);
 
800
 
 
801
   if (check_reverse)
 
802
   {
 
803
        bvect bv_tmp2(BM_GAP);
 
804
        operation_deserializer<bvect>::deserialize(bv_tmp2,
 
805
                                                   smem2,
 
806
                                                   0,
 
807
                                                   set_ASSIGN);
 
808
        int res = bv_tmp2.compare(bv2);
 
809
        if (res != 0)
 
810
        {
 
811
            cout << "set_ASSIGN failed 2! " << endl;
 
812
            exit(1);
 
813
        }
 
814
//        PrintSet(cout, bv_tmp2);
 
815
//bv1.stat();
 
816
        unsigned count_rev =
 
817
        operation_deserializer<bvect>::deserialize(bv_tmp2,
 
818
                                                   smem1,
 
819
                                                   0,
 
820
                                                   op);
 
821
        if (count != count_rev)
 
822
        {
 
823
            bv1.stat();
 
824
            cout << "Serialization operation reverse check failed"
 
825
                 << " count = " << count 
 
826
                 << " count rev= " << count_rev
 
827
                 << endl;
 
828
            exit(1);
 
829
        }
 
830
 
 
831
   }
 
832
 
 
833
   delete [] smem1;
 
834
   delete [] smem2;
 
835
 
 
836
   return count;
 
837
}
 
838
 
 
839
void SerializationOperation2Test(bvect*        bv_target,
 
840
                                 bvect&        bv1,
 
841
                                 bvect&        bv2,
 
842
                                 unsigned      predicted_count,
 
843
                                 set_operation op_count,
 
844
                                 set_operation op_combine)
 
845
{
 
846
    bv_target->clear(true);
 
847
    unsigned scount1 = SerializationOperation(0, 
 
848
                                              bv1,
 
849
                                              bv2,
 
850
                                              op_count,
 
851
                                              true /*reverse check*/);
 
852
 
 
853
    unsigned scount2 = SerializationOperation(bv_target, 
 
854
                                              bv1,
 
855
                                              bv2,
 
856
                                              op_combine);
 
857
    scount2 = bv_target->count();
 
858
    if (predicted_count != scount2 || scount1 != scount2)
 
859
    {
 
860
        cout << "Serialization count != predicted" << endl 
 
861
             << " predicted=" << predicted_count 
 
862
             << " scount1="   << scount1
 
863
             << " scount2="   << scount2
 
864
             << endl;
 
865
 
 
866
        cout << endl << "target:" << endl;
 
867
        bv_target->stat();
 
868
        cout << endl << endl << "Reference" << endl;
 
869
        if (op_combine == set_AND)
 
870
        {
 
871
            bv1 &= bv2;
 
872
            bv1.stat();
 
873
        }
 
874
 
 
875
        exit(1);
 
876
    }
 
877
}
 
878
 
 
879
 
 
880
void print_mv(const bvect_mini &bvect_min, unsigned size)
 
881
{
 
882
    unsigned i;
 
883
    for (i = 0; i < size; ++i)
 
884
    {
 
885
        bool bflag = bvect_min.is_bit_true(i) != 0; 
 
886
 
 
887
        if (bflag)
 
888
            printf("1");
 
889
        else
 
890
            printf("0");
 
891
        if ((i % 31) == 0 && (i != 0))
 
892
            printf(".");
 
893
    }
 
894
 
 
895
    printf("\n");
 
896
}
 
897
 
 
898
void print_gap(const gap_vector& gap_vect, unsigned size)
 
899
{
 
900
    const gap_word_t *buf = gap_vect.get_buf();
 
901
    unsigned len = gap_length(buf);
 
902
    printf("[%i:]", *buf++ & 1);
 
903
 
 
904
    for (unsigned i = 1; i < len; ++i)
 
905
    {
 
906
        printf("%i,", *buf++);
 
907
    }
 
908
 
 
909
    printf("\n");
 
910
}
 
911
 
 
912
void CheckGAPMin(const gap_vector& gapv, const bvect_mini& bvect_min, unsigned len)
 
913
{
 
914
    for (unsigned i = 0; i < len; ++i)
 
915
    {
 
916
        int bit1 = (gapv.is_bit_true(i) == 1);
 
917
        int bit2 = (bvect_min.is_bit_true(i) != 0);
 
918
        if(bit1 != bit2)
 
919
        {
 
920
           cout << "Bit comparison failed. " << "Bit N=" << i << endl;
 
921
           assert(0);
 
922
           exit(1);
 
923
        }
 
924
    }
 
925
}
 
926
 
 
927
void CheckIntervals(const bvect& bv, unsigned max_bit)
 
928
{
 
929
    unsigned cnt0 = count_intervals(bv);
 
930
    unsigned cnt1 = 1;
 
931
    bool bit_prev = bv.test(0);
 
932
    for (unsigned i = 1; i < max_bit; ++i)
 
933
    {
 
934
        bool bit = bv.test(i);
 
935
        cnt1 += bit_prev ^ bit;
 
936
        bit_prev = bit;
 
937
    }
 
938
    if (cnt0 != cnt1)
 
939
    {
 
940
        cout << "CheckIntervals error. " << "bm count=" << cnt0
 
941
             << " Control = " << cnt1 << endl;
 
942
        exit(1);
 
943
    }
 
944
}
 
945
 
 
946
template<class T> void CheckCountRange(const T& vect, 
 
947
                                       unsigned left, 
 
948
                                       unsigned right,
 
949
                                       unsigned* block_count_arr=0)
 
950
{
 
951
    unsigned cnt1 = vect.count_range(left, right, block_count_arr);
 
952
    unsigned cnt2 = 0;
 
953
//cout << endl;
 
954
    for (unsigned i = left; i <= right; ++i)
 
955
    {
 
956
        if (vect.test(i))
 
957
        {
 
958
//            cout << i << " " << flush;
 
959
            ++cnt2;
 
960
        }
 
961
    }
 
962
//cout << endl;
 
963
    if (cnt1 != cnt2)
 
964
    {
 
965
        cout << "Bitcount range failed!" << "left=" << left 
 
966
             << " right=" << right << endl
 
967
             << "count_range()=" << cnt1 
 
968
             << " check=" << cnt2;
 
969
        exit(1);
 
970
    }
 
971
}
 
972
 
 
973
 
 
974
unsigned BitCountChange(unsigned word)
 
975
{
 
976
    unsigned count = 1;
 
977
    unsigned bit_prev = word & 1;
 
978
    word >>= 1;
 
979
    for (unsigned i = 1; i < 32; ++i)
 
980
    {
 
981
        unsigned bit = word & 1;
 
982
        count += bit ^ bit_prev;
 
983
        bit_prev = bit;
 
984
        word >>= 1;
 
985
    }
 
986
    return count;
 
987
}
 
988
 
 
989
 
 
990
void DetailedCheckVectors(const bvect_mini &bvect_min, 
 
991
                          const bvect      &bvect_full,
 
992
                          unsigned size)
 
993
{
 
994
    cout << "Detailed check" << endl;
 
995
 
 
996
    //bvect_full.stat();
 
997
 
 
998
    // detailed bit by bit comparison. Paranoia check.
 
999
 
 
1000
    unsigned i;
 
1001
    for (i = 0; i < size; ++i)
 
1002
    {
 
1003
        bool bv_m_flag = bvect_min.is_bit_true(i) != 0; 
 
1004
        bool bv_f_flag = bvect_full.get_bit(i) != 0;
 
1005
 
 
1006
        if (bv_m_flag != bv_f_flag)
 
1007
        {
 
1008
            printf("Bit %u is non conformant. vect_min=%i vect_full=%i\n",
 
1009
                i, (int)bv_m_flag, (int)bv_f_flag);
 
1010
 
 
1011
            cout << "Non-conformant block number is: " << unsigned(i >>  bm::set_block_shift) << endl;
 
1012
//            throw 110;
 
1013
            exit(1);
 
1014
        }
 
1015
 
 
1016
        if (PROGRESS_PRINT)
 
1017
        {
 
1018
            if ( (i % PROGRESS_PRINT) == 0)
 
1019
            {
 
1020
                printf(".");
 
1021
            }
 
1022
        }
 
1023
             
 
1024
    }
 
1025
    
 
1026
    printf("\n detailed check ok.\n");
 
1027
 
 
1028
}
 
1029
 
 
1030
 
 
1031
// vectors comparison check
 
1032
 
 
1033
void CheckVectors(bvect_mini &bvect_min, 
 
1034
                  bvect      &bvect_full,
 
1035
                  unsigned size,
 
1036
                  bool     detailed)
 
1037
{
 
1038
    cout << "\nVectors checking...bits to compare = " << size << endl;
 
1039
 
 
1040
    cout << "Bitcount summary : " << endl;
 
1041
    unsigned min_count = bvect_min.bit_count();
 
1042
    cout << "minvector count = " << min_count << endl;
 
1043
    unsigned count = bvect_full.count();
 
1044
    unsigned full_count = bvect_full.recalc_count();
 
1045
    cout << "fullvector re-count = " << full_count << endl;
 
1046
    
 
1047
    if (min_count != full_count)
 
1048
    {
 
1049
        cout << "fullvector count = " << count << endl;
 
1050
        cout << "Count comparison failed !!!!" << endl;
 
1051
        bvect_full.stat();
 
1052
        DetailedCheckVectors(bvect_min, bvect_full, size);
 
1053
 
 
1054
        exit(1);  
 
1055
    } 
 
1056
 
 
1057
    if (full_count)
 
1058
    {
 
1059
        bool any = bvect_full.any();
 
1060
        if (!any)
 
1061
        {
 
1062
            cout << "Anycheck failed!" << endl;
 
1063
            exit(1);
 
1064
        }
 
1065
    }
 
1066
 
 
1067
    // get_next comparison
 
1068
 
 
1069
    cout << "Positive bits comparison..." << flush;
 
1070
    unsigned nb_min = bvect_min.get_first();
 
1071
    unsigned nb_ful = bvect_full.get_first();
 
1072
 
 
1073
    bvect::counted_enumerator en = bvect_full.first();
 
1074
    unsigned nb_en = *en;
 
1075
    if (nb_min != nb_ful)
 
1076
    {
 
1077
         cout << "!!!! First bit comparison failed. Full id = " 
 
1078
              << nb_ful << " Min id = " << nb_min 
 
1079
              << endl;
 
1080
 
 
1081
         bool bit_f = bvect_full.get_bit(nb_ful);
 
1082
         cout << "Full vector'd bit #" << nb_ful << "is:" 
 
1083
              << bit_f << endl;
 
1084
 
 
1085
         bool bit_m = (bvect_min.is_bit_true(nb_min) == 1);
 
1086
         cout << "Min vector'd bit #" << nb_min << "is:" 
 
1087
              << bit_m << endl;
 
1088
 
 
1089
 
 
1090
         bvect_full.stat();
 
1091
 
 
1092
         DetailedCheckVectors(bvect_min, bvect_full, size);
 
1093
 
 
1094
         exit(1);
 
1095
    }
 
1096
 
 
1097
    if (full_count)
 
1098
    {
 
1099
       unsigned bit_count = 1;
 
1100
       unsigned en_prev = nb_en;
 
1101
 
 
1102
       do
 
1103
       {
 
1104
           nb_min = bvect_min.get_next(nb_min);
 
1105
           if (nb_min == 0)
 
1106
           {
 
1107
               break;
 
1108
           }
 
1109
 
 
1110
           en_prev = nb_en;
 
1111
           ++en;
 
1112
 
 
1113
           nb_en = *en;
 
1114
//           nb_en = bvect_full.get_next(nb_en);
 
1115
 
 
1116
           ++bit_count;
 
1117
 
 
1118
           if (nb_en != nb_min)
 
1119
           {
 
1120
               nb_ful = bvect_full.get_next(en_prev);
 
1121
               cout << "!!!!! next bit comparison failed. Full id = " 
 
1122
                    << nb_ful << " Min id = " << nb_min 
 
1123
                    << " Enumerator = " << nb_en
 
1124
                    << endl;
 
1125
 
 
1126
     //          bvect_full.stat();
 
1127
 
 
1128
     //          DetailedCheckVectors(bvect_min, bvect_full, size);
 
1129
 
 
1130
               exit(1);
 
1131
           }
 
1132
            if ( (bit_count % PROGRESS_PRINT) == 0)
 
1133
           {
 
1134
                cout << "." << flush;
 
1135
            }
 
1136
 
 
1137
       } while (en.valid());
 
1138
       if (bit_count != min_count)
 
1139
       {
 
1140
           cout << " Bit count failed."  
 
1141
                << " min = " << min_count 
 
1142
                << " bit = " << bit_count 
 
1143
                << endl;
 
1144
           exit(1);
 
1145
       }
 
1146
    }
 
1147
 
 
1148
    cout << "OK" << endl;
 
1149
 
 
1150
    return;
 
1151
}
 
1152
 
 
1153
 
 
1154
void ClearAllTest()
 
1155
{
 
1156
    bvect     bvect_full;
 
1157
 
 
1158
    for (int i = 0; i < 100000; ++i)
 
1159
    {
 
1160
        bvect_full.set_bit(i);
 
1161
    }
 
1162
    bvect_full.optimize();
 
1163
    bvect_full.clear();
 
1164
 
 
1165
    bvect_full.stat();
 
1166
 
 
1167
    int count = bvect_full.count();
 
1168
    assert(count == 0);
 
1169
    bvect_full.stat();
 
1170
}
 
1171
 
 
1172
 
 
1173
void WordCmpTest()
 
1174
{
 
1175
    cout << "---------------------------- WordCmp test" << endl;
 
1176
 
 
1177
    for (int i = 0; i < 10000000; ++i)
 
1178
    {
 
1179
        unsigned w1 = rand();
 
1180
        unsigned w2 = rand();
 
1181
        int res = wordcmp0(w1, w2);
 
1182
        int res2 = wordcmp(w1, w2);
 
1183
        if (res != res2)
 
1184
        {
 
1185
            printf("WordCmp failed !\n");
 
1186
            exit(1);
 
1187
        }
 
1188
 
 
1189
        res = wordcmp0((unsigned)0U, (unsigned)w2);
 
1190
        res2 = wordcmp((unsigned)0U, (unsigned)w2);
 
1191
 
 
1192
        if (res != res2)
 
1193
        {
 
1194
            printf("WordCmp 0 test failed !\n");
 
1195
            exit(1);
 
1196
        }
 
1197
 
 
1198
        res = wordcmp0((unsigned)~0U, (unsigned)w2);
 
1199
        res2 = wordcmp((unsigned)~0U, (unsigned)w2);
 
1200
 
 
1201
        if (res != res2)
 
1202
        {
 
1203
            printf("WordCmp ~0 test failed !\n");
 
1204
            exit(1);
 
1205
        }
 
1206
 
 
1207
        res = wordcmp0((unsigned)w2, (unsigned)0);
 
1208
        res2 = wordcmp((unsigned)w2, (unsigned)0);
 
1209
 
 
1210
        if (res != res2)
 
1211
        {
 
1212
            printf("WordCmp 0-2 test failed !\n");
 
1213
            exit(1);
 
1214
        }
 
1215
 
 
1216
    }
 
1217
 
 
1218
    cout << "Ok." << endl;
 
1219
}
 
1220
 
 
1221
void BasicFunctionalityTest()
 
1222
{
 
1223
    cout << "---------------------------- Basic functinality test" << endl;
 
1224
 
 
1225
    assert(ITERATIONS < BITVECT_SIZE);
 
1226
 
 
1227
 
 
1228
    bvect_mini     bvect_min(BITVECT_SIZE);
 
1229
    bvect          bvect_full;
 
1230
    bvect          bvect_full1;
 
1231
 
 
1232
    printf("\nBasic functionality test.\n");
 
1233
    
 
1234
    // filling vectors with regular values
 
1235
 
 
1236
    unsigned i;
 
1237
    for (i = 0; i < ITERATIONS; ++i)
 
1238
    {
 
1239
        bvect_min.set_bit(i);
 
1240
        bvect_full.set_bit(i);
 
1241
    }
 
1242
    
 
1243
    bvect_full1.set_range(0, ITERATIONS-1);
 
1244
    
 
1245
    CheckCountRange(bvect_full, 0, ITERATIONS);
 
1246
    CheckCountRange(bvect_full, 10, ITERATIONS+10);
 
1247
 
 
1248
    if (bvect_full1 != bvect_full)
 
1249
    {
 
1250
        cout << "set_range failed!" << endl;
 
1251
        bvect_full1.stat();
 
1252
        exit(1);
 
1253
    }
 
1254
 
 
1255
    bvect_full.stat();
 
1256
    bvect_full1.stat();
 
1257
 
 
1258
    // checking the results
 
1259
    unsigned count_min = 0;
 
1260
    for (i = 0; i < ITERATIONS; ++i)
 
1261
    {
 
1262
        if (bvect_min.is_bit_true(i))
 
1263
            ++count_min;
 
1264
    }
 
1265
 
 
1266
 
 
1267
    
 
1268
    unsigned count_full = bvect_full.count();
 
1269
 
 
1270
    if (count_min == count_full)
 
1271
    {
 
1272
        printf("simple count test ok.\n");
 
1273
    }
 
1274
    else
 
1275
    {
 
1276
        printf("simple count test failed count_min = %i  count_full = %i\n", 
 
1277
               count_min, count_full);
 
1278
        exit(1);
 
1279
    }
 
1280
 
 
1281
 
 
1282
    // detailed vectors verification
 
1283
 
 
1284
    CheckVectors(bvect_min, bvect_full, ITERATIONS);
 
1285
 
 
1286
    // now clearning
 
1287
 
 
1288
    for (i = 0; i < ITERATIONS; i+=2)
 
1289
    {
 
1290
        bvect_min.clear_bit(i);
 
1291
        bvect_full.clear_bit(i);
 
1292
        bvect_full1.set_range(i, i, false);
 
1293
    }
 
1294
 
 
1295
    CheckVectors(bvect_min, bvect_full, ITERATIONS);
 
1296
    CheckVectors(bvect_min, bvect_full1, ITERATIONS);
 
1297
 
 
1298
    for (i = 0; i < ITERATIONS; ++i)
 
1299
    {
 
1300
        bvect_min.clear_bit(i);
 
1301
    }
 
1302
    bvect_full.clear();
 
1303
 
 
1304
    CheckVectors(bvect_min, bvect_full, ITERATIONS);
 
1305
 
 
1306
    cout << "Random step filling" << endl;
 
1307
 
 
1308
    for (i = rand()%10; i < ITERATIONS; i+=rand()%10)
 
1309
    {
 
1310
        bvect_min.clear_bit(i);
 
1311
        bvect_full.clear_bit(i);
 
1312
    }
 
1313
    
 
1314
    CheckVectors(bvect_min, bvect_full, ITERATIONS);
 
1315
 
 
1316
    bvect bv1;
 
1317
    bvect bv2;
 
1318
 
 
1319
    bv1[10] = true;
 
1320
    bv1[1000] = true;
 
1321
 
 
1322
    bv2[200] = bv2[700] = bv2[500] = true;
 
1323
 
 
1324
    bv1.swap(bv2);
 
1325
 
 
1326
    if (bv1.count() != 3)
 
1327
    {
 
1328
        cout << "Swap test failed!" << endl;
 
1329
        exit(1);
 
1330
    }
 
1331
 
 
1332
    if (bv2.count() != 2)
 
1333
    {
 
1334
        cout << "Swap test failed!" << endl;
 
1335
        exit(1);
 
1336
    }
 
1337
}
 
1338
 
 
1339
void SimpleRandomFillTest()
 
1340
{
 
1341
    assert(ITERATIONS < BITVECT_SIZE);
 
1342
 
 
1343
    cout << "-------------------------- SimpleRandomFillTest" << endl;
 
1344
 
 
1345
    {
 
1346
    printf("Simple random fill test 1.");
 
1347
    bvect_mini   bvect_min(BITVECT_SIZE);
 
1348
    bvect      bvect_full;
 
1349
    bvect_full.set_new_blocks_strat(bm::BM_BIT);
 
1350
 
 
1351
 
 
1352
    unsigned iter = ITERATIONS / 5;
 
1353
 
 
1354
    printf("\nSimple Random fill test ITERATIONS = %i\n", iter);
 
1355
 
 
1356
    bvect_min.set_bit(0);
 
1357
    bvect_full.set_bit(0);
 
1358
 
 
1359
    unsigned i;
 
1360
    for (i = 0; i < iter; ++i)
 
1361
    {
 
1362
        unsigned num = ::rand() % iter;
 
1363
        bvect_min.set_bit(num);
 
1364
        bvect_full.set_bit(num);
 
1365
        if ((i % 1000) == 0) cout << "." << flush;
 
1366
        CheckCountRange(bvect_full, 0, num);
 
1367
        CheckCountRange(bvect_full, num, num+iter);
 
1368
    }
 
1369
 
 
1370
    CheckVectors(bvect_min, bvect_full, iter);
 
1371
    CheckCountRange(bvect_full, 0, iter);
 
1372
 
 
1373
    printf("Simple random fill test 2.");
 
1374
 
 
1375
    for(i = 0; i < iter; ++i)
 
1376
    {
 
1377
        unsigned num = ::rand() % iter;
 
1378
        bvect_min.clear_bit(num);
 
1379
        bvect_full.clear_bit(num);
 
1380
    }
 
1381
 
 
1382
    CheckVectors(bvect_min, bvect_full, iter);
 
1383
    }
 
1384
 
 
1385
 
 
1386
    {
 
1387
    printf("\nSimple random fill test 3.\n");
 
1388
    bvect_mini   bvect_min(BITVECT_SIZE);
 
1389
    bvect      bvect_full(bm::BM_GAP);
 
1390
 
 
1391
 
 
1392
    unsigned iter = ITERATIONS;
 
1393
 
 
1394
    printf("\nSimple Random fill test ITERATIONS = %i\n", iter);
 
1395
 
 
1396
    unsigned i;
 
1397
    for(i = 0; i < iter; ++i)
 
1398
    {
 
1399
        unsigned num = ::rand() % iter;
 
1400
        bvect_min.set_bit(num);
 
1401
        bvect_full.set_bit(num);
 
1402
        CheckCountRange(bvect_full, 0, 65535);
 
1403
        CheckCountRange(bvect_full, 0, num);
 
1404
        CheckCountRange(bvect_full, num, num+iter);
 
1405
        if ((i % 1000) == 0) cout << "." << flush;
 
1406
    }
 
1407
 
 
1408
    CheckVectors(bvect_min, bvect_full, iter);
 
1409
 
 
1410
    printf("Simple random fill test 4.");
 
1411
 
 
1412
    for(i = 0; i < iter; ++i)
 
1413
    {
 
1414
        unsigned num = ::rand() % iter;
 
1415
        bvect_min.clear_bit(num);
 
1416
        bvect_full.clear_bit(num);
 
1417
        CheckCountRange(bvect_full, 0, num);
 
1418
        CheckCountRange(bvect_full, num, num+iter);
 
1419
        if ((i % 1000) == 0) cout << "." << flush;
 
1420
    }
 
1421
 
 
1422
    CheckVectors(bvect_min, bvect_full, iter);
 
1423
    CheckCountRange(bvect_full, 0, iter);
 
1424
    }
 
1425
 
 
1426
 
 
1427
}
 
1428
 
 
1429
 
 
1430
 
 
1431
 
 
1432
void RangeRandomFillTest()
 
1433
{
 
1434
    assert(ITERATIONS < BITVECT_SIZE);
 
1435
 
 
1436
    cout << "----------------------------------- RangeRandomFillTest" << endl;
 
1437
 
 
1438
    {
 
1439
    bvect_mini   bvect_min(BITVECT_SIZE);
 
1440
    bvect     bvect_full;
 
1441
 
 
1442
    printf("Range Random fill test\n");
 
1443
 
 
1444
    unsigned min = BITVECT_SIZE / 2;
 
1445
    unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
 
1446
    if (max > BITVECT_SIZE) 
 
1447
        max = BITVECT_SIZE - 1;
 
1448
 
 
1449
    FillSets(&bvect_min, &bvect_full, min, max, 0);
 
1450
 
 
1451
    CheckVectors(bvect_min, bvect_full, BITVECT_SIZE);
 
1452
    CheckCountRange(bvect_full, min, max);
 
1453
 
 
1454
    }
 
1455
 
 
1456
    
 
1457
    {
 
1458
    bvect_mini   bvect_min(BITVECT_SIZE);
 
1459
    bvect     bvect_full;
 
1460
 
 
1461
    printf("Range Random fill test\n");
 
1462
 
 
1463
    unsigned min = BITVECT_SIZE / 2;
 
1464
    unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
 
1465
    if (max > BITVECT_SIZE) 
 
1466
        max = BITVECT_SIZE - 1;
 
1467
 
 
1468
    FillSetsIntervals(&bvect_min, &bvect_full, min, max, 4);
 
1469
 
 
1470
    CheckVectors(bvect_min, bvect_full, BITVECT_SIZE);
 
1471
    CheckCountRange(bvect_full, min, max);
 
1472
    }
 
1473
    
 
1474
 
 
1475
}
 
1476
 
 
1477
 
 
1478
 
 
1479
void AndOperationsTest()
 
1480
{
 
1481
    assert(ITERATIONS < BITVECT_SIZE);
 
1482
 
 
1483
    cout << "----------------------------------- AndOperationTest" << endl;
 
1484
 
 
1485
    {
 
1486
 
 
1487
    bvect_mini   bvect_min1(256);
 
1488
    bvect_mini   bvect_min2(256);
 
1489
    bvect        bvect_full1;
 
1490
    bvect        bvect_full2;
 
1491
 
 
1492
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
1493
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
1494
 
 
1495
 
 
1496
 
 
1497
    printf("AND test\n");
 
1498
 
 
1499
    bvect_min1.set_bit(1);
 
1500
    bvect_min1.set_bit(12);
 
1501
    bvect_min1.set_bit(13);
 
1502
 
 
1503
    bvect_min2.set_bit(12);
 
1504
    bvect_min2.set_bit(13);
 
1505
 
 
1506
    bvect_min1.combine_and(bvect_min2);
 
1507
 
 
1508
    bvect_full1.set_bit(1);
 
1509
    bvect_full1.set_bit(12);
 
1510
    bvect_full1.set_bit(13);
 
1511
 
 
1512
    bvect_full2.set_bit(12);
 
1513
    bvect_full2.set_bit(13);
 
1514
 
 
1515
    bm::id_t predicted_count = bm::count_and(bvect_full1, bvect_full2);
 
1516
 
 
1517
    bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
 
1518
    if (predicted_any == 0 && predicted_count != 0)
 
1519
    {
 
1520
        cout << "Predicted any error!" << endl;
 
1521
        exit(1);
 
1522
    }
 
1523
 
 
1524
    bvect    bv_target_s;
 
1525
    SerializationOperation2Test(&bv_target_s,
 
1526
                                bvect_full1,
 
1527
                                bvect_full2,
 
1528
                                predicted_count,
 
1529
                                set_COUNT_AND,
 
1530
                                set_AND);
 
1531
 
 
1532
 
 
1533
    bvect_full1.bit_and(bvect_full2);
 
1534
 
 
1535
    bm::id_t count = bvect_full1.count();
 
1536
    if (count != predicted_count)
 
1537
    {
 
1538
        cout << "Predicted count error!" << endl;
 
1539
        exit(1);
 
1540
    }
 
1541
 
 
1542
    CheckVectors(bvect_min1, bvect_full1, 256);
 
1543
    CheckVectors(bvect_min1, bv_target_s, 256);
 
1544
    CheckCountRange(bvect_full1, 0, 256);
 
1545
 
 
1546
    }
 
1547
 
 
1548
    {
 
1549
 
 
1550
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
1551
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
1552
    bvect        bvect_full1;
 
1553
    bvect        bvect_full2;
 
1554
 
 
1555
 
 
1556
    printf("AND test stage 1.\n");
 
1557
 
 
1558
    for (int i = 0; i < 112; ++i)
 
1559
    {
 
1560
        bvect_min1.set_bit(i);
 
1561
        bvect_full1.set_bit(i);
 
1562
 
 
1563
        bvect_min2.set_bit(i);
 
1564
        bvect_full2.set_bit(i);
 
1565
 
 
1566
    }
 
1567
 
 
1568
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
 
1569
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
 
1570
 
 
1571
//    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
 
1572
//    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
 
1573
 
 
1574
    bvect_min1.combine_and(bvect_min2);
 
1575
 
 
1576
    bm::id_t predicted_count = bm::count_and(bvect_full1,bvect_full2);
 
1577
    bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
 
1578
    if (predicted_any == 0 && predicted_count != 0)
 
1579
    {
 
1580
        cout << "Predicted any error!" << endl;
 
1581
        exit(1);
 
1582
    }
 
1583
 
 
1584
    bvect    bv_target_s;
 
1585
    SerializationOperation2Test(&bv_target_s,
 
1586
                                bvect_full1,
 
1587
                                bvect_full2,
 
1588
                                predicted_count,
 
1589
                                set_COUNT_AND,
 
1590
                                set_AND);
 
1591
 
 
1592
    bvect_full1.bit_and(bvect_full2);
 
1593
 
 
1594
    bm::id_t count = bvect_full1.count();
 
1595
    if (count != predicted_count)
 
1596
    {
 
1597
        cout << "Predicted count error!" << endl;
 
1598
        exit(1);
 
1599
    }
 
1600
 
 
1601
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
 
1602
    CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10);
 
1603
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
 
1604
 
 
1605
    }
 
1606
 
 
1607
 
 
1608
    {
 
1609
 
 
1610
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
1611
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
1612
    bvect        bvect_full1;
 
1613
    bvect        bvect_full2;
 
1614
 
 
1615
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
1616
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
1617
 
 
1618
    printf("AND test stage 2.\n");
 
1619
 
 
1620
 
 
1621
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
 
1622
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
 
1623
 
 
1624
    bm::id_t predicted_count = bm::count_and(bvect_full1,bvect_full2);
 
1625
    bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
 
1626
    if (predicted_any == 0 && predicted_count != 0)
 
1627
    {
 
1628
        cout << "Predicted any error!" << endl;
 
1629
        exit(1);
 
1630
    }
 
1631
 
 
1632
    bvect    bv_target_s;
 
1633
    SerializationOperation2Test(&bv_target_s,
 
1634
                                bvect_full1,
 
1635
                                bvect_full2,
 
1636
                                predicted_count,
 
1637
                                set_COUNT_AND,
 
1638
                                set_AND);
 
1639
 
 
1640
    bvect_min1.combine_and(bvect_min2);
 
1641
 
 
1642
    bvect_full1.bit_and(bvect_full2);
 
1643
 
 
1644
    bm::id_t count = bvect_full1.count();
 
1645
    if (count != predicted_count)
 
1646
    {
 
1647
        cout << "Predicted count error!" << endl;
 
1648
        bvect_full1.stat();
 
1649
        exit(1);
 
1650
    }
 
1651
 
 
1652
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
 
1653
    CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10);
 
1654
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
 
1655
 
 
1656
    }
 
1657
 
 
1658
    {
 
1659
 
 
1660
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
1661
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
1662
    bvect        bvect_full1;
 
1663
    bvect        bvect_full2;
 
1664
 
 
1665
    bvect_full1.set_new_blocks_strat(bm::BM_BIT);
 
1666
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
 
1667
 
 
1668
    cout << "------------------------------" << endl;
 
1669
    printf("AND test stage 3.\n");
 
1670
 
 
1671
 
 
1672
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
 
1673
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
 
1674
 
 
1675
    bvect_min1.combine_and(bvect_min2);
 
1676
 
 
1677
    bm::id_t predicted_count = bm::count_and(bvect_full1, bvect_full2);
 
1678
    bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
 
1679
    if (predicted_any == 0 && predicted_count != 0)
 
1680
    {
 
1681
        cout << "Predicted any error!" << endl;
 
1682
        exit(1);
 
1683
    }
 
1684
    
 
1685
    bvect    bv_target_s;
 
1686
    SerializationOperation2Test(&bv_target_s,
 
1687
                                bvect_full1,
 
1688
                                bvect_full2,
 
1689
                                predicted_count,
 
1690
                                set_COUNT_AND,
 
1691
                                set_AND);
 
1692
 
 
1693
    bvect_full1.bit_and(bvect_full2);
 
1694
 
 
1695
    bm::id_t count = bvect_full1.count();
 
1696
    if (count != predicted_count)
 
1697
    {
 
1698
        cout << "Predicted count error!" << endl;
 
1699
        exit(1);
 
1700
    }
 
1701
 
 
1702
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
1703
    CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE);
 
1704
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
 
1705
 
 
1706
    bvect_full1.optimize();
 
1707
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
1708
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
 
1709
    CheckCountRange(bvect_full1, BITVECT_SIZE/2, BITVECT_SIZE);
 
1710
 
 
1711
    }
 
1712
 
 
1713
    cout << "------------------------------" << endl;
 
1714
    printf("AND test stage 4. combine_and_sorted\n");
 
1715
    {
 
1716
    unsigned ids[] = {0, 1, 2, 3, 10, 65535, 65536, 65535*2, 65535*3};
 
1717
    unsigned to_add = sizeof(ids)/sizeof(unsigned);
 
1718
    bvect        bvect_full1;
 
1719
    bvect        bvect_full2;    
 
1720
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
1721
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
1722
 
 
1723
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
1724
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
1725
    
 
1726
    for (unsigned i = 2; i < to_add; ++i)
 
1727
    {
 
1728
        bvect_full1.set(ids[i]);
 
1729
        bvect_min1.set_bit(ids[i]);
 
1730
        bvect_full2.set(ids[i]);
 
1731
        bvect_min2.set_bit(ids[i]);
 
1732
    }
 
1733
    
 
1734
    unsigned* first = ids;
 
1735
    unsigned* last = ids + to_add;
 
1736
    
 
1737
    bvect_min1.combine_and(bvect_min2);
 
1738
 
 
1739
    bm::combine_and_sorted(bvect_full1, first, last);
 
1740
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
1741
    }
 
1742
 
 
1743
}
 
1744
 
 
1745
 
 
1746
void OrOperationsTest()
 
1747
{
 
1748
    assert(ITERATIONS < BITVECT_SIZE);
 
1749
 
 
1750
    cout << "----------------------------------- OrOperationTest" << endl;
 
1751
 
 
1752
    {
 
1753
 
 
1754
    bvect_mini   bvect_min1(256);
 
1755
    bvect_mini   bvect_min2(256);
 
1756
    bvect        bvect_full1;
 
1757
    bvect        bvect_full2;
 
1758
 
 
1759
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
1760
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
1761
 
 
1762
 
 
1763
 
 
1764
    printf("OR test\n");
 
1765
 
 
1766
    bvect_min1.set_bit(1);
 
1767
    bvect_min1.set_bit(12);
 
1768
    bvect_min1.set_bit(13);
 
1769
 
 
1770
    bvect_min2.set_bit(12);
 
1771
    bvect_min2.set_bit(13);
 
1772
 
 
1773
    bvect_min1.combine_or(bvect_min2);
 
1774
 
 
1775
    bvect_full1.set_bit(1);
 
1776
    bvect_full1.set_bit(12);
 
1777
    bvect_full1.set_bit(13);
 
1778
 
 
1779
    bvect_full2.set_bit(12);
 
1780
    bvect_full2.set_bit(13);
 
1781
    
 
1782
    bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);    
 
1783
    bm::id_t predicted_any = bm::any_or(bvect_full1, bvect_full2);
 
1784
    if (predicted_any == 0 && predicted_count != 0)
 
1785
    {
 
1786
        cout << "Predicted any error!" << endl;
 
1787
        exit(1);
 
1788
    }
 
1789
 
 
1790
    bvect    bv_target_s;
 
1791
    SerializationOperation2Test(&bv_target_s,
 
1792
                                bvect_full1,
 
1793
                                bvect_full2,
 
1794
                                predicted_count,
 
1795
                                set_COUNT_OR,
 
1796
                                set_OR);
 
1797
 
 
1798
 
 
1799
    bvect_full1.bit_or(bvect_full2);
 
1800
 
 
1801
    bm::id_t count = bvect_full1.count();
 
1802
    if (count != predicted_count)
 
1803
    {
 
1804
        cout << "Predicted count error!" << endl;
 
1805
        cout << predicted_count << " " << count << endl;
 
1806
        bvect_full1.stat();
 
1807
        exit(1);
 
1808
    }
 
1809
 
 
1810
 
 
1811
    CheckVectors(bvect_min1, bvect_full1, 256);
 
1812
    CheckVectors(bvect_min1, bv_target_s, 256);
 
1813
    CheckCountRange(bvect_full1, 0, 256);
 
1814
    CheckCountRange(bvect_full1, 128, 256);
 
1815
    }
 
1816
 
 
1817
    {
 
1818
 
 
1819
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
1820
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
1821
    bvect        bvect_full1;
 
1822
    bvect        bvect_full2;
 
1823
 
 
1824
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
1825
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
1826
 
 
1827
    printf("OR test stage 2.\n");
 
1828
 
 
1829
 
 
1830
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
 
1831
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
 
1832
 
 
1833
    bvect_min1.combine_or(bvect_min2);
 
1834
 
 
1835
    bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);    
 
1836
    bm::id_t predicted_any = bm::any_or(bvect_full1, bvect_full2);
 
1837
    if (predicted_any == 0 && predicted_count != 0)
 
1838
    {
 
1839
        cout << "Predicted any error!" << endl;
 
1840
        exit(1);
 
1841
    }
 
1842
 
 
1843
    bvect    bv_target_s;
 
1844
    SerializationOperation2Test(&bv_target_s,
 
1845
                                bvect_full1,
 
1846
                                bvect_full2,
 
1847
                                predicted_count,
 
1848
                                set_COUNT_OR,
 
1849
                                set_OR);
 
1850
 
 
1851
 
 
1852
    bvect_full1.bit_or(bvect_full2);
 
1853
 
 
1854
    bm::id_t count = bvect_full1.count();
 
1855
    if (count != predicted_count)
 
1856
    {
 
1857
        cout << "Predicted count error!" << endl;
 
1858
        exit(1);
 
1859
    }
 
1860
 
 
1861
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
 
1862
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
 
1863
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
 
1864
 
 
1865
    }
 
1866
 
 
1867
    {
 
1868
 
 
1869
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
1870
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
1871
    bvect        bvect_full1;
 
1872
    bvect        bvect_full2;
 
1873
 
 
1874
    bvect_full1.set_new_blocks_strat(bm::BM_BIT);
 
1875
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
 
1876
 
 
1877
    cout << "------------------------------" << endl;
 
1878
    printf("OR test stage 3.\n");
 
1879
 
 
1880
 
 
1881
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
 
1882
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
 
1883
 
 
1884
    bvect_min1.combine_or(bvect_min2);
 
1885
    
 
1886
    bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);    
 
1887
    bm::id_t predicted_any = bm::any_or(bvect_full1, bvect_full2);
 
1888
    if (predicted_any == 0 && predicted_count != 0)
 
1889
    {
 
1890
        cout << "Predicted any error!" << endl;
 
1891
        exit(1);
 
1892
    }
 
1893
 
 
1894
    bvect    bv_target_s;
 
1895
    SerializationOperation2Test(&bv_target_s,
 
1896
                                bvect_full1,
 
1897
                                bvect_full2,
 
1898
                                predicted_count,
 
1899
                                set_COUNT_OR,
 
1900
                                set_OR);
 
1901
 
 
1902
    bvect_full1.bit_or(bvect_full2);
 
1903
 
 
1904
    bm::id_t count = bvect_full1.count();
 
1905
    if (count != predicted_count)
 
1906
    {
 
1907
        cout << "Predicted count error!" << endl;
 
1908
        exit(1);
 
1909
    }
 
1910
 
 
1911
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
1912
 
 
1913
    bvect_full1.optimize();
 
1914
 
 
1915
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
1916
    CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE);
 
1917
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
 
1918
 
 
1919
 
 
1920
    }
 
1921
    
 
1922
    cout << "Testing combine_or" << endl;
 
1923
    
 
1924
    {
 
1925
    
 
1926
    bvect        bvect_full1;
 
1927
    bvect        bvect_full2;
 
1928
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
1929
    
 
1930
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
1931
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
1932
 
 
1933
    unsigned ids[10000];
 
1934
    unsigned to_add = 10000;
 
1935
    
 
1936
    unsigned bn = 0;
 
1937
    for (unsigned i = 0; i < to_add; ++i)
 
1938
    {
 
1939
        ids[i] = bn;
 
1940
        bvect_full2.set(bn);
 
1941
        bvect_min1.set_bit(bn);
 
1942
        bn += 15;
 
1943
    }
 
1944
    
 
1945
    unsigned* first = ids;
 
1946
    unsigned* last = ids + to_add;
 
1947
    
 
1948
    bm::combine_or(bvect_full1, first, last);
 
1949
 
 
1950
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
1951
    
 
1952
    bm::combine_or(bvect_full1, first, last);
 
1953
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
1954
    
 
1955
    }
 
1956
    
 
1957
    
 
1958
    {
 
1959
    unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
 
1960
    unsigned to_add = sizeof(ids)/sizeof(unsigned);
 
1961
    bvect        bvect_full1;
 
1962
    bvect        bvect_full2;    
 
1963
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
1964
 
 
1965
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
1966
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
1967
    
 
1968
    unsigned bn = 0;
 
1969
    for (unsigned i = 0; i < to_add; ++i)
 
1970
    {
 
1971
        ids[i] = bn;
 
1972
        bvect_full2.set(bn);
 
1973
        bvect_min1.set_bit(bn);
 
1974
        bn += 15;
 
1975
    }
 
1976
    
 
1977
    unsigned* first = ids;
 
1978
    unsigned* last = ids + to_add;
 
1979
    
 
1980
    bm::combine_or(bvect_full1, first, last);
 
1981
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
1982
 
 
1983
    bm::combine_or(bvect_full1, first, last);
 
1984
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);    
 
1985
    }
 
1986
    
 
1987
 
 
1988
}
 
1989
 
 
1990
 
 
1991
 
 
1992
void SubOperationsTest()
 
1993
{
 
1994
    assert(ITERATIONS < BITVECT_SIZE);
 
1995
 
 
1996
    cout << "----------------------------------- SubOperationTest" << endl;
 
1997
 
 
1998
    {
 
1999
 
 
2000
    bvect_mini   bvect_min1(256);
 
2001
    bvect_mini   bvect_min2(256);
 
2002
    bvect        bvect_full1;
 
2003
    bvect        bvect_full2;
 
2004
 
 
2005
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
2006
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
2007
 
 
2008
 
 
2009
 
 
2010
    printf("SUB test\n");
 
2011
 
 
2012
    bvect_min1.set_bit(1);
 
2013
    bvect_min1.set_bit(12);
 
2014
    bvect_min1.set_bit(13);
 
2015
 
 
2016
    bvect_min2.set_bit(12);
 
2017
    bvect_min2.set_bit(13);
 
2018
 
 
2019
    bvect_min1.combine_sub(bvect_min2);
 
2020
 
 
2021
    bvect_full1.set_bit(1);
 
2022
    bvect_full1.set_bit(12);
 
2023
    bvect_full1.set_bit(13);
 
2024
 
 
2025
    bvect_full2.set_bit(12);
 
2026
    bvect_full2.set_bit(13);
 
2027
 
 
2028
    bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
 
2029
    bm::id_t predicted_any = bm::any_sub(bvect_full1, bvect_full2);
 
2030
    if (predicted_any == 0 && predicted_count != 0)
 
2031
    {
 
2032
        cout << "Predicted any error!" << endl;
 
2033
        exit(1);
 
2034
    }
 
2035
 
 
2036
    bvect    bv_target_s;
 
2037
    SerializationOperation2Test(&bv_target_s,
 
2038
                                bvect_full1,
 
2039
                                bvect_full2,
 
2040
                                predicted_count,
 
2041
                                set_COUNT_SUB_AB,
 
2042
                                set_SUB);
 
2043
 
 
2044
 
 
2045
    bvect_full1.bit_sub(bvect_full2);
 
2046
    
 
2047
    bm::id_t count = bvect_full1.count();
 
2048
    if (count != predicted_count)
 
2049
    {
 
2050
        cout << "Predicted count error!" << endl;
 
2051
        exit(1);
 
2052
    }
 
2053
 
 
2054
    CheckVectors(bvect_min1, bvect_full1, 256);
 
2055
    CheckVectors(bvect_min1, bv_target_s, 256);
 
2056
    CheckCountRange(bvect_full1, 0, 256);
 
2057
 
 
2058
    }
 
2059
 
 
2060
    {
 
2061
 
 
2062
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
2063
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
2064
    bvect        bvect_full1;
 
2065
    bvect        bvect_full2;
 
2066
 
 
2067
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
2068
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
2069
 
 
2070
    printf("SUB test stage 2.\n");
 
2071
 
 
2072
 
 
2073
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
 
2074
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
 
2075
 
 
2076
    bvect_min1.combine_sub(bvect_min2);
 
2077
 
 
2078
    bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
 
2079
    bm::id_t predicted_any = bm::any_sub(bvect_full1, bvect_full2);
 
2080
    if (predicted_any == 0 && predicted_count != 0)
 
2081
    {
 
2082
        cout << "Predicted any error!" << endl;
 
2083
        exit(1);
 
2084
    }
 
2085
 
 
2086
    bvect    bv_target_s;
 
2087
    SerializationOperation2Test(&bv_target_s,
 
2088
                                bvect_full1,
 
2089
                                bvect_full2,
 
2090
                                predicted_count,
 
2091
                                set_COUNT_SUB_AB,
 
2092
                                set_SUB);
 
2093
 
 
2094
    bvect_full1.bit_sub(bvect_full2);
 
2095
    
 
2096
    bm::id_t count = bvect_full1.count();
 
2097
    if (count != predicted_count)
 
2098
    {
 
2099
        cout << "Predicted count error!" << endl;
 
2100
        cout << predicted_count << " " << count << endl;
 
2101
bvect_full1.stat();    
 
2102
        
 
2103
        exit(1);
 
2104
    }
 
2105
    
 
2106
 
 
2107
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
 
2108
    CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10);
 
2109
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
 
2110
 
 
2111
    }
 
2112
 
 
2113
    {
 
2114
 
 
2115
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
2116
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
2117
    bvect        bvect_full1;
 
2118
    bvect        bvect_full2;
 
2119
 
 
2120
    bvect_full1.set_new_blocks_strat(bm::BM_BIT);
 
2121
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
 
2122
 
 
2123
    cout << "------------------------------" << endl;
 
2124
    printf("SUB test stage 3.\n");
 
2125
 
 
2126
 
 
2127
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
 
2128
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
 
2129
 
 
2130
    bvect_min1.combine_sub(bvect_min2);
 
2131
    
 
2132
    bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
 
2133
    bm::id_t predicted_any = bm::any_sub(bvect_full1, bvect_full2);
 
2134
    if (predicted_any == 0 && predicted_count != 0)
 
2135
    {
 
2136
        cout << "Predicted any error!" << endl;
 
2137
        exit(1);
 
2138
    }
 
2139
 
 
2140
    bvect    bv_target_s;
 
2141
    SerializationOperation2Test(&bv_target_s,
 
2142
                                bvect_full1,
 
2143
                                bvect_full2,
 
2144
                                predicted_count,
 
2145
                                set_COUNT_SUB_AB,
 
2146
                                set_SUB);
 
2147
 
 
2148
    bvect_full1.bit_sub(bvect_full2);
 
2149
 
 
2150
    bm::id_t count = bvect_full1.count();
 
2151
    if (count != predicted_count)
 
2152
    {
 
2153
        cout << "Predicted count error!" << endl;
 
2154
        exit(1);
 
2155
    }
 
2156
 
 
2157
 
 
2158
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
2159
 
 
2160
    bvect_full1.optimize();
 
2161
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
2162
    CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE);
 
2163
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
 
2164
 
 
2165
    }
 
2166
 
 
2167
}
 
2168
 
 
2169
 
 
2170
 
 
2171
void XorOperationsTest()
 
2172
{
 
2173
    assert(ITERATIONS < BITVECT_SIZE);
 
2174
 
 
2175
    cout << "----------------------------------- XorOperationTest" << endl;
 
2176
    {
 
2177
 
 
2178
    bvect_mini   bvect_min1(256);
 
2179
    bvect_mini   bvect_min2(256);
 
2180
    bvect        bvect_full1;
 
2181
    bvect        bvect_full2;
 
2182
 
 
2183
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
2184
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
2185
 
 
2186
 
 
2187
 
 
2188
    printf("XOR test\n");
 
2189
 
 
2190
    bvect_min1.set_bit(1);
 
2191
    bvect_min1.set_bit(12);
 
2192
    bvect_min1.set_bit(13);
 
2193
 
 
2194
    bvect_min2.set_bit(12);
 
2195
    bvect_min2.set_bit(13);
 
2196
 
 
2197
    bvect_min1.combine_xor(bvect_min2);
 
2198
 
 
2199
    bvect_full1.set_bit(1);
 
2200
    bvect_full1.set_bit(12);
 
2201
    bvect_full1.set_bit(13);
 
2202
 
 
2203
    bvect_full2.set_bit(12);
 
2204
    bvect_full2.set_bit(13);
 
2205
 
 
2206
    bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
 
2207
    bm::id_t predicted_any = bm::any_xor(bvect_full1, bvect_full2);
 
2208
    if (predicted_any == 0 && predicted_count != 0)
 
2209
    {
 
2210
        cout << "Predicted any error!" << endl;
 
2211
        exit(1);
 
2212
    }
 
2213
 
 
2214
    bvect    bv_target_s;
 
2215
    SerializationOperation2Test(&bv_target_s,
 
2216
                                bvect_full1,
 
2217
                                bvect_full2,
 
2218
                                predicted_count,
 
2219
                                set_COUNT_XOR,
 
2220
                                set_XOR);
 
2221
 
 
2222
 
 
2223
    bvect_full1.bit_xor(bvect_full2);
 
2224
 
 
2225
    bm::id_t count = bvect_full1.count();
 
2226
    if (count != predicted_count)
 
2227
    {
 
2228
        cout << "1.Predicted count error!" << endl;
 
2229
        exit(1);
 
2230
    }
 
2231
 
 
2232
    CheckVectors(bvect_min1, bvect_full1, 256);
 
2233
    CheckVectors(bvect_min1, bv_target_s, 256);
 
2234
    CheckCountRange(bvect_full1, 0, 256);
 
2235
    CheckCountRange(bvect_full1, 128, 256);
 
2236
 
 
2237
    }
 
2238
    {
 
2239
        bvect  bvect1;
 
2240
        bvect_mini  bvect_min1(BITVECT_SIZE);
 
2241
 
 
2242
        bvect  bvect2;
 
2243
        bvect_mini  bvect_min2(BITVECT_SIZE);
 
2244
 
 
2245
 
 
2246
        for (int i = 0; i < 150000; ++i)
 
2247
        {
 
2248
            bvect2.set_bit(i);
 
2249
            bvect_min2.set_bit(i);
 
2250
        }
 
2251
 
 
2252
        bvect2.optimize();
 
2253
 
 
2254
        bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
 
2255
        bm::id_t predicted_any = bm::any_xor(bvect1, bvect2);
 
2256
        if (predicted_any == 0 && predicted_count != 0)
 
2257
        {
 
2258
            cout << "Predicted any error!" << endl;
 
2259
            exit(1);
 
2260
        }
 
2261
 
 
2262
        bvect    bv_target_s;
 
2263
        SerializationOperation2Test(&bv_target_s,
 
2264
                                    bvect1,
 
2265
                                    bvect2,
 
2266
                                    predicted_count,
 
2267
                                    set_COUNT_XOR,
 
2268
                                    set_XOR);
 
2269
 
 
2270
        bvect1.bit_xor(bvect2);
 
2271
        
 
2272
        bm::id_t count = bvect1.count();
 
2273
        if (count != predicted_count)
 
2274
        {
 
2275
            cout << "2.Predicted count error!" << endl;
 
2276
            exit(1);
 
2277
        }
 
2278
        
 
2279
        bvect_min1.combine_xor(bvect_min2);
 
2280
        CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, true);
 
2281
        CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE, true);
 
2282
        CheckCountRange(bvect1, 0, BITVECT_SIZE);
 
2283
    }
 
2284
 
 
2285
 
 
2286
    {
 
2287
        bvect  bvect1;
 
2288
        bvect_mini  bvect_min1(BITVECT_SIZE);
 
2289
 
 
2290
        bvect  bvect2;
 
2291
        bvect_mini  bvect_min2(BITVECT_SIZE);
 
2292
 
 
2293
 
 
2294
        for (int i = 0; i < 150000; ++i)
 
2295
        {
 
2296
            bvect1.set_bit(i);
 
2297
            bvect_min1.set_bit(i);
 
2298
        }
 
2299
 
 
2300
        bvect1.optimize();
 
2301
        
 
2302
        bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
 
2303
        bm::id_t predicted_any = bm::any_xor(bvect1, bvect2);
 
2304
        if (predicted_any == 0 && predicted_count != 0)
 
2305
        {
 
2306
            cout << "Predicted any error!" << endl;
 
2307
            exit(1);
 
2308
        }
 
2309
 
 
2310
        bvect    bv_target_s;
 
2311
        SerializationOperation2Test(&bv_target_s,
 
2312
                                    bvect1,
 
2313
                                    bvect2,
 
2314
                                    predicted_count,
 
2315
                                    set_COUNT_XOR,
 
2316
                                    set_XOR);
 
2317
 
 
2318
        bvect1.bit_xor(bvect2);
 
2319
 
 
2320
        bm::id_t count = bvect1.count();
 
2321
        if (count != predicted_count)
 
2322
        {
 
2323
            cout << "3.Predicted count error!" << endl;
 
2324
            exit(1);
 
2325
        }
 
2326
        
 
2327
        bvect_min1.combine_xor(bvect_min2);
 
2328
        CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, true);
 
2329
        CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE, true);
 
2330
    }
 
2331
 
 
2332
 
 
2333
    {
 
2334
        bvect  bvect1;
 
2335
        bvect_mini  bvect_min1(BITVECT_SIZE);
 
2336
 
 
2337
        bvect  bvect2;
 
2338
        bvect_mini  bvect_min2(BITVECT_SIZE);
 
2339
 
 
2340
 
 
2341
        for (int i = 0; i < 150000; ++i)
 
2342
        {
 
2343
            bvect1.set_bit(i);
 
2344
            bvect_min1.set_bit(i);
 
2345
            bvect2.set_bit(i);
 
2346
            bvect_min2.set_bit(i);
 
2347
        }
 
2348
 
 
2349
        bvect1.optimize();
 
2350
        
 
2351
        bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
 
2352
        bm::id_t predicted_any = bm::any_xor(bvect1, bvect2);
 
2353
        if (predicted_any == 0 && predicted_count != 0)
 
2354
        {
 
2355
            cout << "Predicted any error!" << endl;
 
2356
            exit(1);
 
2357
        }
 
2358
 
 
2359
        bvect    bv_target_s;
 
2360
        SerializationOperation2Test(&bv_target_s,
 
2361
                                    bvect1,
 
2362
                                    bvect2,
 
2363
                                    predicted_count,
 
2364
                                    set_COUNT_XOR,
 
2365
                                    set_XOR);
 
2366
 
 
2367
        bvect1.bit_xor(bvect2);
 
2368
 
 
2369
        bm::id_t count = bvect1.count();
 
2370
        if (count != predicted_count)
 
2371
        {
 
2372
            cout << "4.Predicted count error!" << endl;
 
2373
            cout << count << " " << predicted_count << endl;
 
2374
            
 
2375
            exit(1);
 
2376
        }
 
2377
        
 
2378
        bvect_min1.combine_xor(bvect_min2);
 
2379
        CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, true);
 
2380
    }
 
2381
 
 
2382
 
 
2383
 
 
2384
    {
 
2385
 
 
2386
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
2387
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
2388
    bvect        bvect_full1;
 
2389
    bvect        bvect_full2;
 
2390
 
 
2391
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
2392
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
2393
 
 
2394
    printf("XOR test stage 2.\n");
 
2395
 
 
2396
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
 
2397
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
 
2398
 
 
2399
    bvect_min1.combine_xor(bvect_min2);
 
2400
    
 
2401
    bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
 
2402
    bm::id_t predicted_any = bm::any_xor(bvect_full1, bvect_full2);
 
2403
    if (predicted_any == 0 && predicted_count != 0)
 
2404
    {
 
2405
        cout << "Predicted any error!" << endl;
 
2406
        exit(1);
 
2407
    }
 
2408
 
 
2409
    bvect    bv_target_s;
 
2410
    SerializationOperation2Test(&bv_target_s,
 
2411
                                bvect_full1,
 
2412
                                bvect_full2,
 
2413
                                predicted_count,
 
2414
                                set_COUNT_XOR,
 
2415
                                set_XOR);
 
2416
 
 
2417
 
 
2418
    bvect_full1.bit_xor(bvect_full2);
 
2419
    
 
2420
    bm::id_t count = bvect_full1.count();
 
2421
    if (count != predicted_count)
 
2422
    {
 
2423
        cout << "5.Predicted count error!" << endl;
 
2424
        cout << count << " " << predicted_count << endl;
 
2425
        bvect_full1.stat();
 
2426
        exit(1);
 
2427
    }
 
2428
 
 
2429
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
 
2430
    CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10);
 
2431
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
 
2432
 
 
2433
    }
 
2434
 
 
2435
    {
 
2436
 
 
2437
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
2438
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
2439
    bvect        bvect_full1;
 
2440
    bvect        bvect_full2;
 
2441
 
 
2442
    bvect_full1.set_new_blocks_strat(bm::BM_BIT);
 
2443
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
 
2444
 
 
2445
    cout << "------------------------------" << endl;
 
2446
    printf("XOR test stage 3.\n");
 
2447
 
 
2448
 
 
2449
    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
 
2450
    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
 
2451
 
 
2452
    bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
 
2453
    bm::id_t predicted_any = bm::any_xor(bvect_full1, bvect_full2);
 
2454
    if (predicted_any == 0 && predicted_count != 0)
 
2455
    {
 
2456
        cout << "Predicted any error!" << endl;
 
2457
        exit(1);
 
2458
    }
 
2459
 
 
2460
    bvect    bv_target_s;
 
2461
    SerializationOperation2Test(&bv_target_s,
 
2462
                                bvect_full1,
 
2463
                                bvect_full2,
 
2464
                                predicted_count,
 
2465
                                set_COUNT_XOR,
 
2466
                                set_XOR);
 
2467
 
 
2468
    bvect_min1.combine_xor(bvect_min2);
 
2469
 
 
2470
    bvect_full1.bit_xor(bvect_full2);
 
2471
 
 
2472
    bm::id_t count = bvect_full1.count();
 
2473
    if (count != predicted_count)
 
2474
    {
 
2475
        cout << "6.Predicted count error!" << endl;
 
2476
        exit(1);
 
2477
    }
 
2478
 
 
2479
 
 
2480
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
2481
 
 
2482
    bvect_full1.optimize();
 
2483
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
2484
    CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE);
 
2485
    CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
 
2486
 
 
2487
 
 
2488
    }
 
2489
 
 
2490
 
 
2491
    cout << "Testing combine_xor" << endl;
 
2492
    
 
2493
    {
 
2494
    
 
2495
    bvect        bvect_full1;
 
2496
    bvect        bvect_full2;
 
2497
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
2498
    
 
2499
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
2500
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
2501
 
 
2502
    unsigned ids[10000];
 
2503
    unsigned to_add = 10000;
 
2504
    
 
2505
    unsigned bn = 0;
 
2506
    for (unsigned i = 0; i < to_add; ++i)
 
2507
    {
 
2508
        ids[i] = bn;
 
2509
        bvect_full2.set(bn);
 
2510
        bvect_min1.set_bit(bn);
 
2511
        bn += 15;
 
2512
    }
 
2513
    
 
2514
    unsigned* first = ids;
 
2515
    unsigned* last = ids + to_add;
 
2516
    
 
2517
    bm::combine_xor(bvect_full1, first, last);
 
2518
 
 
2519
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
2520
    
 
2521
    bm::combine_xor(bvect_full1, first, last);
 
2522
    if (bvect_full1.count())
 
2523
    {
 
2524
        cout << "combine_xor count failed!" << endl;
 
2525
        exit(1);
 
2526
    }
 
2527
    
 
2528
    }
 
2529
 
 
2530
    {
 
2531
    
 
2532
    bvect        bvect_full1;
 
2533
    bvect        bvect_full2;
 
2534
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
2535
    
 
2536
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
2537
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
2538
 
 
2539
    unsigned ids[10000]={0,};
 
2540
    unsigned to_add = 10000;
 
2541
    
 
2542
    for (unsigned i = 0; i < to_add; i+=100)
 
2543
    {
 
2544
        ids[i] = i;
 
2545
        bvect_full2.set(i);
 
2546
        bvect_min1.set_bit(i);
 
2547
    }
 
2548
    unsigned* first = ids;
 
2549
    unsigned* last = ids + to_add;
 
2550
    
 
2551
    bm::combine_xor(bvect_full1, first, last);
 
2552
 
 
2553
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
2554
    
 
2555
    bm::combine_xor(bvect_full1, first, last);
 
2556
    if (bvect_full1.count())
 
2557
    {
 
2558
        cout << "combine_xor count failed!" << endl;
 
2559
        exit(1);
 
2560
    }
 
2561
    
 
2562
    }
 
2563
 
 
2564
    
 
2565
    {
 
2566
    unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
 
2567
    unsigned to_add = sizeof(ids)/sizeof(unsigned);
 
2568
    bvect        bvect_full1;
 
2569
    bvect        bvect_full2;    
 
2570
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
2571
 
 
2572
    bvect_full1.set_new_blocks_strat(bm::BM_BIT);
 
2573
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
 
2574
    
 
2575
    unsigned bn = 0;
 
2576
    for (unsigned i = 0; i < to_add; ++i)
 
2577
    {
 
2578
        ids[i] = bn;
 
2579
        bvect_full2.set(bn);
 
2580
        bvect_min1.set_bit(bn);
 
2581
        bn += 15;
 
2582
    }
 
2583
    
 
2584
    unsigned* first = ids;
 
2585
    unsigned* last = ids + to_add;
 
2586
    
 
2587
    bm::combine_xor(bvect_full1, first, last);
 
2588
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
2589
 
 
2590
    bm::combine_xor(bvect_full1, first, last);
 
2591
    if (bvect_full1.count())
 
2592
    {
 
2593
        cout << "combine_xor count failed!" << endl;
 
2594
        exit(1);
 
2595
    }
 
2596
    }
 
2597
    
 
2598
    
 
2599
    {
 
2600
    unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
 
2601
    unsigned to_add = sizeof(ids)/sizeof(unsigned);
 
2602
    bvect        bvect_full1;
 
2603
    bvect        bvect_full2;    
 
2604
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
2605
 
 
2606
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
2607
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
2608
    
 
2609
    unsigned bn = 0;
 
2610
    for (unsigned i = 0; i < to_add; ++i)
 
2611
    {
 
2612
        ids[i] = bn;
 
2613
        bvect_full2.set(bn);
 
2614
        bvect_min1.set_bit(bn);
 
2615
        bn += 15;
 
2616
    }
 
2617
    
 
2618
    unsigned* first = ids;
 
2619
    unsigned* last = ids + to_add;
 
2620
    
 
2621
    bm::combine_xor(bvect_full1, first, last);
 
2622
    CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
 
2623
 
 
2624
    bm::combine_xor(bvect_full1, first, last);
 
2625
    if (bvect_full1.count())
 
2626
    {
 
2627
        cout << "combine_xor count failed!" << endl;
 
2628
        exit(1);
 
2629
    }
 
2630
    }
 
2631
 
 
2632
}
 
2633
 
 
2634
 
 
2635
void ComparisonTest()
 
2636
{
 
2637
    cout << "-------------------------------------- ComparisonTest" << endl;
 
2638
 
 
2639
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
2640
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
2641
    bvect        bvect_full1;
 
2642
    bvect        bvect_full2;
 
2643
    int res1, res2;
 
2644
 
 
2645
    bvect_full1.set_bit(31); 
 
2646
    bvect_full2.set_bit(63); 
 
2647
 
 
2648
    res1 = bvect_full1.compare(bvect_full2);
 
2649
    if (res1 != 1)
 
2650
    {
 
2651
        printf("Comparison test failed 1\n");
 
2652
        exit(1);
 
2653
    }
 
2654
 
 
2655
    bvect_full1.clear();
 
2656
    bvect_full2.clear();
 
2657
 
 
2658
    bvect_min1.set_bit(10);
 
2659
    bvect_min2.set_bit(10);
 
2660
 
 
2661
    bvect_full1.set_bit(10);
 
2662
    bvect_full2.set_bit(10);
 
2663
 
 
2664
    res1 = bvect_min1.compare(bvect_min2);
 
2665
    res2 = bvect_full1.compare(bvect_full2);
 
2666
 
 
2667
    if (res1 != res2)
 
2668
    {
 
2669
        printf("Comparison test failed 1\n");
 
2670
        exit(1);
 
2671
    }
 
2672
 
 
2673
    printf("Comparison 2.\n");
 
2674
 
 
2675
    bvect_min1.set_bit(11);
 
2676
    bvect_full1.set_bit(11);
 
2677
 
 
2678
    res1 = bvect_min1.compare(bvect_min2);
 
2679
    res2 = bvect_full1.compare(bvect_full2);
 
2680
 
 
2681
    if (res1 != res2 && res1 != 1)
 
2682
    {
 
2683
        printf("Comparison test failed 2\n");
 
2684
        exit(1);
 
2685
    }
 
2686
 
 
2687
    res1 = bvect_min2.compare(bvect_min1);
 
2688
    res2 = bvect_full2.compare(bvect_full1);
 
2689
 
 
2690
    if (res1 != res2 && res1 != -1)
 
2691
    {
 
2692
        printf("Comparison test failed 2.1\n");
 
2693
        exit(1);
 
2694
    }
 
2695
 
 
2696
    printf("Comparison 3.\n");
 
2697
 
 
2698
    bvect_full1.optimize();
 
2699
 
 
2700
    res1 = bvect_min1.compare(bvect_min2);
 
2701
    res2 = bvect_full1.compare(bvect_full2);
 
2702
 
 
2703
    if (res1 != res2 && res1 != 1)
 
2704
    {
 
2705
        printf("Comparison test failed 3\n");
 
2706
        exit(1);
 
2707
    }
 
2708
 
 
2709
    res1 = bvect_min2.compare(bvect_min1);
 
2710
    res2 = bvect_full2.compare(bvect_full1);
 
2711
 
 
2712
    if (res1 != res2 && res1 != -1)
 
2713
    {
 
2714
        printf("Comparison test failed 3.1\n");
 
2715
        exit(1);
 
2716
    }
 
2717
 
 
2718
    printf("Comparison 4.\n");
 
2719
 
 
2720
    bvect_full2.optimize();
 
2721
 
 
2722
    res1 = bvect_min1.compare(bvect_min2);
 
2723
    res2 = bvect_full1.compare(bvect_full2);
 
2724
 
 
2725
    if (res1 != res2 && res1 != 1)
 
2726
    {
 
2727
        printf("Comparison test failed 4\n");
 
2728
        exit(1);
 
2729
    }
 
2730
 
 
2731
    res1 = bvect_min2.compare(bvect_min1);
 
2732
    res2 = bvect_full2.compare(bvect_full1);
 
2733
 
 
2734
    if (res1 != res2 && res1 != -1)
 
2735
    {
 
2736
        printf("Comparison test failed 4.1\n");
 
2737
        exit(1);
 
2738
    }
 
2739
 
 
2740
    printf("Comparison 5.\n");
 
2741
 
 
2742
    unsigned i;
 
2743
    for (i = 0; i < 65536; ++i)
 
2744
    {
 
2745
        bvect_full1.set_bit(i);
 
2746
    }
 
2747
 
 
2748
    res1 = bvect_min1.compare(bvect_min2);
 
2749
    res2 = bvect_full1.compare(bvect_full2);
 
2750
 
 
2751
    if (res1 != res2 && res1 != 1)
 
2752
    {
 
2753
        printf("Comparison test failed 5\n");
 
2754
        exit(1);
 
2755
    }
 
2756
 
 
2757
    bvect_full1.optimize();
 
2758
 
 
2759
    res1 = bvect_min2.compare(bvect_min1);
 
2760
    res2 = bvect_full2.compare(bvect_full1);
 
2761
 
 
2762
    if (res1 != res2 && res1 != -1)
 
2763
    {
 
2764
        printf("Comparison test failed 5.1\n");
 
2765
        exit(1);
 
2766
    }
 
2767
 
 
2768
}
 
2769
 
 
2770
void DesrializationTest2()
 
2771
{
 
2772
   bvect  bvtotal;
 
2773
   unsigned size = BITVECT_SIZE - 10;
 
2774
 
 
2775
 
 
2776
   bvect  bv1;
 
2777
   bvect  bv2;
 
2778
   int i;
 
2779
   for (i = 10; i < 165536; i+=2)
 
2780
   {
 
2781
      bv1.set_bit(i);
 
2782
   }
 
2783
 
 
2784
   bv1.optimize();
 
2785
   bv1.stat();
 
2786
 
 
2787
   struct bvect::statistics st1;
 
2788
   bv1.calc_stat(&st1);
 
2789
 
 
2790
 
 
2791
   unsigned char* sermem = new unsigned char[st1.max_serialize_mem];
 
2792
 
 
2793
   unsigned slen2 = bm::serialize(bv1, sermem);
 
2794
   assert(slen2);
 
2795
   slen2 = 0;
 
2796
 
 
2797
   bm::deserialize(bvtotal, sermem);
 
2798
    bvect  bv_target_s;
 
2799
    operation_deserializer<bvect>::deserialize(bv_target_s,
 
2800
                                                sermem,
 
2801
                                                0,
 
2802
                                                set_OR);
 
2803
 
 
2804
   bvtotal.optimize();
 
2805
   int res = bvtotal.compare(bv_target_s);
 
2806
   if (res != 0)
 
2807
   {
 
2808
       cout << "Operation deserialization error 1" << endl;
 
2809
       exit(1);
 
2810
   }
 
2811
 
 
2812
   for (i = 55000; i < 165536; ++i)
 
2813
   {
 
2814
      bv2.set_bit(i);
 
2815
   }
 
2816
   bv2.optimize();
 
2817
   bv2.stat();
 
2818
 
 
2819
   struct bvect::statistics st2;
 
2820
   bv2.calc_stat(&st2);
 
2821
 
 
2822
   unsigned char* sermem2 = new unsigned char[st2.max_serialize_mem];
 
2823
 
 
2824
   unsigned slen = bm::serialize(bv2, sermem2);
 
2825
   assert(slen);
 
2826
   slen = 0;
 
2827
 
 
2828
   bm::deserialize(bvtotal, sermem2);
 
2829
   bvtotal.stat();
 
2830
    operation_deserializer<bvect>::deserialize(bv_target_s,
 
2831
                                               sermem2,
 
2832
                                               0,
 
2833
                                               set_OR);
 
2834
    res = bvtotal.compare(bv_target_s);
 
2835
    if (res != 0)
 
2836
    {
 
2837
        cout << "Operation deserialization error 2" << endl;
 
2838
        exit(1);
 
2839
    }
 
2840
 
 
2841
//   bvtotal.optimize();
 
2842
 //  bvtotal.stat();
 
2843
 
 
2844
   bm::deserialize(bvtotal, sermem2);
 
2845
 
 
2846
   bm::deserialize(bvtotal, sermem);
 
2847
 
 
2848
    operation_deserializer<bvect>::deserialize(bv_target_s,
 
2849
                                               sermem2,
 
2850
                                               0,
 
2851
                                               set_OR);
 
2852
    operation_deserializer<bvect>::deserialize(bv_target_s,
 
2853
                                               sermem,
 
2854
                                               0,
 
2855
                                               set_OR);
 
2856
 
 
2857
    res = bvtotal.compare(bv_target_s);
 
2858
    if (res != 0)
 
2859
    {
 
2860
        cout << "Deserialization test failed! 3" << endl;
 
2861
        exit(1);
 
2862
    }
 
2863
 
 
2864
   delete [] sermem;
 
2865
   delete [] sermem2;
 
2866
 
 
2867
 
 
2868
   bvtotal.clear();
 
2869
   bv_target_s.clear(false);
 
2870
 
 
2871
   int clcnt = 0;
 
2872
 
 
2873
   int repetitions = 25;
 
2874
   for (i = 0; i < repetitions; ++i)
 
2875
   {
 
2876
        cout << endl << "Deserialization STEP " << i << endl;
 
2877
 
 
2878
        bvect_mini*   bvect_min1= new bvect_mini(size);
 
2879
        bvect*        bvect_full1= new bvect();
 
2880
 
 
2881
        FillSetsRandomMethod(bvect_min1, bvect_full1, 1, size, 1);
 
2882
 
 
2883
       struct bvect::statistics st;
 
2884
       bvect_full1->calc_stat(&st);
 
2885
 
 
2886
       unsigned char* sermem = new unsigned char[st.max_serialize_mem];
 
2887
 
 
2888
       unsigned slen = bm::serialize(*bvect_full1, sermem);
 
2889
 
 
2890
       unsigned char* smem = new unsigned char[slen];
 
2891
       ::memcpy(smem, sermem, slen);
 
2892
 
 
2893
//       cout << "Serialized vector" << endl;
 
2894
//       bvect_full1->stat();
 
2895
 
 
2896
//       cout << "Before deserialization" << endl;
 
2897
//       bvtotal.stat();
 
2898
       bm::deserialize(bvtotal, smem);
 
2899
        operation_deserializer<bvect>::deserialize(bv_target_s,
 
2900
                                                smem,
 
2901
                                                0,
 
2902
                                                set_OR);
 
2903
        res = bvtotal.compare(bv_target_s);
 
2904
        if (res != 0)
 
2905
        {
 
2906
            cout << "Operation deserialization error 2" << endl;
 
2907
            exit(1);
 
2908
        }
 
2909
 
 
2910
//       cout << "After deserialization" << endl;
 
2911
//       bvtotal.stat();
 
2912
 
 
2913
       bvtotal.optimize();
 
2914
       bv_target_s.optimize();
 
2915
 
 
2916
//       cout << "After optimization" << endl;
 
2917
//       bvtotal.stat();
 
2918
 
 
2919
 
 
2920
       if (++clcnt == 5)
 
2921
       {
 
2922
          clcnt = 0;
 
2923
          bvtotal.clear();
 
2924
          bv_target_s.clear();
 
2925
 
 
2926
//          cout << "Post clear." << endl;
 
2927
//          bvtotal.stat();
 
2928
 
 
2929
       }
 
2930
 
 
2931
       delete [] sermem;
 
2932
       delete [] smem;
 
2933
       delete bvect_min1;
 
2934
       delete bvect_full1;
 
2935
 
 
2936
   } // for i
 
2937
 
 
2938
}
 
2939
 
 
2940
 
 
2941
void StressTest(int repetitions)
 
2942
{
 
2943
 
 
2944
   unsigned RatioSum = 0;
 
2945
   unsigned SRatioSum = 0;
 
2946
   unsigned DeltaSum = 0;
 
2947
   unsigned SDeltaSum = 0;
 
2948
 
 
2949
   unsigned clear_count = 0;
 
2950
 
 
2951
   bvect  bvtotal;
 
2952
   bvtotal.set_new_blocks_strat(bm::BM_GAP);
 
2953
 
 
2954
 
 
2955
   cout << "----------------------------StressTest" << endl;
 
2956
 
 
2957
   unsigned size = BITVECT_SIZE - 10;
 
2958
//size = BITVECT_SIZE / 10;
 
2959
   int i;
 
2960
   for (i = 0; i < repetitions; ++i)
 
2961
   {
 
2962
        cout << endl << " - - - - - - - - - - - - STRESS STEP " << i << endl;
 
2963
 
 
2964
        switch (rand() % 3)
 
2965
        {
 
2966
        case 0:
 
2967
            size = BITVECT_SIZE / 10;
 
2968
            break;
 
2969
        case 1:
 
2970
            size = BITVECT_SIZE / 2;
 
2971
            break;
 
2972
        default:
 
2973
            size = BITVECT_SIZE - 10;
 
2974
            break;
 
2975
        } // switch
 
2976
 
 
2977
 
 
2978
        bvect_mini*   bvect_min1= new bvect_mini(size);
 
2979
        bvect_mini*   bvect_min2= new bvect_mini(size);
 
2980
        bvect*        bvect_full1= new bvect();
 
2981
        bvect*        bvect_full2= new bvect();
 
2982
 
 
2983
        bvect_full1->set_new_blocks_strat(i&1 ? bm::BM_GAP : bm::BM_BIT);
 
2984
        bvect_full2->set_new_blocks_strat(i&1 ? bm::BM_GAP : bm::BM_BIT);
 
2985
 
 
2986
        int opt = rand() % 2;
 
2987
 
 
2988
        unsigned start1 = 0;
 
2989
 
 
2990
        switch (rand() % 3)
 
2991
        {
 
2992
        case 1:
 
2993
            start1 += size / 5;
 
2994
            break;
 
2995
        default:
 
2996
            break;
 
2997
        }
 
2998
 
 
2999
        unsigned start2 = 0;
 
3000
 
 
3001
        switch (rand() % 3)
 
3002
        {
 
3003
        case 1:
 
3004
            start2 += size / 5;
 
3005
            break;
 
3006
        default:
 
3007
            break;
 
3008
        }
 
3009
/*
 
3010
        if (i == 3)
 
3011
        {
 
3012
            g_cnt_check = 1;
 
3013
        }
 
3014
*/
 
3015
        FillSetsRandomMethod(bvect_min1, bvect_full1, start1, size, opt);
 
3016
        FillSetsRandomMethod(bvect_min2, bvect_full2, start2, size, opt);
 
3017
 
 
3018
 
 
3019
 
 
3020
        unsigned arr[bm::set_total_blocks]={0,};
 
3021
        bm::id_t cnt = bvect_full1->count();
 
3022
        unsigned last_block = bvect_full1->count_blocks(arr);
 
3023
        unsigned sum = bm::sum_arr(&arr[0], &arr[last_block+1]);
 
3024
 
 
3025
        if (sum != cnt)
 
3026
        {
 
3027
            cout << "Error in function count_blocks." << endl;
 
3028
            cout << "Array sum = " << sum << endl;
 
3029
            cout << "BitCount = " << cnt << endl;
 
3030
            cnt = bvect_full1->count();
 
3031
            for (unsigned i = 0; i <= last_block; ++i)
 
3032
            {
 
3033
                if (arr[i])
 
3034
                {
 
3035
                    cout << "[" << i << ":" << arr[i] << "]";
 
3036
                }
 
3037
            }
 
3038
            cout << endl;
 
3039
            cout << "================" << endl;
 
3040
            bvect_full1->stat();
 
3041
 
 
3042
 
 
3043
            exit(1);
 
3044
        }
 
3045
 
 
3046
        CheckCountRange(*bvect_full1, start1, BITVECT_SIZE, arr);
 
3047
        CheckIntervals(*bvect_full1, BITVECT_SIZE);
 
3048
 
 
3049
 
 
3050
        
 
3051
 
 
3052
        CheckCountRange(*bvect_full2, start2, BITVECT_SIZE);
 
3053
 
 
3054
        CheckCountRange(*bvect_full1, 0, start1, arr);
 
3055
        CheckCountRange(*bvect_full2, 0, start2);
 
3056
 
 
3057
 
 
3058
/*        
 
3059
        cout << "!!!!!!!!!!!!!!!" << endl;
 
3060
        CheckVectors(*bvect_min1, *bvect_full1, size);
 
3061
        cout << "!!!!!!!!!!!!!!!" << endl;
 
3062
        CheckVectors(*bvect_min2, *bvect_full2, size);
 
3063
        cout << "!!!!!!!!!!!!!!!" << endl;
 
3064
 
 
3065
        
 
3066
         bvect_full1->stat();
 
3067
         cout << " --" << endl;
 
3068
         bvect_full2->stat();
 
3069
*/
 
3070
 
 
3071
        int operation = rand()%4;
 
3072
//operation = 2;
 
3073
 
 
3074
        switch(operation)
 
3075
        {
 
3076
        case 0:
 
3077
            cout << "Operation OR" << endl;
 
3078
            bvect_min1->combine_or(*bvect_min2);
 
3079
            break;
 
3080
 
 
3081
        case 1:
 
3082
            cout << "Operation SUB" << endl;
 
3083
            bvect_min1->combine_sub(*bvect_min2);
 
3084
            break;
 
3085
 
 
3086
        case 2:
 
3087
            cout << "Operation XOR" << endl;
 
3088
            bvect_min1->combine_xor(*bvect_min2);
 
3089
            break;
 
3090
 
 
3091
        default:
 
3092
            cout << "Operation AND" << endl;
 
3093
            bvect_min1->combine_and(*bvect_min2);
 
3094
            break;
 
3095
        }
 
3096
 
 
3097
        int cres1 = bvect_min1->compare(*bvect_min2);
 
3098
 
 
3099
        delete bvect_min2;
 
3100
 
 
3101
        switch(operation)
 
3102
        {
 
3103
        case 0:
 
3104
            {
 
3105
            cout << "Operation OR" << endl;
 
3106
 
 
3107
            bm::id_t predicted_count = bm::count_or(*bvect_full1, *bvect_full2);
 
3108
            bm::id_t predicted_any = bm::any_or(*bvect_full1, *bvect_full2);
 
3109
            if (predicted_any == 0 && predicted_count != 0)
 
3110
            {
 
3111
                cout << "Predicted any error!" << endl;
 
3112
                exit(1);
 
3113
            }
 
3114
            
 
3115
            bvect    bv_target_s;
 
3116
            SerializationOperation2Test(&bv_target_s,
 
3117
                                        *bvect_full1,
 
3118
                                        *bvect_full2,
 
3119
                                        predicted_count,
 
3120
                                        set_COUNT_OR,
 
3121
                                        set_OR);
 
3122
 
 
3123
            bvect_full1->bit_or(*bvect_full2);
 
3124
            
 
3125
            bm::id_t count = bvect_full1->count();
 
3126
 
 
3127
            if (count != predicted_count)
 
3128
            {
 
3129
                cout << "Predicted count error!" << endl;
 
3130
                cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
 
3131
                exit(1);
 
3132
            }
 
3133
            int res = bvect_full1->compare(bv_target_s);
 
3134
            if (res != 0)
 
3135
            {
 
3136
                cout << "Serialization operation failed!" << endl;
 
3137
                exit(1);
 
3138
            }
 
3139
            
 
3140
            }
 
3141
            break;
 
3142
 
 
3143
        case 1:
 
3144
            {
 
3145
            cout << "Operation SUB" << endl;
 
3146
            
 
3147
            bm::id_t predicted_count = bm::count_sub(*bvect_full1, *bvect_full2);
 
3148
            bm::id_t predicted_any = bm::any_sub(*bvect_full1, *bvect_full2);
 
3149
            if (predicted_any == 0 && predicted_count != 0)
 
3150
            {
 
3151
                cout << "Predicted any error!" << endl;
 
3152
                exit(1);
 
3153
            }
 
3154
            
 
3155
            bvect    bv_target_s;
 
3156
            SerializationOperation2Test(&bv_target_s,
 
3157
                                        *bvect_full1,
 
3158
                                        *bvect_full2,
 
3159
                                        predicted_count,
 
3160
                                        set_COUNT_SUB_AB,
 
3161
                                        set_SUB);
 
3162
 
 
3163
            bvect_full1->bit_sub(*bvect_full2);
 
3164
            
 
3165
            bm::id_t count = bvect_full1->count();
 
3166
 
 
3167
            if (count != predicted_count)
 
3168
            {
 
3169
                cout << "Predicted count error!" << endl;
 
3170
                cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
 
3171
                exit(1);
 
3172
            }
 
3173
            int res = bvect_full1->compare(bv_target_s);
 
3174
            if (res != 0)
 
3175
            {
 
3176
                cout << "Serialization operation failed!" << endl;
 
3177
                exit(1);
 
3178
            }
 
3179
            
 
3180
            
 
3181
            }
 
3182
            break;
 
3183
 
 
3184
        case 2:
 
3185
            {
 
3186
            cout << "Operation XOR" << endl;
 
3187
           
 
3188
            bm::id_t predicted_count = bm::count_xor(*bvect_full1, *bvect_full2);
 
3189
            bm::id_t predicted_any = bm::any_xor(*bvect_full1, *bvect_full2);
 
3190
            if (predicted_any == 0 && predicted_count != 0)
 
3191
            {
 
3192
                cout << "Predicted any error!" << endl;
 
3193
                exit(1);
 
3194
            }
 
3195
 
 
3196
            bvect    bv_target_s;
 
3197
            SerializationOperation2Test(&bv_target_s,
 
3198
                                        *bvect_full1,
 
3199
                                        *bvect_full2,
 
3200
                                        predicted_count,
 
3201
                                        set_COUNT_XOR,
 
3202
                                        set_XOR);
 
3203
            
 
3204
            bvect_full1->bit_xor(*bvect_full2);
 
3205
            
 
3206
            bm::id_t count = bvect_full1->count();
 
3207
 
 
3208
            if (count != predicted_count)
 
3209
            {
 
3210
                cout << "Predicted count error!" << endl;
 
3211
                cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
 
3212
                exit(1);
 
3213
            }
 
3214
            int res = bvect_full1->compare(bv_target_s);
 
3215
            if (res != 0)
 
3216
            {
 
3217
                cout << "Serialization operation failed!" << endl;
 
3218
                exit(1);
 
3219
            }
 
3220
            
 
3221
            }
 
3222
            
 
3223
            break;
 
3224
 
 
3225
        default:
 
3226
            {
 
3227
            cout << "Operation AND" << endl;
 
3228
 
 
3229
            bm::id_t predicted_count = bm::count_and(*bvect_full1, *bvect_full2);
 
3230
            bm::id_t predicted_any = bm::any_and(*bvect_full1, *bvect_full2);
 
3231
            if (predicted_any == 0 && predicted_count != 0)
 
3232
            {
 
3233
                cout << "Predicted any error!" << endl;
 
3234
                exit(1);
 
3235
            }
 
3236
 
 
3237
            bvect    bv_target_s;
 
3238
            SerializationOperation2Test(&bv_target_s,
 
3239
                                        *bvect_full1,
 
3240
                                        *bvect_full2,
 
3241
                                        predicted_count,
 
3242
                                        set_COUNT_AND,
 
3243
                                        set_AND);
 
3244
 
 
3245
            bvect_full1->bit_and(*bvect_full2);
 
3246
 
 
3247
            bm::id_t count = bvect_full1->count();
 
3248
 
 
3249
            if (count != predicted_count)
 
3250
            {
 
3251
                cout << "Predicted count error!" << endl;
 
3252
                cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
 
3253
                exit(1);
 
3254
            }
 
3255
            int res = bvect_full1->compare(bv_target_s);
 
3256
            if (res != 0)
 
3257
            {
 
3258
                cout << "Serialization operation failed!" << endl;
 
3259
                exit(1);
 
3260
            }
 
3261
 
 
3262
            }
 
3263
            break;
 
3264
        }
 
3265
 
 
3266
        cout << "Operation comparison" << endl;
 
3267
        CheckVectors(*bvect_min1, *bvect_full1, size);
 
3268
 
 
3269
        int cres2 = bvect_full1->compare(*bvect_full2);
 
3270
 
 
3271
        CheckIntervals(*bvect_full1, BITVECT_SIZE);
 
3272
 
 
3273
        if (cres1 != cres2)
 
3274
        {
 
3275
            cout << cres1 << " " << cres2 << endl;
 
3276
            cout << bvect_full1->get_first() << " " << bvect_full1->count() << endl;
 
3277
            cout << bvect_full2->get_first() << " " << bvect_full2->count() << endl;
 
3278
 
 
3279
           // bvect_full1->stat(1000);
 
3280
            cout << endl;
 
3281
           // bvect_full2->stat(1000);
 
3282
            printf("Bitset comparison operation failed.\n");
 
3283
            exit(1);
 
3284
        }
 
3285
 
 
3286
        {
 
3287
            bvect bv1(*bvect_full1);
 
3288
            unsigned idx = rand() % size;
 
3289
            bool b = bv1[idx];
 
3290
            bool changed;
 
3291
            if (b) 
 
3292
            {
 
3293
                changed = bv1.set_bit_conditional(idx, true, false);
 
3294
                if (changed)
 
3295
                {
 
3296
                    cout << "Set bit conditional failed!" << endl;
 
3297
                    exit(1);
 
3298
                }
 
3299
                b = bv1[idx];
 
3300
                if (!b)
 
3301
                {
 
3302
                    cout << "Set bit conditional failed!" << endl;
 
3303
                    exit(1);
 
3304
                }
 
3305
 
 
3306
                changed = bv1.set_bit_conditional(idx, false, false);
 
3307
                if (changed)
 
3308
                {
 
3309
                    cout << "Set bit conditional failed!" << endl;
 
3310
                    exit(1);
 
3311
                }
 
3312
                changed = bv1.set_bit_conditional(idx, true, true);
 
3313
                if (changed)
 
3314
                {
 
3315
                    cout << "Set bit conditional failed!" << endl;
 
3316
                    exit(1);
 
3317
                }
 
3318
                changed = bv1.set_bit_conditional(idx, false, true);
 
3319
                if (!changed)
 
3320
                {
 
3321
                    cout << "Set bit conditional failed!" << endl;
 
3322
                    exit(1);
 
3323
                }
 
3324
                b = bv1[idx];
 
3325
                if (b)
 
3326
                {
 
3327
                    cout << "Set bit conditional failed!" << endl;
 
3328
                    exit(1);
 
3329
                }
 
3330
            } 
 
3331
            else 
 
3332
            {
 
3333
                changed = bv1.set_bit_conditional(idx, false, true);
 
3334
                if (changed)
 
3335
                {
 
3336
                    cout << "Set bit conditional failed!" << endl;
 
3337
                    exit(1);
 
3338
                }
 
3339
                changed = bv1.set_bit_conditional(idx, true, false);
 
3340
                if (!changed)
 
3341
                {
 
3342
                    cout << "Set bit conditional failed!" << endl;
 
3343
                    exit(1);
 
3344
                }
 
3345
                b = bv1[idx];
 
3346
                if (!b)
 
3347
                {
 
3348
                    cout << "Set bit conditional failed!" << endl;
 
3349
                    exit(1);
 
3350
                }
 
3351
            }
 
3352
 
 
3353
 
 
3354
        }
 
3355
 
 
3356
        delete bvect_full2;
 
3357
 
 
3358
 
 
3359
        struct bvect::statistics st1;
 
3360
        bvect_full1->calc_stat(&st1);
 
3361
        bvect_full1->optimize();
 
3362
        bvect_full1->optimize_gap_size();
 
3363
        struct bvect::statistics st2;
 
3364
        bvect_full1->calc_stat(&st2);
 
3365
 
 
3366
        unsigned Ratio = (st2.memory_used * 100)/st1.memory_used;
 
3367
        RatioSum+=Ratio;
 
3368
        DeltaSum+=st1.memory_used - st2.memory_used;
 
3369
 
 
3370
        cout << "Optimization statistics: " << endl  
 
3371
             << "   MemUsedBefore=" << st1.memory_used
 
3372
             << "   MemUsed=" << st2.memory_used 
 
3373
             << "   Ratio=" << Ratio << "%"
 
3374
             << "   Delta=" << st1.memory_used - st2.memory_used
 
3375
             << endl;
 
3376
                
 
3377
        cout << "Optimization comparison" << endl;
 
3378
 
 
3379
        CheckVectors(*bvect_min1, *bvect_full1, size);
 
3380
 
 
3381
        bvect_full1->set_gap_levels(gap_len_table_min<true>::_len);
 
3382
        CheckVectors(*bvect_min1, *bvect_full1, size);
 
3383
        CheckIntervals(*bvect_full1, BITVECT_SIZE);
 
3384
 
 
3385
        //CheckCountRange(*bvect_full1, 0, size);
 
3386
 
 
3387
 
 
3388
        // Serialization
 
3389
        bvect_full1->calc_stat(&st2);
 
3390
 
 
3391
        cout << "Memory allocation: " << st2.max_serialize_mem << endl;
 
3392
        unsigned char* sermem = new unsigned char[st2.max_serialize_mem];
 
3393
 
 
3394
//    bvect_full1->stat();
 
3395
 
 
3396
        cout << "Serialization...";
 
3397
        unsigned slen = 
 
3398
            bm::serialize(*bvect_full1, sermem, 
 
3399
                          BM_NO_GAP_LENGTH|BM_NO_BYTE_ORDER);
 
3400
        cout << "Ok" << endl;
 
3401
 
 
3402
        delete bvect_full1;
 
3403
 
 
3404
        unsigned SRatio = (slen*100)/st2.memory_used;
 
3405
        SRatioSum+=SRatio;
 
3406
        SDeltaSum+=st2.memory_used - slen;
 
3407
 
 
3408
 
 
3409
        cout << "Serialized mem_max = " << st2.max_serialize_mem 
 
3410
             << " size= " << slen 
 
3411
             << " Ratio=" << SRatio << "%"
 
3412
             << " Delta=" << st2.memory_used - slen
 
3413
             << endl;
 
3414
 
 
3415
        bvect*        bvect_full3= new bvect();
 
3416
        unsigned char* new_sermem = new unsigned char[slen];
 
3417
        memcpy(new_sermem, sermem, slen);
 
3418
        delete [] sermem;
 
3419
 
 
3420
        cout << "Deserialization...";
 
3421
 
 
3422
        bm::deserialize(*bvect_full3, new_sermem);
 
3423
 
 
3424
        bm::deserialize(bvtotal, new_sermem);
 
3425
 
 
3426
        bvect* bv_target_s=new bvect();
 
3427
        operation_deserializer<bvect>::deserialize(*bv_target_s,
 
3428
                                            new_sermem,
 
3429
                                            0,
 
3430
                                            set_OR);
 
3431
 
 
3432
        cout << "Ok." << endl;
 
3433
        delete [] new_sermem;
 
3434
 
 
3435
        cout << "Optimization...";
 
3436
        bvtotal.optimize();
 
3437
        cout << "Ok." << endl;
 
3438
 
 
3439
        ++clear_count;
 
3440
 
 
3441
        if (clear_count == 4)
 
3442
        {
 
3443
           bvtotal.clear();
 
3444
           clear_count = 0;
 
3445
        }
 
3446
 
 
3447
        cout << "Serialization comparison" << endl;
 
3448
 
 
3449
        CheckVectors(*bvect_min1, *bvect_full3, size, true);
 
3450
        int res = bv_target_s->compare(*bvect_full3);
 
3451
        if (res != 0)
 
3452
        {
 
3453
            CheckVectors(*bvect_min1, *bv_target_s, size, true);
 
3454
        }
 
3455
 
 
3456
        delete bv_target_s;
 
3457
        delete bvect_min1;
 
3458
        delete bvect_full3;
 
3459
 
 
3460
    }
 
3461
 
 
3462
    --i;
 
3463
    cout << "Repetitions:" << i <<
 
3464
            " AVG optimization ratio:" << RatioSum/i 
 
3465
         << " AVG Delta:" << DeltaSum/i
 
3466
         << endl
 
3467
         << " AVG serialization Ratio:"<< SRatioSum/i
 
3468
         << " Delta:" << SDeltaSum/i
 
3469
         << endl;
 
3470
}
 
3471
 
 
3472
void GAPCheck()
 
3473
{
 
3474
   cout << "-------------------------------------------GAPCheck" << endl;
 
3475
 
 
3476
    {
 
3477
 
 
3478
    gap_vector   gapv(0);
 
3479
    bvect_mini  bvect_min(bm::gap_max_bits);
 
3480
 
 
3481
    unsigned i;
 
3482
    for( i = 0; i < 454; ++i)
 
3483
    {
 
3484
        bvect_min.set_bit(i);
 
3485
        gapv.set_bit(i);
 
3486
    }
 
3487
 
 
3488
    for(i = 0; i < 254; ++i)
 
3489
    {
 
3490
        bvect_min.clear_bit(i);
 
3491
        gapv.clear_bit(i);
 
3492
    }
 
3493
 
 
3494
    for(i = 5; i < 10; ++i)
 
3495
    {
 
3496
        bvect_min.set_bit(i);
 
3497
        gapv.set_bit(i);
 
3498
    }
 
3499
 
 
3500
    for( i = 0; i < bm::gap_max_bits; ++i)
 
3501
    {
 
3502
        int bit1 = (gapv.is_bit_true(i) == 1);
 
3503
        int bit2 = (bvect_min.is_bit_true(i) != 0);
 
3504
        int bit3 = (gapv.test(i) == 1);
 
3505
        if (bit1 != bit2)
 
3506
        {
 
3507
            cout << "problem with bit comparison " << i << endl;
 
3508
            exit(1);
 
3509
        }
 
3510
        if (bit1 != bit3)
 
3511
        {
 
3512
            cout << "problem with bit test comparison " << i << endl;
 
3513
            exit(1);
 
3514
        }
 
3515
 
 
3516
    }
 
3517
 
 
3518
    }
 
3519
 
 
3520
 
 
3521
   {
 
3522
   gap_vector gapv(1);
 
3523
   int bit = gapv.is_bit_true(65535);
 
3524
 
 
3525
   if (bit != 1)
 
3526
   {
 
3527
      cout << "Bit != 1" << endl;
 
3528
      exit(1);
 
3529
   }
 
3530
   
 
3531
   int i;
 
3532
   for (i = 0; i < 65536; ++i)
 
3533
   {
 
3534
        bit = gapv.is_bit_true(i);
 
3535
        if (bit != 1)
 
3536
        {
 
3537
            cout << "2.Bit != 1" << endl;
 
3538
            exit(1);
 
3539
        }
 
3540
   }
 
3541
   unsigned cnt = gapv.count_range(0, 65535);
 
3542
   if (cnt != 65536)
 
3543
   {
 
3544
       cout << "count_range failed:" << cnt << endl;
 
3545
       exit(1);
 
3546
   }
 
3547
   
 
3548
   CheckCountRange(gapv, 10, 20);
 
3549
   CheckCountRange(gapv, 0, 20);
 
3550
 
 
3551
   printf("gapv 1 check ok\n");
 
3552
   }
 
3553
 
 
3554
   {
 
3555
   gap_vector gapv(0);
 
3556
 
 
3557
 
 
3558
   int bit = gapv.is_bit_true(65535);
 
3559
   int bit1 = gapv.test(65535);
 
3560
   if(bit != 0)
 
3561
   {
 
3562
      cout << "Bit != 0" << endl;
 
3563
      exit(1);
 
3564
   }
 
3565
      
 
3566
   int i;
 
3567
   for (i = 0; i < 65536; ++i)
 
3568
   {
 
3569
        bit = gapv.is_bit_true(i);
 
3570
        bit1 = gapv.test(i);
 
3571
        if (bit != 0)
 
3572
        {
 
3573
            cout << "2.Bit != 0 bit =" << i << endl;
 
3574
            exit(1);
 
3575
        }
 
3576
        if (bit1 != 0)
 
3577
        {
 
3578
            cout << "2.Bit test != 0 bit =" << i << endl;
 
3579
            exit(1);
 
3580
        }
 
3581
   }
 
3582
   unsigned cnt = gapv.count_range(0, 65535);
 
3583
   if (cnt != 0)
 
3584
   {
 
3585
       cout << "count_range failed:" << cnt << endl;
 
3586
       exit(1);
 
3587
   }
 
3588
   CheckCountRange(gapv, 10, 20);
 
3589
   CheckCountRange(gapv, 0, 20);
 
3590
 
 
3591
   printf("gapv 2 check ok\n");
 
3592
   }
 
3593
 
 
3594
   {
 
3595
   gap_vector gapv(0);
 
3596
 
 
3597
   gapv.set_bit(1);
 
3598
   gapv.set_bit(0);
 
3599
 
 
3600
   gapv.control();
 
3601
   CheckCountRange(gapv, 0, 20);
 
3602
 
 
3603
   int bit = gapv.is_bit_true(0);
 
3604
 
 
3605
   if (bit != 1)
 
3606
   {
 
3607
      cout << "Trouble" << endl;
 
3608
      exit(1);
 
3609
   }
 
3610
   
 
3611
   bit = gapv.is_bit_true(1);
 
3612
   if (bit != 1)
 
3613
   {
 
3614
      cout << "Trouble 2." << endl;
 
3615
      exit(1);
 
3616
   }
 
3617
 
 
3618
 
 
3619
   bit = gapv.is_bit_true(2);
 
3620
   if(bit != 0)
 
3621
   {
 
3622
      cout << "Trouble 3." << endl;
 
3623
      exit(1);
 
3624
   }
 
3625
 
 
3626
   }
 
3627
 
 
3628
   {
 
3629
   gap_vector gapv(0);
 
3630
 
 
3631
   gapv.set_bit(0);
 
3632
   gapv.control();
 
3633
   gapv.set_bit(1);
 
3634
   gapv.control();
 
3635
 
 
3636
   gapv.set_bit(4);
 
3637
   gapv.control();
 
3638
   gapv.set_bit(5);
 
3639
   gapv.control();
 
3640
   CheckCountRange(gapv, 4, 5);
 
3641
   CheckCountRange(gapv, 3, 5);
 
3642
 
 
3643
   gapv.set_bit(3);
 
3644
   CheckCountRange(gapv, 3, 3);
 
3645
   CheckCountRange(gapv, 3, 5);
 
3646
 
 
3647
   gapv.control();
 
3648
   
 
3649
   int bit = gapv.is_bit_true(0);
 
3650
   if(bit!=1)
 
3651
   {
 
3652
      cout << "Bug" << endl;
 
3653
   }
 
3654
   bit = gapv.is_bit_true(1);
 
3655
   if(bit!=1)
 
3656
   {
 
3657
      cout << "Bug2" << endl;
 
3658
   }
 
3659
 
 
3660
   gapv.control();
 
3661
   gapv.set_bit(4);
 
3662
   gapv.control();
 
3663
 
 
3664
 
 
3665
   printf("gapv 3 check ok\n");
 
3666
   }
 
3667
 
 
3668
   {
 
3669
        gap_vector gapv(0);
 
3670
        bvect_mini   bvect_min(bm::gap_max_bits);
 
3671
cout << "++++++1" << endl;
 
3672
print_gap(gapv, 10);
 
3673
        gapv.set_bit(bm::gap_max_bits-1);
 
3674
        gapv.control();
 
3675
        print_gap(gapv, 10);
 
3676
 
 
3677
//cout << "++++++2" << endl;
 
3678
//cout << "m_buf=" << bvect_min.m_buf << endl;
 
3679
 
 
3680
        bvect_min.set_bit(bm::gap_max_bits-1);
 
3681
cout << "++++++3" << endl;
 
3682
        gapv.set_bit(5);
 
3683
        print_gap(gapv,15);
 
3684
        gapv.control();
 
3685
        bvect_min.set_bit(5);
 
3686
cout << "++++++4" << endl;
 
3687
 
 
3688
        CheckCountRange(gapv, 13, 150);
 
3689
        gapv.control();
 
3690
 
 
3691
        unsigned i;
 
3692
        for (i = 0; i < bm::gap_max_bits; ++i)
 
3693
        {
 
3694
            if (i == 65535)
 
3695
                printf("%i\n", i);
 
3696
            int bit1 = (gapv.is_bit_true(i) == 1);
 
3697
            int bit2 = (bvect_min.is_bit_true(i) != 0);
 
3698
            int bit3 = (gapv.test(i) == 1);
 
3699
            if (bit1 != bit2)
 
3700
            {
 
3701
                cout << "problem with bit comparison " << i << endl;
 
3702
            }
 
3703
            if (bit1 != bit3)
 
3704
            {
 
3705
                cout << "problem with bit test comparison " << i << endl;
 
3706
            }
 
3707
 
 
3708
        }
 
3709
 
 
3710
        gapv.clear_bit(5);
 
3711
        bvect_min.clear_bit(5);
 
3712
        gapv.control();
 
3713
 
 
3714
        for ( i = 0; i < bm::gap_max_bits; ++i)
 
3715
        {
 
3716
            if (i == 65535)
 
3717
                printf("%i\n", i);
 
3718
            int bit1 = (gapv.is_bit_true(i) == 1);
 
3719
            int bit2 = (bvect_min.is_bit_true(i) != 0);
 
3720
            int bit3 = (gapv.test(i) == 1);
 
3721
            if (bit1 != bit2)
 
3722
            {
 
3723
                cout << "2.problem with bit comparison " << i << endl;
 
3724
            }
 
3725
            if (bit1 != bit3)
 
3726
            {
 
3727
                cout << "2.problem with bit test comparison " << i << endl;
 
3728
            }
 
3729
        }
 
3730
   printf("gapv check 4 ok.\n");
 
3731
   }
 
3732
 
 
3733
   {
 
3734
        gap_vector gapv(0);
 
3735
        bvect_mini   bvect_min(65536);
 
3736
        
 
3737
        int i;
 
3738
        for (i = 10; i > 0; i-=2)
 
3739
        {
 
3740
            bvect_min.set_bit(i);
 
3741
            gapv.set_bit(i);
 
3742
            gapv.control();
 
3743
            CheckCountRange(gapv, 0, i);
 
3744
 
 
3745
            int bit1 = (gapv.is_bit_true(i) == 1);
 
3746
            int bit2 = (bvect_min.is_bit_true(i) != 0);
 
3747
            int bit3 = (gapv.test(i) != 0);
 
3748
            if (bit1 != bit2)
 
3749
            {
 
3750
                cout << "3.problem with bit comparison " << i << endl;
 
3751
            }
 
3752
            if (bit1 != bit3)
 
3753
            {
 
3754
                cout << "3.problem with bit test comparison " << i << endl;
 
3755
            }
 
3756
 
 
3757
        }
 
3758
        for (i = 0; i < (int)bm::gap_max_bits; ++i)
 
3759
        {
 
3760
            int bit1 = (gapv.is_bit_true(i) == 1);
 
3761
            int bit2 = (bvect_min.is_bit_true(i) != 0);
 
3762
            int bit3 = (gapv.test(i) == 1);
 
3763
 
 
3764
            if (bit1 != bit2)
 
3765
            {
 
3766
                cout << "3.problem with bit comparison " << i << endl;
 
3767
            }
 
3768
            if (bit1 != bit3)
 
3769
            {
 
3770
                cout << "3.problem with bit test comparison " << i << endl;
 
3771
            }
 
3772
        }
 
3773
   printf("gapv check 5 ok.\n");
 
3774
   }
 
3775
 
 
3776
   {
 
3777
        gap_vector gapv(0);
 
3778
        bvect_mini   bvect_min(bm::gap_max_bits);
 
3779
        
 
3780
        int i;
 
3781
        for (i = 0; i < 25; ++i)
 
3782
        {
 
3783
            unsigned id = random_minmax(0, bm::gap_max_bits);
 
3784
            bvect_min.set_bit(id);
 
3785
            gapv.set_bit(id);
 
3786
            gapv.control();
 
3787
            CheckCountRange(gapv, 0, id);
 
3788
            CheckCountRange(gapv, id, 65535);
 
3789
        }
 
3790
 
 
3791
        for (i = 0; i < (int)bm::gap_max_bits; ++i)
 
3792
        {
 
3793
            int bit1 = (gapv.is_bit_true(i) == 1);
 
3794
            int bit2 = (bvect_min.is_bit_true(i) != 0);
 
3795
            if (bit1 != bit2)
 
3796
            {
 
3797
                cout << "4.problem with bit comparison " << i << endl;
 
3798
            }
 
3799
        }
 
3800
 
 
3801
        for (i = bm::gap_max_bits; i < 0; --i)
 
3802
        {
 
3803
            int bit1 = (gapv.is_bit_true(i) == 1);
 
3804
            int bit2 = (bvect_min.is_bit_true(i) != 0);
 
3805
            if (bit1 != bit2)
 
3806
            {
 
3807
                cout << "5.problem with bit comparison " << i << endl;
 
3808
            }
 
3809
        }
 
3810
   printf("gapv check 6 ok.\n");
 
3811
 
 
3812
   }
 
3813
 
 
3814
   printf("gapv random bit set check ok.\n");
 
3815
 
 
3816
 
 
3817
   // conversion functions test
 
3818
   
 
3819
   {
 
3820
   // aligned position test
 
3821
   bvect        bvect;
 
3822
 
 
3823
   bvect.set_bit(1);
 
3824
   bvect.clear();
 
3825
 
 
3826
 
 
3827
   unsigned* buf = (unsigned*) bvect.get_block(0);
 
3828
 
 
3829
   bm::or_bit_block(buf, 0, 4);
 
3830
   unsigned cnt = bm::bit_block_calc_count_range(buf, 0, 3);
 
3831
   assert(cnt == 4);
 
3832
   
 
3833
   bool bit = bvect.get_bit(0);
 
3834
   assert(bit);
 
3835
   bit = bvect.get_bit(1);
 
3836
   assert(bit);
 
3837
   bit = bvect.get_bit(2);
 
3838
   assert(bit);
 
3839
   bit = bvect.get_bit(3);
 
3840
   assert(bit);
 
3841
   bit = bvect.get_bit(4);
 
3842
   assert(bit==0);
 
3843
 
 
3844
   bm::or_bit_block(buf, 0, 36); 
 
3845
   cnt = bm::bit_block_calc_count_range(buf, 0, 35);
 
3846
   assert(cnt == 36);
 
3847
 
 
3848
   for (int i = 0; i < 36; ++i)
 
3849
   {
 
3850
        bit = (bvect.get_bit(i) != 0);
 
3851
        assert(bit);
 
3852
   }
 
3853
   bit = (bvect.get_bit(36) != 0);
 
3854
   assert(bit==0);
 
3855
 
 
3856
   unsigned count = bvect.recalc_count();
 
3857
   assert(count == 36);   
 
3858
   
 
3859
   cout << "Aligned position test ok." << endl; 
 
3860
 
 
3861
   }
 
3862
 
 
3863
 
 
3864
   {
 
3865
   // unaligned position test
 
3866
   bvect   bvect;
 
3867
 
 
3868
   bvect.set_bit(0);
 
3869
   bvect.clear();
 
3870
 
 
3871
   unsigned* buf = (unsigned*) bvect.get_block(0);
 
3872
 
 
3873
   bm::or_bit_block(buf, 5, 32);
 
3874
   bool bit = (bvect.get_bit(4) != 0);
 
3875
   assert(bit==0);
 
3876
   unsigned cnt = bm::bit_block_calc_count_range(buf, 5, 5+32-1);
 
3877
   assert(cnt == 32);
 
3878
   cnt = bm::bit_block_calc_count_range(buf, 5, 5+32);
 
3879
   assert(cnt == 32);
 
3880
 
 
3881
   int i;
 
3882
   for (i = 5; i < 4 + 32; ++i)
 
3883
   {
 
3884
        bit = bvect.get_bit(i);
 
3885
        assert(bit);
 
3886
   }
 
3887
   int count = bvect.recalc_count();
 
3888
   assert(count==32);
 
3889
 
 
3890
   cout << "Unaligned position ok." << endl;
 
3891
 
 
3892
   } 
 
3893
 
 
3894
   // random test
 
3895
   {
 
3896
   cout << "random test" << endl;
 
3897
 
 
3898
   bvect   bvect;
 
3899
 
 
3900
   bvect.set_bit(0);
 
3901
   bvect.clear();
 
3902
 
 
3903
   unsigned* buf = (unsigned*) bvect.get_block(0);
 
3904
   for (int i = 0; i < 5000; ++i)
 
3905
   {
 
3906
        unsigned start = rand() % 65535;
 
3907
        unsigned end = rand() % 65535;
 
3908
        if (start > end)
 
3909
        {
 
3910
            unsigned tmp = end;
 
3911
            end = start;
 
3912
            start = tmp;
 
3913
        }
 
3914
        unsigned len = end - start;
 
3915
        if (len)
 
3916
        {
 
3917
           bm::or_bit_block(buf, start, len);
 
3918
           unsigned cnt = bm::bit_block_calc_count_range(buf, start, end);
 
3919
           if (cnt != len)
 
3920
           {
 
3921
            cout << "random test: count_range comparison failed. " 
 
3922
                 << " LEN = " << len << " cnt = " << cnt
 
3923
                 << endl;
 
3924
                 exit(1);
 
3925
           }
 
3926
 
 
3927
           unsigned count = bvect.recalc_count();
 
3928
 
 
3929
           if (count != len)
 
3930
           {
 
3931
            cout << "random test: count comparison failed. " 
 
3932
                 << " LEN = " << len << " count = " << count
 
3933
                 << endl;
 
3934
                 exit(1);
 
3935
           }            
 
3936
 
 
3937
           for (unsigned j = start; j < end; ++j)
 
3938
           {
 
3939
                bool bit = bvect.get_bit(j);
 
3940
                if (!bit)
 
3941
                {
 
3942
                    cout << "random test: bit comparison failed. bit#" 
 
3943
                         << i << endl;
 
3944
                    exit(1);
 
3945
                } 
 
3946
           } // for j
 
3947
 
 
3948
        } 
 
3949
        bvect.clear();
 
3950
 
 
3951
        if ((i % 100)==0)
 
3952
        {
 
3953
            cout << "*" << flush;
 
3954
        }
 
3955
   } // for i
 
3956
 
 
3957
   cout << endl << "Random test Ok." << endl;
 
3958
 
 
3959
   }
 
3960
 
 
3961
 
 
3962
   // conversion test
 
3963
 
 
3964
   cout << "Conversion test" << endl;
 
3965
    
 
3966
   {
 
3967
   
 
3968
   gap_vector gapv(0);
 
3969
   bvect   bvect;
 
3970
 
 
3971
   gapv.set_bit(0);
 
3972
   gapv.set_bit(2);
 
3973
   gapv.set_bit(10);
 
3974
   gapv.set_bit(11);
 
3975
   gapv.set_bit(12);
 
3976
   
 
3977
   CheckCountRange(gapv, 3, 15);
 
3978
 
 
3979
   print_gap(gapv, 100);
 
3980
   bvect.set_bit(0);
 
3981
   bvect.clear();
 
3982
 
 
3983
   unsigned* buf = (unsigned*) bvect.get_block(0);
 
3984
 
 
3985
 
 
3986
   gapv.convert_to_bitset(buf);
 
3987
 
 
3988
 
 
3989
   unsigned bitcount = bvect.recalc_count();
 
3990
 
 
3991
 
 
3992
   if (bitcount != 5)
 
3993
   {
 
3994
      cout << "test failed: bitcout = " << bitcount << endl;
 
3995
      exit(1);
 
3996
   }
 
3997
 
 
3998
 
 
3999
   gap_vector gapv1(0);
 
4000
   gap_word_t* gap_buf = gapv1.get_buf();
 
4001
   *gap_buf = 0;
 
4002
   bit_convert_to_gap(gap_buf, buf, bm::gap_max_bits, bm::gap_max_buff_len);
 
4003
   print_gap(gapv1, 100);
 
4004
 
 
4005
   bitcount = gapv1.bit_count();
 
4006
   if(bitcount != 5)
 
4007
   {
 
4008
      cout << "2.test_failed: bitcout = " << bitcount << endl;
 
4009
      exit(1);
 
4010
   }
 
4011
 
 
4012
   printf("conversion test ok.\n");
 
4013
    
 
4014
   }
 
4015
 
 
4016
   // gap AND test
 
4017
 
 
4018
   {
 
4019
   // special case 1: operand is all 1
 
4020
   gap_vector gapv1(0);
 
4021
   gapv1.set_bit(2);
 
4022
   gap_vector gapv2(1); 
 
4023
 
 
4024
   gapv1.combine_and(gapv2.get_buf());
 
4025
   gapv1.control();
 
4026
   print_gap(gapv1, 0);
 
4027
 
 
4028
   int count = gapv1.bit_count();
 
4029
   assert(count == 1);
 
4030
   int bit = gapv1.is_bit_true(2);
 
4031
   if(bit == 0)
 
4032
   {
 
4033
      cout << "Wrong bit" << endl;
 
4034
      exit(1);
 
4035
   }
 
4036
   CheckCountRange(gapv1, 0, 17);
 
4037
 
 
4038
   }
 
4039
 
 
4040
   {
 
4041
   // special case 2: src is all 1
 
4042
   gap_vector gapv1(1);
 
4043
   gap_vector gapv2(0); 
 
4044
   gapv2.set_bit(2);
 
4045
 
 
4046
   gapv1.combine_and(gapv2.get_buf());
 
4047
   gapv1.control();
 
4048
   print_gap(gapv1, 0);
 
4049
 
 
4050
   int count = gapv1.bit_count();
 
4051
   assert(count == 1);
 
4052
   int bit = gapv1.is_bit_true(2);
 
4053
   assert(bit);
 
4054
 
 
4055
   }
 
4056
 
 
4057
   {
 
4058
   gap_vector gapv;
 
4059
   gap_vector gapv1(0);
 
4060
 
 
4061
   gapv1.set_bit(3);
 
4062
   gapv1.set_bit(4);
 
4063
   print_gap(gapv1, 0);
 
4064
 
 
4065
   gap_vector gapv2(0); 
 
4066
   gapv2.set_bit(2);
 
4067
   gapv2.set_bit(3);
 
4068
   print_gap(gapv2, 0);
 
4069
 
 
4070
   bm::gap_buff_op((gap_word_t*)gapv.get_buf(), 
 
4071
                         gapv1.get_buf(), 0,
 
4072
                         gapv2.get_buf(), 0, bm::and_op); 
 
4073
   print_gap(gapv, 0);
 
4074
   gapv.control();
 
4075
 
 
4076
 
 
4077
    int bit1 = (gapv.is_bit_true(3) == 1);
 
4078
    if(bit1 == 0)
 
4079
    {
 
4080
       cout << "Checking failed." << endl;
 
4081
       exit(0);
 
4082
    }
 
4083
 
 
4084
   gapv1.combine_or(gapv2);
 
4085
   print_gap(gapv1, 0);
 
4086
   gapv1.control();
 
4087
 
 
4088
   }
 
4089
 
 
4090
   {
 
4091
        printf("gap AND test 1.\n");
 
4092
        gap_vector gapv1(0);
 
4093
        gap_vector gapv2(0);
 
4094
        bvect_mini   bvect_min1(65536);
 
4095
        bvect_mini   bvect_min2(65536);
 
4096
 
 
4097
        gapv1.set_bit(65535);
 
4098
        bvect_min1.set_bit(65535);
 
4099
        gapv1.set_bit(4);
 
4100
        bvect_min1.set_bit(4);
 
4101
 
 
4102
        gapv2.set_bit(65535);
 
4103
        bvect_min2.set_bit(65535);
 
4104
        gapv2.set_bit(3);
 
4105
        bvect_min2.set_bit(3);
 
4106
        CheckCountRange(gapv2, 3, 65535);
 
4107
 
 
4108
        gapv2.control();
 
4109
 
 
4110
        printf("vect1:"); print_gap(gapv1, 0);
 
4111
        printf("vect2:");print_gap(gapv2, 0);
 
4112
 
 
4113
        gapv1.combine_and(gapv2.get_buf());
 
4114
        printf("vect1:");print_gap(gapv1, 0);
 
4115
 
 
4116
        gapv1.control();
 
4117
        unsigned bit1 = gapv1.is_bit_true(65535);
 
4118
        assert(bit1);
 
4119
 
 
4120
        bvect_min1.combine_and(bvect_min2);
 
4121
        CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
 
4122
   }
 
4123
 
 
4124
   {
 
4125
        printf("gap random AND test.\n");
 
4126
        gap_vector gapv1(0);
 
4127
        gap_vector gapv2(0);
 
4128
        bvect_mini   bvect_min1(65536);
 
4129
        bvect_mini   bvect_min2(65536);
 
4130
        
 
4131
        int i;
 
4132
        for (i = 0; i < 25; ++i)
 
4133
        {
 
4134
            unsigned id = random_minmax(0, 65535);
 
4135
            bvect_min1.set_bit(id);
 
4136
            gapv1.set_bit(id);
 
4137
            gapv1.control();
 
4138
            CheckCountRange(gapv1, 0, id);
 
4139
            CheckCountRange(gapv1, id, 65535);
 
4140
        }
 
4141
        for (i = 0; i < 25; ++i)
 
4142
        {
 
4143
            unsigned id = random_minmax(0, 65535);
 
4144
            bvect_min2.set_bit(id);
 
4145
            gapv2.set_bit(id);
 
4146
            gapv2.control();
 
4147
        }
 
4148
 
 
4149
        gapv1.combine_and(gapv2.get_buf());
 
4150
        gapv1.control();
 
4151
        gapv2.control();
 
4152
        bvect_min1.combine_and(bvect_min2);
 
4153
 
 
4154
        CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
 
4155
 
 
4156
        printf("gap random AND test ok.\n");
 
4157
 
 
4158
   }
 
4159
 
 
4160
   {
 
4161
        printf("gap OR test.\n");
 
4162
 
 
4163
        gap_vector gapv1(0);
 
4164
        gap_vector gapv2(0);
 
4165
 
 
4166
        gapv1.set_bit(2);
 
4167
        gapv2.set_bit(3);
 
4168
 
 
4169
        gapv1.combine_or(gapv2);
 
4170
        gapv1.control();
 
4171
        print_gap(gapv1, 0);   
 
4172
        int bit1 = (gapv1.is_bit_true(0) == 1);
 
4173
        assert(bit1==0);
 
4174
        bit1=(gapv1.is_bit_true(2) == 1);
 
4175
        assert(bit1);
 
4176
        bit1=(gapv1.is_bit_true(3) == 1);
 
4177
        assert(bit1);
 
4178
   }
 
4179
 
 
4180
   {
 
4181
        printf("gap XOR test.\n");
 
4182
 
 
4183
        gap_vector gapv1(0);
 
4184
        gap_vector gapv2(0);
 
4185
 
 
4186
        gapv1.set_bit(2);
 
4187
        gapv2.set_bit(3);
 
4188
        gapv1.set_bit(4);
 
4189
        gapv2.set_bit(4);
 
4190
        print_gap(gapv1, 0);   
 
4191
        print_gap(gapv2, 0);   
 
4192
 
 
4193
        gapv1.combine_xor(gapv2);
 
4194
        gapv1.control();
 
4195
        print_gap(gapv1, 0);   
 
4196
        int bit1 = (gapv1.is_bit_true(0) == 0);
 
4197
        assert(bit1);
 
4198
        bit1=(gapv1.is_bit_true(2) == 1);
 
4199
        assert(bit1);
 
4200
        bit1=(gapv1.is_bit_true(3) == 1);
 
4201
        assert(bit1);
 
4202
        bit1=(gapv1.is_bit_true(4) == 0);
 
4203
        assert(bit1);
 
4204
 
 
4205
   }
 
4206
 
 
4207
 
 
4208
   {
 
4209
        int i;
 
4210
        printf("gap random OR test.\n");
 
4211
        gap_vector gapv1(0);
 
4212
        gap_vector gapv2(0);
 
4213
        bvect_mini   bvect_min1(bm::gap_max_bits);
 
4214
        bvect_mini   bvect_min2(bm::gap_max_bits);
 
4215
        
 
4216
        for (i = 0; i < 10; ++i)
 
4217
        {
 
4218
            unsigned id = random_minmax(0, 100);
 
4219
            bvect_min1.set_bit(id);
 
4220
            gapv1.set_bit(id);
 
4221
            gapv1.control();
 
4222
        }
 
4223
        for (i = 0; i < 10; ++i)
 
4224
        {
 
4225
            unsigned id = random_minmax(0, 100);
 
4226
            bvect_min2.set_bit(id);
 
4227
            gapv2.set_bit(id);
 
4228
            gapv2.control();
 
4229
        }
 
4230
 
 
4231
        print_mv(bvect_min1, 64);
 
4232
        print_mv(bvect_min2, 64);
 
4233
 
 
4234
        gapv1.combine_or(gapv2);
 
4235
        gapv1.control();
 
4236
        gapv2.control();
 
4237
        bvect_min1.combine_or(bvect_min2);
 
4238
 
 
4239
        print_mv(bvect_min1, 64);
 
4240
 
 
4241
        CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
 
4242
 
 
4243
        printf("gap random OR test ok.\n");
 
4244
 
 
4245
   }
 
4246
 
 
4247
 
 
4248
   {
 
4249
        int i;
 
4250
        printf("gap random SUB test.\n");
 
4251
        gap_vector gapv1(0);
 
4252
        gap_vector gapv2(0);
 
4253
        bvect_mini   bvect_min1(bm::gap_max_bits);
 
4254
        bvect_mini   bvect_min2(bm::gap_max_bits);
 
4255
        
 
4256
        for (i = 0; i < 25; ++i)
 
4257
        {
 
4258
            unsigned id = random_minmax(0, 100);
 
4259
            bvect_min1.set_bit(id);
 
4260
            gapv1.set_bit(id);
 
4261
            gapv1.control();
 
4262
        }
 
4263
        for (i = 0; i < 25; ++i)
 
4264
        {
 
4265
            unsigned id = random_minmax(0, 100);
 
4266
            bvect_min2.set_bit(id);
 
4267
            gapv2.set_bit(id);
 
4268
            gapv2.control();
 
4269
        }
 
4270
 
 
4271
        print_mv(bvect_min1, 64);
 
4272
        print_mv(bvect_min2, 64);
 
4273
 
 
4274
        gapv1.combine_sub(gapv2);
 
4275
        gapv1.control();
 
4276
        gapv2.control();
 
4277
        bvect_min1.combine_sub(bvect_min2);
 
4278
 
 
4279
        print_mv(bvect_min1, 64);
 
4280
 
 
4281
        CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
 
4282
 
 
4283
        printf("gap random SUB test ok.\n");
 
4284
   }
 
4285
 
 
4286
   {
 
4287
       printf("GAP comparison test.\n");
 
4288
 
 
4289
       gap_vector gapv1(0);
 
4290
       gap_vector gapv2(0);
 
4291
 
 
4292
       gapv1.set_bit(3);
 
4293
       gapv2.set_bit(3);
 
4294
 
 
4295
       int res = gapv1.compare(gapv2);
 
4296
       if (res != 0)
 
4297
       {
 
4298
           printf("GAP comparison failed!");
 
4299
           exit(1);
 
4300
       }
 
4301
 
 
4302
       gapv1.set_bit(4);
 
4303
       gapv2.set_bit(4);
 
4304
 
 
4305
       res = gapv1.compare(gapv2);
 
4306
       if (res != 0)
 
4307
       {
 
4308
           printf("GAP comparison failed!");
 
4309
           exit(1);
 
4310
       }
 
4311
 
 
4312
       gapv1.set_bit(0);
 
4313
       gapv1.set_bit(1);
 
4314
 
 
4315
       res = gapv1.compare(gapv2);
 
4316
       if (res != 1)
 
4317
       {
 
4318
           printf("GAP comparison failed!");
 
4319
           exit(1);
 
4320
       }
 
4321
 
 
4322
       gapv2.set_bit(0);
 
4323
       gapv2.set_bit(1);
 
4324
       res = gapv1.compare(gapv2);
 
4325
       if (res != 0)
 
4326
       {
 
4327
           printf("GAP comparison failed!");
 
4328
           exit(1);
 
4329
       }
 
4330
 
 
4331
       gapv1.clear_bit(1);
 
4332
 
 
4333
       res = gapv1.compare(gapv2);
 
4334
       if (res != -1)
 
4335
       {
 
4336
           printf("GAP comparison failed!");
 
4337
           exit(1);
 
4338
       }
 
4339
 
 
4340
 
 
4341
   }
 
4342
 
 
4343
 
 
4344
}
 
4345
 
 
4346
// -----------------------------------------------------------------------------
 
4347
 
 
4348
void MutationTest()
 
4349
{
 
4350
 
 
4351
    cout << "--------------------------------- MutationTest" << endl;
 
4352
 
 
4353
    bvect_mini     bvect_min(BITVECT_SIZE);
 
4354
    bvect          bvect_full;
 
4355
 
 
4356
    printf("\nMutation test.\n");
 
4357
 
 
4358
    bvect_full.set_new_blocks_strat(bm::BM_GAP);
 
4359
 
 
4360
    bvect_full.set_bit(5);
 
4361
    bvect_full.set_bit(5);
 
4362
 
 
4363
    bvect_min.set_bit(5);
 
4364
 
 
4365
    bvect_full.set_bit(65535);
 
4366
    bvect_full.set_bit(65537);
 
4367
    bvect_min.set_bit(65535);
 
4368
    bvect_min.set_bit(65537);
 
4369
 
 
4370
    bvect_min.set_bit(100000);
 
4371
    bvect_full.set_bit(100000);
 
4372
 
 
4373
bvect_full.stat();
 
4374
    // detailed vectors verification
 
4375
    ::CheckVectors(bvect_min, bvect_full, ITERATIONS, false);
 
4376
 
 
4377
    int i;
 
4378
    for (i = 5; i < 20000; i+=3)
 
4379
    {
 
4380
        bvect_min.set_bit(i);
 
4381
        bvect_full.set_bit(i);
 
4382
    }
 
4383
bvect_full.stat();
 
4384
    ::CheckVectors(bvect_min, bvect_full, ITERATIONS, false);
 
4385
 
 
4386
    for (i = 100000; i < 200000; i+=3)
 
4387
    {
 
4388
        bvect_min.set_bit(i);
 
4389
        bvect_full.set_bit(i);
 
4390
    }
 
4391
 
 
4392
    ::CheckVectors(bvect_min, bvect_full, 300000);
 
4393
 
 
4394
    // set-clear functionality
 
4395
 
 
4396
    {
 
4397
        printf("Set-clear functionality test.");
 
4398
 
 
4399
        bvect_mini     bvect_min(BITVECT_SIZE);
 
4400
        bvect          bvect_full;
 
4401
        bvect_full.set_new_blocks_strat(bm::BM_GAP);
 
4402
 
 
4403
        int i;
 
4404
        for (i = 100000; i < 100010; ++i)
 
4405
        {
 
4406
            bvect_min.set_bit(i);
 
4407
            bvect_full.set_bit(i);            
 
4408
        }
 
4409
        ::CheckVectors(bvect_min, bvect_full, 300000);
 
4410
 
 
4411
        for (i = 100000; i < 100010; ++i)
 
4412
        {
 
4413
            bvect_min.clear_bit(i);
 
4414
            bvect_full.clear_bit(i);            
 
4415
        }
 
4416
        ::CheckVectors(bvect_min, bvect_full, 300000);
 
4417
        
 
4418
        bvect_full.optimize();
 
4419
        CheckVectors(bvect_min, bvect_full, 65536);//max+10);
 
4420
    }
 
4421
 
 
4422
}
 
4423
 
 
4424
void MutationOperationsTest()
 
4425
{
 
4426
 
 
4427
   cout << "------------------------------------ MutationOperationsTest" << endl;
 
4428
 
 
4429
   printf("Mutation operations test 1.\n");
 
4430
   {
 
4431
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
4432
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
4433
    bvect        bvect_full1;
 
4434
    bvect        bvect_full2;
 
4435
 
 
4436
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
4437
    bvect_full2.set_new_blocks_strat(bm::BM_BIT);
 
4438
 
 
4439
    bvect_full1.set_bit(100);
 
4440
    bvect_min1.set_bit(100);
 
4441
 
 
4442
    int i;
 
4443
    for(i = 0; i < 10000; i+=2)
 
4444
    {
 
4445
       bvect_full2.set_bit(i);
 
4446
       bvect_min2.set_bit(i);
 
4447
    }
 
4448
    bvect_full2.stat();
 
4449
    CheckVectors(bvect_min2, bvect_full2, 65536, true);
 
4450
    
 
4451
    bvect_min1.combine_and(bvect_min2);
 
4452
    bvect_full1.bit_and(bvect_full2);
 
4453
 
 
4454
    CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
 
4455
 
 
4456
   }
 
4457
 
 
4458
   printf("Mutation operations test 2.\n");
 
4459
   {
 
4460
    unsigned delta = 65536;
 
4461
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
4462
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
4463
    bvect        bvect_full1;
 
4464
    bvect        bvect_full2;
 
4465
 
 
4466
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
4467
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
4468
 
 
4469
    int i;
 
4470
    for(i = 0; i < 1000; i+=1)
 
4471
    {
 
4472
       bvect_full1.set_bit(delta+i);
 
4473
       bvect_min1.set_bit(delta+i);
 
4474
    }
 
4475
 
 
4476
    for(i = 0; i < 100; i+=2)
 
4477
    {
 
4478
       bvect_full2.set_bit(delta+i);
 
4479
       bvect_min2.set_bit(delta+i);
 
4480
    }
 
4481
//    CheckVectors(bvect_min2, bvect_full2, 65536);
 
4482
    
 
4483
    bvect_min1.combine_and(bvect_min2);
 
4484
    bvect_full1.bit_and(bvect_full2);
 
4485
 
 
4486
    CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
 
4487
    bvect_full1.optimize();
 
4488
    CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
 
4489
 
 
4490
   }
 
4491
 
 
4492
   {
 
4493
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
4494
    bvect        bvect_full1;
 
4495
 
 
4496
    bvect_full1.set_bit(3);
 
4497
    bvect_min1.set_bit(3);
 
4498
 
 
4499
    struct bvect::statistics st;
 
4500
    bvect_full1.calc_stat(&st);
 
4501
 
 
4502
    // serialization
 
4503
 
 
4504
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
 
4505
    unsigned slen = bm::serialize(bvect_full1, sermem);
 
4506
    cout << "BVECTOR SERMEM=" << slen << endl;
 
4507
 
 
4508
 
 
4509
    bvect        bvect_full3;
 
4510
    bm::deserialize(bvect_full3, sermem);
 
4511
    bvect_full3.stat();
 
4512
    CheckVectors(bvect_min1, bvect_full3, 100, true);
 
4513
   }
 
4514
 
 
4515
 
 
4516
   printf("Mutation operations test 3.\n");
 
4517
   {
 
4518
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
4519
    bvect_mini   bvect_min2(BITVECT_SIZE);
 
4520
    bvect        bvect_full1;
 
4521
    bvect        bvect_full2;
 
4522
 
 
4523
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
4524
    bvect_full2.set_new_blocks_strat(bm::BM_GAP);
 
4525
 
 
4526
   
 
4527
    unsigned min = BITVECT_SIZE / 2 - ITERATIONS;
 
4528
    unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
 
4529
    if (max > BITVECT_SIZE) 
 
4530
        max = BITVECT_SIZE - 1;
 
4531
 
 
4532
    unsigned len = max - min;
 
4533
 
 
4534
    FillSets(&bvect_min1, &bvect_full1, min, max, 0);
 
4535
    FillSets(&bvect_min1, &bvect_full1, 0, len, 5);
 
4536
    printf("Bvect_FULL 1 STAT\n");
 
4537
    bvect_full1.stat();
 
4538
//    CheckVectors(bvect_min1, bvect_full1, max+10, false);
 
4539
    FillSets(&bvect_min2, &bvect_full2, min, max, 0);
 
4540
    FillSets(&bvect_min2, &bvect_full2, 0, len, 0);
 
4541
    printf("Bvect_FULL 2 STAT\n");
 
4542
    bvect_full2.stat();
 
4543
//    CheckVectors(bvect_min2, bvect_full2, max+10);
 
4544
    
 
4545
 
 
4546
    bvect_min1.combine_and(bvect_min2);
 
4547
    bvect_full1.bit_and(bvect_full2);
 
4548
    printf("Bvect_FULL 1 STAT after AND\n");
 
4549
    bvect_full1.stat();
 
4550
 
 
4551
    CheckVectors(bvect_min1, bvect_full1, max+10, false);
 
4552
 
 
4553
    struct bvect::statistics st;
 
4554
    bvect_full1.calc_stat(&st);
 
4555
    cout << "BVECTOR: GAP=" << st.gap_blocks << " BIT=" << st.bit_blocks 
 
4556
         << " MEM=" << st.memory_used << " SERMAX=" << st.max_serialize_mem
 
4557
         << endl;
 
4558
    cout << "MINIVECT: " << bvect_min1.mem_used() << endl;
 
4559
 
 
4560
    bvect_full1.optimize();
 
4561
    bvect_full1.stat();
 
4562
 
 
4563
    CheckVectors(bvect_min1, bvect_full1, max+10, false);
 
4564
 
 
4565
    bvect_full1.calc_stat(&st);
 
4566
    cout << "BVECTOR: GAP=" << st.gap_blocks << " BIT=" << st.bit_blocks 
 
4567
         << " MEM=" << st.memory_used << " SERMAX=" << st.max_serialize_mem
 
4568
         << endl;
 
4569
    cout << "MINIVECT: " << bvect_min1.mem_used() << endl;
 
4570
 
 
4571
 
 
4572
 
 
4573
    // serialization
 
4574
 
 
4575
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
 
4576
    unsigned slen = bm::serialize(bvect_full1, sermem);
 
4577
    cout << "BVECTOR SERMEM=" << slen << endl;
 
4578
 
 
4579
 
 
4580
    
 
4581
    bvect        bvect_full3;
 
4582
    bm::deserialize(bvect_full3, sermem);
 
4583
    bvect_full3.stat();
 
4584
    CheckVectors(bvect_min1, bvect_full3, max+10, true);
 
4585
    
 
4586
    delete [] sermem;
 
4587
    
 
4588
 
 
4589
    cout << "Copy constructor check." << endl;
 
4590
 
 
4591
 
 
4592
    {
 
4593
    bvect       bvect_full4(bvect_full3);
 
4594
    bvect_full3.stat();
 
4595
    CheckVectors(bvect_min1, bvect_full4, max+10, true);
 
4596
    }
 
4597
    
 
4598
 
 
4599
   }
 
4600
 
 
4601
}
 
4602
 
 
4603
 
 
4604
void SerializationTest()
 
4605
{
 
4606
 
 
4607
   cout << " ----------------------------------- SerializationTest" << endl;
 
4608
 
 
4609
   cout << "Serialization STEP 0" << endl;
 
4610
 
 
4611
   {
 
4612
    unsigned size = BITVECT_SIZE/6000;
 
4613
 
 
4614
 
 
4615
    bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
 
4616
    bvect*        bvect_full1= new bvect();
 
4617
    bvect*        bvect_full2= new bvect();
 
4618
    bvect*        bv_target_s= new bvect();
 
4619
 
 
4620
    bvect_full1->set_new_blocks_strat(bm::BM_BIT);
 
4621
    bvect_full2->set_new_blocks_strat(bm::BM_BIT);
 
4622
 
 
4623
    for(unsigned i = 0; i < size; ++i)
 
4624
    {
 
4625
        bvect_full1->set_bit(i);
 
4626
        bvect_min1->set_bit(i);
 
4627
    }
 
4628
 
 
4629
    bvect_full1->optimize();
 
4630
    CheckVectors(*bvect_min1, *bvect_full1, size, true);
 
4631
 
 
4632
 
 
4633
 
 
4634
    bvect::statistics st;
 
4635
    bvect_full1->calc_stat(&st);
 
4636
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
 
4637
    unsigned slen = bm::serialize(*bvect_full1, sermem);
 
4638
    cout << "Serialized mem_max = " << st.max_serialize_mem 
 
4639
         << " size= " << slen 
 
4640
         << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
 
4641
         << endl;
 
4642
 
 
4643
    bm::deserialize(*bvect_full2, sermem);
 
4644
    operation_deserializer<bvect>::deserialize(*bv_target_s,
 
4645
                                               sermem,
 
4646
                                               0,
 
4647
                                               set_OR);
 
4648
    delete [] sermem;
 
4649
 
 
4650
 
 
4651
    CheckVectors(*bvect_min1, *bvect_full2, size, true);
 
4652
    CheckVectors(*bvect_min1, *bv_target_s, size, true);
 
4653
 
 
4654
 
 
4655
    delete bvect_full2;
 
4656
    delete bvect_min1;
 
4657
    delete bvect_full1;
 
4658
    delete bv_target_s;
 
4659
 
 
4660
    }
 
4661
 
 
4662
 
 
4663
   {
 
4664
    unsigned size = BITVECT_SIZE/6000;
 
4665
 
 
4666
 
 
4667
    bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
 
4668
    bvect*        bvect_full1= new bvect();
 
4669
    bvect*        bvect_full2= new bvect();
 
4670
    bvect*        bv_target_s= new bvect();
 
4671
 
 
4672
    bvect_full1->set_new_blocks_strat(bm::BM_BIT);
 
4673
    bvect_full2->set_new_blocks_strat(bm::BM_BIT);
 
4674
 
 
4675
        bvect_full1->set_bit(131072);
 
4676
        bvect_min1->set_bit(131072);
 
4677
    
 
4678
 
 
4679
    bvect_full1->optimize();
 
4680
 
 
4681
    bvect::statistics st;
 
4682
    bvect_full1->calc_stat(&st);
 
4683
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
 
4684
    unsigned slen = bm::serialize(*bvect_full1, sermem);
 
4685
    cout << "Serialized mem_max = " << st.max_serialize_mem 
 
4686
         << " size= " << slen 
 
4687
         << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
 
4688
         << endl;
 
4689
 
 
4690
    bm::deserialize(*bvect_full2, sermem);
 
4691
    operation_deserializer<bvect>::deserialize(*bv_target_s,
 
4692
                                               sermem,
 
4693
                                               0,
 
4694
                                               set_OR);
 
4695
 
 
4696
    delete [] sermem;
 
4697
 
 
4698
    CheckVectors(*bvect_min1, *bvect_full2, size, true);
 
4699
    CheckVectors(*bvect_min1, *bv_target_s, size, true);
 
4700
 
 
4701
    delete bvect_full2;
 
4702
    delete bvect_min1;
 
4703
    delete bvect_full1;
 
4704
    delete bv_target_s;
 
4705
 
 
4706
    }
 
4707
 
 
4708
 
 
4709
    cout << "Serialization STEP 1." << endl;
 
4710
 
 
4711
    {
 
4712
    bvect_mini   bvect_min1(BITVECT_SIZE);
 
4713
    bvect        bvect_full1;
 
4714
 
 
4715
    bvect_full1.set_new_blocks_strat(bm::BM_GAP);
 
4716
   
 
4717
    unsigned min = BITVECT_SIZE / 2 - ITERATIONS;
 
4718
    unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
 
4719
    if (max > BITVECT_SIZE) 
 
4720
        max = BITVECT_SIZE - 1;
 
4721
 
 
4722
    unsigned len = max - min;
 
4723
 
 
4724
    FillSets(&bvect_min1, &bvect_full1, min, max, 0);
 
4725
    FillSets(&bvect_min1, &bvect_full1, 0, len, 5);
 
4726
 
 
4727
    // shot some random bits
 
4728
 
 
4729
    int i;
 
4730
    for (i = 0; i < 10000; ++i)
 
4731
    {
 
4732
        unsigned bit = rand() % BITVECT_SIZE;
 
4733
        bvect_full1.set_bit(bit);
 
4734
        bvect_min1.set_bit(bit);
 
4735
    }
 
4736
 
 
4737
    bvect::statistics st;
 
4738
    bvect_full1.calc_stat(&st);
 
4739
 
 
4740
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
 
4741
    bvect_full1.stat();
 
4742
    
 
4743
    unsigned slen = bm::serialize(bvect_full1, sermem);
 
4744
 
 
4745
    cout << "Serialized len = " << slen << endl;
 
4746
 
 
4747
    bvect        bvect_full3;
 
4748
    bm::deserialize(bvect_full3, sermem);
 
4749
    bvect*  bv_target_s = new bvect();
 
4750
    operation_deserializer<bvect>::deserialize(*bv_target_s,
 
4751
                                               sermem,
 
4752
                                               0,
 
4753
                                               set_OR);
 
4754
 
 
4755
    CheckVectors(bvect_min1, bvect_full3, max+10, true);
 
4756
    CheckVectors(bvect_min1, *bv_target_s, max+10, true);
 
4757
 
 
4758
    delete [] sermem;
 
4759
    delete bv_target_s;
 
4760
 
 
4761
    }
 
4762
 
 
4763
 
 
4764
   cout << "Stage 2" << endl;
 
4765
 
 
4766
   {
 
4767
 
 
4768
    bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
 
4769
//    bm::bvect_mini*   bvect_min2= new bm::bvect_mini(BITVECT_SIZE);
 
4770
    bvect*        bvect_full1= new bvect();
 
4771
    bvect*        bvect_full2= new bvect();
 
4772
 
 
4773
    bvect_full1->set_new_blocks_strat(bm::BM_GAP);
 
4774
    bvect_full2->set_new_blocks_strat(bm::BM_GAP);
 
4775
 
 
4776
    FillSetsRandomMethod(bvect_min1, bvect_full1, 1, BITVECT_SIZE-10, 1);
 
4777
//    FillSetsRandomMethod(bvect_min2, bvect_full2, 1, BITVECT_SIZE-10, 1);
 
4778
 
 
4779
//bvect_full1->stat();
 
4780
cout << "Filling. OK." << endl;
 
4781
    bvect::statistics st;
 
4782
    bvect_full1->calc_stat(&st);
 
4783
cout << st.max_serialize_mem << endl;
 
4784
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
 
4785
cout << "Serialization" << endl;
 
4786
    unsigned slen = bm::serialize(*bvect_full1, sermem);
 
4787
 
 
4788
    cout << "Serialized mem_max = " << st.max_serialize_mem 
 
4789
         << " size= " << slen 
 
4790
         << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
 
4791
         << endl;
 
4792
cout << "Deserialization" << endl;
 
4793
    bm::deserialize(*bvect_full2, sermem);
 
4794
cout << "Deserialization ok" << endl;
 
4795
    bvect*  bv_target_s=new bvect();
 
4796
    operation_deserializer<bvect>::deserialize(*bv_target_s,
 
4797
                                               sermem,
 
4798
                                               0,
 
4799
                                               set_OR);
 
4800
 
 
4801
    CheckVectors(*bvect_min1, *bvect_full2, BITVECT_SIZE, true);
 
4802
    CheckVectors(*bvect_min1, *bv_target_s, BITVECT_SIZE, true);
 
4803
 
 
4804
    delete [] sermem;
 
4805
 
 
4806
    delete bv_target_s;
 
4807
    delete bvect_full2;
 
4808
    delete bvect_min1;
 
4809
    delete bvect_full1;
 
4810
 
 
4811
    }
 
4812
 
 
4813
 
 
4814
 
 
4815
   cout << "Stage 3" << endl;
 
4816
 
 
4817
   {
 
4818
 
 
4819
    bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
 
4820
    bvect_mini*   bvect_min2= new bvect_mini(BITVECT_SIZE);
 
4821
    bvect*        bvect_full1= new bvect();
 
4822
    bvect*        bvect_full2= new bvect();
 
4823
 
 
4824
    bvect_full1->set_new_blocks_strat(bm::BM_GAP);
 
4825
    bvect_full2->set_new_blocks_strat(bm::BM_GAP);
 
4826
 
 
4827
 
 
4828
    FillSetsRandomMethod(bvect_min1, bvect_full1, 1, BITVECT_SIZE, 1);
 
4829
    FillSetsRandomMethod(bvect_min2, bvect_full2, 1, BITVECT_SIZE, 1);
 
4830
 
 
4831
    bvect::statistics st;
 
4832
    bvect_full1->calc_stat(&st);
 
4833
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
 
4834
    unsigned slen = bm::serialize(*bvect_full1, sermem);
 
4835
    delete bvect_full1;
 
4836
 
 
4837
    cout << "Serialized mem_max = " << st.max_serialize_mem 
 
4838
         << " size= " << slen 
 
4839
         << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
 
4840
         << endl;
 
4841
 
 
4842
    bvect*  bv_target_s=new bvect(*bvect_full2);
 
4843
    bv_target_s->stat();
 
4844
 
 
4845
    bm::deserialize(*bvect_full2, sermem);
 
4846
 
 
4847
    operation_deserializer<bvect>::deserialize(*bv_target_s,
 
4848
                                               sermem,
 
4849
                                               0,
 
4850
                                               set_OR);
 
4851
    delete [] sermem;
 
4852
    
 
4853
    bvect_min2->combine_or(*bvect_min1);
 
4854
    delete bvect_min1;
 
4855
 
 
4856
    CheckVectors(*bvect_min2, *bvect_full2, BITVECT_SIZE, true);
 
4857
    CheckVectors(*bvect_min2, *bv_target_s, BITVECT_SIZE, true);
 
4858
 
 
4859
    delete bv_target_s;
 
4860
    delete bvect_full2;
 
4861
    delete bvect_min2;    
 
4862
 
 
4863
 
 
4864
    }
 
4865
 
 
4866
   cout << "Stage 4. " << endl;
 
4867
 
 
4868
   {
 
4869
    unsigned size = BITVECT_SIZE/3;
 
4870
 
 
4871
 
 
4872
    bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
 
4873
    bvect*        bvect_full1= new bvect();
 
4874
    bvect*        bvect_full2= new bvect();
 
4875
 
 
4876
    bvect_full1->set_new_blocks_strat(bm::BM_BIT);
 
4877
    bvect_full2->set_new_blocks_strat(bm::BM_BIT);
 
4878
 
 
4879
    unsigned i;
 
4880
    for(i = 0; i < 65000; ++i)
 
4881
    {
 
4882
        bvect_full1->set_bit(i);
 
4883
        bvect_min1->set_bit(i);
 
4884
    }
 
4885
 
 
4886
    for(i = 65536; i < 65536+65000; ++i)
 
4887
    {
 
4888
        bvect_full1->set_bit(i);
 
4889
        bvect_min1->set_bit(i);
 
4890
    }
 
4891
 
 
4892
    for (i = 65536*2; i < size/6; ++i)
 
4893
    {
 
4894
        bvect_full1->set_bit(i);
 
4895
        bvect_min1->set_bit(i);
 
4896
    }
 
4897
 
 
4898
 
 
4899
    bvect_full1->optimize();
 
4900
 
 
4901
    bvect_full1->stat();
 
4902
 
 
4903
 
 
4904
    bvect::statistics st;
 
4905
    bvect_full1->calc_stat(&st);
 
4906
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
 
4907
    unsigned slen = bm::serialize(*bvect_full1, sermem);
 
4908
    cout << "Serialized mem_max = " << st.max_serialize_mem 
 
4909
         << " size= " << slen 
 
4910
         << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
 
4911
         << endl;
 
4912
    
 
4913
    unsigned char* new_sermem = new unsigned char[st.max_serialize_mem];
 
4914
    ::memcpy(new_sermem, sermem, slen);
 
4915
 
 
4916
    bvect  bv_target_s(*bvect_full2);
 
4917
 
 
4918
    bm::deserialize(*bvect_full2, new_sermem);
 
4919
    operation_deserializer<bvect>::deserialize(bv_target_s,
 
4920
                                               new_sermem,
 
4921
                                               0,
 
4922
                                               set_OR);
 
4923
 
 
4924
    delete [] sermem;
 
4925
    delete [] new_sermem;
 
4926
 
 
4927
    CheckVectors(*bvect_min1, *bvect_full2, size, true);
 
4928
    CheckVectors(*bvect_min1, bv_target_s, size, true);
 
4929
 
 
4930
 
 
4931
    delete bvect_full2;
 
4932
    delete bvect_min1;
 
4933
    delete bvect_full1;
 
4934
 
 
4935
    }
 
4936
 
 
4937
 
 
4938
}
 
4939
 
 
4940
void GetNextTest()
 
4941
{
 
4942
   cout << "-------------------------------------------- GetNextTest" << endl;
 
4943
 
 
4944
   int i;
 
4945
   for(i = 0; i < 2; ++i)
 
4946
   {
 
4947
      cout << "Strategy " << i << endl;
 
4948
 
 
4949
   {
 
4950
      bvect       bvect_full1;
 
4951
      bvect_mini  bvect_min1(BITVECT_SIZE);
 
4952
 
 
4953
      bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
 
4954
 
 
4955
      bvect_full1.set_bit(0);
 
4956
      bvect_min1.set_bit(0);
 
4957
 
 
4958
 
 
4959
      bvect_full1.set_bit(65536);
 
4960
      bvect_min1.set_bit(65536);
 
4961
 
 
4962
      unsigned nbit1 = bvect_full1.get_first();
 
4963
      unsigned nbit2 = bvect_min1.get_first();
 
4964
 
 
4965
      if (nbit1 != nbit2)
 
4966
      {
 
4967
         cout << "1. get_first failed() " <<  nbit1 << " " << nbit2 << endl;
 
4968
         exit(1);
 
4969
      }
 
4970
      nbit1 = bvect_full1.get_next(nbit1);
 
4971
      nbit2 = bvect_min1.get_next(nbit2);
 
4972
      if ((nbit1 != nbit2) || (nbit1 != 65536))
 
4973
      {
 
4974
         cout << "1. get_next failed() " <<  nbit1 << " " << nbit2 << endl;
 
4975
         exit(1);
 
4976
      }
 
4977
   }
 
4978
 
 
4979
 
 
4980
 
 
4981
   {
 
4982
      bvect       bvect_full1;
 
4983
      bvect_mini  bvect_min1(BITVECT_SIZE);
 
4984
      bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
 
4985
 
 
4986
      bvect_full1.set_bit(65535);
 
4987
      bvect_min1.set_bit(65535);
 
4988
 
 
4989
      unsigned nbit1 = bvect_full1.get_first();
 
4990
      unsigned nbit2 = bvect_min1.get_first();
 
4991
 
 
4992
      if ((nbit1 != nbit2) || (nbit1 != 65535))
 
4993
      {
 
4994
         cout << "1. get_first failed() " <<  nbit1 << " " << nbit2 << endl;
 
4995
         exit(1);
 
4996
      }
 
4997
      nbit1 = bvect_full1.get_next(nbit1);
 
4998
      nbit2 = bvect_min1.get_next(nbit2);
 
4999
      if (nbit1 != nbit2 )
 
5000
      {
 
5001
         cout << "1. get_next failed() " <<  nbit1 << " " << nbit2 << endl;
 
5002
         exit(1);
 
5003
      }
 
5004
   }
 
5005
 
 
5006
   {
 
5007
      cout << "--------------" << endl;
 
5008
      bvect       bvect_full1;
 
5009
      bvect_mini  bvect_min1(BITVECT_SIZE);
 
5010
      bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
 
5011
 
 
5012
      bvect_full1.set_bit(655350);
 
5013
      bvect_min1.set_bit(655350);
 
5014
 
 
5015
      unsigned nbit1 = bvect_full1.get_first();
 
5016
      unsigned nbit2 = bvect_min1.get_first();
 
5017
 
 
5018
      if (nbit1 != nbit2 || nbit1 != 655350)
 
5019
      {
 
5020
         cout << "1. get_first failed() " <<  nbit1 << " " << nbit2 << endl;
 
5021
         exit(1);
 
5022
      }
 
5023
 
 
5024
      nbit1 = bvect_full1.get_next(nbit1);
 
5025
      nbit2 = bvect_min1.get_next(nbit2);
 
5026
      if (nbit1 != nbit2)
 
5027
      {
 
5028
         cout << "1. get_next failed() " <<  nbit1 << " " << nbit2 << endl;
 
5029
         exit(1);
 
5030
      }
 
5031
   }
 
5032
 
 
5033
 
 
5034
   {
 
5035
   bvect       bvect_full1;
 
5036
   bvect_mini  bvect_min1(BITVECT_SIZE);
 
5037
 
 
5038
   bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
 
5039
 
 
5040
   bvect_full1.set_bit(256);
 
5041
   bvect_min1.set_bit(256);
 
5042
 
 
5043
//   bvect_full1.clear_bit(256);
 
5044
   bvect_full1.set_bit(65536);
 
5045
   bvect_min1.set_bit(65536);
 
5046
 
 
5047
   unsigned nbit1 = bvect_full1.get_first();
 
5048
   unsigned nbit2 = bvect_min1.get_first();
 
5049
 
 
5050
   if (nbit1 != nbit2)
 
5051
   {
 
5052
      cout << "get_first failed " <<  nbit1 << " " << nbit2 << endl;
 
5053
      exit(1);
 
5054
   }
 
5055
 
 
5056
   while (nbit1)
 
5057
   {
 
5058
      cout << nbit1 << endl;
 
5059
      nbit1 = bvect_full1.get_next(nbit1);
 
5060
      nbit2 = bvect_min1.get_next(nbit2);
 
5061
      if (nbit1 != nbit2)
 
5062
      {
 
5063
         cout << "get_next failed " <<  nbit1 << " " << nbit2 << endl;
 
5064
         exit(1);
 
5065
      }
 
5066
 
 
5067
   } // while
 
5068
 
 
5069
   }
 
5070
 
 
5071
   
 
5072
   }// for
 
5073
 
 
5074
}
 
5075
 
 
5076
// Test contributed by Maxim Shemanarev.
 
5077
 
 
5078
void MaxSTest()
 
5079
{
 
5080
   bvect vec;
 
5081
 
 
5082
   int i, j;
 
5083
   unsigned id;
 
5084
   for(i = 0; i < 100; i++)
 
5085
   {
 
5086
      int n = rand() % 2000 + 1;
 
5087
      id = 1;
 
5088
      for(j = 0; j < n; j++)
 
5089
      {
 
5090
         id += rand() % 10 + 1;
 
5091
         vec.set_bit(id);
 
5092
 
 
5093
      }
 
5094
      vec.optimize();
 
5095
      vec.clear();
 
5096
      fprintf(stderr, ".");
 
5097
   }
 
5098
}
 
5099
 
 
5100
 
 
5101
void CalcBeginMask()
 
5102
{
 
5103
    printf("BeginMask:\n");
 
5104
 
 
5105
    int i;
 
5106
    for (i = 0; i < 32; ++i)
 
5107
    {
 
5108
    unsigned mask = 0;
 
5109
 
 
5110
        for(int j = i; j < 32; ++j)
 
5111
        {
 
5112
            unsigned nbit  = j; 
 
5113
            nbit &= bm::set_word_mask;
 
5114
            bm::word_t  mask1 = (((bm::word_t)1) << j);
 
5115
 
 
5116
            mask |= mask1;
 
5117
        }
 
5118
 
 
5119
        printf("0x%x, ", mask);
 
5120
        
 
5121
    } 
 
5122
    printf("\n");
 
5123
}
 
5124
 
 
5125
void CalcEndMask()
 
5126
{
 
5127
    printf("EndMask:\n");
 
5128
 
 
5129
    int i;
 
5130
    for (i = 0; i < 32; ++i)
 
5131
    {
 
5132
    unsigned mask = 1;
 
5133
 
 
5134
        for(int j = i; j > 0; --j)
 
5135
        {
 
5136
            unsigned nbit  = j; 
 
5137
            nbit &= bm::set_word_mask;
 
5138
            bm::word_t  mask1 = (((bm::word_t)1) << j);
 
5139
 
 
5140
            mask |= mask1;
 
5141
        }
 
5142
 
 
5143
        printf("0x%x,", mask);
 
5144
        
 
5145
    } 
 
5146
    printf("\n");
 
5147
}
 
5148
 
 
5149
 
 
5150
void EnumeratorTest()
 
5151
{
 
5152
    cout << "-------------------------------------------- EnumeratorTest" << endl;
 
5153
 
 
5154
    {
 
5155
    bvect bvect1;
 
5156
 
 
5157
    bvect1.set_bit(100);
 
5158
 
 
5159
    bvect::enumerator en = bvect1.first();
 
5160
 
 
5161
    if (*en != 100)
 
5162
    {
 
5163
        cout << "Enumerator error !" << endl;
 
5164
        exit(1);
 
5165
    }
 
5166
 
 
5167
    bvect1.clear_bit(100);
 
5168
 
 
5169
    bvect1.set_bit(2000000000);
 
5170
    en.go_first();
 
5171
 
 
5172
    if (*en != 2000000000)
 
5173
    {
 
5174
        cout << "Enumerator error !" << endl;
 
5175
        exit(1);
 
5176
    }
 
5177
    }
 
5178
 
 
5179
    {
 
5180
        bvect bvect1;
 
5181
        bvect1.set_bit(0);
 
5182
        bvect1.set_bit(10);
 
5183
        bvect1.set_bit(35);
 
5184
        bvect1.set_bit(1000);
 
5185
        bvect1.set_bit(2016519);
 
5186
        bvect1.set_bit(2034779);
 
5187
        bvect1.set_bit(bm::id_max-1);
 
5188
 
 
5189
        bvect::enumerator en = bvect1.first();
 
5190
 
 
5191
        unsigned num = bvect1.get_first();
 
5192
 
 
5193
        bvect::enumerator end = bvect1.end();
 
5194
        while (en < end)
 
5195
        {
 
5196
            if (*en != num)
 
5197
            {
 
5198
                cout << "Enumeration comparison failed !" << 
 
5199
                        " enumerator = " << *en <<
 
5200
                        " get_next() = " << num << endl; 
 
5201
                exit(1);
 
5202
            }
 
5203
            ++en;
 
5204
            num = bvect1.get_next(num);
 
5205
        }
 
5206
        if (num != 0)
 
5207
        {
 
5208
            cout << "Enumeration error!" << endl;
 
5209
            exit(1);
 
5210
        }
 
5211
    }
 
5212
/*
 
5213
    {
 
5214
        bvect bvect1;
 
5215
        bvect1.set();
 
5216
 
 
5217
        bvect::enumerator en = bvect1.first();
 
5218
 
 
5219
        unsigned num = bvect1.get_first();
 
5220
 
 
5221
        while (en < bvect1.end())
 
5222
        {
 
5223
            if (*en != num)
 
5224
            {
 
5225
                cout << "Enumeration comparison failed !" << 
 
5226
                        " enumerator = " << *en <<
 
5227
                        " get_next() = " << num << endl; 
 
5228
                exit(1);
 
5229
            }
 
5230
 
 
5231
            ++en;
 
5232
            num = bvect1.get_next(num);
 
5233
        }
 
5234
        if (num != 0)
 
5235
        {
 
5236
            cout << "Enumeration error!" << endl;
 
5237
            exit(1);
 
5238
        }
 
5239
    }
 
5240
*/
 
5241
 
 
5242
    {
 
5243
        bvect bvect1;
 
5244
 
 
5245
        int i;
 
5246
        for(i = 0; i < 65536; ++i)
 
5247
        {
 
5248
            bvect1.set_bit(i);
 
5249
        }
 
5250
/*
 
5251
        for(i = 65536*10; i < 65536*20; i+=3)
 
5252
        {
 
5253
            bvect1.set_bit(i);
 
5254
        }
 
5255
*/
 
5256
 
 
5257
        bvect::enumerator en = bvect1.first();
 
5258
 
 
5259
        unsigned num = bvect1.get_first();
 
5260
 
 
5261
        while (en < bvect1.end())
 
5262
        {
 
5263
            if (*en != num)
 
5264
            {
 
5265
                cout << "Enumeration comparison failed !" << 
 
5266
                        " enumerator = " << *en <<
 
5267
                        " get_next() = " << num << endl; 
 
5268
                exit(1);
 
5269
            }
 
5270
            ++en;
 
5271
            num = bvect1.get_next(num);
 
5272
            if (num == 31)
 
5273
            {
 
5274
                num = num + 0;
 
5275
            }
 
5276
        }
 
5277
        if (num != 0)
 
5278
        {
 
5279
            cout << "Enumeration error!" << endl;
 
5280
            exit(1);
 
5281
        }
 
5282
    }
 
5283
 
 
5284
 
 
5285
    {
 
5286
    bvect bvect1;
 
5287
    bvect1.set_new_blocks_strat(bm::BM_GAP);
 
5288
    bvect1.set_bit(100);
 
5289
 
 
5290
    bvect::enumerator en = bvect1.first();
 
5291
 
 
5292
    if (*en != 100)
 
5293
    {
 
5294
        cout << "Enumerator error !" << endl;
 
5295
        exit(1);
 
5296
    }
 
5297
 
 
5298
    bvect1.clear_bit(100);
 
5299
 
 
5300
    bvect1.set_bit(2000000);
 
5301
    en.go_first();
 
5302
 
 
5303
    if (*en != 2000000)
 
5304
    {
 
5305
        cout << "Enumerator error !" << endl;
 
5306
        exit(1);
 
5307
    }
 
5308
    bvect1.stat();
 
5309
    }
 
5310
 
 
5311
    {
 
5312
        bvect bvect1;
 
5313
        bvect1.set_new_blocks_strat(bm::BM_GAP);
 
5314
        bvect1.set_bit(0);
 
5315
        bvect1.set_bit(1);
 
5316
        bvect1.set_bit(10);
 
5317
        bvect1.set_bit(100);
 
5318
        bvect1.set_bit(1000);
 
5319
 
 
5320
        bvect::enumerator en = bvect1.first();
 
5321
 
 
5322
        unsigned num = bvect1.get_first();
 
5323
 
 
5324
        while (en < bvect1.end())
 
5325
        {
 
5326
            if (*en != num)
 
5327
            {
 
5328
                cout << "Enumeration comparison failed !" << 
 
5329
                        " enumerator = " << *en <<
 
5330
                        " get_next() = " << num << endl; 
 
5331
                exit(1);
 
5332
            }
 
5333
            ++en;
 
5334
            num = bvect1.get_next(num);
 
5335
        }
 
5336
        if (num != 0)
 
5337
        {
 
5338
            cout << "Enumeration error!" << endl;
 
5339
            exit(1);
 
5340
        }
 
5341
    }
 
5342
 
 
5343
 
 
5344
}
 
5345
 
 
5346
 
 
5347
 
 
5348
void BlockLevelTest()
 
5349
{
 
5350
    bvect  bv;
 
5351
    bvect  bv2;
 
5352
 
 
5353
    bv.set_new_blocks_strat(bm::BM_GAP);
 
5354
    bv2.set_new_blocks_strat(bm::BM_GAP);
 
5355
 
 
5356
    int i;
 
5357
    for (i = 0; i < 500; i+=1)
 
5358
    {
 
5359
        bv.set_bit(i);
 
5360
    }
 
5361
    bv.stat();
 
5362
 
 
5363
    for (i = 0; i < 1000; i+=2)
 
5364
    {
 
5365
        bv2.set_bit(i);
 
5366
    }
 
5367
    bv2.stat();
 
5368
 
 
5369
    struct bvect::statistics st;
 
5370
    bv2.calc_stat(&st);
 
5371
 
 
5372
    unsigned char* sermem = new unsigned char[st.max_serialize_mem];
 
5373
 
 
5374
    unsigned slen = bm::serialize(bv2, sermem);
 
5375
    assert(slen);
 
5376
    slen = 0;
 
5377
    
 
5378
    bm::deserialize(bv, sermem);
 
5379
//    bv.optimize();
 
5380
 
 
5381
    bv.stat();
 
5382
 
 
5383
}
 
5384
 
 
5385
/*
 
5386
__int64 CalcBitCount64(__int64 b)
 
5387
{
 
5388
    b = (b & 0x5555555555555555) + (b >> 1 & 0x5555555555555555);
 
5389
    b = (b & 0x3333333333333333) + (b >> 2 & 0x3333333333333333);
 
5390
    b = b + (b >> 4) & 0x0F0F0F0F0F0F0F0F;
 
5391
    b = b + (b >> 8);
 
5392
    b = b + (b >> 16);
 
5393
    b = b + (b >> 32) & 0x0000007F;
 
5394
    return b;
 
5395
}
 
5396
 
 
5397
unsigned CalcBitCount32(unsigned b)
 
5398
{
 
5399
    b = (b & 0x55555555) + (b >> 1 & 0x55555555);
 
5400
    b = (b & 0x33333333) + (b >> 2 & 0x33333333);
 
5401
    b = b + (b >> 4) & 0x0F0F0F0F;
 
5402
    b = b + (b >> 8);
 
5403
    b = b + (b >> 16) & 0x0000003F;
 
5404
    return b;
 
5405
}
 
5406
*/
 
5407
 
 
5408
 
 
5409
void SyntaxTest()
 
5410
{
 
5411
    cout << "----------------------------- Syntax test." << endl;
 
5412
    bvect bv1;
 
5413
    
 
5414
    bvect::allocator_type a = bv1.get_allocator();
 
5415
 
 
5416
    bvect bv2(bv1);
 
5417
    bvect bv3;
 
5418
    bv3.swap(bv1);
 
5419
     
 
5420
    bv1[100] = true;
 
5421
    bool v = bv1[100];
 
5422
    assert(v);
 
5423
    v = false;
 
5424
 
 
5425
    bv1[100] = false;
 
5426
 
 
5427
    bv2 |= bv1;
 
5428
    bv2 &= bv1;
 
5429
    bv2 ^= bv1;
 
5430
    bv2 -= bv1;
 
5431
 
 
5432
    bv3 = bv1 | bv2;
 
5433
 
 
5434
    if (bv1 < bv2)
 
5435
    {
 
5436
    }
 
5437
 
 
5438
    bvect::reference ref = bv1[10];
 
5439
    bool bn = !ref;
 
5440
    bool bn2 = ~ref;
 
5441
 
 
5442
    bn = bn2 = false;
 
5443
 
 
5444
    ref.flip();
 
5445
 
 
5446
    bvect bvn = ~bv1;
 
5447
 
 
5448
    cout << "----------------------------- Syntax test ok." << endl;
 
5449
}
 
5450
 
 
5451
 
 
5452
void SetTest()
 
5453
{
 
5454
    unsigned cnt;
 
5455
    bvect bv;
 
5456
 
 
5457
    bv.set();
 
5458
 
 
5459
    cnt = bv.count();
 
5460
    if (cnt != bm::id_max)
 
5461
    {
 
5462
        cout << "Set test failed!." << endl;
 
5463
        exit(1);
 
5464
    }
 
5465
 
 
5466
    bv.invert();
 
5467
    cnt = bv.count();
 
5468
    if (cnt != 0)
 
5469
    {
 
5470
        cout << "Set invert test failed!." << endl;
 
5471
        exit(1);
 
5472
    }
 
5473
 
 
5474
    bv.set(0);
 
5475
    bv.set(bm::id_max-1);
 
5476
    cnt = bv.count();
 
5477
 
 
5478
    assert(cnt == 2);
 
5479
 
 
5480
    bv.invert();
 
5481
    bv.stat();
 
5482
    cnt = bv.count();
 
5483
 
 
5484
    if (cnt != bm::id_max-2)
 
5485
    {
 
5486
        cout << "Set invert test failed!." << endl;
 
5487
        exit(1);
 
5488
    }
 
5489
 
 
5490
    bv.clear();
 
5491
    bv[1] &= true;
 
5492
    bool v = bv[1];
 
5493
    if (v)
 
5494
    {
 
5495
        cout << "Set &= test failed!" << endl;
 
5496
        exit(1);
 
5497
    }
 
5498
 
 
5499
    bv[1] = true;
 
5500
    bv[1] &= true;
 
5501
    v = bv[1];
 
5502
    if (!v)
 
5503
    {
 
5504
        cout << "Set &= test failed (2)!" << endl;
 
5505
        exit(1);
 
5506
    }
 
5507
    bv.clear(true);
 
5508
    bv.invert();
 
5509
    bv[1] &= true;
 
5510
    v = bv[1];
 
5511
    if (!v)
 
5512
    {
 
5513
        cout << "Set &= test failed (2)!" << endl;
 
5514
        exit(1);
 
5515
    }
 
5516
 
 
5517
    bvect bv2;
 
5518
    bv2[1] = true;
 
5519
    bv2[1] = false;
 
5520
    bvect::statistics stat1;
 
5521
    bv2.calc_stat(&stat1);
 
5522
    
 
5523
    bv2.optimize();
 
5524
 
 
5525
    bvect::statistics stat2;
 
5526
    bv2.calc_stat(&stat2);
 
5527
 
 
5528
    if (stat2.bit_blocks != 0 || 
 
5529
        stat2.gap_blocks != 0 ||
 
5530
        stat1.memory_used <= stat2.memory_used)
 
5531
    {
 
5532
        cout << "Optimization memory free test failed (2)!" << endl;
 
5533
        exit(1);
 
5534
    }
 
5535
 
 
5536
    {
 
5537
        bvect bv3;
 
5538
        bool changed;
 
5539
        changed = bv3.set_bit_conditional(10, true, true);
 
5540
        v = bv3[10];
 
5541
        if (v || changed) {
 
5542
            cout << "Conditional bit set failed." << endl;
 
5543
            exit(1);
 
5544
        }
 
5545
        changed = bv3.set_bit_conditional(10, true, false);
 
5546
        v = bv3[10];
 
5547
        if (!v || !changed) {
 
5548
            cout << "Conditional bit set failed." << endl;
 
5549
            exit(1);
 
5550
        }
 
5551
        changed = bv3.set_bit_conditional(10, false, false);
 
5552
        v = bv3[10];
 
5553
        if (!v || changed) {
 
5554
            cout << "Conditional bit set failed." << endl;
 
5555
            exit(1);
 
5556
        }
 
5557
        changed = bv3.set_bit_conditional(10, false, true);
 
5558
        v = bv3[10];
 
5559
        if (v || !changed) {
 
5560
            cout << "Conditional bit set failed." << endl;
 
5561
            exit(1);
 
5562
        }
 
5563
    }
 
5564
    {
 
5565
        bvect bv3(bm::BM_GAP);
 
5566
        bool changed;
 
5567
        changed = bv3.set_bit_conditional(10, true, true);
 
5568
        v = bv3[10];
 
5569
        if (v || changed) {
 
5570
            cout << "Conditional bit set failed." << endl;
 
5571
            exit(1);
 
5572
        }
 
5573
        changed = bv3.set_bit_conditional(10, true, false);
 
5574
        v = bv3[10];
 
5575
        if (!v || !changed) {
 
5576
            cout << "Conditional bit set failed." << endl;
 
5577
            exit(1);
 
5578
        }
 
5579
        changed = bv3.set_bit_conditional(10, false, false);
 
5580
        v = bv3[10];
 
5581
        if (!v || changed) {
 
5582
            cout << "Conditional bit set failed." << endl;
 
5583
            exit(1);
 
5584
        }
 
5585
        changed = bv3.set_bit_conditional(10, false, true);
 
5586
        v = bv3[10];
 
5587
        if (v || !changed) {
 
5588
            cout << "Conditional bit set failed." << endl;
 
5589
            exit(1);
 
5590
        }
 
5591
    }
 
5592
    {
 
5593
        bvect bv3(bm::BM_GAP);
 
5594
        bv3.invert();
 
5595
        bv3.optimize();
 
5596
        bool changed;
 
5597
        changed = bv3.set_bit_conditional(10, true, true);
 
5598
        v = bv3[10];
 
5599
        if (!v || changed) {
 
5600
            cout << "Conditional bit set failed." << endl;
 
5601
            exit(1);
 
5602
        }
 
5603
        changed = bv3.set_bit_conditional(10, true, false);
 
5604
        v = bv3[10];
 
5605
        if (!v || changed) {
 
5606
            cout << "Conditional bit set failed." << endl;
 
5607
            exit(1);
 
5608
        }
 
5609
        changed = bv3.set_bit_conditional(10, false, false);
 
5610
        v = bv3[10];
 
5611
        if (!v || changed) {
 
5612
            cout << "Conditional bit set failed." << endl;
 
5613
            exit(1);
 
5614
        }
 
5615
        changed = bv3.set_bit_conditional(10, false, true);
 
5616
        v = bv3[10];
 
5617
        if (v || !changed) {
 
5618
            cout << "Conditional bit set failed." << endl;
 
5619
            exit(1);
 
5620
        }
 
5621
        changed = bv3.set_bit_conditional(10, true, false);
 
5622
        v = bv3[10];
 
5623
        if (!v || !changed) {
 
5624
            cout << "Conditional bit set failed." << endl;
 
5625
            exit(1);
 
5626
        }
 
5627
    }
 
5628
 
 
5629
}
 
5630
 
 
5631
 
 
5632
template<class A, class B> void CompareMiniSet(const A& ms,
 
5633
                                          const B& bvm)
 
5634
{
 
5635
    for (unsigned i = 0; i < bm::set_total_blocks; ++i)
 
5636
    {
 
5637
        bool ms_val = ms.test(i)!=0;
 
5638
        bool bvm_val = bvm.is_bit_true(i)!=0;
 
5639
        if (ms_val != bvm_val)
 
5640
        {
 
5641
            printf("MiniSet comparison error: %u\n",i);
 
5642
            exit(1);
 
5643
        }
 
5644
    }
 
5645
}
 
5646
 
 
5647
void MiniSetTest()
 
5648
{
 
5649
    cout << "----------------------- MiniSetTest" << endl;
 
5650
    {
 
5651
    bm::miniset<bm::block_allocator, bm::set_total_blocks> ms;
 
5652
    bvect_mini bvm(bm::set_total_blocks);
 
5653
 
 
5654
 
 
5655
    CompareMiniSet(ms, bvm);
 
5656
 
 
5657
 
 
5658
    ms.set(1);
 
5659
    bvm.set_bit(1);
 
5660
 
 
5661
    CompareMiniSet(ms, bvm);
 
5662
 
 
5663
    unsigned i;
 
5664
 
 
5665
    for (i = 1; i < 10; i++)
 
5666
    {
 
5667
        ms.set(i);
 
5668
        bvm.set_bit(i);
 
5669
    }
 
5670
    CompareMiniSet(ms, bvm);
 
5671
 
 
5672
    for (i = 1; i < 10; i++)
 
5673
    {
 
5674
        ms.set(i, false);
 
5675
        bvm.clear_bit(i);
 
5676
    }
 
5677
    CompareMiniSet(ms, bvm);
 
5678
 
 
5679
 
 
5680
    for (i = 1; i < 10; i+=3)
 
5681
    {
 
5682
        ms.set(i);
 
5683
        bvm.set_bit(i);
 
5684
    }
 
5685
    CompareMiniSet(ms, bvm);
 
5686
 
 
5687
    for (i = 1; i < 5; i+=3)
 
5688
    {
 
5689
        ms.set(i, false);
 
5690
        bvm.clear_bit(i);
 
5691
    }
 
5692
    CompareMiniSet(ms, bvm);
 
5693
    }
 
5694
 
 
5695
 
 
5696
    {
 
5697
    bm::miniset<bm::block_allocator, bm::set_total_blocks> ms;
 
5698
    bvect_mini bvm(bm::set_total_blocks);
 
5699
 
 
5700
 
 
5701
    ms.set(1);
 
5702
    bvm.set_bit(1);
 
5703
 
 
5704
    CompareMiniSet(ms, bvm);
 
5705
 
 
5706
    unsigned i;
 
5707
    for (i = 1; i < bm::set_total_blocks; i+=3)
 
5708
    {
 
5709
        ms.set(i);
 
5710
        bvm.set_bit(i);
 
5711
    }
 
5712
    CompareMiniSet(ms, bvm);
 
5713
 
 
5714
    for (i = 1; i < bm::set_total_blocks/2; i+=3)
 
5715
    {
 
5716
        ms.set(i, false);
 
5717
        bvm.clear_bit(i);
 
5718
    }
 
5719
    CompareMiniSet(ms, bvm);
 
5720
    }
 
5721
 
 
5722
 
 
5723
    {
 
5724
    bm::bvmini<bm::set_total_blocks> ms(0);
 
5725
    bvect_mini bvm(bm::set_total_blocks);
 
5726
 
 
5727
 
 
5728
    CompareMiniSet(ms, bvm);
 
5729
 
 
5730
 
 
5731
    ms.set(1);
 
5732
    bvm.set_bit(1);
 
5733
 
 
5734
    CompareMiniSet(ms, bvm);
 
5735
 
 
5736
    unsigned i;
 
5737
 
 
5738
    for (i = 1; i < 10; i++)
 
5739
    {
 
5740
        ms.set(i);
 
5741
        bvm.set_bit(i);
 
5742
    }
 
5743
    CompareMiniSet(ms, bvm);
 
5744
 
 
5745
    for (i = 1; i < 10; i++)
 
5746
    {
 
5747
        ms.set(i, false);
 
5748
        bvm.clear_bit(i);
 
5749
    }
 
5750
    CompareMiniSet(ms, bvm);
 
5751
 
 
5752
 
 
5753
    for (i = 1; i < bm::set_total_blocks; i+=3)
 
5754
    {
 
5755
        ms.set(i);
 
5756
        bvm.set_bit(i);
 
5757
    }
 
5758
    CompareMiniSet(ms, bvm);
 
5759
 
 
5760
    for (i = 1; i < bm::set_total_blocks/2; i+=3)
 
5761
    {
 
5762
        ms.set(i, false);
 
5763
        bvm.clear_bit(i);
 
5764
    }
 
5765
    CompareMiniSet(ms, bvm);
 
5766
    }
 
5767
 
 
5768
 
 
5769
    {
 
5770
    bm::miniset<bm::block_allocator, bm::set_total_blocks> ms;
 
5771
    bvect_mini bvm(bm::set_total_blocks);
 
5772
 
 
5773
 
 
5774
    ms.set(1);
 
5775
    bvm.set_bit(1);
 
5776
 
 
5777
    CompareMiniSet(ms, bvm);
 
5778
 
 
5779
    unsigned i;
 
5780
    for (i = 1; i < 15; i+=3)
 
5781
    {
 
5782
        ms.set(i);
 
5783
        bvm.set_bit(i);
 
5784
    }
 
5785
    CompareMiniSet(ms, bvm);
 
5786
 
 
5787
    for (i = 1; i < 7; i+=3)
 
5788
    {
 
5789
        ms.set(i, false);
 
5790
        bvm.clear_bit(i);
 
5791
    }
 
5792
    CompareMiniSet(ms, bvm);
 
5793
    }
 
5794
 
 
5795
 
 
5796
    cout << "----------------------- MiniSetTest ok" << endl;
 
5797
}
 
5798
 
 
5799
 
 
5800
unsigned CalcBitCount32(unsigned b)
 
5801
{
 
5802
    b = (b & 0x55555555) + (b >> 1 & 0x55555555);
 
5803
    b = (b & 0x33333333) + (b >> 2 & 0x33333333);
 
5804
    b = b + (b >> 4) & 0x0F0F0F0F;
 
5805
    b = b + (b >> 8);
 
5806
    b = b + (b >> 16) & 0x0000003F;
 
5807
    return b;
 
5808
}
 
5809
 
 
5810
 
 
5811
void PrintGapLevels(const gap_word_t* glevel)
 
5812
{
 
5813
    cout << "Gap levels:" << endl;
 
5814
    unsigned i;
 
5815
    for (i = 0; i < bm::gap_levels; ++i)
 
5816
    {
 
5817
        cout << glevel[i] << ",";
 
5818
    }
 
5819
    cout << endl;
 
5820
}
 
5821
 
 
5822
void OptimGAPTest()
 
5823
{
 
5824
    gap_word_t    glevel[bm::gap_levels];
 
5825
    ::memcpy(glevel, gap_len_table<true>::_len, bm::gap_levels * sizeof(gap_word_t));
 
5826
 
 
5827
    {
 
5828
    gap_word_t  length[] = { 2, 2, 5, 5, 10, 11, 12 };
 
5829
    unsigned lsize = sizeof(length) / sizeof(gap_word_t);
 
5830
 
 
5831
    bm::improve_gap_levels(length, length + lsize, glevel);
 
5832
 
 
5833
    PrintGapLevels(glevel);
 
5834
    }
 
5835
 
 
5836
    {
 
5837
    gap_word_t  length[] = { 3, 5, 15, 15, 100, 110, 120 };
 
5838
    unsigned lsize = sizeof(length) / sizeof(gap_word_t);
 
5839
 
 
5840
    bm::improve_gap_levels(length, length + lsize, glevel);
 
5841
    PrintGapLevels(glevel);
 
5842
    }
 
5843
 
 
5844
    {
 
5845
    gap_word_t  length[] = { 15, 80, 5, 3, 100, 110, 95 };
 
5846
    unsigned lsize = sizeof(length) / sizeof(gap_word_t);
 
5847
 
 
5848
    bm::improve_gap_levels(length, length + lsize, glevel);
 
5849
    PrintGapLevels(glevel);
 
5850
    }
 
5851
 
 
5852
    {
 
5853
    gap_word_t  length[] = 
 
5854
    { 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,
 
5855
      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,
 
5856
      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,
 
5857
      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,
 
5858
      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,
 
5859
      8,12,10,10,14,14,14,8,4,4,10,10,4,10,4,8,6,52,104,584,218
 
5860
    };
 
5861
    unsigned lsize = sizeof(length) / sizeof(gap_word_t);
 
5862
 
 
5863
    bm::improve_gap_levels(length, length + lsize, glevel);
 
5864
    PrintGapLevels(glevel);
 
5865
    }
 
5866
 
 
5867
    {
 
5868
    gap_word_t  length[] = {
 
5869
     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
 
5870
    ,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
 
5871
    ,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
 
5872
    ,8,10,6,4,6 
 
5873
    };
 
5874
    unsigned lsize = sizeof(length) / sizeof(gap_word_t);
 
5875
 
 
5876
    bm::improve_gap_levels(length, length + lsize, glevel);
 
5877
    PrintGapLevels(glevel);
 
5878
 
 
5879
    }
 
5880
 
 
5881
}
 
5882
 
 
5883
void BitCountChangeTest()
 
5884
{
 
5885
    cout << "---------------------------- BitCountChangeTest " << endl;
 
5886
 
 
5887
    unsigned i;
 
5888
    for(i = 0xFFFFFFFF; i; i <<= 1) 
 
5889
    { 
 
5890
        unsigned a0 = bm::bit_count_change(i);
 
5891
        unsigned a1 = BitCountChange(i);
 
5892
        
 
5893
        if (a0 != a1)
 
5894
        {
 
5895
            cout << hex 
 
5896
                 << "Bit count change test failed!" 
 
5897
                 << " i = " << i << " return = " 
 
5898
                 << a0 << " check = " << a1
 
5899
                 << endl;
 
5900
            exit(1);
 
5901
        }
 
5902
    }
 
5903
 
 
5904
    cout << "---------------------------- STEP 2 " << endl;
 
5905
 
 
5906
    for(i = 0xFFFFFFFF; i; i >>= 1) 
 
5907
    { 
 
5908
        unsigned a0 = bm::bit_count_change(i);
 
5909
        unsigned a1 = BitCountChange(i);
 
5910
        
 
5911
        if (a0 != a1)
 
5912
        {
 
5913
            cout << "Bit count change test failed!" 
 
5914
                 << " i = " << i << " return = " 
 
5915
                 << a0 << " check = " << a1
 
5916
                 << endl;
 
5917
            exit(1);
 
5918
        }
 
5919
    }
 
5920
 
 
5921
    cout << "---------------------------- STEP 3 " << endl;
 
5922
 
 
5923
    for (i = 0; i < 0xFFFFFFF; ++i)
 
5924
    {
 
5925
        unsigned a0 = bm::bit_count_change(i);
 
5926
        unsigned a1 = BitCountChange(i);
 
5927
        
 
5928
        if (a0 != a1)
 
5929
        {
 
5930
            cout << "Bit count change test failed!" 
 
5931
                 << " i = " << i << " return = " 
 
5932
                 << a0 << " check = " << a1
 
5933
                 << endl;
 
5934
            exit(1);
 
5935
        }
 
5936
    }
 
5937
   
 
5938
 
 
5939
    bm::word_t   arr[16] = {0,};
 
5940
    arr[0] = (bm::word_t)(1 << 31);
 
5941
    arr[1] = 1; //(bm::word_t)(1 << 31);
 
5942
    
 
5943
    bm::id_t cnt;
 
5944
    
 
5945
    cnt = bm::bit_count_change(arr[1]);
 
5946
    cout << cnt << endl;
 
5947
    if (cnt != 2)
 
5948
    {
 
5949
        cout << "0.count_change() failed " << cnt << endl;
 
5950
        exit(1);
 
5951
    }
 
5952
    
 
5953
    cnt = bm::bit_block_calc_count_change(arr, arr+4);
 
5954
    
 
5955
    if (cnt != 3)
 
5956
    {
 
5957
        cout << "1.count_intervals() failed " << cnt << endl;
 
5958
        exit(1);
 
5959
    }
 
5960
 
 
5961
    arr[0] = arr[1] = arr[2] = 0xFFFFFFFF;
 
5962
    arr[3] = (bm::word_t)(0xFFFFFFFF >> 1);
 
5963
    
 
5964
    cnt = bm::bit_block_calc_count_change(arr, arr+4);
 
5965
    cout << cnt << endl;
 
5966
    
 
5967
    if (cnt != 2)
 
5968
    {
 
5969
        cout << "1.1 count_intervals() failed " << cnt << endl;
 
5970
        exit(1);
 
5971
    }
 
5972
    
 
5973
 
 
5974
    cout << "---------------------------- STEP 4 " << endl;
 
5975
 
 
5976
    bvect   bv1;
 
5977
    cnt = bm::count_intervals(bv1);
 
5978
    
 
5979
    if (cnt != 1)
 
5980
    {
 
5981
        cout << "1.count_intervals() failed " << cnt << endl;
 
5982
        exit(1);
 
5983
    }
 
5984
    CheckIntervals(bv1, 65536);
 
5985
    
 
5986
    bv1.invert();
 
5987
 
 
5988
    cnt = count_intervals(bv1);
 
5989
    cout << "Inverted cnt=" << cnt << endl;
 
5990
    
 
5991
    if (cnt != 2)
 
5992
    {
 
5993
        cout << "2.inverted count_intervals() failed " << cnt << endl;
 
5994
        exit(1);
 
5995
    }
 
5996
    
 
5997
    bv1.invert();
 
5998
        
 
5999
    for (i = 10; i < 100000; ++i)
 
6000
    {
 
6001
        bv1.set(i);
 
6002
    }
 
6003
    
 
6004
    cnt = count_intervals(bv1);
 
6005
    
 
6006
    if (cnt != 3)
 
6007
    {
 
6008
        cout << "3.count_intervals() failed " << cnt << endl;
 
6009
        exit(1);
 
6010
    }
 
6011
    cout << "-----" << endl;
 
6012
    CheckIntervals(bv1, 65536*2);
 
6013
    cout << "Optmization..." << endl; 
 
6014
    bv1.optimize();
 
6015
    cnt = count_intervals(bv1);
 
6016
    
 
6017
    if (cnt != 3)
 
6018
    {
 
6019
        cout << "4.count_intervals() failed " << cnt << endl;
 
6020
        exit(1);
 
6021
    }
 
6022
    
 
6023
    CheckIntervals(bv1, 65536*2);
 
6024
 
 
6025
    cout << "---------------------------- BitCountChangeTest Ok." << endl;
 
6026
}
 
6027
 
 
6028
/*
 
6029
#define POWER_CHECK(w, mask) \
 
6030
    (bm::bit_count_table<true>::_count[(w&mask) ^ ((w&mask)-1)])
 
6031
 
 
6032
void BitListTest()
 
6033
{
 
6034
    unsigned bits[64] = {0,};
 
6035
 
 
6036
    unsigned w = 0;
 
6037
 
 
6038
    w = (1 << 3) | 1;
 
6039
 
 
6040
 
 
6041
    int bn3 = POWER_CHECK(w, 1 << 3) - 1;
 
6042
    int bn2 = POWER_CHECK(w, 1 << 2) - 1;
 
6043
    int bn0 = POWER_CHECK(w, 1 << 0) - 1;
 
6044
 
 
6045
    bit_list(w, bits+1);
 
6046
  
 
6047
}
 
6048
*/
 
6049
 
 
6050
void ResizeTest()
 
6051
{
 
6052
    {{
 
6053
    bvect bv(0);
 
6054
    assert(bv.any() == false);
 
6055
    assert(bv.count() == 0);
 
6056
    }}
 
6057
 
 
6058
    {{
 
6059
    bvect bv1(10);
 
6060
    bvect bv2(bv1);
 
6061
    assert(bv1.size() == bv2.size());
 
6062
    }}
 
6063
 
 
6064
    {{
 
6065
    bvect bv(10);
 
6066
    assert(bv.any() == false);
 
6067
    assert(bv.count() == 0);
 
6068
    bv.invert();
 
6069
    unsigned cnt = bv.count();
 
6070
    assert(cnt == 10);
 
6071
    }}
 
6072
 
 
6073
    {{
 
6074
    bvect bv1(10);
 
6075
    bv1.set(1);
 
6076
    bvect bv2(0);
 
6077
 
 
6078
    assert(bv1.size() == 10);
 
6079
    assert(bv2.size() == 0);
 
6080
    assert(bv1.count() == 1);
 
6081
    assert(bv2.count() == 0);
 
6082
    
 
6083
    bv1.swap(bv2);
 
6084
 
 
6085
    assert(bv2.size() == 10);
 
6086
    assert(bv2.count() == 1);
 
6087
    assert(bv1.size() == 0);
 
6088
    assert(bv1.count() == 0);
 
6089
 
 
6090
    }}
 
6091
 
 
6092
    {{
 
6093
    bvect bv1;
 
6094
    bv1.set(65536);
 
6095
    bv1.set(100);
 
6096
    assert(bv1.size() == bm::id_max);
 
6097
    assert(bv1.count() == 2);
 
6098
    bv1.resize(101);
 
6099
    assert(bv1.size() == 101);
 
6100
    assert(bv1.count() == 1);
 
6101
    {{
 
6102
    bm::id_t f = bv1.get_first();
 
6103
    assert(f == 100);
 
6104
    f = bv1.get_next(f);
 
6105
    assert(f == 0);
 
6106
    }}
 
6107
 
 
6108
    bv1.resize(10);
 
6109
    assert(bv1.size() == 10);
 
6110
    assert(bv1.count() == 0);
 
6111
    bm::id_t f = bv1.get_first();
 
6112
    assert(f == 0);
 
6113
    }}
 
6114
 
 
6115
    {{
 
6116
    bvect bv;
 
6117
    bv.stat();
 
6118
    bv.set(100);
 
6119
    bv.set(65536 + 10);
 
6120
    bv.stat();
 
6121
    bv.set_range(0, 65536*10, false);
 
6122
    bv.stat();
 
6123
    }}
 
6124
 
 
6125
    // test logical operations
 
6126
 
 
6127
    {{
 
6128
    bvect bv1(65536 * 10);
 
6129
    bvect bv2(65536 * 100);
 
6130
    bv1.set(5);
 
6131
    bv2.set(5);
 
6132
    bv2.set(65536 * 2);
 
6133
    bv2 &= bv1;
 
6134
    assert(bv2.size() == 65536 * 100);
 
6135
    assert(bv2.count() == 1);
 
6136
    assert(bv2.get_first() == 5);
 
6137
    }}
 
6138
 
 
6139
    {{
 
6140
    bvect bv1(10);
 
6141
    bvect bv2;
 
6142
    bv1.set(5);
 
6143
    bv2.set(5);
 
6144
    bv2.set(65536 * 2);
 
6145
    bv1 &= bv2;
 
6146
    assert(bv1.size() == bv2.size());
 
6147
    assert(bv1.count() == 1);
 
6148
    assert(bv1.get_first() == 5);
 
6149
    }}
 
6150
 
 
6151
    {{
 
6152
    bvect bv1(10);
 
6153
    bvect bv2;
 
6154
    bv1.set(5);
 
6155
    bv2.set(6);
 
6156
    bv2.set(65536 * 2);
 
6157
    bv1 |= bv2;
 
6158
    assert(bv1.size() == bv2.size());
 
6159
    assert(bv1.count() == 3);
 
6160
    }}
 
6161
 
 
6162
    // comparison test
 
6163
 
 
6164
    {{
 
6165
    int cmp;
 
6166
    bvect bv1(10);
 
6167
    bvect bv2;
 
6168
    bv2.set(65536 * 2);
 
6169
 
 
6170
    cmp = bv1.compare(bv2);
 
6171
    assert(cmp < 0);
 
6172
 
 
6173
    bv1.set(5);
 
6174
    assert(cmp < 0);
 
6175
    cmp = bv1.compare(bv2);
 
6176
    assert(cmp > 0);
 
6177
    cmp = bv2.compare(bv1);
 
6178
    assert(cmp < 0);
 
6179
 
 
6180
    }}
 
6181
 
 
6182
    // inserter
 
6183
 
 
6184
    {{
 
6185
    bvect bv1(10);
 
6186
    {
 
6187
        bvect::insert_iterator it(bv1);
 
6188
        *it = 100 * 65536;
 
6189
    }
 
6190
    assert(bv1.size() ==  100 * 65536 + 1);
 
6191
    }}
 
6192
 
 
6193
    // serialization
 
6194
 
 
6195
    {{
 
6196
    bvect bv1(10);
 
6197
    bv1.set(5);
 
6198
    struct bvect::statistics st1;
 
6199
    bv1.calc_stat(&st1);
 
6200
 
 
6201
    unsigned char* sermem = new unsigned char[st1.max_serialize_mem];
 
6202
    unsigned slen2 = bm::serialize(bv1, sermem);
 
6203
 
 
6204
    bvect bv2(0);
 
6205
    bm::deserialize(bv2, sermem);
 
6206
    delete [] sermem;
 
6207
 
 
6208
    assert(bv2.size() == 10);
 
6209
    assert(bv2.count() == 1);
 
6210
    assert(bv2.get_first() == 5);
 
6211
    
 
6212
    }}
 
6213
 
 
6214
    {{
 
6215
    bvect bv1(10);
 
6216
    bv1.set(5);
 
6217
    unsigned int arg[] = { 10, 65536, 65537, 65538 * 10000 };
 
6218
    unsigned* it1 = arg;
 
6219
    unsigned* it2 = arg + 4;
 
6220
    combine_or(bv1, it1, it2);
 
6221
    assert(bv1.size() == 65538 * 10000 + 1);
 
6222
    bvect::enumerator en = bv1.first();
 
6223
    while (en.valid())
 
6224
    {
 
6225
        cout << *en << " ";
 
6226
        ++en;
 
6227
    }
 
6228
    }}
 
6229
 
 
6230
}
 
6231
 
 
6232
void ExportTest()
 
6233
{
 
6234
    cout << "---------------------------- ExportTest..." << endl;
 
6235
 
 
6236
    {
 
6237
    char buf[20] = {0,};
 
6238
 
 
6239
    buf[0] = 1;
 
6240
    buf[1] = 1;
 
6241
    buf[2]= (char)(1 << 1);
 
6242
 
 
6243
    bvect bv1;
 
6244
    export_array(bv1, buf + 0, buf + 20);
 
6245
 
 
6246
    assert(bv1.count() == 3);
 
6247
    assert(bv1.test(0));
 
6248
    assert(bv1.test(8));
 
6249
    assert(bv1.test(17));
 
6250
    }
 
6251
 
 
6252
    {
 
6253
    char buf[65536*10] = {0,};
 
6254
 
 
6255
    buf[0] = 1;
 
6256
    buf[1] = 1;
 
6257
    buf[2]= (char)(1 << 1);
 
6258
 
 
6259
    bvect bv1;
 
6260
    export_array(bv1, buf + 0, buf + 65536*10);
 
6261
 
 
6262
    assert(bv1.count() == 3);
 
6263
    assert(bv1.test(0));
 
6264
    assert(bv1.test(8));
 
6265
    assert(bv1.test(17));
 
6266
    }
 
6267
 
 
6268
    {
 
6269
    short buf[20] = {0,};
 
6270
 
 
6271
    buf[0] = 1;
 
6272
    buf[1] = 1;
 
6273
    buf[2]= (char)(1 << 1);
 
6274
 
 
6275
    bvect bv1;
 
6276
    export_array(bv1, buf + 0, buf + 20);
 
6277
 
 
6278
    assert(bv1.count() == 3);
 
6279
    assert(bv1.test(0));
 
6280
    assert(bv1.test(16));
 
6281
    assert(bv1.test(33));
 
6282
    }
 
6283
 
 
6284
    {
 
6285
    int buf[20] = {0,};
 
6286
 
 
6287
    buf[0] = 1;
 
6288
    buf[1] = 1;
 
6289
    buf[2]= (char)(1 << 1);
 
6290
 
 
6291
    bvect bv1;
 
6292
    export_array(bv1, buf + 0, buf + 20);
 
6293
 
 
6294
    assert(bv1.count() == 3);
 
6295
    assert(bv1.test(0));
 
6296
    assert(bv1.test(32));
 
6297
    assert(bv1.test(65));
 
6298
    }
 
6299
 
 
6300
 
 
6301
    cout << "---------------------------- ExportTest Ok." << endl;
 
6302
}
 
6303
 
 
6304
 
 
6305
void TestRecomb()
 
6306
{
 
6307
    bm::word_t b1[bm::set_block_size]= {0,};
 
6308
    bm::word_t b2[bm::set_block_size]= {0,};
 
6309
    bm::word_t br[bm::set_block_size]= {0,};
 
6310
 
 
6311
    b1[0] = 1;
 
6312
    b1[1] = 1;
 
6313
    b2[0] = 1;
 
6314
 
 
6315
    bitblock_get_adapter bga1(b1);
 
6316
    bitblock_get_adapter bga2(b2);
 
6317
    bitblock_store_adapter bbsa(br);
 
6318
    bm::bit_AND<bm::word_t> and_func;
 
6319
    bit_recomb<bitblock_get_adapter,
 
6320
               bitblock_get_adapter,
 
6321
               bm::bit_AND<bm::word_t>,
 
6322
               bitblock_store_adapter>
 
6323
           (bga1, bga2,and_func, bbsa);
 
6324
/*
 
6325
    bit_recomb(bitblock_get_adapter(b1),
 
6326
               bitblock_get_adapter(b2),
 
6327
               bit_AND<bm::word_t>(),
 
6328
               bitblock_store_adapter(br)
 
6329
               );
 
6330
 
 
6331
    assert(br[0] == 1);
 
6332
    for (int i = 1; i < bm::set_block_size; ++i)
 
6333
    {
 
6334
        assert(br[i] == 0);
 
6335
    }
 
6336
 
 
6337
    bitblock_sum_adapter sa;
 
6338
    bit_recomb(bitblock_get_adapter(b1),
 
6339
               bitblock_get_adapter(b2),
 
6340
               bit_COUNT_AND<bm::word_t>(),
 
6341
               sa
 
6342
               );
 
6343
    assert(sa.sum() == 1);
 
6344
*/
 
6345
}
 
6346
 
 
6347
 
 
6348
int main(void)
 
6349
{
 
6350
    time_t      start_time = time(0);
 
6351
    time_t      finish_time;
 
6352
 
 
6353
    TestRecomb();
 
6354
 
 
6355
    OptimGAPTest();
 
6356
 
 
6357
    CalcBeginMask();
 
6358
    CalcEndMask();
 
6359
/*
 
6360
    unsigned a = bm::id_max / 65536;
 
6361
 
 
6362
    bvect bv(BM_BIT, bm::gap_len_table<true>::_len, 65536*10);
 
6363
    bm::id_t c = bv.capacity();
 
6364
    bv.set(bm::id_max-1);
 
6365
*/
 
6366
 
 
6367
//   cout << sizeof(__int64) << endl;
 
6368
 
 
6369
//   ::srand((unsigned)::time(NULL));
 
6370
 
 
6371
/*
 
6372
    bvect bv;
 
6373
 
 
6374
    bv.set(786694);
 
6375
    bv.set(895785); 
 
6376
    bv.set(977200); 
 
6377
    bv.set(1038827); 
 
6378
    bv.set(1110924); 
 
6379
    bv.set(1119532); 
 
6380
    bv.set(1123158); 
 
6381
    bv.set(1203562); 
 
6382
    bv.set(1209502); 
 
6383
    bv.set(1247904); 
 
6384
    bv.set(1276296); 
 
6385
    bv.set(1384903);
 
6386
 
 
6387
    cout << bv.count() << endl;
 
6388
 
 
6389
    bvect::statistics st;
 
6390
    bv.calc_stat(&st);
 
6391
    unsigned char buf[2048];
 
6392
    unsigned slen = bm::serialize(bv, buf);
 
6393
    cout << "slen=" << slen << endl;
 
6394
 
 
6395
    bvect bv2;
 
6396
    operation_deserializer<bvect>::deserialize(bv2,
 
6397
                                                buf,
 
6398
                                                0,
 
6399
                                                set_ASSIGN);
 
6400
 
 
6401
    cout << "count2=" << bv2.count() << endl;
 
6402
    PrintSet(cout, bv2);
 
6403
    return 0;
 
6404
*/
 
6405
     ExportTest();
 
6406
     ResizeTest();
 
6407
 
 
6408
     MiniSetTest();
 
6409
 
 
6410
     SyntaxTest();
 
6411
 
 
6412
     SetTest();
 
6413
 
 
6414
     BitCountChangeTest();
 
6415
 
 
6416
     EnumeratorTest();
 
6417
 
 
6418
     BasicFunctionalityTest();
 
6419
 
 
6420
     ClearAllTest();
 
6421
 
 
6422
     GAPCheck();
 
6423
 
 
6424
     MaxSTest();
 
6425
 
 
6426
     GetNextTest();
 
6427
 
 
6428
     SimpleRandomFillTest();
 
6429
     
 
6430
     RangeRandomFillTest();
 
6431
 
 
6432
     AndOperationsTest();   
 
6433
           
 
6434
     OrOperationsTest();
 
6435
 
 
6436
     XorOperationsTest();
 
6437
 
 
6438
     SubOperationsTest();
 
6439
 
 
6440
     WordCmpTest();
 
6441
 
 
6442
     ComparisonTest();
 
6443
 
 
6444
     MutationTest();
 
6445
 
 
6446
     MutationOperationsTest();
 
6447
 
 
6448
     SerializationTest();
 
6449
 
 
6450
     DesrializationTest2();
 
6451
 
 
6452
     BlockLevelTest();
 
6453
 
 
6454
     StressTest(800);
 
6455
 
 
6456
    finish_time = time(0);
 
6457
 
 
6458
 
 
6459
    cout << "Test execution time = " << finish_time - start_time << endl;
 
6460
 
 
6461
#ifdef MEM_DEBUG
 
6462
    cout << "Number of BLOCK allocations = " <<  dbg_block_allocator::na_ << endl;
 
6463
    cout << "Number of PTR allocations = " <<  dbg_ptr_allocator::na_ << endl << endl;
 
6464
 
 
6465
    assert(dbg_block_allocator::balance() == 0);
 
6466
    assert(dbg_ptr_allocator::balance() == 0);
 
6467
#endif
 
6468
 
 
6469
    return 0;
 
6470
}
 
6471