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

sample8.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 sample8.cpp
00025 
00026     Example demonstrates some STL compatability set operations using
00027     set iterators.
00028  
00029   \sa bm::bvector<>::enumerator 
00030   \sa bm::bvector<>::insert_iterator
00031 
00032    For more information please visit:  http://bmagic.sourceforge.net
00033 
00034 */
00035 
00036 #include <iostream>
00037 #include <algorithm>
00038 #include <vector>
00039 #include <list>
00040 
00041 using std::vector;
00042 using std::list;
00043 
00044 // This example requires STL compatibility
00045 #ifdef BM_NO_STL
00046 # undef BM_NO_STL
00047 #endif
00048 
00049 #include "bm.h"
00050 
00051 using namespace std;
00052 
00053 void Print(unsigned n)
00054 {
00055     cout << n << endl;;
00056 }
00057 
00058 // Utility template function used to print container
00059 template<class T> void PrintContainer(T first, T last)
00060 {
00061     if (first == last)
00062         cout << "<EMPTY SET>";
00063     else
00064         for(;first != last; ++first)
00065             cout << *first << ";";
00066     cout << endl;
00067 }
00068 
00069 int main(void)
00070 {
00071     bm::bvector<>   bv;    
00072 
00073     bv[10] = true;
00074     bv[100] = true;
00075     bv[10000] = true;
00076     
00077     cout << "Source set:";
00078     PrintContainer(bv.first(), bv.end());
00079     
00080     // copy all bitset information into STL vector using copy algorithm
00081     {
00082         vector<unsigned> vect;
00083         vect.resize(bv.count());
00084         std::copy(bv.first(), bv.end(), vect.begin());
00085         cout << "Vector:";
00086         PrintContainer(vect.begin(), vect.end());
00087     }
00088 
00089     // doing the same with the help of back_inserter
00090 
00091     {
00092         list<unsigned> lst;
00093         std::copy(bv.first(), bv.end(), std::back_inserter(lst));
00094         cout << "List:";
00095         PrintContainer(lst.begin(), lst.end());
00096     }
00097 
00098     {
00099         vector<unsigned>   vect;
00100         vector<unsigned>   res1, res2, res3;
00101         
00102         vect.push_back(100);
00103         vect.push_back(15);
00104         vect.push_back(150);
00105         
00106         cout << "Argument vector for set operations:";
00107         PrintContainer(vect.begin(), vect.end());
00108         
00109         // set should be ordered by < to make set algorithms possible
00110         std::sort(vect.begin(), vect.end());
00111         cout << endl;
00112         
00113         std::set_union(bv.first(), bv.end(),
00114                        vect.begin(), vect.end(),
00115                        std::back_inserter(res1)); //10;15;100;150;10000
00116         cout << "Set union:" << endl;
00117         PrintContainer(res1.begin(), res1.end());
00118         
00119         std::set_intersection(bv.first(), bv.end(),
00120                               vect.begin(), vect.end(),
00121                               std::back_inserter(res2));  // 100
00122         cout << "Set intersection:" << endl;
00123         PrintContainer(res2.begin(), res2.end());
00124 
00125         vector<unsigned>::const_iterator it1 = vect.begin();
00126         vector<unsigned>::const_iterator it2 = vect.end();
00127         bm::bvector<>::enumerator en = bv.first();
00128         bm::bvector<>::enumerator en2= bv.end();
00129         
00130         std::set_difference(en, en2,
00131                             it1, it2,
00132                             std::back_inserter(res3));  // 10;10000
00133 
00134         cout << "Set diff:" << endl;
00135         PrintContainer(res3.begin(), res3.end());
00136         
00137     }
00138 
00139     // Using bvector<>::insert_iterator to set bits
00140     {
00141         bm::bvector<> bv1;
00142         std::vector<unsigned> vect;
00143         
00144         vect.push_back(300);
00145         vect.push_back(200);
00146         vect.push_back(275);
00147         vect.push_back(200);
00148         
00149         cout << endl << "Source vector:";
00150         PrintContainer(vect.begin(), vect.end()); // 300;200;275;200;
00151         
00152         // The "side effect" of this operation is that we sorted
00153         // the input sequence and eliminated duplicates
00154         
00155         std::copy(vect.begin(), vect.end(), bv1.inserter());
00156         cout << "Bitset:";
00157         
00158         PrintContainer(bv1.first(), bv1.end());  // 200;275;300
00159     }
00160     
00161     
00162     return 0;
00163 }
00164 

Generated on Sun Aug 5 14:12:26 2007 for BitMagic by  doxygen 1.4.1