add_pointer Subroutine

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

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

Note

If an item with the same key is already in the list, it is removed and the new one will replace it.

Type Bound

list

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.


Calls

proc~~add_pointer~~CallsGraph proc~add_pointer list%add_pointer proc~get_node list%get_node proc~add_pointer->proc~get_node proc~remove_by_pointer list%remove_by_pointer proc~add_pointer->proc~remove_by_pointer proc~keys_equal list%keys_equal proc~get_node->proc~keys_equal proc~destroy_node_data node%destroy_node_data proc~remove_by_pointer->proc~destroy_node_data proc~uppercase uppercase proc~keys_equal->proc~uppercase

Called by

proc~~add_pointer~~CalledByGraph proc~add_pointer list%add_pointer proc~add_clone list%add_clone proc~add_clone->proc~add_pointer

Source Code

    subroutine add_pointer(me,key,value,destroy_on_delete)

    implicit none

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

    type(node),pointer :: p

    !only allowing integer, string, or key_class keys:
    select type (key)
    type is (integer)
        !ok
    type is (character(len=*))
        if (len_trim(key)<1) error stop 'Error: key must be nonblank.'
    class is (key_class)
        !ok
    class default
        error stop 'Error: key must be an integer, character string, or key_class.'
    end select

    ! if the node is already there, then remove it
    call me%get_node(key,p)
    if (associated(p)) call me%remove_by_pointer(p)

    if (associated(me%tail)) then
        allocate(me%tail%next)  !insert new item at the end
        p => me%tail%next
        p%previous => me%tail
    else
        allocate(me%head)  !first item in the list
        p => me%head
    end if

    me%tail  => p
    me%count =  me%count + 1

    allocate(p%key, source = key)
    p%value  => value

    if (present(destroy_on_delete)) then
        p%destroy_on_delete = destroy_on_delete
    end if

    end subroutine add_pointer