Advertisement
Guest User

Tuple iterator test by Anthony Williams

a guest
Dec 13th, 2012
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.57 KB | None | 0 0
  1. #include "tupleit.hh"
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <string>
  6. #include <iterator>
  7. #include <deque>
  8. #include <list>
  9. #include <assert.h>
  10.  
  11. struct MyCompareFunc
  12. {
  13.     template<typename Tuple>
  14.     bool operator()(Tuple const& lhs,Tuple const& rhs) const
  15.     {
  16.         if(lhs.template get<0>()<rhs.template get<0>())
  17.         {
  18.             return true;
  19.         }
  20.         else return false;
  21.     }
  22. }
  23. myCompareFunc;
  24.  
  25. struct WriteTriple
  26. {
  27.     template<typename Tuple>
  28.     void operator()(Tuple const& tuple) const
  29.     {
  30.         std::cout<<"("<<tuple.template get<0>()<<","<<tuple.get_tail().template get<0>()<<","<<tuple.get_tail().get_tail().template get<0>()<<")"<<std::endl;
  31.     }
  32. } writeTriple;
  33.  
  34. template<typename Tuple>
  35. void copyTuple(Tuple t)
  36. {
  37.     Tuple t1(t);
  38.     t1=t;
  39. }
  40.  
  41. void testStuff()
  42. {
  43.     typedef std::vector<int> VecInt;
  44.     VecInt v1,v2;
  45.     // initialize v1 and v2
  46.     v1.resize(3);
  47.     v2.resize(3);
  48.     v1[0]=42;
  49.     v2[0]=23;
  50.  
  51.     typedef iterators::TupleIt<boost::tuple<VecInt::iterator,VecInt::iterator> >
  52.         TupleIteratorType;
  53.  
  54.     TupleIteratorType tupleIterator=iterators::makeTupleIterator(v1.begin(),v2.begin());
  55.  
  56.     *tupleIterator=boost::tuple<int,int>(1,3); // write to vectors
  57.     std::cout<<"v1[0]=="<<v1[0]<<std::endl;
  58.     std::cout<<"v2[0]=="<<v2[0]<<std::endl;
  59.     assert(v1[0]==1);
  60.     assert(v2[0]==3);
  61.  
  62.     TupleIteratorType::value_type valueCopy=*tupleIterator; // read values
  63.     valueCopy=boost::tuple<int,int>(9,27); // write to copy only
  64.  
  65.     // verify original vector unchanged
  66.     std::cout<<"v1[0]=="<<v1[0]<<std::endl;
  67.     std::cout<<"v2[0]=="<<v2[0]<<std::endl;
  68.     std::cout<<"valueCopy<0>=="<<valueCopy.get<0>()<<std::endl;
  69.     std::cout<<"valueCopy<1>=="<<valueCopy.get<1>()<<std::endl;
  70.     assert(v1[0]==1);
  71.     assert(v2[0]==3);
  72.  
  73.     TupleIteratorType::value_type& opStarResult=*tupleIterator; // Ok.
  74.     int& intRef=opStarResult.get<0>(); // ref to element in v1
  75.     intRef=46;
  76.     assert(v1[0]==46);
  77.     std::cout<<"v1[0]=="<<v1[0]<<std::endl;
  78.     std::cout<<"v2[0]=="<<v2[0]<<std::endl;
  79. }
  80.  
  81. class bigstuff
  82. {
  83.     char array[256];
  84. };
  85.  
  86.  
  87. int main()
  88. {
  89.     std::vector<int> v1,v2,v3;
  90.    
  91.     for(int i=20;i;--i)
  92.     {
  93.         v1.push_back(i);
  94.         v2.push_back(2*i);
  95.         v3.push_back(3*i);
  96.     }
  97.  
  98.     std::cout<<"Initial"<<std::endl;
  99.     for(int i=0;i<v1.size();++i)
  100.     {
  101.         std::cout<<"entry["<<i<<"]=("<<v1[i]<<","<<v2[i]<<","<<v3[i]<<")"<<std::endl;
  102.     }
  103.     std::for_each(iterators::makeTupleIterator(v1.begin(),v2.begin(),v3.begin()),iterators::makeTupleIterator(v1.end(),v2.end(),v3.end()),writeTriple);
  104.    
  105.     std::sort(iterators::makeTupleIterator(v1.begin(),v2.begin(),v3.begin()),iterators::makeTupleIterator(v1.end(),v2.end(),v3.end()),myCompareFunc);
  106.  
  107.     std::cout<<"Sorted"<<std::endl;
  108.     for(int i=0;i<v1.size();++i)
  109.     {
  110.         std::cout<<"entry["<<i<<"]=("<<v1[i]<<","<<v2[i]<<","<<v3[i]<<")"<<std::endl;
  111.     }
  112.  
  113.     std::random_shuffle(iterators::makeTupleIterator(v1.begin(),v2.begin(),v3.begin()),iterators::makeTupleIterator(v1.end(),v2.end(),v3.end()));
  114.  
  115.     std::cout<<"Shuffled"<<std::endl;
  116.     for(int i=0;i<v1.size();++i)
  117.     {
  118.         std::cout<<"entry["<<i<<"]=("<<v1[i]<<","<<v2[i]<<","<<v3[i]<<")"<<std::endl;
  119.     }
  120.    
  121.     std::sort(iterators::makeTupleIterator(v1.begin(),v2.begin(),v3.begin()),iterators::makeTupleIterator(v1.end(),v2.end(),v3.end()),myCompareFunc);
  122.  
  123.     std::cout<<"Sorted"<<std::endl;
  124.     for(int i=0;i<v1.size();++i)
  125.     {
  126.         std::cout<<"entry["<<i<<"]=("<<v1[i]<<","<<v2[i]<<","<<v3[i]<<")"<<std::endl;
  127.     }
  128.  
  129.     std::istream_iterator<std::string> cinIt(std::cin);
  130.  
  131.     writeTriple(*iterators::makeTupleIterator(cinIt,v1.begin(),v2.begin()));
  132.  
  133.     std::ostream_iterator<std::string> coutIt(std::cout);
  134.    
  135.     std::vector<std::string> vs;
  136.     std::deque<int> di;
  137.     std::list<int> li;
  138.  
  139.     *iterators::makeTupleIterator(coutIt,std::back_inserter(vs),std::back_inserter(di),std::back_inserter(li))=boost::make_tuple("hello\n","there",1,2);
  140.     *iterators::makeTupleIterator(coutIt,std::back_inserter(vs),std::back_inserter(di),std::back_inserter(li))=boost::make_tuple("goodbye\n","world",3,4);
  141.    
  142.     std::for_each(iterators::makeTupleIterator(vs.begin(),di.begin(),li.begin()),iterators::makeTupleIterator(vs.end(),di.end(),li.end()),writeTriple);
  143.  
  144.     testStuff();
  145.  
  146.     std::cout<<"sizeof(tuplevectoriterator)="<<sizeof(iterators::TupleIt<boost::tuple<std::vector<bigstuff>::iterator,std::vector<bigstuff>::iterator,std::vector<bigstuff>::iterator> >)<<std::endl;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement