Ponca  4d2a58fa5c6375adef5c4b208f4d47e016cecd6d
Point Cloud Analysis library
Loading...
Searching...
No Matches
bitset.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{
13 template <int N, typename T>
14 typename BitSet<N, T>::iterator BitSet<N, T>::begin()
15 {
16 return m_data.begin();
17 }
18
19 template <int N, typename T>
20 typename BitSet<N, T>::iterator BitSet<N, T>::end()
21 {
22 return m_data.begin() + N;
23 }
27 template <int N, typename T>
29 {
30 Ponca::internal::fill(m_data.begin(), m_data.begin() + ARRAY_SIZE, T(0));
31 }
32
33 template <int N, typename T>
34 bool BitSet<N, T>::erase(const int value)
35 {
36 PONCA_ASSERT_MSG(value >= 0 && value < N,
37 "Attempted to remove a value that is outside the scope of the BitSet");
38 const int byte = value / BIT_SIZE;
39 const int bit = value % BIT_SIZE;
40 const T bitMask = (T(1) << bit);
41 const bool alreadyEmpty = (m_data[byte] & bitMask) != 0;
42 m_data[byte] &= ~bitMask;
43 return alreadyEmpty;
44 }
45
46 template <int N, typename T>
47 std::pair<typename BitSet<N, T>::iterator, bool> BitSet<N, T>::insert(const int& value)
48 {
49 PONCA_ASSERT_MSG(value >= 0 && value < N, "Inserted value is outside the scope of the BitSet");
50 const int byte = value / BIT_SIZE;
51 const int bit = value % BIT_SIZE;
52 const T bitMask = (T(1) << bit);
53 const bool alreadyInserted = (m_data[byte] & bitMask) == 0;
54 m_data[byte] |= bitMask;
55 return std::make_pair(m_data.begin() + byte, alreadyInserted);
56 }
57
58 template <int N, typename T>
59 bool BitSet<N, T>::contains(const int value) const
60 {
61 PONCA_ASSERT_MSG(value >= 0 && value < N, "Searched value is outside the scope of the BitSet");
62 const int byte = value / BIT_SIZE;
63 const int bit = value % BIT_SIZE;
64 return (m_data[byte] & (T(1) << bit)) != 0;
65 }
66
70 template <int N, typename T>
71 void BitSet<N, T>::flip(const int i)
72 {
73 PONCA_ASSERT_MSG(i >= 0 && i < N, "Flipped value is outside the scope of the BitSet");
74 const int byte = i / BIT_SIZE;
75 const int bit = i % BIT_SIZE;
76 m_data[byte] ^= (T(1) << bit);
77 }
78} // namespace Ponca
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:260
std::pair< iterator, bool > insert(const int &value)
Tries to insert a value in the set.
Definition bitset.hpp:47
void clear()
Sets all the bits to EMPTY.
Definition bitset.hpp:28
void flip(int i)
Toggles the value of a bit.
Definition bitset.hpp:71
bool erase(int value)
Tries to insert a value in the set.
Definition bitset.hpp:34
bool contains(int value) const
Search if the value was already inserted or not.
Definition bitset.hpp:59
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