Graph is read as input parameter instead of randomly generated

master
denes 6 years ago
parent fab767c32b
commit a5ba112a83
Signed by: denes
GPG Key ID: A7D50EAD42F9FC9F

@ -1,12 +1,10 @@
cmake_minimum_required (VERSION 2.6) cmake_minimum_required (VERSION 2.6)
project (PROJECT_GRAPH_BROWSER) project (PROJECT_GRAPH_BROWSER)
set(CMAKE_EXPORT_COMPILE_COMMANDS, 1)
set (CXX_FLAGS "-std=c++14 -ggdb") set (CXX_FLAGS "-std=c++14 -ggdb")
add_definitions(${CXX_FLAGS}) 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) add_executable (graph_browser main.cpp graph_browser.cpp)
include_directories(graph/lib/) include_directories(graph/lib/)
target_link_libraries(graph_browser ncurses menu) target_link_libraries(graph_browser ncurses menu)

@ -3,12 +3,16 @@ Graph broswing and editing in text mode with ncurses
Using 3 windows: 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. back/forward to move back in history/set the selected node as the current.
2. Vertically listed neighbours of the selected node. 2. Vertically listed neighbours of the selected node.
3. Neighbours of neighbours of the selected node, comma separated list. 3. Neighbours of neighbours of the selected node, comma separated list.
Todo: Todo:
* insert/delete node * insert/delete node
* add/remove edge * add/remove edge
To fetch the submodule graph after git clone, issue:
git submodule update --init --recursive
to update:
git submodule update --recursive --remote

@ -1,58 +1,14 @@
#include <graph/graph.hpp> #include <iostream>
// #include <string>
#include <string>
#include <signal.h> #include <signal.h>
#include <graph/graph.hpp>
#include <graph/graph_plaintext.hpp>
#include <functional> #include <functional>
#include "graph_browser.hpp" #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<std::string> generateRandomStrings(size_t number_of_strings,
size_t min_length,
size_t max_length)
{
std::vector<std::string> 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<std::string> generateRandomGraph(size_t number_of_vertices,
size_t number_of_edges,
size_t vertex_text_min_length,
size_t vertex_text_max_length)
{
Graph<std::string> graph;
const std::vector<std::string> 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<void(int)> callback_wrapper; std::function<void(int)> callback_wrapper;
void callback_function( int value ) void callback_function( int value )
@ -60,14 +16,34 @@ void callback_function( int value )
callback_wrapper(value); callback_wrapper(value);
} }
// template<typename T>T conveter(T){};
// template<>std::string conveter(const std::string& s)<std::string>{retun s;}
inline std::string atoa(const std::string s)
{
return s;
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
Graph<std::string> 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<std::string> g;
try {
g = readGraphFromPlainText<std::string>(graph_file, atoa);
} catch (std::runtime_error &e) {
std::cerr << e.what() << std::endl;
return 1;
}
GraphBrowser::init(); GraphBrowser::init();
{ {
GraphBrowser gb(g); GraphBrowser gb(g);
gb.setStartVertex(*g.begin());
if (!empty(g))
gb.setStartVertex(*g.begin());
callback_wrapper = std::bind(&GraphBrowser::terminalResizedEvent, callback_wrapper = std::bind(&GraphBrowser::terminalResizedEvent,
&gb, &gb,
@ -80,5 +56,7 @@ int main(int argc, char* argv[])
} }
GraphBrowser::destroy(); GraphBrowser::destroy();
writeGraphToPlainText<std::string>(g, graph_file, atoa);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

Loading…
Cancel
Save