Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove other derived type from hashmap #843

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
51a55df
RC1
chuckyvt Jun 30, 2024
1888280
Update src/stdlib_hashmap_chaining.f90
chuckyvt Jul 3, 2024
fa23a44
Update src/stdlib_hashmap_open.f90
chuckyvt Jul 3, 2024
18ed909
Update src/stdlib_hashmap_chaining.f90
chuckyvt Jul 3, 2024
97ae06d
Other_type cleanup and removal
chuckyvt Jul 3, 2024
cd435fc
Merge branch 'remove_other_type' of https://github.com/chuckyvt/stdli…
chuckyvt Jul 3, 2024
1906b4c
Remove other type from test_chaining and test_open
chuckyvt Jul 3, 2024
a52302d
Update stdlib_hashmap_chaining.f90
chuckyvt Jul 3, 2024
a31b2f9
Update example_hashmaps_set_other_data.f90
chuckyvt Jul 3, 2024
f47b4c5
Update CMakeLists.txt
chuckyvt Jul 4, 2024
ed99423
Update CMakeLists.txt
chuckyvt Jul 4, 2024
3d1adc8
Update CMakeLists.txt
chuckyvt Jul 4, 2024
50ae961
Revert "Update CMakeLists.txt"
chuckyvt Jul 4, 2024
d5f098e
Update CMakeLists.txt
chuckyvt Jul 4, 2024
b10a851
Update example_hashmaps_set_other_data.f90
chuckyvt Jul 4, 2024
cba213d
Update stdlib_hashmaps.md
chuckyvt Jul 7, 2024
16d2d34
Update to remove other_type references
chuckyvt Jul 10, 2024
8ade03d
Update example_hashmaps_map_entry.f90
chuckyvt Jul 10, 2024
793c049
Update example_hashmaps_rehash.f90
chuckyvt Jul 10, 2024
25a1146
Update example_hashmaps_remove.f90
chuckyvt Jul 10, 2024
775be76
Test allocate again
chuckyvt Jul 10, 2024
154579e
Update to include allocate statement
chuckyvt Jul 10, 2024
d32406b
Typo fix
chuckyvt Jul 10, 2024
b73e0fc
Update doc/specs/stdlib_hashmaps.md
chuckyvt Jul 11, 2024
244b308
Update doc/specs/stdlib_hashmaps.md
chuckyvt Jul 11, 2024
6c73db7
Update doc/specs/stdlib_hashmaps.md
chuckyvt Jul 11, 2024
054f712
Update doc/specs/stdlib_hashmaps.md
chuckyvt Jul 11, 2024
057fcac
Update example/hashmaps/CMakeLists.txt
chuckyvt Jul 11, 2024
c3e4d31
Update example/hashmaps/example_hashmaps_get_other_data.f90
chuckyvt Jul 11, 2024
facd189
Update example/hashmaps/example_hashmaps_map_entry.f90
chuckyvt Jul 11, 2024
2b2c290
Update example/hashmaps/example_hashmaps_remove.f90
chuckyvt Jul 11, 2024
0e29642
Update example/hashmaps/example_hashmaps_set_other_data.f90
chuckyvt Jul 11, 2024
3c60ba4
Reverting back to in-line allocation
chuckyvt Jul 11, 2024
7118fc5
Revert "Reverting back to in-line allocation"
chuckyvt Jul 11, 2024
624a7c8
Reapply "Reverting back to in-line allocation"
chuckyvt Jul 12, 2024
0df1d81
Update stdlib_hashmap_chaining.f90
chuckyvt Jul 12, 2024
9415a37
Update doc/specs/stdlib_hashmaps.md
chuckyvt Jul 15, 2024
351e628
Update stdlib_hashmap_chaining.f90
chuckyvt Jul 16, 2024
3e7337e
Update stdlib_hashmap_chaining.f90
chuckyvt Jul 16, 2024
4bd0bb4
Update stdlib_hashmap_chaining.f90
chuckyvt Jul 16, 2024
bf3c90d
Set_other CI fix
chuckyvt Jul 16, 2024
2ae0d6d
Update set_other again
chuckyvt Jul 16, 2024
937cade
Update stdlib_hashmap_chaining.f90
chuckyvt Jul 17, 2024
873a6de
Update stdlib_hashmap_chaining.f90
chuckyvt Jul 17, 2024
e6296c8
Revert "Update stdlib_hashmap_chaining.f90"
chuckyvt Jul 17, 2024
f7d35c5
Revert "Update stdlib_hashmap_chaining.f90"
chuckyvt Jul 17, 2024
ef1d924
Merge branch 'master' into remove_other_type
chuckyvt Aug 31, 2024
188a74c
Update stdlib_hashmaps.f90
chuckyvt Aug 31, 2024
fbce81f
Revert "Update stdlib_hashmaps.f90"
chuckyvt Sep 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions example/hashmaps/example_hashmaps_copy_other.f90
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
!! This example left for reference, however 'other_type' has largely
!! been depreciated in the stdlib hashmaps.

program example_copy_other
use stdlib_hashmap_wrappers, only: &
copy_other, other_type
Expand Down
3 changes: 3 additions & 0 deletions example/hashmaps/example_hashmaps_free_other.f90
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
!! This example left for reference, however 'other_type' has largely
!! been depreciated in stdlib hashmaps.

program example_free_other
use stdlib_hashmap_wrappers, only: &
copy_other, free_other, other_type
Expand Down
10 changes: 3 additions & 7 deletions example/hashmaps/example_hashmaps_get_all_keys.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ program example_hashmaps_get_all_keys
implicit none
type(chaining_hashmap_type) :: map
type(key_type) :: key
type(other_type) :: other

type(key_type), allocatable :: keys(:)
integer(int32) :: i
Expand All @@ -17,16 +16,13 @@ program example_hashmaps_get_all_keys

! adding key-value pairs to the map
call set(key, "initial key")
call set(other, "value 1")
call map%map_entry(key, other)
call map%map_entry(key, "value 1")

call set(key, "second key")
call set(other, "value 2")
call map%map_entry(key, other)
call map%map_entry(key, "value 2")

call set(key, "last key")
call set(other, "value 3")
call map%map_entry(key, other)
call map%map_entry(key, "value 3")

! getting all the keys in the map
call map%get_all_keys(keys)
Expand Down
37 changes: 19 additions & 18 deletions example/hashmaps/example_hashmaps_get_other_data.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ program example_get_other_data
implicit none
logical :: conflict
type(key_type) :: key
type(other_type) :: other

chuckyvt marked this conversation as resolved.
Show resolved Hide resolved
type(chaining_hashmap_type) :: map
type dummy_type
integer :: value(4)
Expand All @@ -21,17 +21,18 @@ program example_get_other_data
! Hashmap functions are setup to store scalar value types (other). Use a dervied
! type wrapper to store arrays.
dummy%value = [4, 3, 2, 1]
call set(other, dummy)

! Explicitly set key type using set function
call set(key, [0, 1])
call map%map_entry(key, other, conflict)
call map%map_entry(key, dummy, conflict)
if (.not. conflict) then
call map%get_other_data(key, other)
call map%get_other_data(key, data)
else
error stop 'Key is already present in the map.'
end if
call get(other, data)

! Get_other_data returns data as an unlimited polymorphic scalar.
! To use this type in other operations, there must be a select type operation.
select type (data)
type is (dummy_type)
print *, 'Other data % value = ', data%value
Expand All @@ -41,29 +42,29 @@ program example_get_other_data

! Also can use map_entry and get_other_data generic key interfaces.
! This is an exmple with integer arrays.
call map%map_entry( [2,3], other, conflict)
call map%map_entry( [2,3], dummy, conflict)
if (.not. conflict) then
call map%get_other_data( [2,3], other)
call map%get_other_data( [2,3], data)
else
error stop 'Key is already present in the map.'
end if
call get(other, data)

select type (data)
type is (dummy_type)
print *, 'Other data % value = ', data%value
class default
print *, 'Invalid data type in other'
end select

! Integer scalars need to be passed as an array.
! Integer scalar keys need to be passed as an array.
int_scalar = 2
call map%map_entry( [int_scalar], other, conflict)
call map%map_entry( [int_scalar], dummy, conflict)
if (.not. conflict) then
call map%get_other_data( [int_scalar], other)
call map%get_other_data( [int_scalar], data)
else
error stop 'Key is already present in the map.'
end if
call get(other, data)

select type (data)
type is (dummy_type)
print *, 'Other data % value = ', data%value
Expand All @@ -72,13 +73,13 @@ program example_get_other_data
end select

! Example using character type key interface
call map%map_entry( 'key_string', other, conflict)
call map%map_entry( 'key_string', dummy, conflict)
if (.not. conflict) then
call map%get_other_data( 'key_string', other)
call map%get_other_data( 'key_string', data)
else
error stop 'Key is already present in the map.'
end if
call get(other, data)

select type (data)
type is (dummy_type)
print *, 'Other data % value = ', data%value
Expand All @@ -88,13 +89,13 @@ program example_get_other_data

! Transfer to int8 arrays to generate key for unsupported types.
key_array = transfer( [0_int64, 1_int64], [0_int8] )
call map%map_entry( key_array, other, conflict)
call map%map_entry( key_array, dummy, conflict)
if (.not. conflict) then
call map%get_other_data( key_array, other)
call map%get_other_data( key_array, data)
else
error stop 'Key is already present in the map.'
end if
call get(other, data)

select type (data)
type is (dummy_type)
print *, 'Other data % value = ', data%value
Expand Down
29 changes: 20 additions & 9 deletions example/hashmaps/example_hashmaps_map_entry.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,49 @@ program example_map_entry
type(chaining_hashmap_type) :: map
type(key_type) :: key
logical :: conflict
type(other_type) :: other
integer :: int_scalar

type :: array_data_wrapper
integer, allocatable :: array(:)
end type

type(array_data_wrapper) :: array_example


chuckyvt marked this conversation as resolved.
Show resolved Hide resolved
! Initialize hashmap with 2^10 slots.
! Hashmap will dynamically increase size if needed.
call map%init(fnv_1_hasher, slots_bits=10)
! Initialize other type with data to store.
call set(other, 4)

! Explicitly set key using set function
call set(key, [1, 2, 3])
call map%map_entry(key, other, conflict)
call map%map_entry(key, 4, conflict)
print *, 'CONFLICT = ', conflict

! Using map_entry int32 array interface
call map%map_entry( [4, 5, 6], other, conflict)
call map%map_entry( [4, 5, 6], 4, conflict)
print *, 'CONFLICT = ', conflict

! Integer scalars need to be passed as an array.
int_scalar = 1
call map%map_entry( [int_scalar], other, conflict)
call map%map_entry( [int_scalar], 4, conflict)
print *, 'CONFLICT = ', conflict

! Using map_entry character interface
call map%map_entry( 'key_string', other, conflict)
call map%map_entry( 'key_string', 4, conflict)
print *, 'CONFLICT = ', conflict

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

! Keys can be mapped alone without a corresponding value (other).
! Keys can be mapped alone without a corresponding value (other) for 'Set' type functionality.
call map%map_entry( [7, 8, 9], conflict=conflict)
print *, 'CONFLICT = ', conflict

! Currently only scalar data can be mapped.
! Arrays will need a wrapper.
array_example % array = [1,2,3,4,5]
call map % map_entry( [10,11,12], array_example, conflict=conflict )
print *, 'CONFLICT = ', conflict

end program example_map_entry
16 changes: 6 additions & 10 deletions example/hashmaps/example_hashmaps_remove.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,37 @@ program example_remove
implicit none
type(open_hashmap_type) :: map
type(key_type) :: key
type(other_type) :: other
logical :: existed
integer :: int_scalar

! Initialize hashmap with 2^10 slots.
! Hashmap will dynamically increase size if needed.
call map%init(fnv_1_hasher, slots_bits=10)

! Initialize other type with data to store.
call set(other, 4.0)


chuckyvt marked this conversation as resolved.
Show resolved Hide resolved
! Explicitly set key type using set function
call set(key, [1, 2, 3])
call map%map_entry(key, other)
call map%map_entry(key, 4.0)
call map%remove(key, existed)
print *, "Removed key existed = ", existed

! Using map_entry and remove int32 generic interface.
call map%map_entry([1, 2, 3], other)
call map%map_entry([1, 2, 3], 4.0)
call map%remove([1, 2, 3], existed)
print *, "Removed key existed = ", existed

! Integer scalars need to be passed as an array.
int_scalar = 1
call map%map_entry( [int_scalar], other)
call map%map_entry( [int_scalar], 4.0)
call map%remove( [int_scalar], existed)
print *, "Removed key existed = ", existed

! Using map_entry and remove character generic interface.
call map%map_entry('key_string', other)
call map%map_entry('key_string', 4.0)
call map%remove('key_string', existed)
print *, "Removed key existed = ", existed

! Use transfer to int8 arrays for unsupported key types.
call map%map_entry( transfer( [1_int64, 2_int64], [0_int8] ), other)
call map%map_entry( transfer( [1_int64, 2_int64], [0_int8] ), 4.0)
call map%remove( transfer( [1_int64,2_int64], [0_int8] ), existed)
print *, "Removed key existed = ", existed
end program example_remove
23 changes: 16 additions & 7 deletions example/hashmaps/example_hashmaps_set_other_data.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ program example_set_other_data
implicit none
logical :: exists
type(open_hashmap_type) :: map
type(key_type) :: key
type(other_type) :: other
class(*), allocatable :: data

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

call map%map_entry([5, 7, 4, 13], 'A value')

call map%set_other_data([5, 7, 4, 13], 'Another value', exists)

call set(other, 'Another value')
call map%set_other_data(key, other, exists)
print *, 'The entry to have its other data replaced exists = ', exists

call map%get_other_data( [5, 7, 4, 13], data)

! Hashmaps return an unlimited polymorphic type as other.
! Must be included in a select type operation to do further operations.
select type (data)
type is (character(*))
print *, 'Value is = ', data
class default
print *, 'Invalid data type in other'
end select

end program example_set_other_data
18 changes: 9 additions & 9 deletions src/stdlib_hashmap_chaining.f90
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ module subroutine get_other_chaining_data( map, key, other, exists )
!
class(chaining_hashmap_type), intent(inout) :: map
type(key_type), intent(in) :: key
type(other_type), intent(out) :: other
class(*), allocatable, intent(out) :: other
jvdp1 marked this conversation as resolved.
Show resolved Hide resolved
logical, intent(out), optional :: exists

integer(int_index) :: inmap
Expand All @@ -345,7 +345,7 @@ module subroutine get_other_chaining_data( map, key, other, exists )
end if
else if ( associated( map % inverse(inmap) % target ) ) then
if (present(exists) ) exists = .true.
call copy_other( map % inverse(inmap) % target % other, other )
other = map % inverse(inmap) % target % other
chuckyvt marked this conversation as resolved.
Show resolved Hide resolved
else
if ( present(exists) ) then
exists = .false.
Expand Down Expand Up @@ -535,7 +535,7 @@ module subroutine map_chain_entry(map, key, other, conflict)
!
class(chaining_hashmap_type), intent(inout) :: map
type(key_type), intent(in) :: key
type(other_type), intent(in), optional :: other
class(*), intent(in), optional :: other
logical, intent(out), optional :: conflict

integer(int_hash) :: hash_index
Expand Down Expand Up @@ -568,8 +568,7 @@ module subroutine map_chain_entry(map, key, other, conflict)
new_ent % next => map % slots(hash_index) % target
map % slots(hash_index) % target => new_ent
call copy_key( key, new_ent % key )
if ( present(other) ) call copy_other( other, new_ent % other )

if ( present(other) ) new_ent % other = other
chuckyvt marked this conversation as resolved.
Show resolved Hide resolved
if ( new_ent % inmap == 0 ) then
map % num_entries = map % num_entries + 1
inmap = map % num_entries
Expand Down Expand Up @@ -793,7 +792,7 @@ module subroutine set_other_chaining_data( map, key, other, exists )
!
class(chaining_hashmap_type), intent(inout) :: map
type(key_type), intent(in) :: key
type(other_type), intent(in) :: other
class(*), intent(in) :: other
logical, intent(out), optional :: exists

integer(int_index) :: inmap
Expand All @@ -811,9 +810,10 @@ module subroutine set_other_chaining_data( map, key, other, exists )
end if
else if ( associated( map % inverse(inmap) % target ) ) then
associate( target => map % inverse(inmap) % target )
call copy_other( other, target % other )
if ( present(exists) ) exists = .true.
return

chuckyvt marked this conversation as resolved.
Show resolved Hide resolved
target % other = other
if ( present(exists) ) exists = .true.
return
end associate
else
error stop submodule_name // ' % ' // procedure // ': ' // &
Expand Down
Loading
Loading