Module stack_mod
Uses:
module kinds_mod
Types:
public type stack
Subroutines and functions:
private subroutine stack_create (st, inisize)
private subroutine stack_delete (st)
private subroutine stack_trim (st)
private subroutine stack_push (st, v)
private pure subroutine stack_push_fast (st, v)
private subroutine stack_push2 (st, v1, v2)
private subroutine stack_push_array (st, v)
private subroutine stack_move (st_src, st_dest)
private subroutine stack_remove_duplicates (st)
private subroutine stack_binary_search (st, v, n, idx)
A simple stack of integers which grows ad infinutum if required.
Data is stored in a single contiguous array.
type(stack) :: st
call stack_create(st,10)
stack_push(st,4)
stack_push(st,7)
print *,'first element on stack is:',st%a(1)
call stack_delete(st)
Clear the stack, i.e. set topmost element = bottom.
The size of the storage array is left unchanged.
st%pos = 0
Get element from stack.
The index must be in the interval 1 <= i <= size(st%a).
st%a(i)
Add
use stack_mod
to your module/subroutine and
include 'stack.incf'
after the contains statement to enable inlining of the stack routines.
Author: Matthias Lieber
Description of Types
stack
public type stack
integer (kind=i_k), pointer, dimension (:) :: a => null ()
integer (kind=i_k) :: pos = 0
end type stack
Components:
a | the actual data is stored in a |
pos | position of topmost element in the stack |
Description of Subroutines and Functions
stack_create
private subroutine stack_create (st, inisize)
type (stack), intent(inout) :: st
integer (kind=i_k), intent(in) :: inisize
end subroutine stack_create
Parameters:
st | the stack |
inisize | initial size of array |
Create a stack or reallocate at given size.
If the stack is already allocated, the function reallocates
the stack only in the case, that the current stack size is
less then inisize. Otherwise the stack is only cleared,
i.e. st%pos is set to 0.
stack_delete
private subroutine stack_delete (st)
type (stack), intent(inout) :: st
end subroutine stack_delete
Parameters:
Delete the stack and all of its memory.
stack_trim
private subroutine stack_trim (st)
type (stack), intent(inout) :: st
end subroutine stack_trim
Parameters:
Reallocate the stack array to exactly fit the required size.
stack_push
private subroutine stack_push (st, v)
type (stack), intent(inout) :: st
integer (kind=i_k), intent(in) :: v
end subroutine stack_push
Parameters:
st | the stack |
v | value to push to stack |
Push a new element to the top of the stack.
The storage array will be enlarged by a factor of two if required.
stack_push_fast
private pure subroutine stack_push_fast (st, v)
type (stack), intent(inout) :: st
integer (kind=i_k), intent(in) :: v
end subroutine stack_push_fast
Parameters:
st | the stack |
v | value to push to stack |
Push a new element to the top of the stack.
The storage array will not be enlarged and no checking is performed whether the space is large enough.
Only call this subroutine, if you really know what you are doing!
stack_push2
private subroutine stack_push2 (st, v1, v2)
type (stack), intent(inout) :: st
integer (kind=i_k), intent(in) :: v1
integer (kind=i_k), intent(in) :: v2
end subroutine stack_push2
Parameters:
st | the stack |
v1 | values to push to stack |
v2 | values to push to stack |
Push two new elements to the top of the stack.
The storage array will be enlarged by a factor of two if required.
stack_push_array
private subroutine stack_push_array (st, v)
type (stack), intent(inout) :: st
integer (kind=i_k), intent(in), dimension (:) :: v
end subroutine stack_push_array
Parameters:
st | the stack |
v | values to push to stack |
Push new elements to the top of the stack.
The storage array will be enlarged by a factor of two if required.
stack_move
private subroutine stack_move (st_src, st_dest)
type (stack), intent(inout) :: st_src
type (stack), intent(inout) :: st_dest
end subroutine stack_move
Parameters:
st_src | the stack |
st_dest | the stack |
Move (shallow copy) the data from
st_src to
st_dest.
All data contained in
st_dest will be removed.
st_src will be empty on return.
stack_remove_duplicates
private subroutine stack_remove_duplicates (st)
type (stack), intent(inout) :: st
end subroutine stack_remove_duplicates
Parameters:
Remove duplicates from
st, this is damn costly
stack_binary_search
private subroutine stack_binary_search (st, v, n, idx)
type (stack), intent(in) :: st
integer (kind=i_k), intent(in) :: v
integer (kind=i_k), intent(in) :: n
integer (kind=i_k), intent(out) :: idx
end subroutine stack_binary_search
Parameters:
st | the stack |
v | value to find in the stack |
n | number of elements to scan (upper bound) |
idx | position of found item, 0 if not found |
Binary search!
The first
n elements of the stack are searched for
v.
Of course the stack must be sorted for the binary search to work!