Ignore:
Timestamp:
Jan 16, 2011, 3:08:56 PM (13 years ago)
Author:
george
Message:
  • Přidáno: Kódování a dekódování dynamického čísla. Použita kombinace rekurzivní délky a absolutní binární hodnoty s virtuální nejvyšší jedničkou.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • CoolStreaming/UDynNumber.pas

    r126 r127  
    66
    77uses
    8   Classes, SysUtils, UBitStream;
     8  Classes, SysUtils, UBitStream, Math;
    99
    1010type
     
    2626procedure TDynamicNumber.Write(Value: QWord);
    2727var
    28   C: Integer;
    29   Parts: array of Integer;
     28  Bit: Byte;
     29  Length: Integer;
    3030begin
    31   C := 0;
    32   for C := 0 to Value do
    33     Stream.WriteBit(True);
    34   Stream.WriteBit(False);
     31  Length := Floor(Log2(Value)) + 1;
     32  if Length > 1 then begin
     33    Stream.WriteNumber(1, 1);
     34    Write(Length - 2);
     35  end else Stream.WriteNumber(0, 1);
     36  if Length > 1 then Length := Length - 1;
     37  Stream.WriteNumber(Value, Length);
    3538end;
    3639
    3740function TDynamicNumber.Read: QWord;
     41var
     42  Bit: Byte;
     43  Length: Integer;
    3844begin
    39 
     45  Bit := Stream.ReadNumber(1);
     46  if Bit = 0 then Length := 1
     47    else Length := Read + 2;
     48  Result := Stream.ReadNumber(Length);
     49  if Length > 0 then Result := Result or (1 shl Length);
    4050end;
    4151
Note: See TracChangeset for help on using the changeset viewer.