From a5ba112a8358882a4e25967cc202801e48b8d782 Mon Sep 17 00:00:00 2001 From: denes Date: Tue, 2 Jul 2019 11:45:02 +0200 Subject: [PATCH] Graph is read as input parameter instead of randomly generated --- CMakeLists.txt | 4 +-- README.md | 10 +++++-- main.cpp | 80 ++++++++++++++++++-------------------------------- 3 files changed, 37 insertions(+), 57 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d42ed7e..cc1c47e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,10 @@ cmake_minimum_required (VERSION 2.6) project (PROJECT_GRAPH_BROWSER) +set(CMAKE_EXPORT_COMPILE_COMMANDS, 1) set (CXX_FLAGS "-std=c++14 -ggdb") add_definitions(${CXX_FLAGS}) -set (CMAKE_CXX_COMPILER "/usr/bin/g++-5.1.0") -# set (CMAKE_CXX_COMPILER "/usr/bin/clang++-3.6.0") - add_executable (graph_browser main.cpp graph_browser.cpp) include_directories(graph/lib/) target_link_libraries(graph_browser ncurses menu) diff --git a/README.md b/README.md index 9cb632b..fa7c332 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,16 @@ Graph broswing and editing in text mode with ncurses Using 3 windows: - 1. The current node's neighbours, move up/down to inspect, + 1. The current node's neighbours, move up/down to inspect, back/forward to move back in history/set the selected node as the current. 2. Vertically listed neighbours of the selected node. 3. Neighbours of neighbours of the selected node, comma separated list. - + Todo: * insert/delete node * add/remove edge - + +To fetch the submodule graph after git clone, issue: +git submodule update --init --recursive +to update: +git submodule update --recursive --remote diff --git a/main.cpp b/main.cpp index 9626bca..a550a88 100644 --- a/main.cpp +++ b/main.cpp @@ -1,58 +1,14 @@ -#include - -#include +#include +// #include #include +#include +#include + #include #include "graph_browser.hpp" -std::string generateRandomString(size_t length) -{ - const char charset[] = - "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; - const size_t max_index = (sizeof(charset) - 1); - - std::string str(length,0); - for (size_t i = 0; i < length; ++i) { - const size_t r = rand() % max_index; - str[i] = charset[r]; - } - return str; -} - -std::vector generateRandomStrings(size_t number_of_strings, - size_t min_length, - size_t max_length) -{ - std::vector v; - v.resize(number_of_strings); - for (size_t i = 0; i < number_of_strings; ++i) { - const size_t l = min_length + rand()%(max_length - min_length); - v[i] = generateRandomString(l); - } - return v; -} - -Graph generateRandomGraph(size_t number_of_vertices, - size_t number_of_edges, - size_t vertex_text_min_length, - size_t vertex_text_max_length) -{ - Graph graph; - const std::vector v = generateRandomStrings(number_of_vertices, - vertex_text_min_length, - vertex_text_max_length); - for (size_t i = 0; i < number_of_edges; ++i) { - const std::string source = v[rand()%number_of_vertices]; - const std::string destination = v[rand()%number_of_vertices]; - graph.addEdge(source, destination); - } - return graph; -} - std::function callback_wrapper; void callback_function( int value ) @@ -60,14 +16,34 @@ void callback_function( int value ) callback_wrapper(value); } +// templateT conveter(T){}; +// template<>std::string conveter(const std::string& s){retun s;} +inline std::string atoa(const std::string s) +{ + return s; +} + int main(int argc, char* argv[]) { - Graph g = generateRandomGraph(10, 200, 5, 20); + if (argc != 2) { + std::cout << "Usage: " << argv[0] << " [graph file]" << std::endl; + return 1; + } + const std::string graph_file(argv[1]); + Graph g; + try { + g = readGraphFromPlainText(graph_file, atoa); + } catch (std::runtime_error &e) { + std::cerr << e.what() << std::endl; + return 1; + } GraphBrowser::init(); { GraphBrowser gb(g); - gb.setStartVertex(*g.begin()); + + if (!empty(g)) + gb.setStartVertex(*g.begin()); callback_wrapper = std::bind(&GraphBrowser::terminalResizedEvent, &gb, @@ -80,5 +56,7 @@ int main(int argc, char* argv[]) } GraphBrowser::destroy(); + writeGraphToPlainText(g, graph_file, atoa); + return EXIT_SUCCESS; }