Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Generic class <T> , wie füge ich konkrete Daten ein ? (https://www.delphipraxis.net/214402-generic-class-t-wie-fuege-ich-konkrete-daten-ein.html)

bernhard_LA 10. Jan 2024 23:56

AW: Generic class <T> , wie füge ich konkrete Daten ein ?
 
Liste der Anhänge anzeigen (Anzahl: 1)
Generic List TList<CLass> vs. TList<Records> bei meinen Versuchen liegt TList<Rec> deutlich vor der Class Variante , in Bezug auf Speed und Speicherverbrauch,
sehe ich hier was falsch?




Delphi-Quellcode:
type

  /// <summary>
  /// here it is just a simple pixel but can be more in future
  /// </summary>
  TClusterData3 = class
    DrawingColor: TColor;
    x, y: Integer;
    chrlabel : char;
    // ...
    // ..
    // .
  end;


  TClusterData3REC = record
    DrawingColor: TColor;
    x, y: Integer;
    chrlabel : char;
    // ...
    // ..
    // .
  end;



  /// <summary>
  /// a bit different pixeldefinition
  /// </summary>
  TClusterData2 = class
    BWColor: Byte;
    x, y: Integer;
    // tbd.
    // ...
    // ..
    // .
  end;

  TClusterData = class
    DrawingColor: TColor;
    x, y: Integer;
  end;

  /// <summary>
  /// here it can be just a simple pixel, in general we store the complete morginal data inside this list
  /// </summary>
  TRawData<T> = class(TList<T>)
  end;






procedure TForm1.CornerButton_list_classClick(Sender: TObject);
var
      rawdata_class : TRawData<TClusterData3>;
      i : Integer;
      newclass : TClusterData3;

begin

     StatusbarLabel.Text := ' Create Elements';

     AProfiler.Start;

     rawdata_class:=TRawData<TClusterData3>.Create;

     for I := 0 to (16000*16000) do
       begin
          newclass :=TClusterData3.Create;

          newclass.x := random(1000);
          newclass.y := random(1000);
          newclass.DrawingColor := 20000 ;

          rawdata_class.Add(newclass);
       end;

      AProfiler.Stop;

      StatusbarLabel.Text := 'DONE CLASS: ' + AProfiler.GetElapsedTime(total_time) ;

       rawdata_class.Free;
end;

procedure TForm1.CornerButton_list_recClick(Sender: TObject);
var  rawdata_rec : TRawData<TClusterData3REC>;
      i : Integer;
      newrec : TClusterData3REC;

begin

     StatusbarLabel.Text := ' Create Elements';

     AProfiler.Start;

     rawdata_rec:=TRawData<TClusterData3REC>.Create;

     for I := 0 to (16000*16000) do
       begin
          newrec.x := random(1000);
          newrec.y := random(1000);
          newrec.DrawingColor := 20000 ;

          rawdata_rec.Add(newrec);
       end;

      AProfiler.Stop;

      StatusbarLabel.Text := 'DONE REC: ' + AProfiler.GetElapsedTime(total_time) ;

      rawdata_rec.Free;
end;

freimatz 11. Jan 2024 08:00

AW: Generic class <T> , wie füge ich konkrete Daten ein ?
 
Was Speed betrifft halte ich das für sehr plausibel. Bei Speicherverbrauch würde ich sagen hängt davon ab wie viel Speicher der record benötigt und ob das gerade so in einen Block passt.
Wenn Dir das wichtig ist, würde ich noch TArray<> anschauen.

jaenicke 11. Jan 2024 08:18

AW: Generic class <T> , wie füge ich konkrete Daten ein ?
 
Das kommt schon hin, denn die Erzeugung einer Klasse beinhaltet ja mehr als nur die Speicherreservierung und enthält z.B. auch in TObject Daten, die auch im Speicher landen.

Bei der Verwendung hat eine Klasse dann wieder Vorteile bei der Geschwindigkeit, da diese als Referenz verwendet wird, während der Record aus der Liste kopiert wird. Das kannst du aber wie gesagt durch die Verwendung von Pointern auf die Records umgehen, sprich indem du selbst den Speicher reservierst und freigibst, so dass in der Liste nur die Pointer auf die Records liegen.

himitsu 11. Jan 2024 13:02

AW: Generic class <T> , wie füge ich konkrete Daten ein ?
 
Wird aber "oft" die Liste geändert (die hat ja intern auch nur ein TArray<T>),
dann muß da bei Objekten nur eine kurze Liste aus Zeigern umkopiert werden, während es bei den Records die kompletten Daten sind.

Vor allem bei sehr großen Listen und/oder Records kann das negative Auswirkungen haben.

bernhard_LA 12. Jan 2024 07:42

AW: Generic class <T> , wie füge ich konkrete Daten ein ?
 
wenn ich aus Speicherbedarf und Performance Gründen bei Records bleiben möchte bzw. auch muss :-) ...
gibt es keine Option meine Klassse generisch zu lassen (dh. ohne Informationen über die Art der Daten die ich verarbeite) und das Feld FRawData: TArray<T>; mit Daten über eine Klassenfunktion zu befüllen ?


Delphi-Quellcode:
 

TKMeans<T> = class
  private

  private
    FClusteredData: TClusterList<T>;

    FRawData: TArray<T>;

    FNumClusters: Integer;

    FDistanceMetric: TDistanceMetricfunction<T>;

    FCentroidfct: TCentroidfunction<T>;

    FMaxIterations: Integer;

  public
    constructor Create(NumClusters: Integer;
      DistanceMetric: TDistanceMetricfunction<T>;
      Centroidfct: TCentroidfunction<T>; MaxIterations: Integer = 10);

    procedure LoadData(SoureBitMap: TBitmap); overload;

    function FindNewClusterCentroids: Boolean;

    procedure GroupData2NearestCluster;

    procedure SaveData(OutBitMap: TBitmap);
  end;

Der Code um zu Klasse zu verwenden sieht aktuell so aus:

Delphi-Quellcode:
 

 function DistanceMetric(const A, B: TClusterDataREC): Double;
  begin
    Result := Sqrt(Sqr(A.x - B.x) + Sqr(A.y - B.y));
  end;

// Define a centroid function that returns the TClusterData3 object with the average coordinates of all elements in the cluster
  function Centroidfct(const A: TClusterDataREC): Cardinal;
  begin
    Result := Round((A.DrawingColor and $FF) + ((A.DrawingColor shr 8) and $FF)
      + ((A.DrawingColor shr 16) and $FF)) div 3;
  end;



procedure TForm1.CornerButton1Click(Sender: TObject);
var
  MyKMeans: TKMeans<TClusterDataREC>;

begin

  MyKMeans := TKMeans<TClusterDataREC>.Create(5, DistanceMetric,
    Centroidfct, 10);
  try
     ///  hoer kommt dann noch was ...
  finally
    MyKMeans.Free;
  end;
end;
Müsste ich die Loaddata Funktion der Klasse procedure
Delphi-Quellcode:
TKMeans<T>.LoadData(SoureBitMap: TBitmap);
auch als Parameter an die create Funktion meiner Klasse übergeben ? Wie würdet Ihr dies Implementieren , was ist hier ein besserer Ansatz ?
ach ja , Loaddata kann natürlich wieder von allen möglichen Daten laden müssen ( Bitmaps, csv - Files, Datenbanken, ......)

TiGü 12. Jan 2024 10:03

AW: Generic class <T> , wie füge ich konkrete Daten ein ?
 
Zitat:

Zitat von bernhard_LA (Beitrag 1531829)
wenn ich aus Speicherbedarf und Performance Gründen bei Records bleiben möchte bzw. auch muss :-) ...
gibt es keine Option meine Klassse generisch zu lassen (dh. ohne Informationen über die Art der Daten die ich verarbeite) und das Feld FRawData: TArray<T>; mit Daten über eine Klassenfunktion zu befüllen ?


Delphi-Quellcode:
 

TKMeans<T> = class
  private

  private
    FClusteredData: TClusterList<T>;

    FRawData: TArray<T>;

Zum lesen des internen Arrays der Clusterliste (array of T) müsstest du einen direkten Zugriff haben.
Ungefähr sowas wie FClusteredData.List.
Siehe: https://docwiki.embarcadero.com/Libr...ons.TList.List
Man kann darüber auch iterieren und sich den RangeCheck in TList.GetItem sparen.
Auch brauchst du dann wahrscheinlich keine Doppelung mit FRawData.

Zum Bulk befüllen müsste es diverse AddRange-Methoden geben:
https://docwiki.embarcadero.com/Libr...TList.AddRange

bernhard_LA 12. Jan 2024 15:50

AW: Generic class <T> , wie füge ich konkrete Daten ein ?
 
nur eine limitierte Flexibility erreicht via Vererbung ...


Delphi-Quellcode:
const
  Infinity = 10000000;

type
  TClusterDataREC = record
    DrawingColor: TColor;
    x, y: Integer;
    chrlabel: char;
    // ...
    // ..
    // .
  end;

  /// <summary>
  /// a bit different pixeldefinition
  /// </summary>
  TClusterDataREC2 = class
    BWColor: Byte;
    x, y: Integer;
    // tbd.
    // ...
    // ..
    // .
  end;

  /// <summary>
  /// here it can be just a simple pixel description,
  /// in general we store the complete orginal data inside this list
  /// </summary>
  TRawData<T> = class(TList<T>)
  end;

  /// <summary>
  /// store the data now inside a cluster with a Centroid
  /// </summary>
  TCluster<T> = record
    /// <summary>
    /// <para>
    /// as of today T, but in future some other data type , depending
    /// </para>
    /// <para>
    /// on future research :-)
    /// </para>
    /// </summary>
    Center: T;

    /// <summary>
    /// the selected elements from out complete raw data
    /// </summary>
    ClusterElements: TArray<T>;
  end;

  /// <summary>
  /// the cluster list
  /// </summary>
  TClusterList<T> = class(TList < TCluster < T >> )
  private
    function GetItem(Aindex: Integer): TCluster<T>;
    procedure SetItem(Aindex: Integer; const Value: TCluster<T>);
  public

    property Items[Aindex: Integer]: TCluster<T> Read GetItem Write SetItem;
  end;

type
  /// <summary>
  /// measure the distance according to this function
  /// </summary
TDistanceMetricfunction < T >= reference to
function(const A, B: T): Double;

type
  /// <summary>
  /// result of this function could be the TColor value , but also
  /// coordinates my have some impact in future ....
  /// </summary
TCentroidfunction < T >= reference to
function(const A: T): Cardinal;

type
  TKMeans<T> = class
  private

  private
    FClusteredData: TClusterList<T>;

    FRawData: TArray<T>;

    FNumClusters: Integer;

    FDistanceMetric: TDistanceMetricfunction<T>;

    FCentroidfct: TCentroidfunction<T>;

    FMaxIterations: Integer;

  public
    constructor Create(NumClusters: Integer;
      DistanceMetric: TDistanceMetricfunction<T>;
      Centroidfct: TCentroidfunction<T>; MaxIterations: Integer = 10);

    function FindNewClusterCentroids: Boolean;

    procedure GroupData2NearestCluster;

    property RawData: TArray<T> read FRawData write FRawData;
  end;

type
  TImageClusterKMeans = class(TKMeans<TClusterDataREC>)

  public

    procedure LoadData(SoureBitMap: TBitmap);

    procedure SaveData(OutBitMap: TBitmap);
  end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:17 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz