Skip to content

Commit 350f19b

Browse files
committed
API for get_other_scalar simplified
1 parent 88e6f36 commit 350f19b

File tree

2 files changed

+51
-42
lines changed

2 files changed

+51
-42
lines changed

src/stdlib_hashmap_wrappers.fypp

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#:include "common.fypp"
2+
#:set IRLC_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES + LOG_KINDS_TYPES + CMPLX_KINDS_TYPES
23
!! The module STDLIB_HASHMAP_WRAPPERS provides wrappers for various
34
!! entities used by the hash map procedures. These include wrappers for the
45
!! `key` and `other` data, and hashing procedures to operate on entities of
@@ -99,6 +100,15 @@ module stdlib_hashmap_wrappers
99100

100101
end interface get
101102

103+
interface get_other_scalar
104+
105+
module procedure get_other_scalar_char
106+
#:for k1, t1 in IRLC_KINDS_TYPES
107+
module procedure get_other_scalar_${t1[0]}$${k1}$
108+
#:endfor
109+
110+
end interface get_other_scalar
111+
102112

103113
interface operator(==)
104114
module procedure equal_keys
@@ -268,28 +278,13 @@ contains
268278

269279
end subroutine get_other
270280

271-
272-
subroutine get_other_scalar(other, value_char &
273-
#:set IRL_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES + LOG_KINDS_TYPES
274-
#:for k1, t1 in IRL_KINDS_TYPES
275-
, value_${k1}$ &
276-
#:endfor
277-
#:for k1, t1 in CMPLX_KINDS_TYPES
278-
, value_c${k1}$ &
279-
#:endfor
280-
, exists)
281+
subroutine get_other_scalar_char(other, value, exists)
281282
!! Version: Experimental
282283
!!
283284
!! Gets the content of the other as a scalar of a kind provided by stdlib_kinds
284285
!! ([Specifications](../page/specs/stdlib_hashmaps.html#get_other_scalar-extracts-a-scalar-value-from-a-derived-type))
285286
class(other_type), intent(in) :: other
286-
character(len=:), allocatable, intent(out), optional :: value_char
287-
#:for k1, t1 in IRL_KINDS_TYPES
288-
${t1}$, intent(out), optional :: value_${k1}$
289-
#:endfor
290-
#:for k1, t1 in CMPLX_KINDS_TYPES
291-
${t1}$, intent(out), optional :: value_c${k1}$
292-
#:endfor
287+
character(len=:), allocatable, intent(out) :: value
293288
logical, intent(out), optional :: exists
294289

295290
logical :: exists_
@@ -303,29 +298,43 @@ contains
303298

304299
select type(d => other % value)
305300
type is ( character(*) )
306-
if (present(value_char)) then
307-
value_char = d
308-
exists_ = .true.
309-
end if
310-
#:for k1, t1 in IRL_KINDS_TYPES
311-
type is ( ${t1}$ )
312-
if (present(value_${k1}$)) then
313-
value_${k1}$ = d
314-
exists_ = .true.
315-
end if
316-
#:endfor
317-
#:for k1, t1 in CMPLX_KINDS_TYPES
301+
value = d
302+
exists_ = .true.
303+
end select
304+
305+
if (present(exists)) exists = exists_
306+
307+
end subroutine
308+
309+
#:for k1, t1 in IRLC_KINDS_TYPES
310+
subroutine get_other_scalar_${t1[0]}$${k1}$(other, value, exists)
311+
!! Version: Experimental
312+
!!
313+
!! Gets the content of the other as a scalar of a kind provided by stdlib_kinds
314+
!! ([Specifications](../page/specs/stdlib_hashmaps.html#get_other_scalar-extracts-a-scalar-value-from-a-derived-type))
315+
class(other_type), intent(in) :: other
316+
${t1}$, intent(out) :: value
317+
logical, intent(out), optional :: exists
318+
319+
logical :: exists_
320+
321+
exists_ = .false.
322+
323+
if (.not.allocated(other % value)) then
324+
if (present(exists)) exists = exists_
325+
return
326+
end if
327+
328+
select type(d => other % value)
318329
type is ( ${t1}$ )
319-
if (present(value_c${k1}$)) then
320-
value_c${k1}$ = d
321-
exists_ = .true.
322-
end if
323-
#:endfor
330+
value = d
331+
exists_ = .true.
324332
end select
325333

326334
if (present(exists)) exists = exists_
327335

328336
end subroutine
337+
#:endfor
329338

330339
subroutine get_int8_key( key, value )
331340
!! Version: Experimental

src/tests/hashmaps/test_maps.fypp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ contains
4646

4747
call set ( other, value_in )
4848

49-
call get_other_scalar(other, value_char = value_out)
49+
call get_other_scalar(other, value_out)
5050

5151
call check(error, value_in, value_out, "get_other_scalar char: value_in not equal to value_out")
5252
return
5353

54-
call get_other_scalar(other, value_char = value_out, exists = exists)
54+
call get_other_scalar(other, value_out, exists = exists)
5555
call check(error, value_in, value_out, "get_other_scalar char: value_in not equal to value_out")
5656
return
5757
call check(error, exists, "get_other_scalar char: exists should be .true.")
@@ -70,12 +70,12 @@ contains
7070

7171
call set ( other, value_in )
7272

73-
call get_other_scalar(other, value_${k1}$ = value_out)
73+
call get_other_scalar(other, value_out)
7474

7575
call check(error, value_in, value_out, "get_other_scalar ${k1}$: value_in not equal to value_out")
7676
return
7777

78-
call get_other_scalar(other, value_${k1}$ = value_out, exists = exists)
78+
call get_other_scalar(other, value_out, exists = exists)
7979

8080
call check(error, value_in, value_out, "get_other_scalar ${k1}$: value_in not equal to value_out")
8181
return
@@ -97,12 +97,12 @@ contains
9797

9898
call set ( other, value_in )
9999

100-
call get_other_scalar(other, value_c${k1}$ = value_out)
100+
call get_other_scalar(other, value_out)
101101

102102
call check(error, value_in, value_out, "get_other_scalar c${k1}$: value_in not equal to value_out")
103103
return
104104

105-
call get_other_scalar(other, value_c${k1}$ = value_out, exists = exists)
105+
call get_other_scalar(other, value_out, exists = exists)
106106

107107
call check(error, value_in, value_out, "get_other_scalar c${k1}$: value_in not equal to value_out")
108108
return
@@ -124,12 +124,12 @@ contains
124124

125125
call set ( other, value_in )
126126

127-
call get_other_scalar(other, value_lk = value_out)
127+
call get_other_scalar(other, value_out)
128128

129129
call check(error, value_in .eqv. value_out, "get_other_scalar lk: value_in not equal to value_out")
130130
return
131131

132-
call get_other_scalar(other, value_lk = value_out, exists = exists)
132+
call get_other_scalar(other, value_out, exists = exists)
133133

134134
call check(error, value_in .eqv. value_out, "get_other_scalar lk: value_in not equal to value_out")
135135
return

0 commit comments

Comments
 (0)