-
Notifications
You must be signed in to change notification settings - Fork 10
What are containers?
Containers are a bit of overloaded terminology nowadays. In our context, containers are software objects whose purpose are to hold dynamic collections of other software objects. For Fortran developers who may be less familiar with object-oriented terminology, for our purposes, an object is a data structure that has attached procedures (aka "methods") that perform key operations.
One important example of containers that is built into Fortran is the array. (A purist would probably insist that we restrict ourselves to allocatable arrays.) Each Fortran array contains a logically rectangular set of elements of the same type, and that type can be an intrinsic type or a user-defined derived type. Individual elements are accessed by specifying a tuple of indices: arr(i,j,k)
. Fortran provides various "methods" for arrays:
SIZE(arr)
SHAPE(arr)
LBOUND(arr)
- ...
While arrays are the perfect tool when you know the size of your collection before you need to start putting items into the array. For example, when an array is used to represent a physical quantity on some 3D cartesian grid, one would typically first allocate the array ALLOCATE(arr(nx,ny,nz))
and then loop through the elements to assign values. If you later want that array to be larger (or smaller) you must explicitly deallocate and reallocate the array. (And, of course, somehow save the elements that are to be reused.)
In many algorithms, the size of a collection is not known in advance and is instead repeatedly updated, typically during some iteration process. An example of this that may be familiar to some Fortran developers is that of sparse matrices. Here, we only want to store the non-zero elements of a matrix, but the number of non-zero elements may change during the course of a simulation. Fortran does not provide direct support for sparse matrices, but many libraries have been developed that use a variety of compression schemes.