@@ -43,6 +43,9 @@ interface
43
43
// TDynArray and TSynDictionary, then specialization helps a little more
44
44
{ .$define NOSPECIALIZE}
45
45
46
+ // you could try to define this conditional to generate even less code, which
47
+ // may be slightly slower - perhaps not really noticeable on production
48
+ { .$define SMALLGENERICS}
46
49
47
50
uses
48
51
classes,
@@ -591,6 +594,7 @@ TIKeyValueParent = class(TInterfacedObject)
591
594
function GetValueTypeInfo : PRttiInfo;
592
595
procedure AddOne (key, value : pointer);
593
596
procedure GetDefault (value : pointer);
597
+ procedure GetDefaultAndUnlock (value : pointer);
594
598
function GetCapacity : integer;
595
599
procedure SetCapacity (value : integer);
596
600
function GetTimeOutSeconds : cardinal;
@@ -654,6 +658,7 @@ TIKeyValue<TKey, TValue> = class(
654
658
function TryAdd (const key: TKey; const value : TValue): boolean;
655
659
// / IKeyValue<> method to search a key and return its associated value
656
660
function TryGetValue (const key: TKey; var value : TValue): boolean;
661
+ { $ifndef SMALLGENERICS} inline; { $endif}
657
662
// / IKeyValue<> method to search a key or a supplied default
658
663
function GetValueOrDefault (const key: TKey;
659
664
const defaultValue: TValue): TValue;
@@ -1369,6 +1374,14 @@ procedure TIKeyValueParent.AddOne(key, value: pointer);
1369
1374
end ;
1370
1375
1371
1376
procedure TIKeyValueParent.GetDefault (value : pointer);
1377
+ begin
1378
+ if kvoDefaultIfNotFound in fOptions then
1379
+ fData.Values.ItemClear(value )
1380
+ else
1381
+ raise EIKeyValue.CreateUtf8(' %.GetItem: key not found' , [self]);
1382
+ end ;
1383
+
1384
+ procedure TIKeyValueParent.GetDefaultAndUnlock (value : pointer);
1372
1385
begin
1373
1386
if kvoDefaultIfNotFound in fOptions then
1374
1387
fData.Values.ItemClear(value )
@@ -1437,19 +1450,26 @@ procedure TIKeyValueParent.ReadUnLock;
1437
1450
{ TIKeyValue<TKey, TValue> }
1438
1451
1439
1452
function TIKeyValue <TKey, TValue>.GetItem(const key: TKey): TValue;
1453
+ { $ifdef SMALLGENERICS}
1454
+ begin
1455
+ if not fData.FindAndCopy(key, result, fHasTimeout) then
1456
+ GetDefault(@result)
1457
+ end ;
1458
+ { $else}
1440
1459
var
1441
- ndx: PtrInt; // slightly more verbose but faster than FindAndCopy
1460
+ ndx: PtrInt; // slightly more verbose but faster than plain FindAndCopy
1442
1461
begin
1443
1462
if fHasLock then
1444
1463
fData.Safe^.ReadLock;
1445
1464
ndx := fData.Find(key, fHasTimeout);
1446
1465
if ndx < 0 then
1447
- GetDefault (@result) // may ReadUnLock and raise EIKeyValue
1466
+ GetDefaultAndUnlock (@result) // may ReadUnLock and raise EIKeyValue
1448
1467
else
1449
1468
result := TArray<TValue>(fData.Values.Value ^)[ndx]; // very efficient
1450
1469
if fHasLock then
1451
1470
fData.Safe^.ReadUnLock;
1452
1471
end ;
1472
+ { $endif SMALLGENERICS}
1453
1473
1454
1474
function TIKeyValue <TKey, TValue>.GetKey(ndx: PtrInt): TKey;
1455
1475
begin
@@ -1481,14 +1501,37 @@ function TIKeyValue<TKey, TValue>.TryAdd(const key: TKey;
1481
1501
1482
1502
function TIKeyValue <TKey, TValue>.TryGetValue(const key: TKey;
1483
1503
var value : TValue): boolean;
1504
+ { $ifdef SMALLGENERICS}
1484
1505
begin
1485
1506
result := fData.FindAndCopy(key, value , fHasTimeout);
1486
1507
end ;
1508
+ { $else}
1509
+ var
1510
+ ndx: PtrInt;
1511
+ begin
1512
+ if fHasLock then
1513
+ fData.Safe^.ReadLock;
1514
+ ndx := fData.Find(key, fHasTimeout);
1515
+ if ndx >= 0 then
1516
+ begin
1517
+ value := TArray<TValue>(fData.Values.Value ^)[ndx];
1518
+ result := true;
1519
+ end
1520
+ else
1521
+ result := false;
1522
+ if fHasLock then
1523
+ fData.Safe^.ReadUnLock;
1524
+ end ;
1525
+ { $endif SMALLGENERICS}
1487
1526
1488
1527
function TIKeyValue <TKey, TValue>.GetValueOrDefault(const key: TKey;
1489
1528
const defaultValue: TValue): TValue;
1490
1529
begin
1530
+ { $ifdef SMALLGENERICS}
1491
1531
if not fData.FindAndCopy(key, result, fHasTimeout) then
1532
+ { $else}
1533
+ if not TryGetValue(key, result{ %H-} ) then
1534
+ { $endif SMALLGENERICS}
1492
1535
result := defaultValue;
1493
1536
end ;
1494
1537
0 commit comments