Skip to content

Commit 9da9cc3

Browse files
committed
improved and simplified count based operations
1 parent fdf26aa commit 9da9cc3

11 files changed

+500
-158
lines changed

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ TEnumerableBase = class abstract(TRefCountedObject)
8989
this: Pointer;
9090
{$REGION 'Property Accessors'}
9191
function GetCount: Integer;
92-
function GetIsEmpty: Boolean;
9392
function GetCountFast: Integer;
93+
function GetIsEmpty: Boolean;
9494
{$ENDREGION}
9595
function IsCountInRange(min, max, limit: Integer): Boolean;
9696
public
@@ -217,9 +217,9 @@ TEnumerator = class(TRefCountedObject, IEnumerator)
217217
fSource: IEnumerable;
218218
fGetCurrent: TGetCurrentFunc;
219219
function GetCount: Integer;
220+
function GetCountFast: Integer;
220221
function GetElementType: PTypeInfo;
221222
function GetIsEmpty: Boolean;
222-
function GetCountFast: Integer;
223223
protected
224224
function QueryInterface(const IID: TGUID; out obj): HResult; stdcall;
225225
public
@@ -376,7 +376,7 @@ TArrayIterator<T> = class(TIteratorBase<T>, IInterface,
376376
private
377377
{$REGION 'Property Accessors'}
378378
function GetCount: Integer;
379-
function GetIsEmpty: Boolean;
379+
function GetCountFast: Integer;
380380
function GetItem(index: Integer): T;
381381
{$ENDREGION}
382382
public
@@ -526,7 +526,7 @@ TEnumerator = record
526526
fOffset: Integer;
527527
{$REGION 'Property Accessors'}
528528
function GetCount: Integer;
529-
function GetIsEmpty: Boolean;
529+
function GetCountFast: Integer;
530530
{$ENDREGION}
531531
protected
532532
function GetElementType: PTypeInfo; override;
@@ -581,7 +581,7 @@ TEnumerator = record
581581
{$REGION 'Property Accessors'}
582582
function GetCapacity: Integer; inline;
583583
function GetCount: Integer; inline;
584-
function GetIsEmpty: Boolean;
584+
function GetCountFast: Integer;
585585
function GetOnChanged: ICollectionChangedEvent<T>;
586586
function GetOwnsObjects: Boolean; inline;
587587
procedure SetCapacity(value: Integer);
@@ -898,7 +898,7 @@ class function TEnumerableBase.NewInstance: TObject;
898898

899899
function TEnumerableBase.Any: Boolean;
900900
begin
901-
Result := not IEnumerable(this).IsEmpty;
901+
Result := IsCountInRange(1, MaxInt, 1);
902902
end;
903903

904904
function TEnumerableBase.AtLeast(count: Integer): Boolean;
@@ -955,11 +955,8 @@ function TEnumerableBase.GetCount: Integer;
955955
end;
956956

957957
function TEnumerableBase.GetIsEmpty: Boolean;
958-
var
959-
enumerator: IEnumerator;
960958
begin
961-
enumerator := IEnumerable(this).GetEnumerator;
962-
Result := not enumerator.MoveNext;
959+
Result := IsCountInRange(0, 0, 1);
963960
end;
964961

965962
function TEnumerableBase.IsCountInRange(min, max, limit: Integer): Boolean;
@@ -986,7 +983,7 @@ function TEnumerableBase.IsCountInRange(min, max, limit: Integer): Boolean;
986983
function TEnumerableBase.GetCountFast: Integer;
987984
begin
988985
// implementing IReadOnlyCollection is an indicator for having its own count
989-
if GetInterfaceEntry(IReadOnlyCollection<Integer>) <> nil then
986+
if GetInterfaceEntry(IReadOnlyCollectionOfTGuid) <> nil then
990987
Result := IEnumerable(this).Count
991988
else
992989
Result := -1;
@@ -2180,6 +2177,11 @@ function TInnerCollection<T>.GetCount: Integer;
21802177
Result := fHashTable.Count;
21812178
end;
21822179

2180+
function TInnerCollection<T>.GetCountFast: Integer;
2181+
begin
2182+
Result := fHashTable.Count;
2183+
end;
2184+
21832185
function TInnerCollection<T>.GetElementType: PTypeInfo;
21842186
begin
21852187
Result := fElementType;
@@ -2197,11 +2199,6 @@ function TInnerCollection<T>.GetEnumerator: IEnumerator<T>;
21972199
end;
21982200
end;
21992201

2200-
function TInnerCollection<T>.GetIsEmpty: Boolean;
2201-
begin
2202-
Result := fHashTable.Count = 0;
2203-
end;
2204-
22052202
function TInnerCollection<T>.ToArray: TArray<T>;
22062203
var
22072204
hashTable: PHashTable;
@@ -2329,6 +2326,11 @@ function TCircularArrayBuffer<T>.GetCount: Integer;
23292326
Result := fCount and CountMask;
23302327
end;
23312328

2329+
function TCircularArrayBuffer<T>.GetCountFast: Integer;
2330+
begin
2331+
Result := fCount and CountMask;
2332+
end;
2333+
23322334
function TCircularArrayBuffer<T>.GetEnumerator: IEnumerator<T>;
23332335
begin
23342336
_AddRef;
@@ -2341,11 +2343,6 @@ function TCircularArrayBuffer<T>.GetEnumerator: IEnumerator<T>;
23412343
end;
23422344
end;
23432345

2344-
function TCircularArrayBuffer<T>.GetIsEmpty: Boolean;
2345-
begin
2346-
Result := Count = 0;
2347-
end;
2348-
23492346
function TCircularArrayBuffer<T>.GetOnChanged: ICollectionChangedEvent<T>;
23502347
begin
23512348
Result := fOnChanged;
@@ -3200,7 +3197,7 @@ function TIteratorBase.GetCountFast: Integer;
32003197
{$IFNDEF OVERFLOWCHECKS_ON}{$Q-}{$ENDIF}
32013198
end;
32023199
end;
3203-
TIteratorKind.Ordered, TIteratorKind.Reversed:
3200+
TIteratorKind.Ordered, TIteratorKind.Reversed, TIteratorKind.Shuffled:
32043201
Result := fSource.GetCountFast;
32053202
TIteratorKind.Partition:
32063203
begin
@@ -3542,9 +3539,9 @@ function TArrayIterator<T>.GetCount: Integer;
35423539
Result := fCount;
35433540
end;
35443541

3545-
function TArrayIterator<T>.GetIsEmpty: Boolean;
3542+
function TArrayIterator<T>.GetCountFast: Integer;
35463543
begin
3547-
Result := fItems = nil;
3544+
Result := fCount;
35483545
end;
35493546

35503547
function TArrayIterator<T>.GetItem(index: Integer): T;

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

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ TEnumerator = record
8888
{$REGION 'Property Accessors'}
8989
function GetCapacity: Integer; inline;
9090
function GetCount: Integer;
91-
function GetIsEmpty: Boolean;
91+
function GetCountFast: Integer;
9292
function GetItem(const key: TKey): TValue;
9393
function GetKeys: IReadOnlyCollection<TKey>;
9494
function GetValues: IReadOnlyCollection<TValue>;
@@ -177,8 +177,8 @@ TInverse = class(TCollectionBase<TValueKeyPair>,
177177
{$REGION 'Property Accessors'}
178178
function GetCapacity: Integer;
179179
function GetCount: Integer;
180+
function GetCountFast: Integer;
180181
function GetInverse: IBidiDictionary<TKey, TValue>;
181-
function GetIsEmpty: Boolean;
182182
function GetItem(const value: TValue): TKey;
183183
function GetKeys: IReadOnlyCollection<TValue>;
184184
function GetKeyType: PTypeInfo;
@@ -269,7 +269,7 @@ TKeyCollection = class(TEnumerableBase<TKey>,
269269
fSource: TBidiDictionary<TKey, TValue>;
270270
{$REGION 'Property Accessors'}
271271
function GetCount: Integer;
272-
function GetIsEmpty: Boolean;
272+
function GetCountFast: Integer;
273273
{$ENDREGION}
274274
public
275275
constructor Create(const source: TBidiDictionary<TKey, TValue>);
@@ -293,7 +293,7 @@ TValueCollection = class(TEnumerableBase<TValue>,
293293
fSource: TBidiDictionary<TKey, TValue>;
294294
{$REGION 'Property Accessors'}
295295
function GetCount: Integer;
296-
function GetIsEmpty: Boolean;
296+
function GetCountFast: Integer;
297297
{$ENDREGION}
298298
public
299299
constructor Create(const source: TBidiDictionary<TKey, TValue>);
@@ -329,8 +329,8 @@ TValueCollection = class(TEnumerableBase<TValue>,
329329
{$REGION 'Property Accessors'}
330330
function GetCapacity: Integer; inline;
331331
function GetCount: Integer;
332+
function GetCountFast: Integer;
332333
function GetInverse: IBidiDictionary<TValue, TKey>;
333-
function GetIsEmpty: Boolean;
334334
function GetItem(const key: TKey): TValue;
335335
function GetKeys: IReadOnlyCollection<TKey>;
336336
function GetValues: IReadOnlyCollection<TValue>;
@@ -444,7 +444,7 @@ TKeyCollection = class(TEnumerableBase<TKey>,
444444
fSource: TSortedDictionary<TKey, TValue>;
445445
{$REGION 'Property Accessors'}
446446
function GetCount: Integer;
447-
function GetIsEmpty: Boolean;
447+
function GetCountFast: Integer;
448448
{$ENDREGION}
449449
public
450450
constructor Create(const source: TSortedDictionary<TKey, TValue>);
@@ -467,7 +467,7 @@ TValueCollection = class(TEnumerableBase<TValue>,
467467
fSource: TSortedDictionary<TKey, TValue>;
468468
{$REGION 'Property Accessors'}
469469
function GetCount: Integer;
470-
function GetIsEmpty: Boolean;
470+
function GetCountFast: Integer;
471471
{$ENDREGION}
472472
public
473473
constructor Create(const source: TSortedDictionary<TKey, TValue>);
@@ -494,7 +494,7 @@ TValueCollection = class(TEnumerableBase<TValue>,
494494
{$REGION 'Property Accessors'}
495495
function GetCapacity: Integer;
496496
function GetCount: Integer;
497-
function GetIsEmpty: Boolean;
497+
function GetCountFast: Integer;
498498
function GetItem(const key: TKey): TValue;
499499
function GetKeys: IReadOnlyCollection<TKey>;
500500
function GetValues: IReadOnlyCollection<TValue>;
@@ -768,9 +768,9 @@ function TDictionary<TKey, TValue>.GetCount: Integer;
768768
Result := fHashTable.Count;
769769
end;
770770

771-
function TDictionary<TKey, TValue>.GetIsEmpty: Boolean;
771+
function TDictionary<TKey, TValue>.GetCountFast: Integer;
772772
begin
773-
Result := fHashTable.Count = 0;
773+
Result := fHashTable.Count;
774774
end;
775775

776776
procedure TDictionary<TKey, TValue>.Add(const key: TKey; const value: TValue);
@@ -1588,6 +1588,11 @@ function TBidiDictionary<TKey, TValue>.GetCount: Integer;
15881588
Result := fCount;
15891589
end;
15901590

1591+
function TBidiDictionary<TKey, TValue>.GetCountFast: Integer;
1592+
begin
1593+
Result := fCount;
1594+
end;
1595+
15911596
procedure TBidiDictionary<TKey, TValue>.AddOrSetKey(const value: TValue; const key: TKey);
15921597
var
15931598
keyHashCode, keyBucketIndex, valueHashCode, valueBucketIndex, keyItemIndex, valueItemIndex: Integer;
@@ -1797,11 +1802,6 @@ function TBidiDictionary<TKey, TValue>.GetInverse: IBidiDictionary<TValue, TKey>
17971802
Result := fInverse;
17981803
end;
17991804

1800-
function TBidiDictionary<TKey, TValue>.GetIsEmpty: Boolean;
1801-
begin
1802-
Result := fCount = 0;
1803-
end;
1804-
18051805
function TBidiDictionary<TKey, TValue>.GetKeys: IReadOnlyCollection<TKey>;
18061806
begin
18071807
Result := fKeys;
@@ -1973,6 +1973,11 @@ function TBidiDictionary<TKey, TValue>.TInverse.GetCount: Integer;
19731973
Result := fSource.fCount;
19741974
end;
19751975

1976+
function TBidiDictionary<TKey, TValue>.TInverse.GetCountFast: Integer;
1977+
begin
1978+
Result := fSource.fCount;
1979+
end;
1980+
19761981
function TBidiDictionary<TKey, TValue>.TInverse.GetEnumerator: IEnumerator<TValueKeyPair>;
19771982
begin
19781983
Result := TInverseEnumerator.Create(fSource);
@@ -1983,13 +1988,7 @@ function TBidiDictionary<TKey, TValue>.TInverse.GetInverse: IBidiDictionary<TKey
19831988
Result := fSource;
19841989
end;
19851990

1986-
function TBidiDictionary<TKey, TValue>.TInverse.GetIsEmpty: Boolean;
1987-
begin
1988-
Result := fSource.fCount = 0;
1989-
end;
1990-
1991-
function TBidiDictionary<TKey, TValue>.TInverse.GetItem(
1992-
const value: TValue): TKey;
1991+
function TBidiDictionary<TKey, TValue>.TInverse.GetItem(const value: TValue): TKey;
19931992
var
19941993
valueBucketIndex, valueItemIndex: Integer;
19951994
begin
@@ -2266,14 +2265,14 @@ function TBidiDictionary<TKey, TValue>.TKeyCollection.GetCount: Integer;
22662265
Result := fSource.fCount;
22672266
end;
22682267

2269-
function TBidiDictionary<TKey, TValue>.TKeyCollection.GetEnumerator: IEnumerator<TKey>;
2268+
function TBidiDictionary<TKey, TValue>.TKeyCollection.GetCountFast: Integer;
22702269
begin
2271-
Result := TEnumerator.Create(fSource);
2270+
Result := fSource.fCount;
22722271
end;
22732272

2274-
function TBidiDictionary<TKey, TValue>.TKeyCollection.GetIsEmpty: Boolean;
2273+
function TBidiDictionary<TKey, TValue>.TKeyCollection.GetEnumerator: IEnumerator<TKey>;
22752274
begin
2276-
Result := fSource.fCount = 0;
2275+
Result := TEnumerator.Create(fSource);
22772276
end;
22782277

22792278
function TBidiDictionary<TKey, TValue>.TKeyCollection.ToArray: TArray<TKey>;
@@ -2332,14 +2331,14 @@ function TBidiDictionary<TKey, TValue>.TValueCollection.GetCount: Integer;
23322331
Result := fSource.fCount;
23332332
end;
23342333

2335-
function TBidiDictionary<TKey, TValue>.TValueCollection.GetEnumerator: IEnumerator<TValue>;
2334+
function TBidiDictionary<TKey, TValue>.TValueCollection.GetCountFast: Integer;
23362335
begin
2337-
Result := TEnumerator.Create(fSource);
2336+
Result := fSource.fCount;
23382337
end;
23392338

2340-
function TBidiDictionary<TKey, TValue>.TValueCollection.GetIsEmpty: Boolean;
2339+
function TBidiDictionary<TKey, TValue>.TValueCollection.GetEnumerator: IEnumerator<TValue>;
23412340
begin
2342-
Result := fSource.fCount = 0;
2341+
Result := TEnumerator.Create(fSource);
23432342
end;
23442343

23452344
function TBidiDictionary<TKey, TValue>.TValueCollection.ToArray: TArray<TValue>;
@@ -2565,14 +2564,14 @@ function TSortedDictionary<TKey, TValue>.GetCount: Integer;
25652564
Result := fTree.Count;
25662565
end;
25672566

2568-
function TSortedDictionary<TKey, TValue>.GetEnumerator: IEnumerator<TKeyValuePair>;
2567+
function TSortedDictionary<TKey, TValue>.GetCountFast: Integer;
25692568
begin
2570-
Result := TEnumerator.Create(Self);
2569+
Result := fTree.Count;
25712570
end;
25722571

2573-
function TSortedDictionary<TKey, TValue>.GetIsEmpty: Boolean;
2572+
function TSortedDictionary<TKey, TValue>.GetEnumerator: IEnumerator<TKeyValuePair>;
25742573
begin
2575-
Result := fTree.Count = 0;
2574+
Result := TEnumerator.Create(Self);
25762575
end;
25772576

25782577
function TSortedDictionary<TKey, TValue>.GetItem(const key: TKey): TValue;
@@ -2825,14 +2824,14 @@ function TSortedDictionary<TKey, TValue>.TKeyCollection.GetCount: Integer;
28252824
Result := fSource.fTree.Count;
28262825
end;
28272826

2828-
function TSortedDictionary<TKey, TValue>.TKeyCollection.GetEnumerator: IEnumerator<TKey>;
2827+
function TSortedDictionary<TKey, TValue>.TKeyCollection.GetCountFast: Integer;
28292828
begin
2830-
Result := TEnumerator.Create(fSource);
2829+
Result := fSource.fTree.Count;
28312830
end;
28322831

2833-
function TSortedDictionary<TKey, TValue>.TKeyCollection.GetIsEmpty: Boolean;
2832+
function TSortedDictionary<TKey, TValue>.TKeyCollection.GetEnumerator: IEnumerator<TKey>;
28342833
begin
2835-
Result := fSource.fTree.Count = 0;
2834+
Result := TEnumerator.Create(fSource);
28362835
end;
28372836

28382837
function TSortedDictionary<TKey, TValue>.TKeyCollection.ToArray: TArray<TKey>;
@@ -2883,14 +2882,14 @@ function TSortedDictionary<TKey, TValue>.TValueCollection.GetCount: Integer;
28832882
Result := fSource.fTree.Count;
28842883
end;
28852884

2886-
function TSortedDictionary<TKey, TValue>.TValueCollection.GetEnumerator: IEnumerator<TValue>;
2885+
function TSortedDictionary<TKey, TValue>.TValueCollection.GetCountFast: Integer;
28872886
begin
2888-
Result := TEnumerator.Create(fSource);
2887+
Result := fSource.fTree.Count;
28892888
end;
28902889

2891-
function TSortedDictionary<TKey, TValue>.TValueCollection.GetIsEmpty: Boolean;
2890+
function TSortedDictionary<TKey, TValue>.TValueCollection.GetEnumerator: IEnumerator<TValue>;
28922891
begin
2893-
Result := fSource.fTree.Count = 0;
2892+
Result := TEnumerator.Create(fSource);
28942893
end;
28952894

28962895
function TSortedDictionary<TKey, TValue>.TValueCollection.ToArray: TArray<TValue>;

0 commit comments

Comments
 (0)