NCBI C++ ToolKit
map.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef OBJMGR_MAP__HPP
2 #define OBJMGR_MAP__HPP
3 
4 #include <corelib/ncbistd.hpp>
5 #include <corelib/ncbicntr.hpp>
6 #include <map>
7 
9 
10 #define R_WRAP(Type, Call, Declaration) \
11  Type Declaration \
12  { \
13  Type ret; \
14  RLock(); \
15  ret = m_Container.Call; \
16  RUnlock(); \
17  return ret; \
18  }
19 #define R_WRAP_VOID(Call, Declaration) \
20  void Declaration \
21  { \
22  RLock(); \
23  m_Container.Call; \
24  RUnlock(); \
25  }
26 #define W_WRAP(Type, Call, Declaration) \
27  Type Declaration \
28  { \
29  Type ret; \
30  WLock(); \
31  ret = m_Container.Call; \
32  WUnlock(); \
33  return ret; \
34  }
35 #define W_WRAP_VOID(Call, Declaration) \
36  void Declaration \
37  { \
38  WLock(); \
39  m_Container.Call; \
40  WUnlock(); \
41  }
42 
43 template<class Container>
45 {
46  typedef Container container_type;
48 
49 public:
50  typedef typename container_type::size_type size_type;
51  typedef typename container_type::key_type key_type;
53  typedef typename container_type::const_iterator const_iterator;
54  typedef typename container_type::iterator iterator;
55 
56 protected:
57  typedef pair<const_iterator, const_iterator> const_iterator_pair;
58  typedef pair<iterator, iterator> iterator_pair;
59  typedef pair<iterator, bool> iterator_bool;
60 
64 
65 public:
66 
67  void RLock() const
68  {
69  if ( m_RCounter.Add(1) <= 0 || m_WCounter.Get() != 0 )
70  abort();
71  }
72  void RUnlock() const
73  {
74  if ( m_WCounter.Get() != 0 || m_RCounter.Add(-1) < 0 )
75  abort();
76  }
77  void WLock() const
78  {
79  if ( m_WCounter.Add(1) != 1 || m_RCounter.Get() != 0 )
80  abort();
81  }
82  void WUnlock() const
83  {
84  if ( m_RCounter.Get() != 0 || m_WCounter.Add(-1) != 0 )
85  abort();
86  }
87 
89  {
90  if ( pos != m_Container.end() ) {
91  iterator it = m_Container.find(pos->first);
92  if ( it != pos )
93  abort();
94  }
95  return pos;
96  }
97 
99  {
100  }
102  {
103  WLock();
104  }
106  {
107  *this = m;
108  }
110  {
111  WLock();
112  m.RLock();
114  m.RUnlock();
115  WUnlock();
116  return *this;
117  }
118  void swap(this_type& m)
119  {
120  WLock();
121  m.WLock();
122  m_Container.swap(m);
123  m.WUnlock();
124  WUnlock();
125  }
126 
127  bool operator==(const this_type& m) const
128  {
129  bool ret;
130  RLock();
131  m.RLock();
132  ret = m_Container == m.m_Container;
133  m.RUnlock();
134  RUnlock();
135  return ret;
136  }
137  bool operator<(const this_type& m) const
138  {
139  bool ret;
140  RLock();
141  m.RLock();
142  ret = m_Container < m.m_Container;
143  m.RUnlock();
144  RUnlock();
145  return ret;
146  }
147 
148  R_WRAP(size_type, size(), size() const);
149  R_WRAP(bool, empty(), empty() const);
150 
152  R_WRAP(const_iterator, end(), end() const);
157 
164 
170 
171  typename container_type::mapped_type& operator[](const key_type& key)
172  {
173  WLock();
174  typename container_type::mapped_type& ret = m_Container[key];
175  WUnlock();
176  return ret;
177  }
178 };
179 
180 
181 template<class Container>
183 {
184  typedef Container container_type;
186 
187 public:
188  typedef typename container_type::size_type size_type;
189  typedef typename container_type::key_type key_type;
191  typedef typename container_type::const_iterator const_iterator;
192  typedef typename container_type::iterator iterator;
193 
194 protected:
195  typedef pair<const_iterator, const_iterator> const_iterator_pair;
196  typedef pair<iterator, iterator> iterator_pair;
197  typedef pair<iterator, bool> iterator_bool;
198 
202 
203 public:
204 
205  void RLock() const
206  {
207  if ( m_RCounter.Add(1) <= 0 || m_WCounter.Get() != 0 )
208  abort();
209  }
210  void RUnlock() const
211  {
212  if ( m_WCounter.Get() != 0 || m_RCounter.Add(-1) < 0 )
213  abort();
214  }
215  void WLock() const
216  {
217  if ( m_WCounter.Add(1) != 1 || m_RCounter.Get() != 0 )
218  abort();
219  }
220  void WUnlock() const
221  {
222  if ( m_RCounter.Get() != 0 || m_WCounter.Add(-1) != 0 )
223  abort();
224  }
225 
227  {
228  if ( pos != m_Container.end() ) {
229  iterator it = m_Container.find(pos->first);
230  while ( it != m_Container.end() && it != pos && it->first == pos->first )
231  ++it;
232  if ( it != pos )
233  abort();
234  }
235  return pos;
236  }
237 
239  {
240  }
242  {
243  WLock();
244  }
246  {
247  *this = m;
248  }
250  {
251  WLock();
252  m.RLock();
254  m.RUnlock();
255  WUnlock();
256  return *this;
257  }
258  void swap(this_type& m)
259  {
260  WLock();
261  m.WLock();
262  m_Container.swap(m);
263  m.WUnlock();
264  WUnlock();
265  }
266 
267  bool operator==(const this_type& m) const
268  {
269  bool ret;
270  RLock();
271  m.RLock();
272  ret = m_Container == m.m_Container;
273  m.RUnlock();
274  RUnlock();
275  return ret;
276  }
277  bool operator<(const this_type& m) const
278  {
279  bool ret;
280  RLock();
281  m.RLock();
282  ret = m_Container < m.m_Container;
283  m.RUnlock();
284  RUnlock();
285  return ret;
286  }
287 
288  R_WRAP(size_type, size(), size() const);
289  R_WRAP(bool, empty(), empty() const);
290 
292  R_WRAP(const_iterator, end(), end() const);
297 
304 
310 };
311 
312 
313 template<class Container>
314 class rangemultimap_checker : public multimap_checker<Container>
315 {
316  typedef Container container_type;
317 public:
318  typedef typename container_type::key_type key_type;
319  typedef typename container_type::range_type range_type;
320  typedef typename container_type::iterator iterator;
321  typedef typename container_type::const_iterator constiterator;
322 
327 };
328 
329 
330 
331 #undef R_WRAP
332 #undef R_WRAP_VOID
333 #undef W_WRAP
334 #undef W_WRAP_VOID
335 
336 template<typename Key, typename T, typename Compare = less<Key> >
337 class map : public map_checker< std::map<Key, T, Compare> >
338 {
339 };
340 
341 
342 template<typename Key, typename T, typename Compare = less<Key> >
343 class multimap : public multimap_checker< std::multimap<Key, T, Compare> >
344 {
345 };
346 
347 
349 
350 #endif//OBJMGR_MAP__HPP
CAtomicCounter_WithAutoInit –.
Definition: ncbicntr.hpp:120
void erase(iterator pos)
Definition: map.hpp:167
map_checker()
Definition: map.hpp:98
iterator begin()
Definition: map.hpp:158
size_type size() const
Definition: map.hpp:148
container_type m_Container
Definition: map.hpp:61
container_type::const_iterator const_iterator
Definition: map.hpp:53
void WLock() const
Definition: map.hpp:77
iterator end()
Definition: map.hpp:159
iterator check(iterator pos)
Definition: map.hpp:88
Container container_type
Definition: map.hpp:46
void WUnlock() const
Definition: map.hpp:82
map_checker(const this_type &m)
Definition: map.hpp:105
iterator upper_bound(const key_type &key)
Definition: map.hpp:162
container_type::iterator iterator
Definition: map.hpp:54
map_checker< Container > this_type
Definition: map.hpp:47
const_iterator begin() const
Definition: map.hpp:151
bool operator<(const this_type &m) const
Definition: map.hpp:137
container_type::mapped_type & operator[](const key_type &key)
Definition: map.hpp:171
const_iterator end() const
Definition: map.hpp:152
container_type::key_type key_type
Definition: map.hpp:51
iterator insert(iterator pos, const value_type &val)
Definition: map.hpp:166
const_iterator lower_bound(const key_type &key) const
Definition: map.hpp:154
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
CAtomicCounter_WithAutoInit m_RCounter
Definition: map.hpp:63
~map_checker()
Definition: map.hpp:101
this_type & operator=(const this_type &m)
Definition: map.hpp:109
const_iterator_pair equal_range(const key_type &key) const
Definition: map.hpp:156
bool operator==(const this_type &m) const
Definition: map.hpp:127
pair< const_iterator, const_iterator > const_iterator_pair
Definition: map.hpp:57
void RUnlock() const
Definition: map.hpp:72
iterator_pair equal_range(const key_type &key)
Definition: map.hpp:163
container_type::size_type size_type
Definition: map.hpp:50
bool empty() const
Definition: map.hpp:149
iterator find(const key_type &key)
Definition: map.hpp:160
void RLock() const
Definition: map.hpp:67
const_iterator upper_bound(const key_type &key) const
Definition: map.hpp:155
pair< iterator, iterator > iterator_pair
Definition: map.hpp:58
container_type::value_type value_type
Definition: map.hpp:52
CAtomicCounter_WithAutoInit m_WCounter
Definition: map.hpp:62
void clear()
Definition: map.hpp:169
pair< iterator, bool > iterator_bool
Definition: map.hpp:59
iterator lower_bound(const key_type &key)
Definition: map.hpp:161
const_iterator find(const key_type &key) const
Definition: map.hpp:153
size_type erase(const key_type &key)
Definition: map.hpp:168
void swap(this_type &m)
Definition: map.hpp:118
Definition: map.hpp:338
const_iterator_pair equal_range(const key_type &key) const
Definition: map.hpp:296
CAtomicCounter_WithAutoInit m_WCounter
Definition: map.hpp:200
void clear()
Definition: map.hpp:309
size_type size() const
Definition: map.hpp:288
iterator insert(iterator pos, const value_type &val)
Definition: map.hpp:306
void RUnlock() const
Definition: map.hpp:210
pair< iterator, iterator > iterator_pair
Definition: map.hpp:196
container_type::const_iterator const_iterator
Definition: map.hpp:191
container_type m_Container
Definition: map.hpp:199
pair< iterator, bool > iterator_bool
Definition: map.hpp:197
const_iterator find(const key_type &key) const
Definition: map.hpp:293
void erase(iterator pos)
Definition: map.hpp:307
~multimap_checker()
Definition: map.hpp:241
iterator find(const key_type &key)
Definition: map.hpp:300
container_type::size_type size_type
Definition: map.hpp:188
const_iterator lower_bound(const key_type &key) const
Definition: map.hpp:294
const_iterator end() const
Definition: map.hpp:292
iterator end()
Definition: map.hpp:299
iterator insert(const value_type &val)
Definition: map.hpp:305
container_type::key_type key_type
Definition: map.hpp:189
iterator begin()
Definition: map.hpp:298
void swap(this_type &m)
Definition: map.hpp:258
const_iterator begin() const
Definition: map.hpp:291
multimap_checker(const this_type &m)
Definition: map.hpp:245
multimap_checker< Container > this_type
Definition: map.hpp:185
CAtomicCounter_WithAutoInit m_RCounter
Definition: map.hpp:201
iterator lower_bound(const key_type &key)
Definition: map.hpp:301
bool operator==(const this_type &m) const
Definition: map.hpp:267
container_type::iterator iterator
Definition: map.hpp:192
Container container_type
Definition: map.hpp:184
iterator_pair equal_range(const key_type &key)
Definition: map.hpp:303
iterator upper_bound(const key_type &key)
Definition: map.hpp:302
bool empty() const
Definition: map.hpp:289
void WUnlock() const
Definition: map.hpp:220
container_type::value_type value_type
Definition: map.hpp:190
pair< const_iterator, const_iterator > const_iterator_pair
Definition: map.hpp:195
bool operator<(const this_type &m) const
Definition: map.hpp:277
this_type & operator=(const this_type &m)
Definition: map.hpp:249
const_iterator upper_bound(const key_type &key) const
Definition: map.hpp:295
iterator check(iterator pos)
Definition: map.hpp:226
multimap_checker()
Definition: map.hpp:238
void RLock() const
Definition: map.hpp:205
size_type erase(const key_type &key)
Definition: map.hpp:308
void WLock() const
Definition: map.hpp:215
iterator begin()
Definition: map.hpp:323
iterator begin(const range_type &range)
Definition: map.hpp:325
container_type::const_iterator constiterator
Definition: map.hpp:321
container_type::iterator iterator
Definition: map.hpp:320
const_iterator begin(const range_type &range) const
Definition: map.hpp:326
container_type::range_type range_type
Definition: map.hpp:319
const_iterator begin() const
Definition: map.hpp:324
Container container_type
Definition: map.hpp:316
container_type::key_type key_type
Definition: map.hpp:318
Include a standard set of the NCBI C++ Toolkit most basic headers.
TValue Add(int delta) THROWS_NONE
Atomically add value (=delta), and return new counter value.
Definition: ncbicntr.hpp:278
TValue Get(void) const THROWS_NONE
Get atomic counter value.
Definition: ncbicntr.hpp:168
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define R_WRAP(Type, Call, Declaration)
Definition: map.hpp:10
#define W_WRAP_VOID(Call, Declaration)
Definition: map.hpp:35
#define W_WRAP(Type, Call, Declaration)
Definition: map.hpp:26
range(_Ty, _Ty) -> range< _Ty >
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
const struct ncbi::grid::netcache::search::fields::KEY key
void abort()
Modified on Fri Sep 20 14:58:19 2024 by modify_doxy.py rev. 669887