Skip to content

Commit b6550f0

Browse files
committed
Add sorting and error handling example
1 parent 2c20b56 commit b6550f0

File tree

3 files changed

+96
-18
lines changed

3 files changed

+96
-18
lines changed

doc/examples.rst

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@
66
Examples
77
********
88

9-
TODO
9+
The following standalone codes demonstrate how Flibcpp can be used in native
10+
Fortran code.
11+
12+
String conversion and sort
13+
==========================
14+
15+
This example:
16+
17+
- Introspects the Flibcpp version;
18+
- Converts a user input to an integer, validating it with useful error
19+
messages;
20+
- Fills an array with normally-distributed real numbers; and
21+
- Sorts the array before printing the first few entries.
22+
23+
.. literalinclude:: ../example/sort.f90
24+
:linenos:
1025

1126
.. ############################################################################
1227
.. end of doc/examples.rst

example/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ macro(swig_fortran_add_example name)
1212
endmacro()
1313

1414
swig_fortran_add_example(sort
15-
flc_algorithm)
15+
flc_algorithm flc_random flc_string)
1616

1717
##---------------------------------------------------------------------------##
1818
## end of common/CMakeLists.txt

example/sort.f90

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,92 @@
11
!-----------------------------------------------------------------------------!
22
! \file example/sort.f90
3+
!
4+
! Copyright (c) 2019 Oak Ridge National Laboratory, UT-Battelle, LLC.
35
!-----------------------------------------------------------------------------!
46

57
program main
6-
use ISO_FORTRAN_ENV
7-
use, intrinsic :: ISO_C_BINDING
8-
use flc
9-
use flc_algorithm, only : sort
8+
use ISO_FORTRAN_ENV
9+
use, intrinsic :: ISO_C_BINDING
10+
use flc
11+
use flc_algorithm, only : sort
12+
use flc_random, only : Engine, normal_distribution
13+
implicit none
14+
integer, parameter :: STDOUT = OUTPUT_UNIT
15+
integer :: arr_size
16+
real(c_double), dimension(:), allocatable :: x
17+
real(c_double), parameter :: MEAN = 1.0d0, SIGMA = 0.5d0
18+
type(Engine) :: rng
1019

11-
implicit none
12-
integer :: n = 10000
13-
real(c_double), dimension(:), allocatable :: x
20+
! Print version information
21+
write(STDOUT, "(a)") "========================================"
22+
write(STDOUT, "(a, a)") "Flibcpp version: ", get_flibcpp_version()
23+
write(STDOUT, "(a, 2(i1,'.'), (i1), a)") "(Numeric version: ", &
24+
flibcpp_version_major, flibcpp_version_minor, flibcpp_version_patch, &
25+
")"
26+
write(STDOUT, "(a)") "========================================"
1427

15-
write(*,"(a, 2(i1,'.'), (i1))") "Flibcpp version number: ", &
16-
flibcpp_version_major, flibcpp_version_minor, flibcpp_version_patch
17-
write(*,*) "Flibcpp version string: ", get_flibcpp_version()
28+
! Get array size
29+
arr_size = read_positive_int("array size")
30+
allocate(x(arr_size))
1831

19-
allocate(x(n))
20-
! TODO: fill randomly
21-
x = 1
22-
call sort(x)
32+
! Fill randomly with normal distribution
33+
rng = Engine()
34+
call normal_distribution(MEAN, SIGMA, rng, x)
2335

24-
write(*,*) "Success!"
25-
end program
36+
! Sort the array
37+
call sort(x)
38+
39+
! Write output
40+
write(STDOUT, "(a, 4(f8.3,','))") "First few elements:", x(:min(4, size(x)))
41+
contains
42+
43+
! Loop until the user inputs a positive integer. Catch error conditions.
44+
function read_positive_int(desc) result(result_int)
45+
use flc
46+
use flc_string, only : stoi
47+
use ISO_FORTRAN_ENV
48+
implicit none
49+
character(len=*), intent(in) :: desc
50+
integer, parameter :: STDOUT = OUTPUT_UNIT, STDIN = INPUT_UNIT
51+
character(len=80) :: readstr
52+
integer :: result_int, io_ierr
53+
do
54+
write(STDOUT, *) "Enter " // desc // ": "
55+
read(STDIN, "(a)", iostat=io_ierr) readstr
56+
if (io_ierr == IOSTAT_END) then
57+
! Error condition: ctrl-D during input
58+
write(STDOUT, *) "End of line"
59+
stop 0
60+
endif
61+
62+
result_int = stoi(readstr)
63+
if (ierr == 0) then
64+
if (result_int <= 0) then
65+
! Error condition: non-positive value
66+
write(STDOUT, *) "Invalid " // desc // ": ", result_int
67+
continue
68+
end if
2669

70+
write(STDOUT, *) "Read " // desc // "=", result_int
71+
exit
72+
endif
73+
74+
if (ierr == SWIG_OVERFLOWERROR) then
75+
! Error condition: integer doesn't fit in native integer
76+
write(0,*) "Your integer is too darn big!"
77+
else if (ierr == SWIG_VALUEERROR) then
78+
! Error condition: not an integer at all
79+
write(0,*) "That text you entered? It wasn't an integer."
80+
else
81+
write(0,*) "Unknown error", ierr
82+
end if
83+
write(0,*) "(Detailed error message: ", get_serr(), ")"
84+
85+
! Clear error flag so the next call to stoi succeeds
86+
ierr = 0
87+
end do
88+
end function
89+
end program
2790

2891
!-----------------------------------------------------------------------------!
2992
! end of example/sort.f90

0 commit comments

Comments
 (0)