-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread_safe_map.h
183 lines (131 loc) · 11.6 KB
/
thread_safe_map.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef THREAD_SAFE_MAP_H_INCLUDED
#define THREAD_SAFE_MAP_H_INCLUDED
#include <boost/thread.hpp>
#include <map>
namespace thread_safe {
template < class Key, class T, class Compare = std::less<Key>, class Allocator = std::allocator<std::pair<const Key,T> > >
class map {
public:
typedef typename std::map<Key, T, Compare, Allocator>::iterator iterator;
typedef typename std::map<Key, T, Compare, Allocator>::const_iterator const_iterator;
typedef typename std::map<Key, T, Compare, Allocator>::reverse_iterator reverse_iterator;
typedef typename std::map<Key, T, Compare, Allocator>::const_reverse_iterator const_reverse_iterator;
typedef typename std::map<Key, T, Compare, Allocator>::allocator_type allocator_type;
typedef typename std::map<Key, T, Compare, Allocator>::size_type size_type;
typedef typename std::map<Key, T, Compare, Allocator>::key_compare key_compare;
typedef typename std::map<Key, T, Compare, Allocator>::value_compare value_compare;
typedef typename std::map<Key, T, Compare, Allocator>::value_type value_type;
//Constructors
explicit map ( const Compare& comp = Compare(), const Allocator & alloc = Allocator() ) : storage( comp, alloc ) { }
template <class InputIterator> map ( InputIterator first, InputIterator last, const Compare& comp = Compare(), const Allocator & alloc = Allocator() ) : storage( first, last, comp, alloc ) { }
map( const thread_safe::map<Key, T, Compare, Allocator> & x ) : storage( x.storage ) { }
//Copy
thread_safe::map<Key, Compare, Allocator> & operator=( const thread_safe::map<Key,Compare,Allocator> & x ) { boost::lock_guard<boost::mutex> lock( mutex ); boost::lock_guard<boost::mutex> lock2( x.mutex ); storage = x.storage; return *this; }
//Destructor
~map( void ) { }
//Iterators
iterator begin( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.begin(); }
const_iterator begin( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.begin(); }
iterator end( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.end(); }
const_iterator end( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.end(); }
reverse_iterator rbegin( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rbegin(); }
const_reverse_iterator rbegin( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rbegin(); }
reverse_iterator rend( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rend(); }
const_reverse_iterator rend( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rend(); }
//Capacity
size_type size( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.size(); }
size_type max_size( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.max_size(); }
bool empty( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.empty(); }
//Element Access
T & operator[]( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage[x]; }
//Modifiers
std::pair<iterator, bool> insert( const value_type & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.insert( x ); }
iterator insert( iterator position, const value_type & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.insert( position, x ); }
template <class InputIterator> void insert( InputIterator first, InputIterator last ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.insert( first, last ); }
void erase( iterator pos ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.erase( pos ); }
size_type erase( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.erase( x ); }
void erase( iterator begin, iterator end ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.erase( begin, end ); }
void swap( thread_safe::map<Key, T, Compare, Allocator> & x ) { boost::lock_guard<boost::mutex> lock( mutex ); boost::lock_guard<boost::mutex> lock2( x.mutex ); storage.swap( x.storage ); }
void clear( void ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.clear(); }
//Observers
key_compare key_comp( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.key_comp(); }
value_compare value_comp( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.value_comp(); }
//Operations
const_iterator find( const Key & x ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.find( x ); }
iterator find( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.find( x ); }
size_type count( const Key & x ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.count( x ); }
const_iterator lower_bound( const Key & x ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.lower_bound( x ); }
iterator lower_bound( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.lower_bound( x ); }
const_iterator upper_bound( const Key & x ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.upper_bound( x ); }
iterator upper_bound( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.upper_bound( x ); }
std::pair<const_iterator,const_iterator> equal_range( const Key & x ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.equal_range( x ); }
std::pair<iterator,iterator> equal_range( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.equal_range( x ); }
//Allocator
allocator_type get_allocator( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.get_allocator(); }
private:
std::map<Key, T, Compare, Allocator> storage;
mutable boost::mutex mutex;
};
template < class Key, class T, class Compare = std::less<Key>, class Allocator = std::allocator<std::pair<const Key,T> > >
class multimap {
public:
typedef typename std::multimap<Key, T, Compare, Allocator>::iterator iterator;
typedef typename std::multimap<Key, T, Compare, Allocator>::const_iterator const_iterator;
typedef typename std::multimap<Key, T, Compare, Allocator>::reverse_iterator reverse_iterator;
typedef typename std::multimap<Key, T, Compare, Allocator>::const_reverse_iterator const_reverse_iterator;
typedef typename std::multimap<Key, T, Compare, Allocator>::allocator_type allocator_type;
typedef typename std::multimap<Key, T, Compare, Allocator>::size_type size_type;
typedef typename std::multimap<Key, T, Compare, Allocator>::key_compare key_compare;
typedef typename std::multimap<Key, T, Compare, Allocator>::value_compare value_compare;
typedef typename std::multimap<Key, T, Compare, Allocator>::value_type value_type;
//Constructors
explicit multimap ( const Compare& comp = Compare(), const Allocator & alloc = Allocator() ) : storage( comp, alloc ) { }
template <class InputIterator> multimap ( InputIterator first, InputIterator last, const Compare& comp = Compare(), const Allocator & alloc = Allocator() ) : storage( first, last, comp, alloc ) { }
multimap( const thread_safe::multimap<Key, T, Compare, Allocator> & x ) : storage( x.storage ) { }
//Copy
thread_safe::multimap<Key, Compare, Allocator> & operator=( const thread_safe::multimap<Key,Compare,Allocator> & x ) { boost::lock_guard<boost::mutex> lock( mutex ); boost::lock_guard<boost::mutex> lock2( x.mutex ); storage = x.storage; return *this; }
//Destructor
~multimap( void ) { }
//Iterators
iterator begin( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.begin(); }
const_iterator begin( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.begin(); }
iterator end( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.end(); }
const_iterator end( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.end(); }
reverse_iterator rbegin( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rbegin(); }
const_reverse_iterator rbegin( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rbegin(); }
reverse_iterator rend( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rend(); }
const_reverse_iterator rend( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rend(); }
//Capacity
size_type size( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.size(); }
size_type max_size( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.max_size(); }
bool empty( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.empty(); }
//Modifiers
std::pair<iterator, bool> insert( const value_type & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.insert( x ); }
iterator insert( iterator position, const value_type & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.insert( position, x ); }
template <class InputIterator> void insert( InputIterator first, InputIterator last ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.insert( first, last ); }
void erase( iterator pos ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.erase( pos ); }
size_type erase( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.erase( x ); }
void erase( iterator begin, iterator end ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.erase( begin, end ); }
void swap( thread_safe::multimap<Key, T, Compare, Allocator> & x ) { boost::lock_guard<boost::mutex> lock( mutex ); boost::lock_guard<boost::mutex> lock2( x.mutex ); storage.swap( x.storage ); }
void clear( void ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.clear(); }
//Observers
key_compare key_comp( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.key_comp(); }
value_compare value_comp( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.value_comp(); }
//Operations
const_iterator find( const Key & x ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.find( x ); }
iterator find( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.find( x ); }
size_type count( const Key & x ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.count( x ); }
const_iterator lower_bound( const Key & x ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.lower_bound( x ); }
iterator lower_bound( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.lower_bound( x ); }
const_iterator upper_bound( const Key & x ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.upper_bound( x ); }
iterator upper_bound( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.upper_bound( x ); }
std::pair<const_iterator,const_iterator> equal_range( const Key & x ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.equal_range( x ); }
std::pair<iterator,iterator> equal_range( const Key & x ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.equal_range( x ); }
//Allocator
allocator_type get_allocator( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.get_allocator(); }
private:
std::multimap<Key, T, Compare, Allocator> storage;
mutable boost::mutex mutex;
};
}
#endif // THREAD_SAFE_MAP_H_INCLUDED