Skip to content

Instantly share code, notes, and snippets.

@michaliskambi
Created February 1, 2024 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaliskambi/1fc74a433e4da6afcc24fc686f468ff4 to your computer and use it in GitHub Desktop.
Save michaliskambi/1fc74a433e4da6afcc24fc686f468ff4 to your computer and use it in GitHub Desktop.
// 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