// little-endian persistable sclass BitBuffer is IntSize { new ByteBuffer byteBuffer; // TODO (optimization): use a growing circular buffer int currentByte, currentBit; void add(int b) { add(odd(b)); } void add(bool b) { if (b) currentByte |= 1 << currentBit; if (currentBit == 7) { byteBuffer.add((byte) currentByte); currentByte = 0; currentBit = 0; } else ++currentBit; } bool get(int iBit) { int iByte = iBit >> 3; int theByte = iByte == byteBuffer.size() ? currentByte : byteBuffer.get(iByte); ret (theByte & (1 << (iBit & 7))) != 0; } bool hasFullByte() { ret !byteBuffer.isEmpty(); } byte popFullByte() { ret byteBuffer.popFirst(); } public int size() { ret byteBuffer.size()*8 + currentBit; } // TODO: optimize void writeBits(int data, int nBits) }