1 |
module Vector_class |
2 |
|
3 |
implicit NONE |
4 |
PRIVATE |
5 |
|
6 |
public :: initialize |
7 |
public :: getSize |
8 |
public :: getElementAt |
9 |
public :: getPropertyListSize |
10 |
public :: getPropertyNameAt |
11 |
public :: addElement |
12 |
public :: setElementProperty |
13 |
public :: getElementProperty |
14 |
|
15 |
integer, parameter :: logical_data_type = 1 |
16 |
integer, parameter :: integer_data_type = 2 |
17 |
integer, parameter :: real_data_type = 3 |
18 |
|
19 |
type, public :: Vector |
20 |
PRIVATE |
21 |
integer :: initialCapacity = 10 |
22 |
integer :: capacityIncrement = 0 |
23 |
integer :: elementCount = 0 |
24 |
|
25 |
integer :: initialProperties = 5 |
26 |
integer :: PropertyIncrement = 0 |
27 |
integer :: propertyCount = 0 |
28 |
|
29 |
integer, pointer :: ElementData(:) |
30 |
character(len=100), pointer :: PropertyDescriptions(:) |
31 |
integer, pointer :: PropertyDataType(:) |
32 |
real(kind = 8), pointer :: realElementProperties(:,:) |
33 |
integer, pointer :: integerElementProperties(:,:) |
34 |
logical, pointer :: logicalElementProperties(:,:) |
35 |
end type Vector |
36 |
|
37 |
interface initialize |
38 |
module procedure initialize_0i |
39 |
module procedure initialize_1i |
40 |
module procedure initialize_2i |
41 |
module procedure initialize_3i |
42 |
module procedure initialize_4i |
43 |
end interface |
44 |
|
45 |
interface setElementProperty |
46 |
module procedure setElementPropertyReal |
47 |
module procedure setElementPropertyInt |
48 |
module procedure setElementPropertyLogical |
49 |
end interface |
50 |
|
51 |
interface getElementProperty |
52 |
module procedure getElementPropertyReal |
53 |
module procedure getElementPropertyInt |
54 |
module procedure getElementPropertyLogical |
55 |
end interface |
56 |
contains |
57 |
|
58 |
function getSize(this) result (ne) |
59 |
type(Vector), pointer :: this |
60 |
integer :: ne |
61 |
ne = this%elementCount |
62 |
end function getSize |
63 |
|
64 |
function getElementAt(this, loc) result (id) |
65 |
type(Vector), pointer :: this |
66 |
integer, intent(in) :: loc |
67 |
integer :: id |
68 |
id = this%ElementData(loc) |
69 |
end function getElementAt |
70 |
|
71 |
function getPropertyListSize(this) result (np) |
72 |
type(Vector), pointer :: this |
73 |
integer :: np |
74 |
np = this%propertyCount |
75 |
end function getPropertyListSize |
76 |
|
77 |
function getPropertyNameAt(this, loc) result (pn) |
78 |
type(Vector), pointer :: this |
79 |
integer, intent(in) :: loc |
80 |
character(len=len(this%PropertyDescriptions)) :: pn |
81 |
pn = this%PropertyDescriptions(loc) |
82 |
end function getPropertyNameAt |
83 |
|
84 |
|
85 |
subroutine getElementPropertyReal(this, id, PropName, pv) |
86 |
type(Vector), pointer :: this |
87 |
integer :: id, whichprop |
88 |
character(len=*) :: PropName |
89 |
real(kind=8) :: pv |
90 |
|
91 |
whichprop = getPropertyIndex(this, PropName) |
92 |
if (whichprop .eq. 0 ) then |
93 |
write(*,*) 'unknown property!' |
94 |
pv = 0.0 |
95 |
else |
96 |
if (this%PropertyDataType(whichprop) .ne. real_data_type) then |
97 |
write(*,*) 'wrong data type for this property!' |
98 |
pv = 0.0 |
99 |
else |
100 |
pv = this%realElementProperties(id, whichprop) |
101 |
endif |
102 |
endif |
103 |
end subroutine getElementPropertyReal |
104 |
|
105 |
subroutine getElementPropertyInt(this, id, PropName, pv) |
106 |
type(Vector), pointer :: this |
107 |
integer :: id, whichprop |
108 |
character(len=*) :: PropName |
109 |
integer :: pv |
110 |
|
111 |
whichprop = getPropertyIndex(this, PropName) |
112 |
if (whichprop .eq. 0 ) then |
113 |
write(*,*) 'unknown property!' |
114 |
pv = 0 |
115 |
else |
116 |
if (this%PropertyDataType(whichprop) .ne. integer_data_type) then |
117 |
write(*,*) 'wrong data type for this property!' |
118 |
pv = 0 |
119 |
else |
120 |
pv = this%integerElementProperties(id, whichprop) |
121 |
endif |
122 |
endif |
123 |
end subroutine getElementPropertyInt |
124 |
|
125 |
subroutine getElementPropertyLogical(this, id, PropName, pv) |
126 |
type(Vector), pointer :: this |
127 |
integer :: id, whichprop |
128 |
character(len=*) :: PropName |
129 |
logical :: pv |
130 |
|
131 |
whichprop = getPropertyIndex(this, PropName) |
132 |
if (whichprop .eq. 0 ) then |
133 |
write(*,*) 'unknown property!' |
134 |
pv = .false. |
135 |
else |
136 |
if (this%PropertyDataType(whichprop) .ne. logical_data_type) then |
137 |
write(*,*) 'wrong data type for this property!' |
138 |
pv = .false. |
139 |
else |
140 |
pv = this%logicalElementProperties(id, whichprop) |
141 |
endif |
142 |
endif |
143 |
end subroutine getElementPropertyLogical |
144 |
|
145 |
function getPropertyIndex(this, PropName) result (id) |
146 |
type(Vector), pointer :: this |
147 |
integer :: id, i |
148 |
character(len=*) :: PropName |
149 |
|
150 |
do i = 1, this%propertyCount |
151 |
if (this%PropertyDescriptions(i) == PropName) then |
152 |
id = i |
153 |
return |
154 |
endif |
155 |
enddo |
156 |
id = 0 |
157 |
end function getPropertyIndex |
158 |
|
159 |
subroutine ensureCapacityHelper(this, minCapacity, minPropCap) |
160 |
type(Vector), pointer :: this, that |
161 |
integer, intent(in) :: minCapacity, minPropCap |
162 |
integer :: oldCapacity, oldPropCap |
163 |
integer :: newCapacity, newPropCap |
164 |
logical :: resizeFlag = .false. |
165 |
|
166 |
oldCapacity = size(this%ElementData) |
167 |
oldPropCap = size(this%PropertyDescriptions) |
168 |
|
169 |
if (minCapacity > oldCapacity) then |
170 |
if (this%capacityIncrement .gt. 0) then |
171 |
newCapacity = oldCapacity + this%capacityIncrement |
172 |
else |
173 |
newCapacity = oldCapacity * 2 |
174 |
endif |
175 |
if (newCapacity .lt. minCapacity) then |
176 |
newCapacity = minCapacity |
177 |
endif |
178 |
resizeFlag = .true. |
179 |
endif |
180 |
|
181 |
if (minPropCap > oldPropCap) then |
182 |
if (this%PropertyIncrement .gt. 0) then |
183 |
newPropCap = oldPropCap + this%PropertyIncrement |
184 |
else |
185 |
newPropCap = oldPropCap * 2 |
186 |
endif |
187 |
if (newPropCap .lt. minPropCap) then |
188 |
newPropCap = minPropCap |
189 |
endif |
190 |
resizeFlag = .true. |
191 |
endif |
192 |
|
193 |
if (resizeFlag) then |
194 |
that = initialize(newCapacity, newPropCap, & |
195 |
this%capacityIncrement, this%PropertyIncrement) |
196 |
call copyAllData(this, that) |
197 |
deallocate(this) |
198 |
this => that |
199 |
endif |
200 |
end subroutine ensureCapacityHelper |
201 |
|
202 |
subroutine copyAllData(v1, v2) |
203 |
type(Vector), pointer :: v1 |
204 |
type(Vector), pointer :: v2 |
205 |
integer :: i, j |
206 |
|
207 |
do i = 1, v1%elementCount |
208 |
v2%elementData(i) = v1%elementData(i) |
209 |
do j = 1, v1%propertyCount |
210 |
|
211 |
if (v1%PropertyDataType(j) .eq. integer_data_type) & |
212 |
v2%integerElementProperties(i,j) = & |
213 |
v1%integerElementProperties(i,j) |
214 |
|
215 |
if (v1%PropertyDataType(j) .eq. real_data_type) & |
216 |
v2%realElementProperties(i,j) = v1%realElementProperties(i,j) |
217 |
|
218 |
if (v1%PropertyDataType(j) .eq. logical_data_type) & |
219 |
v2%logicalElementProperties(i,j) = & |
220 |
v1%logicalElementProperties(i,j) |
221 |
enddo |
222 |
enddo |
223 |
|
224 |
do j = 1, v1%propertyCount |
225 |
v2%PropertyDescriptions(j) = v1%PropertyDescriptions(j) |
226 |
v2%PropertyDataType(j) = v1%PropertyDataType(j) |
227 |
enddo |
228 |
|
229 |
v2%elementCount = v1%elementCount |
230 |
v2%propertyCount = v1%propertyCount |
231 |
|
232 |
return |
233 |
end subroutine copyAllData |
234 |
|
235 |
function addElement(this) result (id) |
236 |
type(Vector), pointer :: this |
237 |
integer :: id |
238 |
call ensureCapacityHelper(this, this%elementCount + 1, this%PropertyCount) |
239 |
this%elementCount = this%elementCount + 1 |
240 |
this%elementData = this%elementCount |
241 |
id = this%elementCount |
242 |
end function addElement |
243 |
|
244 |
recursive subroutine setElementPropertyReal(this, id, PropName, PropValue) |
245 |
type(Vector), pointer :: this |
246 |
integer :: id, i |
247 |
character(len=*), intent(in) :: PropName |
248 |
real( kind=8 ), intent(in) :: PropValue |
249 |
logical :: foundit = .false. |
250 |
! first make sure that the PropName isn't in the list of known properties: |
251 |
do i = 1, this%propertyCount |
252 |
if (PropName == this%PropertyDescriptions(i)) then |
253 |
foundit = .true. |
254 |
this%realElementProperties(id,i) = PropValue |
255 |
endif |
256 |
enddo |
257 |
|
258 |
if (.not.foundit) then |
259 |
call addPropertyToVector(this, PropName, real_data_type) |
260 |
call setElementPropertyReal(this, id, PropName, PropValue) |
261 |
endif |
262 |
end subroutine setElementPropertyReal |
263 |
|
264 |
recursive subroutine setElementPropertyInt(this, id, PropName, PropValue) |
265 |
type(Vector), pointer :: this |
266 |
integer :: id, i |
267 |
character(len=*), intent(in) :: PropName |
268 |
integer, intent(in) :: PropValue |
269 |
logical :: foundit = .false. |
270 |
! first make sure that the PropName isn't in the list of known properties: |
271 |
do i = 1, this%propertyCount |
272 |
if (PropName == this%PropertyDescriptions(i)) then |
273 |
foundit = .true. |
274 |
this%integerElementProperties(id,i) = PropValue |
275 |
endif |
276 |
enddo |
277 |
|
278 |
if (.not.foundit) then |
279 |
call addPropertyToVector(this, PropName, integer_data_type) |
280 |
call setElementPropertyInt(this, id, PropName, PropValue) |
281 |
endif |
282 |
end subroutine setElementPropertyInt |
283 |
|
284 |
recursive subroutine setElementPropertyLogical(this, id, PropName, PropValue) |
285 |
type(Vector), pointer :: this |
286 |
integer :: id, i |
287 |
character(len=*), intent(in) :: PropName |
288 |
logical, intent(in) :: PropValue |
289 |
logical :: foundit = .false. |
290 |
! first make sure that the PropName isn't in the list of known properties: |
291 |
do i = 1, this%propertyCount |
292 |
if (PropName == this%PropertyDescriptions(i)) then |
293 |
foundit = .true. |
294 |
this%logicalElementProperties(id,i) = PropValue |
295 |
endif |
296 |
enddo |
297 |
|
298 |
if (.not.foundit) then |
299 |
call addPropertyToVector(this, PropName, logical_data_type) |
300 |
call setElementPropertyLogical(this, id, PropName, PropValue) |
301 |
endif |
302 |
end subroutine setElementPropertyLogical |
303 |
|
304 |
subroutine addPropertyToVector(this, PropName, data_type) |
305 |
type(Vector), pointer :: this |
306 |
character(len=*), intent(in) :: PropName |
307 |
integer data_type |
308 |
call ensureCapacityHelper(this, this%elementCount, this%propertyCount + 1) |
309 |
this%propertyCount = this%propertyCount + 1 |
310 |
this%PropertyDescriptions(this%propertyCount) = PropName |
311 |
this%PropertyDataType(this%propertyCount) = data_type |
312 |
end subroutine addPropertyToVector |
313 |
|
314 |
function initialize_0i() result(this) |
315 |
type(Vector), pointer :: this |
316 |
nullify(this) |
317 |
this = initialize_2i(10, 5) |
318 |
end function initialize_0i |
319 |
|
320 |
function initialize_1i(nprop) result(this) |
321 |
integer, intent(in) :: nprop |
322 |
type(Vector), pointer :: this |
323 |
nullify(this) |
324 |
this = initialize_2i(10, nprop) |
325 |
end function initialize_1i |
326 |
|
327 |
function initialize_2i(cap, nprop) result(this) |
328 |
integer, intent(in) :: cap, nprop |
329 |
type(Vector), pointer :: this |
330 |
nullify(this) |
331 |
this = initialize_4i(cap, nprop, 0, 0) |
332 |
end function initialize_2i |
333 |
|
334 |
function initialize_3i(cap, nprop, capinc) result(this) |
335 |
integer, intent(in) :: cap, nprop, capinc |
336 |
type(Vector), pointer :: this |
337 |
nullify(this) |
338 |
this = initialize_4i(cap, nprop, capinc, 0) |
339 |
end function initialize_3i |
340 |
|
341 |
function initialize_4i(cap, nprop, capinc, propinc) result(this) |
342 |
integer, intent(in) :: cap, nprop, capinc, propinc |
343 |
integer :: error |
344 |
type(Vector), pointer :: this |
345 |
nullify(this) |
346 |
if (cap .lt. 0) then |
347 |
write(*,*) 'Bogus Capacity:', cap |
348 |
stop |
349 |
endif |
350 |
if (nprop .lt. 0) then |
351 |
write(*,*) 'Bogus Number of Properties:', nprop |
352 |
stop |
353 |
endif |
354 |
|
355 |
allocate(this, stat=error) |
356 |
if(error .ne. 0) write(*,*) 'Could not allocate Vector!' |
357 |
|
358 |
this%initialCapacity = cap |
359 |
this%initialProperties = nprop |
360 |
this%capacityIncrement = capinc |
361 |
this%propertyIncrement = propinc |
362 |
|
363 |
allocate(this%elementData(this%initialCapacity), stat=error) |
364 |
if(error .ne. 0) write(*,*) 'Could not allocate elementData!' |
365 |
|
366 |
allocate(this%PropertyDescriptions(this%initialProperties), & |
367 |
stat=error) |
368 |
if(error .ne. 0) write(*,*) 'Could not allocate PropertyDescriptions!' |
369 |
|
370 |
allocate(this%integerElementProperties(this%initialCapacity, & |
371 |
this%initialProperties), stat=error) |
372 |
if(error .ne. 0) write(*,*) 'Could not allocate integerElementProperties!' |
373 |
|
374 |
allocate(this%realElementProperties(this%initialCapacity, & |
375 |
this%initialProperties), stat=error) |
376 |
if(error .ne. 0) write(*,*) 'Could not allocate realElementProperties!' |
377 |
|
378 |
allocate(this%logicalElementProperties(this%initialCapacity, & |
379 |
this%initialProperties), stat=error) |
380 |
if(error .ne. 0) write(*,*) 'Could not allocate logicalElementProperties!' |
381 |
end function initialize_4i |
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
end module Vector_class |