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 | Intent | Optional | 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. |
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