-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrail.py
More file actions
62 lines (54 loc) · 2.02 KB
/
trail.py
File metadata and controls
62 lines (54 loc) · 2.02 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
# import copy
import variable
import domain
import copy
class Trail:
def __init__(self):
"""
Represents the trail of changes made. This allows backtracking to occur.
"""
self.trailStack = []
self.trailMarker = []
######### Accessors Method #########
def size(self):
return len(self.trailStack)
######### Modified Method #########
def placeTrailMarker(self):
"""
places a marker at the current point in the trail. Each time undo is
called, the latest marker is popped and the trail
"""
self.trailMarker.append(len(self.trailStack))
# print("self.trailMarker: " + str(self.trailMarker))
def push(self, v):
"""
Adds a deep copy of a variable and its domain onto the trail.
"""
# print("v in push--> " + str(v))
domainCopy = copy.deepcopy(v.domain)
vPair = [v, domainCopy] # v Variable and its deepcopy backup domain onto the trail
self.trailStack.append(vPair)
# print("======================= ")
# print("self.trailStack at the moment: ")
# for i in self.trailStack:
# print("variable --> " + str(i[0]))
# print("domain backup --> " + str(i[1]))
# print("======================= ")
def undo(self):
"""
Pops changes pushed onto the trail until it reaches the latest marker.
Also pops the latest marker.
"""
targetSize = self.trailMarker.pop() # targetSize target position on the trail to backtrack to
size = len(self.trailStack)
while size > targetSize:
vPair = self.trailStack.pop()
v = vPair[0]
v.setDomain(vPair[1])
size -= 1
def __str__(self):
output = "trailStack: " + str(self.trailStack)
output += "\ntrailMarker: " + str(self.trailMarker)
return output
# Trail follows the singleton design pattern. A single class/object ever created.
masterTrailVariable = Trail()