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

Go to the SVN repository for this file.

1 #ifndef OBJMGR_SET__HPP
2 #define OBJMGR_SET__HPP
3 
4 #include <corelib/ncbistd.hpp>
5 #include <corelib/ncbicntr.hpp>
6 #include <set>
7 
9 
10 #define R_WRAP(Type, Call, Declaration) \
11  Type Declaration \
12  { \
13  Type ret; \
14  RLock(); \
15  ret = parent_type::Call; \
16  RUnlock(); \
17  return ret; \
18  }
19 #define R_WRAP_VOID(Call, Declaration) \
20  void Declaration \
21  { \
22  RLock(); \
23  parent_type::Call; \
24  RUnlock(); \
25  }
26 #define W_WRAP(Type, Call, Declaration) \
27  Type Declaration \
28  { \
29  Type ret; \
30  WLock(); \
31  ret = parent_type::Call; \
32  WUnlock(); \
33  return ret; \
34  }
35 #define W_WRAP_VOID(Call, Declaration) \
36  void Declaration \
37  { \
38  WLock(); \
39  parent_type::Call; \
40  WUnlock(); \
41  }
42 
43 template<typename Key, typename Compare = less<Key> >
44 class set : std::set<Key, Compare>
45 {
46  typedef std::set<Key, Compare> parent_type;
47  typedef checked_set<Key, Compare> this_type;
48  typedef pair<typename parent_type::const_iterator,
49  typename parent_type::const_iterator> const_iterator_pair;
50  typedef pair<typename parent_type::iterator,
51  typename parent_type::iterator> iterator_pair;
52  typedef pair<typename parent_type::iterator, bool> iterator_bool;
53 
54  void RLock() const
55  {
56  if ( m_RCounter.Add(1) <= 0 || m_WCounter.Get() != 0 )
57  abort();
58  }
59  void RUnlock() const
60  {
61  if ( m_WCounter.Get() != 0 || m_RCounter.Add(-1) < 0 )
62  abort();
63  }
64  void WLock() const
65  {
66  if ( m_WCounter.Add(1) != 1 || m_RCounter.Get() != 0 )
67  abort();
68  }
69  void WUnlock() const
70  {
71  if ( m_RCounter.Get() != 0 || m_WCounter.Add(-1) != 0 )
72  abort();
73  }
74 
75 public:
76  typedef typename parent_type::size_type size_type;
77  typedef typename parent_type::key_type key_type;
79  typedef typename parent_type::const_iterator const_iterator;
80  typedef typename parent_type::iterator iterator;
81 
83  {
84  }
86  {
87  WLock();
88  }
90  {
91  *this = m;
92  }
94  {
95  WLock();
96  m.RLock();
97  parent_type::operator=(m);
98  m.RUnlock();
99  WUnlock();
100  return *this;
101  }
102  void swap(this_type& m)
103  {
104  WLock();
105  m.WLock();
107  m.WUnlock();
108  WUnlock();
109  }
110 
111  bool operator==(const this_type& m) const
112  {
113  bool ret;
114  RLock();
115  m.RLock();
116  ret = parent_type::operator==(m);
117  m.RUnlock();
118  RUnlock();
119  return ret;
120  }
121  bool operator<(const this_type& m) const
122  {
123  bool ret;
124  RLock();
125  m.RLock();
126  ret = parent_type::operator<(m);
127  m.RUnlock();
128  RUnlock();
129  return ret;
130  }
131 
132  R_WRAP(size_type, size(), size() const);
133  R_WRAP(bool, empty(), empty() const);
134 
136  R_WRAP(const_iterator, end(), end() const);
141 
148 
154 
155 private:
158 };
159 
160 
161 template<typename Key, typename Compare = less<Key> >
162 class multiset : std::multiset<Key, Compare>
163 {
164  typedef std::multiset<Key, Compare> parent_type;
165  typedef checked_multiset<Key, Compare> this_type;
166  typedef pair<typename parent_type::const_iterator,
167  typename parent_type::const_iterator> const_iterator_pair;
168  typedef pair<typename parent_type::iterator,
169  typename parent_type::iterator> iterator_pair;
170  typedef pair<typename parent_type::iterator, bool> iterator_bool;
171 
172  void RLock() const
173  {
174  if ( m_RCounter.Add(1) <= 0 || m_WCounter.Get() != 0 )
175  abort();
176  }
177  void RUnlock() const
178  {
179  if ( m_WCounter.Get() != 0 || m_RCounter.Add(-1) < 0 )
180  abort();
181  }
182  void WLock() const
183  {
184  if ( m_WCounter.Add(1) != 1 || m_RCounter.Get() != 0 )
185  abort();
186  }
187  void WUnlock() const
188  {
189  if ( m_RCounter.Get() != 0 || m_WCounter.Add(-1) != 0 )
190  abort();
191  }
192 
193 public:
194  typedef typename parent_type::size_type size_type;
195  typedef typename parent_type::key_type key_type;
197  typedef typename parent_type::const_iterator const_iterator;
198  typedef typename parent_type::iterator iterator;
199 
201  {
202  }
204  {
205  WLock();
206  }
208  {
209  *this = m;
210  }
212  {
213  WLock();
214  m.RLock();
215  parent_type::operator=(m);
216  m.RUnlock();
217  WUnlock();
218  return *this;
219  }
220  void swap(this_type& m)
221  {
222  WLock();
223  m.WLock();
225  m.WUnlock();
226  WUnlock();
227  }
228 
229  bool operator==(const this_type& m) const
230  {
231  bool ret;
232  RLock();
233  m.RLock();
234  ret = parent_type::operator==(m);
235  m.RUnlock();
236  RUnlock();
237  return ret;
238  }
239  bool operator<(const this_type& m) const
240  {
241  bool ret;
242  RLock();
243  m.RLock();
244  ret = parent_type::operator<(m);
245  m.RUnlock();
246  RUnlock();
247  return ret;
248  }
249 
250 #define R_WRAP(Type, Call, Declaration) \
251  Type Declaration \
252  { \
253  Type ret; \
254  RLock(); \
255  ret = parent_type::Call; \
256  RUnlock(); \
257  return ret; \
258  }
259 #define R_WRAP_VOID(Call, Declaration) \
260  void Declaration \
261  { \
262  RLock(); \
263  parent_type::Call; \
264  RUnlock(); \
265  }
266 #define W_WRAP(Type, Call, Declaration) \
267  Type Declaration \
268  { \
269  Type ret; \
270  WLock(); \
271  ret = parent_type::Call; \
272  WUnlock(); \
273  return ret; \
274  }
275 #define W_WRAP_VOID(Call, Declaration) \
276  void Declaration \
277  { \
278  WLock(); \
279  parent_type::Call; \
280  WUnlock(); \
281  }
282 
283  R_WRAP(size_type, size(), size() const);
284  R_WRAP(bool, empty(), empty() const);
285 
287  R_WRAP(const_iterator, end(), end() const);
292 
299 
305 
306 private:
309 };
310 
311 #undef R_WRAP
312 #undef R_WRAP_VOID
313 #undef W_WRAP
314 #undef W_WRAP_VOID
315 
317 
318 #endif//OBJMGR_SET__HPP
CAtomicCounter_WithAutoInit –.
Definition: ncbicntr.hpp:120
checked_multiset()
Definition: set.hpp:200
size_type erase(const key_type &key)
Definition: set.hpp:303
checked_multiset< Key, Compare > this_type
Definition: set.hpp:165
void RUnlock() const
Definition: set.hpp:177
iterator insert(iterator pos, const value_type &val)
Definition: set.hpp:301
pair< typename parent_type::const_iterator, typename parent_type::const_iterator > const_iterator_pair
Definition: set.hpp:167
const_iterator_pair equal_range(const key_type &key) const
Definition: set.hpp:291
this_type & operator=(const this_type &m)
Definition: set.hpp:211
iterator upper_bound(const key_type &key)
Definition: set.hpp:297
void WUnlock() const
Definition: set.hpp:187
iterator find(const key_type &key)
Definition: set.hpp:295
const_iterator begin() const
Definition: set.hpp:286
iterator lower_bound(const key_type &key)
Definition: set.hpp:296
const_iterator upper_bound(const key_type &key) const
Definition: set.hpp:290
pair< typename parent_type::iterator, bool > iterator_bool
Definition: set.hpp:170
iterator insert(const value_type &val)
Definition: set.hpp:300
CAtomicCounter_WithAutoInit m_WCounter
Definition: set.hpp:304
const_iterator end() const
Definition: set.hpp:287
checked_multiset(const this_type &m)
Definition: set.hpp:207
parent_type::size_type size_type
Definition: set.hpp:194
iterator begin()
Definition: set.hpp:293
void clear()
Definition: set.hpp:304
parent_type::value_type value_type
Definition: set.hpp:196
parent_type::iterator iterator
Definition: set.hpp:198
~checked_multiset()
Definition: set.hpp:203
const_iterator find(const key_type &key) const
Definition: set.hpp:288
bool operator==(const this_type &m) const
Definition: set.hpp:229
void RLock() const
Definition: set.hpp:172
void WLock() const
Definition: set.hpp:182
void swap(this_type &m)
Definition: set.hpp:220
iterator end()
Definition: set.hpp:294
iterator_pair equal_range(const key_type &key)
Definition: set.hpp:298
std::multiset< Key, Compare > parent_type
Definition: set.hpp:164
size_type size() const
Definition: set.hpp:283
CAtomicCounter_WithAutoInit m_RCounter
Definition: set.hpp:308
parent_type::key_type key_type
Definition: set.hpp:195
void erase(iterator pos)
Definition: set.hpp:302
bool empty() const
Definition: set.hpp:284
bool operator<(const this_type &m) const
Definition: set.hpp:239
const_iterator lower_bound(const key_type &key) const
Definition: set.hpp:289
pair< typename parent_type::iterator, typename parent_type::iterator > iterator_pair
Definition: set.hpp:169
parent_type::const_iterator const_iterator
Definition: set.hpp:197
Definition: set.hpp:45
CAtomicCounter_WithAutoInit m_RCounter
Definition: set.hpp:157
iterator begin()
Definition: set.hpp:142
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
const_iterator begin() const
Definition: set.hpp:135
iterator upper_bound(const key_type &key)
Definition: set.hpp:146
pair< typename parent_type::const_iterator, typename parent_type::const_iterator > const_iterator_pair
Definition: set.hpp:49
void clear()
Definition: set.hpp:153
const_iterator_pair equal_range(const key_type &key) const
Definition: set.hpp:140
parent_type::iterator iterator
Definition: set.hpp:80
const_iterator upper_bound(const key_type &key) const
Definition: set.hpp:139
size_type size() const
Definition: set.hpp:132
iterator end()
Definition: set.hpp:143
bool operator<(const this_type &m) const
Definition: set.hpp:121
CAtomicCounter_WithAutoInit m_WCounter
Definition: set.hpp:153
parent_type::value_type value_type
Definition: set.hpp:78
parent_type::key_type key_type
Definition: set.hpp:77
iterator find(const key_type &key)
Definition: set.hpp:144
void RLock() const
Definition: set.hpp:54
bool empty() const
Definition: set.hpp:133
iterator lower_bound(const key_type &key)
Definition: set.hpp:145
void WLock() const
Definition: set.hpp:64
std::set< Key, Compare > parent_type
Definition: set.hpp:46
const_iterator find(const key_type &key) const
Definition: set.hpp:137
checked_set(const this_type &m)
Definition: set.hpp:89
iterator_pair equal_range(const key_type &key)
Definition: set.hpp:147
size_type erase(const key_type &key)
Definition: set.hpp:152
bool operator==(const this_type &m) const
Definition: set.hpp:111
pair< typename parent_type::iterator, typename parent_type::iterator > iterator_pair
Definition: set.hpp:51
~checked_set()
Definition: set.hpp:85
void erase(iterator pos)
Definition: set.hpp:151
void WUnlock() const
Definition: set.hpp:69
const_iterator end() const
Definition: set.hpp:136
void RUnlock() const
Definition: set.hpp:59
checked_set< Key, Compare > this_type
Definition: set.hpp:47
pair< typename parent_type::iterator, bool > iterator_bool
Definition: set.hpp:52
iterator insert(iterator pos, const value_type &val)
Definition: set.hpp:150
checked_set()
Definition: set.hpp:82
const_iterator lower_bound(const key_type &key) const
Definition: set.hpp:138
parent_type::size_type size_type
Definition: set.hpp:76
parent_type::const_iterator const_iterator
Definition: set.hpp:79
this_type & operator=(const this_type &m)
Definition: set.hpp:93
void swap(this_type &m)
Definition: set.hpp:102
Include a standard set of the NCBI C++ Toolkit most basic headers.
bool operator<(const CEquivRange &A, const CEquivRange &B)
bool operator==(const CEquivRange &A, const CEquivRange &B)
void swap(NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair1, NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair2)
Definition: ncbimisc.hpp:1508
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
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()
#define R_WRAP(Type, Call, Declaration)
Definition: set.hpp:250
#define W_WRAP_VOID(Call, Declaration)
Definition: set.hpp:275
#define W_WRAP(Type, Call, Declaration)
Definition: set.hpp:266
Modified on Wed Mar 27 11:26:44 2024 by modify_doxy.py rev. 669887