Custom key types can be defined simply by extension of the abstract fhash_key_t type
defined in fhash_key_base.
Extensions of this type must implement the equals, hash and to_string procedures.
Optionally, you may also override the fhash_key interface with a helper function to
generate a key from your custom type.
To perform the hashing, the included fhash_fnv module provides the fnv_1a interface
which supports default scalar characters and 32bit/64bit scalar/1D integers.
You can use this interface to generate a hash from the components of your derived type.
In this example, a key_string_t key container type is defined as an extension
of fhash_key_T which allows the string_t derived type to be used as a key.
!> Example program demonstrating how a custom key-type
!> can be used.
!> See the `my_key_type` module for the definition of the
!> custom key type.
!>
!> See README.md for an explanation
!>
program fhash_demo
use fhash, only: fhash_tbl_t
use my_key_type, only: string_t, key_string_t, key=>fhash_key
implicit none
type(fhash_tbl_t) :: tbl
type(string_t) :: str1, str2
integer :: val
print *, '# fhash demo program: custom-key-demo'
str1%s = 'Hello world'
str2%s = 'Hello fhash'
print *, 'Storing value 10 with key: "',str1%s,'"'
call tbl%set(key([str1]), value=10)
print *, 'Storing value 20 with key: "',str2%s,'"'
call tbl%set(key([str2]), value=20)
print *, 'Storing value 30 with key: ["',str1%s,'", "',str2%s,'"]'
call tbl%set(key([str1,str2]), value=30)
print *, 'Retrieving value with key: "',str1%s,'"'
call tbl%get(key([str1]),val)
print *, ' value = ',val
print *, 'Retrieving value with key: "',str2%s,'"'
call tbl%get(key([str2]),val)
print *, ' value = ',val
print *, 'Retrieving value with key: ["',str1%s,'", "',str2%s,'"]'
call tbl%get(key([str1,str2]),val)
print *, ' value = ',val
print *
end program fhash_demo