| 1 | unit UBitStream;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, DateUtils, syncobjs, UObjectTypeBase, UObjectBoolean;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 | { TMemoryBitStream }
|
|---|
| 12 |
|
|---|
| 13 | TMemoryBitStream = class(TInterfacedObject, IAssignable, IComparable)
|
|---|
| 14 | private
|
|---|
| 15 | FMemoryStream: TMemoryStream;
|
|---|
| 16 | FPositionOffset: Byte;
|
|---|
| 17 | FSizeOffset: Byte;
|
|---|
| 18 | function GetBit(Bit: Longint): TBoolean;
|
|---|
| 19 | function GetPosition:Int64;
|
|---|
| 20 | function GetSize:Int64;
|
|---|
| 21 | procedure SetBit(Bit: Longint;const AValue: TBoolean);
|
|---|
| 22 | procedure SetPosition(const AValue: Int64);
|
|---|
| 23 | procedure SetSize(const AValue: Int64);
|
|---|
| 24 | public
|
|---|
| 25 | procedure ReadBits(Bits: TMemoryBitStream; Count: Integer);
|
|---|
| 26 | procedure WriteBits(Bits: TMemoryBitStream);
|
|---|
| 27 |
|
|---|
| 28 | procedure AndBy(Bits: TMemoryBitStream);
|
|---|
| 29 | procedure OrBy(Bits: TMemoryBitStream);
|
|---|
| 30 | procedure XorBy(Bits: TMemoryBitStream);
|
|---|
| 31 | procedure NotBy(Bits: TMemoryBitStream);
|
|---|
| 32 |
|
|---|
| 33 | function Lookup(Bit: TBoolean): Integer;
|
|---|
| 34 | procedure Reverse;
|
|---|
| 35 | procedure Clear;
|
|---|
| 36 | procedure Assign(Source: TInterfacedObject);
|
|---|
| 37 | function EqualTo(Operand: IComparable): TBoolean;
|
|---|
| 38 |
|
|---|
| 39 | constructor Create;
|
|---|
| 40 | destructor Destroy; override;
|
|---|
| 41 |
|
|---|
| 42 | property Position: Int64 read GetPosition write SetPosition;
|
|---|
| 43 | property Size: Int64 read GetSize write SetSize;
|
|---|
| 44 | property Bits[Bit: Longint]: TBoolean read GetBit write SetBit; default;
|
|---|
| 45 | end;
|
|---|
| 46 |
|
|---|
| 47 | implementation
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | { TMemoryBitStream }
|
|---|
| 51 |
|
|---|
| 52 | function TMemoryBitStream.GetBit(Bit: Longint): TBoolean;
|
|---|
| 53 | begin
|
|---|
| 54 |
|
|---|
| 55 | end;
|
|---|
| 56 |
|
|---|
| 57 | function TMemoryBitStream.GetPosition:Int64;
|
|---|
| 58 | begin
|
|---|
| 59 | Result := FMemoryStream.Position + FPositionOffset;
|
|---|
| 60 | end;
|
|---|
| 61 |
|
|---|
| 62 | function TMemoryBitStream.GetSize:Int64;
|
|---|
| 63 | begin
|
|---|
| 64 |
|
|---|
| 65 | end;
|
|---|
| 66 |
|
|---|
| 67 | procedure TMemoryBitStream.SetBit(Bit: Longint;const AValue: TBoolean);
|
|---|
| 68 | begin
|
|---|
| 69 | end;
|
|---|
| 70 |
|
|---|
| 71 | procedure TMemoryBitStream.SetPosition(const AValue: Int64);
|
|---|
| 72 | begin
|
|---|
| 73 | FMemoryStream.Position := AValue shr 3;
|
|---|
| 74 | FPositionOffset := AValue and 7;
|
|---|
| 75 | end;
|
|---|
| 76 |
|
|---|
| 77 | procedure TMemoryBitStream.SetSize(const AValue: Int64);
|
|---|
| 78 | begin
|
|---|
| 79 | FSizeOffset := AValue and 7;
|
|---|
| 80 | if FSizeOffset = 0 then FMemoryStream.Size := (AValue shr 3)
|
|---|
| 81 | else FMemoryStream.Size := (AValue shr 3) + 1;
|
|---|
| 82 | end;
|
|---|
| 83 |
|
|---|
| 84 | procedure TMemoryBitStream.ReadBits(Bits:TMemoryBitStream;Count:Integer);
|
|---|
| 85 | begin
|
|---|
| 86 |
|
|---|
| 87 | end;
|
|---|
| 88 |
|
|---|
| 89 | procedure TMemoryBitStream.WriteBits(Bits:TMemoryBitStream);
|
|---|
| 90 | begin
|
|---|
| 91 |
|
|---|
| 92 | end;
|
|---|
| 93 |
|
|---|
| 94 | procedure TMemoryBitStream.AndBy(Bits:TMemoryBitStream);
|
|---|
| 95 | begin
|
|---|
| 96 |
|
|---|
| 97 | end;
|
|---|
| 98 |
|
|---|
| 99 | procedure TMemoryBitStream.OrBy(Bits:TMemoryBitStream);
|
|---|
| 100 | begin
|
|---|
| 101 |
|
|---|
| 102 | end;
|
|---|
| 103 |
|
|---|
| 104 | procedure TMemoryBitStream.XorBy(Bits:TMemoryBitStream);
|
|---|
| 105 | begin
|
|---|
| 106 |
|
|---|
| 107 | end;
|
|---|
| 108 |
|
|---|
| 109 | procedure TMemoryBitStream.NotBy(Bits:TMemoryBitStream);
|
|---|
| 110 | begin
|
|---|
| 111 |
|
|---|
| 112 | end;
|
|---|
| 113 |
|
|---|
| 114 | function TMemoryBitStream.Lookup(Bit:TBoolean):Integer;
|
|---|
| 115 | begin
|
|---|
| 116 | while (Position < Size) and (Bits[Position] <> Bit) do
|
|---|
| 117 | Position := Position + 1;
|
|---|
| 118 | if Position < Size then Result := Position
|
|---|
| 119 | else Result := -1;
|
|---|
| 120 | end;
|
|---|
| 121 |
|
|---|
| 122 | procedure TMemoryBitStream.Reverse;
|
|---|
| 123 | begin
|
|---|
| 124 |
|
|---|
| 125 | end;
|
|---|
| 126 |
|
|---|
| 127 | procedure TMemoryBitStream.Clear;
|
|---|
| 128 | begin
|
|---|
| 129 | Size := 0;
|
|---|
| 130 | Position := 0;
|
|---|
| 131 | end;
|
|---|
| 132 |
|
|---|
| 133 | procedure TMemoryBitStream.Assign(Source:TInterfacedObject);
|
|---|
| 134 | begin
|
|---|
| 135 | if Assigned(Source) and (Source is TMemoryBitStream) then begin
|
|---|
| 136 | FMemoryStream.LoadFromStream(TMemoryBitStream(Source).FMemoryStream);
|
|---|
| 137 | FPositionOffset := TMemoryBitStream(Source).FPositionOffset;
|
|---|
| 138 | FSizeOffset := TMemoryBitStream(Source).FSizeOffset;
|
|---|
| 139 | end;
|
|---|
| 140 | end;
|
|---|
| 141 |
|
|---|
| 142 | function TMemoryBitStream.EqualTo(Operand:IComparable):TBoolean;
|
|---|
| 143 | begin
|
|---|
| 144 |
|
|---|
| 145 | end;
|
|---|
| 146 |
|
|---|
| 147 | constructor TMemoryBitStream.Create;
|
|---|
| 148 | begin
|
|---|
| 149 | FMemoryStream := TMemoryStream.Create;
|
|---|
| 150 | end;
|
|---|
| 151 |
|
|---|
| 152 | destructor TMemoryBitStream.Destroy;
|
|---|
| 153 | begin
|
|---|
| 154 | FMemoryStream.Destroy;
|
|---|
| 155 | inherited Destroy;
|
|---|
| 156 | end;
|
|---|
| 157 |
|
|---|
| 158 | end.
|
|---|