Class that act as stacks, queues, and priority queues. These classes use dynamically allocated contiguous blocks of memory to store a list of numbers. The queues can be sorted to become priority queues and use binary searches to quickly insert new numbers. If the allocated memory is filled, the available space is doubled. Memory is only reallocated to a smaller size, if the utilization is a quarter of that allocated. The array can be specified as fixed, so that no reallocation occurs. This is useful for heaps of given like k nearest neighbours, or k smallest.
program dynamicArray_test use variableKind, only: i32 use dDynamicArray_Class, only: dDynamicArray implicit none type(dDynamicArray) :: da, da2 integer(i32) :: ia da = dDynamicArray(10) call da%insertAt(1, 10.d0) call da%insertAt(1, 20.d0) call da%prepend(30.d0) call da%append(40.d0) call da%remove(2) call da%tighten() da2 = da da2%values(2) = 50.d0 call da%deallocate() call da2%deallocate() da = dDynamicArray(3, sorted=.true.) call da%insertSorted(20.d0) call da%insertSorted(30.d0) call da%insertSorted(10.d0) ia = da%locationOf(20.d0) call da%insertSortedUnique(10.d0) call da%insertSortedUnique(15.d0) call da%deallocate() da = dDynamicArray(3, sorted=.true., fixed=.true.) call da%insertSorted(20.d0) call da%insertSorted(30.d0) call da%insertSorted(10.d0) ia = da%locationOf(20.d0) call da%insertSortedUnique(10.d0) call da%insertSortedUnique(15.d0) call da%deallocate() end program
Overloaded by interface dDynamicArray()
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=i32), | intent(in), | optional | :: | M | Amount of memory to allocate. |
|
logical, | intent(in), | optional | :: | sorted | Maintain a sorted array. |
|
logical, | intent(in), | optional | :: | fixed | Maintain a fixed size array. |
Return type.
Overloaded by interface dDynamicArray()
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=r64), | intent(in) | :: | values(:) | Set of values to initialize with. |
||
integer(kind=i32), | intent(in), | optional | :: | M | Amount of memory to allocate. |
|
logical, | intent(in), | optional | :: | sorted | Maintain a sorted array. |
|
logical, | intent(in), | optional | :: | fixed | Maintain a fixed size array. |
Return type
Class that act as stacks, queues, and priority queues. See dDynamicArray_Class for more information on how to use this class.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer(kind=i32), | public | :: | N | Current size of the array |
|||
real(kind=r64), | public, | allocatable | :: | values(:) | Memory for values, can be larger than N |
||
logical, | public | :: | sorted | = | .false. | Keep track of whether the array is sorted for potential speed increases |
|
logical, | public | :: | fixed | = | .false. | Don't allow the memory to change after initial instantiation. |
private function init_dDynamicArray_i1(M, sorted, fixed) | Overloaded by interface dDynamicArray() |
private function init_dDynamicArray_d1D(values, M, sorted, fixed) | Overloaded by interface dDynamicArray() |
procedure, public :: append => append_dDynamicArray | dDynamicArray%append() - Append a value to the end of the dynamic array. Will change a sorted dynamic array to unsorted. |
procedure, public :: deallocate => deallocate_dDynamicArray | dDynamicArray%deallocate() - Deallocate a dynamic array. |
procedure, public :: insertAt => insertAt_dDynamicArray | dDynamicArray%insertAt() - Insert a value at a given index. |
procedure, public :: insertSorted => insertSorted_dDynamicArray | dDynamicArray%insertSorted() - Insert a value into a sorted dynamic array. |
procedure, public :: insertSortedUnique => insertSortedUnique_dDynamicArray | dDynamicArray%insertSortedUnique() - Inserts only unique numbers into a dynamic array. |
procedure, public :: isEmpty => isEmpty_dDynamicArray | dDynamicArray%isEmpty() - True if the array is empty. |
procedure, public :: isFilled => isFilled_dDynamicArray | dDynamicArray%isFilled() - True if the array is filled. |
procedure, public :: locationOf => locationOf_dDynamicArray | dDynamicArray%locationOf() - Get the location of a value in a sorted dynamic array. |
procedure, public :: prepend => prepend_dDynamicArray | dDynamicArray%prepend() - Prepend a value to the start of the dynamic array. Only for unsorted dynamic arrays |
procedure, public :: reallocate => reallocate_dDynamicArray | dDynamicArray%reallocate() - Create new contiguous memory to match the needs of the expanding or shrinking array. |
procedure, public :: remove => remove_dDynamicArray | dDynamicArray%remove() - Remove an element from the array. |
procedure, public :: tighten => tighten_dDynamicArray | dDynamicArray%tighten() - Removes excess buffer memory and trims it to the current length. |