generated from embedded-dev-research/cpp-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
added a graph class #17
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
Open
Lepsps
wants to merge
17
commits into
embedded-dev-research:main
Choose a base branch
from
Lepsps:shekhirev_vlad
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ac67f77
added a graph class
5f36230
gtest dir fix
4867937
CMake fix 1.0
fb57554
CMake fix 2.0
f2afeea
CMake fix 2.0
82f0dc4
clang-tidy, clang-format and ubuntu-build fix 1.0
2f1141a
clang-tidy, clang-format, ubuntu-build and cmake fix 2.0
fdfe528
CMake, builts, clang format/tidy fix 3.0
116cf4a
tensor add 1.0
55a9ee8
graph pr fix
dfb2fd9
codecov fix
74aa3be
codecov fix 2
e9c92bf
tests fix
375119f
clang-format fix
7ae4dce
clang-format fix
9b79f08
clang-format fix
38a2cbf
build fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
cmake_minimum_required(VERSION 3.20) | ||
cmake_minimum_required(VERSION 3.15) | ||
|
||
project(cpp_template) | ||
set(ProjectName "itlab") | ||
|
||
include(cmake/configure.cmake) | ||
project(${ProjectName}) | ||
|
||
include_directories(include) | ||
|
||
enable_testing() | ||
|
||
add_subdirectory(3rdparty) | ||
add_subdirectory(app) | ||
add_subdirectory(include) | ||
add_subdirectory(src) | ||
add_subdirectory(3rdparty/googletest/gtest) | ||
add_subdirectory(src/graph) | ||
add_subdirectory(test) | ||
|
||
# REPORT | ||
message( STATUS "") | ||
message( STATUS "General configuration for ${PROJECT_NAME}") | ||
message( STATUS "======================================") | ||
message( STATUS "") | ||
message( STATUS " Configuration: ${CMAKE_BUILD_TYPE}") | ||
message( STATUS "") |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef GRAPH_H | ||
#define GRAPH_H | ||
|
||
#include <iostream> | ||
#include <unordered_map> | ||
#include <list> | ||
#include <queue> | ||
|
||
class Vertex { | ||
private: | ||
int id; | ||
std::list<int> neighbors; | ||
|
||
public: | ||
Vertex(int id); | ||
void addNeighbor(int neighbor); | ||
void removeNeighbor(int neighbor); | ||
void print() const; | ||
int getId() const; | ||
const std::list<int>& getNeighbors() const; | ||
}; | ||
|
||
|
||
|
||
class Graph { | ||
private: | ||
std::unordered_map<int, Vertex*> vertices; | ||
|
||
public: | ||
|
||
Graph(); | ||
|
||
void addVertex(int id); | ||
void getVertex() const; | ||
void addEdge(int u, int v); | ||
void removeEdge(int u, int v); | ||
void removeVertex(int id); | ||
int vertexCount() const; | ||
int edgeCount() const; | ||
bool empty() const; | ||
void printGraph() const; | ||
bool hasPath(int u, int v); | ||
std::vector<int> BFS(int start); | ||
~Graph(); | ||
}; | ||
|
||
#endif |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
file(GLOB_RECURSE HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h") | ||
file(GLOB_RECURSE SOURCE_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp") | ||
|
||
add_library(${ProjectName} STATIC ${SOURCE_FILES} ${HEADER_FILES}) | ||
target_sources(${ProjectName} PRIVATE ${HEADER_FILES}) | ||
|
||
target_include_directories(${ProjectName} PUBLIC ${CMAKE_SOURCE_DIR}/src) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#include "./graph/graph.h" | ||
|
||
Vertex::Vertex(int id) : id(id) {} | ||
|
||
void Vertex::addNeighbor(int neighbor) { | ||
if (neighbor != id) { | ||
neighbors.push_back(neighbor); | ||
} | ||
} | ||
|
||
void Vertex::removeNeighbor(int neighbor) { | ||
neighbors.remove(neighbor); | ||
} | ||
|
||
void Vertex::print() const { | ||
std::cout << id << ": "; | ||
for (const int& neighbor : neighbors) { | ||
std::cout << neighbor << " "; | ||
} | ||
std::cout << std::endl; | ||
} | ||
|
||
int Vertex::getId() const { | ||
return id; | ||
} | ||
|
||
const std::list<int>& Vertex::getNeighbors() const { | ||
return neighbors; | ||
} | ||
|
||
Graph::Graph() {} | ||
|
||
void Graph::addVertex(int id) { | ||
if (vertices.find(id) == vertices.end()) { | ||
vertices[id] = new Vertex(id); | ||
} | ||
} | ||
|
||
void Graph::getVertex() const { | ||
if (!this->empty()) { | ||
for (auto it = vertices.begin(); it != vertices.end(); it++) | ||
std::cout << it->first << " "; | ||
std::cout << std::endl; | ||
} | ||
} | ||
|
||
void Graph::addEdge(int u, int v) { | ||
if (vertices.find(u) == vertices.end()) { addVertex(u); } | ||
if (vertices.find(v) == vertices.end()) { addVertex(v); } | ||
vertices[u]->addNeighbor(v); | ||
} | ||
|
||
void Graph::removeEdge(int u, int v) { | ||
if (vertices.find(u) != vertices.end()) { | ||
vertices[u]->removeNeighbor(v); | ||
} | ||
} | ||
|
||
void Graph::removeVertex(int id) { | ||
for (auto& pair : vertices) { pair.second->removeNeighbor(id); } | ||
auto it = vertices.find(id); | ||
if (it != vertices.end()) { | ||
delete it->second; | ||
vertices.erase(it); | ||
} | ||
} | ||
|
||
int Graph::vertexCount() const { | ||
int count = 0; | ||
for (auto it = vertices.begin(); it != vertices.end(); it++) | ||
count++; | ||
return count; | ||
} | ||
|
||
int Graph::edgeCount() const { | ||
int count = 0; | ||
for (auto it = vertices.begin(); it != vertices.end(); it++) { | ||
count += (it->second->getNeighbors()).size(); | ||
} | ||
return count; | ||
} | ||
|
||
|
||
bool Graph::empty() const { | ||
return vertices.empty(); | ||
} | ||
|
||
void Graph::printGraph() const { | ||
for (const auto& pair : vertices) { | ||
pair.second->print(); | ||
} | ||
} | ||
|
||
bool Graph::hasPath(int u, int v) { | ||
if (vertices.find(u) != vertices.end() && vertices.find(v) != vertices.end()) { | ||
std::unordered_map <int, bool> visited; | ||
std::queue<int> queue; | ||
queue.push(u); | ||
|
||
while (!queue.empty()) { | ||
int current = queue.front(); | ||
queue.pop(); | ||
if (current == v) { return true; } | ||
visited[current] = true; | ||
|
||
for (const int& neighbor : vertices[current]->getNeighbors()) | ||
if (!visited[neighbor]) | ||
queue.push(neighbor); | ||
|
||
} | ||
return false; | ||
|
||
} | ||
} | ||
|
||
std::vector<int> Graph::BFS(int start) { | ||
std::vector<int> traversal_order; | ||
std::unordered_map<int, bool> visited; | ||
std::queue<int> queue; | ||
|
||
visited[start] = true; | ||
queue.push(start); | ||
|
||
while (!queue.empty()) { | ||
int current = queue.front(); | ||
queue.pop(); | ||
traversal_order.push_back(current); | ||
|
||
if (vertices.find(current) != vertices.end()) { | ||
for (const int& neighbor : vertices[current]->getNeighbors()) { | ||
if (!visited[neighbor]) { | ||
visited[neighbor] = true; | ||
queue.push(neighbor); | ||
} | ||
} | ||
} | ||
} | ||
//for (int i : traversal_order) | ||
// std::cout << i << " "; | ||
|
||
return traversal_order; | ||
} | ||
|
||
Graph::~Graph() { | ||
for (auto& pair : vertices) { | ||
delete pair.second; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
file(GLOB_RECURSE TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) | ||
file(GLOB_RECURSE TEST_FILES ./*.cpp) | ||
|
||
add_executable(run_tests ${TEST_SRC_FILES}) | ||
target_link_libraries(run_tests PUBLIC | ||
gtest_main | ||
) | ||
set(TestsName "Tests") | ||
|
||
add_executable(${TestsName} ${TEST_FILES}) | ||
|
||
target_link_libraries(${TestsName} PRIVATE ${ProjectName} gtest) | ||
|
||
enable_testing() | ||
add_test(NAME ${TestsName} COMMAND ${TestsName}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#include <gtest/gtest.h> | ||
#include <gtest.h> | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Does it need to be lowered?