trying to get fork based snapshotting to work
[model-checker.git] / cyclegraph.cc
1 #include "cyclegraph.h"
2 #include "action.h"
3
4 /** Initializes a CycleGraph object. */
5 CycleGraph::CycleGraph() {
6         hasCycles=false;
7 }
8
9 CycleGraph::~CycleGraph() {
10 }
11
12 /** Returns the CycleNode for a given ModelAction. */
13 CycleNode * CycleGraph::getNode(const ModelAction * action) {
14         CycleNode *node=actionToNode.get(action);
15         if (node==NULL) {
16                 node=new CycleNode(action);
17                 actionToNode.put(action, node);
18         }
19         return node;
20 }
21
22 /** Adds an edge between two ModelActions. */
23 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to) {
24         CycleNode *fromnode=getNode(from);
25         CycleNode *tonode=getNode(to);
26         if (!hasCycles) {
27                 // Check for Cycles
28                 hasCycles=checkReachable(tonode, fromnode);
29         }
30         fromnode->addEdge(tonode);
31 }
32
33 /** Checks whether the first CycleNode can reach the second one. */
34 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
35         std::vector<CycleNode *> queue;
36         HashTable<CycleNode *, CycleNode *, uintptr_t, 4> discovered;
37
38         queue.push_back(from);
39         discovered.put(from, from);
40         while(!queue.empty()) {
41                 CycleNode * node=queue.back();
42                 queue.pop_back();
43                 if (node==to)
44                         return true;
45
46                 for(unsigned int i=0;i<node->getEdges()->size();i++) {
47                         CycleNode *next=(*node->getEdges())[i];
48                         if (!discovered.contains(next)) {
49                                 discovered.put(next,next);
50                                 queue.push_back(next);
51                         }
52                 }
53         }
54         return false;
55 }
56
57 /** Returns whether a CycleGraph contains cycles. */
58 bool CycleGraph::checkForCycles() {
59         return hasCycles;
60 }
61
62 /** Constructor for a CycleNode. */
63 CycleNode::CycleNode(const ModelAction *modelaction) {
64         action=modelaction;
65 }
66
67 /** Returns a vector of the edges from a CycleNode. */
68 std::vector<CycleNode *> * CycleNode::getEdges() {
69         return &edges;
70 }
71
72 /** Adds an edge to a CycleNode. */
73 void CycleNode::addEdge(CycleNode * node) {
74         edges.push_back(node);
75 }