Assignment Search Framework
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
node_table.h
Go to the documentation of this file.
1 /*
2  * node_table.h
3  *
4  * LICENSE HERE
5  *
6  * Created on: 2016-09-12
7  * Author: Rick Valenzano
8  */
9 
10 #ifndef NODE_TABLE_H_
11 #define NODE_TABLE_H_
12 
13 #include <cassert>
14 #include <vector>
15 #include <unordered_map>
16 #include <utility>
17 #include <stdio.h>
18 #include <iostream>
19 #include "../../generic_defs/state_hash_function.h"
20 
21 typedef unsigned NodeID;
22 
30 {
31  std::size_t operator()(StateHash hash_value) const;
32 };
33 
44 template<class node_t>
45 class NodeTable
46 {
47  typedef std::unordered_map<StateHash, NodeID, NodeKeyHash> NodeMap;
48 
49 public:
53  NodeTable();
54 
58  virtual ~NodeTable();
59 
69  bool isNodeStored(StateHash hash_value, NodeID &id) const;
70 
80  NodeID addNewSearchNode(node_t new_node, StateHash hash_value);
81 
85  void clear();
86 
92  std::size_t size() const;
93 
102  node_t &getNode(NodeID node_id);
103 
104  const node_t &getNode(NodeID node_id) const;
105 
114  node_t &operator[](NodeID node_id);
115 
116  const node_t &operator[](NodeID node_id) const;
117 
118 protected:
119  std::vector<node_t> nodes;
120 
121  NodeMap node_map;
122 };
123 
124 template<class node_t>
126 {
127 }
128 
129 template<class node_t>
131 {
132 }
133 
134 template<class node_t>
136 {
137  assert(nodes.size() == node_map.size());
138 
139  typename NodeMap::const_iterator node_check = node_map.find(hash_value);
140 
141  if(node_check == node_map.end())
142  return false;
143 
144  id = node_check->second;
145  assert(id < nodes.size());
146  return true;
147 }
148 
149 template<class node_t>
151 {
152  nodes.push_back(new_node);
153  node_map[hash_value] = nodes.size() - 1;
154 
155  return nodes.size() - 1;
156 }
157 
158 template<class node_t>
160 {
161  nodes.clear();
162  node_map.clear();
163 }
164 
165 template<class node_t>
167 {
168  if(node_id >= nodes.size()) {
169  //std::cout << "Bad node id " << node_id << std::endl;
170  assert(false);
171  }
172  return nodes[node_id];
173 }
174 
175 template<class node_t>
176 const node_t& NodeTable<node_t>::getNode(NodeID node_id) const
177 {
178  if(node_id >= nodes.size()) {
179  //std::cout << "Bad node id " << node_id << std::endl;
180  assert(false);
181  }
182  return nodes[node_id];
183 }
184 
185 template<class node_t>
186 inline std::size_t NodeTable<node_t>::size() const
187 {
188  return nodes.size();
189 }
190 
191 template<class node_t>
193 {
194  return getNode(node_id);
195 }
196 
197 template<class node_t>
198 const node_t& NodeTable<node_t>::operator [](NodeID node_id) const
199 {
200  return getNode(node_id);
201 }
202 
203 #endif /* NODE_TABLE_H_ */
std::vector< node_t > nodes
A list of the nodes being stored.
Definition: node_table.h:119
uint64_t StateHash
The hash value of a state.
Definition: state_hash_function.h:14
std::size_t operator()(StateHash hash_value) const
Definition: node_table.cpp:12
void clear()
Definition: node_table.h:159
bool isNodeStored(StateHash hash_value, NodeID &id) const
Definition: node_table.h:135
NodeMap node_map
The map used to determine if a hash value is already associated with a node.
Definition: node_table.h:121
Definition: node_table.h:29
NodeID addNewSearchNode(node_t new_node, StateHash hash_value)
Definition: node_table.h:150
virtual ~NodeTable()
Definition: node_table.h:130
NodeTable()
Definition: node_table.h:125
unsigned NodeID
The ID of a node is the location of the node in the node table.
Definition: node_table.h:21
Definition: node_table.h:45
node_t & getNode(NodeID node_id)
Definition: node_table.h:166
std::size_t size() const
Definition: node_table.h:186
node_t & operator[](NodeID node_id)
Definition: node_table.h:192