diff --git a/core/indigo-core/graph/simple_cycle_basis.h b/core/indigo-core/graph/simple_cycle_basis.h index 7e59ee05df..312b6b6b85 100644 --- a/core/indigo-core/graph/simple_cycle_basis.h +++ b/core/indigo-core/graph/simple_cycle_basis.h @@ -19,6 +19,8 @@ #ifndef _SIMPLE_CYCLE_BASIS_H #define _SIMPLE_CYCLE_BASIS_H +#include + #include "base_cpp/array.h" #include "base_cpp/obj_array.h" #include "base_cpp/red_black.h" @@ -56,10 +58,10 @@ namespace indigo void _prepareSubgraph(Graph& subgraph); - RedBlackMap vertices_spanning_tree; + std::map vertices_spanning_tree; - RedBlackMap spanning_tree_vertices; - RedBlackMap _edgeIndexMap; + std::map spanning_tree_vertices; + std::map _edgeIndexMap; const Graph& _graph; @@ -72,20 +74,22 @@ namespace indigo { // graph to aux. graph - RedBlackMap _vertexMap0; - RedBlackMap _vertexMap1; + std::map _vertexMap0; + std::map _vertexMap1; - RedBlackMap _auxVertexMap; + std::map _auxVertexMap; // aux. edge to edge - RedBlackMap _auxEdgeMap; + std::map _auxEdgeMap; const Graph& _graph; Array& _u; - RedBlackMap& _edgeIndexMap; + std::map& _edgeIndexMap; + + int _findOrCreateVertex(std::map& vertexMap, int vertex); public: - AuxiliaryGraph(const Graph& graph, Array& u, RedBlackMap& edgeIndexMap) : _graph(graph), _u(u), _edgeIndexMap(edgeIndexMap) + AuxiliaryGraph(const Graph& graph, Array& u, std::map& edgeIndexMap) : _graph(graph), _u(u), _edgeIndexMap(edgeIndexMap) { } diff --git a/core/indigo-core/graph/src/simple_cycle_basis.cpp b/core/indigo-core/graph/src/simple_cycle_basis.cpp index d208de7c3c..a3463098e0 100644 --- a/core/indigo-core/graph/src/simple_cycle_basis.cpp +++ b/core/indigo-core/graph/src/simple_cycle_basis.cpp @@ -71,8 +71,8 @@ void SimpleCycleBasis::create() int new_vertex = spanning_tree.addVertex(); - vertices_spanning_tree.insert(current_vertex, new_vertex); - spanning_tree_vertices.insert(new_vertex, current_vertex); + vertices_spanning_tree.emplace(current_vertex, new_vertex); + spanning_tree_vertices.emplace(new_vertex, current_vertex); vertex_queue.push(current_vertex); @@ -101,18 +101,18 @@ void SimpleCycleBasis::create() int next_vertex = subgraph.getEdge(edge).findOtherEnd(current_vertex); - if (!vertices_spanning_tree.find(next_vertex)) + if (vertices_spanning_tree.find(next_vertex) == vertices_spanning_tree.end()) { // tree edge tree_edges.push(edge); int new_vertex = spanning_tree.addVertex(); - vertices_spanning_tree.insert(next_vertex, new_vertex); - spanning_tree_vertices.insert(new_vertex, next_vertex); + vertices_spanning_tree.emplace(next_vertex, new_vertex); + spanning_tree_vertices.emplace(new_vertex, next_vertex); // create a new (directed) Edge object (as explained above) - spanning_tree.addEdge(vertices_spanning_tree.at(current_vertex), vertices_spanning_tree.at(next_vertex)); + spanning_tree.addEdge(vertices_spanning_tree.at(current_vertex), new_vertex); // add the next vertex to the BFS-FIFO vertex_queue.push(next_vertex); @@ -369,7 +369,7 @@ void SimpleCycleBasis::_createEdgeIndexMap() _edgeIndexMap.clear(); for (int i = 0; i < _edgeList.size(); ++i) { - _edgeIndexMap.insert(_edgeList[i], i); + _edgeIndexMap.emplace(_edgeList[i], i); } } @@ -427,28 +427,29 @@ void SimpleCycleBasis::_prepareSubgraph(Graph& subgraph) } } -int AuxiliaryGraph::auxVertex0(int vertex) +int AuxiliaryGraph::_findOrCreateVertex(std::map& vertexMap, int vertex) { - if (!_vertexMap0.find(vertex)) + const auto it = vertexMap.find(vertex); + if (it != vertexMap.end()) { - int newVertex0 = addVertex(); - _vertexMap0.insert(vertex, newVertex0); - _auxVertexMap.insert(newVertex0, vertex); - return newVertex0; + return it->second; } - return _vertexMap0.at(vertex); + + int newVertex = addVertex(); + vertexMap.emplace(vertex, newVertex); + _auxVertexMap.emplace(newVertex, vertex); + + return newVertex; +} + +int AuxiliaryGraph::auxVertex0(int vertex) +{ + return _findOrCreateVertex(_vertexMap0, vertex); } int AuxiliaryGraph::auxVertex1(int vertex) { - if (!_vertexMap1.find(vertex)) - { - int newVertex1 = addVertex(); - _vertexMap1.insert(vertex, newVertex1); - _auxVertexMap.insert(newVertex1, vertex); - return newVertex1; - } - return _vertexMap1.at(vertex); + return _findOrCreateVertex(_vertexMap1, vertex); } const Vertex& AuxiliaryGraph::getVertexAndBuild(int auxVertex) @@ -472,7 +473,7 @@ const Vertex& AuxiliaryGraph::getVertexAndBuild(int auxVertex) if (ex_aux_edge == -1) { int auxEdge = addEdge(vertex1u, vertex2u); - _auxEdgeMap.insert(auxEdge, edge); + _auxEdgeMap.emplace(auxEdge, edge); } vertex1u = auxVertex1(vertex1); @@ -482,7 +483,7 @@ const Vertex& AuxiliaryGraph::getVertexAndBuild(int auxVertex) if (ex_aux_edge == -1) { int auxEdge = addEdge(vertex1u, vertex2u); - _auxEdgeMap.insert(auxEdge, edge); + _auxEdgeMap.emplace(auxEdge, edge); } } else @@ -493,7 +494,7 @@ const Vertex& AuxiliaryGraph::getVertexAndBuild(int auxVertex) if (ex_aux_edge == -1) { int auxEdge = addEdge(vertex1u, vertex2u); - _auxEdgeMap.insert(auxEdge, edge); + _auxEdgeMap.emplace(auxEdge, edge); } vertex1u = auxVertex1(vertex1); @@ -502,7 +503,7 @@ const Vertex& AuxiliaryGraph::getVertexAndBuild(int auxVertex) if (ex_aux_edge == -1) { int auxEdge = addEdge(vertex1u, vertex2u); - _auxEdgeMap.insert(auxEdge, edge); + _auxEdgeMap.emplace(auxEdge, edge); } } } @@ -512,5 +513,5 @@ const Vertex& AuxiliaryGraph::getVertexAndBuild(int auxVertex) int AuxiliaryGraph::edge(int auxEdge) { - return _auxEdgeMap.value(auxEdge); + return _auxEdgeMap.at(auxEdge); }