File: bvsetalgebra.cpp

package info (click to toggle)
bmagic 6.3.0-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 49,956 kB
  • sloc: cpp: 84,298; ansic: 9,703; sh: 1,664; makefile: 742
file content (607 lines) | stat: -rw-r--r-- 17,041 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

For more information please visit:  http://bitmagic.io
*/

/** \example bvsetalgebra.cpp
     Example demonstrates variety of algebra of sets operations.
     http://bitmagic.io/set-algebra.html
 
     <a href="http://bitmagic.io/set-algebra.html">Algebra Of Sets</a>

  \sa bvector

  \sa bvector<>::bit_or
  \sa bvector<>::bit_and
  \sa bvector<>::bit_xor
  \sa bvector<>::bit_sub

  \sa bm::aggregator
  \sa bm::operation_deserializer

  \sa bm::combine_and
  \sa bm::combine_and_sorted
  \sa bm::combine_sub
  \sa bm::combine_or
  \sa bm::combine_xor

  \sa sample7.cpp

*/

/*! \file bvsetalgebra.cpp
    \brief Example: algebra of sets operations
*/


#include <iostream>
#include <vector>


#include "bm.h"
#include "bmalgo.h"
#include "bmserial.h"
#include "bmaggregator.h"


using namespace std;

// utility function to print a set
static
void print_bvector(const bm::bvector<>& bv)
{
    bm::bvector<>::size_type cnt = 0;
    bm::bvector<>::enumerator en = bv.first();
    for (; en.valid() && cnt < 10; ++en, ++cnt)
        cout << *en << ", ";
    if (cnt == 10)
        cout << " ...";
    cout << "(size = "<< bv.size() << ")" << endl;
}

// utility function to create serialized bit-vector BLOB
static
void make_BLOB(vector<unsigned char>& target_buf, bm::bvector<>& bv)
{
    BM_DECLARE_TEMP_BLOCK(tb)
    bm::serializer<bm::bvector<> > bvs(tb);
    bvs.set_compression_level(4);
    
    bv.optimize(tb, bm::bvector<>::opt_compress); // memory compression

    bm::serializer<bm::bvector<> >::buffer sbuf;
    bvs.serialize(bv, sbuf, 0);
    target_buf.resize(sbuf.size());
    ::memcpy(target_buf.data(), sbuf.buf(), sbuf.size());
}


// -------------------------------------------------------------
// Demo for Set Union (OR) operations
//
static
void DemoOR()
{
    typedef bm::bvector<>::size_type size_type;
    bm::operation_deserializer<bm::bvector<> > od;

    // bit-vector set union operation: bv_A |= bv_B
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.bit_or(bv_B);
        
        print_bvector(bv_A); // 1, 2, 3, 4
    }
    // same, but sizes are set, observe size gets extended up
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.resize(5);
        bv_B.resize(10);

        bv_A.bit_or(bv_B);
        
        print_bvector(bv_A); // 1, 2, 3, 4 (size = 10)
    }
    // 3-operand OR: bv_T = bv_A | bv_B
    {
        bm::bvector<>   bv_T;
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };

        bv_T.bit_or(bv_A, bv_B, bm::bvector<>::opt_compress);
        
        print_bvector(bv_T); // 1, 2, 3, 4 (size = 10)
    }
    
    
    // merge operation is a logical equivalent of OR
    // except it can destroy the source vector to borrow memory blocks from it
    // (this is faster, especially in multi-threaded cases)
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.merge(bv_B);
        
        print_bvector(bv_A); // 1, 2, 3, 4 (size = 10)
    }

    // bit-vector set union operation (opcode interpeter mode)
    // maybe useful for building query interpetors
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.combine_operation(bv_B, bm::BM_OR);
        
        print_bvector(bv_A); // 1, 2, 3, 4
    }

    // Set union between bit-vector and STL container
    {
        bm::bvector<>      bv_A { 1, 2, 3 };
        vector<size_type>   vect_B { 1, 2, 4 };
        
        bm::combine_or(bv_A, vect_B.begin(), vect_B.end());
        print_bvector(bv_A); // 1, 2, 3, 4
    }
    
    // Set union between bit-vector and C-array.
    // This tends to be faster then "combine_or()" especially on sorted vectors
    // and in SIMD enabled configurations
    {
        bm::bvector<>      bv_A { 1, 2, 3 };
        vector<size_type>   vect_B { 1, 2, 4 };
        
        const size_type* arr = &vect_B[0];
        bv_A.set(arr, unsigned(vect_B.size()), bm::BM_SORTED); // sorted - fastest
        print_bvector(bv_A); // 1, 2, 3, 4
    }

    // Set union between bit-vector and a serialized bit-vector BLOB
    // (created on the fly)
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        vector<unsigned char> blob;
        {
            bm::bvector<>   bv_B { 1, 2, 4 };
            make_BLOB(blob, bv_B);
        }
        od.deserialize(bv_A, blob.data(), bm::set_OR);
        print_bvector(bv_A); // 1, 2, 3, 4
    }
    
    // Union of many sets with bm::aggegator<>
    // target := A OR B OR C
    //
    // This method is best when we have multiple vectors at hands, aggregator
    // is capable of doing it faster, than pair by pair OR
    {
        bm::bvector<>    bv_T; // target vector
        
        bm::bvector<>    bv_A { 1, 2 };
        bm::bvector<>    bv_B { 2, 3 };
        bm::bvector<>    bv_C { 3, 4 };
        
        bm::aggregator<bm::bvector<> > agg;
        agg.set_optimization(); // perform on-the-fly optimization of result
        
        // attach vectors to group 0 for OR operation
        agg.add(&bv_A);
        agg.add(&bv_B);
        agg.add(&bv_C);
        
        agg.combine_or(bv_T);
        
        agg.reset(); // reset the aggregator parameters
        
        print_bvector(bv_T); // 1, 2, 3, 4
    }
    
}


// -------------------------------------------------------------
// Demo for Set Intersect (AND) operations
//
static
void DemoAND()
{
    typedef bm::bvector<>::size_type size_type;
    bm::operation_deserializer<bm::bvector<> > od;

    // bit-vector set intersect operation: bv_A &= bv_B
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.bit_and(bv_B);
        
        print_bvector(bv_A); // 1, 2
    }
    // same, but sizes are set, observe size gets extended up
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.resize(5);
        bv_B.resize(10);

        bv_A.bit_and(bv_B);
        
        print_bvector(bv_A); // 1, 2 (size = 10)
    }
    // 3-operand AND: bv_T = bv_A & bv_B
    {
        bm::bvector<>   bv_T;
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_T.bit_and(bv_A, bv_B, bm::bvector<>::opt_compress);
        
        print_bvector(bv_T); // 1, 2
    }

    // bit-vector set union operation (opcode interpeter mode)
    // maybe useful for building query interpetors
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.combine_operation(bv_B, bm::BM_AND);
        
        print_bvector(bv_A); // 1, 2
    }

    // Set Intersect between bit-vector and STL container
    {
        bm::bvector<>      bv_A { 1, 2, 3 };
        vector<unsigned>   vect_B { 1, 2, 4 };
        
        bm::combine_and(bv_A, vect_B.begin(), vect_B.end());
        print_bvector(bv_A); // 1, 2
    }
    
    // Set Intersect between bit-vector and C-array.
    // This may be faster then "combine_and()" especially on sorted vectors
    {
        bm::bvector<>      bv_A { 1, 2, 3 };
        vector<size_type>   vect_B { 1, 2, 4 };
        
        const size_type* arr = &vect_B[0];
        bv_A.keep(arr, size_type(vect_B.size()), bm::BM_SORTED); // sorted - fastest
        print_bvector(bv_A); // 1, 2
    }

    // Set Intersect between bit-vector and a serialized bit-vector BLOB
    //
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        vector<unsigned char> blob;
        {
            bm::bvector<>   bv_B { 1, 2, 4 };
            make_BLOB(blob, bv_B);
        }
        od.deserialize(bv_A, blob.data(), bm::set_AND);
        print_bvector(bv_A); // 1, 2
    }
    
    // Intersection of many sets with bm::aggegator<> (find common subset)
    // target := A AND B AND C
    //
    // This method is best when we have multiple vectors at hands, aggregator
    // is capable of doing it faster, than pair by pair AND
    {
        bm::bvector<>    bv_T; // target vector
        
        bm::bvector<>    bv_A { 1, 2 };
        bm::bvector<>    bv_B { 1, 2, 3 };
        bm::bvector<>    bv_C { 1, 2, 3, 4 };
        
        bm::aggregator<bm::bvector<> > agg;
        agg.set_optimization(); // perform on-the-fly optimization of result
        
        // attach vectors to group 0 for OR operation
        agg.add(&bv_A);
        agg.add(&bv_B);
        agg.add(&bv_C);
        
        agg.combine_and(bv_T);
        
        agg.reset(); // reset the aggregator parameters
        
        print_bvector(bv_T); // 1, 2
    }
}

// -------------------------------------------------------------
// Demo for XOR operations
//
static
void DemoXOR()
{
    bm::operation_deserializer<bm::bvector<> > od;

    // bit-vector xor operation: bv_A ^= bv_B
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.bit_xor(bv_B);
        
        print_bvector(bv_A); // 3, 4
    }
    // same, but sizes are set, observe size gets extended up
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.resize(5);
        bv_B.resize(10);

        bv_A.bit_xor(bv_B);
        
        print_bvector(bv_A); // 3, 4 (size = 10)
    }
    // 3-operand XOR: bv_T = bv_A ^ bv_B
    {
        bm::bvector<>   bv_T;
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_T.bit_xor(bv_A, bv_B, bm::bvector<>::opt_compress);
        
        print_bvector(bv_T); // 3, 4
    }

    // bit-vector xor operation (opcode interpeter mode)
    // maybe useful for building query interpetors
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.combine_operation(bv_B, bm::BM_XOR);
        
        print_bvector(bv_A); // 3, 4
    }

    // xor between bit-vector and STL container
    {
        bm::bvector<>      bv_A { 1, 2, 3 };
        vector<unsigned>   vect_B { 1, 2, 4 };
        
        bm::combine_xor(bv_A, vect_B.begin(), vect_B.end());
        print_bvector(bv_A); // 3, 4
    }

    // xor between bit-vector and a serialized bit-vector BLOB
    //
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        vector<unsigned char> blob;
        {
            bm::bvector<>   bv_B { 1, 2, 4 };
            make_BLOB(blob, bv_B);
        }
        od.deserialize(bv_A, blob.data(), bm::set_XOR);
        print_bvector(bv_A); // 3, 4
    }
}


// -------------------------------------------------------------
// Demo for Set Substract (AND NOT) operations
//
static
void DemoSUB()
{
    typedef bm::bvector<>::size_type size_type;
    bm::operation_deserializer<bm::bvector<> > od;

    // bit-vector set union operation: bv_A -= bv_B
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.bit_sub(bv_B);
        
        print_bvector(bv_A); // 3
    }
    // same, but sizes are set, observe size gets extended up
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.resize(5);
        bv_B.resize(10);

        bv_A.bit_sub(bv_B);
        
        print_bvector(bv_A); // 3 (size = 10)
    }
    
    // 3-operand SUB: bv_T = bv_A - bv_B
    {
        bm::bvector<>   bv_T;
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_T.bit_sub(bv_A, bv_B, bm::bvector<>::opt_compress);
        
        print_bvector(bv_T); // 3
    }

    // bit-vector minus operation (opcode interpeter mode)
    // maybe useful for building query interpetors
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        bm::bvector<>   bv_B { 1, 2, 4 };
        bv_A.combine_operation(bv_B, bm::BM_SUB);
        
        print_bvector(bv_A); // 3
    }

    // and not between bit-vector and STL container
    {
        bm::bvector<>      bv_A { 1, 2, 3 };
        vector<size_type>   vect_B { 1, 2, 4 };
        
        bm::combine_sub(bv_A, vect_B.begin(), vect_B.end());
        print_bvector(bv_A); // 3
    }

    // Set Intersect between bit-vector and C-array.
    // This may be faster then "combine_and()" especially on sorted vectors
    {
        bm::bvector<>      bv_A { 1, 2, 3 };
        vector<size_type>   vect_B { 1, 2, 4 };
        
        const size_type* arr = &vect_B[0];
        bv_A.clear(arr, unsigned(vect_B.size()), bm::BM_SORTED); // sorted - fastest
        print_bvector(bv_A); // 3
    }

    // Set union between bit-vector and a serialized bit-vector BLOB
    //
    {
        bm::bvector<>   bv_A { 1, 2, 3 };
        vector<unsigned char> blob;
        {
            bm::bvector<>   bv_B { 1, 2, 4 };
            make_BLOB(blob, bv_B);
        }
        od.deserialize(bv_A, blob.data(), bm::set_SUB);
        print_bvector(bv_A); // 3
    }

    // Subtraction of many sets with bm::aggegator<>
    // target := (target SUB A) OR (target SUB B) OR (target SUB C)
    //
    {
        bm::bvector<>    bv_T; // target vector
        
        bm::bvector<>    bv_A { 1, 2, 3, 4 };
        bm::bvector<>    bv_B { 1, 2 };
        bm::bvector<>    bv_C { 1, 2, 4 };
        
        bm::aggregator<bm::bvector<> > agg;
        agg.set_optimization(); // perform on-the-fly optimization of result
        
        // here we are really using AND SUB operation
        // where group 0 is all ANDed and group 1 SUBtracted from the result
        // group 1 is only 1 vector, so AND part will be no-op
        //
        agg.add(&bv_A, 0); // add to group 0 (subtraction source)
        
        agg.add(&bv_B, 1); // add to group 1 (subtraction arguments)
        agg.add(&bv_C, 1);
        
        agg.combine_and_sub(bv_T);
        
        agg.reset(); // reset the aggregator parameters
        
        print_bvector(bv_T); // 3
    }
}

// -------------------------------------------------------------
// Demo for Set Invert (NOT)
//
static
void DemoINV()
{
    // bit-vector invert operation
    // by default it inverts the whole 32-bit space
    {
        bm::bvector<>   bv_A { 4, 5, 6  };
        bv_A.invert();
        
        print_bvector(bv_A); // 0, 1, 2, 3, 7 ...
    }
    
    // bit-vector invert operation
    // it is size bound, inverts within set limits
    {
        bm::bvector<>   bv_A { 4, 5, 6  };
        bv_A.resize(7);
        bv_A.invert();
        
        print_bvector(bv_A); // 0, 1, 2, 3, size = 7
    }
}


// -------------------------------------------------------------
// Demo for AND-SUB
//  AND-SUB implements a search pattern "all this but not that"
//
static
void DemoAND_SUB()
{
    // Operation on two groups of vectors using aggregator
    // 1. Group 0 - find common subset (Set Intersect / AND)
    // 2. Group 1 - find union (OR) of the group and SUBtract it from #1
    //
    // target := (A AND D AND ...) AND NOT (B OR C OR ...)
    //
    {
        bm::bvector<>    bv_T; // target vector
        
        bm::bvector<>    bv_A { 1, 2, 3, 4 };
        bm::bvector<>    bv_B { 1, 2 };
        bm::bvector<>    bv_C { 1, 2, 4 };
        bm::bvector<>    bv_D { 0, 2, 3, 4, 5 };
        
        bm::aggregator<bm::bvector<> > agg;
        agg.set_optimization(); // perform on-the-fly optimization of result
        
        // here we are really using AND SUB operation
        // where group 0 is all ANDed and group 1 SUBtracted from the result
        //
        agg.add(&bv_A, 0); // add to group 0 for AND
        agg.add(&bv_D, 0); //

        agg.add(&bv_B, 1); // add to group 1 SUB tract from group 0 result
        agg.add(&bv_C, 1);
        
        agg.combine_and_sub(bv_T);
        
        agg.reset(); // reset the aggregator parameters
        
        print_bvector(bv_T); // 3
    }
}



int main(void)
{
    try
    {
        cout << endl << "Set Union (OR) demo" << endl << endl;
        DemoOR();
        
        cout << endl << "Set Intersect (AND) demo" << endl << endl;
        DemoAND();

        cout << endl << "XOR demo" << endl << endl;
        DemoXOR();

        cout << endl << "Set Minus (SUB/AND-NOT) demo" << endl << endl;
        DemoSUB();
        
        cout << endl << "Set Invert (NOT) demo" << endl << endl;
        DemoINV();
        
        cout << endl << "Set AND-SUB demo" << endl << endl;
        DemoAND_SUB();
    }
    catch(std::exception& ex)
    {
        std::cerr << ex.what() << std::endl;
    }

    return 0;
}