Skip to content

Commit 6c2565d

Browse files
authored
Merge pull request #843 from chuckyvt/remove_other_type
Remove other derived type from hashmap
2 parents 7ab1a5b + 250252d commit 6c2565d

17 files changed

+222
-448
lines changed

doc/specs/stdlib_hashmaps.md

+67-161
Large diffs are not rendered by default.

example/hashmaps/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
ADD_EXAMPLE(hashmaps_calls)
22
ADD_EXAMPLE(hashmaps_copy_key)
3-
ADD_EXAMPLE(hashmaps_copy_other)
43
ADD_EXAMPLE(hashmaps_entries)
54
ADD_EXAMPLE(hashmaps_equal_keys)
65
ADD_EXAMPLE(hashmaps_fnv_1a_hasher)
76
ADD_EXAMPLE(hashmaps_fnv_1_hasher)
87
ADD_EXAMPLE(hashmaps_free_key)
9-
ADD_EXAMPLE(hashmaps_free_other)
108
ADD_EXAMPLE(hashmaps_get)
119
ADD_EXAMPLE(hashmaps_get_all_keys)
1210
ADD_EXAMPLE(hashmaps_get_other_data)

example/hashmaps/example_hashmaps_copy_other.f90

-22
This file was deleted.

example/hashmaps/example_hashmaps_free_other.f90

-18
This file was deleted.

example/hashmaps/example_hashmaps_get_all_keys.f90

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ program example_hashmaps_get_all_keys
22
use stdlib_kinds, only: int32
33
use stdlib_hashmaps, only: chaining_hashmap_type
44
use stdlib_hashmap_wrappers, only: fnv_1_hasher, get, &
5-
key_type, other_type, set
5+
key_type, set
66
implicit none
77
type(chaining_hashmap_type) :: map
88
type(key_type) :: key
9-
type(other_type) :: other
109

1110
type(key_type), allocatable :: keys(:)
1211
integer(int32) :: i
@@ -17,16 +16,13 @@ program example_hashmaps_get_all_keys
1716

1817
! adding key-value pairs to the map
1918
call set(key, "initial key")
20-
call set(other, "value 1")
21-
call map%map_entry(key, other)
19+
call map%map_entry(key, "value 1")
2220

2321
call set(key, "second key")
24-
call set(other, "value 2")
25-
call map%map_entry(key, other)
22+
call map%map_entry(key, "value 2")
2623

2724
call set(key, "last key")
28-
call set(other, "value 3")
29-
call map%map_entry(key, other)
25+
call map%map_entry(key, "value 3")
3026

3127
! getting all the keys in the map
3228
call map%get_all_keys(keys)

example/hashmaps/example_hashmaps_get_other_data.f90

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
program example_get_other_data
22
use stdlib_kinds, only: int8, int64
3-
use stdlib_hashmaps, only: chaining_hashmap_type
4-
use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type, other_type, set, get
3+
use stdlib_hashmaps, only: chaining_hashmap_type, int_index
4+
use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type, set, get
55
implicit none
66
logical :: conflict
77
type(key_type) :: key
8-
type(other_type) :: other
98
type(chaining_hashmap_type) :: map
109
type dummy_type
1110
integer :: value(4)
@@ -21,17 +20,18 @@ program example_get_other_data
2120
! Hashmap functions are setup to store scalar value types (other). Use a dervied
2221
! type wrapper to store arrays.
2322
dummy%value = [4, 3, 2, 1]
24-
call set(other, dummy)
2523

2624
! Explicitly set key type using set function
2725
call set(key, [0, 1])
28-
call map%map_entry(key, other, conflict)
26+
call map%map_entry(key, dummy, conflict)
2927
if (.not. conflict) then
30-
call map%get_other_data(key, other)
28+
call map%get_other_data(key, data)
3129
else
3230
error stop 'Key is already present in the map.'
3331
end if
34-
call get(other, data)
32+
33+
! Get_other_data returns data as an unlimited polymorphic scalar.
34+
! To use this type in other operations, there must be a select type operation.
3535
select type (data)
3636
type is (dummy_type)
3737
print *, 'Other data % value = ', data%value
@@ -41,29 +41,29 @@ program example_get_other_data
4141

4242
! Also can use map_entry and get_other_data generic key interfaces.
4343
! This is an exmple with integer arrays.
44-
call map%map_entry( [2,3], other, conflict)
44+
call map%map_entry( [2,3], dummy, conflict)
4545
if (.not. conflict) then
46-
call map%get_other_data( [2,3], other)
46+
call map%get_other_data( [2,3], data)
4747
else
4848
error stop 'Key is already present in the map.'
4949
end if
50-
call get(other, data)
50+
5151
select type (data)
5252
type is (dummy_type)
5353
print *, 'Other data % value = ', data%value
5454
class default
5555
print *, 'Invalid data type in other'
5656
end select
5757

58-
! Integer scalars need to be passed as an array.
58+
! Integer scalar keys need to be passed as an array.
5959
int_scalar = 2
60-
call map%map_entry( [int_scalar], other, conflict)
60+
call map%map_entry( [int_scalar], dummy, conflict)
6161
if (.not. conflict) then
62-
call map%get_other_data( [int_scalar], other)
62+
call map%get_other_data( [int_scalar], data)
6363
else
6464
error stop 'Key is already present in the map.'
6565
end if
66-
call get(other, data)
66+
6767
select type (data)
6868
type is (dummy_type)
6969
print *, 'Other data % value = ', data%value
@@ -72,13 +72,13 @@ program example_get_other_data
7272
end select
7373

7474
! Example using character type key interface
75-
call map%map_entry( 'key_string', other, conflict)
75+
call map%map_entry( 'key_string', dummy, conflict)
7676
if (.not. conflict) then
77-
call map%get_other_data( 'key_string', other)
77+
call map%get_other_data( 'key_string', data)
7878
else
7979
error stop 'Key is already present in the map.'
8080
end if
81-
call get(other, data)
81+
8282
select type (data)
8383
type is (dummy_type)
8484
print *, 'Other data % value = ', data%value
@@ -88,13 +88,13 @@ program example_get_other_data
8888

8989
! Transfer to int8 arrays to generate key for unsupported types.
9090
key_array = transfer( [0_int64, 1_int64], [0_int8] )
91-
call map%map_entry( key_array, other, conflict)
91+
call map%map_entry( key_array, dummy, conflict)
9292
if (.not. conflict) then
93-
call map%get_other_data( key_array, other)
93+
call map%get_other_data( key_array, data)
9494
else
9595
error stop 'Key is already present in the map.'
9696
end if
97-
call get(other, data)
97+
9898
select type (data)
9999
type is (dummy_type)
100100
print *, 'Other data % value = ', data%value
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
program example_map_entry
22
use, intrinsic:: iso_fortran_env, only: int8, int64
33
use stdlib_hashmaps, only: chaining_hashmap_type
4-
use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type, other_type, set
4+
use stdlib_hashmap_wrappers, only: fnv_1_hasher, key_type, set
55
implicit none
66
type(chaining_hashmap_type) :: map
77
type(key_type) :: key
88
logical :: conflict
9-
type(other_type) :: other
109
integer :: int_scalar
1110

11+
type :: array_data_wrapper
12+
integer, allocatable :: array(:)
13+
end type
14+
15+
type(array_data_wrapper) :: array_example
16+
1217
! Initialize hashmap with 2^10 slots.
1318
! Hashmap will dynamically increase size if needed.
1419
call map%init(fnv_1_hasher, slots_bits=10)
15-
! Initialize other type with data to store.
16-
call set(other, 4)
1720

1821
! Explicitly set key using set function
1922
call set(key, [1, 2, 3])
20-
call map%map_entry(key, other, conflict)
23+
call map%map_entry(key, 4, conflict)
2124
print *, 'CONFLICT = ', conflict
2225

2326
! Using map_entry int32 array interface
24-
call map%map_entry( [4, 5, 6], other, conflict)
27+
call map%map_entry( [4, 5, 6], 4, conflict)
2528
print *, 'CONFLICT = ', conflict
2629

2730
! Integer scalars need to be passed as an array.
2831
int_scalar = 1
29-
call map%map_entry( [int_scalar], other, conflict)
32+
call map%map_entry( [int_scalar], 4, conflict)
3033
print *, 'CONFLICT = ', conflict
3134

3235
! Using map_entry character interface
33-
call map%map_entry( 'key_string', other, conflict)
36+
call map%map_entry( 'key_string', 4, conflict)
3437
print *, 'CONFLICT = ', conflict
3538

3639
! Transfer unsupported key types to int8 arrays.
37-
call map%map_entry( transfer( [1_int64, 2_int64, 3_int64], [0_int8] ), other, conflict)
40+
call map%map_entry( transfer( [1_int64, 2_int64, 3_int64], [0_int8] ), 4, conflict)
3841
print *, 'CONFLICT = ', conflict
3942

40-
! Keys can be mapped alone without a corresponding value (other).
43+
! Keys can be mapped alone without a corresponding value (other) for 'Set' type functionality.
4144
call map%map_entry( [7, 8, 9], conflict=conflict)
4245
print *, 'CONFLICT = ', conflict
46+
47+
! Currently only scalar data can be mapped.
48+
! Arrays will need a wrapper.
49+
array_example % array = [1,2,3,4,5]
50+
call map % map_entry( [10,11,12], array_example, conflict=conflict )
51+
print *, 'CONFLICT = ', conflict
52+
4353
end program example_map_entry

example/hashmaps/example_hashmaps_rehash.f90

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ program example_rehash
22
use stdlib_kinds, only: int8
33
use stdlib_hashmaps, only: open_hashmap_type
44
use stdlib_hashmap_wrappers, only: fnv_1_hasher, fnv_1a_hasher, &
5-
key_type, other_type, set
5+
key_type, set
66
implicit none
77
type(open_hashmap_type) :: map
88
type(key_type) :: key
9-
type(other_type) :: other
10-
class(*), allocatable :: dummy
11-
allocate (dummy, source='a dummy value')
129
call map%init(fnv_1_hasher, slots_bits=10)
1310
call set(key, [5_int8, 7_int8, 4_int8, 13_int8])
14-
call set(other, dummy)
15-
call map%map_entry(key, other)
11+
call map%map_entry(key, 'A value')
1612
call map%rehash(fnv_1a_hasher)
1713
end program example_rehash

example/hashmaps/example_hashmaps_remove.f90

+6-11
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,40 @@ program example_remove
22
use stdlib_kinds, only: int8, int64
33
use stdlib_hashmaps, only: open_hashmap_type
44
use stdlib_hashmap_wrappers, only: fnv_1_hasher, &
5-
fnv_1a_hasher, key_type, other_type, set
5+
fnv_1a_hasher, key_type, set
66
implicit none
77
type(open_hashmap_type) :: map
88
type(key_type) :: key
9-
type(other_type) :: other
109
logical :: existed
1110
integer :: int_scalar
1211

1312
! Initialize hashmap with 2^10 slots.
1413
! Hashmap will dynamically increase size if needed.
1514
call map%init(fnv_1_hasher, slots_bits=10)
16-
17-
! Initialize other type with data to store.
18-
call set(other, 4.0)
19-
2015
! Explicitly set key type using set function
2116
call set(key, [1, 2, 3])
22-
call map%map_entry(key, other)
17+
call map%map_entry(key, 4.0)
2318
call map%remove(key, existed)
2419
print *, "Removed key existed = ", existed
2520

2621
! Using map_entry and remove int32 generic interface.
27-
call map%map_entry([1, 2, 3], other)
22+
call map%map_entry([1, 2, 3], 4.0)
2823
call map%remove([1, 2, 3], existed)
2924
print *, "Removed key existed = ", existed
3025

3126
! Integer scalars need to be passed as an array.
3227
int_scalar = 1
33-
call map%map_entry( [int_scalar], other)
28+
call map%map_entry( [int_scalar], 4.0)
3429
call map%remove( [int_scalar], existed)
3530
print *, "Removed key existed = ", existed
3631

3732
! Using map_entry and remove character generic interface.
38-
call map%map_entry('key_string', other)
33+
call map%map_entry('key_string', 4.0)
3934
call map%remove('key_string', existed)
4035
print *, "Removed key existed = ", existed
4136

4237
! Use transfer to int8 arrays for unsupported key types.
43-
call map%map_entry( transfer( [1_int64, 2_int64], [0_int8] ), other)
38+
call map%map_entry( transfer( [1_int64, 2_int64], [0_int8] ), 4.0)
4439
call map%remove( transfer( [1_int64,2_int64], [0_int8] ), existed)
4540
print *, "Removed key existed = ", existed
4641
end program example_remove
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
program example_set_other_data
2-
use stdlib_hashmaps, only: open_hashmap_type
3-
use stdlib_hashmap_wrappers, only: fnv_1_hasher, &
4-
fnv_1a_hasher, key_type, other_type, set
2+
use stdlib_kinds, only: int8
3+
use stdlib_hashmaps, only: open_hashmap_type, chaining_hashmap_type
4+
use stdlib_hashmap_wrappers, only: key_type, set, fnv_1_hasher
5+
56
implicit none
67
logical :: exists
7-
type(open_hashmap_type) :: map
8-
type(key_type) :: key
9-
type(other_type) :: other
8+
type(chaining_hashmap_type) :: map
9+
class(*), allocatable :: data
10+
type(key_type) :: key
1011

1112
! Initialize hashmap with 2^10 slots.
1213
! Hashmap will dynamically increase size if needed.
1314
call map%init(fnv_1_hasher, slots_bits=10)
15+
1416
call set(key, [5, 7, 4, 13])
15-
call set(other, 'A value')
16-
call map%map_entry(key, other)
1717

18-
call set(other, 'Another value')
19-
call map%set_other_data(key, other, exists)
18+
call map%map_entry(key, 'A value')
19+
20+
call map%set_other_data(key, 'Another value', exists)
21+
2022
print *, 'The entry to have its other data replaced exists = ', exists
2123

24+
call map%get_other_data(key, data, exists)
25+
26+
print *, 'Get_other_data was successful = ', exists
27+
28+
! Hashmaps return an unlimited polymorphic type as other.
29+
! Must be included in a select type operation to do further operations.
30+
select type (data)
31+
type is (character(*))
32+
print *, 'Value is = ', data
33+
class default
34+
print *, 'Invalid data type in other'
35+
end select
36+
2237
end program example_set_other_data

0 commit comments

Comments
 (0)