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