1 |
! vector_class.F90 |
2 |
!! Module Vector_class |
3 |
!! Fortran 95 Vector class module. Similar to java.util vector class. |
4 |
!! |
5 |
!! The Vector class implements a growable array of objects. Like an array, |
6 |
!! it contains components that can be accessed using an integer index. |
7 |
!! However, the size of a Vector can grow as needed to accommodate |
8 |
!! adding and removing items after the Vector has been created. |
9 |
!! Each vector tries to optimize storage management by maintaining a |
10 |
!! capacity and a capacityIncrement. The capacity is always at least as |
11 |
!! large as the vector size; |
12 |
!! it is usually larger because as components are added to the vector, |
13 |
!! the vector's storage increases in chunks the size of capacityIncrement. |
14 |
!! An application can increase the capacity of a vector before inserting a |
15 |
!! large number of components; this reduces the amount of incremental |
16 |
!! reallocation. |
17 |
!! |
18 |
!! |
19 |
!! @author J. Daniel Gezelter |
20 |
!! @author Charles F. Vardeman II |
21 |
!! @author Matthew Meineke |
22 |
!! @version $Id: vector_class.F90,v 1.1.1.1 2004-09-24 04:16:37 gezelter Exp $, $Date: 2004-09-24 04:16:37 $, $Name: not supported by cvs2svn $, $Revision: 1.1.1.1 $ |
23 |
|
24 |
module Vector_class |
25 |
|
26 |
implicit NONE |
27 |
PRIVATE |
28 |
|
29 |
public :: initialize |
30 |
public :: getSize |
31 |
public :: getElementAt |
32 |
public :: getPropertyListSize |
33 |
public :: getPropertyNameAt |
34 |
public :: addElement |
35 |
public :: setElementProperty |
36 |
public :: getElementProperty |
37 |
public :: getMatchingElementList |
38 |
public :: getFirstMatchingElement |
39 |
|
40 |
integer, parameter :: logical_data_type = 1 |
41 |
integer, parameter :: integer_data_type = 2 |
42 |
integer, parameter :: real_data_type = 3 |
43 |
|
44 |
!! |
45 |
type, public :: Vector |
46 |
PRIVATE |
47 |
integer :: initialCapacity = 10 |
48 |
integer :: capacityIncrement = 0 |
49 |
integer :: elementCount = 0 |
50 |
|
51 |
integer :: initialProperties = 5 |
52 |
integer :: PropertyIncrement = 0 |
53 |
integer :: propertyCount = 0 |
54 |
|
55 |
integer, pointer :: ElementData(:) => null() |
56 |
character(len=100), pointer :: PropertyDescriptions(:) => null() |
57 |
integer, pointer :: PropertyDataType(:) => null() |
58 |
real(kind = 8), pointer :: realElementProperties(:,:) => null() |
59 |
integer, pointer :: integerElementProperties(:,:) => null() |
60 |
logical, pointer :: logicalElementProperties(:,:) => null() |
61 |
end type Vector |
62 |
|
63 |
!! Initialize vector |
64 |
interface initialize |
65 |
module procedure initialize_0i |
66 |
module procedure initialize_1i |
67 |
module procedure initialize_2i |
68 |
module procedure initialize_3i |
69 |
module procedure initialize_4i |
70 |
end interface |
71 |
|
72 |
interface setElementProperty |
73 |
module procedure setElementPropertyReal |
74 |
module procedure setElementPropertyInt |
75 |
module procedure setElementPropertyLogical |
76 |
end interface |
77 |
|
78 |
interface getElementProperty |
79 |
module procedure getElementPropertyReal |
80 |
module procedure getElementPropertyInt |
81 |
module procedure getElementPropertyLogical |
82 |
end interface |
83 |
|
84 |
interface getMatchingElementList |
85 |
module procedure getMatchingElementList1i |
86 |
module procedure getMatchingElementList2i |
87 |
module procedure getMatchingElementList1l |
88 |
module procedure getMatchingElementList2l |
89 |
end interface |
90 |
|
91 |
interface getFirstMatchingElement |
92 |
module procedure getFirstMatchingElement1i |
93 |
module procedure getFirstMatchingElement2i |
94 |
module procedure getFirstMatchingElement1l |
95 |
module procedure getFirstMatchingElement2l |
96 |
end interface |
97 |
contains |
98 |
|
99 |
function getSize(this) result (ne) |
100 |
type(Vector), pointer :: this |
101 |
integer :: ne |
102 |
ne = this%elementCount |
103 |
end function getSize |
104 |
|
105 |
function getElementAt(this, loc) result (id) |
106 |
type(Vector), pointer :: this |
107 |
integer, intent(in) :: loc |
108 |
integer :: id |
109 |
id = this%ElementData(loc) |
110 |
end function getElementAt |
111 |
|
112 |
function getPropertyListSize(this) result (np) |
113 |
type(Vector), pointer :: this |
114 |
integer :: np |
115 |
np = this%propertyCount |
116 |
end function getPropertyListSize |
117 |
|
118 |
function getPropertyNameAt(this, loc) result (pn) |
119 |
type(Vector), pointer :: this |
120 |
integer, intent(in) :: loc |
121 |
character(len=len(this%PropertyDescriptions)) :: pn |
122 |
pn = this%PropertyDescriptions(loc) |
123 |
end function getPropertyNameAt |
124 |
|
125 |
function getFirstMatchingElement1i(this, MatchName, MatchValue) result (id) |
126 |
type(Vector), pointer :: this |
127 |
character(len=*), intent(in) :: MatchName |
128 |
integer, intent(in) :: MatchValue |
129 |
integer :: id |
130 |
integer :: i, j |
131 |
|
132 |
id = 0 |
133 |
|
134 |
do i = 1, this%propertyCount |
135 |
if (this%PropertyDescriptions(i) == MatchName) then |
136 |
do j = 1, this%elementCount |
137 |
if (this%integerElementProperties(j, i) == MatchValue) then |
138 |
id = j |
139 |
return |
140 |
endif |
141 |
enddo |
142 |
endif |
143 |
enddo |
144 |
return |
145 |
end function getFirstMatchingElement1i |
146 |
|
147 |
function getFirstMatchingElement2i(this, MatchName1, MatchValue1, & |
148 |
MatchName2, MatchValue2) result (id) |
149 |
type(Vector), pointer :: this |
150 |
character(len=*), intent(in) :: MatchName1, MatchName2 |
151 |
integer, intent(in) :: MatchValue1, MatchValue2 |
152 |
integer :: id |
153 |
integer :: i, j, MatchID1, MatchID2 |
154 |
logical :: found1 = .false. |
155 |
logical :: found2 = .false. |
156 |
|
157 |
id = 0 |
158 |
! first figure out which properties we are using to do the match: |
159 |
|
160 |
do i = 1, this%propertyCount |
161 |
if (this%PropertyDescriptions(i) == MatchName1) then |
162 |
MatchID1 = i |
163 |
found1 = .true. |
164 |
endif |
165 |
if (this%PropertyDescriptions(i) == MatchName2) then |
166 |
MatchID2 = i |
167 |
found2 = .true. |
168 |
endif |
169 |
|
170 |
if (found1.and.found2) then |
171 |
do j = 1, this%elementCount |
172 |
if ((this%integerElementProperties(j, MatchID1) == MatchValue1) & |
173 |
.and. & |
174 |
(this%integerElementProperties(j, MatchID2) ==MatchValue2)) & |
175 |
then |
176 |
id = j |
177 |
return |
178 |
endif |
179 |
enddo |
180 |
endif |
181 |
end do |
182 |
|
183 |
return |
184 |
end function getFirstMatchingElement2i |
185 |
|
186 |
function getFirstMatchingElement1l(this, MatchName, MatchValue) result (id) |
187 |
type(Vector), pointer :: this |
188 |
character(len=*), intent(in) :: MatchName |
189 |
logical, intent(in) :: MatchValue |
190 |
integer :: id |
191 |
integer :: i, j |
192 |
|
193 |
id = 0 |
194 |
|
195 |
do i = 1, this%propertyCount |
196 |
if (this%PropertyDescriptions(i) == MatchName) then |
197 |
do j = 1, this%elementCount |
198 |
if (this%logicalElementProperties(j, i) .eqv. MatchValue) then |
199 |
id = j |
200 |
return |
201 |
endif |
202 |
enddo |
203 |
endif |
204 |
enddo |
205 |
return |
206 |
end function getFirstMatchingElement1l |
207 |
|
208 |
function getFirstMatchingElement2l(this, MatchName1, MatchValue1, & |
209 |
MatchName2, MatchValue2) result (id) |
210 |
type(Vector), pointer :: this |
211 |
character(len=*), intent(in) :: MatchName1, MatchName2 |
212 |
logical, intent(in) :: MatchValue1, MatchValue2 |
213 |
integer :: id |
214 |
integer :: i, j, MatchID1, MatchID2 |
215 |
logical :: found1 = .false. |
216 |
logical :: found2 = .false. |
217 |
|
218 |
id = 0 |
219 |
! first figure out which properties we are using to do the match: |
220 |
|
221 |
do i = 1, this%propertyCount |
222 |
if (this%PropertyDescriptions(i) == MatchName1) then |
223 |
MatchID1 = i |
224 |
found1 = .true. |
225 |
endif |
226 |
if (this%PropertyDescriptions(i) == MatchName2) then |
227 |
MatchID2 = i |
228 |
found2 = .true. |
229 |
endif |
230 |
|
231 |
if (found1.and.found2) then |
232 |
do j = 1, this%elementCount |
233 |
if ((this%logicalElementProperties(j, MatchID1).eqv.MatchValue1) & |
234 |
.and. & |
235 |
(this%logicalElementProperties(j, MatchID2).eqv.MatchValue2)) & |
236 |
then |
237 |
id = j |
238 |
return |
239 |
endif |
240 |
enddo |
241 |
endif |
242 |
end do |
243 |
|
244 |
return |
245 |
end function getFirstMatchingElement2l |
246 |
|
247 |
subroutine getMatchingElementList1i(this, MatchName, MatchValue, & |
248 |
nMatches, MatchList) |
249 |
type(Vector), pointer :: this |
250 |
character(len=*), intent(in) :: MatchName |
251 |
integer, intent(in) :: MatchValue |
252 |
integer, intent(out) :: nMatches |
253 |
integer, pointer :: MatchList(:) |
254 |
integer :: i |
255 |
|
256 |
! first figure out which property we are using to do the match: |
257 |
|
258 |
do i = 1, this%propertyCount |
259 |
if (this%PropertyDescriptions(i) == MatchName) then |
260 |
call getAllMatches1i(this, i, MatchValue, nMatches, MatchList) |
261 |
return |
262 |
endif |
263 |
enddo |
264 |
return |
265 |
end subroutine getMatchingElementList1i |
266 |
|
267 |
subroutine getMatchingElementList2i(this, MatchName1, MatchValue1, & |
268 |
MatchName2, MatchValue2, nMatches, MatchList) |
269 |
type(Vector), pointer :: this |
270 |
character(len=*), intent(in) :: MatchName1, MatchName2 |
271 |
integer, intent(in) :: MatchValue1, MatchValue2 |
272 |
integer, intent(out) :: nMatches |
273 |
integer, pointer :: MatchList(:) |
274 |
integer :: i, MatchID1, MatchID2 |
275 |
logical :: found1 = .false. |
276 |
logical :: found2 = .false. |
277 |
|
278 |
! first figure out which properties we are using to do the match: |
279 |
|
280 |
do i = 1, this%propertyCount |
281 |
if (this%PropertyDescriptions(i) == MatchName1) then |
282 |
MatchID1 = i |
283 |
found1 = .true. |
284 |
endif |
285 |
if (this%PropertyDescriptions(i) == MatchName2) then |
286 |
MatchID2 = i |
287 |
found2 = .true. |
288 |
endif |
289 |
|
290 |
if (found1.and.found2) then |
291 |
call getAllMatches2i(this, MatchID1, MatchValue1, & |
292 |
MatchID2, MatchValue2, nMatches, MatchList) |
293 |
return |
294 |
endif |
295 |
enddo |
296 |
return |
297 |
end subroutine getMatchingElementList2i |
298 |
|
299 |
subroutine getMatchingElementList1l(this, MatchName, MatchValue, & |
300 |
nMatches, MatchList) |
301 |
type(Vector), pointer :: this |
302 |
character(len=*), intent(in) :: MatchName |
303 |
logical, intent(in) :: MatchValue |
304 |
integer, intent(out) :: nMatches |
305 |
integer, pointer :: MatchList(:) |
306 |
integer :: i |
307 |
|
308 |
! first figure out which property we are using to do the match: |
309 |
|
310 |
do i = 1, this%propertyCount |
311 |
if (this%PropertyDescriptions(i) == MatchName) then |
312 |
call getAllMatches1l(this, i, MatchValue, nMatches, MatchList) |
313 |
return |
314 |
endif |
315 |
enddo |
316 |
return |
317 |
end subroutine getMatchingElementList1l |
318 |
|
319 |
subroutine getMatchingElementList2l(this, MatchName1, MatchValue1, & |
320 |
MatchName2, MatchValue2, nMatches, MatchList) |
321 |
type(Vector), pointer :: this |
322 |
character(len=*), intent(in) :: MatchName1, MatchName2 |
323 |
logical, intent(in) :: MatchValue1, MatchValue2 |
324 |
integer, intent(out) :: nMatches |
325 |
integer, pointer :: MatchList(:) |
326 |
integer :: i, MatchID1, MatchID2 |
327 |
logical :: found1 = .false. |
328 |
logical :: found2 = .false. |
329 |
|
330 |
! first figure out which properties we are using to do the match: |
331 |
|
332 |
do i = 1, this%propertyCount |
333 |
if (this%PropertyDescriptions(i) == MatchName1) then |
334 |
MatchID1 = i |
335 |
found1 = .true. |
336 |
endif |
337 |
if (this%PropertyDescriptions(i) == MatchName2) then |
338 |
MatchID2 = i |
339 |
found2 = .true. |
340 |
endif |
341 |
|
342 |
if (found1.and.found2) then |
343 |
call getAllMatches2l(this, MatchID1, MatchValue1, & |
344 |
MatchID2, MatchValue2, nMatches, MatchList) |
345 |
return |
346 |
endif |
347 |
enddo |
348 |
return |
349 |
end subroutine getMatchingElementList2l |
350 |
|
351 |
subroutine getAllMatches1i(this, MatchID, MatchValue, nMatches, MatchList) |
352 |
type(Vector), pointer :: this |
353 |
integer, intent(in) :: MatchID |
354 |
integer, intent(in) :: MatchValue |
355 |
integer, pointer :: MatchList(:) |
356 |
integer, allocatable :: MatchListTemp(:) |
357 |
integer, intent(out) :: nMatches |
358 |
integer :: error, i |
359 |
|
360 |
if(associated(MatchList)) deallocate(MatchList) |
361 |
MatchList => null() |
362 |
|
363 |
allocate(MatchListTemp(this%elementCount), stat=error) |
364 |
if(error .ne. 0) write(*,*) 'Could not allocate MatchListTemp!' |
365 |
|
366 |
nMatches = 0 |
367 |
|
368 |
do i = 1, this%elementCount |
369 |
if (this%integerElementProperties(i, MatchID) == MatchValue) then |
370 |
nMatches = nMatches + 1 |
371 |
MatchListTemp(nMatches) = i |
372 |
endif |
373 |
enddo |
374 |
|
375 |
|
376 |
if (nMatches .ne. 0) then |
377 |
allocate(MatchList(nMatches), stat=error) |
378 |
if (error.ne.0) write(*, *) 'Could not allocate MatchList!' |
379 |
do i = 1, nMatches |
380 |
MatchList(i) = MatchListTemp(i) |
381 |
enddo |
382 |
endif |
383 |
|
384 |
deallocate(MatchListTemp) |
385 |
|
386 |
|
387 |
end subroutine getAllMatches1i |
388 |
|
389 |
subroutine getAllMatches2i(this, MatchID1, MatchValue1, & |
390 |
MatchID2, MatchValue2, nMatches, MatchList) |
391 |
type(Vector), pointer :: this |
392 |
integer, intent(in) :: MatchID1, MatchID2 |
393 |
integer, intent(in) :: MatchValue1, MatchValue2 |
394 |
integer, pointer :: MatchList(:) |
395 |
integer, allocatable :: MatchListTemp(:) |
396 |
integer, intent(out) :: nMatches |
397 |
integer :: error, i |
398 |
|
399 |
if(associated(MatchList)) deallocate(MatchList) |
400 |
MatchList => null() |
401 |
|
402 |
allocate(MatchListTemp(this%elementCount), stat=error) |
403 |
if(error .ne. 0) write(*,*) 'Could not allocate MatchListTemp!' |
404 |
|
405 |
nMatches = 0 |
406 |
|
407 |
do i = 1, this%elementCount |
408 |
if ((this%integerElementProperties(i, MatchID1) == MatchValue1) .and. & |
409 |
(this%integerElementProperties(i, MatchID2) == MatchValue2)) then |
410 |
nMatches = nMatches + 1 |
411 |
MatchListTemp(nMatches) = i |
412 |
endif |
413 |
enddo |
414 |
|
415 |
if (nMatches .ne. 0) then |
416 |
allocate(MatchList(nMatches), stat=error) |
417 |
if (error.ne.0) write(*, *) 'Could not allocate MatchList!' |
418 |
do i = 1, nMatches |
419 |
MatchList(i) = MatchListTemp(i) |
420 |
enddo |
421 |
endif |
422 |
|
423 |
deallocate(MatchListTemp) |
424 |
|
425 |
end subroutine getAllMatches2i |
426 |
|
427 |
subroutine getAllMatches1l(this, MatchID, MatchValue, nMatches, MatchList) |
428 |
type(Vector), pointer :: this |
429 |
integer, intent(in) :: MatchID |
430 |
logical, intent(in) :: MatchValue |
431 |
integer, pointer :: MatchList(:) |
432 |
integer, allocatable :: MatchListTemp(:) |
433 |
integer, intent(out) :: nMatches |
434 |
integer :: error, i |
435 |
|
436 |
if(associated(MatchList)) deallocate(MatchList) |
437 |
MatchList => null() |
438 |
|
439 |
allocate(MatchListTemp(this%elementCount), stat=error) |
440 |
if(error .ne. 0) write(*,*) 'Could not allocate MatchListTemp!' |
441 |
|
442 |
nMatches = 0 |
443 |
|
444 |
do i = 1, this%elementCount |
445 |
if (this%logicalElementProperties(i, MatchID).eqv.MatchValue) then |
446 |
nMatches = nMatches + 1 |
447 |
MatchListTemp(nMatches) = i |
448 |
endif |
449 |
enddo |
450 |
|
451 |
if (nMatches .ne. 0) then |
452 |
allocate(MatchList(nMatches), stat=error) |
453 |
if (error.ne.0) write(*, *) 'Could not allocate MatchList!' |
454 |
do i = 1, nMatches |
455 |
MatchList(i) = MatchListTemp(i) |
456 |
enddo |
457 |
endif |
458 |
|
459 |
deallocate(MatchListTemp) |
460 |
|
461 |
end subroutine getAllMatches1l |
462 |
|
463 |
subroutine getAllMatches2l(this, MatchID1, MatchValue1, & |
464 |
MatchID2, MatchValue2, nMatches, MatchList) |
465 |
type(Vector), pointer :: this |
466 |
integer, intent(in) :: MatchID1, MatchID2 |
467 |
logical, intent(in) :: MatchValue1, MatchValue2 |
468 |
integer, pointer :: MatchList(:) |
469 |
integer, allocatable :: MatchListTemp(:) |
470 |
integer, intent(out) :: nMatches |
471 |
integer :: error, i |
472 |
|
473 |
if(associated(MatchList)) deallocate(MatchList) |
474 |
MatchList => null() |
475 |
|
476 |
allocate(MatchListTemp(this%elementCount), stat=error) |
477 |
if(error .ne. 0) write(*,*) 'Could not allocate MatchListTemp!' |
478 |
|
479 |
nMatches = 0 |
480 |
|
481 |
do i = 1, this%elementCount |
482 |
if ((this%logicalElementProperties(i, MatchID1).eqv.MatchValue1) .and. & |
483 |
(this%logicalElementProperties(i, MatchID2).eqv.MatchValue2)) then |
484 |
nMatches = nMatches + 1 |
485 |
MatchListTemp(nMatches) = i |
486 |
endif |
487 |
enddo |
488 |
|
489 |
if (nMatches .ne. 0) then |
490 |
allocate(MatchList(nMatches), stat=error) |
491 |
if (error.ne.0) write(*, *) 'Could not allocate MatchList!' |
492 |
do i = 1, nMatches |
493 |
MatchList(i) = MatchListTemp(i) |
494 |
enddo |
495 |
endif |
496 |
|
497 |
deallocate(MatchListTemp) |
498 |
|
499 |
end subroutine getAllMatches2l |
500 |
|
501 |
|
502 |
subroutine getElementPropertyReal(this, id, PropName, pv) |
503 |
type(Vector), pointer :: this |
504 |
integer :: id, whichprop |
505 |
character(len=*) :: PropName |
506 |
real( kind = 8 ) :: pv |
507 |
|
508 |
whichprop = getPropertyIndex(this, PropName) |
509 |
if (whichprop .eq. 0 ) then |
510 |
write(*,*) 'unknown property: ', PropName |
511 |
pv = 0.0 |
512 |
else |
513 |
if (this%PropertyDataType(whichprop) .ne. real_data_type) then |
514 |
write(*,*) 'Property: ', PropName, " is not real data type." |
515 |
pv = 0.0 |
516 |
else |
517 |
pv = this%realElementProperties(id, whichprop) |
518 |
endif |
519 |
endif |
520 |
end subroutine getElementPropertyReal |
521 |
|
522 |
subroutine getElementPropertyInt(this, id, PropName, pv) |
523 |
type(Vector), pointer :: this |
524 |
integer :: id, whichprop |
525 |
character(len=*) :: PropName |
526 |
integer :: pv |
527 |
|
528 |
whichprop = getPropertyIndex(this, PropName) |
529 |
if (whichprop .eq. 0 ) then |
530 |
write(*,*) 'unknown property! ', PropName |
531 |
pv = 0 |
532 |
else |
533 |
if (this%PropertyDataType(whichprop) .ne. integer_data_type) then |
534 |
write(*,*) 'Property! ', PropName, " is not integer data type." |
535 |
pv = 0 |
536 |
else |
537 |
pv = this%integerElementProperties(id, whichprop) |
538 |
endif |
539 |
endif |
540 |
end subroutine getElementPropertyInt |
541 |
|
542 |
subroutine getElementPropertyLogical(this, id, PropName, pv) |
543 |
type(Vector), pointer :: this |
544 |
integer :: id, whichprop |
545 |
character(len=*) :: PropName |
546 |
logical :: pv |
547 |
|
548 |
whichprop = getPropertyIndex(this, PropName) |
549 |
if (whichprop .eq. 0 ) then |
550 |
write(*,*) 'unknown property! ', PropName |
551 |
pv = .false. |
552 |
else |
553 |
if (this%PropertyDataType(whichprop) .ne. logical_data_type) then |
554 |
write(*,*) 'Property! ', PropName, " is not logical data type." |
555 |
pv = .false. |
556 |
else |
557 |
pv = this%logicalElementProperties(id, whichprop) |
558 |
endif |
559 |
endif |
560 |
end subroutine getElementPropertyLogical |
561 |
|
562 |
function getPropertyIndex(this, PropName) result (id) |
563 |
type(Vector), pointer :: this |
564 |
integer :: id, i |
565 |
character(len=*) :: PropName |
566 |
|
567 |
do i = 1, this%propertyCount |
568 |
if (this%PropertyDescriptions(i) == PropName) then |
569 |
id = i |
570 |
return |
571 |
endif |
572 |
enddo |
573 |
id = 0 |
574 |
end function getPropertyIndex |
575 |
|
576 |
subroutine ensureCapacityHelper(this, minCapacity, minPropCap) |
577 |
type(Vector), pointer :: this, that |
578 |
integer, intent(in) :: minCapacity, minPropCap |
579 |
integer :: oldCapacity, oldPropCap |
580 |
integer :: newCapacity, newPropCap |
581 |
logical :: resizeFlag |
582 |
|
583 |
resizeFlag = .false. |
584 |
|
585 |
! first time: allocate a new vector with default size |
586 |
|
587 |
if (.not. associated(this)) then |
588 |
this => initialize() |
589 |
endif |
590 |
|
591 |
oldCapacity = size(this%ElementData) |
592 |
oldPropCap = size(this%PropertyDescriptions) |
593 |
|
594 |
if (minCapacity > oldCapacity) then |
595 |
if (this%capacityIncrement .gt. 0) then |
596 |
newCapacity = oldCapacity + this%capacityIncrement |
597 |
else |
598 |
newCapacity = oldCapacity * 2 |
599 |
endif |
600 |
if (newCapacity .lt. minCapacity) then |
601 |
newCapacity = minCapacity |
602 |
endif |
603 |
resizeFlag = .true. |
604 |
else |
605 |
newCapacity = oldCapacity |
606 |
endif |
607 |
|
608 |
!!! newCapacity is not set..... |
609 |
if (minPropCap > oldPropCap) then |
610 |
if (this%PropertyIncrement .gt. 0) then |
611 |
newPropCap = oldPropCap + this%PropertyIncrement |
612 |
else |
613 |
newPropCap = oldPropCap * 2 |
614 |
endif |
615 |
if (newPropCap .lt. minPropCap) then |
616 |
newPropCap = minPropCap |
617 |
endif |
618 |
resizeFlag = .true. |
619 |
else |
620 |
newPropCap = oldPropCap |
621 |
endif |
622 |
|
623 |
if (resizeFlag) then |
624 |
that => initialize(newCapacity, newPropCap, & |
625 |
this%capacityIncrement, this%PropertyIncrement) |
626 |
call copyAllData(this, that) |
627 |
deallocate(this) |
628 |
this => that |
629 |
endif |
630 |
end subroutine ensureCapacityHelper |
631 |
|
632 |
subroutine copyAllData(v1, v2) |
633 |
type(Vector), pointer :: v1 |
634 |
type(Vector), pointer :: v2 |
635 |
integer :: i, j |
636 |
|
637 |
do i = 1, v1%elementCount |
638 |
v2%elementData(i) = v1%elementData(i) |
639 |
do j = 1, v1%propertyCount |
640 |
|
641 |
if (v1%PropertyDataType(j) .eq. integer_data_type) & |
642 |
v2%integerElementProperties(i,j) = & |
643 |
v1%integerElementProperties(i,j) |
644 |
|
645 |
if (v1%PropertyDataType(j) .eq. real_data_type) & |
646 |
v2%realElementProperties(i,j) = v1%realElementProperties(i,j) |
647 |
|
648 |
if (v1%PropertyDataType(j) .eq. logical_data_type) & |
649 |
v2%logicalElementProperties(i,j) = & |
650 |
v1%logicalElementProperties(i,j) |
651 |
enddo |
652 |
enddo |
653 |
|
654 |
do j = 1, v1%propertyCount |
655 |
v2%PropertyDescriptions(j) = v1%PropertyDescriptions(j) |
656 |
v2%PropertyDataType(j) = v1%PropertyDataType(j) |
657 |
enddo |
658 |
|
659 |
v2%elementCount = v1%elementCount |
660 |
v2%propertyCount = v1%propertyCount |
661 |
|
662 |
return |
663 |
end subroutine copyAllData |
664 |
|
665 |
function addElement(this) result (id) |
666 |
type(Vector), pointer :: this |
667 |
integer :: id |
668 |
integer :: error |
669 |
|
670 |
if (.not. associated(this)) then |
671 |
call ensureCapacityHelper(this,1,0) |
672 |
else |
673 |
call ensureCapacityHelper(this, this%elementCount + 1, this%PropertyCount) |
674 |
end if |
675 |
|
676 |
this%elementCount = this%elementCount + 1 |
677 |
|
678 |
!! We never use this and we set the entire array to the same value |
679 |
this%elementData = this%elementCount |
680 |
id = this%elementCount |
681 |
end function addElement |
682 |
|
683 |
recursive subroutine setElementPropertyReal(this, id, PropName, PropValue) |
684 |
type(Vector), pointer :: this |
685 |
integer :: id, i |
686 |
character(len=*), intent(in) :: PropName |
687 |
real( kind = 8 ), intent(in) :: PropValue |
688 |
logical :: foundit |
689 |
|
690 |
foundit = .false. |
691 |
|
692 |
! first make sure that the PropName isn't in the list of known properties: |
693 |
|
694 |
do i = 1, this%propertyCount |
695 |
if (PropName == this%PropertyDescriptions(i)) then |
696 |
foundit = .true. |
697 |
this%realElementProperties(id,i) = PropValue |
698 |
endif |
699 |
enddo |
700 |
|
701 |
if (.not.foundit) then |
702 |
call addPropertyToVector(this, PropName, real_data_type) |
703 |
call setElementPropertyReal(this, id, PropName, PropValue) |
704 |
endif |
705 |
end subroutine setElementPropertyReal |
706 |
|
707 |
recursive subroutine setElementPropertyInt(this, id, PropName, PropValue) |
708 |
type(Vector), pointer :: this |
709 |
integer :: id, i |
710 |
character(len=*), intent(in) :: PropName |
711 |
integer, intent(in) :: PropValue |
712 |
logical :: foundit |
713 |
|
714 |
foundit = .false. |
715 |
! first make sure that the PropName isn't in the list of known properties: |
716 |
do i = 1, this%propertyCount |
717 |
if (PropName == this%PropertyDescriptions(i)) then |
718 |
foundit = .true. |
719 |
this%integerElementProperties(id,i) = PropValue |
720 |
endif |
721 |
enddo |
722 |
|
723 |
if (.not.foundit) then |
724 |
call addPropertyToVector(this, PropName, integer_data_type) |
725 |
call setElementPropertyInt(this, id, PropName, PropValue) |
726 |
endif |
727 |
end subroutine setElementPropertyInt |
728 |
|
729 |
recursive subroutine setElementPropertyLogical(this, id, PropName, PropValue) |
730 |
type(Vector), pointer :: this |
731 |
integer :: id, i |
732 |
character(len=*), intent(in) :: PropName |
733 |
logical, intent(in) :: PropValue |
734 |
logical :: foundit |
735 |
|
736 |
foundit = .false. |
737 |
! first make sure that the PropName isn't in the list of known properties: |
738 |
do i = 1, this%propertyCount |
739 |
if (PropName == this%PropertyDescriptions(i)) then |
740 |
foundit = .true. |
741 |
this%logicalElementProperties(id,i) = PropValue |
742 |
endif |
743 |
enddo |
744 |
|
745 |
if (.not.foundit) then |
746 |
call addPropertyToVector(this, PropName, logical_data_type) |
747 |
call setElementPropertyLogical(this, id, PropName, PropValue) |
748 |
endif |
749 |
end subroutine setElementPropertyLogical |
750 |
|
751 |
subroutine addPropertyToVector(this, PropName, data_type) |
752 |
type(Vector), pointer :: this |
753 |
character(len=*), intent(in) :: PropName |
754 |
integer data_type |
755 |
|
756 |
call ensureCapacityHelper(this, this%elementCount, this%propertyCount + 1) |
757 |
this%propertyCount = this%propertyCount + 1 |
758 |
this%PropertyDescriptions(this%propertyCount) = PropName |
759 |
this%PropertyDataType(this%propertyCount) = data_type |
760 |
end subroutine addPropertyToVector |
761 |
|
762 |
function initialize_0i() result(this) |
763 |
type(Vector), pointer :: this |
764 |
this => initialize_2i(10, 5) |
765 |
end function initialize_0i |
766 |
|
767 |
function initialize_1i(nprop) result(this) |
768 |
integer, intent(in) :: nprop |
769 |
type(Vector), pointer :: this |
770 |
this => initialize_2i(10, nprop) |
771 |
end function initialize_1i |
772 |
|
773 |
function initialize_2i(cap, nprop) result(this) |
774 |
integer, intent(in) :: cap, nprop |
775 |
type(Vector), pointer :: this |
776 |
this => initialize_4i(cap, nprop, 0, 0) |
777 |
end function initialize_2i |
778 |
|
779 |
function initialize_3i(cap, nprop, capinc) result(this) |
780 |
integer, intent(in) :: cap, nprop, capinc |
781 |
type(Vector), pointer :: this |
782 |
this => initialize_4i(cap, nprop, capinc, 0) |
783 |
end function initialize_3i |
784 |
|
785 |
function initialize_4i(cap, nprop, capinc, propinc) result(this) |
786 |
integer, intent(in) :: cap, nprop, capinc, propinc |
787 |
integer :: error |
788 |
type(Vector), pointer :: this |
789 |
|
790 |
nullify(this) |
791 |
|
792 |
if (cap < 0) then |
793 |
write(*,*) 'Bogus Capacity:', cap |
794 |
return |
795 |
endif |
796 |
if (nprop < 0) then |
797 |
write(*,*) 'Bogus Number of Properties:', nprop |
798 |
return |
799 |
endif |
800 |
|
801 |
allocate(this,stat=error) |
802 |
if ( error /= 0 ) then |
803 |
write(*,*) 'Could not allocate Vector!' |
804 |
return |
805 |
end if |
806 |
|
807 |
this%initialCapacity = cap |
808 |
this%initialProperties = nprop |
809 |
this%capacityIncrement = capinc |
810 |
this%propertyIncrement = propinc |
811 |
|
812 |
allocate(this%elementData(this%initialCapacity), stat=error) |
813 |
if(error /= 0) write(*,*) 'Could not allocate elementData!' |
814 |
|
815 |
|
816 |
allocate(this%PropertyDescriptions(this%initialProperties), & |
817 |
stat=error) |
818 |
if(error /= 0) write(*,*) 'Could not allocate PropertyDescriptions!' |
819 |
|
820 |
allocate(this%PropertyDataType(this%initialProperties), & |
821 |
stat=error) |
822 |
if(error /= 0) write(*,*) 'Could not allocate PropertyDataType!' |
823 |
|
824 |
allocate(this%integerElementProperties(this%initialCapacity, & |
825 |
this%initialProperties), stat=error) |
826 |
if(error /= 0) write(*,*) 'Could not allocate integerElementProperties!' |
827 |
|
828 |
allocate(this%realElementProperties(this%initialCapacity, & |
829 |
this%initialProperties), stat=error) |
830 |
if(error /= 0) write(*,*) 'Could not allocate realElementProperties!' |
831 |
|
832 |
allocate(this%logicalElementProperties(this%initialCapacity, & |
833 |
this%initialProperties), stat=error) |
834 |
if(error .ne. 0) write(*,*) 'Could not allocate logicalElementProperties!' |
835 |
|
836 |
end function initialize_4i |
837 |
|
838 |
|
839 |
|
840 |
end module Vector_class |