sortedset

Redis: Is ZADD better than O(logN) when the inserted element is at the beginning or end?

北城余情 提交于 2019-11-30 23:58:21
The redis documentation for ZADD states the operation is O(log N ). However, does anyone know if ZADD is better than O(log N ) when the inserted element is at the beginning or end of the sort order? E.g. for certain implementations this could be O(1). Specifically, the redis tutorial states that: Sorted sets are implemented via a dual-ported data structure containing both a skip list and an hash table, so every time we add an element Redis performs an O(log( N )) operation. It seems plausible to modify a skip list to support O( k ) insert at beginning and end, where k is the max level of the

Understanding TreeSet when compareto returns 0

我是研究僧i 提交于 2019-11-30 23:38:58
I have created a Student class like this: public class Student implements Comparable<Student> { private String firstName; private String lastName; public Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } // Getters & Setters follow here... @Override public int compareTo(Student student) { int hash = this.firstName.compareTo(student.firstName); return hash; } @Override public String toString() { return "Student [firstName=" + firstName + ", lastName=" + lastName + "]"; } } This is my test class where I just add elements to my TreeSet: public

Redis: Is ZADD better than O(logN) when the inserted element is at the beginning or end?

半腔热情 提交于 2019-11-30 18:30:50
问题 The redis documentation for ZADD states the operation is O(log N ). However, does anyone know if ZADD is better than O(log N ) when the inserted element is at the beginning or end of the sort order? E.g. for certain implementations this could be O(1). Specifically, the redis tutorial states that: Sorted sets are implemented via a dual-ported data structure containing both a skip list and an hash table, so every time we add an element Redis performs an O(log( N )) operation. It seems plausible

C# SortedSet<T> and equality

こ雲淡風輕ζ 提交于 2019-11-30 17:53:33
I am a bit puzzled about the behaviour of SortedSet, see following example: public class Blah { public double Value { get; private set; } public Blah(double value) { Value = value; } } public class BlahComparer : Comparer<Blah> { public override int Compare(Blah x, Blah y) { return Comparer<double>.Default.Compare(x.Value, y.Value); } } public static void main() { var blahs = new List<Blah> {new Blah(1), new Blah(2), new Blah(3), new Blah(2)} //contains all 4 entries var set = new HashSet<Blah>(blahs); //contains only Blah(1), Blah(2), Blah(3) var sortedset = new SortedSet<Blah>(blahs, new

C# fastest intersection of 2 sets of sorted numbers

会有一股神秘感。 提交于 2019-11-30 02:12:45
I'm calculating intersection of 2 sets of sorted numbers in a time-critical part of my application. This calculation is the biggest bottleneck of the whole application so I need to speed it up. I've tried a bunch of simple options and am currently using this: foreach (var index in firstSet) { if (secondSet.BinarySearch(index) < 0) continue; //do stuff } Both firstSet and secondSet are of type List. I've also tried using LINQ: var intersection = firstSet.Where(t => secondSet.BinarySearch(t) >= 0).ToList(); and then looping through intersection . But as both of these sets are sorted I feel there

Serialization issue with SortedSet, Arrays, an Serializable

爷,独闯天下 提交于 2019-11-29 17:13:59
I have this before the process: protected void onPostExecute(SortedSet<RatedMessage> result) { List<Object> list=Arrays.asList(result.toArray()); lancon.putExtra("results", list.toArray()); // as serializable } then in the other part I have Object o=this.getIntent().getSerializableExtra("results"); //at this point the o holds the correct value (checked by debugger) RatedMessage[] rm = (RatedMessage[]) o;// this line hangs out w ClassCastException resultSet = new TreeSet<RatedMessage>(new Comp()); Collections.addAll(resultSet, rm); Why I get the ClassCastException? Finally I got it to work this

Limited SortedSet

社会主义新天地 提交于 2019-11-29 07:00:40
i'm looking for an implementation of SortedSet with a limited number of elements. So if there are more elements added then the specified Maximum the comparator decides if to add the item and remove the last one from the Set. SortedSet<Integer> t1 = new LimitedSet<Integer>(3); t1.add(5); t1.add(3); t1.add(1); // [1,3,5] t1.add(2); // [1,2,3] t1.add(9); // [1,2,3] t1.add(0); // [0,1,2] Is there an elegant way in the standard API to accomplish this? I've wrote a JUnit Test for checking implementations: @Test public void testLimitedSortedSet() { final LimitedSortedSet<Integer> t1 = new

ordering a hashset example?

删除回忆录丶 提交于 2019-11-28 23:10:47
I need an example on how to use a comparable class on a HashSet to get an ascending order. Let’s say I have a HashSet like this one: HashSet<String> hs = new HashSet<String>(); How can I get hs to be in ascending order? Use a TreeSet instead. It has a constructor taking a Comparator . It will automatically sort the Set . If you want to convert a HashSet to a TreeSet , then do so: Set<YourObject> hashSet = getItSomehow(); Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator()); treeSet.addAll(hashSet); // Now it's sorted based on the logic as implemented in YourComparator. If

C# fastest intersection of 2 sets of sorted numbers

大兔子大兔子 提交于 2019-11-28 23:08:40
问题 I'm calculating intersection of 2 sets of sorted numbers in a time-critical part of my application. This calculation is the biggest bottleneck of the whole application so I need to speed it up. I've tried a bunch of simple options and am currently using this: foreach (var index in firstSet) { if (secondSet.BinarySearch(index) < 0) continue; //do stuff } Both firstSet and secondSet are of type List. I've also tried using LINQ: var intersection = firstSet.Where(t => secondSet.BinarySearch(t) >=

Redis sorted sets and best way to store uids

大兔子大兔子 提交于 2019-11-28 09:31:29
I have data consisting of user_ids and tags of these user ids. The user_ids occur multiple times and have pre-specified number of tags (500) however that might change in the feature. What must be stored is the user_id, their tags and their count. I want later to easily find tags with top score.. etc. Every time a tag appears it is incremented My implementation in redis is done using sorted sets every user_id is a sorted set key is user_id and is a hex number works like this: zincrby user_id:x 1 "tag0" zincrby user_id:x 1 "tag499" zincrby user_id:y 1 "tag3" and so on having in mind that I want