-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseTypes.pas
218 lines (175 loc) · 3.59 KB
/
BaseTypes.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
214
215
216
217
unit BaseTypes;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Utility;
type
rVec2 = record
x, y: single;
end;
rVec3 = record
x, y, z: single;
end;
rInt2 = record
x, y: longint;
end;
rInt3 = record
x, y, z: longint;
end;
{ tAutoArray }
generic tAutoArray<_T> = class
private
fCapacity: longword;
procedure SetCapacity(AValue: longword); inline;
function GetValues(i: longword): _T; inline;
procedure SetValues(i: longword; AValue: _T); inline; //get rid of?
public
Count: longword;
fValues: array of _T;
property Values[i: longword]: _T read GetValues write SetValues; default;
property Capacity: longword read fCapacity write SetCapacity;
constructor Create(InitialCap: longword = 4);
destructor Destroy; override;
procedure Add(b: array of _T);
procedure Add(v: _T); inline;
procedure Clear;
end;
// pPtrListElement = ^rListElement;
generic rListElement<_T> = record
Content: _T;
Next, Prev: ^rListElement;
end;
{ tList }
generic tList<_E> = class
type
rLE = specialize rListElement<_E>;
pListElement = ^rLe;
var
Root: pListElement;
Count: longword;
class procedure Delete(pElement: pListElement);
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure AddNew(c: pointer);
end;
//Manhattan distance
function MHD(c1, c2: rVec3): single;
function MHD(c1, c2: rInt3): longword;
operator + (a, b: rVec3) r: rVec3;
implementation
uses
math;
function MHD(c1, c2: rVec3): single;
begin
Result:= abs(c1.x - c2.x) + abs(c1.y - c2.y) + abs(c1.z - c2.z);
end;
function MHD(c1, c2: rInt3): longword;
begin
Result:= abs(c1.x - c2.x) + abs(c1.y - c2.y) + abs(c1.z - c2.z);
end;
operator + (a, b: rVec3) r: rVec3;
begin
r.x:= a.x + b.x;
r.y:= a.y + b.y;
r.z:= a.z + b.z;
end;
{ tAutoArray }
procedure tAutoArray.SetCapacity(AValue: longword);
begin
if fCapacity = AValue then
Exit;
fCapacity:= AValue;
setlength(fValues, fCapacity);
end;
function tAutoArray.GetValues(i: longword): _T;
begin
Result:= fValues[i];
end;
procedure tAutoArray.SetValues(i: longword; AValue: _T);
begin
fValues[i]:= AValue;
end;
constructor tAutoArray.Create(InitialCap: longword = 4);
begin
Count:= 0;
if InitialCap > 4 then
Capacity:= InitialCap
else
Capacity:= 4;
end;
destructor tAutoArray.Destroy;
begin
setlength(fValues, 0);
end;
procedure tAutoArray.Add(b: array of _T);
var
i, c: integer;
begin
c:= Count;
Count+= length(b);
if Count > Capacity then
Capacity:= Capacity + max(Capacity shr 2, length(b));
for i:= 0 to high(b) do
Values[c + i]:= b[i];
end;
procedure tAutoArray.Add(v: _T);
begin
inc(Count);
if Count > Capacity then
Capacity:= Capacity + Capacity shr 2;
Values[Count - 1]:= v;
end;
procedure tAutoArray.Clear;
begin
Count:= 0;
Capacity:= 4;
end;
{ tList }
class procedure tList.Delete(pElement: pListElement);
begin
with pElement^ do
begin
freeandnil(Content); { TODO : dispose?? possible on objects}
//TObject(Content).Destroy;
Prev^.Next:= Next;
dispose(pListElement(@self));
end;
end;
constructor tList.Create;
begin
Root:= nil;
Count:= 0;
end;
destructor tList.Destroy;
begin
Clear;
end;
procedure tList.Clear;
var
p1, p2: pListElement;
begin
p1:= Root;
while p1 <> nil do
begin
p2:= p1^.Next;
dispose(p1);
p1:= p2;
end;
Count:= 0;
end;
procedure tList.AddNew(c: pointer);
var
p: pListElement;
begin
inc(Count);
new(p);
with p^ do
begin
Content:= c;
Next:= Root;
Prev:= nil;
end;
Root:= p;
end;
end.