mCRL2
Loading...
Searching...
No Matches
indexed_set.h
Go to the documentation of this file.
1// Author(s): Maurice Laveaux
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8
9#ifndef MCRL2_UTILITIES_INDEXED_SET_H
10#define MCRL2_UTILITIES_INDEXED_SET_H
11
12#include <deque>
13#include <mutex>
14
19
20namespace mcrl2
21{
22namespace utilities
23{
24
26template<typename Key,
27 bool ThreadSafe = false,
28 typename Hash = std::hash<Key>,
29 typename Equals = std::equal_to<Key>,
30 typename Allocator = std::allocator<Key>,
31 typename KeyTable = std::deque< Key, Allocator > >
33{
34private:
35 std::vector<detail::atomic_wrapper<std::size_t>> m_hashtable;
36 KeyTable m_keys;
37
39 mutable std::shared_ptr<std::mutex> m_mutex;
40 mutable std::vector<shared_mutex> m_shared_mutexes;
41
43 // has not yet been used. This allows to increase m_keys in
44 // large steps, avoiding exclusive access too often.
46
48 Equals m_equals;
49
54 void reserve_indices(std::size_t thread_index);
55
57 std::size_t put_in_hashtable(const Key& key, std::size_t value, std::size_t& new_position);
58
60 inline void resize_hashtable();
61
62public:
63 typedef Key key_type;
64 typedef std::size_t size_type;
65 typedef std::pair<const key_type, size_type> value_type;
66 typedef Equals key_equal;
67 typedef Hash hasher;
68
72 typedef const value_type* const_pointer;
73
74 typedef typename KeyTable::iterator iterator;
75 typedef typename KeyTable::const_iterator const_iterator;
76
77 typedef typename KeyTable::reverse_iterator reverse_iterator;
78 typedef typename KeyTable::const_reverse_iterator const_reverse_iterator;
79
80 typedef std::ptrdiff_t difference_type;
81
84 static constexpr size_type npos = std::numeric_limits<std::size_t>::max();
85
88
98 indexed_set(std::size_t number_of_threads);
99
109 std::size_t number_of_threads,
110 std::size_t initial_hashtable_size,
111 const hasher& hash = hasher(),
112 const key_equal& equals = key_equal());
113
116 size_type index(const key_type& key, std::size_t thread_index = 0) const;
117
122 const key_type& at(const size_type index) const;
123
128 const key_type& operator[](const size_type index) const;
129
132 iterator begin(std::size_t thread_index = 0)
133 {
134 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
135 iterator i = m_keys.begin();
136 return i;
137 }
138
140 iterator end(std::size_t thread_index = 0)
141 {
142 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
143 iterator i = m_keys.begin()+m_next_index;
144 return i;
145 }
146
149 const_iterator begin(std::size_t thread_index = 0) const
150 {
151 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
152 const_iterator i = m_keys.begin();
153 return i;
154 }
155
157 const_iterator end(std::size_t thread_index = 0) const
158 {
159 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
161 return i;
162 }
163
165 const_iterator cbegin(std::size_t thread_index = 0) const
166 {
167 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
168 const_iterator i = m_keys.begin();
169 return i;
170 }
171
173 const_iterator cend(std::size_t thread_index = 0) const
174 {
175 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
176 const_iterator i = m_keys.cbegin() + m_next_index;
177 return i;
178 }
179
181 reverse_iterator rbegin(std::size_t thread_index = 0)
182 {
183 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
185 return i;
186 }
187
189 reverse_iterator rend(std::size_t thread_index = 0)
190 {
191 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
192 reverse_iterator i = m_keys.rend();
193 return i;
194 }
195
197 const_reverse_iterator crbegin(std::size_t thread_index = 0) const
198 {
199 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
201 return i;
202 }
203
205 const_reverse_iterator crend(std::size_t thread_index = 0) const
206 {
207 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
208 reverse_iterator i = m_keys.crend();
209 return i;
210 }
211
213 void clear(std::size_t thread_index=0);
214
221 std::pair<size_type, bool> insert(const key_type& key, std::size_t thread_index = 0);
222
226 const_iterator find(const key_type& key, std::size_t thread_index = 0) const;
227
231 size_type size(std::size_t thread_index = 0) const
232 {
233 shared_guard guard = m_shared_mutexes[thread_index].lock_shared();
234 size_type result=m_next_index;
235 return result;
236 }
237};
238
239} // end namespace utilities
240} // end namespace mcrl2
241
243
244
245#endif // MCRL2_UTILITIES_INDEXED_SET_H
A set that assigns each element an unique index.
Definition indexed_set.h:33
void resize_hashtable()
Resizes the hash table to twice its current size.
const key_type & operator[](const size_type index) const
Operator that provides a const reference at the position indicated by index.
std::vector< shared_mutex > m_shared_mutexes
Definition indexed_set.h:40
indexed_set()
Constructor of an empty indexed set. Starts with a hashtable of size 128 and assumes one single threa...
std::vector< detail::atomic_wrapper< std::size_t > > m_hashtable
Definition indexed_set.h:35
iterator end(std::size_t thread_index=0)
End of the forward iterator.
std::pair< size_type, bool > insert(const key_type &key, std::size_t thread_index=0)
Insert a key in the indexed set and return its index.
detail::atomic_wrapper< size_t > m_next_index
m_next_index indicates the next index that
Definition indexed_set.h:45
reverse_iterator rbegin(std::size_t thread_index=0)
Reverse iterator going through the elements in the set from the largest to the smallest index.
const_reverse_iterator crbegin(std::size_t thread_index=0) const
Reverse const_iterator going through the elements from the highest to the lowest numbered element.
const_reverse_iterator crend(std::size_t thread_index=0) const
End of the reverse const_iterator.
const_iterator begin(std::size_t thread_index=0) const
Forward iterator which runs through the elements from the lowest to the largest number.
std::shared_ptr< std::mutex > m_mutex
Mutex for the m_hashtable and m_keys data structures.
Definition indexed_set.h:39
void clear(std::size_t thread_index=0)
Clears the indexed set by removing all its elements. It is not guaranteed that the memory is released...
KeyTable::reverse_iterator reverse_iterator
Definition indexed_set.h:77
iterator begin(std::size_t thread_index=0)
Forward iterator which runs through the elements from the lowest to the largest number.
const_iterator cbegin(std::size_t thread_index=0) const
const_iterator going through the elements in the set numbered from zero upwards.
std::pair< const key_type, size_type > value_type
Definition indexed_set.h:65
const value_type & const_reference
Definition indexed_set.h:70
indexed_set(std::size_t number_of_threads, std::size_t initial_hashtable_size, const hasher &hash=hasher(), const key_equal &equals=key_equal())
Constructor of an empty index set. Starts with a hashtable of the indicated size.
const_iterator find(const key_type &key, std::size_t thread_index=0) const
Provides an iterator to the stored key in the indexed set.
size_type index(const key_type &key, std::size_t thread_index=0) const
Returns a reference to the mapped value.
KeyTable::const_iterator const_iterator
Definition indexed_set.h:75
reverse_iterator rend(std::size_t thread_index=0)
End of the reverse iterator.
void reserve_indices(std::size_t thread_index)
Reserve indices that can be used. Doing this infrequently prevents obtaining an exclusive lock for th...
const_iterator end(std::size_t thread_index=0) const
End of the forward iterator.
std::size_t put_in_hashtable(const Key &key, std::size_t value, std::size_t &new_position)
Inserts the given (key, n) pair into the indexed set.
size_type size(std::size_t thread_index=0) const
The number of elements in the indexed set.
indexed_set(std::size_t number_of_threads)
Constructor of an empty indexed set.
KeyTable::iterator iterator
Definition indexed_set.h:74
static constexpr size_type npos
Value returned when an element does not exist in the set.
Definition indexed_set.h:84
const key_type & at(const size_type index) const
Returns a reference to the mapped value.
const value_type * const_pointer
Definition indexed_set.h:72
KeyTable::const_reverse_iterator const_reverse_iterator
Definition indexed_set.h:78
const_iterator cend(std::size_t thread_index=0) const
End of the forward const_iterator.
A shared lock guard for the shared_mutex.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
#define hash(l, r, m)
Definition tree_set.cpp:23
add your file description here.