list Derived Type

type, public :: list

linked list of pointers to polymorphic types.


Inherits

type~~list~~InheritsGraph type~list list type~node node type~list->type~node head, tail type~node->type~node next, previous

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

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)


Finalization Procedures

final :: list_finalizer

  • private impure elemental subroutine list_finalizer(me)

    just a wrapper for destroy_list.

    Arguments

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

Type-Bound Procedures

procedure, public :: add_pointer

add a pointer item to the list

  • 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.

procedure, public :: add_clone

add a non-pointer item to the list

  • 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

procedure, public :: get => get_data

get a pointer to an item in the list

  • 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

procedure, public :: destroy => destroy_list

destroy the list and deallocate/finalize all the data

  • 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

procedure, public :: has_key

if the key is present in the list

  • 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

procedure, public :: traverse

traverse the list are return each key & value

  • 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.

procedure, public :: remove => remove_by_key

remove item from the list, given the key

  • 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

procedure, public :: remove_by_pointer

remove node from list, given pointer to it

  • 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

procedure, public :: get_node

get a pointer to a node in the list

  • 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

procedure, public :: traverse_list

traverse each node of the list

  • 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.

procedure, private :: keys_equal

for testing key string equality

  • 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

Source Code

    type,public :: list

        !! linked list of pointers to polymorphic types.

        private

        logical :: case_sensitive = .true. !! character key lookup is case sensitive
        integer :: count          = 0      !! number of items in the list

        type(node),pointer :: head => null() !! the first item in the list
        type(node),pointer :: tail => null() !! the last item in the list

        contains

        private

        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

        ! procedures that operate on nodes:
        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

        !private routines:
        procedure :: keys_equal     !! for testing key string equality

        final :: list_finalizer

    end type list