module Gecode::IntEnum::IntEnumOperand
A IntEnumOperand is a enumeration of IntOperand on which the constraints defined in IntEnumConstraintReceiver can be placed.
Enumerations of integer operands can be created either by using Gecode::Mixin#int_var_array and Gecode::Mixin#int_var_matrix, or by wrapping an existing enumeration containing IntOperand using Gecode::Mixin#wrap_enum. The enumerations, no matter how they were created, all respond to the properties defined by IntEnumOperand.
Examples ¶ ↑
Produces an array of five int operands with domain 0..9 inside a problem formulation using Gecode::Mixin#int_var_array:
int_enum = int_var_array(5, 0..9)
Uses Gecode::Mixin#wrap_enum
inside a problem formulation to create a IntEnumOperand from an existing enumeration
containing the integer operands int_operand1
and
int_operand2
:
int_enum = wrap_enum([int_operand1, int_operand2])
Public Instance Methods
Produces a new IntOperand representing the number of times
int_operand_or_fixnum
is present in this enumeration.
Examples ¶ ↑
# The number of times 17 occurs in +int_enum+. int_enum.count(17) # The number of times +int_operand+ occurs in +int_enum+. int_enum.count(int_operand)
# File doc/tmp/rdoc_dev/gecoder/interface/constraints/int_enum/count.rb, line 13 def count(int_operand_or_fixnum) unless int_operand_or_fixnum.respond_to? :to_int_var or int_operand_or_fixnum.kind_of?(Fixnum) raise TypeError, 'Expected integer operand of fixnum, got ' + "#{int_operand_or_fixnum.class}." end Count::IntEnumCountOperand.new(@model, self, int_operand_or_fixnum) end
# File doc/tmp/rdoc_dev/gecoder/interface/constraints/int_enum_constraints.rb, line 32 def method_missing(method, *args) if Gecode::IntEnum::Dummy.instance_methods.include? method.to_s # Delegate to the int enum. to_int_enum.method(method).call(*args) else super end end
Public Class Methods
Produces an IntOperand representing the i:th integer operand in the enumeration, where i is the value of the integer operand used as index. Think of it as array access in the world of constraint programming.
Examples ¶ ↑
# The operand at the +x+:th position in +int_enum+, # where +x+ is an integer operand. int_enum[x]
# File doc/tmp/rdoc_dev/gecoder/interface/constraints/int_enum/element.rb, line 28 def [](*vars) if vars.first.respond_to? :to_int_var return Element::ElementIntOperand.new( model, self, vars.first) else pre_element_access(*vars) if respond_to? :pre_element_access end end
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.
# File doc/tmp/rdoc_dev/gecoder/interface/constraints/int_enum/element.rb, line 6 def self.included(enum_mod) enum_mod.module_eval do # Now we enter the module IntEnumOperands is mixed into. class << self alias_method :pre_element_included, :included def included(mod) mod.module_eval do if instance_methods.include? '[]' alias_method :pre_element_access, :[] end # Produces an IntOperand representing the # i:th integer operand in the enumeration, where i is the # value of the integer operand used as index. Think of it # as array access in the world of constraint programming. # # ==== Examples # # # The operand at the +x+:th position in +int_enum+, # # where +x+ is an integer operand. # int_enum[x] # def [](*vars) if vars.first.respond_to? :to_int_var return Element::ElementIntOperand.new( model, self, vars.first) else pre_element_access(*vars) if respond_to? :pre_element_access end end end pre_element_included(mod) end end end end