Skip to content

Commit

Permalink
Fixes antlr#3959 Was comparing C++ arrays with different lengths.
Browse files Browse the repository at this point in the history
Signed-off-by: Terence Parr <[email protected]>
  • Loading branch information
parrt committed Nov 22, 2022
1 parent bd8af73 commit ac03e01
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ between X1 and X2 or between X3 and X4
;

[skip]
Cpp
Python2
Python3
JavaScript
Expand Down
21 changes: 20 additions & 1 deletion runtime/Cpp/runtime/src/atn/ArrayPredictionContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,37 @@ bool ArrayPredictionContext::equals(const PredictionContext &other) const {
if (getContextType() != other.getContextType()) {
return false;
}
/*
const auto &array = downCast<const ArrayPredictionContext&>(other);
return returnStates.size() == array.returnStates.size() &&
parents.size() == array.parents.size() &&
cachedHashCodeEqual(cachedHashCode(), array.cachedHashCode()) &&
std::memcmp(returnStates.data(), array.returnStates.data(), returnStates.size() * sizeof(decltype(returnStates)::value_type)) == 0 &&
*/
const auto &array = downCast<const ArrayPredictionContext&>(other);
const bool sameSize = returnStates.size() == array.returnStates.size() &&
parents.size() == array.parents.size();
if ( !sameSize ) {
return false;
}

const bool sameHash = cachedHashCodeEqual(cachedHashCode(), array.cachedHashCode());
if ( !sameHash ) {
return false;
}

const size_t stateSizeBytes = sizeof(decltype(returnStates)::value_type);
const bool returnStateArraysEqual =
std::memcmp(returnStates.data(), array.returnStates.data(),
returnStates.size() * stateSizeBytes) == 0;
if ( !returnStateArraysEqual ) {
return false;
}

// stack of contexts is the same
const bool parentCtxEqual =
std::equal(parents.begin(), parents.end(), array.parents.begin(), predictionContextEqual);
return sameSize && sameHash && returnStateArraysEqual && parentCtxEqual;
return parentCtxEqual;
}

std::string ArrayPredictionContext::toString() const {
Expand Down

0 comments on commit ac03e01

Please sign in to comment.