class TestProf::Utils::SizedOrderedSet

Ordered set with capacity

Attributes

comparator[R]
data[R]
max_size[R]

Public Class Methods

new(max_size, sort_by: nil, &block) click to toggle source
# File lib/test_prof/utils/sized_ordered_set.rb, line 14
def initialize(max_size, sort_by: nil, &block)
  @max_size = max_size
  @comparator =
    if block
      block
    elsif !sort_by.nil?
      ->(x, y) { x[sort_by] >= y[sort_by] }
    else
      ->(x, y) { x >= y }
    end
  @data = []
end

Public Instance Methods

<<(item) click to toggle source
# File lib/test_prof/utils/sized_ordered_set.rb, line 27
def <<(item)
  return if data.size == max_size &&
    comparator.call(data.last, item)

  # Find an index of a smaller element
  index = data.bsearch_index { |x| !comparator.call(x, item) }

  if index.nil?
    data << item
  else
    data.insert(index, item)
  end

  data.pop if data.size > max_size
  data.size
end
each(&block) click to toggle source
# File lib/test_prof/utils/sized_ordered_set.rb, line 44
def each(&block)
  if block
    data.each(&block)
  else
    data.each
  end
end
size() click to toggle source
# File lib/test_prof/utils/sized_ordered_set.rb, line 52
def size
  data.size
end
to_a() click to toggle source
# File lib/test_prof/utils/sized_ordered_set.rb, line 56
def to_a
  data.dup
end