hash_map_remove Subroutine

private subroutine hash_map_remove(this, key, err)

Remove key from the map.

The entry disappears from the iteration order and every remaining entry keeps its relative position: entries inserted before it are untouched, entries inserted after it move down one index. The key itself is forgotten completely, so inserting it again afterwards is a fresh insertion that lands at the end of the iteration order.

Removing a key that is not in the map leaves the map unchanged and reports ERROR_VALIDATION in err. When err is absent the call is simply a no-op.

Type Bound

hash_map_t

Arguments

Type IntentOptional Attributes Name
class(hash_map_t), intent(inout) :: this
character(len=*), intent(in) :: key
type(error_t), intent(out), optional :: err

Calls

proc~~hash_map_remove~~CallsGraph proc~hash_map_remove hash_map_t%hash_map_remove proc~error_set error_t%error_set proc~hash_map_remove->proc~error_set proc~find_entry find_entry proc~hash_map_remove->proc~find_entry proc~rebuild_buckets rebuild_buckets proc~hash_map_remove->proc~rebuild_buckets proc~home_bucket home_bucket proc~find_entry->proc~home_bucket proc~key_matches key_matches proc~find_entry->proc~key_matches proc~entry_key entry_key proc~rebuild_buckets->proc~entry_key proc~probe_free probe_free proc~rebuild_buckets->proc~probe_free interface~fnv_1a_hash fnv_1a_hash proc~home_bucket->interface~fnv_1a_hash proc~probe_free->proc~home_bucket

Variables

Type Visibility Attributes Name Initial
integer(kind=default_int), private :: entry
integer(kind=default_int), private :: i
integer(kind=default_int), private :: klen
integer(kind=default_int), private :: kstart

Source Code

   subroutine hash_map_remove(this, key, err)
      !! Remove `key` from the map.
      !!
      !! The entry disappears from the iteration order and every remaining
      !! entry keeps its relative position: entries inserted before it are
      !! untouched, entries inserted after it move down one index. The key
      !! itself is forgotten completely, so inserting it again afterwards is a
      !! fresh insertion that lands at the *end* of the iteration order.
      !!
      !! Removing a key that is not in the map leaves the map unchanged and
      !! reports `ERROR_VALIDATION` in `err`. When `err` is absent the call is
      !! simply a no-op.
      class(hash_map_t), intent(inout) :: this
      character(len=*), intent(in) :: key
      type(error_t), intent(out), optional :: err

      integer(default_int) :: entry, klen, kstart, i

      entry = find_entry(this, key)
      if (entry <= 0) then
         if (present(err)) then
            call err%set(ERROR_VALIDATION, "pic_hash_map: key not found: '"//key//"'")
         end if
         return
      end if

      kstart = this%key_start(entry)
      klen = this%key_size(entry)

      do i = kstart + klen, this%pool_used
         this%key_pool(i - klen) = this%key_pool(i)
      end do
      this%pool_used = this%pool_used - klen

      do i = entry, this%n_entries - 1
         this%key_start(i) = this%key_start(i + 1) - klen
         this%key_size(i) = this%key_size(i + 1)
         this%entry_value(i) = this%entry_value(i + 1)
      end do
      this%n_entries = this%n_entries - 1

      call rebuild_buckets(this)
   end subroutine hash_map_remove