|
| 1 | +program example_set_other_data |
| 2 | + use stdlib_kinds, only: int32 |
| 3 | + use stdlib_hashmaps, only: chaining_hashmap_type |
| 4 | + use stdlib_hashmap_wrappers, only: fnv_1_hasher, & |
| 5 | + key_type, other_type, set |
| 6 | + implicit none |
| 7 | + type(chaining_hashmap_type) :: map |
| 8 | + type(key_type) :: key |
| 9 | + type(other_type) :: other |
| 10 | + |
| 11 | + type(key_type), allocatable :: keys(:) |
| 12 | + integer(int32) :: i |
| 13 | + |
| 14 | + call map%init(fnv_1_hasher) |
| 15 | + |
| 16 | + ! adding key-value pairs to the map |
| 17 | + call set(key, "initial key") |
| 18 | + call set(other, "value 1") |
| 19 | + call map%map_entry(key, other) |
| 20 | + |
| 21 | + call set(key, "second key") |
| 22 | + call set(other, "value 2") |
| 23 | + call map%map_entry(key, other) |
| 24 | + |
| 25 | + call set(key, "last key") |
| 26 | + call set(other, "value 3") |
| 27 | + call map%map_entry(key, other) |
| 28 | + |
| 29 | + ! getting all the keys in the map |
| 30 | + call map%get_all_keys(keys) |
| 31 | + |
| 32 | + print '("Number of keys in the hashmap = ", I0)', size(keys) |
| 33 | + !Number of keys in the hashmap = 3 |
| 34 | + |
| 35 | + do i = 1, size(keys) |
| 36 | + print '("Value of key ", I0, " = ", A)', i, key_to_char(keys(i)) |
| 37 | + end do |
| 38 | + !Value of key 1 = initial key |
| 39 | + !Value of key 2 = second key |
| 40 | + !Value of key 3 = last key |
| 41 | + |
| 42 | +contains |
| 43 | + !Converts key type to character type |
| 44 | + function key_to_char(key) result(str) |
| 45 | + implicit none |
| 46 | + type(key_type), intent(in) :: key |
| 47 | + character(:), allocatable :: str |
| 48 | + character(:), allocatable :: str_mold |
| 49 | + |
| 50 | + allocate( character(len=size(key%value)) :: str_mold ) |
| 51 | + str = transfer(key%value, str_mold) |
| 52 | + end function key_to_char |
| 53 | +end program example_set_other_data |
0 commit comments