def computeRarity(classifiedPeers)
      # 1. Make a list of the rarity of pieces.
      rarity = Array.new(@numPieces,0)
      # This first list represents rarity by number if peers having that piece. 1 = rarest.
      classifiedPeers.requestablePeers.each do |peer|
        @numPieces.times do |i|
          rarity[i] += 1 if peer.bitfield.set?(i)
        end
      end

      # 2. Make a new list that indexes the first list by order of descending rarity.
      rarityOrder = Array.new(@numPieces)
      # The elements of this second list are pairs. The first element in the pair is the rarity, second is the piece index.
      @numPieces.times do |i|
        rarityOrder[i] = [rarity[i],i]
      end
      rarityOrder.sort!{ |a,b| a[0] <=> b[0] }

      # 3. Randomize the list order within classes of the same rarity.
      left = 0
      leftVal = rarityOrder[left][0]
      @numPieces.times do |i|
        if rarityOrder[i][0] != leftVal
          # New range
          rangeLen = i-left+1
          
          QuartzTorrent.arrayShuffleRange!(rarityOrder, left, rangeLen)

          left = i+1
          leftVal = rarityOrder[left][0] if left < @numPieces
        end
      end
      QuartzTorrent.arrayShuffleRange!(rarityOrder, left, @numPieces-left) if left < @numPieces
      
      rarityOrder
    end
def computeRarity(classifiedPeers)
      # 1. Make a list of the rarity of pieces.
      rarity = Array.new(@numPieces,0)
      # This first list represents rarity by number if peers having that piece. 1 = rarest.
      classifiedPeers.requestablePeers.each do |peer|
        @numPieces.times do |i|
          rarity[i] += 1 if peer.bitfield.set?(i)
        end
      end

      # 2. Make a new list that indexes the first list by order of descending rarity.
      rarityOrder = Array.new(@numPieces)
      # The elements of this second list are pairs. The first element in the pair is the rarity, second is the piece index.
      @numPieces.times do |i|
        rarityOrder[i] = [rarity[i],i]
      end
      rarityOrder.sort!{ |a,b| a[0] <=> b[0] }

      # 3. Randomize the list order within classes of the same rarity.
      left = 0
      leftVal = rarityOrder[left][0]
      @numPieces.times do |i|
        if rarityOrder[i][0] != leftVal
          # New range
          rangeLen = i-left+1
          
          QuartzTorrent.arrayShuffleRange!(rarityOrder, left, rangeLen)

          left = i+1
          leftVal = rarityOrder[left][0] if left < @numPieces
        end
      end
      QuartzTorrent.arrayShuffleRange!(rarityOrder, left, @numPieces-left) if left < @numPieces
      
      rarityOrder
    end
Compute an array representing the relative rarity of each piece of the torrent.
 The returned array has one entry for each piece of the torrent. Each entry is a two-element
 array where the first element is the rarity of the piece where lower is more rare (i.e. 0 is rarest
 and represents 0 peers with that piece), and the second element in the entry is the piece index.
 The returned array is sorted in order of ascending rarity value (rarest is first), but within each
 class of the same rarity value the piece indices are randomized. For example, rarity 1 elements are
 all before rarity 2 elements, but the piece indices with rarity 1 are in a random order.