mrhash__define.pro
mrHash
Methods:
Constructor
mrHash::init : constructor, 2nd part
Public methods
mrHash::set : key, value
mrHash::get : key, returns value
mrHash::exists : key, returns 0 or 1
mrHash::delete : key, remove key from array
mrHash::count : returns number of entries in the hash
mrHash::keys : returns string array containing all keys
mrHash::load : load a hash array from a file
mrHash::save : write a hash array to file
Private functions
mrHash__define : constructor, 1st part
mrHash::cleanup : the destructor
Description:
Implementation of an associative array capable of storing int, real
(=double), string or object references.
Although this is only a quick hack (linear search via where), it is
reasonable fast for a couple of thousand entries and in that range
compares well with the more sophisticated implementation available
on the RSINC contribution pages.
If hashes get considerably bigger than that, IDL might anyway not
be the language of choice.
The written file format is key=value, i.e. keys may not contain the
'=' character.
Example code:
phone = obj_new('mrhash', 'string')
phone->set, 'Kay', '+49 123 123456'
phone->set, 'Meike', '+46 987 9823'
print, "Kay's phone number: ", phone->get('Kay')
mrHash::init
Syntax:
obj = obj_new('mrhash', type, blocksize=blocksize)
Arguments:
type
String denoting the type of the parameter. One of 'int', 'real',
'string' or 'object'
blocksize
Data blocks are allocated in chunks of size blocksize. Default is 100.
Description:
Instanciate a mrHash object with initally blocksize data elements.
mrHash::cleanup
Syntax:
obj->cleanup, /destroy
Arguments:
destroy
If the array stored objects and destroy is set to '1', the
objects are destroyed. Default is '0'.
Description:
The destructor
mrHash::set
Syntax:
obj->set, key, value
Arguments:
key
String with the key - if empty, the method returns immediately
value
A value of the correct type
Description:
Set a key-value pair
mrHash::get
Syntax:
value = obj->get(key, defined)
Arguments:
key
key string
defined
If present, set to '1' if the key existed or to '0' otherwise.
Description:
Retrieve a value from the array. If the key does not exist,
0, 0., '', or obj_new() are returned, depending on the array type.
mrHash::delete
Syntax:
obj->delete, key
Arguments:
key
key string
Description:
Remove the entry from the array.
mrHash::keys
Syntax:
keys = obj->keys( count=count )
Return value:
A string array containing all currently known keys - the array
has no specific order. If the optional parameter count is set, the
number of elements in the array is returned.
mrHash::exists
Syntax:
res = obj->exists(key)
Arguments:
key
String holding the key
Description:
Returns '1', if the array element exists, '0' otherwise
mrHash::count
Syntax:
nentries = obj->count()
Description:
Returns the number of entries in the hash
mrHash::load
Syntax:
res = obj->load( filename, count=count, /compress )
Arguments:
filename
The file to load
count
If set, contains the number of loaded key-value pairs.
compress
Needs to be set if loading a compressed file.
Return value:
0 in case of failure, 1 otherwise
Description:
Loads a hashtable from disk. The file format is
key=value
keys thus may not contain the '=' character
Lines containing no '=' sign or starting with an '=' sign are invalid.
They are ignored and can thus be used as comment lines
If a value should not be of the specified type, the routine returns
with an error.
mrHash::save
Syntax:
res = obj->save( filename, /compress )
Arguments:
filename
The file to save to
compress
If set, compress the file via IDL's buildin compress option
Description:
Saves a hash to file - this obviously will not work
for hashes storing object references.
Return value:
Returns '1' for success, '0' in case of failure
mrHash__define
Description:
Private Procedure (constructor)