Apollo  6.0
Open source self driving car software
graph.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifndef CYBER_SERVICE_DISCOVERY_CONTAINER_GRAPH_H_
18 #define CYBER_SERVICE_DISCOVERY_CONTAINER_GRAPH_H_
19 
20 #include <cstdint>
21 #include <string>
22 #include <unordered_map>
23 
25 
26 namespace apollo {
27 namespace cyber {
28 namespace service_discovery {
29 
43 };
44 
45 // opt impl of Vertice/Edge/Graph, replace stl with base
46 class Vertice {
47  public:
48  explicit Vertice(const std::string& val = "");
49  Vertice(const Vertice& other);
50  virtual ~Vertice();
51 
52  Vertice& operator=(const Vertice& rhs);
53  bool operator==(const Vertice& rhs) const;
54  bool operator!=(const Vertice& rhs) const;
55 
56  bool IsDummy() const;
57  const std::string& GetKey() const;
58 
59  const std::string& value() const { return value_; }
60 
61  private:
62  std::string value_;
63 };
64 
65 class Edge {
66  public:
67  Edge();
68  Edge(const Edge& other);
69  Edge(const Vertice& src, const Vertice& dst, const std::string& val);
70  virtual ~Edge();
71 
72  Edge& operator=(const Edge& rhs);
73  bool operator==(const Edge& rhs) const;
74 
75  bool IsValid() const;
76  std::string GetKey() const;
77 
78  const Vertice& src() const { return src_; }
79  void set_src(const Vertice& v) { src_ = v; }
80 
81  const Vertice& dst() const { return dst_; }
82  void set_dst(const Vertice& v) { dst_ = v; }
83 
84  const std::string& value() const { return value_; }
85  void set_value(const std::string& val) { value_ = val; }
86 
87  private:
88  Vertice src_;
89  Vertice dst_;
90  std::string value_;
91 };
92 
93 class Graph {
94  public:
95  using VerticeSet = std::unordered_map<std::string, Vertice>;
96  using AdjacencyList = std::unordered_map<std::string, VerticeSet>;
97 
98  Graph();
99  virtual ~Graph();
100 
101  void Insert(const Edge& e);
102  void Delete(const Edge& e);
103 
104  uint32_t GetNumOfEdge();
105  FlowDirection GetDirectionOf(const Vertice& lhs, const Vertice& rhs);
106 
107  private:
108  struct RelatedVertices {
109  RelatedVertices() {}
110 
111  VerticeSet src;
112  VerticeSet dst;
113  };
114  using EdgeInfo = std::unordered_map<std::string, RelatedVertices>;
115 
116  void InsertOutgoingEdge(const Edge& e);
117  void InsertIncomingEdge(const Edge& e);
118  void InsertCompleteEdge(const Edge& e);
119  void DeleteOutgoingEdge(const Edge& e);
120  void DeleteIncomingEdge(const Edge& e);
121  void DeleteCompleteEdge(const Edge& e);
122  bool LevelTraverse(const Vertice& start, const Vertice& end);
123 
124  EdgeInfo edges_;
125  AdjacencyList list_;
126  base::AtomicRWLock rw_lock_;
127 };
128 
129 } // namespace service_discovery
130 } // namespace cyber
131 } // namespace apollo
132 
133 #endif // CYBER_SERVICE_DISCOVERY_CONTAINER_GRAPH_H_
Vertice & operator=(const Vertice &rhs)
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: atomic_rw_lock.h:36
const Vertice & src() const
Definition: graph.h:78
const std::string & GetKey() const
FlowDirection
describe the flow direction between nodes As the DAG below A-—>B--—>C<--—D GetDirectionOf(A, B) is UPSTREAM GetDirectionOf(C, A) is DOWNSTREAM GetDirectionOf(D, A) is UNREACHABLE GetDirectionOf(A, D) is UNREACHABLE
Definition: graph.h:39
const std::string & value() const
Definition: graph.h:84
std::unordered_map< std::string, VerticeSet > AdjacencyList
Definition: graph.h:96
void set_src(const Vertice &v)
Definition: graph.h:79
bool operator!=(const Vertice &rhs) const
const std::string & value() const
Definition: graph.h:59
void set_value(const std::string &val)
Definition: graph.h:85
bool operator==(const Vertice &rhs) const
const Vertice & dst() const
Definition: graph.h:81
std::unordered_map< std::string, Vertice > VerticeSet
Definition: graph.h:95
Vertice(const std::string &val="")
void set_dst(const Vertice &v)
Definition: graph.h:82