-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_m.F90
258 lines (202 loc) · 7.81 KB
/
linked_list_m.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
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! !!
!! FortsRaw !!
!! !!
!! Copyright (c) 2019, Thomas Stainer !!
!! !!
!! All rights reserved. !!
!! Licensed under the 3-clause BSD license. !!
!! !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Module for linked list in fortran
module linked_list_m
use fork_m
implicit none
private
!> list node type - takes ownership of deallocation of the value pointer in finalize
type, public :: LinkedListNode
class(*), pointer :: value => null()
type(LinkedListNode), pointer :: next => null()
contains
final :: nodefinalize
end type LinkedListNode
!> list type - takes ownership of deallocation of the value pointer in finalize
!! Note - do not copy lists, i.e. list1 = list2, this causes memory issues, always pass by reference
!! Do not return a list from a function
!! ToDo: Implement copying of lists, do not deep copy pointers
!! ToDo: Give user option for taking ownership or not (i.e. is user responsible for deallocation of pointers or not)
type, public :: LinkedList
private
integer(ki4) :: size = 0_ki4
type(LinkedListNode), pointer :: head => null()
type(LinkedListNode), pointer :: tail => null()
contains
procedure :: append
procedure :: first
procedure :: last
procedure :: atindex
procedure :: reset
procedure :: length
procedure :: traverse
procedure, private :: cleanup
final :: listfinalize
end type LinkedList
!> extends LinkedList but makes use of a cached last node
!! if using getatindex in an iterative manner
!! Improves performance for large lists
type, extends(LinkedList), public :: CachedLinkedList
private
integer(ki4) :: cachedlastindex = 0_ki4
type(LinkedListNode), pointer :: cachedlastnode => null()
contains
procedure :: cachedaccess
procedure :: reset => reset_cached
final :: cachedlistfinalize
end type CachedLinkedList
contains
! Clean up node - The value is deallocated here
subroutine nodefinalize(this)
type(LinkedListNode), intent(inout) :: this
if(associated(this%value))then
deallocate(this%value)
nullify(this%value)
nullify(this%next)
end if
end subroutine nodefinalize
!> Add a value to the list at the tail
subroutine append(this, value)
class(LinkedList), intent(inout) :: this
class(*), intent(in), pointer :: value
type(LinkedListNode), pointer :: node_ptr, next_ptr, current_ptr
! Create a new node and set the value
allocate(node_ptr)
node_ptr%value => value
node_ptr%next => null()
this%size = this%size + 1_ki4
if(.not. associated(this%head))then
this%head => node_ptr
this%tail => node_ptr
else
this%tail%next => node_ptr
this%tail => node_ptr
end if
end subroutine append
!> Traverse the list
subroutine traverse(this, iterator_func)
class(LinkedList), intent(inout) :: this
interface
subroutine iterator_func(node)
import LinkedListNode
type(LinkedListNode), pointer, intent(inout) :: node
end subroutine iterator_func
end interface
type(LinkedListNode), pointer :: current_ptr, temp_ptr
current_ptr => this%head
do while(associated(current_ptr))
nullify(temp_ptr)
temp_ptr => current_ptr%next
call iterator_func(current_ptr)
current_ptr => temp_ptr
end do
end subroutine traverse
!> Reset the list and cleanup
subroutine reset(this)
class(LinkedList), intent(inout) :: this
call this%cleanup()
end subroutine reset
!> Get the size of the list
pure function length(this) result(size)
class(LinkedList), intent(in) :: this
integer(ki4) :: size
size = this%size
end function length
! Get the first node
function first(this) result(firstnode)
class(LinkedList), intent(in) :: this
type(LinkedListNode), pointer :: firstnode
firstnode => this%head
end function first
! Get the last node
function last(this) result(lastnode)
class(LinkedList), intent(in) :: this
type(LinkedListNode), pointer :: lastnode
lastnode => this%tail
end function last
! Get the node at index
! must be between 1 and length()
function atindex(this, index) result(indexnode)
class(LinkedList), intent(in) :: this
integer(ki4), intent(in) :: index
type(LinkedListNode), pointer :: indexnode
integer(ki4) :: i
nullify(indexnode)
if(index > 0_ki4 .and. index <= this%size)then
indexnode => this%head
do i=1, index-1
indexnode => indexnode%next
end do
end if
end function atindex
!> Clean up - deallocation of the nodes in the list
subroutine listfinalize(this)
type(LinkedList), intent(inout) :: this
call this%cleanup()
end subroutine listfinalize
!> Clean up - deallocation of the nodes in the list
subroutine cleanup(this)
class(LinkedList), intent(inout) :: this
type(LinkedListNode), pointer :: current_ptr
call this%traverse(destroyall)
nullify(this%head)
nullify(this%tail)
contains
subroutine destroyall(node)
type(LinkedListNode), pointer, intent(inout) :: node
this%head => node%next
deallocate(node)
nullify(node)
this%size = this%size - 1_ki4
end subroutine destroyall
end subroutine cleanup
!> Get the node at index
! must be between 1 and length()
! It uses the cached index if was set
! and then sets the cached node after access
! for subsequent calls
function cachedaccess(this, index) result(indexnode)
class(CachedLinkedList), intent(inout) :: this
integer(ki4), intent(in) :: index
type(LinkedListNode), pointer :: indexnode
integer(ki4) :: i
integer(ki4) :: startindx
nullify(indexnode)
if(index > 0_ki4 .and. index <= this%size)then
! if last access was cached then use that for speed if we are after it
if(this%cachedlastindex > 0_ki4 .and. index >= this%cachedlastindex)then
indexnode => this%cachedlastnode
startindx = this%cachedlastindex
! else start at head
else
indexnode => this%head
startindx = 1_ki4
end if
do i=startindx, index-1
indexnode => indexnode%next
end do
this%cachedlastindex = index
this%cachedlastnode => indexnode
end if
end function cachedaccess
!> Reset the list and cleanup
subroutine reset_cached(this)
class(CachedLinkedList), intent(inout) :: this
call this%cleanup()
nullify(this%cachedlastnode)
this%cachedlastindex = 0_ki4
end subroutine reset_cached
!> Clean up - deallocation of the nodes in the list
subroutine cachedlistfinalize(this)
type(CachedLinkedList), intent(inout) :: this
call this%cleanup()
end subroutine cachedlistfinalize
end module linked_list_m