CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Database.hh
Go to the documentation of this file.
1 #ifndef CASM_Database
2 #define CASM_Database
3 
4 #include <algorithm>
5 #include <boost/iterator/iterator_facade.hpp>
6 #include <iterator>
7 #include <map>
8 #include <memory>
9 
10 #include "casm/clex/HasPrimClex.hh"
14 
15 namespace CASM {
16 
17 class PrimClex;
18 
19 namespace DB {
20 
21 template <typename ObjType>
22 class Selection;
23 
25 class DatabaseBase : public HasPrimClex<CRTPBase<DatabaseBase>> {
26  public:
27  DatabaseBase(const PrimClex &_primclex) : m_primclex(&_primclex) {}
28 
29  virtual ~DatabaseBase() {}
30 
31  virtual DatabaseBase &open() = 0;
32  virtual void commit() = 0;
33  virtual void close() = 0;
34 
35  const PrimClex &primclex() const { return *m_primclex; }
36 
37  private:
39 };
40 
41 template <typename ValueType>
42 class DatabaseIterator;
43 
80 template <typename ValueType>
83 
84  public:
85  typedef ValueType value_type;
86  typedef const value_type &reference;
87 
89  virtual ~DatabaseIteratorBase() {}
90 
91  virtual std::string name() const = 0;
92 
93  std::unique_ptr<DatabaseIteratorBase> clone() const {
94  return std::unique_ptr<DatabaseIteratorBase>(this->_clone());
95  }
96 
97  private:
98  virtual void increment() = 0;
99 
100  virtual reference dereference() const = 0;
101 
102  virtual bool equal(const DatabaseIteratorBase<ValueType> &other) const = 0;
103 
104  virtual DatabaseIteratorBase *_clone() const = 0;
105 
106  /* Do not implement this
107  virtual long distance_to(const DatabaseIteratorBase<ValueType> &other) const =
108  0;
109  */
110 };
111 
145 
146 template <typename ValueType>
148 
149  public boost::iterator_facade<DatabaseIterator<ValueType>, ValueType,
150  std::forward_iterator_tag, const ValueType &,
151  long> {
152  public:
155 
158  : m_ptr(notstd::clone(it)) {}
159 
160  std::string name() const { return m_ptr->name(); }
161 
162  DatabaseIteratorBase<ValueType> *get() const { return m_ptr.unique().get(); }
163 
164  private:
165  friend boost::iterator_core_access;
166 
168  void increment() { m_ptr->increment(); }
169 
171  const ValueType &dereference() const { return m_ptr->dereference(); }
172 
174  bool equal(const DatabaseIterator &B) const {
175  return m_ptr->equal(*(B.m_ptr));
176  }
177 
178  /*
179  long distance_to(const DatabaseIterator &B) const {
180  Do not define this or boost::iterator_range<T>::size will compile but
181  cause runtime errors. Use boost::distance instead.
182  }
183  */
184 
186 };
187 
220 template <typename ValueType>
221 class ValDatabase : public DatabaseBase {
222  public:
224  typedef ValueType value_type;
225  typedef Index size_type;
226 
227  ValDatabase(const PrimClex &_primclex) : DatabaseBase(_primclex) {}
228 
229  virtual ~ValDatabase() {}
230 
231  virtual iterator begin() const = 0;
232 
233  virtual iterator end() const = 0;
234 
235  virtual size_type size() const = 0;
236  bool empty() const { return this->size() == 0; }
237 
239  std::pair<iterator, bool> set_alias(const std::string &name_or_alias,
240  const std::string &alias);
241 
247  std::string name(const std::string &name_or_alias) const;
248 
250  std::string alias(const std::string &name_or_alias) const;
251 
252  virtual std::pair<iterator, bool> insert(const ValueType &obj) = 0;
253  virtual std::pair<iterator, bool> insert(const ValueType &&obj) = 0;
254 
255  template <typename InputIt>
256  void insert(InputIt first, InputIt last) {
257  std::for_each(first, last,
258  [&](const ValueType &obj) { this->insert(obj); });
259  }
260 
261  virtual iterator erase(iterator pos) = 0;
262  size_type erase(const std::string &name_or_alias) {
263  this->erase(this->find(name_or_alias));
264  return 1;
265  }
266  size_type erase(const ValueType &obj) { return this->erase(obj.name()); }
267 
268  virtual size_type count(const std::string &name_or_alias) const {
269  if (this->find(name_or_alias) != this->end()) {
270  return 1;
271  }
272  return 0;
273  }
274 
275  virtual iterator find(const std::string &name_or_alias) const = 0;
276 
277  /* Don't do this, it's confusing. Use find(obj.name) or insert(obj):
278  iterator find(const ValueType &obj) const {
279  return find(obj.name());
280  }
281  */
282 
283  virtual void commit() = 0;
284 
286 
288  return m_master_selection;
289  }
290 
291  protected:
292  void read_aliases();
293 
294  void write_aliases();
295 
297  template <typename _ValueType>
298  void clear_name(const _ValueType &obj) const {
299  obj.clear_name();
300  }
301 
303  template <typename _ValueType>
304  void set_id(const _ValueType &obj, Index id) const {
305  obj.set_id(id);
306  }
307 
309  template <typename _ValueType>
310  void set_id(const _ValueType &obj, std::string id) const {
311  obj.set_id(id);
312  }
313 
314  private:
315  // enable lookup of name -> alias
316  std::map<std::string, std::string> m_name_to_alias;
317 
318  // enable lookup of alias -> name
319  std::map<std::string, std::string> m_alias_to_name;
320 
321  // Master selection to update upon insertion or removal.
323 };
324 
325 template <typename ValueType>
326 class Database;
327 
328 } // namespace DB
329 } // namespace CASM
330 
331 #endif
Fully generic database interface for use by DatabaseHandler.
Definition: Database.hh:25
DatabaseBase(const PrimClex &_primclex)
Definition: Database.hh:27
const PrimClex & primclex() const
Definition: Database.hh:35
const PrimClex * m_primclex
Definition: Database.hh:38
virtual void commit()=0
virtual ~DatabaseBase()
Definition: Database.hh:29
virtual void close()=0
virtual DatabaseBase & open()=0
std::unique_ptr< DatabaseIteratorBase > clone() const
Definition: Database.hh:93
virtual DatabaseIteratorBase * _clone() const =0
virtual std::string name() const =0
virtual bool equal(const DatabaseIteratorBase< ValueType > &other) const =0
const value_type & reference
Definition: Database.hh:86
virtual reference dereference() const =0
Wrapper class for specializations DatabaseIteratorBase.
Definition: Database.hh:151
const ValueType & dereference() const
boost::iterator_facade implementation
Definition: Database.hh:171
bool equal(const DatabaseIterator &B) const
boost::iterator_facade implementation
Definition: Database.hh:174
notstd::cloneable_ptr< DatabaseIteratorBase< ValueType > > m_ptr
Definition: Database.hh:185
void increment()
boost::iterator_facade implementation
Definition: Database.hh:168
DatabaseIterator(const DatabaseIteratorBase< ValueType > &it)
Construct iterator.
Definition: Database.hh:157
DatabaseIteratorBase< ValueType > * get() const
Definition: Database.hh:162
std::string name() const
Definition: Database.hh:160
DatabaseIterator()
Default constructor.
Definition: Database.hh:154
Generic interface for database of a particular CASM type.
Definition: Database.hh:221
virtual iterator end() const =0
virtual size_type size() const =0
size_type erase(const std::string &name_or_alias)
Definition: Database.hh:262
std::string name(const std::string &name_or_alias) const
void set_id(const _ValueType &obj, std::string id) const
Only ValDatabase<ValueType> is allowed to do a const id change.
Definition: Database.hh:310
virtual iterator begin() const =0
std::string alias(const std::string &name_or_alias) const
Get alias from name_or_alias.
std::map< std::string, std::string > m_alias_to_name
Definition: Database.hh:319
std::map< std::string, std::string > m_name_to_alias
Definition: Database.hh:316
virtual void commit()=0
std::pair< iterator, bool > set_alias(const std::string &name_or_alias, const std::string &alias)
For setting an alias.
void set_id(const _ValueType &obj, Index id) const
Only ValDatabase<ValueType> is allowed to do a const id change.
Definition: Database.hh:304
Selection< ValueType > & master_selection()
Definition: Database.hh:285
virtual ~ValDatabase()
Definition: Database.hh:229
Selection< ValueType > m_master_selection
Definition: Database.hh:322
void clear_name(const _ValueType &obj) const
Only ValDatabase<ValueType> is allowed to do clear_name.
Definition: Database.hh:298
const Selection< ValueType > & master_selection() const
Definition: Database.hh:287
ValueType value_type
Definition: Database.hh:224
size_type erase(const ValueType &obj)
Definition: Database.hh:266
virtual iterator find(const std::string &name_or_alias) const =0
DatabaseIterator< ValueType > iterator
Definition: Database.hh:223
bool empty() const
Definition: Database.hh:236
virtual std::pair< iterator, bool > insert(const ValueType &obj)=0
virtual iterator erase(iterator pos)=0
ValDatabase(const PrimClex &_primclex)
Definition: Database.hh:227
virtual size_type count(const std::string &name_or_alias) const
Definition: Database.hh:268
virtual std::pair< iterator, bool > insert(const ValueType &&obj)=0
void insert(InputIt first, InputIt last)
Definition: Database.hh:256
Implements PrimClex dependent functions.
Definition: HasPrimClex.hh:14
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'.
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)