CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Selection.hh
Go to the documentation of this file.
1 #ifndef CASM_Selection
2 #define CASM_Selection
3 
4 #include <boost/filesystem.hpp>
5 #include <boost/iterator/iterator_facade.hpp>
6 #include <boost/range/iterator_range.hpp>
7 #include <map>
8 
12 #include "casm/misc/CASM_TMP.hh"
13 
14 namespace CASM {
15 namespace DB {
17 
19 } // namespace DB
20 template <>
21 inline std::string singleline_enum_help<DB::SELECTION_TYPE>() {
22  return standard_singleline_enum_help<DB::SELECTION_TYPE>(
24 }
26 } // namespace CASM
27 
28 namespace CASM {
29 
30 class PrimClex;
31 class jsonParser;
32 
33 namespace DB {
34 template <typename ValueType>
35 class Database;
36 
37 template <typename ValueType>
38 class Database;
39 
40 template <typename ObjType>
41 class Selection;
42 
43 template <typename ObjType, typename BaseIterator>
45  : public boost::iterator_facade<SelectionIterator<ObjType, BaseIterator>,
46  ObjType, std::bidirectional_iterator_tag,
47  const ObjType &, long> {
48  public:
49  typedef typename CASM_TMP::ConstSwitch<
50  std::is_same<BaseIterator,
51  std::map<std::string, bool>::const_iterator>::value,
52  bool>
54 
57 
59  std::string name() const;
60 
63 
65  bool is_selected() const;
66 
67  private:
68  friend boost::iterator_core_access;
69 
71 
73  SelectionIterator(const Selection<ObjType> &_list, BaseIterator _it,
74  bool _selected_only);
75 
77  void increment();
78 
80  void decrement();
81 
83  const ObjType &dereference() const;
84 
86  bool equal(const SelectionIterator &B) const;
87 
88  /*
89  long distance_to(const SelectionIterator &B) const {
90  Do not define this or boost::iterator_range<T>::size will compile but
91  cause runtime errors. Use boost::distance instead.
92  }
93  */
94 
96  BaseIterator m_it;
98 };
99 
112 template <typename ObjType>
113 class Selection {
114  public:
115  struct Compare {
116  bool operator()(std::string A, std::string B) const {
117  return traits<ObjType>::name_compare(A, B);
118  }
119  };
120 
121  typedef std::map<std::string, bool, Compare> map_type;
122  typedef typename map_type::iterator base_iterator;
123  typedef typename map_type::const_iterator base_const_iterator;
126  typedef Index size_type;
127 
129  Selection();
130 
132  Selection(const PrimClex &_primclex, fs::path selection_path = "MASTER");
133 
135  Selection(Database<ObjType> &_db, fs::path selection_path = "MASTER");
136 
137  const PrimClex &primclex() const;
138 
139  Database<ObjType> &db() const;
140 
142 
144 
146 
148 
149  map_type &data();
150 
151  const map_type &data() const;
152 
153  Index size() const;
154 
155  Index selected_size() const;
156 
157  const std::vector<std::string> &col_headers() const;
158 
159  const std::string &name() const;
160 
162  bool is_selected(const std::string &name_or_alias) const;
163 
165  void set(const DataFormatterDictionary<ObjType> &dict,
166  const std::string &criteria);
167 
169  void set(const DataFormatterDictionary<ObjType> &dict,
170  const std::string &criteria, bool selected);
171 
172  void read(std::istream &_input);
173 
174  void print(const DataFormatterDictionary<ObjType> &_dict, std::ostream &_out,
175  bool only_selected = false) const;
176 
177  const jsonParser &from_json(const jsonParser &_json);
178 
180  jsonParser &_json, bool only_selected = false) const;
181 
182  void write(const DataFormatterDictionary<ObjType> &dict,
183  const fs::path &out_path, bool write_json,
184  bool only_selected) const;
185 
186  private:
189 
190  // first will only be 'name', no matter whether 'name' or 'alias' is
191  // written in the selection file
193 
194  std::vector<std::string> m_col_headers;
195  std::string m_name;
196 };
197 
198 } // namespace DB
199 } // namespace CASM
200 
201 #endif
#define ENUM_TRAITS(ENUM)
Definition: io_traits.hh:213
#define ENUM_IO_DECL(ENUM)
Definition: stream_io.hh:8
void set(const DataFormatterDictionary< ObjType > &dict, const std::string &criteria)
Set selected objects to value of criteria.
Definition: Selection.cc:291
boost::iterator_range< iterator > selected()
Definition: Selection.cc:237
void write(const DataFormatterDictionary< ObjType > &dict, const fs::path &out_path, bool write_json, bool only_selected) const
Write selection to file – will overwrite.
Definition: Selection.cc:487
void print(const DataFormatterDictionary< ObjType > &_dict, std::ostream &_out, bool only_selected=false) const
Print csv selection to stream.
Definition: Selection.cc:470
std::map< std::string, bool, Compare > map_type
Definition: Selection.hh:121
Selection()
Default construct into invalid state.
Definition: Selection.cc:139
Index selected_size() const
Definition: Selection.cc:275
map_type & data()
Definition: Selection.cc:250
const PrimClex & primclex() const
Definition: Selection.cc:207
bool is_selected(const std::string &name_or_alias) const
True if obj is in Selection and is selected; false otherwise.
Definition: Selection.cc:281
const jsonParser & from_json(const jsonParser &_json)
Read selection from JSON.
Definition: Selection.cc:393
Index size() const
Definition: Selection.cc:260
SelectionIterator< ObjType, base_iterator > iterator
Definition: Selection.hh:124
SelectionIterator< ObjType, base_const_iterator > const_iterator
Definition: Selection.hh:125
boost::iterator_range< iterator > all()
Definition: Selection.cc:222
jsonParser & to_json(const DataFormatterDictionary< ObjType > &_dict, jsonParser &_json, bool only_selected=false) const
Write selection to JSON.
Definition: Selection.cc:448
map_type::iterator base_iterator
Definition: Selection.hh:122
Database< ObjType > * m_db
Definition: Selection.hh:187
std::string m_name
Definition: Selection.hh:195
std::vector< std::string > m_col_headers
Definition: Selection.hh:194
const std::vector< std::string > & col_headers() const
Definition: Selection.cc:265
map_type::const_iterator base_const_iterator
Definition: Selection.hh:123
const PrimClex * m_primclex
Definition: Selection.hh:188
const std::string & name() const
Definition: Selection.cc:270
void read(std::istream &_input)
Read csv selection from stream.
Definition: Selection.cc:365
Database< ObjType > & db() const
Definition: Selection.cc:212
void decrement()
boost::iterator_facade implementation
Definition: Selection.cc:114
std::string name() const
Name of object the iterator points at.
Definition: Selection.cc:74
bool equal(const SelectionIterator &B) const
boost::iterator_facade implementation
Definition: Selection.cc:130
bool_type & is_selected()
Reference to value 'is_selected'.
Definition: Selection.cc:81
void increment()
boost::iterator_facade implementation
Definition: Selection.cc:104
CASM_TMP::ConstSwitch< std::is_same< BaseIterator, std::map< std::string, bool >::const_iterator >::value, bool > bool_type
Definition: Selection.hh:53
const ObjType & dereference() const
boost::iterator_facade implementation
Definition: Selection.cc:124
SelectionIterator()
Default constructor (equals end)
Definition: Selection.hh:56
const Selection< ObjType > * m_list
Definition: Selection.hh:95
Parsing dictionary for constructing a DataFormatter<DataObject> object.
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
typename std::conditional< IsConst, const T, T >::type ConstSwitch
Definition: CASM_TMP.hh:78
SELECTION_TYPE
Definition: Selection.hh:16
Main CASM namespace.
Definition: APICommand.hh:8
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
bool operator()(std::string A, std::string B) const
Definition: Selection.hh:116