Comparable.pas contains TComparable and some of its descendants. These classes add hashcode and compareTo functions to TObject. The included descendants of TComparable are classes for storage of primitive data types, such as strings, integers and floats.
Please note - the class definitions in this section list only public and published fields, methods and properties.
- ECompareException
- The exception that is raised when objects cannot be compared.
tComparable = class(tObject)
protected
function compareObjects(object2 : tComparable) : integer; virtual; abstract;
public
function hashCode : integer; virtual; abstract;
function compareTo(object2 : tComparable) : integer;
end;
- TComparable, as the name implies, is an object that can be compared.
It is a TObject with a hashCode and a compareTo function. Since both the hash code and the comparisons
have to be defined depending on what is stored in TComparable,
it is declared as an abstract type. Treat it as a Java language Interface.
It's a good idea to derive all objects which will be used with this collection from tComparable because of the additinonal functionality it provides.
For example, you can only search for a value stored in hashTable if it's a TComparable.
HashTable keys must be TComparable.
function compareObjects(object2 : tComparable) : integer is the function that does the actual comparison of objects.
It is called by compareTo so it does not need to be made public. compareTo checks to see if object2 is nil and returns -1 if that's the case.
Otherwise it passes control to compareObjects, which should return a value
< 0 if object2 is less than current object
> 0 if object2 is greater than current object
= 0 if objects are equal
function hashCode : integer returns the hash code of the object. The only requirement to hash code is that equal objects produce equal hash codes.
The reverse of this is not a requirement (equal hash codes to not imply equal objects), but it would be very good if it were. HashCode function should try return different values for different objects or at least provide a good distribution of values.
function compareTo(object2 : tComparable) : integer, as mentioned before, compares object2 to nil and returns -1 or calls compareObjects. The returns follow the same logic as in compareObjects
tNumeric = class(tComparable)
protected
function compareObjects(object2 : tComparable) : integer; override;
public
function asDouble : double; virtual; abstract;
end;
-
This is the base class for numeric objects. These objects do not have to be derived from TNumeric but they should if they ever need to be compared across classes, ie tInteger vs tFloat
function compareObjects(object2 : tComparable) : integer compares object values based on the return of their asDouble function
function asDouble : doublereturns the value of the object as Double. It would not work for any value that is larger than Double or has higher precision, but it will work for most numeric types.
tInteger = class(tNumeric)
property value : integer read ... write ...
function hashCode : integer
constructor create(val : integer)
-
TInteger is a class containing one integer value. It is a child of tNumeric
property value allows read / write access to the contents of the object
hashCode returns the value if it's below 0 and value + 1 if it's above or equal to 0.
tDouble = class(tNumeric)
protected
procedure setValue(value : double)
public
property value : double read ... write ...
function hashCode : integer
constructor create(val : double)
-
TDouble is a class containing double values. It is a child of TNumeric.
property value allows read and write access to the value of the object
function hashCode returns the hash code. The hash code is generated at assignment time by setValue or by the constructor. It multiplies / divides the value by 10 until it's as close as possible to maxInt. This allows for fairly decent distribution of hash codes in all ranges of values.
tString = class(tComparable)
property value : string read ... write ...;
function hashCode : integer
constructor create(val : string)
-
TString is a class encapsulating a string. It's a case-sensitive class. Value comparisons are done with fiunction compareStr
property value provides read / write access to the contents of the object
function hashCode returns the hash code, which is set at assign-time, similar to tDouble. The code which calculates the hash code is lifted from Java String class.
tStringNoCase = class(tString)
-
TStringNoCase is a case-insensitive version of TString. It uses compareText to compare values.
function equal(obj1, obj2 : tComparable) : boolean
- returns true if the objects are equal or if they are both nil
function compare(Item1, Item2 : tComparable) : integer
- returns a value < 0 if item1 < item2, 0 if item1 = item2 or both item1 and item2 are nil, and > 0 if item1 > item2