From dcc4520eaae826baf04cd5883ffa721ebe56eb6d Mon Sep 17 00:00:00 2001 From: Zack Grannan Date: Sat, 3 May 2014 17:26:21 -0700 Subject: [PATCH] Added BitInputStream + BitOutputStream --- BitInputStream.hpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ BitOutputStream.hpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 BitInputStream.hpp create mode 100644 BitOutputStream.hpp diff --git a/BitInputStream.hpp b/BitInputStream.hpp new file mode 100644 index 0000000..6d7f73c --- /dev/null +++ b/BitInputStream.hpp @@ -0,0 +1,53 @@ +#ifndef BITINPUTSTREAM_HPP +#define BITINPUTSTREAM_HPP + +#include +#include + +using namespace std; + +/** A class for reading bits (and ints) from an istream + */ +class BitInputStream { +private: + istream& in; // the istream to delegate to + char buf; // the buffer of bits + //bitset<8> buf; + int bufi; // the bit buffer index + +public: + /** Initialize a BitInputStream object, given an istream. + */ + BitInputStream(istream& s) : in(s), buf(0), bufi(8) { } + // BitInputStream(istream& s) : in(s), bufi(8) { } + + /** Read the next bit from the bit buffer. + * If the bit buffer is currently empty, + * fill the bit buffer by reading a char from the istream first. + * Return the bit read as the least signficant bit of an int. + * Return -1 on EOF. + * This must be consistent with BitOutputStream::writeBit(), in terms + * of ordering of bits in the stream. + */ + int readBit(); + + /** Read a byte from the ostream. + * Return -1 on EOF. + * This function doesn't touch the bit buffer. + * The client has to manage interaction between reading bits + * and reading bytes. + */ + int readByte(); + + /** Read a non-negative int from the ostream. + * Return -1 on EOF. + * This function doesn't touch the bit buffer. + * The client has to manage interaction between reading bits + * and reading ints. + */ + int readInt(); + + +}; + +#endif // BITINPUTSTREAM_HPP diff --git a/BitOutputStream.hpp b/BitOutputStream.hpp new file mode 100644 index 0000000..0b1f033 --- /dev/null +++ b/BitOutputStream.hpp @@ -0,0 +1,51 @@ +#ifndef BITOUTPUTSTREAM_HPP +#define BITOUTPUTSTREAM_HPP + +#include +#include + +using namespace std; + +/** A class for writing bits (and chars and ints) to an ostream + */ +class BitOutputStream { + +private: + ostream& out; // the ostream to delegate to + char buf; // the buffer of bits + // bitset<8> buf; // the buffer of bits + int bufi; // the bit buffer index + +public: + BitOutputStream(ostream& s) : out(s), buf(0), bufi(0) { } + // BitOutputStream(ostream& s) : out(s), bufi(0) { } + + /** Write the least significant bit of the argument into + * the bit buffer, and increment the bit buffer index. + * Flush to the ostream first if the bit buffer is full. + * This must be consistent with BitInputStream::readBit(). + */ + void writeBit(int bit); + + /** Write the least significant byte of the argument to the ostream. + * This function doesn't touch the bit buffer. + * The client has to manage interaction between writing bits + * and writing bytes. + */ + void writeByte(int b); + + /** Write the argument to the ostream. + * This function doesn't touch the bit buffer. + * The client has to manage interaction between writing bits + * and writing ints. + */ + void writeInt(int i); + + /** If the bit buffer contains any bits, flush the bit buffer to the ostream, + * clear the bit buffer, and set the bit buffer index to 0. + * Also flush the ostream itself. + */ + void flush(); +}; + +#endif // BITOUTPUTSTREAM_HPP -- 1.9.1