[Overview][Constants][Types][Classes][Procedures and functions][Variables] Reference for unit 'Objects' (#rtl)

TSortedCollection

[Properties (by Name)] [Methods (by Name)] [Events (by Name)]

Abstract sorted collection.

Declaration

Source position: objects.pp line 432

type TSortedCollection = object(TCollection)

  Duplicates: Boolean;

  

If Trueduplicate strings are allowed in the collection.

  constructor Init();

  

Instantiates a new instance of a TSortedCollection

  constructor Load();

  

Instantiates a new instance of a TSortedCollectionand loads it from stream.

  function KeyOf(); virtual;

  

Return the key of an item

  function IndexOf(); virtual;

  

Return index of an item in the collection.

  function Compare(); virtual;

  

Compare two items in the collection.

  function Search(); virtual;

  

Search for item with given key.

  procedure Insert(); virtual;

  

Insert new item in collection.

  procedure Store();

  

Write the collection to the stream.

end;

Inheritance

TSortedCollection

  

Abstract sorted collection.

TCollection

  

Manage a collection of pointers of objects

TObject

  

Basis of all objects

Description

TSortedCollectionis an abstract class, implementing a sorted collection. You should never use an instance of TSortedCollectiondirectly, instead you should declare a descendent type, and override the Comparemethod.

Because the collection is ordered, TSortedCollectionoverrides some TCollectionmethods, to provide faster routines for lookup.

The Comparemethod decides how elements in the collection should be ordered. Since TCollectionhas no way of knowing how to order pointers, you must override the compare method.

Additionally, TCollectionprovides a means to filter out duplicates. if you set Duplicatesto False(the default) then duplicates will not be allowed.

The example below defines a descendent of TSortedCollectionwhich is used in the examples.

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.