DiaDes  0.1
DIAgnosis of Discrete-Event System
Selection.hh
Go to the documentation of this file.
1 #ifndef __DIADES__UTILS__SELECTION__HH
2 #define __DIADES__UTILS__SELECTION__HH
3 
4 #include<cstddef>
5 #include<vector>
6 #include<string>
7 #include<iostream>
8 #include<sstream>
9 
10 
11 
12 namespace Diades
13 {
14  namespace Utils
15  {
16  using std::vector;
17  using std::string;
18  using std::cout;
19  using std::endl;
20  using std::stringstream;
21 
29  template<typename Item>
30  class Selection
31  {
32  public:
33  typedef vector<Item> ItemVector;
34 
35  private:
36 
37  const ItemVector & _items;
38  vector<unsigned> _unselectedItemPool;
39  vector<unsigned> _position;
41 
42 
43  public:
44 
46  {
47  public:
48  typedef IndexIterator self;
49  typedef std::ptrdiff_t difference_type;
50  typedef std::forward_iterator_tag iterator_category;
51  typedef unsigned value_type;
52  typedef unsigned* pointer;
53  typedef const unsigned& reference;
54 
55 
56  private:
57  const vector<unsigned> & _unselectedItemPool;
58  unsigned _index;
59  public:
60 
61  IndexIterator(const vector<unsigned> & unselectedItemPool,
62  unsigned index) : _unselectedItemPool(unselectedItemPool),
63  _index(index)
64  {
65  }
66 
67  reference operator*() const
68  {
69  return _unselectedItemPool[_index];
70  }
71 
72  pointer operator->() const
73  {
74  return &_unselectedItemPool[_index];
75  }
76 
77  self & operator++()
78  {
79  ++_index;
80  return *this;
81  }
82 
83  self operator++(int)
84  {
85  self tmp = *this;
86  ++_index;
87  return tmp;
88  }
89 
90  bool operator==(const self & it) const
91  {
92  return(_unselectedItemPool == it._unselectedItemPool) && (_index == it._index);
93  }
94 
95  bool operator!=(const self & it) const
96  {
97  return !(*this == it);
98  }
99  };
100 
107  Selection(const vector<Item> & items) : _items(items),
108  _unselectedItemPool(items.size()),
109  _position(items.size()),
110  _selectedIndexStart(items.size())
111  {
112  for(unsigned index = 0; index < _items.size(); ++index)
113  {
114  _unselectedItemPool[index] = index;
115  _position[index] = index;
116  }
117  }
118 
127  indexBegin() const
128  {
129  return IndexIterator(_unselectedItemPool, 0);
130  }
131 
140  indexEnd() const
141  {
142  return IndexIterator(_unselectedItemPool, _unselectedItemPool.size());
143  }
144 
151  unsigned
153  {
154  return _selectedIndexStart;
155  }
156 
163  unsigned
165  {
167  }
168 
175  unsigned
177  {
178  return _items.size();
179  }
180 
190  {
191  return IndexIterator(_unselectedItemPool, _selectedIndexStart);
192  }
193 
203  {
204  return IndexIterator(_unselectedItemPool, _unselectedItemPool.size());
205  }
206 
216  {
217  return IndexIterator(_unselectedItemPool, 0);
218  }
219 
229  {
230  return IndexIterator(_unselectedItemPool, _selectedIndexStart);
231  }
232 
240  typename ItemVector::const_iterator
242  {
243  return _items.begin() + _selectedIndexStart;
244  }
245 
253  typename ItemVector::const_iterator
254  selectedEnd() const
255  {
256  return _items.end();
257  }
258 
266  typename ItemVector::const_iterator
268  {
269  return _items.begin();
270  }
271 
279  typename ItemVector::const_iterator
281  {
282  return _items.begin() + _selectedIndexStart;
283  }
284 
293  bool
294  hasBeenAlreadySelectedOnce(unsigned index) const
295  {
296  return index >= _selectedIndexStart;
297  }
298 
306  bool
307  allSelected() const
308  {
309  return _selectedIndexStart == 0;
310  }
311 
321  unsigned
323  {
324  unsigned result = _unselectedItemPool[0];
325  select(_unselectedItemPool[0]);
326  return result;
327  }
328 
336  const Item &
337  getItem(unsigned index) const
338  {
339  return _items[index];
340  }
341 
347  void
348  select(unsigned index)
349  {
351  unsigned theSelectedToSwap = _unselectedItemPool[_position[index]];
352  unsigned oldPositionOfTheSelectedToSwap = _position[index];
353  unsigned theNonSelectedToSwap = _unselectedItemPool[_selectedIndexStart];
354  _unselectedItemPool[oldPositionOfTheSelectedToSwap] = theNonSelectedToSwap;
355  _unselectedItemPool[_selectedIndexStart] = theSelectedToSwap;
356  _position[theSelectedToSwap] = _selectedIndexStart;
357  _position[theNonSelectedToSwap] = oldPositionOfTheSelectedToSwap;
358  }
359 
364  void
366  {
367  cout << "*******************************************************************************" << endl;
368  cout << "CURRENT TABLES:" << endl;
369  cout << "\t";
370  for(unsigned i = 0; i < _items.size(); ++i)
371  {
372  cout << _items[i] << "\t";
373  }
374  cout << endl;
375  cout << "\t";
376  for(unsigned i = 0; i < _items.size(); ++i)
377  {
378  cout << _unselectedItemPool[i] << "\t";
379  }
380  cout << endl;
381  cout << "\t";
382  for(unsigned i = 0; i < _items.size(); ++i)
383  {
384  cout << _position[i] << "\t";
385  }
386  cout << endl;
387 
388  cout << "SELECTED ITEMS:" << endl;
389  cout << "\t";
390  for(unsigned i = _selectedIndexStart; i < _items.size(); ++i)
391  {
392  cout << _items[_unselectedItemPool[i]] << "\t";
393  }
394  cout << endl;
395  cout << "UNSELECTED ITEMS:" << endl;
396  cout << "\t";
397  for(unsigned i = 0; i < _selectedIndexStart; ++i)
398  {
399  cout << _items[_unselectedItemPool[i]] << "\t";
400  }
401 
402  cout << endl;
403  cout << "POSITIONS (should be exactly the same as the first table:" << endl;
404  cout << "\t";
405  for(unsigned i = 0; i < _items.size(); ++i)
406  {
407  cout << _items[_unselectedItemPool[_position[i]]] << "\t";
408  }
409  cout << endl;
410  cout << "*******************************************************************************" << endl;
411  }
412  };
413  };
414 };
415 
416 #endif
IndexIterator selectedIndexEnd() const
Definition: Selection.hh:202
const ItemVector & _items
Definition: Selection.hh:37
unsigned _selectedIndexStart
Definition: Selection.hh:40
ItemVector::const_iterator nonSelectedBegin() const
Definition: Selection.hh:267
IndexIterator(const vector< unsigned > &unselectedItemPool, unsigned index)
Definition: Selection.hh:61
IndexIterator nonSelectedIndexBegin() const
Definition: Selection.hh:215
IndexIterator nonSelectedIndexEnd() const
Definition: Selection.hh:228
ItemVector::const_iterator nonSelectedEnd() const
Definition: Selection.hh:280
IndexIterator indexEnd() const
Definition: Selection.hh:140
bool hasBeenAlreadySelectedOnce(unsigned index) const
Definition: Selection.hh:294
unsigned numberOfItems() const
Definition: Selection.hh:176
vector< unsigned > _position
Definition: Selection.hh:39
void select(unsigned index)
Definition: Selection.hh:348
std::forward_iterator_tag iterator_category
Definition: Selection.hh:50
vector< unsigned > _unselectedItemPool
Definition: Selection.hh:38
const Item & getItem(unsigned index) const
Definition: Selection.hh:337
Namespace of the Diades project.
bool operator==(const self &it) const
Definition: Selection.hh:90
const vector< unsigned > & _unselectedItemPool
Definition: Selection.hh:57
unsigned numberOfSelectedItems() const
Definition: Selection.hh:164
bool allSelected() const
Definition: Selection.hh:307
ItemVector::const_iterator selectedEnd() const
Definition: Selection.hh:254
IndexIterator indexBegin() const
Definition: Selection.hh:127
ItemVector::const_iterator selectedBegin() const
Definition: Selection.hh:241
IndexIterator selectedIndexBegin() const
Definition: Selection.hh:189
bool operator!=(const self &it) const
Definition: Selection.hh:95
unsigned numberOfNonSelectedItems() const
Definition: Selection.hh:152
vector< Item > ItemVector
Definition: Selection.hh:33
Selection(const vector< Item > &items)
Definition: Selection.hh:107