class QuartzTorrent::RegionMap

This class is used to map consecutive integer regions to objects. The lowest end of the lowest region is assumed to be 0.

Public Class Methods

new() click to toggle source
# File lib/quartz_torrent/regionmap.rb, line 46
def initialize
  @map = []
  @sorted = false
end

Public Instance Methods

[](i) click to toggle source
# File lib/quartz_torrent/regionmap.rb, line 107
def [](i)
  at(i)
end
add(regionEnd, obj) click to toggle source

Add a region that ends at the specified ‘regionEnd’ with the associated ‘obj’

# File lib/quartz_torrent/regionmap.rb, line 52
def add(regionEnd, obj)
  @map.push [regionEnd, obj]
  @sorted = false
end
at(i) click to toggle source

For the region with index i, return an array of the form [value, left, right]

# File lib/quartz_torrent/regionmap.rb, line 96
def at(i)
  return nil if @map.length == 0
  if i == 0
    left = 0
  else
    left = @map[i-1][0]+1
  end

  [@map[i][1],left,@map[i][0]]
end
find(value) click to toggle source

Given a value, return a list of the form [index, value, left, right, offset] where index is the zero-based index in this map of the region, value is the associated object, left is the lowest value in the region, right is the highest, and offset is the offset within the region of the value.

# File lib/quartz_torrent/regionmap.rb, line 80
def find(value)
  
  index = findIndex(value)
  return nil if ! index
  result = at(index)

  if index == 0
    offset = value
  else
    offset = value - result[1]
  end
  
  [index, result[0], result[1], result[2], offset]
end
findIndex(value) click to toggle source
# File lib/quartz_torrent/regionmap.rb, line 67
def findIndex(value)
  if ! @sorted
    @map.sort{ |a,b| a[0] <=> b[0] }
    @sorted = true
  end
  
  @map.binsearch_index{|x| x[0] >= value}
end
findValue(value) click to toggle source

Given an integer value, find which region it falls in and return the object associated with that region.

# File lib/quartz_torrent/regionmap.rb, line 58
def findValue(value)
  if ! @sorted
    @map.sort{ |a,b| a[0] <=> b[0] }
    @sorted = true
  end
  
  @map.binsearch{|x| x[0] >= value}[1]
end
last() click to toggle source

For the final region, return an array of the form [index, value, left, right]

# File lib/quartz_torrent/regionmap.rb, line 117
def last
  return nil if ! @map.last
  if @map.length == 1
    left = 0
  else
    left = @map[@map.length-2][0]+1
  end
  [@map.length-1,@map.last[1],left,@map.last[0]]
end
maxIndex() click to toggle source

Return the rightmost index of the final region, or -1 if no regions have been added.

# File lib/quartz_torrent/regionmap.rb, line 112
def maxIndex
  @map.size-1
end