Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
simple_graph.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// Common utilities for parsing routing instances.
15
16#ifndef OR_TOOLS_ROUTING_PARSERS_SIMPLE_GRAPH_H_
17#define OR_TOOLS_ROUTING_PARSERS_SIMPLE_GRAPH_H_
18
19#include <algorithm>
20#include <functional>
21#include <ostream>
22#include <string>
23
24#include "absl/hash/hash.h"
25
26namespace operations_research {
27
28class Arc;
29
30// Edge, undirected, between the head to the tail.
31// With a few bells and whistles to allow its use within hash tables.
32class Edge {
33 public:
34 Edge(int64_t tail, int64_t head) : tail_(tail), head_(head) {}
35 explicit Edge(const Arc& arc);
36
37 int64_t tail() const { return tail_; }
38 int64_t head() const { return head_; }
39
40 bool operator==(const Edge& other) const {
41 return (head_ == other.head_ && tail_ == other.tail_) ||
42 (head_ == other.tail_ && tail_ == other.head_);
43 }
44
45 bool operator!=(const Edge& other) const { return !this->operator==(other); }
46
47 template <typename H>
48 friend H AbslHashValue(H h, const Edge& a) {
49 // This hash value should not depend on the direction of the edge, hence
50 // the use of min and max.
51 return H::combine(std::move(h), std::min(a.head_, a.tail_),
52 std::max(a.head_, a.tail_));
53 }
54
55 private:
56 const int64_t tail_;
57 const int64_t head_;
58};
59
60// Arc, directed, from the tail to the head.
61// With a few bells and whistles to allow its use within hash tables.
62class Arc {
63 public:
64 Arc(int64_t tail, int64_t head) : tail_(tail), head_(head) {}
65 explicit Arc(const Edge& edge);
66
67 int64_t tail() const { return tail_; }
68 int64_t head() const { return head_; }
69 Arc reversed() const { return {head_, tail_}; }
70
71 bool operator==(const Arc& other) const {
72 return head_ == other.head_ && tail_ == other.tail_;
73 }
74
75 bool operator!=(const Arc& other) const { return !this->operator==(other); }
76
77 template <typename H>
78 friend H AbslHashValue(H h, const Arc& a) {
79 // Unlike the edge, this value *must* depend on the direction of the arc.
80 return H::combine(std::move(h), a.tail_, a.head_);
81 }
82
83 private:
84 const int64_t tail_;
85 const int64_t head_;
86};
87
88// Mapping between an edge (given by its tail and its head) and its weight.
89typedef std::function<int64_t(int, int)> EdgeWeights;
90
91// Real-world coordinates.
92template <typename T>
94 T x = {};
95 T y = {};
96
97 Coordinates2() = default;
98 Coordinates2(T x, T y) : x(x), y(y) {}
99
100 friend bool operator==(const Coordinates2& a, const Coordinates2& b) {
101 return a.x == b.x && a.y == b.y;
102 }
103 friend bool operator!=(const Coordinates2& a, const Coordinates2& b) {
104 return !(a == b);
105 }
106 friend std::ostream& operator<<(std::ostream& stream,
107 const Coordinates2& coordinates) {
108 return stream << "{x = " << coordinates.x << ", y = " << coordinates.y
109 << "}";
110 }
111 template <typename H>
112 friend H AbslHashValue(H h, const Coordinates2& coordinates) {
113 return H::combine(std::move(h), coordinates.x, coordinates.y);
114 }
115};
116
117template <typename T>
119 T x = {};
120 T y = {};
121 T z = {};
122
123 Coordinates3() = default;
124 Coordinates3(T x, T y, T z) : x(x), y(y), z(z) {}
125
126 friend bool operator==(const Coordinates3& a, const Coordinates3& b) {
127 return a.x == b.x && a.y == b.y && a.z == b.z;
128 }
129 friend bool operator!=(const Coordinates3& a, const Coordinates3& b) {
130 return !(a == b);
131 }
132 friend std::ostream& operator<<(std::ostream& stream,
133 const Coordinates3& coordinates) {
134 return stream << "{x = " << coordinates.x << ", y = " << coordinates.y
135 << ", z = " << coordinates.z << "}";
136 }
137 template <typename H>
138 friend H AbslHashValue(H h, const Coordinates3& coordinates) {
139 return H::combine(std::move(h), coordinates.x, coordinates.y,
140 coordinates.z);
141 }
142};
143
144// Time window, typically used for a node.
145// Name chosen to avoid clash with tour_optimization.proto, defining a
146// TimeWindow message with more fields.
147template <typename T>
151};
152
153} // namespace operations_research
154
155#endif // OR_TOOLS_ROUTING_PARSERS_SIMPLE_GRAPH_H_
bool operator!=(const Arc &other) const
friend H AbslHashValue(H h, const Arc &a)
bool operator==(const Arc &other) const
Arc(int64_t tail, int64_t head)
bool operator!=(const Edge &other) const
friend H AbslHashValue(H h, const Edge &a)
Edge(int64_t tail, int64_t head)
bool operator==(const Edge &other) const
int64_t b
Definition table.cc:45
int64_t a
Definition table.cc:44
Edge edge
int arc
In SWIG mode, we don't want anything besides these top-level includes.
std::function< int64_t(int, int)> EdgeWeights
Mapping between an edge (given by its tail and its head) and its weight.
Real-world coordinates.
friend bool operator==(const Coordinates2 &a, const Coordinates2 &b)
friend bool operator!=(const Coordinates2 &a, const Coordinates2 &b)
friend H AbslHashValue(H h, const Coordinates2 &coordinates)
friend std::ostream & operator<<(std::ostream &stream, const Coordinates2 &coordinates)
friend bool operator==(const Coordinates3 &a, const Coordinates3 &b)
friend H AbslHashValue(H h, const Coordinates3 &coordinates)
friend bool operator!=(const Coordinates3 &a, const Coordinates3 &b)
friend std::ostream & operator<<(std::ostream &stream, const Coordinates3 &coordinates)