Skip to content

Commit

Permalink
[dimension/indexof] adds a strict flag to index of function of RangeD…
Browse files Browse the repository at this point in the history
…im ...

This is to define whether invalid start - end ranges are silently ignored
or in an exception should be thrown
  • Loading branch information
jgrewe committed Jul 28, 2020
1 parent 7519868 commit 7bded05
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
8 changes: 7 additions & 1 deletion include/nix/Dimensions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,12 +789,18 @@ class NIXAPI RangeDimension : public base::ImplContainer<base::IRangeDimension>
*
* @param start_positions Vector of start positions
* @param end_positions Vector of end positions
* @param match Enum entry that defines whether the range is inclusive
* or exclusive regarding the end position
* @param strict Bool that indicates whether the function will throw an
* OutOfBounds exception when an invalid range occurred or
* if such ranges are silently ignored. Default is true.
*
* @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,
RangeMatch match = RangeMatch::Inclusive) const;
RangeMatch match = RangeMatch::Inclusive,
bool strict = true) const;


/**
Expand Down
11 changes: 7 additions & 4 deletions src/Dimensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ boost::optional<ndsize_t> getIndex(const double position, std::vector<double> &t
idx = 0;
return idx;
} else if (position > *prev(ticks.end())) {
if (matching == PositionMatch::Less || matching == PositionMatch::LessOrEqual)
if (matching == PositionMatch::Less || matching == PositionMatch::LessOrEqual)
idx = prev(ticks.end()) - ticks.begin();
return idx;
}
Expand Down Expand Up @@ -467,10 +467,10 @@ boost::optional<std::pair<ndsize_t, ndsize_t>> RangeDimension::indexOf(double st
if (start > end){
std::swap(start, end);
}

boost::optional<ndsize_t> si = getIndex(start, ticks, PositionMatch::GreaterOrEqual);
if (!si) {
return range;
return range;
}
PositionMatch endMatching = (match == RangeMatch::Inclusive) ? PositionMatch::LessOrEqual : PositionMatch::Less;
boost::optional<ndsize_t> ei = getIndex(end, ticks, endMatching);
Expand Down Expand Up @@ -505,7 +505,7 @@ 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,
RangeMatch match) const {
RangeMatch match, bool strict) const {
if (start_positions.size() != end_positions.size()) {
throw runtime_error("Dimension::IndexOf - Number of start and end positions must match!");
}
Expand All @@ -516,6 +516,9 @@ std::vector<std::pair<ndsize_t, ndsize_t>> RangeDimension::indexOf(const std::ve
for (size_t i = 0; i < start_positions.size(); ++i) {
boost::optional<std::pair<ndsize_t, ndsize_t>> range;
range = this->indexOf(start_positions[i], end_positions[i], std::move(ticks), match);
if (!range && strict) {
throw nix::OutOfBounds("RangeDimension::indexOf an invalid range occurred!");
}
if (range) {
indices.push_back(*range);
}
Expand Down
11 changes: 6 additions & 5 deletions test/BaseTestDimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ void BaseTestDimension::testRangeDimIndexOfOld() {
CPPUNIT_ASSERT(rd.indexOf(-10.0) == 1);
CPPUNIT_ASSERT(rd.indexOf(-5.0) == 1);
CPPUNIT_ASSERT(rd.indexOf(5.0) == 2);

CPPUNIT_ASSERT_NO_THROW(rd.indexOf(257.28));
CPPUNIT_ASSERT_THROW(rd.indexOf(257.28, false), nix::OutOfBounds);
CPPUNIT_ASSERT_THROW(rd.indexOf(-257.28), nix::OutOfBounds);
Expand All @@ -431,7 +431,7 @@ void BaseTestDimension::testRangeDimIndexOfOld() {
CPPUNIT_ASSERT(range.first == 0 && range.second == 4);
range = rd.indexOf(-200., 200.);
CPPUNIT_ASSERT(range.first == 0 && range.second == 4);

CPPUNIT_ASSERT_THROW(rd.indexOf({-100.0, -90, 0.0}, {10.}), std::runtime_error);
CPPUNIT_ASSERT_NO_THROW(rd.indexOf({-100.0, 20.0, 40.0}, {-45, 120., 100.}));
CPPUNIT_ASSERT(rd.indexOf({-100.0, 20.0, 40.0}, {-45, 120., 100.}).size() == 3);
Expand All @@ -453,13 +453,13 @@ void BaseTestDimension::testRangeDimIndexOf() {
CPPUNIT_ASSERT(!rd.indexOf(-110., PositionMatch::LessOrEqual));
CPPUNIT_ASSERT(!rd.indexOf(-110., PositionMatch::Less));
CPPUNIT_ASSERT(!rd.indexOf(-110., PositionMatch::Equal));

CPPUNIT_ASSERT(*rd.indexOf(-100., PositionMatch::GreaterOrEqual) == 0);
CPPUNIT_ASSERT(*rd.indexOf(-100., PositionMatch::Greater) == 1);
CPPUNIT_ASSERT(*rd.indexOf(-100., PositionMatch::LessOrEqual) == 0);
CPPUNIT_ASSERT(!rd.indexOf(-100., PositionMatch::Less));
CPPUNIT_ASSERT(*rd.indexOf(-100., PositionMatch::Equal) == 0);

CPPUNIT_ASSERT(*rd.indexOf(-50., PositionMatch::GreaterOrEqual) == 1);
CPPUNIT_ASSERT(*rd.indexOf(-50., PositionMatch::Greater) == 1);
CPPUNIT_ASSERT(*rd.indexOf(-50., PositionMatch::LessOrEqual) == 0);
Expand Down Expand Up @@ -517,7 +517,8 @@ void BaseTestDimension::testRangeDimIndexOf() {
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_THROW(rd.indexOf({40., -100., -100.}, {100., 100., 101.}, RangeMatch::Exclusive), nix::OutOfBounds);
ranges = rd.indexOf({40., -100., -100.}, {100., 100., 101.}, RangeMatch::Exclusive, false);
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);
Expand Down

0 comments on commit 7bded05

Please sign in to comment.