Modernized GAlib  3.0.0 current
GABinStr.hpp
1 /* ----------------------------------------------------------------------------
2  mbwall 30jun95
3  Copyright (c) 1995 Massachusetts Institute of Technology
4 ---------------------------------------------------------------------------- */
5 
6 #pragma once
7 
8 
9 #include <cstring>
10 #include <garandom.h>
11 #include <gatypes.h>
12 #include <vector>
13 
24 {
25  public:
26  explicit GABinaryString(unsigned int s)
27  {
28  resize(s);
29  }
30 
35  void copy(const GABinaryString &orig)
36  {
37  data = orig.data;
38  }
39 
48  int resize(unsigned int x)
49  {
50  data.resize(x);
51  return data.size();
52  }
53 
54  int size() const { return data.size(); }
55 
56  short bit(unsigned int a) const { return (data[a]); }
57 
58  short bit(unsigned int a, short val)
59  { // set/unset the bit
60  return (data[a] = (val != 0 ? 1 : 0));
61  }
62 
71  bool equal(const GABinaryString &rhs, unsigned int lhsIdx, unsigned int rhsIdx, unsigned int l) const
72  {
73  return (std::memcmp(&data[lhsIdx], &rhs.data[rhsIdx], l) != 0 ? false : true);
74  }
75 
84  void copy(const GABinaryString &orig, unsigned int destIdx, unsigned int origIdx, unsigned int l)
85  {
86  data.resize(orig.data.size());
87  std::memcpy(&data[destIdx], &orig.data[origIdx], l);
88  }
89 
98  void move(unsigned int destIdx, unsigned int sourceIdx, unsigned int l)
99  {
100  std::memmove(&data[destIdx], &data[sourceIdx], l);
101  }
102 
103  void set(unsigned int a, unsigned int l)
104  {
105  for (unsigned int i = a; i < l; i++)
106  {
107  data.at(i) = 1;
108  }
109  }
110 
111  void unset(unsigned int a, unsigned int l)
112  {
113  for (unsigned int i = a; i < l; i++)
114  {
115  data.at(i) = 0;
116  }
117  }
118 
119  void randomize(unsigned int a, unsigned int l)
120  {
121  for (unsigned int i = 0; i < l; i++)
122  {
123  data[i + a] = static_cast<GABit>(GARandomBit());
124  }
125  }
126 
127  void randomize()
128  {
129  for (auto &item : data)
130  {
131  item = static_cast<GABit>(GARandomBit());
132  }
133  }
134 
135  protected:
137  std::vector<GABit> data;
138 };
This header defines the interface for the binary string.
Definition: GABinStr.hpp:24
int resize(unsigned int x)
Resize the bitstream to the specified number of bits.
Definition: GABinStr.hpp:48
std::vector< GABit > data
the data themselves
Definition: GABinStr.hpp:137
void copy(const GABinaryString &orig, unsigned int destIdx, unsigned int origIdx, unsigned int l)
Definition: GABinStr.hpp:84
void copy(const GABinaryString &orig)
Copy the contents of the bitstream.
Definition: GABinStr.hpp:35
bool equal(const GABinaryString &rhs, unsigned int lhsIdx, unsigned int rhsIdx, unsigned int l) const
Are two (subset) bitstreams equal?
Definition: GABinStr.hpp:71
void move(unsigned int destIdx, unsigned int sourceIdx, unsigned int l)
Copy (sub) bitstream.
Definition: GABinStr.hpp:98