SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
Graph.hpp
Go to the documentation of this file.
1
#ifndef _GRAPH_H_
2
#define _GRAPH_H_
3
4
namespace
Sleak
5
{
6
/**
7
* @class Graph
8
* @brief Represents a graph data structure with vertices and edges.
9
*
10
* The Graph class provides functionality for managing a collection of vertices and their
11
* connections (edges). It is suitable for modeling networks, relationships, and
12
* interconnected systems. Use this class when you need to represent entities with
13
* connections, such as social networks, road networks, or dependency graphs.
14
*
15
* Example Use Cases:
16
* - Representing a social network where vertices are users and edges are friendships.
17
* - Modeling a road network for navigation and route planning.
18
* - Implementing dependency tracking in a build system.
19
* - Simulating network communication protocols.
20
*
21
* Implementation Details:
22
* - Uses an adjacency list representation for efficient storage of sparse graphs.
23
* - Provides methods for adding vertices and edges, and for traversing the graph.
24
* @ingroup utility
25
*/
26
template
<
typename
T>
27
class
Graph
28
{
29
private
:
30
/// One entry in a vertex's adjacency list.
31
struct
Node
32
{
33
T data;
34
Node* next;
35
36
Node(
const
T& value) : data(value), next(
nullptr
) {}
37
};
38
39
/// A graph vertex plus its outgoing edges and link to the next vertex.
40
struct
Vertex
41
{
42
T data;
43
Node* adjacencyList;
44
Vertex* next;
45
};
46
47
Vertex* vertices;
48
49
/// Linear search for the vertex holding value, or nullptr.
50
Vertex* findVertex(
const
T& value)
51
{
52
Vertex* current = vertices;
53
while
(current)
54
{
55
if
(current->data == value)
56
return
current;
57
current = current->next;
58
}
59
return
nullptr
;
60
}
61
62
public
:
63
Graph
() : vertices(nullptr) {}
64
65
/// Frees every vertex and its adjacency list.
66
~Graph
()
67
{
68
while
(vertices)
69
{
70
Vertex* temp = vertices;
71
while
(temp->adjacencyList)
72
{
73
Node* adjTemp = temp->adjacencyList;
74
temp->adjacencyList = temp->adjacencyList->next;
75
delete
adjTemp;
76
}
77
vertices = vertices->next;
78
delete
temp;
79
}
80
}
81
82
/// Adds value as a vertex if it isn't already present.
83
void
addVertex
(
const
T& value)
84
{
85
if
(!findVertex(value))
86
{
87
Vertex* newVertex =
new
Vertex{value,
nullptr
, vertices};
88
vertices = newVertex;
89
}
90
}
91
92
/// Adds a directed edge from -> to; no-ops if either vertex is missing.
93
void
addEdge
(
const
T& from,
const
T& to)
94
{
95
Vertex* fromVertex = findVertex(from);
96
Vertex* toVertex = findVertex(to);
97
98
if
(!fromVertex || !toVertex)
99
return
;
100
101
Node* newEdge =
new
Node(to);
102
newEdge->next = fromVertex->adjacencyList;
103
fromVertex->adjacencyList = newEdge;
104
}
105
};
106
}
107
108
#endif
// _GRAPH_H_
Sleak::Graph::addVertex
void addVertex(const T &value)
Adds value as a vertex if it isn't already present.
Definition
Graph.hpp:83
Sleak::Graph::Graph
Graph()
Definition
Graph.hpp:63
Sleak::Graph::addEdge
void addEdge(const T &from, const T &to)
Adds a directed edge from -> to; no-ops if either vertex is missing.
Definition
Graph.hpp:93
Sleak::Graph::~Graph
~Graph()
Frees every vertex and its adjacency list.
Definition
Graph.hpp:66
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
include
public
Utility
Container
Graph.hpp
Generated on
for SleakEngine by
1.18.0