Browse Source

vector sum_if

Former-commit-id: 67b2ef9ff6
tempestpy_adaptions
sjunges 9 years ago
parent
commit
707a4f500b
  1. 16
      src/utility/vector.h
  2. 14
      test/functional/utility/VectorTest.cpp

16
src/utility/vector.h

@ -249,6 +249,22 @@ namespace storm {
return filter(values, fnc);
}
/**
* Sum the entries from values that are set to one in the filter vector.
* @param values
* @param filter
* @return The sum of the values with a corresponding one in the filter.
*/
template<typename VT>
VT sum_if(std::vector<VT> const& values, storm::storage::BitVector const& filter) {
assert(values.size() >= filter.size());
VT sum = storm::utility::zero<VT>();
for(uint_fast64_t i : filter) {
sum += values[i];
}
return sum;
}
/*!
* Reduces the given source vector by selecting an element according to the given filter out of each row group.
*

14
test/functional/utility/VectorTest.cpp

@ -0,0 +1,14 @@
#include "gtest/gtest.h"
#include "storm-config.h"
#include "src/storage/BitVector.h"
#include "src/utility/vector.h"
TEST(VectorTest, sum_if) {
std::vector<double> a = {1.0, 2.0, 4.0, 8.0, 16.0};
storm::storage::BitVector f1(5, {2,4});
storm::storage::BitVector f2(5, {3,4});
ASSERT_EQ(20.0, storm::utility::vector::sum_if(a, f1));
ASSERT_EQ(24.0, storm::utility::vector::sum_if(a, f2));
}
Loading…
Cancel
Save