CASM  1.1.0
A Clusters Approach to Statistical Mechanics
BaseCounter.hh
Go to the documentation of this file.
1 #ifndef BASECOUNTER_HH
2 #define BASECOUNTER_HH
3 
4 #include <iterator>
5 
6 #include "casm/misc/CASM_TMP.hh"
7 
8 namespace CASM {
9 
15 template <typename CounterType>
17  public:
18  typedef typename CounterType::value_type value_type;
21  typedef std::random_access_iterator_tag iterator_category;
22  typedef const value_type &reference;
23  typedef const value_type *pointer;
24 
25  CounterValueIterator(CounterType const *_counter_ptr = nullptr,
26  size_type _ind = 0)
27  : m_counter_ptr(_counter_ptr), m_ind(_ind) {}
28 
29  // explicit CounterValueIterator(const CounterValueIterator &_it) :
30  // m_counter_ptr(_it.m_counter_ptr),
31  // m_ind(_it.m_ind) {}
32 
33  bool operator==(const CounterValueIterator &_it) {
34  return (m_counter_ptr == _it.m_counter_ptr && m_ind == _it.m_ind);
35  }
36 
37  bool operator!=(const CounterValueIterator &_it) { return !((*this) == _it); }
38 
40  ++m_ind;
41  return *this;
42  }
43 
45  CounterValueIterator t_it(*this);
46  ++(*this);
47  return t_it;
48  }
49 
50  reference operator*() const { return (*m_counter_ptr)[m_ind]; }
51  pointer operator->() const { return &operator*(); }
52 
53  private:
54  CounterType const *m_counter_ptr;
56 };
57 
118 /*template<typename Container,
119  typename value_type = typename Container::value_type,
120  typename size_type = typename Container::size_type,
121  typename Access = CASM_TMP::BracketAccess<Container, value_type,
122  size_type>, typename Compare = CASM_TMP::MuchLessThan<value_type> >
123 */
124 template <typename DerivedCounter>
125 class BaseCounter {
126  public:
133 
136 
143  const Container &_increment, Access _access = Access(),
145  : m_access(_access),
148  m_final(_final),
150  m_lower(_initial),
151  m_upper(_initial),
153  m_valid(_initial.size() != 0) {
154  //_init();
155  }
156 
160  bool valid() const { return m_valid; };
161 
162  operator bool() const { return valid(); }
163 
168  operator const Container &() const { return m_current; }
169 
170  const value_type &operator[](size_type index) const {
171  return m_access(m_current, index);
172  };
173 
178  size_type size() const { return m_current.size(); }
179 
181  return const_value_iterator(static_cast<DerivedCounter const *>(this), 0);
182  }
183 
185  return const_value_iterator(static_cast<DerivedCounter const *>(this),
186  size());
187  }
188 
193  bool compare(const value_type &A, const value_type &B) {
194  return m_compare(A, B);
195  }
196 
201  const Container &current() const { return m_current; }
202 
207  const value_type &current(size_type index) const {
208  return m_access(m_current, index);
209  }
210 
215  const Container &operator()() const { return m_current; }
216 
221  const Container &initial() const { return m_initial; }
222 
227  const Container & final() const { return m_final; }
228 
233  const Container &increment() const { return m_increment; }
234 
235  protected:
237  bool &_valid() { return m_valid; }
238 
244  const value_type &_current(size_type index) const {
245  return m_access(m_current, index);
246  }
247 
253  value_type &_current(size_type index) { return m_access(m_current, index); }
254 
259  Container &_current() { return m_current; }
260 
266  const value_type &_initial(size_type index) const {
267  return m_access(m_initial, index);
268  }
269 
275  value_type &_initial(size_type index) { return m_access(m_initial, index); }
276 
282  const value_type &_final(size_type index) const {
283  return m_access(m_final, index);
284  }
285 
291  value_type &_final(size_type index) { return m_access(m_final, index); }
292 
298  const value_type &_increment(size_type index) const {
299  return m_access(m_increment, index);
300  }
301 
308  return m_access(m_increment, index);
309  }
310 
316  const value_type &_upper(size_type index) const {
317  return m_access(m_upper, index);
318  }
319 
325  value_type &_upper(size_type index) { return m_access(m_upper, index); }
326 
332  const value_type &_lower(size_type index) const {
333  return m_access(m_lower, index);
334  }
335 
341  value_type &_lower(size_type index) { return m_access(m_lower, index); }
342 
343  private:
347 
350 
353 
356 
359 
362 
365 
368 
371  bool m_valid;
372 };
373 
376 } // namespace CASM
377 
378 #endif
A Counter allows looping over many incrementing variables in one loop.
Definition: BaseCounter.hh:125
Container m_current
The current state of the container.
Definition: BaseCounter.hh:367
const value_type & _initial(size_type index) const
Definition: BaseCounter.hh:266
BaseCounter(const Container &_initial, const Container &_final, const Container &_increment, Access _access=Access(), Compare _compare=Compare())
Construct a Counter-type object.
Definition: BaseCounter.hh:142
CounterValueIterator< DerivedCounter > const_value_iterator
Definition: BaseCounter.hh:132
value_type & _final(size_type index)
Definition: BaseCounter.hh:291
value_type & _initial(size_type index)
Definition: BaseCounter.hh:275
value_type & _upper(size_type index)
Definition: BaseCounter.hh:325
Compare m_compare
A functor that enables custom comparison, by default: operator<.
Definition: BaseCounter.hh:349
traits< DerivedCounter >::Access Access
Definition: BaseCounter.hh:130
BaseCounter()
Default construct a Counter.
Definition: BaseCounter.hh:135
const value_type & _upper(size_type index) const
Definition: BaseCounter.hh:316
Container & _current()
Definition: BaseCounter.hh:259
size_type size() const
Definition: BaseCounter.hh:178
bool & _valid()
non-const reference bool validity flag
Definition: BaseCounter.hh:237
const value_type & operator[](size_type index) const
Definition: BaseCounter.hh:170
const Container & increment() const
Definition: BaseCounter.hh:233
const value_type & _final(size_type index) const
Definition: BaseCounter.hh:282
bool compare(const value_type &A, const value_type &B)
Definition: BaseCounter.hh:193
Container m_lower
The minimum of each value of m_initial and m_final.
Definition: BaseCounter.hh:361
Container m_increment
Amount to increment each container value.
Definition: BaseCounter.hh:358
const value_type & current(size_type index) const
Definition: BaseCounter.hh:207
traits< DerivedCounter >::size_type size_type
Definition: BaseCounter.hh:129
const value_type & _current(size_type index) const
Definition: BaseCounter.hh:244
traits< DerivedCounter >::value_type value_type
Definition: BaseCounter.hh:128
value_type & _current(size_type index)
Definition: BaseCounter.hh:253
value_type & _increment(size_type index)
Definition: BaseCounter.hh:307
value_type & _lower(size_type index)
Definition: BaseCounter.hh:341
const value_type & _increment(size_type index) const
Definition: BaseCounter.hh:298
Container m_initial
Initial container values.
Definition: BaseCounter.hh:352
bool valid() const
Definition: BaseCounter.hh:160
traits< DerivedCounter >::Container Container
Definition: BaseCounter.hh:127
const value_type & _lower(size_type index) const
Definition: BaseCounter.hh:332
traits< DerivedCounter >::Compare Compare
Definition: BaseCounter.hh:131
const Container & operator()() const
Definition: BaseCounter.hh:215
Container m_final
Final container values.
Definition: BaseCounter.hh:355
const_value_iterator value_begin() const
Definition: BaseCounter.hh:180
const_value_iterator value_end() const
Definition: BaseCounter.hh:184
const Container & current() const
Definition: BaseCounter.hh:201
Container m_upper
The maximum of each value of m_initial and m_final.
Definition: BaseCounter.hh:364
const Container & initial() const
Definition: BaseCounter.hh:221
pointer operator->() const
Definition: BaseCounter.hh:51
CounterValueIterator(CounterType const *_counter_ptr=nullptr, size_type _ind=0)
Definition: BaseCounter.hh:25
CounterValueIterator operator++(int)
Definition: BaseCounter.hh:44
bool operator!=(const CounterValueIterator &_it)
Definition: BaseCounter.hh:37
reference operator*() const
Definition: BaseCounter.hh:50
CounterType::value_type value_type
Definition: BaseCounter.hh:18
CounterValueIterator & operator++()
Definition: BaseCounter.hh:39
bool operator==(const CounterValueIterator &_it)
Definition: BaseCounter.hh:33
CounterType const * m_counter_ptr
Definition: BaseCounter.hh:54
std::random_access_iterator_tag iterator_category
Definition: BaseCounter.hh:21
CounterType::size_type difference_type
Definition: BaseCounter.hh:19
const value_type & reference
Definition: BaseCounter.hh:22
CounterType::size_type size_type
Definition: BaseCounter.hh:20
const value_type * pointer
Definition: BaseCounter.hh:23
Main CASM namespace.
Definition: APICommand.hh:8
static bool _compare(T const &a, T const &b, double tol)