sortedset

How to find the index of an element in a TreeSet?

百般思念 提交于 2019-11-28 08:07:58
I'm using a TreeSet<Integer> and I'd quite simply like to find the index of a number in the set. Is there a nice way to do this that actually makes use of the O(log(n)) complexity of binary trees? (If not, what should I do, and does anyone know why not? I'm curious why such a class would be included in Java without something like a search function.) As @Yrlec points out set.headSet(element).size will returns 0 though there is no this element in the set. So we'd better check: return set.contains(element)? set.headSet(element).size(): -1; Here is a test case to show the problem: public static

Why are there no sorted containers in Python's standard libraries?

偶尔善良 提交于 2019-11-28 06:07:55
Is there a Python design decision (PEP) that precludes a sorted container from being added to Python? ( OrderedDict is not a sorted container since it is ordered by insertion order.) It's a conscious design decision on Guido's part (he was even somewhat reluctant regarding the addition of the collections module). His goal is to preserve "one obvious way to do it" when it comes to the selection of data types for applications. The basic concept is that if a user is sophisticated enough to realise that the builtin types aren't the right solution for their problem, then they're also up to the task

maintaining TreeSet sort as object changes value

a 夏天 提交于 2019-11-28 06:07:53
I've got a object that defines a 'natural sort order' using Comparable<>. These are being stored in TreeSets. Other than removing and re-adding the object, is there another way to update the sort when the members that are used to define the sort order are updated? As others have noted, there is no in-built way. But you can always subclass that TreeSet, with your constructor(s) of choice, and add in the required functionality: public class UpdateableTreeSet<T extends Updateable> extends TreeSet<T> { // definition of updateable interface Updateable{ void update(Object value); } // constructors

Redis: Sum of SCORES in Sorted Set

。_饼干妹妹 提交于 2019-11-27 12:34:30
问题 What's the best way to get the sum of SCORES in a Redis sorted set? 回答1: The only option I think is iterating the sorted set and computing the sum client side. 回答2: Available since Redis v2.6 is the most awesome ability to execute Lua scripts on the Redis server. This renders the challenge of summing up a Sorted Set's scores to trivial: local sum=0 local z=redis.call('ZRANGE', KEYS[1], 0, -1, 'WITHSCORES') for i=2, #z, 2 do sum=sum+z[i] end return sum Runtime example: ~$ redis-cli zadd z 1 a

Why SortedSet<T>.GetViewBetween isn't O(log N)?

早过忘川 提交于 2019-11-27 07:55:53
In .NET 4.0+, a class SortedSet<T> has a method called GetViewBetween(l, r) , which returns an interface view on a tree part containing all the values between the two specified. Given that SortedSet<T> is implemented as a red-black tree, I naturally expect it to run in O(log N) time. The similar method in C++ is std::set::lower_bound/upper_bound , in Java it's TreeSet.headSet/tailSet , and they are logarithmic. However, that is not true. The following code runs in 32 sec, whereas the equivalent O(log N) version of GetViewBetween would make this code run in 1-2 sec. var s = new SortedSet<int>()

maintaining TreeSet sort as object changes value

↘锁芯ラ 提交于 2019-11-27 05:38:22
问题 I've got a object that defines a 'natural sort order' using Comparable<>. These are being stored in TreeSets. Other than removing and re-adding the object, is there another way to update the sort when the members that are used to define the sort order are updated? 回答1: As others have noted, there is no in-built way. But you can always subclass that TreeSet, with your constructor(s) of choice, and add in the required functionality: public class UpdateableTreeSet<T extends Updateable> extends

Why are there no sorted containers in Python's standard libraries?

蹲街弑〆低调 提交于 2019-11-27 00:59:29
问题 Is there a Python design decision (PEP) that precludes a sorted container from being added to Python? ( OrderedDict is not a sorted container since it is ordered by insertion order.) 回答1: It's a conscious design decision on Guido's part (he was even somewhat reluctant regarding the addition of the collections module). His goal is to preserve "one obvious way to do it" when it comes to the selection of data types for applications. The basic concept is that if a user is sophisticated enough to

Why SortedSet<T>.GetViewBetween isn't O(log N)?

蓝咒 提交于 2019-11-26 22:17:08
问题 In .NET 4.0+, a class SortedSet<T> has a method called GetViewBetween(l, r) , which returns an interface view on a tree part containing all the values between the two specified. Given that SortedSet<T> is implemented as a red-black tree, I naturally expect it to run in O(log N) time. The similar method in C++ is std::set::lower_bound/upper_bound , in Java it's TreeSet.headSet/tailSet , and they are logarithmic. However, that is not true. The following code runs in 32 sec, whereas the