Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ jobs:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
os: ["ubuntu-20.04", "macos-latest", "windows-latest"]
unsigned: [true, false]

steps:
- uses: actions/checkout@v3 # Pull the repository
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Use unsigned indices
if: ${{ matrix.unsigned }}
run: |
cat src/annoymodule.cc | sed "s/.*\(#define ANNOYLIB_UNSIGNED_INDEX\)/\1/" > .tmp
cmp .tmp src/annoymodule.cc && exit 1 # ensure a change was actually made.
mv .tmp src/annoymodule.cc

- run: pip install .
- run: pip install h5py numpy pytest
- run: pytest -v
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

if os.name != 'nt':
extra_compile_args += ['-O3', '-ffast-math', '-fno-associative-math']
extra_compile_args += ['-Wall', '-Wpedantic', '-Wextra']

# Add multithreaded build flag for all platforms using Python 3 and
# for non-Windows Python 2 platforms
Expand Down
30 changes: 27 additions & 3 deletions src/annoylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,17 +408,29 @@ struct Base {
static inline void preprocess(void* nodes, size_t _s, const S node_count, const int f) {
// Override this in specific metric structs below if you need to do any pre-processing
// on the entire set of nodes passed into this index.

(void)nodes; // silence unused variable warnings.
(void)_s;
(void)node_count;
(void)f;
}

template<typename T, typename S, typename Node>
static inline void postprocess(void* nodes, size_t _s, const S node_count, const int f) {
// Override this in specific metric structs below if you need to do any post-processing
// on the entire set of nodes passed into this index.

(void)nodes; // silence unused variable warnings.
(void)_s;
(void)node_count;
(void)f;
}

template<typename Node>
static inline void zero_value(Node* dest) {
// Initialize any fields that require sane defaults within this node.

(void)dest; // silence unused variable warnings.
}

template<typename T, typename Node>
Expand Down Expand Up @@ -695,6 +707,7 @@ struct DotProduct : Angular {

template<typename T, typename S, typename Node>
static inline void postprocess(void* nodes, size_t _s, const S node_count, const int f) {
(void)f; // silence unused variable warnings.
for (S i = 0; i < node_count; i++) {
Node* node = get_node_ptr<S, Node>(nodes, _s, i);
// When an index is built, we will remember it in index item nodes to compute distances differently
Expand Down Expand Up @@ -743,12 +756,14 @@ struct Hamming : Base {
}
template<typename S, typename T>
static inline bool margin(const Node<S, T>* n, const T* y, int f) {
(void)f; // silence unused variable warnings.
static const size_t n_bits = sizeof(T) * 8;
T chunk = n->v[0] / n_bits;
return (y[chunk] & (static_cast<T>(1) << (n_bits - 1 - (n->v[0] % n_bits)))) != 0;
}
template<typename S, typename T, typename Random>
static inline bool side(const Node<S, T>* n, const T* y, int f, Random& random) {
(void)random; // silence unused variable warnings.
return margin(n, y, f);
}
template<typename S, typename T, typename Random>
Expand All @@ -757,6 +772,7 @@ struct Hamming : Base {
}
template<typename S, typename T, typename Random>
static inline void create_split(const vector<Node<S, T>*>& nodes, int f, size_t s, Random& random, Node<S, T>* n) {
(void)s; // silence unused variable warnings.
size_t cur_size = 0;
size_t i = 0;
int dim = f * 8 * sizeof(T);
Expand Down Expand Up @@ -796,6 +812,8 @@ struct Hamming : Base {
}
template<typename S, typename T>
static inline void init_node(Node<S, T>* n, int f) {
(void)n; // silence unused variable warnings.
(void)f;
}
static const char* name() {
return "hamming";
Expand Down Expand Up @@ -864,6 +882,8 @@ struct Euclidean : Minkowski {
}
template<typename S, typename T>
static inline void init_node(Node<S, T>* n, int f) {
(void)n; // silence unused variable warnings.
(void)f;
}
static const char* name() {
return "euclidean";
Expand Down Expand Up @@ -895,6 +915,8 @@ struct Manhattan : Minkowski {
}
template<typename S, typename T>
static inline void init_node(Node<S, T>* n, int f) {
(void)n; // silence unused variable warnings.
(void)f;
}
static const char* name() {
return "manhattan";
Expand Down Expand Up @@ -1202,9 +1224,10 @@ template<typename S, typename T, typename Distance, typename Random, class Threa
// Find the roots by scanning the end of the file and taking the nodes with most descendants
_roots.clear();
S m = -1;
for (S i = _n_nodes - 1; i >= 0; i--) {
for (S x = _n_nodes; x > 0; x--) { // S may be unsigned, so don't use >= 0 to terminate
S i = x - 1;
S k = _get(i)->n_descendants;
if (m == -1 || k == m) {
if (m == static_cast<S>(-1) || k == m) { // first condition is still valid if S is unsigned, as m would be the largest possible number of descendants
_roots.push_back(i);
m = k;
} else {
Expand Down Expand Up @@ -1483,7 +1506,7 @@ template<typename S, typename T, typename Distance, typename Random, class Threa
// To avoid calculating distance multiple times for any items, sort by id
std::sort(nns.begin(), nns.end());
vector<pair<T, S> > nns_dist;
S last = -1;
S last = -1; // Safe sentinel for unsigned S; all indices must be less than the max value of S, otherwise get_n_items() would overflow.
for (size_t i = 0; i < nns.size(); i++) {
S j = nns[i];
if (j == last)
Expand All @@ -1508,6 +1531,7 @@ class AnnoyIndexSingleThreadedBuildPolicy {
public:
template<typename S, typename T, typename D, typename Random>
static void build(AnnoyIndex<S, T, D, Random, AnnoyIndexSingleThreadedBuildPolicy>* annoy, int q, int n_threads) {
(void)n_threads; // silence unused variable warnings.
AnnoyIndexSingleThreadedBuildPolicy threaded_build_policy;
annoy->thread_build(q, 0, threaded_build_policy);
}
Expand Down
Loading