-- $Date: 2004/03/04 04:46:00 $
-- $Revision: 1.1 $
-- $Author: jcrocholl $

with Interfaces; use Interfaces;
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;

package Bit_Buffers is

   -- A buffer for an array of bits.
   type Bit_Buffer(Byte_Count : Positive) is limited private;

   -- Access type.
   type Bit_Buffer_Access is access Bit_Buffer;

   -- Create a bit buffer with a capacity of 8 * Byte_Count bits.
   function Create
     (Byte_Count : in Positive-- The number of bytes in the buffer.
     return Bit_Buffer_Access;  -- The newly created bit buffer.

   -- Free the bit buffer's memory and set the instance variable to
   -- null.
   procedure Deallocate
     (This : in out Bit_Buffer_Access); -- Free this bit buffer.

   -- Restart reading from the left of the bit buffer.
   procedure Reset
     (This : access Bit_Buffer); -- Reset this bit buffer.

   -- Set all bits in this bit buffer to False.
   procedure Clear
     (This : access Bit_Buffer); -- Clear this bit buffer.

   -- Fill the bit buffer with data from a stream.
   procedure Read_Row
     (From : in Stream_Access;   -- Read from this stream.
      This : access Bit_Buffer); -- Read data into this bit buffer.

   -- Read the next bit from the bit buffer and advance the bit pointer.
   function Read_Bit
     (This : access Bit_Buffer-- Read from this bit buffer.
     return Boolean;            -- The resulting bit value.

private

   -- Smallest storage unit.
   subtype Byte is Unsigned_8;

   -- A buffer for bit data.
   type Byte_Array is array (Positive range <>) of Byte;

   -- Record for bit data and pointers.
   type Bit_Buffer(Byte_Count : Positive) is record
      Bytes      : Byte_Array(1 .. Byte_Count);
      Temp       : Byte;    -- The byte that is currently being processed.
      Temp_Bits  : Natural-- The number of unread bits in Temp.
      Temp_Index : Natural-- The position of Temp in Bytes.
   end record;

end Bit_Buffers;