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 
36 class BitArray
37 {
38  public:
39 
43  enum Result
44  {
48  };
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 */
BitArray::set
void set(const Size bit, const bool value=true)
Sets the given bit to the given value.
Definition: BitArray.cpp:50
Macros.h
BitArray::isSet
bool isSet(const Size bit) const
Verify if a given bit is set.
Definition: BitArray.cpp:82
Types.h
BitArray::setRange
void setRange(const Size from, const Size to)
Set a range of bits inside the map to 1.
Definition: BitArray.cpp:89
BitArray::m_array
u8 * m_array
Array containing the bits.
Definition: BitArray.h:186
BitArray::array
u8 * array() const
Retrieve a pointer to the internal BitArray.
Definition: BitArray.cpp:165
Assert.h
BitArray::BitArray
BitArray(const Size bitCount, u8 *array=ZERO)
Class constructor.
Definition: BitArray.cpp:22
BitArray::~BitArray
virtual ~BitArray()
Class destructor.
Definition: BitArray.cpp:32
BitArray::m_set
Size m_set
Set bits in the array.
Definition: BitArray.h:183
BitArray::Result
Result
Result codes.
Definition: BitArray.h:43
BitArray::Success
@ Success
Definition: BitArray.h:45
BitArray::setNext
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
BitArray::unset
void unset(const Size bit)
Sets the given bit to zero.
Definition: BitArray.cpp:77
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
BitArray::m_bitCount
Size m_bitCount
Total number of bits in the array.
Definition: BitArray.h:180
BitArray::count
Size count(const bool on) const
Get the number of bits in the map which have the given value.
Definition: BitArray.cpp:45
BitArray::InvalidArgument
@ InvalidArgument
Definition: BitArray.h:46
BitArray::calculateBitmapSize
Size calculateBitmapSize(const Size bitCount) const
Calculate required size of bitmap array in bytes.
Definition: BitArray.cpp:218
BitArray::m_allocated
bool m_allocated
True if m_array was allocated interally.
Definition: BitArray.h:189
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
BitArray::setArray
void setArray(u8 *array, const Size bitCount=ZERO)
Use the given pointer as the BitArray buffer.
Definition: BitArray.cpp:170
BitArray::OutOfMemory
@ OutOfMemory
Definition: BitArray.h:47
BitArray::clear
void clear()
Set all bits to zero.
Definition: BitArray.cpp:199
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
BitArray
Represents an array of bits.
Definition: BitArray.h:36
BitArray::size
Size size() const
Returns the maximum size of this Container.
Definition: BitArray.cpp:40
BitArray::operator[]
bool operator[](const Size bit) const
Retrieve the value of the given bit.
Definition: BitArray.cpp:208