File: Dmatrix.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 (131 lines) | stat: -rw-r--r-- 3,656 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*PGR-GNU*****************************************************************
File: Dmatrix.hpp

Copyright (c) 2016-2026 pgRouting developers
Mail: project@pgrouting.org

------

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*/

/*! @file */

#ifndef INCLUDE_CPP_COMMON_DMATRIX_HPP_
#define INCLUDE_CPP_COMMON_DMATRIX_HPP_
#pragma once

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

typedef struct IID_t_rt IID_t_rt;

namespace pgrouting {
namespace tsp {

class Tour;  // for tourCost

class Dmatrix {
 public:
    Dmatrix() = default;
    explicit Dmatrix(const std::vector < IID_t_rt > &data_costs);
    explicit Dmatrix(const std::map<std::pair<double, double>, int64_t> &euclidean_data);

    bool has_no_infinity() const;
    bool obeys_triangle_inequality() const;
    bool is_symmetric() const;

    /*! @brief sets a special value for the distance(i,j)
     *
     * @param [in] i - index in matrix
     * @param [in] j - index in matrix
     * @param [in] dist - distance from i to j & from j to i
     *
     */
    void set(size_t i, size_t j, double dist) {
        costs[i][j] = costs[j][i] = dist;}

    /*! @brief original id -> true
     *
     * @param [in] id - original id
     * @returns true if id is in the distance table
     */
    bool has_id(int64_t id) const;

    /*! @brief original id -> idx
     *
     * @param [in] id - original id
     * @returns idx index of the id in the distance table
     */
    size_t get_index(int64_t id) const;

    /*! @brief idx -> original id
     *
     * @param [in] idx - index (i-th coordinate)
     * @returns the original id corresponding to idx
     */
    int64_t get_id(size_t idx) const;

    /*! @brief |idx|
     *
     * @returns the total number of coordinates
     */
    size_t size() const {return ids.size();}


    /*! @brief returns a row of distances
     *
     * @param [in] idx - row index
     * @returns distances from idx to all other coordinates
     */
    const std::vector<double>& get_row(size_t idx) const {
        return costs[idx];}

    double comparable_distance(size_t i, size_t j) const {
        return distance(i, j);}

    double distance(int64_t i, int64_t j) const {
        return distance(get_index(i), get_index(j));}

    double distance(size_t i, size_t j) const {
        return costs[i][j];}

    friend std::ostream& operator<<(
            std::ostream &log,
            const Dmatrix &matrix);

    bool empty() const {
        return ids.empty();
    }

 protected:
    void set_ids(const std::vector<IID_t_rt> &data_costs);
    std::vector<int64_t> ids;

 private:
    typedef std::vector < std::vector < double > > Costs;
    Costs costs;
    std::vector< double >& operator[] (size_t i) {return costs[i];}
    const std::vector< double >& operator[] (size_t i) const {return costs[i];}
};

}  // namespace tsp
}  // namespace pgrouting

#endif  // INCLUDE_CPP_COMMON_DMATRIX_HPP_