Menu

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

Download this file

486 lines (445 with data), 16.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
{
* 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-2010 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit ULists;
interface
uses
// Delphi
Generics.Collections;
type
{
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: TObjectList<TObject>; // 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);
{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;
{Destructor. 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: TEnumerator<TObject>;
{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: TObjectList<TObject>; // 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);
{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;
{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: TEnumerator<TObject>;
{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
// Project
UExceptions;
{ 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);
{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 := TObjectList<TObject>.Create(OwnsObjects);
fCompare := CompareFn;
end;
destructor TSortedObjectList.Destroy;
{Destructor. Tears down object. Frees object in list if list owns objects.
}
begin
fItems.Free; // 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: TEnumerator<TObject>;
{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);
{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 := TObjectList<TObject>.Create(OwnsValues);
end;
destructor TSortedObjectDictionary.Destroy;
{Destructor. Tears down object and frees keys and / or values if required.
}
begin
fValues.Free;
fKeys.Free;
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: TEnumerator<TObject>;
{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.