votca 2024.2-dev
Loading...
Searching...
No Matches
edgecontainer.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Standard includes
21#include <algorithm>
22#include <cassert>
23#include <exception>
24#include <set>
25#include <vector>
26
27// Local VOTCA includes
28#include "votca/tools/edge.h"
30
31namespace votca {
32namespace tools {
33
34using namespace std;
35
37
38EdgeContainer::EdgeContainer(vector<Edge> edges) {
39 for (Edge& edge : edges) {
40 addEdge(edge);
41 }
42}
43
45 Index max = 0;
46 for (const auto& vertex_and_neigh_and_count : adj_list_) {
47 Index degree = getDegree(vertex_and_neigh_and_count.first);
48 if (degree > max) {
49 max = degree;
50 }
51 }
52 return max;
53}
54
56 if (!adj_list_.count(vertex)) {
57 throw invalid_argument("vertex is not defined");
58 }
59 Index degree_count = 0;
60 if (adj_list_.at(vertex).size() == 0) {
61 return degree_count;
62 }
63 for (const pair<const Index, Index>& neighbor_and_count :
64 adj_list_.at(vertex)) {
65 if (neighbor_and_count.first == vertex) {
66 degree_count += neighbor_and_count.second * 2;
67 } else {
68 degree_count += neighbor_and_count.second;
69 }
70 }
71 return degree_count;
72}
73
74vector<Index> EdgeContainer::getVerticesDegree(Index degree) const {
75 vector<Index> vertices;
76 for (const auto& vertex_and_neigh_and_count : adj_list_) {
77 Index degree_count = getDegree(vertex_and_neigh_and_count.first);
78 if (degree_count == degree) {
79 vertices.push_back(vertex_and_neigh_and_count.first);
80 }
81 }
82 return vertices;
83}
84
86 for (const auto& vertex_and_neigh_and_count : adj_list_) {
87 Index degree_count = getDegree(vertex_and_neigh_and_count.first);
88 if (degree_count == degree) {
89 return true;
90 }
91 }
92 return false;
93}
94
95bool EdgeContainer::edgeExist(const Edge& edge) const {
96 if (adj_list_.count(edge.getEndPoint1())) {
97 if (adj_list_.at(edge.getEndPoint1()).count(edge.getEndPoint2())) {
98 return true;
99 }
100 }
101 if (adj_list_.count(edge.getEndPoint2())) {
102 if (adj_list_.at(edge.getEndPoint2()).count(edge.getEndPoint1())) {
103 return true;
104 }
105 }
106 return false;
107}
108
109bool EdgeContainer::vertexExist(const Index vertex) const {
110 return adj_list_.count(vertex);
111}
112
114
115 Index point1 = edge.getEndPoint1();
116 Index point2 = edge.getEndPoint2();
117 if (adj_list_[point1].count(point2)) {
118 ++adj_list_[point1][point2];
119 } else {
120 adj_list_[point1][point2] = 1;
121 }
122
123 // Do not add the same edge a second time if the points are the same
124 if (point1 != point2) {
125 if (adj_list_[point2].count(point1)) {
126 ++adj_list_[point2][point1];
127 } else {
128 adj_list_[point2][point1] = 1;
129 }
130 }
131 return;
132}
133
135 assert(adj_list_.count(vertex) == 0 && "Cannot add vertex already exists");
136 unordered_map<Index, Index> empty_temp;
137 adj_list_[vertex] = empty_temp;
138}
139
140vector<Index> EdgeContainer::getVertices() const {
141 vector<Index> vertices;
142 for (const pair<const Index, unordered_map<Index, Index>>&
143 vertex_and_neigh_and_count : adj_list_) {
144 vertices.push_back(vertex_and_neigh_and_count.first);
145 }
146 return vertices;
147}
148
149vector<Index> EdgeContainer::getNeighVertices(Index vertex) const {
150 vector<Index> neigh_verts;
151 if (adj_list_.count(vertex)) {
152 for (const pair<const Index, Index>& neigh_and_count :
153 adj_list_.at(vertex)) {
154 neigh_verts.push_back(neigh_and_count.first);
155 }
156 }
157 return neigh_verts;
158}
159
160vector<Edge> EdgeContainer::getNeighEdges(Index vertex) const {
161 vector<Edge> neigh_edges;
162 if (adj_list_.count(vertex)) {
163 for (const pair<const Index, Index>& neigh_and_count :
164 adj_list_.at(vertex)) {
165 for (Index count = 0;
166 count < adj_list_.at(vertex).at(neigh_and_count.first); ++count) {
167 neigh_edges.push_back(Edge(vertex, neigh_and_count.first));
168 }
169 }
170 }
171 return neigh_edges;
172}
173
174vector<Edge> EdgeContainer::getEdges() const {
175 unordered_map<Edge, Index> extra_edge_count;
176 for (const auto& vertex_and_neigh_and_count : adj_list_) {
177 for (const pair<const Index, Index>& neigh_and_count :
178 vertex_and_neigh_and_count.second) {
179 extra_edge_count[Edge(vertex_and_neigh_and_count.first,
180 neigh_and_count.first)] = neigh_and_count.second;
181 }
182 }
183 vector<Edge> edges;
184 for (pair<const Edge, Index>& edge_count : extra_edge_count) {
185 for (Index count = 0; count < edge_count.second; ++count) {
186 edges.push_back(edge_count.first);
187 }
188 }
189 return edges;
190}
191
192ostream& operator<<(ostream& os, const EdgeContainer edgecontainer) {
193 auto edges = edgecontainer.getEdges();
194 for (auto edge : edges) {
195 os << edge << endl;
196 }
197 return os;
198}
199
200} // namespace tools
201} // namespace votca
EdgeContainer is responsible for operations on groups of edges.
std::vector< Index > getVerticesDegree(Index degree) const
Contains vector of all vertices with degree.
Index getDegree(const Index vertex) const
Determine the degree of the vertex/number of edges attached.
EdgeContainer()=default
Constructors can take no arguments a single Edge or a vector of edges.
std::vector< Edge > getNeighEdges(Index vertex) const
Get the edges neighboring vert.
void addEdge(Edge edge)
Add an edge to the container.
bool vertexExistWithDegree(Index degree) const
Determine if a vertex with the specified degree exists.
void addVertex(Index vertex)
Add a lone vertex.
std::unordered_map< Index, std::unordered_map< Index, Index > > adj_list_
The vertex, the neighboring vertices and the number of edges.
std::vector< Edge > getEdges() const
Get all the edges in vector form.
std::vector< Index > getVertices() const
Get all the vertices in vector form.
std::vector< Index > getNeighVertices(Index vertex) const
Get the vertices neighboring vert.
bool vertexExist(Index vertex) const
Check if the vertex exists returns true or false.
Index getMaxDegree() const
Get the value of the max degree.
bool edgeExist(const Edge &edge) const
Check if the edge exists returns true or false.
Connects to vertices.
Definition edge.h:42
Index getEndPoint1() const
grab the smaller integer
Definition edge.h:55
Index getEndPoint2() const
grab the larger integer
Definition edge.h:57
STL namespace.
std::ostream & operator<<(std::ostream &out, const Correlate &c)
Definition correlate.h:53
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26