Apollo 10.0
自动驾驶开放平台
graph.h
浏览该文件的文档.
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
26namespace apollo {
27namespace cyber {
28namespace service_discovery {
29
44
45// opt impl of Vertice/Edge/Graph, replace stl with base
46class 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
65class 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
93class 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_
void set_value(const std::string &val)
Definition graph.h:85
void set_src(const Vertice &v)
Definition graph.h:79
void set_dst(const Vertice &v)
Definition graph.h:82
const std::string & value() const
Definition graph.h:84
const Vertice & src() const
Definition graph.h:78
Edge & operator=(const Edge &rhs)
Definition graph.cc:67
bool operator==(const Edge &rhs) const
Definition graph.cc:76
const Vertice & dst() const
Definition graph.h:81
std::unordered_map< std::string, VerticeSet > AdjacencyList
Definition graph.h:96
FlowDirection GetDirectionOf(const Vertice &lhs, const Vertice &rhs)
Definition graph.cc:145
std::unordered_map< std::string, Vertice > VerticeSet
Definition graph.h:95
bool operator==(const Vertice &rhs) const
Definition graph.cc:42
const std::string & GetKey() const
Definition graph.cc:52
bool operator!=(const Vertice &rhs) const
Definition graph.cc:46
Vertice & operator=(const Vertice &rhs)
Definition graph.cc:35
const std::string & value() const
Definition graph.h:59
FlowDirection
describe the flow direction between nodes As the DAG below A-—>B--—>C<--—D GetDirectionOf(A,...
Definition graph.h:39
class register implement
Definition arena_queue.h:37