Copyright 1999, Jim O'Flaherty, Jr.



A TList Replacement


Delphi has included a collection class from its initial release, TList.  Unfortunately, the implementation chosen focused on using Pointer as opposed to references.  This creates a problem in attempting to use TList to contain descendents of TObject.  Every time TList is used to submit an instance for later retrieval, a case of Pointer(...) is required.  Retrieval involves casting the returned Pointer into the appropriate class.

I have create TListO as a simple replacement for TList.  My goal was to replicate the behavior of TList.  Most who are familiar with Delphi are familiar with TList.  So, TListO should directly apply.  TListO's implementation is still based directly upon TList.  However, where all the methods previously used Pointer, those references have been replaced with TObject.  So, the goal of TListO was to be a direct replacement for TList.  I have only included two additional methods that seemed to be pretty useful.

I have created a derivation of TListO, TListOOUN.  If offers five significant additional features; Ownership, Uniqueness, No Nil entries, Finding and Sorting.  Finding and sorting both rely on the Compare property to determine how to find and/or sort.  These are features I have found to be quite useful in the past.  So, now to elaborate on these additional features.

Ownership can only be set when an instance of TListOOUN is created.  Since memory reclamation is a very serious matter, enabling the memory managment policy to fluctuate during the life of a TListOOUN instance does not sound very useful.  In fact, it seems pretty dangerous.  When an entry is placed into TListOOUN, if Owns is True, whenever that entry stops being referenced by the TListOOUN instance (for example, when Delete is called for the entry), the entry's .Free method is invoked before the reference is lost.  There is not a set policy on exactly when this occurs.  It is just guaranteed to occur before the TListOOUN instance loses track of the reference.

Uniqueness may be modified at any time.  This feature can be used to ensure the contents of an instance of TListOOUN have no *reference* duplicity.  In other words, if Unique is True, the same instance may be referred to from one and only one entry in the Items property.  Attempting to add a reference to the same instance more than once when Unique is True will result in an exception.  When setting Unique to True, the list of references in Items is checked to ensure there is no existing duplicity before being set.  This operation involves around N^2/2 reference comparisons, so the number of elements already present when Unique is set to True will determine the expense of the operation.  It is recommended you treat Unique the same as Owns.  Set it at creation and leave it that way through the life of the instance.  It makes thinking about the contracts involving the methods easier.

No Nil entries can be modified at any time.  This feature can be used to ensure all the entries in an instance of TListOOUN are not nil.  There are times when it is desirable to ensure the list is unable to have nil entries assigned. It is recommended you treat NoNils the same as Owns.  Set it at creation and leave it that way through the life of the instance.  It makes thinking about the contracts involving the methods easier.

Finding and sorting are related in the fact they both rely upon the Compare property of TListOOUN.  Find, if the list is sorted, will use a binary search to find the matching element, or return the insertion position * -1.  Find, if the list is not sorted, will do a linear search from the beginning of the Items property.  If the Sorted property is True, each relevant operation will ensure the list remains sorted after it is called.

I have included a file named 'ListOMyClass'.  Use this file as a template file.  It is a simple (low effort) way to create a type specific version of either TListO or TListOOUN.  Directions on how to deal with the modification are in the file itself.  Hope this helps.  The file 'MyClass' is included just to allow a test project to compile ensuring the template is class/compile valid.


I hope these assist you in your development.  If you find them useful and you can afford (the time) to, please send a minimum of US $10 to (if check, make payable to Jim O'Flaherty):

Creative Logic, Inc.
Attn: Jim O'Flaherty, Jr.
P.O.Box 851903
Richardson, Tx 75085-1903


Any feedback on ways to make this better would be greatly appreciated.  Please do not hesitate to e-mail me at jim_oflaherty_work@bigfoot.com.  If it is reasonable, I will integrate the feedback I get and then put it in the new release.  If I do not integrate your suggestion and given I can make the time, I will send you a detailed response as to why your suggestion(s) did not make it into the next version.

May this save you as much time as it has already saved me.


Sincerely,

-Jim O'Flaherty, Jr.


PS.  I will continue working on this as I can.  Here is a list of things I would like to add:
       - Create a derivation which allows for Undo/Redo functionality.
       - Create a new version of TListO, TListP, which is derived from TPersistent.  The goal is to facilitate streaming of the structure using the methods on TPersistent.  This would also make it easier to use by other derivations of TPersistent.


