CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
ConfigSelection.hh
Go to the documentation of this file.
1 #ifndef ConfigSelection_HH
2 #define ConfigSelection_HH
3 
4 #include <limits>
7 #include "casm/clex/ConfigIO.hh"
8 #include "casm/clex/PrimClex.hh"
9 
10 namespace CASM {
11 
12  template <bool IsConst, bool IsConstIterator>
14 
15  template <bool IsConst>
16  class ConfigSelection;
17 
19 
25  template <bool IsConst, bool IsConstIterator>
26  class ConfigSelectionIterator : public std::iterator <std::bidirectional_iterator_tag,
27  typename std::conditional<IsConstIterator, const Configuration, CASM_TMP::ConstSwitch<IsConst, Configuration> >::type > {
28 
29  typedef std::iterator<std::bidirectional_iterator_tag,
30  typename std::conditional<IsConstIterator, const Configuration,
32 
33  public:
34 
35  typedef typename std::conditional<IsConstIterator,
36  std::map<std::string, bool>::const_iterator,
37  std::map<std::string, bool>::iterator>::type MapIterator;
39  typedef typename std_iterator_type::reference reference;
40  typedef typename std_iterator_type::pointer pointer;
41 
42 
44 
45  ConfigSelectionIterator(const MapIterator &it,
46  const MapIterator &begin,
47  const MapIterator &end,
48  PrimClexType *prim,
49  bool _selected_only = false);
50 
51  template<bool ArgIsConst, bool ArgIsConstIterator>
53 
54  bool selected() const;
55 
56  void set_selected(bool is_selected);
57 
58  const std::string &name() const;
59 
60  bool operator==(const ConfigSelectionIterator &_it) const;
61 
62  bool operator!=(const ConfigSelectionIterator &_it) const;
63 
64  reference operator*() const;
65 
66  pointer operator->() const;
67 
69 
71 
73 
75 
76  private:
77  friend class ConfigSelectionIterator < IsConst, !IsConstIterator >;
78  friend class ConfigSelection<IsConst>;
79 
80  PrimClexType *m_primclex;
81  MapIterator m_it;
82  MapIterator m_begin;
83  MapIterator m_end;
85  };
86 
87  template <bool IsConst>
88  class ConfigSelection {
89  public:
94 
95 
98 
100  //explicit ConfigSelection(PrimClexType &_primclex);
101 
114  ConfigSelection(PrimClexType &_primclex, const fs::path &selection_path = "MASTER");
115 
116  //ConfigSelection(const ConfigSelection &) =default;
117 
120  //swap(m_col_headers,RHS.m_col_headers);
121  //swap(m_config,RHS.m_config);
122  }
123 
124  ConfigSelection &operator=(const ConfigSelection &) = default;
125 
126  Index size() const {
127  return m_config.size();
128  }
129 
130  iterator find(const std::string &configname) {
131  return iterator(m_config.find(configname), m_config.begin(), m_config.end(), m_primclex);
132  }
133 
134  const_iterator find(const std::string &configname) const {
135  return const_iterator(m_config.find(configname), m_config.begin(), m_config.end(), m_primclex);
136  }
137 
138  void read(std::istream &_input);
139 
140  const jsonParser &from_json(const jsonParser &_json);
141 
143  jsonParser &_json,
144  bool only_selected = false) const;
145 
147  bool selected(const std::string &configname) const {
148  auto find_it = m_config.find(configname);
149  return find_it != m_config.end() && find_it->second;
150  }
151 
152  bool selected(const Configuration &config) const {
153  return selected(config.name());
154  }
155 
156  void set_selected(const std::string &configname, bool is_selected) {
157  m_config[configname] = is_selected;
158  }
159 
160  void set_selected(const Configuration &config, bool is_selected) {
161  set_selected(config.name(), is_selected);
162  }
163 
164  iterator config_begin() {
165  return iterator(m_config.begin(), m_config.begin(), m_config.end(), m_primclex);
166  }
167 
168  const_iterator config_begin() const {
169  return const_iterator(m_config.cbegin(), m_config.cbegin(), m_config.cend(), m_primclex);
170  }
171 
172  iterator config_end() {
173  return iterator(m_config.end(), m_config.begin(), m_config.end(), m_primclex);
174  }
175 
176  const_iterator config_end() const {
177  return const_iterator(m_config.cend(), m_config.cbegin(), m_config.cend(), m_primclex);
178  }
179 
180  const_iterator config_cbegin() const {
181  return const_iterator(m_config.cbegin(), m_config.cbegin(), m_config.cend(), m_primclex);
182  }
183 
184  const_iterator config_cend() const {
185  return const_iterator(m_config.cend(), m_config.cbegin(), m_config.cend(), m_primclex);
186  }
187 
189  return iterator(m_config.begin(), m_config.begin(), m_config.end(), m_primclex, true);
190  }
191 
192  const_iterator selected_config_begin() const {
193  return const_iterator(m_config.cbegin(), m_config.cbegin(), m_config.cend(), m_primclex, true);
194  }
195 
196  iterator selected_config_end() {
197  return iterator(m_config.end(), m_config.begin(), m_config.end(), m_primclex, true);
198  }
199 
200  const_iterator selected_config_end() const {
201  return const_iterator(m_config.cend(), m_config.cbegin(), m_config.cend(), m_primclex, true);
202  }
203 
204  const_iterator selected_config_cbegin() const {
205  return const_iterator(m_config.cbegin(), m_config.cbegin(), m_config.cend(), m_primclex, true);
206  }
207 
208  const_iterator selected_config_cend() const {
209  return const_iterator(m_config.cend(), m_config.cbegin(), m_config.cend(), m_primclex, true);
210  }
211 
212  std::pair<iterator, bool> insert(const std::pair<std::string, bool> &value) {
213  auto res = m_config.insert(value);
214  return std::make_pair(iterator(res.first, m_config.begin(), m_config.end(), m_primclex), res.second);
215  }
216 
217  const_iterator erase(const const_iterator &it) {
218  bool selected_only = it.m_selected_only;
219  return const_iterator(m_config.erase(it.m_it), m_config.cbegin(), m_config.cend(), m_primclex, selected_only);
220  }
221 
222  int erase(const std::string &configname) {
223  return m_config.erase(configname);
224  }
225 
226  const std::vector<std::string> &col_headers() const {
227  return m_col_headers;
228  }
229 
230  const std::string &name() const {
231  return m_name;
232  }
233 
235  std::ostream &_out,
236  bool only_selected = false) const;
237 
238  private:
239  friend class ConfigSelection < !IsConst >;
240 
241  PrimClexType *m_primclex;
242  std::string m_name;
243  std::map<std::string, bool> m_config;
244  std::vector<std::string> m_col_headers;
245 
246  };
247 
248  template<bool IsConst, bool IsConstIterator>
250 
251  template<bool IsConst, bool IsConstIterator>
257  bool _selected_only) :
258  m_primclex(prim), m_it(it), m_begin(begin), m_end(end), m_selected_only(_selected_only) {
259 
260  while(m_selected_only && m_begin != m_end && !m_begin->second)
261  ++m_begin;
262 
263  while(m_selected_only && m_it != m_end && !m_it->second)
264  ++m_it;
265  }
266 
267  /*
268  template<>
269  ConfigSelectionIterator<true, true>::ConfigSelectionIterator(const ConfigSelectionIterator<true, true> &iter) :
270  m_it(iter.m_it),
271  m_begin(iter.m_begin),
272  m_end(iter.m_end),
273  m_primclex(iter.m_primclex),
274  m_selected_only(iter.m_selected_only) {}
275 
276  template<>
277  ConfigSelectionIterator<true, true>::ConfigSelectionIterator(const ConfigSelectionIterator<true, false> &iter) :
278  m_it(iter.m_it),
279  m_begin(iter.m_begin),
280  m_end(iter.m_end),
281  m_primclex(iter.m_primclex),
282  m_selected_only(iter.m_selected_only) {}
283 
284  template<>
285  ConfigSelectionIterator<false, false>::ConfigSelectionIterator(const ConfigSelectionIterator<false, false> &iter) :
286  m_it(iter.m_it),
287  m_begin(iter.m_begin),
288  m_end(iter.m_end),
289  m_primclex(iter.m_primclex),
290  m_selected_only(iter.m_selected_only) {}
291 
292  template<>
293  ConfigSelectionIterator<false, true>::ConfigSelectionIterator(const ConfigSelectionIterator<false, false> &iter) :
294  m_it(iter.m_it),
295  m_begin(iter.m_begin),
296  m_end(iter.m_end),
297  m_primclex(iter.m_primclex),
298  m_selected_only(iter.m_selected_only) {}
299  */
300 
301  template<bool IsConst, bool IsConstIterator>
302  template<bool ArgIsConst, bool ArgIsConstIterator>
304  m_it(iter.m_it),
305  m_begin(iter.m_begin),
306  m_end(iter.m_end),
307  m_primclex(iter.m_primclex),
308  m_selected_only(iter.m_selected_only) {}
309 
310 
311  template<bool IsConst, bool IsConstIterator>
313  return m_it->second;
314  }
315 
316  template<bool IsConst, bool IsConstIterator>
318  m_it->second = is_selected;
319  }
320 
321  template<bool IsConst, bool IsConstIterator>
323  return m_it->first;
324  }
325 
326  template<bool IsConst, bool IsConstIterator>
328  return (m_it == _it.m_it);
329  }
330 
331  template<bool IsConst, bool IsConstIterator>
333  return !((*this) == _it);
334  }
335 
336  template<bool IsConst, bool IsConstIterator>
338  return m_primclex->configuration(m_it->first);
339  }
340 
341  template<bool IsConst, bool IsConstIterator>
343  return &(m_primclex->configuration(m_it->first));
344  }
345 
346  template<bool IsConst, bool IsConstIterator>
348  ++m_it;
349  while(m_selected_only && m_it != m_end && !selected())
350  ++m_it;
351  return (*this);
352  }
353 
354  template<bool IsConst, bool IsConstIterator>
356  ConfigSelectionIterator t_it(*this);
357  ++(*this);
358  return t_it;
359  }
360 
361  template<bool IsConst, bool IsConstIterator>
363  --m_it;
364  while(m_selected_only && m_it != m_begin && !selected())
365  --m_it;
366  return (*this);
367  }
368 
369  template<bool IsConst, bool IsConstIterator>
371  ConfigSelectionIterator t_it(*this);
372  --(*this);
373  return t_it;
374  }
375 
376 
377  template<bool IsConst>
378  std::ostream &operator<<(std::ostream &_stream, const ConfigSelection<IsConst> &selection) {
379  selection.print(_stream);
380  return _stream;
381  }
382 
383  bool get_selection(const Array<std::string> &criteria, const Configuration &config, bool is_selected);
384 
385  namespace ConfigSelection_impl {
386 
387  bool is_operator(const std::string &q);
388 
389  std::string operate(const std::string &q, const std::string &A);
390 
391  std::string operate(const std::string &q, const std::string &A, const std::string &B);
392 
393  bool is_unary(const std::string &q);
394 
395  std::string convert_variable(const std::string &q, const Configuration &config);
396  }
397 
399 }
401 #endif
const std::string & name() const
typename std::conditional< IsConst, const T, T >::type ConstSwitch
Definition: CASM_TMP.hh:93
bool get_selection(const Array< std::string > &criteria, const Configuration &config, bool is_selected)
ConfigSelection(const ConfigSelection< false > &RHS)
ConfigSelectionIterator & operator--()
std::iterator< std::bidirectional_iterator_tag, typename std::conditional< IsConstIterator, const Configuration, CASM_TMP::ConstSwitch< IsConst, Configuration > >::type > std_iterator_type
const_iterator selected_config_cbegin() const
std::map< std::string, bool > m_config
jsonParser & to_json(const DataFormatterDictionary< Configuration > &_dict, jsonParser &_json, bool only_selected=false) const
void print(const DataFormatterDictionary< Configuration > &_dict, std::ostream &_out, bool only_selected=false) const
const_iterator selected_config_begin() const
const_iterator config_cbegin() const
iterator selected_config_begin()
iterator find(const std::string &configname)
const_iterator config_end() const
void read(std::istream &_input)
std_iterator_type::reference reference
bool selected(const std::string &configname) const
check if configuration is selected (returns false if 'configname' cannot be found ...
std::string operate(const std::string &q, const std::string &A)
Main CASM namespace.
Definition: complete.cpp:8
bool selected(const Configuration &config) const
bool is_operator(const std::string &q)
const jsonParser & from_json(const jsonParser &_json)
PrimClexType * m_primclex
CASM_TMP::ConstSwitch< IsConst, PrimClex > PrimClexType
std_iterator_type::pointer pointer
void set_selected(const std::string &configname, bool is_selected)
std::conditional< IsConstIterator, std::map< std::string, bool >::const_iterator, std::map< std::string, bool >::iterator >::type MapIterator
void set_selected(bool is_selected)
CASM_TMP::ConstSwitch< IsConst, PrimClex > PrimClexType
const_iterator erase(const const_iterator &it)
EigenIndex Index
For long integer indexing:
const_iterator find(const std::string &configname) const
const_iterator config_cend() const
bool operator!=(const ConfigSelectionIterator &_it) const
ConfigSelection & operator=(const ConfigSelection &)=default
std::pair< iterator, bool > insert(const std::pair< std::string, bool > &value)
ConfigSelectionIterator< IsConst, true > const_iterator
const_iterator selected_config_cend() const
std::string name() const
SCELV_A_B_C_D_E_F/i.
ConfigIO::GenericConfigFormatter< std::string > configname()
Constructs DataFormmaterDictionary containing all Configuration DatumFormatters.
Definition: ConfigIO.cc:340
const_iterator config_begin() const
ConfigSelection()
Default constructor.
void set_selected(const Configuration &config, bool is_selected)
CASM_TMP::ConstSwitch< IsConst, Configuration > value_type
bool operator==(const ConfigSelectionIterator &_it) const
bool is_unary(const std::string &q)
ConfigSelection< true > ConstConfigSelection
const_iterator selected_config_end() const
int erase(const std::string &configname)
ConfigSelectionIterator & operator++()
std::string convert_variable(const std::string &q, const Configuration &config)
std::vector< std::string > m_col_headers
const std::vector< std::string > & col_headers() const
ConfigSelectionIterator< IsConst, false > iterator
Parsing dictionary for constructing a DataFormatter object.
A Configuration represents the values of all degrees of freedom in a Supercell.
const std::string & name() const