Sha256: 17b5cc2476ae058978d2afaa9827e094015c2cf0049b83be287d607e64d686d7

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module Gecode::Constraints::Int
  class Expression
    # Creates a domain constraint using the specified domain.
    def in(domain, options = {})
      @params.update(Gecode::Constraints::Util.decode_options(options))
      @params[:domain] = domain
      if domain.kind_of? Range
        @model.add_constraint Domain::RangeDomainConstraint.new(@model, @params)
      else
        @model.add_constraint Domain::NonRangeDomainConstraint.new(@model, 
          @params)
      end
    end
  end
  
  # A module that gathers the classes and modules used in domain constraints.
  module Domain
    # Describes a range domain constraint.
    class RangeDomainConstraint < Gecode::Constraints::ReifiableConstraint
      def post
        var, domain, reif_var, strength = @params.values_at(:lhs, :domain, 
          :reif, :strength)
        (params = []) << var.bind
        params << domain.first << domain.last
        params << reif_var.bind if reif_var.respond_to? :bind
        params << strength
        Gecode::Raw::dom(@model.active_space, *params)
      end
      negate_using_reification
    end
    
    # Describes a non-range domain constraint.
    class NonRangeDomainConstraint < Gecode::Constraints::ReifiableConstraint
      def post
        space = @model.active_space
      
        var, domain, reif_var, strength = @params.values_at(:lhs, :domain, 
          :reif, :strength)
        domain = domain.to_a
        
        (params = []) << var.bind
        params << Gecode::Raw::IntSet.new(domain, domain.size)
        params << reif_var.bind if reif_var.respond_to? :bind
        params << strength
        Gecode::Raw::dom(space, *params)
      end
      negate_using_reification
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gecoder-0.4.0 lib/gecoder/interface/constraints/int/domain.rb