-
Notifications
You must be signed in to change notification settings - Fork 0
/
slist.f90
2531 lines (1768 loc) · 68.7 KB
/
slist.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
module xxmodulebase___slist_ftl
!-------------------------------------------------------------------------------
! Copyright : 2022, Fran Martinez Fadrique <[email protected]>
! Project : Atlantis
! Author : Fran Martinez Fadrique
! Language : Object Oriented Fortran 2018
! Reference : http:https://www.cplusplus.com/reference/list
! Synopsis : Single linked list container template
! Despite of the STL forward_list, this container has been built
! to mimic the list functionality in the understanding of the
! limitations of slist with respect to reverse iteration.
! Limitations with respect to STL C++
! - No reverse iteration.
! - No constant iterators.
! - Max size is dummy (dummy value not computed from architecture).
! - No emplace functions.
! - Splice can append lists with an extension on the STL C++
! interface that emulates the past-last-element with null iterator
!
! License : This file is part of the Fortran Template Library (FTL).
!
! FTL 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.
!
! FTL 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 FTL.
! If not, see <http:https://www.gnu.org/licenses/>.
!-------------------------------------------------------------------------------
!---USE statements--------------------------------------------------------------
use m_object
use xxuse__
!---End of use statements-------------------------------------------------------
implicit none
!---Public/Private declarations-------------------------------------------------
private
public xxtypebase___slist_ftl, xxconstructor___slist_ftl
public xxtypebase___slist_ftl_iterator
public distance, swap
!---End of public/private declarations------------------------------------------
!---Declaration of module variables---------------------------------------------
! List node type
type t_slist_node
private
! The element data instance
class(xxtypebase__), pointer :: element => null()
! Pointer to the next mode in the list (null if last)
type(t_slist_node), pointer :: next => null()
end type t_slist_node
! Single linked list container type
type, extends(t_object) :: xxtypebase___slist_ftl
private
! The number of nodes in the list
integer :: count = 0
! The first node
type(t_slist_node), pointer :: first => null()
! The last node
type(t_slist_node), pointer :: last => null()
contains
! Assign content
generic :: assignment(=) => slist_assign_from_slist
procedure :: slist_assign_from_slist
! Iterators
procedure :: begin => slist_begin
procedure :: end => slist_end
! Capacity
procedure :: empty => slist_empty
procedure :: size => slist_size
procedure, nopass :: max_size => slist_max_size
! Element access
procedure :: front => slist_front
procedure :: back => slist_back
! Modifiers
generic :: assign => slist_assign_from_slist, &
slist_assign_from_range, &
slist_assign_from_fill, &
slist_assign_from_array
procedure, private :: slist_assign_from_range
procedure, private :: slist_assign_from_fill
procedure, private :: slist_assign_from_array
procedure :: push_front => slist_push_front
procedure :: pop_front => slist_pop_front
procedure :: push_back => slist_push_back
procedure :: pop_back => slist_pop_back
generic :: insert => slist_insert_single, &
slist_insert_fill, &
slist_insert_range, &
slist_insert_array
procedure, private :: slist_insert_single
procedure, private :: slist_insert_fill
procedure, private :: slist_insert_range
procedure, private :: slist_insert_array
generic :: erase => slist_erase_single, &
slist_erase_range
procedure, private :: slist_erase_single
procedure, private :: slist_erase_range
procedure :: swap => slist_swap
procedure :: resize => slist_resize
procedure :: clear => slist_clear
! Operations
generic :: splice => slist_splice_slist, &
slist_splice_range, &
slist_splice_single
procedure, private :: slist_splice_slist
procedure, private :: slist_splice_range
procedure, private :: slist_splice_single
procedure, private :: slist_splice_nodes
procedure :: remove => slist_remove
procedure :: remove_if => slist_remove_if
procedure :: unique => slist_unique
procedure :: merge => slist_merge
procedure :: sort => slist_sort
procedure, private :: quick_sort
procedure :: reverse => slist_reverse
! Selection
procedure :: binary_search => slist_binary_search
procedure :: select => slist_select
procedure :: at => slist_at_get
! Conversion
procedure :: array => slist_array
! Destructor
final :: slist_
end type xxtypebase___slist_ftl
! Constructor interface
interface xxconstructor___slist_ftl
module procedure slist_default
module procedure slist_fill
module procedure slist_range
module procedure slist_copy
module procedure slist_copy_from_array
end interface xxconstructor___slist_ftl
! Interface to provide user comparison functions
abstract interface
pure function comparison( from_list, reference ) result(res)
use xxuse__
class(xxtypebase__), intent(in) :: from_list
class(xxtypebase__), intent(in) :: reference
logical :: res
end function comparison
end interface
! Interface to provide predicate algorithm to the contained element
abstract interface
pure function predicate( a ) result(res)
use xxuse__
class(xxtypebase__), intent(in) :: a
logical :: res
end function predicate
end interface
! Interface to provide binary predicate algorithm to the contained elements
abstract interface
pure function binary_predicate( a, b ) result(res)
use xxuse__
class(xxtypebase__), intent(in) :: a
class(xxtypebase__), intent(in) :: b
logical :: res
end function binary_predicate
end interface
! Single linked list iterator type
type, extends(t_object) :: xxtypebase___slist_ftl_iterator
private
! Pointer to the referenced node
! Iterator has no defined constructor
! Iterator is constructed by defaul using this attribute initialisation
type(t_slist_node), pointer :: node => null()
! Pointer to the containing list; required for reverse navigation operations
type(xxtypebase___slist_ftl), pointer :: parent => null()
contains
! Access
procedure :: get_element => slist_iterator_get_element
procedure :: set_element => slist_iterator_set_element
procedure :: get_element_ptr => slist_iterator_get_element_ptr
! Navigation
procedure :: next => slist_iterator_next
procedure :: previous => slist_iterator_previous
procedure :: associated => slist_iterator_associated
procedure :: nullify => slist_iterator_nullify
procedure :: distance => slist_iterator_distance
procedure :: swap => slist_iterator_swap_iterators
! Assignment
generic :: assignment(=) => slist_iterator_assign
procedure, private :: slist_iterator_assign
! Comparison operators
generic :: operator(==) => slist_iterator_equal
procedure, private :: slist_iterator_equal
generic :: operator(/=) => slist_iterator_not_equal
procedure, private :: slist_iterator_not_equal
end type xxtypebase___slist_ftl_iterator
! Interfaces for procedures not bound to type
interface distance
module procedure slist_iterator_distance
end interface distance
interface swap
module procedure slist_iterator_swap_iterators
end interface swap
!---End of declaration of module variables--------------------------------------
contains
! (1) empty container constructor (default constructor)
! Constructs an empty container, with no elements.
function slist_default() result( res )
! The result list
type(xxtypebase___slist_ftl) :: res
! Initialise
res%first => null()
res%last => null()
res%count = 0
end function slist_default
! (2) fill constructor
! Constructs a container with n elements.
! Each element is a copy of val.
function slist_fill( n, val ) result( res )
! The number of elements
integer, intent(in) :: n
! The element to use to fill the list
class(xxtypebase__), intent(in) :: val
! The result list
type(xxtypebase___slist_ftl) :: res
! Assign input to output
call res%assign( n, val )
end function slist_fill
! (3) range constructor
! Constructs a container with as many elements as the range (first,last),
! with each element constructed from its corresponding element in that range,
! in the same order.
! This interface is also designed to allow inheritance of the list type
! and then to extend also the list_iterator type such that the derived list type
! can invoke this method with the derivied list_iterator type
function slist_range( first, last ) result( res )
! Iterator to first node to insert
class(xxtypebase___slist_ftl_iterator), intent(in) :: first
! Iterator to last node to insert
class(xxtypebase___slist_ftl_iterator), intent(in) :: last
! The result list
type(xxtypebase___slist_ftl) :: res
! Assign input to output
call res%assign( first, last )
end function slist_range
! (4) copy constructor
! Constructs a container with a copy of each of the elements in other,
! in the same order.
function slist_copy( other ) result( res )
! The input list
type(xxtypebase___slist_ftl), intent(in) :: other
! The result list
type(xxtypebase___slist_ftl) :: res
! Assign input to output
call res%assign( other )
end function slist_copy
! Copy constructor from array
function slist_copy_from_array( val ) result(res)
! The input array
class(xxtypebase__), dimension(:), intent(in) :: val
! The result list
type(xxtypebase___slist_ftl) :: res
! Assign input to output
call res%assign( val )
end function slist_copy_from_array
! Assign content
! Assigns new contents to the container, replacing its current contents, and
! modifying its size accordingly.
subroutine slist_assign_from_slist( this, other )
! The output list
class(xxtypebase___slist_ftl), intent(out) :: this
! The input list
class(xxtypebase___slist_ftl), intent(in) :: other
! Local node pointers
type(t_slist_node), pointer :: lptr
! Initialise navigation pointer
lptr => other%first
! Loop on the list
do while( associated(lptr) )
! Add element to the output list
call this%push_back( lptr%element )
! Iterate
lptr => lptr%next
end do
end subroutine slist_assign_from_slist
! List destructor
! Destroys the container object.
subroutine slist_( this )
! The list
type(xxtypebase___slist_ftl), intent(inout) :: this
! Clear the list
if( this%count > 0 ) call this%clear()
end subroutine slist_
! Return iterator to beginning
! Returns an iterator pointing to the first element in the list container.
! If the container is empty, the returned iterator value shall not be dereferenced.
function slist_begin( this ) result(res)
! The list
class(xxtypebase___slist_ftl), target, intent(in) :: this
! Pointer to beginning of the list
type(xxtypebase___slist_ftl_iterator) :: res
! Return pointer to first node in the list
res%node => this%first
res%parent => this
end function slist_begin
! Return iterator to end
! Returns an iterator referring to the last element in the list container.
! If the container is empty, the returned iterator value shall not be dereferenced.
function slist_end( this ) result(res)
! The list
class(xxtypebase___slist_ftl), target, intent(in) :: this
! Pointer to end of the list
type(xxtypebase___slist_ftl_iterator) :: res
! Return pointer to the last node in the liss
res%node => this%last
res%parent => this
end function slist_end
! Test whether container is empty
! Returns whether the list container is empty (i.e. whether its size is 0).
! This function does not modify the container in any way.
pure function slist_empty( this ) result(res)
! The list
class(xxtypebase___slist_ftl), intent(in) :: this
! The list empty status
logical :: res
! Assign the return value
res = ( this%count == 0 )
end function slist_empty
! Return size
! Returns the number of elements in the list container.
pure function slist_size( this ) result(res)
! The list
class(xxtypebase___slist_ftl), intent(in) :: this
! The list size
integer :: res
! Assign the return value
res = this%count
end function slist_size
! Return maximum size
! Returns the maximum number of elements that the list container can hold.
! This is the maximum potential size the container can reach due to known system
! or library implementation limitations, but the container is by no means
! guaranteed to be able to reach that size: it can still fail to allocate
! storage at any point before that size is reached.
pure function slist_max_size() result(res)
! The list size
integer :: res
! Assign the return value (dummy from C++)
res = 1073741823
end function slist_max_size
! Access first element
! Returns a reference to the first element in the list container.
! Calling this function on an empty container causes undefined behaviour.
pure function slist_front( this ) result(res)
! The list
class(xxtypebase___slist_ftl), intent(in) :: this
! Pointer to the element in the first node in the list
class(xxtypebase__), allocatable :: res
! Assign the return value
call element_assign_allocatable( res, this%first%element )
end function slist_front
! Access last element
! Returns a reference to the last element in the list container.
! Calling this function on an empty container causes undefined behaviour.
pure function slist_back( this ) result(res)
! The list
class(xxtypebase___slist_ftl), intent(in) :: this
! Pointer to the element in the last node in the list
class(xxtypebase__), allocatable :: res
! Assign the return value
call element_assign_allocatable( res, this%last%element )
end function slist_back
! Assign new content to container
! Assigns new contents to the list container, replacing its current contents,
! and modifying its size accordingly.
! (1), the new contents are elements constructed from each of the elements in the
! range between first and last, in the same order.
! This interface is also designed to allow inheritance of the list type
! and then to extend also the list_iterator type such that the derived list type
! can invoke this method with the derivied list_iterator type
subroutine slist_assign_from_range( this, first, last )
! The output list
class(xxtypebase___slist_ftl), intent(out) :: this
! Iterator to first node to insert
class(xxtypebase___slist_ftl_iterator), intent(in) :: first
! Iterator to last node to insert
class(xxtypebase___slist_ftl_iterator), intent(in) :: last
! Local iterator
type(xxtypebase___slist_ftl_iterator) :: it
! Initialise navigation pointer
it = first
! Loop on the list
do while( it%associated() )
! Add this element
call this%push_back( it%node%element )
! Check if this was the last element
if( it == last ) exit
! Iterate
it = it%next()
end do
end subroutine slist_assign_from_range
! Assign new content to container
! Assigns new contents to the list container, replacing its current contents,
! and modifying its size accordingly.
! (2), the new contents are n elements, each initialized to a copy of val.
subroutine slist_assign_from_fill( this, n, val )
! The output list
class(xxtypebase___slist_ftl), intent(out) :: this
! The number of elements
integer, intent(in) :: n
! The element to used to populate the container
class(xxtypebase__), intent(in) :: val
! Local variables
integer :: i
! Loop on the number of elements
do i = 1, n
! Add this element
call this%push_back( val )
end do
end subroutine slist_assign_from_fill
! Assign a list from an array
subroutine slist_assign_from_array( this, val )
! The output list
class(xxtypebase___slist_ftl), intent(out) :: this
! The input array
class(xxtypebase__), dimension(:), intent(in) :: val
! Local counter
integer :: i
! Loop on the input array
do i = 1, size(val)
! Add element to the output list
call this%push_back( val(i) )
end do
end subroutine slist_assign_from_array
! Insert element at beginning
! Inserts a new element at the beginning of the list, right before its current
! first element. The content of val is copied (or moved) to the inserted element.
! This effectively increases the container size by one.
subroutine slist_push_front( this, val )
! The list
class(xxtypebase___slist_ftl), intent(inout) :: this
! The element
class(xxtypebase__), intent(in) :: val
! Local node pointer
type(t_slist_node), pointer :: node
! Allocate the new node
allocate( node )
! Check if list already contains elements
if( associated(this%last) ) then
! Reasgign new node pointers
node%next => this%first
this%first => node
else
! Assign pointers
this%first => node
this%last => this%first
end if
! Copy the element into its list position
call element_assign_pointer( this%first%element, val )
! Increase counter
this%count = this%count + 1
end subroutine slist_push_front
! Delete first element
! Removes the first element in the list container,
! effectively reducing its size by one.
! This destroys the removed element.
subroutine slist_pop_front( this )
! The list
class(xxtypebase___slist_ftl), intent(inout) :: this
! Local node pointer
type(t_slist_node), pointer :: node
! Check that the list is not empty
if( associated(this%first) ) then
! Destroy data element in the first list node
deallocate( this%first%element )
! Check if there is more than one node
if( .not. associated(this%first%next) ) then
! Only one node in list; remove it
deallocate( this%first )
this%last => null()
else
! More than one node in the list; remove the first one
node => this%first
this%first => this%first%next
deallocate( node )
end if
! Decrease counter
this%count = this%count - 1
end if
end subroutine slist_pop_front
! Add element at the end
! Adds a new element at the end of the list container, after its current
! last element. The content of val is copied (or moved) to the new element.
! This effectively increases the container size by one.
subroutine slist_push_back( this, val )
! The list
class(xxtypebase___slist_ftl), intent(inout) :: this
! The element
class(xxtypebase__), intent(in) :: val
! Check if list already contains elements
if( associated(this%last) ) then
! Allocate new node
allocate( this%last%next )
! Reassign new node pointers
this%last => this%last%next
else
! Allocate memory for first node
allocate(this%first)
! Assign pointers
this%last => this%first
end if
! Assign node after last
this%last%next => null()
! Copy the element into its list position
call element_assign_pointer( this%last%element, val )
! Increase counter
this%count = this%count + 1
end subroutine slist_push_back
! Delete last element
! Removes the last element in the list container,
! effectively reducing the container size by one.
! This destroys the removed element.
subroutine slist_pop_back( this )
! The list
class(xxtypebase___slist_ftl), intent(inout) :: this
! Local node pointer
type(t_slist_node), pointer :: node
! Check that the list is not empty
if( associated(this%first) ) then
! Destroy data element in the last list node
deallocate( this%last%element )
! Check if there is more than one node
if( .not. associated(this%first%next) ) then
! Only one node in list; remove it
deallocate( this%last )
this%first => null()
else
! More than one node in the list; look for the last-but-one node
call previous_node( this%last, this%first, node )
! Remove the last node
deallocate( node%next )
! Reassign last list pointer
this%last => node
end if
! Decrease counter
this%count = this%count - 1
end if
end subroutine slist_pop_back
! Insert elements
! The container is extended by inserting new elements before the element at
! the specified position.
! This effectively increases the list size by one.
! Iterator remains associated to the node in input
! Note that this operation is expensive as the node before the insertion
! position must be located since the input node knows nothing about
! its predecesor
function slist_insert_single( this, iterator, val ) result(res)
! The list
class(xxtypebase___slist_ftl), target, intent(inout) :: this
! Iterator to node used as reference for insertion
class(xxtypebase___slist_ftl_iterator), intent(in) :: iterator
! The element
class(xxtypebase__), intent(in) :: val
! Iterator to the inserted element
type(xxtypebase___slist_ftl_iterator) :: res
! Local node pointers
type(t_slist_node), pointer :: new
type(t_slist_node), pointer :: node
! Allocate new node
allocate( new )
! Check if input iterator points to first element
if( associated(iterator%node,this%first) ) then
! Associate pointers
new%next => this%first
this%first => new
else
! Look for the node before the insertion node
call previous_node( iterator%node, this%first, node )
! Associate pointers
new%next => node%next
node%next => new
end if
! Copy the element into its list position
call element_assign_pointer( new%element, val )
! Increase counter
this%count = this%count + 1
! Return the iterator to the inserted element
res%node => new
res%parent => this
end function slist_insert_single
! Insert elements
! The container is extended by inserting new elements before the element at
! the specified position.
! This effectively increases the list size by n.
! Iterator remains associated to the node in input
! Note that this operation is expensive as the node before the insertion
! position must be located since the input node knows nothing about
! its predecesor
! This interface is also designed to allow inheritance of the list type
! and then to extend also the list_iterator type such that the derived list type
! can invoke this method with the derivied list_iterator type
function slist_insert_fill( this, iterator, n, val ) result(res)
! The list
class(xxtypebase___slist_ftl), intent(inout) :: this
! Iterator to node used as reference for insertion
class(xxtypebase___slist_ftl_iterator), intent(in) :: iterator
! The number of times to insert the element
integer, intent(in) :: n
! The element
class(xxtypebase__), intent(in) :: val
! Iterator to the inserted element
type(xxtypebase___slist_ftl_iterator) :: res
! Local variables
integer :: i
type(xxtypebase___slist_ftl_iterator) :: it
! Insert the first element to store the iterator
res = this%insert( iterator, val )
! Insert the rest of the element
do i = 2, n
! ***** Provisionally use the insert_single function *****
! ***** This may be optimised for speed in further versions *****
it = this%insert( iterator, val )
end do
end function slist_insert_fill
! Insert elements
! The container is extended by inserting new elements before the element at
! the specified position.
! This effectively increases the list size by the number of element in (first,last].
! Iterator remains associated to the node in input
! This assumes that the range (first,last) is an actual connected range,
! i.e. it is possible to navigate from first to last, otherwise the resulting
! list is corrupted.
! This interface is also designed to allow inheritance of the list type
! and then to extend also the list_iterator type such that the derived list type
! can invoke this method with the derivied list_iterator type
function slist_insert_range( this, iterator, first, last ) result(res)
! The list
class(xxtypebase___slist_ftl), target, intent(inout) :: this
! Iterator to node used as reference for insertion
type(xxtypebase___slist_ftl_iterator), intent(in) :: iterator
! Iterator to first node to insert
class(xxtypebase___slist_ftl_iterator), intent(in) :: first
! Iterator to last node to insert
class(xxtypebase___slist_ftl_iterator), intent(in) :: last
! Iterator to the inserted element
type(xxtypebase___slist_ftl_iterator) :: res
! Local nodes
type(t_slist_node), pointer :: inode
type(t_slist_node), pointer :: node
type(t_slist_node), pointer :: previous, nextnode
type(xxtypebase___slist_ftl_iterator) :: it
! Check the insertion point position
if( associated(iterator%node,this%first) ) then
! Inserting before the first node
previous => null()
else
! Locate the node previous to insertion
! The insertion process takes place always between previous and iterator
previous => this%first
do while(.not. associated(previous%next,iterator%node) )
previous => previous%next
end do
end if
! Allocate first node to keep it in return
allocate(node)
res%node => node
res%parent => this
! Initialise input node navigation
inode => first%node
it = iterator
! Navigate the input nodes
! Exits on last%next or null
do
! Keep the next node to preserve it after pointer assignment
nextnode => inode%next
! Assign forward pointers
if( associated(it%node,this%first) ) then
! Inserting at the beginning of the list
this%first => node
else
! Inserting in an arbitrary position
previous%next => node
end if
node%next => it%node
! Copy the element into its list position
call element_assign_pointer( node%element, inode%element )
! Increase counter
this%count = this%count + 1
! Iterate
inode => nextnode
if( .not. associated(inode) ) exit
if( associated(inode,last%node%next) ) exit
! Previous node is now the just inserted node
previous => node
! Allocate new node
! In each iteration, the control of the memory behind node is passed
! to the node within the list. Allocation creates a new node each time.
allocate( node )
end do
end function slist_insert_range
! Insert elements
! The container is extended by inserting new elements before the element at
! the specified position.
! This effectively increases the list size by the size of the array.
! Iterator remains associated to the node in input
! This interface is also designed to allow inheritance of the list type
! and then to extend also the list_iterator type such that the derived list type
! can invoke this method with the derivied list_iterator type
function slist_insert_array( this, iterator, val ) result(res)
! The list
class(xxtypebase___slist_ftl), intent(inout) :: this