-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathdomain.py
More file actions
102 lines (73 loc) · 2.86 KB
/
Copy pathdomain.py
File metadata and controls
102 lines (73 loc) · 2.86 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Module containing the code for the Variable and Domain classes."""
def check_if_compiled() -> bool:
"""Check if this code has been compiled with Cython.
Returns:
bool: whether the code has been compiled.
"""
try:
from cython import compiled
return compiled
except ImportError or ModuleNotFoundError:
return False
# ----------------------------------------------------------------------
# Variables
# ----------------------------------------------------------------------
class Variable:
"""Helper class for variable definition.
Using this class is optional, since any hashable object,
including plain strings and integers, may be used as variables.
"""
def __init__(self, name):
"""Initialization method.
Args:
name (string): Generic variable name for problem-specific
purposes
"""
self.name = name
def __repr__(self):
"""Represents itself with the name attribute."""
return self.name
Unassigned = Variable("Unassigned") #: Helper object instance representing unassigned values
# ----------------------------------------------------------------------
# Domains
# ----------------------------------------------------------------------
class Domain(list):
"""Class used to control possible values for variables.
When list or tuples are used as domains, they are automatically
converted to an instance of that class.
"""
def __init__(self, set):
"""Initialization method.
Args:
set: Set of values, comparable by equality, that the given variables may assume.
"""
list.__init__(self, set)
self._hidden = []
self._states = []
def resetState(self):
"""Reset to the original domain state, including all possible values."""
self.extend(self._hidden)
del self._hidden[:]
del self._states[:]
def pushState(self):
"""Save current domain state.
Variables hidden after that call are restored when that state is popped from the stack.
"""
self._states.append(len(self))
def popState(self):
"""Restore domain state from the top of the stack.
Variables hidden since the last popped state are then available again.
"""
diff = self._states.pop() - len(self)
if diff:
self.extend(self._hidden[-diff:])
del self._hidden[-diff:]
def hideValue(self, value):
"""Hide the given value from the domain.
After that call the given value won't be seen as a possible value on that domain anymore.
The hidden value will be restored when the previous saved state is popped.
Args:
value: Object currently available in the domain
"""
list.remove(self, value)
self._hidden.append(value)