Registriert seit: 2. Mär 2004
5.508 Beiträge
Delphi 5 Professional
|
Mengenoperationen mit Stringlisten
15. Apr 2004, 11:58
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]
Andreas
|