Skip to content

Commit bca711f

Browse files
committed
added documentation for clear, constructor and fidx/bidx
1 parent 8ed81c0 commit bca711f

File tree

2 files changed

+185
-21
lines changed

2 files changed

+185
-21
lines changed

doc/specs/stdlib_stringlist.md

Lines changed: 176 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,126 @@ An instance is independent of the stringlist it is used with and hence, an index
3737
Experimental
3838

3939

40+
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
41+
### fidx/bidx
42+
43+
#### Description
44+
45+
`fidx`: Returns an instance which represents forward index `idx`.
46+
`bidx`: Returns an instance which represents backward index `idx`.
47+
48+
#### Syntax
49+
50+
For fidx: `res = [[stdlib_stringlist(module):fidx()]] (idx)`
51+
For bidx: `res = [[stdlib_stringlist(module):bidx()]] (idx)`
52+
53+
#### Status
54+
55+
Experimental.
56+
57+
#### Class
58+
59+
Pure function.
60+
61+
#### Argument
62+
63+
- `idx`: integer.
64+
This argument is intent(in).
65+
66+
#### Result value
67+
68+
The result is of type `stringlist_index_type`.
69+
70+
#### Example
71+
72+
```fortran
73+
program demo_fidx_bidx
74+
use stdlib_stringlist, only: stringlist_type, stringlist_index_type, fidx, bidx
75+
implicit none
76+
77+
type(stringlist_type) :: stringlist
78+
type(stringlist_index_type) :: index
79+
80+
index = fidx(1)
81+
82+
!> inserting 2 elements to the stringlist
83+
call stringlist%insert_at( fidx(1), "Element No. one" )
84+
call stringlist%insert_at( index, "Element No. two" )
85+
! stringlist <-- {"Element No. two", "Element No. one"}
86+
87+
index = bidx(3)
88+
89+
!> inserting 2 elements to the stringlist
90+
call stringlist%insert_at( bidx(1), "Element No. three" )
91+
call stringlist%insert_at( index, "Element No. four" )
92+
! stringlist <-- {"Element No. two", "Element No. four",
93+
"Element No. one", "Element No. three"}
94+
95+
end program demo_fidx_bidx
96+
```
97+
98+
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
99+
### Constructor for stringlist
100+
101+
#### Description
102+
103+
No arguments given: Initializes an empty stringlist(a list containing no elements in it).
104+
105+
With argument: Initializes a stringlist equivalent to the input array `array` i.e. a stringlist containing all elements of the input array `array` in the same order.
106+
107+
#### Syntax
108+
109+
No arguments given: `res = [[stdlib_stringlist(module):stringlist_type(interface)]] ()`
110+
With argument: `res = [[stdlib_stringlist(module):stringlist_type(interface)]] (array)`
111+
112+
#### Status
113+
114+
Experimental
115+
116+
#### Class
117+
118+
Pure function.
119+
120+
#### Argument
121+
122+
No arguments.
123+
124+
With argument: - `array`: array of Character scalar or array of [[stdlib_string_type(module):string_type(type)]].
125+
This argument is intent(in).
126+
127+
#### Result value
128+
129+
The result is an instance of type `stringlist_type`.
130+
131+
#### Example
132+
133+
```fortran
134+
program demo_constructor
135+
use stdlib_stringlist, only: stringlist_type
136+
use stdlib_string_type, only: string_type
137+
implicit none
138+
139+
type(stringlist_type) :: stringlist
140+
141+
stringlist = stringlist_type()
142+
! stringlist <-- { } (empty stringlist)
143+
144+
stringlist = stringlist_type(["#1", "#2", "#3"])
145+
! stringlist <-- {"#1", "#2", "#3"}
146+
147+
stringlist = stringlist_type([string_type("#1"), string_type("#2")])
148+
! stringlist <-- {"#1", "#2"}
149+
150+
end program demo_constructor
151+
```
152+
153+
40154
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
41155
### insert_at
42156

43157
#### Description
44158

45-
Inserts the string `string` at the index `idx` such that after insertion is done the newly added element is at index `idx`.
159+
Inserts the string `string` _AT_ the index `idx`, so that the newly added element is present at index `idx` after insertion. Inserting an element _AT_ index beyond `length + 1` inserts the element _AT_ `list_tail`, and likewise inserting _AT_ a non-positive index inserts the element _AT_ `list_head`.
46160

47161
#### Syntax
48162

@@ -72,7 +186,7 @@ No result.
72186

73187
```fortran
74188
program demo_insert_at
75-
use stdlib_stringlist, only: stringlist_type, insert_at, stringlist_index_type, fidx, bidx
189+
use stdlib_stringlist, only: stringlist_type, stringlist_index_type, fidx, bidx
76190
use stdlib_string_type, only: string_type
77191
implicit none
78192
@@ -102,7 +216,7 @@ end program demo_insert_at
102216

103217
#### Description
104218

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.
219+
Returns the string present currently at the index `idx` in a stringlist. If index `idx` is out of bounds, then an empty string is returned.
106220

107221
#### Syntax
108222

@@ -129,14 +243,14 @@ The result is a string of type `string_type`.
129243

130244
```fortran
131245
program demo_get
132-
use stdlib_stringlist, only: stringlist_type, insert_at, fidx, get
246+
use stdlib_stringlist, only: stringlist_type, fidx, bidx
133247
use stdlib_string_type, only: string_type
134248
implicit none
135249
136250
type(stringlist_type) :: stringlist
137251
type(string_type) :: output
138252
139-
!> adding 4 elements to the stringlist
253+
!> inserting 4 elements to the stringlist
140254
call stringlist%insert_at( fidx(1), "Element No. one" )
141255
call stringlist%insert_at( fidx(1), "Element No. two" )
142256
call stringlist%insert_at( fidx(1), "Element No. three" )
@@ -164,7 +278,7 @@ end program demo_get
164278

165279
#### Description
166280

167-
Returns the number of elements present in the stringlist.
281+
Returns the number of elements present currently in the stringlist.
168282

169283
#### Syntax
170284

@@ -190,7 +304,7 @@ The result is of type `integer`.
190304

191305
```fortran
192306
program demo_len
193-
use stdlib_stringlist, only: stringlist_type, insert_at, bidx, len
307+
use stdlib_stringlist, only: stringlist_type, bidx
194308
implicit none
195309
196310
type(stringlist_type) :: stringlist
@@ -199,13 +313,65 @@ program demo_len
199313
output = stringlist%len()
200314
! output <-- 0
201315
202-
!> adding 2 elements to the stringlist
316+
!> inserting 2 elements to the stringlist
203317
call stringlist%insert_at( bidx(1), "Element No. one" )
204318
call stringlist%insert_at( bidx(1), "Element No. two" )
205319
! stringlist <-- {"Element No. one", "Element No. two"}
206320
207-
output = stringlist%len()
208-
! output <-- 2
321+
print'(a)', stringlist%len()
322+
! 2
209323
210324
end program demo_len
211325
```
326+
327+
328+
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
329+
### clear
330+
331+
#### Description
332+
333+
Removes all elements from a stringlist.
334+
335+
#### Syntax
336+
337+
`call [[stdlib_stringlist(module):stringlist_type(type)]]%[[stdlib_stringlist(module):clear()]] ()`
338+
339+
#### Status
340+
341+
Experimental.
342+
343+
#### Class
344+
345+
Pure subroutine.
346+
347+
#### Argument
348+
349+
No arguments.
350+
351+
#### Result value
352+
353+
No result.
354+
355+
#### Example
356+
357+
```fortran
358+
program demo_clear
359+
use stdlib_stringlist, only: stringlist_type, fidx
360+
implicit none
361+
362+
type(stringlist_type) :: stringlist
363+
364+
!> inserting 2 elements to the stringlist
365+
call stringlist%insert_at( fidx(1), "Element No. one" )
366+
call stringlist%insert_at( fidx(1), "Element No. two" )
367+
! stringlist <-- {"Element No. two", "Element No. one"}
368+
369+
call stringlist%clear()
370+
! stringlist <-- { } (empty stringlist)
371+
372+
!> inserting 1 element to the stringlist
373+
call stringlist%insert_at( fidx(1), "Element No. one" )
374+
! stringlist <-- {"Element No. one"}
375+
376+
end program demo_clear
377+
```

src/stdlib_stringlist.f90

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
! The strings may have arbitrary lengths, not necessarily the same
44
!
55
! insert AT: Inserts an element BEFORE the element present currently at the asked index
6-
! for forward indexes, otherwise
6+
! for forward indexes
77
! Inserts an element AFTER the element present currently at the asked index
88
! for backward indexes
99
! In other words, after insertion the element will be present at the asked index
@@ -17,7 +17,6 @@
1717
module stdlib_stringlist
1818
use stdlib_string_type, only: string_type, operator(/=) !, move
1919
use stdlib_math, only: clip
20-
! use stdlib_optval, only: optval
2120
implicit none
2221
private
2322

@@ -163,14 +162,14 @@ end function new_stringlist
163162

164163
!> Constructor to convert chararray to stringlist
165164
!> Returns a new instance of type stringlist
166-
pure function new_stringlist_carray( carray )
167-
character(len=*), dimension(:), intent(in) :: carray
165+
pure function new_stringlist_carray( array )
166+
character(len=*), dimension(:), intent(in) :: array
168167
type(stringlist_type) :: new_stringlist_carray
169-
type(string_type), dimension( size(carray) ) :: sarray
168+
type(string_type), dimension( size(array) ) :: sarray
170169
integer :: i
171170

172-
do i = 1, size(carray)
173-
sarray(i) = string_type( carray(i) )
171+
do i = 1, size(array)
172+
sarray(i) = string_type( array(i) )
174173
end do
175174

176175
new_stringlist_carray = stringlist_type( sarray )
@@ -179,11 +178,11 @@ end function new_stringlist_carray
179178

180179
!> Constructor to convert stringarray to stringlist
181180
!> Returns a new instance of type stringlist
182-
pure function new_stringlist_sarray( sarray )
183-
type(string_type), dimension(:), intent(in) :: sarray
181+
pure function new_stringlist_sarray( array )
182+
type(string_type), dimension(:), intent(in) :: array
184183
type(stringlist_type) :: new_stringlist_sarray
185184

186-
new_stringlist_sarray = stringlist_type( size(sarray), sarray )
185+
new_stringlist_sarray = stringlist_type( size(array), array )
187186

188187
end function new_stringlist_sarray
189188

@@ -612,7 +611,6 @@ subroutine insert_before_empty_positions( list, idxn, positions )
612611
if (positions > 0) then
613612

614613
idxn = clip( idxn, 1, list%len() + 1 )
615-
! Matlab's infinitely expandable list (Ivan's comment)??
616614
old_len = list%len()
617615
new_len = old_len + positions
618616

0 commit comments

Comments
 (0)