-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleVectorModule.f90
272 lines (207 loc) · 6.39 KB
/
DoubleVectorModule.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
! ____ _ __ ____ __ ____
! / __/___(_) / ___ ____/ __ \__ _____ ___ / /_ / _/__ ____
! _\ \/ __/ / _ \/ -_) __/ /_/ / // / -_|_-</ __/ _/ // _ \/ __/
! /___/\__/_/_.__/\__/_/ \___\_\_,_/\__/___/\__/ /___/_//_/\__(_)
!
!
! Copyright 2010 SciberQuest Inc.
!
! No permission is granted to reproduce this software.
!
! This is experimental software and is provided ‘‘as is’’, with no
! warranties of any kind whatsoever, no support, no promise of updates,
! or printed documentation.
!==============================================================================
module DoubleVectorModule
type DoubleVector
real*8 , pointer, dimension(:) :: DData
integer*8 DDataAt
integer*8 DDataEnd
end type
interface NewDoubleVector
module procedure NewDoubleVectorDefault
module procedure NewDoubleVectorCopy
end interface
interface DoubleVectorAppend
module procedure DoubleVectorAppendN
module procedure DoubleVectorAppendRange
end interface
contains
!----------------------------------------------------------------------------
function NewDoubleVectorDefault() result(vec)
implicit none
type(DoubleVector), pointer :: vec
integer iErr
allocate(vec,stat=iErr)
if (iErr.ne.0) then
write(0,*)"Error: Failed to allocate object DoubleVector."
stop
end if
vec%DData => null()
vec%DDataEnd=0
vec%DDataAt=1
call DoubleVectorRealloc(vec,16_8)
end function
!----------------------------------------------------------------------------
function NewDoubleVectorCopy(other) result(vec)
implicit none
type(DoubleVector), pointer :: vec
type(DoubleVector) other
integer iErr
allocate(vec,stat=iErr)
if (iErr.ne.0) then
write(0,*)"Error: Failed to allocate object DoubleVector."
stop
end if
call DoubleVectorTruncate(vec)
call DoubleVectorAppend(vec,other%DData,DoubleVectorGetSize(other))
end function
!----------------------------------------------------------------------------
subroutine DeleteDoubleVector(vec)
implicit none
type(DoubleVector), pointer :: vec
if (.not.associated(vec)) return
if (associated(vec%DData)) then
deallocate(vec%DData)
end if
nullify(vec%DData)
deallocate(vec)
nullify(vec)
end subroutine
!---------------------------------------------------------------------------
function DoubleVectorGetSize(vec) result(n)
implicit none
type(DoubleVector) vec
integer*8 n
n=vec%DDataAt-1
end function
!---------------------------------------------------------------------------
subroutine DoubleVectorTruncate(vec)
implicit none
type(DoubleVector) vec
vec%DDataAt=1
end subroutine
!----------------------------------------------------------------------------
function DoubleVectorGetAllocatedSize(vec) result(n)
implicit none
type(DoubleVector) vec
integer*8 n
n=vec%DDataEnd
end function
!----------------------------------------------------------------------------
function DoubleVectorGetFreeSpace(vec) result(n)
implicit none
type(DoubleVector) vec
integer*8 n
n=vec%DDataEnd-vec%DDataAt+1
end function
!----------------------------------------------------------------------------
subroutine DoubleVectorRealloc(vec,n)
implicit none
type(DoubleVector) vec
real*8,pointer,dimension(:) :: newData
integer*8 n,m,i
integer iErr
if (n.lt.1) then
write(0,*)"Error: Request reallocation to 0 doubles."
stop
end if
if (n.gt.67108864) then
write(0,*)"Error: Request reallocation greater than 64M doubles."
stop
end if
! allocte the new array
allocate(newData(n),stat=iErr)
if (iErr.ne.0) then
write(0,*)"Error: Failed to allocate newData."
stop
end if
! copy
m=DoubleVectorGetSize(vec)
do i=1,m
newData(i)=vec%DData(i)
end do
! initialize
do i=m+1,n
newData(i)=0.0
end do
! free the old array
if (associated(vec%DData)) then
deallocate(vec%DData)
end if
! asign
vec%DData=>newData
vec%DDataEnd=n
end subroutine
!----------------------------------------------------------------------------
subroutine DoubleVectorPush(vec,val)
implicit none
type(DoubleVector) vec
real*8 val
real*8 ddata(1)
ddata(1)=val
call DoubleVectorAppendRange(vec,ddata,1_8,1_8)
end subroutine
!----------------------------------------------------------------------------
function DoubleVectorPop(vec) result(ddata)
implicit none
type(DoubleVector) vec
real*8 ddata
vec%DDataAt=vec%DDataAt-1
ddata=vec%DData(vec%DDataAt)
end function
!----------------------------------------------------------------------------
subroutine DoubleVectorAppendN(vec,ddata,n)
implicit none
type(DoubleVector) vec
real*8 ddata(:)
integer*8 n
call DoubleVectorAppendRange(vec,ddata,1_8,n)
end subroutine
!----------------------------------------------------------------------------
subroutine DoubleVectorAppendRange(vec,ddata,startId,endId)
implicit none
type(DoubleVector) vec
real*8 ddata(:)
integer*8 i,j
integer*8 reqSize,curSize,newSize,startId,endId,ddataSize
ddataSize=endId-startId+1
! resize
reqSize=DoubleVectorGetSize(vec)+ddataSize
curSize=DoubleVectorGetAllocatedSize(vec)
if (reqSize.gt.curSize) then
newSize=curSize
do
if (reqSize.le.newSize) exit
newSize=2*newSize
end do
call DoubleVectorRealloc(vec,newSize)
end if
! append
i=vec%DDataAt
do j=startId,endId
vec%DData(i)=ddata(j)
i=i+1
end do
vec%DDataAt=vec%DDataAt+ddataSize
end subroutine
!----------------------------------------------------------------------------
subroutine DoubleVectorWrite(vec,unitNo,iErr)
implicit none
type(DoubleVector) vec
integer unitNo
integer n
integer iErr
character(len=256) eStr
n=DoubleVectorGetSize(vec)
! write(unit=unitNo,fmt='(A,I5)')'DataAt=',vec%DDataAt
! write(unit=unitNo,fmt='(A,I5)')'DataEnd=',vec%DDataEnd
! write(unit=unitNo,fmt='(A,I5)')'Size=',n
write(unit=unitNo,iostat=iErr,iomsg=eStr)vec%DData(1:n)
if (iErr.ne.0) then
write(0,*)"Error: ",trim(eStr)
write(0,*)"Error: Failed to write file."
return
end if
end subroutine
end module