MPQC  3.0.0-alpha
registry.timpl.h
1 //
2 // registry.timpl.h
3 //
4 // Copyright (C) 2008 Edward Valeev
5 //
6 // Author: Edward Valeev <evaleev@vt.edu>
7 // Maintainer: EV
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27 
28 #ifndef _mpqc_src_lib_chemistry_qc_mbptr12_registrytimpl_h
29 #define _mpqc_src_lib_chemistry_qc_mbptr12_registrytimpl_h
30 
31 #include <cassert>
32 #include <iterator>
33 #include <util/state/statein.h>
34 #include <util/state/stateout.h>
35 #include <util/misc/registry.h>
36 
37 namespace sc {
38 
39  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
40  Ref< Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual> >
41  Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::instance()
42  {
43  return CreationPolicy< Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual> >::instance();
44  }
45 
46  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
47  void
48  Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::save_instance(const Ref<Registry>& obj, StateOut& so)
49  {
50  CreationPolicy< Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual> >::save_instance(obj, so);
51  }
52 
53  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
54  Ref< Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual> >
55  Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::restore_instance(StateIn& si)
56  {
57  return CreationPolicy< Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual> >::instance(si);
58  }
59 
60  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
61  Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::Registry() :
62  lock_(ThreadGrp::get_default_threadgrp()->new_lock())
63  {
64  }
65 
66  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
67  Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::Registry(StateIn& si) :
68  lock_(ThreadGrp::get_default_threadgrp()->new_lock())
69  {
70  si.get(map_);
71  }
72 
73  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
74  void
75  Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::save_data_state(StateOut& so)
76  {
77  so.put(map_);
78  }
79 
80  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
81  void
83  {
84  ThreadLockHolder lh(lock_);
85  map_.clear();
86  }
87 
88  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
89  typename Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::const_iterator
91  {
92  // if KeyEqual is std::equal_to<Key> then can use the fast find function
93  if (SameType<KeyEqual,std::equal_to<Key> >::result) {
94  const_iterator result = map_.find(key);
95  return result;
96  }
97  else {
98  KeyEqual keyeq;
99  for(const_iterator v=map_.begin(); v!= map_.end(); ++v)
100  if (keyeq(v->first,key))
101  return v;
102  return map_.end();
103  }
104  }
105 
106  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
107  typename Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::iterator
108  Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::find_by_key(const Key& key)
109  {
110  // if KeyEqual is std::equal_to<Key> then can use the fast find function
111  if (SameType<KeyEqual,std::equal_to<Key> >::result) {
112  iterator result = map_.find(key);
113  return result;
114  }
115  else {
116  KeyEqual keyeq;
117  for(iterator v=map_.begin(); v!= map_.end(); ++v)
118  if (keyeq(v->first,key))
119  return v;
120  return map_.end();
121  }
122  }
123 
124  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
125  typename Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::const_iterator
126  Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::find_by_value(const Value& value) const
127  {
128  const_iterator result;
129 
130  ValueEqual valeq;
131  for(const_iterator v=map_.begin(); v!= map_.end(); ++v)
132  if (valeq(v->second,value))
133  return v;
134 
135  return map_.end();
136  }
137 
138  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
139  bool
141  {
142  bool result = false;
143 
144  // although this does not modify the map, cannot search map while someone else is changing it
145  const_iterator v = find_by_key(key);
146  ThreadLockHolder lh(lock_);
147  if (v != map_.end())
148  result = true;
149 
150  return result;
151  }
152 
153  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
154  bool
156  {
157  bool result = false;
158 
159  // although this does not modify the map, cannot search map while someone else is changing it
160  ThreadLockHolder lh(lock_);
161  const_iterator v = find_by_value(value);
162  if (v != map_.end())
163  result = true;
164 
165  return result;
166  }
167 
168  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
169  const Key&
171  {
172  // although this does not modify the map, cannot search map while someone else is changing it
173  ThreadLockHolder lh(lock_);
174  const_iterator v = find_by_value(value);
175  if (v != map_.end()) {
176  return v->first;
177  }
178  else {
179  throw not_found("value not found");
180  }
181  // unreachable
182  MPQC_ASSERT(false);
183  }
184 
185  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
186  const Value&
188  {
189  // although this does not modify the map, cannot search map while someone else is changing it
190  ThreadLockHolder lh(lock_);
191  const_iterator v = find_by_key(key);
192  if (v != map_.end()) {
193  return v->second;
194  }
195  else {
196  lh.unlock();
197  this->print(ExEnv::out0());
198  throw not_found("key not found");
199  }
200  // unreachable
201  MPQC_ASSERT(false);
202  }
203 
204  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
205  void
207  const Value& value)
208  {
209  // check if key already exists
210  if (key_exists(key)) {
211  this->print(ExEnv::out0());
212  throw std::logic_error("key already exists");
213  }
214  ThreadLockHolder lh(lock_);
215  map_[key] = value;
216  }
217  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
218  void
219  Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::add(const std::pair<Key,Value>& keyval_pair)
220  {
221  this->add(keyval_pair.first,keyval_pair.second);
222  }
223 
224  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
225  void
227  {
228  ThreadLockHolder lh(lock_);
229  iterator v = find_by_key(key);
230  if (v != map_.end())
231  map_.erase(v);
232  }
233 
234  template <typename T1, typename T2>
235  std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& val) {
236  os << "{ " << val.first << ", " << val.second << " }";
237  return os;
238  }
239 
240  template <typename Key, typename Value, template <typename> class CreationPolicy, typename KeyEqual, typename ValueEqual >
241  void
242  Registry<Key,Value,CreationPolicy,KeyEqual,ValueEqual>::print(std::ostream& os) const
243  {
244  ThreadLockHolder lh(lock_);
245  for(typename Map::const_iterator iter = map_.begin();
246  iter != map_.end();
247  ++iter) {
248  os << iter->first << " " << iter->second << std::endl;
249  }
250  }
251 
252 } // end of namespace sc
253 
254 #endif // end of header guard
255 
256 
257 // Local Variables:
258 // mode: c++
259 // c-file-style: "CLJ-CONDENSED"
260 // End:
sc::Registry::add
void add(const Key &key, const Value &obj)
registers this object
Definition: registry.timpl.h:206
sc::Registry::value
const Value & value(const Key &key) const
returns object that corresponds to this key. If key is not known, throws
Definition: registry.timpl.h:187
sc::Registry::clear
void clear()
erases all entries
Definition: registry.timpl.h:82
sc::ThreadLockHolder::unlock
void unlock()
Release the lock before the DTOR is called, if it is still held.
Definition: thread.h:66
sc::operator<<
std::vector< unsigned int > operator<<(const GaussianBasisSet &B, const GaussianBasisSet &A)
computes a map from basis functions in A to the equivalent basis functions in B.
sc::Registry::key
const Key & key(const Value &obj) const
returns key that corresponds to this object. If obj is not known, throws
Definition: registry.timpl.h:170
sc::Registry::value_exists
bool value_exists(const Value &value) const
value exists?
Definition: registry.timpl.h:155
sc::ExEnv::out0
static std::ostream & out0()
Return an ostream that writes from node 0.
sc::Registry::remove
void remove(const Key &key)
removes the object corresponding to key
Definition: registry.timpl.h:226
sc::ThreadLockHolder
Acquire a lock on creation and release it on destruction.
Definition: thread.h:56
sc::Registry::not_found
Definition: registry.h:150
sc::Registry::key_exists
bool key_exists(const Key &key) const
key exists?
Definition: registry.timpl.h:140
sc
Contains all MPQC code up to version 3.
Definition: mpqcin.h:14
sc::Registry
Registry wraps std::map and can be policy-configured to act as a Singleton or a regular object.
Definition: registry.h:112

Generated at Sun Jan 26 2020 23:24:02 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.