Class: DepSelector::DenselyPackedSet

Inherits:
Object
  • Object
show all
Defined in:
lib/dep_selector/densely_packed_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ DenselyPackedSet

Returns a new instance of DenselyPackedSet.



26
27
28
29
30
# File 'lib/dep_selector/densely_packed_set.rb', line 26

def initialize(elements)
  @sorted_elements = elements.sort
  @element_to_index = {}
  @sorted_elements.each_with_index{|elt, idx| @element_to_index[elt] = idx}
end

Instance Attribute Details

#sorted_elementsObject (readonly)

Returns the value of attribute sorted_elements.



24
25
26
# File 'lib/dep_selector/densely_packed_set.rb', line 24

def sorted_elements
  @sorted_elements
end

Instance Method Details

#[](constraint) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dep_selector/densely_packed_set.rb', line 44

def [](constraint)
  # TODO [cw/mark,2010/11/22]: don't actually need an array here, re-write
  range = []
  started = false
  done = false
  sorted_elements.each_with_index do |element, idx|
    if constraint.include?(element)
      raise "Currently only handle continuous gap between #{range.last} and #{idx} for #{constraint.to_s} over #{@sorted_elements.join(', ')}" if (range.any? && range.last+1 != idx)
      range << idx
    end
  end

  range.empty? ? [] : Range.new(range.first, range.last)
end

#index(element) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/dep_selector/densely_packed_set.rb', line 36

def index(element)
  unless @element_to_index.has_key?(element)
    msg = "#{element} is not a valid version for this package"
    raise Exceptions::InvalidVersion.new(msg)
  end
  @element_to_index[element]
end

#rangeObject



32
33
34
# File 'lib/dep_selector/densely_packed_set.rb', line 32

def range
  Range.new(0, @sorted_elements.size-1)
end