- Main Page
- Modules
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

# sample3.cpp

Go to the documentation of this file.

    00001 /*
    00002 Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
    00003
    00004 Permission is hereby granted, free of charge, to any person
    00005 obtaining a copy of this software and associated documentation
    00006 files (the "Software"), to deal in the Software without restriction,
    00007 including without limitation the rights to use, copy, modify, merge,
    00008 publish, distribute, sublicense, and/or sell copies of the Software,
    00009 and to permit persons to whom the Software is furnished to do so,
    00010 subject to the following conditions:
    00011
    00012 The above copyright notice and this permission notice shall be included
    00013 in all copies or substantial portions of the Software.
    00014
    00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    00016 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    00017 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    00018 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    00019 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    00020 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    00021 OTHER DEALINGS IN THE SOFTWARE.
    00022 */
    00023
    00024 /** \example sample3.cpp
    00025  Exmaple demonstrates using bitvectors with different initial
    00026  block allocation strategy.
    00027  Bitvector 1 (bv1) by default working without RLE compression option
    00028  (best performance, maximum memory consumption).
    00029  Bitvector 2 (bv2) will be working in compression mode and use less memory.
    00030
    00031   \sa bm::bvector<>::set_new_blocks_strat()
    00032
    00033   For more information please visit: http://bmagic.sourceforge.net
    00034
    00035 */
    00036
    00037 #include <stdlib.h>
    00038 #include <iostream>
    00039 #include "bm.h"
    00040
    00041 using namespace std;
    00042
    00043 const unsigned MAX_VALUE = 1000000;
    00044
    00045 // This procedure creates very dense bitvectors.
    00046 // The resulting set will consists mostly from ON (1) bits
    00047 // interrupted with small gaps of 0 bits.
    00048
    00049 void fill_bvector(bm::bvector<>* bv1, bm::bvector<>* bv2)
    00050 {
    00051     for (unsigned i = 0; i < MAX_VALUE; ++i)
    00052     {
    00053         if (rand() % 2500)
    00054         {
    00055             bv1->set_bit(i);
    00056             bv2->set_bit(i);
    00057         }
    00058     }
    00059 }
    00060
    00061
    00062 void print_statistics(const bm::bvector<>& bv)
    00063 {
    00064     bm::bvector<>::statistics st;
    00065     bv.calc_stat(&st);
    00066
    00067     cout << "Bits count:" << bv.count() << endl;
    00068     cout << "Bit blocks:" << st.bit_blocks << endl;
    00069     cout << "GAP blocks:" << st.gap_blocks << endl;
    00070     cout << "Memory used:"<< st.memory_used << endl;
    00071     cout << "Max.serialize mem.:" << st.max_serialize_mem << endl << endl;;
    00072 }
    00073
    00074
    00075 int main(void)
    00076 {
    00077     bm::bvector<>   bv1;
    00078     bm::bvector<>   bv2;
    00079
    00080     bv2.set_new_blocks_strat(bm::BM_GAP);  //  set DGAP compression mode ON
    00081
    00082     fill_bvector(&bv1, &bv2);  // Fill both bvectors with the same values
    00083
    00084     // For a given distrubution statistics should demonstrate
    00085     // lower memory consumption for the vector with compression
    00086
    00087     print_statistics(bv1);
    00088     print_statistics(bv2);
    00089
    00090     // Now run optimization procedure for bv1 and see statistics.
    00091
    00092     bv1.optimize();
    00093
    00094     print_statistics(bv1);
    00095
    00096     return 0;
    00097 }
    00098