Package org.ddolib.ddo.core.cache
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>
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 ClassesModifier and TypeClassDescriptionstatic classInner class representing a synchronized cache layer for a specific depth. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear(int nbVariables) Clears all layers of the cache up to the given number of variables.voidclearLayer(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.voidinitialize(Problem<T> problem) Initializes the cache based on the number of variables of the problem.stats()Returns statistics about the cache, including number of hits, tests, and total entries.voidupdateThreshold(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, waitMethods inherited from interface org.ddolib.ddo.core.cache.Cache
mustExplore
-
Constructor Details
-
SimpleCache
public SimpleCache()Constructs an empty SimpleCache.
-
-
Method Details
-
initialize
Initializes the cache based on the number of variables of the problem. Creates a layer for each variable (depth).- Specified by:
initializein interfaceCache<T>- Parameters:
problem- the problem for which the cache is initialized
-
getLayer
Returns the layer at a given depth. -
getThreshold
Retrieves the threshold associated with a given state at a specific depth.- Specified by:
getThresholdin interfaceCache<T>- Parameters:
state- the state to querydepth- the depth of the state in the DP model- Returns:
- an
Optionalcontaining the threshold if present, otherwise empty
-
updateThreshold
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:
updateThresholdin interfaceCache<T>- Parameters:
state- the state to updatedepth- the depth of the statethreshold- the new threshold value
-
clearLayer
public void clearLayer(int depth) Clears all thresholds in a specific layer.- Specified by:
clearLayerin interfaceCache<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. -
stats
Returns statistics about the cache, including number of hits, tests, and total entries.- Returns:
- a string summarizing cache statistics
-