Skip to content

Commit

Permalink
expose save_text
Browse files Browse the repository at this point in the history
  • Loading branch information
aaalgo committed Mar 16, 2018
1 parent 83e53ea commit 6e097d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
15 changes: 15 additions & 0 deletions kgraph.cpp
Expand Up @@ -334,6 +334,21 @@ namespace kgraph {
} }


virtual void save (char const *path, int format) const { 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); ofstream os(path, ios::binary);
uint32_t N = graph.size(); uint32_t N = graph.size();
os.write(KGRAPH_MAGIC, KGRAPH_MAGIC_SIZE); os.write(KGRAPH_MAGIC, KGRAPH_MAGIC_SIZE);
Expand Down
3 changes: 2 additions & 1 deletion kgraph.h
Expand Up @@ -127,7 +127,8 @@ namespace kgraph {


enum { enum {
FORMAT_DEFAULT = 0, FORMAT_DEFAULT = 0,
FORMAT_NO_DIST = 1 FORMAT_NO_DIST = 1,
FORMAT_TEXT = 128
}; };


/// Information and statistics of the indexing algorithm. /// Information and statistics of the indexing algorithm.
Expand Down
8 changes: 8 additions & 0 deletions python/pykgraph.cpp
Expand Up @@ -269,6 +269,9 @@ namespace {
void save (char const *path) const { void save (char const *path) const {
index->save(path); index->save(path);
} }
void save_text (char const *path) const {
index->save(path, kgraph::KGraph::FORMAT_TEXT);
}
}; };


template <typename DATA_TYPE, typename METRIC_TYPE> template <typename DATA_TYPE, typename METRIC_TYPE>
Expand Down Expand Up @@ -400,6 +403,10 @@ class KGraph {
impl->save(path); impl->save(path);
} }


void save_text (char const *path) const {
impl->save_text(path);
}

void build (unsigned iterations, void build (unsigned iterations,
unsigned L, unsigned L,
unsigned K, unsigned K,
Expand Down Expand Up @@ -468,6 +475,7 @@ BOOST_PYTHON_MODULE(pykgraph)
// (python::arg("data"), python::arg("metric") = "euclidean"))) // (python::arg("data"), python::arg("metric") = "euclidean")))
.def("load", &KGraph::load) .def("load", &KGraph::load)
.def("save", &KGraph::save) .def("save", &KGraph::save)
.def("save_text", &KGraph::save_text)
.def("build", &KGraph::build, .def("build", &KGraph::build,
(python::arg("data"), (python::arg("data"),
python::arg("iterations") = kgraph::default_iterations, python::arg("iterations") = kgraph::default_iterations,
Expand Down

0 comments on commit 6e097d3

Please sign in to comment.