Menu

[r595]: / trunk / Src / ULists.pas  Maximize  Restore  History

Download this file

841 lines (773 with data), 26.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
{
* ULists.pas
*
* Defines various classes that maintain lists of data of various types.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is ULists.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit ULists;
interface
uses
// Delphi
Contnrs;
type
{
TObjectListEnum:
Enumerator for object lists.
}
TObjectListEnum = class(TObject)
strict private
var
fList: TObjectList; // Reference to object list being enumerated
fIndex: Integer; // Index of current object in enumeration
public
constructor Create(const List: TObjectList);
{Class constructor. Initialises enumeration.
@param List [in] Object to be enurmerated.
}
function GetCurrent: TObject;
{Gets current object in enumeration.
@return Required object.
}
function MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if beyond last item.
}
property Current: TObject read GetCurrent;
{Current object in enumeration}
end;
{
TObjectListEx:
Extended version of TObjectList that adds extra features.
}
TObjectListEx = class(TObjectList)
public
function IsEmpty: Boolean;
{Checks if list is empty.
@return True if empty, False otherwise.
}
function Contains(const Obj: TObject): Boolean;
{Checks if the list contains an object.
@param Obj [in] Object being checked for.
@return True if list contains Obj, False otherwise.
}
function GetEnumerator: TObjectListEnum;
{Gets an enumerator for the list.
@return Required enumerator.
}
end;
{
TIntegerList:
Maintains a list of integers and associated objects.
}
TIntegerList = class(TObject)
strict private
type
{
TListItem:
Wrapper object for items stored in list.
}
TListItem = class(TObject)
strict private
var
fValue: Integer; // Value of Value property
fObj: TObject; // Value of Obj property
public
constructor Create(const Value: Integer; const Obj: TObject = nil);
{Class constructor. Sets up object represent an integer and any
associated object.
}
property Value: Integer read fValue;
{Integer value of list item}
property Obj: TObject read fObj;
{Object associated with Value, or nil if no such object}
end;
type
{
TEnumerator:
List enumerator.
}
TEnumerator = class(TObject)
strict private
var
fIndex: Integer; // Index of current item in enumeration
fList: TIntegerList; // List being eumerated
public
function GetCurrent: Integer;
{Gets current integer in enumeration.
@return Current integer.
}
function MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if enumeration
completed.
}
property Current: Integer read GetCurrent;
{Current item in enumeration}
constructor Create(const List: TIntegerList);
{Class constructor. Sets up enumerator for a list.
@param List [in] List to be enumerated.
}
end;
var
fItems: TObjectListEx; // Stores list items
function GetItem(Idx: Integer): Integer;
{Read accessor for Items[] property.
@param Idx [in] Index of required item.
@return Requested integer.
}
function GetCount: Integer;
{Read accessor for Count property.
@return Number of items in list.
}
function GetObject(Idx: Integer): TObject;
{Read accessor for Objects[] property.
@param Idx [in] Index of required object.
@return Requested object reference.
}
public
constructor Create;
{Class constructor. Sets up object.
}
destructor Destroy; override;
{Class destructor Tears down object.
}
function Add(const Value: Integer; const Obj: TObject = nil): Integer;
{Adds an integer with optional associated object to the list.
@param Value [in] Integer to be added.
@param Obj [in] Object associated with integer or nil.
@return Index of new list item.
}
function IndexOf(const Value: Integer): Integer;
{Finds index of an integer in list.
@param Value [in] Value to be found.
@return Index of Value in list, or -1 if not found.
}
function FindObject(const Value: Integer): TObject;
{Finds object associated with a integer in list.
@param Value [in] Integer value assoicated with object.
@return Required object reference or nil if not found or no object
associated with Value.
}
function GetEnumerator: TEnumerator;
{Gets an enumerator for the list.
@return Enumerator instance.
}
property Items[Idx: Integer]: Integer read GetItem; default;
{Accesses all integers in list by index}
property Objects[Idx: Integer]: TObject read GetObject;
{Accesses all associated objects in list by index}
property Count: Integer read GetCount;
{Number of items in list}
end;
{
TObjectCompare:
Type of comparison method used to order objects in TSortedObjectList
objects.
@param Obj1 [in] First object to be compared.
@param Obj2 [in] Second object to be compared.
@return -ve if Obj1 < Obj2, 0 if Obj1 = Obj2 or +ve if Obj1 > Obj2.
}
TObjectCompare = function(const Obj1, Obj2: TObject): Integer of object;
{
TSortedObjectList:
Class that maintains a sorted list of objects. Caller must provide a
comparison method that determines sort order of objects. Objects must be
unique (i.e. comparison method must not evaluate to zero for two objects in
the list which means that two distinct objects can be determined to be
equal).
}
TSortedObjectList = class(TObject)
strict private
var
fItems: TObjectListEx; // Maintains list of objects
fCompare: TObjectCompare; // Comparison method.
function GetItem(Idx: Integer): TObject;
{Read accessor for Items property.
@param Idx [in] Index of required item.
@return Required object.
}
function GetCount: Integer;
{Read accessor for Count property.
@return Number of objects in list.
}
function GetOwnsObjects: Boolean;
{Read accessor for OwnsObjects property.
@return True list owns list item objects, False if not.
}
strict protected
function Find(const SearchObj: TObject; out Index: Integer): Boolean;
overload;
{Finds an object in list.
@param SearchObj [in] Object to be searched for.
@param Index [out] Index of object in list if found, undefined if not
found.
@return True if object found, False if not.
}
public
constructor Create(const OwnsObjects: Boolean;
const CompareFn: TObjectCompare);
{Class constructor. Sets up object.
@param OwnsObjects [in] Flag indicating if list owns the objects added
to it. When True objects are freed when list is freed. When False
objects are not freed.
@param CompareFn [in] Method used to compare objects in list.
}
destructor Destroy; override;
{Class detructor. Tears down object. Frees object in list if list owns
objects.
}
function Add(const Obj: TObject): Integer;
{Adds an object to the list, maintaining list's sort order.
@param Obj [in] Object to be added to list.
@return Index where object was inserted in list.
@except EBug raised if attempt made to add duplicate to list.
}
function Contains(const Obj: TObject): Boolean;
{Checks if list contains an object.
@param Obj [in] Object to be checked.
@return True if Obj is in the list, False if not.
}
function Find(const SearchObj: TObject): TObject; overload;
{Finds a matching object in list.
@param SearchObj [in] Object to be searched for.
@return Reference to found object or nil if not found. Returned object
may or may not not be the same instance as SearchObj, may be a
different object that compares the same.
}
function IndexOf(const SearchObj: TObject): Integer;
{Gets index of a matching object in list.
@param SearchObj [in] Object to be searched for.
@return Index of matching object in list or -1 if not in list.
}
function GetEnumerator: TObjectListEnum;
{Creates an enumerator for this object.
@return Reference to new enumerator. Caller is repsonsible for freeing
this object.
}
property Items[Idx: Integer]: TObject read GetItem; default;
{Indexed array of objects in list. Maintained in sort order}
property Count: Integer read GetCount;
{Number of objects in list}
property OwnsObjects: Boolean read GetOwnsObjects;
{Flag indicates whether list owns, and frees, objects in list}
end;
{
TSortedObjectDictionary:
Dictionary mapping key objects onto associated value objects. Keys are
maintained in an order determined by compare method passed to constructor.
}
TSortedObjectDictionary = class(TObject)
strict private
var
fKeys: TSortedObjectList; // list of key objects
fValues: TObjectListEx; // list of value objects
function GetKey(Idx: Integer): TObject;
{Read accessor for Keys[] property.
@param Idx [in] Index of required key.
@return Requested key object.
}
function GetValue(Key: TObject): TObject;
{Read accessor for Values[] property. Retrieves value associated with a
key.
@param Key [in] Key associated with required value.
@return Associated value or nil if Key is not found.
}
function GetCount: Integer;
{Read accessor for Count property.
@return Number of key / value pairs in dictionary.
}
public
constructor Create(const KeyCompare: TObjectCompare;
const OwnsKeys: Boolean = False; const OwnsValues: Boolean = False);
{Class constructor. Sets up object.
@param KeyCompare [in] Method used to compare key objects in list.
@param OwnsKeys [in] Whether dictionary owns key objects and frees them
when deleted. If True no object may be used more than once, either as
a key or a value, otherwise an access violation may occur.
@param OwnsValues [in] Whether dictionary owns value objects and frees
them when deleted. If True no object may be used more than once, either
as a key or a value, otherwise an access violation may occur.
}
destructor Destroy; override;
{Class destructor. Tears down object and frees keys and / or values if
required.
}
function Add(const Key, Value: TObject): Integer;
{Adds a key/value pair to the dictionary, maintaining key sort order.
@param Key [in] Object representing key.
@param Value [in] Object value associated with key.
}
function IndexOf(const Key: TObject): Integer;
{Gets index of a key in dictionary.
@param Key [in] Key to locate in dictionary.
@return Index of Key in dictionary or -1 if not found.
}
function KeyExists(const Key: TObject): Boolean;
{Checks whether a key is present in dictionary.
@param Key [in] Key to be checked.
@return True if a matching key is present in dictionary, False if not.
}
function GetEnumerator: TObjectListEnum;
{Gets enumerator for dictionary keys.
@return Required enumerator.
}
property Keys[Idx: Integer]: TObject read GetKey;
{Indexed ordered array of dictionary keys}
property Values[Key: TObject]: TObject read GetValue;
{List of values indexed by keys}
property Count: Integer read GetCount;
{Number of entries in dictionary}
end;
implementation
uses
// Delphi
SysUtils,
// Project
UExceptions;
{ TObjectListEnum }
constructor TObjectListEnum.Create(const List: TObjectList);
{Class constructor. Initialises enumeration.
@param List [in] Object to be enurmerated.
}
begin
inherited Create;
fList := List;
fIndex := -1;
end;
function TObjectListEnum.GetCurrent: TObject;
{Gets current object in enumeration.
@return Required object.
}
begin
Result := fList[fIndex];
end;
function TObjectListEnum.MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if beyond last item.
}
begin
Result := fIndex < Pred(fList.Count);
if Result then
Inc(fIndex);
end;
{ TObjectListEx }
function TObjectListEx.Contains(const Obj: TObject): Boolean;
{Checks if the list contains an object.
@param Obj [in] Object being checked for.
@return True if list contains Obj, False otherwise.
}
begin
Result := IndexOf(Obj) >= 0;
end;
function TObjectListEx.GetEnumerator: TObjectListEnum;
{Gets an enumerator for the list.
@return Required enumerator.
}
begin
Result := TObjectListEnum.Create(Self);
end;
function TObjectListEx.IsEmpty: Boolean;
{Checks if list is empty.
@return True if empty, False otherwise.
}
begin
Result := Count = 0;
end;
{ TIntegerList }
function TIntegerList.Add(const Value: Integer; const Obj: TObject): Integer;
{Adds an integer with optional associated object to the list.
@param Value [in] Integer to be added.
@param Obj [in] Object associated with integer or nil.
@return Index of new list item.
}
begin
Result := fItems.Add(TListItem.Create(Value, Obj));
end;
constructor TIntegerList.Create;
{Class constructor. Sets up object.
}
begin
inherited Create;
fItems := TObjectListEx.Create(True);
end;
destructor TIntegerList.Destroy;
{Class destructor Tears down object.
}
begin
FreeAndNil(fItems); // frees owned TListItem objects
inherited;
end;
function TIntegerList.FindObject(const Value: Integer): TObject;
{Finds object associated with a integer in list.
@param Value [in] Integer value assoicated with object.
@return Required object reference or nil if not found or no object
associated with Value.
}
var
Idx: Integer; // loops thru list items
begin
Idx := IndexOf(Value);
if Idx >= 0 then
Result := Objects[Idx]
else
Result := nil;
end;
function TIntegerList.GetCount: Integer;
{Read accessor for Count property.
@return Number of items in list.
}
begin
Result := fItems.Count;
end;
function TIntegerList.GetEnumerator: TEnumerator;
{Gets an enumerator for the list.
@return Enumerator instance.
}
begin
Result := TEnumerator.Create(Self);
end;
function TIntegerList.GetItem(Idx: Integer): Integer;
{Read accessor for Items[] property.
@param Idx [in] Index of required item.
@return Requested integer.
}
begin
Result := (fItems[Idx] as TListItem).Value;
end;
function TIntegerList.GetObject(Idx: Integer): TObject;
{Read accessor for Objects[] property.
@param Idx [in] Index of required object.
@return Requested object reference.
}
begin
Result := (fItems[Idx] as TListItem).Obj;
end;
function TIntegerList.IndexOf(const Value: Integer): Integer;
{Finds index of an integer in list.
@param Value [in] Value to be found.
@return Index of Value in list, or -1 if not found.
}
var
Idx: Integer; // loops all items in list
begin
Result := -1;
for Idx := 0 to Pred(Count) do
begin
if Items[Idx] = Value then
begin
Result := Idx;
Break;
end;
end;
end;
{ TIntegerList.TListItem }
constructor TIntegerList.TListItem.Create(const Value: Integer;
const Obj: TObject);
{Class constructor. Sets up object represent an integer and any associated
object.
}
begin
inherited Create;
fValue := Value;
fObj := Obj;
end;
{ TIntegerList.TEnumerator }
constructor TIntegerList.TEnumerator.Create(const List: TIntegerList);
{Class constructor. Sets up enumerator for a list.
@param List [in] List to be enumerated.
}
begin
inherited Create;
fList := List;
fIndex := -1;
end;
function TIntegerList.TEnumerator.GetCurrent: Integer;
{Gets current integer in enumeration.
@return Current integer.
}
begin
Result := fList[fIndex];
end;
function TIntegerList.TEnumerator.MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if enumeration completed.
}
begin
Result := fIndex < Pred(fList.Count);
if Result then
Inc(fIndex);
end;
{ TSortedObjectList }
function TSortedObjectList.Add(const Obj: TObject): Integer;
{Adds an object to the list, maintaining list's sort order.
@param Obj [in] Object to be added to list.
@return Index where object was inserted in list.
@except EBug raised if attempt made to add duplicate to list.
}
var
Idx: Integer; // loops thru list
Cmp: Integer; // result of comparison of snippet names
begin
// Assume adding to end of list
Result := Count;
// Loop thru existing items searching for location to insert
for Idx := 0 to Pred(Count) do
begin
// compare new item to current item
Cmp := fCompare(Obj, Items[Idx]);
if Cmp = 0 then
// don't allow duplicates
raise EBug.CreateFmt('%s.Add: Duplicate object', [ClassName]);
if Cmp < 0 then
begin
// current item is greater than new one: insert before it
Result := Idx;
Break;
end;
end;
// add the new item
fItems.Insert(Result, Obj);
end;
function TSortedObjectList.Contains(const Obj: TObject): Boolean;
{Checks if list contains an object.
@param Obj [in] Object to be checked.
@return True if Obj is in the list, False if not.
}
var
Dummy: Integer; // index of object in list: unused
begin
Result := Find(Obj, Dummy);
end;
constructor TSortedObjectList.Create(const OwnsObjects: Boolean;
const CompareFn: TObjectCompare);
{Class constructor. Sets up object.
@param OwnsObjects [in] Flag indicating if list owns the objects added to
it. When True objects are freed when list is freed. When False objects are
not freed.
@param CompareFn [in] Method used to compare objects in list.
}
begin
Assert(Assigned(CompareFn), ClassName + '.Create: CompareFn not assigned');
inherited Create;
fItems := TObjectListEx.Create(OwnsObjects);
fCompare := CompareFn;
end;
destructor TSortedObjectList.Destroy;
{Class detructor. Tears down object. Frees object in list if list owns
objects.
}
begin
FreeAndNil(fItems); // frees contained objects if specified
inherited;
end;
function TSortedObjectList.Find(const SearchObj: TObject): TObject;
{Finds a matching object in list.
@param SearchObj [in] Object to be searched for.
@return Reference to found object or nil if not found. Returned object may
or may not not be the same instance as SearchObj, may be a different
object that compares the same.
}
var
Idx: Integer; // index of match of SearchObj in list if present
begin
if Find(SearchObj, Idx) then
Result := fItems[Idx]
else
Result := nil;
end;
function TSortedObjectList.Find(const SearchObj: TObject;
out Index: Integer): Boolean;
{Finds an object in list.
@param SearchObj [in] Object to be searched for.
@param Index [out] Index of object in list if found, undefined if not found.
@return True if object found, False if not.
}
var
Low, High: Integer; // low and high bounds of search
Cur: Integer; // current item to be tested
Cmp: Integer; // result of comparing two items
begin
// Uses binary search: based on TStringList.Find from Delphi Classes unit
// Initialise
Result := False;
Low := 0;
High := Count - 1;
// Perform binary search
while Low <= High do
begin
// Choose item to be compared: mid point of range
Cur := (Low + High) shr 1;
// Compare chosen
Cmp := fCompare(Items[Cur], SearchObj);
if Cmp < 0 then
// Current item < required: search above current item
Low := Cur + 1
else
begin
// Current item >= required: search below current item
High := Cur - 1;
if Cmp = 0 then
begin
// We found it: set Low to Cur because Cur > High => loop will terminate
Result := True;
Low := Cur;
end;
end;
end;
// Set index (Low = Cur if found)
Index := Low;
end;
function TSortedObjectList.GetCount: Integer;
{Read accessor for Count property.
@return Number of objects in list.
}
begin
Result := fItems.Count;
end;
function TSortedObjectList.GetEnumerator: TObjectListEnum;
{Creates an enumerator for this object.
@return Reference to new enumerator. Caller is repsonsible for freeing
this object.
}
begin
Result := fItems.GetEnumerator;
end;
function TSortedObjectList.GetItem(Idx: Integer): TObject;
{Read accessor for Items property.
@param Idx [in] Index of required item.
@return Required object.
}
begin
Result := fItems[Idx];
end;
function TSortedObjectList.GetOwnsObjects: Boolean;
{Read accessor for OwnsObjects property.
@return True list owns list item objects, False if not.
}
begin
Result := fItems.OwnsObjects;
end;
function TSortedObjectList.IndexOf(const SearchObj: TObject): Integer;
{Gets index of a matching object in list.
@param SearchObj [in] Object to be searched for.
@return Index of matching object in list or -1 if not in list.
}
begin
if not Find(SearchObj, Result) then
Result := -1;
end;
{ TSortedObjectDictionary }
function TSortedObjectDictionary.Add(const Key, Value: TObject): Integer;
{Adds a key/value pair to the dictionary, maintaining key sort order.
@param Key [in] Object representing key.
@param Value [in] Object value associated with key.
}
begin
Result := fKeys.Add(Key);
fValues.Insert(Result, Value);
end;
constructor TSortedObjectDictionary.Create(const KeyCompare: TObjectCompare;
const OwnsKeys, OwnsValues: Boolean);
{Class constructor. Sets up object.
@param KeyCompare [in] Method used to compare key objects in list.
@param OwnsKeys [in] Whether dictionary owns key objects and frees them
when deleted. If True no object may be used more than once, either as
a key or a value, otherwise an access violation may occur.
@param OwnsValues [in] Whether dictionary owns value objects and frees
them when deleted. If True no object may be used more than once, either
as a key or a value, otherwise an access violation may occur.
}
begin
inherited Create;
fKeys := TSortedObjectList.Create(OwnsKeys, KeyCompare);
fValues := TObjectListEx.Create(OwnsValues);
end;
destructor TSortedObjectDictionary.Destroy;
{Class destructor. Tears down object and frees keys and / or values if
required.
}
begin
FreeAndNil(fValues);
FreeAndNil(fKeys);
inherited;
end;
function TSortedObjectDictionary.GetCount: Integer;
{Read accessor for Count property.
@return Number of key / value pairs in dictionary.
}
begin
Result := fKeys.Count;
end;
function TSortedObjectDictionary.GetEnumerator: TObjectListEnum;
{Gets enumerator for dictionary keys.
@return Required enumerator.
}
begin
Result := fKeys.GetEnumerator;
end;
function TSortedObjectDictionary.GetKey(Idx: Integer): TObject;
{Read accessor for Keys[] property.
@param Idx [in] Index of required key.
@return Requested key object.
}
begin
Result := fKeys[Idx];
end;
function TSortedObjectDictionary.GetValue(Key: TObject): TObject;
{Read accessor for Values[] property. Retrieves value associated with a key.
@param Key [in] Key associated with required value.
@return Associated value or nil if Key is not found.
}
var
Index: Integer; // index of Key in list
begin
Index := fKeys.IndexOf(Key);
if Index >= 0 then
Result := fValues[Index]
else
Result := nil;
end;
function TSortedObjectDictionary.IndexOf(const Key: TObject): Integer;
{Gets index of a key in dictionary.
@param Key [in] Key to locate in dictionary.
@return Index of Key in dictionary or -1 if not found.
}
begin
Result := fKeys.IndexOf(Key);
end;
function TSortedObjectDictionary.KeyExists(const Key: TObject): Boolean;
{Checks whether a key is present in dictionary.
@param Key [in] Key to be checked.
@return True if a matching key is present in dictionary, False if not.
}
begin
Result := fKeys.Contains(Key);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.