File: graph_to_edges.hpp

package info (click to toggle)
pgrouting 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,332 kB
  • sloc: cpp: 21,315; sql: 10,419; ansic: 9,795; perl: 1,142; sh: 919; javascript: 314; xml: 182; makefile: 29
file content (89 lines) | stat: -rw-r--r-- 2,650 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*PGR-GNU*****************************************************************
File: graph_to_edges.hpp

Function's developer:
Copyright (c) 2024 Vicky Vergara
Mail: vicky at gmail.com

------

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 ********************************************************************PGR-GNU*/

#ifndef INCLUDE_BGRAPH_GRAPH_TO_EDGES_HPP_
#define INCLUDE_BGRAPH_GRAPH_TO_EDGES_HPP_

#include <vector>
#include <map>
#include <utility>
#include <cstdint>

#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>

#include "cpp_common/edge_t.hpp"
#include "c_types/edge_rt.h"

namespace pgrouting {
namespace b_g {

/** @brief converts a bg to Edges_t
 * @param[in] bg the boost graph
 * @param[in] add_self_loop flag to include self loops on the graph
 * @returns a set of Edges_t that exist on the graph
 *   id, source, target, cost, reverse_cost
 *   source < target
 */
template<typename G>
std::vector<Edge_t> graph_to_existing_edges(const G &bg, bool add_self_loop) {
    std::vector<Edge_t> results;

    std::map<std::pair<int64_t, int64_t>, Edge_t> st_to_edge;
    int64_t count = 0;

    auto bg_edges = boost::edges(bg);
    for (auto e = bg_edges.first; e != bg_edges.second; ++e) {
        auto s = bg[boost::source(*e, bg)].id;
        auto t = bg[boost::target(*e, bg)].id;

        if (s == t && !add_self_loop) continue;

        /*
         * Reverse edge already been added
         */
        if (st_to_edge.find({t, s}) != st_to_edge.end()) {
            st_to_edge[std::pair<int64_t, int64_t>(t, s)].reverse_cost = 1.0;
            continue;
        }

        /*
         * Already been added
         */
        if (st_to_edge.find({s, t}) != st_to_edge.end()) continue;

        st_to_edge[std::pair<int64_t, int64_t>(s, t)] = {++count, s, t, 1, -1};
    }

    for (const auto &st : st_to_edge) {
        results.push_back(st.second);
    }
    return results;
}

}  // namespace b_g
}  // namespace pgrouting

#endif  // INCLUDE_BGRAPH_GRAPH_TO_EDGES_HPP_