TSortedCollection
[Properties (by Name)][Methods (by Name)][Events (by Name)]
Abstract sorted collection.
Declaration
Source position: objects.pp line 466
Type
TSortedCollection = object (TCollection)
Duplicates : Boolean;
constructor Init(ALimit: Sw_Integer; ADelta: Sw_Integer);
constructor Load(var S: TStream);
function KeyOf(Item: Pointer) : Pointer; Virtual;
function IndexOf(Item: Pointer) : Sw_Integer; Virtual;
function Compare(Key1: Pointer; Key2: Pointer) : Sw_Integer; Virtual;
function Search(Key: Pointer; var Index: Sw_Integer) : Boolean; Virtual;
procedure Insert(Item: Pointer); Virtual;
procedure Store(var S: TStream);
end
;
Description
TSortedCollection is an abstract class, implementing a sorted collection. You should never use an instance of TSortedCollection directly, instead you should declare a descendent type, and override the Compare method.
Because the collection is ordered, TSortedCollection overrides some TCollection methods, to provide faster routines for lookup.
The Compare method decides how elements in the collection should be ordered. Since TCollection has no way of knowing how to order pointers, you must override the compare method.
Additionally, TCollection provides a means to filter out duplicates. if you set Duplicates to False (the default) then duplicates will not be allowed.
The example below defines a descendent of TSortedCollection which is used in the examples.
Members
Member | Type | Visibility | Description |
---|---|---|---|
Compare | Method | default | Compare two items in the collection. |
Duplicates | Field | default | If True duplicate strings are allowed in the collection. |
IndexOf | Method | default | Return index of an item in the collection. |
Init | Method | default | Instantiates a new instance of a TSortedCollection |
Insert | Method | default | Insert new item in collection. |
KeyOf | Method | default | Return the key of an item |
Load | Method | default | Instantiates a new instance of a TSortedCollection and loads it from stream. |
Search | Method | default | Search for item with given key. |
Store | Method | default | Write the collection to the stream. |
Inheritance
Class | Description |
---|---|
TSortedCollection | Abstract sorted collection. |
Example
Unit MySortC;
Interface
Uses Objects;
Type
PMySortedCollection = ^TMySortedCollection;
TMySortedCollection = Object(TSortedCollection)
Function Compare (Key1,Key2 : Pointer): Sw_integer; virtual;
end;
Implementation
Uses MyObject;
Function TMySortedCollection.Compare (Key1,Key2 : Pointer) :sw_integer;
begin
Compare:=PMyobject(Key1)^.GetField - PMyObject(Key2)^.GetField;
end;
end.