Delphi-PRAXiS
Seite 3 von 3     123   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi Wert [Word] in Liste/Array vorhanden (https://www.delphipraxis.net/178134-wert-%5Bword%5D-liste-array-vorhanden.html)

Alex_ITA01 20. Dez 2013 22:29

AW: Wert [Word] in Liste/Array vorhanden
 
Hallo zusammen,

also das array ist nicht sortiert und kann unterschiedlich groß sein.

Ich habe es jetzt mit einer TStringList erstmal gemacht und frage dann IndexOf ab.
Generic List hätte ich auch machen können.

Die ASM Funktion muss ich gestehen, verstehe ich nicht ganz. Vielleicht hat mal einer Zeit und Lust mir das zu erklären, was da genau in jeder Zeile passiert?!

Hashset sieht erstmal für meinen "kleinen" Anwendungsfall überdimensioniert aus ;-)
Gucke mir das aber gerne mal noch genauer an.

Viele Grüße

Sir Rufo 21. Dez 2013 09:10

AW: Wert [Word] in Liste/Array vorhanden
 
Das mit der StringList ist aber weder elegant noch schnell.

Für solche Basics lege ich mir ein Helferlein zu und kann dann
Delphi-Quellcode:
  var
    LArray : array of Word;
    LValue : Word;
  begin
    if TArrayHandler<Word>.Contains( TArray<Word>( LArray ), LValue )
    then
      begin

      end;
  end;
oder eben eleganter
Delphi-Quellcode:
  var
    LArray : TArray<Word>;
    LValue : Word;
  begin
    if TArrayHandler<Word>.Contains( LArray, LValue )
    then
      begin

      end;
  end;
Das Helferlein selber, dem man auch noch viel mehr beibringen kann (Sortieren, etc.)
Delphi-Quellcode:
unit Utils.ArrayHandler;

interface

  uses
    System.Generics.Defaults;

  type
    TArrayHandler<T> = class
    public
      // Append
      class procedure Append( var AArray : TArray<T>; const AValue : T ); overload;
      class procedure Append( var AArray : TArray<T>; const AValues : TArray<T> ); overload;
      class procedure Append( var AArray : TArray<T>; const AValues : array of T ); overload;
      // Contains
      class function Contains( const AArray : TArray<T>; const AValue : T ) : Boolean; overload;
      class function Contains( const AArray : TArray<T>; const AValue : T; AComparer : IComparer<T> ) : Boolean; overload;
      // Shuffle
      class procedure Shuffle( var AArray : TArray<T> );
    end;

implementation

  { TArrayHandler<T> }

  class procedure TArrayHandler<T>.Append( var AArray : TArray<T>; const AValue : T );
    begin
      SetLength( AArray, Length( AArray ) + 1 );
      AArray[high( AArray )] := AValue;
    end;

  class procedure TArrayHandler<T>.Append( var AArray : TArray<T>; const AValues : TArray<T> );
    var
      LStart : Integer;
      LIdx  : Integer;
      LLow  : Integer;
    begin
      LStart := high( AArray ) + 1;
      SetLength( AArray, Length( AArray ) + Length( AValues ) );
      LLow := low( AValues );

      for LIdx := LLow to high( AValues ) do
        begin
          AArray[LStart + LIdx - LLow] := AValues[LIdx];
        end;
    end;

  class procedure TArrayHandler<T>.Append( var AArray : TArray<T>; const AValues : array of T );
    var
      LStart : Integer;
      LIdx  : Integer;
      LLow  : Integer;
    begin
      LStart := high( AArray ) + 1;
      SetLength( AArray, Length( AArray ) + Length( AValues ) );
      LLow := low( AValues );

      for LIdx := LLow to high( AValues ) do
        begin
          AArray[LStart + LIdx - LLow] := AValues[LIdx];
        end;
    end;

  class function TArrayHandler<T>.Contains( const AArray : TArray<T>; const AValue : T; AComparer : IComparer<T> ) : Boolean;
    var
      LIdx : Integer;
    begin
      for LIdx := low( AArray ) to high( AArray ) do
        if AComparer.Compare( AValue, AArray[LIdx] ) = 0
        then
          Exit( True );
      Result := False;
    end;

  class procedure TArrayHandler<T>.Shuffle( var AArray : TArray<T> );
    var
      LIdx   : Integer;
      LNewIdx : Integer;
      LTmp   : T;
    begin
      for LIdx := high( AArray ) downto low( AArray ) + 1 do
        begin
          LNewIdx := Random( LIdx + 1 );
          if LIdx <> LNewIdx
          then
            begin
              LTmp           := AArray[LIdx];
              AArray[LIdx]   := AArray[LNewIdx];
              AArray[LNewIdx] := LTmp;
            end;
        end;
    end;

  class function TArrayHandler<T>.Contains( const AArray : TArray<T>; const AValue : T ) : Boolean;
    begin
      Result := Self.Contains( AArray, AValue, TComparer<T>.Default );
    end;

end.


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:14 Uhr.
Seite 3 von 3     123   

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