summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2015-08-11 11:00:28 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2015-08-11 11:00:28 -0400
commite27ed7e95658c16f16d24e55e788947a359f3877 (patch)
treed881b3d646a7510c7f715cf63323b644aba46621
parent32032147aa5264987742f15f28889edfd117fd86 (diff)
Optimized C++ version.
-rw-r--r--scheduling/scheduling.cc17
1 files changed, 6 insertions, 11 deletions
diff --git a/scheduling/scheduling.cc b/scheduling/scheduling.cc
index bd1f6bb..6b03e31 100644
--- a/scheduling/scheduling.cc
+++ b/scheduling/scheduling.cc
@@ -91,7 +91,7 @@ template <typename GeneratorT>
vector<std::tuple<string, string>> GenerateMatches(
GeneratorT generator,
vector<Player> players) {
- std::list<std::tuple<int, int>> possible_matches;
+ std::vector<std::tuple<int, int>> possible_matches;
for (int i = 0; i < players.size(); i++) {
for (int j = 0; j < players.size(); j++) {
// X vs. y and y vs. x is the same match. Also excludes x vs. x.
@@ -102,18 +102,13 @@ vector<std::tuple<string, string>> GenerateMatches(
}
}
}
+ std::shuffle(possible_matches.begin(), possible_matches.end(),
+ generator);
vector<std::tuple<string, string>> result;
- while (!possible_matches.empty()) {
- std::uniform_int_distribution<int> distribution(
- 0, possible_matches.size() - 1);
- int i = distribution(generator);
- auto it = possible_matches.begin();
- std::advance(it, i);
- Player* p1 = &players[std::get<0>(*it)];
- Player* p2 = &players[std::get<1>(*it)];
- possible_matches.erase(it);
-
+ for (const auto&m : possible_matches) {
+ Player* p1 = &players[std::get<0>(m)];
+ Player* p2 = &players[std::get<1>(m)];
if (p1->desired_games_ && p2->desired_games_) {
result.push_back(std::tuple<string, string>(p1->id_, p2->id_));
p1->desired_games_--;