Skip to content

Commit edb2ef1

Browse files
[Feature:Plagiarism] Add region merging to matches.json (#29)
* Refactor: fix warnings in C++, make code more readable * Merge adjacent regions * Nightly progress * Reimplement algorithm * Implement save matches * Fix bugs * Add rankings * Minor changes * Final touches * Add "common code" functionality * Debug output files * Fix merging * Fix bugs in writing the files * Initial draft for individual student ranking * Fix compiler errors * Update ranking directory structure * Minor fix to the directory path * Another minor fix * Fix merging code Co-authored-by: sbelsk <[email protected]>
1 parent 7c61679 commit edb2ef1

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

compare_hashes/compare_hashes.cpp

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
121121
bool 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

Comments
 (0)