Skip to content

Commit c0972a0

Browse files
committed
Updated the README with new C++ usage instructions.
1 parent 61bbd2d commit c0972a0

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

README.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,21 @@ The idea is that downstream packages will link against the **knncolle** C++ inte
106106
This allows downstream packages to (i) save time by avoiding the need to re-compile all algorithms and (ii) support more algorithms in **knncolle** extensions.
107107
To do so:
108108

109-
1. Add `assorthead.includes()` to the compiler's include path for your package.
109+
1. Add `knncolle.includes()` and `assorthead.includes()` to the compiler's include path for the package.
110110
This can be done through `include_dirs=` of the `Extension()` definition in `setup.py`
111-
or by adding a `target_include_directories()` in CMake, depending on your build system.
111+
or by adding a `target_include_directories()` in CMake, depending on the build system.
112112
2. Call `knncolle.build_index()` to construct a `GenericIndex` instance.
113113
This exposes a shared pointer to the C++-allocated index via its `ptr` property.
114-
3. Pass `ptr` to **pybind11**-wrapped C++ code as a [shared pointer to a `knncolle::Prebuilt`](lib/src/def.h),
114+
3. Pass `ptr` to C++ code as a `uintptr_t` referencing a `knncolle::Prebuilt`.
115115
which can be interrogated as described in the [**knncolle** documentation](https://github.com/knncolle/knncolle).
116116

117117
So, for example, the C++ code in our downstream package might look like this:
118118

119119
```cpp
120-
int do_something(
121-
const std::shared_ptr<
122-
knncolle::Prebuilt<uint32_t, uint32_t, double>
123-
>& index)
124-
{
120+
#include "knncolle_py.h"
121+
122+
int do_something(uintptr_t ptr) {
123+
const auto& prebuilt = knncolle_py::cast_prebuilt(ptr)->ptr;
125124
// Do something with the search index interface.
126125
return 1;
127126
}
@@ -143,17 +142,13 @@ def do_something(idx: GenericIndex):
143142

144143
In some scenarios, it may be more convenient to construct the search index inside C++,
145144
e.g., if the dataset to be searched is not available before the call to the C++ function.
146-
This can be accommodated by accepting a [shared pointer to a `knncolle::Builder`](lib/src/def.h) in the C++ code:
145+
This can be accommodated by accepting a `uintptr_t` to a `knncolle::Builder` in the C++ code:
147146

148147
```cpp
149-
int do_something_mk2(
150-
const std::shared_ptr<
151-
knncolle::Builder<
152-
knncolle::SimpleMatrix<uint32_t, uint32_t, double>,
153-
double
154-
>
155-
>& builder)
156-
{
148+
#include "knncolle_py.h"
149+
150+
int do_something_mk2(uintptr_t ptr) {
151+
const auto& builder = knncolle_py::cast_builder(ptr)->ptr;
157152
// The builder is a algorithm-specific factory that accepts a matrix and
158153
// returns a search index for that algorithm. Presumably we construct a
159154
// new search index inside this function and use it.
@@ -176,24 +171,24 @@ def do_something(param: Parameters):
176171
return lib.do_something_mk2(builder.ptr)
177172
```
178173

179-
See also the definitions in [`lib/src/def.h`](lib/src/def.h) for the types of the pointers to be used in the C++ code.
174+
Check out [the included header](src/knncolle/include/knncolle_py.h) for more definitions.
180175

181176
## Extending to more algorithms
182177

183178
### Via `define_builder()`
184179

185180
The best way to extend **knncolle** is to do so in C++.
186-
This involves writing subclasses of the `Builder`, `Prebuilt` and `Searcher` interfaces in the [**knncolle**](https://github.com/knncolle/knncolle) library.
181+
This involves writing subclasses of the interfaces in the [**knncolle**](https://github.com/knncolle/knncolle) library.
187182
Once this is done, it is a simple matter of writing the following Python bindings:
188183

189184
- Implement a `SomeNewParameters` class that inherits from `Parameters`.
190185
- Implement a `SomeNewIndex` class that inherits from `GenericIndex`.
191186
This should accept a single `ptr` in its constructor and have a `ptr` property that returns the same value.
192187
- Register a `define_builder()` method that dispatches on `SomeNewParameters`.
193-
This should call into C++ and return a tuple containing a **pybind11**-wrapped [`BuilderPointer`](lib/src/def.h) and the `SomeNewIndex` constructor.
188+
This should call into C++ and return a tuple containing a `Builder` object and the `SomeNewIndex` constructor.
194189

195190
No new methods are required for `find_knn()`, `build_index()`, etc. as the default method will work automatically if a `define_builder()` method is available.
196-
This approach also allows the new method to be used in C++ code of downstream packages that accept a [`PrebuiltPointer`](lib/src/def.h) instance.
191+
This approach also allows the new method to be used in C++ code of downstream packages.
197192

198193
### Without `define_builder()`
199194

0 commit comments

Comments
 (0)