-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectors.pas
101 lines (84 loc) · 2.22 KB
/
vectors.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
unit vectors;
{$mode objfpc}
{$H+}
{$modeswitch advancedrecords}
interface
type
TVector=record
private
const DefaultDelta = 32;
private
FInstance: Pointer;
procedure InternalSetCapacity(NewCapacity: PtrUInt);
procedure SetCapacity( NewCapacity: PtrUInt);
function GetCapacity: PtrUInt;
public
class function New( InitialCapacity: PtrUInt=4): TVector;static;
procedure Free;
property Capacity: PtrUInt read GetCapacity write SetCapacity;
function Count: PtrUInt;
procedure Grow( Delta: PtrUInt=DefaultDelta);
function Push( P: Pointer): PtrUInt;
end;
implementation
type
VHeader= packed record
FCapacity: PtrUInt;
FCount: PtrUInt;
FPointers: array [0..512*1024*1024-4] of Pointer;
end;
class function TVector.New( InitialCapacity: PtrUInt=4 ): TVector;
begin
Result.FInstance:=nil;
if InitialCapacity>0 then Result.SetCapacity(InitialCapacity);
end;
procedure TVector.Free;
begin
if FInstance<>nil then
begin
FreeMem(FInstance);
FInstance:=nil;
end;
end;
function TVector.Count: PtrUInt;inline;
begin
if FInstance=nil then Result:=0
else Result:=VHeader(FInstance^).FCount
end;
function TVector.GetCapacity: PtrUInt;inline;
begin
if FInstance<>nil then
Result:=VHeader(FInstance^).FCapacity
else Result:=0;
end;
procedure TVector.InternalSetCapacity( NewCapacity: PtrUInt);
begin
ReAllocMem(FInstance,(NewCapacity+2)*SizeOf(PtrUInt));
VHeader(FInstance^).FCapacity:=NewCapacity;
end;
procedure TVector.SetCapacity( NewCapacity: PtrUInt );
begin
if NewCapacity=0 then Free
else
begin
InternalSetCapacity(NewCapacity);
with VHeader(FInstance^) do if FCount>FCapacity then FCount:=FCapacity;
end
end;
procedure TVector.Grow( Delta: PtrUInt=DefaultDelta);inline;
begin
InternalSetCapacity(Capacity+Delta);
end;
function TVector.Push( P: Pointer): PtrUInt;
var C: PtrUInt;
begin
C:=Count;
if C>=Capacity then Grow;
with VHeader(FInstance^) do
begin
FPointers[C]:=P;
Inc(C);
FCount:=C
end
end;
end.