| 1 |
{%MainUnit castleutils.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2021 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 |
{ TStructList } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
ELinearInterpolationImpossible = class(Exception); |
| 23 |
|
| 24 |
{ List of structures. |
| 25 |
This is just TList class from Generics.Collections, with some useful helpers. } |
| 26 |
{$ifdef FPC}generic{$endif} |
| 27 |
TStructList<T> = class({$ifdef FPC}specialize{$endif} TList<T>) |
| 28 |
public |
| 29 |
type |
| 30 |
PtrT = ^T; |
| 31 |
{ TODO: Can we calculate better size on Delphi? |
| 32 |
Now we just assume that T has max size 16 * 16. |
| 33 |
This declaration will fail if you try to use TStructList with larger T. |
| 34 |
Note: we cannot declare list size too small, or using it may fail with range check error |
| 35 |
(e.g. Delphi, in IndexedPolygons_TrianglesCount, testcase: build_3d_object_by_code_2_tunnel. } |
| 36 |
TTypeList = array [0 .. MaxInt div {$ifdef FPC}SizeOf(T){$else}(16 * 16){$endif} - 1] of T; |
| 37 |
PTypeList = ^TTypeList; |
| 38 |
|
| 39 |
{ Access the list contents through a pointer to an array of items. |
| 40 |
|
| 41 |
This is exactly the same pointer as @link(L), but the type is different: |
| 42 |
List points to an array of items, while L points to a single item. |
| 43 |
|
| 44 |
Using @link(L) is preferred over this @link(List), because @link(L) |
| 45 |
avoids "fake infinite list" type TTypeList, that in particular on Delphi |
| 46 |
may have too small size (and cause range check errors). |
| 47 |
|
| 48 |
See the @link(L) description for a more detailed explanation and example. |
| 49 |
|
| 50 |
@seealso L } |
| 51 |
function List: PTypeList; deprecated 'use L, like MyList.L[123] instead of MyList.List^[123]'; |
| 52 |
|
| 53 |
{ Access the list contents through a pointer, to get or set them efficiently. |
| 54 |
|
| 55 |
Use this to directly access a list value, like this: |
| 56 |
|
| 57 |
@longCode(# |
| 58 |
MyIntList.L[I] := 123; |
| 59 |
// Equivalent but less efficient to syntax without L: |
| 60 |
// MyIntList[I] := 123; |
| 61 |
|
| 62 |
MyIntList.L[I] := MyIntList.L[I] + 123; |
| 63 |
// Equivalent but less efficient to syntax without L: |
| 64 |
// MyIntList[I] := MyIntList[I] + 123; |
| 65 |
|
| 66 |
MyRecordList.L[I].MyField := 123; |
| 67 |
// Never use syntax without L (see below for reasons): |
| 68 |
// MyRecordList[I].MyField := 123; //< never do this! |
| 69 |
#) |
| 70 |
|
| 71 |
The above examples of L work only in FPC ObjFpc mode or in Delphi with pointermath "on" |
| 72 |
(see https://docwiki.embarcadero.com/RADStudio/Sydney/en/Pointer_Math_(Delphi) ). |
| 73 |
These are CGE default settings when being compiled with FPC or Delphi, |
| 74 |
so you can depend on them at least in CGE implementation. |
| 75 |
Note that Delphi applications just using L don't actually have pointermath "on", |
| 76 |
it is enough that this unit is compiled with pointermath "on" and then one can use |
| 77 |
math on PtrT. |
| 78 |
|
| 79 |
This is in particular useful if you have a list of records and you would like |
| 80 |
to set their fields. E.g. this is incorrect (and should not even compile if |
| 81 |
MyField is really a simple field): |
| 82 |
|
| 83 |
@longCode(# |
| 84 |
type |
| 85 |
TMyRecord = record MyField: Integer; end; |
| 86 |
TMyRecordList = specialize TGenericStructList<TMyRecord>; |
| 87 |
var |
| 88 |
MyList: TMyRecordList; |
| 89 |
begin |
| 90 |
// ... |
| 91 |
MyList[I].MyField := 123; |
| 92 |
#) |
| 93 |
|
| 94 |
It will not work OK because you would modify only a temporary record |
| 95 |
returned by the @code(MyList[I]) getter. |
| 96 |
|
| 97 |
In contrast, this will work OK: |
| 98 |
|
| 99 |
@longCode(# |
| 100 |
MyList.L[I].MyField := 123; |
| 101 |
#) |
| 102 |
|
| 103 |
Using @link(L) is preferred over this @link(List), because it avoids "fake infinite list" |
| 104 |
type TTypeList, that in particular on Delphi may have too small size. |
| 105 |
|
| 106 |
@seealso List } |
| 107 |
function L: PtrT; |
| 108 |
|
| 109 |
{$ifndef FPC} |
| 110 |
function InternalGetItem(const Index: TListSize): T; |
| 111 |
procedure InternalSetItem(const Index: TListSize; const Value: T); |
| 112 |
{$endif} |
| 113 |
|
| 114 |
{ Increase Count and return pointer to new item. |
| 115 |
Comfortable and efficient way to add a new item that you want to immediately |
| 116 |
initialize. } |
| 117 |
function Add: PtrT; overload; |
| 118 |
|
| 119 |
{ Pointer to ith item. } |
| 120 |
function Ptr(const I: TListSize): PtrT; |
| 121 |
|
| 122 |
procedure Assign(const Source: TStructList {$ifndef FPC}<T>{$endif}); overload; |
| 123 |
procedure Assign(const A: array of T); overload; |
| 124 |
|
| 125 |
function ItemSize: TListSize; |
| 126 |
|
| 127 |
{ Add a subrange of another list here. |
| 128 |
|
| 129 |
This method may ignore the OnNotify mechanism, for the sake of fast execution. } |
| 130 |
procedure AddSubRange(const Source: TStructList {$ifndef FPC}<T>{$endif}; |
| 131 |
const Index, AddCount: TListSize); |
| 132 |
|
| 133 |
{ Assign here a linear interpolation of two other arrays. |
| 134 |
We take ACount items, from V1[Index1 ... Index1 + ACount - 1] and |
| 135 |
V2[Index2 ... Index2 + ACount - 1], and interpolate between them. |
| 136 |
|
| 137 |
It's Ok for both V1 and V2 to be the same objects. |
| 138 |
But their ranges should not overlap, for future optimizations. |
| 139 |
|
| 140 |
@raises(ELinearInterpolationImpossible On classes where linear interpolation is not possible, |
| 141 |
e.g. we cannot linearly interpolate lists of strings.) } |
| 142 |
procedure AssignLerpRange(const Fraction: Single; |
| 143 |
const V1, V2: TStructList {$ifndef FPC}<T>{$endif}; |
| 144 |
const Index1, Index2, ACount: TListSize); virtual; |
| 145 |
|
| 146 |
{ Assign linear interpolation between two other arrays. |
| 147 |
@raises EListsDifferentCount If V1 and V2 have different count. |
| 148 |
@raises(ELinearInterpolationImpossible On classes where linear interpolation is not possible, |
| 149 |
e.g. we cannot linearly interpolate lists of strings.) } |
| 150 |
procedure AssignLerp(const Fraction: Single; |
| 151 |
const V1, V2: TStructList {$ifndef FPC}<T>{$endif}); |
| 152 |
|
| 153 |
{ Does the SecondValue have equal type, length and content. |
| 154 |
The values are compared perfectly, without any tolerance for difference. } |
| 155 |
function PerfectlyEquals( |
| 156 |
const SecondValue: TStructList{$ifndef FPC}<T>{$endif}): Boolean; |
| 157 |
|
| 158 |
procedure AddArray(const A: array of T); deprecated 'use AddRange'; |
| 159 |
procedure AddList(const Source: TStructList {$ifndef FPC}<T>{$endif}); deprecated 'use AddRange'; |
| 160 |
procedure AssignArray(const A: array of T); deprecated 'use Assign'; |
| 161 |
end; |
| 162 |
|
| 163 |
{$endif} |
| 164 |
|
| 165 |
{$ifdef read_implementation} |
| 166 |
|
| 167 |
{ TStructList ---------------------------------------------------------------- } |
| 168 |
|
| 169 |
function TStructList{$ifndef FPC}<T>{$endif}.List: PTypeList; |
| 170 |
begin |
| 171 |
Result := PTypeList({$ifdef FPC} FItems {$else} inherited List {$endif}); |
| 172 |
end; |
| 173 |
|
| 174 |
function TStructList{$ifndef FPC}<T>{$endif}.L: PtrT; |
| 175 |
begin |
| 176 |
Result := PtrT({$ifdef FPC} FItems {$else} inherited List {$endif}); |
| 177 |
end; |
| 178 |
|
| 179 |
function TStructList{$ifndef FPC}<T>{$endif}.Add: PtrT; |
| 180 |
begin |
| 181 |
Count := Count + 1; |
| 182 |
Result := Ptr(Count - 1); |
| 183 |
end; |
| 184 |
|
| 185 |
function TStructList{$ifndef FPC}<T>{$endif}.Ptr(const I: TListSize): PtrT; |
| 186 |
begin |
| 187 |
{ List^ upper bound in Delphi may be too small, so ignore range check errors. |
| 188 |
We cannot use Addr(L[I]) or @L[I] as then FPC compilation fails. } |
| 189 |
Result := PtrT(PtrUInt(L) + PtrUInt(I) * PtrUInt(SizeOf(T))); |
| 190 |
end; |
| 191 |
|
| 192 |
procedure TStructList{$ifndef FPC}<T>{$endif}.Assign(const Source: TStructList{$ifndef FPC}<T>{$endif}); |
| 193 |
begin |
| 194 |
Clear; |
| 195 |
AddRange(Source); |
| 196 |
end; |
| 197 |
|
| 198 |
procedure TStructList{$ifndef FPC}<T>{$endif}.Assign(const A: array of T); |
| 199 |
begin |
| 200 |
Clear; |
| 201 |
AddRange(A); |
| 202 |
end; |
| 203 |
|
| 204 |
function TStructList{$ifndef FPC}<T>{$endif}.ItemSize: TListSize; |
| 205 |
begin |
| 206 |
Result := SizeOf(T); |
| 207 |
end; |
| 208 |
|
| 209 |
procedure TStructList{$ifndef FPC}<T>{$endif}.AddArray(const A: array of T); |
| 210 |
begin |
| 211 |
AddRange(A); |
| 212 |
end; |
| 213 |
|
| 214 |
procedure TStructList{$ifndef FPC}<T>{$endif}.AddList(const Source: TStructList{$ifndef FPC}<T>{$endif}); |
| 215 |
begin |
| 216 |
AddRange(Source); |
| 217 |
end; |
| 218 |
|
| 219 |
procedure TStructList{$ifndef FPC}<T>{$endif}.AddSubRange( |
| 220 |
const Source: TStructList{$ifndef FPC}<T>{$endif}; |
| 221 |
const Index, AddCount: TListSize); |
| 222 |
var |
| 223 |
OldCount: TListSize; |
| 224 |
begin |
| 225 |
OldCount := Count; |
| 226 |
Count := Count + AddCount; |
| 227 |
if Source.Count <> 0 then |
| 228 |
System.Move(Source.Ptr(Index)^, Ptr(OldCount)^, SizeOf(T) * AddCount); |
| 229 |
end; |
| 230 |
|
| 231 |
function TStructList{$ifndef FPC}<T>{$endif}.PerfectlyEquals( |
| 232 |
const SecondValue: TStructList{$ifndef FPC}<T>{$endif}): boolean; |
| 233 |
begin |
| 234 |
Result := |
| 235 |
(Count = SecondValue.Count) and |
| 236 |
CompareMem(L, SecondValue.L, SizeOf(T) * Count); |
| 237 |
end; |
| 238 |
|
| 239 |
procedure TStructList{$ifndef FPC}<T>{$endif}.AssignLerpRange(const Fraction: Single; |
| 240 |
const V1, V2: TStructList{$ifndef FPC}<T>{$endif}; |
| 241 |
const Index1, Index2, ACount: TListSize); |
| 242 |
begin |
| 243 |
raise Exception.Create('AssignLerpRange not implemented for ' + ClassName); |
| 244 |
end; |
| 245 |
|
| 246 |
procedure TStructList{$ifndef FPC}<T>{$endif}.AssignLerp(const Fraction: Single; |
| 247 |
const V1, V2: TStructList {$ifndef FPC}<T>{$endif}); |
| 248 |
begin |
| 249 |
if V1.Count <> V2.Count then |
| 250 |
raise EListsDifferentCount.CreateFmt('Different lists length: %d and %d, cannot do AssignLerp on the lists', |
| 251 |
[V1.Count, V2.Count]); |
| 252 |
AssignLerpRange(Fraction, V1, V2, 0, 0, V1.Count); |
| 253 |
end; |
| 254 |
|
| 255 |
procedure TStructList{$ifndef FPC}<T>{$endif}.AssignArray(const A: array of T); |
| 256 |
begin |
| 257 |
Assign(A); |
| 258 |
end; |
| 259 |
|
| 260 |
{$ifndef FPC} |
| 261 |
function TStructList<T>.InternalGetItem( |
| 262 |
const Index: TListSize): T; |
| 263 |
begin |
| 264 |
{ List^ upper bound in Delphi may be too small, so ignore range check errors. |
| 265 |
Unfortunately we cannot use L here, as then Delphi cannot compile cubic Bezier nodes... } |
| 266 |
{$warnings off} // using deprecated |
| 267 |
{$I norqcheckbegin.inc} |
| 268 |
Result := List^[Index]; |
| 269 |
{$I norqcheckend.inc} |
| 270 |
{$warnings on} |
| 271 |
end; |
| 272 |
|
| 273 |
procedure TStructList<T>.InternalSetItem( |
| 274 |
const Index: TListSize; const Value: T); |
| 275 |
begin |
| 276 |
Items[Index] := Value; |
| 277 |
end; |
| 278 |
{$endif} |
| 279 |
|
| 280 |
{$endif read_implementation} |