MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BitVector.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  BitVector.h Extended integral type
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef BITVECTOR_H
28 #define BITVECTOR_H
29 
30 #include "itype.h"
31 #include "ByteSink.h"
32 #include "ByteSource.h"
33 #include "Util.h" /* for MakeMaskClip */
34 #include <climits> /* for CHAR_BIT */
35 #include <stdlib.h> /* for abort */
36 
37 namespace MFM {
38 
46  template <u32 B>
47  class BitVector
48  {
49  public:
50  enum { BITS = B };
51 
62  u32 GetLength() const
63  {
64  return BITS;
65  }
66 
67  typedef u32 BitUnitType;
68  static const u32 BITS_PER_UNIT = sizeof(BitUnitType) * CHAR_BIT;
69 
70  static const u32 ARRAY_LENGTH = (BITS + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
71 
72  private:
73  BitUnitType m_bits[ARRAY_LENGTH];
74 
81  inline void WriteToUnit(const u32 idx, const u32 startIdx, const u32 length, const u32 value) {
82  if (length == 0) return;
83  const u32 shift = BITS_PER_UNIT - (startIdx + length);
84  u32 mask = MakeMaskClip(length) << shift;
85  m_bits[idx] = (m_bits[idx] & ~mask) | ((value << shift) & mask);
86  }
87 
94  inline u32 ReadFromUnit(const u32 idx, const u32 startIdx, const u32 length) const {
95  if (length==0) { return 0; }
96  if(idx >= ARRAY_LENGTH) abort();
97  const u32 shift = BITS_PER_UNIT - (startIdx + length);
98  return (m_bits[idx] >> shift) & MakeMaskClip(length);
99  }
100 
101  public:
102 
110  void WriteBit(u32 idx, bool bit);
111 
119  bool ReadBit(u32 idx);
120 
125  BitVector();
126 
133  BitVector(const BitVector & other);
134 
143  BitVector(const u32 * const values);
144 
157  inline u32 Read(const u32 startIdx, const u32 length) const;
158 
171  void Write(const u32 startIdx, const u32 length, const u32 value);
172 
185  inline u64 ReadLong(const u32 startIdx, const u32 length) const;
186 
199  void WriteLong(const u32 startIdx, const u32 length, const u64 value);
200 
207  void SetBit(const u32 idx) {
208  Write(idx, 1, 1);
209  }
210 
217  void ClearBit(const u32 idx) {
218  Write(idx, 1, 0);
219  }
220 
228  bool ReadBit(const u32 idx) const {
229  return Read(idx, 1) != 0;
230  }
231 
239  bool ToggleBit(const u32 idx);
240 
252  void SetBits(const u32 startIdx, const u32 length) {
253  StoreBits(0xffffffff, startIdx, length);
254  }
255 
267  void ClearBits(const u32 startIdx, const u32 length) {
268  StoreBits(0, startIdx, length);
269  }
270 
290  void StoreBits(const u32 bits, const u32 startIdx, const u32 length) ;
291 
295  void Clear();
296 
303  void Print(ByteSink & ostream) const;
304 
311  void PrintBinary(ByteSink& ostream) const;
312 
326  bool Read(ByteSource & bs);
327 
341  bool ReadBinary(ByteSource& bs);
342 
343  bool operator==(const BitVector & rhs) const;
344 
345  };
346 } /* namespace MFM */
347 
348 #include "BitVector.tcc"
349 
350 #endif /*BITVECTOR_H*/
u32 GetLength() const
Definition: BitVector.h:62
void Write(const u32 startIdx, const u32 length, const u32 value)
Definition: BitVector.tcc:99
void Clear()
Definition: BitVector.tcc:26
u32 Read(const u32 startIdx, const u32 length) const
Definition: BitVector.tcc:129
bool ReadBit(const u32 idx) const
Definition: BitVector.h:228
void ClearBits(const u32 startIdx, const u32 length)
Definition: BitVector.h:267
void WriteBit(u32 idx, bool bit)
Definition: BitVector.tcc:41
void Print(ByteSink &ostream) const
Definition: BitVector.tcc:202
void StoreBits(const u32 bits, const u32 startIdx, const u32 length)
Definition: BitVector.tcc:160
void ClearBit(const u32 idx)
Definition: BitVector.h:217
Definition: ByteSource.h:44
bool ToggleBit(const u32 idx)
Definition: BitVector.tcc:54
void SetBit(const u32 idx)
Definition: BitVector.h:207
bool ReadBinary(ByteSource &bs)
Definition: BitVector.tcc:239
Definition: ByteSink.h:47
void PrintBinary(ByteSink &ostream) const
Definition: BitVector.tcc:209
void SetBits(const u32 startIdx, const u32 length)
Definition: BitVector.h:252
bool ReadBit(u32 idx)
Definition: BitVector.tcc:65
BitVector()
Definition: BitVector.tcc:8
Definition: BitVector.h:47
void WriteLong(const u32 startIdx, const u32 length, const u64 value)
Definition: BitVector.tcc:87
u64 ReadLong(const u32 startIdx, const u32 length) const
Definition: BitVector.tcc:74