linked_list_module Module

A generic list.

It uses an unlimited polymorphic class(*) pointer variable to allow it to contain any type of data. The key can be an integer, string, or any user-defined key_class.


Uses

  • module~~linked_list_module~~UsesGraph module~linked_list_module linked_list_module iso_fortran_env iso_fortran_env module~linked_list_module->iso_fortran_env module~key_module key_module module~linked_list_module->module~key_module module~key_module->iso_fortran_env

Interfaces

public interface list

  • private function initialize_list(case_sensitive) result(lst)

    list constructor.

    Arguments

    Type IntentOptional Attributes Name
    logical, intent(in) :: case_sensitive

    if true, then string key searches are case sensitive.

    Return Value type(list)


Abstract Interfaces

abstract interface

  • private subroutine iterator_func(me, done)

    internal function for traversing all nodes in a list

    Arguments

    Type IntentOptional Attributes Name
    type(node), pointer :: me
    logical, intent(out) :: done

    set to true to stop traversing

abstract interface

  • private subroutine key_iterator(key, value, done)

    for traversing all keys in a list

    Arguments

    Type IntentOptional Attributes Name
    class(*), intent(in) :: key

    the node key

    class(*), pointer :: value

    pointer to the node value

    logical, intent(out) :: done

    set to true to stop traversing


Derived Types

type, public ::  node

a node in the linked list. This is the container to the unlimited polymorphic value variable.

Components

Type Visibility Attributes Name Initial
class(*), private, allocatable :: key

the key (can be integer, string, or key_class)

class(*), private, pointer :: value => null()

the data to hold

logical, private :: destroy_on_delete = .true.

if true, value pointer is deallocated when it is removed from the list, or the list is destroyed. If false, it is only nullified.

type(node), private, pointer :: next => null()

the next one in the list

type(node), private, pointer :: previous => null()

the previous one in the list

Type-Bound Procedures

procedure, public :: destroy => destroy_node_data ../../

deallocate value

procedure, public :: get_data => get_node_data ../../

get data from a node

type, public ::  list

linked list of pointers to polymorphic types.

Components

Type Visibility Attributes Name Initial
logical, private :: case_sensitive = .true.

character key lookup is case sensitive

integer, private :: count = 0

number of items in the list

type(node), private, pointer :: head => null()

the first item in the list

type(node), private, pointer :: tail => null()

the last item in the list

Constructor

private function initialize_list (case_sensitive)

list constructor.

Finalizations Procedures

final :: list_finalizer

Type-Bound Procedures

procedure, public :: add_pointer ../../

add a pointer item to the list

procedure, public :: add_clone ../../

add a non-pointer item to the list

procedure, public :: get => get_data ../../

get a pointer to an item in the list

procedure, public :: destroy => destroy_list ../../

destroy the list and deallocate/finalize all the data

procedure, public :: has_key ../../

if the key is present in the list

procedure, public :: traverse ../../

traverse the list are return each key & value

procedure, public :: remove => remove_by_key ../../

remove item from the list, given the key

procedure, public :: remove_by_pointer ../../

remove node from list, given pointer to it

procedure, public :: get_node ../../

get a pointer to a node in the list

procedure, public :: traverse_list ../../

traverse each node of the list

procedure, private :: keys_equal ../../

for testing key string equality


Functions

private function has_key(me, key)

Returns true if the key is present in the list

Arguments

Type IntentOptional Attributes Name
class(list), intent(inout) :: me
class(*), intent(in) :: key

Return Value logical

private function initialize_list(case_sensitive) result(lst)

list constructor.

Arguments

Type IntentOptional Attributes Name
logical, intent(in) :: case_sensitive

if true, then string key searches are case sensitive.

Return Value type(list)

private pure function keys_equal(me, k1, k2)

Returns true if the two keys are equal.

Read more…

Arguments

Type IntentOptional Attributes Name
class(list), intent(in) :: me
class(*), intent(in) :: k1
class(*), intent(in) :: k2

Return Value logical

private pure function uppercase(str) result(string)

Convert a string to uppercase.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str

Return Value character(len=len)


Subroutines

private subroutine traverse_list(me, iterator)

traverse list from head to tail, and call the iterator function for each node.

Arguments

Type IntentOptional Attributes Name
class(list), intent(inout) :: me
procedure(iterator_func) :: iterator

the function to call for each node.

private subroutine traverse(me, iterator)

traverse list from head to tail, and call the iterator function for each key.

Arguments

Type IntentOptional Attributes Name
class(list), intent(inout) :: me
procedure(key_iterator) :: iterator

the function to call for each node.

private impure elemental subroutine destroy_node_data(me)

destroy the data in the node.

Arguments

Type IntentOptional Attributes Name
class(node), intent(inout) :: me

private impure elemental subroutine list_finalizer(me)

just a wrapper for destroy_list.

Arguments

Type IntentOptional Attributes Name
type(list), intent(inout) :: me

private impure elemental subroutine destroy_list(me)

destroy the list (traverses from head to tail)

Arguments

Type IntentOptional Attributes Name
class(list), intent(inout) :: me

private impure recursive subroutine destroy_node(me)

destroy the node (and subsequent ones in the list).

Arguments

Type IntentOptional Attributes Name
type(node), pointer :: me

private subroutine remove_by_key(me, key)

Remove an item from the list (given the key).

Arguments

Type IntentOptional Attributes Name
class(list), intent(inout) :: me
class(*), intent(in) :: key

private subroutine remove_by_pointer(me, p)

Remove an item from the list.

Arguments

Type IntentOptional Attributes Name
class(list), intent(inout) :: me
type(node), pointer :: p

the item to remove

private subroutine get_node_data(me, value)

Get the data from a node

Arguments

Type IntentOptional Attributes Name
class(node), intent(in) :: me
class(*), intent(out), pointer :: value

private subroutine get_data(me, key, value)

Returns a pointer to the data stored in the list.

Arguments

Type IntentOptional Attributes Name
class(list), intent(in) :: me
class(*), intent(in) :: key
class(*), intent(out), pointer :: value

private subroutine get_node(me, key, p_node)

Returns a pointer to a node in a list.

Arguments

Type IntentOptional Attributes Name
class(list), intent(in) :: me
class(*), intent(in) :: key
type(node), intent(out), pointer :: p_node

private subroutine add_clone(me, key, value)

Add an item to the end of the list by cloning it. That is, using a sourced allocation: allocate(newitem, source=value). A clone is made of the original value, which is not affected. The list contains only the clone, which will be deallocated (and finalized if a finalizer is present) when removed from the list.

Read more…

Arguments

Type IntentOptional Attributes Name
class(list), intent(inout) :: me
class(*), intent(in) :: key
class(*), intent(in) :: value

private subroutine add_pointer(me, key, value, destroy_on_delete)

Add an item to the list, and associate its pointer to the input value.

Read more…

Arguments

Type IntentOptional Attributes Name
class(list), intent(inout) :: me
class(*), intent(in) :: key
class(*), intent(in), pointer :: value

value is unlimited polymorphic, so it can be any scalar type. If the type includes pointers or other objects that must be cleaned up when it is destroyed, then it should include a finalizer.

logical, intent(in), optional :: destroy_on_delete

If false, the finalizer will not be called when the item is removed from the list (the pointer will only be nullified, so the caller is responsible for cleaning it up to avoid memory leaks). The default is True.