-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphNode.h
111 lines (99 loc) · 2.39 KB
/
GraphNode.h
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef GRAPHNODE_H
#define GRAPHNODE_H
/** \file GraphNode.h
\brief Contains a class for a Node(vertex) to be used in graphs.
\author René H. Thomsen
\date 22/04 -11
*/
// License: GPL
#include <vector> // Used to contain edges
/** \brief Class that implements a node which can be used for directed graphs and the label can be of user chosen type.
\warning Functions have not been implemented as 'const' so doesn´t work on constant nodes.
*/
template<typename Item>
class GraphNode
{
private:
// Variables
std::vector<GraphNode*> edges; // Pointer to nodes which node points to
std::vector<unsigned char> weights; // Weights for the edges
Item label; // Label for the node
bool marked; // Used for search algorithms and etc.
public:
// Functions
// -Constructors and destructor
/// Default constructor
GraphNode(const Item argLabel)
{
// Set start values
label = argLabel;
marked = false;
}
/// Destructor
~GraphNode()
{
// Nothing
}
// -Utility functions
/// Adds an edge to the node
void addEdge(GraphNode* otherNode)
{
edges.push_back(otherNode);
weights.push_back(0);
}
/// Adds an edge to the node with weight
void addEdge(GraphNode* otherNode, unsigned char weight)
{
edges.push_back(otherNode);
weights.push_back(weight);
}
/// Removes and edge from the node
void removeEdge(GraphNode* otherNode)
{
for(int i = 0; i < edges.size(); i++)
{
if(edges[i] == otherNode)
{
edges.erase(edges.begin() + i);
weights.erase(weights.begin() + i);
break; // Only erases one occurrence
}
}
}
/// Changes the label to the copy of the argument
void changeLabel(const Item newLabel)
{
label = newLabel;
}
/// Returns 'true' if the node is marked
bool isMarked()
{
return(marked);
}
/// Sets the node as marked('true')
void mark()
{
marked = true;
}
/// Unmarks the node('false')
void unMark()
{
marked = false;
}
/// Returns the list of edges
std::vector<GraphNode*>& getEdges()
{
return(edges);
}
/// Returns the label value
const Item& getLabel()
{
return(label);
}
/// Returns the weights for the edges
const std::vector<unsigned char>& getWeights()
{
return(weights);
}
};
#endif