Skip to content

ftlArray

Robert Rüger edited this page Mar 29, 2018 · 4 revisions

ftlArray provides iterators for plain Fortran arrays. This allows the ftlAlgorithms to work on normal Fortran arrays.

If the ftlArray template is instantiated for a particular template type, it provides Begin() and End() functions for arrays of that type. These functions return random access iterators that can be used for calling methods from ftlAlgorithms. In principle template instantiation works exactly like it does for ftlDynArray, only that the resulting module does not provide a container object (because the array itself is the container), but only the iterators. This sounds much more complicated than it is. Here is an example:

#define FTL_TEMPLATE_TYPE integer
#define FTL_TEMPLATE_TYPE_NAME Int
#define FTL_TEMPLATE_TYPE_PROVIDES_COMPARATOR_SMALLER
#define FTL_INSTANTIATE_TEMPLATE
#include "ftlArray.F90_template"
#include "ftlAlgorithms.F90_template"

This produces the module ftlArrayIntModule which gives all arrays of integer the Begin() and End() methods, that return random access iterators. That in itself is not too useful, but we also instatiated the ftlAlgorithms template, which produced the ftlArrayIntAlgorithmsModule module that provides the usual algorithms working on plain Fortran arrays of integers:

use ftlArrayIntModule
use ftlArrayIntAlgorithmsModule

integer, allocatable :: a(:)

a = [ 42, 37, 3, 51 ]
call ftlSort(a)
write (*,*) a ! prints: 3, 37, 42, 51

Note that we could also have done the template instantiation in two steps, first ftlArray and then ftlAlgorithms, but since ftlArray is not particularly useful in itself, it makes a lot of sense to do it in one go ...

Instantiation

Macros

ftlArray uses the following macros for instantiation:

FTL_TEMPLATE_TYPE
The type of the objects to be stored in the array, e.g. integer(int64) or MyDerivedType. Do not add the enclosing type() for derived types.
FTL_TEMPLATE_TYPE_IS_DERIVED
This needs to be defined if FTL_TEMPLATE_TYPE is a derived type.
FTL_TEMPLATE_TYPE_MODULE
The name of the module in which FTL_TEMPLATE_TYPE is defined. Specifying this is probably only necessary if FTL_TEMPLATE_TYPE is a derived type, or uses a kind defined in another module, e.g. int64 from the iso_fortran_env module.
FTL_TEMPLATE_TYPE_NAME
A convenient user-readable name for the stored type, e.g. Int. This will be used for the name of the module which provides the iterators for the arrays. Setting this to Int will result in the module ftlArrayIntModule.
FTL_INSTANTIATE_TEMPLATE
If this macro is defined the template will be instantiated. If not, the template will not be instantiated, but some information will be passed on to subsequent template instantiations (e.g. ftlAlgorithms).

Methods

Iterators

Begin(array)

Returns a random access iterator to the first element of the array.

ftlArrayTIterator function Begin(array)
   T, intent(in) :: array(:)

If the container is empty, the returned iterator will be equal to End().

End(array)

Returns a random access iterator to the element following the last element of the array. This element acts as a placeholder; attempting to access it results in undefined behavior.

ftlArrayTIterator function End(array)
   T, intent(in) :: array(:)
Clone this wiki locally