diff --git a/src/utility/vector.h b/src/utility/vector.h index bd21028f9..dd0ac7cf3 100644 --- a/src/utility/vector.h +++ b/src/utility/vector.h @@ -366,6 +366,47 @@ namespace storm { return sum; } + /** + * Computes the maximum of the entries from the values that are set to one in the filter vector + * @param values + * @param filter + * @param smallestPossibleValue A value which is not larger than any value in values. If the filter is empty, this value is returned. + * @return The maximum over the subset of the values and the smallestPossibleValue. + */ + template + VT max_if(std::vector const& values, storm::storage::BitVector const& filter, VT const& smallestPossibleValue) { + assert(values.size() >= filter.size()); + + VT max = smallestPossibleValue; + for(uint_fast64_t i : filter) { + if(values[i] > max) { + max = values[i]; + } + } + return max; + } + + /** + * Computes the minimum of the entries from the values that are set to one in the filter vector + * @param values + * @param filter + * @param largestPossibleValue A value which is not smaller than any value in values. If the filter is empty, this value is returned. + * @return The minimum over the subset of the values and the largestPossibleValue. + */ + template + VT min_if(std::vector const& values, storm::storage::BitVector const& filter, VT const& largestPossibleValue) { + assert(values.size() >= filter.size()); + VT min = largestPossibleValue; + for(uint_fast64_t i : filter) { + if(values[i] < min) { + min = values[i]; + } + } + return min; + } + + + /*! * Reduces the given source vector by selecting an element according to the given filter out of each row group. * diff --git a/test/functional/utility/VectorTest.cpp b/test/functional/utility/VectorTest.cpp index 33f3c5cfe..9494d6146 100644 --- a/test/functional/utility/VectorTest.cpp +++ b/test/functional/utility/VectorTest.cpp @@ -12,3 +12,22 @@ TEST(VectorTest, sum_if) { ASSERT_EQ(24.0, storm::utility::vector::sum_if(a, f2)); } + +TEST(VectorTest, max_if) { + std::vector a = {1.0, 2.0, 34.0, 8.0, 16.0}; + storm::storage::BitVector f1(5, {2,4}); + storm::storage::BitVector f2(5, {3,4}); + + ASSERT_EQ(34.0, storm::utility::vector::max_if(a,f1,0.0)); + ASSERT_EQ(16.0, storm::utility::vector::max_if(a,f2,0.0)); +} + + +TEST(VectorTest, min_if) { + std::vector a = {1.0, 2.0, 34.0, 8.0, 16.0}; + storm::storage::BitVector f1(5, {2,4}); + storm::storage::BitVector f2(5, {3,4}); + + ASSERT_EQ(16.0, storm::utility::vector::min_if(a,f1,100.0)); + ASSERT_EQ(8.0, storm::utility::vector::min_if(a,f2,100.0)); +}