CASM  1.1.0
A Clusters Approach to Statistical Mechanics
RandomAccessEnumerator.hh
Go to the documentation of this file.
1 #ifndef CASM_RandomAccessEnumerator
2 #define CASM_RandomAccessEnumerator
3 
4 #include <string>
5 
8 
9 namespace CASM {
10 
11 template <typename ValueType, bool IsConst>
12 class RandomAccessEnumIterator;
13 
14 template <typename ValueType, bool IsConst>
15 class RandomAccessEnumeratorBase;
16 
17 template <typename ValueType, bool IsConst>
18 class RandomAccessEnumerator;
19 
30 template <typename ValueType, bool IsConst = true>
32  : public InputEnumIteratorBase<ValueType, IsConst> {
34 
35  public:
40 
42 
45  : InputEnumIteratorBase<ValueType, IsConst>(enumerator, is_end) {
46  if (!is_end) {
47  m_it_step = 0;
48  } else {
49  m_it_step = _enum().size();
50  }
51  }
52 
55 
59  step_type step() const override { return m_it_step; }
60 
66  bool is_end() const override {
67  this->_assert_ptr();
68  return _constructed_as_end() || step() == _enum().size();
69  }
70 
71  std::unique_ptr<RandomAccessEnumIteratorBase> clone() const {
72  return std::unique_ptr<RandomAccessEnumIteratorBase>(this->_clone());
73  }
74 
75  protected:
78 
79  private:
81  reference dereference() const override {
82  _enum().goto_step(m_it_step);
83  return _enum().current();
84  }
85 
87  void increment() override { ++m_it_step; }
88 
90  void decrement() { --m_it_step; }
91 
93  void advance(step_type n) { m_it_step += n; }
94 
97  return B.m_it_step - m_it_step;
98  }
99 
103  }
104 
108  *_enum_ptr());
109  }
110 
114  *_enum_ptr());
115  }
116 
118 };
119 
122 template <typename ValueType, bool IsConst = true>
124 
125  public boost::iterator_facade<RandomAccessEnumIterator<ValueType, IsConst>,
126  ValueType, boost::random_access_traversal_tag,
127  CASM_TMP::ConstSwitch<IsConst, ValueType> &,
128  long> {
129  public:
132  typedef ValueType value_type;
136 
139 
143  : m_ptr(notstd::clone(it)) {}
144 
145  step_type step() const { return m_ptr->step(); }
146 
147  jsonParser source() const { return m_ptr->source(); }
148 
149  std::string name() const { return m_ptr->name(); }
150 
151  std::unique_ptr<RandomAccessEnumIterator> clone() const {
152  return std::unique_ptr<RandomAccessEnumIterator>(this->_clone());
153  }
154 
155  private:
156  friend boost::iterator_core_access;
157 
160  return new RandomAccessEnumIterator(*this);
161  }
162 
164  bool equal(const RandomAccessEnumIterator &B) const {
165  return m_ptr->equal(*(B.m_ptr));
166  }
167 
168  reference dereference() const { return m_ptr->dereference(); }
169 
170  void increment() { m_ptr->increment(); }
171 
172  void decrement() { m_ptr->decrement(); }
173 
174  void advance(step_type n) { m_ptr->advance(n); }
175 
177  return m_ptr->distance_to(*(B.m_ptr));
178  }
179 
182 };
183 
250 template <typename ValueType, bool IsConst = true>
252  : public InputEnumeratorBase<ValueType, IsConst> {
254 
255  public:
260  typedef std::reverse_iterator<iterator> reverse_iterator;
262 
264  : InputEnumeratorBase<ValueType, IsConst>(), m_size(size) {}
265 
267 
271  using InputEnumeratorBase<ValueType, IsConst>::name; // pure virtual
273 
276  }
277 
280  }
281 
283 
285 
287  size_type size() const { return m_size; };
288 
291  this->goto_step(0);
292  return this->current();
293  }
294 
297  this->goto_step(size() - 1);
298  return this->current();
299  }
300 
302  reference initial() { return front(); }
303 
305  reference final() { return back(); }
306 
309  this->goto_step(n);
310  return this->current();
311  }
312 
313  protected:
320 
322  void _set_size(size_type val) { m_size = val; }
323 
324  private:
331 
337  if (this->step() == n) {
338  return;
339  }
340  this->_set_step(n);
341  if (n >= 0 && n < this->size()) {
342  this->_validate();
343  this->_set_current_ptr(this->at_step(n));
344  } else {
345  this->_invalidate();
346  }
347  }
348 
351  void increment() override { goto_step(step() + 1); }
352 
353  size_type m_size;
354 };
355 
359 template <typename ValueType, bool IsConst = true>
361  public:
364  typedef ValueType value_type;
368  typedef std::reverse_iterator<iterator> reverse_iterator;
371 
373 
376  : ptr(e.release()) {}
377 
380 
381  iterator begin() { return ptr->begin(); }
382 
383  iterator end() { return ptr->end(); }
384 
386 
388 
390  reference current() const { return ptr->current(); }
391 
393  step_type step() const { return ptr->step(); }
394 
396  bool valid() const { return ptr->valid(); }
397 
398  jsonParser source(step_type step) const { return ptr->source(step); }
399 
401  std::string name() const { return ptr->name(); }
402 
403  std::unique_ptr<RandomAccessEnumeratorBase<ValueType, IsConst> > ptr;
404 };
405 
406 } // namespace CASM
407 
408 #endif
EnumeratorBase::step_type step_type
Definition: Enumerator.hh:225
void _validate()
Used if random access enumerator step is moved into valid range.
Definition: Enumerator.hh:162
Base class for implementing specialized input enumerators.
RandomAccessEnumIterator implemenation.
void decrement()
boost::iterator_facade implementation
difference_type distance_to(const RandomAccessEnumIteratorBase &B) const
boost::iterator_facade implementation
RandomAccessEnumeratorBase< ValueType, IsConst > & _enum() const
static_cast enumerator pointer to RandomAccessEnumeratorBase&
void increment() override
boost::iterator_facade implementation
void advance(step_type n)
boost::iterator_facade implementation
reference dereference() const override
boost::iterator_facade implementation
std::unique_ptr< RandomAccessEnumIteratorBase > clone() const
RandomAccessEnumIteratorBase(RandomAccessEnumeratorBase< ValueType, IsConst > &enumerator, bool is_end)
RandomAccessEnumIteratorBase< ValueType, IsConst > * _clone() const override
clone implementation
RandomAccessEnumeratorBase< ValueType, IsConst > & _enum()
static_cast enumerator pointer to RandomAccessEnumeratorBase&
The iterator type for RandomAccessEnumerator.
notstd::cloneable_ptr< RandomAccessEnumIteratorBase< ValueType, IsConst > > m_ptr
std::unique_ptr< RandomAccessEnumIterator > clone() const
bool equal(const RandomAccessEnumIterator &B) const
boost::iterator_facade implementation
RandomAccessEnumIterator(const RandomAccessEnumIteratorBase< ValueType, IsConst > &it)
Construct iterator.
RandomAccessEnumIteratorBase< ValueType, IsConst >::step_type step_type
RandomAccessEnumIterator()
Default constructor.
RandomAccessEnumIterator * _clone() const
clone implementation
RandomAccessEnumIteratorBase< ValueType, IsConst >::reference reference
difference_type distance_to(const RandomAccessEnumIterator &B) const
Base class for implementing specialized random access enumerators.
reference front()
Reference to first element in enumerator.
reference operator[](size_type n)
Reference an element in enumerator.
RandomAccessEnumIterator< ValueType, IsConst > iterator
std::reverse_iterator< iterator > reverse_iterator
void _set_size(size_type val)
Set size value.
virtual CASM_TMP::ConstSwitch< IsConst, value_type > * at_step(step_type n)=0
size_type size() const
Number of elements in enumerator.
reference initial()
Reference to first element in enumerator.
reference back()
Reference to last element in enumerator.
Generic random access enumerator.
bool valid() const
Returns false if enumeration is complete.
std::unique_ptr< RandomAccessEnumeratorBase< ValueType, IsConst > > ptr
RandomAccessEnumerator(std::unique_ptr< RandomAccessEnumeratorBase< ValueType, IsConst > > e)
RandomAccessEnumerator(const RandomAccessEnumerator &)=delete
RandomAccessEnumeratorBase< ValueType, IsConst >::size_type size_type
RandomAccessEnumeratorBase< ValueType, IsConst >::step_type step_type
RandomAccessEnumeratorBase< ValueType, IsConst >::reference reference
std::reverse_iterator< iterator > reverse_iterator
reference current() const
Access the current ObjectType by reference.
std::string name() const
Derived enumerators must implement name.
RandomAccessEnumerator & operator=(const RandomAccessEnumerator &)=delete
jsonParser source(step_type step) const
step_type step() const
Increments with each enumerated object.
RandomAccessEnumIterator< ValueType, IsConst > iterator
EnumeratorBase * _enum_ptr()
Definition: Enumerator.hh:290
ValEnumerator< ValueType, IsConst >::reference reference
Definition: Enumerator.hh:310
void _assert_ptr() const
Definition: Enumerator.hh:262
void _set_current_ptr(CASM_TMP::ConstSwitch< IsConst, value_type > *_new)
Change the pointer.
Definition: Enumerator.hh:215
reference current() const
Access the current ObjectType by reference.
Definition: Enumerator.hh:211
void _invalidate()
Call if enumeration complete.
Definition: Enumerator.hh:159
step_type step() const
Increments with each enumerated object.
Definition: Enumerator.hh:115
CASM_TMP::ConstSwitch< IsConst, ValueType > & reference
Definition: Enumerator.hh:174
void _set_step(step_type val)
Set current step value.
Definition: Enumerator.hh:150
A 'cloneable_ptr' can be used in place of 'unique_ptr'.
typename std::conditional< IsConst, const T, T >::type ConstSwitch
Definition: CASM_TMP.hh:78
Main CASM namespace.
Definition: APICommand.hh:8
Non-std smart pointer classes and functions.