Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals | Examples

sample4.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 sample4.cpp
00025  Exmaple demonstrates bitvector serialization/deserialization.
00026  
00027 For more information please visit:  http://bmagic.sourceforge.net
00028 
00029   \sa bm::serialize 
00030   \sa bm::deserialize 
00031 
00032 */
00033 
00034 #include <stdlib.h>
00035 #include <iostream>
00036 #include "bm.h"
00037 #include "bmserial.h"
00038 
00039 using namespace std;
00040 
00041 
00042 // This exmaple demonstrates bitvector serialization/deserialization.
00043 
00044 
00045 
00046 const unsigned MAX_VALUE = 1000000;
00047 
00048 // This procedure creates very dense bitvector.
00049 // The resulting set will consists mostly from ON (1) bits
00050 // interrupted with small gaps of 0 bits.
00051 
00052 void fill_bvector(bm::bvector<>* bv)
00053 {
00054     for (unsigned i = 0; i < MAX_VALUE; ++i)
00055     {
00056         if (rand() % 2500)
00057         {
00058             bv->set_bit(i);
00059         }
00060     }
00061 }
00062 
00063 
00064 void print_statistics(const bm::bvector<>& bv)
00065 {
00066     bm::bvector<>::statistics st;
00067     bv.calc_stat(&st);
00068 
00069     cout << "Bits count:" << bv.count() << endl;
00070     cout << "Bit blocks:" << st.bit_blocks << endl;
00071     cout << "GAP blocks:" << st.gap_blocks << endl;
00072     cout << "Memory used:"<< st.memory_used << endl;
00073     cout << "Max.serialize mem.:" << st.max_serialize_mem << endl << endl;;
00074 }
00075 
00076 
00077 unsigned char* serialize_bvector(bm::bvector<>& bv)
00078 {
00079     // It is reccomended to optimize vector before serialization.
00080     bv.optimize();  
00081 
00082     bm::bvector<>::statistics st;
00083     bv.calc_stat(&st);
00084 
00085     cout << "Bits count:" << bv.count() << endl;
00086     cout << "Bit blocks:" << st.bit_blocks << endl;
00087     cout << "GAP blocks:" << st.gap_blocks << endl;
00088     cout << "Memory used:"<< st.memory_used << endl;
00089     cout << "Max.serialize mem.:" << st.max_serialize_mem << endl;
00090 
00091     // Allocate serialization buffer.
00092 
00093     unsigned char*  buf = new unsigned char[st.max_serialize_mem];
00094 
00095     // Serialization to memory.
00096 
00097     unsigned len = bm::serialize(bv, buf);
00098 
00099     cout << "Serialized size:" << len << endl << endl;
00100 
00101     return buf;
00102 }
00103 
00104 
00105 int main(void)
00106 {
00107     bm::bvector<>   bv1;    
00108     bm::bvector<>   bv2;
00109 
00110     bv2.set_new_blocks_strat(bm::BM_GAP);  //  set DGAP compression mode ON
00111 
00112     fill_bvector(&bv1);
00113     fill_bvector(&bv2);
00114     
00115     unsigned char* buf1 = serialize_bvector(bv1);
00116     unsigned char* buf2 = serialize_bvector(bv2);
00117 
00118     // Serialized bvectors (buf1 and buf2) now ready to be
00119     // saved to a database, file or send over a network.
00120 
00121     // ...
00122 
00123     // Deserialization.
00124 
00125     bm::bvector<>  bv3;
00126 
00127     // As a result of desrialization bv3 will contain all bits from
00128     // bv1 and bv3:
00129     //   bv3 = bv1 OR bv2
00130 
00131     bm::deserialize(bv3, buf1);
00132     bm::deserialize(bv3, buf2);
00133 
00134     print_statistics(bv3);
00135 
00136     // After a complex operation we can try to optimize bv3.
00137 
00138     bv3.optimize();
00139 
00140     print_statistics(bv3);
00141 
00142     delete [] buf1;
00143     delete [] buf2;
00144 
00145     return 0;
00146 }
00147 

Generated on Thu Apr 20 13:28:46 2006 for BitMagic by  doxygen 1.4.1