23#ifndef SLIM_BITSTREAM_H
24#define SLIM_BITSTREAM_H
37#include "bit_constants.h"
45 int buffersize=DEFAULT_IOBUFFER_SIZE);
48 virtual void close() = 0;
49 virtual bool is_open()
const = 0;
51 virtual void windup()=0;
72 enum {DEFAULT_IOBUFFER_SIZE=1024*1024};
73 enum {MAX_BITSTREAM_BUFSIZE=16*1024*1024};
86 obitstream(FILE *file,
int buffersize=DEFAULT_IOBUFFER_SIZE);
88 int buffersize=DEFAULT_IOBUFFER_SIZE);
92 void writestring(
const char *str,
bool write_trailing_null=
false);
93 template <
typename T>
void writeword(
const T data);
95 virtual void print()
const;
99 void flush(
bool flush_trailing_bits);
109 int c1,c2, c3, c4, c5, c6;
119 LZ4F_decompressionContext_t lz4_ctx;
125 ibitstream(FILE *file,
int buffersize=DEFAULT_IOBUFFER_SIZE);
127 ibitstream(ZZIP_FILE *file,
int buffersize=DEFAULT_IOBUFFER_SIZE);
130 int buffersize=DEFAULT_IOBUFFER_SIZE);
131 ibitstream(
int fd,
int buffersize=DEFAULT_IOBUFFER_SIZE);
134 virtual void close();
138 virtual void print()
const;
151 int partial_word_bitptr;
170 if (i>lowestNset32bits[15]) {
171 if (i>lowestNset32bits[23]) {
172 if (i>lowestNset32bits[27]) {
173 if (i>lowestNset32bits[29]) {
174 if (i>lowestNset32bits[30]) {
180 if (i>lowestNset32bits[28]) {
187 if (i>lowestNset32bits[25]) {
188 if (i>lowestNset32bits[26]) {
194 if (i>lowestNset32bits[24]) {
202 if (i>lowestNset32bits[19]) {
203 if (i>lowestNset32bits[21]) {
204 if (i>lowestNset32bits[22]) {
210 if (i>lowestNset32bits[20]) {
217 if (i>lowestNset32bits[17]) {
218 if (i>lowestNset32bits[18]) {
224 if (i>lowestNset32bits[16]) {
233 if (i>lowestNset32bits[7]) {
234 if (i>lowestNset32bits[11]) {
235 if (i>lowestNset32bits[13]) {
236 if (i>lowestNset32bits[14]) {
242 if (i>lowestNset32bits[12]) {
249 if (i>lowestNset32bits[9]) {
250 if (i>lowestNset32bits[10]) {
256 if (i>lowestNset32bits[8]) {
264 if (i>lowestNset32bits[3]) {
265 if (i>lowestNset32bits[5]) {
266 if (i>lowestNset32bits[6]) {
272 if (i>lowestNset32bits[4]) {
279 if (i>lowestNset32bits[1]) {
280 if (i>lowestNset32bits[2]) {
286 if (i>lowestNset32bits[0]) {
303static inline unsigned int bit_size(
unsigned int u) {
304 for (
int bs=1; bs<=32; bs++)
305 if (u == (u&lowestNset32bits[bs]))
307 throw "Bit size (unsigned int) fails!";
static unsigned int bit_size(int32_t i)
Find size (on [0,32]) of the smallest # that can hold the integer i.
Definition bitstream.h:166
Bit stream base class.
Definition bitstream.h:41
static const int Bits_per_word
Bits per buffer word.
Definition bitstream.h:57
size_t bufsize
Size of I/O buffer (bytes)
Definition bitstream.h:59
Byte_t * Bptr
Pointer to the current word (as Byte_t *).
Definition bitstream.h:64
int get_bitptr()
Get the position of the bitptr.
Definition bitstream.cpp:170
bitstream()
Dummy default constructor.
Definition bitstream.cpp:46
virtual void setupstream()
Allocate a buffer and set up all pointers.
Definition bitstream.cpp:141
Byte_t * buffer_base
Pointer to the buffer.
Definition bitstream.h:61
size_t buf_used
Definition bitstream.h:60
int bitptr
Pointer to the current bits.
Definition bitstream.h:67
virtual void print() const =0
Print properties of stream (pure virt)
virtual int get_bytes_used()
Return the number of bytes used so far in this stream.
Definition bitstream.cpp:162
union bitstream::@0 buffptr
Pointer to the current word.
virtual ~bitstream()
Destructor deletes output buffer, closes file.
Definition bitstream.cpp:91
Word_t * Dptr
Pointer to the current word (as Word_t *).
Definition bitstream.h:65
Byte_t * beyondbuffer
Pointer just beyond buffer (convenience).
Definition bitstream.h:62
Input bit stream.
Definition bitstream.h:105
~ibitstream()
Destructor only uses base class destructor.
Definition bitstream.cpp:479
ibitstream(FILE *file, int buffersize=DEFAULT_IOBUFFER_SIZE)
Start ibitstream using a FILE ptr to an open file.
Definition bitstream.cpp:380
Word_t readbits(int nbits)
Read data from the buffer as unsigned ints.
Definition bitstream.cpp:556
void windup()
Put the current word to the buffer with upper 0 bits as needed.
Definition bitstream.cpp:543
virtual bool is_open() const
Is the IO file closed?
Definition bitstream.cpp:130
int readstring(char *s, int count=-1)
Read a null-terminated string of 8-bit characters from the bit stream.
Definition bitstream.cpp:590
Word_t read_unary()
Read a single unary-coded value.
Definition bitstream.cpp:622
virtual void close()
Close the IO file.
Definition bitstream.cpp:102
int fill()
Fill the read buffer and reset for more data.
Definition bitstream.cpp:661
virtual int get_bytes_used()
Return the number of bytes used so far in this stream.
Definition bitstream.cpp:509
void next_word()
Move to next word in buffer, refilling it if needed.
Definition bitstream.cpp:529
int32_t readbits_int(int nbits)
Read data from the buffer as (signed) ints.
Definition bitstream.cpp:607
void setupstream()
Allocate a buffer and set up all pointers, then fill buffer.
Definition bitstream.cpp:517
virtual void print() const
Print buffer contents.
Definition bitstream.cpp:636
Output bit stream.
Definition bitstream.h:78
void write_unary(unsigned int value)
Write a unary code for the value.
Definition bitstream.cpp:303
obitstream(FILE *file, int buffersize=DEFAULT_IOBUFFER_SIZE)
Start obitstream using a FILE ptr to an open file.
Definition bitstream.cpp:182
void writestring(const char *str, bool write_trailing_null=false)
Write 8-bit character strings to the buffer.
Definition bitstream.cpp:273
virtual void close()
Close the output stream by flushing and closing the FILE *.
Definition bitstream.cpp:211
void windup()
Put the current word to the buffer with upper 0 bits as needed.
Definition bitstream.cpp:224
virtual bool is_open() const
Is the IO file closed?
Definition bitstream.cpp:124
void flush(bool flush_trailing_bits)
Flush the write buffer and reset for more data.
Definition bitstream.cpp:351
void writebits(uint32_t data, int nbits)
Write data to the buffer.
Definition bitstream.cpp:237
void writeword(const T data)
Write all the bits of a word.
Definition bitstream.cpp:284
virtual void print() const
Print buffer contents.
Definition bitstream.cpp:320
FILE * fp
The I/O stream.
Definition bitstream.h:83