This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collections_data.f90
325 lines (282 loc) · 11 KB
/
collections_data.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
! collections_data.f90
submodule (collections) collections_data
use fcore_constants
contains
! ------------------------------------------------------------------------------
module subroutine dt_clear(this)
! Arguments
class(data_table), intent(inout) :: this
! Local Variables
integer(int32) :: i, j
! Quick Return
if (.not.associated(this%m_table)) return
! Process
do j = 1, size(this%m_table, 2)
do i = 1, size(this%m_table, 1)
if (associated(this%m_table(i,j)%item)) then
deallocate(this%m_table(i,j)%item)
end if
end do
end do
deallocate(this%m_table)
nullify(this%m_table)
end subroutine
! ------------------------------------------------------------------------------
module subroutine dt_final(this)
type(data_table), intent(inout) :: this
call this%clear()
end subroutine
! ------------------------------------------------------------------------------
pure module function dt_get_row_count(this) result(rst)
class(data_table), intent(in) :: this
integer(int32) :: rst
if (associated(this%m_table)) then
rst = size(this%m_table, 1)
else
rst = 0
end if
end function
! ------------------------------------------------------------------------------
pure module function dt_get_column_count(this) result(rst)
class(data_table), intent(in) :: this
integer(int32) :: rst
if (associated(this%m_table)) then
rst = size(this%m_table, 2)
else
rst = 0
end if
end function
! ------------------------------------------------------------------------------
module subroutine dt_initialize(this, m, n, err)
! Arguments
class(data_table), intent(inout) :: this
integer(int32), intent(in) :: m, n
class(errors), intent(inout), optional, target :: err
! Local Variables
integer(int32) :: flag
class(errors), pointer :: errmgr
type(errors), target :: deferr
! Set up error handling
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
! Check the inputs
if (m <= 0) then
call errmgr%report_error("dt_initialize", "The number of " // &
"rows must be a positive integer.", FCORE_INVALID_INPUT_ERROR)
return
end if
if (n <= 0) then
call errmgr%report_error("dt_initialize", "The number of " // &
"columns must be a positive integer.", &
FCORE_INVALID_INPUT_ERROR)
return
end if
! Clear the contents
call this%clear()
! Process
allocate(this%m_table(m, n), stat = flag)
if (flag /= 0) then
call errmgr%report_error("dt_initialize", "There is " // &
"insufficient memory available for this operation.", &
FCORE_OUT_OF_MEMORY_ERROR)
return
end if
end subroutine
! ------------------------------------------------------------------------------
module function dt_get(this, i, j, err) result(rst)
! Arguments
class(data_table), intent(in) :: this
integer(int32) :: i, j
class(errors), intent(inout), optional, target :: err
class(*), pointer :: rst
! Local Variables
class(errors), pointer :: errmgr
type(errors), target :: deferr
character(len = 256) :: errmsg
! Set up error handling
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
! Quick Return
if (.not.associated(this%m_table)) then
nullify(rst)
return
end if
! Bounds Checking
if (i <= 0 .or. i > size(this%m_table, 1)) then
write(errmsg, '(AI0AI0A)') "Row index outside the bounds " // &
"of the array. Found: ", i, ", but must lie between 1 and ", &
size(this%m_table, 1), "."
call errmgr%report_error("dt_get", trim(errmsg), &
FCORE_INDEX_OUT_OF_RANGE_ERROR)
return
end if
if (j <= 0 .or. j > size(this%m_table, 2)) then
write(errmsg, '(AI0AI0A)') "Column index outside the bounds " // &
"of the array. Found: ", j, ", but must lie between 1 and ", &
size(this%m_table, 2), "."
call errmgr%report_error("dt_get", trim(errmsg), &
FCORE_INDEX_OUT_OF_RANGE_ERROR)
return
end if
! Process
rst => this%m_table(i,j)%item
end function
! --------------------
module subroutine dt_set(this, i, j, x, err)
! Arguments
class(data_table), intent(inout) :: this
integer(int32), intent(in) :: i, j
class(*), intent(in) :: x
class(errors), intent(inout), optional, target :: err
! Local Variables
integer(int32) :: flag
class(errors), pointer :: errmgr
type(errors), target :: deferr
character(len = 256) :: errmsg
! Set up error handling
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
! Ensure we've got an array to work with
if (.not.associated(this%m_table)) then
call errmgr%report_error("dt_set", "The data table has not " // &
"yet been initialized.", FCORE_NULL_REFERENCE_ERROR)
return
end if
! Bounds Checking
if (i <= 0 .or. i > size(this%m_table, 1)) then
write(errmsg, '(AI0AI0A)') "Row index outside the bounds " // &
"of the array. Found: ", i, ", but must lie between 1 and ", &
size(this%m_table, 1), "."
call errmgr%report_error("dt_set", trim(errmsg), &
FCORE_INDEX_OUT_OF_RANGE_ERROR)
return
end if
if (j <= 0 .or. j > size(this%m_table, 2)) then
write(errmsg, '(AI0AI0A)') "Column index outside the bounds " // &
"of the array. Found: ", j, ", but must lie between 1 and ", &
size(this%m_table, 2), "."
call errmgr%report_error("dt_set", trim(errmsg), &
FCORE_INDEX_OUT_OF_RANGE_ERROR)
return
end if
! Clear the existing item, and store the new item
if (associated(this%m_table(i,j)%item)) then
deallocate(this%m_table(i,j)%item)
end if
allocate(this%m_table(i,j)%item, source = x, stat = flag)
if (flag /= 0) then
call errmgr%report_error("dt_set", &
"Insufficient memory available.", FCORE_OUT_OF_MEMORY_ERROR)
return
end if
end subroutine
! ------------------------------------------------------------------------------
module subroutine dt_insert_rows(this, rstart, x, err)
! Arguments
class(data_table), intent(inout) :: this
integer(int32), intent(in) :: rstart
class(*), intent(in), dimension(:,:) :: x
class(errors), intent(inout), optional, target :: err
! Local Variables
integer(int32) :: i, j, k, mnew, m, n, flag
type(container), allocatable, dimension(:,:) :: copy
class(errors), pointer :: errmgr
type(errors), target :: deferr
character(len = 256) :: errmsg
! Set up error handling
if (present(err)) then
errmgr => err
else
errmgr => deferr
end if
! Initialization
m = this%get_row_count()
n = this%get_column_count()
mnew = m + size(x, 1)
! Input Check
if (rstart < 1) then
call errmgr%report_error("dt_insert_rows", &
"The insertion index must be at least 1.", &
FCORE_INDEX_OUT_OF_RANGE_ERROR)
return
end if
if (rstart > 1 + m) then
write(errmsg, '(AI0AI0A)') "The insertion index must not be " // &
"greater than ", m + 1, ", but was found to be ", rstart, "."
call errmgr%report_error("dt_insert_rows", trim(errmsg), &
FCORE_INDEX_OUT_OF_RANGE_ERROR)
return
end if
if (associated(this%m_table) .and. size(x, 2) /= n) then
write(errmsg, '(AI0AI0A)') "The input data set was expected " // &
"to have ", n, " columns, but was found to have ", &
size(x, 2), "."
call errmgr%report_error("dt_insert_rows", trim(errmsg), &
FCORE_ARRAY_SIZE_ERROR)
return
end if
! If the array is not allocated, allocate and store as the input array
! will define the table structure
if (.not.associated(this%m_table)) then
call this%initialize(size(x, 1), size(x, 2), err = errmgr)
if (errmgr%has_error_occurred()) return
do j = 1, size(x, 2)
do i = 1, size(x, 1)
call this%set(i, j, x(i,j), err = errmgr)
if (errmgr%has_error_occurred()) return
end do
end do
return
end if
! If we're here, there was already a properly allocated matrix, and the
! size of X is OK. Start by copying m_table, and then reallocate
! m_table to allow fitting of the new data
allocate(copy(m, n), stat = flag)
if (flag /= 0) go to 100
copy = this%m_table
deallocate(this%m_table)
allocate(this%m_table(mnew, n), stat = flag)
if (flag /= 0) then
! Put copy back to m_table, and then handle the error
this%m_table = copy
go to 100
end if
! Copy back the contents into m_table
do j = 1, n
do i = 1, rstart - 1
this%m_table(i,j) = copy(i,j)
end do
k = rstart
do i = 1, size(x, 1)
call this%set(k, j, x(i,j), err = errmgr)
if (errmgr%has_error_occurred()) return
k = k + 1
end do
do i = rstart, m
this%m_table(k,j) = copy(i,j)
k = k + 1
end do
end do
return
100 continue
! Deal with memory errors
call errmgr%report_error("dt_insert_rows", &
"Insufficient memory available.", FCORE_OUT_OF_MEMORY_ERROR)
return
end subroutine
! ------------------------------------------------------------------------------
! ------------------------------------------------------------------------------
! ------------------------------------------------------------------------------
! ------------------------------------------------------------------------------
! ------------------------------------------------------------------------------
! ------------------------------------------------------------------------------
end submodule