aboutsummaryrefslogtreecommitdiff
path: root/site/udo/twigs/checkpointing.udo
blob: bd2594eeb02fb44a4ed2397752a480cd4a650e27 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef UDO_TWIGS_CHECKPOINTING
#define UDO_TWIGS_CHECKPOINTING ##

gitwgs_maxundolevels = 32
gitwgs_checkpoints[] init gitwgs_maxundolevels
gitwgs_checkpointstate = 0
gitwgs_checkpointencodemult = 10000


opcode twgs_checkpoint_encode, i, ii
	ifnL, ifnR xin
	iencoded = (ifnL * gitwgs_checkpointencodemult) + (ifnR / gitwgs_checkpointencodemult)
	xout iencoded
endop

opcode twgs_checkpoint_decode, ii, i
	iencoded xin
	ifnL = int(iencoded) / gitwgs_checkpointencodemult
	ifnR = frac(iencoded) * gitwgs_checkpointencodemult
	xout ifnL, ifnR
endop

opcode twgs_checkpoint_clear, 0, 0
	icheckpointnumber = gitwgs_checkpointstate
	while (icheckpointnumber >= 0) do
		ifnL, ifnR twgs_checkpoint_decode gitwgs_checkpoints[icheckpointnumber]
		if (ifnL > 0 && ftexists(ifnL) == 1) then
			tpvf_destroy ifnL
		endif
		if (ifnR > 0  && ftexists(ifnR) == 1) then
			tpvf_destroy ifnR
		endif
		icheckpointnumber -= 1
	od
	gitwgs_checkpointstate = 0
endop

opcode twgs_checkpoint, 0, 0
	imaxundo chnget "twgs_maxundo"
	imaxundo = (imaxundo == -1) ? gitwgs_maxundolevels : imaxundo
	if (imaxundo == 0 || imaxundo > gitwgs_maxundolevels) then
		goto complete
	endif

	icheckpointnumber = gitwgs_checkpointstate
	if (icheckpointnumber >= imaxundo) then
		ifnL, ifnR twgs_checkpoint_decode gitwgs_checkpoints[0]
		if (ifnL > 0 && ftexists(ifnL) == 1) then
			tpvf_destroy ifnL
		endif
		if (ifnR > 0 && ftexists(ifnR) == 1) then
			tpvf_destroy ifnR
		endif
		index = 1
		itemp[] = gitwgs_checkpoints
		while (index <= imaxundo) do
			gitwgs_checkpoints[index - 1] = itemp[index]
			index += 1
		od
		gitwgs_checkpointstate = icheckpointnumber ;- 1
	else
		gitwgs_checkpointstate = icheckpointnumber + 1
	endif
	
	ifnL = gitwgs_tpvfHandleL
	ifnR = gitwgs_tpvfHandleR
	
	ifnCheckpointL tpvf_clone ifnL
print ifnCheckpointL
	if (ifnR > 0) then
		ifnCheckpointR tpvf_clone ifnR
	else
		ifnCheckpointR = 0
	endif
	
	iencoded twgs_checkpoint_encode ifnCheckpointL, ifnCheckpointR
	gitwgs_checkpoints[icheckpointnumber] = iencoded
	
complete:
endop

opcode twgs_undo, i, p
	iapplyundo xin

	icheckpointnumber = gitwgs_checkpointstate

	icheckpointnumber -= 1
	if (icheckpointnumber < 0) then
		istatus = -1
		goto complete
	endif

	gitwgs_checkpointstate = icheckpointnumber
	ifnL, ifnR twgs_checkpoint_decode gitwgs_checkpoints[icheckpointnumber]

	if (iapplyundo == 1) then ; apply or just step back and forget
		if (ifnL > 0 && ftexists(ifnL) = 1) then
			tpvf_destroy gitwgs_tpvfHandleL
			gitwgs_tpvfHandleL = ifnL
		endif
		if (ifnR > 0 && ftexists(ifnR) == 1) then
			tpvf_destroy gitwgs_tpvfHandleR
			gitwgs_tpvfHandleR = ifnR
		endif
	else
		if (ifnL > 0 && ftexists(ifnL) = 1) then
			tpvf_destroy ifnL
		endif
		if (ifnL > 0 && ftexists(ifnR) = 1) then
			tpvf_destroy ifnR
		endif
	endif

	istatus = 1
	
complete:
	xout istatus
endop

#end