-
Notifications
You must be signed in to change notification settings - Fork 23
Homework #6
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
base: main
Are you sure you want to change the base?
Homework #6
Changes from all commits
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,126 @@ | ||||||
#ifndef __GRAPH_H__ | ||||||
#define __GRAPH_H__ | ||||||
|
||||||
#include <unordered_map> | ||||||
#include <vector> | ||||||
#include <initializer_list> | ||||||
#include <algorithm> | ||||||
#include <iostream> | ||||||
|
||||||
template<typename T, typename NameType> | ||||||
class Graph { | ||||||
static_assert(std::is_arithmetic<T>::value, "Graph: paths' lenghts must be arithmetic!"); | ||||||
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: no header providing "std::is_arithmetic" is directly included [misc-include-cleaner] lib/src/graph.cpp:3: - #include <unordered_map>
+ #include <type_traits>
+ #include <unordered_map> |
||||||
class node { | ||||||
public: | ||||||
node(NameType name) : name{name}, paths{} {} | ||||||
NameType name{NameType()}; | ||||||
std::unordered_map<node*, T> paths; | ||||||
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 default member initializer for 'paths' [cppcoreguidelines-use-default-member-init] lib/src/graph.cpp:14: - node(NameType name) : name{name}, paths{} {}
+ node(NameType name) : name{name}, {}
Suggested change
|
||||||
|
||||||
void descr_path(node* n, T lenght = 1) { | ||||||
paths[n] = lenght; | ||||||
} | ||||||
|
||||||
~node() { | ||||||
paths = {}; | ||||||
name = NameType(); | ||||||
} | ||||||
}; | ||||||
|
||||||
public: | ||||||
|
||||||
Graph(std::initializer_list<std::pair<NameType, NameType>> links, T base_lenght = 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: no header providing "std::pair" is directly included [misc-include-cleaner] lib/src/graph.cpp:4: - #include <vector>
+ #include <utility>
+ #include <vector> |
||||||
add_paths(links, base_lenght); | ||||||
} | ||||||
|
||||||
Graph(std::initializer_list<std::pair<std::pair<NameType, NameType>, T>> links) { | ||||||
add_paths(links); | ||||||
} | ||||||
|
||||||
node* find_via_name(NameType name) { | ||||||
auto ans = std::find_if(nodes.begin(), nodes.end(), [&name](const node* x){ return x->name == name; } ); | ||||||
if (ans == nodes.end()) { | ||||||
return nullptr; | ||||||
} | ||||||
return *ans; | ||||||
} | ||||||
|
||||||
node* operator[](NameType name) { | ||||||
find_via_name(name); | ||||||
} | ||||||
|
||||||
void add_paths(std::initializer_list<std::pair<NameType, NameType>> links, T base_lenght = 1) { | ||||||
/* { {Name1, Name2}, ... }*/ | ||||||
for (auto i: links) { | ||||||
node* first = find_via_name(i.first); | ||||||
if (! first ) { | ||||||
first = new node{i.first}; | ||||||
nodes.push_back(first); | ||||||
} | ||||||
node* second = find_via_name(i.second); | ||||||
if (! second && first != second){ | ||||||
second = new node{i.second}; | ||||||
nodes.push_back(second); | ||||||
} | ||||||
first->descr_path(second, base_lenght); | ||||||
continue; | ||||||
} | ||||||
} | ||||||
|
||||||
void add_paths(std::initializer_list<std::pair<std::pair<NameType, NameType>, T>> links) { | ||||||
/* { { Name1, Name2 }, lenght }, ... } | ||||||
*/ | ||||||
for (auto i: links) { | ||||||
node* first = find_via_name(i.first.first); | ||||||
if (! first ) { | ||||||
first = new node{i.first.first}; | ||||||
nodes.push_back(first); | ||||||
} | ||||||
node* second = find_via_name(i.first.second); | ||||||
if (! second && first != second){ | ||||||
second = new node{i.first.second}; | ||||||
nodes.push_back(second); | ||||||
} | ||||||
first->descr_path(second, i.second); | ||||||
continue; | ||||||
} | ||||||
} | ||||||
|
||||||
~Graph() { | ||||||
for (node* i : nodes) { | ||||||
delete i; | ||||||
} | ||||||
nodes = {}; | ||||||
} | ||||||
|
||||||
void see_vertical() { | ||||||
for (auto i : nodes) { | ||||||
if (i->paths.size() == 0) { | ||||||
std::cout << i->name << std::endl; | ||||||
continue; | ||||||
} | ||||||
for (auto k : i->paths) { | ||||||
std::cout << i->name << " -> " << k.first->name << ", l:" << k.second << std::endl; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
friend std::ostream& operator<< (std::ostream& os, const Graph<T, NameType>& g) { | ||||||
os << "Graph( "; | ||||||
for (auto i : g.nodes) { | ||||||
if (i->paths.size() == 0) { | ||||||
os << "{" << i->name << "}"; | ||||||
continue; | ||||||
} | ||||||
for (auto k : i->paths) { | ||||||
os << "{" << i->name << " -> " << k.first->name << ", l:" << k.second << "}, "; | ||||||
} | ||||||
} | ||||||
os << ")"; | ||||||
return os; | ||||||
} | ||||||
|
||||||
private: | ||||||
std::vector<node*> nodes; | ||||||
}; | ||||||
|
||||||
#endif |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1 @@ | ||||||
#include <stack> | ||||||
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: included header stack is not used directly [misc-include-cleaner]
Suggested change
|
This file was deleted.
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: declaration uses identifier 'GRAPH_H', which is a reserved identifier [bugprone-reserved-identifier]