void closestColumnsAndDistances( const MappedMatrix& inMatrix, const MappedColumnVector& inVector, DistanceFunction& inMetric, RandomAccessIterator ioFirst, RandomAccessIterator ioLast) { ReverseLexicographicComparator< typename std::iterator_traits<RandomAccessIterator>::value_type> comparator; std::fill(ioFirst, ioLast, std::make_tuple(0, std::numeric_limits<double>::infinity())); for (Index i = 0; i < inMatrix.cols(); ++i) { double currentDist = AnyType_cast<double>( inMetric(MappedColumnVector(inMatrix.col(i)), inVector) ); // outIndicesAndDistances is a heap, so the first element is maximal if (currentDist < std::get<1>(*ioFirst)) { // Unfortunately, the STL does not have a decrease-key function, // so we are wasting a bit of performance here std::pop_heap(ioFirst, ioLast, comparator); *(ioLast - 1) = std::make_tuple(i, currentDist); std::push_heap(ioFirst, ioLast, comparator); } } std::sort_heap(ioFirst, ioLast, comparator); }
std::tuple<Index, double> closestColumnAndDistance( const MappedMatrix& inMatrix, const MappedColumnVector& inVector, DistanceFunction& inMetric) { Index closestColumn = 0; double minDist = std::numeric_limits<double>::infinity(); for (Index i = 0; i < inMatrix.cols(); ++i) { double currentDist = AnyType_cast<double>( inMetric(MappedColumnVector(inMatrix.col(i)), inVector) ); if (currentDist < minDist) { closestColumn = i; minDist = currentDist; } } return std::tuple<Index, double>(closestColumn, minDist); }