Created
April 11, 2014 10:24
-
-
Save rofr/10456364 to your computer and use it in GitHub Desktop.
Is there a more an efficient way to get the index of an item in a SortedSet?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. | |
/// The rank (or index) is 0-based, which means that the member with the lowest score has rank 0. | |
/// </summary> | |
/// <param name="key"></param> | |
/// <param name="member"></param> | |
/// <returns>the rank of member or null if the set or member does not exist</returns> | |
public int? ZRank(string key, string member) | |
{ | |
SortedSet<ZSetEntry> set = GetSortedSet(key); | |
if (set == null) return null; | |
int idx = -1; | |
foreach (var entry in set) | |
{ | |
idx++; | |
if (entry.Value == member) return idx; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Binary Search?