remove_by_pointer Subroutine

private subroutine remove_by_pointer(me, p)

Remove an item from the list.

Type Bound

list

Arguments

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

the item to remove


Calls

proc~~remove_by_pointer~~CallsGraph proc~remove_by_pointer list%remove_by_pointer proc~destroy_node_data node%destroy_node_data proc~remove_by_pointer->proc~destroy_node_data

Called by

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

Source Code

    subroutine remove_by_pointer(me,p)

    implicit none

    class(list),intent(inout) :: me
    type(node),pointer        :: p   !! the item to remove

    logical :: has_next, has_previous

    if (associated(p)) then

        call p%destroy()  ! destroy the data

        has_next     = associated(p%next)
        has_previous = associated(p%previous)

        if (has_next .and. has_previous) then    !neither first nor last in a list
            p%previous%next => p%next
            p%next%previous => p%previous
        elseif (has_next .and. .not. has_previous) then    !first one in a list
            me%head          => p%next
            me%head%previous => null()
        elseif (has_previous .and. .not. has_next) then    !last one in a list
            me%tail      => p%previous
            me%tail%next => null()
        elseif (.not. has_previous .and. .not. has_next) then  !only one in the list
            me%head => null()
            me%tail => null()
        end if

        deallocate(p)
        nullify(p)

        me%count = me%count - 1

    end if

    end subroutine remove_by_pointer