manzilzaheer / CoverTree

Cover Tree implementation in C++ for k-Nearest Neighbours and range search
Apache License 2.0
90 stars 19 forks source link

allow different distance metrics and point classes #8

Open flying-sheep opened 7 years ago

flying-sheep commented 7 years ago

you hardcode euclidean distance and Eigen::VectorXd points. i would prefer to have the options of

  1. using points that can be owned, or slices of (sparse or dense) matrices.
  2. using other distances.

i.e. i’d like to have

template<Point>
class CoverTree {
   ...
}

with Point needing to specify distance and operator==, e.g.:

template<class Distance, class Vector>
class IndexedPoint {
private:
    Vector _vec;
    size_t _idx;
public:
    IndexedPoint(Vector v, size_t i) : _vec(v), _idx(i) {}
    const Vector& vec() const { return this->_vec; }
    size_t               idx() const { return this->_idx; }
    bool operator==(const IndexedPoint<Distance>& p) {
        return is_true(all(this->_vec == p.vec()));
    };
    double distance(const IndexedPoint<Distance>& p) const {
        return Distance::distance(*this, p);
    };
};

class CosineDistance {
public:
    static double distance(const IndexedPoint<CosineDistance>& p1, const IndexedPoint<CosineDistance>& p2) {
        return 1 - cor(p1.vec(), p2.vec());
    }
};

class EuclideanDistance { ... }

then i can do:

CoverTree<IndexedPoint<EuclideanDistance>> ct;