-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Dynarray.{equal, compare} #13144
Dynarray.{equal, compare} #13144
Conversation
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.
I just left bikesheedding comments on the docstrings.
45b2f89
to
f562733
Compare
Thanks both, I took your comments into account. |
Extending this reasoning, we should have comparison functions for other "container" types in the standard library, too, because their elements might not have canonical representations. For example, |
I don´t disagree and in fact I was surprised to find that |
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.
This looks good to me.
let compare cmp a1 a2 = | ||
let Pack {arr = arr1; length = length; dummy = dum1} = a1 in | ||
let Pack {arr = arr2; length = len2; dummy = dum2} = a2 in | ||
if length <> len2 then length - len2 |
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.
Smart!
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.
(... and only correct because we know that they are both non-negative, I think.)
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.
Well, insofar as you consider that a dynarray of length −12 is lesser than a dynarray of length −2, it is correct!
Olivier Nicole (2024/05/16 08:45 -0700):
Well, insofar as you consider that a dynarray of length −12 is lesser than a dynarray of length −2, it is correct!
:-)
|
This was suggested by @OlivierNicole in #12885 (comment) .
Having comparison functions for
Dynarray
is useful because they do not have a canonical representation: two dynarrays may have the same elements, but yet have(=)
return false orStdlib.compare
return non-0 due to having a different capacity.I reused and adapted the docstring of List.equal and Seq.compare.
Comparing of different-length arrays
For
compare
there is a choice between:[10] > [1; 2]
because10 > 1
[10] < [1; 2]
because the first list is shorter (before comparing the elements)Using
Stdlib.compare (Dynarray.to_list a) (Dynarray.to_list b)
would result in lexicographic order (1), andStdlib.compare (Dynarray.to_array a) (Dynarray.to_array b)
would result in shortlex order (2). (In other words, "doing the same as other data structures" does not force us to choose one or the other.)I went for the shortlex order which is slightly faster.