OpenZWave Library
1.4.0
Toggle main menu visibility
Loading...
Searching...
No Matches
cpp
src
Bitfield.h
Go to the documentation of this file.
1
//-----------------------------------------------------------------------------
2
//
3
// Bitfield.h
4
//
5
// Variable length bitfield implementation
6
//
7
// Copyright (c) 2011 Mal Lansell <openzwave@lansell.org>
8
//
9
// SOFTWARE NOTICE AND LICENSE
10
//
11
// This file is part of OpenZWave.
12
//
13
// OpenZWave is free software: you can redistribute it and/or modify
14
// it under the terms of the GNU Lesser General Public License as published
15
// by the Free Software Foundation, either version 3 of the License,
16
// or (at your option) any later version.
17
//
18
// OpenZWave is distributed in the hope that it will be useful,
19
// but WITHOUT ANY WARRANTY; without even the implied warranty of
20
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
// GNU Lesser General Public License for more details.
22
//
23
// You should have received a copy of the GNU Lesser General Public License
24
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
25
//
26
//-----------------------------------------------------------------------------
27
28
#ifndef _Bitfield_H
29
#define _Bitfield_H
30
31
#include <vector>
32
#include "
Defs.h
"
33
34
namespace
OpenZWave
35
{
36
class
OPENZWAVE_EXPORT
Bitfield
37
{
38
friend
class
Iterator
;
39
40
public
:
41
Bitfield
():m_numSetBits(0){}
42
~Bitfield
(){}
43
44
void
Set
(
uint32
_idx )
45
{
46
if
( !
IsSet
(_idx) )
47
{
48
uint32
newSize = (_idx>>5)+1;
49
if
( newSize > m_bits.size() )
50
{
51
m_bits.resize( newSize, 0 );
52
}
53
m_bits[_idx>>5] |= (1<<(_idx&0x1f));
54
++m_numSetBits;
55
}
56
}
57
58
void
Clear
(
uint32
_idx )
59
{
60
if
(
IsSet
(_idx) )
61
{
62
m_bits[_idx>>5] &= ~(1<<(_idx&0x1f));
63
--m_numSetBits;
64
}
65
}
66
67
bool
IsSet
(
uint32
_idx )
const
68
{
69
if
( (_idx>>5) < m_bits.size() )
70
{
71
return
( ( m_bits[_idx>>5] & (1<<(_idx&0x1f)) ) !=0 );
72
}
73
return
false
;
74
}
75
76
uint32
GetNumSetBits
()
const
{
return
m_numSetBits; }
77
78
class
Iterator
79
{
80
friend
class
Bitfield
;
81
82
public
:
83
uint32
operator *()
const
84
{
85
return
m_idx;
86
}
87
88
Iterator&
operator++
()
89
{
90
// Search forward to next set bit
91
NextSetBit();
92
return
*
this
;
93
}
94
95
Iterator
operator++
(
int
)
96
{
97
Iterator tmp = *
this
;
98
++tmp;
99
return
tmp;
100
}
101
102
bool
operator ==(
const
Iterator &rhs)
103
{
104
return
m_idx == rhs.m_idx;
105
}
106
107
bool
operator !=(
const
Iterator &rhs)
108
{
109
return
m_idx != rhs.m_idx;
110
}
111
112
private
:
113
Iterator
(
Bitfield
const
* _bitfield,
uint32
_idx ): m_idx( _idx ), m_bitfield( _bitfield )
114
{
115
// If this is a begin iterator, move it to the first set bit
116
if
( ( _idx == 0 ) && ( !m_bitfield->IsSet(0) ) )
117
{
118
NextSetBit();
119
}
120
}
121
122
void
NextSetBit()
123
{
124
while
( ((++m_idx)>>5)<m_bitfield->m_bits.size() )
125
{
126
// See if there are any bits left to find in the current uint32
127
if
( ( m_bitfield->m_bits[m_idx>>5] & ~((1<<(m_idx&0x1f))-1) ) == 0 )
128
{
129
// No more bits - move on to next uint32 (or rather one less than
130
// the next uint32 because of the preincrement in the while statement)
131
m_idx = (m_idx&0xffffffe0)+31;
132
}
133
else
134
{
135
if
( (m_bitfield->m_bits[m_idx>>5] & (1<<(m_idx&0x1f))) !=0 )
136
{
137
// This bit is set
138
return
;
139
}
140
}
141
}
142
}
143
144
uint32
m_idx;
145
Bitfield
const
* m_bitfield;
146
};
147
148
Iterator
Begin
()
const
{
return
Iterator
(
this
, 0 ); }
149
Iterator
End
()
const
{
return
Iterator
(
this
, (
uint32
) m_bits.size()<<5 ); }
150
151
private
:
152
OPENZWAVE_EXPORT_WARNINGS_OFF
153
vector<uint32> m_bits;
154
OPENZWAVE_EXPORT_WARNINGS_ON
155
uint32
m_numSetBits;
156
};
157
}
// namespace OpenZWave
158
159
#endif
160
161
162
Defs.h
uint32
unsigned int uint32
Definition
Defs.h:80
OPENZWAVE_EXPORT_WARNINGS_ON
#define OPENZWAVE_EXPORT_WARNINGS_ON
Definition
Defs.h:54
OPENZWAVE_EXPORT
#define OPENZWAVE_EXPORT
Definition
Defs.h:52
OPENZWAVE_EXPORT_WARNINGS_OFF
#define OPENZWAVE_EXPORT_WARNINGS_OFF
Definition
Defs.h:53
OpenZWave::Bitfield::Iterator
Definition
Bitfield.h:79
OpenZWave::Bitfield::Iterator::Bitfield
friend class Bitfield
Definition
Bitfield.h:80
OpenZWave::Bitfield::Iterator::operator++
Iterator & operator++()
Definition
Bitfield.h:88
OpenZWave::Bitfield::Iterator::operator++
Iterator operator++(int)
Definition
Bitfield.h:95
OpenZWave::Bitfield::Bitfield
Bitfield()
Definition
Bitfield.h:41
OpenZWave::Bitfield::IsSet
bool IsSet(uint32 _idx) const
Definition
Bitfield.h:67
OpenZWave::Bitfield::~Bitfield
~Bitfield()
Definition
Bitfield.h:42
OpenZWave::Bitfield::End
Iterator End() const
Definition
Bitfield.h:149
OpenZWave::Bitfield::Set
void Set(uint32 _idx)
Definition
Bitfield.h:44
OpenZWave::Bitfield::Clear
void Clear(uint32 _idx)
Definition
Bitfield.h:58
OpenZWave::Bitfield::Begin
Iterator Begin() const
Definition
Bitfield.h:148
OpenZWave::Bitfield::GetNumSetBits
uint32 GetNumSetBits() const
Definition
Bitfield.h:76
OpenZWave
Definition
Bitfield.h:35
Generated on
for OpenZWave Library by
1.18.0