INET Framework for OMNeT++/OMNEST
inet::BitVector Class Reference

#include <BitVector.h>

Public Member Functions

 BitVector ()
 
 BitVector (const char *bits)
 
 BitVector (unsigned int bits)
 
 BitVector (unsigned int bits, unsigned int fixedSize)
 
 BitVector (const BitVector &other)
 
unsigned int toDecimal () const
 
unsigned int reverseToDecimal () const
 
void appendBit (bool value)
 
void appendBit (bool value, int n)
 
void setBit (int pos, bool value)
 
void toggleBit (int pos)
 
bool getBit (int pos) const
 
void appendByte (uint8_t value)
 
unsigned int getSize () const
 
unsigned int getNumberOfBytes () const
 
const std::vector< uint8 > & getBytes () const
 
int computeHammingDistance (const BitVector &u) const
 
BitVectoroperator= (const BitVector &rhs)
 
bool operator== (const BitVector &rhs) const
 
bool operator!= (const BitVector &rhs) const
 
std::string toString () const
 

Private Member Functions

int containerSize () const
 
void stringToBitVector (const char *str)
 
void copy (const BitVector &other)
 

Private Attributes

std::vector< uint8bytes
 
int size
 

Friends

std::ostream & operator<< (std::ostream &out, const BitVector &bitVector)
 

Constructor & Destructor Documentation

inet::BitVector::BitVector ( )
24 {
25  size = 0;
26  bytes.push_back(uint8(0));
27 }
uint8_t uint8
Definition: Compat.h:34
std::vector< uint8 > bytes
Definition: BitVector.h:30
int size
Definition: BitVector.h:31
inet::BitVector::BitVector ( const char *  bits)
30 {
31  size = 0;
32  stringToBitVector(bits);
33 }
int size
Definition: BitVector.h:31
void stringToBitVector(const char *str)
Definition: BitVector.cc:136
inet::BitVector::BitVector ( unsigned int  bits)
36 {
37  size = 0;
38  if (bits == 0)
39  appendBit(false);
40  while (bits > 0)
41  {
42  appendBit(bits % 2);
43  bits /= 2;
44  }
45 }
int size
Definition: BitVector.h:31
void appendBit(bool value)
Definition: BitVector.cc:106
inet::BitVector::BitVector ( unsigned int  bits,
unsigned int  fixedSize 
)
48 {
49  this->size = 0;
50  if (bits == 0)
51  appendBit(false);
52  while (bits > 0)
53  {
54  appendBit(bits % 2);
55  bits /= 2;
56  }
57  if (getSize() < size)
58  appendBit(false, size - getSize());
59 }
unsigned int getSize() const
Definition: BitVector.h:53
int size
Definition: BitVector.h:31
void appendBit(bool value)
Definition: BitVector.cc:106
inet::BitVector::BitVector ( const BitVector other)
inline
43 { copy(other); }
void copy(const BitVector &other)
Definition: BitVector.cc:209

Member Function Documentation

void inet::BitVector::appendBit ( bool  value)

Referenced by appendBit(), appendByte(), inet::physicallayer::Ieee80211LayeredOFDMTransmitter::appendPadding(), BitVector(), copy(), inet::physicallayer::Ieee80211LayeredOFDMTransmitter::createBitModel(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::createCompleteBitModel(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::createCompletePacketModel(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::createDataFieldBitModel(), inet::physicallayer::Ieee80211LayeredOFDMTransmitter::createDataFieldPacketModel(), inet::physicallayer::APSKLayeredTransmitter::createPacketModel(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::createSignalFieldBitModel(), inet::physicallayer::Ieee80211LayeredOFDMTransmitter::createSignalFieldPacketModel(), inet::physicallayer::APSKDemodulator::demodulate(), inet::physicallayer::Ieee80211OFDMDemodulator::demodulate(), inet::physicallayer::Ieee80211OFDMDemodulator::demodulateSymbol(), inet::physicallayer::ConvolutionalCoder::depuncturing(), inet::physicallayer::ConvolutionalCoder::encode(), inet::physicallayer::AdditiveScrambler::generateScramblingSequence(), inet::physicallayer::ConvolutionalCoder::getPuncturedIndices(), inet::physicallayer::ConvolutionalCoder::puncturing(), inet::physicallayer::AdditiveScrambler::scramble(), inet::serializer::Ieee80211PhySerializer::serialize(), stringToBitVector(), inet::physicallayer::ConvolutionalCoder::traversePath(), and inet::serializer::Ieee80211PhySerializer::writeToBitVector().

107 {
108  setBit(size, value);
109 }
int size
Definition: BitVector.h:31
void setBit(int pos, bool value)
Definition: BitVector.cc:61
void inet::BitVector::appendBit ( bool  value,
int  n 
)
112 {
113  while (n--)
114  appendBit(value);
115 }
void appendBit(bool value)
Definition: BitVector.cc:106
void inet::BitVector::appendByte ( uint8_t  value)

Referenced by inet::physicallayer::APSKPhyFrameSerializer::serialize().

118 {
119  for (unsigned int i = 0; i < 8; i++)
120  appendBit(value & (1 << i));
121 }
void appendBit(bool value)
Definition: BitVector.cc:106
int inet::BitVector::computeHammingDistance ( const BitVector u) const
176 {
177  if (getSize() != u.getSize())
178  throw cRuntimeError("You can't compute Hamming distance between two vectors with different sizes");
179  int hammingDistance = 0;
180  for (unsigned int i = 0; i < getSize(); i++)
181  if (u.getBit(i) != getBit(i))
182  hammingDistance++;
183  return hammingDistance;
184 }
unsigned int getSize() const
Definition: BitVector.h:53
bool getBit(int pos) const
Definition: BitVector.cc:82
int inet::BitVector::containerSize ( ) const
inlineprivate

Referenced by setBit().

34 { return bytes.size() * 8; }
std::vector< uint8 > bytes
Definition: BitVector.h:30
void inet::BitVector::copy ( const BitVector other)
private

Referenced by operator=().

210 {
211  size = 0;
212  bytes.clear();
213  for (unsigned int i = 0; i < other.getSize(); i++)
214  appendBit(other.getBit(i));
215 }
std::vector< uint8 > bytes
Definition: BitVector.h:30
int size
Definition: BitVector.h:31
void appendBit(bool value)
Definition: BitVector.cc:106
bool inet::BitVector::getBit ( int  pos) const

Referenced by computeHammingDistance(), copy(), inet::physicallayer::Ieee80211LayeredOFDMTransmitter::createBitModel(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::createCompletePacketModel(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::createDataFieldBitModel(), inet::physicallayer::Ieee80211LayeredOFDMTransmitter::createDataFieldPacketModel(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::createSignalFieldBitModel(), inet::physicallayer::Ieee80211LayeredOFDMTransmitter::createSignalFieldPacketModel(), inet::physicallayer::Ieee80211OFDMInterleaver::deinterleave(), inet::physicallayer::Ieee80211OFDMDemodulator::demodulate(), inet::physicallayer::ConvolutionalCoder::depuncturing(), inet::physicallayer::ConvolutionalCoder::encode(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::getRate(), inet::physicallayer::Ieee80211OFDMDecoder::getSignalFieldLength(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::getSignalFieldLength(), inet::physicallayer::Ieee80211OFDMDecoder::getSignalFieldRate(), inet::physicallayer::ConvolutionalCoder::giveNextOutputSymbol(), inet::physicallayer::Ieee80211OFDMInterleaver::interleave(), inet::physicallayer::APSKModulator::modulate(), inet::physicallayer::Ieee80211OFDMModulator::modulate(), inet::operator<<(), operator==(), inet::ieee80211::QoSAckHandler::processReceivedBlockAck(), inet::physicallayer::ConvolutionalCoder::puncturing(), reverseToDecimal(), inet::physicallayer::AdditiveScrambler::scramble(), toDecimal(), toString(), and inet::physicallayer::ConvolutionalCoder::traversePath().

83 {
84  if (pos >= size)
85  throw cRuntimeError("Out of range with bit position %d", pos);
86  uint8 field = bytes[pos / UINT8_LENGTH];
87  return field & (1 << (pos % UINT8_LENGTH));
88 }
uint8_t uint8
Definition: Compat.h:34
std::vector< uint8 > bytes
Definition: BitVector.h:30
int size
Definition: BitVector.h:31
#define UINT8_LENGTH
Definition: BitVector.h:25
const std::vector<uint8>& inet::BitVector::getBytes ( ) const
inline
unsigned int inet::BitVector::getNumberOfBytes ( ) const
inline
54 { return bytes.size(); }
std::vector< uint8 > bytes
Definition: BitVector.h:30
unsigned int inet::BitVector::getSize ( ) const
inline

Referenced by BitVector(), inet::physicallayer::LayeredErrorModelBase::computeBitModel(), inet::physicallayer::Ieee80211OFDMErrorModel::computeBitModel(), computeHammingDistance(), inet::physicallayer::APSKLayeredTransmitter::computePaddingLength(), copy(), inet::physicallayer::APSKLayeredTransmitter::createBitModel(), inet::physicallayer::Ieee80211LayeredOFDMTransmitter::createBitModel(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::createCompletePacketModel(), inet::physicallayer::Ieee80211LayeredOFDMReceiver::createDataFieldBitModel(), inet::physicallayer::Ieee80211LayeredOFDMTransmitter::createDataFieldPacketModel(), inet::physicallayer::Ieee80211LayeredOFDMTransmitter::createScalarAnalogModel(), inet::physicallayer::ConvolutionalCoder::decode(), inet::physicallayer::Ieee80211OFDMInterleaver::deinterleave(), inet::physicallayer::Ieee80211OFDMDemodulator::demodulate(), inet::physicallayer::ConvolutionalCoder::depuncturing(), inet::physicallayer::APSKEncoder::encode(), inet::physicallayer::ConvolutionalCoder::encode(), inet::physicallayer::ConvolutionalCoder::giveNextOutputSymbol(), inet::physicallayer::Ieee80211OFDMInterleaver::interleave(), inet::physicallayer::APSKModulator::modulate(), inet::physicallayer::Ieee80211OFDMModulator::modulate(), operator==(), inet::physicallayer::ConvolutionalCoder::puncturing(), reverseToDecimal(), inet::physicallayer::AdditiveScrambler::scramble(), toDecimal(), toString(), and inet::physicallayer::ConvolutionalCoder::traversePath().

53 { return size; }
int size
Definition: BitVector.h:31
bool inet::BitVector::operator!= ( const BitVector rhs) const
205 {
206  return !(rhs == *this);
207 }
BitVector & inet::BitVector::operator= ( const BitVector rhs)
187 {
188  if (this == &rhs)
189  return *this;
190  copy(rhs);
191  return *this;
192 }
void copy(const BitVector &other)
Definition: BitVector.cc:209
bool inet::BitVector::operator== ( const BitVector rhs) const
195 {
196  if (rhs.getSize() != getSize())
197  return false;
198  for (unsigned int i = 0; i < getSize(); i++)
199  if (getBit(i) != rhs.getBit(i))
200  return false;
201  return true;
202 }
unsigned int getSize() const
Definition: BitVector.h:53
bool getBit(int pos) const
Definition: BitVector.cc:82
unsigned int inet::BitVector::reverseToDecimal ( ) const
163 {
164  unsigned int dec = 0;
165  unsigned int powerOfTwo = 1;
166  for (int i = getSize() - 1; i >= 0; i--)
167  {
168  if (getBit(i))
169  dec += powerOfTwo;
170  powerOfTwo *= 2;
171  }
172  return dec;
173 }
unsigned int getSize() const
Definition: BitVector.h:53
bool getBit(int pos) const
Definition: BitVector.cc:82
void inet::BitVector::setBit ( int  pos,
bool  value 
)

Referenced by appendBit(), inet::ieee80211::RecipientBlockAckProcedure::buildBlockAck(), inet::physicallayer::Ieee80211OFDMInterleaver::deinterleave(), inet::physicallayer::ConvolutionalCoder::getPuncturedIndices(), and inet::physicallayer::Ieee80211OFDMInterleaver::interleave().

62 {
63  while (containerSize() <= pos)
64  bytes.push_back(uint8(0));
65  if (pos + 1 > size)
66  size = pos + 1;
67  uint8& field = bytes[pos / UINT8_LENGTH];
68  if (value)
69  field |= 1 << (pos % UINT8_LENGTH);
70  else
71  field &= ~(1 << (pos % UINT8_LENGTH));
72 }
uint8_t uint8
Definition: Compat.h:34
int containerSize() const
Definition: BitVector.h:34
std::vector< uint8 > bytes
Definition: BitVector.h:30
int size
Definition: BitVector.h:31
#define UINT8_LENGTH
Definition: BitVector.h:25
void inet::BitVector::stringToBitVector ( const char *  str)
private

Referenced by BitVector().

137 {
138  int strSize = strlen(str);
139  for (int i = 0; i < strSize; i++)
140  {
141  if (str[i] == '1')
142  appendBit(true);
143  else if (str[i] == '0')
144  appendBit(false);
145  else
146  throw cRuntimeError("str must represent a binary number");
147  }
148 }
void appendBit(bool value)
Definition: BitVector.cc:106
unsigned int inet::BitVector::toDecimal ( ) const
151 {
152  unsigned int dec = 0;
153  unsigned int powerOfTwo = 1;
154  for (unsigned int i = 0; i < getSize(); i++)
155  {
156  if (getBit(i))
157  dec += powerOfTwo;
158  powerOfTwo *= 2;
159  }
160  return dec;
161 }
unsigned int getSize() const
Definition: BitVector.h:53
bool getBit(int pos) const
Definition: BitVector.cc:82
void inet::BitVector::toggleBit ( int  pos)

Referenced by inet::physicallayer::LayeredErrorModelBase::computeBitModel(), and inet::physicallayer::Ieee80211OFDMErrorModel::corruptBits().

75 {
76  if (pos >= size)
77  throw cRuntimeError("Out of range with bit position %d", pos);
78  uint8& field = bytes[pos / UINT8_LENGTH];
79  field ^= 1 << (pos % UINT8_LENGTH);
80 }
uint8_t uint8
Definition: Compat.h:34
std::vector< uint8 > bytes
Definition: BitVector.h:30
int size
Definition: BitVector.h:31
#define UINT8_LENGTH
Definition: BitVector.h:25
std::string inet::BitVector::toString ( ) const
124 {
125  std::string str;
126  for (unsigned int i = 0; i < getSize(); i++)
127  {
128  if (getBit(i))
129  str += "1";
130  else
131  str += "0";
132  }
133  return str;
134 }
unsigned int getSize() const
Definition: BitVector.h:53
bool getBit(int pos) const
Definition: BitVector.cc:82

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
const BitVector bitVector 
)
friend
91 {
92  if (bitVector.getBit(0))
93  out << "1";
94  else
95  out << "0";
96  for (int i = 1; i < bitVector.size; i++)
97  {
98  if (bitVector.getBit(i))
99  out << " 1";
100  else
101  out << " 0";
102  }
103  return out;
104 }

Member Data Documentation

std::vector<uint8> inet::BitVector::bytes
private
int inet::BitVector::size
private

The documentation for this class was generated from the following files: