FreeNOS
BitArray.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __LIBSTD_BITARRAY_H
19#define __LIBSTD_BITARRAY_H
20
21#include "Macros.h"
22#include "Assert.h"
23#include "Types.h"
24
37{
38 public:
39
49
50 public:
51
58 BitArray(const Size bitCount, u8 *array = ZERO);
59
63 virtual ~BitArray();
64
70 Size size() const;
71
79 Size count(const bool on) const;
80
87 void set(const Size bit, const bool value = true);
88
95 void setRange(const Size from, const Size to);
96
107 Result setNext(Size *bit,
108 const Size count = 1,
109 const Size offset = 0,
110 const Size boundary = 1);
111
117 void unset(const Size bit);
118
122 void clear();
123
131 bool isSet(const Size bit) const;
132
138 u8 * array() const;
139
146 void setArray(u8 *array, const Size bitCount = ZERO);
147
155 bool operator[](const Size bit) const;
156
164 bool operator[](const int bit) const;
165
166 private:
167
175 Size calculateBitmapSize(const Size bitCount) const;
176
177 private:
178
181
184
187
190};
191
197#endif /* __LIBSTD_BITARRAY_H */
Represents an array of bits.
Definition BitArray.h:37
void setArray(u8 *array, const Size bitCount=ZERO)
Use the given pointer as the BitArray buffer.
Definition BitArray.cpp:170
u8 * array() const
Retrieve a pointer to the internal BitArray.
Definition BitArray.cpp:165
u8 * m_array
Array containing the bits.
Definition BitArray.h:186
void set(const Size bit, const bool value=true)
Sets the given bit to the given value.
Definition BitArray.cpp:50
Size m_set
Set bits in the array.
Definition BitArray.h:183
Size calculateBitmapSize(const Size bitCount) const
Calculate required size of bitmap array in bytes.
Definition BitArray.cpp:218
Size m_bitCount
Total number of bits in the array.
Definition BitArray.h:180
bool isSet(const Size bit) const
Verify if a given bit is set.
Definition BitArray.cpp:82
bool operator[](const Size bit) const
Retrieve the value of the given bit.
Definition BitArray.cpp:208
Size count(const bool on) const
Get the number of bits in the map which have the given value.
Definition BitArray.cpp:45
void unset(const Size bit)
Sets the given bit to zero.
Definition BitArray.cpp:77
virtual ~BitArray()
Class destructor.
Definition BitArray.cpp:32
void clear()
Set all bits to zero.
Definition BitArray.cpp:199
void setRange(const Size from, const Size to)
Set a range of bits inside the map to 1.
Definition BitArray.cpp:89
Result setNext(Size *bit, const Size count=1, const Size offset=0, const Size boundary=1)
Sets the next unset bit(s).
Definition BitArray.cpp:97
bool m_allocated
True if m_array was allocated interally.
Definition BitArray.h:189
Size size() const
Returns the maximum size of this Container.
Definition BitArray.cpp:40
Result
Result codes.
Definition BitArray.h:44
@ OutOfMemory
Definition BitArray.h:47
@ InvalidArgument
Definition BitArray.h:46
@ Success
Definition BitArray.h:45
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59