Skip to content

Commit 0a7d48d

Browse files
Merge remote-tracking branch 'upstream/develop' into develop
2 parents 2a0c0ed + 70c36b5 commit 0a7d48d

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

Source/Base/Collections/Spring.Collections.Dictionaries.pas

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,15 @@ TInverse = class(TCollectionBase<TValueKeyPair>,
237237
end;
238238

239239
TEnumerator = class(TRefCountedObject,
240-
IEnumerator<TKeyValuePair>, IEnumerator<TKey>, IEnumerator<TValue>,
241-
IEnumerator<TValueKeyPair>)
240+
IEnumerator<TKeyValuePair>, IEnumerator<TKey>, IEnumerator<TValue>)
242241
private
243242
{$IFDEF AUTOREFCOUNT}[Unsafe]{$ENDIF}
244243
fSource: TBidiDictionary<TKey, TValue>;
245244
fItemIndex: Integer;
246245
fVersion: Integer;
247246
function GetCurrent: TKeyValuePair;
248-
function GetCurrentInverse: TValueKeyPair;
249247
function GetCurrentKey: TKey;
250248
function GetCurrentValue: TValue;
251-
function IEnumerator<TValueKeyPair>.GetCurrent = GetCurrentInverse;
252249
function IEnumerator<TKey>.GetCurrent = GetCurrentKey;
253250
function IEnumerator<TValue>.GetCurrent = GetCurrentValue;
254251
public
@@ -257,6 +254,11 @@ TEnumerator = class(TRefCountedObject,
257254
function MoveNext: Boolean;
258255
end;
259256

257+
TInverseEnumerator = class(TEnumerator, IEnumerator<TValueKeyPair>)
258+
private
259+
function GetCurrent: TValueKeyPair;
260+
end;
261+
260262
TKeyCollection = class(TEnumerableBase<TKey>,
261263
IEnumerable<TKey>, IReadOnlyCollection<TKey>)
262264
private
@@ -1946,7 +1948,7 @@ function TBidiDictionary<TKey, TValue>.TInverse.GetCount: Integer;
19461948

19471949
function TBidiDictionary<TKey, TValue>.TInverse.GetEnumerator: IEnumerator<TValueKeyPair>;
19481950
begin
1949-
Result := TEnumerator.Create(fSource);
1951+
Result := TInverseEnumerator.Create(fSource);
19501952
end;
19511953

19521954
function TBidiDictionary<TKey, TValue>.TInverse.GetInverse: IBidiDictionary<TKey, TValue>;
@@ -2170,12 +2172,6 @@ function TBidiDictionary<TKey, TValue>.TEnumerator.GetCurrent: TKeyValuePair;
21702172
Result.Value := fSource.fItems[fItemIndex].Value;
21712173
end;
21722174

2173-
function TBidiDictionary<TKey, TValue>.TEnumerator.GetCurrentInverse: TValueKeyPair;
2174-
begin
2175-
Result.Key := fSource.fItems[fItemIndex].Value;
2176-
Result.Value := fSource.fItems[fItemIndex].Key;
2177-
end;
2178-
21792175
function TBidiDictionary<TKey, TValue>.TEnumerator.GetCurrentKey: TKey;
21802176
begin
21812177
Result := fSource.fItems[fItemIndex].Key;
@@ -2194,6 +2190,17 @@ function TBidiDictionary<TKey, TValue>.TEnumerator.MoveNext: Boolean;
21942190
{$ENDREGION}
21952191

21962192

2193+
{$REGION 'TBidiDictionary<TKey, TValue>.TInverseEnumerator' }
2194+
2195+
function TBidiDictionary<TKey, TValue>.TInverseEnumerator.GetCurrent: TValueKeyPair;
2196+
begin
2197+
Result.Key := fSource.fItems[fItemIndex].Value;
2198+
Result.Value := fSource.fItems[fItemIndex].Key;
2199+
end;
2200+
2201+
{$ENDREGION}
2202+
2203+
21972204
{$REGION 'TBidiDictionary<TKey, TValue>.TKeyCollection'}
21982205

21992206
constructor TBidiDictionary<TKey, TValue>.TKeyCollection.Create(

Tests/Source/Base/Spring.Tests.Base.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4261,7 +4261,7 @@ procedure TSortTest.TestSort<T>(const genvalue: Func<T>);
42614261
Generics.Collections.TArray.Sort<T>(data2, comparer);
42624262
for i := 1 to High(data) do
42634263
begin
4264-
Check(comparer.Compare(data[i-1], data[i-1]) <= 0);
4264+
Check(comparer.Compare(data[i-1], data[i]) <= 0);
42654265
Check(comparer.Compare(data[i], data2[i]) = 0);
42664266
end;
42674267
end;

Tests/Source/Base/Spring.Tests.Collections.Dictionaries.pas

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ TTestBidiDictionaryBase = class(TTestDictionaryBase)
173173
TTestBidiDictionary = class(TTestBidiDictionaryBase)
174174
protected
175175
procedure SetUp; override;
176+
published
177+
procedure TestEnumerators;
176178
end;
177179

178180
TTestBidiDictionaryInverse = class(TTestBidiDictionaryBase)
@@ -1360,6 +1362,33 @@ procedure TTestBidiDictionary.SetUp;
13601362
SUTinverse := dict.Inverse;
13611363
end;
13621364

1365+
procedure TTestBidiDictionary.TestEnumerators;
1366+
var
1367+
SUT: IBidiDictionary<string,string>;
1368+
i: Integer;
1369+
p: TPair<string,string>;
1370+
begin
1371+
SUT := TCollections.CreateBidiDictionary<string,string>;
1372+
SUT['Key1'] := 'Val1';
1373+
SUT['Key2'] := 'Val2';
1374+
SUT['Key3'] := 'Val3';
1375+
1376+
i := 0;
1377+
for p in SUT do
1378+
begin
1379+
Inc(i);
1380+
CheckEquals('Key' + IntToStr(i), p.Key);
1381+
CheckEquals('Val' + IntToStr(i), p.Value);
1382+
end;
1383+
i := 0;
1384+
for p in SUT.Inverse do
1385+
begin
1386+
Inc(i);
1387+
CheckEquals('Val' + IntToStr(i), p.Key);
1388+
CheckEquals('Key' + IntToStr(i), p.Value);
1389+
end;
1390+
end;
1391+
13631392
{$ENDREGION}
13641393

13651394

0 commit comments

Comments
 (0)