MCPcopy Create free account
hub / github.com/NGT-labs/NGT / linearSearch

Method linearSearch

python/src/ngtpy.cpp:369–425  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

367 }
368
369 py::object linearSearch(
370 py::object query,
371 size_t size = 0, // the number of resultant objects
372 bool withDistance = true
373 ) {
374 py::array_t<float> qobject(query);
375 py::buffer_info qinfo = qobject.request();
376 NGT::Object *ngtquery = 0;
377 try {
378 ngtquery = NGT::Index::allocateObject(static_cast<float*>(qinfo.ptr), qinfo.size);
379 } catch (NGT::Exception &e) {
380 std::cerr << e.what() << std::endl;
381 if (!withDistance) {
382 return py::array_t<int>();
383 } else {
384 return py::list();
385 }
386 }
387
388 NGT::SearchContainer sc(*ngtquery);
389 sc.setSize(size == 0 ? defaultNumOfSearchObjects : size); // the number of resulting objects.
390 sc.setRadius(defaultRadius); // the radius of search.
391 NGT::ObjectDistances rs;
392 sc.setResults(&rs);
393
394 NGT::Index::linearSearch(sc);
395
396 numOfDistanceComputations += sc.distanceComputationCount;
397
398 NGT::Index::deleteObject(ngtquery);
399 if (!withDistance) {
400 py::array_t<int> ids(rs.size());
401 py::buffer_info idsinfo = ids.request();
402 int *ptr = reinterpret_cast<int*>(idsinfo.ptr);
403 if (zeroNumbering) {
404 for (auto ri = rs.begin(); ri != rs.end(); ++ri) {
405 *ptr++ = (*ri).id - 1;
406 }
407 } else {
408 for (auto ri = rs.begin(); ri != rs.end(); ++ri) {
409 *ptr++ = (*ri).id;
410 }
411 }
412 return ids;
413 }
414 py::list results;
415 if (zeroNumbering) {
416 for (auto ri = rs.begin(); ri != rs.end(); ++ri) {
417 results.append(py::make_tuple((*ri).id - 1, (*ri).distance));
418 }
419 } else {
420 for (auto ri = rs.begin(); ri != rs.end(); ++ri) {
421 results.append(py::make_tuple((*ri).id, (*ri).distance));
422 }
423 }
424 return results;
425 }
426

Callers

nothing calls this directly

Calls 11

allocateObjectFunction · 0.85
linearSearchFunction · 0.85
deleteObjectFunction · 0.85
whatMethod · 0.80
setSizeMethod · 0.80
setRadiusMethod · 0.80
setResultsMethod · 0.80
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected