Class SimpleCache<T>

java.lang.Object
org.ddolib.ddo.core.cache.SimpleCache<T>
Type Parameters:
T - the type of state stored in the cache
All Implemented Interfaces:
Cache<T>

public class SimpleCache<T> extends Object implements Cache<T>
A simple implementation of a Cache for storing threshold values associated with states in a dynamic programming model.

The cache maintains a list of layers, where each layer corresponds to a depth in the problem. Each layer stores thresholds for states at that depth and is thread-safe via a ReadWriteLock.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Inner class representing a synchronized cache layer for a specific depth.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty SimpleCache.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    clear(int nbVariables)
    Clears all layers of the cache up to the given number of variables.
    void
    clearLayer(int depth)
    Clears all thresholds in a specific layer.
    getLayer(int depth)
    Returns the layer at a given depth.
    getThreshold(T state, int depth)
    Retrieves the threshold associated with a given state at a specific depth.
    void
    initialize(Problem<T> problem)
    Initializes the cache based on the number of variables of the problem.
    Returns statistics about the cache, including number of hits, tests, and total entries.
    void
    updateThreshold(T state, int depth, Threshold threshold)
    Updates the threshold for a given state at a specified depth.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.ddolib.ddo.core.cache.Cache

    mustExplore
  • Constructor Details

    • SimpleCache

      public SimpleCache()
      Constructs an empty SimpleCache.
  • Method Details

    • initialize

      public void initialize(Problem<T> problem)
      Initializes the cache based on the number of variables of the problem. Creates a layer for each variable (depth).
      Specified by:
      initialize in interface Cache<T>
      Parameters:
      problem - the problem for which the cache is initialized
    • getLayer

      public SimpleCache.Layer<T> getLayer(int depth)
      Returns the layer at a given depth.
      Specified by:
      getLayer in interface Cache<T>
      Parameters:
      depth - the depth of the layer
      Returns:
      the layer object
    • getThreshold

      public Optional<Threshold> getThreshold(T state, int depth)
      Retrieves the threshold associated with a given state at a specific depth.
      Specified by:
      getThreshold in interface Cache<T>
      Parameters:
      state - the state to query
      depth - the depth of the state in the DP model
      Returns:
      an Optional containing the threshold if present, otherwise empty
    • updateThreshold

      public void updateThreshold(T state, int depth, Threshold threshold)
      Updates the threshold for a given state at a specified depth. If the new threshold is greater than the current threshold (or no current threshold exists), the threshold is updated.
      Specified by:
      updateThreshold in interface Cache<T>
      Parameters:
      state - the state to update
      depth - the depth of the state
      threshold - the new threshold value
    • clearLayer

      public void clearLayer(int depth)
      Clears all thresholds in a specific layer.
      Specified by:
      clearLayer in interface Cache<T>
      Parameters:
      depth - the depth of the layer to clear
    • clear

      public void clear(int nbVariables)
      Clears all layers of the cache up to the given number of variables.
      Specified by:
      clear in interface Cache<T>
      Parameters:
      nbVariables - number of layers to clear
    • stats

      public String stats()
      Returns statistics about the cache, including number of hits, tests, and total entries.
      Returns:
      a string summarizing cache statistics