From 2424b8cf04033e5725a193ae37526976ef3f5408 Mon Sep 17 00:00:00 2001 From: Bergmann89 Date: Wed, 27 Apr 2016 21:54:13 +0200 Subject: [PATCH] * [uutlGenerics] added ForEach method for all list types --- uutlGenerics.pas | 40 ++++++++++++++++++++++++++++++++++++-- uutlObservableGenerics.pas | 21 +++++++++----------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/uutlGenerics.pas b/uutlGenerics.pas index 86f05db..7469206 100644 --- a/uutlGenerics.pas +++ b/uutlGenerics.pas @@ -82,6 +82,7 @@ type PListItem = ^TListItem; public type + TItemEvent = procedure(aSender: TObject; const aIndex: Integer; const aItem: T) of object; TEnumerator = class(TObject) private fReverse: Boolean; @@ -117,6 +118,7 @@ type function GetEnumerator: TEnumerator; function GetReverseEnumerator: TEnumerator; + procedure ForEach(const aEvent: TItemEvent); procedure Clear; constructor Create(const aOwnsObjects: Boolean = true); @@ -180,12 +182,15 @@ type //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// generic TutlHashSetBase = class(specialize TutlListBase) public type + THashItemEvent = procedure(aSender: TObject; const aItem: T) of object; IComparer = specialize IutlComparer; private fComparer: IComparer; protected function SearchItem(const aMin, aMax: Integer; const aItem: T; out aIndex: Integer): Integer; public + procedure ForEach(const aEvent: THashItemEvent); + constructor Create(aComparer: IComparer; const aOwnsObjects: Boolean = true); destructor Destroy; override; end; @@ -224,6 +229,8 @@ type generic TutlMapBase = class(TObject) public type + TKeyValuePairEvent = procedure(aSender: TObject; const aKey: TKey; const aValue: TValue) of object; + IComparer = specialize IutlComparer; TKeyValuePair = packed record Key: TKey; @@ -316,6 +323,7 @@ type procedure DeleteAt(const aIndex: Integer); procedure Clear; + procedure ForEach(const aEvent: TKeyValuePairEvent); function GetEnumerator: TValueEnumerator; function GetReverseEnumerator: TValueEnumerator; @@ -895,6 +903,15 @@ begin result := TEnumerator.Create(fList, true); end; +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TutlListBase.ForEach(const aEvent: TItemEvent); +var i: Integer; +begin + if not Assigned(aEvent) then + for i := 0 to fList.Count-1 do + aEvent(self, i, PListItem(fList[i])^.data); +end; + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlListBase.Clear; begin @@ -1131,6 +1148,15 @@ begin end; end; +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TutlHashSetBase.ForEach(const aEvent: THashItemEvent); +var item: T; +begin + if Assigned(aEvent) then + for item in self do + aEvent(self, item); +end; + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TutlHashSetBase.Create(aComparer: IComparer; const aOwnsObjects: Boolean); begin @@ -1450,6 +1476,15 @@ begin fHashSetRef.Clear; end; +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure TutlMapBase.ForEach(const aEvent: TKeyValuePairEvent); +var kvp: TKeyValuePair; +begin + if Assigned(aEvent) then + for kvp in fHashSetRef do + aEvent(self, kvp.Key, kvp.Value); +end; + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlMapBase.GetEnumerator: TValueEnumerator; begin @@ -1747,7 +1782,8 @@ class function TutlEnumHelper.TryToEnum(aStr: String; out aValue: T): Boolean; var a: T; begin - Result:= false; + a := T(0); + Result := false; if Length(aStr) = 0 then exit; @@ -1757,7 +1793,7 @@ begin Result:= IOResult <> 106; {$Pop} if Result then - aValue:= a; + aValue := a; end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/uutlObservableGenerics.pas b/uutlObservableGenerics.pas index ca03813..ac35d02 100644 --- a/uutlObservableGenerics.pas +++ b/uutlObservableGenerics.pas @@ -28,8 +28,7 @@ type //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// generic TutlObservableCustomList = class(specialize TutlCustomList) public type - TEvent = procedure(aSender: TObject; const aIndex: Integer; const aItem: T) of object; - TEventList = specialize TutlEventList; + TEventList = specialize TutlEventList; private fOnAddItem: TEventList; fOnRemoveItem: TEventList; @@ -55,8 +54,7 @@ type //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// generic TutlObservableCustomHashSet = class(specialize TutlCustomHashSet) public type - TEvent = procedure(aSender: TObject; const aItem: T) of object; - TEventList = specialize TutlEventList; + TEventList = specialize TutlEventList; private fOnAddItem: TEventList; fOnRemoveItem: TEventList; @@ -82,8 +80,7 @@ type //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// generic TutlObservableCustomMap = class(specialize TutlMapBase) public type - TEvent = procedure(aSender: TObject; const aKey: TKey; const aValue: TValue) of object; - TEventList = specialize TutlEventList; + TEventList = specialize TutlEventList; TObservableHashSet = class(THashSet) private fOwner: TObject; @@ -173,7 +170,7 @@ end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlObservableCustomList.InsertIntern(const aIndex: Integer; const aItem: T); var - e: TEvent; + e: TItemEvent; begin inherited InsertIntern(aIndex, aItem); if Assigned(fOnAddItem) then @@ -184,7 +181,7 @@ end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlObservableCustomList.DeleteIntern(const aIndex: Integer; const aFreeItem: Boolean); var - e: TEvent; + e: TItemEvent; begin if Assigned(fOnRemoveItem) then for e in fOnRemoveItem do @@ -221,7 +218,7 @@ end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlObservableCustomHashSet.InsertIntern(const aIndex: Integer; const aItem: T); var - e: TEvent; + e: THashItemEvent; begin inherited InsertIntern(aIndex, aItem); if Assigned(fOnAddItem) then @@ -232,7 +229,7 @@ end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlObservableCustomHashSet.DeleteIntern(const aIndex: Integer; const aFreeItem: Boolean); var - e: TEvent; + e: THashItemEvent; begin if Assigned(fOnRemoveItem) then for e in fOnRemoveItem do @@ -269,7 +266,7 @@ end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlObservableCustomMap.TObservableHashSet.InsertIntern(const aIndex: Integer; const aItem: TKeyValuePair); var - e: TEvent; + e: TKeyValuePairEvent; begin inherited InsertIntern(aIndex, aItem); if Assigned(fOnAddItem) then begin @@ -281,7 +278,7 @@ end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlObservableCustomMap.TObservableHashSet.DeleteIntern(const aIndex: Integer; const aFreeItem: Boolean); var - e: TEvent; + e: TKeyValuePairEvent; tmp: TKeyValuePair; begin if Assigned(fOnRemoveItem) then begin