@@ -84,18 +84,20 @@ module stdlib_stringlist_type
84
84
insert_before_chararray_int, &
85
85
insert_before_stringarray_int
86
86
87
- procedure :: get_string_idx = > get_string_idx_impl
88
- generic, public :: get = > get_string_idx
87
+ procedure :: get_idx = > get_idx_impl
88
+ procedure :: get_range_idx = > get_range_idx_impl
89
+ generic, public :: get = > get_idx, &
90
+ get_range_idx
89
91
90
- procedure :: pop_idx = > pop_idx_impl
91
- procedure :: pop_range_idx = > pop_range_idx_impl
92
- generic, public :: pop = > pop_idx, &
93
- pop_range_idx
92
+ procedure :: pop_idx = > pop_idx_impl
93
+ procedure :: pop_range_idx = > pop_range_idx_impl
94
+ generic, public :: pop = > pop_idx, &
95
+ pop_range_idx
94
96
95
- procedure :: drop_idx = > drop_idx_impl
96
- procedure :: drop_range_idx = > drop_range_idx_impl
97
- generic, public :: drop = > drop_idx, &
98
- drop_range_idx
97
+ procedure :: drop_idx = > drop_idx_impl
98
+ procedure :: drop_range_idx = > drop_range_idx_impl
99
+ generic, public :: drop = > drop_idx, &
100
+ drop_range_idx
99
101
100
102
end type stringlist_type
101
103
@@ -453,6 +455,21 @@ pure logical function ineq_sarray_stringlist( lhs, rhs )
453
455
454
456
end function ineq_sarray_stringlist
455
457
458
+ ! Version: experimental
459
+ ! >
460
+ ! > Shifts a stringlist_index by integer 'shift_by'
461
+ ! > Returns the shifted stringlist_index
462
+ pure function shift ( idx , shift_by )
463
+ ! > Not a part of public API
464
+ type (stringlist_index_type), intent (in ) :: idx
465
+ integer , intent (in ) :: shift_by
466
+
467
+ type (stringlist_index_type), intent (in ) :: shift
468
+
469
+ shift = merge ( fidx( idx% offset + shift_by ), bidx( idx% offset + shift_by ), idx% forward )
470
+
471
+ end function shift
472
+
456
473
! clear:
457
474
458
475
! > Version: experimental
@@ -588,7 +605,7 @@ end subroutine insert_at_stringarray_idx_wrap
588
605
! >
589
606
! > Inserts 'positions' number of empty positions BEFORE integer index 'idxn'
590
607
! > Modifies the input stringlist 'list'
591
- subroutine insert_before_empty_positions ( list , idxn , positions )
608
+ subroutine insert_before_engine ( list , idxn , positions )
592
609
! > Not a part of public API
593
610
class(stringlist_type), intent (inout ) :: list
594
611
integer , intent (inout ) :: idxn
@@ -618,7 +635,7 @@ subroutine insert_before_empty_positions( list, idxn, positions )
618
635
619
636
end if
620
637
621
- end subroutine insert_before_empty_positions
638
+ end subroutine insert_before_engine
622
639
623
640
! > Version: experimental
624
641
! >
@@ -633,7 +650,7 @@ subroutine insert_before_string_int_impl( list, idxn, string )
633
650
integer :: work_idxn
634
651
635
652
work_idxn = idxn
636
- call insert_before_empty_positions ( list, work_idxn, 1 )
653
+ call insert_before_engine ( list, work_idxn, 1 )
637
654
638
655
list% stringarray(work_idxn) = string
639
656
@@ -655,7 +672,7 @@ subroutine insert_before_stringlist_int_impl( list, idxn, slist )
655
672
656
673
work_idxn = idxn
657
674
pre_length = slist% len ()
658
- call insert_before_empty_positions ( list, work_idxn, pre_length )
675
+ call insert_before_engine ( list, work_idxn, pre_length )
659
676
post_length = slist% len ()
660
677
661
678
do i = 1 , min ( work_idxn - 1 , pre_length )
@@ -684,7 +701,7 @@ subroutine insert_before_chararray_int_impl( list, idxn, carray )
684
701
integer :: work_idxn, idxnew
685
702
686
703
work_idxn = idxn
687
- call insert_before_empty_positions ( list, work_idxn, size ( carray ) )
704
+ call insert_before_engine ( list, work_idxn, size ( carray ) )
688
705
689
706
do i = 1 , size ( carray )
690
707
idxnew = work_idxn + i - 1
@@ -707,7 +724,7 @@ subroutine insert_before_stringarray_int_impl( list, idxn, sarray )
707
724
integer :: work_idxn, idxnew
708
725
709
726
work_idxn = idxn
710
- call insert_before_empty_positions ( list, work_idxn, size ( sarray ) )
727
+ call insert_before_engine ( list, work_idxn, size ( sarray ) )
711
728
712
729
do i = 1 , size ( sarray )
713
730
idxnew = work_idxn + i - 1
@@ -718,68 +735,113 @@ end subroutine insert_before_stringarray_int_impl
718
735
719
736
! get:
720
737
738
+ ! > Version: experimental
739
+ ! >
740
+ ! > Returns strings present at stringlist_indexes in interval ['first', 'last']
741
+ ! > Stores requested strings in array 'capture_strings'
742
+ ! > No return
743
+ subroutine get_engine ( list , first , last , capture_strings )
744
+ class(stringlist_type) :: list
745
+ type (stringlist_index_type), intent (in ) :: first, last
746
+ type (string_type), allocatable , intent (out ) :: capture_strings(:)
747
+
748
+ integer :: from, to
749
+ integer :: i, inew
750
+
751
+ from = max ( list% to_current_idxn( first ), 1 )
752
+ to = min ( list% to_current_idxn( last ), list% len () )
753
+
754
+ ! out of bounds indexes won't be captured in capture_strings
755
+ if ( from <= to ) then
756
+ pos = to - from + 1
757
+ allocate ( capture_strings(pos) )
758
+
759
+ inew = 1
760
+ do i = from, to
761
+ capture_strings(inew) = list% stringarray(i)
762
+ inew = inew + 1
763
+ end do
764
+
765
+ else
766
+ allocate ( capture_strings(0 ) )
767
+ end if
768
+
769
+ end subroutine get_engine
770
+
721
771
! > Version: experimental
722
772
! >
723
773
! > Returns the string present at stringlist_index 'idx' in stringlist 'list'
724
774
! > Returns string_type instance
725
- pure function get_string_idx_impl ( list , idx )
726
- class(stringlist_type), intent (in ) :: list
727
- type (stringlist_index_type), intent (in ) :: idx
728
- type (string_type) :: get_string_idx_impl
729
-
730
- integer :: idxn
775
+ pure function get_idx_impl ( list , idx )
776
+ class(stringlist_type), intent (in ) :: list
777
+ type (stringlist_index_type), intent (in ) :: idx
778
+ type (string_type) :: get_idx_impl
731
779
732
- idxn = list % to_current_idxn( idx )
780
+ type (string_type), allocatable :: capture_strings(: )
733
781
734
- ! if the index is out of bounds, returns a string_type instance equivalent to empty string
735
- if ( 1 <= idxn .and. idxn <= list% len () ) then
736
- get_string_idx_impl = list% stringarray(idxn)
782
+ call get_engine( list, idx, idx, capture_strings )
737
783
784
+ ! if index 'idx' is out of bounds, returns an empty string
785
+ if ( size (capture_strings) == 1 ) then
786
+ call move( capture_strings(1 ), get_idx_impl )
738
787
end if
739
788
740
- end function get_string_idx_impl
789
+ end function get_idx_impl
790
+
791
+ ! > Version: experimental
792
+ ! >
793
+ ! > Returns strings present at stringlist_indexes in interval ['first', 'last']
794
+ ! > Returns array of string_type instances
795
+ pure function get_range_idx_impl ( list , first , last )
796
+ class(stringlist_type), intent (in ) :: list
797
+ type (stringlist_index_type), intent (in ) :: first, last
798
+
799
+ type (string_type), allocatable :: get_range_idx_impl(:)
800
+
801
+ call get_engine( list, first, last, get_range_idx_impl )
802
+
803
+ end function get_range_idx_impl
804
+
805
+ ! pop & drop:
741
806
742
807
! > Version: experimental
743
808
! >
744
809
! > Removes strings present at indexes in interval ['first', 'last']
745
- ! > Returns captured popped strings
746
- subroutine pop_engine ( list , first , last , capture_popped )
810
+ ! > Stores captured popped strings in array 'capture_popped'
811
+ ! > No return
812
+ subroutine pop_drop_engine ( list , first , last , capture_popped )
747
813
class(stringlist_type) :: list
748
814
type (stringlist_index_type), intent (in ) :: first, last
749
815
type (string_type), allocatable , intent (out ), optional :: capture_popped(:)
750
816
751
- integer :: firstn, lastn
752
- integer :: i, inew
753
- integer :: pos, old_len, new_len
817
+ integer :: firstn, lastn, from, to
818
+ integer :: i, inew, pos, old_len, new_len
754
819
type (string_type), dimension (:), allocatable :: new_stringarray
755
820
756
821
old_len = list% len ()
757
-
758
- firstn = max ( list% to_current_idxn( first ), 1 )
759
- lastn = min ( list% to_current_idxn( last ), old_len )
822
+ firstn = list% to_current_idxn( first )
823
+ lastn = list% to_current_idxn( last )
824
+ from = max ( firstn , 1 )
825
+ to = min ( lastn , old_len )
760
826
761
827
! out of bounds indexes won't modify stringlist
762
- if ( firstn <= lastn ) then
763
- pos = lastn - firstn + 1
828
+ if ( from <= to ) then
829
+ pos = to - from + 1
764
830
new_len = old_len - pos
765
831
766
832
allocate ( new_stringarray(new_len) )
767
- do i = 1 , firstn - 1
833
+ do i = 1 , from - 1
768
834
call move( list% stringarray(i), new_stringarray(i) )
769
835
end do
770
836
771
837
! capture popped strings
772
838
if ( present (capture_popped) ) then
773
- allocate ( capture_popped(pos) )
774
- inew = 1
775
- do i = firstn, lastn
776
- call move( list% stringarray(i), capture_popped(inew) )
777
- inew = inew + 1
778
- end do
839
+ call get_engine( list, shift( first, from - firstn ), &
840
+ & shift( last, lastn - to ), capture_popped )
779
841
end if
780
842
781
- inew = firstn
782
- do i = lastn + 1 , old_len
843
+ inew = from
844
+ do i = to + 1 , old_len
783
845
call move( list% stringarray(i), new_stringarray(inew) )
784
846
inew = inew + 1
785
847
end do
@@ -791,9 +853,7 @@ subroutine pop_engine( list, first, last, capture_popped)
791
853
end if
792
854
end if
793
855
794
- end subroutine pop_engine
795
-
796
- ! pop:
856
+ end subroutine pop_drop_engine
797
857
798
858
! > Version: experimental
799
859
! >
@@ -806,10 +866,10 @@ function pop_idx_impl( list, idx )
806
866
807
867
type (string_type), dimension (:), allocatable :: popped_strings
808
868
809
- call pop_engine ( list, idx, idx, popped_strings )
869
+ call pop_drop_engine ( list, idx, idx, popped_strings )
810
870
811
871
if ( size (popped_strings) == 1 ) then
812
- pop_idx_impl = popped_strings(1 )
872
+ call move( pop_idx_impl, popped_strings(1 ) )
813
873
end if
814
874
815
875
end function pop_idx_impl
@@ -825,12 +885,10 @@ function pop_range_idx_impl( list, first, last )
825
885
826
886
type (string_type), dimension (:), allocatable :: pop_range_idx_impl
827
887
828
- call pop_engine ( list, first, last, pop_range_idx_impl )
888
+ call pop_drop_engine ( list, first, last, pop_range_idx_impl )
829
889
830
890
end function pop_range_idx_impl
831
891
832
- ! drop:
833
-
834
892
! > Version: experimental
835
893
! >
836
894
! > Removes the string present at stringlist_index 'idx' in stringlist 'list'
@@ -839,7 +897,7 @@ subroutine drop_idx_impl( list, idx )
839
897
class(stringlist_type) :: list
840
898
type (stringlist_index_type), intent (in ) :: idx
841
899
842
- call pop_engine ( list, idx, idx )
900
+ call pop_drop_engine ( list, idx, idx )
843
901
844
902
end subroutine drop_idx_impl
845
903
@@ -848,11 +906,11 @@ end subroutine drop_idx_impl
848
906
! > Removes strings present at stringlist_indexes in interval ['first', 'last']
849
907
! > in stringlist 'list'
850
908
! > Doesn't return removed strings
851
- subroutine drop_range_idx_impl ( list , first , last )
909
+ subroutine drop_range_idx_impl ( list , first , last )
852
910
class(stringlist_type) :: list
853
911
type (stringlist_index_type), intent (in ) :: first, last
854
912
855
- call pop_engine ( list, first, last )
913
+ call pop_drop_engine ( list, first, last )
856
914
857
915
end subroutine drop_range_idx_impl
858
916
0 commit comments