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 (614) method.
Because the collection is ordered, TSortedCollection overrides some TCollection methods, to provide faster routines for lookup.
The Compare (614) 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.
Here is the complete declaration of TSortedCollection
TYPE TSortedCollection = OBJECT (TCollection) Duplicates: Boolean; { Duplicates flag } Constructor Init (ALimit, ADelta: Sw_Integer); Constructor Load (Var S: TStream); Function KeyOf (Item: Pointer): Pointer; Virtual; Function IndexOf (Item: Pointer): Sw_Integer; Virtual; Function Compare (Key1, 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; PSortedCollection = ^TSortedCollection; |
In the subsequent examples, the following descendent of TSortedCollection is used:
Listing: objectex/mysortc.pp
You should not call this method directly, since TSortedCollection is a abstract class. Instead, the descendent classes should call it via the inherited keyword.
For an example, see
You should not call this method directly, since TSortedCollection is a abstract class. Instead, the descendent classes should call it via the inherited keyword.
For an example, see TCollection.Load (594).
Keys are used to sort the objects, they are used to search and sort the items in the collection. If descendent types override this method then it allows possibly for faster search/sort methods based on keys rather than on the objects themselves.
In case Item is not found in the collection, -1 is returned.
For an example, see TCollection.IndexOf (596)
The function should compare the two keys of items and return the following function results:
Listing: objectex/mysortc.pp
Instead of a linear search as TCollection does, TSortedCollection uses a binary search based on the keys of the objects. It uses the Compare (614) function to implement this search.
If the item is found, Search returns True, otherwise False is returned.
Listing: objectex/ex36.pp
If Item is already present in the collection, and Duplicates is False, the item will not be inserted.
Listing: objectex/ex35.pp
After a Store, the collection can be loaded from the stream with the constructor Load (613)
For an example, see TCollection.Load (594).