|
1 | 1 | ---
|
2 | 2 | title: stringlist
|
3 | 3 | ---
|
4 |
| -# List of strings |
| 4 | + |
| 5 | +# `stdlib_stringlist` module (1-D List of Strings) |
| 6 | + |
| 7 | +[TOC] |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +The `stdlib_stringlist` module provides with 2 derived types to deal with list of strings. |
| 12 | +`stringlist_type` derived type represents a one-dimensional list of variable-length strings which is compatible with Fortran intrinsic character and `stringlist_type` derived type represents an index to access, modify the elements of a stringlist and insert strings to a stringlist. |
| 13 | + |
| 14 | +## Derived type provided |
| 15 | + |
| 16 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 17 | +### `stringlist_type` derived type |
| 18 | + |
| 19 | +The `stringlist_type` derived type represents one-dimensional list of strings. |
| 20 | +The internal representation of the list is implementation dependent and is not visible to the user of the module. |
| 21 | + |
| 22 | +#### Status |
| 23 | + |
| 24 | +Experimental |
| 25 | + |
| 26 | + |
| 27 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 28 | +### `stringlist_index_type` derived type |
| 29 | + |
| 30 | +An instance of the derived type `stringlist_index_type` represents either a forward index OR a backward index. |
| 31 | +The internal representation is implementation dependent and is not visible to the user of the module. |
| 32 | +`list_head` and `list_tail` are 2 special instances of this type representing the head and the tail of a stringlist respectively. |
| 33 | +An instance is independent of the stringlist it is used with and hence, an index can be used with multiple stringlists in the same program. |
| 34 | + |
| 35 | +#### Status |
| 36 | + |
| 37 | +Experimental |
| 38 | + |
| 39 | + |
| 40 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 41 | +### insert_at |
| 42 | + |
| 43 | +#### Description |
| 44 | + |
| 45 | +Inserts the string `string` at the index `idx` such that after insertion is done the newly added element is at index `idx`. |
| 46 | + |
| 47 | +#### Syntax |
| 48 | + |
| 49 | +`call [[stdlib_stringlist(module):stringlist_type(type)]]%[[stdlib_stringlist(module):insert_at(generic)]] (idx, string)` |
| 50 | + |
| 51 | +#### Status |
| 52 | + |
| 53 | +Experimental. |
| 54 | + |
| 55 | +#### Class |
| 56 | + |
| 57 | +Pure subroutine. |
| 58 | + |
| 59 | +#### Argument |
| 60 | + |
| 61 | +- `idx`: [[stdlib_stringlist(module):stringlist_index_type(type)]]. |
| 62 | + This argument is intent(in). |
| 63 | + |
| 64 | +- `string`: Character scalar or [[stdlib_string_type(module):string_type(type)]]. |
| 65 | + This argument is intent(in). |
| 66 | + |
| 67 | +#### Result value |
| 68 | + |
| 69 | +No result. |
| 70 | + |
| 71 | +#### Example |
| 72 | + |
| 73 | +```fortran |
| 74 | +program demo_insert_at |
| 75 | + use stdlib_stringlist, only: stringlist_type, insert_at, stringlist_index_type, fidx, bidx |
| 76 | + use stdlib_string_type, only: string_type |
| 77 | + implicit none |
| 78 | +
|
| 79 | + type(stringlist_type) :: stringlist |
| 80 | + type(stringlist_index_type) :: index |
| 81 | +
|
| 82 | + index = fidx(1) |
| 83 | + call stringlist%insert_at( index, "Element No. one" ) |
| 84 | + ! stringlist <-- {"Element No. one"} |
| 85 | +
|
| 86 | + index = bidx(1) |
| 87 | + call stringlist%insert_at( index, string_type( "Element No. two" ) ) |
| 88 | + ! stringlist <-- {"Element No. one", "Element No. two"} |
| 89 | +
|
| 90 | + call stringlist%insert_at( fidx(2), string_type( "Element No. three" ) ) |
| 91 | + ! stringlist <-- {"Element No. one", "Element No. three", "Element No. two"} |
| 92 | +
|
| 93 | + call stringlist%insert_at( bidx(1), "Element No. four" ) |
| 94 | + ! stringlist <-- {"Element No. one", "Element No. three", "Element No. two", "Element No. four"} |
| 95 | +
|
| 96 | +end program demo_insert_at |
| 97 | +``` |
| 98 | + |
| 99 | + |
| 100 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 101 | +### get |
| 102 | + |
| 103 | +#### Description |
| 104 | + |
| 105 | +Returns the string present currently at the index `idx` in the given stringlist. If `idx` is out of bounds an empty string is returned. |
| 106 | + |
| 107 | +#### Syntax |
| 108 | + |
| 109 | +`res = [[stdlib_stringlist(module):stringlist_type(type)]]%[[stdlib_stringlist(module):get(generic)]] (idx)` |
| 110 | + |
| 111 | +#### Status |
| 112 | + |
| 113 | +Experimental. |
| 114 | + |
| 115 | +#### Class |
| 116 | + |
| 117 | +Pure function. |
| 118 | + |
| 119 | +#### Argument |
| 120 | + |
| 121 | +- `idx`: [[stdlib_stringlist(module):stringlist_index_type(type)]]. |
| 122 | + This argument is intent(in). |
| 123 | + |
| 124 | +#### Result value |
| 125 | + |
| 126 | +The result is a string of type `string_type`. |
| 127 | + |
| 128 | +#### Example |
| 129 | + |
| 130 | +```fortran |
| 131 | +program demo_get |
| 132 | + use stdlib_stringlist, only: stringlist_type, insert_at, fidx, get |
| 133 | + use stdlib_string_type, only: string_type |
| 134 | + implicit none |
| 135 | +
|
| 136 | + type(stringlist_type) :: stringlist |
| 137 | + type(string_type) :: output |
| 138 | +
|
| 139 | + !> adding 4 elements to the stringlist |
| 140 | + call stringlist%insert_at( fidx(1), "Element No. one" ) |
| 141 | + call stringlist%insert_at( fidx(1), "Element No. two" ) |
| 142 | + call stringlist%insert_at( fidx(1), "Element No. three" ) |
| 143 | + call stringlist%insert_at( fidx(1), "Element No. four" ) |
| 144 | + ! stringlist <-- {"Element No. four", "Element No. three", "Element No. two", "Element No. one"} |
| 145 | +
|
| 146 | + output = stringlist%get( fidx(1) ) |
| 147 | + ! output <-- "Element No. four" |
| 148 | + |
| 149 | + output = stringlist%get( bidx(1) ) |
| 150 | + ! output <-- "Element No. one" |
| 151 | +
|
| 152 | + !> accessing out of bounds index |
| 153 | + output = stringlist%get( bidx(5) ) |
| 154 | + ! output <-- "" |
| 155 | + output = stringlist%get( fidx(0) ) |
| 156 | + ! output <-- "" |
| 157 | +
|
| 158 | +end program demo_get |
| 159 | +``` |
| 160 | + |
| 161 | + |
| 162 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 163 | +### len |
| 164 | + |
| 165 | +#### Description |
| 166 | + |
| 167 | +Returns the number of elements present in the stringlist. |
| 168 | + |
| 169 | +#### Syntax |
| 170 | + |
| 171 | +`res = [[stdlib_stringlist(module):stringlist_type(type)]]%[[stdlib_stringlist(module):len()]] ()` |
| 172 | + |
| 173 | +#### Status |
| 174 | + |
| 175 | +Experimental. |
| 176 | + |
| 177 | +#### Class |
| 178 | + |
| 179 | +Pure function. |
| 180 | + |
| 181 | +#### Argument |
| 182 | + |
| 183 | +No arguments. |
| 184 | + |
| 185 | +#### Result value |
| 186 | + |
| 187 | +The result is of type `integer`. |
| 188 | + |
| 189 | +#### Example |
| 190 | + |
| 191 | +```fortran |
| 192 | +program demo_len |
| 193 | + use stdlib_stringlist, only: stringlist_type, insert_at, bidx, len |
| 194 | + implicit none |
| 195 | +
|
| 196 | + type(stringlist_type) :: stringlist |
| 197 | + integer :: output |
| 198 | +
|
| 199 | + output = stringlist%len() |
| 200 | + ! output <-- 0 |
| 201 | +
|
| 202 | + !> adding 2 elements to the stringlist |
| 203 | + call stringlist%insert_at( bidx(1), "Element No. one" ) |
| 204 | + call stringlist%insert_at( bidx(1), "Element No. two" ) |
| 205 | + ! stringlist <-- {"Element No. one", "Element No. two"} |
| 206 | +
|
| 207 | + output = stringlist%len() |
| 208 | + ! output <-- 2 |
| 209 | +
|
| 210 | +end program demo_len |
| 211 | +``` |
0 commit comments