-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cpp
More file actions
40 lines (34 loc) · 1.07 KB
/
Node.cpp
File metadata and controls
40 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "Node.h"
#include "algorithm"
Node::Node(const std::string& id) : id(id) {}
const std::string& Node::getId() const {
return id;
}
//adds the pointer to the neighbor vector if it is not already a neighbor
void Node::addNeighbor(std::shared_ptr<Node> neighbor){
if(!hasNeighbor(neighbor->getId())) {
neighbors.push_back(neighbor);
}
}
//remove neighbor of specified id
void Node::removeNeighbor(const std::string& neighborId) {
neighbors.erase(
std::remove_if(neighbors.begin(), neighbors.end(),
[&](const std::shared_ptr<Node>& n) {
return n->getId() == neighborId;
}),
neighbors.end()
);
}
const std::vector<std::shared_ptr<Node>>& Node::getNeighbors() const {
return neighbors;
}
//checks if the argument pointer to a node is in the neighbor vector
bool Node::hasNeighbor(const std::string& neighborId) const{
for (const auto& neighbor : neighbors) {
if (neighbor->getId() == neighborId) {
return true;
}
}
return false;
}