参考 HRTR_UTILITIES
- /**
- *@author Vinay Vegunta
- *@date 03/09/2004
- *
- *This class provides a simple Hashtable implementation for storing and looking up objects based on String keys
- */
- class Hashtable
- method Hashtable();
- method Put(&key As string, &value As object);
- method Get(&key As string) Returns object;
- method GetKeys() Returns array of string;
- method IsKey(&key As string) Returns boolean;
- method GetValues() Returns array of object;
- private
- method GetKeyPosition(&key As string) Returns number;
- instance array of string &keyArray;
- instance array of object &valueArray;
- end-class;
- /*
- *Constructor
- */
- method Hashtable
- /*Instantiate the arrays */
- &keyArray = CreateArrayRept("", 0);
- &valueArray = CreateArrayRept( Null, 0);
- end-method;
- /**
- *Put a value in the Hashtable. If the key already exists, it's value will be replaced with a new one .
- */
- method Put
- /+ &key as String, +/
- /+ &value as Object +/
- Local number &nbr_pos = %This.GetKeyPosition(&key);
- /**Check if a new entry is to be made or an old value has to be replaced **/
- If (&nbr_pos <> - 1) Then
- &keyArray [&nbr_pos] = &key;
- &valueArray [&nbr_pos] = &value;
- Else
- &keyArray.Push(&key);
- &valueArray.Push(&value);
- End-If;
- end-method;
- /**
- *Get the value for a given key. Returns null if no value is found .
- */
- method Get
- /+ &key as String +/
- /+ Returns Object +/
- Local number &nbr_pos = %This.GetKeyPosition(&key);
- If (&nbr_pos <> - 1) Then
- Return &valueArray [&nbr_pos];
- End-If;
- Return Null;
- end-method;
- /**
- *Returns the array of keys
- */
- method GetKeys
- /+ Returns Array of String +/
- /**Return a clone of the array so that the original array cannot be tampered with **/
- Return &keyArray.Clone();
- end-method;
- /**
- *Returns the array of values
- */
- method GetValues
- /+ Returns Array of Object +/;
- /**Return a clone of the array so that the original array cannot be tampered with **/
- Return &valueArray.Clone();
- end-method;
- /**
- *Returns a bolean indicating if the provided String is a key or not
- */
- method IsKey
- /+ &key as String +/
- /+ Returns Boolean +/
- Local number &nbr_pos = %This.GetKeyPosition(&key);
- If (&nbr_pos <> - 1) Then
- Return True;
- End-If;
- Return False;
- end-method;
- /**
- *Returns the position in the array of the requested key. Will return -1 if the key is not present .
- */
- method GetKeyPosition
- /+ &key as String +/
- /+ Returns Number +/
- Local number &nbr_Len = &keyArray.Len;
- Local number &i;
- For &i = 1 To &nbr_Len
- If (Exact(&key, &keyArray [&i])) Then
- Return &i;
- End-If;
- End-For;
- Return - 1;
- end-method;