source: trunk/Packages/CoolStreaming/DynNumber.pas

Last change on this file was 323, checked in by chronos, 6 months ago
File size: 1.7 KB
Line 
1unit DynNumber;
2
3interface
4
5uses
6 Classes, SysUtils, BitStream, Math;
7
8type
9
10 { TDynamicNumber }
11
12 TDynamicNumber = class
13 Stream: TBitStream;
14 procedure WriteNumber(Value: QWord);
15 function ReadNumber: QWord;
16 constructor Create;
17 destructor Destroy; override;
18 private
19 function ReadNumber2: QWord;
20 end;
21
22
23implementation
24
25{ TDynamicNumber }
26
27procedure TDynamicNumber.WriteNumber(Value: QWord);
28var
29 Length: Integer;
30begin
31 Length := Floor(Log2(Value)) + 1;
32 if Length > 1 then begin
33 Stream.WriteNumber(1, 1);
34 WriteNumber(Length - 2);
35 end else Stream.WriteNumber(0, 1);
36 if Length > 1 then Length := Length - 1;
37 Stream.WriteNumber(Value, Length);
38end;
39
40function TDynamicNumber.ReadNumber: QWord;
41var
42 Bit: Byte;
43 Length: Integer;
44begin
45 Length := 0;
46 Bit := Stream.ReadNumber(1);
47 if Bit = 0 then Length := 1
48 else Length := ReadNumber2 + 2;
49 if Length > 1 then Result := Stream.ReadNumber(Length - 1)
50 else Result := Stream.ReadNumber(Length);
51 if Length > 1 then Result := Result or (QWord(1) shl (Length - 1));
52end;
53
54function TDynamicNumber.ReadNumber2: QWord;
55var
56 Bit: Byte;
57 Length: Integer;
58begin
59 Length := 0;
60 Bit := Stream.ReadNumber(1);
61 if Bit = 0 then Length := 1
62 else Length := ReadNumber + 2;
63 if Length > 1 then Result := Stream.ReadNumber(Length - 1)
64 else Result := Stream.ReadNumber(Length);
65 if Length > 1 then Result := Result or (QWord(1) shl (Length - 1));
66end;
67
68constructor TDynamicNumber.Create;
69begin
70 Stream := TMemoryBitStream.Create;
71end;
72
73destructor TDynamicNumber.Destroy;
74begin
75 FreeAndNil(Stream);
76 inherited;
77end;
78
79end.
80
Note: See TracBrowser for help on using the repository browser.