Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Algorithmen (https://www.delphipraxis.net/28-library-algorithmen/)
-   -   Delphi Mengenoperationen mit Stringlisten (https://www.delphipraxis.net/20262-mengenoperationen-mit-stringlisten.html)

shmia 15. Apr 2004 10:58


Mengenoperationen mit Stringlisten
 
Folgende Unit enthält 3 nützliche Proceduren zum Bilden der Schnittmenge, Differenzmenge oder
Vereinigungsmenge von 2 Stringlisten (bzw. TStrings-Objekten):

Delphi-Quellcode:
unit UStringListUtils;

interface

uses Classes;

procedure IntersectionStrings(const a,b, intersection:TStrings);
procedure DifferenceStrings(const a, b, diff: TStrings);
procedure UnionStrings(const a, b, union: TStrings);


implementation


{**************************************************************************
 * NAME:   IntersectionStrings
 * DESC:   kopiert die Schnittmenge von a und b nach intersection
 * EXAMPLE: a = ['Porsche', 'Daimler', 'BMW', 'Ferrari']
 *          b = ['VW', 'BMW', 'Fiat', 'Volvo', 'Smart']
 *          intersection = ['BMW']
 * PARAMS: [-]
 * CREATED: 00-00-2004/shmia
 * CHANGED: 00-00-2004/shmia
 *************************************************************************}
procedure IntersectionStrings(const a,b, intersection:TStrings);
var
  i : Integer;
begin
  Assert(Assigned(a));
  Assert(Assigned(b));
  Assert(Assigned(intersection));

  intersection.BeginUpdate;
  try
    intersection.Clear;
    for i:=0 to a.Count-1 do
    begin
      if b.IndexOf(a.Strings[i]) >= 0 then
        intersection.AddObject(a.Strings[i], a.Objects[i]);
    end;
  finally
    intersection.EndUpdate;
  end;
end;

{**************************************************************************
 * NAME:   DifferenceStrings
 * DESC:   kopiert Menge a OHNE Menge b nach diff
 * EXAMPLE: a = ['Porsche', 'Daimler', 'BMW', 'Ferrari']
 *          b = ['VW', 'BMW', 'Fiat', 'Volvo', 'Smart']
 *          diff = ['Porsche', 'Daimler', 'Ferrari']
 * PARAMS: [-]
 * CREATED: 00-00-2004/shmia
 * CHANGED: 00-00-2004/shmia
 *************************************************************************}
procedure DifferenceStrings(const a, b, diff: TStrings);
var
  i, idx : Integer;
begin
  Assert(Assigned(a));
  Assert(Assigned(b));
  Assert(Assigned(diff));
  diff.BeginUpdate;
  try
    diff.Assign(a);
    for i := 0 to b.Count -1 do
    begin
      idx := diff.IndexOf(b.Strings[i]);
      if idx >= 0 then
        diff.Delete(idx);
    end;
  finally
    diff.EndUpdate;
  end;
end;


{**************************************************************************
 * NAME:   UnionStrings
 * DESC:   Kopiert die Vereinigungsmenge von a und b nach union
 * EXAMPLE: a = ['Porsche', 'Daimler', 'BMW', 'Ferrari']
 *          b = ['VW', 'BMW', 'Fiat', 'Volvo', 'Smart']
 *          union = ['Porsche', 'Daimler', 'BMW', 'Ferrari', 'VW', 'Fiat', 'Volvo', 'Smart']
 * PARAMS: [-]
 * CREATED: 00-00-2004/shmia
 * CHANGED: 00-00-2004/shmia
 *************************************************************************}
procedure UnionStrings(const a, b, union: TStrings);
var
  i : Integer;
begin
  Assert(Assigned(a));
  Assert(Assigned(b));
  Assert(Assigned(union));
  union.BeginUpdate;  
  try
    union.Assign(a);
    for i := 0 to b.Count -1 do
    begin
      if union.IndexOf(b.Strings[i]) = -1 then
        union.AddObject(b.Strings[i], b.Objects[i]);
    end;
  finally
    union.EndUpdate;
  end;
end;

end.
[edit=Matze]Code formatiert. Mfg, Matze[/edit]


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:10 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