diff --git a/src/storm/utility/vector.h b/src/storm/utility/vector.h index 82d698dde..2721ee381 100644 --- a/src/storm/utility/vector.h +++ b/src/storm/utility/vector.h @@ -1170,6 +1170,16 @@ namespace storm { return result; } + template + std::vector applyInversePermutation(std::vector const& inversePermutation, std::vector const& source) { + std::vector result; + result.reserve(source.size()); + for (uint64_t sourceIndex : inversePermutation) { + result.push_back(source[sourceIndex]); + } + return result; + } + /*! * Output vector as string. * diff --git a/src/test/storm/utility/VectorTest.cpp b/src/test/storm/utility/VectorTest.cpp index ffcdf6cef..bdabda3c8 100644 --- a/src/test/storm/utility/VectorTest.cpp +++ b/src/test/storm/utility/VectorTest.cpp @@ -31,3 +31,13 @@ TEST(VectorTest, min_if) { ASSERT_EQ(16.0, storm::utility::vector::min_if(a, f1)); ASSERT_EQ(8.0, storm::utility::vector::min_if(a, f2)); } + +TEST(VectorTest, permute) { + std::vector a = {1.0, 2.0, 3.0, 4.0}; + std::vector inversePermutation = {0, 3, 1, 2}; + std::vector aperm = storm::utility::vector::applyInversePermutation(inversePermutation, a); + EXPECT_EQ(aperm[0], a[0]); + EXPECT_EQ(aperm[1], a[3]); + EXPECT_EQ(aperm[2], a[1]); + EXPECT_EQ(aperm[3], a[2]); +} \ No newline at end of file