Index: trunk/tests/test_KDTree.cc =================================================================== --- trunk/tests/test_KDTree.cc (revision 831) +++ trunk/tests/test_KDTree.cc (revision 832) @@ -1,321 +1,321 @@ #include "UnitTest++.h" #include "test_utils.hh" #include "mvarPointCount.hh" #include "npstat/nm/KDTree.hh" #include "geners/CPP11_array.hh" using namespace npstat; using namespace std; #define KDDIM 4 #define NCYCLES 10 #define NPOINTS 1000 #define NSEARCHES 1000 namespace { static int int_rng() { return static_cast(50.0*test_rng()) - 25; } TEST(KDTree_float) { typedef CPP11_array Point; Point ftmp; VisitCounter pCount; std::vector dimUse(KDDIM); for (unsigned i=0; i data; for (unsigned i=0; i tree(data, &dimUse[0], dimUse.size()); // Test "nCdf" for (unsigned i=0; i upper[dim]) le = 0; icdf_brute += le; } // Count the number of cdf points with the tree const unsigned icdf_tree = tree.nCdf(&upper[0], KDDIM); CHECK_EQUAL(icdf_brute, icdf_tree); } // Test "nInBox" for (unsigned i=0; i box(KDDIM); for (unsigned k=0; k hi); box[k].setBounds(lo, hi); } // Count the number of points by brute force unsigned n_brute = 0U; for (unsigned m=0; m Point; std::vector dimUse(KDDIM); VisitCounter pCount; for (unsigned icycle=0; icycle data; data.reserve(NPOINTS); for (unsigned i=0; i tree(data, &dimUse[0], dimUse.size()); dimUse[0] = 3; dimUse[1] = 0; dimUse[2] = 2; KDTree tree2(data, &dimUse[0], 3); // Test "nCdf" for (unsigned i=0; i upper[k]) { allLE = false; break; } if (allLE) ++icdf_brute; } // Count the number of cdf points with the tree unsigned icdf_tree = tree.nCdf(&upper[0], upper.size()); CHECK_EQUAL(icdf_brute, icdf_tree); // Check using the other tree std::vector upper2(KDDIM); for (unsigned k=0; k<3; ++k) upper2[k] = upper[dimUse[k]]; upper2[3] = 100; // Count the number of cdf points by brute force icdf_brute = 0U; upper[1] = 100; for (unsigned m=0; m upper[k]) { allLE = false; break; } if (allLE) ++icdf_brute; } // Count the number of cdf points with the tree icdf_tree = tree2.nCdf(&upper2[0], 3); CHECK_EQUAL(icdf_brute, icdf_tree); } // Test "nInBox" for (unsigned i=0; i box(KDDIM); for (unsigned k=0; k hi); box[k].setBounds(lo, hi); } // Count the number of points by brute force unsigned n_brute = 0U; for (unsigned m=0; m swapped(3); for (unsigned k=0; k<3; ++k) swapped[k].setBounds(box[dimUse[k]].min(), box[dimUse[k]].max()); // Count the number of cdf points by brute force n_brute = 0U; box[1].setBounds(-100, 100); for (unsigned m=0; m box(KDDIM); for (unsigned k=0; k hi); box[k].setBounds(lo, hi); } std::vector bv(KDDIM); for (unsigned k=0; k(test_rng()*4); // Count the number of points by brute force unsigned n_brute = 0U; for (unsigned m=0; m Point; unsigned index[3]; std::vector dimUse(dim); for (unsigned i=0; i data; data.reserve(NPOINTS); { Point newpt; for (unsigned i=0; i tree(data, &dimUse[0], dimUse.size()); for (unsigned i=0; i& counts = + const ArrayND& counts = mvarPointCount(data, limits[0], limits[1], limits[2]); - const unsigned long len = counts.length(); + const unsigned len = counts.length(); BoxND box(dim); - for (unsigned long iarr=0; iarr #include #include "npstat/nm/ArrayND.hh" #include "npstat/nm/PointDimensionality.hh" template -npstat::ArrayND mvarPointCount( +npstat::ArrayND mvarPointCount( const std::vector& data, const double x, const double y, const double z) { - const unsigned maxDim = 3U; + static const unsigned maxDim = 3U; + static const unsigned shape[maxDim] = {2U, 2U, 2U}; + // The result that will be filled and returned const unsigned dim = npstat::PointDimensionality::dim_size; - assert(dim == 2U || dim == 3U); - const unsigned long sz = data.size(); - - const unsigned shape[maxDim] = {2U, 2U, 2U}; - npstat::ArrayND result(shape, dim); + assert(dim >= 2U || dim <= maxDim); + npstat::ArrayND result(shape, dim); result.clear(); + const unsigned sz = data.size(); + + // The code below assumes that boolean "true" converts into unsigned 1 if (dim == 2U) { - for (unsigned long ipt=0; ipt x : pt[0] <= x) - for (unsigned iy=0U; iy<2U && continueSearching; ++iy) - if (iy ? pt[1] > y : pt[1] <= y) - { - ++result(ix, iy); - continueSearching = false; - } + const Point& pt(data[ipt]); + const unsigned ix = pt[0] > x; + const unsigned iy = pt[1] > y; + ++result(ix, iy); } } else if (dim == 3U) { - for (unsigned long ipt=0; ipt x : pt[0] <= x) - for (unsigned iy=0U; iy<2U && continueSearching; ++iy) - if (iy ? pt[1] > y : pt[1] <= y) - for (unsigned iz=0U; iz<2U && continueSearching; ++iz) - if (iz ? pt[2] > z : pt[2] <= z) - { - ++result(ix, iy, iz); - continueSearching = false; - } + const Point& pt(data[ipt]); + const unsigned ix = pt[0] > x; + const unsigned iy = pt[1] > y; + const unsigned iz = pt[2] > z; + ++result(ix, iy, iz); } } else - assert(!"Unprocessed dimensionality in mvarPointCount"); + assert(!"Unsupported dimensionality case in mvarPointCount"); - assert(result.sum() == sz); return result; } #endif // MVARPOINTCOUNT_HH_