-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
semester_tasks #13
base: main
Are you sure you want to change the base?
semester_tasks #13
Changes from 40 commits
f0bd3c8
1d846ff
d3602f6
22ffad7
3709d12
e3e497c
75d57fa
0c36f3e
3fe4cd4
22d90b8
e1ce917
310e284
4eb21cb
c84a76b
e2f2f28
7a6280a
4f1a34d
4246f4b
8c0bdf5
faa6e67
d545794
d4966ea
6aa324c
474f6a6
158b669
e202e3d
b5c3636
8221b80
d861367
1d2d204
85afe6b
ad12812
0d34e44
b7cce0e
3bf6e15
e417c49
a1bdeff
1375f5d
c3f6de7
1ea4514
89105a3
238a5a3
a91e0b3
5deaffe
8bd78f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,211 @@ | ||||||||||||||||||||||||||||||||||||||
#pragma once | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
#include <algorithm> | ||||||||||||||||||||||||||||||||||||||
#include <iostream> | ||||||||||||||||||||||||||||||||||||||
#include <memory> | ||||||||||||||||||||||||||||||||||||||
#include <set> | ||||||||||||||||||||||||||||||||||||||
#include <type_traits> | ||||||||||||||||||||||||||||||||||||||
#include <vector> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/// @brief Graphs vertex | ||||||||||||||||||||||||||||||||||||||
/// @tparam T | ||||||||||||||||||||||||||||||||||||||
template <typename T> | ||||||||||||||||||||||||||||||||||||||
struct Vertex { | ||||||||||||||||||||||||||||||||||||||
Vertex(const T &d) : data(d) {} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
T data; | ||||||||||||||||||||||||||||||||||||||
std::set<std::shared_ptr<Vertex<T>>> adjacent; | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
template <typename T, typename VT> | ||||||||||||||||||||||||||||||||||||||
concept IsVertex = std::is_base_of<Vertex<VT>, T>::value; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/// @brief Basic graph | ||||||||||||||||||||||||||||||||||||||
/// @tparam VertexType | ||||||||||||||||||||||||||||||||||||||
/// @tparam T | ||||||||||||||||||||||||||||||||||||||
template <typename VertexType, typename T> | ||||||||||||||||||||||||||||||||||||||
requires IsVertex<VertexType, T> | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: expected ';' at end of declaration [clang-diagnostic-error]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: unknown type name 'requires' [clang-diagnostic-error] requires IsVertex<VertexType, T>
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list [clang-diagnostic-error]
Suggested change
|
||||||||||||||||||||||||||||||||||||||
class Graph { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: constructor does not initialize these fields: vertices_ [cppcoreguidelines-pro-type-member-init] lib/src/graph.hpp:208: - std::vector<std::shared_ptr<VertexType>> vertices_;
+ std::vector<std::shared_ptr<VertexType>> vertices_{}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: destructor of 'Graph' is public and non-virtual [cppcoreguidelines-virtual-class-destructor] class Graph {
^ Additional contextlib/src/graph.hpp:26: make it public and virtual class Graph {
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: constructor does not initialize these fields: vertices_ [cppcoreguidelines-pro-type-member-init] lib/src/graph.hpp:209: - std::vector<std::shared_ptr<VertexType>> vertices_;
+ std::vector<std::shared_ptr<VertexType>> vertices_{}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: destructor of 'Graph' is public and non-virtual [cppcoreguidelines-virtual-class-destructor] class Graph {
^ Additional contextlib/src/graph.hpp:27: make it public and virtual class Graph {
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: constructor does not initialize these fields: vertices_ [cppcoreguidelines-pro-type-member-init] lib/src/graph.hpp:209: - std::vector<std::shared_ptr<VertexType>> vertices_;
+ std::vector<std::shared_ptr<VertexType>> vertices_{}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: destructor of 'Graph' is public and non-virtual [cppcoreguidelines-virtual-class-destructor] class Graph {
^ Additional contextlib/src/graph.hpp:27: make it public and virtual class Graph {
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: constructor does not initialize these fields: vertices_ [cppcoreguidelines-pro-type-member-init] lib/src/graph.hpp:209: - std::vector<std::shared_ptr<VertexType>> vertices_;
+ std::vector<std::shared_ptr<VertexType>> vertices_{}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: destructor of 'Graph' is public and non-virtual [cppcoreguidelines-virtual-class-destructor] class Graph {
^ Additional contextlib/src/graph.hpp:27: make it public and virtual class Graph {
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: constructor does not initialize these fields: vertices_ [cppcoreguidelines-pro-type-member-init] lib/src/graph.hpp:209: - std::vector<std::shared_ptr<VertexType>> vertices_;
+ std::vector<std::shared_ptr<VertexType>> vertices_{}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: destructor of 'Graph' is public and non-virtual [cppcoreguidelines-virtual-class-destructor] class Graph {
^ Additional contextlib/src/graph.hpp:27: make it public and virtual class Graph {
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: constructor does not initialize these fields: vertices_ [cppcoreguidelines-pro-type-member-init] lib/src/graph.hpp:209: - std::vector<std::shared_ptr<VertexType>> vertices_;
+ std::vector<std::shared_ptr<VertexType>> vertices_{}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: destructor of 'Graph' is public and non-virtual [cppcoreguidelines-virtual-class-destructor] class Graph {
^ Additional contextlib/src/graph.hpp:27: make it public and virtual class Graph {
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: constructor does not initialize these fields: vertices_ [cppcoreguidelines-pro-type-member-init] lib/src/graph.hpp:209: - std::vector<std::shared_ptr<VertexType>> vertices_;
+ std::vector<std::shared_ptr<VertexType>> vertices_{}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: destructor of 'Graph' is public and non-virtual [cppcoreguidelines-virtual-class-destructor] class Graph {
^ Additional contextlib/src/graph.hpp:27: make it public and virtual class Graph {
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: constructor does not initialize these fields: vertices_ [cppcoreguidelines-pro-type-member-init] lib/src/graph.hpp:209: - std::vector<std::shared_ptr<VertexType>> vertices_;
+ std::vector<std::shared_ptr<VertexType>> vertices_{}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: destructor of 'Graph' is public and non-virtual [cppcoreguidelines-virtual-class-destructor] class Graph {
^ Additional contextlib/src/graph.hpp:27: make it public and virtual class Graph {
^ |
||||||||||||||||||||||||||||||||||||||
public: | ||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Add a new vertex to the graph | ||||||||||||||||||||||||||||||||||||||
* @param data | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void AddVertex(const T &data) { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: unknown type name 'T' [clang-diagnostic-error] virtual void AddVertex(const T &data) {
^ |
||||||||||||||||||||||||||||||||||||||
vertices_.push_back(std::make_shared<VertexType>(data)); | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] vertices_.push_back(std::make_shared<VertexType>(data));
^ |
||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
std::shared_ptr<VertexType> operator[](size_t index) { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] std::shared_ptr<VertexType> operator[](size_t index) {
^ |
||||||||||||||||||||||||||||||||||||||
return vertices_[index]; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const std::shared_ptr<VertexType> operator[](size_t index) const { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] const std::shared_ptr<VertexType> operator[](size_t index) const {
^ |
||||||||||||||||||||||||||||||||||||||
return vertices_[index]; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Find vertexes index | ||||||||||||||||||||||||||||||||||||||
* @param vertex | ||||||||||||||||||||||||||||||||||||||
* @return size_t | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
size_t Find(const T &vertex) const { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: unknown type name 'T' [clang-diagnostic-error] size_t Find(const T &vertex) const {
^ |
||||||||||||||||||||||||||||||||||||||
size_t index; | ||||||||||||||||||||||||||||||||||||||
for (index = 0; index < vertices_.size(); ++index) | ||||||||||||||||||||||||||||||||||||||
if (vertices_[index]->data == vertex) return index; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
return index; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Remove a vertex from the graph via index | ||||||||||||||||||||||||||||||||||||||
* @param vertex_id | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void RemoveVertex(size_t vertex_id) { | ||||||||||||||||||||||||||||||||||||||
if (vertex_id >= Size()) { | ||||||||||||||||||||||||||||||||||||||
// Vertex not found | ||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Remove edges pointing to the vertex | ||||||||||||||||||||||||||||||||||||||
for (auto &v : vertices_[vertex_id]->adjacent) { | ||||||||||||||||||||||||||||||||||||||
RemoveDirEdge(v, vertices_[vertex_id]); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Remove the vertex from the graph | ||||||||||||||||||||||||||||||||||||||
vertices_.erase(vertices_.begin() + vertex_id); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Remove a vertex from the graph | ||||||||||||||||||||||||||||||||||||||
* @param vertex | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void RemoveVertex(std::shared_ptr<VertexType> vertex) { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] virtual void RemoveVertex(std::shared_ptr<VertexType> vertex) {
^ |
||||||||||||||||||||||||||||||||||||||
// Find the vertex in the graph | ||||||||||||||||||||||||||||||||||||||
auto it = std::find(vertices_.begin(), vertices_.end(), vertex); | ||||||||||||||||||||||||||||||||||||||
if (it == vertices_.end()) { | ||||||||||||||||||||||||||||||||||||||
// Vertex not found | ||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
RemoveVertex(it - vertices_.begin()); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Returns the number of vertices in the graph | ||||||||||||||||||||||||||||||||||||||
* @return size_t | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
size_t Size() const { return vertices_.size(); } | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Add a directed edge between two vertices via indices | ||||||||||||||||||||||||||||||||||||||
* @param source_id | ||||||||||||||||||||||||||||||||||||||
* @param target_id | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void AddDirEdge(size_t source_id, size_t target_id) { | ||||||||||||||||||||||||||||||||||||||
operator[](source_id)->adjacent.insert(operator[](target_id)); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Add a directed edge between two vertices | ||||||||||||||||||||||||||||||||||||||
* @param source | ||||||||||||||||||||||||||||||||||||||
* @param target | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void AddDirEdge(std::shared_ptr<VertexType> source, | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] virtual void AddDirEdge(std::shared_ptr<VertexType> source,
^ |
||||||||||||||||||||||||||||||||||||||
std::shared_ptr<VertexType> target) { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] std::shared_ptr<VertexType> target) {
^ |
||||||||||||||||||||||||||||||||||||||
source->adjacent.insert(target); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Remove a directed edge between two vertices via indices | ||||||||||||||||||||||||||||||||||||||
* @param source_id | ||||||||||||||||||||||||||||||||||||||
* @param target_id | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void RemoveDirEdge(size_t source_id, size_t target_id) { | ||||||||||||||||||||||||||||||||||||||
operator[](source_id)->adjacent.erase(*std::find( | ||||||||||||||||||||||||||||||||||||||
operator[](source_id)->adjacent.begin(), | ||||||||||||||||||||||||||||||||||||||
operator[](source_id)->adjacent.end(), operator[](target_id))); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Remove a directed edge between two vertices | ||||||||||||||||||||||||||||||||||||||
* @param source | ||||||||||||||||||||||||||||||||||||||
* @param target | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void RemoveDirEdge(std::shared_ptr<VertexType> source, | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] virtual void RemoveDirEdge(std::shared_ptr<VertexType> source,
^ |
||||||||||||||||||||||||||||||||||||||
std::shared_ptr<VertexType> target) { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] std::shared_ptr<VertexType> target) {
^ |
||||||||||||||||||||||||||||||||||||||
source->adjacent.erase( | ||||||||||||||||||||||||||||||||||||||
*std::find(source->adjacent.begin(), source->adjacent.end(), target)); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Add a non-directed edge between two vertices via indices | ||||||||||||||||||||||||||||||||||||||
* @param first_id | ||||||||||||||||||||||||||||||||||||||
* @param second_id | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void AddEdge(size_t first_id, size_t second_id) { | ||||||||||||||||||||||||||||||||||||||
AddDirEdge(first_id, second_id); | ||||||||||||||||||||||||||||||||||||||
AddDirEdge(second_id, first_id); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Add a non-directed edge between two vertices | ||||||||||||||||||||||||||||||||||||||
* @param vertex_1 | ||||||||||||||||||||||||||||||||||||||
* @param vertex_2 | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void AddEdge(std::shared_ptr<VertexType> vertex_1, | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] virtual void AddEdge(std::shared_ptr<VertexType> vertex_1,
^ |
||||||||||||||||||||||||||||||||||||||
std::shared_ptr<VertexType> vertex_2) { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] std::shared_ptr<VertexType> vertex_2) {
^ |
||||||||||||||||||||||||||||||||||||||
AddDirEdge(vertex_1, vertex_2); | ||||||||||||||||||||||||||||||||||||||
AddDirEdge(vertex_2, vertex_1); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Remove a non-directed edge between two vertices via indices | ||||||||||||||||||||||||||||||||||||||
* @param first_id | ||||||||||||||||||||||||||||||||||||||
* @param second_id | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void RemoveEdge(size_t first_id, size_t second_id) { | ||||||||||||||||||||||||||||||||||||||
RemoveDirEdge(first_id, second_id); | ||||||||||||||||||||||||||||||||||||||
RemoveDirEdge(second_id, first_id); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Remove a non-directed edge between two vertices | ||||||||||||||||||||||||||||||||||||||
* @param vertex_1 | ||||||||||||||||||||||||||||||||||||||
* @param vertex_2 | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void RemoveEdge(std::shared_ptr<VertexType> vertex_1, | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] virtual void RemoveEdge(std::shared_ptr<VertexType> vertex_1,
^ |
||||||||||||||||||||||||||||||||||||||
std::shared_ptr<VertexType> vertex_2) { | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] std::shared_ptr<VertexType> vertex_2) {
^ |
||||||||||||||||||||||||||||||||||||||
RemoveDirEdge(vertex_1, vertex_2); | ||||||||||||||||||||||||||||||||||||||
RemoveDirEdge(vertex_2, vertex_1); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||
* @brief | ||||||||||||||||||||||||||||||||||||||
* Print the adjacency list of the graph | ||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||
virtual void PrintGraph() const { | ||||||||||||||||||||||||||||||||||||||
for (const auto &vertex : vertices_) { | ||||||||||||||||||||||||||||||||||||||
std::cout << vertex->data << " -> "; | ||||||||||||||||||||||||||||||||||||||
for (const auto &neighbor : vertex->adjacent) { | ||||||||||||||||||||||||||||||||||||||
std::cout << neighbor->data << " "; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
std::cout << std::endl; | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use 'std::endl' with streams; use '\n' instead [performance-avoid-endl]
Suggested change
|
||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
protected: | ||||||||||||||||||||||||||||||||||||||
std::vector<std::shared_ptr<VertexType>> vertices_; | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes] std::vector<std::shared_ptr<VertexType>> vertices_;
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes] std::vector<std::shared_ptr<VertexType>> vertices_;
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use of undeclared identifier 'VertexType' [clang-diagnostic-error] std::vector<std::shared_ptr<VertexType>> vertices_;
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes] std::vector<std::shared_ptr<VertexType>> vertices_;
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes] std::vector<std::shared_ptr<VertexType>> vertices_;
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes] std::vector<std::shared_ptr<VertexType>> vertices_;
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes] std::vector<std::shared_ptr<VertexType>> vertices_;
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: member variable 'vertices_' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes] std::vector<std::shared_ptr<VertexType>> vertices_;
^ |
||||||||||||||||||||||||||||||||||||||
}; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "utils.hpp" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#pragma once | ||
|
||
#include <limits> | ||
|
||
#include "graph.hpp" | ||
|
||
/// @brief Basic weighted graph | ||
/// @tparam VertexType | ||
/// @tparam T | ||
template <typename VertexType, typename T> | ||
class WeightedGraph : public Graph<VertexType, T> { | ||
public: | ||
WeightedGraph() : Graph<VertexType, T>() { | ||
weights.resize(Graph<VertexType, T>::vertices_.size(), | ||
std::vector<int>(Graph<VertexType, T>::vertices_.size(), | ||
std::numeric_limits<int>::max())); | ||
} | ||
|
||
/** | ||
* @brief | ||
* Override AddVertex to handle weights matrix resizing | ||
* @param data | ||
*/ | ||
void AddVertex(const T& data) override { | ||
Graph<VertexType, T>::AddVertex(data); | ||
weights.resize(Graph<VertexType, T>::vertices_.size(), | ||
std::vector<int>(Graph<VertexType, T>::vertices_.size(), | ||
std::numeric_limits<int>::max())); | ||
for (auto& row : weights) | ||
row.resize(Graph<VertexType, T>::vertices_.size(), | ||
std::numeric_limits<int>::max()); | ||
} | ||
|
||
/** | ||
* @brief | ||
* Override RemoveVertex via index to handle weights matrix resizing | ||
* @param vertex_id | ||
*/ | ||
void RemoveVertex(size_t vertex_id) override { | ||
// Remove the vertex from the base class | ||
Graph<VertexType, T>::RemoveVertex(vertex_id); | ||
|
||
// Remove the corresponding row and column from the weights matrix | ||
weights.erase(weights.begin() + vertex_id); // Remove the row | ||
|
||
for (auto& row : weights) | ||
row.erase(row.begin() + vertex_id); // Remove the column | ||
} | ||
|
||
/** | ||
* @brief | ||
* Add a weighted directed edge | ||
* @param source_id | ||
* @param target_id | ||
* @param weight | ||
*/ | ||
void AddDirEdge(size_t source_id, size_t target_id, int weight) { | ||
Graph<VertexType, T>::AddDirEdge(source_id, target_id); | ||
weights[source_id][target_id] = weight; | ||
} | ||
|
||
/** | ||
* @brief | ||
* Add a weighted undirected edge | ||
* @param source_id | ||
* @param target_id | ||
* @param weight | ||
*/ | ||
void AddEdge(size_t source_id, size_t target_id, int weight) { | ||
AddDirEdge(source_id, target_id, weight); | ||
AddDirEdge(target_id, source_id, weight); | ||
} | ||
|
||
/** | ||
* @brief | ||
* Remove a weighted directed edge | ||
* @param source_id | ||
* @param target_id | ||
*/ | ||
void RemoveDirEdge(size_t source_id, size_t target_id) override { | ||
Graph<VertexType, T>::RemoveDirEdge(source_id, target_id); | ||
weights[source_id][target_id] = std::numeric_limits<int>::max(); | ||
} | ||
|
||
/** | ||
* @brief | ||
* Remove a weighted undirected edge | ||
* @param source_id | ||
* @param target_id | ||
*/ | ||
void RemoveEdge(size_t source_id, size_t target_id) override { | ||
RemoveDirEdge(source_id, target_id); | ||
RemoveDirEdge(target_id, source_id); | ||
} | ||
|
||
/** | ||
* @brief | ||
* Get the weight of an edge | ||
* @param source_id | ||
* @param target_id | ||
* @return int | ||
*/ | ||
int GetWeight(size_t source_id, size_t target_id) const { | ||
return weights[source_id][target_id]; | ||
} | ||
|
||
/** | ||
* @brief | ||
* Reweight an edge | ||
* @param source_id | ||
* @param target_id | ||
* @param new_weight | ||
*/ | ||
void Reweight(size_t source_id, size_t target_id, int new_weight) { | ||
weights[source_id][target_id] = new_weight; | ||
} | ||
|
||
/** | ||
* @brief | ||
* Print the weighted graph | ||
*/ | ||
void PrintGraph() const override { | ||
for (size_t i = 0; i < Graph<VertexType, T>::vertices_.size(); ++i) { | ||
std::cout << Graph<VertexType, T>::vertices_[i]->data << " -> "; | ||
for (const auto& neighbor : | ||
Graph<VertexType, T>::vertices_[i]->adjacent) { | ||
size_t j = Graph<VertexType, T>::Find(neighbor->data); | ||
std::cout << "(" << neighbor->data << ", " << weights[i][j] << ") "; | ||
} | ||
std::cout << std::endl; | ||
} | ||
} | ||
|
||
private: | ||
std::vector<std::vector<int>> weights; | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1 +1,25 @@ | ||||||||||||||||||||||
int main() { return 0; } | ||||||||||||||||||||||
#include "packman.hpp" | ||||||||||||||||||||||
|
||||||||||||||||||||||
int main() { | ||||||||||||||||||||||
DependencyGraph dg; | ||||||||||||||||||||||
|
||||||||||||||||||||||
std::vector<std::string> packages = {"Lib_1", "Lib_2", "Lib_3", "Lib_4", | ||||||||||||||||||||||
"Lib_5"}; | ||||||||||||||||||||||
for (const auto& package : packages) dg.AddVertex(package); | ||||||||||||||||||||||
|
||||||||||||||||||||||
dg.AddDirEdge(0, 1); // Lib_1 depends on Lib_2 | ||||||||||||||||||||||
dg.AddDirEdge(0, 2); // Lib_1 depends on Lib_3 | ||||||||||||||||||||||
dg.AddDirEdge(2, 3); // Lib_3 depends on Lib_4 | ||||||||||||||||||||||
dg.AddDirEdge(3, 1); // Lib_4 depends on Lib_2 | ||||||||||||||||||||||
dg.AddDirEdge(0, 3); // Lib_1 depends on Lib_4 | ||||||||||||||||||||||
|
||||||||||||||||||||||
PackageManager packman(dg); | ||||||||||||||||||||||
|
||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'packman' of type 'PackageManager' can be declared 'const' [misc-const-correctness]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'packman' of type 'PackageManager' can be declared 'const' [misc-const-correctness]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'packman' of type 'PackageManager' can be declared 'const' [misc-const-correctness]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'packman' of type 'PackageManager' can be declared 'const' [misc-const-correctness]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'packman' of type 'PackageManager' can be declared 'const' [misc-const-correctness]
Suggested change
|
||||||||||||||||||||||
packman.FindDownloadingOrder("Lib_1"); | ||||||||||||||||||||||
packman.FindDownloadingOrder("Lib_2"); | ||||||||||||||||||||||
packman.FindDownloadingOrder("Lib_3"); | ||||||||||||||||||||||
packman.FindDownloadingOrder("Lib_4"); | ||||||||||||||||||||||
packman.FindDownloadingOrder("Lib_5"); | ||||||||||||||||||||||
|
||||||||||||||||||||||
return 0; | ||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: unknown type name 'concept' [clang-diagnostic-error]
concept IsVertex = std::is_base_of<Vertex<VT>, T>::value; ^