-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathftlList.F90_template
770 lines (520 loc) · 20 KB
/
ftlList.F90_template
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
! Copyright (c) 2016, 2017 Robert Rüger
!
! This file is part of of the Fortran Template Library.
!
! The Fortran Template Library is free software: you can redistribute it and/or
! modify it under the terms of the GNU Lesser General Public License as
! published by the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! The Fortran Template Library is distributed in the hope that it will be
! useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
! General Public License for more details.
!
! You should have received a copy of the GNU Lesser General Public License along
! with the Fortran Template Library. If not, see <http://www.gnu.org/licenses/>.
#define FTL_CONTAINER ftlList
#define FTL_CONTAINER_PROVIDES_BIDIRECTIONAL_ITERATOR
#ifdef FTL_INSTANTIATE_TEMPLATE
#if !defined(FTL_TEMPLATE_TYPE)
#error FTL_TEMPLATE_TYPE must be defined when instantiating ftlList
#endif
#if !defined(FTL_TEMPLATE_TYPE_NAME)
#error FTL_TEMPLATE_TYPE_NAME must be defined when instantiating ftlList
#endif
#include "ftlMacros.inc"
#ifdef FTL_TEMPLATE_TYPE_IS_DERIVED
#define FTL_TEMPLATE_TYPE_WRAP type(FTL_TEMPLATE_TYPE)
#else
#define FTL_TEMPLATE_TYPE_WRAP FTL_TEMPLATE_TYPE
#endif
module CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Module)
#ifdef FTL_TEMPLATE_TYPE_MODULE
use FTL_TEMPLATE_TYPE_MODULE
#endif
implicit none
private
! ====== Type of the ftlList container itself ====================================================================================
type :: ListNode
class(ListNode), pointer :: prev => null()
class(ListNode), pointer :: next => null()
end type
type, extends(ListNode) :: DataNode
FTL_TEMPLATE_TYPE_WRAP :: data
end type
type, public :: CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)
private
integer :: psize = 0
type(ListNode) :: sentinel
FTL_TEMPLATE_TYPE_WRAP, pointer, public :: front => null()
FTL_TEMPLATE_TYPE_WRAP, pointer, public :: back => null()
contains
private
procedure :: NewDefault
procedure :: NewCopyOther
procedure :: NewFill
procedure :: NewFromArray
procedure :: NewFromIteratorPair
generic , public :: New => NewDefault, NewCopyOther, NewFill, NewFromArray, NewFromIteratorPair
procedure, public :: Delete
final :: Finalizer
procedure, public :: Begin => BeginList
procedure, public :: End => EndList
procedure :: SizeList
generic , public :: Size => SizeList
procedure, public :: Empty
procedure :: AssignOther
procedure :: AssignArray
generic , public :: assignment(=) => AssignOther, AssignArray
procedure :: InsertSingle
procedure :: InsertFill
procedure :: InsertArray
procedure :: InsertIteratorPair
generic , public :: Insert => InsertSingle, InsertFill, InsertArray, InsertIteratorPair
procedure :: EraseSingle
procedure :: EraseIteratorPair
generic , public :: Erase => EraseSingle, EraseIteratorPair
procedure, public :: PushFront
procedure, public :: PopFront
procedure, public :: PushBack
procedure, public :: PopBack
procedure, public :: Resize
procedure, public :: Clear
procedure :: FixValuePtrs
end type
public :: Begin
interface Begin
module procedure BeginList
end interface
public :: End
interface End
module procedure EndList
end interface
public :: Size
interface Size
module procedure SizeList
end interface
public :: ftlSwap
interface ftlSwap
module procedure SwapList
end interface
public :: ftlMove
interface ftlMove
module procedure ftlMoveList
end interface
! ====== Type of an iterator over a ftlList container ============================================================================
type, public :: CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)
private
class(ListNode) , pointer :: node => null()
FTL_TEMPLATE_TYPE_WRAP, pointer, public :: value => null()
contains
private
procedure :: NewItDefault
procedure :: NewItCopyOther
generic , public :: New => NewItDefault, NewItCopyOther
procedure, public :: Inc
procedure, public :: Dec
end type
public :: operator(==)
interface operator(==)
module procedure EqualOther
end interface
public :: operator(/=)
interface operator(/=)
module procedure UnequalOther
end interface
contains
! ====== Implementation of ftlList methods =======================================================================================
subroutine NewDefault(self)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout), target :: self
call self%Delete()
self%sentinel%next => self%sentinel
self%sentinel%prev => self%sentinel
end subroutine
!
impure elemental subroutine NewCopyOther(self, other)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: self
type(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(in) :: other
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: it
call self%New()
it = other%Begin()
do while (it /= other%End())
call self%PushBack(it%value)
call it%Inc()
enddo
end subroutine
!
subroutine NewFill(self, n, val)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: self
integer , intent(in) :: n
FTL_TEMPLATE_TYPE_WRAP , intent(in), optional :: val
integer :: i
call self%New()
if (present(val)) then
do i = 1, n
call self%PushBack(val)
enddo
else
stop 'TODO: Implement ftlList%NewFill without val'
endif
end subroutine
!
subroutine NewFromArray(self, array)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: self
FTL_TEMPLATE_TYPE_WRAP , intent(in) :: array(:)
integer :: i
call self%New()
do i = 1, size(array)
call self%PushBack(array(i))
enddo
end subroutine
!
subroutine NewFromIteratorPair(self, first, last)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)) , intent(inout) :: self
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(in) :: first
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(in) :: last
call self%New()
call self%Insert(self%Begin(), first, last)
end subroutine
impure elemental subroutine Delete(self)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout), target :: self
class(ListNode), pointer :: walker, deletor
walker => self%sentinel%next
do while (associated(walker) .and. .not.associated(walker,self%sentinel))
deletor => walker
walker => walker%next
deallocate(deletor)
enddo
self%psize = 0
nullify(self%sentinel%prev)
nullify(self%sentinel%next)
nullify(self%front)
nullify(self%back)
end subroutine
!
impure elemental subroutine Finalizer(self)
type(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: self
call self%Delete()
end subroutine
! =============> Iterators:
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) function BeginList(self) result(Begin)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(in), target :: self
Begin%node => self%sentinel%next
select type (node => Begin%node)
type is (DataNode)
Begin%value => node%data
end select
end function
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) function EndList(self) result(End)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(in), target :: self
End%node => self%sentinel
end function
! =============> Capacity:
pure integer function SizeList(self) result(Size)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(in) :: self
Size = self%psize
end function
pure logical function Empty(self)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(in) :: self
Empty = (self%psize == 0)
end function
! =============> Modifiers:
! TODO: implement using existing list nodes instead of copy construction
!
impure elemental subroutine AssignOther(self, other)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: self
type(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(in) :: other
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: it
integer :: i
call self%New()
i = 1
it = other%Begin()
do while (i <= other%Size())
call self%PushBack(it%value)
i = i + 1
call it%Inc()
enddo
end subroutine
!
subroutine AssignArray(self, array)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: self
FTL_TEMPLATE_TYPE_WRAP , intent(in) :: array(:)
call self%New(array)
end subroutine
subroutine InsertSingle(self, position, val)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)) , intent(inout) :: self
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: position
FTL_TEMPLATE_TYPE_WRAP , intent(in) :: val
call self%InsertFill(position, 1, val)
end subroutine
!
subroutine InsertFill(self, position, n, val)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)) , intent(inout) :: self
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: position
integer , intent(in) :: n
FTL_TEMPLATE_TYPE_WRAP , intent(in) :: val
integer :: i
do i = 1, n
call InsertNodeBefore(position%node, val)
enddo
self%psize = self%psize + n
call self%FixValuePtrs()
end subroutine
!
subroutine InsertArray(self, position, array)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)) , intent(inout) :: self
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: position
FTL_TEMPLATE_TYPE_WRAP , intent(in) :: array(:)
integer :: i
do i = 1, size(array)
call InsertNodeBefore(position%node, array(i))
enddo
self%psize = self%psize + size(array)
call self%FixValuePtrs()
end subroutine
!
subroutine InsertIteratorPair(self, position, first, last)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)) , intent(inout) :: self
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: position
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(in) :: first
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(in) :: last
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: it
it = first
do while (it /= last)
call InsertNodeBefore(position%node, it%value)
self%psize = self%psize + 1
call it%Inc()
enddo
call self%FixValuePtrs()
end subroutine
subroutine PushFront(self, val)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout), target :: self
FTL_TEMPLATE_TYPE_WRAP , intent(in) :: val
call InsertNodeBefore(self%sentinel%next, val)
self%psize = self%psize + 1
call self%FixValuePtrs()
end subroutine
FTL_TEMPLATE_TYPE_WRAP function PopFront(self)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout), target :: self
class(ListNode), pointer :: oldfirst
oldfirst => self%sentinel%next
#ifdef FTL_TEMPLATE_TYPE_PROVIDES_FTLMOVE
call ftlMove(self%front, PopFront)
#else
PopFront = self%front
#endif
self%psize = self%psize - 1
call UnlinkNode(oldfirst)
deallocate(oldfirst)
call self%FixValuePtrs()
end function
subroutine PushBack(self, val)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: self
FTL_TEMPLATE_TYPE_WRAP , intent(in) :: val
call InsertNodeAfter(self%sentinel%prev, val)
self%psize = self%psize + 1
call self%FixValuePtrs()
end subroutine
FTL_TEMPLATE_TYPE_WRAP function PopBack(self)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout), target :: self
class(ListNode), pointer :: oldlast
oldlast => self%sentinel%prev
#ifdef FTL_TEMPLATE_TYPE_PROVIDES_FTLMOVE
call ftlMove(self%back, PopBack)
#else
PopBack = self%back
#endif
self%psize = self%psize - 1
call UnlinkNode(oldlast)
deallocate(oldlast)
call self%FixValuePtrs()
end function
subroutine EraseSingle(self, position)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)) , intent(inout) :: self
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: position
call UnlinkNode(position%node)
deallocate(position%node)
self%psize = self%psize - 1
call self%FixValuePtrs()
end subroutine
!
subroutine EraseIteratorPair(self, first, last)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)) , intent(inout) :: self
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: first
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(in) :: last
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: deletor
associate(walker => first)
do while (walker /= last)
deletor = walker
call walker%Inc()
call self%EraseSingle(deletor)
enddo
end associate
end subroutine
subroutine SwapList(self, other)
type(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout), target :: self
type(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout), target :: other
integer :: tmpSize
type(ListNode) :: tmpNode
! fix pointers from data nodes to the sentinels
self%sentinel%prev%next => other%sentinel
self%sentinel%next%prev => other%sentinel
other%sentinel%prev%next => self%sentinel
other%sentinel%next%prev => self%sentinel
! exchange sentinels themselves
tmpNode = self%sentinel
tmpSize = self%psize
self%sentinel = other%sentinel
self%psize = other%psize
other%sentinel = tmpNode
other%psize = tmpSize
! fix front/back pointers for both lists
call self%FixValuePtrs()
call other%FixValuePtrs()
end subroutine
subroutine Resize(self, n, val)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: self
integer , intent(in) :: n
FTL_TEMPLATE_TYPE_WRAP , intent(in) , optional :: val
type(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)) :: it
integer :: i
if (n == self%psize) then
return
else if (n < self%psize) then
it = self%Begin()
do i = 2, n
call it%Inc()
enddo
call it%Inc()
call self%Erase(it,self%End())
else ! n > self%psize
do i = 1, n - self%psize
call InsertNodeAfter(self%sentinel%prev, val)
enddo
endif
self%psize = n
call self%FixValuePtrs()
end subroutine
subroutine Clear(self)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: self
call self%New()
end subroutine
! =============> FTL methods:
subroutine ftlMoveList(src, dest)
type(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: src
type(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(out) , target :: dest
dest%psize = src%psize
dest%sentinel = src%sentinel
if (associated(dest%sentinel%next)) dest%sentinel%next%prev => dest%sentinel
if (associated(dest%sentinel%prev)) dest%sentinel%prev%next => dest%sentinel
call dest%FixValuePtrs()
nullify(src%sentinel%prev)
nullify(src%sentinel%next)
nullify(src%front)
nullify(src%back)
src%psize = 0
end subroutine
! =============> Internal methods:
subroutine InsertNodeBefore(beforenode, val)
class(ListNode), pointer, intent(inout) :: beforenode
FTL_TEMPLATE_TYPE_WRAP , intent(in) , optional :: val
class(ListNode), pointer :: oldprev, newprev
oldprev => beforenode%prev
allocate(DataNode::beforenode%prev)
newprev => beforenode%prev
newprev%next => beforenode
newprev%prev => oldprev
oldprev%next => newprev
if (present(val)) then
select type (newprev)
type is (DataNode) ! always true
newprev%data = val
end select
endif
end subroutine
subroutine InsertNodeAfter(afternode, val)
class(ListNode), pointer, intent(inout) :: afternode
FTL_TEMPLATE_TYPE_WRAP , intent(in) , optional :: val
class(ListNode), pointer :: oldnext, newnext
oldnext => afternode%next
allocate(DataNode::afternode%next)
newnext => afternode%next
newnext%prev => afternode
newnext%next => oldnext
oldnext%prev => newnext
if (present(val)) then
select type (newnext)
type is (DataNode) ! always true
newnext%data = val
end select
endif
end subroutine
subroutine UnlinkNode(node)
class(ListNode), intent(inout) :: node
node%next%prev => node%prev
node%prev%next => node%next
end subroutine
subroutine FixValuePtrs(self)
class(CAT(ftlList,FTL_TEMPLATE_TYPE_NAME)), intent(inout) :: self
if (self%psize == 0) then
nullify(self%front,self%back)
else
select type (first => self%sentinel%next)
type is (DataNode)
self%front => first%data
end select
select type (last => self%sentinel%prev)
type is (DataNode)
self%back => last%data
end select
endif
end subroutine
! ====== Implementation of ftlListIterator methods ===============================================================================
subroutine NewItDefault(self)
class(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(inout) :: self
nullify(self%node)
nullify(self%value)
end subroutine
!
subroutine NewItCopyOther(self, other)
class(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(out) :: self
class(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(in) :: other
self%node => other%node
select type (node => self%node)
type is (DataNode)
self%value => node%data
end select
end subroutine
! =============> Arithmetic operations:
subroutine Inc(self)
class(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(inout) :: self
self%node => self%node%next
select type (node => self%node)
type is (DataNode)
self%value => node%data
end select
end subroutine
!
subroutine Dec(self)
class(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(inout) :: self
self%node => self%node%prev
select type (node => self%node)
type is (DataNode)
self%value => node%data
end select
end subroutine
! =============> Logical operations:
pure logical function EqualOther(self, other)
class(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(in) :: self
class(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(in) :: other
EqualOther = associated(self%node,other%node)
end function
!
pure logical function UnequalOther(self, other)
class(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(in) :: self
class(CAT3(ftlList,FTL_TEMPLATE_TYPE_NAME,Iterator)), intent(in) :: other
UnequalOther = .not.associated(self%node,other%node)
end function
end module
#endif