ALPSCore reference
dictionary.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 #ifndef ALPS_PARAMS_DICTIONARY_HPP_e15039548f43464996cad06f9c8a3220
8 #define ALPS_PARAMS_DICTIONARY_HPP_e15039548f43464996cad06f9c8a3220
9 
10 #include <alps/config.hpp>
11 #include <map>
12 #include "./params/dict_value.hpp"
13 
14 namespace alps {
15  namespace params_ns {
16 
18  class dictionary {
19  public:
21 
22  private:
23  typedef std::map<std::string, value_type> map_type;
24  map_type map_;
25 
26  public:
27  typedef map_type::const_iterator const_iterator;
28 
30  const_iterator begin() const { return map_.begin(); }
31 
33  const_iterator end() const { return map_.end(); }
34 
36  virtual ~dictionary() {}
37 
39  bool empty() const { return map_.empty(); }
40 
42  std::size_t size() const { return map_.size(); }
43 
45  void erase(const std::string& key) { map_.erase(key); }
46 
48  value_type& operator[](const std::string& key);
49 
51  const value_type& operator[](const std::string& key) const;
52 
54  const_iterator find(const std::string& key) const {
55  return map_.find(key);
56  }
57 
58  private:
60  map_type::const_iterator find_nonempty_(const std::string& key) const;
61 
62  public:
63 
65  bool exists(const std::string& key) const {
66  return find_nonempty_(key)!=map_.end();
67  }
68 
70  template <typename T>
71  bool exists(const std::string& key) const {
72  map_type::const_iterator it=find_nonempty_(key);
73  return it!=map_.end() && (it->second).isType<T>();
74  }
75 
77  friend void swap(dictionary& d1, dictionary& d2) { using std::swap; swap(d1.map_, d2.map_); }
78 
80  bool equals(const dictionary& rhs) const;
81 
83  void save(alps::hdf5::archive& ar) const;
84 
86  void load(alps::hdf5::archive& ar);
87 
88  friend std::ostream& operator<<(std::ostream&, const dictionary&);
89 
90 #ifdef ALPS_HAVE_MPI
91  void broadcast(const alps::mpi::communicator& comm, int root);
93 #endif
94  };
95 
96  inline bool operator==(const dictionary& lhs, const dictionary& rhs) {
97  return lhs.equals(rhs);
98  }
99 
100  inline bool operator!=(const dictionary& lhs, const dictionary& rhs) {
101  return !(lhs==rhs);
102  }
103 
105 
109  template <typename F>
110  inline typename F::result_type apply_visitor(F& visitor, dictionary::const_iterator it) {
111  return boost::apply_visitor(visitor, it->second);
112  }
113 
115 
119  template <typename F>
120  inline typename F::result_type apply_visitor(const F& visitor, dictionary::const_iterator it) {
121  return boost::apply_visitor(visitor, it->second);
122  }
123 
124  } // params_ns::
125 } // alps::
126 
127 
128 #endif /* ALPS_PARAMS_DICTIONARY_HPP_e15039548f43464996cad06f9c8a3220 */
bool empty() const
True if the cdictionary does not contain elements (even empty ones)
Definition: dictionary.hpp:39
const_iterator begin() const
Const-iterator to the beginning of the contained map.
Definition: dictionary.hpp:30
void broadcast(C const &c, P &p, int r=0)
Definition: api.hpp:56
std::size_t size() const
Size of the dictionary (including empty elements)
Definition: dictionary.hpp:42
void swap(params &p1, params &p2)
bool operator==(const dictionary &lhs, const dictionary &rhs)
Definition: dictionary.hpp:96
Encapsulation of an MPI communicator and some communicator-related operations.
Definition: mpi.hpp:111
F::result_type apply_visitor(F &visitor, dictionary::const_iterator it)
Const-access visitor to a value by an iterator.
Definition: dictionary.hpp:110
F::result_type apply_visitor(const F &visitor, dictionary::const_iterator it)
Const-access visitor to a value by an iterator.
Definition: dictionary.hpp:120
bool equals(const dictionary &rhs) const
Compare two dictionaries (true if all entries are of the same type and value)
Definition: dictionary.cpp:63
virtual ~dictionary()
Virtual destructor to make dictionary inheritable.
Definition: dictionary.hpp:36
friend std::ostream & operator<<(std::ostream &, const dictionary &)
Definition: dictionary.cpp:84
void erase(const std::string &key)
Erase an element if it exists.
Definition: dictionary.hpp:45
void load(alps::hdf5::archive &ar)
Load the dictionary from an archive.
Definition: dictionary.cpp:75
void save(alps::hdf5::archive &ar) const
Save the dictionary to an archive.
Definition: dictionary.cpp:70
friend void swap(dictionary &d1, dictionary &d2)
Swap the dictionaries.
Definition: dictionary.hpp:77
value_type & operator[](const std::string &key)
Access with intent to assign.
Definition: dictionary.cpp:31
bool exists(const std::string &key) const
Check if a key exists and has a value of a particular type (without creating the key) ...
Definition: dictionary.hpp:71
const_iterator end() const
Const-iterator to the end of the contained map.
Definition: dictionary.hpp:33
bool exists(const std::string &key) const
Check if a key exists and has a value (without creating the key)
Definition: dictionary.hpp:65
Python-like dictionary.
Definition: dictionary.hpp:18
const_iterator find(const std::string &key) const
Obtain read-only iterator to a name.
Definition: dictionary.hpp:54
map_type::const_iterator const_iterator
Definition: dictionary.hpp:27
bool operator!=(const dictionary &lhs, const dictionary &rhs)
Definition: dictionary.hpp:100