Skip to content

Commit 7cde920

Browse files
committed
added an example for get_all_keys
1 parent f7fa292 commit 7cde920

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

example/hashmaps/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ADD_EXAMPLE(hashmaps_fnv_1_hasher)
88
ADD_EXAMPLE(hashmaps_free_key)
99
ADD_EXAMPLE(hashmaps_free_other)
1010
ADD_EXAMPLE(hashmaps_get)
11+
ADD_EXAMPLE(hashmaps_get_all_keys)
1112
ADD_EXAMPLE(hashmaps_get_other_data)
1213
ADD_EXAMPLE(hashmaps_hasher_fun)
1314
ADD_EXAMPLE(hashmaps_init)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)