-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDomain.py
82 lines (63 loc) · 2.24 KB
/
Domain.py
1
2
3
4
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Represents the domain of a variable, i.e. the possible values that each
variable may assign.
"""
class Domain:
# ==================================================================
# Constructors
# ==================================================================
def __init__ ( self, value_or_values ):
self.values = []
if type( value_or_values ) is int:
self.values.append( value_or_values )
else:
self.values = value_or_values
self.modified = False
def copy ( self, values ):
self.values = values
# ==================================================================
# Accessors
# ==================================================================
# Checks if value exists within the domain
def contains ( self, v ):
return v in self.values
# Returns number of values in the domain
def size ( self ):
return len(self.values)
# Returns true if no values are contained in the domain
def isEmpty ( self ):
return not self.values
# Returns whether or not the domain has been modified
def isModified ( self ):
return self.modified
# ==================================================================
# Modifiers
# ==================================================================
# Adds a value to the domain
def add ( self, num ):
if num not in self.values:
self.values.append( num )
# Remove a value from the domain
def remove ( self, num ):
if num in self.values:
self.modified = True
self.values.remove( num )
return True
else:
return False
# Sets the modified flag
def setModified ( self, modified ):
self.modified = modified
# ==================================================================
# String representation
# ==================================================================
def __str__ ( self ):
output = "{"
for i in range(len(self.values) - 1):
output += str(self.values[i]) + ", "
try:
output += str(self.values[-1])
except:
pass
output += "}"
return output