Skip to content

Commit 5880cf5

Browse files
committed
Improve documentation
1 parent 73b736e commit 5880cf5

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

doc/examples.rst

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,11 @@ Examples
99
The following standalone codes demonstrate how Flibcpp can be used in native
1010
Fortran code.
1111

12-
String conversion and sort
12+
Random numbers and sorting
1313
==========================
1414

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.
15+
This simple example generates an array of normally-distributed double-precision
16+
reals, sorts them, and then shuffles them again.
2217

2318
.. literalinclude:: ../example/sort.f90
2419
:linenos:
@@ -32,6 +27,39 @@ from native Fortran strings.
3227
.. literalinclude:: ../example/vecstr.f90
3328
:linenos:
3429

30+
.. _example_generic:
31+
32+
Generic sorting
33+
===============
34+
35+
Since sorting algorithms often allow :math:`O(N)` algorithms to be written in
36+
:math:`O(\log N)`, providing generic sorting routines is immensely useful in
37+
applications that operate on large chunks of data. This example demonstrates
38+
the generic version of the :ref:`modules_algorithm_argsort` subroutine by
39+
sorting a native Fortran array of native Fortran types using a native Fortran
40+
subroutine. The only C interaction needed is to create C pointers to the
41+
Fortran array entries and to provide a C-bound comparator that converts those
42+
pointers back to native Fortran pointers.
43+
44+
.. literalinclude:: ../example/sort_generic.f90
45+
:linenos:
46+
47+
.. _example_utils:
48+
49+
Example utilities module
50+
========================
51+
52+
This pure-Fortran module builds on top of functionality from Flibcpp. It
53+
provides procedures to:
54+
55+
- Format and print the Flibcpp version;
56+
- Converts a user input to an integer, validating it with useful error
57+
messages;
58+
- Reads a dynamically sized vector of strings from the user.
59+
60+
.. literalinclude:: ../example/example_utils.f90
61+
:linenos:
62+
3563
.. ############################################################################
3664
.. end of doc/examples.rst
3765
.. ############################################################################

doc/modules/algorithm.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ Sorting
2121

2222
Sorting algorithms for numeric types default to increasing order when provided
2323
with a single array argument. Numeric sorting routines accept an optional
24-
second argument, a comparator function,
24+
second argument, a comparator function, which should return ``true`` if the
25+
first argument is strictly less than the right-hand side.
26+
27+
.. warning:: For every value of ``a`` and ``b``, the comparator ``cmp`` *must*
28+
satisfy ``.not. (cmp(a, b) .and. cmp(b, a))``. If this strict ordering is
29+
not satisfied, some of the algorithms below may crash the program.
30+
31+
All sorting algorithms are *also* instantiated so that they accept an array of
32+
``type(C_PTR)`` and a generic comparator function. **This enables arrays of any
33+
native Fortran object to be sorted**. See :ref:`the generic
34+
sorting example <example_generic>` for a demonstration.
2535

2636
sort
2737
----
@@ -45,6 +55,8 @@ Checking the ordering of array is just as simple::
4555

4656
sortitude = is_sorted(iarr)
4757

58+
.. _modules_algorithm_argsort:
59+
4860
argsort
4961
-------
5062

@@ -75,6 +87,10 @@ zero.
7587
Searching
7688
=========
7789

90+
Like the sorting algorithms, searching algorithms are instantiated on numeric
91+
types and the C pointer type, and they provide an optional procedure pointer
92+
argument that allows the arrays to be ordered with an arbitrary comparator.
93+
7894
.. _modules_algorithm_binary_search:
7995

8096
binary_search

0 commit comments

Comments
 (0)