Very basic hash table implementation for a modest number of elements that stores only unlimited polymorphic pointers to values. The hash table uses the FNV-1a algorithm to generate hashes.
Make sure that the values do not go out of scope.
In the following example, the hash table stores pointers to values of a string array:
character(len=32), target :: values(3)
class(*), pointer :: ptr
integer :: rc
type(hash_table_type) :: table
values(1) = 'bar'
values(2) = 'baz'
values(3) = 'qux'
rc = dm_hash_table_create(table, size(values))
rc = dm_hash_table_set(table, 'foo', values(1))
rc = dm_hash_table_set(table, 'zap', values(2))
rc = dm_hash_table_set(table, 'uxn', values(3))
rc = dm_hash_table_get(table, 'zap', ptr)
select type (value => ptr)
type is (character(len=*)); print '(a)', trim(value)
class default; error stop
end select
call dm_hash_table_destroy(table)
Generic interface to hash table get functions.
Returns pointer to element in hash table by index loc. On error,
value will point to null.
The function returns the following error codes:
E_BOUNDS if the index is outside the array bounds.E_NULL if the hash table value pointer is not associated.| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(hash_table_type), | intent(inout) | :: | hash_table |
Hash table type. |
||
| integer, | intent(in) | :: | loc |
Hash value index. |
||
| class(*), | intent(out), | pointer | :: | value |
Associated value. |
Returns pointer to element in hash table by key. On error, value
will point to null. The intrinsic findloc() should be sufficient for
a small number of elements. For larger hash tables, buckets have to be
added.
The function returns the following error codes:
E_NOT_FOUND if the key was not found.E_NULL if the hash table value pointer is not associated.| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(hash_table_type), | intent(inout) | :: | hash_table |
Hash table type. |
||
| character(len=*), | intent(in) | :: | key |
Hash table key. |
||
| class(*), | intent(out), | pointer | :: | value |
Associated value. |
Container that keeps generic pointer to hash table value.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| class(*), | public, | pointer | :: | ptr | => | null() |
Opaque hash table type of key-value pairs.
Create a new hash table with maximum number of entries.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(hash_table_type), | intent(inout) | :: | hash_table |
Hash table type. |
||
| integer, | intent(in) | :: | max_entries |
Maximum number of entries. |
Returns .true. if hash table arrays have been allocated.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(hash_table_type), | intent(inout) | :: | hash_table |
Hash table type. |
Adds element to hash table, or replaces existing value. This function does not resolve hash collisions.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(hash_table_type), | intent(inout) | :: | hash_table |
Hash table type. |
||
| character(len=*), | intent(in) | :: | key |
Hash table key. |
||
| class(*), | intent(inout), | target | :: | value |
Associated value. |
Finalises hash table. If the hash table items contain allocatable data types, you have to deallocate them manually beforehand.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(hash_table_type), | intent(inout) | :: | hash_table |
Hash table type. |
Returns cursor position in n and maximum size of hash table in
max_size.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(hash_table_type), | intent(inout) | :: | hash_table |
Hash table type. |
||
| integer, | intent(out), | optional | :: | n |
Current number of values. |
|
| integer, | intent(out), | optional | :: | max_size |
Max. number of values. |