-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.f90
2666 lines (1860 loc) · 73 KB
/
vector.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___vector_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/vector
! http:https://www.cplusplus.com/reference/list
! Synopsis : Vector (dynamic) 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.
! - No operator [].
! - 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___vector_ftl, xxconstructor___vector_ftl
public xxtypebase___vector_ftl_iterator
public distance, swap
!---End of public/private declarations------------------------------------------
!---Declaration of module variables---------------------------------------------
! Vector node type
type t_node
private
! The element data instance
class(xxtypebase__), pointer :: element => null()
end type t_node
! Vector container type
type, extends(t_object) :: xxtypebase___vector_ftl
private
! The number of nodes in the vector
integer :: count = 0
! The internal array
type(t_node), dimension(:), allocatable :: data
contains
! Assign content
generic :: assignment(=) => vector_assign_from_vector
procedure :: vector_assign_from_vector
! Destructor
final :: vector_
! Iterators
procedure :: begin => vector_begin
procedure :: end => vector_end
! Capacity
procedure :: empty => vector_empty
procedure :: size => vector_size
procedure, nopass :: max_size => vector_max_size
procedure :: capacity => vector_capacity
! Element access
procedure :: front => vector_front
procedure :: back => vector_back
! Modifiers
generic :: assign => vector_assign_from_vector, &
vector_assign_from_range, &
vector_assign_from_fill, &
vector_assign_from_array
procedure, private :: vector_assign_from_range
procedure, private :: vector_assign_from_fill
procedure, private :: vector_assign_from_array
procedure :: push_front => vector_push_front
procedure :: pop_front => vector_pop_front
procedure :: push_back => vector_push_back
procedure :: pop_back => vector_pop_back
generic :: insert => vector_insert_single, &
vector_insert_single_at, &
vector_insert_fill, &
vector_insert_fill_at, &
vector_insert_range, &
vector_insert_range_at, &
vector_insert_array, &
vector_insert_array_at
procedure, private :: vector_insert_single
procedure, private :: vector_insert_single_at
procedure, private :: vector_insert_fill
procedure, private :: vector_insert_fill_at
procedure, private :: vector_insert_range
procedure, private :: vector_insert_range_at
procedure, private :: vector_insert_array
procedure, private :: vector_insert_array_at
generic :: erase => vector_erase_single, &
vector_erase_single_at, &
vector_erase_range, &
vector_erase_range_at
procedure, private :: vector_erase_single
procedure, private :: vector_erase_single_at
procedure, private :: vector_erase_range
procedure, private :: vector_erase_range_at
generic :: swap => vector_swap_index, &
vector_swap
procedure, private :: vector_swap_index
procedure, private :: vector_swap
procedure :: resize => vector_resize
procedure :: clear => vector_clear
! Operations
generic :: splice => vector_splice_vector, &
vector_splice_range, &
vector_splice_single
procedure, private :: vector_splice_vector
procedure, private :: vector_splice_range
procedure, private :: vector_splice_single
procedure, private :: vector_splice_nodes
procedure :: remove => vector_remove
procedure :: remove_if => vector_remove_if
procedure :: unique => vector_unique
procedure :: merge => vector_merge
procedure :: sort => vector_sort
procedure, private :: quick_sort
procedure :: reverse => vector_reverse
! Selection
procedure :: binary_search => vector_binary_search
procedure :: select => vector_select
procedure :: at => vector_at_get
! Element access
procedure :: get_element => vector_element_from_index
procedure :: get_element_ptr => vector_element_ptr_from_index
procedure :: set_element => vector_element_to_index
! Conversion
procedure :: array => vector_array
! Memory allocation management
procedure :: reserve => vector_reserve
procedure, private :: realloc => vector_realloc
end type xxtypebase___vector_ftl
! Reference vector size parameters
integer, parameter :: vector_base_capacity = 100
real, parameter :: vector_base_capacity_increase = 1.5
! Constructor interface
interface xxconstructor___vector_ftl
module procedure vector_default
module procedure vector_fill
module procedure vector_range
module procedure vector_copy
module procedure vector_copy_from_array
end interface xxconstructor___vector_ftl
! Interface to provide user comparison functions
abstract interface
pure function comparison( from_vector, reference ) result(res)
use xxuse__
class(xxtypebase__), intent(in) :: from_vector
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
! Vector iterator type
type, extends(t_object) :: xxtypebase___vector_ftl_iterator
private
! The element index within the array (iterator not initialised if 0)
integer :: idx = 0
! Pointer to the container vector
type(xxtypebase___vector_ftl), pointer :: parent => null()
contains
! Access
procedure :: get_element => vector_iterator_get_element
procedure :: set_element => vector_iterator_set_element
procedure :: get_element_ptr => vector_iterator_get_element_ptr
! Navigation
procedure :: next => vector_iterator_next
procedure :: previous => vector_iterator_previous
procedure :: associated => vector_iterator_associated
procedure :: nullify => vector_iterator_nullify
procedure :: index => vector_iterator_index
procedure :: distance => vector_iterator_distance
procedure :: swap => vector_iterator_swap_iterators
! Assignment
generic :: assignment(=) => vector_iterator_assign
procedure, private :: vector_iterator_assign
! Comparison operators
generic :: operator(==) => vector_iterator_equal
procedure, private :: vector_iterator_equal
generic :: operator(/=) => vector_iterator_not_equal
procedure, private :: vector_iterator_not_equal
end type xxtypebase___vector_ftl_iterator
! Interfaces for procedures not bound to type
interface distance
module procedure vector_iterator_distance
end interface distance
interface swap
module procedure vector_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 vector_default() result( res )
! The result vector
type(xxtypebase___vector_ftl) :: res
! Initialise counter
res%count = 0
end function vector_default
! (2) fill constructor
! Constructs a container with n elements.
! Each element is a copy of val.
function vector_fill( n, val ) result( res )
! The number of elements
integer, intent(in) :: n
! The element to use to fill the vector
class(xxtypebase__), intent(in) :: val
! The result vector
type(xxtypebase___vector_ftl) :: res
! Assign input to output
call res%assign( n, val )
end function vector_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 vector_range( first, last ) result( res )
! Iterator to first node to insert
class(xxtypebase___vector_ftl_iterator), intent(in) :: first
! Iterator to last node to insert
class(xxtypebase___vector_ftl_iterator), intent(in) :: last
! The result vector
type(xxtypebase___vector_ftl) :: res
! Assign input to output
call res%assign( first, last )
end function vector_range
! (4) copy constructor
! Constructs a container with a copy of each of the elements in other,
! in the same order.
function vector_copy( other ) result( res )
! The input vector
type(xxtypebase___vector_ftl), intent(in) :: other
! The result vector
type(xxtypebase___vector_ftl) :: res
! Assign input to output
call res%assign( other )
end function vector_copy
! Copy constructor from array
function vector_copy_from_array( val ) result(res)
! The input array
class(xxtypebase__), dimension(:), intent(in) :: val
! The result vector
type(xxtypebase___vector_ftl) :: res
! Assign input to output
call res%assign( val )
end function vector_copy_from_array
! Assign content
! Assigns new contents to the container, replacing its current contents, and
! modifying its size accordingly.
subroutine vector_assign_from_vector( this, other )
! The output vector
class(xxtypebase___vector_ftl), intent(out) :: this
! The input vector
type(xxtypebase___vector_ftl), intent(in) :: other
! Local variables
integer :: i
! Check if source is allocated
if( other%count > 0 ) then
! Allocate memory
allocate(this%data( size(other%data) ))
! Copy the vector
do i = 1, other%count
allocate( this%data(i)%element, source=other%data(i)%element )
end do
! Set counter
this%count = other%count
end if
end subroutine vector_assign_from_vector
! Vector destructor
! Destroys the container object.
subroutine vector_( this )
! The vector
type(xxtypebase___vector_ftl), intent(inout) :: this
! Clear the vector
call this%clear()
end subroutine vector_
! Return iterator to beginning
! Returns an iterator pointing to the first element in the vector container.
! If the container is empty, the returned iterator value shall not be dereferenced.
function vector_begin( this ) result(res)
! The vector
class(xxtypebase___vector_ftl), target, intent(in) :: this
! Pointer to beginning of the vector
type(xxtypebase___vector_ftl_iterator) :: res
! Return pointer to first node in the vector
if( this%count > 0 ) then
res%idx = 1
else
res%idx = 0
end if
! Assign the parent container
res%parent => this
end function vector_begin
! Return iterator to end
! Returns an iterator referring to the last element in the vector container.
! If the container is empty, the returned iterator value shall not be dereferenced.
function vector_end( this ) result(res)
! The vector
class(xxtypebase___vector_ftl), target, intent(in) :: this
! Pointer to end of the vector
type(xxtypebase___vector_ftl_iterator) :: res
! Return pointer to the last stored node in the vector
res%idx = this%count
! Assign the parent container
res%parent => this
end function vector_end
! Test whether container is empty
! Returns whether the vector container is empty (i.e. whether its size is 0).
! This function does not modify the container in any way.
pure function vector_empty( this ) result(res)
! The vector
class(xxtypebase___vector_ftl), intent(in) :: this
! The vector empty status
logical :: res
! Assign the return value
res = ( this%count == 0 )
end function vector_empty
! Return size
! Returns the number of elements in the vector container.
pure function vector_size( this ) result(res)
! The vector
class(xxtypebase___vector_ftl), intent(in) :: this
! The vector size
integer :: res
! Assign the return value
res = this%count
end function vector_size
! Return maximum size
! Returns the maximum number of elements that the vector 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 vector_max_size() result(res)
! The vector size
integer :: res
! Assign the return value (dummy from C++)
res = 1073741823
end function vector_max_size
! Return size of allocated storage capacity
! Returns the size of the storage space currently allocated for the vector,
! expressed in terms of elements.
! This capacity is not necessarily equal to the vector size.
! It can be equal or greater, with the extra space allowing to accommodate
! for growth without the need to reallocate on each insertion.
! Notice that this capacity does not suppose a limit on the size of the vector.
! When this capacity is exhausted and more is needed, it is automatically expanded
! by the container (reallocating it storage space).
! The capacity of a vector can be explicitly altered by calling member vecto::reserve.
pure function vector_capacity( this ) result(res)
! The vector
class(xxtypebase___vector_ftl), intent(in) :: this
! The vector capacity
integer :: res
! Assign the return value (dummy from C++)
if( allocated(this%data) ) then
res = size(this%data)
else
res = 0
end if
end function vector_capacity
! Access first element
! Returns a reference to the first element in the vector container.
! Calling this function on an empty container causes undefined behaviour.
pure function vector_front( this ) result(res)
! The vector
class(xxtypebase___vector_ftl), intent(in) :: this
! Pointer to the element in the first node in the vector
class(xxtypebase__), allocatable :: res
! Assign the return value
call element_assign_allocatable( res, this%data(1)%element )
end function vector_front
! Access last element
! Returns a reference to the last element in the vector container.
! Calling this function on an empty container causes undefined behaviour.
pure function vector_back( this ) result(res)
! The vector
class(xxtypebase___vector_ftl), intent(in) :: this
! Pointer to the element in the last node in the vector
class(xxtypebase__), allocatable :: res
! Assign the return value
call element_assign_allocatable( res, this%data(this%count)%element )
end function vector_back
! Assign new content to container
! Assigns new contents to the vector 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 vector_assign_from_range( this, first, last )
! The output vector
class(xxtypebase___vector_ftl), intent(out) :: this
! Iterator to first node to insert
class(xxtypebase___vector_ftl_iterator), intent(in) :: first
! Iterator to last node to insert
class(xxtypebase___vector_ftl_iterator), intent(in) :: last
! Local variables
type(xxtypebase___vector_ftl_iterator) :: it
integer :: i, n
! Allocate the vector storage
n = distance( first, last ) + 1
call this%reserve(n)
! Initialise navigation pointer
it = first
i = 1
! Loop on the vector
do while( it%associated() )
! Add this element
allocate( this%data(i)%element, source=it%parent%data(it%idx)%element )
! Check if this was the last element
if( it == last ) exit
! Iterate
it = it%next()
i = i + 1
end do
! Set the counter
this%count = n
end subroutine vector_assign_from_range
! Assign new content to container
! Assigns new contents to the vector 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 vector_assign_from_fill( this, n, val )
! The output vector
class(xxtypebase___vector_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
! Allocate the vector storage
call this%reserve(n)
! Add the element to the vector, the prescribed number of times
do i = 1, n
call element_assign_pointer( this%data(i)%element, val )
end do
! Set the counter
this%count = n
end subroutine vector_assign_from_fill
! Assign a vector from an array
subroutine vector_assign_from_array( this, val )
! The output vector
class(xxtypebase___vector_ftl), intent(out) :: this
! The input array
class(xxtypebase__), dimension(:), intent(in) :: val
! Local variables
integer :: i, n
! Allocate the vector storage
n = size(val)
call this%reserve(n)
! Copy the vector
do i = 1, n
call element_assign_pointer( this%data(i)%element, val(i) )
end do
! Set the counter
this%count = n
end subroutine vector_assign_from_array
! Insert element at beginning
! Inserts a new element at the beginning of the vector, 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 vector_push_front( this, val )
! The vector
class(xxtypebase___vector_ftl), intent(inout) :: this
! The element
class(xxtypebase__), intent(in) :: val
! Local variables
integer :: i
! Check for allocation status and initialize if required
call this%realloc()
! Move all elements one position forward
do i = this%count, 1, -1
this%data(i+1)%element => this%data(i)%element
end do
! Copy the element into the array
call element_assign_pointer( this%data(1)%element, val )
! Increase counter
this%count = this%count + 1
end subroutine vector_push_front
! Delete first element
! Removes the first element in the vector container,
! effectively reducing its size by one.
! This destroys the removed element.
subroutine vector_pop_front( this )
! The vector
class(xxtypebase___vector_ftl), intent(inout) :: this
! Local counter
integer :: i
! Check if the vector is already empty
if( this%count > 0 ) then
! Destroy data element in the first vector node
deallocate( this%data(1)%element )
! Shift all elements one position backwards
do i = 1, this%count - 1
this%data(i)%element => this%data(i+1)%element
end do
! Decrease counter
this%count = this%count - 1
end if
end subroutine vector_pop_front
! Add element at the end
! Adds a new element at the end of the vector 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 vector_push_back( this, val )
! The vector
class(xxtypebase___vector_ftl), intent(inout) :: this
! The element
class(xxtypebase__), intent(in) :: val
! Check for allocation status and initialize if required
call this%realloc()
! Increase counter
this%count = this%count + 1
! Copy the element into the array
call element_assign_pointer( this%data(this%count)%element, val )
end subroutine vector_push_back
! Delete last element
! Removes the last element in the vector container,
! effectively reducing the container size by one.
! This destroys the removed element.
subroutine vector_pop_back( this )
! The vector
class(xxtypebase___vector_ftl), intent(inout) :: this
! Check if the vector is already empty
if( this%count > 0 ) then
! Destroy data element in the last vector node
deallocate( this%data(this%count)%element )
! Decrease counter
this%count = this%count - 1
end if
end subroutine vector_pop_back
! Insert elements
! The container is extended by inserting new elements before the element at
! the specified position.
! This effectively increases the vector 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 vector_insert_single( this, iterator, val ) result(res)
! The vector
class(xxtypebase___vector_ftl), target, intent(inout) :: this
! Iterator to node used as reference for insertion
class(xxtypebase___vector_ftl_iterator), intent(in) :: iterator
! The element
class(xxtypebase__), intent(in) :: val
! Iterator to the inserted element
type(xxtypebase___vector_ftl_iterator) :: res
! Insert by index
res%idx = this%insert( iterator%idx, val )
! Set pointer to parent
res%parent => this
end function vector_insert_single
! Insert elements
! The container is extended by inserting new elements before the element at
! the specified position (by absolute index)
! This effectively increases the vector size by one.
! Iterator remains associated to the node in input
function vector_insert_single_at( this, index, val ) result(res)
! The vector
class(xxtypebase___vector_ftl), intent(inout) :: this
! Index to element used as reference for insertion
integer, intent(in) :: index
! The element
class(xxtypebase__), intent(in) :: val
! The index in this of the inserted element
integer :: res
! Counter
integer :: i
! Check that index is within bounds; else do nothing
if( index >= 1 .and. index <= this%count ) then
! Check for allocation status and initialize if required
call this%realloc()
! Make room for the inserted element. Move pointers, not memory
do i = this%count, index, -1
this%data(i+1)%element => this%data(i)%element
end do
! Allocate memory for the new element
allocate( this%data(index)%element, mold=val )
! Copy element
call element_assign_pointer( this%data(index)%element, val )
! Increase vector size
this%count = this%count + 1
! Set the return index
res = index
end if
end function vector_insert_single_at
! Insert elements
! The container is extended by inserting new elements before the element at
! the specified position.
! This effectively increases the vector 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 vector_insert_fill( this, iterator, n, val ) result(res)
! The vector
class(xxtypebase___vector_ftl), target, intent(inout) :: this
! Iterator to node used as reference for insertion
class(xxtypebase___vector_ftl_iterator), intent(in) :: iterator
! The number of times to insert the element
integer, intent(in) :: n
! Iterator to the inserted element
type(xxtypebase___vector_ftl_iterator) :: res
! The element
class(xxtypebase__), intent(in) :: val
! Insert by index
res%idx = this%insert( iterator%idx, n, val )
! Set pointer to parent
res%parent => this
end function vector_insert_fill
! Insert elements
! The container is extended by inserting new elements before the element at
! the specified position (by absolute index)
! This effectively increases the vector size by n.
! Iterator remains associated to the node in input
function vector_insert_fill_at( this, index, n, val ) result(res)
! The vector
class(xxtypebase___vector_ftl), intent(inout) :: this
! Index to element used as reference for insertion
integer, intent(in) :: index
! The number of times to insert the element
integer, intent(in) :: n
! The element
class(xxtypebase__), intent(in) :: val
! Index in this to the start of the inserted section
integer :: res
! Counter
integer :: i, j
! Check that index is within bounds; else do nothing
if( index >= 1 .and. index <= this%count ) then
! Check for allocation status
call this%reserve( this%count + n )
! Make room for the inserted elements. Move pointers, not memory
do i = this%count, index, -1
this%data(i+n)%element => this%data(i)%element
end do
! Loop on the number of elements to insert
do i = 1, n
! Allocate memory for the new element and copy
j = index + i - 1
call element_assign_pointer( this%data(j)%element, val )
end do
! Increase vector size
this%count = this%count + n
! Set the return index
res = index
end if
end function vector_insert_fill_at
! Insert elements
! The container is extended by inserting new elements before the element at
! the specified position.
! This effectively increases the vector 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
! vector is corrupted.
function vector_insert_range( this, iterator, first, last ) result(res)
! The vector
class(xxtypebase___vector_ftl), target, intent(inout) :: this
! Iterator to node used as reference for insertion
type(xxtypebase___vector_ftl_iterator), intent(in) :: iterator
! Iterator to first node to insert
type(xxtypebase___vector_ftl_iterator), intent(in) :: first
! Iterator to last node to insert
type(xxtypebase___vector_ftl_iterator), intent(in) :: last
! Iterator to the inserted section
type(xxtypebase___vector_ftl_iterator) :: res
! Insert by index
res%idx = this%insert( iterator%idx, first, last )
! Set pointer to parent
res%parent => this
end function vector_insert_range
! Insert elements
! The container is extended by inserting new elements before the element at
! the specified position (by absolute index)
! This effectively increases the vector 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