File: sample7.cpp

package info (click to toggle)
bmagic 3.4.0-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,208 kB
  • ctags: 2,500
  • sloc: cpp: 9,399; ansic: 3,142; sh: 2,430; makefile: 340
file content (197 lines) | stat: -rw-r--r-- 5,717 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
/*
Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)

Permission is hereby granted, free of charge, to any person 
obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, 
publish, distribute, sublicense, and/or sell copies of the Software, 
and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included 
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
OTHER DEALINGS IN THE SOFTWARE.
*/

/*! @example sample7.cpp
 This example demonstrates using of memory save mode of bitset operations.

For more information please visit:  http://bmagic.sourceforge.net

*/

/*! @file
    @ingroup mset
*/

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>


// BM library version 3.1.3 and later can keep internal bit flags in pointers.
// This is efficient but not completely portable hack. 
// For compatibility it can be disabled it by defining BM_DISBALE_BIT_IN_PTR.
// If you do not disable bits in pointer the second template parameter of bvector
// is simply ignored and portion of this example is becoming irrelevant.
#define BM_DISBALE_BIT_IN_PTR
#include "bm.h"

using namespace std;

// Customized bitvector uses standard memory allocator and uses
// an alternative implementation of internal set. Saves memory when we 
// work with sparse or dense bitsets.
typedef bm::bvector<bm::standard_allocator, 
                    bm::miniset<bm::block_allocator, bm::set_total_blocks> > bvect;


const unsigned setscount = 10000;
const unsigned randombits = 150;
const unsigned maxbit = 100000000;

bvect*  bitsets[setscount];

// ---------------------------------------------------------

void CreateSets()
{
    unsigned mu = 0;
    for (unsigned i = 0; i < setscount; ++i)
    {
        if ((i % 100) == 0) { cout << "."; cout.flush(); }
        // create bitvector using in GAP mode using an alternative
        // GAP levels table (minimalistic).
        bitsets[i] = 
            new bvect(bm::BM_GAP, bm::gap_len_table_min<true>::_len, maxbit);
        bvect& bv = *bitsets[i];
        bvect::statistics st;
        bv.calc_stat(&st);
        mu += st.memory_used;
    }
    cout << endl << "Created " << setscount << " sets." << endl; 
    cout << "Used " << mu / (1024*1024)<< " MB." << endl;
}

// ---------------------------------------------------------

void FillSets()
{
    unsigned mu, bit_blocks, gap_blocks;
    cout << "Filling sets...";
    mu = bit_blocks = gap_blocks = 0;
    for (unsigned i = 0; i < setscount; ++i)
    {
        if ((i % 100) == 0) { cout << "."; cout.flush(); }
        if ((i % 3) == 0) continue;
        bvect& bv = *bitsets[i];
        unsigned bn = 0;
        for (unsigned j = 0; j < randombits; j+=3)
        {
            bn += (maxbit / randombits) + rand() % 10;
            if (bn > maxbit) bn = rand() % maxbit;

            bv[bn] = true;
            bv[bn+1] = true;
            bv[bn+2] = true;
        }
        bvect::statistics st;
        bv.calc_stat(&st);
        mu += st.memory_used;
        bit_blocks += st.bit_blocks;
        gap_blocks += st.gap_blocks;
    }
    cout << endl << "Used " << mu / (1024*1024)<< " MB." << endl;

    cout << "BIT Blocks=" << bit_blocks << endl;
    cout << "GAP Blocks=" << gap_blocks << endl;
}

// ---------------------------------------------------------

void EnumerateSets()
{
    cout << "Enumerating sets...";
    unsigned bitcnt = 0;
    for (unsigned i = 0; i < setscount; ++i)
    {
        if ((i % 100) == 0) { cout << "."; cout.flush(); }
        bvect& bv = *bitsets[i];

        bvect::enumerator en = bv.first();
        bvect::enumerator en_end = bv.end();

        for (;en < en_end; ++en)
        {
            ++bitcnt;
        }
    }
    cout << endl << bitcnt << endl;
}

// ---------------------------------------------------------

void DestroySets()
{
    for (unsigned i = 0; i < setscount; ++i)
    {
        delete bitsets[i];
    }
}

// ---------------------------------------------------------

void OrSets()
{
    bvect res;
    cout << "Calculating Or...";
    for (unsigned i = 0; i < setscount; ++i)
    {
        if ((i % 100) == 0) { cout << "."; cout.flush(); }
        const bvect& bv = *bitsets[i];

        res |= bv;
    }
    cout << endl << res.count() << endl;
}

// ---------------------------------------------------------



int main(void)
{
    time_t start_time = time(0);

    CreateSets();
    FillSets();
    EnumerateSets();
    OrSets();

    time_t end_time = time(0);
    unsigned elapsed = end_time - start_time;
    cout << "elapsed=" << elapsed << endl;
    unsigned ops;
    if (elapsed)
        ops = setscount / elapsed;
    else
        ops = setscount;

    cout << "Time = " << (end_time - start_time) << endl;
    cout << "Operations per second:" << ops << endl;

    DestroySets();

    return 0;
}