nodestack: action_set_t: replace STL 'set' with 'list'
[cdsspec-compiler.git] / nodestack.h
1 /** @file nodestack.h
2  *  @brief Stack of operations for use in backtracking.
3 */
4
5 #ifndef __NODESTACK_H__
6 #define __NODESTACK_H__
7
8 #include <list>
9 #include <vector>
10 #include <cstddef>
11 #include "threads.h"
12 #include "mymemory.h"
13
14 class ModelAction;
15
16 typedef std::list< ModelAction *, MyAlloc< ModelAction * > > action_set_t;
17
18 /**
19  * @brief A single node in a NodeStack
20  *
21  * Represents a single node in the NodeStack. Each Node is associated with up
22  * to one action and up to one parent node. A node holds information
23  * regarding the last action performed (the "associated action"), the thread
24  * choices that have been explored (explored_children) and should be explored
25  * (backtrack), and the actions that the last action may read from.
26  */
27 class Node {
28 public:
29         Node(ModelAction *act = NULL, Node *par = NULL, int nthreads = 1);
30         ~Node();
31         /* return true = thread choice has already been explored */
32         bool has_been_explored(thread_id_t tid);
33         /* return true = backtrack set is empty */
34         bool backtrack_empty();
35         void explore_child(ModelAction *act);
36         /* return false = thread was already in backtrack */
37         bool set_backtrack(thread_id_t id);
38         thread_id_t get_next_backtrack();
39         bool is_enabled(Thread *t);
40         ModelAction * get_action() { return action; }
41
42         /** @return the parent Node to this Node; that is, the action that
43          * occurred previously in the stack. */
44         Node * get_parent() const { return parent; }
45
46         void add_read_from(ModelAction *act);
47
48         void print();
49
50         MEMALLOC
51 private:
52         void explore(thread_id_t tid);
53
54         ModelAction *action;
55         Node *parent;
56         int num_threads;
57         std::vector< bool, MyAlloc<bool> > explored_children;
58         std::vector< bool, MyAlloc<bool> > backtrack;
59         int numBacktracks;
60
61         /** The set of ModelActions that this the action at this Node may read
62          *  from. Only meaningful if this Node represents a 'read' action. */
63         action_set_t may_read_from;
64 };
65
66 typedef std::list< Node *, MyAlloc< Node * > > node_list_t;
67
68 /**
69  * @brief A stack of nodes
70  *
71  * Holds a Node linked-list that can be used for holding backtracking,
72  * may-read-from, and replay information. It is used primarily as a
73  * stack-like structure, in that backtracking points and replay nodes are
74  * only removed from the top (most recent).
75  */
76 class NodeStack {
77 public:
78         NodeStack();
79         ~NodeStack();
80         ModelAction * explore_action(ModelAction *act);
81         Node * get_head();
82         Node * get_next();
83         void reset_execution();
84
85         int get_total_nodes() { return total_nodes; }
86
87         void print();
88
89         MEMALLOC
90 private:
91         node_list_t node_list;
92         node_list_t::iterator iter;
93
94         int total_nodes;
95 };
96
97 #endif /* __NODESTACK_H__ */