|
| 1 | +.. ############################################################################ |
| 2 | +.. File : doc/modules/vector.rst |
| 3 | +.. ############################################################################ |
| 4 | +
|
| 5 | +.. _modules_Vector: |
| 6 | + |
| 7 | +****** |
| 8 | +Vector |
| 9 | +****** |
| 10 | + |
| 11 | +Vectors are resizeable arrays of elements. All vectors support the following |
| 12 | +basic operations. |
| 13 | + |
| 14 | +Construction and destruction |
| 15 | +---------------------------- |
| 16 | + |
| 17 | +Vectors are constructed using three interface functions: |
| 18 | + |
| 19 | + - The function without arguments creates an empty vector; |
| 20 | + - A single integer argument assigns that many elements with default values; |
| 21 | + and |
| 22 | + - An integer argument followed by an element with the vector's element type |
| 23 | + will copy that value to all elements of the vector. |
| 24 | + |
| 25 | +(To do: copy construction) |
| 26 | + |
| 27 | +Here are three examples of initialization:: |
| 28 | + |
| 29 | + use flc_vector, only : Vector => VectorInt4 |
| 30 | + type(Vector) :: v |
| 31 | + |
| 32 | + v = Vector() |
| 33 | + ! v%size() == 0 |
| 34 | + v = Vector(10) |
| 35 | + ! v%size() == 10 |
| 36 | + ! v%get(i) == 0 |
| 37 | + v = Vector(10, 123) |
| 38 | + ! v%size() == 10 |
| 39 | + ! v%get(i) == 123 |
| 40 | + |
| 41 | +Vectors are destroyed using the ``release`` type-bound subroutine:: |
| 42 | + |
| 43 | + call v%release() |
| 44 | + |
| 45 | +Modification |
| 46 | +------------ |
| 47 | + |
| 48 | +Vectors can be resized dynamically using ``resize``, which acts like the |
| 49 | +constructors described above. An element can be added to |
| 50 | +the end of the vector (increasing the size by one) with ``push_back``. The |
| 51 | +``insert`` method can insert an element at a specific index, and ``erase`` |
| 52 | +removes a specific vector index or range of indices. ``clear`` removes |
| 53 | +all elements. Finally, ``set`` sets the value of an element at a given index. |
| 54 | + |
| 55 | +.. important:: Unlike the C++ version of this class, **all vectors in flibcpp |
| 56 | + use 1-offset indexing**. This means that ``v%get(1)`` is the same as the C++ |
| 57 | + ``v[0]``: it returns the first element (i.e. the element with an offset of |
| 58 | + zero). |
| 59 | + |
| 60 | +Here's an example of modifying a vector:: |
| 61 | + |
| 62 | + use flc_vector, only : Vector => VectorInt4 |
| 63 | + type(Vector) :: v |
| 64 | + v = Vector() |
| 65 | + call v%resize(4, 123) ! give each element the value 123 |
| 66 | + call v%push_back(-1) ! size increased by 1, last element has value -1 |
| 67 | + call v%insert(2, -2) ! First 3 elements are [123, 123, -2] |
| 68 | + call v%erase(1, 3) ! Remove the first two elements |
| 69 | + call v%erase(2) ! Remove the second element |
| 70 | + call v%set(1, -123) ! Change the value of the first element |
| 71 | + call v%clear() ! Remove all elements, size is now zero |
| 72 | + |
| 73 | +Access |
| 74 | +------ |
| 75 | + |
| 76 | +The size of a vector is returned by the bound function ``size``; ``get`` |
| 77 | +returns the value at an index; and ``front`` and ``back`` are aliases for |
| 78 | +``get(1)`` and ``get(v%size())``, respectively. |
| 79 | + |
| 80 | +Numeric vectors |
| 81 | +=============== |
| 82 | + |
| 83 | +As with the algorithms and other methods, the ``flc_vector`` module includes |
| 84 | +three numeric instantiations. They each have distinct derived types: |
| 85 | + |
| 86 | + - ``VectorInt4``: each element is ``integer(4)`` |
| 87 | + - ``VectorInt8``: each element is ``integer(8)`` |
| 88 | + - ``VectorReal8``: each element is ``real(8)`` |
| 89 | + |
| 90 | +Construct from an array |
| 91 | +----------------------- |
| 92 | + |
| 93 | +Numeric vectors can be created very efficiently from Fortran data by accepting |
| 94 | +an array pointer:: |
| 95 | + |
| 96 | + use flc_vector, only : Vector => VectorInt4 |
| 97 | + integer(4), dimension(4), parameter :: iarr = [ 1, -2, 4, -8 ] |
| 98 | + type(Vector) :: v |
| 99 | + |
| 100 | + v = Vector(iarr) |
| 101 | + write(0,*) "Size should be 4:", v%size() |
| 102 | + |
| 103 | +View as an array pointer |
| 104 | +------------------------ |
| 105 | + |
| 106 | +Numeric vectors can also return an array pointer to the vector's contents. |
| 107 | +These views support native Fortran array operations and access the same |
| 108 | +underlying memory as the C++ object:: |
| 109 | + |
| 110 | + use flc_vector, only : Vector => VectorInt4 |
| 111 | + integer(4), dimension(:), pointer :: vptr |
| 112 | + type(Vector) :: v |
| 113 | + |
| 114 | + ! <snip> |
| 115 | + vptr => v%view() |
| 116 | + if (size(vptr) > 2) then |
| 117 | + vptr(2) = 4 |
| 118 | + end if |
| 119 | + |
| 120 | +.. warning:: A vector's view is valid **only** as long as the vector's size is |
| 121 | + not changed. Calling ``erase``, ``push_back``, and so forth will invalidate |
| 122 | + the view; accessing it at that point results in undefined behavior. |
| 123 | + |
| 124 | +String vectors |
| 125 | +============== |
| 126 | + |
| 127 | +String vectors are not yet implemented. |
| 128 | + |
| 129 | +.. ############################################################################ |
| 130 | +.. end of doc/modules/vector.rst |
| 131 | +.. ############################################################################ |
0 commit comments