Created
February 1, 2024 15:06
-
-
Save michaliskambi/1fc74a433e4da6afcc24fc686f468ff4 to your computer and use it in GitHub Desktop.
Testing list ownership, see https://discord.com/channels/389676745957310465/468902667003887646/1202630782791852084
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// castle-engine simple-compile project1.lpr --compiler-option=-gh --compiler-option=-gl --verbose | |
program project1; | |
uses | |
SysUtils, Generics.Collections, Generics.Defaults; | |
type | |
TComplexObj = class | |
private | |
FMainKey: String; | |
FSetKey: String; | |
public | |
procedure Add(const AMainKey: String; const ASetKey: String); | |
end; | |
//PComplexObj = ^TComplexObj; | |
TSomeList = specialize TObjectList<TComplexObj>; | |
procedure TComplexObj.Add(const AMainKey, ASetKey: String); | |
begin | |
FMainKey := AMainKey; | |
FSetKey := ASetKey; | |
end; | |
var | |
I: Integer; | |
obj: TComplexObj; | |
Index: TSomeList; | |
SetIndex: specialize TObjectDictionary<string, TSomeList>; | |
MasterList: TSomeList; | |
begin | |
// Masterlist owns all instances of TComplexObj | |
MasterList := TSomeList.Create(true); | |
// SetIndex just uses pointers to them | |
SetIndex := specialize TObjectDictionary<string, TSomeList>.Create([doOwnsValues]); | |
// Populate MasterList | |
obj := TComplexObj.Create; | |
obj.Add('abc', 'set1'); | |
MasterList.Add(obj); | |
obj := TComplexObj.Create; | |
obj.Add('abd', 'set2'); | |
MasterList.Add(obj); | |
obj := TComplexObj.Create; | |
obj.Add('abe', 'set2'); | |
MasterList.Add(obj); | |
obj := TComplexObj.Create; | |
obj.Add('abf', 'set1'); | |
MasterList.Add(obj); | |
for I := 0 to MasterList.Count - 1 do | |
begin | |
obj := MasterList[I]; | |
if SetIndex.TryGetValue(obj.FSetKey, Index) then | |
begin | |
Index.Add(obj); | |
Writeln('add to index for ', obj.FSetKey); | |
end | |
else | |
begin | |
Index := TSomeList.Create(false); | |
Index.Add(obj); | |
SetIndex.Add(obj.FSetKey, Index); | |
Writeln('new index for ', obj.FSetKey); | |
end; | |
end; | |
SetIndex.Free; | |
MasterList.Free; | |
end. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment