34 #if __BYTE_ORDER != __LITTLE_ENDIAN 35 #error "iobitstream not tested on big endian systems" 100 void testMask(
unsigned int mSize, VTYPE compareAgainst)
const {
103 for (
int j=0; j<mSize; j++)
104 maskCheck|=(maskCheck<<1|1);
105 if (maskCheck!=compareAgainst) {
106 fprintf(stderr,
"BitStream::testMask(%d) failed\n",mSize);
107 fprintf(stderr,
"BitStream::compare against 0x%x\n",compareAgainst);
108 fprintf(stderr,
"BitStream::compare maskcheck 0x%x\n",maskCheck);
109 assert(
"BitStream : testMask is failing");
135 return sizeof(
VTYPE)*CHAR_BIT;
151 if (M>
sizeof(VTYPE)*CHAR_BIT)
152 assert(
"BitStream::maskBitsToRight : you can't shift right more bits then the word size. M > sizeof(VTYPE)*8");
163 if (M+offset>
sizeof(VTYPE)*CHAR_BIT)
164 assert(
"BitStream::maskOffsetBitsToRight : you can't shift right more bits then the word size. M+offset > sizeof(VTYPE)*CHAR_BIT");
174 if (M>
sizeof(VTYPE)*CHAR_BIT)
175 assert(
"BitStream::maskBitsToLeft : you can't shift left more bits then the word size. M > sizeof(VTYPE)*8");
187 for (
int i=0; i<N/2; i++) {
188 char lastChar=revChars[bits[N-1-i]];
189 bits[N-1-i]=revChars[bits[i]];
193 bits[N/2]=revChars[bits[N/2]];
201 VTYPE
shiftLeftSubword(std::vector<VTYPE>::iterator firstWord, std::vector<VTYPE>::iterator lastWord,
const unsigned int N);
208 VTYPE
shiftRightSubword(std::vector<VTYPE>::iterator firstWord, std::vector<VTYPE>::iterator lastWord,
const unsigned int N);
216 #ifndef NDEBUG // if debugging, test the mask by default 217 testMask(M, (VTYPE)((
unsigned long )pow(2.,(
float)M)-1));
219 return (VTYPE)((
unsigned long )pow(2.,(
float)M)-1);
221 #else // mingw seems to have trouble with the unsigned long 223 #ifndef NDEBUG // if debugging, test the mask by default 224 testMask(M, (VTYPE)((
unsigned long long )pow(2.,(
float)M)-1));
226 return (VTYPE)((
unsigned long long)pow(2.,(
float)M)-1);
238 std::vector<VTYPE>::size_type
size()
const;
253 unsigned int N=
sizeof(T)*CHAR_BIT;
266 unsigned int charCnt=
sizeof(T);
267 if (N>charCnt*CHAR_BIT) N=charCnt*CHAR_BIT;
269 bits>>=charCnt*CHAR_BIT-
N;
280 int N=
sizeof(T)*CHAR_BIT;
294 VTYPE *tempBits=(VTYPE*)&bits;
312 if (NN && (
size()>0)) {
318 data.resize(data.size()-1);
324 bits=pop_back<T>(tb);
325 T newBits=pop_back<T>(NN);
326 bits|=(
genMask(NN)&newBits)<<tb;
342 return pop_back<T>(
N);
361 std::ostream&
hexDump(std::ostream& stream);
379 int reserve(std::vector<VTYPE>::size_type N);
384 std::vector<VTYPE>::size_type
capacity()
const;
402 T
getBits(std::vector<VTYPE>::size_type i,
unsigned int N)
const {
404 assert(
"BitStream::operator[] : you requested an index which is out of range. The bitstream is smaller then your starting point and the size of your requested type.");
406 unsigned int wordLoc=i-whichWord*
VTYPEBits();
407 unsigned int M=std::min<unsigned int>(
N,
VTYPEBits()-wordLoc);
411 M=std::min<unsigned int>(N-=M,
VTYPEBits());
426 return getBits<T>(i,
sizeof(T)*CHAR_BIT);
435 std::vector<std::vector<VTYPE>::size_type>
find(T toFind,
const unsigned int N)
const {
438 return find(toFindRef, N);
446 std::vector<std::vector<VTYPE>::size_type>
find(
BitStream toFind,
const unsigned int N)
const;
449 #endif // BITSTREAM_H_ void testMask(unsigned int mSize) const
unsigned int takenBits() const
T reverseBits(T bits, unsigned int N) const
VTYPE shiftRightSubword(std::vector< VTYPE >::iterator firstWord, std::vector< VTYPE >::iterator lastWord, const unsigned int N)
BitStream & operator<<(T bits)
virtual ~BitStream()
Destructor.
std::ostream & hexDump(std::ostream &stream)
std::vector< VTYPE > data
The array to hold the bitstream.
T pop_back(const unsigned int N)
void testMask(unsigned int mSize, VTYPE compareAgainst) const
BitStream & rotateR(const unsigned int N)
T pop_front(const unsigned int N)
T operator[](std::vector< VTYPE >::size_type i) const
void reverseBits(unsigned char *bits, const unsigned int N) const
std::vector< VTYPE >::size_type capacity() const
VTYPE shiftLeftSubword(std::vector< VTYPE >::iterator firstWord, std::vector< VTYPE >::iterator lastWord, const unsigned int N)
T getBits(std::vector< VTYPE >::size_type i, unsigned int N) const
static const unsigned char revChars[]
Characters reversed. 8 bit reversals.
VTYPE maskOffsetBitsToRight(const unsigned int M, const unsigned int offset, const VTYPE &bits) const
BitStream & push_back(const T bits, const int N)
VTYPE genMask(int M) const
VTYPE maskBitsToLeft(const unsigned int M, const VTYPE bits) const
unsigned int VTYPE
Use this type for the basic stream type.
T reverseBits(T bits) const
int freeBits
The number of free bits in the array.
int reserve(std::vector< VTYPE >::size_type N)
std::vector< VTYPE >::size_type size() const
BitStream & push_backVType(const VTYPE *tempBits, int N, const int sizeOfT)
BitStream & rotateL(const unsigned int N)
VTYPE maskBitsToRight(const unsigned int M, const VTYPE &bits) const
std::vector< std::vector< VTYPE >::size_type > find(T toFind, const unsigned int N) const
unsigned int VTYPEBits() const