CASM  1.1.0
A Clusters Approach to Statistical Mechanics
MultiCounter.hh
Go to the documentation of this file.
1 #ifndef MULTICOUNTER_HH
2 #define MULTICOUNTER_HH
3 
5 
6 namespace CASM {
7 
13 template <typename SubCounterType>
14 class MultiCounter;
15 
16 template <typename SubCounterType>
17 class CounterValueIterator<MultiCounter<SubCounterType> > {
18  public:
22  typedef typename SubCounterType::const_value_iterator sub_iterator;
23 
24  CounterValueIterator(CounterType const *_counter_ptr = nullptr,
25  size_type _ind = 0,
26  sub_iterator _sub_iter = sub_iterator())
27  : m_counter_ptr(_counter_ptr), m_ind(_ind), m_sub_iter(_sub_iter) {}
28  // CounterValueIterator(const CounterValueIterator &_it);
29 
30  bool operator==(const CounterValueIterator &_it) {
31  return (m_counter_ptr == _it.m_counter_ptr && m_sub_iter == _it.m_sub_iter);
32  }
33 
34  bool operator!=(const CounterValueIterator &_it) { return !((*this) == _it); }
35 
37  /* //assume user doesn't need safeguards
38  if(m_counter_ptr == nullptr || m_ind < 0 || m_counter_ptr->size() <= m_ind)
39  return *this;
40  */
41 
42  ++m_sub_iter;
43  while (m_sub_iter == (*m_counter_ptr)[m_ind].value_end() &&
44  (m_ind + 1) < m_counter_ptr->size())
45  m_sub_iter = (*m_counter_ptr)[++m_ind].value_begin();
46 
47  return *this;
48  }
49 
51  CounterValueIterator t_it(*this);
52  ++(*this);
53  return t_it;
54  }
55 
56  const value_type &operator*() const { return *m_sub_iter; }
57  value_type const *operator->() const { return &operator*(); }
58  /*
59  size_type index() {
60  return m_ind;
61  }
62  */
63  private:
67 };
68 
74 template <typename CounterType>
75 class MultiCounter {
76  protected:
77  bool m_is_valid;
79 
80  public:
81  typedef typename CounterType::value_type value_type;
84 
85  MultiCounter();
86  MultiCounter(const Array<CounterType> &_counter);
89  void operator++(int) { ++(*this); }
90  bool operator+=(Index steps);
91  bool valid() const;
92 
93  void reset();
94 
95  operator const Array<CounterType> &() const { return m_counters; }
96 
97  Index size() const { return m_counters.size(); }
98 
100  if (size() != 0)
101  return const_value_iterator(this, 0, m_counters[0].value_begin());
102  else
103  return const_value_iterator(this);
104  }
105 
107  if (size() != 0)
108  return const_value_iterator(this, size() - 1,
109  m_counters[size() - 1].value_end());
110  else
111  return const_value_iterator(this);
112  }
113 
114  void push_back(const CounterType &new_counter) {
115  m_counters.push_back(new_counter);
116  reset();
117  }
118 
119  CounterType &back() { return m_counters.back(); }
120 
121  const CounterType &back() const { return m_counters.back(); }
122 
123  CounterType &operator[](Index i) { return m_counters[i]; }
124 
125  const CounterType &operator[](Index i) const { return m_counters[i]; }
126 
127  const Array<CounterType> &current() const { return m_counters; }
128 
129  const Array<CounterType> &operator()() const { return m_counters; }
130 };
131 
132 template <typename CounterType>
134  m_is_valid = false;
135 }
136 
137 template <typename CounterType>
139  m_counters = _counters;
140  reset();
141 }
142 
143 template <typename CounterType>
145  m_is_valid = (size() != 0);
146  for (Index i = 0; i < size(); i++) m_counters[i].reset();
147 }
148 
149 template <typename CounterType>
151  return m_is_valid;
152 }
153 
154 template <typename CounterType>
156  Index index(0);
157 
158  while (valid() && index < size()) {
159  if (!(++m_counters[index]).valid()) {
160  index++;
161  continue;
162  }
163 
164  for (Index i = 0; i < index; i++) {
165  m_counters[i].reset();
166  }
167  break;
168  }
169 
170  if (index == size()) m_is_valid = false;
171 
172  return *this;
173 }
174 
175 template <typename CounterType>
177  for (Index i = 0; i < steps && m_is_valid; i++) (*this)++;
178 
179  return m_is_valid;
180 }
181 
182 template <typename CounterType>
183 std::ostream &operator<<(std::ostream &stream,
184  const MultiCounter<CounterType> &counter) {
185  return stream << counter();
186 }
187 
189 } // namespace CASM
190 
191 #endif
T & back()
Definition: Array.hh:160
Index size() const
Definition: Array.hh:131
bool operator!=(const CounterValueIterator &_it)
Definition: MultiCounter.hh:34
bool operator==(const CounterValueIterator &_it)
Definition: MultiCounter.hh:30
CounterValueIterator(CounterType const *_counter_ptr=nullptr, size_type _ind=0, sub_iterator _sub_iter=sub_iterator())
Definition: MultiCounter.hh:24
reference operator*() const
Definition: BaseCounter.hh:50
CounterType const * m_counter_ptr
Definition: BaseCounter.hh:54
const CounterType & back() const
bool valid() const
CounterType & back()
const Array< CounterType > & current() const
const_value_iterator value_end() const
const CounterType & operator[](Index i) const
bool operator+=(Index steps)
CounterType & operator[](Index i)
Array< CounterType > m_counters
Definition: MultiCounter.hh:78
CounterType::size_type size_type
Definition: MultiCounter.hh:82
void push_back(const CounterType &new_counter)
CounterValueIterator< MultiCounter > const_value_iterator
Definition: MultiCounter.hh:83
Index size() const
Definition: MultiCounter.hh:97
const_value_iterator value_begin() const
Definition: MultiCounter.hh:99
MultiCounter & operator++()
CounterType::value_type value_type
Definition: MultiCounter.hh:81
void operator++(int)
Definition: MultiCounter.hh:89
const Array< CounterType > & operator()() const
void push_back(const T &toPush)
Definition: Array.hh:431
Main CASM namespace.
Definition: APICommand.hh:8
std::ostream & operator<<(std::ostream &_stream, const FormattedPrintable &_formatted)
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39