OR-Tools  9.6
simple_graph.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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_SIMPLE_GRAPH_H_
17 #define OR_TOOLS_ROUTING_SIMPLE_GRAPH_H_
18 
19 #include <algorithm>
20 #include <functional>
21 #include <ostream>
22 #include <string>
23 
24 #include "absl/hash/hash.h"
25 
26 namespace operations_research {
27 
28 class 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.
32 class 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.
62 class 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.
89 typedef std::function<int64_t(int, int)> EdgeWeights;
90 
91 // Real-world coordinates.
92 template <typename T>
93 struct Coordinates2 {
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 
117 template <typename T>
118 struct Coordinates3 {
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.
147 template <typename T>
149  T start;
150  T end;
151 };
152 
153 } // namespace operations_research
154 
155 #endif // OR_TOOLS_ROUTING_SIMPLE_GRAPH_H_
int64_t max
Definition: alldiff_cst.cc:140
int64_t min
Definition: alldiff_cst.cc:139
bool operator==(const Arc &other) const
Definition: simple_graph.h:71
int64_t head() const
Definition: simple_graph.h:68
Arc(int64_t tail, int64_t head)
Definition: simple_graph.h:64
friend H AbslHashValue(H h, const Arc &a)
Definition: simple_graph.h:78
bool operator!=(const Arc &other) const
Definition: simple_graph.h:75
int64_t tail() const
Definition: simple_graph.h:67
friend H AbslHashValue(H h, const Edge &a)
Definition: simple_graph.h:48
bool operator==(const Edge &other) const
Definition: simple_graph.h:40
int64_t head() const
Definition: simple_graph.h:38
int64_t tail() const
Definition: simple_graph.h:37
Edge(int64_t tail, int64_t head)
Definition: simple_graph.h:34
bool operator!=(const Edge &other) const
Definition: simple_graph.h:45
int64_t b
int64_t a
int arc
Collection of objects used to extend the Constraint Solver library.
std::function< int64_t(int, int)> EdgeWeights
Definition: simple_graph.h:89
friend bool operator==(const Coordinates2 &a, const Coordinates2 &b)
Definition: simple_graph.h:100
friend bool operator!=(const Coordinates2 &a, const Coordinates2 &b)
Definition: simple_graph.h:103
friend std::ostream & operator<<(std::ostream &stream, const Coordinates2 &coordinates)
Definition: simple_graph.h:106
friend H AbslHashValue(H h, const Coordinates2 &coordinates)
Definition: simple_graph.h:112
friend bool operator==(const Coordinates3 &a, const Coordinates3 &b)
Definition: simple_graph.h:126
friend std::ostream & operator<<(std::ostream &stream, const Coordinates3 &coordinates)
Definition: simple_graph.h:132
friend H AbslHashValue(H h, const Coordinates3 &coordinates)
Definition: simple_graph.h:138
friend bool operator!=(const Coordinates3 &a, const Coordinates3 &b)
Definition: simple_graph.h:129