CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Counter.hh
Go to the documentation of this file.
1 #ifndef COUNTER_HH
2 #define COUNTER_HH
3 
5 
6 namespace CASM {
7 
16  template<typename _Container, typename _value_type, typename _size_type, typename _Access, typename _Compare>
71  class Counter;
72 
73  namespace CASM_TMP {
74  template<typename _Container, typename _value_type, typename _size_type, typename _Access, typename _Compare>
75  struct traits<Counter<_Container, _value_type, _size_type, _Access, _Compare> > {
77  typedef _Container Container;
78  typedef _value_type value_type;
79  typedef _size_type size_type;
80  typedef _Access Access;
81  typedef _Compare Compare;
82  };
83  }
84 
85  template < typename _Container,
86  typename _value_type = typename _Container::value_type,
87  typename _size_type = typename _Container::size_type,
89  typename _Compare = CASM_TMP::MuchLessThan<_value_type> >
90  class Counter : public BaseCounter<Counter<_Container, _value_type, _size_type, _Access, _Compare> > {
92  using Base::_valid;
93  using Base::_current;
94  using Base::_increment;
95  using Base::_upper;
96  using Base::_lower;
97  using Base::_initial;
98  using Base::_final;
99  public:
100  typedef _Container Container;
101  typedef _value_type value_type;
102  typedef _size_type size_type;
103  typedef _Access Access;
104  typedef _Compare Compare;
105 
106  using Base::valid;
107  using Base::initial;
108  using Base::current;
109  using Base::size;
110  using Base::compare;
111  using Base::operator();
112  using Base::operator[];
113  using Base::operator bool;
114 
116  Counter() {}
117 
118 
124  Counter(const Container &_initial,
125  const Container &_final,
126  const Container &_increment,
127  Access _access = Access(),
128  Compare _compare = Compare()) :
129  Base(_initial, _final, _increment, _access, _compare) {
130  _init();
131  }
132 
138  if(!valid())
139  return *this;
140  size_type i = 0;
141  while(compare(_upper(i), _current(i) + _increment(i)) ||
142  compare(_current(i) + _increment(i), _lower(i))) {
143  _current(i) = _initial(i);
144  i++;
145  if(i == this->size()) {
146  _valid() = false;
147  return *this;
148  }
149  }
150  _current(i) += _increment(i);
151  return *this;
152  }
153 
154  void operator++(int) {
155  ++(*this);
156  }
157 
158  void set_current(const Container &new_current) {
159  if(current().size() != new_current.size()) {
160  std::cerr << "CRITICAL ERROR: In IsoCounter::set_current(), new state is incompatible with this counter.\n"
161  << " Exiting...\n";
162  exit(1);
163  }
164  _current() = new_current;
166  }
167 
168 
170  void reset() {
171  _current() = initial();
172  _valid() = size() != 0;
173  };
174 
175  private:
177  void _init() {
178  for(size_type i = 0; i < initial().size(); i++) {
179  if(compare(_initial(i), _final(i))) {
180  _lower(i) = _initial(i);
181  _upper(i) = _final(i);
182  }
183  else {
184  _lower(i) = _final(i);
185  _upper(i) = _initial(i);
186  }
187  }
188  }
189 
191  _valid() = true;
192  for(size_type i = 0; i < size() && valid(); i++) {
193  _valid() = valid()
194  && compare(_current(i), _upper(i))
195  && compare(_lower(i), _current(i));
196  }
197  return valid();
198  }
199 
200  };
201 
202 
203  template <typename EigenType>
204  using EigenCounter = Counter< EigenType, typename EigenType::Scalar, typename EigenType::Index,
206 
225 }
226 
227 
228 #endif
Counter< EigenType, typename EigenType::Scalar, typename EigenType::Index, CASM_TMP::ParenthesesAccess< EigenType, typename EigenType::Scalar, typename EigenType::Index > > EigenCounter
Definition: Counter.hh:205
CASM_TMP::traits< Counter >::Base Base
Definition: Counter.hh:91
A Counter allows looping over many incrementing variables in one loop.
Definition: Counter.hh:71
bool compare(const T &A, const T &B, double tol)
Floating point comparison with tol, return A < B.
Definition: CASM_math.hh:89
EigenCounter< Eigen::Matrix3i > EigenMatrix3iCounter
Counter for Eigen::MatrixXd.
Definition: Counter.hh:222
Counter & operator++()
Definition: Counter.hh:137
Main CASM namespace.
Definition: complete.cpp:8
_value_type value_type
Definition: Counter.hh:101
BaseCounter< Counter< _Container, _value_type, _size_type, _Access, _Compare > > Base
Definition: Counter.hh:76
EigenCounter< Eigen::Matrix3d > EigenMatrix3dCounter
Counter for Eigen::Matrix3d.
Definition: Counter.hh:212
EigenCounter< Eigen::MatrixXi > EigenMatrixXiCounter
Counter for Eigen::MatrixXd.
Definition: Counter.hh:220
_Compare Compare
Definition: Counter.hh:104
Helper Functor for Counter container access using operator()
Definition: CASM_TMP.hh:180
void set_current(const Container &new_current)
Definition: Counter.hh:158
EigenCounter< Eigen::VectorXi > EigenVectorXiCounter
Counter for Eigen::VectorXi.
Definition: Counter.hh:218
_Container Container
Definition: Counter.hh:100
EigenIndex Index
For long integer indexing:
A Counter allows looping over many incrementing variables in one loop.
Definition: BaseCounter.hh:114
void operator++(int)
Definition: Counter.hh:154
Counter()
Default construct a Counter.
Definition: Counter.hh:116
bool _compute_validity()
Definition: Counter.hh:190
Helper Functor for Counter container access using operator[].
Definition: CASM_TMP.hh:125
EigenCounter< Eigen::Vector3d > EigenVector3dCounter
Counter for Eigen::Vector3d.
Definition: Counter.hh:214
_Access Access
Definition: Counter.hh:103
EigenCounter< Eigen::Vector3i > EigenVector3iCounter
Counter for Eigen::Vector3i.
Definition: Counter.hh:216
void _init()
Called from the constructor to set m_lower and m_upper appropriately.
Definition: Counter.hh:177
EigenCounter< Eigen::MatrixXd > EigenMatrixXdCounter
Counter for Eigen::MatrixXd.
Definition: Counter.hh:210
Counter(const Container &_initial, const Container &_final, const Container &_increment, Access _access=Access(), Compare _compare=Compare())
Construct a Counter-type object.
Definition: Counter.hh:124
EigenCounter< Eigen::VectorXd > EigenVectorXdCounter
Counter for Eigen::VectorXd.
Definition: Counter.hh:208
_size_type size_type
Definition: Counter.hh:102
void reset()
Reset the current value of the Counter to the initial value.
Definition: Counter.hh:170
typename std::conditional< boost::is_integral< T >::value, IntegralLessThan< T >, FloatingPointLessThan< T > >::type MuchLessThan
Definition: CASM_TMP.hh:117