ranges.f90
View source code here on GitHub!
Note
This module directly ports some of the logic found in Python's range
. Because of that,
these functions are 0-indexed.
- function ranges/range_entry3(start, step, idx)
- Parameters:
start [integer]
step [integer]
idx [integer]
- Return:
answer [integer] :: start + (step * idx)
- function ranges/range_entry4(start, end, step, idx)
- Parameters:
start [integer]
end [integer]
step [integer]
idx [integer]
- Return:
answer [integer] :: The
idx
th entry ofstart,(end-1),step
- Call to:
1module ranges
2 use constants
3 implicit none
4contains
5 pure integer function range_entry3(start, step, idx) result(answer)
6 integer, intent(in) :: start, step, idx
7 answer = start + (step * idx)
8 end function
9
10 pure integer function range_entry4(start, end, step, idx) result(answer)
11 integer, intent(in) :: start, end, step, idx
12 integer :: length, l_idx
13
14 l_idx = idx
15 if (step > 0 .and. start < end) then
16 length = 1 + (end - 1 - start) / step
17 elseif (step < 0 .and. start > end) then
18 length = 1 + (start - 1 - end) / (-step)
19 else
20 answer = huge(kind(answer)) ! error
21 return
22 end if
23
24 if (l_idx < 0) then
25 l_idx = length + l_idx
26 end if
27
28 if (l_idx < 0 .or. l_idx >= length) then
29 answer = huge(kind(answer)) ! error
30 return
31 end if
32
33 answer = range_entry3(start, step, l_idx)
34 end function
35end module ranges