get_from_cache Subroutine

private subroutine get_from_cache(me, x, i, f, found)

Check if the x vector is in the cache, if so return f. Note that only some of the elements may be present, so it will return the ones there are there, and indicate which ones were found.

Type Bound

function_cache

Arguments

Type IntentOptional Attributes Name
class(function_cache), intent(inout) :: me
integer(kind=ip), intent(in), dimension(:) :: x

independant variable vector

integer(kind=ip), intent(out) :: i

index in the hash table

integer(kind=ip), intent(out), dimension(:), allocatable :: f

f(x) from the cache (if it was found)

logical, intent(out) :: found

if x was found in the cache


Calls

proc~~get_from_cache~2~~CallsGraph proc~get_from_cache~2 aoc_cache_module::function_cache%get_from_cache proc~vector_djb_hash~2 aoc_cache_module::vector_djb_hash proc~get_from_cache~2->proc~vector_djb_hash~2

Called by

proc~~get_from_cache~2~~CalledByGraph proc~get_from_cache~2 aoc_cache_module::function_cache%get_from_cache proc~get_all_pieces_above problem_22::get_all_pieces_above proc~get_all_pieces_above->proc~get_from_cache~2 proc~get_all_pieces_above->proc~get_all_pieces_above proc~get_all_pieces_below problem_22::get_all_pieces_below proc~get_all_pieces_below->proc~get_from_cache~2 proc~get_all_pieces_below->proc~get_all_pieces_below proc~go~4 problem_12b::go proc~go~4->proc~get_from_cache~2 proc~ipoint problem_12b::ipoint proc~go~4->proc~ipoint proc~ipound problem_12b::ipound proc~go~4->proc~ipound proc~index_in_queue~2 problem_17::index_in_queue proc~index_in_queue~2->proc~get_from_cache~2 proc~check~2 problem_17::check proc~check~2->proc~index_in_queue~2 proc~ipoint->proc~go~4 proc~ipound->proc~go~4 program~problem_12b problem_12b program~problem_12b->proc~go~4 program~problem_22 problem_22 program~problem_22->proc~get_all_pieces_above program~problem_22->proc~get_all_pieces_below program~problem_17~2 problem_17 program~problem_17~2->proc~check~2

Source Code

    subroutine get_from_cache(me,x,i,f,found)

    implicit none

    class(function_cache),intent(inout)      :: me
    integer(ip),dimension(:),intent(in)      :: x      !! independant variable vector
    integer(ip),intent(out)                  :: i      !! index in the hash table
    integer(ip),dimension(:),allocatable,intent(out) :: f      !! `f(x)` from the cache (if it was found)
    logical,intent(out)                      :: found  !! if `x` was found in the cache

    integer :: j !! counter

    ! initialize:
    found = .false.

    if (allocated(me%c)) then

        ! get index in the hash table:
        i = mod( abs(vector_djb_hash(x)), int(size(me%c),ip) )

        ! check the table:
        if (allocated(me%c(i)%x)) then
            if (size(me%c(i)%x)==size(x)) then
                if (all(me%c(i)%x==x)) then
                    found = .true.
                    f = me%c(i)%f
                end if
            end if
        end if

    else
        error stop 'Error: the cache has not been initialized.'
    end if

    end subroutine get_from_cache