src/utilities/math/Permutation.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #ifndef UTILITIES_MATH_PERMUTATION_HPP | ||
| 7 | #define UTILITIES_MATH_PERMUTATION_HPP | ||
| 8 | |||
| 9 | #include "../core/Assert.hpp" | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | namespace openstudio { | ||
| 13 | |||
| 14 | /** Returns the permutation that transforms sequence container original into newOrder. | ||
| 15 | * T is required to have .size() and [i] defined. The type of element held by T must | ||
| 16 | * have == defined. original and newOrder should contain the same elements, jut in a | ||
| 17 | * different order. */ | ||
| 18 | template <class T> | ||
| 19 | 1 | std::vector<unsigned> permutation(const T& original, const T& newOrder) { | |
| 20 | |||
| 21 |
1/3✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 5 not taken.
|
1 | OS_ASSERT(original.size() == newOrder.size()); |
| 22 | |||
| 23 |
1/1✓ Branch 2 taken 1 time.
|
2 | std::vector<unsigned> result(original.size()); |
| 24 |
1/1✓ Branch 2 taken 1 time.
|
1 | std::vector<bool> usedAsJ(newOrder.size(), false); |
| 25 | 1 | unsigned start = 0; | |
| 26 | |||
| 27 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
|
5 | for (unsigned i = 0; i < original.size(); ++i) { |
| 28 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | for (unsigned j = start; j < newOrder.size(); ++j) { |
| 29 |
6/7✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
✓ Branch 5 taken 4 times.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 4 times.
✓ Branch 11 taken 2 times.
|
6 | if ((original[i] == newOrder[j]) && (!usedAsJ[j])) { |
| 30 | 4 | result[i] = j; | |
| 31 |
1/1✓ Branch 1 taken 4 times.
|
4 | usedAsJ[j] = true; |
| 32 |
7/7✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 4 times.
|
8 | while ((start < newOrder.size()) && (usedAsJ[start])) { |
| 33 | 4 | ++start; | |
| 34 | } | ||
| 35 | 4 | break; | |
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | 2 | return result; | |
| 41 | 1 | } | |
| 42 | |||
| 43 | /** Returns the contents of original reorders as per order. That is, result[order[i]] = | ||
| 44 | * original[i]. T is required to have .size() and [i] defined. T must also be constructable | ||
| 45 | * from .size(). */ | ||
| 46 | template <class T> | ||
| 47 | 1 | T permute(const T& original, const std::vector<unsigned>& order) { | |
| 48 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
|
1 | OS_ASSERT(original.size() == order.size()); |
| 49 |
1/1✓ Branch 2 taken 1 time.
|
1 | T result(original.size()); |
| 50 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
|
5 | for (unsigned i = 0; i < original.size(); ++i) { |
| 51 | 4 | result[order[i]] = original[i]; | |
| 52 | } | ||
| 53 | 1 | return result; | |
| 54 | } | ||
| 55 | |||
| 56 | } // namespace openstudio | ||
| 57 | |||
| 58 | #endif // UTILITIES_MATH_PERMUTATION_HPP | ||
| 59 |