Resize the hash table (internal method)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(json_hash_table), | intent(inout) | :: | me | |||
| integer(kind=IK), | intent(in) | :: | new_capacity |
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