void SortTimeFunction::f() {
  const int n           = m_copy.size();
  const int elementSize = m_copy.getElementSize();
  DataArray data        = m_copy;
  switch(elementSize) {
  case 1 : m_sortMethod->getMethod()(data.getData(), n, elementSize, CountComparator<BYTE>(          m_compareCount)); break;
  case 2 : m_sortMethod->getMethod()(data.getData(), n, elementSize, CountComparator<unsigned short>(m_compareCount)); break;
  default: m_sortMethod->getMethod()(data.getData(), n, elementSize, CountComparator<unsigned int  >(m_compareCount)); break;
  }
}
Esempio n. 2
0
void BaseTestDataArray::testData() {
    typedef boost::multi_array<double, 3> array_type;
    typedef array_type::index index;
    array_type A(boost::extents[3][4][2]);

    int values = 0;
    for(index i = 0; i != 3; ++i)
        for(index j = 0; j != 4; ++j)
            for(index k = 0; k != 2; ++k)
                A[i][j][k] = values++;

    CPPUNIT_ASSERT_EQUAL(array1.dataType(), nix::DataType::Double);
    CPPUNIT_ASSERT_EQUAL(array1.dataExtent(), nix::NDSize({0, 0, 0}));
    CPPUNIT_ASSERT(array1.getDimension(1) == false);

    array1.setData(A);

    array_type B(boost::extents[1][1][1]);
    array1.getData(B);

    int verify = 0;
    int errors = 0;
    for(index i = 0; i != 3; ++i) {
        for(index j = 0; j != 4; ++j) {
            for(index k = 0; k != 2; ++k) {
                int v = verify++;
                errors += B[i][j][k] != v;
            }
        }
    }

    CPPUNIT_ASSERT_EQUAL(errors, 0);

    DataArray direct = block.createDataArray("da_direct", "double", A);
    array_type Adirect(boost::extents[3][4][2]);
    direct.getData(Adirect);

    errors = 0;
    verify = 0;
    for(index i = 0; i != 3; ++i) {
        for(index j = 0; j != 4; ++j) {
            for(index k = 0; k != 2; ++k) {
                int v = verify++;
                errors += Adirect[i][j][k] != v;
            }
        }
    }

    CPPUNIT_ASSERT_EQUAL(0, errors);

    //test createDataArray overload that takes data but specify an storage datat type
    DataArray directFloat = block.createDataArray("da_direct_int", "int", A, DataType::Int32);
    CPPUNIT_ASSERT_EQUAL(DataType::Int32, directFloat.dataType());
    boost::multi_array<int, 3> AdirectFloat(boost::extents[3][4][2]);
    direct.getData(AdirectFloat);

    errors = 0;
    verify = 0;
    for(index i = 0; i != 3; ++i) {
        for(index j = 0; j != 4; ++j) {
            for(index k = 0; k != 2; ++k) {
                int v = verify++;
                errors += AdirectFloat[i][j][k] != v;
            }
        }
    }

    CPPUNIT_ASSERT_EQUAL(0, errors);

    typedef boost::multi_array<double, 2> array2D_type;
    typedef array_type::index index;
    array2D_type C(boost::extents[5][5]);

    for(index i = 0; i != 5; ++i)
        for(index j = 0; j != 5; ++j)
            C[i][j] = 42.0;

    CPPUNIT_ASSERT_EQUAL(array2.dataExtent(), nix::NDSize({20, 20}));

    array2.setData(C, nix::NDSize({0, 0}));
    array2.dataExtent(nix::NDSize({40, 40}));
    CPPUNIT_ASSERT_EQUAL(array2.dataExtent(), nix::NDSize({40, 40}));

    array2D_type D(boost::extents[5][5]);
    for(index i = 0; i != 5; ++i)
        for(index j = 0; j != 5; ++j)
            D[i][j] = 42.0;


        array2.setData(D, nix::NDSize({ 20, 20 }));

    array2D_type E(boost::extents[1][1]);
    array2.getData(E, nix::NDSize({ 5, 5 }), nix::NDSize({ 20, 20 }));

    for(index i = 0; i != 5; ++i)
        for(index j = 0; j != 5; ++j)
            CPPUNIT_ASSERT_DOUBLES_EQUAL(D[i][j], E[i][j],
                std::numeric_limits<double>::epsilon());


    array2D_type F(boost::extents[5][5]);

    array2.getData(F, nix::NDSize({ 20, 20 }));

    for(index i = 0; i != 5; ++i)
        for(index j = 0; j != 5; ++j)
            CPPUNIT_ASSERT_DOUBLES_EQUAL(D[i][j], F[i][j],
                std::numeric_limits<double>::epsilon());


    nix::DataArray da3 = block.createDataArray("direct-vector",
                                               "double",
                                               nix::DataType::Double,
                                               nix::NDSize({5}));

    CPPUNIT_ASSERT(da3.dataExtent() == nix::NDSize{5});
    CPPUNIT_ASSERT(da3.getDimension(1) == false);

    std::vector<double> dv = {1.0, 2.0, 3.0, 4.0, 5.0};
    da3.setData(nix::DataType::Double, dv.data(), nix::NDSize({ 5 }), nix::NDSize({ 0 }));

    std::vector<double> dvin;
    dvin.resize(5);
    da3.getData(nix::DataType::Double, dvin.data(), nix::NDSize({ 5 }), nix::NDSize({ 0 }));

    for(size_t i = 0; i < dvin.size(); i++) {
        CPPUNIT_ASSERT_DOUBLES_EQUAL(dv[i], dvin[i],
                                     std::numeric_limits<double>::epsilon());
    }

    // test IO of a scalar

    double scalar = -1.0;

    //value + offset + count
    // nix::NDSize({}) indicates a scalar
    scalar = -1.0;
    da3.getData(scalar, nix::NDSize({}), nix::NDSize({0}));
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dv[0], scalar,
                                 std::numeric_limits<double>::epsilon());


    // nix::NDSize({1, 1, ..., 1}) has a number of elemts == 1
    // so that should work too
    scalar = -1.0;
    da3.getData(scalar, nix::NDSize({1}), nix::NDSize({0}));
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dv[0], scalar,
                                 std::numeric_limits<double>::epsilon());

    scalar = -1.0;
    da3.getData(scalar, nix::NDSize({1, 1}), nix::NDSize({0}));
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dv[0], scalar,
                                 std::numeric_limits<double>::epsilon());

    //value + offset
    scalar = -1.0;
    da3.getData(scalar, nix::NDSize({0}));
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dv[0], scalar,
                                 std::numeric_limits<double>::epsilon());

    //TODO: setData(scalar) + getData(scalar)

    DataArray daA = block.createDataArray("append", "double", DataType::Double , NDSize({0, 4, 5}));

    CPPUNIT_ASSERT_THROW(daA.appendData(DataType::Double, nullptr, NDSize({2, 4, 5}), 3), InvalidRank);
    CPPUNIT_ASSERT_THROW(daA.appendData(DataType::Double, nullptr, NDSize({2, 4, 5, 6}), 0), IncompatibleDimensions);
    CPPUNIT_ASSERT_THROW(daA.appendData(DataType::Double, nullptr, NDSize({4, 4, 6}), 0), IncompatibleDimensions);

    std::vector<double> append_data(3*4*5, 1);

    daA.appendData(DataType::Double, append_data.data(), {3, 4, 5}, 0);
    CPPUNIT_ASSERT_EQUAL(NDSize({3, 4, 5}), daA.dataExtent());

    std::fill(append_data.begin(), append_data.end(), 2);

    daA.appendData(DataType::Double, append_data.data(), {3, 4, 5}, 0);
    CPPUNIT_ASSERT_EQUAL(NDSize({6, 4, 5}), daA.dataExtent());

    std::vector<double> append_check(6*4*5, 0);

    daA.getData(DataType::Double, append_check.data(), {6, 4, 5}, {});

    for(size_t i = 0; i < 6*4*5; i++) {
        CPPUNIT_ASSERT_EQUAL(i > 3*4*5-1 ? 2.0 : 1.0, append_check[i]);
    }

}
Esempio n. 3
0
void getOffsetAndCount(const MultiTag &tag, const DataArray &array, size_t index, NDSize &offsets, NDSize &counts) {
    DataArray positions = tag.positions();
    DataArray extents = tag.extents();
    NDSize position_size, extent_size;
    size_t dimension_count = array.dimensionCount();

    if (positions) {
        position_size = positions.dataExtent();
    }

    if (extents) {
        extent_size = extents.dataExtent();
    }

    if (!positions || index >= position_size[0]) {
        throw nix::OutOfBounds("Index out of bounds of positions!", 0);
    }

    if (extents && index >= extent_size[0]) {
        throw nix::OutOfBounds("Index out of bounds of positions or extents!", 0);
    }
    
    if (position_size.size() == 1 && dimension_count != 1) {
        throw nix::IncompatibleDimensions("Number of dimensions in positions does not match dimensionality of data", 
                                          "util::getOffsetAndCount");
    }

    if (position_size.size() > 1 && position_size[1] > dimension_count) {
        throw nix::IncompatibleDimensions("Number of dimensions in positions does not match dimensionality of data",
                                          "util::getOffsetAndCount");
    }
    
    if (extents && extent_size.size() > 1 && extent_size[1] > dimension_count) {
        throw nix::IncompatibleDimensions("Number of dimensions in extents does not match dimensionality of data",
                                          "util::getOffsetAndCount");
    }

    NDSize temp_offset = NDSize{static_cast<NDSize::value_type>(index), static_cast<NDSize::value_type>(0)};
    NDSize temp_count{static_cast<NDSize::value_type>(1), static_cast<NDSize::value_type>(dimension_count)};
    vector<double> offset;
    positions.getData(offset, temp_count, temp_offset);

    NDSize data_offset(dimension_count, static_cast<size_t>(0));
    NDSize data_count(dimension_count, static_cast<size_t>(1));
    vector<string> units = tag.units();
    
    for (size_t i = 0; i < offset.size(); ++i) {
        Dimension dimension = array.getDimension(i+1);
        string unit = "none";
        if (i <= units.size() && units.size() > 0) {
            unit = units[i];
        }
        data_offset[i] = positionToIndex(offset[i], unit, dimension);
    }
    
    if (extents) {
        vector<double> extent;
        extents.getData(extent, temp_count, temp_offset);
        for (size_t i = 0; i < extent.size(); ++i) {
            Dimension dimension = array.getDimension(i+1);
            string unit = "none";
            if (i <= units.size() && units.size() > 0) {
                unit = units[i];
            }
            ndsize_t c = positionToIndex(offset[i] + extent[i], unit, dimension) - data_offset[i];
            data_count[i] = (c > 1) ? c : 1;
        }
    }

    offsets = data_offset;
    counts = data_count;
}