-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystolic.tcl
executable file
·334 lines (212 loc) · 5.7 KB
/
systolic.tcl
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/tclsh
oo::class create Program {
constructor {} {
# Algorithm input
variable input {3 4 1 2}
variable inputSize [llength $input]
# Sorting cells
variable cells {{} {} {} {}}
# Input index starting from right
variable index [expr {$inputSize-1}]
# Current step
variable step
# Passed cell
variable currentCell
# Passed value
variable value
# Loop index
variable i
# New list for every step
variable newLst
# Variables to swap
variable toLeft
variable toRight
# Final result
variable result {}
}
method createList {} {
variable inputSize
set newList {}
for {set i 0} {$i < $inputSize} {incr i} {
lappend newLst {}
}
return $newLst
}
method swapValues {} {
variable toLeft
variable toRight
if {$toLeft>$toRight} {
set temp $toRight
set toRight $toLeft
set toLeft $temp
}
}
method moveLastLeft {} {
variable inputSize
variable cells
variable newLst
set lastIndex [expr {$inputSize-1}]
set last [lindex $cells $lastIndex]
if {[llength $last]==1} {
# Cell before last
set newCell [lindex $cells [expr {$lastIndex-1}]]
# Add last value
lappend newCell $last
# Update cell
lset newLst [expr {$lastIndex-1}] $newCell
}
}
method compareAndPush {} {
variable currentCell
variable i
variable newLst
variable result
variable toLeft
variable toRight
set toLeft [lindex $currentCell 0] ; set toRight [lindex $currentCell 1]
my swapValues
set leftIndex [expr {$i-1}]
set rightIndex [expr {$i+1}]
if {$leftIndex<0} {
lappend result $toLeft
} else {
set currentLeft [lindex $newLst $leftIndex]
lappend currentLeft $toLeft
lset newLst $leftIndex $currentLeft
}
set currentRight [lindex $newLst $rightIndex]
lappend currentRight $toRight
lset newLst $rightIndex $currentRight
}
# Move value right or compare and push
method moveMidRight {} {
variable inputSize
variable cells
variable value
variable i
variable currentCell
for {set i 0} {$i<$inputSize-1} {incr i} {
set currentCell [lindex $cells $i]
if {[llength $currentCell]==2} {
# Move small value left and large value right
my compareAndPush
# Move 1 value right
} elseif {[llength $currentCell]==1} {
set value [lindex $currentCell 0]
my move right
}
}
}
# Add value from input to first cell
method addAtFirst {} {
variable step
variable input
variable index
variable newLst
if {$step%2!=0} {
set newValue [lindex $input $index]
set cFirst [lindex $newLst 0]
lappend cFirst $newValue
lset newLst 0 $cFirst
incr index -1
}
}
# Move single value left or right
method move {dir} {
variable newLst
variable i
variable value
set nextIndex [expr {$i+($dir == "right" ? 1 : -1)}]
set nextCell [lindex $newLst $nextIndex]
lappend nextCell $value
lset newLst $nextIndex $nextCell
}
method stepPhase1 {} {
variable inputSize
variable newLst
variable cells
set newLst [my createList]
set start [expr {[lindex $cells 0]=={} ? 1 : 0}]
# Move all values right
for {set j $start} {$j < ($inputSize-1)} {incr j 2} {
lset newLst [expr {$j+1}] [lindex $cells $j]
}
# Add number from input to first
# cell when step number is odd
my addAtFirst
set cells $newLst
}
method stepPhase2 {} {
variable newLst
variable cells
set newLst [my createList]
# If last cell contains 1
# value, move it left
my moveLastLeft
# Move 1 value right. Compare 2 values in
# cell and move them left and right
my moveMidRight
my addAtFirst
set cells $newLst
}
method stepPhase3 {} {
variable cells
variable result
variable inputSize
variable newLst
variable value
variable i
variable currentCell
set newLst [my createList]
for {set i 0} {$i < $inputSize} {incr i} {
set currentCell [lindex $cells $i]
set valuesInCell [llength $currentCell]
if {$valuesInCell==1} {
set value [lindex $currentCell 0]
if {$i==0} {
# Add single value from first cell to result
lappend result $value
} else {
my move left
}
} elseif {$valuesInCell==2} {
my compareAndPush
}
}
set cells $newLst
}
method phase1 {} {
variable step
variable inputSize
for {set step 1} {$step <= $inputSize} {incr step} { my stepPhase1 }
}
method phase2 {} {
variable step
variable inputSize
for {set step 1} {$step <= $inputSize-1} {incr step} { my stepPhase2 }
}
method phase3 {} {
variable result
variable inputSize
while {[llength $result]<$inputSize} { my stepPhase3 }
}
method main {} {
variable input
variable cells
variable result
puts "Input: $input"
my phase1
puts "After first phase"
puts $cells
my phase2
puts "After second phase"
puts $cells
my phase3
puts "After third phase"
puts $cells
puts "Result"
puts $result
}
}
set program [Program new]
$program main