diff --git a/src/annoylib.h b/src/annoylib.h index f3f23998..3c231e6f 100644 --- a/src/annoylib.h +++ b/src/annoylib.h @@ -39,6 +39,7 @@ #include #include #include +#include // This allows others to supply their own logger / error printer without // requiring Annoy to import their headers. See RcppAnnoy for a use case. @@ -202,17 +203,13 @@ struct Euclidean { }; template static inline T distance(const T* x, const T* y, int f) { - T d = 0.0; - for (int i = 0; i < f; i++, x++, y++) - d += ((*x) - (*y)) * ((*x) - (*y)); - return d; + Eigen::Map p(x, f, 1), q(y, f, 1); + return (p - q).squaredNorm(); } template static inline T margin(const Node* n, const T* y, int f) { - T dot = n->a; - for (int z = 0; z < f; z++) - dot += n->v[z] * y[z]; - return dot; + Eigen::Map p((float *)n->v, f, 1), q(y, f, 1); + return n->a + p.dot(q); } template static inline bool side(const Node* n, const T* y, int f, Random& random) {