Sign in Join
FPDoc Onlineexperiment

TDynHashArray class

In unit DynHashArray

Name of the class that store all functions and data for an hashed array.

Declaration

pascal
TDynHashArray = class

Description

TDynHashArray is a class which is similar to TList which allows both pointers and objects to be maintained in its internal storage. It includes common methods like Add, Remove, Contains, First, Count and Clear. Because of its hashing nature, the add, remove, and find operations are done in constant time on average.

Inner structure:

There are three parts:

  1. The array itself (FItems). Every entry is a pointer to the first TDynHashArrayItem of a list with the same hash index. The first item of every same index list is the list beginning and its IsOverflow flag is set to false. All other items are overflow items. To get all items with the same hash index, do a FindHashItem. Then search through all "Next" items until Next is nil or its IsOverflow flag is set to false.
  2. The items beginning with FFirstItem is a 2-way-connected list of TDynHashArrayItem. This list contains all used items.
  3. To reduce GetMem/FreeMem calls, free items are cached.

Issues:

The maximum capacity is the PrimeNumber. You can store more items, but the performance decreases. The best idea is to provide your own hash function.

Important: Items in the TDynHashArray must not change their key. When changing the key of an item, remove it and add it after the change.

Properties

CountpublicNumber of elements stored in the set/array.
FirstHashItempublicPointer which provides access to the first TDynHashArrayItem stored in the hash array.
MinCapacitypublicThe minimum number of items that will be allocated for an array.
MaxCapacitypublicThe size of the array cannot be larger than this number.
CapacitypublicSpace is allocated for this number of items.
CustomHashFunctionpublicThe pointer to the user-defined hash function.
OwnerHashFunctionpublicThe pointer to the user-defined hash function.
OptionspublicSet of TDynHashArrayOption values enabled for the class instance.

Methods

RebuildItemsprotectedRebuilds the internal data structures.
SaveCacheItemprotectedSets the cache.
CreatepublicConstructor for the class instance.
DestroypublicDestructor for the class instance.
AddpublicAdds an item to the set/array.
ContainspublicReturns True if the item is in the set.
ContainsKeypublicReturns True if the item is in the set.(through OnGetKeyForHashItem).
RemovepublicRemoves an element from the set/hash array.
ClearpublicRemoves all elements from the set/hash array.
ClearCachepublicClears the FContainsCache cache.
FirstpublicReturns the first item or nil.
IndexOfpublicReturns calculated index from Item through OnGetKeyForHashItem.
IndexOfKeypublicReturns calculated index from Item.
FindHashItempublicFinds an item as PDynHashArrayItem among all items.
FindHashItemWithKeypublicFinds an item as PDynHashArrayItem among all items (through OnGetKeyForHashItem).
FindItemWithKeypublicFinds an item among all items (through OnGetKeyForHashItem).
GetHashItempublicGets a link list from the "main" array FItems by index.
DeletepublicDeletes a PDynHashArrayItem from link list.
AssignTopublicCopies all items into a given TList.
ForEachpublicCalls the specified function for each of the items in the hash array.
SlowAlternativeHashMethodpublicAnother hash function that can be used.
ConsistencyCheckpublicCheck if data in TDynHashArray are OK.
WriteDebugReportpublicPrints out essential data to aid debugging TDynHashArray.

Events

OnGetKeyForHashItempublicUser-defined function to obtain a key from an item.

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.