-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.f90
2443 lines (1708 loc) · 65.8 KB
/
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
module xxmodulebase___list_ftl
!-------------------------------------------------------------------------------
! Copyright : 2022, Fran Martinez Fadrique <Fran.Martinez.Fadrique@gmail.com>
! Project : Atlantis
! Author : Fran Martinez Fadrique
! Language : Object Oriented Fortran 2018
! Reference : http://www.cplusplus.com/reference/list
! Synopsis : Double linked list container template
! 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://www.gnu.org/licenses/>.
!-------------------------------------------------------------------------------
!---USE statements--------------------------------------------------------------
use m_object
use xxuse__
!---End of use statements-------------------------------------------------------
implicit none
!---Public/Private declarations-------------------------------------------------
private
public xxtypebase___list_ftl, xxconstructor___list_ftl
public xxtypebase___list_ftl_iterator
public distance, swap
!---End of public/private declarations------------------------------------------
!---Declaration of module variables---------------------------------------------
! List node type
type t_list_node
private
! The element data instance
class(xxtypebase__), pointer :: element => null()
! Pointer to the previous node in the list (null if first)
type(t_list_node), pointer :: previous => null()
! Pointer to the next mode in the list (null if last)
type(t_list_node), pointer :: next => null()
end type t_list_node
! Double linked list container type
type, extends(t_object) :: xxtypebase___list_ftl
private
! The number of nodes in the list
integer :: count = 0
! The first node
type(t_list_node), pointer :: first => null()
! The last node
type(t_list_node), pointer :: last => null()
contains
! Assign content
generic :: assignment(=) => list_assign_from_list
procedure :: list_assign_from_list
! Iterators
procedure :: begin => list_begin
procedure :: end => list_end
! Capacity
procedure :: empty => list_empty
procedure :: size => list_size
procedure, nopass :: max_size => list_max_size
! Element access
procedure :: front => list_front
procedure :: back => list_back
! Modifiers
generic :: assign => list_assign_from_list, &
list_assign_from_range, &
list_assign_from_fill, &
list_assign_from_array
procedure, private :: list_assign_from_range
procedure, private :: list_assign_from_fill
procedure, private :: list_assign_from_array
procedure :: push_front => list_push_front
procedure :: pop_front => list_pop_front
procedure :: push_back => list_push_back
procedure :: pop_back => list_pop_back
generic :: insert => list_insert_single, &
list_insert_fill, &
list_insert_range, &
list_insert_array
procedure, private :: list_insert_single
procedure, private :: list_insert_fill
procedure, private :: list_insert_range
procedure, private :: list_insert_array
generic :: erase => list_erase_single, &
list_erase_range
procedure, private :: list_erase_single
procedure, private :: list_erase_range
procedure :: swap => list_swap
procedure :: resize => list_resize
procedure :: clear => list_clear
! Operations
generic :: splice => list_splice_list, &
list_splice_range, &
list_splice_single
procedure, private :: list_splice_list
procedure, private :: list_splice_range
procedure, private :: list_splice_single
procedure, private :: list_splice_nodes
procedure :: remove => list_remove
procedure :: remove_if => list_remove_if
procedure :: unique => list_unique
procedure :: merge => list_merge
procedure :: sort => list_sort
procedure, private :: quick_sort
procedure :: reverse => list_reverse
! Selection
procedure :: binary_search => list_binary_search
procedure :: select => list_select
procedure :: at => list_at_get
! Conversion
procedure :: array => list_array
! Destructor
final :: list_
end type xxtypebase___list_ftl
! Constructor interface
interface xxconstructor___list_ftl
module procedure list_default
module procedure list_fill
module procedure list_range
module procedure list_copy
module procedure list_copy_from_array
end interface xxconstructor___list_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
! Double linked list iterator type
type, extends(t_object) :: xxtypebase___list_ftl_iterator
private
! Pointer to the referenced node
! Iterator has no defined constructor
! Iterator is constructed by defaul using this attribute initialisation
type(t_list_node), pointer :: node => null()
contains
! Access
procedure :: get_element => list_iterator_get_element
procedure :: set_element => list_iterator_set_element
procedure :: get_element_ptr => list_iterator_get_element_ptr
! Navigation
procedure :: next => list_iterator_next
procedure :: previous => list_iterator_previous
procedure :: associated => list_iterator_associated
procedure :: nullify => list_iterator_nullify
procedure :: distance => list_iterator_distance
procedure :: swap => list_iterator_swap_iterators
! Assignment
generic :: assignment(=) => list_iterator_assign
procedure, private :: list_iterator_assign
! Comparison operators
generic :: operator(==) => list_iterator_equal
procedure, private :: list_iterator_equal
generic :: operator(/=) => list_iterator_not_equal
procedure, private :: list_iterator_not_equal
end type xxtypebase___list_ftl_iterator
! Interfaces for procedures not bound to type
interface distance
module procedure list_iterator_distance
end interface distance
interface swap
module procedure list_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 list_default() result( res )
! The result list
type(xxtypebase___list_ftl) :: res
! Initialise
res%first => null()
res%last => null()
res%count = 0
end function list_default
! (2) fill constructor
! Constructs a container with n elements.
! Each element is a copy of val.
function list_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___list_ftl) :: res
! Assign input to output
call res%assign( n, val )
end function list_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 list_range( first, last ) result( res )
! Iterator to first node to insert
class(xxtypebase___list_ftl_iterator), intent(in) :: first
! Iterator to last node to insert
class(xxtypebase___list_ftl_iterator), intent(in) :: last
! The result list
type(xxtypebase___list_ftl) :: res
! Assign input to output
call res%assign( first, last )
end function list_range
! (4) copy constructor
! Constructs a container with a copy of each of the elements in other,
! in the same order.
function list_copy( other ) result( res )
! The input list
type(xxtypebase___list_ftl), intent(in) :: other
! The result list
type(xxtypebase___list_ftl) :: res
! Assign input to output
call res%assign( other )
end function list_copy
! Copy constructor from array
function list_copy_from_array( val ) result(res)
! The input array
class(xxtypebase__), dimension(:), intent(in) :: val
! The result list
type(xxtypebase___list_ftl) :: res
! Assign input to output
call res%assign( val )
end function list_copy_from_array
! Assign content
! Assigns new contents to the container, replacing its current contents, and
! modifying its size accordingly.
subroutine list_assign_from_list( this, other )
! The output list
class(xxtypebase___list_ftl), intent(out) :: this
! The input list
class(xxtypebase___list_ftl), intent(in) :: other
! Local node pointers
type(t_list_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 list_assign_from_list
! List destructor
! Destroys the container object.
subroutine list_( this )
! The list
type(xxtypebase___list_ftl), intent(inout) :: this
! Clear the list
if( this%count > 0 ) call this%clear()
end subroutine list_
! 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 list_begin( this ) result(res)
! The list
class(xxtypebase___list_ftl), target, intent(in) :: this
! Pointer to beginning of the list
type(xxtypebase___list_ftl_iterator) :: res
! Return pointer to first node in the list
res%node => this%first
end function list_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 list_end( this ) result(res)
! The list
class(xxtypebase___list_ftl), target, intent(in) :: this
! Pointer to end of the list
type(xxtypebase___list_ftl_iterator) :: res
! Return pointer to the last node in the list
res%node => this%last
end function list_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 list_empty( this ) result(res)
! The list
class(xxtypebase___list_ftl), intent(in) :: this
! The list empty status
logical :: res
! Assign the return value
res = ( this%count == 0 )
end function list_empty
! Return size
! Returns the number of elements in the list container.
pure function list_size( this ) result(res)
! The list
class(xxtypebase___list_ftl), intent(in) :: this
! The list size
integer :: res
! Assign the return value
res = this%count
end function list_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 list_max_size() result(res)
! The list size
integer :: res
! Assign the return value (dummy from C++)
res = 1073741823
end function list_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 list_front( this ) result(res)
! The list
class(xxtypebase___list_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 list_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 list_back( this ) result(res)
! The list
class(xxtypebase___list_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 list_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 list_assign_from_range( this, first, last )
! The output list
class(xxtypebase___list_ftl), intent(out) :: this
! Iterator to first node to insert
class(xxtypebase___list_ftl_iterator), intent(in) :: first
! Iterator to last node to insert
class(xxtypebase___list_ftl_iterator), intent(in) :: last
! Local iterator
type(xxtypebase___list_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 list_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 list_assign_from_fill( this, n, val )
! The output list
class(xxtypebase___list_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 list_assign_from_fill
! Assign a list from an array
subroutine list_assign_from_array( this, val )
! The output list
class(xxtypebase___list_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 list_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 list_push_front( this, val )
! The list
class(xxtypebase___list_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%first%previous )
! Reassign new node pointers
this%first%previous%next => this%first
this%first=> this%first%previous
else
! Allocate memory for first node
allocate(this%first)
! Assign pointers
this%last => this%first
end if
! Assign node before first
this%first%previous => null()
! Copy the element into its list position
call element_assign_pointer( this%first%element, val )
! Increase counter
this%count = this%count + 1
end subroutine list_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 list_pop_front( this )
! The list
class(xxtypebase___list_ftl), intent(inout) :: this
! 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
this%first => this%first%next
deallocate( this%first%previous )
! Assign node before first
this%first%previous => null()
end if
! Decrease counter
this%count = this%count - 1
end if
end subroutine list_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 list_push_back( this, val )
! The list
class(xxtypebase___list_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%next%previous => this%last
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 list_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 list_pop_back( this )
! The list
class(xxtypebase___list_ftl), intent(inout) :: this
! 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; remove the last one
this%last => this%last%previous
deallocate( this%last%next )
! Assign node after last
this%last%next => null()
end if
! Decrease counter
this%count = this%count - 1
end if
end subroutine list_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
! 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 list_insert_single( this, iterator, val ) result(res)
! The list
class(xxtypebase___list_ftl), intent(inout) :: this
! Iterator to node used as reference for insertion
class(xxtypebase___list_ftl_iterator), intent(in) :: iterator
! The element
class(xxtypebase__), intent(in) :: val
! Iterator to the inserted element
type(xxtypebase___list_ftl_iterator) :: res
! Local node pointer
type(t_list_node), pointer :: node
! Allocate new node
allocate( node )
! Assign forward pointers
if( .not. associated(iterator%node%previous) ) then
this%first => node
this%first%previous => null()
else
iterator%node%previous%next => node
end if
node%next => iterator%node
! Assign backward pointers
node%previous => iterator%node%previous
iterator%node%previous => node
! Copy the element into its list position
call element_assign_pointer( node%element, val )
! Increase counter
this%count = this%count + 1
! Return the iterator to the inserted element
res%node => node
end function list_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
! 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 list_insert_fill( this, iterator, n, val ) result(res)
! The list
class(xxtypebase___list_ftl), intent(inout) :: this
! Iterator to node used as reference for insertion
class(xxtypebase___list_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___list_ftl_iterator) :: res
! Local variables
integer :: i
type(xxtypebase___list_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 list_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 list_insert_range( this, iterator, first, last ) result(res)
! The list
class(xxtypebase___list_ftl), intent(inout) :: this
! Iterator to node used as reference for insertion
type(xxtypebase___list_ftl_iterator), intent(in) :: iterator
! Iterator to first node to insert
class(xxtypebase___list_ftl_iterator), intent(in) :: first
! Iterator to last node to insert
class(xxtypebase___list_ftl_iterator), intent(in) :: last
! Iterator to the inserted element
type(xxtypebase___list_ftl_iterator) :: res
! Local nodes
type(t_list_node), pointer :: inode
type(t_list_node), pointer :: node
type(t_list_node), pointer :: previous
type(xxtypebase___list_ftl_iterator) :: it
! Allocate first node to keep if in return
allocate(node)
res%node => node
! Initialise input node navigation
inode => first%node
it = iterator
previous => it%node%previous
! Navigate the input nodes
! Exits on last%next or null
do
! Assign forward pointers
if( associated(it%node,this%first) ) then
! Inserting at the beginning of the list
this%first => node
this%first%previous => null()
else
! Inserting in an arbitrary position
previous%next => node
end if
node%next => it%node
! Assign backward pointers
node%previous => previous
it%node%previous => 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 => inode%next
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 list_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 list_insert_array( this, iterator, val ) result(res)
! The list
class(xxtypebase___list_ftl), intent(inout) :: this
! Iterator to node used as reference for insertion
class(xxtypebase___list_ftl_iterator), intent(in) :: iterator
! The element
class(xxtypebase__), dimension(:), intent(in) :: val
! Iterator to the inserted element
type(xxtypebase___list_ftl_iterator) :: res
! Local variables
integer :: i
type(xxtypebase___list_ftl_iterator) :: it
! Insert the first element to store the iterator
res = this%insert( iterator, val(1) )
! Loop on the rest of elements
do i = 2, size(val)
! ***** Provisionally use the insert_single function *****
! ***** This may be optimised for speed in further versions *****
it = this%insert( iterator, val(i) )
end do
end function list_insert_array
! Erase elements
! Removes from the list container either a single element (position)
! This effectively reduces the container size by one element, which is destroyed.
! Input iterator returns not associated