diff --git a/src/server/sparkd/cmds/spartan/market_data/utils/tests/synthetic_book_tests.cpp b/src/server/sparkd/cmds/spartan/market_data/utils/tests/synthetic_book_tests.cpp index f593bb74d..1b72862c6 100644 --- a/src/server/sparkd/cmds/spartan/market_data/utils/tests/synthetic_book_tests.cpp +++ b/src/server/sparkd/cmds/spartan/market_data/utils/tests/synthetic_book_tests.cpp @@ -1,6 +1,6 @@ #include "server/sparkd/cmds/spartan/market_data/utils/book_utils.h" #include "server/sparkd/cmds/spartan/market_data/utils/mdp3_utils.h" -#include "server/sparkd/cmds/spartan/utils/permutation.h" +#include "server/sparkd/cmds/spartan/utils/Permutation.h" #include #include @@ -375,7 +375,7 @@ void test_synth_book(LegMarkets synth) { auto expected = expected_synth_book(synth); // test every permutation of the legs. All permutations of a particular // set of legs should produce the same synthetic book. - auto perms = bts::spartan::utils::permutation::all_permutations(synth); + auto perms = bts::spartan::Permutation::all(synth); for (const auto &p : perms) { SmallBook book_out; bts::spartan::mdp3::book_utils::compute_synthetic_book(book_out, p, 10); diff --git a/src/server/sparkd/cmds/spartan/utils/Permutation.cc b/src/server/sparkd/cmds/spartan/utils/Permutation.cc new file mode 100644 index 000000000..afb876d7c --- /dev/null +++ b/src/server/sparkd/cmds/spartan/utils/Permutation.cc @@ -0,0 +1,78 @@ +#include "Permutation.h" + +namespace bts::spartan { + +/** + * Recursively produce permutations of the specified values. + * + * @param[in] values The values to permute. + * @param[out] perms The vector to add permutations to + * @param[in] perm_index The index to permutate in this particular call + */ +template +static void _all( + const std::vector &values, + std::vector> &perms, + size_t perm_index = 0) { + // Sub-permutations are created recursively, beginning with the length 1 + // permutation of the last value in the vector. With each recursive call, + // 'perm_index' is incremented until it points to the last element in the + // vector. Then, the first sub-permutation (the last element in the vector) + // is added to 'perms,' and execution begins to move back up the stack, where + // 'perm_index' points to the second to last element. + // + // Next, for each sub-permutation (only 1 so far), the second to last element + // is inserted in every possible position, multiplying the number of + // permutations factorially (since the first sub-permutation was of length 1, + // there are 2 places to insert this element (index 0, or 1). Now there are + // 1 * 2 = 2! sub-permutations of length 2. + // + // Then execution continues to the previous level of the stack, where + // 'perm_index' points to the third-to-last element of the vector. For each + // sub-permutation (2 so far), this third-to-last element can be inserted in + // 3 different places (index 0, 1, or 2). Now there are 1 * 2 * 3 = 3! + // sub-permutations of length 3. + // + // When execution finally reaches the first level of the call stack, the + // first element in the vector (of length n) is inserted into every index for + // each of the (n-1)! sub-permutations of the n-1 other elements. Now there + // are 1 * 2 * ... * (n - 1) * n = n! permutations of length n. + if (perm_index == values.size() - 1) { + // Add the first permutation (of length 1), and let the recursive calls + // unwind. + perms.emplace_back(std::vector{values.back()}); + } else if (perm_index < values.size()) { + std::vector> sub_perms; + // Call recursively until 'perm_index' == values.size() - 1 + _all(values, sub_perms, perm_index + 1); + // For each sub-permutation, insert the new element (at + // values[perm_index]) into each of the 'values.length() - 1' positions to + // produce 'values.length() - 1' new permutations per sub-permutation. + // The number of permutations grows factorially, as 'values.length() - 1' + // increments by 1 each time execution returns to the previous level of + // the callstack. + for (auto sp : sub_perms) { + for (uint32_t i = 0; i < values.size() - perm_index; i++) { + sp.insert(sp.begin() + i, values[perm_index]); + perms.emplace_back(sp); + sp.erase(sp.begin() + i); + } + } + } +} + +template +inline void Permutation::all( + const std::vector &values, std::vector> &perms) { + _all(values, perms); +} + +template +inline std::vector> +Permutation::all(const std::vector &values) { + std::vector> perms; + _all(values, perms); + return perms; +} + +} // namespace bts::spartan diff --git a/src/server/sparkd/cmds/spartan/utils/Permutation.h b/src/server/sparkd/cmds/spartan/utils/Permutation.h new file mode 100644 index 000000000..cdc31f04c --- /dev/null +++ b/src/server/sparkd/cmds/spartan/utils/Permutation.h @@ -0,0 +1,30 @@ +#pragma once +#include + +namespace bts::spartan { + +struct Permutation { + /** + * Add all permutations of the specified values to a vector. + * + * @param[in] values The values to permute + * @param[out] permutations The vector to add permutations to + */ + template + static void + all(const std::vector &values, std::vector> &permutations); + + /** + * Add all permutations of the specified values to a vector. + * + * @overload + * @param[in] values The values to permute + * @return The vector of permutations + */ + template + static std::vector> all(const std::vector &values); +}; + +} // namespace bts::spartan + +#include "Permutation.cc" diff --git a/src/server/sparkd/cmds/spartan/utils/permutation.h b/src/server/sparkd/cmds/spartan/utils/permutation.h deleted file mode 100644 index e55887317..000000000 --- a/src/server/sparkd/cmds/spartan/utils/permutation.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once -#include -#include - -namespace bts::spartan::utils::permutation { - -template -inline void all_permutations( - const std::vector &values, - std::vector> &perms, - uint32_t depth = 0) { - if (depth == values.size() - 1) { - perms.emplace_back(std::vector{values.back()}); - } else if (depth < values.size()) { - std::vector> sub_perms; - all_permutations(values, sub_perms, depth + 1); - for (auto sp : sub_perms) { - for (uint32_t i = 0; i < values.size() - depth; i++) { - sp.insert(sp.begin() + i, values[depth]); - perms.emplace_back(sp); - sp.erase(sp.begin() + i); - } - } - } -} - -template -inline std::vector> -all_permutations(const std::vector &values, uint32_t depth = 0) { - std::vector> perms; - all_permutations(values, perms, depth); - return perms; -} -} // namespace bts::spartan::utils::permutation \ No newline at end of file