panga
BitVector.h
1 //-------------------------------------------------------------------------------------------------------
2 // Copyright (C) Taylor Woll and panga contributors. All rights reserved.
3 // Licensed under the MIT license. See LICENSE.txt file in the project root for
4 // full license information.
5 //-------------------------------------------------------------------------------------------------------
6 
7 #ifndef BITVECTOR_H__
8 #define BITVECTOR_H__
9 
10 #include <cassert>
11 #include <climits>
12 #include <cstddef>
13 #include <cstdint>
14 #include <iostream>
15 #include <vector>
16 
17 namespace panga {
18 
19 class BitVector {
20  public:
21  explicit BitVector(size_t bit_count = 0);
22  BitVector(const BitVector& source);
23  ~BitVector() = default;
24 
29  BitVector& operator=(const BitVector& rhs);
30 
36  bool Equals(const BitVector& rhs, size_t bits_to_compare) const;
37 
42  bool Equals(const BitVector& rhs) const;
43 
48  void SetBitCount(size_t bit_count);
49 
53  size_t GetBitCount() const;
54 
60  void Clip(size_t bit_count);
61 
65  void Clear();
66 
71  void Set(size_t index);
72 
77  void Unset(size_t index);
78 
84  bool Get(size_t index) const;
85 
90  void Flip(size_t index);
91 
98  template <typename IntegerType = uint64_t>
99  IntegerType GetInt(size_t bit_index, size_t bit_width) const {
100  assert((bit_index + bit_width) <= this->bit_count_);
101  return ReadInt<IntegerType>(this->bytes_.data(), bit_index, bit_width);
102  }
103 
111  template <typename IntegerType = uint64_t>
112  void SetInt(IntegerType value, size_t bit_index, size_t bit_width) {
113  assert((bit_index + bit_width) <= this->bit_count_);
114  WriteInt<IntegerType>(this->bytes_.data(), bit_index, bit_width, value);
115  }
116 
126  void SubVector(BitVector* destination, size_t destination_start_bit_offset,
127  size_t source_start_bit_offset, size_t bits_to_copy) const;
128 
135  size_t HammingDistance(const BitVector& rhs) const;
136 
149  size_t ToString(char* buffer, size_t buffer_length) const;
150 
156  void FromString(const char* buffer, size_t buffer_length);
157 
169  size_t ToStringHex(char* buffer, size_t buffer_length) const;
170 
177  void FromStringHex(const char* buffer, size_t buffer_length);
178 
179  protected:
183  const std::vector<std::byte>& GetBytes() const;
184 
188  std::vector<std::byte>& GetBytesWritable();
189 
195  void Resize(size_t bit_count);
196 
201  template <typename IntegerType = uint64_t>
202  static void WriteInt(std::byte* bytes, size_t start_bit_index,
203  size_t bits_to_copy, IntegerType value) {
204  assert(bits_to_copy <= CHAR_BIT * sizeof(IntegerType));
205  WriteBytes(reinterpret_cast<std::byte*>(&value), 0, bytes, start_bit_index,
206  bits_to_copy);
207  }
208 
213  template <typename IntegerType = uint64_t>
214  static IntegerType ReadInt(const std::byte* bytes, size_t start_bit_index,
215  size_t bits_to_copy) {
216  assert(bits_to_copy <= CHAR_BIT * sizeof(IntegerType));
217  IntegerType value = 0;
218  WriteBytes(bytes, start_bit_index, reinterpret_cast<std::byte*>(&value), 0,
219  bits_to_copy);
220  return value;
221  }
222 
229  static void WriteBytes(const std::byte* source,
230  size_t source_start_bit_offset, std::byte* destination,
231  size_t destination_start_bit_offset,
232  size_t bits_to_copy);
233 
239  static bool Compare(const std::byte* left, const std::byte* right,
240  size_t bits_to_compare);
241 
242  public:
244  std::ostream& _os;
245  };
246  struct HexFormat_t {
247  friend HexFormatWrapper operator<<(std::ostream& out,
248  const HexFormat_t& manip);
249  };
250 
262 
263  friend std::ostream& operator<<(const HexFormatWrapper& wrapper,
264  const BitVector& bv);
265  friend std::ostream& operator<<(std::ostream& out, const BitVector& bv);
266 
267  protected:
268  void WriteToStream(std::ostream& out) const;
269  void WriteToStreamHex(std::ostream& out) const;
270 
271  private:
275  std::vector<std::byte> bytes_;
276 
282  size_t bit_count_ = 0;
283 };
284 
285 } // namespace panga
286 
287 #endif // BITVECTOR_H__
panga::BitVector::SetInt
void SetInt(IntegerType value, size_t bit_index, size_t bit_width)
Definition: BitVector.h:112
panga::BitVector::Clip
void Clip(size_t bit_count)
Definition: BitVector.cc:187
panga::BitVector::Get
bool Get(size_t index) const
Definition: BitVector.cc:224
panga::BitVector::SetBitCount
void SetBitCount(size_t bit_count)
Definition: BitVector.cc:182
panga::BitVector::HammingDistance
size_t HammingDistance(const BitVector &rhs) const
Definition: BitVector.cc:246
panga::BitVector::FromStringHex
void FromStringHex(const char *buffer, size_t buffer_length)
Definition: BitVector.cc:339
panga::BitVector::ToString
size_t ToString(char *buffer, size_t buffer_length) const
Definition: BitVector.cc:286
panga::BitVector::ToStringHex
size_t ToStringHex(char *buffer, size_t buffer_length) const
Definition: BitVector.cc:313
panga::BitVector::Equals
bool Equals(const BitVector &rhs, size_t bits_to_compare) const
Definition: BitVector.cc:275
panga::BitVector::GetBytes
const std::vector< std::byte > & GetBytes() const
Definition: BitVector.cc:62
panga::BitVector::SubVector
void SubVector(BitVector *destination, size_t destination_start_bit_offset, size_t source_start_bit_offset, size_t bits_to_copy) const
Definition: BitVector.cc:234
panga::BitVector::HexFormat
static HexFormat_t HexFormat
Definition: BitVector.h:261
panga::BitVector::GetInt
IntegerType GetInt(size_t bit_index, size_t bit_width) const
Definition: BitVector.h:99
panga::BitVector::Resize
void Resize(size_t bit_count)
Definition: BitVector.cc:164
panga::BitVector::ReadInt
static IntegerType ReadInt(const std::byte *bytes, size_t start_bit_index, size_t bits_to_copy)
Definition: BitVector.h:214
panga::BitVector::FromString
void FromString(const char *buffer, size_t buffer_length)
Definition: BitVector.cc:299
panga::BitVector::WriteBytes
static void WriteBytes(const std::byte *source, size_t source_start_bit_offset, std::byte *destination, size_t destination_start_bit_offset, size_t bits_to_copy)
Definition: BitVector.cc:67
panga::BitVector
Definition: BitVector.h:19
panga::BitVector::GetBytesWritable
std::vector< std::byte > & GetBytesWritable()
Definition: BitVector.cc:64
panga::BitVector::Clear
void Clear()
Definition: BitVector.cc:160
panga::BitVector::Flip
void Flip(size_t index)
Definition: BitVector.cc:197
panga::BitVector::operator=
BitVector & operator=(const BitVector &rhs)
Definition: BitVector.cc:54
panga::BitVector::Set
void Set(size_t index)
Definition: BitVector.cc:206
panga::BitVector::WriteInt
static void WriteInt(std::byte *bytes, size_t start_bit_index, size_t bits_to_copy, IntegerType value)
Definition: BitVector.h:202
panga::BitVector::Compare
static bool Compare(const std::byte *left, const std::byte *right, size_t bits_to_compare)
Definition: BitVector.cc:141
panga::BitVector::HexFormat_t
Definition: BitVector.h:246
panga::BitVector::GetBitCount
size_t GetBitCount() const
Definition: BitVector.cc:195
panga::BitVector::Unset
void Unset(size_t index)
Definition: BitVector.cc:215
panga::BitVector::HexFormatWrapper
Definition: BitVector.h:243