![]() |
Universelles Array
Gegeben ist ein record in dem Daten in einem zweidimensionalen Array abgelegt sind. Die Dimension des Arrays wird durch zwei Aufzählungstypen bestimmt. Dazu noch ein paar Getter und Setter und Funktionen zur Summenberechnung, denen ein Set der Aufzählungstypen übergeben wird.
Ein vereinfachtes Besispiel ist hier:
Delphi-Quellcode:
Dieses Konstrukt verwende ich mehrfach mit weiteren Aufzählungstypen.
Type
TEnumA = (A1, A2, A3, A4); TEnumASet = Set Of TEnumA; TEnumB = (B1, B2, B3, B4); TEnumBSet = Set Of TEnumB; type TccArray = record private fCount:array[TEnumA,TEnumB] of Integer; fValue:array[TEnumA,TEnumB] of Double; function GetCount(IndexA: TEnumA; IndexB: TEnumB): integer; procedure SetCount(IndexA: TEnumA; IndexB: TEnumB; const Value: integer); function GetValue(IndexA: TEnumA; IndexB: TEnumB): integer; procedure SetValue(IndexA: TEnumA; IndexB: TEnumB; const Value: integer); public Property Count[IndexA:TEnumA;IndexB:TEnumB]:integer read GetCount write SetCount; Property Value[IndexA:TEnumA;IndexB:TEnumB]:integer read GetValue write SetValue; Function Average(IndexA:TEnumA;IndexB:TEnumB):Double; Function CountSum(IndexASet:TEnumASet;IndexBSet:TEnumBSet):Integer; Function ValueSum(IndexASet:TEnumASet;IndexBSet:TEnumBSet):Double; end;
Delphi-Quellcode:
In der Vergangenheit wurde dann das obrige Beispiel durch Copy/Paste an die weiteren Aufzählungstypen angepasst.TEnumC = (C1, C2, C3, C4); TEnumCSet = Set Of TEnumC; TEnumD = (D1, D2, D3, D4); TEnumDSet = Set Of TEnumD; Das stört mich mittlerweile. Es muss was anderes her. Ich habe an Generics gedacht. Es würde sich folgendes anbieten:
Delphi-Quellcode:
Die Implementation ist erst mal zweitrangig. Statt Dictionary kann auch was anderes verwendet werden. Der Vorteil ist, ich brauche nur noch folgende Ableitung:TccArray<TXType, TYType> = class private fCount: TDictionary<TPair<TXType, TYType>, Integer>; fValue: TDictionary<TPair<TXType, TYType>, Integer>; function GetCount(IndexA: TXType; IndexB: TYType): Integer; function GetValue(IndexA: TXType; IndexB: TYType): Integer; procedure SetCount(IndexA: TXType; IndexB: TYType; const Value: Integer); procedure SetValue(IndexA: TXType; IndexB: TYType; const Value: Integer); public constructor Create; Destructor Destroy; override; Property Count[IndexA: TXType; IndexB: TYType]: Integer read GetCount write SetCount; Property Value[IndexA: TXType; IndexB: TYType]: Integer read GetValue write SetValue; Function Average(IndexA: TXType; IndexB: TYType): Double; end;
Delphi-Quellcode:
Und schon habe ich die komplette Funktionalität.TMyArrayAB = TccArray<TEnumA, TEnumB>; TMyArrayCD = TccArray<TEnumC, TEnumD>; Mit sind aber die Funktionen mit den Sets sehr wichtig.
Delphi-Quellcode:
Wie bekommen ich das mit Generics hin?Function CountSum(IndexASet:TEnumASet;IndexBSet:TEnumBSet):Integer; Function ValueSum(IndexASet:TEnumASet;IndexBSet:TEnumBSet):Double; |
AW: Universelles Array
Wird nicht funktionieren, da du TXType und TYType nicht auf "enum/ordinal Typ" einschränken kannst, was aber für die Benutzung in einem statischen array und/oder set notwendig ist.
Was dir eventuell das Copy/Paste ersparen könnte, wäre ![]() |
AW: Universelles Array
Zitat:
Zitat:
|
AW: Universelles Array
Blöd, dass Delphi keine Makros kann,
bzw. dass man bei den Generics die Typenbeschränkung nicht so schön differenzieren kann und keine Konstanten als "Parameter" verwenden darf. :cry: |
AW: Universelles Array
Zitat:
Übergibt doch dem Record einfach im Constructor eine Methodenzeiger der aus dem Set's bytes macht und entsprechend der Set-Größe dir die Arrays dynamisch erzeugt... Oder ruf es direkt so auf
Delphi-Quellcode:
TccArray.Count[SetAtoByte(A1),SetBtoByte(B2)] := 7;
|
AW: Universelles Array
Zitat:
Zitat:
Die Klasse sieht jetzt so aus:
Delphi-Quellcode:
Darin sind zwei TDictionary, welche die Daten aufnehmen. Wer mir nicht so sicher, ob es performant genug ist.
TccArray2D<TIndexX, TIndexY> = class
private fCount: TDictionary<TPair<TIndexX, TIndexY>, Integer>; fValue: TDictionary<TPair<TIndexX, TIndexY>, Double>; function GetCount(aIndexX: TIndexX; aIndexY: TIndexY): Integer; function GetValue(aIndexX: TIndexX; aIndexY: TIndexY): Double; procedure SetCount(aIndexX: TIndexX; aIndexY: TIndexY; const Value: Integer); procedure SetValue(aIndexX: TIndexX; aIndexY: TIndexY; const Value: Double); Function IndexPair(aKey: TIndexX; aValue: TIndexY): TPair<TIndexX, TIndexY>; public constructor Create; Destructor Destroy; override; Property Count[aIndexX: TIndexX; aIndexY: TIndexY]: Integer read GetCount write SetCount; Property Value[aIndexX: TIndexX; aIndexY: TIndexY]: Double read GetValue write SetValue; Function Average(aIndexX: TIndexX; aIndexY: TIndexY): Double; Function CountSum(aIndexX: array of TIndexX; aIndexY: array of TIndexY): Integer; Function ValueSum(aIndexX: array of TIndexX; aIndexY: array of TIndexY): Double; Procedure IncCount(aIndexX: TIndexX; aIndexY: TIndexY; aInc: Integer); Procedure AddValue(aIndexX: TIndexX; aIndexY: TIndexY; aValue: Double); end; Ein paar Pi-daumen-tests haben ergeben, 32000000 Werte zufügen benötigt ca. 5 Sekunden. Reicht für mich. Das mit der Funktion, welches die Sets in ein Array wandeln ist ne gute Idee. Auch kann ich die Klasse einfach erweitern. Einfach in der Ableitung eine Funktion mit den Sets definieren. Darin in Array Wandeln und an die vorhandene Array-Funktion übergeben
Delphi-Quellcode:
TMyArray = class(TccArray2D<TEnumA, TEnumB>)
private public Function CountSumFromSet(IndexA: TEnumASet; IndexB: TEnumBSet): Integer; end; |
AW: Universelles Array
Zitat:
|
AW: Universelles Array
Zitat:
![]() Frank |
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:21 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz