CASM  1.1.0
A Clusters Approach to Statistical Mechanics
PropertiesDatabase.hh
Go to the documentation of this file.
1 #ifndef CASM_PropertiesDatabase
2 #define CASM_PropertiesDatabase
3 
4 #include <boost/iterator/iterator_facade.hpp>
5 #include <set>
6 #include <string>
7 
10 
11 namespace CASM {
12 struct MappedProperties;
13 
14 namespace DB {
15 
16 class PropertiesDatabaseIterator;
17 
54 
55  public:
57  typedef const value_type &reference;
58 
61 
62  std::unique_ptr<PropertiesDatabaseIteratorBase> clone() const {
63  return std::unique_ptr<PropertiesDatabaseIteratorBase>(this->_clone());
64  }
65 
66  private:
67  virtual void increment() = 0;
68 
69  virtual reference dereference() const = 0;
70 
71  virtual bool equal(const PropertiesDatabaseIteratorBase &other) const = 0;
72 
73  virtual PropertiesDatabaseIteratorBase *_clone() const = 0;
74 
75  virtual long distance_to(
76  const PropertiesDatabaseIteratorBase &other) const = 0;
77 };
78 
112 
114 
115  public boost::iterator_facade<PropertiesDatabaseIterator, MappedProperties,
116  std::forward_iterator_tag,
117  const MappedProperties &, long> {
118  public:
121 
124  : m_ptr(notstd::clone(it)) {}
125 
126  PropertiesDatabaseIteratorBase *get() const { return m_ptr.unique().get(); }
127 
128  private:
129  friend boost::iterator_core_access;
130 
132  void increment() { m_ptr->increment(); }
133 
135  const MappedProperties &dereference() const { return m_ptr->dereference(); }
136 
138  bool equal(const PropertiesDatabaseIterator &B) const {
139  return m_ptr->equal(*(B.m_ptr));
140  }
141 
143  return m_ptr->distance_to(*(B.m_ptr));
144  }
145 
147 };
148 
191  public:
195  class Compare {
196  public:
197  Compare(const PropertiesDatabase *_map, std::string _to_configname,
198  const ScoreMappedProperties &_score)
199  : m_map(_map), m_to(_to_configname), m_score(_score) {}
200 
203  bool operator()(const std::string &origin_A,
204  const std::string &origin_B) const;
205 
206  const ScoreMappedProperties &score_method() const { return m_score; }
207 
208  private:
210  std::string m_to;
212  };
213 
216  typedef Index size_type;
217 
218  PropertiesDatabase(const PrimClex &_primclex) : DatabaseBase(_primclex) {}
219 
221  virtual iterator begin() const = 0;
222 
224  virtual iterator end() const = 0;
225 
226  virtual size_type size() const = 0;
227  bool empty() const { return this->size() == 0; }
228 
233  virtual iterator find_via_to(std::string to_configname) const = 0;
234 
236  virtual iterator find_via_origin(std::string origin) const = 0;
237 
241  virtual std::set<std::string, Compare> all_origins(
242  std::string to_configname) const = 0;
243 
245  virtual void set_score_method(std::string to_configname,
246  const ScoreMappedProperties &score) = 0;
247 
250  return all_origins(to_configname).value_comp().score_method();
251  }
252 
254  double best_score(std::string to_configname) const {
255  return score(*find_via_to(to_configname));
256  }
257 
259  double score(std::string origin) const {
260  return score(*find_via_origin(origin));
261  }
262 
264  double score(const MappedProperties &value) const {
265  return score_method(value.to)(value);
266  }
267 
269  std::pair<iterator, bool> insert(const MappedProperties &value);
270 
272  iterator erase(iterator pos);
273 
275  size_type erase_via_origin(std::string origin) {
276  auto it = find_via_origin(origin);
277  if (it == end()) {
278  return 0;
279  }
280  erase(it);
281  return 1;
282  }
283 
284  private:
286  virtual std::pair<iterator, bool> _insert(const MappedProperties &value) = 0;
287 
289  virtual iterator _erase(iterator pos) = 0;
290 
293  virtual void _set_all_origins(std::string to_configname,
294  const std::set<std::string, Compare> &_set) = 0;
295 };
296 
297 } // namespace DB
298 } // namespace CASM
299 
300 #endif
Fully generic database interface for use by DatabaseHandler.
Definition: Database.hh:25
Compare type for std::set<std::string, Compare>, which sorts to determine the 'origin' configname for...
bool operator()(const std::string &origin_A, const std::string &origin_B) const
Compare mapped properties 'origin_A' and 'origin_B', preferring self-mapped results.
const ScoreMappedProperties & score_method() const
Compare(const PropertiesDatabase *_map, std::string _to_configname, const ScoreMappedProperties &_score)
virtual std::set< std::string, Compare > all_origins(std::string to_configname) const =0
Names of all configurations that relaxed 'origin'->'to'.
double best_score(std::string to_configname) const
Best score of configurations that relaxed 'origin'->'to'.
PropertiesDatabaseIterator iterator
virtual std::pair< iterator, bool > _insert(const MappedProperties &value)=0
Private _insert MappedProperties, without modifying 'relaxed_from'.
size_type erase_via_origin(std::string origin)
Erase data.
virtual iterator begin() const =0
Begin iterator over data entries.
PropertiesDatabase(const PrimClex &_primclex)
virtual iterator end() const =0
End iterator over data entries.
virtual void _set_all_origins(std::string to_configname, const std::set< std::string, Compare > &_set)=0
Set sorted container of names of all configurations that relaxed 'from'->'to'.
double score(const MappedProperties &value) const
Score mapping 'from'->'to'.
double score(std::string origin) const
Score mapping 'origin'->'to'.
virtual iterator find_via_to(std::string to_configname) const =0
Return iterator to data entry that is the best mapping to specified config.
virtual iterator find_via_origin(std::string origin) const =0
Return iterator to data entry that is from the specified origin.
virtual void set_score_method(std::string to_configname, const ScoreMappedProperties &score)=0
Change the score method for a single configuration.
std::pair< iterator, bool > insert(const MappedProperties &value)
Insert data.
ScoreMappedProperties score_method(std::string to_configname) const
Name of ScoreMappedProperties method.
virtual size_type size() const =0
iterator erase(iterator pos)
Erase data.
virtual iterator _erase(iterator pos)=0
Private _erase MappedProperties, without modifying 'relaxed_from'.
std::unique_ptr< PropertiesDatabaseIteratorBase > clone() const
virtual PropertiesDatabaseIteratorBase * _clone() const =0
virtual bool equal(const PropertiesDatabaseIteratorBase &other) const =0
virtual reference dereference() const =0
virtual long distance_to(const PropertiesDatabaseIteratorBase &other) const =0
Wrapper class for specializations PropertiesDatabaseIteratorBase.
void increment()
boost::iterator_facade implementation
notstd::cloneable_ptr< PropertiesDatabaseIteratorBase > m_ptr
long distance_to(const PropertiesDatabaseIterator &B) const
PropertiesDatabaseIterator()
Default constructor.
PropertiesDatabaseIterator(const PropertiesDatabaseIteratorBase &it)
Construct iterator.
const MappedProperties & dereference() const
boost::iterator_facade implementation
bool equal(const PropertiesDatabaseIterator &B) const
boost::iterator_facade implementation
PropertiesDatabaseIteratorBase * get() const
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
A 'cloneable_ptr' can be used in place of 'unique_ptr'.
GenericDatumFormatter< std::string, Result > to_configname()
Definition: ConfigData.cc:92
Main CASM namespace.
Definition: APICommand.hh:8
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Non-std smart pointer classes and functions.
std::unique_ptr< T > clone(const T &obj)