Ponca  82fc77e8c6294111c6f00670e92ec58a24e2ecf2
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 // Iterators --------------------------------------------------------------------
11 template <int N, typename T, template <int, typename> typename HF, T OFFSET>
12 requires ValidCapacity<N>
13 typename HashSet<N, T, HF, OFFSET>::const_iterator HashSet<N, T, HF, OFFSET>::cbegin() const
14 {
15 return m_data.begin();
16 }
17
18 template <int N, typename T, template <int, typename> typename HF, T OFFSET>
19 requires ValidCapacity<N>
20 typename HashSet<N, T, HF, OFFSET>::const_iterator HashSet<N, T, HF, OFFSET>::cend() const
21 {
22 return m_data.begin() + N;
23 }
24
25 template <int N, typename T, template <int, typename> typename HF, T OFFSET>
26 requires ValidCapacity<N>
27 typename HashSet<N, T, HF, OFFSET>::iterator HashSet<N, T, HF, OFFSET>::begin()
28 {
29 return m_data.begin();
30 }
31
32 template <int N, typename T, template <int, typename> typename HF, T OFFSET>
33 requires ValidCapacity<N>
34 typename HashSet<N, T, HF, OFFSET>::iterator HashSet<N, T, HF, OFFSET>::end()
35 {
36 return m_data.begin() + N;
37 }
38
39 // Set-like methods --------------------------------------------------------------------
40 template <int N, typename T, template <int, typename> typename HF, T OFFSET>
41 requires ValidCapacity<N>
43 {
44 Ponca::internal::fill(m_data.begin(), m_data.begin() + N, 0);
45 }
46
47 template <int N, typename T, template <int, typename> typename HF, T OFFSET>
48 requires ValidCapacity<N>
49 bool HashSet<N, T, HF, OFFSET>::search(const T _value, T& _searchedIdx) const
50 {
51 const int h = HashFunctor::hash(_value);
52
53 // Try to find the value
54 for (int i = 0; i < N; ++i)
55 {
56 _searchedIdx = (h + i) % N;
57 const T& slot = m_data[_searchedIdx]; // Get the address
58
59 // Stops the search here if the address is empty
60 if (slot == 0)
61 return false;
62
63 // Is stored as value+OFFSET in the array (see insert)
64 if (slot == _value + OFFSET)
65 return true; // Value was found
66
67 // The value might have been inserted elsewhere, keep looking...
68 }
69
70 // Value not in HashSet
71 _searchedIdx = -1;
72 return false;
73 }
74
75 template <int N, typename T, template <int, typename> typename HF, T OFFSET>
76 requires ValidCapacity<N>
77 std::pair<typename HashSet<N, T, HF, OFFSET>::iterator, bool> HashSet<N, T, HF, OFFSET>::insert(const T& _value)
78 {
79 PONCA_ASSERT_MSG(_value != -OFFSET, "Illegal value was inserted into the HashSet");
80 int availableIdx = 0;
81 if (search(_value, availableIdx)) // If search is successful
82 return std::make_pair(m_data.begin() + availableIdx,
83 false); // Insertion can't be done because found the value in the array
84
85 // The value wasn't found in the array, so either :
86 // A - The set is full (The last search index is -1 if it didn't find an available address in the array)
87 if (availableIdx == -1)
88 {
89 return std::make_pair(end(), false);
90 }
91 // B - The set isn't full and the value can be inserted. Therefore, the last search index is the next available
92 // address in the array
93 m_data[availableIdx] = _value + OFFSET;
94 return std::make_pair(m_data.begin() + availableIdx, true);
95 }
96
97 template <int N, typename T, template <int, typename> typename HF, T OFFSET>
98 requires ValidCapacity<N>
100 {
101 PONCA_DEBUG_ASSERT_MSG(_value != -OFFSET, "Illegal value was searched from the HashSet");
102 int i;
103 return search(_value, i);
104 }
105} // namespace Ponca
std::pair< typename Self::iterator, bool > insert(const T &_value)
Tries to insert a value in the HashSet.
Definition hashset.hpp:77
bool contains(T _value) const
Tries to find a value in the HashSet.
Definition hashset.hpp:99
void clear()
Empty the array.
Definition hashset.hpp:42
Self::const_iterator cend() const
The end of the internal array.
Definition hashset.hpp:20
bool search(T _value, T &_searchedIdx) const
Search for a value in the HashSet.
Definition hashset.hpp:49
Self::iterator end()
The end of the internal array.
Definition hashset.hpp:34
Self::const_iterator cbegin() const
The beginning of the internal array.
Definition hashset.hpp:13
Self::iterator begin()
The beginning of the internal array.
Definition hashset.hpp:27
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 concepts.h:11