aoc_cache_module Module

For caching function evaluations.

This is based on the cache module from NumDiff. It has been modified to cache integers (int64) instead of reals.


Uses

  • module~~aoc_cache_module~~UsesGraph module~aoc_cache_module aoc_cache_module module~aoc_utilities aoc_utilities module~aoc_cache_module->module~aoc_utilities iso_fortran_env iso_fortran_env module~aoc_utilities->iso_fortran_env

Used by

  • module~~aoc_cache_module~~UsedByGraph module~aoc_cache_module aoc_cache_module program~problem_12b problem_12b program~problem_12b->module~aoc_cache_module program~problem_17~2 problem_17 program~problem_17~2->module~aoc_cache_module program~problem_22 problem_22 program~problem_22->module~aoc_cache_module

Derived Types

type, private ::  fx

an [x,f(x)] cached pair. x is a vector and f is a vector.

Components

Type Visibility Attributes Name Initial
integer(kind=ip), private, dimension(:), allocatable :: x

vector of input values

integer(kind=ip), private, dimension(:), allocatable :: f

output functions

type, public ::  function_cache

a vector function cache.

Components

Type Visibility Attributes Name Initial
type(fx), private, dimension(:), allocatable :: c

the cache of f(x)

integer, private :: chunk_size = 100

for resizing vectors in the unique function

Type-Bound Procedures

procedure, public :: initialize => initialize_cache
procedure, public :: get => get_from_cache
procedure, public :: put => put_in_cache
procedure, public :: destroy => destroy_cache

Functions

private pure function vector_djb_hash(r) result(hash)

DJB hash algorithm for a integer(ip) vector.

Read more…

Arguments

Type IntentOptional Attributes Name
integer(kind=ip), intent(in), dimension(:) :: r

the vector

Return Value integer(kind=ip)

the hash value


Subroutines

private subroutine initialize_cache(me, isize, chunk_size)

Initialize the cache. Must be called first before use.

Arguments

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

the size of the hash table

integer, intent(in), optional :: chunk_size

chunk size to speed up reallocation of arrays. A good value is a guess for the actual number of elements of f that will be saved per value of x [default is 100]

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.

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

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

Put a value into the cache.

Arguments

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

index in the hash table

integer(kind=ip), intent(in), dimension(:) :: x

independant variable vector

integer(kind=ip), intent(in), dimension(:) :: f

function

private subroutine destroy_cache(me)

Destroy a cache.

Arguments

Type IntentOptional Attributes Name
class(function_cache), intent(out) :: me