DGtal  1.5.beta
testIndexedListWithBlocks.cpp
Go to the documentation of this file.
1 
29 //#define TRACE_BITS
30 
31 #include <cstdio>
32 #include <cmath>
33 #include <iostream>
34 #include <algorithm>
35 #include <vector>
36 #include "DGtal/base/Common.h"
37 #include "DGtal/base/IndexedListWithBlocks.h"
38 #include "DGtal/base/Labels.h"
39 
40 using namespace DGtal;
41 using namespace std;
42 
43 template <typename Container1, typename Container2>
44 bool
45 isEqual( Container1 & c1, Container2 & c2 )
46 {
47  return ( c1.size() == c2.size() )
48  && std::equal( c1.begin(), c1.end(), c2.begin() );
49 }
50 
51 template <typename VContainer1, typename LContainer2>
52 void insert( VContainer1 & c1, LContainer2 & c2, unsigned int idx, double v )
53 {
54  c1.insert( c1.begin() + idx, v );
55  c2.insert( idx, v );
56 }
57 
58 template <typename VContainer1, typename LContainer2>
59 bool
60 checkInsert( VContainer1 & v, LContainer2 & l,
61  unsigned int nb )
62 {
63  for ( unsigned int i = 0; i < nb; ++i )
64  {
65  unsigned int idx = rand() % ( l.size() + 1 );
66  double val = ( (double)rand() ) / RAND_MAX;
67  insert( v, l, idx, val );
68  }
69  return isEqual( v, l );
70 }
71 
72 template <typename VContainer1, typename LContainer2>
73 void erase( VContainer1 & c1, LContainer2 & c2, unsigned int idx )
74 {
75  c1.erase( c1.begin() + idx );
76  c2.erase( idx );
77 }
78 
79 template <typename VContainer1, typename LContainer2>
80 bool
81 checkErase( VContainer1 & v, LContainer2 & l,
82  unsigned int nb )
83 {
84  for ( unsigned int i = 0; i < nb; ++i )
85  {
86  unsigned int idx = rand() % ( l.size() );
87  erase( v, l, idx );
88  }
89  return isEqual( v, l );
90 }
91 
92 
93 int main()
94 {
95  typedef IndexedListWithBlocks<double, 2, 10> MyIndexedList;
96  BOOST_CONCEPT_ASSERT(( boost::Container< MyIndexedList > ));
97  BOOST_CONCEPT_ASSERT(( boost::ForwardIterator< MyIndexedList::Iterator > ));
99  unsigned int nb = 0;
100  unsigned int nbok = 0;
101  trace.beginBlock ( "Testing IndexedListWithBlocks" );
102  MyIndexedList l;
103  vector<double> v;
104  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
105  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
106  insert( v, l, 0, 4.5 );
107  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
108  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
109  insert( v, l, 0, 10.1 );
110  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
111  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
112  insert( v, l, 1, 3.7 );
113  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
114  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
115  insert( v, l, 2, 8.4 );
116  insert( v, l, 1, 2.1 );
117  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
118  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
119  insert( v, l, 2, -3.0 );
120  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
121  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
122  insert( v, l, (unsigned int)v.size(), -13.1 );
123  ++nb; nbok += isEqual( v, l ) ? 1 : 0;
124  std::cout << "(" << nbok << "/" << nb << ") l=" << l << std::endl;
125  ++nb; nbok += checkInsert( v, l, 10000 ) ? 1 : 0;
126  std::cout << "(" << nbok << "/" << nb << ") 10000 insertions" << std::endl;
127  ++nb; nbok += checkErase( v, l, 10000 ) ? 1 : 0;
128  std::cout << "(" << nbok << "/" << nb << ") 10000 deletions l=" << l << std::endl;
129  trace.endBlock();
130  return ( nb == nbok ) ? 0 : 1;
131 }
Aim: Represents a mixed list/array structure which is useful in some context. It is essentially a lis...
void beginBlock(const std::string &keyword="")
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Go to http://www.sgi.com/tech/stl/Container.html.
Definition: Boost.dox:104
Go to http://www.sgi.com/tech/stl/ForwardIterator.html.
Definition: Boost.dox:40
void erase(VContainer1 &c1, LContainer2 &c2, unsigned int idx)
bool isEqual(Container1 &c1, Container2 &c2)
bool checkInsert(VContainer1 &v, LContainer2 &l, unsigned int nb)
void insert(VContainer1 &c1, LContainer2 &c2, unsigned int idx, double v)
bool checkErase(VContainer1 &v, LContainer2 &l, unsigned int nb)