CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Counter.hh
Go to the documentation of this file.
1 #ifndef COUNTER_HH
2 #define COUNTER_HH
3 
4 #include <iostream>
5 
7 #include "casm/global/eigen.hh"
8 #include "casm/misc/CASM_math.hh"
9 
10 namespace CASM {
11 
83 template <typename _Container, typename _value_type, typename _size_type,
84  typename _Access, typename _Compare>
85 class Counter;
86 
87 template <typename _Container, typename _value_type, typename _size_type,
88  typename _Access, typename _Compare>
89 struct traits<
90  Counter<_Container, _value_type, _size_type, _Access, _Compare> > {
91  typedef BaseCounter<
94  typedef _Container Container;
95  typedef _value_type value_type;
96  typedef _size_type size_type;
97  typedef _Access Access;
98  typedef _Compare Compare;
99 };
100 
101 template <typename _Container,
102  typename _value_type = typename _Container::value_type,
103  typename _size_type = typename _Container::size_type,
104  typename _Access =
106  typename _Compare = CASM_TMP::MuchLessThan<_value_type> >
107 class Counter
108  : public BaseCounter<
109  Counter<_Container, _value_type, _size_type, _Access, _Compare> > {
110  typedef typename traits<Counter>::Base Base;
111  using Base::_current;
112  using Base::_final;
113  using Base::_increment;
114  using Base::_initial;
115  using Base::_lower;
116  using Base::_upper;
117  using Base::_valid;
118 
119  public:
120  typedef _Container Container;
121  typedef _value_type value_type;
122  typedef _size_type size_type;
123  typedef _Access Access;
124  typedef _Compare Compare;
125 
126  using Base::compare;
127  using Base::current;
128  using Base::initial;
129  using Base::size;
130  using Base::valid;
131  using Base::operator();
132  using Base::operator[];
133  using Base::operator bool;
134 
136  Counter() {}
137 
144  const Container &_increment, Access _access = Access(),
146  : Base(_initial, _final, _increment, _access, _compare) {
147  _init();
148  }
149 
155  if (!valid()) return *this;
156  size_type i = 0;
157  while (compare(_upper(i), _current(i) + _increment(i)) ||
158  compare(_current(i) + _increment(i), _lower(i))) {
159  _current(i) = _initial(i);
160  i++;
161  if (i == this->size()) {
162  _valid() = false;
163  return *this;
164  }
165  }
166  _current(i) += _increment(i);
167  return *this;
168  }
169 
170  void operator++(int) { ++(*this); }
171 
172  void set_current(const Container &new_current) {
173  if (current().size() != new_current.size()) {
174  std::cerr << "CRITICAL ERROR: In IsoCounter::set_current(), new state is "
175  "incompatible with this counter.\n"
176  << " Exiting...\n";
177  exit(1);
178  }
179  _current() = new_current;
181  }
182 
184  void reset() {
185  _current() = initial();
186  _valid() = size() != 0;
187  };
188 
189  private:
192  void _init() {
193  for (size_type i = 0; i < initial().size(); i++) {
194  if (compare(_initial(i), _final(i))) {
195  _lower(i) = _initial(i);
196  _upper(i) = _final(i);
197  } else {
198  _lower(i) = _final(i);
199  _upper(i) = _initial(i);
200  }
201  }
202  }
203 
205  _valid() = true;
206  for (size_type i = 0; i < size() && valid(); i++) {
207  _valid() = valid() && compare(_current(i), _upper(i)) &&
208  compare(_lower(i), _current(i));
209  }
210  return valid();
211  }
212 };
213 
214 template <typename EigenType>
216  Counter<EigenType, typename EigenType::Scalar, typename EigenType::Index,
217  CASM_TMP::ParenthesesAccess<EigenType, typename EigenType::Scalar,
218  typename EigenType::Index> >;
219 
238 } // namespace CASM
239 
240 #endif
A Counter allows looping over many incrementing variables in one loop.
Definition: BaseCounter.hh:125
A Counter allows looping over many incrementing variables in one loop.
Definition: Counter.hh:109
_Access Access
Definition: Counter.hh:123
void _init()
Called from the constructor to set m_lower and m_upper appropriately.
Definition: Counter.hh:192
_Compare Compare
Definition: Counter.hh:124
Counter()
Default construct a Counter.
Definition: Counter.hh:136
void reset()
Reset the current value of the Counter to the initial value.
Definition: Counter.hh:184
_Container Container
Definition: Counter.hh:120
Counter & operator++()
Definition: Counter.hh:154
traits< Counter >::Base Base
Definition: Counter.hh:110
_size_type size_type
Definition: Counter.hh:122
bool _compute_validity()
Definition: Counter.hh:204
Counter(const Container &_initial, const Container &_final, const Container &_increment, Access _access=Access(), Compare _compare=Compare())
Construct a Counter-type object.
Definition: Counter.hh:143
_value_type value_type
Definition: Counter.hh:121
void operator++(int)
Definition: Counter.hh:170
void set_current(const Container &new_current)
Definition: Counter.hh:172
EigenCounter< Eigen::Vector3i > EigenVector3iCounter
Counter for Eigen::Vector3i.
Definition: Counter.hh:229
EigenCounter< Eigen::MatrixXd > EigenMatrixXdCounter
Counter for Eigen::MatrixXd.
Definition: Counter.hh:223
EigenCounter< Eigen::MatrixXi > EigenMatrixXiCounter
Counter for Eigen::MatrixXd.
Definition: Counter.hh:233
EigenCounter< Eigen::VectorXi > EigenVectorXiCounter
Counter for Eigen::VectorXi.
Definition: Counter.hh:231
EigenCounter< Eigen::VectorXd > EigenVectorXdCounter
Counter for Eigen::VectorXd.
Definition: Counter.hh:221
EigenCounter< Eigen::Vector3d > EigenVector3dCounter
Counter for Eigen::Vector3d.
Definition: Counter.hh:227
EigenCounter< Eigen::Matrix3d > EigenMatrix3dCounter
Counter for Eigen::Matrix3d.
Definition: Counter.hh:225
EigenCounter< Eigen::Matrix3i > EigenMatrix3iCounter
Counter for Eigen::MatrixXd.
Definition: Counter.hh:235
typename std::conditional< std::is_integral< T >::value, IntegralLessThan< T >, FloatingPointLessThan< T > >::type MuchLessThan
Definition: CASM_math.hh:74
Main CASM namespace.
Definition: APICommand.hh:8
bool compare(ClusterInvariants const &A, ClusterInvariants const &B, double tol)
Compare ClusterInvariants.
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
static bool _compare(T const &a, T const &b, double tol)
Helper Functor for Counter container access using operator[].
Definition: CASM_TMP.hh:110
Helper Functor for Counter container access using operator()
Definition: CASM_TMP.hh:165
BaseCounter< Counter< _Container, _value_type, _size_type, _Access, _Compare > > Base
Definition: Counter.hh:93