refactor: Rename 'permutation.h' -> 'Permutation.h/cc'

Also refactor the namespace 'all_permutations' methods to be static
methods in the newly created class 'Permutation.'  This is part of
the effort to favor static class methods over namespaced functions.
This commit is contained in:
John Colvin committed 2021-02-01 22:56:21 -06:00
1 parent 1770274dd0
commit d18a345b5b
4 files changed
+110 -36

No files matched your search

@@ -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 <catch/catch.hpp>
#include <limits>
@@ -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);
@@ -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 <class T>
static void _all(
const std::vector<T> &values,
std::vector<std::vector<T>> &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<T>{values.back()});
} else if (perm_index < values.size()) {
std::vector<std::vector<T>> 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 <class T>
inline void Permutation::all(
const std::vector<T> &values, std::vector<std::vector<T>> &perms) {
_all(values, perms);
}
template <class T>
inline std::vector<std::vector<T>>
Permutation::all(const std::vector<T> &values) {
std::vector<std::vector<T>> perms;
_all(values, perms);
return perms;
}
} // namespace bts::spartan
@@ -0,0 +1,30 @@
#pragma once
#include <vector>
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 <class T>
static void
all(const std::vector<T> &values, std::vector<std::vector<T>> &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 <class T>
static std::vector<std::vector<T>> all(const std::vector<T> &values);
};
} // namespace bts::spartan
#include "Permutation.cc"
@@ -1,34 +0,0 @@
#pragma once
#include <string>
#include <vector>
namespace bts::spartan::utils::permutation {
template <class T>
inline void all_permutations(
const std::vector<T> &values,
std::vector<std::vector<T>> &perms,
uint32_t depth = 0) {
if (depth == values.size() - 1) {
perms.emplace_back(std::vector<T>{values.back()});
} else if (depth < values.size()) {
std::vector<std::vector<T>> 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 <class T>
inline std::vector<std::vector<T>>
all_permutations(const std::vector<T> &values, uint32_t depth = 0) {
std::vector<std::vector<T>> perms;
all_permutations(values, perms, depth);
return perms;
}
} // namespace bts::spartan::utils::permutation