AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein Delphi Wert [Word] in Liste/Array vorhanden
Thema durchsuchen
Ansicht
Themen-Optionen

Wert [Word] in Liste/Array vorhanden

Ein Thema von Alex_ITA01 · begonnen am 19. Dez 2013 · letzter Beitrag vom 21. Dez 2013
Antwort Antwort
Seite 3 von 3     123   
Alex_ITA01

Registriert seit: 22. Sep 2003
1.115 Beiträge
 
Delphi 12 Athens
 
#21

AW: Wert [Word] in Liste/Array vorhanden

  Alt 20. Dez 2013, 22:29
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
Let's fetz sprach der Frosch und sprang in den Mixer
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#22

AW: Wert [Word] in Liste/Array vorhanden

  Alt 21. Dez 2013, 09:10
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.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 3 von 3     123   


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:35 Uhr.
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