Class: ConstraintSolver::Domain

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

Overview

This class represents the domain of a variable, i.e. a set of values that can be assigned to the variable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = nil) ⇒ Domain

Initialises a new domain. Optionally, a set of initial values can be given.



12
13
14
15
16
17
18
# File 'lib/Domain.rb', line 12

def initialize(values=nil)
    if not (values.nil? or values.kind_of?(Set))
	raise ArgumentError, "Values must be a set!"
    end
    @values = values.nil? ? Set.new : values
    @undoStack = Array.new
end

Instance Attribute Details

#undoStackObject (readonly)

Returns the value of attribute undoStack.



9
10
11
# File 'lib/Domain.rb', line 9

def undoStack
  @undoStack
end

#valuesObject (readonly)

Returns the value of attribute values.



9
10
11
# File 'lib/Domain.rb', line 9

def values
  @values
end

Instance Method Details

#<<(value) ⇒ Object

Adds value to the domain.



21
22
23
# File 'lib/Domain.rb', line 21

def <<(value)
    @values << value
end

#==(domain) ⇒ Object



89
90
91
92
# File 'lib/Domain.rb', line 89

def ==(domain)
    return false unless domain.kind_of?(Domain)
    (@values == domain.values) and (@undoStack == domain.undoStack)
end

#collect(&block) ⇒ Object



52
53
54
# File 'lib/Domain.rb', line 52

def collect(&block)
    values.collect(&block)
end

#delete(value) ⇒ Object

Deletes value from the domain.



26
27
28
# File 'lib/Domain.rb', line 26

def delete(value)
    @values.delete(value)
end

#eachObject



42
43
44
45
46
# File 'lib/Domain.rb', line 42

def each
    @values.each { |value|
	yield value
    }
end

#empty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/Domain.rb', line 56

def empty?
    @values.empty?
end

#firstObject



34
35
36
# File 'lib/Domain.rb', line 34

def first
    @values.entries[0]
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/Domain.rb', line 81

def include?(value)
    @values.include?(value)
end

#include_any?(enum) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/Domain.rb', line 38

def include_any?(enum)
    @values.include_any?(enum)
end

#prune(values) ⇒ Object

Prunes the values from the domain.



61
62
63
64
65
66
67
68
69
70
# File 'lib/Domain.rb', line 61

def prune(values)
    if not values.kind_of?(Set)
	values = Set.new([ values ])
    end
    @undoStack.push(@values)
    @values -= values
    if @values.empty?
	raise DomainWipeoutException
    end
end

#sizeObject



30
31
32
# File 'lib/Domain.rb', line 30

def size
    @values.size
end

#sort(&block) ⇒ Object



48
49
50
# File 'lib/Domain.rb', line 48

def sort(&block)
    @values.sort(&block)
end

#to_sObject



85
86
87
# File 'lib/Domain.rb', line 85

def to_s
    "{" + @values.entries.join(", ") + "}"
end

#undoPruningObject

Undoes pruning by replacing the current list of values with the one before the last time prune was called.



74
75
76
77
78
79
# File 'lib/Domain.rb', line 74

def undoPruning
    if @undoStack.empty?
	raise UndoStackEmptyException, "No more prunes to undo!"
    end
    @values = @undoStack.pop
end