浏览代码

* [uutlGenerics] added ForEach method for all list types

master
Bergmann89 10 年前
父节点
当前提交
2424b8cf04
共有 2 个文件被更改,包括 47 次插入14 次删除
  1. +38
    -2
      uutlGenerics.pas
  2. +9
    -12
      uutlObservableGenerics.pas

+ 38
- 2
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<T> = class(specialize TutlListBase<T>)
public type
THashItemEvent = procedure(aSender: TObject; const aItem: T) of object;
IComparer = specialize IutlComparer<T>;
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<TKey, TValue> = class(TObject)
public type
TKeyValuePairEvent = procedure(aSender: TObject; const aKey: TKey; const aValue: TValue) of object;

IComparer = specialize IutlComparer<TKey>;
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;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


+ 9
- 12
uutlObservableGenerics.pas 查看文件

@@ -28,8 +28,7 @@ type
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
generic TutlObservableCustomList<T> = class(specialize TutlCustomList<T>)
public type
TEvent = procedure(aSender: TObject; const aIndex: Integer; const aItem: T) of object;
TEventList = specialize TutlEventList<TEvent>;
TEventList = specialize TutlEventList<TItemEvent>;
private
fOnAddItem: TEventList;
fOnRemoveItem: TEventList;
@@ -55,8 +54,7 @@ type
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
generic TutlObservableCustomHashSet<T> = class(specialize TutlCustomHashSet<T>)
public type
TEvent = procedure(aSender: TObject; const aItem: T) of object;
TEventList = specialize TutlEventList<TEvent>;
TEventList = specialize TutlEventList<THashItemEvent>;
private
fOnAddItem: TEventList;
fOnRemoveItem: TEventList;
@@ -82,8 +80,7 @@ type
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
generic TutlObservableCustomMap<TKey, TValue> = class(specialize TutlMapBase<TKey, TValue>)
public type
TEvent = procedure(aSender: TObject; const aKey: TKey; const aValue: TValue) of object;
TEventList = specialize TutlEventList<TEvent>;
TEventList = specialize TutlEventList<TKeyValuePairEvent>;
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


正在加载...
取消
保存