GEDLIB  1.0
node_map.ipp
Go to the documentation of this file.
1 /***************************************************************************
2 * *
3 * Copyright (C) 2018 by David B. Blumenthal *
4 * *
5 * This file is part of GEDLIB. *
6 * *
7 * GEDLIB is free software: you can redistribute it and/or modify it *
8 * under the terms of the GNU Lesser General Public License as published *
9 * by the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * GEDLIB is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with GEDLIB. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21 
27 #ifndef SRC_ENV_NODE_MAP_IPP_
28 #define SRC_ENV_NODE_MAP_IPP_
29 
30 namespace ged {
31 
33 NodeMap(std::size_t num_nodes_g, std::size_t num_nodes_h) :
34 forward_map_(num_nodes_g, GEDGraph::undefined_node()),
35 backward_map_(num_nodes_h, GEDGraph::undefined_node()),
36 induced_cost_{std::numeric_limits<double>::infinity()} {}
37 
39 NodeMap(const NodeMap & node_map) :
40 forward_map_(node_map.forward_map_),
41 backward_map_(node_map.backward_map_),
42 induced_cost_(node_map.induced_cost_) {}
43 
44 void
46 operator=(const NodeMap & node_map) {
47  forward_map_ = node_map.forward_map_;
48  backward_map_ = node_map.backward_map_;
49  induced_cost_ = node_map.induced_cost_;
50 }
51 
52 void
54 clear() {
55  for (auto & node_id : forward_map_) {
56  node_id = GEDGraph::undefined_node();
57  }
58  for (auto & node_id : backward_map_) {
59  node_id = GEDGraph::undefined_node();
60  }
61 }
62 
63 bool
65 empty() const {
66  for (const auto & node_id : forward_map_) {
67  if (node_id != GEDGraph::undefined_node()) {
68  return false;
69  }
70  }
71  for (const auto & node_id : backward_map_) {
72  if (node_id != GEDGraph::undefined_node()) {
73  return false;
74  }
75  }
76  return true;
77 }
78 
79 std::size_t
82  return forward_map_.size();
83 }
84 
85 std::size_t
88  return backward_map_.size();
89 }
90 
91 bool
93 complete(const GEDGraph & g, const GEDGraph & h) const {
94  for (const auto & node_id : forward_map_) {
95  if (node_id == GEDGraph::undefined_node()) {
96  return false;
97  }
98  }
99  for (const auto & node_id : backward_map_) {
100  if (node_id == GEDGraph::undefined_node()) {
101  return false;
102  }
103  }
104  return true;
105 }
106 
107 
109 NodeMap::
110 image(GEDGraph::NodeID node) const {
111  if (node < forward_map_.size()) {
112  return forward_map_.at(node);
113  }
114  else {
115  throw Error("The node with ID " + std::to_string(node) + " is not contained in the source nodes of the node map.");
116  }
117  return GEDGraph::undefined_node();
118 }
119 
120 void
121 NodeMap::
123  if (node < forward_map_.size()) {
124  GEDGraph::NodeID image{forward_map_.at(node)};
125  forward_map_[node] = GEDGraph::undefined_node();
126  if (image < backward_map_.size()) {
127  backward_map_[image] = GEDGraph::undefined_node();
128  }
129  }
130  else {
131  throw Error("The node with ID " + std::to_string(node) + " is not contained in the source nodes of the node map.");
132  }
133 }
134 
136 NodeMap::
138  if (node < backward_map_.size()) {
139  return backward_map_.at(node);
140  }
141  else {
142  throw Error("The node with ID " + std::to_string(node) + " is not contained in the target nodes of the node map.");
143  }
144  return GEDGraph::undefined_node();
145 }
146 
147 void
148 NodeMap::
150  if (node < backward_map_.size()) {
151  GEDGraph::NodeID pre_image{backward_map_.at(node)};
152  backward_map_[node] = GEDGraph::undefined_node();
153  if (pre_image < forward_map_.size()) {
154  forward_map_[pre_image] = GEDGraph::undefined_node();
155  }
156  }
157  else {
158  throw Error("The node with ID " + std::to_string(node) + " is not contained in the target nodes of the node map.");
159  }
160 }
161 
162 void
163 NodeMap::
164 as_relation(std::vector<Assignment> & relation) const {
165  relation.clear();
166  GEDGraph::NodeID i, k;
167  for (i = 0; i < forward_map_.size(); i++) {
168  k = forward_map_.at(i);
169  if (k != GEDGraph::undefined_node()) {
170  relation.emplace_back(i, k);
171  }
172  }
173  for (k = 0; k < backward_map_.size(); k++) {
174  i = backward_map_.at(k);
175  if (i == GEDGraph::dummy_node()) {
176  relation.emplace_back(i, k);
177  }
178  }
179 }
180 
181 void
182 NodeMap::
184  if (i != GEDGraph::dummy_node()) {
185  if (i < forward_map_.size()) {
186  forward_map_[i] = k;
187  }
188  else {
189  throw Error("The node with ID " + std::to_string(i) + " is not contained in the source nodes of the node map.");
190  }
191  }
192  if (k != GEDGraph::dummy_node()) {
193  if (k < backward_map_.size()) {
194  backward_map_[k] = i;
195  }
196  else {
197  throw Error("The node with ID " + std::to_string(k) + " is not contained in the target nodes of the node map.");
198  }
199  }
200 }
201 
202 void
203 NodeMap::
205  induced_cost_= induced_cost;
206 }
207 
208 double
209 NodeMap::
210 induced_cost() const {
211  return induced_cost_;
212 }
213 
214 bool
215 NodeMap::
216 operator<(const NodeMap & rhs) const {
217  return (induced_cost_ < rhs.induced_cost_);
218 }
219 
220 bool
221 NodeMap::
222 operator==(const NodeMap & node_map) const {
223  return ((forward_map_ == node_map.forward_map_) and (backward_map_ == node_map.backward_map_));
224 }
225 
226 std::ostream & operator<<(std::ostream & os, const NodeMap & node_map) {
227  std::vector<NodeMap::Assignment> relation;
228  node_map.as_relation(relation);
229  os << "{ ";
230  for (const auto & assignment : relation) {
231  os << assignment << " ";
232  }
233  os << "}";
234  return os;
235 }
236 
237 std::ostream & operator<<(std::ostream & os, const NodeMap::Assignment & assignment) {
238  os << "(";
239  if (assignment.first == GEDGraph::dummy_node()) {
240  os << "DUMMY,";
241  }
242  else {
243  os << assignment.first << ",";
244  }
245  if (assignment.second == GEDGraph::dummy_node()) {
246  os << "DUMMY";
247  }
248  else {
249  os << assignment.second << "";
250  }
251  os << ")";
252  return os;
253 }
254 
255 }
256 
257 #endif /* SRC_ENV_NODE_MAP_IPP_ */
void erase_image(GEDGraph::NodeID node)
Erases image of a node.
Definition: node_map.ipp:122
void as_relation(std::vector< Assignment > &relation) const
Constructs the representation as relation.
Definition: node_map.ipp:164
A class for node maps.
Definition: node_map.hpp:43
bool empty() const
Checks if the node map is empty.
Definition: node_map.ipp:65
std::size_t num_source_nodes() const
Returns number of source nodes contained in the node map.
Definition: node_map.ipp:81
GEDGraph::NodeID pre_image(GEDGraph::NodeID node) const
Returns pre-image of a node.
Definition: node_map.ipp:137
double induced_cost() const
Returns the induced cost.
Definition: node_map.ipp:210
std::ostream & operator<<(std::ostream &os, const NodeMap &node_map)
Streams a node map.
Definition: node_map.ipp:226
bool complete(const GEDGraph &g, const GEDGraph &h) const
Checks if the node map is complete.
Definition: node_map.ipp:93
Runtime error class.
Definition: error.hpp:37
static NodeID dummy_node()
Returns a dummy node.
Definition: ged_graph.ipp:56
static NodeID undefined_node()
Returns an undefined node.
Definition: ged_graph.ipp:62
NodeMap(std::size_t num_nodes_g, std::size_t num_nodes_h)
Default constructor.
Definition: node_map.ipp:33
The normalized input graphs used by GEDLIB. All labels are integers.
Definition: ged_graph.hpp:104
void add_assignment(GEDGraph::NodeID i, GEDGraph::NodeID k)
Add node substitution, insertion, or deletion to the node map.
Definition: node_map.ipp:183
void set_induced_cost(double induced_cost)
Sets the induced cost of the node map.
Definition: node_map.ipp:204
std::size_t num_target_nodes() const
Returns number of target nodes contained in the node map.
Definition: node_map.ipp:87
Global namespace for GEDLIB.
void clear()
Clears the node map.
Definition: node_map.ipp:54
void erase_pre_image(GEDGraph::NodeID node)
Erases pre-image of a node.
Definition: node_map.ipp:149
bool operator==(const NodeMap &node_map) const
Checks if two node maps are the same.
Definition: node_map.ipp:222
GEDGraph::NodeID image(GEDGraph::NodeID node) const
Returns image of a node.
Definition: node_map.ipp:110
bool operator<(const NodeMap &node_map) const
Compares this node map to another node map.
Definition: node_map.ipp:216
void operator=(const NodeMap &node_map)
Assignment operator.
Definition: node_map.ipp:46
std::size_t NodeID
Internally used vertex ID type.
Definition: ged_graph.hpp:108