Method: Redis::HashRing.binary_search
- Defined in:
- lib/redis/hash_ring.rb
.binary_search(ary, value) ⇒ Object
Find the closest index in HashRing with value <= the given value
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/redis/hash_ring.rb', line 67 def self.binary_search(ary, value) upper = ary.size - 1 lower = 0 idx = 0 while lower <= upper idx = (lower + upper) / 2 comp = ary[idx] <=> value if comp == 0 return idx elsif comp > 0 upper = idx - 1 else lower = idx + 1 end end upper = ary.size - 1 if upper < 0 upper end |