def new_width
      # decides how many queue elements to sample
      return 1.0 if @size < 2
      n = if @size <= 5
        @size
      else
        5 + (@size / 10).to_i
      end
      n = 25 if n > 25

      # record variables
      tmp_last_bucket = @last_bucket
      tmp_last_priority = @last_priority
      tmp_bucket_top = @bucket_top

      # dequeue n events from the queue and record their priorities with
      # resize_enabled set to false.
      @resize_enabled = false
      tmp = Array.new(n)
      average = 0.0
      i = 0
      while i < n
        # dequeue events to get a test sample
        tmp[i] = self.pop
        # and sum up the differences in time
        average += tmp[i].time_next - tmp[i-1].time_next if i > 0
        i += 1
      end

      # calculate average separation of sampled events
      average = average / (n-1).to_f

      # put the first sample back onto the queue
      self << tmp[0]

      # recalculate average using only separations smaller than twice the
      # original average
      new_average = 0.0
      j = 0
      i = 1
      while i < n
        sub = tmp[i].time_next - tmp[i-1].time_next
        if sub < average * 2.0
          new_average += sub
          j += 1
        end
        # put the remaining samples back onto the queue
        self << tmp[i]
        i += 1
      end
      new_average = new_average / j.to_f

      # restore variables
      @resize_enabled = true
      @last_bucket = tmp_last_bucket
      @last_priority = tmp_last_priority
      @bucket_top = tmp_bucket_top

      # this is the new width
      if new_average > 0.0
        new_average * 3.0
      elsif average > 0.0
        average * 2.0
      else
        1.0
      end
    end
def new_width
      # decides how many queue elements to sample
      return 1.0 if @size < 2
      n = if @size <= 5
        @size
      else
        5 + (@size / 10).to_i
      end
      n = 25 if n > 25

      # record variables
      tmp_last_bucket = @last_bucket
      tmp_last_priority = @last_priority
      tmp_bucket_top = @bucket_top

      # dequeue n events from the queue and record their priorities with
      # resize_enabled set to false.
      @resize_enabled = false
      tmp = Array.new(n)
      average = 0.0
      i = 0
      while i < n
        # dequeue events to get a test sample
        tmp[i] = self.pop
        # and sum up the differences in time
        average += tmp[i].time_next - tmp[i-1].time_next if i > 0
        i += 1
      end

      # calculate average separation of sampled events
      average = average / (n-1).to_f

      # put the first sample back onto the queue
      self << tmp[0]

      # recalculate average using only separations smaller than twice the
      # original average
      new_average = 0.0
      j = 0
      i = 1
      while i < n
        sub = tmp[i].time_next - tmp[i-1].time_next
        if sub < average * 2.0
          new_average += sub
          j += 1
        end
        # put the remaining samples back onto the queue
        self << tmp[i]
        i += 1
      end
      new_average = new_average / j.to_f

      # restore variables
      @resize_enabled = true
      @last_bucket = tmp_last_bucket
      @last_priority = tmp_last_priority
      @bucket_top = tmp_bucket_top

      # this is the new width
      if new_average > 0.0
        new_average * 3.0
      elsif average > 0.0
        average * 2.0
      else
        1.0
      end
    end
Calculates the width to use for buckets