-
-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
Add a sort method to Dictionary and HashMap #77213
Conversation
|
||
#include "core/variant/variant.h" | ||
|
||
bool _hashmap_variant_less_than(const Variant &p_left, const Variant &p_right) { |
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.
Since hash_map.h
does not have the complete definition of Variant, we have to put this in a .cpp
file. And because as far as I can tell it's impossible to define a template class's method outside of the header, we can't put sort
in the .cpp
or put _hashmap_variant_less_than
inside of HashMap::
.
Perhaps we also need an
|
@dalexeev Yeah that makes sense. It might also be sensible to have an insert without any specified before_key or at_index that would assume the Dictionary was sorted and just insert a new value. |
This would be great and might be useful for my RC client too.
What difference would it make to |
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 tested it and looked over the code and this looks good to me.
8bde083
to
97ac9ea
Compare
97ac9ea
to
e943932
Compare
e943932
to
7676eb2
Compare
7676eb2
to
6c7eb26
Compare
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.
Tested locally (rebased on top of master
9adb7c7), it works as expected. The rationale for adding a sort()
method to Dictionary makes sense to me.
Dictionaries use insertion order for determinism, so for the vast majority of cases, you don't need this. |
Dictionaries are already auto-sorted when you serialize them to string. While it's supper inefficient, you should be able to do |
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.
Code looks good
@reduz I understand the determinism of having keys in the inserted order, but I need this for a different kind of determinism:
I really don't think that this is an unreasonable use case. Perhaps Godot hasn't had this before because hobbyist projects either haven't done much advanced operations on Dictionaries or have used hacks like |
6c7eb26
to
d955f99
Compare
Thanks! |
Implements this proposal and closes godotengine/godot-proposals#7060
This PR adds a method to Dictionary, and internally in HashMap, to sort in-place by key. This can be used to ensure dictionaries with the same contents produce equivalent results when getting the keys, getting the values, and converting to a string. This is also useful when wanting a JSON representation consistent with what is in memory, and useful for storing on a database that requires dictionaries to be sorted.
I used insertion sort because we want this operation to be fast for the common case where the input is already sorted or nearly sorted. The worst case is O(n²) but the best case is O(n).
I am planning to use this method in The Mirror, and while I could just implement in GDScript or keep it to our engine fork, this seems like it would be broadly useful to have in upstream core.