00001 /* 00002 Copyright(c) 2002-2009 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::serializer 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::serializer<bm::bvector<> >& bvs, 00078 bm::bvector<>& bv) 00079 { 00080 // It is reccomended to optimize vector before serialization. 00081 bv.optimize(); 00082 00083 bm::bvector<>::statistics st; 00084 bv.calc_stat(&st); 00085 00086 cout << "Bits count:" << bv.count() << endl; 00087 cout << "Bit blocks:" << st.bit_blocks << endl; 00088 cout << "GAP blocks:" << st.gap_blocks << endl; 00089 cout << "Memory used:"<< st.memory_used << endl; 00090 cout << "Max.serialize mem.:" << st.max_serialize_mem << endl; 00091 00092 // Allocate serialization buffer. 00093 unsigned char* buf = new unsigned char[st.max_serialize_mem]; 00094 00095 // Serialization to memory. 00096 unsigned len = bvs.serialize(bv, buf, 0); 00097 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 // Prepare a serializer class 00116 // for best performance it is best to create serilizer once and reuse it 00117 // (saves a lot of memory allocations) 00118 // 00119 bm::serializer<bm::bvector<> > bvs; 00120 00121 // next settings provide lowest serilized size 00122 bvs.byte_order_serialization(false); 00123 bvs.gap_length_serialization(false); 00124 bvs.set_compression_level(4); 00125 00126 00127 unsigned char* buf1 = serialize_bvector(bvs, bv1); 00128 unsigned char* buf2 = serialize_bvector(bvs, bv2); 00129 00130 // Serialized bvectors (buf1 and buf2) now ready to be 00131 // saved to a database, file or send over a network. 00132 00133 // ... 00134 00135 // Deserialization. 00136 00137 bm::bvector<> bv3; 00138 00139 // As a result of desrialization bv3 will contain all bits from 00140 // bv1 and bv3: 00141 // bv3 = bv1 OR bv2 00142 00143 bm::deserialize(bv3, buf1); 00144 bm::deserialize(bv3, buf2); 00145 00146 print_statistics(bv3); 00147 00148 // After a complex operation we can try to optimize bv3. 00149 00150 bv3.optimize(); 00151 00152 print_statistics(bv3); 00153 00154 delete [] buf1; 00155 delete [] buf2; 00156 00157 return 0; 00158 } 00159