Skip to content

TSortedCollection

Abstract sorted collection.

Declaration

Source position: objects.pp line 480

type

  TSortedCollection = object(TCollection)
    Duplicates: Boolean;
    constructor Init;
    constructor Load;
    function KeyOf : Pointer;
    function IndexOf : Sw_Integer;
    function Compare : Sw_Integer;
    function Search : Boolean;
    procedure Insert;
    procedure Store;
  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.

Methods

Member Visibility Description
Compare default Compare two items in the collection.
IndexOf default Return index of an item in the collection.
Init default Instantiates a new instance of a TSortedCollection.
Insert default Insert new item in collection.
KeyOf default Return the key of an item.
Load default Instantiates a new instance of a TSortedCollection and loads it from stream.
Search default Search for item with given key.
Store default Write the collection to the stream.
See all Methods

Fieldss

Member Visibility Description
Duplicates default If True duplicate strings are allowed in the collection.
See all Fieldss

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.