Delphi:TCountedList
Internally, TCountedList uses two TIntLists. One is used to store the actual integer values whilst the other is used to store a reference count. TCountedList does not have any repeated values. Attempting to add an existing value to the list merely increments the reference count of the existing entry. Attempts to remove entries from the list result in the reference count for the entry being decremented. Actual removal only occurs if the reference count is reduced to zero.
- IntCount: Integer - The number of integers in the list.
- Integers[Index: Integer] :Integer - The array of integer values stored in the list. This is the default property.
- RefCounts[Index: Integer] :Integer - The reference count of the integer value stored at Index.
The principal methods of TCountedList are listed below
- constructor CreateEx - This is the object constructor. Internally, it creates two instances of TIntList, one to store reference counts and the other to store actual integer values.
- function Add( Value: Integer ) :Integer - If Value already exists, its reference count is incremented by unity. Otherwise, Value is added to the list and given a reference count of unity. Returns the number of entries in the list after addition.
- procedure ClearEx( ACount: Integer ) - Empties the list and prepares it to accept ACount new entries.
- function CountFor( Value: Integer ) :Integer - Returns the reference count for Value if it exists in the list. Otherwise, it returns -1.
- function Discard( Value: Integer; AForce: Boolean ) :Boolean - Finds and discards Value from the list if it has a reference count of unity or AForce is True. Returns False and does nothing if Value is not found in the list or has a reference count greater than unity.
- function Remove( Value: Integer ) :Boolean - Removes Value from the list if it exists and has a reference count of unity. Otherwise, returns False and does nothing.
Usage: Create an instance of TCountedList whenever you need to store a sequence of reference counted integers. Use TCountedList methods and properties to manage your entries. Remember to free the instance once you are done.
Download