-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement compare for bytearray #771
Implement compare for bytearray #771
Conversation
25277d2
to
469ae81
Compare
Hey, what are the next steps of this PR? We could either:
|
rough draft
|
We'll also probably need to add it for JS, it should be roughly something like, uuuh:
then clamp to |
469ae81
to
5548a6e
Compare
Where should I add the JS function? In |
2a31523
to
61fd5d6
Compare
function bytearray$compare(arr1, arr2) { | ||
const len = Math.min(arr1.length, arr2.length); | ||
|
||
for (let i = 0; i < len; i++) { | ||
if (arr1[i] !== arr2[i]) { | ||
return arr1[i] < arr2[i] ? -1 : 1; | ||
} | ||
} | ||
|
||
if (arr1.length !== arr2.length) { | ||
return arr1.length < arr2.length ? -1 : 1; | ||
} else { | ||
return 0; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we first compare the length? This is supposed to be much faster.
function bytearray$compare(arr1, arr2) { | |
const len = Math.min(arr1.length, arr2.length); | |
for (let i = 0; i < len; i++) { | |
if (arr1[i] !== arr2[i]) { | |
return arr1[i] < arr2[i] ? -1 : 1; | |
} | |
} | |
if (arr1.length !== arr2.length) { | |
return arr1.length < arr2.length ? -1 : 1; | |
} else { | |
return 0; | |
} | |
} | |
function bytearray$compare(arr1, arr2) { | |
if (arr1.length !== arr2.length) { | |
return arr1.length < arr2.length ? -1 : 1; | |
} | |
const len = Math.min(arr1.length, arr2.length); | |
for (let i = 0; i < len; i++) { | |
if (arr1[i] !== arr2[i]) { | |
return arr1[i] < arr2[i] ? -1 : 1; | |
} | |
} | |
return 0; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I repeating a mistake again?
Ahhh... it is inequality... I am trapped by that every time.
def main() = { | ||
val b1 = fromString("Hey") | ||
val b2 = fromString("Hey") | ||
val b3 = fromString("He") | ||
val b4 = fromString("Heys") | ||
println(compareByteArray(b1, b1)) | ||
println(compareByteArray(b1, b2)) | ||
println(compareByteArray(b1, b3)) | ||
println(compareByteArray(b1, b4)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make sure that these test cases cover all paths in the implementation? (shorter, longer, different prefix, etc.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, I'd love a test comparing [haha!] this with equality. :)
compare == 0 <=> equals == true
compare != 0 <=> equals == false
Fixes #748