Debian Codegraph @

src/base/castleutils_primitive_lists.inc

raw
1 {%MainUnit castleutils.pas}
2 {
3 Copyright 2002-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Lists of primitive types.
18 Based on TStructList, which is based on TList from Generics.Collections. }
19
20 {$ifdef read_interface}
21
22 type
23 TSingleList = class;
24 TDoubleList = class;
25 TInt32List = class;
26 TUInt32List = class;
27
28 { Raised by various AssignLerp implementations on lists,
29 like @link(TStructList.AssignLerp),
30 when the arguments have different length. }
31 EListsDifferentCount = class(Exception);
32
33 { List of Word (16-bit unsigned) values. }
34 TWordList = class({$ifdef FPC}specialize{$endif} TStructList<Word>)
35 public
36 function Max: Word;
37 procedure Assign(const Source: TInt32List); overload;
38 procedure Assign(const Source: TUInt32List); overload;
39 end;
40
41 TUInt16List = TWordList;
42
43 { List of 32-bit unsigned values. }
44 TUInt32List = class({$ifdef FPC}specialize{$endif} TStructList<UInt32>)
45 public
46 function Max: UInt32;
47 procedure Assign(const Source: TInt32List);
48 end;
49
50 { List of Float values (defined in Math unit as float with maximum precision).
51 This is deprecated as precision of floats is not defined cross-platform,
52 see https://castle-engine.io/coding_conventions#_do_not_use_extended_use_single_double . }
53 TFloatList = class({$ifdef FPC}specialize{$endif} TStructList<Float>)
54 procedure AppendFloats(Floats: TSingleList);
55 end {$ifdef FPC}deprecated 'use TSingleList for most cases, eventually TDoubleList'{$endif};
56
57 TSingleArray = packed array [0..MaxInt div SizeOf(Single) - 1] of Single;
58 PSingleArray = ^TSingleArray;
59
60 { List of Single (single-precision floating point) values. }
61 TSingleList = class({$ifdef FPC}specialize{$endif} TStructList<Single>)
62 function ToDouble: TDoubleList;
63
64 { Assign value from TDoubleList, converting to single-precision. }
65 procedure Assign(const Source: TDoubleList); overload;
66
67 procedure AssignLerpRange(const Fraction: Single;
68 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<Single>;
69 const Index1, Index2, ACount: TListSize); override;
70
71 procedure AddListRange(const Source: TSingleList; const Index, AddCount: TListSize); deprecated 'use AddSubRange';
72
73 { Does the SecondValue have equal length and content.
74 The values are compared with an Epsilon tolerance, as usual for floating-point
75 values. }
76 function Equals(SecondValue: TObject): boolean; override;
77
78 function Sum: Single;
79
80 { Multiply all items by Factor. }
81 procedure MultiplyAll(const Factor: Single);
82
83 { Sort and remove (exact) duplicates.
84 Removal of duplicates is very fast, utilizing the fact that the list is sorted. }
85 procedure SortAndRemoveDuplicates;
86
87 {$warnings off} // using deprecated, to keep it working
88 procedure AppendFloats(Floats: TFloatList);
89 {$warnings on}
90 end;
91
92 { List of Double (double-precision floating point) values. }
93 TDoubleList = class({$ifdef FPC}specialize{$endif} TStructList<Double>)
94 function ToSingle: TSingleList;
95
96 { Assign value from TSingleList, converting to single-precision. }
97 procedure Assign(const Source: TSingleList); overload;
98
99 procedure AssignLerpRange(const Fraction: Single;
100 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<Double>;
101 const Index1, Index2, ACount: TListSize); override;
102
103 { Does the SecondValue have equal length and content.
104 The values are compared with an Epsilon tolerance, as usual for floating-point
105 values. }
106 function Equals(SecondValue: TObject): boolean; override;
107 end;
108
109 { List of Cardinal (unsigned) values.
110
111 In practice, Cardinal is always 32-bit signed now,
112 but use this when you don't want to explicitly care about size,
113 see https://castle-engine.io/coding_conventions#_do_not_use_longint_longword_use_int32_uint32_int64_uint64_integer_cardinal . }
114 TCardinalList = class({$ifdef FPC}specialize{$endif} TStructList<Cardinal>)
115 function BigSum: Int64;
116 function Sum: Cardinal;
117 end;
118
119 { List of booleans. }
120 TBooleanList = class({$ifdef FPC}specialize{$endif} TStructList<boolean>)
121 public
122 { Set appropriate item to given value.
123 These are useful methods to pass as callback in some situations
124 (e.g. TCastleScene.RenderFrustumOctree passes
125 RenderFrustumOctree_Visible.SetTrue method as callback
126 that should mark visible items.)
127 @groupBegin }
128 procedure SetFalse(const Index: TListSize);
129 procedure SetTrue(const Index: TListSize);
130 { @groupEnd }
131 function ToInt32: TInt32List;
132
133 { Does the SecondValue have equal length and content.
134 This method does the same thing as @link(PerfectlyEquals)
135 in case of Booleans list, that are always compared perfectly. }
136 function Equals(SecondValue: TObject): boolean; override;
137 end;
138
139 TInt32Array = packed array [0..MaxInt div SizeOf(Int32) - 1] of Int32;
140 PInt32Array = ^TInt32Array;
141
142 { List of 32-bit signed integer values. }
143 TInt32List = class({$ifdef FPC}specialize{$endif} TStructList<Int32>)
144 public
145 function Max: Int32;
146 function Sum: Int32;
147 procedure DeleteRange(const Index: TListSize; DelCount: TListSize = 1);
148 { Add given Item a number of times to the list. }
149 procedure AddDuplicate(const Item: Int32; const DuplicateCount: TListSize);
150
151 { Does the SecondValue have equal length and content.
152 This method does the same thing as @link(PerfectlyEquals)
153 in case of Int32 list, that are always compared perfectly. }
154 function Equals(SecondValue: TObject): boolean; override;
155 end;
156
157 { List of Integer (signed) values.
158
159 In practice, Integer is always 32-bit signed now,
160 but use this when you don't want to explicitly care about size,
161 see https://castle-engine.io/coding_conventions#_do_not_use_longint_longword_use_int32_uint32_int64_uint64_integer_cardinal . }
162 TIntegerList = class({$ifdef FPC}specialize{$endif} TStructList<Integer>)
163 public
164 function Sum: Integer;
165 end;
166 {$endif}
167
168 {$ifdef read_implementation}
169
170 { TWordList ------------------------------------------------------------ }
171
172 function TWordList.Max: Word;
173 var
174 I: TListSize;
175 begin
176 Result := Low(Word);
177 for I := 0 to Count - 1 do
178 MaxVar(Result, Items[I]);
179 end;
180
181 procedure TWordList.Assign(const Source: TInt32List);
182 var
183 I: TListSize;
184 Src: PInt32;
185 Dest: PWord;
186 begin
187 Count := Source.Count;
188 Src := PInt32(Source.L);
189 Dest := PWord(L);
190 for I := 0 to Count - 1 do
191 begin
192 Dest^ := Src^;
193 Inc(Src);
194 Inc(Dest);
195 end;
196 end;
197
198 procedure TWordList.Assign(const Source: TUInt32List);
199 var
200 I: TListSize;
201 Src: PUInt32;
202 Dest: PWord;
203 begin
204 Count := Source.Count;
205 Src := PUInt32(Source.L);
206 Dest := PWord(L);
207 for I := 0 to Count - 1 do
208 begin
209 Dest^ := Src^;
210 Inc(Src);
211 Inc(Dest);
212 end;
213 end;
214
215 { TUInt32List ------------------------------------------------------------ }
216
217 function TUInt32List.Max: UInt32;
218 var
219 I: TListSize;
220 begin
221 Result := Low(UInt32);
222 for I := 0 to Count - 1 do
223 MaxVar(Result, Items[I]);
224 end;
225
226 procedure TUInt32List.Assign(const Source: TInt32List);
227 begin
228 { Copy memory directly.
229 This implicitly converts Int32 to UInt32 values, without range checking.
230 But, in exchange, it's fast. }
231 Count := Source.Count;
232 if Count <> 0 then
233 System.Move(Source.L[0], L[0], SizeOf(UInt32) * Count);
234 end;
235
236 { TFloatList ------------------------------------------------------------ }
237
238 procedure TFloatList.AppendFloats(Floats: TSingleList);
239 var
240 OldCount, I: TListSize;
241 begin
242 OldCount := Count;
243 Count := Count + Floats.Count;
244 for i := 0 to Floats.Count - 1 do
245 Items[OldCount + i] := Floats.Items[i];
246 end;
247
248 { TSingleList ------------------------------------------------------------ }
249
250 procedure TSingleList.AppendFloats(Floats: TFloatList);
251 var
252 OldCount, I: TListSize;
253 begin
254 OldCount := Count;
255 Count := Count + Floats.Count;
256 for i := 0 to Floats.Count - 1 do
257 Items[OldCount+i] := Floats.Items[i];
258 end;
259
260 function TSingleList.ToDouble: TDoubleList;
261 begin
262 Result := TDoubleList.Create;
263 Result.Assign(Self);
264 end;
265
266 procedure TSingleList.Assign(const Source: TDoubleList);
267 var
268 I: TListSize;
269 Src: PDouble;
270 Dest: PSingle;
271 begin
272 Count := Source.Count;
273 Src := PDouble(Source.L);
274 Dest := PSingle(L);
275 for I := 0 to Count - 1 do
276 begin
277 Dest^ := Src^;
278 Inc(Src);
279 Inc(Dest);
280 end;
281 end;
282
283 procedure TSingleList.AssignLerpRange(const Fraction: Single;
284 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<Single>;
285 const Index1, Index2, ACount: TListSize);
286 var
287 I: TListSize;
288 PtrTarget, Ptr1, Ptr2: PtrT;
289 begin
290 Count := ACount;
291 PtrTarget := L;
292 Ptr1 := V1.Ptr(Index1);
293 Ptr2 := V2.Ptr(Index2);
294 for I := 0 to Count - 1 do
295 begin
296 PtrTarget^ := Lerp(Fraction, Ptr1^, Ptr2^);
297 Inc(PtrTarget);
298 Inc(Ptr1);
299 Inc(Ptr2);
300 end;
301 end;
302
303 procedure TSingleList.AddListRange(const Source: TSingleList;
304 const Index, AddCount: TListSize);
305 begin
306 AddSubRange(Source, Index, AddCount);
307 end;
308
309 function TSingleList.Equals(SecondValue: TObject): boolean;
310 var
311 I: TListSize;
312 begin
313 Result := SecondValue is TSingleList;
314 if Result then
315 begin
316 Result := Count = TSingleList(SecondValue).Count;
317 if Result then
318 for I := 0 to Count - 1 do
319 if not SameValue(TSingleList(SecondValue).L[I], L[I]) then
320 begin
321 Result := false;
322 Exit;
323 end;
324 end;
325 end;
326
327 function TSingleList.Sum: Single;
328 var
329 I: TListSize;
330 begin
331 Result := 0;
332 for I := 0 to Count - 1 do
333 Result := Result + Items[I];
334 end;
335
336 procedure TSingleList.MultiplyAll(const Factor: Single);
337 var
338 I: TListSize;
339 begin
340 for I := 0 to Count - 1 do
341 Items[I] := Items[I] * Factor;
342 end;
343
344 procedure TSingleList.SortAndRemoveDuplicates;
345 var
346 NewPosition, SourcePosition: TListSize;
347 begin
348 Sort;
349
350 { Both numbers NewPosition and SourcePosition move from 0 to Count.
351 While SourcePosition always increases (and will definitely reach Count),
352 NewPosition may grow slower, it indicates where should the Items[SourcePosition]
353 be placed in the output list.
354 Always NewPosition <= SourcePosition. }
355 NewPosition := 0;
356 SourcePosition := 0;
357 while SourcePosition + 1 < Count do
358 begin
359 if Items[SourcePosition] <> Items[SourcePosition + 1] then
360 begin
361 Inc(NewPosition);
362 Items[NewPosition] := Items[SourcePosition + 1];
363 end;
364 Inc(SourcePosition);
365 end;
366 Count := NewPosition + 1;
367 end;
368
369 { TDoubleList ------------------------------------------------------------ }
370
371 function TDoubleList.ToSingle: TSingleList;
372 begin
373 Result := TSingleList.Create;
374 Result.Assign(Self);
375 end;
376
377 procedure TDoubleList.Assign(const Source: TSingleList);
378 var
379 I: TListSize;
380 Src: PSingle;
381 Dest: PDouble;
382 begin
383 Count := Source.Count;
384 Src := PSingle(Source.L);
385 Dest := PDouble(L);
386 for I := 0 to Count - 1 do
387 begin
388 Dest^ := Src^;
389 Inc(Src);
390 Inc(Dest);
391 end;
392 end;
393
394 procedure TDoubleList.AssignLerpRange(const Fraction: Single;
395 const V1, V2: {$ifdef FPC}specialize{$endif} TStructList<Double>;
396 const Index1, Index2, ACount: TListSize);
397 var
398 I: TListSize;
399 PtrTarget, Ptr1, Ptr2: PtrT;
400 begin
401 Count := ACount;
402 PtrTarget := L;
403 Ptr1 := V1.Ptr(Index1);
404 Ptr2 := V2.Ptr(Index2);
405 for I := 0 to Count - 1 do
406 begin
407 PtrTarget^ := Lerp(Fraction, Ptr1^, Ptr2^);
408 Inc(PtrTarget);
409 Inc(Ptr1);
410 Inc(Ptr2);
411 end;
412 end;
413
414 function TDoubleList.Equals(SecondValue: TObject): boolean;
415 var
416 I: TListSize;
417 begin
418 Result := SecondValue is TDoubleList;
419 if Result then
420 begin
421 Result := Count = TDoubleList(SecondValue).Count;
422 if Result then
423 for I := 0 to Count - 1 do
424 if not SameValue(TDoubleList(SecondValue).L[I], L[I]) then
425 begin
426 Result := false;
427 Exit;
428 end;
429 end;
430 end;
431
432 { TCardinalList ------------------------------------------------------------ }
433
434 function TCardinalList.BigSum: Int64;
435 var
436 i: TListSize;
437 begin
438 result := 0;
439 for i := 0 to Count-1 do
440 result := result + Items[i];
441 end;
442
443 function TCardinalList.Sum: Cardinal;
444 var
445 I: TListSize;
446 begin
447 Result := 0;
448 for I := 0 to Count - 1 do
449 Result := Result + Items[I];
450 end;
451
452 { TBooleanList ------------------------------------------------------------ }
453
454 procedure TBooleanList.SetFalse(const Index: TListSize);
455 begin
456 Items[Index] := false;
457 end;
458
459 procedure TBooleanList.SetTrue(const Index: TListSize);
460 begin
461 Items[Index] := true;
462 end;
463
464 function TBooleanList.ToInt32: TInt32List;
465 var
466 I: TListSize;
467 begin
468 Result := TInt32List.Create;
469 Result.Count := Count;
470 for I := 0 to Count - 1 do
471 Result.Items[I] := Ord(Items[I]);
472 end;
473
474 function TBooleanList.Equals(SecondValue: TObject): boolean;
475 begin
476 Result := (SecondValue is TBooleanList) and PerfectlyEquals(TBooleanList(SecondValue));
477 end;
478
479 { TInt32List ----------------------------------------------------------- }
480
481 function TInt32List.Max: Int32;
482 var
483 I: TListSize;
484 begin
485 Result := Low(Int32);
486 for I := 0 to Count - 1 do
487 MaxVar(Result, Items[I]);
488 end;
489
490 function TInt32List.Sum: Int32;
491 var
492 I: TListSize;
493 begin
494 Result := 0;
495 for I := 0 to Count - 1 do
496 Result := Result + Items[I];
497 end;
498
499 procedure TInt32List.DeleteRange(const Index: TListSize; DelCount: TListSize);
500 var
501 I: TListSize;
502 begin
503 { Make sure Index and DelCount are sensible first }
504 if Index >= Count then
505 Exit;
506 MinVar(DelCount, Count - Index);
507
508 for I := Index to Count - 1 - DelCount do
509 Items[I] := Items[I + DelCount];
510
511 Count := Count - DelCount;
512 end;
513
514 procedure TInt32List.AddDuplicate(const Item: Int32; const DuplicateCount: TListSize);
515 var
516 I, C: TListSize;
517 begin
518 C := Count;
519 Count := Count + DuplicateCount;
520 for I := C to Count - 1 do
521 Items[I] := Item;
522 end;
523
524 function TInt32List.Equals(SecondValue: TObject): boolean;
525 begin
526 Result := (SecondValue is TInt32List) and PerfectlyEquals(TInt32List(SecondValue));
527 end;
528
529 { TIntegerList ----------------------------------------------------------- }
530
531 function TIntegerList.Sum: Integer;
532 var
533 I: Integer;
534 begin
535 Result := 0;
536 for I := 0 to Count - 1 do
537 Result := Result + Items[I];
538 end;
539
540 {$endif}