CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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) {
35  return !((*this) == _it);
36  }
37 
39  /* //assume user doesn't need safeguards
40  if(m_counter_ptr == nullptr || m_ind < 0 || m_counter_ptr->size() <= m_ind)
41  return *this;
42  */
43 
44  ++m_sub_iter;
45  while(m_sub_iter == (*m_counter_ptr)[m_ind].value_end() && (m_ind + 1) < m_counter_ptr->size())
46  m_sub_iter = (*m_counter_ptr)[++m_ind].value_begin();
47 
48  return *this;
49  }
50 
52  CounterValueIterator t_it(*this);
53  ++(*this);
54  return t_it;
55  }
56 
57  const value_type &operator*() const {
58  return *m_sub_iter;
59  }
60  value_type const *operator->() const {
61  return &operator*();
62  }
63  /*
64  size_type index() {
65  return m_ind;
66  }
67  */
68  private:
69  CounterType const *m_counter_ptr;
70  size_type m_ind;
71  sub_iterator m_sub_iter;
72 
73 
74  };
75 
76 
80  template <typename CounterType>
81  class MultiCounter {
82 
83  protected:
84  bool m_is_valid;
86 
87  public:
88  typedef typename CounterType::value_type value_type;
89  typedef typename CounterType::size_type size_type;
91 
92  MultiCounter();
93  MultiCounter(const Array<CounterType> &_counter);
96  void operator++(int) {
97  ++(*this);
98  }
99  bool operator+=(Index steps);
100  bool valid()const;
101 
102  void reset();
103 
104  operator const Array<CounterType > &() const {
105  return m_counters;
106  }
107 
108  Index size() const {
109  return m_counters.size();
110  }
111 
112  const_value_iterator value_begin() const {
113  if(size() != 0)
114  return const_value_iterator(this, 0, m_counters[0].value_begin());
115  else
116  return const_value_iterator(this);
117  }
118 
119  const_value_iterator value_end() const {
120  if(size() != 0)
121  return const_value_iterator(this, size() - 1, m_counters[size() - 1].value_end());
122  else
123  return const_value_iterator(this);
124  }
125 
126  void push_back(const CounterType &new_counter) {
127  m_counters.push_back(new_counter);
128  reset();
129  }
130 
131  CounterType &back() {
132  return m_counters.back();
133  }
134 
135  const CounterType &back() const {
136  return m_counters.back();
137  }
138 
139  CounterType &operator[](Index i) {
140  return m_counters[i];
141  }
142 
143  const CounterType &operator[](Index i) const {
144  return m_counters[i];
145  }
146 
147  const Array<CounterType> &current() const {
148  return m_counters;
149  }
150 
152  return m_counters;
153  }
154 
155  };
156 
157 
158  template <typename CounterType>
160  m_is_valid = false;
161  }
162 
163  template <typename CounterType>
165  m_counters = _counters;
166  reset();
167 
168  }
169 
170  template <typename CounterType>
172  m_is_valid = (size() != 0);
173  for(Index i = 0; i < size(); i++)
174  m_counters[i].reset();
175  }
176 
177  template <typename CounterType>
179  return m_is_valid;
180  }
181 
182 
183  template <typename CounterType>
185  Index index(0);
186 
187  while(valid() && index < size()) {
188  if(!(++m_counters[index]).valid()) {
189  index++;
190  continue;
191  }
192 
193  for(Index i = 0; i < index; i++) {
194  m_counters[i].reset();
195  }
196  break;
197  }
198 
199  if(index == size())
200  m_is_valid = false;
201 
202  return *this;
203  }
204 
205  template <typename CounterType>
207  for(Index i = 0; i < steps && m_is_valid; i++)
208  (*this)++;
209 
210  return m_is_valid;
211  }
212 
213  template <typename CounterType>
214  std::ostream &operator<<(std::ostream &stream, const MultiCounter<CounterType> &counter) {
215  return stream << counter();
216  }
217 
219 }
220 
221 
222 #endif
const Array< CounterType > & current() const
void push_back(const CounterType &new_counter)
CounterValueIterator< MultiCounter > const_value_iterator
Definition: MultiCounter.hh:90
Index size() const
Definition: Array.hh:145
const Array< CounterType > & operator()() const
Index size() const
void push_back(const T &toPush)
Definition: Array.hh:513
Main CASM namespace.
Definition: complete.cpp:8
CounterValueIterator(CounterType const *_counter_ptr=nullptr, size_type _ind=0, sub_iterator _sub_iter=sub_iterator())
Definition: MultiCounter.hh:24
const_value_iterator value_end() const
bool operator==(const CounterValueIterator &_it)
Definition: MultiCounter.hh:30
CounterType & operator[](Index i)
CounterType::size_type size_type
Definition: MultiCounter.hh:89
EigenIndex Index
For long integer indexing:
CounterType const * m_counter_ptr
Definition: BaseCounter.hh:49
bool valid() const
CounterType::value_type value_type
Definition: MultiCounter.hh:88
const CounterType & back() const
const value_type & operator*() const
Definition: BaseCounter.hh:41
Array< CounterType > m_counters
Definition: MultiCounter.hh:85
CounterType & back()
void operator++(int)
Definition: MultiCounter.hh:96
const CounterType & operator[](Index i) const
bool operator!=(const CounterValueIterator &_it)
Definition: MultiCounter.hh:34
bool operator+=(Index steps)
T & back()
Definition: Array.hh:177
const_value_iterator value_begin() const
MultiCounter & operator++()