-
Notifications
You must be signed in to change notification settings - Fork 1
/
gfc_list.F90
1392 lines (1348 loc) · 63.8 KB
/
gfc_list.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
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!Generic Fortran Containers (GFC): Bi-directional linked list
!AUTHOR: Dmitry I. Lyakh (Liakh): [email protected], [email protected]
!REVISION: 2018-05-09 (started 2016-02-28)
!Copyright (C) 2014-2017 Dmitry I. Lyakh (Liakh)
!Copyright (C) 2014-2017 Oak Ridge National Laboratory (UT-Battelle)
!This file is part of ExaTensor.
!ExaTensor 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.
!ExaTensor 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 ExaTensor. If not, see <http://www.gnu.org/licenses/>.
!NOTES:
! # A list is a linked derivative of an abstract (unlinked) GFC container.
! A sublist is a list incorporated into another (composite) list.
! # All accesses, updates, scans, and actions are performed on a list
! via the list iterator associated with the list. Elements of a sublist
! can be accessed by both the original and the combined list iterators.
! Multiple iterators can be associated with a list at the same time.
!FOR DEVELOPERS ONLY:
! # Boundary/current element reference counting needs to be implemented.
module gfc_list
use gfc_base
use gfc_vector
use timers
implicit none
private
!PARAMETERS:
!Basic:
integer(INTD), private:: CONS_OUT=6 !output device
logical, private:: VERBOSE=.TRUE. !verbositiy for errors
integer(INTD), private:: DEBUG=0 !debugging level (0:none)
!TYPES:
!Linked list element:
type, extends(gfc_cont_elem_t), public:: list_elem_t
class(list_elem_t), pointer, private:: next_elem=>NULL()
class(list_elem_t), pointer, private:: prev_elem=>NULL()
contains
procedure, private:: ListElemConstruct
generic, public:: list_elem_ctor=>ListElemConstruct !constructs the content of the list element
procedure, public:: is_first=>ListElemIsFirst !returns GFC_TRUE if the element is the first in the list
procedure, public:: is_last=>ListElemIsLast !returns GFC_TRUE if the element is the last in the list
end type list_elem_t
!Iterator position in the linked list:
type, public:: list_pos_t
class(list_elem_t), pointer, private:: elem_p=>NULL() !pointer to a list element
contains
procedure, public:: is_set=>ListPosIsSet !returns TRUE if the list position is set
procedure, public:: clean=>ListPosClean !resets the list position to NULL
end type list_pos_t
type(list_pos_t), parameter, public:: LIST_POS_NULL=list_pos_t(NULL())
!Linked list:
type, extends(gfc_container_t), public:: list_bi_t
class(list_elem_t), pointer, private:: first_elem=>NULL() !first element of the linked list
class(list_elem_t), pointer, private:: last_elem=>NULL() !last element of the linked list
contains
procedure, public:: is_empty=>ListIsEmpty !returns GFC_TRUE if the list is empty, GFC_FALSE otherwise (or error code)
procedure, public:: is_sublist=>ListIsSublist !returns TRUE if the list is a sublist of a larger list, FALSE otherwise
procedure, private:: ListDuplicateToList !duplicates a list into another list either by value or by reference
procedure, private:: ListDuplicateToVector !duplicates a list into a vector either by value or by reference
generic, public:: duplicate=>ListDuplicateToList,ListDuplicateToVector
procedure, private:: ListBiAssign !copy assignment
generic, public:: assignment(=)=>ListBiAssign
final:: list_bi_dtor !dtor
end type list_bi_t
!List iterator:
type, extends(gfc_iter_t), public:: list_iter_t
class(list_elem_t), pointer, private:: current=>NULL() !current element of the linked list
class(list_bi_t), pointer, private:: container=>NULL() !linked list associated with the iterator
contains
procedure, public:: init=>ListIterInit !initializes the iterator by associating it with a list and resetting to the beginning
procedure, public:: reset=>ListIterReset !resets the iterator to the beginning of the list
procedure, public:: reset_back=>ListIterResetBack !resets the iterator to the end of the list
procedure, public:: release=>ListIterRelease !releases the iterator (dissociates it from the container)
procedure, public:: pointee=>ListIterPointee !returns the container element currently pointed to by the iterator
procedure, public:: next=>ListIterNext !moves the iterator to the next list element
procedure, public:: previous=>ListIterPrevious !moves the iterator to the previous list element
procedure, public:: on_first=>ListIterOnFirst !returns TRUE if the iterator is positioned on the first element of the list
procedure, public:: on_last=>ListIterOnLast !returns TRUE if the iterator is positioned on the last element of the list
procedure, public:: append=>ListIterAppend !inserts a new element either at the beginning or at the end of the container
procedure, public:: insert_elem=>ListIterInsertElem !inserts a new element at the current position of the container
procedure, public:: insert_list=>ListIterInsertList !inserts another linked list at the current position of the container
procedure, public:: move_elem=>ListIterMoveElem !moves a list element from one iterator to another
procedure, private:: ListIterMoveListBas !moves an entire list or its part from one iterator to another
procedure, private:: ListIterMoveListPrd !moves an entire list or its part from one iterator to another with a potential predicated early stop
generic, public:: move_list=>ListIterMoveListBas,ListIterMoveListPrd
procedure, public:: split=>ListIterSplit !splits the list into two parts at the current position
procedure, public:: delete=>ListIterDelete !deletes the list element in the current position
procedure, public:: delete_sublist=>ListIterDeleteSublist!deletes all elements either prior or after the current iterator position
procedure, public:: delete_all=>ListIterDeleteAll !deletes all list elements
procedure, public:: bookmark=>ListIterBookmark !bookmarks the current iterator position
procedure, public:: jump=>ListIterJump !jumps to a previously bookmarked iterator position
procedure, public:: jump_=>ListIterJump_ !PRIVATE: moves the iterator to a specified list element
procedure, private:: pop_=>ListIterPop_ !PRIVATE: Pops up the element at the current iterator position without destroying it
end type list_iter_t
!INTERFACES:
!VISIBILITY:
!list_elem_t:
private ListElemConstruct
private ListElemIsFirst
private ListElemIsLast
!list_pos_t:
private ListPosIsSet
private ListPosClean
!list_bi_t:
private ListIsEmpty
private ListIsSublist
private ListDuplicateToList
private ListDuplicateToVector
private ListBiAssign
public list_bi_dtor
!list_iter_t:
private ListIterInit
private ListIterReset
private ListIterResetBack
private ListIterRelease
private ListIterPointee
private ListIterNext
private ListIterPrevious
private ListIterOnFirst
private ListIterOnLast
private ListIterAppend
private ListIterInsertElem
private ListIterInsertList
private ListIterMoveElem
private ListIterMoveListBas
private ListIterMoveListPrd
private ListIterSplit
private ListIterDelete
private ListIterDeleteSublist
private ListIterDeleteAll
private ListIterBookmark
private ListIterJump
private ListIterJump_
private ListIterPop_
contains
!IMPLEMENTATION:
!-------------------------------------------------------------------------
#if !(defined(__GNUC__) && __GNUC__ < 9)
subroutine ListElemConstruct(this,obj,ierr,assoc_only,copy_ctor_f)
#else
subroutine ListElemConstruct(this,obj,ierr,assoc_only)
#endif
!Constructs the content of the list element.
implicit none
class(list_elem_t), intent(inout):: this !inout: element of a list
class(*), target, intent(in):: obj !in: value to be stored
integer(INTD), intent(out), optional:: ierr !out: error code
logical, intent(in), optional:: assoc_only !in: if TRUE, the value will be assigned by reference, otherwise by value (allocated): Defaults to FALSE
#if !(defined(__GNUC__) && __GNUC__ < 9)
procedure(gfc_copy_i), optional:: copy_ctor_f !in: generic copy constructor
#endif
integer(INTD):: errc
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
if(present(assoc_only)) then
call this%construct_base(obj,errc,assoc_only,copy_ctor_f)
else
call this%construct_base(obj,errc,copy_ctor_f=copy_ctor_f)
endif
else
#endif
if(present(assoc_only)) then
call this%construct_base(obj,errc,assoc_only=assoc_only)
else
call this%construct_base(obj,errc)
endif
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(present(ierr)) ierr=errc
return
end subroutine ListElemConstruct
!------------------------------------------------------
function ListElemIsFirst(this,ierr) result(res)
!Returns GFC_TRUE if this list element is the first in the list.
implicit none
integer(INTD):: res !out: result
class(list_elem_t), intent(in):: this !in: list element
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
integer(INTD):: errc
errc=GFC_SUCCESS; res=GFC_TRUE
if(associated(this%prev_elem)) res=GFC_FALSE
if(present(ierr)) ierr=errc
return
end function ListElemIsFirst
!-----------------------------------------------------
function ListElemIsLast(this,ierr) result(res)
!Returns GFC_TRUE if this list element is the last in the list.
implicit none
integer(INTD):: res !out: result
class(list_elem_t), intent(in):: this !in: list element
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
integer(INTD):: errc
errc=GFC_SUCCESS; res=GFC_TRUE
if(associated(this%next_elem)) res=GFC_FALSE
if(present(ierr)) ierr=errc
return
end function ListElemIsLast
![list_pos_t]==================================
function ListPosIsSet(this) result(ans)
!Returns TRUE if the list position is set, FALSE otherwise
implicit none
logical:: ans !out: answer
class(list_pos_t), intent(in):: this !in: list position
ans=associated(this%elem_p)
return
end function ListPosIsSet
!------------------------------------
subroutine ListPosClean(this)
!Resets the list position to an empty state.
implicit none
class(list_pos_t), intent(inout):: this !inout: list position
this%elem_p=>NULL()
return
end subroutine ListPosClean
!---------------------------------------------
function ListIsEmpty(this) result(res)
!Returns GFC_TRUE if the list is empty, GFC_FALSE otherwise (or error code).
implicit none
integer(INTD):: res !out: result of query (or error code)
class(list_bi_t), intent(in):: this !in: list
if(associated(this%first_elem)) then
if(associated(this%last_elem)) then
res=GFC_FALSE
else
res=GFC_CORRUPTED_CONT
endif
else
if(associated(this%last_elem)) then
res=GFC_CORRUPTED_CONT
else
res=GFC_TRUE
endif
endif
return
end function ListIsEmpty
!----------------------------------------------------
function ListIsSublist(this,ierr) result(res)
!Returns TRUE if the list is a sublist of another list, FALSE otherwise.
implicit none
logical:: res !out: result
class(list_bi_t), intent(in):: this !in: list
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
integer(INTD):: errc
errc=GFC_SUCCESS; res=.FALSE.
if(associated(this%first_elem).and.associated(this%last_elem)) then
res=(associated(this%first_elem%prev_elem).or.associated(this%last_elem%next_elem))
else
if(associated(this%first_elem).or.associated(this%last_elem)) then
errc=GFC_CORRUPTED_CONT
else
errc=GFC_EMPTY_CONT
endif
endif
if(present(ierr)) ierr=errc
return
end function ListIsSublist
!----------------------------------------------------------------------------------
#if !(defined(__GNUC__) && __GNUC__ < 9)
function ListDuplicateToList(this,list,assoc_only,copy_ctor_f) result(ierr)
#else
function ListDuplicateToList(this,list,assoc_only) result(ierr)
#endif
!Duplicates a list into another list either by value or by reference.
implicit none
integer(INTD):: ierr !out: error code
class(list_bi_t), intent(in):: this !in: input list (may be empty)
class(list_bi_t), intent(inout):: list !out: output duplicate list (must be empty on input)
logical, intent(in), optional:: assoc_only !in: if TRUE, the list will be duplicated by reference, otherwise by value (default)
#if !(defined(__GNUC__) && __GNUC__ < 9)
procedure(gfc_copy_i), optional:: copy_ctor_f !in: optional copy constructor (may be needed when duplicating by value)
#endif
integer(INTD):: errc
type(list_iter_t):: ilit,olit
class(*), pointer:: up
logical:: assoc
ierr=GFC_SUCCESS
if(list%is_empty().eq.GFC_TRUE) then
if(this%is_empty().eq.GFC_FALSE) then
ierr=ilit%init(this)
if(ierr.eq.GFC_SUCCESS) then
ierr=olit%init(list)
if(ierr.eq.GFC_SUCCESS) then
assoc=.FALSE.; if(present(assoc_only)) assoc=assoc_only
errc=GFC_SUCCESS
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
do while(errc.eq.GFC_SUCCESS)
up=>ilit%get_value(ierr); if(ierr.ne.GFC_SUCCESS) exit
ierr=olit%append(up,.FALSE.,assoc,copy_ctor_f)
errc=ilit%next()
enddo
else
#endif
do while(errc.eq.GFC_SUCCESS)
up=>ilit%get_value(ierr); if(ierr.ne.GFC_SUCCESS) exit
ierr=olit%append(up,.FALSE.,assoc)
errc=ilit%next()
enddo
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(errc.eq.GFC_NO_MOVE) errc=GFC_SUCCESS
if(errc.ne.GFC_SUCCESS) ierr=errc
errc=olit%release(); if(errc.ne.GFC_SUCCESS.and.ierr.eq.GFC_SUCCESS) ierr=errc
endif
errc=ilit%release(); if(errc.ne.GFC_SUCCESS.and.ierr.eq.GFC_SUCCESS) ierr=errc
endif
endif
else
ierr=GFC_INVALID_ARGS
endif
return
end function ListDuplicateToList
!--------------------------------------------------------------------------------------
#if !(defined(__GNUC__) && __GNUC__ < 9)
function ListDuplicateToVector(this,vector,assoc_only,copy_ctor_f) result(ierr)
#else
function ListDuplicateToVector(this,vector,assoc_only) result(ierr)
#endif
!Duplicates a list into a vector either by value or by reference.
implicit none
integer(INTD):: ierr !out: error code
class(list_bi_t), intent(in):: this !in: input list (must be non-empty on input)
class(vector_t), intent(inout):: vector !out: output vector (must be empty on input)
logical, intent(in), optional:: assoc_only !in: if TRUE, the list will be duplicated by reference, otherwise by value (default)
#if !(defined(__GNUC__) && __GNUC__ < 9)
procedure(gfc_copy_i), optional:: copy_ctor_f !in: optional copy constructor (may be needed when duplicating by value)
#endif
integer(INTD):: errc
type(list_iter_t):: ilit
type(vector_iter_t):: ovit
class(*), pointer:: up
logical:: assoc
ierr=GFC_SUCCESS
if(vector%is_empty().eq.GFC_TRUE) then
if(this%is_empty().eq.GFC_FALSE) then
ierr=ilit%init(this)
if(ierr.eq.GFC_SUCCESS) then
ierr=ovit%init(vector)
if(ierr.eq.GFC_SUCCESS) then
assoc=.FALSE.; if(present(assoc_only)) assoc=assoc_only
errc=GFC_SUCCESS
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
do while(errc.eq.GFC_SUCCESS)
up=>ilit%get_value(ierr); if(ierr.ne.GFC_SUCCESS) exit
ierr=ovit%append(up,assoc,copy_ctor_f)
errc=ilit%next()
enddo
else
#endif
do while(errc.eq.GFC_SUCCESS)
up=>ilit%get_value(ierr); if(ierr.ne.GFC_SUCCESS) exit
ierr=ovit%append(up,assoc)
errc=ilit%next()
enddo
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(errc.eq.GFC_NO_MOVE) errc=GFC_SUCCESS
if(errc.ne.GFC_SUCCESS) ierr=errc
errc=ovit%release(); if(errc.ne.GFC_SUCCESS.and.ierr.eq.GFC_SUCCESS) ierr=errc
endif
errc=ilit%release(); if(errc.ne.GFC_SUCCESS.and.ierr.eq.GFC_SUCCESS) ierr=errc
endif
endif
else
ierr=GFC_INVALID_ARGS
endif
return
end function ListDuplicateToVector
!----------------------------------------
subroutine ListBiAssign(this,src)
!Copy assignment.
implicit none
class(list_bi_t), intent(inout), target:: this !out: destination list
class(list_bi_t), intent(in), target:: src !in: source list
integer(INTD):: errc
class(list_bi_t), pointer:: ds,sr
type(list_iter_t):: lit
ds=>this; sr=>src
if(.not.associated(ds,sr)) then
errc=lit%init(this)
if(errc.eq.GFC_SUCCESS) then
errc=lit%delete_all()
if(errc.eq.GFC_SUCCESS) then
errc=lit%release()
if(errc.eq.GFC_SUCCESS) errc=src%duplicate(this)
endif
endif
if(errc.ne.GFC_SUCCESS) then
if(VERBOSE) write(CONS_OUT,'("#ERROR(list_bi_t.assignment(=)): Duplication failed with error ",i11)') errc
endif
endif
return
end subroutine ListBiAssign
!------------------------------------
subroutine list_bi_dtor(this)
implicit none
type(list_bi_t):: this
type(list_iter_t):: lit
integer(INTD):: errc
errc=lit%init(this)
errc=lit%delete_all()
errc=lit%release()
return
end subroutine list_bi_dtor
!----------------------------------------------------
function ListIterInit(this,cont) result(ierr)
!Initializes the iterator and resets it to the beginning of the container.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(list_iter_t), intent(inout):: this !inout: iterator
class(gfc_container_t), target, intent(in):: cont !in: container
ierr=GFC_SUCCESS
select type(cont)
class is(list_bi_t)
this%container=>cont
ierr=this%reset()
class default
ierr=GFC_INVALID_ARGS
end select
return
end function ListIterInit
!------------------------------------------------
function ListIterReset(this) result(ierr)
!Resets the iterator to the beginning (first element).
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(list_iter_t), intent(inout):: this !inout: iterator
ierr=GFC_SUCCESS
if(associated(this%container)) then
this%current=>this%container%first_elem
if(associated(this%current)) then
ierr=this%set_status_(GFC_IT_ACTIVE)
else
ierr=this%set_status_(GFC_IT_EMPTY)
endif
call this%reset_count() !reset all iteration counters
else
this%current=>NULL()
ierr=this%set_status_(GFC_IT_NULL)
ierr=GFC_IT_NULL
endif
return
end function ListIterReset
!----------------------------------------------------
function ListIterResetBack(this) result(ierr)
!Resets the iterator to the end (last element).
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(list_iter_t), intent(inout):: this !inout: iterator
ierr=GFC_SUCCESS
if(associated(this%container)) then
this%current=>this%container%last_elem
if(associated(this%current)) then
ierr=this%set_status_(GFC_IT_ACTIVE)
else
ierr=this%set_status_(GFC_IT_EMPTY)
endif
call this%reset_count() !reset all iteration counters
else
this%current=>NULL()
ierr=this%set_status_(GFC_IT_NULL)
ierr=GFC_IT_NULL
endif
return
end function ListIterResetBack
!--------------------------------------------------
function ListIterRelease(this) result(ierr)
!Dissociates the iterator from its container.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(list_iter_t), intent(inout):: this !inout: iterator
this%current=>NULL(); this%container=>NULL()
call this%reset_count(); ierr=this%set_status_(GFC_IT_NULL)
return
end function ListIterRelease
!--------------------------------------------------------
function ListIterPointee(this,ierr) result(pntee)
!Returns the container element the iterator is currently pointing to.
implicit none
class(gfc_cont_elem_t), pointer:: pntee !out: container element currently pointed to by the iterator
class(list_iter_t), intent(in):: this !in: iterator
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
integer(INTD):: errc
errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) then
pntee=>this%current; errc=GFC_SUCCESS
else
pntee=>NULL()
endif
if(present(ierr)) ierr=errc
return
end function ListIterPointee
!------------------------------------------------------
function ListIterNext(this,elem_p) result(ierr)
!If <elem_p> is absent, the iterator moves to the next element, if any.
!If <elem_p> is present, the iterator simply returns the next element in <elem_p> without moving.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(list_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
class(list_elem_t), pointer:: lep
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
ierr=GFC_SUCCESS
if(.not.associated(this%current,this%container%last_elem)) then
lep=>this%current%next_elem
if(present(elem_p)) then
elem_p=>lep
else
this%current=>lep
endif
else
this%current=>NULL()
ierr=this%set_status_(GFC_IT_DONE)
lep=>NULL()
endif
if(.not.associated(lep)) then; ierr=GFC_NO_MOVE; else; lep=>NULL(); endif
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function ListIterNext
!----------------------------------------------------------
function ListIterPrevious(this,elem_p) result(ierr)
!If <elem_p> is absent, the iterator moves to the previous element, if any.
!If <elem_p> is present, the iterator simply returns the previous element in <elem_p> without moving.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(list_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
class(list_elem_t), pointer:: lep
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
ierr=GFC_SUCCESS
if(.not.associated(this%current,this%container%first_elem)) then
lep=>this%current%prev_elem
if(present(elem_p)) then
elem_p=>lep
else
this%current=>lep
endif
else
this%current=>NULL()
ierr=this%set_status_(GFC_IT_DONE)
lep=>NULL()
endif
if(.not.associated(lep)) then; ierr=GFC_NO_MOVE; else; lep=>NULL(); endif
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function ListIterPrevious
!------------------------------------------------------
function ListIterOnFirst(this,ierr) result(res)
!Returns TRUE if the iterator is positioned on the first element of the list
logical:: res !out: result
class(list_iter_t), intent(in):: this !in: list iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
res=.FALSE.; errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) then
errc=GFC_SUCCESS
res=associated(this%current,this%container%first_elem)
endif
if(present(ierr)) ierr=errc
return
end function ListIterOnFirst
!-----------------------------------------------------
function ListIterOnLast(this,ierr) result(res)
!returns TRUE if the iterator is positioned on the last element of the list
logical:: res !out: result
class(list_iter_t), intent(in):: this !in: list iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
res=.FALSE.; errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) then
errc=GFC_SUCCESS
res=associated(this%current,this%container%last_elem)
endif
if(present(ierr)) ierr=errc
return
end function ListIterOnLast
!----------------------------------------------------------------------------------------
#if !(defined(__GNUC__) && __GNUC__ < 9)
function ListIterAppend(this,elem_val,at_top,assoc_only,copy_ctor_f) result(ierr)
#else
function ListIterAppend(this,elem_val,at_top,assoc_only) result(ierr)
#endif
!Appends an element at the beginning or at the end of the list/sublist, either by value or by reference.
!For non-empty iterators, the iterator position is kept unchanged (even if it is GFC_IT_DONE).
!If the iterator is empty on input, its status will be changed to GFC_IT_ACTIVE via reset.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(list_iter_t), intent(inout):: this !inout: iterator
class(*), target, intent(in):: elem_val !in: value to be stored
logical, intent(in), optional:: at_top !in: TRUE:append at the top, FALSE:append at the end (default)
logical, intent(in), optional:: assoc_only !in: storage type: TRUE:by reference, FALSE:by value (default)
#if !(defined(__GNUC__) && __GNUC__ < 9)
procedure(gfc_copy_i), optional:: copy_ctor_f !user-defined generic copy constructor (when storing by value only)
#endif
logical:: assoc,top
integer:: errc
integer(INTL):: nelems
class(list_elem_t), pointer:: lep,oep
if(present(at_top)) then; top=at_top; else; top=.FALSE.; endif
if(present(assoc_only)) then; assoc=assoc_only; else; assoc=.FALSE.; endif
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE.or.ierr.eq.GFC_IT_DONE) then !non-empty container
if(associated(this%container)) then
if(associated(this%container%first_elem).and.associated(this%container%last_elem)) then
ierr=GFC_SUCCESS
if(top) then
oep=>this%container%first_elem%prev_elem !not necessarily NULL for sublists
allocate(this%container%first_elem%prev_elem,STAT=errc)
if(errc.eq.0) lep=>this%container%first_elem%prev_elem
else
oep=>this%container%last_elem%next_elem !not necessarily NULL for sublists
allocate(this%container%last_elem%next_elem,STAT=errc)
if(errc.eq.0) lep=>this%container%last_elem%next_elem
endif
if(errc.eq.0) then
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
call lep%list_elem_ctor(elem_val,ierr,assoc_only=assoc,copy_ctor_f=copy_ctor_f)
else
#endif
call lep%list_elem_ctor(elem_val,ierr,assoc_only=assoc)
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(ierr.eq.GFC_SUCCESS) then
if(top) then
lep%next_elem=>this%container%first_elem
lep%prev_elem=>oep
if(associated(oep)) oep%next_elem=>lep
this%container%first_elem=>lep
else
lep%prev_elem=>this%container%last_elem
lep%next_elem=>oep
if(associated(oep)) oep%prev_elem=>lep
this%container%last_elem=>lep
endif
if(this%container%num_elems_().ge.0) then !quick counting is on
nelems=this%container%update_num_elems_(1_INTL,ierr); if(ierr.ne.GFC_SUCCESS) ierr=GFC_CORRUPTED_CONT
endif
else
if(top) then
deallocate(this%container%first_elem%prev_elem); this%container%first_elem%prev_elem=>oep
else
deallocate(this%container%last_elem%next_elem); this%container%last_elem%next_elem=>oep
endif
ierr=GFC_ERROR
endif
lep=>NULL()
else
if(top) then
this%container%first_elem%prev_elem=>oep
else
this%container%last_elem%next_elem=>oep
endif
ierr=GFC_MEM_ALLOC_FAILED
endif
oep=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
else
ierr=GFC_CORRUPTED_CONT
endif
elseif(ierr.eq.GFC_IT_EMPTY) then !empty container
if(associated(this%container)) then
ierr=GFC_SUCCESS
if(.not.(associated(this%current).or.&
&associated(this%container%first_elem).or.associated(this%container%last_elem))) then
allocate(this%container%first_elem,STAT=errc)
if(errc.eq.0) then
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
call this%container%first_elem%list_elem_ctor(elem_val,ierr,assoc_only=assoc,copy_ctor_f=copy_ctor_f)
else
#endif
call this%container%first_elem%list_elem_ctor(elem_val,ierr,assoc_only=assoc)
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(ierr.eq.GFC_SUCCESS) then
this%container%first_elem%prev_elem=>NULL()
this%container%first_elem%next_elem=>NULL()
this%container%last_elem=>this%container%first_elem
ierr=this%reset() !reset the iterator to GFC_IT_ACTIVE
if(this%container%num_elems_().ge.0) then !quick counting is on
nelems=this%container%update_num_elems_(1_INTL,ierr); if(ierr.ne.GFC_SUCCESS) ierr=GFC_CORRUPTED_CONT
endif
else
deallocate(this%container%first_elem); this%container%first_elem=>NULL()
ierr=GFC_ERROR
endif
else
this%container%first_elem=>NULL()
ierr=GFC_MEM_ALLOC_FAILED
endif
else
ierr=GFC_CORRUPTED_CONT
endif
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function ListIterAppend
!-----------------------------------------------------------------------------------------------------
#if !(defined(__GNUC__) && __GNUC__ < 9)
function ListIterInsertElem(this,elem_val,precede,assoc_only,no_move,copy_ctor_f) result(ierr)
#else
function ListIterInsertElem(this,elem_val,precede,assoc_only,no_move) result(ierr)
#endif
!Inserts a new element in the current position (either right before or right after),
!either by value or by reference. If the iterator is empty it will be set to active
!after inserting the value as the first element of the container.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(list_iter_t), intent(inout):: this !inout: iterator
class(*), target, intent(in):: elem_val !in: value to be stored
logical, intent(in), optional:: precede !in: TRUE:insert before, FALSE:insert after (default)
logical, intent(in), optional:: assoc_only !in: storage type: TRUE:by reference, FALSE:by value (default)
logical, intent(in), optional:: no_move !in: if TRUE, the iterator position does not change, FALSE it does move to the newly added element (default)
#if !(defined(__GNUC__) && __GNUC__ < 9)
procedure(gfc_copy_i), optional:: copy_ctor_f !user-defined generic copy constructor
#endif
logical:: assoc,before,move
integer:: errc
integer(INTL):: nelems
class(list_elem_t), pointer:: lep
if(present(precede)) then; before=precede; else; before=.FALSE.; endif
if(present(assoc_only)) then; assoc=assoc_only; else; assoc=.FALSE.; endif
if(present(no_move)) then; move=.not.no_move; else; move=.TRUE.; endif
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%container)) then
if(associated(this%current)) then
ierr=GFC_SUCCESS
allocate(lep,STAT=errc)
if(errc.eq.0) then
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
call lep%list_elem_ctor(elem_val,ierr,assoc_only=assoc,copy_ctor_f=copy_ctor_f)
else
#endif
call lep%list_elem_ctor(elem_val,ierr,assoc_only=assoc)
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(ierr.eq.GFC_SUCCESS) then
if(before) then
lep%prev_elem=>this%current%prev_elem
lep%next_elem=>this%current
if(associated(this%current%prev_elem)) this%current%prev_elem%next_elem=>lep
this%current%prev_elem=>lep
if(associated(this%current,this%container%first_elem)) this%container%first_elem=>lep
else
lep%next_elem=>this%current%next_elem
lep%prev_elem=>this%current
if(associated(this%current%next_elem)) this%current%next_elem%prev_elem=>lep
this%current%next_elem=>lep
if(associated(this%current,this%container%last_elem)) this%container%last_elem=>lep
endif
if(move) this%current=>lep
if(this%container%num_elems_().ge.0) then !quick counting is on
nelems=this%container%update_num_elems_(1_INTL,ierr); if(ierr.ne.GFC_SUCCESS) ierr=GFC_CORRUPTED_CONT
endif
else
deallocate(lep); ierr=GFC_ERROR
endif
else
ierr=GFC_MEM_ALLOC_FAILED
endif
lep=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
else
ierr=GFC_CORRUPTED_CONT
endif
elseif(ierr.eq.GFC_IT_EMPTY) then
if(associated(this%container)) then
if(.not.(associated(this%current).or.&
&associated(this%container%first_elem).or.associated(this%container%last_elem))) then
ierr=GFC_SUCCESS
allocate(this%container%first_elem,STAT=errc)
if(errc.eq.0) then
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
call this%container%first_elem%list_elem_ctor(elem_val,ierr,assoc_only=assoc,copy_ctor_f=copy_ctor_f)
else
#endif
call this%container%first_elem%list_elem_ctor(elem_val,ierr,assoc_only=assoc)
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(ierr.eq.GFC_SUCCESS) then
this%container%first_elem%prev_elem=>NULL()
this%container%first_elem%next_elem=>NULL()
this%container%last_elem=>this%container%first_elem
ierr=this%reset() !reset the iterator to GFC_IT_ACTIVE (regardless of <no_move>)
if(this%container%num_elems_().ge.0) then !quick counting is on
nelems=this%container%update_num_elems_(1_INTL,ierr); if(ierr.ne.GFC_SUCCESS) ierr=GFC_CORRUPTED_CONT
endif
else
deallocate(this%container%first_elem); this%container%first_elem=>NULL()
ierr=GFC_ERROR
endif
else
this%container%first_elem=>NULL()
ierr=GFC_MEM_ALLOC_FAILED
endif
else
ierr=GFC_CORRUPTED_CONT
endif
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function ListIterInsertElem
!---------------------------------------------------------------------
function ListIterInsertList(this,sublist,precede) result(ierr)
!Inserts another list at the current list iterator position (either before or after).
!The inserted list becomes a sublist after insertion. The iterator position does not change.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(list_iter_t), intent(inout):: this !inout: list iterator
class(list_bi_t), intent(inout):: sublist !in: inserted list (must not be a sublist)
logical, intent(in), optional:: precede !in: if TRUE the sublist will be inserted prior to the current iterator position (defaults to FALSE)
logical:: before
integer(INTL):: nelems,new_elems
if(present(precede)) then; before=precede; else; before=.FALSE.; endif
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
if(associated(sublist%first_elem).and.associated(sublist%last_elem).and.(.not.sublist%is_sublist())) then
ierr=GFC_SUCCESS
if(before) then !insert prior to the current position
if(associated(this%current%prev_elem)) then
this%current%prev_elem%next_elem=>sublist%first_elem
sublist%first_elem%prev_elem=>this%current%prev_elem
else
this%container%first_elem=>sublist%first_elem
endif
sublist%last_elem%next_elem=>this%current
this%current%prev_elem=>sublist%last_elem
else !insert after the current position
if(associated(this%current%next_elem)) then
this%current%next_elem%prev_elem=>sublist%last_elem
sublist%last_elem%next_elem=>this%current%next_elem
else
this%container%last_elem=>sublist%last_elem
endif
sublist%first_elem%prev_elem=>this%current
this%current%next_elem=>sublist%first_elem
endif
call this%container%quick_counting_off_() !turn off quick element counting
call sublist%quick_counting_off_() !turn off quick element counting
else
ierr=GFC_INVALID_ARGS
endif
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function ListIterInsertList
!-------------------------------------------------------------------
function ListIterMoveElem(this,another,precede) result(ierr)
!Moves a list element at the current iterator position into another
!list iterator at its current position, either prior or after it.
!Another iterator will shift to the just added element (prior or after).
!The current iterator will shift to the next element, and if none,
!it will shift to the previous element, and if none, it will become empty.
implicit none
integer(INTD):: ierr !out: error code
class(list_iter_t), intent(inout):: this !inout: source list iterator (from)
class(list_iter_t), intent(inout):: another !inout: destination list iterator (to)
logical, intent(in), optional:: precede !in: if TRUE, the list element will be moved prior, otherwise after (default)
class(list_elem_t), pointer:: list_elem
integer(INTL):: nelems
logical:: before
before=.FALSE.; if(present(precede)) before=precede
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
ierr=another%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
list_elem=>this%pop_(ierr)
if(ierr.eq.GFC_SUCCESS) then
if(before) then
list_elem%prev_elem=>another%current%prev_elem
if(associated(list_elem%prev_elem)) list_elem%prev_elem%next_elem=>list_elem
list_elem%next_elem=>another%current
another%current%prev_elem=>list_elem
if(associated(another%current,another%container%first_elem)) another%container%first_elem=>list_elem
else !after
list_elem%next_elem=>another%current%next_elem
if(associated(list_elem%next_elem)) list_elem%next_elem%prev_elem=>list_elem
list_elem%prev_elem=>another%current
another%current%next_elem=>list_elem
if(associated(another%current,another%container%last_elem)) another%container%last_elem=>list_elem
endif
call another%jump_(list_elem); list_elem=>NULL()
if(another%container%num_elems_().ge.0) then !quick counting is on
nelems=another%container%update_num_elems_(1_INTL,ierr); if(ierr.ne.GFC_SUCCESS) ierr=GFC_CORRUPTED_CONT
endif
endif
elseif(ierr.eq.GFC_IT_EMPTY) then !another iterator was empty
list_elem=>this%pop_(ierr)
if(ierr.eq.GFC_SUCCESS) then
another%container%first_elem=>list_elem
another%container%last_elem=>list_elem
call another%jump_(list_elem); list_elem=>NULL()
if(another%container%num_elems_().ge.0) then !quick counting is on
nelems=another%container%update_num_elems_(1_INTL,ierr); if(ierr.ne.GFC_SUCCESS) ierr=GFC_CORRUPTED_CONT
endif
endif
else
ierr=GFC_INVALID_ARGS
endif
else
ierr=GFC_INVALID_REQUEST
endif
return
end function ListIterMoveElem
!----------------------------------------------------------------------------------------
function ListIterMoveListBas(this,another,max_elems,num_elems_moved) result(ierr)
!Moves the entire list or its part from one iterator to another by
!appending the moved elements right after another list's current iterator
!position. Another iterator will then reposition to the last appended element.
!If <max_elems> is absent, all list elements will be moved from <this> to
!<another> and <this> list will become empty at the end. If <max_elems>
!is present, only up to that number of list elements will be moved,
!starting from the current iterator position of <this>. If the end
!of <this> list is reached before <max_elems> elements have been moved,
!no further elements will be moved. An optional <num_elems_moved> will
!reflect the exact number of list elements moved.
implicit none
integer(INTD):: ierr !out: error code
class(list_iter_t), intent(inout):: this !inout: source list iterator (from)
class(list_iter_t), intent(inout):: another !inout: destination list iterator (to)
integer(INTD), intent(in), optional:: max_elems !in: upper limit on the number of moved elements
integer(INTD), intent(out), optional:: num_elems_moved !out: number of list elements moved
integer(INTD):: n,nelems,num_moved
num_moved=0; ierr=another%get_status()
if(ierr.eq.GFC_IT_ACTIVE.or.ierr.eq.GFC_IT_EMPTY) then
nelems=-1; if(present(max_elems)) nelems=max_elems
if(nelems.ne.0) then
if(nelems.lt.0) then !move all list elements