On branch main
Changes to be committed: new file: Graph.cpp 1) Moved the actual declarations of functions from Graph.h into this file 2) Added get_p_edges() which will get all the edges from a specified point p 3) Added add_edge() which adds an edge between two points (start and end) modified: graph.h 1) Created initial function, constructor, and deconstructor declarations 2) Defined Point structure 3) Defined Edge structure modified: main.cpp 1) Removed any old code from my last project. I am starting clean on this file.
This commit is contained in:
96
Graph.cpp
Normal file
96
Graph.cpp
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
#include "Graph.h"
|
||||||
|
|
||||||
|
// Begin : Construction & Deconstruction
|
||||||
|
|
||||||
|
/** Constructor
|
||||||
|
* 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::Graph(size_t max_points, size_t max_edges) noexcept
|
||||||
|
{
|
||||||
|
all_points = new(std::nothrow) Point[max_points] {};
|
||||||
|
all_edges = new(std::nothrow) Edge[max_edges] {};
|
||||||
|
|
||||||
|
if(all_points && all_edges)
|
||||||
|
{
|
||||||
|
p_capacity = max_points;
|
||||||
|
e_capacity = max_edges;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**Deconstruction
|
||||||
|
* NOTE on the '~Graph()':
|
||||||
|
* The ~ flags this block as a deconstructor. It makes it so this runs before the object is removed from memory preventing memory leaks
|
||||||
|
* Find the memory buffers pointed to by all_points and all_edges, and mark that memory as available for other programs to use.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
Graph::~Graph()
|
||||||
|
{
|
||||||
|
delete[] all_points;
|
||||||
|
delete[] all_edges;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Move Constructor
|
||||||
|
* Instructions to move the current graph object from one memory location to another without copying
|
||||||
|
*/
|
||||||
|
Graph::Graph(Graph&& other) noexcept
|
||||||
|
: all_points(std::exchange(other.all_points, nullptr)),
|
||||||
|
all_edges(std::exchange(other.all_edges, nullptr)),
|
||||||
|
p_capacity(std::exchange(other.p_capacity, 0)),
|
||||||
|
e_capacity(std::exchange(other.e_capacity, 0)) {}
|
||||||
|
// End : Construction & Deconstruction
|
||||||
|
|
||||||
|
|
||||||
|
// Begin : Getters
|
||||||
|
|
||||||
|
/** Getter Function: get_p_edges
|
||||||
|
* 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* Graph::get_p_edges(int p_index) noexcept
|
||||||
|
{
|
||||||
|
//Checking for for index validity and that the point has edges
|
||||||
|
if(p_index < 0 || (size_t)p_index >= p_capacity) return nullptr;
|
||||||
|
|
||||||
|
//assigning the start_point_index to be the first_edge in the points array of edges
|
||||||
|
//if it's negative one, there are not edges assigned
|
||||||
|
int start_point_index = all_points[p_index].first_edge;
|
||||||
|
if(start_point_index == -1) return nullptr;
|
||||||
|
|
||||||
|
//if there are edges it returns the address of the first edge so that the user can iterate through them like an array
|
||||||
|
return &all_edges[start_point_index];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Begin : Setters
|
||||||
|
/** add_edge
|
||||||
|
* Adds an edge between two points
|
||||||
|
*
|
||||||
|
* @param start_point_index : the index of the starting point
|
||||||
|
* @param end_point_index : the index of the ending point
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
void Graph::add_edge(int start_point_index, int end_point_index) noexcept
|
||||||
|
{
|
||||||
|
if (next_possible_edge >= e_capacity) return; // Buffer full
|
||||||
|
|
||||||
|
// 1. If this is the first edge for this point, record the start index
|
||||||
|
if (all_points[start_point_index].e_count == 0)
|
||||||
|
{
|
||||||
|
all_points[end_point_index].first_edge = next_possible_edge;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Place the edge in the flat buffer
|
||||||
|
all_edges[next_possible_edge].target_point = end_point_index;
|
||||||
|
|
||||||
|
// 3. Update counters
|
||||||
|
all_points[start_point_index].e_count++;
|
||||||
|
next_possible_edge++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// End : Setters
|
||||||
76
graph.h
76
graph.h
@@ -1,51 +1,41 @@
|
|||||||
#pragma once
|
#ifndef GRAPH_H
|
||||||
#include <iostream>
|
#define GRAPH_H
|
||||||
#include <random>
|
#include <new>
|
||||||
#include <vector>
|
#include <utility>
|
||||||
|
|
||||||
using namespace std;
|
struct Point {
|
||||||
|
float x, y, z;
|
||||||
|
int first_edge = -1;
|
||||||
|
int e_count = 0;
|
||||||
|
};
|
||||||
|
|
||||||
class graph {
|
struct Edge {
|
||||||
|
int target_point;
|
||||||
struct Point {
|
float e_weight;
|
||||||
private:
|
};
|
||||||
string name;
|
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
|
|
||||||
vector<Point*> edges[5];
|
|
||||||
|
|
||||||
Point() : name("Unnamed"), x(0), y(0) {};
|
|
||||||
Point(string new_name, int new_x, int new_y) : name(new_name), x(new_x), y(new_y) {};
|
|
||||||
Point(string new_name, int new_x, int new_y, Point* edge) : name(new_name), x(new_x), y(new_y) {
|
|
||||||
edges->push_back(edge);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
class Graph {
|
||||||
private:
|
private:
|
||||||
Point* start;
|
Point* all_points = nullptr;
|
||||||
Point* end;
|
Edge* all_edges = nullptr;
|
||||||
|
size_t next_possible_edge = 0;
|
||||||
|
size_t p_capacity = 0;
|
||||||
|
size_t e_capacity = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//class constructor
|
Graph(size_t maxPoints, size_t maxEdges) noexcept;
|
||||||
graph() : start(nullptr), end(nullptr) {};
|
~Graph();
|
||||||
|
|
||||||
|
// Move constructor
|
||||||
|
Graph(Graph&& other) noexcept;
|
||||||
|
|
||||||
|
Edge* get_p_edges(int p_index) noexcept;
|
||||||
|
|
||||||
int rand_int() {};
|
void add_edge(int start_point_index, int end_point_index) noexcept;
|
||||||
void del_all_data(){};
|
|
||||||
void del_element(int){};
|
//Preventing mem-spikes by preventing copying and enforcing deletions.
|
||||||
void insert(int, int, vector<Point*>){};
|
Graph(const Graph&) = delete;
|
||||||
void gen_list(int){};
|
Graph& operator=(const Graph&) = delete;
|
||||||
void print_graph(){};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
//Setters
|
|
||||||
int rand_int() {
|
|
||||||
random_device rd;
|
|
||||||
mt19937 gen(rd());
|
|
||||||
uniform_int_distribution<int> dist(1, 30);
|
|
||||||
return dist(gen);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Mutators
|
|
||||||
|
|
||||||
};
|
|
||||||
54
main.cpp
54
main.cpp
@@ -1,60 +1,10 @@
|
|||||||
// Linked List Cycle.cpp :
|
// Linked List Cycle.cpp :
|
||||||
|
|
||||||
#include "d_linked_list.h"
|
#include "Graph.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
d_linked_list list = d_linked_list();
|
|
||||||
string input;
|
|
||||||
while (true) {
|
|
||||||
cout
|
|
||||||
<<"Instructions: " << endl
|
|
||||||
<< "pop" << endl
|
|
||||||
<< "push" << endl
|
|
||||||
<< "delete" << endl
|
|
||||||
<< "insert" << endl
|
|
||||||
<< "generate" << endl
|
|
||||||
<< "sort" << endl
|
|
||||||
<< "print" << endl
|
|
||||||
<< "erase" << endl
|
|
||||||
<< "quit" << endl
|
|
||||||
<< ": ";
|
|
||||||
cin >> input;
|
|
||||||
cout << endl;
|
|
||||||
|
|
||||||
if (input == "pop") {
|
|
||||||
list.pop();
|
|
||||||
}
|
|
||||||
else if (input == "push") {
|
|
||||||
cout << "Input: ";
|
|
||||||
int target;
|
|
||||||
cin >> target;
|
|
||||||
list.push(target);
|
|
||||||
}
|
|
||||||
else if (input == "delete") {
|
|
||||||
int target;
|
|
||||||
cout << "Enter element: ";
|
|
||||||
cin >> target;
|
|
||||||
list.del_element(target);
|
|
||||||
}
|
|
||||||
else if (input == "quit") {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if (input == "print") {
|
|
||||||
list.print_data();
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (input == "erase") {
|
|
||||||
list.del_all_data();
|
|
||||||
input = "";
|
|
||||||
}
|
|
||||||
else if (input == "generate") {
|
|
||||||
int num_items;
|
|
||||||
cout << "Enter number of items: ";
|
|
||||||
cin >> num_items;
|
|
||||||
list.gen_list(num_items);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user