Skip to content

Commit

Permalink
v1.8.2 add C++ APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
masajiro committed Nov 13, 2019
2 parents e5225da + dcd3bd7 commit 09f04df
Show file tree
Hide file tree
Showing 23 changed files with 1,109 additions and 940 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.1
1.8.2
2 changes: 2 additions & 0 deletions bin/ngt/ngt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#define GIT_TAG "-"
#endif

using namespace std;

static void
version(ostream &os)
{
Expand Down
47 changes: 21 additions & 26 deletions bin/search/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,51 @@
int
main(int argc, char **argv)
{
string indexFile = "index"; // Index.
string query = "./data/sift-query-3.tsv"; // Query file.
std::string indexFile = "index"; // Index.
std::string query = "./data/sift-query-3.tsv"; // Query file.
int size = 20; // The number of resultant objects.
float radius = FLT_MAX; // Radius of search range.
float epsilon = 0.1; // Epsilon to expand explored range.
float epsilon = 0.1; // Exploration coefficient(epsilon) to expand explored range

try {
NGT::Index index(indexFile); // open the specified index.
ifstream is(query); // open a query file.
std::ifstream is(query); // open a query file.
if (!is) {
cerr << "Cannot open the specified file. " << query << endl;
std::cerr << "Cannot open the specified file. " << query << std::endl;
return 1;
}
string line;
std::string line;
if (getline(is, line)) { // read a query object from a query file.
NGT::Object *query = 0;
std::vector<double> queryObject;
{
vector<string> tokens;
std::vector<std::string> tokens;
NGT::Common::tokenize(line, tokens, " \t"); // split a string into words by the separators.
// create a vector from the words.
vector<double> obj;
for (vector<string>::iterator ti = tokens.begin(); ti != tokens.end(); ++ti) {
obj.push_back(NGT::Common::strtol(*ti));
for (std::vector<std::string>::iterator ti = tokens.begin(); ti != tokens.end(); ++ti) {
queryObject.push_back(NGT::Common::strtol(*ti));
}
// allocate query object.
query = index.allocateObject(obj);
}
// set search prameters.
NGT::SearchContainer sc(*query); // search parametera container.
NGT::ObjectDistances objects; // a result set.
sc.setResults(&objects); // set the result set.
sc.setSize(size); // the number of resultant objects.
sc.setRadius(radius); // search radius.
sc.setEpsilon(epsilon); // set exploration coefficient.
NGT::SearchQuery query(queryObject); // search query
NGT::ObjectDistances objects; // result objects
query.setResults(&objects); // set the result objects.
query.setSize(size); // the number of result objects
query.setRadius(radius); // search radius
query.setEpsilon(epsilon); // set exploration coefficient.

index.search(sc);
index.search(query);

// output resultant objects.
cout << "Rank\tID\tDistance" << endl;
std::cout << "Rank\tID\tDistance" << std::endl;
for (size_t i = 0; i < objects.size(); i++) {
cout << i + 1 << "\t" << objects[i].id << "\t" << objects[i].distance << endl;
std::cout << i + 1 << "\t" << objects[i].id << "\t" << objects[i].distance << std::endl;
}
// delete the query object.
index.deleteObject(query);
}
} catch (NGT::Exception &err) {
cerr << "Error " << err.what() << endl;
std::cerr << "Error " << err.what() << std::endl;
return 1;
} catch (...) {
cerr << "Error" << endl;
std::cerr << "Error" << std::endl;
return 1;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/NGT/Clustering.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "NGT/Index.h"

using namespace std;

#if defined(NGT_AVX_DISABLED)
#define NGT_CLUSTER_NO_AVX
#else
Expand Down
3 changes: 3 additions & 0 deletions lib/NGT/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "NGT/Optimizer.h"


using namespace std;


void
NGT::Command::create(Args &args)
{
Expand Down
16 changes: 8 additions & 8 deletions lib/NGT/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class Command {
trial = args.getl("t", 1);
{
beginOfEpsilon = endOfEpsilon = stepOfEpsilon = 0.1;
string epsilon = args.getString("e", "0.1");
vector<string> tokens;
std::string epsilon = args.getString("e", "0.1");
std::vector<std::string> tokens;
NGT::Common::tokenize(epsilon, tokens, ":");
if (tokens.size() >= 1) { beginOfEpsilon = endOfEpsilon = NGT::Common::strtod(tokens[0]); }
if (tokens.size() >= 2) { endOfEpsilon = NGT::Common::strtod(tokens[1]); }
Expand All @@ -61,12 +61,12 @@ class Command {
}
}
char openMode;
string query;
std::string query;
size_t querySize;
char indexType;
int size;
long edgeSize;
string outputMode;
std::string outputMode;
float radius;
float beginOfEpsilon;
float endOfEpsilon;
Expand All @@ -79,16 +79,16 @@ class Command {

void create(Args &args);
void append(Args &args);
static void search(NGT::Index &index, SearchParameter &searchParameter, ostream &stream)
static void search(NGT::Index &index, SearchParameter &searchParameter, std::ostream &stream)
{
ifstream is(searchParameter.query);
std::ifstream is(searchParameter.query);
if (!is) {
cerr << "Cannot open the specified file. " << searchParameter.query << endl;
std::cerr << "Cannot open the specified file. " << searchParameter.query << std::endl;
return;
}
search(index, searchParameter, is, stream);
}
static void search(NGT::Index &index, SearchParameter &searchParameter, istream &is, ostream &stream);
static void search(NGT::Index &index, SearchParameter &searchParameter, std::istream &is, std::ostream &stream);
void search(Args &args);
void remove(Args &args);
void exportIndex(Args &args);
Expand Down
Loading

0 comments on commit 09f04df

Please sign in to comment.