sorting_module Module

Generic searching and sorting routines.



Interfaces

public interface expand_vector

Add elements to a vector in chunks.

  • private subroutine item_expand_vector(vec, n, chunk_size, val, finished)

    Arguments

    Type IntentOptional Attributes Name
    class(item), intent(inout), dimension(:), allocatable :: vec
    integer, intent(inout) :: n

    counter for last element added to vec. must be initialized to size(vec) (or 0 if not allocated) before first call

    integer, intent(in) :: chunk_size

    allocate vec in blocks of this size (>0)

    class(item), intent(in), optional :: val

    the value to add to vec

    logical, intent(in), optional :: finished

    set to true to return vec as its correct size (n)

  • private subroutine int32_expand_vector(vec, n, chunk_size, val, finished)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int32), intent(inout), dimension(:), allocatable :: vec
    integer, intent(inout) :: n

    counter for last element added to vec. must be initialized to size(vec) (or 0 if not allocated) before first call

    integer, intent(in) :: chunk_size

    allocate vec in blocks of this size (>0)

    integer(kind=int32), intent(in), optional :: val

    the value to add to vec

    logical, intent(in), optional :: finished

    set to true to return vec as its correct size (n)

  • private subroutine int64_expand_vector(vec, n, chunk_size, val, finished)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int64), intent(inout), dimension(:), allocatable :: vec
    integer, intent(inout) :: n

    counter for last element added to vec. must be initialized to size(vec) (or 0 if not allocated) before first call

    integer, intent(in) :: chunk_size

    allocate vec in blocks of this size (>0)

    integer(kind=int64), intent(in), optional :: val

    the value to add to vec

    logical, intent(in), optional :: finished

    set to true to return vec as its correct size (n)

  • private subroutine real64_expand_vector(vec, n, chunk_size, val, finished)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=real64), intent(inout), dimension(:), allocatable :: vec
    integer, intent(inout) :: n

    counter for last element added to vec. must be initialized to size(vec) (or 0 if not allocated) before first call

    integer, intent(in) :: chunk_size

    allocate vec in blocks of this size (>0)

    real(kind=real64), intent(in), optional :: val

    the value to add to vec

    logical, intent(in), optional :: finished

    set to true to return vec as its correct size (n)

  • private subroutine real32_expand_vector(vec, n, chunk_size, val, finished)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=real32), intent(inout), dimension(:), allocatable :: vec
    integer, intent(inout) :: n

    counter for last element added to vec. must be initialized to size(vec) (or 0 if not allocated) before first call

    integer, intent(in) :: chunk_size

    allocate vec in blocks of this size (>0)

    real(kind=real32), intent(in), optional :: val

    the value to add to vec

    logical, intent(in), optional :: finished

    set to true to return vec as its correct size (n)

public interface unique

Returns only the unique elements of the vector.

  • private subroutine item_unique(vec, vec_unique, chunk_size)

    Arguments

    Type IntentOptional Attributes Name
    class(item), intent(in), dimension(:) :: vec

    a vector of items

    class(item), intent(out), dimension(:), allocatable :: vec_unique

    unique elements of vec

    integer, intent(in), optional :: chunk_size

    chunk size for expanding arrays. if not present, default is 100.

  • private subroutine int32_unique(vec, vec_unique, chunk_size)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int32), intent(in), dimension(:) :: vec

    a vector

    integer(kind=int32), intent(out), dimension(:), allocatable :: vec_unique

    unique elements of vec

    integer, intent(in), optional :: chunk_size

    chunk size for expanding arrays. if not present, default is 100.

  • private subroutine int64_unique(vec, vec_unique, chunk_size)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int64), intent(in), dimension(:) :: vec

    a vector

    integer(kind=int64), intent(out), dimension(:), allocatable :: vec_unique

    unique elements of vec

    integer, intent(in), optional :: chunk_size

    chunk size for expanding arrays. if not present, default is 100.

  • private subroutine real64_unique(vec, vec_unique, chunk_size)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=real64), intent(in), dimension(:) :: vec

    a vector

    real(kind=real64), intent(out), dimension(:), allocatable :: vec_unique

    unique elements of vec

    integer, intent(in), optional :: chunk_size

    chunk size for expanding arrays. if not present, default is 100.

  • private subroutine real32_unique(vec, vec_unique, chunk_size)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=real32), intent(in), dimension(:) :: vec

    a vector

    real(kind=real32), intent(out), dimension(:), allocatable :: vec_unique

    unique elements of vec

    integer, intent(in), optional :: chunk_size

    chunk size for expanding arrays. if not present, default is 100.

public interface sort_ascending

Sorts an array in increasing order.

Uses a basic recursive quicksort (with insertion sort for partitions with 20 elements). Replaces the original array.

  • private subroutine item_sort_ascending(vec)

    Arguments

    Type IntentOptional Attributes Name
    class(item), intent(inout), dimension(:) :: vec
  • private subroutine int32_sort_ascending(vec)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int32), intent(inout), dimension(:) :: vec
  • private subroutine int64_sort_ascending(vec)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int64), intent(inout), dimension(:) :: vec
  • private subroutine real64_sort_ascending(vec)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=real64), intent(inout), dimension(:) :: vec
  • private subroutine real32_sort_ascending(vec)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=real32), intent(inout), dimension(:) :: vec

public interface sort_descending

Sorts an array in decreasing order.

Uses a basic recursive quicksort (with insertion sort for partitions with 20 elements). Replaces the original array.

  • private subroutine item_sort_descending(vec)

    Arguments

    Type IntentOptional Attributes Name
    class(item), intent(inout), dimension(:) :: vec
  • private subroutine int32_sort_descending(vec)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int32), intent(inout), dimension(:) :: vec
  • private subroutine int64_sort_descending(vec)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int64), intent(inout), dimension(:) :: vec
  • private subroutine real64_sort_descending(vec)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=real64), intent(inout), dimension(:) :: vec
  • private subroutine real32_sort_descending(vec)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=real32), intent(inout), dimension(:) :: vec

public interface binary_search

Binary search of a sorted array. (assumed to be in ascending order)

  • private function item_binary_search(val, vec) result(jloc)

    Arguments

    Type IntentOptional Attributes Name
    class(item), intent(in) :: val

    value to match in vec

    class(item), intent(in), dimension(:) :: vec

    array to search (it is assumed to be sorted)

    Return Value integer

    the first matched index in vec (if not found, 0 is returned)

  • private function int32_binary_search(val, vec) result(jloc)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int32), intent(in) :: val

    value to match in vec

    integer(kind=int32), intent(in), dimension(:) :: vec

    array to search (it is assumed to be sorted)

    Return Value integer

    the first matched index in vec (if not found, 0 is returned)

  • private function int64_binary_search(val, vec) result(jloc)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int64), intent(in) :: val

    value to match in vec

    integer(kind=int64), intent(in), dimension(:) :: vec

    array to search (it is assumed to be sorted)

    Return Value integer

    the first matched index in vec (if not found, 0 is returned)

  • private function real64_binary_search(val, vec) result(jloc)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=real64), intent(in) :: val

    value to match in vec

    real(kind=real64), intent(in), dimension(:) :: vec

    array to search (it is assumed to be sorted)

    Return Value integer

    the first matched index in vec (if not found, 0 is returned)

  • private function real32_binary_search(val, vec) result(jloc)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=real32), intent(in) :: val

    value to match in vec

    real(kind=real32), intent(in), dimension(:) :: vec

    array to search (it is assumed to be sorted)

    Return Value integer

    the first matched index in vec (if not found, 0 is returned)


Derived Types

type, public ::  item

An item to be sorted or searched.

Read more…

Type-Bound Procedures

generic, public :: operator(>) => greater_than
generic, public :: operator(<) => less_than
generic, public :: operator(==) => equal_to
generic, public :: assignment(=) => assign_equal
procedure(greater_than_func), public, deferred :: greater_than
procedure(less_than_func), public, deferred :: less_than
procedure(equal_to_func), public, deferred :: equal_to
procedure(assign_equal_func), public, deferred :: assign_equal