put_in_cache Subroutine

private subroutine put_in_cache(me, i, x, f, ifs)

Put a value into the cache.

Type Bound

function_cache

Arguments

Type IntentOptional Attributes Name
class(function_cache), intent(inout) :: me
integer, intent(in) :: i

index in the hash table

real(kind=wp), intent(in), dimension(:) :: x

independant variable vector (dimension n)

real(kind=wp), intent(in), dimension(:) :: f

function vector f(x) (dimension m)

integer, intent(in), dimension(:) :: ifs

elements of f to add (should all be >0, <=m)


Calls

proc~~put_in_cache~~CallsGraph proc~put_in_cache numdiff_cache_module::function_cache%put_in_cache interface~unique numdiff_utilities_module::unique proc~put_in_cache->interface~unique proc~unique_int numdiff_utilities_module::unique_int interface~unique->proc~unique_int proc~unique_real numdiff_utilities_module::unique_real interface~unique->proc~unique_real interface~expand_vector numdiff_utilities_module::expand_vector proc~unique_int->interface~expand_vector interface~sort_ascending numdiff_utilities_module::sort_ascending proc~unique_int->interface~sort_ascending proc~unique_real->interface~expand_vector proc~unique_real->interface~sort_ascending proc~expand_vector_int numdiff_utilities_module::expand_vector_int interface~expand_vector->proc~expand_vector_int proc~expand_vector_real numdiff_utilities_module::expand_vector_real interface~expand_vector->proc~expand_vector_real proc~sort_ascending_int numdiff_utilities_module::sort_ascending_int interface~sort_ascending->proc~sort_ascending_int proc~sort_ascending_real numdiff_utilities_module::sort_ascending_real interface~sort_ascending->proc~sort_ascending_real interface~swap numdiff_utilities_module::swap proc~sort_ascending_int->interface~swap proc~sort_ascending_real->interface~swap proc~swap_int numdiff_utilities_module::swap_int interface~swap->proc~swap_int proc~swap_real numdiff_utilities_module::swap_real interface~swap->proc~swap_real

Called by

proc~~put_in_cache~~CalledByGraph proc~put_in_cache numdiff_cache_module::function_cache%put_in_cache proc~compute_function_with_cache numerical_differentiation_module::compute_function_with_cache proc~compute_function_with_cache->proc~put_in_cache

Source Code

    subroutine put_in_cache(me,i,x,f,ifs)

    implicit none

    class(function_cache),intent(inout) :: me
    integer,intent(in)                  :: i    !! index in the hash table
    real(wp),dimension(:),intent(in)    :: x    !! independant variable vector (dimension `n`)
    real(wp),dimension(:),intent(in)    :: f    !! function vector `f(x)` (dimension `m`)
    integer,dimension(:),intent(in)     :: ifs  !! elements of `f` to add (should all be `>0, <=m`)

    real(wp),parameter :: null = huge(1.0_wp) !! an unusual value to initialize arrays

    if (allocated(me%c)) then
        if (i<=size(me%c)) then

            if (allocated(me%c(i)%x)) then
                ! we need to check if there is an x already there,
                ! which may already have some function indices present.
                ! if same x, then add the new indices to them.
                ! if a different x, then replace indices.
                if (all(me%c(i)%x==x)) then
                    ! this x is already present in this location.
                    ! so merge the new f,ifs into what is already there.
                    if (allocated(me%c(i)%f)) then
                        me%c(i)%ifs = unique([me%c(i)%ifs,ifs],&
                                                chunk_size=me%chunk_size)
                    else
                        allocate(me%c(i)%f(me%m))
                        me%c(i)%f   = null ! initialize to an unusual value
                        me%c(i)%ifs = ifs
                    end if
                    me%c(i)%f(ifs) = f(ifs)
                else
                    ! replace existing x and f.
                    me%c(i)%x   = x
                    me%c(i)%ifs = ifs
                    if (allocated(me%c(i)%f)) deallocate(me%c(i)%f)
                    allocate(me%c(i)%f(me%m))
                    me%c(i)%f      = null ! initialize to an unusual value
                    me%c(i)%f(ifs) = f(ifs)
                end if
            else
                ! new entry in the cache:
                me%c(i)%x = x
                allocate(me%c(i)%f(me%m))
                me%c(i)%f      = null ! initialize to an unusual value
                me%c(i)%ifs    = ifs
                me%c(i)%f(ifs) = f(ifs)
            end if

        else
            error stop 'Error: invalid index in hash table.'
        end if
    else
        error stop 'Error: the cache has not been initialized.'
    end if

    end subroutine put_in_cache