|
| 1 | +!-----------------------------------------------------------------------------! |
| 2 | +! \file example/vecstr.f90 |
| 3 | +! |
| 4 | +! Copyright (c) 2019 Oak Ridge National Laboratory, UT-Battelle, LLC. |
| 5 | +!-----------------------------------------------------------------------------! |
| 6 | + |
| 7 | +program vecstr_example |
| 8 | + use, intrinsic :: ISO_FORTRAN_ENV |
| 9 | + use, intrinsic :: ISO_C_BINDING |
| 10 | + use flc |
| 11 | + use flc_string, only : String |
| 12 | + use flc_vector, only : VectorString |
| 13 | + implicit none |
| 14 | + integer, parameter :: STDOUT = OUTPUT_UNIT |
| 15 | + integer :: i |
| 16 | + type(VectorString) :: vec |
| 17 | + type(String) :: back, front |
| 18 | + character(C_CHAR), dimension(:), pointer :: chars |
| 19 | + |
| 20 | + ! Read a vector of strings |
| 21 | + call read_strings(vec) |
| 22 | + |
| 23 | + write(STDOUT, "(a, i3, a)") "Read ", vec%size(), " strings:" |
| 24 | + do i = 1, vec%size() |
| 25 | + write(STDOUT, "(i3, ': ', a)") i, vec%get(i) |
| 26 | + end do |
| 27 | + |
| 28 | + if (vec%empty()) then |
| 29 | + write(STDOUT, *) "No vectors provided" |
| 30 | + stop 0 |
| 31 | + endif |
| 32 | + |
| 33 | + ! Get the final string for modification |
| 34 | + back = vec%back_ref() |
| 35 | + chars => back%view() |
| 36 | + ! Change all characters to exclamation points |
| 37 | + chars(:) = '!' |
| 38 | + write(STDOUT, *) "The last string is very excited: " // vec%get(vec%size()) |
| 39 | + |
| 40 | + |
| 41 | + ! *Copy* back string to front, and add a question mark |
| 42 | + front = vec%front_ref() |
| 43 | + ! XXX: this creates an *alias* rather than assigning values. Revisit |
| 44 | + ! ownership semantics?? |
| 45 | + ! front = back |
| 46 | + call vec%set_ref(1, back) |
| 47 | + call front%push_back("?") |
| 48 | + |
| 49 | + ! Modify back to be something else. |
| 50 | + call back%assign("the end") |
| 51 | + |
| 52 | + ! Modifying 'back' invalidates the 'chars' view. Clear it to be safe. |
| 53 | + chars => NULL() |
| 54 | + |
| 55 | + write(STDOUT, *) "Modified 'front' string is " // vec%get(1) |
| 56 | + write(STDOUT, *) "Modified 'back' string is " // vec%get(vec%size()) |
| 57 | + |
| 58 | + ! Remove the first string (invalidating back and front references) |
| 59 | + call vec%erase(1) |
| 60 | + call back%release() |
| 61 | + call front%release() |
| 62 | + |
| 63 | + write(STDOUT, "(a, i3, a)") "Ended up with ", vec%size(), " strings:" |
| 64 | + do i = 1, vec%size() |
| 65 | + write(STDOUT, "(i3, ': ', a)") i, vec%get(i) |
| 66 | + end do |
| 67 | + |
| 68 | + ! Free allocated vector memory |
| 69 | + call vec%release() |
| 70 | +contains |
| 71 | + |
| 72 | +! Loop until the user inputs a positive integer. Catch error conditions. |
| 73 | +subroutine read_strings(vec) |
| 74 | + use flc |
| 75 | + use flc_string, only : stoi |
| 76 | + use ISO_FORTRAN_ENV |
| 77 | + implicit none |
| 78 | + type(VectorString), intent(out) :: vec |
| 79 | + integer, parameter :: STDOUT = OUTPUT_UNIT, STDIN = INPUT_UNIT |
| 80 | + character(len=80) :: readstr |
| 81 | + integer :: io_ierr |
| 82 | + type(String) :: str |
| 83 | + |
| 84 | + ! Allocate the vector |
| 85 | + vec = VectorString() |
| 86 | + |
| 87 | + do |
| 88 | + ! Request and read a string |
| 89 | + write(STDOUT, "(a, i3, a)") "Enter string #", vec%size() + 1, & |
| 90 | + " or Ctrl-D/empty string to complete" |
| 91 | + read(STDIN, "(a)", iostat=io_ierr) readstr |
| 92 | + if (io_ierr == IOSTAT_END) then |
| 93 | + ! Break out of loop on ^D (EOF) |
| 94 | + exit |
| 95 | + end if |
| 96 | + |
| 97 | + ! Add string to the end of the vector |
| 98 | + call vec%push_back(trim(readstr)) |
| 99 | + ! Get a String object reference to the back to check if it's empty |
| 100 | + str = vec%back_ref() |
| 101 | + if (str%empty()) then |
| 102 | + ! Remove the empty string |
| 103 | + call vec%pop_back() |
| 104 | + exit |
| 105 | + end if |
| 106 | + end do |
| 107 | +end subroutine |
| 108 | +end program |
| 109 | + |
| 110 | +!-----------------------------------------------------------------------------! |
| 111 | +! end of example/sort.f90 |
| 112 | +!-----------------------------------------------------------------------------! |
0 commit comments