Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions includes/RhythmGameUtilities/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <regex>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

#include "Utilities.h"
Expand Down Expand Up @@ -110,6 +111,29 @@ std::vector<std::string> Split(const char *contents, const char delimiter)
return parts;
}

std::vector<std::tuple<int, int>>
GenerateAdjacentKeyPairs(std::map<int, int> keyValuePairs)
{
auto adjacentKeyPairs = std::vector<std::tuple<int, int>>();

std::vector<int> keys;

for (auto item : keyValuePairs)
{
keys.push_back(item.first);
}

std::vector<int> sortedKeys(keys.begin(), keys.end());

for (auto i = 0; i < size(sortedKeys) - 1; i += 1)
{
adjacentKeyPairs.push_back(
std::make_tuple(sortedKeys[i], sortedKeys[i + 1]));
}

return adjacentKeyPairs;
}

std::vector<std::string> FindAllMatches(const char *contents,
std::regex pattern)
{
Expand Down
3 changes: 3 additions & 0 deletions includes/RhythmGameUtilities/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ std::string Trim(const char *contents);

std::vector<std::string> Split(const char *contents, const char delimiter);

std::vector<std::tuple<int, int>>
GenerateAdjacentKeyPairs(std::map<int, int> keyValuePairs);

std::vector<std::string> FindAllMatches(const char *contents,
std::regex pattern);

Expand Down
14 changes: 14 additions & 0 deletions tests/MusicGameUtilities/Utilities.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cassert>
#include <cmath>
#include <iostream>
#include <tuple>

#include "RhythmGameUtilities/Utilities.h"

Expand Down Expand Up @@ -74,6 +75,18 @@ void testSplit()
std::cout << ".";
}

void testGenerateAdjacentKeyPairs()
{
auto adjacentKeyPairs =
GenerateAdjacentKeyPairs(std::map<int, int>{{1, 2}, {3, 4}, {5, 6}});

assert(adjacentKeyPairs.size() == 2);
assert(adjacentKeyPairs[0] == std::make_tuple(1, 3));
assert(adjacentKeyPairs[1] == std::make_tuple(3, 5));

std::cout << ".";
}

void testFindAllMatches()
{
std::regex pattern("\\w+");
Expand Down Expand Up @@ -107,6 +120,7 @@ int main()

testTrim();
testSplit();
testGenerateAdjacentKeyPairs();
testFindAllMatches();
testFindMatchGroups();

Expand Down