Access value in key comparator #39
Labels
No labels
bug
discussion
documentation
duplicate
ignore
new feature
question
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
golibs/gms-gods#39
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I want to implement a structure base on
TreeMapwhere I have IP addresses as the key and aTreeSetas the value. I want to be able to sort theTreeMapusing thesize()of theTreeSet. This is currently impossible because the comparator can only access either the keys or values.For example:
Unfortunately, the
myComparatorcan only access the keys:What do you think about adding access to the values for key comparators used by maps?
If you do it in that order that you gave in your example, i.e. create the sets and then insert them thereafter into the map, then you could do something like this (keep the reference to the value in the key):
Obviously the order is important here, since the map won't be sorted automatically if you add/remove from the sets after insertion into the map.
Another problem with the above solution is that you can't lookup the map by IP address anymore in case you need that and I suppose you do.
Hey,
Thanks for your help! Haven't thought of setting the
TreeSetas the key, which was really smart! However, I ran into a few problems:fmt.Println(m)hangs on Go 1.7.1 64-bit on Windows after making a change to one of theTreeSets:TreeSet, says1, I would have to remove it then add it back to get it to sort correctly right? How expensive would this be?If I change the last 3 times of the code in my comment, it seems to lose entries in the map.
Code:
Result:
Ah my bad, the bi-directional map won't work. Sorry, I was tired last night when I thought of this. The problem here is that the inverse key in the bidi-map example is the size of the set, which will override all other sets with the same size LOL. I edited the comment above to remove that piece of buggy code. Shame on me :)
I'll think of another way later of doing this in case you need lookups by IP, which I suppose you do. Also does this list have to be sorted automatically when you populate into the sets, even after you've added the set as the value to the map? There are many solutions to this, but it all depends on the requirements.
What I also suggest is to create a simple TreeMap where the key is IP and the value is the set. Then just dynamically sort (
utils.Sort) all the values of the map based on its size. That way you have lookups by IP address and the sorting is performed when you need it and it's consistent. I'll write an example later, but it's pretty straight forward.Thanks for looking into it! 😄
In my case, I need to be able to look up by IP, so a TreeMap is probably a good fit here. The list must always be sorted by the number of items in each TreeSet. If I manually call
utils.Sorteverytime I update the TreeMap or any of the TreeSets, is it more inefficient than lettinggodshandle it itself?Also,
utils.Sortappears to only be able to sort either keys or values. Do you have an example on how it can be used to sort a TreeMap by its values?I understand that you need the lookup by IP address to get the set.
I understand that you need this to be sorted by the number of elements in the set.
However, I am not sure if you are going to add more IP addresses after you've initialized everything? Also I am not sure if you are going to add more elements to the set after the initialization? The solution really depends on those two questions.
I'll give you an example later of how to accomplish all of the above requirements if you need that.
Ah, I see! In my case, I would need to be able to add and remove ip addresses after initialisation.
OK, but you won't need to add/remove values to the set?
It hangs on all versions, because the key is modified internally, but that example doesn't work anyways. Below is the updated example that should work and you should not add/remove from the set once inserted into the map as stated at the bottom.
This is essentially the same example with the bi-directional map, just corrected to account for equal sized sets and added comparators for all builtin types. I hope this works this time:
You can add more IP addresses in this example, but you can't add more values to the set once you've placed it in the bi-directional map.
Wow, thanks! That's very helpful! 😄
I am looking at a similar problem, but in my case I also need to be able to modify the Value (the set in the above example) after it gets placed/added. Is there a way to achieve this? @emirpasic Thanks!