Add a node pointer to the dictionary.
If a node with the same key is already in the dictionary, it is removed and the new one will replace it.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(dictionary), | intent(inout) | :: | self | The dictionary. |
||
class(*), | intent(in) | :: | key | The key. |
||
class(*), | intent(in), | pointer | :: | content | The content. |
|
integer(kind=I4P), | intent(in), | optional | :: | buckets_number | Buckets number. |
subroutine add_pointer(self, key, content, buckets_number)
!---------------------------------------------------------------------------------------------------------------------------------
!< Add a node pointer to the dictionary.
!<
!< @note If a node with the same key is already in the dictionary, it is removed and the new one will replace it.
!---------------------------------------------------------------------------------------------------------------------------------
class(dictionary), intent(inout) :: self !< The dictionary.
class(*), intent(in) :: key !< The key.
class(*), pointer, intent(in) :: content !< The content.
integer(I4P), intent(in), optional :: buckets_number !< Buckets number.
type(dictionary_node), pointer :: p !< Pointer to scan the dictionary.
!---------------------------------------------------------------------------------------------------------------------------------
!---------------------------------------------------------------------------------------------------------------------------------
if (.not.is_key_allowed(key)) error stop 'error: key type not supported'
! if the node is already there, then remove it
p => self%node(key=key)
if (associated(p)) call self%remove_by_pointer(p)
! update next/previous pointers
if (associated(self%tail)) then ! insert new node at the end
allocate(self%tail%next)
p => self%tail%next
p%previous => self%tail
else
allocate(self%head) ! insert new node as first node
p => self%head
end if
self%tail => p
call p%set_pointer(key=key, content=content, buckets_number=buckets_number) ! fill the new node with provided contents
call self%add_id(id=p%key%id())
self%nodes_number = self%nodes_number + 1
!---------------------------------------------------------------------------------------------------------------------------------
endsubroutine add_pointer