@@ -120,37 +120,27 @@ bool operator < (const HashLocation &hl1, const HashLocation &hl2) {
120120// ensures that all of the regions in the two parameters are adjacent
121121bool matchingPositionsAreAdjacent (const nlohmann::json &first, const nlohmann::json &second) {
122122 // they can't all be adjacent if there are an unequal number between the two lists
123- if (first.size () != second.size ()) {
123+ if (first[ " matchingpositions " ] .size () != second[ " matchingpositions " ] .size ()) {
124124 return false ;
125125 }
126126
127- nlohmann::json::const_iterator itr1 = first.begin ();
128- nlohmann::json::const_iterator itr2 = second.begin ();
129- // iterate over each matching submission
130- for (; itr1 != first.end () && itr2 != second.end (); itr1++, itr2++) {
131- // the number of matches must be the same
132- if ((*itr1)[" matchingpositions" ].size () != (*itr2)[" matchingpositions" ].size ()) {
127+ nlohmann::json::const_iterator itr1 = first[" matchingpositions" ].begin ();
128+ nlohmann::json::const_iterator itr2 = second[" matchingpositions" ].begin ();
129+ // iterate over each matching submission (first and second are the same length so we don't have to check for the end of second)
130+ for (; itr1 != first[" matchingpositions" ].end (); itr1++, itr2++) {
131+ if ((*itr1)[" end" ].get <int >() + 1 != (*itr2)[" end" ].get <int >()) {
133132 return false ;
134133 }
135-
136- nlohmann::json::const_iterator itr3 = (*itr1)[" matchingpositions" ].begin ();
137- nlohmann::json::const_iterator itr4 = (*itr2)[" matchingpositions" ].begin ();
138- // iterate over each matching position in the submission
139- for (; itr3 != (*itr1)[" matchingpositions" ].end () && itr4 != (*itr2)[" matchingpositions" ].end (); itr3++, itr4++) {
140- if ((*itr3)[" end" ].get <int >() + 1 != (*itr4)[" end" ].get <int >()) {
141- return false ;
142- }
143- }
144134 }
145135 return true ;
146136}
147137
148138
149139// increments the end position for each of the matches in the json provided,
150140// merging overlapping regions where necessary
151- void incrementEndPositionsForMatches (nlohmann::json &matches ) {
152- nlohmann::json::iterator itr = matches .begin ();
153- for (; itr != matches .end (); itr++) {
141+ void incrementEndPositionsForMatches (nlohmann::json &others ) {
142+ nlohmann::json::iterator itr = others .begin ();
143+ for (; itr != others .end (); itr++) {
154144 nlohmann::json::iterator itr2 = (*itr)[" matchingpositions" ].begin ();
155145 nlohmann::json::iterator itr3 = ++((*itr)[" matchingpositions" ].begin ());
156146 for (; itr3 != (*itr)[" matchingpositions" ].end ();) {
@@ -164,6 +154,7 @@ void incrementEndPositionsForMatches(nlohmann::json &matches) {
164154 itr3++;
165155 }
166156 }
157+ (*itr2)[" end" ] = (*itr2)[" end" ].get <int >() + 1 ;
167158 }
168159}
169160
@@ -458,19 +449,21 @@ int main(int argc, char* argv[]) {
458449 for (unsigned int position = 1; position < result.size(); position++) {
459450 nlohmann::json* prevPosition = &result[position - 1];
460451 nlohmann::json* currPosition = &result[position];
461- if ((*currPosition)["end"].get<int>() == (*prevPosition)["end"].get<int>() + 1) { // check whether they are next to each other
452+ // check whether they are next to each other and have the same type
453+ if ((*currPosition)["end"].get<int>() == (*prevPosition)["end"].get<int>() + 1 && (*currPosition)["type"] == (*prevPosition)["type"]) {
462454 bool canBeMerged = true;
463- nlohmann::json::iterator prevPosItr = (*prevPosition)["others"].begin();
464- nlohmann::json::iterator currPosItr = (*currPosition)["others"].begin();
455+ // easy check to see if they can't be merged for certain
465456 if ((*prevPosition)["others"].size() != (*currPosition)["others"].size()) {
466457 canBeMerged = false;
467458 }
468459 else {
460+ nlohmann::json::iterator prevPosItr = (*prevPosition)["others"].begin();
461+ nlohmann::json::iterator currPosItr = (*currPosition)["others"].begin();
469462 for (; prevPosItr != (*prevPosition)["others"].end() && currPosItr != (*currPosition)["others"].end(); prevPosItr++, currPosItr++) {
470463 // we can't merge the two positions if they are different in any way, except for the ending positions
471464 if ((*prevPosItr)["username"] != (*currPosItr)["username"] ||
472465 (*prevPosItr)["version"] != (*currPosItr)["version"] ||
473- !matchingPositionsAreAdjacent((*prevPosItr)["others"] , (*currPosItr)["others"] )) {
466+ !matchingPositionsAreAdjacent((*prevPosItr), (*currPosItr))) {
474467 canBeMerged = false;
475468 break;
476469 }
0 commit comments