void compare_libraries(std::size_t data_size, const Distribution& distribution) { std::cout << "\t" << N << " dimensions, " << data_size << " objects:" << std::endl; std::vector<Point> data; std::vector<Point> targets; data.reserve(data_size); targets.reserve(data_size); for (std::size_t i = 0; i < data_size; ++i) { data.push_back(Point(distribution)); targets.push_back(Point(distribution)); } { // Nearest neighbor begin into an idle_point_multiset std::cout << "\t\tidle_point_multiset:\t" << std::flush; spatial::idle_point_multiset<N, Point> cobaye; cobaye.insert_rebalance(data.begin(), data.end()); utils::time_point start = utils::process_timer_now(); for (typename std::vector<Point>::const_iterator i = targets.begin(); i != targets.end(); ++i) neighbor_upper_bound(cobaye, *i, (*i)[0]); utils::time_point stop = utils::process_timer_now(); std::cout << (stop - start) << "sec" << std::endl; } { // Nearest neighbor begin into an idle_point_multiset std::cout << "\t\tpoint_multiset:\t" << std::flush; spatial::point_multiset<N, Point> cobaye; cobaye.insert(data.begin(), data.end()); utils::time_point start = utils::process_timer_now(); for (typename std::vector<Point>::const_iterator i = targets.begin(); i != targets.end(); ++i) neighbor_upper_bound(cobaye, *i, (*i)[0]); utils::time_point stop = utils::process_timer_now(); std::cout << (stop - start) << "sec" << std::endl; } }
BOOST_AUTO_TEST_CASE_TEMPLATE ( test_neighbor_upper_bound, Tp, quad_sets ) { typedef quadrance<typename Tp::container_type, int, quad_diff> metric_type; typedef typename metric_type::distance_type distance_type; typedef neighbor_iterator<typename Tp::container_type, metric_type> neighbor_iterator_type; // Prove that you can find lower bound with N nodes, down to 1 node { Tp fix(100, randomize(-20, 20)); metric_type metric; quad target; while (!fix.container.empty()) { randomize(-22, 22)(target, 0, 0); // Find min and max dist first distance_type min_dist; distance_type max_dist; typename Tp::container_type::iterator it = fix.container.begin(); min_dist = max_dist = metric.distance_to_key(fix.container.rank()(), *it, target); ++it; for (; it != fix.container.end(); ++it) { distance_type tmp = metric.distance_to_key(fix.container.rank()(), *it, target); if (tmp < min_dist) min_dist = tmp; if (tmp > max_dist) max_dist = tmp; } distance_type avg_dist = (min_dist + max_dist) / 2; // use this knowledge to test the upper bound neighbor_iterator_type i = neighbor_upper_bound(fix.container, metric, target, min_dist); BOOST_CHECK(i != neighbor_begin(fix.container, metric, target)); if (i != neighbor_end(fix.container, metric, target)) { BOOST_CHECK_LT(min_dist, distance(i)); } BOOST_CHECK_EQUAL(min_dist, distance(--i)); i = neighbor_upper_bound(fix.container, metric, target, max_dist); BOOST_CHECK(i == neighbor_end(fix.container, metric, target)); i = neighbor_upper_bound(fix.container, metric, target, avg_dist); if (i != neighbor_end(fix.container, metric, target)) { BOOST_CHECK_GT(distance(i), avg_dist); } if (i == neighbor_end(fix.container, metric, target)) { BOOST_CHECK_LE(distance(--i), avg_dist); } fix.container.erase(i); } } // Prove that you can find the upper bound when node and target are same { Tp fix(100, same()); metric_type metric; quad target; same()(target, 0, 100); // All points and targets are the same. while (!fix.container.empty()) { neighbor_iterator_type i = neighbor_upper_bound(fix.container, metric, target, 1); BOOST_CHECK(i == neighbor_end(fix.container, metric, target)); i = neighbor_upper_bound(fix.container, metric, target, 0); BOOST_CHECK(i == neighbor_end(fix.container, metric, target)); fix.container.erase(--i); } } // Prove that you can find the upper bound in an unbalanced tree { Tp fix(100, increase()); metric_type metric; quad target; while (!fix.container.empty()) { randomize(0, 100)(target, 0, 0); // Find min and max dist first distance_type min_dist; distance_type max_dist; typename Tp::container_type::iterator it = fix.container.begin(); min_dist = max_dist = metric.distance_to_key(fix.container.rank()(), *it, target); ++it; for (; it != fix.container.end(); ++it) { distance_type tmp = metric.distance_to_key(fix.container.rank()(), *it, target); if (tmp < min_dist) min_dist = tmp; if (tmp > max_dist) max_dist = tmp; } distance_type avg_dist = (min_dist + max_dist) / 2; // Use this knowledge to test the upper bound neighbor_iterator_type i = neighbor_upper_bound(fix.container, metric, target, min_dist); BOOST_CHECK(i != neighbor_begin(fix.container, metric, target)); if (i != neighbor_end(fix.container, metric, target)) { BOOST_CHECK_LT(min_dist, distance(i)); } BOOST_CHECK_EQUAL(min_dist, distance(--i)); i = neighbor_upper_bound(fix.container, metric, target, max_dist); BOOST_CHECK(i == neighbor_end(fix.container, metric, target)); i = neighbor_upper_bound(fix.container, metric, target, avg_dist); if (i != neighbor_end(fix.container, metric, target)) { BOOST_CHECK_GT(distance(i), avg_dist); } if (i == neighbor_end(fix.container, metric, target)) { BOOST_CHECK_LE(distance(--i), avg_dist); } fix.container.erase(i); } } // Prove that you can find the upper bound in an unbalanced tree { Tp fix(100, decrease()); metric_type metric; quad target; while (!fix.container.empty()) { randomize(0, 100)(target, 0, 0); // Find min and max dist first distance_type min_dist; distance_type max_dist; typename Tp::container_type::iterator it = fix.container.begin(); min_dist = max_dist = metric.distance_to_key(fix.container.rank()(), *it, target); ++it; for (; it != fix.container.end(); ++it) { distance_type tmp = metric.distance_to_key(fix.container.rank()(), *it, target); if (tmp < min_dist) min_dist = tmp; if (tmp > max_dist) max_dist = tmp; } distance_type avg_dist = (min_dist + max_dist) / 2; // Use this knowledge to test the upper bound neighbor_iterator_type i = neighbor_upper_bound(fix.container, metric, target, min_dist); BOOST_CHECK(i != neighbor_begin(fix.container, metric, target)); if (i != neighbor_end(fix.container, metric, target)) { BOOST_CHECK_LT(min_dist, distance(i)); } BOOST_CHECK_EQUAL(min_dist, distance(--i)); i = neighbor_upper_bound(fix.container, metric, target, max_dist); BOOST_CHECK(i == neighbor_end(fix.container, metric, target)); i = neighbor_upper_bound(fix.container, metric, target, avg_dist); if (i != neighbor_end(fix.container, metric, target)) { BOOST_CHECK_GT(distance(i), avg_dist); } if (i == neighbor_end(fix.container, metric, target)) { BOOST_CHECK_LE(distance(--i), avg_dist); } fix.container.erase(i); } } }