GEDLIB  1.0
misc.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_UTIL_MISC_IPP_
28 #define SRC_UTIL_MISC_IPP_
29 
30 namespace ged {
31 
32 namespace util {
33 
34 void
35 init_adj_matrix(const GEDGraph & graph, DMatrix & adj_matrix) {
36  for (std::size_t row{0}; row < adj_matrix.num_rows(); row++) {
37  for (std::size_t col{0}; col < adj_matrix.num_cols(); col++) {
38  if (graph.is_edge(row, col)) {
39  adj_matrix(row, col) = 1.0;
40  }
41  else {
42  adj_matrix(row, col) = 0.0;
43  }
44  }
45  }
46 }
47 
48 void
49 parse_config_file(const std::string & filename, std::map<std::string, std::string> & options) {
50  std::ifstream config_file(filename);
51  std::string line;
52  std::size_t line_nr{1};
53  while(std::getline(config_file, line)) {
54  if (line.at(0) == '#') {
55  continue;
56  }
57  std::string error_msg("Line " + std::to_string(line_nr) + " has invalid format.\nExpected format: \"<key>=<value>\"\nLine " + std::to_string(line_nr) + ": " + line);
58  std::istringstream line_stream(line);
59  std::string key;
60  if (not std::getline(line_stream, key, '=')) {
61  throw Error(error_msg);
62  }
63  std::string value;
64  if(not std::getline(line_stream, value)) {
65  throw Error(error_msg);
66  }
67  else {
68  options[key] = value;
69  }
70  line_nr++;
71  }
72  config_file.close();
73 }
74 
75 void
76 save_as_config_file(const std::string & filename, const std::map<std::string, std::string> & options) {
77  std::ofstream config_file(filename);
78  for (auto key_value = options.begin(); key_value != options.end(); key_value++) {
79  config_file << key_value->first << "=" << key_value->second << "\n";
80  }
81  config_file.close();
82 }
83 
84 template<class Solver>
85 void
86 construct_node_map_from_solver(const Solver & solver, NodeMap & node_map, std::size_t solution_id) {
87  node_map.clear();
88  std::size_t num_nodes_g{node_map.num_source_nodes()};
89  std::size_t num_nodes_h{node_map.num_target_nodes()};
90 
91  // add deletions and substitutions
92  for (std::size_t row{0}; row < num_nodes_g; row++) {
93  std::size_t col{solver.get_assigned_col(row, solution_id)};
94  if (col >= num_nodes_h) {
95  node_map.add_assignment(row, GEDGraph::dummy_node());
96  }
97  else {
98  node_map.add_assignment(row, col);
99  }
100  }
101 
102  // insertions
103  for (std::size_t col{0}; col < num_nodes_h; col++) {
104  if (solver.get_assigned_row(col, solution_id) >= num_nodes_g) {
105  node_map.add_assignment(GEDGraph::dummy_node(), col);
106  }
107  }
108 }
109 
110 void
111 counting_sort(std::vector<LabelID>::iterator first, std::vector<LabelID>::iterator last) {
112  // Find maximum label value and range.
113  LabelID max_label_val{0};
114  std::size_t range{0};
115  for (auto label = first; label != last; label++) {
116  max_label_val = std::max(max_label_val, *label);
117  range++;
118  }
119 
120  // Compute histograms that store the number of labels in input for each label value.
121  std::vector<LabelID> hist(max_label_val + 1, 0);
122  for (auto label = first; label != last; label++) {
123  hist[*label]++;
124  }
125 
126  // Compute starting position for each label value;
127  std::vector<LabelID> pos(max_label_val + 1, 0);
128  for (std::size_t label_val{0}; label_val < max_label_val; label_val++) {
129  pos[label_val + 1] = pos.at(label_val) + hist.at(label_val);
130  }
131 
132  // Compute sorted label vector.
133  std::vector<LabelID> sorted_labels(range);
134  for (auto label = first; label != last; label++) {
135  sorted_labels[pos[*label]++] = *label;
136  }
137 
138  // Write sorted label vector into input.
139  for (auto label = sorted_labels.begin(); label != sorted_labels.end(); label++) {
140  *first++ = *label;
141  }
142 }
143 
144 }
145 
146 }
147 
148 #endif /* SRC_UTIL_MISC_IPP_ */
A matrix class with basic functionality.
Definition: matrix.hpp:38
Definition: util.hpp:37
A class for node maps.
Definition: node_map.hpp:43
std::size_t num_source_nodes() const
Returns number of source nodes contained in the node map.
Definition: node_map.ipp:81
bool is_edge(NodeID tail, NodeID head) const
Checks if an edge exists.
Definition: ged_graph.ipp:262
std::size_t num_cols() const
Returns the number of columns.
Definition: matrix.ipp:110
std::size_t LabelID
Internally used type of node and edge labels.
void counting_sort(std::vector< LabelID >::iterator first, std::vector< LabelID >::iterator last)
Implementation of counting sort.
Definition: misc.ipp:111
Runtime error class.
Definition: error.hpp:37
static NodeID dummy_node()
Returns a dummy node.
Definition: ged_graph.ipp:56
void parse_config_file(const std::string &filename, std::map< std::string, std::string > &options)
Parses a configuration file.
Definition: misc.ipp:49
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
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 save_as_config_file(const std::string &filename, const std::map< std::string, std::string > &options)
Saves a string map as a configuration file as expected by parse_config_file().
Definition: misc.ipp:76
void construct_node_map_from_solver(const Solver &solver, NodeMap &node_map, std::size_t solution_id=0)
Constructs a node map from a solution to LSAPE or LSAPE stored in a ged::LSAPESolver or a ged::LSAPSo...
Definition: misc.ipp:86
void init_adj_matrix(const GEDGraph &graph, DMatrix &adj_matrix)
Initalizes the adjacency matrix of a graph.
Definition: misc.ipp:35
std::size_t num_rows() const
Returns the number of rows.
Definition: matrix.ipp:85