Ponca  82fc77e8c6294111c6f00670e92ec58a24e2ecf2
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 requires ValidCapacity<N>
15 typename BitSet<N, T>::iterator BitSet<N, T>::begin()
16 {
17 return m_data.begin();
18 }
19
20 template <int N, typename T>
21 requires ValidCapacity<N>
22 typename BitSet<N, T>::iterator BitSet<N, T>::end()
23 {
24 return m_data.begin() + N;
25 }
29 template <int N, typename T>
30 requires ValidCapacity<N>
32 {
33 Ponca::internal::fill(m_data.begin(), m_data.begin() + ARRAY_SIZE, T(0));
34 }
35
36 template <int N, typename T>
37 requires ValidCapacity<N>
38 bool BitSet<N, T>::erase(const int value)
39 {
40 PONCA_ASSERT_MSG(value >= 0 && value < N,
41 "Attempted to remove a value that is outside the scope of the BitSet");
42 const int byte = value / BIT_SIZE;
43 const int bit = value % BIT_SIZE;
44 const T bitMask = (T(1) << bit);
45 const bool alreadyEmpty = (m_data[byte] & bitMask) != 0;
46 m_data[byte] &= ~bitMask;
47 return alreadyEmpty;
48 }
49
50 template <int N, typename T>
51 requires ValidCapacity<N>
52 std::pair<typename BitSet<N, T>::iterator, bool> BitSet<N, T>::insert(const int& value)
53 {
54 PONCA_ASSERT_MSG(value >= 0 && value < N, "Inserted value is outside the scope of the BitSet");
55 const int byte = value / BIT_SIZE;
56 const int bit = value % BIT_SIZE;
57 const T bitMask = (T(1) << bit);
58 const bool alreadyInserted = (m_data[byte] & bitMask) == 0;
59 m_data[byte] |= bitMask;
60 return std::make_pair(m_data.begin() + byte, alreadyInserted);
61 }
62
63 template <int N, typename T>
64 requires ValidCapacity<N>
65 bool BitSet<N, T>::contains(const int value) const
66 {
67 PONCA_ASSERT_MSG(value >= 0 && value < N, "Searched value is outside the scope of the BitSet");
68 const int byte = value / BIT_SIZE;
69 const int bit = value % BIT_SIZE;
70 return (m_data[byte] & (T(1) << bit)) != 0;
71 }
72
76 template <int N, typename T>
77 requires ValidCapacity<N>
78 void BitSet<N, T>::flip(const int i)
79 {
80 PONCA_ASSERT_MSG(i >= 0 && i < N, "Flipped value is outside the scope of the BitSet");
81 const int byte = i / BIT_SIZE;
82 const int bit = i % BIT_SIZE;
83 m_data[byte] ^= (T(1) << bit);
84 }
85} // namespace Ponca
std::pair< iterator, bool > insert(const int &value)
Tries to insert a value in the set.
Definition bitset.hpp:52
void clear()
Sets all the bits to EMPTY.
Definition bitset.hpp:31
void flip(int i)
Toggles the value of a bit.
Definition bitset.hpp:78
bool contains(int value) const
Search if the value was already inserted or not.
Definition bitset.hpp:65
bool erase(int value)
Tries to insert a value in the set.
Definition bitset.hpp:38
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