AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi TStringList ignoriert doppelte Einträge nicht
Thema durchsuchen
Ansicht
Themen-Optionen

TStringList ignoriert doppelte Einträge nicht

Offene Frage von "himitsu"
Ein Thema von DevidEspenschied · begonnen am 6. Sep 2010 · letzter Beitrag vom 6. Sep 2010
Antwort Antwort
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.326 Beiträge
 
Delphi 12 Athens
 
#1

AW: TStringList ignoriert doppelte Einträge nicht

  Alt 6. Sep 2010, 21:17
Auch wenn der Code, bei einer unsortierten Liste extrem langsam werden kann, was sich Aufgrund des Listentypes nicht ändern lößt.
Eine HashListe oder ein Suchindex in Form eines binären Baums oder einer weiteren Hashliste wäre besser.

Ich würde die OH ändern, vor den langsameren Einfügeoperationen waren und dann den Code so ändern:
Delphi-Quellcode:
procedure TStringList.Put(Index: Integer; const S: string);
begin
  if Cardinal(Index) >= Cardinal(FCount) then
    Error(@SListIndexError, Index);
  if (Duplicates <> dupIgnore) and (FList^[Index].FString <> S) and (IndexOf(S) >= 0) then
    case Duplicates of
      dupIgnore: begin
        Delete(Index);
        Exit;
      end;
      dupError: Error(@SDuplicateString, 0);
    end;
  Changing;
  FList^[Index].FString := S;
  Changed;
end;

function TStringList.AddObject(const S: string; AObject: TObject): Integer;
begin
  if not Sorted then
  begin
    if (Duplicates <> dupIgnore) and (IndexOf(S) >= 0) then
      case Duplicates of
        dupIgnore: Exit;
        dupError: Error(@SDuplicateString, 0);
      end;
    Result := FCount;
  end
  else
    if Find(S, Result) then
      case Duplicates of
        dupIgnore: Exit;
        dupError: Error(@SDuplicateString, 0);
      end;
  InsertItem(Result, S, AObject);
end;

procedure TStringList.InsertObject(Index: Integer; const S: string; AObject: TObject);
begin
  if Sorted then Error(@SSortedListError, 0);
  if (Index < 0) or (Index > FCount) then Error(@SListIndexError, Index);
  if (Duplicates <> dupIgnore) and (IndexOf(S) >= 0) then
    case Duplicates of
      dupIgnore: Exit;
      dupError: Error(@SDuplicateString, 0);
    end;
  InsertItem(Index, S, AObject);
end;
als neue Komponente ergibt das:
Delphi-Quellcode:
uses
  RTLConsts;

type
  TDupStringList = class(TStringList)
  protected
    procedure Put(Index: Integer; const S: string); override;
  public
    function AddObject(const S: String; AObject: TObject): Integer; override;
    procedure InsertObject(Index: Integer; const S: String; AObject: TObject); override;
  end;

procedure TDupStringList.Put(Index: Integer; const S: string);
begin
  if Cardinal(Index) >= Cardinal(Count) then
    Error(@SListIndexError, Index);
  if (Duplicates <> dupIgnore) and (Strings[Index] <> S) and (IndexOf(S) >= 0) then
    case Duplicates of
      dupIgnore: begin
        Delete(Index);
        Exit;
      end;
      dupError: Error(@SDuplicateString, 0);
    end;
  Inherited;
end;

function TDupStringList.AddObject(const S: string; AObject: TObject): Integer;
begin
  if not Sorted then
  begin
    if (Duplicates <> dupIgnore) and (IndexOf(S) >= 0) then
      case Duplicates of
        dupIgnore: Exit;
        dupError: Error(@SDuplicateString, 0);
      end;
    Result := Count;
  end
  else
    if Find(S, Result) then
      case Duplicates of
        dupIgnore: Exit;
        dupError: Error(@SDuplicateString, 0);
      end;
  InsertItem(Result, S, AObject);
end;

procedure TDupStringList.InsertObject(Index: Integer; const S: string; AObject: TObject);
begin
  if Sorted then Error(@SSortedListError, 0);
  if (Index < 0) or (Index > Count) then Error(@SListIndexError, Index);
  if (Duplicates <> dupIgnore) and (IndexOf(S) >= 0) then
    case Duplicates of
      dupIgnore: Exit;
      dupError: Error(@SDuplicateString, 0);
    end;
  InsertItem(Index, S, AObject);
end;
Code ungetestet, da einfach so dahingetippt, aber er sollte funktionieren.
Ein Therapeut entspricht 1024 Gigapeut.

Geändert von himitsu ( 6. Sep 2010 um 21:34 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort


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 08:38 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 by Thomas Breitkreuz