Skip to content

Commit 1a123ee

Browse files
committed
Relocate add_array to stdlib_array bc that's where it belongs
1 parent af0441f commit 1a123ee

File tree

5 files changed

+85
-84
lines changed

5 files changed

+85
-84
lines changed

example/io/example_save_npz.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
program example_save_npz
2-
use stdlib_array, only: t_array_wrapper
3-
use stdlib_io_np, only: add_array, save_npz
2+
use stdlib_array, only: t_array_wrapper, add_array
3+
use stdlib_io_np, only: save_npz
44
implicit none
55

66
type(t_array_wrapper), allocatable :: arrays(:)

src/stdlib_array.fypp

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
!> The specification of this module is available [here](../page/specs/stdlib_array.html).
1010
module stdlib_array
1111
use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, xdp, qp
12+
use stdlib_strings, only: to_string
1213
implicit none
1314
private
1415

15-
public :: trueloc, falseloc
16+
public :: add_array, trueloc, falseloc
1617

1718
!> Helper class to allocate t_array as an abstract type.
1819
type, public :: t_array_wrapper
@@ -31,8 +32,87 @@ module stdlib_array
3132
#:endfor
3233
#:endfor
3334

35+
interface add_array
36+
#:for k1, t1 in KINDS_TYPES
37+
#:for rank in RANKS
38+
module subroutine add_array_${t1[0]}$${k1}$_${rank}$(arrays, array, stat, msg, name)
39+
!> Array of arrays to which the array is to be added.
40+
type(t_array_wrapper), allocatable, intent(inout) :: arrays(:)
41+
!> Array to be added.
42+
${t1}$, intent(in) :: array${ranksuffix(rank)}$
43+
!> Status of addition.
44+
integer, intent(out), optional :: stat
45+
!> Error message.
46+
character(len=:), allocatable, intent(out), optional :: msg
47+
!> Name of the array to be added. A default name will be used if not provided.
48+
character(len=*), intent(in), optional :: name
49+
end
50+
#:endfor
51+
#:endfor
52+
end interface
53+
3454
contains
3555

56+
#:for k1, t1 in KINDS_TYPES
57+
#:for rank in RANKS
58+
module subroutine add_array_${t1[0]}$${k1}$_${rank}$(arrays, array, stat, msg, name)
59+
!> Array of arrays to which the array is to be added.
60+
type(t_array_wrapper), allocatable, intent(inout) :: arrays(:)
61+
!> Array to be added.
62+
${t1}$, intent(in) :: array${ranksuffix(rank)}$
63+
!> Status of addition.
64+
integer, intent(out), optional :: stat
65+
!> Error message.
66+
character(len=:), allocatable, intent(out), optional :: msg
67+
!> Name of the array to be added. A default name will be used if not provided.
68+
character(len=*), intent(in), optional :: name
69+
70+
integer :: i, arr_size
71+
type(t_array_${t1[0]}$${k1}$_${rank}$) :: t_arr
72+
type(t_array_wrapper), allocatable :: tmp_arrays(:)
73+
74+
75+
if (present(stat)) stat = 0
76+
77+
if (present(name)) then
78+
if (trim(name) == '') then
79+
if (present(stat)) stat = 1
80+
if (present(msg)) msg = "Array name cannot be empty."
81+
return
82+
end if
83+
t_arr%name = name
84+
else
85+
if (allocated(arrays)) then
86+
t_arr%name = "arr_"//to_string(size(arrays))//".npy"
87+
else
88+
t_arr%name = "arr_0.npy"
89+
end if
90+
end if
91+
92+
allocate(t_arr%values, source=array)
93+
if (.not. allocated(arrays)) then
94+
allocate(arrays(1))
95+
allocate(arrays(1)%array, source=t_arr)
96+
return
97+
end if
98+
99+
arr_size = size(arrays)
100+
do i = 1, arr_size
101+
if (arrays(i)%array%name == t_arr%name) then
102+
if (present(stat)) stat = 1
103+
if (present(msg)) msg = "Array with the same name '"//t_arr%name//"' already exists."
104+
return
105+
end if
106+
end do
107+
108+
allocate(tmp_arrays(arr_size + 1))
109+
tmp_arrays(:arr_size) = arrays
110+
allocate(tmp_arrays(arr_size + 1)%array, source=t_arr)
111+
call move_alloc(tmp_arrays, arrays)
112+
end
113+
#:endfor
114+
#:endfor
115+
36116
!> Version: experimental
37117
!>
38118
!> Return the positions of the true elements in array.

src/stdlib_io_np.fypp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module stdlib_io_np
7474
implicit none
7575
private
7676

77-
public :: load_npy, save_npy, load_npz, save_npz, add_array
77+
public :: load_npy, save_npy, load_npz, save_npz
7878

7979
character(len=*), parameter :: &
8080
type_iint8 = "<i1", type_iint16 = "<i2", type_iint32 = "<i4", type_iint64 = "<i8", &
@@ -144,23 +144,4 @@ module stdlib_io_np
144144
logical, intent(in), optional :: compressed
145145
end
146146
end interface
147-
148-
interface add_array
149-
#:for k1, t1 in KINDS_TYPES
150-
#:for rank in RANKS
151-
module subroutine add_array_${t1[0]}$${k1}$_${rank}$(arrays, array, stat, msg, name)
152-
!> Array of arrays to which the array is to be added.
153-
type(t_array_wrapper), allocatable, intent(inout) :: arrays(:)
154-
!> Array to be added.
155-
${t1}$, intent(in) :: array${ranksuffix(rank)}$
156-
!> Status of addition.
157-
integer, intent(out), optional :: stat
158-
!> Error message.
159-
character(len=:), allocatable, intent(out), optional :: msg
160-
!> Name of the array to be added. A default name will be used if not provided.
161-
character(len=*), intent(in), optional :: name
162-
end
163-
#:endfor
164-
#:endfor
165-
end interface
166147
end

src/stdlib_io_np_save.fypp

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -133,66 +133,6 @@ contains
133133
end if
134134
end
135135
#:endfor
136-
#:endfor
137-
138-
#:for k1, t1 in KINDS_TYPES
139-
#:for rank in RANKS
140-
module subroutine add_array_${t1[0]}$${k1}$_${rank}$(arrays, array, stat, msg, name)
141-
!> Array of arrays to which the array is to be added.
142-
type(t_array_wrapper), allocatable, intent(inout) :: arrays(:)
143-
!> Array to be added.
144-
${t1}$, intent(in) :: array${ranksuffix(rank)}$
145-
!> Status of addition.
146-
integer, intent(out), optional :: stat
147-
!> Error message.
148-
character(len=:), allocatable, intent(out), optional :: msg
149-
!> Name of the array to be added. A default name will be used if not provided.
150-
character(len=*), intent(in), optional :: name
151-
152-
integer :: i, arr_size
153-
type(t_array_${t1[0]}$${k1}$_${rank}$) :: t_arr
154-
type(t_array_wrapper), allocatable :: tmp_arrays(:)
155-
156-
157-
if (present(stat)) stat = 0
158-
159-
if (present(name)) then
160-
if (trim(name) == '') then
161-
if (present(stat)) stat = 1
162-
if (present(msg)) msg = "Array name cannot be empty."
163-
return
164-
end if
165-
t_arr%name = name
166-
else
167-
if (allocated(arrays)) then
168-
t_arr%name = "arr_"//to_string(size(arrays))//".npy"
169-
else
170-
t_arr%name = "arr_0.npy"
171-
end if
172-
end if
173-
174-
allocate(t_arr%values, source=array)
175-
if (.not. allocated(arrays)) then
176-
allocate(arrays(1))
177-
allocate(arrays(1)%array, source=t_arr)
178-
return
179-
end if
180-
181-
arr_size = size(arrays)
182-
do i = 1, arr_size
183-
if (arrays(i)%array%name == t_arr%name) then
184-
if (present(stat)) stat = 1
185-
if (present(msg)) msg = "Array with the same name '"//t_arr%name//"' already exists."
186-
return
187-
end if
188-
end do
189-
190-
allocate(tmp_arrays(arr_size + 1))
191-
tmp_arrays(:arr_size) = arrays
192-
allocate(tmp_arrays(arr_size + 1)%array, source=t_arr)
193-
call move_alloc(tmp_arrays, arrays)
194-
end
195-
#:endfor
196136
#:endfor
197137

198138
!> Version: experimental

test/io/test_np.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module test_np
22
use stdlib_array
33
use stdlib_filesystem, only : temp_dir, exists
44
use stdlib_kinds, only : int8, int16, int32, int64, sp, dp
5-
use stdlib_io_np, only : save_npy, load_npy, load_npz, add_array, save_npz
5+
use stdlib_io_np, only : save_npy, load_npy, load_npz, save_npz
66
use testdrive, only : new_unittest, unittest_type, error_type, check, test_failed
77
implicit none
88
private

0 commit comments

Comments
 (0)