Skip to content

Commit 39d85cc

Browse files
committed
Added GenerateAdjacentKeyPairs method.
Added test.
1 parent d596579 commit 39d85cc

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

includes/RhythmGameUtilities/Utilities.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
#include <cmath>
33
#include <map>
44
#include <regex>
5+
#include <set>
56
#include <sstream>
67
#include <string>
8+
#include <tuple>
79
#include <vector>
810

911
#include "Utilities.h"
@@ -110,6 +112,29 @@ std::vector<std::string> Split(const char *contents, const char delimiter)
110112
return parts;
111113
}
112114

115+
std::vector<std::tuple<int, int>>
116+
GenerateAdjacentKeyPairs(std::map<int, int> keyValuePairs)
117+
{
118+
auto adjacentKeyPairs = std::vector<std::tuple<int, int>>();
119+
120+
std::vector<int> keys;
121+
122+
for (auto item : keyValuePairs)
123+
{
124+
keys.push_back(item.first);
125+
}
126+
127+
std::vector<int> sortedKeys(keys.begin(), keys.end());
128+
129+
for (auto i = 0; i < size(sortedKeys) - 1; i += 1)
130+
{
131+
adjacentKeyPairs.push_back(
132+
std::make_tuple(sortedKeys[i], sortedKeys[i + 1]));
133+
}
134+
135+
return adjacentKeyPairs;
136+
}
137+
113138
std::vector<std::string> FindAllMatches(const char *contents,
114139
std::regex pattern)
115140
{

includes/RhythmGameUtilities/Utilities.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ std::string Trim(const char *contents);
3838

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

41+
std::vector<std::tuple<int, int>>
42+
GenerateAdjacentKeyPairs(std::map<int, int> keyValuePairs);
43+
4144
std::vector<std::string> FindAllMatches(const char *contents,
4245
std::regex pattern);
4346

tests/MusicGameUtilities/Utilities.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <cassert>
22
#include <cmath>
33
#include <iostream>
4+
#include <tuple>
45

56
#include "RhythmGameUtilities/Utilities.h"
67

@@ -74,6 +75,18 @@ void testSplit()
7475
std::cout << ".";
7576
}
7677

78+
void testGenerateAdjacentKeyPairs()
79+
{
80+
auto adjacentKeyPairs =
81+
GenerateAdjacentKeyPairs(std::map<int, int>{{1, 2}, {3, 4}, {5, 6}});
82+
83+
assert(adjacentKeyPairs.size() == 2);
84+
assert(adjacentKeyPairs[0] == std::make_tuple(1, 3));
85+
assert(adjacentKeyPairs[1] == std::make_tuple(3, 5));
86+
87+
std::cout << ".";
88+
}
89+
7790
void testFindAllMatches()
7891
{
7992
std::regex pattern("\\w+");
@@ -107,6 +120,7 @@ int main()
107120

108121
testTrim();
109122
testSplit();
123+
testGenerateAdjacentKeyPairs();
110124
testFindAllMatches();
111125
testFindMatchGroups();
112126

0 commit comments

Comments
 (0)