Refactored for a flat buffer friendly, and O(1) or O(n) time complexities.
This commit is contained in:
108
graph.h
108
graph.h
@@ -1,36 +1,136 @@
|
||||
#pragma once
|
||||
#include <new>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
|
||||
|
||||
/** @struct{Point}
|
||||
* @property float x : width value
|
||||
* @property float y : height value
|
||||
* @property float z : Depth value
|
||||
* @property int first_edge : offset of the first edge in memory for a point
|
||||
* @property int e_count : number of edges for a point
|
||||
*/
|
||||
struct Point {
|
||||
std::string name;
|
||||
float x, y, z;
|
||||
int first_edge = -1;
|
||||
int e_count = 0;
|
||||
bool is_active = false;
|
||||
};
|
||||
|
||||
|
||||
/** Edge
|
||||
* @brief Stores the end point and weight of the edge
|
||||
* @param end_point Index of the point this edge goes to.
|
||||
* @param e_weight Weight of the edge.
|
||||
*/
|
||||
struct Edge {
|
||||
int target_point;
|
||||
int end_point;
|
||||
float e_weight;
|
||||
int next_edge_index = -1;
|
||||
};
|
||||
|
||||
/** @class Graph
|
||||
* @brief Defines the default values for the private variables
|
||||
* @property all_points The buffer that stores all of the points in the graph
|
||||
* @property all_edges The buffer that stores all the edges in the graph
|
||||
* @property next_possible_edge The offset to the next possible edge memory location
|
||||
* @property p_capacity The total number of points able to be stored
|
||||
* @property e_capacity The total number of edges able to be stored
|
||||
*/
|
||||
class Graph {
|
||||
private:
|
||||
Point* all_points = nullptr;
|
||||
Edge* all_edges = nullptr;
|
||||
|
||||
int next_free_point = -1;
|
||||
size_t num_points = 0;
|
||||
|
||||
int next_free_edge = -1;
|
||||
size_t num_edges = 0;
|
||||
|
||||
size_t next_possible_edge = 0;
|
||||
size_t p_capacity = 0;
|
||||
size_t e_capacity = 0;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
/** Constructor
|
||||
* @brief Creates a new Graph object
|
||||
* @param max_points Maximum number of points in the Graph
|
||||
* @param max_edges Maximum number of edges in the GRAPH (not the point)
|
||||
*/
|
||||
Graph(size_t maxPoints, size_t maxEdges) noexcept;
|
||||
|
||||
|
||||
/** Deconstructor
|
||||
* @brief Deletes all_points and all_edges from memory
|
||||
*/
|
||||
~Graph();
|
||||
|
||||
// Move constructor
|
||||
Graph(Graph&& other) noexcept;
|
||||
/** Move Constructor
|
||||
* @brief Instructions to move the current graph object from one memory location to another without copying.
|
||||
* @param new_location The new memory location to move the graph to.
|
||||
*/
|
||||
Graph(Graph&& new_location) noexcept;
|
||||
|
||||
/** Getter Function: get_points
|
||||
* @brief Gets the memory address of all of the points on given graph
|
||||
*
|
||||
* @returns The address of the first Point in the graph
|
||||
*/
|
||||
Point* get_points() noexcept;
|
||||
|
||||
/** Getter Function: get_p_edges
|
||||
* @brief Gets the address of the first edge for Point P at index p_index
|
||||
*
|
||||
* @param p_index The index (int) of the point
|
||||
* @returns Edge* : the address of the first edge for p_index
|
||||
* */
|
||||
Edge* get_p_edges(int p_index) noexcept;
|
||||
|
||||
void add_edge(int start_point_index, int end_point_index) noexcept;
|
||||
/** add_point
|
||||
* @brief Adds a point onto the graph
|
||||
*
|
||||
* @param name The name of the point.
|
||||
* @param x The width value
|
||||
* @param y The heighth value
|
||||
* @param z The depth value
|
||||
* @returns The memory location of the point
|
||||
*/
|
||||
Point* add_point(std::string name, float x, float y, float z) noexcept;
|
||||
|
||||
/** delete_point
|
||||
* @brief Removes a point from the graph
|
||||
*
|
||||
* @param p_index The index of the point to be deleted
|
||||
* @returns True on successful deletion
|
||||
*/
|
||||
bool delete_point(int p_index) noexcept;
|
||||
|
||||
/** add_edge
|
||||
* @brief Adds an edge between two points
|
||||
*
|
||||
* @param e_start : the index of the starting point
|
||||
* @param e_end : the index of the ending point
|
||||
*
|
||||
* @returns A pointer to the newly added edge
|
||||
**/
|
||||
Edge* add_edge(int e_start, int e_end, float weight) noexcept;
|
||||
|
||||
|
||||
/** delete_edge
|
||||
* @brief Deletes on edge from Point A to Point B.
|
||||
*
|
||||
* This function uses a tombstone method to decide if a point is active or just sitting in available memory
|
||||
* @param e_start The index of the starting point.
|
||||
* @param e_end The index of the ending point.
|
||||
*
|
||||
* @return true if the edge was found and deleted
|
||||
*/
|
||||
bool delete_edge(int e_start, int e_end) noexcept;
|
||||
|
||||
//Preventing mem-spikes by preventing copying and enforcing deletions.
|
||||
Graph(const Graph&) = delete;
|
||||
|
||||
Reference in New Issue
Block a user