OWLNext
7.0
Borland's Object Windows Library for the modern age
Toggle main menu visibility
Loading...
Searching...
No Matches
bitset.cpp
Go to the documentation of this file.
1
//----------------------------------------------------------------------------
2
// ObjectWindows
3
// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4
//
5
/// \file
6
/// Implementation of TBitSet, a set of up to 256 bit flags
7
//----------------------------------------------------------------------------
8
#include <
owl/pch.h
>
9
#include <
owl/bitset.h
>
10
#include <
owl/objstrm.h
>
11
#include <type_traits>
12
13
namespace
owl
{
14
15
OWL_DIAGINFO
;
16
17
//
18
/// Constructs a TBitSet object with all bits set to 0.
19
//
20
template
<
class
T>
21
TBitSet<T>::TBitSet
()
22
{
23
for
(
unsigned
int
i = 0; i <
sizeof
(Bits); i++)
24
Bits[i] = 0;
25
}
26
27
//
28
/// Constructs a TBitSet object as a copy of another TBitSet.
29
//
30
template
<
class
T>
31
TBitSet<T>::TBitSet
(
const
TBitSet
& bs)
32
{
33
for
(
unsigned
int
i = 0; i <
sizeof
(Bits); i++)
34
Bits[i] = bs.Bits[i];
35
}
36
37
//
38
/// Return true if a bit is turned on.
39
//
40
template
<
class
T>
41
bool
42
TBitSet<T>::Has
(T item)
const
43
{
44
return
(Bits[Loc(item)] & Mask(item)) != 0;
45
}
46
47
//
48
/// Negate a bit.
49
//
50
template
<
class
T>
51
TBitSet<T>
52
TBitSet<T>::operator ~
()
const
53
{
54
TBitSet
temp;
55
for
(
unsigned
int
i = 0; i <
sizeof
(Bits); i++)
56
temp.Bits[i] =
uint8
(~Bits[i]);
57
return
temp;
58
}
59
60
//
61
/// Turn off a specific bit.
62
//
63
template
<
class
T>
64
void
65
TBitSet<T>::DisableItem
(T item)
66
{
67
Bits[Loc(item)] &=
uint8
(~Mask(item));
68
}
69
70
//
71
/// Turn on a specific bit.
72
//
73
template
<
class
T>
74
void
75
TBitSet<T>::EnableItem
(T item)
76
{
77
Bits[Loc(item)] |= Mask(item);
78
}
79
80
//
81
/// Enable a specific bit.
82
//
83
template
<
class
T>
84
TBitSet<T>
&
TBitSet<T>::operator +=
(T item)
85
{
86
EnableItem
(item);
87
return
*
this
;
88
}
89
90
//
91
/// Disable a specific bit.
92
//
93
template
<
class
T>
94
TBitSet<T>
&
TBitSet<T>::operator -=
(T item)
95
{
96
DisableItem
(item);
97
return
*
this
;
98
}
99
100
//
101
/// Turn off all the given bits.
102
//
103
template
<
class
T>
104
void
105
TBitSet<T>::DisableItem
(
const
TBitSet<T>
& bs)
106
{
107
for
(
unsigned
int
i = 0; i <
sizeof
(Bits); i++)
108
Bits[i] &=
uint8
(~(bs.Bits[i]));
109
}
110
111
//
112
/// Turn on all the given bits.
113
//
114
template
<
class
T>
115
void
116
TBitSet<T>::EnableItem
(
const
TBitSet<T>
& bs)
117
{
118
for
(
unsigned
int
i = 0; i <
sizeof
(Bits); i++)
119
Bits[i] |= bs.Bits[i];
120
}
121
122
//
123
/// Enable all the given bits.
124
//
125
template
<
class
T>
126
TBitSet<T>
&
TBitSet<T>::operator +=
(
const
TBitSet<T>
& bs)
127
{
128
EnableItem
(bs);
129
return
*
this
;
130
}
131
132
//
133
/// Enable all the given bits.
134
//
135
template
<
class
T>
136
TBitSet<T>
&
TBitSet<T>::operator |=
(
const
TBitSet<T>
& bs)
137
{
138
EnableItem
(bs);
139
return
*
this
;
140
}
141
142
//
143
/// Disable all the given bits.
144
//
145
template
<
class
T>
146
TBitSet<T>
&
TBitSet<T>::operator -=
(
const
TBitSet<T>
& bs)
147
{
148
DisableItem
(bs);
149
return
*
this
;
150
}
151
152
//
153
/// Logically AND each individual bits.
154
//
155
template
<
class
T>
156
TBitSet<T>
&
157
TBitSet<T>::operator &=
(
const
TBitSet<T>
& bs)
158
{
159
for
(
unsigned
int
i = 0; i <
sizeof
(Bits); i++)
160
Bits[i] &= bs.Bits[i];
161
return
*
this
;
162
}
163
164
//
165
/// Return true if all bits are off.
166
//
167
template
<
class
T>
168
bool
169
TBitSet<T>::IsEmpty
()
const
170
{
171
for
(
unsigned
int
i = 0; i <
sizeof
(Bits); i++)
172
if
(Bits[i] != 0)
173
return
false
;
174
return
true
;
175
}
176
177
//
178
/// Return the index of the item searched for.
179
//
180
template
<
class
T>
181
int
TBitSet<T>::Loc(T item)
const
182
{
183
return
static_cast<
typename std::make_unsigned<T>::type
>
(item) / 8;
184
}
185
186
//
187
/// Return the mask needed to look for a specific bit.
188
//
189
template
<
class
T>
190
uint8
TBitSet<T>::Mask(T item)
const
191
{
192
return
static_cast<
uint8
>
(1 << (
static_cast<
typename std::make_unsigned<T>::type
>
(item) % 8));
193
}
194
195
//
196
// Explicit instantiations.
197
// These are the only instantiations that are supported.
198
// (Otherwise the implementation above needs to be moved to the header,
199
// so that the user can instantiate it with other template arguments).
200
//
201
template
class
TBitSet<uint8>
;
202
template
class
TBitSet<tchar>
;
203
204
//----------------------------------------------------------------------------
205
206
//
207
/// Construct a default character set.
208
/// All items are set to 0.
209
//
210
TCharSet::TCharSet
()
211
:
212
TBitSet
<
tchar
>()
213
{
214
}
215
216
//
217
/// Copy the bitset into this character set.
218
//
219
TCharSet::TCharSet
(
const
TBitSet<tchar>
& bs)
220
:
221
TBitSet
<
tchar
>(bs)
222
{
223
}
224
225
//
226
/// Construct a character set from a string.
227
/// The characters act as the index for the bitset.
228
//
229
TCharSet::TCharSet
(LPCTSTR str)
230
:
231
TBitSet
<
tchar
>()
232
{
233
for
(LPCTSTR p = str; *p; p++) {
234
if
(*p ==
_T
(
'\\'
))
235
p++;
236
else
if
(*p ==
_T
(
'-'
) && p > str && p[1]) {
// handle "A-Z" type shorthands
237
p++;
238
for
(
tchar
c =
tchar
(p[-2]+1); c < *p; c++)
// replace "-" with "B..Y"
239
EnableItem
(
static_cast<
uint8
>
(c));
240
}
241
EnableItem
(
static_cast<
uint8
>
(*p));
242
}
243
}
244
245
//
246
/// Construct a character set from a string.
247
/// The characters act as the index for the bitset.
248
//
249
TCharSet::TCharSet
(
const
tstring
& s)
250
:
251
TBitSet
<
tchar
>()
252
{
253
const
LPCTSTR str = s.c_str();
254
for
(LPCTSTR p = str; *p; p++) {
255
if
(*p ==
_T
(
'\\'
))
256
p++;
257
else
if
(*p ==
_T
(
'-'
) && p > str && p[1]) {
// handle "A-Z" type shorthands
258
p++;
259
for
(
tchar
c =
tchar
(p[-2]+1); c < *p; c++)
// replace "-" with "B..Y"
260
EnableItem
(
static_cast<
uint8
>
(c));
261
}
262
EnableItem
(
static_cast<
uint8
>
(*p));
263
}
264
}
265
266
}
// OWL namespace
267
//==============================================================================
bitset.h
Definition of a bit set and a character set.
owl::TBitSet
Simplifies setting and testing bits in a 32 count array of uint8 (32 bytes).
Definition
bitset.h:36
owl::TBitSet::operator-=
TBitSet & operator-=(T item)
Disable a specific bit.
Definition
bitset.cpp:94
owl::TBitSet::operator~
TBitSet operator~() const
Negate a bit.
Definition
bitset.cpp:52
owl::TBitSet::DisableItem
void DisableItem(T item)
Turn off a specific bit.
Definition
bitset.cpp:65
owl::TBitSet::operator+=
TBitSet & operator+=(T item)
Enable a specific bit.
Definition
bitset.cpp:84
owl::TBitSet::TBitSet
TBitSet()
Constructs a TBitSet object with all bits set to 0.
Definition
bitset.cpp:21
owl::TBitSet::Has
bool Has(T item) const
Return true if a bit is turned on.
Definition
bitset.cpp:42
owl::TBitSet::operator|=
TBitSet & operator|=(const TBitSet &bs)
Enable all the given bits.
Definition
bitset.cpp:136
owl::TBitSet::EnableItem
void EnableItem(T item)
Turn on a specific bit.
Definition
bitset.cpp:75
owl::TBitSet::IsEmpty
bool IsEmpty() const
Return true if all bits are off.
Definition
bitset.cpp:169
owl::TBitSet::operator&=
TBitSet & operator&=(const TBitSet &bs)
Logically AND each individual bits.
Definition
bitset.cpp:157
owl::TCharSet::TCharSet
TCharSet()
Construct a default character set.
Definition
bitset.cpp:210
_T
#define _T(x)
Definition
cygwin.h:51
pch.h
owl
Object Windows Library (OWLNext Core).
Definition
animctrl.h:22
owl::uint8
unsigned char uint8
Definition
number.h:32
owl::tchar
char tchar
Definition
defs.h:77
owl::OWL_DIAGINFO
OWL_DIAGINFO
Definition
animctrl.cpp:14
owl::tstring
std::string tstring
Definition
defs.h:79
objstrm.h
source
owlcore
bitset.cpp
Generated by
1.18.0