Skip to content

Commit 5bfcfc5

Browse files
authored
Merge pull request #35 from neogeek/feature/generateadjacentkeypairs
[feat] Added GenerateAdjacentKeyPairs method.
2 parents d596579 + 4bdc631 commit 5bfcfc5

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

includes/RhythmGameUtilities/Utilities.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <regex>
55
#include <sstream>
66
#include <string>
7+
#include <tuple>
78
#include <vector>
89

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

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

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)