set_hashed_str_i4 Subroutine

private recursive subroutine set_hashed_str_i4(dict, key, value, hash)

Common code used internally by dict_set() and change_capacity().

Arguments

Type IntentOptional Attributes Name
type(dict_str_i4_type), intent(inout) :: dict
character(len=default_string_length), intent(in) :: key
integer(kind=int_4), intent(in) :: value
integer(kind=int_8), intent(in) :: hash

Source Code

      RECURSIVE SUBROUTINE set_hashed_str_i4 (dict, key, value, hash)
      !! Common code used internally by dict_set() and change_capacity().
         TYPE(dict_str_i4_type), intent(inout)  :: dict
         CHARACTER(LEN=default_string_length), intent(in)                          :: key
         INTEGER(kind=int_4), intent(in)                          :: value
         INTEGER(KIND=int_8), intent(in)                       :: hash
         TYPE(private_item_type_str_i4), POINTER  :: item, new_item
         INTEGER(KIND=int_8)                                   :: idx

         idx = MOD(hash, INT(size(dict%buckets), KIND=int_8)) + 1

         ! if already in dict just update its value
         item => dict%buckets(idx)%p
         do while (ASSOCIATED(item))
            IF (item%hash == hash) THEN
               IF (item%key == key) THEN
                  item%value =value
                  return
               END IF
            END IF
            item => item%next
         end do

         ! check load-factor
         IF (4*dict%size > 3*size(dict%buckets)) THEN ! load-factor > 75%
            call change_capacity_str_i4 (dict, 2*size(dict%buckets)) !double capacity
            idx = MOD(hash, INT(size(dict%buckets), KIND=int_8)) + 1
         END IF

         ! create a new item
         allocate (new_item)
         new_item%hash = hash
         new_item%key =key
         new_item%value =value
         new_item%next => dict%buckets(idx)%p
         dict%buckets(idx)%p => new_item
         dict%size = dict%size + 1

      END SUBROUTINE set_hashed_str_i4