File: sample12.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 (330 lines) | stat: -rw-r--r-- 9,728 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
/*
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 sample12.cpp
  Example of how to use various bit setting techniques. 
  Several techniques benchmarked to better illustrate relative performance.

  \sa bm::bvector<>::set() 
  \sa bm::bvector<>::set_bit()
  \sa bm::bvector<>::set_bit_conditional()
  \sa bm::bvector<>::set_range()
  \sa bm::bvector<>::clear_bit()
  \sa bm::bvector<>::reset()
  \sa bm::bvector<>::flip()
  \sa bm::bvector<>::swap()
  \sa bm::bvector<>::extract_next()
  \sa bm::bvector<>::set_bit_no_check()
  \sa bm::combine_or()
 */

/*! \file sample12.cpp
    \brief Example: bvector<> analysis of bit setting methods
*/

#include <iostream>
#include <vector>
#define BM64ADDR
#include "bm.h"
#include "bmalgo.h"
#include "bmtimer.h"

using namespace std;

// timing storage for benchmarking
bm::chrono_taker::duration_map_type  timing_map;

// depending of the build it may be "unsigned int" or 64-bit "unsigned long long"
typedef bm::bvector<>::size_type bm_size_type;

const unsigned benchmark_count = 1000;
bm_size_type       vector_max = 4000000;


// Utility template function used to print container
template<class T> void PrintContainer(T first, T last)
{
    if (first == last)
        std::cout << "<EMPTY SET>";
    else
        for (; first != last; ++first)
            std::cout << *first << ";";
    std::cout << std::endl;
}


static
void generate_test_vectors(std::vector<bm_size_type> &v1,
                           std::vector<bm_size_type> &v2,
                           std::vector<bm_size_type> &v3)
{
    bm_size_type j;
    for (j = 0; j < vector_max; j += 2)
    {
        v1.push_back(j);
    }
    for (j = 0; j < vector_max; j += 5)
    {
        v2.push_back(j);
    }
    for (j = 0; j < vector_max; j += 120)
    {
        v3.push_back(j);
    }
}


// stress test for bm::bvector<>::set_bit() 
//
static
void bv_set_bit_test()
{
    bm::chrono_taker tt1("1. bvector<>::set_bit()", benchmark_count, &timing_map);
    bm::bvector<> bv1, bv2, bv3;

    for (unsigned i = 0; i < benchmark_count; ++i)
    {
        bm_size_type j;
        for (j = 0; j < vector_max; j += 2)
        {
            bv1.set_bit(j, true);
        }
        for (j = 0; j < vector_max; j += 10)
        {
            bv2.set_bit(j, true);
        }
        for (j = 0; j < vector_max; j += 120)
        {
            bv3.set_bit(j, true);
        }
        bv1.reset();
        bv2.reset();
        bv3.reset();
    } // for
}


// stress test for bm::bvector<>::set_bit() 
//
static
void bv_set_bit_no_check_test()
{
    bm::chrono_taker tt1("2. bvector<>::set_bit_no_check()", benchmark_count, &timing_map);
    bm::bvector<> bv1, bv2, bv3;

    bv1.init();
    bv2.init();
    bv3.init();

    for (unsigned i = 0; i < benchmark_count; ++i)
    {
        bm::id_t j;
        for (j = 0; j < vector_max; j += 2)
        {
            bv1.set_bit_no_check(j);
        }
        for (j = 0; j < vector_max; j += 10)
        {
            bv2.set_bit_no_check(j);
        }
        for (j = 0; j < vector_max; j += 120)
        {
            bv3.set_bit_no_check(j);
        }
    }
}

static
void combine_or_test(std::vector<bm_size_type> &v1,
                     std::vector<bm_size_type> &v2,
                     std::vector<bm_size_type> &v3)
{
    bm::chrono_taker tt1("3. combine_or()", benchmark_count, &timing_map);
    bm::bvector<> bv1, bv2, bv3;

    for (unsigned i = 0; i < benchmark_count; ++i)
    {
        bm::combine_or(bv1, v1.begin(), v1.end());
        bm::combine_or(bv2, v2.begin(), v2.end());
        bm::combine_or(bv3, v3.begin(), v3.end());
    }
}

static
void bvector_bulk_set_test(std::vector<bm_size_type> &v1,
                           std::vector<bm_size_type> &v2,
                           std::vector<bm_size_type> &v3)
{
    bm::chrono_taker tt1("3. bvector<>::set() array", benchmark_count, &timing_map);
    bm::bvector<> bv1, bv2, bv3;

    for (unsigned i = 0; i < benchmark_count; ++i)
    {
        bv1.set(&v1[0], bm_size_type(v1.size()));
        bv2.set(&v2[0], bm_size_type(v2.size()));
        bv3.set(&v3[0], bm_size_type(v3.size()));
    }
}



int main(void)
{
    try
    {
        // 0. create bvector, use brace initialization to add some initial data
        //
        bm::bvector<>   bv1 { 2, 3, 4 };
        
        PrintContainer(bv1.first(), bv1.end()); // 2, 3, 4
        bv1.clear();

        // 1. Set some bits using regular bvector<>::set() method
        //
        bv1.set(10);
        bv1.set(256);
        bv1.set(1000000);

        PrintContainer(bv1.first(), bv1.end()); // 10, 256, 1000000

        // 2. now use bvector<>::set_bit()
        // it returns a report if target actually changed
        //

        bm::id_t bits[] = { 256, 512, 10 };
        unsigned cnt = 0;

        for (unsigned i = 0; i < sizeof(bits) / sizeof(bits[0]); ++i)
        {
            bool b = bv1.set_bit(bits[i], true);
            cnt += b;
        }
        std::cout << "Number of bits changed:" << cnt << std::endl;
        PrintContainer(bv1.first(), bv1.end());

        // 3. set and clear some bits using bvector<>::set_bit_conditional()
        // method sets bit n only if current value equals the condition
        //
        bool b;
        b = bv1.set_bit_conditional(5, true, false); // set bit 5 to true if it is false (yes)
        std::cout << "Bit 5 set:" << (b ? " yes " : " no ") << std::endl; // (yes)
        
        b = bv1.set_bit_conditional(256, true, false); // set bit 256 to true if it is false
        std::cout << "Bit 256 set:" << (b ? " yes " : " no ") << std::endl; // (no)
        
        b = bv1.set_bit_conditional(256, true, true); // set bit 256 to true if it is true 
        std::cout << "Bit 256 set:" << (b ? " yes " : " no ") << std::endl; // (no)

        PrintContainer(bv1.first(), bv1.end());

        // 4. set or clear multiple bits using bvector::set_range()
        // This method is faster than calling bvector::set() many times in a row
        //
        bv1.set_range(10, 15, true); // set all bits in [10..15] closed interval 
        PrintContainer(bv1.first(), bv1.end());

        bv1.set_range(10, 12, false); // clear all bits in [10..15] closed interval 
        PrintContainer(bv1.first(), bv1.end());

        // 5. bvector::clear_bit() - same as set_bit() just a syntax sugar
        //
        b = bv1.clear_bit(13);
        std::cout << "Bit 13 set:" << (b ? " yes " : " no ") << std::endl; // (yes)

        // 6. bvector<>::reset() - clears all the bits, frees the blocks memory
        // same as bvector<>::clear(true);
        //
        bv1.reset();
        PrintContainer(bv1.first(), bv1.end());  // <EMPTY>

        // 7. use bm::combine_or() to set bits
        //
        bm::combine_or(bv1, &bits[0], &bits[0] + (sizeof(bits) / sizeof(bits[0])));
        PrintContainer(bv1.first(), bv1.end()); // 10, 256, 512

        // 8. use bvector<>::flip( ) to flip a bit
        //
        bv1.flip(256); 
        bv1.flip(257);
        PrintContainer(bv1.first(), bv1.end()); // 10, 257, 512

        // 9. bvector<>::swap() to flip content of two bit-vectors
        //
        bm::bvector<> bv2;
        bv1.swap(bv2);
        PrintContainer(bv1.first(), bv1.end());  // <EMPTY>
        PrintContainer(bv2.first(), bv2.end()); // 10, 257, 512

        // 10. use bvector<>::extract_next() to find ON bit and turn it to 0
        // this function is useful for building FIFO queues on bvector
        //
        bm_size_type p = 1;
        for (p = bv2.extract_next(p); p != 0; p = bv2.extract_next(p))
        {
            std::cout << "Extracted p = " << p << std::endl;
        } 
        PrintContainer(bv2.first(), bv2.end()); // <EMPTY>

        // 11. use bvector<>::set_bit_no_check() to set bits faster
        //
        bm::bvector<> bv3;
        bv3.init();   // the key here you MUST call init() before setting bits this way
                      
        bv3.set_bit_no_check(10);
        bv3.set_bit_no_check(100);
        bv3.set_bit_no_check(1000);

        PrintContainer(bv3.first(), bv3.end()); // 10, 100, 1000


        std::vector<bm_size_type> v1, v2, v3;
        generate_test_vectors(v1, v2, v3);

        for (unsigned k = 0; k < 1000; ++k)
        {
            // run a few CPU "pre-heat" loops to get consistent results
            bm_size_type s = 0;
            bm_size_type i;
            for (i = 0; i < v1.size(); ++i)
                s = s + s* v1[i] + i;
            for (i = 0; i < v2.size(); ++i)
                s = s + s* v2[i] + i;
            std::cout << s << "\r";
        }

        std::cout << std::endl << "Running benchmarks..." << std::endl;

        bv_set_bit_test();
        bv_set_bit_no_check_test();
        combine_or_test(v1, v2, v3);
        bvector_bulk_set_test(v1, v2, v3);

        // print all test timing results
        //
        std::cout << std::endl;
        bm::chrono_taker::print_duration_map(timing_map, bm::chrono_taker::ct_all);
    }
    catch(std::exception& ex)
    {
        std::cerr << ex.what() << std::endl;
        return 1;
    }

    return 0;
}