Commit dcc4520eaae826baf04cd5883ffa721ebe56eb6d
1 parent
1629dd754a
Exists in
master
Added BitInputStream + BitOutputStream
Showing 2 changed files with 104 additions and 0 deletions Side-by-side Diff
BitInputStream.hpp
View file @
dcc4520
1 | +#ifndef BITINPUTSTREAM_HPP | |
2 | +#define BITINPUTSTREAM_HPP | |
3 | + | |
4 | +#include <iostream> | |
5 | +#include <bitset> | |
6 | + | |
7 | +using namespace std; | |
8 | + | |
9 | +/** A class for reading bits (and ints) from an istream | |
10 | + */ | |
11 | +class BitInputStream { | |
12 | +private: | |
13 | + istream& in; // the istream to delegate to | |
14 | + char buf; // the buffer of bits | |
15 | + //bitset<8> buf; | |
16 | + int bufi; // the bit buffer index | |
17 | + | |
18 | +public: | |
19 | + /** Initialize a BitInputStream object, given an istream. | |
20 | + */ | |
21 | + BitInputStream(istream& s) : in(s), buf(0), bufi(8) { } | |
22 | + // BitInputStream(istream& s) : in(s), bufi(8) { } | |
23 | + | |
24 | + /** Read the next bit from the bit buffer. | |
25 | + * If the bit buffer is currently empty, | |
26 | + * fill the bit buffer by reading a char from the istream first. | |
27 | + * Return the bit read as the least signficant bit of an int. | |
28 | + * Return -1 on EOF. | |
29 | + * This must be consistent with BitOutputStream::writeBit(), in terms | |
30 | + * of ordering of bits in the stream. | |
31 | + */ | |
32 | + int readBit(); | |
33 | + | |
34 | + /** Read a byte from the ostream. | |
35 | + * Return -1 on EOF. | |
36 | + * This function doesn't touch the bit buffer. | |
37 | + * The client has to manage interaction between reading bits | |
38 | + * and reading bytes. | |
39 | + */ | |
40 | + int readByte(); | |
41 | + | |
42 | + /** Read a non-negative int from the ostream. | |
43 | + * Return -1 on EOF. | |
44 | + * This function doesn't touch the bit buffer. | |
45 | + * The client has to manage interaction between reading bits | |
46 | + * and reading ints. | |
47 | + */ | |
48 | + int readInt(); | |
49 | + | |
50 | + | |
51 | +}; | |
52 | + | |
53 | +#endif // BITINPUTSTREAM_HPP |
BitOutputStream.hpp
View file @
dcc4520
1 | +#ifndef BITOUTPUTSTREAM_HPP | |
2 | +#define BITOUTPUTSTREAM_HPP | |
3 | + | |
4 | +#include <iostream> | |
5 | +#include <bitset> | |
6 | + | |
7 | +using namespace std; | |
8 | + | |
9 | +/** A class for writing bits (and chars and ints) to an ostream | |
10 | + */ | |
11 | +class BitOutputStream { | |
12 | + | |
13 | +private: | |
14 | + ostream& out; // the ostream to delegate to | |
15 | + char buf; // the buffer of bits | |
16 | + // bitset<8> buf; // the buffer of bits | |
17 | + int bufi; // the bit buffer index | |
18 | + | |
19 | +public: | |
20 | + BitOutputStream(ostream& s) : out(s), buf(0), bufi(0) { } | |
21 | + // BitOutputStream(ostream& s) : out(s), bufi(0) { } | |
22 | + | |
23 | + /** Write the least significant bit of the argument into | |
24 | + * the bit buffer, and increment the bit buffer index. | |
25 | + * Flush to the ostream first if the bit buffer is full. | |
26 | + * This must be consistent with BitInputStream::readBit(). | |
27 | + */ | |
28 | + void writeBit(int bit); | |
29 | + | |
30 | + /** Write the least significant byte of the argument to the ostream. | |
31 | + * This function doesn't touch the bit buffer. | |
32 | + * The client has to manage interaction between writing bits | |
33 | + * and writing bytes. | |
34 | + */ | |
35 | + void writeByte(int b); | |
36 | + | |
37 | + /** Write the argument to the ostream. | |
38 | + * This function doesn't touch the bit buffer. | |
39 | + * The client has to manage interaction between writing bits | |
40 | + * and writing ints. | |
41 | + */ | |
42 | + void writeInt(int i); | |
43 | + | |
44 | + /** If the bit buffer contains any bits, flush the bit buffer to the ostream, | |
45 | + * clear the bit buffer, and set the bit buffer index to 0. | |
46 | + * Also flush the ostream itself. | |
47 | + */ | |
48 | + void flush(); | |
49 | +}; | |
50 | + | |
51 | +#endif // BITOUTPUTSTREAM_HPP |