-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathClusteredPointerList.pas
213 lines (170 loc) · 3.86 KB
/
ClusteredPointerList.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
unit ClusteredPointerList;
interface
uses
generics.collections.fixed, betterobject;
const
CLUSTERS = 64;
type
TClusteredPointerList<T:class> = class(TBetterObject)
private
FListList: TList<TList<T>>;
function GetListNumberForObject(obj: T): nativeint;
function GetCount: nativeint;
function GEtListNumberForIndex(idx: nativeint): nativeint;
function GetListBase(iListNumber: nativeint): nativeint;
function GetItem(idx: nativeint): T;
procedure SetItem(idx: nativeint; const Value: T);
public
procedure Init;override;
procedure Detach;override;
destructor Destroy;override;
procedure Add(obj: T);
property Count: nativeint read GetCount;
function IndexOf(obj: T): nativeint;
property Items[idx: nativeint]: T read GetItem write SetItem;default;
procedure REmove(obj: T);
procedure Clear;
procedure Delete(idx: nativeint);
end;
implementation
{ TClusteredPointerList<T> }
procedure TClusteredPointerList<T>.Add(obj: T);
var
nl: nativeint;
begin
nl := GetListNumberForObject(obj);
FListlist[nl].add(obj);
end;
procedure TClusteredPointerList<T>.Clear;
var
l: TList<T>;
x: nativeint;
begin
inherited;
for x := 0 to CLUSTERS-1 do begin
l := FListlist[x];
l.Clear;
end;
end;
procedure TClusteredPointerList<T>.Delete(idx: nativeint);
var
ln, base, sub: nativeint;
begin
ln := Self.GEtListNumberForIndex(idx);
base := GetListBase(ln);
sub := idx-base;
FListList[ln].delete(sub);
end;
destructor TClusteredPointerList<T>.Destroy;
var
l: TLIst<T>;
begin
inherited;
while FListList.count > 0 do begin
l := FListlist[FlistLIst.count-1];
l.free;
l := nil;
FListlist.Delete(FListlist.count-1);
end;
FListList.free;
end;
procedure TClusteredPointerList<T>.Detach;
begin
Clear;
inherited;
end;
function TClusteredPointerList<T>.GetCount: nativeint;
var
x: nativeint;
begin
result := 0;
for x := 0 to CLUSTERS-1 do begin
inc(result, FListList[x].Count);
end;
end;
function TClusteredPointerList<T>.GetItem(idx: nativeint): T;
var
ln, base, sub: nativeint;
begin
ln := Self.GEtListNumberForIndex(idx);
base := GetListBase(ln);
sub := idx-base;
result := FListList[ln][sub];
end;
function TClusteredPointerList<T>.GetListBase(
iListNumber: nativeint): nativeint;
var
x: nativeint;
begin
result := 0;
for x := 0 to iListNumber-1 do begin
result := result + FListLIst[x].count;
end;
end;
function TClusteredPointerList<T>.GEtListNumberForIndex(
idx: nativeint): nativeint;
var
x: nativeint;
base: nativeint;
begin
inherited;
result := -1;
base := 0;
for x := 0 to CLUSTERS-1 do begin
if idx < (FListList[x].Count + base) then begin
result := x;
exit;
end;
inc(base, FListList[x].count);
end;
end;
function TClusteredPointerList<T>.GetListNumberForObject(obj: T): nativeint;
var
p: pointer;
ni: nativeint;
begin
p := pointer(obj);
ni := (nativeint(p) div 16) mod CLUSTERS;
result := ni;
end;
function TClusteredPointerList<T>.IndexOf(obj: T): nativeint;
var
ln: nativeint;
begin
ln := GetListNumberForObject(obj);
result := FListList[ln].IndexOf(obj);
if result < 0 then begin
result := -1;
exit;
end else begin
result := result+ GetListBase(ln);
end;
end;
procedure TClusteredPointerList<T>.Init;
var
x: nativeint;
begin
inherited;
FListList := TList<TList<T>>.create;
for x := 0 to CLUSTERS-1 do begin
FListList.add(TList<T>.create);
end;
end;
procedure TClusteredPointerList<T>.REmove(obj: T);
var
ln: nativeint;
begin
ln := GetListNumberForObject(obj);
if ln >=0 then
FListList[ln].Remove(obj);
end;
procedure TClusteredPointerList<T>.SetItem(idx: nativeint; const Value: T);
var
ln, base, sub: nativeint;
begin
ln := Self.GEtListNumberForIndex(idx);
base := GetListBase(ln);
sub := idx-base;
FListList[ln][sub] := value;
end;
end.