Module: Gecode::SetEnumMethods

Includes:
Constraints::LeftHandSideMethods, VariableEnumMethods
Defined in:
lib/gecoder/interface/enum_wrapper.rb,
lib/gecoder/interface/constraints/set_enum/operation.rb,
lib/gecoder/interface/constraints/set_enum/selection.rb,
lib/gecoder/interface/constraints/set_enum_constraints.rb

Overview

A module containing the methods needed by enumerations containing set variables. Requires that it’s included in an enumerable.

Instance Attribute Summary

Attributes included from EnumMethods

#model

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Constraints::LeftHandSideMethods

#must, #must_not

Methods included from VariableEnumMethods

#values

Methods included from EnumMethods

#active_space

Class Method Details

.included(mod) ⇒ Object

This adds the adder for the methods in the modules including it. The reason for doing it so indirect is that the first #[] won’t be defined before the module that this is mixed into is mixed into an enum.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gecoder/interface/constraints/set_enum/selection.rb', line 5

def self.included(mod) #:nodoc:
  mod.module_eval do
    # Now we enter the module that the module possibly defining #[] 
    # is mixed into.
    if instance_methods.include?('[]') and 
        not instance_methods.include?('pre_selection_access')
      alias_method :pre_selection_access, :[]
    end
  
    def [](*vars)
      # Hook in an element constraint if a variable is used for array 
      # access.
      if vars.first.kind_of? Gecode::FreeIntVar
        params = {:lhs => self, :index => vars.first}
        Gecode::Constraints::SetEnum::Selection::SelectExpressionStub.new(
          @model, params)
      elsif vars.first.kind_of? Gecode::FreeSetVar
        params = {:lhs => self, :indices => vars.first}
        Gecode::Constraints::SetEnum::Selection::SetAccessStub.new(
          @model, params)
      else
        pre_selection_access(*vars) if respond_to? :pre_selection_access
      end
    end
  end
end

Instance Method Details

#to_set_var_arrayObject Also known as: to_var_array

Returns a set variable array with all the bound variables.



115
116
117
118
119
120
121
122
123
124
# File 'lib/gecoder/interface/enum_wrapper.rb', line 115

def to_set_var_array
  space = @model.active_space
  unless @bound_space == space
    elements = to_a
    @bound_arr = Gecode::Raw::SetVarArray.new(active_space, elements.size)
    elements.each_with_index{ |var, index| @bound_arr[index] = var.bind }
    @bound_space = space
  end
  return @bound_arr
end

#upper_bound_rangeObject

Returns the range of the union of the contained sets’ upper bounds.



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/gecoder/interface/enum_wrapper.rb', line 128

def upper_bound_range
  inject(nil) do |range, var|
    upper_bound = var.upper_bound
    min = upper_bound.min
    max = upper_bound.max
    next min..max if range.nil?
    
    range = min..range.last if min < range.first
    range = range.first..max if max > range.last
    range
  end
end