Skip to content

Commit 01abe5b

Browse files
aalekhpatel07abhinavdangeti
authored andcommitted
Add the ability to clone and read binary indexes to the C API. (facebookresearch#3318)
Summary: I noticed we have a pretty decent C API for binary indexes and please correct me if I'm wrong but we seem to be missing a couple of functions, like the ability to clone and read binary indexes. This PR provides those functions. Pull Request resolved: facebookresearch#3318 Reviewed By: algoriddle Differential Revision: D55469615 Pulled By: mdouze fbshipit-source-id: 42e6f827d8b5ad6bc3efe989e47ede3aa06c1810
1 parent c44011f commit 01abe5b

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

c_api/clone_index_c.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "macros_impl.h"
1515

1616
using faiss::Index;
17+
using faiss::IndexBinary;
1718

1819
int faiss_clone_index(const FaissIndex* idx, FaissIndex** p_out) {
1920
try {
@@ -22,3 +23,14 @@ int faiss_clone_index(const FaissIndex* idx, FaissIndex** p_out) {
2223
}
2324
CATCH_AND_HANDLE
2425
}
26+
27+
int faiss_clone_index_binary(
28+
const FaissIndexBinary* idx,
29+
FaissIndexBinary** p_out) {
30+
try {
31+
auto out = faiss::clone_binary_index(
32+
reinterpret_cast<const IndexBinary*>(idx));
33+
*p_out = reinterpret_cast<FaissIndexBinary*>(out);
34+
}
35+
CATCH_AND_HANDLE
36+
}

c_api/clone_index_c.h

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define FAISS_CLONE_INDEX_C_H
1414

1515
#include <stdio.h>
16+
#include "IndexBinary_c.h"
1617
#include "Index_c.h"
1718
#include "faiss_c.h"
1819

@@ -25,6 +26,9 @@ extern "C" {
2526
/** Clone an index. This is equivalent to `faiss::clone_index` */
2627
int faiss_clone_index(const FaissIndex*, FaissIndex** p_out);
2728

29+
/** Clone a binary index. This is equivalent to `faiss::clone_index_binary` */
30+
int faiss_clone_index_binary(const FaissIndexBinary*, FaissIndexBinary** p_out);
31+
2832
#ifdef __cplusplus
2933
}
3034
#endif

c_api/index_factory_c.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
using faiss::Index;
1717

18-
/** Build and index with the sequence of processing steps described in
18+
/** Build an index with the sequence of processing steps described in
1919
* the string.
2020
*/
2121
int faiss_index_factory(
@@ -29,3 +29,17 @@ int faiss_index_factory(
2929
}
3030
CATCH_AND_HANDLE
3131
}
32+
33+
/** Build an index with the sequence of processing steps described in
34+
* the string.
35+
*/
36+
int faiss_index_binary_factory(
37+
FaissIndexBinary** p_index,
38+
int d,
39+
const char* description) {
40+
try {
41+
*p_index = reinterpret_cast<FaissIndexBinary*>(
42+
faiss::index_binary_factory(d, description));
43+
}
44+
CATCH_AND_HANDLE
45+
}

c_api/index_factory_c.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
#ifndef FAISS_INDEX_FACTORY_C_H
1212
#define FAISS_INDEX_FACTORY_C_H
1313

14+
#include "IndexBinary_c.h"
1415
#include "Index_c.h"
1516
#include "faiss_c.h"
1617

1718
#ifdef __cplusplus
1819
extern "C" {
1920
#endif
2021

21-
/** Build and index with the sequence of processing steps described in
22+
/** Build an index with the sequence of processing steps described in
2223
* the string.
2324
*/
2425
int faiss_index_factory(
@@ -27,6 +28,14 @@ int faiss_index_factory(
2728
const char* description,
2829
FaissMetricType metric);
2930

31+
/** Build a binary index with the sequence of processing steps described in
32+
* the string.
33+
*/
34+
int faiss_index_binary_factory(
35+
FaissIndexBinary** p_index,
36+
int d,
37+
const char* description);
38+
3039
#ifdef __cplusplus
3140
}
3241
#endif

0 commit comments

Comments
 (0)