-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathLinked_Lists.F90
More file actions
710 lines (528 loc) · 16 KB
/
Linked_Lists.F90
File metadata and controls
710 lines (528 loc) · 16 KB
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#include "fdebug.h"
module linked_lists
! A module to provide linked lists and operations on them.
use fldebug
implicit none
! Define a linked list for integers
TYPE inode
INTEGER :: value
TYPE (inode), POINTER :: next=>null() ! next node
END TYPE inode
TYPE ilist
integer :: length=0
TYPE (inode), POINTER :: firstnode=>null()
type(inode), pointer :: lastnode => null()
END TYPE ilist
! <pef> I need a linked list for edges in the mesh.
! I'm adding it here.
TYPE edgenode
INTEGER :: i, j
TYPE(edgenode), POINTER :: next => null()
END TYPE edgenode
TYPE elist
INTEGER :: length = 0
TYPE(edgenode), POINTER :: firstnode => null()
TYPE(edgenode), POINTER :: lastnode => null()
END TYPE elist
! <skramer> I need a linked list for reals
! I'm adding it here - sigh, templates anyone?
type rnode
real :: value
type (rnode), pointer :: next=>null() ! next node
end type rnode
type rlist
integer :: length=0
type (rnode), pointer :: firstnode=>null()
type(rnode), pointer :: lastnode => null()
end type rlist
interface insert_ascending
module procedure iinsert_ascending
end interface
interface has_value
module procedure ihas_value, ehas_value
end interface
interface deallocate
module procedure flush_ilist, flush_elist, flush_ilist_v, flush_rlist, flush_rlist_v
end interface
interface insert
module procedure einsert, iinsert, rinsert
end interface
interface flush_list
module procedure flush_ilist, flush_elist, flush_rlist
end interface
interface flush_lists
module procedure flush_ilist_array
end interface flush_lists
interface pop
module procedure ipop, epop_fn, rpop
end interface
interface fetch
module procedure ifetch
end interface
interface spop ! I need this to be a subroutine, not a function
module procedure epop
end interface
interface list2vector
module procedure ilist2vector, rlist2vector
end interface
interface pop_last
module procedure ipop_last
end interface
interface size_intersection
module procedure isize_intersection
end interface
interface has_value_sorted
module procedure ihas_value_sorted
end interface
interface print_list
module procedure iprint, eprint
end interface
interface intersect_ascending
module procedure intersect_ascending_ilist
end interface intersect_ascending
interface copy
module procedure copy_ilist, copy_ilist_array
end interface
interface maxval
module procedure list_maxval
end interface maxval
private
public:: inode, ilist, edgenode, elist, rlist, insert_ascending,&
has_value, deallocate,&
insert, flush_list, flush_lists, pop, fetch, spop, list2vector,&
pop_last, size_intersection, has_value_sorted, print_list,&
intersect_ascending, copy, maxval
contains
integer function list_maxval(list)
type(ilist), intent(in) :: list
type(inode), pointer :: node
node => list%firstnode
list_maxval = node%value
do while (associated(node))
list_maxval = max(list_maxval, node%value)
node => node%next
end do
end function list_maxval
logical function ihas_value(list, value)
! Check if the list contains the value.
type(ilist), intent(in) :: list
integer, intent(in) :: value
type(inode), pointer :: node
ihas_value = .false.
node => list%firstnode
do while (associated(node))
if(value==node%value) then
ihas_value = .true.
return
end if
node => node%next
end do
end function ihas_value
subroutine iinsert_ascending(list, value, discard)
! Insert value in list in such a position as to ensure that list remains
! in ascending order. This assumes that list is in ascending order.
! Duplicate values are discarded!
type(ilist), intent(inout) :: list
integer, intent(in) :: value
logical, optional :: discard
type(inode), pointer :: this_node, next_node
integer :: pos
! Special case for zero length lists.
if (list%length==0) then
allocate(list%firstnode)
list%firstnode%value=value
! The following should not be necessary
list%firstnode%next=>null()
list%length=1
return
end if
this_node=>list%firstnode
next_node=>list%firstnode%next
! Special case for a value smaller than the first value.
if (value<list%firstnode%value) then
allocate(list%firstnode)
list%firstnode%next=>this_node
list%firstnode%value=value
list%length=list%length+1
return
end if
! initialise discard logical
if (present(discard)) discard =.false.
do pos=0,list%length
if(this_node%value==value) then
! Discard duplicates.
if (present(discard)) discard = .true.
return
end if
if (.not.associated(next_node)) then
! We have hit then end of the chain.
allocate(this_node%next)
if (this_node%value<value) then
this_node%next%value=value
else
this_node%next%value=this_node%value
this_node%value=value
end if
! The following should not be necessary
this_node%next%next=>null()
list%length=list%length+1
return
end if
! Mid-chain. At this point we know this_node%value<value
if (next_node%value>value) then
! Need to insert the value here.
allocate(this_node%next)
this_node%next%next=>next_node
this_node%next%value=value
list%length=list%length+1
return
end if
! Move along the chain.
next_node=>next_node%next
this_node=>this_node%next
end do
FLAbort("Walked off the end of the list. This can't happen.")
end subroutine iinsert_ascending
subroutine iinsert(list, i)
type(ilist), intent(inout) :: list
integer, intent(in) :: i
type(inode), pointer :: node
! Special case for zero length lists.
if (list%length==0) then
allocate(list%firstnode)
list%firstnode%value=i
! The following should not be necessary
list%firstnode%next=>null()
list%length=1
list%lastnode => list%firstnode
return
end if
node => list%lastnode
allocate(node%next)
node%next%value = i
! The following should not be necessary
node%next%next => null()
list%length = list%length+1
list%lastnode => node%next
return
end subroutine iinsert
subroutine flush_ilist(list)
! Remove all entries from a list.
type(ilist), intent(inout) ::list
integer :: i, tmp
do i=1,list%length
tmp=pop(list)
end do
end subroutine flush_ilist
subroutine flush_ilist_v(lists)
type(ilist), intent(inout), dimension(:) :: lists
integer :: i
do i=1,size(lists)
call flush_ilist(lists(i))
end do
end subroutine flush_ilist_v
subroutine flush_rlist_v(lists)
type(rlist), intent(inout), dimension(:) :: lists
integer :: i
do i=1,size(lists)
call flush_rlist(lists(i))
end do
end subroutine flush_rlist_v
subroutine flush_ilist_array(lists)
! Remove all entries from an array of lists
type(ilist), dimension(:), intent(inout) :: lists
integer :: i
do i = 1, size(lists)
call flush_list(lists(i))
end do
end subroutine flush_ilist_array
function ipop(list)
! Pop the first value off list.
integer :: ipop
type(ilist), intent(inout) :: list
type(inode), pointer :: firstnode
ipop=list%firstnode%value
firstnode=>list%firstnode
list%firstnode=>firstnode%next
deallocate(firstnode)
list%length=list%length-1
end function ipop
function ipop_last(list)
! Pop the last value off list.
integer :: ipop_last
type(ilist), intent(inout) :: list
type(inode), pointer :: prev_node => null(), node
integer :: i
node => list%firstnode
do i=1,list%length-1
prev_node => node
node => node%next
end do
ipop_last = node%value
deallocate(node)
prev_node%next => null()
list%length = list%length - 1
if (list%length == 0) then
list%lastnode => null()
else
list%lastnode => prev_node
end if
end function ipop_last
function ifetch(list, j)
integer :: ifetch
type(ilist), intent(inout) :: list
integer, intent(in) :: j
type(inode), pointer :: node
integer :: i
node => list%firstnode
do i=1,j-1
node => node%next
end do
ifetch = node%value
end function ifetch
function ilist2vector(list) result (vector)
! Return a vector containing the contents of ilist
type(ilist), intent(in) :: list
integer, dimension(list%length) :: vector
type(inode), pointer :: this_node
integer :: i
this_node=>list%firstnode
do i=1,list%length
vector(i)=this_node%value
this_node=>this_node%next
end do
end function ilist2vector
subroutine einsert(list, i, j)
type(elist), intent(inout) :: list
integer, intent(in) :: i, j
! Special case for zero length lists.
if (list%length==0) then
allocate(list%firstnode)
list%firstnode%i=i
list%firstnode%j=j
! The following should not be necessary
list%firstnode%next=>null()
list%length=1
list%lastnode=>list%firstnode
return
end if
allocate(list%lastnode%next)
list%lastnode%next%i = i
list%lastnode%next%j = j
! The following should not be necessary
list%lastnode%next%next => null()
list%length = list%length+1
list%lastnode => list%lastnode%next
return
end subroutine einsert
logical function ehas_value(list, i, j)
type(elist), intent(inout) :: list
integer, intent(in) :: i, j
type(edgenode), pointer :: node
ehas_value = .false.
node => list%firstnode
do while(associated(node))
if (node%i == i .and. node%j == j) then
ehas_value = .true.
return
end if
node => node%next
end do
end function ehas_value
subroutine flush_elist(list)
! Remove all entries from a list.
type(elist), intent(inout) ::list
integer :: i, tmp1, tmp2
do i=1,list%length
call spop(list, tmp1, tmp2)
end do
end subroutine flush_elist
subroutine epop(list, i, j)
! Pop the first value off list.
integer, intent(out) :: i, j
type(elist), intent(inout) :: list
type(edgenode), pointer :: firstnode
i=list%firstnode%i
j=list%firstnode%j
firstnode=>list%firstnode
list%firstnode=>firstnode%next
deallocate(firstnode)
list%length=list%length-1
end subroutine epop
function isize_intersection(listA, listB) result(x)
type(ilist), intent(in) :: listA, listB
type(inode), pointer :: nodeA, nodeB
integer :: x
x = 0
nodeA => listA%firstnode
do while(associated(nodeA))
nodeB => listB%firstnode
do while(associated(nodeB))
if (nodeA%value == nodeB%value) then
x = x + 1
exit
else
nodeB => nodeB%next
end if
end do
nodeA => nodeA%next
end do
end function isize_intersection
function ihas_value_sorted(list, i) result(isin)
! This function assumes list is sorted
! in ascending order
type(ilist), intent(in) :: list
integer, intent(in) :: i
type(inode), pointer :: node
logical :: isin
node => list%firstnode
isin = .false.
do while(associated(node))
if (node%value > i) then
return
else if (node%value == i) then
isin = .true.
return
end if
node => node%next
end do
end function ihas_value_sorted
function epop_fn(list) result(x)
type(elist), intent(inout) :: list
integer, dimension(2) :: x
type(edgenode), pointer :: firstnode
x(1) = list%firstnode%i
x(2) = list%firstnode%j
firstnode => list%firstnode
list%firstnode => firstnode%next
deallocate(firstnode)
list%length = list%length - 1
end function epop_fn
subroutine iprint(list, priority)
type(ilist), intent(in) :: list
integer, intent(in) :: priority
type(inode), pointer :: node
ewrite(priority, *) "length: ", list%length
node => list%firstnode
do while (associated(node))
ewrite(priority, *) " -- ", node%value
node => node%next
end do
end subroutine
subroutine eprint(list, priority)
type(elist), intent(in) :: list
integer, intent(in) :: priority
type(edgenode), pointer :: node
ewrite(priority, *) "length: ", list%length
node => list%firstnode
do while (associated(node))
ewrite(priority, *) " -- (", node%i, ", ", node%j, ")"
node => node%next
end do
end subroutine
function intersect_ascending_ilist(list1, list2) result(intersection)
!!< Assumes that list1 and list2 are already sorted
type(ilist), intent(in) :: list1
type(ilist), intent(in) :: list2
type(ilist) :: intersection
type(inode), pointer :: node1 => null(), node2 => null()
node1 => list1%firstnode
node2 => list2%firstnode
do while(associated(node1) .and. associated(node2))
if(node1%value == node2%value) then
call insert_ascending(intersection, node1%value)
node1 => node1%next
node2 => node2%next
else
if(node1%value < node2%value) then
node1 => node1%next
else
node2 => node2%next
end if
end if
end do
end function intersect_ascending_ilist
subroutine copy_ilist(copy_list, list)
!!< Make a deep copy of list
type(ilist), intent(out) :: copy_list
type(ilist), intent(in) :: list
type(inode), pointer :: node, copy_node
if (list%length==0) return
! Special case the first entry
node=>list%firstnode
allocate(copy_list%firstnode)
copy_list%firstnode%value=node%value
copy_node=>copy_list%firstnode
copy_list%length=1
node=>node%next
do while(associated(node))
allocate(copy_node%next)
copy_node=>copy_node%next
copy_node%value=node%value
copy_list%length=copy_list%length+1
node=>node%next
end do
end subroutine copy_ilist
subroutine copy_ilist_array(copy_lists, lists)
!!< Make a deep copy of list
type(ilist), dimension(:), intent(in) :: lists
type(ilist), dimension(size(lists)), intent(out) :: copy_lists
integer :: i
do i=1,size(lists)
call copy_ilist(copy_lists(i), lists(i))
end do
end subroutine copy_ilist_array
subroutine rinsert(list, value)
type(rlist), intent(inout) :: list
real, intent(in) :: value
type(rnode), pointer :: node
! Special case for zero length lists.
if (list%length==0) then
allocate(list%firstnode)
list%firstnode%value=value
! The following should not be necessary
list%firstnode%next=>null()
list%length=1
list%lastnode => list%firstnode
return
end if
node => list%lastnode
allocate(node%next)
node%next%value = value
! The following should not be necessary
node%next%next => null()
list%length = list%length+1
list%lastnode => node%next
end subroutine rinsert
subroutine flush_rlist(list)
! Remove all entries from a list.
type(rlist), intent(inout) ::list
integer :: i, tmp
do i=1,list%length
tmp=pop(list)
end do
end subroutine flush_rlist
function rpop(list)
! Pop the first value off list.
real :: rpop
type(rlist), intent(inout) :: list
type(rnode), pointer :: firstnode
rpop=list%firstnode%value
firstnode=>list%firstnode
list%firstnode=>firstnode%next
deallocate(firstnode)
list%length=list%length-1
end function rpop
function rlist2vector(list) result (vector)
! Return a vector containing the contents of rlist
type(rlist), intent(in) :: list
real, dimension(list%length) :: vector
type(rnode), pointer :: this_node
integer :: i
this_node=>list%firstnode
do i=1, list%length
vector(i)=this_node%value
this_node=>this_node%next
end do
end function rlist2vector
end module linked_lists