module Gecode class Model # Wraps a custom enumerable so that constraints can be specified using it. # The argument is altered and returned. def wrap_enum(enum) unless enum.kind_of? Enumerable raise TypeError, 'Only enumerables can be wrapped.' end elements = enum.to_a if elements.empty? raise ArgumentError, 'Enumerable must not be empty.' end if elements.map{ |var| var.kind_of? FreeIntVar }.all? class < range.last range end end end # A module containing the methods needed by enumerations containing boolean # variables. Requires that it's included in an enumerable. module BoolEnumMethods include EnumMethods # Returns a bool variable array with all the bound variables. def to_bool_var_array elements = to_a arr = Gecode::Raw::BoolVarArray.new(active_space, elements.size) elements.each_with_index{ |var, index| arr[index] = var.bind } return arr end alias_method :to_var_array, :to_bool_var_array end # A module containing the methods needed by enumerations containing fixnums. # Requires that it's included in an enumerable. module FixnumEnumMethods include EnumMethods # Returns the smallest range that contains the domains of all integer # variables involved. def domain_range min..max end end end