hash_map_init Subroutine

private subroutine hash_map_init(this, initial_capacity)

Allocate the map, discarding any entries it already held.

initial_capacity is the number of entries the caller expects to store; the bucket table is sized to the smallest power of two that keeps that many entries below the 0.75 load factor. Values smaller than the built-in minimum, including zero and negative values, are silently raised to that minimum. Calling init is optional: the first insert into an unallocated map initialises it with the default capacity.

Type Bound

hash_map_t

Arguments

Type IntentOptional Attributes Name
class(hash_map_t), intent(inout) :: this
integer(kind=default_int), intent(in), optional :: initial_capacity

Calls

proc~~hash_map_init~~CallsGraph proc~hash_map_init hash_map_t%hash_map_init proc~hash_map_destroy hash_map_t%hash_map_destroy proc~hash_map_init->proc~hash_map_destroy

Called by

proc~~hash_map_init~~CalledByGraph proc~hash_map_init hash_map_t%hash_map_init proc~hash_map_insert hash_map_t%hash_map_insert proc~hash_map_insert->proc~hash_map_init

Variables

Type Visibility Attributes Name Initial
integer(kind=default_int), private :: nb
integer(kind=default_int), private :: wanted

Source Code

   subroutine hash_map_init(this, initial_capacity)
      !! Allocate the map, discarding any entries it already held.
      !!
      !! `initial_capacity` is the number of *entries* the caller expects to
      !! store; the bucket table is sized to the smallest power of two that
      !! keeps that many entries below the 0.75 load factor. Values smaller
      !! than the built-in minimum, including zero and negative values, are
      !! silently raised to that minimum. Calling `init` is optional: the
      !! first `insert` into an unallocated map initialises it with the
      !! default capacity.
      class(hash_map_t), intent(inout) :: this
      integer(default_int), intent(in), optional :: initial_capacity

      integer(default_int) :: wanted, nb

      call this%destroy()

      wanted = MIN_ENTRIES
      if (present(initial_capacity)) then
         if (initial_capacity > wanted) wanted = initial_capacity
      end if

      nb = MIN_BUCKETS
      do while (wanted*LOAD_DEN > nb*LOAD_NUM)
         nb = nb*2
      end do

      allocate (this%buckets(nb))
      this%buckets = BUCKET_EMPTY
      this%n_buckets = nb

      allocate (this%key_start(wanted))
      allocate (this%key_size(wanted))
      allocate (this%entry_value(wanted))
      this%entry_cap = wanted

      allocate (this%key_pool(MIN_POOL))
      this%pool_cap = MIN_POOL
      this%pool_used = 0
      this%n_entries = 0
   end subroutine hash_map_init