Skip to content

Commit ac193ea

Browse files
authored
Refactor: added invalidateCache() for core cache rebuilding (#517)
* Refactor: added invalidateCache() for core cache rebuilding (Issue 499) * Refactor: added invalidateCache() for core cache rebuilding (Issue 499) * Fixes #499: Implemented parameterized invalidateCache() for full cache rebuilding
1 parent a68604f commit ac193ea

3 files changed

Lines changed: 37 additions & 22 deletions

File tree

include/CXXGraph/CXXGraphConfig.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// the configured options and settings for CXXGraph
2-
#define CXXGraph_VERSION_MAJOR 4
3-
#define CXXGraph_VERSION_MINOR 1
4-
#define CXXGraph_VERSION_PATCH 0
1+
// the configured options and settings for CXXGraph
2+
#define CXXGraph_VERSION_MAJOR 4
3+
#define CXXGraph_VERSION_MINOR 1
4+
#define CXXGraph_VERSION_PATCH 0

include/CXXGraph/Graph/Graph_decl.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,20 @@ class Graph {
261261
*/
262262
virtual void removeNode(const std::string &nodeUserId);
263263

264+
/**
265+
* \brief
266+
* Invalidates and rebuilds the graph's core caches.
267+
* This includes the adjacency matrix, degree matrix, and Laplacian matrix.
268+
* Optionally, the transition matrix can also be rebuilt.
269+
*
270+
* \param includeTransitionMatrix
271+
* If true, the transition matrix will be rebuilt as part of the cache update.
272+
*
273+
* \note Not thread-safe.
274+
* \note Call after any structural modification of the graph.
275+
*/
276+
void invalidateCache(bool includeTransitionMatrix);
277+
264278
/**
265279
* \brief
266280
* Finds the given edge defined by v1 and v2 within the graph.
@@ -562,10 +576,9 @@ class Graph {
562576
virtual std::shared_ptr<std::vector<Node<T>>> eulerianPath() const;
563577

564578
/**
565-
* @brief Function runs the dijkstra algorithm with inverted metric for some source node and
566-
* target node in the graph and returns the longest distance of target
567-
* from the source (called critical path).
568-
* Note: No Thread Safe
579+
* @brief Function runs the dijkstra algorithm with inverted metric for some
580+
* source node and target node in the graph and returns the longest distance
581+
* of target from the source (called critical path). Note: No Thread Safe
569582
*
570583
* @param source source vertex
571584
* @param target target vertex
@@ -574,9 +587,8 @@ class Graph {
574587
* case if target is not reachable from source or there is error in the
575588
* computation.
576589
*/
577-
virtual const DijkstraResult criticalpath_deterministic(const Node<T>& source,
578-
const Node<T>& target) const;
579-
590+
virtual const DijkstraResult criticalpath_deterministic(
591+
const Node<T> &source, const Node<T> &target) const;
580592

581593
/**
582594
* @brief Function runs the dijkstra algorithm for some source node and

include/CXXGraph/Graph/Graph_impl.hpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ using std::make_unique;
3636
template <typename T>
3737
Graph<T>::Graph() {
3838
/* Caching the adjacency matrix */
39-
cacheAdjMatrix();
40-
cacheDegreeMatrix();
41-
cacheLaplacianMatrix();
42-
cacheTransitionMatrix();
39+
invalidateCache(true);
4340
}
4441

4542
template <typename T>
@@ -48,10 +45,7 @@ Graph<T>::Graph(const T_EdgeSet<T> &edgeSet) {
4845
this->edgeSet.insert(edgeIt);
4946
}
5047
/* Caching the adjacency matrix */
51-
cacheAdjMatrix();
52-
cacheDegreeMatrix();
53-
cacheLaplacianMatrix();
54-
cacheTransitionMatrix();
48+
invalidateCache(true);
5549
}
5650

5751
template <typename T>
@@ -72,9 +66,7 @@ void Graph<T>::setEdgeSet(const T_EdgeSet<T> &edgeSet) {
7266
this->edgeSet.insert(edgeIt);
7367
}
7468
/* Caching the adjacency matrix */
75-
cacheAdjMatrix();
76-
cacheDegreeMatrix();
77-
cacheLaplacianMatrix();
69+
invalidateCache(false);
7870
}
7971

8072
template <typename T>
@@ -610,6 +602,17 @@ void Graph<T>::cacheLaplacianMatrix() {
610602
this->cachedLaplacianMatrix = laplacianMatrix;
611603
}
612604

605+
template <typename T>
606+
void Graph<T>::invalidateCache(bool includeTransitionMatrix) {
607+
cacheAdjMatrix();
608+
cacheDegreeMatrix();
609+
cacheLaplacianMatrix();
610+
611+
if (includeTransitionMatrix) {
612+
cacheTransitionMatrix();
613+
}
614+
}
615+
613616
template <typename T>
614617
shared<TransitionMatrix<T>> Graph<T>::getTransitionMatrix() const {
615618
const auto adjacencyMatrix = this->cachedAdjMatrix;

0 commit comments

Comments
 (0)