From 6e097d3cdf38543414e0d5b4528f1763f0a49018 Mon Sep 17 00:00:00 2001 From: Wei Dong Date: Fri, 16 Mar 2018 19:52:54 -0400 Subject: [PATCH] expose save_text --- kgraph.cpp | 15 +++++++++++++++ kgraph.h | 3 ++- python/pykgraph.cpp | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/kgraph.cpp b/kgraph.cpp index d486f9a..965175b 100644 --- a/kgraph.cpp +++ b/kgraph.cpp @@ -334,6 +334,21 @@ namespace kgraph { } virtual void save (char const *path, int format) const { + if (format == FORMAT_TEXT) { + std::cerr << "Saving to text file; you won't be able to load text file." << std::endl; + ofstream os(path); + os << graph.size() << endl; + for (unsigned i = 0; i < graph.size(); ++i) { + auto const &knn = graph[i]; + uint32_t K = knn.size(); + os << K; + for (unsigned k = 0; k < K; ++k) { + os << ' ' << knn[k].id << ' ' << knn[k].dist; + } + os << endl; + } + return; + } ofstream os(path, ios::binary); uint32_t N = graph.size(); os.write(KGRAPH_MAGIC, KGRAPH_MAGIC_SIZE); diff --git a/kgraph.h b/kgraph.h index 2138cd9..a3e5aaf 100644 --- a/kgraph.h +++ b/kgraph.h @@ -127,7 +127,8 @@ namespace kgraph { enum { FORMAT_DEFAULT = 0, - FORMAT_NO_DIST = 1 + FORMAT_NO_DIST = 1, + FORMAT_TEXT = 128 }; /// Information and statistics of the indexing algorithm. diff --git a/python/pykgraph.cpp b/python/pykgraph.cpp index cd08a9e..8b16a00 100644 --- a/python/pykgraph.cpp +++ b/python/pykgraph.cpp @@ -269,6 +269,9 @@ namespace { void save (char const *path) const { index->save(path); } + void save_text (char const *path) const { + index->save(path, kgraph::KGraph::FORMAT_TEXT); + } }; template @@ -400,6 +403,10 @@ class KGraph { impl->save(path); } + void save_text (char const *path) const { + impl->save_text(path); + } + void build (unsigned iterations, unsigned L, unsigned K, @@ -468,6 +475,7 @@ BOOST_PYTHON_MODULE(pykgraph) // (python::arg("data"), python::arg("metric") = "euclidean"))) .def("load", &KGraph::load) .def("save", &KGraph::save) + .def("save_text", &KGraph::save_text) .def("build", &KGraph::build, (python::arg("data"), python::arg("iterations") = kgraph::default_iterations,