8 #ifndef META_OCEAN_BASE_HASH_SET_H
9 #define META_OCEAN_BASE_HASH_SET_H
31 typedef typename std::pair<std::pair<size_t, size_t>, T>
Element;
71 bool insert(
const T& element,
const bool oneOnly =
true,
const bool extendCapacity =
true);
80 bool insert(T&& element,
const bool oneOnly =
true,
const bool extendCapacity =
true);
87 bool remove(
const T& element);
94 bool find(
const T& element)
const;
105 inline size_t size()
const;
167 template <
typename T>
169 setElements(hashSet.setElements),
170 setSize(hashSet.setSize),
171 setFunction(hashSet.setFunction)
176 template <
typename T>
178 setElements(std::move(hashSet.setElements)),
179 setSize(hashSet.setSize),
180 setFunction(hashSet.setFunction)
185 template <
typename T>
187 setElements(capacity),
189 setFunction(function)
194 template <
typename T>
196 setElements(capacity),
198 setFunction(hashSet.setFunction)
200 ocean_assert(
capacity >= hashSet.size());
202 for (
typename Elements::iterator i = hashSet.setElements.begin(); i != hashSet.setElements.end(); ++i)
203 if (i->first.first != 0)
205 T& element = i->second;
206 insert(std::move(element));
209 ocean_assert(
size() == hashSet.size());
215 template <
typename T>
218 ocean_assert(setSize <= setElements.size());
219 ocean_assert(isConsistent());
222 if (extendCapacity && setSize >= setElements.size() * 80 / 100)
224 *
this =
HashSet<T>(max(
size_t(32), setElements.size() * 2), std::move(*
this));
225 ocean_assert(setSize < setElements.size() * 80 / 100);
228 if (setSize == setElements.size())
234 for (
size_t n = 0; n < setElements.size(); ++n)
236 const size_t value = (setFunction(element) + n) % setElements.size();
239 if (setElements[value].first.first == 0)
241 setElements[value].first.first = 1;
242 setElements[value].first.second = n;
243 setElements[value].second = element;
248 else if (setElements[value].second == element)
251 setElements[value].first.first++;
257 for (
size_t n = 0; n < setElements.size(); ++n)
259 const size_t value = (setFunction(element) + n) % setElements.size();
262 if (setElements[value].first.first == 0)
264 setElements[value].first.first = 1;
265 setElements[value].first.second = n;
266 setElements[value].second = element;
272 setElements[value].first.first++;
276 ocean_assert(
false &&
"This must never happen!");
280 template <
typename T>
283 ocean_assert(setSize <= setElements.size());
284 ocean_assert(isConsistent());
287 if (extendCapacity && setSize >= setElements.size() * 80 / 100)
289 *
this =
HashSet<T>(max(
size_t(32), setElements.size() * 2), std::move(*
this));
290 ocean_assert(setSize < setElements.size() * 80 / 100);
293 if (setSize == setElements.size())
299 for (
size_t n = 0; n < setElements.size(); ++n)
301 const size_t value = (setFunction(element) + n) % setElements.size();
304 if (setElements[value].first.first == 0)
306 setElements[value].first.first = 1;
307 setElements[value].first.second = n;
308 setElements[value].second = std::move(element);
313 else if (setElements[value].second == element)
316 setElements[value].first.first++;
322 for (
size_t n = 0; n < setElements.size(); ++n)
324 const size_t value = (setFunction(element) + n) % setElements.size();
327 if (setElements[value].first.first == 0)
329 setElements[value].first.first = 1;
330 setElements[value].first.second = n;
331 setElements[value].second = std::move(element);
337 setElements[value].first.first++;
341 ocean_assert(
false &&
"This must never happen!");
345 template <
typename T>
348 ocean_assert(setSize <= setElements.size());
349 ocean_assert(isConsistent());
352 for (
size_t n = 0; n < setElements.size(); ++n)
354 const size_t value = (setFunction(element) + n) % setElements.size();
357 if (setElements[value].first.first == 0)
361 if (setElements[value].first.first == 1)
363 if (setElements[value].second == element)
365 setElements[value].first.first = 0;
366 setElements[value].second = T();
369 ocean_assert(isConsistent());
378 ocean_assert(setElements[value].first.first > 1);
380 size_t elementOffset = 0u;
382 if (setElements[value].second == element)
386 size_t localValue = value;
387 size_t endLocation = setElements.size();
391 size_t lastOffset = 0;
394 for (
size_t i = 1; i < endLocation; ++i)
396 const size_t testValue = (localValue + i) % setElements.size();
398 if (setElements[testValue].first.first >= 1)
400 if (setElements[testValue].first.second >= i)
404 if (setElements[testValue].first.first <= 1)
411 ocean_assert(endLocation >= lastOffset);
412 endLocation -= lastOffset;
414 elementOffset += lastOffset;
416 const size_t lastValue = (localValue + lastOffset) % setElements.size();
421 setElements[localValue].first.second = setElements[lastValue].first.second - lastOffset;
422 setElements[localValue].second = setElements[lastValue].second;
424 localValue = lastValue;
426 if (setElements[lastValue].first.first == 1)
431 const size_t startIndex = setFunction(element);
433 for (
size_t i = 0u; i < elementOffset + n; ++i)
434 setElements[(startIndex + i) % setElements.size()].first.first--;
436 setElements[(value + elementOffset) % setElements.size()].first.first = 0;
437 setElements[(value + elementOffset) % setElements.size()].second = T();
440 ocean_assert(isConsistent());
446 ocean_assert(
false &&
"This must never happen!");
450 template <
typename T>
453 ocean_assert(setSize <= setElements.size());
454 ocean_assert(isConsistent());
457 for (
size_t n = 0; n < setElements.size(); ++n)
459 const size_t value = (setFunction(element) + n) % setElements.size();
462 if (setElements[value].first.first == 0)
466 if (setElements[value].second == element)
470 if (setElements[value].first.first == 1)
477 template <
typename T>
480 ocean_assert(isConsistent());
482 for (
typename Elements::iterator i = setElements.begin(); i != setElements.end(); ++i)
487 ocean_assert(isConsistent());
490 template <
typename T>
496 template <
typename T>
499 return setElements.size();
502 template <
typename T>
508 template <
typename T>
511 if (
this != &hashSet)
521 template <
typename T>
524 if (
this != &hashSet)
526 setElements = std::move(hashSet.setElements);
527 setSize = hashSet.setSize;
528 setFunction = hashSet.setFunction;
536 template <
typename T>
539 return size_t(element);
542 template <
typename T>
547 for (
typename Elements::const_iterator i = setElements.begin(); i != setElements.end(); ++i)
548 if (i->first.first != 0)
551 return count == setSize;
This class implements a hash set.
Definition: HashSet.h:25
ValueFunction setFunction
Value function.
Definition: HashSet.h:164
bool remove(const T &element)
Removes an element from this hash set.
Definition: HashSet.h:346
size_t capacity() const
Returns the capacity of this hash set.
Definition: HashSet.h:497
static size_t defaultHashFunction(const T &element)
Default hash function for elements supporting an cast size_t cast.
Definition: HashSet.h:537
void clear()
Removes all elements from this has set.
Definition: HashSet.h:478
HashSet(const HashSet< T > &hashSet)
Copy constructor.
Definition: HashSet.h:168
HashSet< T > & operator=(const HashSet< T > &hashSet)
Assign operator.
Definition: HashSet.h:509
size_t size() const
Returns the number of elements this hash set currently holds.
Definition: HashSet.h:491
size_t(* ValueFunction)(const T &element)
Definition of a function pointer returning a hash set value.
Definition: HashSet.h:41
size_t setSize
Number of elements this has set holds.
Definition: HashSet.h:161
std::vector< Element > Elements
Definition of a vector holding the set objects.
Definition: HashSet.h:36
bool isEmpty() const
Returns whether this hash set is empty.
Definition: HashSet.h:503
std::pair< std::pair< size_t, size_t >, T > Element
Definition of a pair combining a counter states and an object.
Definition: HashSet.h:31
bool insert(const T &element, const bool oneOnly=true, const bool extendCapacity=true)
Adds a new element to this hash set.
Definition: HashSet.h:216
Elements setElements
Hash set elements.
Definition: HashSet.h:158
bool find(const T &element) const
Returns whether this hash set holds a given element.
Definition: HashSet.h:451
bool isConsistent() const
Returns whether this hash set is still consistent.
Definition: HashSet.h:543
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15