Skip to content

Commit

Permalink
[dimension] changes indexOf(vector starts, vector ends, ...
Browse files Browse the repository at this point in the history
to use the optional variants of indexOf, invalid ranges are "silently"
ignored
  • Loading branch information
jgrewe committed May 7, 2020
1 parent f2eba0b commit 5876f31
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
3 changes: 2 additions & 1 deletion include/nix/Dimensions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,8 @@ class NIXAPI RangeDimension : public base::ImplContainer<base::IRangeDimension>
* @return Vector of pairs of start and end indices.
*/
std::vector<std::pair<ndsize_t, ndsize_t>> indexOf(const std::vector<double> &start_positions,
const std::vector<double> &end_positions) const;
const std::vector<double> &end_positions,
RangeMatch match = RangeMatch::Inclusive) const;


/**
Expand Down
11 changes: 7 additions & 4 deletions src/Dimensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ PositionInRange RangeDimension::positionInRange(const double position) const {
}


return index;
boost::optional<ndsize_t> getIndex(const double position, std::vector<double> &ticks, PositionMatch matching) {
boost::optional<ndsize_t> idx;
// check easy cases first ...
Expand Down Expand Up @@ -505,7 +504,8 @@ pair<ndsize_t, ndsize_t> RangeDimension::indexOf(const double start, const doubl


std::vector<std::pair<ndsize_t, ndsize_t>> RangeDimension::indexOf(const std::vector<double> &start_positions,
const std::vector<double> &end_positions) const {
const std::vector<double> &end_positions,
RangeMatch match) const {
if (start_positions.size() != end_positions.size()) {
throw runtime_error("Dimension::IndexOf - Number of start and end positions must match!");
}
Expand All @@ -514,8 +514,11 @@ std::vector<std::pair<ndsize_t, ndsize_t>> RangeDimension::indexOf(const std::ve
vector<double> ticks = this->ticks();

for (size_t i = 0; i < start_positions.size(); ++i) {
indices.emplace_back(getIndex(start_positions[i], ticks, true),
getIndex(end_positions[i], ticks, false));
boost::optional<std::pair<ndsize_t, ndsize_t>> range;
range = this->indexOf(start_positions[i], end_positions[i], ticks, match);
if (range) {
indices.push_back(*range);
}
}
return indices;
}
Expand Down
37 changes: 37 additions & 0 deletions test/BaseTestDimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,43 @@ void BaseTestDimension::testRangeDimIndexOf() {
CPPUNIT_ASSERT(*rd.indexOf(110., PositionMatch::Less) == 4);
CPPUNIT_ASSERT(!rd.indexOf(110., PositionMatch::Equal));

boost::optional<std::pair<ndsize_t, ndsize_t>> range;
range = rd.indexOf(40., 100., {}, RangeMatch::Inclusive);
CPPUNIT_ASSERT(range && (*range).first == 4 && (*range).second == 4);
range = rd.indexOf(40., 100., {}, RangeMatch::Exclusive);
CPPUNIT_ASSERT(!range);
range = rd.indexOf(-100., -45., {}, RangeMatch::Inclusive);
CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 0);
range = rd.indexOf(-100., -45., {}, RangeMatch::Exclusive);
CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 0);
range = rd.indexOf(10., 120., {}, RangeMatch::Inclusive);
CPPUNIT_ASSERT(range && (*range).first == 3 && (*range).second == 4);
range = rd.indexOf(10., 120., {}, RangeMatch::Exclusive);
CPPUNIT_ASSERT(range && (*range).first == 3 && (*range).second == 4);
range = rd.indexOf(100., 120., {}, RangeMatch::Inclusive);
CPPUNIT_ASSERT(range && (*range).first == 4 && (*range).second == 4);
range = rd.indexOf(100., 120., {}, RangeMatch::Exclusive);
CPPUNIT_ASSERT(range && (*range).first == 4 && (*range).second == 4);
range = rd.indexOf(110., 150., {}, RangeMatch::Exclusive);
CPPUNIT_ASSERT(!range);
range = rd.indexOf(100., -100., {}, RangeMatch::Inclusive);
CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 4);
range = rd.indexOf(100., -100., {}, RangeMatch::Exclusive);
CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 3);

range = rd.indexOf(100., -100., rd.ticks(), RangeMatch::Exclusive);
CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 3);

std::vector<std::pair<ndsize_t, ndsize_t>> ranges;
ranges = rd.indexOf({40., -100.}, {100., 100.}, RangeMatch::Inclusive);
CPPUNIT_ASSERT(ranges.size() == 2);
CPPUNIT_ASSERT(ranges[0].first == 4 && ranges[0].second == 4);
CPPUNIT_ASSERT(ranges[1].first == 0 && ranges[1].second == 4);

ranges = rd.indexOf({40., -100., -100.}, {100., 100., 101.}, RangeMatch::Exclusive);
CPPUNIT_ASSERT(ranges.size() == 2);
CPPUNIT_ASSERT(ranges[0].first == 0 && ranges[0].second == 3);
CPPUNIT_ASSERT(ranges[1].first == 0 && ranges[1].second == 4);
data_array.deleteDimensions();
}

Expand Down

0 comments on commit 5876f31

Please sign in to comment.