Skip to content

Commit

Permalink
Add utility function "getStartAndEndOfOpenInterval".
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-rapp committed Feb 28, 2024
1 parent 070bbfa commit 6a954e5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 64 deletions.
34 changes: 33 additions & 1 deletion cpp/subprojects/common/include/mlrl/common/input/interval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
#pragma once

#include "mlrl/common/data/types.hpp"
#include "mlrl/common/data/tuple.hpp"

/**
* Specifies the boundaries of an interval that includes/excludes certain elements in a vector.
Expand Down Expand Up @@ -51,3 +51,35 @@ struct Interval {
return *this;
}
};

/**
* Returns the start and end index of an open interval `[0, interval.end]` or `[interval.start, maxIndex]`, depending on
* a given `Interval`.
*
* @param interval A reference to an object of type `Interval`
* @param maxIndex The maximum index of an open interval
* @return A `Tuple` that stores the start and end index
*/
static inline Tuple<uint32> getStartAndEndOfOpenInterval(const Interval& interval, uint32 maxIndex) {
Tuple<uint32> tuple;

if (interval.inverse) {
if (interval.start > 0) {
tuple.first = 0;
tuple.second = interval.start;
} else {
tuple.first = interval.end;
tuple.second = maxIndex;
}
} else {
tuple.first = interval.start;

if (tuple.first > 0) {
tuple.second = maxIndex;
} else {
tuple.second = interval.end;
}
}

return tuple;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,9 @@ template<typename Decorator>
static inline std::optional<BinnedFeatureVector> createFilteredBinnedFeatureVectorView(
const Decorator& decorator, std::unique_ptr<IFeatureVector>& existing, const Interval& interval) {
const BinnedFeatureVector& featureVector = decorator.getView().firstView;
uint32 start;
uint32 end;

if (interval.inverse) {
if (interval.start > 0) {
start = 0;
end = interval.start;
} else {
start = interval.end;
end = featureVector.numBins;
}
} else {
start = interval.start;

if (start > 0) {
end = featureVector.numBins;
} else {
end = interval.end;
}
}

Tuple<uint32> tuple = getStartAndEndOfOpenInterval(interval, featureVector.numBins);
uint32 start = tuple.first;
uint32 end = tuple.second;
uint32 numFilteredBins = end - start;

if (numFilteredBins > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,9 @@ template<typename Decorator>
static inline std::optional<NumericalFeatureVector> createFilteredNumericalFeatureVectorView(
const Decorator& decorator, std::unique_ptr<IFeatureVector>& existing, const Interval& interval) {
const NumericalFeatureVector& featureVector = decorator.getView().firstView;
uint32 start = interval.start;
uint32 end = interval.end;

if (interval.inverse) {
if (interval.start > 0) {
start = 0;
end = interval.start;
} else {
start = interval.end;
end = featureVector.numElements;
}
} else {
start = interval.start;

if (start > 0) {
end = featureVector.numElements;
} else {
end = interval.end;
}
}

Tuple<uint32> tuple = getStartAndEndOfOpenInterval(interval, featureVector.numElements);
uint32 start = tuple.first;
uint32 end = tuple.second;
uint32 numFilteredElements = end - start;

if (numFilteredElements > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,9 @@ template<typename Decorator>
static inline std::optional<NominalFeatureVector> createFilteredOrdinalFeatureVectorView(
const Decorator& decorator, std::unique_ptr<IFeatureVector>& existing, const Interval& interval) {
const NominalFeatureVector& featureVector = decorator.getView().firstView;
uint32 start;
uint32 end;

if (interval.inverse) {
if (interval.start > 0) {
start = 0;
end = interval.start;
} else {
start = interval.end;
end = featureVector.numValues;
}
} else {
start = interval.start;

if (start > 0) {
end = featureVector.numValues;
} else {
end = interval.end;
}
}

Tuple<uint32> tuple = getStartAndEndOfOpenInterval(interval, featureVector.numValues);
uint32 start = tuple.first;
uint32 end = tuple.second;
uint32 numFilteredValues = end - start;

if (numFilteredValues > 0) {
Expand Down

0 comments on commit 6a954e5

Please sign in to comment.