Ponca  911e152b8d5ac5c934a260b3832f7f62800b65b9
Point Cloud Analysis library
Loading...
Searching...
No Matches
hashset.hpp
1/*
2 This Source Code Form is subject to the terms of the Mozilla Public
3 License, v. 2.0. If a copy of the MPL was not distributed with this
4 file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 \author Auberval Florian
6*/
7
8namespace Ponca
9{
10 template <int N, typename T, template <int, typename> typename HF>
12 {
13 Ponca::internal::fill(m_data, m_data + N, EMPTY);
14 }
15
16 template <int N, typename T, template <int, typename> typename HF>
17 bool HashSet<N, T, HF>::search(const int _value, int& _searchedIdx) const
18 {
19 const int h = HashFunctor::hash(_value);
20
21 // Try to find the value
22 for (int i = 0; i < N; ++i)
23 {
24 _searchedIdx = (h + i) % N;
25 const T& slot = m_data[_searchedIdx]; // Get the address
26
27 // Stops the search here if the address is empty
28 if (slot == EMPTY)
29 return false;
30
31 // Is stored as value+OFFSET in the array (see insert)
32 if (slot == _value + OFFSET)
33 return true; // Value was found
34
35 // The value might have been inserted elsewhere, keep looking...
36 }
37
38 // Value not in HashSet
39 _searchedIdx = -1;
40 return false;
41 }
42
43 template <int N, typename T, template <int, typename> typename HF>
45 {
46 PONCA_ASSERT_MSG(_value != EMPTY - OFFSET, "Illegal value was inserted into the HashSet");
47 int availableIdx = 0;
48 if (search(_value, availableIdx)) // If search is successful
49 return false; // Insertion can't be done because found the value in the array
50
51 // The value wasn't found in the array, so either :
52 // A - The set is full (The last search index shouldn't point to an available address in the array)
53 if (availableIdx == -1) // Search returns -1 if the Set is full
54 return false;
55
56 // B - The set isn't full and the value can be inserted. Therefore, the last search index is the next available
57 // address in the array
58 m_data[availableIdx] = _value + OFFSET;
59 return true;
60 }
61
62 template <int N, typename T, template <int, typename> typename HF>
64 {
65 PONCA_DEBUG_ASSERT_MSG(_value != EMPTY - OFFSET, "Illegal value was searched from the HashSet");
66 int i;
67 return search(_value, i);
68 }
69} // namespace Ponca
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:257
bool insert(int _value)
Tries to insert a value in the HashSet.
Definition hashset.hpp:44
bool search(int _value, int &_searchedIdx) const
Search for a value in the HashSet.
Definition hashset.hpp:17
bool contains(int _value) const
Tries to find a value in the HashSet.
Definition hashset.hpp:63
void clear()
Empty the array.
Definition hashset.hpp:11
void fill(ForwardIt first, ForwardIt last, const T &value)
Assigns the given value to all elements in the range [first, last).
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition bitset.h:16