ALPSCore reference
dictionary.cpp
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 
10 #include <alps/dictionary.hpp>
11 
12 #include <alps/hdf5/map.hpp>
13 
14 #ifdef ALPS_HAVE_MPI
16 #endif
17 
18 namespace alps {
19  namespace params_ns {
20 
21  dictionary::map_type::const_iterator dictionary::find_nonempty_(const std::string& key) const {
22  map_type::const_iterator it=map_.find(key);
23  if (it!=map_.end() && !(it->second).empty())
24  return it;
25  else
26  return map_.end();
27  }
28 
29 
32  map_type::iterator it=map_.lower_bound(key);
33  if (it==map_.end() ||
34  map_.key_comp()(key, it->first)) {
35  // it's a new element, we have to construct it here
36  // and copy it to the map, returning the ref to the inserted element
37  it=map_.insert(it,map_type::value_type(key, value_type(key)));
38  }
39  // return reference to the existing or the newly-created element
40  return it->second;
41  }
42 
44  const dictionary::value_type& dictionary::operator[](const std::string& key) const {
45  map_type::const_iterator it=map_.find(key);
46  if (it==map_.end()) throw exception::uninitialized_value(key, "Attempt to read uninitialized value");
47  return it->second;
48  }
49 
50  namespace {
51 
52  template <typename M>
53  struct compare {
54  typedef typename M::value_type pair_type;
55  bool operator()(const pair_type& lhs, const pair_type& rhs) const
56  {
57  return (lhs.first==rhs.first) && lhs.second.equals(rhs.second);
58  }
59  };
60 
61  }
62 
63  bool dictionary::equals(const dictionary &rhs) const
64  {
65  if (this->size()!=rhs.size()) return false;
66  return std::equal(map_.begin(), map_.end(), rhs.map_.begin(), compare<map_type>());
67  }
68 
69 
71  {
72  ar[""] << map_;
73  }
74 
76  {
77  map_type new_map;
78  ar[""] >> new_map;
79 
80  using std::swap;
81  swap(map_,new_map);
82  }
83 
84  std::ostream& operator<<(std::ostream& s, const dictionary& d)
85  {
86  for (dictionary::const_iterator it=d.begin(); it!=d.end(); ++it) {
87  s << it->first << " = " << it->second << "\n";
88  }
89  return s;
90  }
91 
92 #ifdef ALPS_HAVE_MPI
93  // Defined here to avoid including <mpi_map.hpp> inside user header
94  void dictionary::broadcast(const alps::mpi::communicator& comm, int root) {
96  broadcast(comm, map_, root);
97  }
98 #endif
99 
100  } // params_ns::
101 } // alps::
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)
Exception for using uninitialized value.
Encapsulation of an MPI communicator and some communicator-related operations.
Definition: mpi.hpp:111
bool equals(const dictionary &rhs) const
Compare two dictionaries (true if all entries are of the same type and value)
Definition: dictionary.cpp:63
friend std::ostream & operator<<(std::ostream &, const dictionary &)
Definition: dictionary.cpp:84
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
const_iterator end() const
Const-iterator to the end of the contained map.
Definition: dictionary.hpp:33
void broadcast(const communicator &comm, T *vals, std::size_t count, int root)
Broadcasts array vals of a primitive type T, length count on communicator comm with root root ...
Definition: mpi.hpp:270
Python-like dictionary.
Definition: dictionary.hpp:18
map_type::const_iterator const_iterator
Definition: dictionary.hpp:27
Header for object-oriented interface to MPI for std::map.