Class: TestProf::Utils::SizedOrderedSet
- Inherits:
-
Object
- Object
- TestProf::Utils::SizedOrderedSet
- Includes:
- Enumerable
- Defined in:
- lib/test_prof/utils/sized_ordered_set.rb
Overview
Ordered set with capacity
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(max_size, sort_by: nil, &block) ⇒ SizedOrderedSet
constructor
A new instance of SizedOrderedSet.
- #size ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(max_size, sort_by: nil, &block) ⇒ SizedOrderedSet
Returns a new instance of SizedOrderedSet.
14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 |
Instance Method Details
#<<(item) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/test_prof/utils/sized_ordered_set.rb', line 44 def each(&block) if block data.each(&block) else data.each end end |
#empty? ⇒ Boolean
56 57 58 |
# File 'lib/test_prof/utils/sized_ordered_set.rb', line 56 def empty? size.zero? end |
#size ⇒ Object
52 53 54 |
# File 'lib/test_prof/utils/sized_ordered_set.rb', line 52 def size data.size end |
#to_a ⇒ Object
60 61 62 |
# File 'lib/test_prof/utils/sized_ordered_set.rb', line 60 def to_a data.dup end |