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
467 lines (409 loc) · 15.9 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
! 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
! ------------------------------------------------------------------------------
module subroutine dt_insert_row(this, i, x, err)
! Arguments
class(data_table), intent(inout) :: this
integer(int32), intent(in) :: i
class(*), intent(in), dimension(:) :: x
class(errors), intent(inout), optional, target :: err
! 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
! Check the length of x
if (associated(this%m_table) .and. &
size(x) /= this%get_column_count()) &
then
write(errmsg, '(AI0AI0A)') "The number of items in the array " // &
"to insert ,", size(x), ", must match the number of " // &
"columns in the table ", this%get_column_count(), "."
call errmgr%report_error("dt_insert_row", trim(errmsg), &
FCORE_ARRAY_SIZE_ERROR)
return
end if
! Insert the array
call this%insert_rows(i, reshape(x, [1, size(x)]), err = errmgr)
end subroutine
! ------------------------------------------------------------------------------
module subroutine dt_insert_columns(this, cstart, x, err)
! Arguments
class(data_table), intent(inout) :: this
integer(int32), intent(in) :: cstart
class(*), intent(in), dimension(:,:) :: x
class(errors), intent(inout), optional, target :: err
! Local Variables
integer(int32) :: i, j, k, m, n, nnew, 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()
nnew = n + size(x, 2)
! Input Check
if (cstart < 1) then
call errmgr%report_error("dt_insert_columnss", &
"The insertion index must be at least 1.", &
FCORE_INDEX_OUT_OF_RANGE_ERROR)
return
end if
if (cstart > 1 + n) then
write(errmsg, '(AI0AI0A)') "The insertion index must not be " // &
"greater than ", n + 1, ", but was found to be ", cstart, "."
call errmgr%report_error("dt_insert_columns", trim(errmsg), &
FCORE_INDEX_OUT_OF_RANGE_ERROR)
return
end if
if (associated(this%m_table) .and. size(x, 1) /= m) then
write(errmsg, '(AI0AI0A)') "The input data set was expected " // &
"to have ", m, " rows, but was found to have ", &
size(x, 1), "."
call errmgr%report_error("dt_insert_columns", 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, cstart - 1
do i = 1, m
this%m_table(i, j) = copy(i, j)
end do
end do
k = cstart
do j = 1, size(x, 2)
do i = 1, m
call this%set(i, k, x(i, j), err = errmgr)
if (errmgr%has_error_occurred()) return
end do
k = k + 1
end do
do j = cstart, n
do i = 1, m
this%m_table(i, k) = copy(i, j)
end do
k = k + 1
end do
return
100 continue
! Deal with memory errors
call errmgr%report_error("dt_insert_columns", &
"Insufficient memory available.", FCORE_OUT_OF_MEMORY_ERROR)
return
end subroutine
! ------------------------------------------------------------------------------
! ------------------------------------------------------------------------------
! ------------------------------------------------------------------------------
! ------------------------------------------------------------------------------
end submodule