Remove node from dictionary, given pointer to it.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(dictionary), | intent(inout) | :: | self | The dictionary. |
||
type(dictionary_node), | intent(inout), | pointer | :: | p | Pointer to the node to remove. |
subroutine remove_by_pointer(self, p)
!---------------------------------------------------------------------------------------------------------------------------------
!< Remove node from dictionary, given pointer to it.
!---------------------------------------------------------------------------------------------------------------------------------
class(dictionary), intent(inout) :: self !< The dictionary.
type(dictionary_node), pointer, intent(inout) :: p !< Pointer to the node to remove.
logical :: has_next !< Check if dictionary node has a next item.
logical :: has_previous !< Check if dictionary node has a previous item.
!---------------------------------------------------------------------------------------------------------------------------------
!---------------------------------------------------------------------------------------------------------------------------------
if (associated(p)) then
call self%remove_id(id=p%key%id())
call p%destroy ! destroy the node contents
has_next = associated(p%next)
has_previous = associated(p%previous)
if (has_next.and.has_previous) then ! neither first nor last in dictionary
p%previous%next => p%next
p%next%previous => p%previous
elseif (has_next.and.(.not.has_previous)) then ! first one in dictionary
self%head => p%next
self%head%previous => null()
elseif (has_previous.and.(.not.has_next)) then ! last one in dictionary
self%tail => p%previous
self%tail%next => null()
elseif ((.not.has_previous).and.(.not.has_next)) then ! only one in the dictionary
self%head => null()
self%tail => null()
endif
deallocate(p)
p => null()
self%nodes_number = self%nodes_number - 1
endif
!---------------------------------------------------------------------------------------------------------------------------------
endsubroutine remove_by_pointer