hash_table_resize Subroutine

private subroutine hash_table_resize(me, new_capacity)

Resize the hash table (internal method)

Type Bound

json_hash_table

Arguments

Type IntentOptional Attributes Name
class(json_hash_table), intent(inout) :: me
integer(kind=IK), intent(in) :: new_capacity

Calls

proc~~hash_table_resize~~CallsGraph proc~hash_table_resize json_hash_table%hash_table_resize proc~hash_table_insert json_hash_table%hash_table_insert proc~hash_table_resize->proc~hash_table_insert proc~hash_table_insert->proc~hash_table_resize proc~hash_table_hash json_hash_table%hash_table_hash proc~hash_table_insert->proc~hash_table_hash proc~keys_equal json_hash_table%keys_equal proc~hash_table_insert->proc~keys_equal proc~preprocess_key json_hash_table%preprocess_key proc~hash_table_insert->proc~preprocess_key proc~lowercase_string lowercase_string proc~preprocess_key->proc~lowercase_string

Called by

proc~~hash_table_resize~~CalledByGraph proc~hash_table_resize json_hash_table%hash_table_resize proc~hash_table_insert json_hash_table%hash_table_insert proc~hash_table_resize->proc~hash_table_insert proc~hash_table_insert->proc~hash_table_resize proc~hash_table_create_from_value json_hash_table%hash_table_create_from_value proc~hash_table_create_from_value->proc~hash_table_insert proc~initialize_hash json_file%initialize_hash proc~initialize_hash->proc~hash_table_create_from_value

Source Code

    subroutine hash_table_resize(me, new_capacity)

    class(json_hash_table), intent(inout) :: me
    integer(IK), intent(in) :: new_capacity

    type(hash_node_t), dimension(:), allocatable :: old_buckets !! temporary array to hold old buckets during resizing
    type(hash_node_t), pointer :: current, temp !! pointers for traversing linked lists during rehashing
    integer(IK) :: i !! counter
    integer(IK) :: old_capacity !! previous capacity of the hash table

    ! Save old buckets
    old_capacity = me%capacity
    call move_alloc(me%buckets, old_buckets)

    ! Allocate new buckets
    me%capacity = new_capacity
    allocate(me%buckets(0:me%capacity-1))
    do i = 0, me%capacity - 1
        me%buckets(i)%next => null()
    end do
    me%size = 0

    ! Rehash all elements
    do i = 0, old_capacity - 1
        current => old_buckets(i)%next
        do while (associated(current))
            call me%insert(current%key, current%value)
            temp => current
            current => current%next
            if (allocated(temp%key)) deallocate(temp%key)
            deallocate(temp)
        end do
    end do

    deallocate(old_buckets)

    end subroutine hash_table_resize