-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.js
270 lines (231 loc) · 7.97 KB
/
Sort.js
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
//Author [email protected]
let num_input = 0
let num_levels = 0
let sorter = new Array(num_levels * num_input).fill(0)
let permutations = []
let startId = 0
let canvasEl = document.getElementById("sort_canvas")
ctx = canvasEl.getContext("2d")
//draw a connection between two elements
function drawToCanvas(elem1,elem2)
{
let bodyRect = document.getElementById("sorting_network").getBoundingClientRect()
let startEl = elem1.getBoundingClientRect()
let startY = startEl.top - bodyRect.top
let startX = startEl.left - bodyRect.left
let stopEL = elem2.getBoundingClientRect()
let offsetY = stopEL.top - bodyRect.top
let offsetX = stopEL.left - bodyRect.left
ctx.beginPath();
ctx.moveTo(startX, startY);
//ctx.lineTo(offsetX, offsetY);
let length = Math.abs(offsetY-startY);
let curvature = length/3
ctx.bezierCurveTo(startX+curvature,startY, offsetX+curvature, offsetY,offsetX, offsetY)
ctx.stroke();
}
//draw all connectors
function drawConnections()
{
ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);
for(let index = 0; index < sorter.length; index++)
{
elem1 = document.getElementById("box"+index)
index2 = Number.parseInt(sorter[index])+index
elem2 = document.getElementById("box"+index2)
drawToCanvas(elem1,elem2)
}
}
//convert "box123" to 123
function IdToInt(text){
return Number.parseInt(text.substring(3, text.length))
}
//determines whether two inputs can be connected
function allowDrop(ev) {
let ownid = IdToInt(ev.target.id)
if(Math.floor(startId/num_input) == Math.floor(ownid/num_input))
{
ev.preventDefault();
}
}
//start dragging
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
startId = IdToInt(ev.target.id);
}
//stop dragging
function drop(ev) {
ev.preventDefault();
let id = ev.target.id
let stopIndex = IdToInt(id)
let start = ev.dataTransfer.getData("text");
let startID = IdToInt(start)
//ev.target.appendChild(document.getElementById(data));
let startEl = document.getElementById("box"+startID)
let level = Math.floor(stopIndex/num_input)
changeSorter(level, stopIndex%num_input,startID-stopIndex)
drawToCanvas(ev.target, startEl)
}
//frontend
//////////////////////////////////////////
//backend
//pad the array with zeros
function padArrayStart(arr, desiredLength){
return Array(desiredLength - arr.length).fill(0).concat(arr);
}
//decimal to binary format
function dec2bin(dec) {
return (dec >>> 0).toString(2);
}
//binary number to interger list
function bin2list(bin, desiredLength) {
list = []
for (const c of bin) {
list.push(Number.parseInt(c))
}
return padArrayStart(list,desiredLength)
}
//checks if the sorter can sort the combination
function check_sorter_functionality(comb){
for(let level = 0 ; level < num_levels; level++){
let input = []
for(let i = 0 ; i< num_input; i++){
let index = level*num_input+i
let sorterValue = Number.parseInt(sorter[index])
if(sorterValue >= 0){
input.push(Math.max(comb[i], comb[i + sorterValue]))
}
else{
input.push(Math.min(comb[i], comb[i + sorterValue]))
}
}
comb = input
}
let nonzero = comb.filter(function(x){
return x===1
}).length
for(let i = 0; i< nonzero; i++){
if(comb[i]===0){
return false
}
}
return true
}
//called whenever the size changes
function sizeChange()
{
num_input = document.getElementById("Inputs").value
num_levels = document.getElementById('Depth').value
//backend stuff
sorter = new Array(num_levels * num_input).fill(0)
permutations = new Array()
for(let i = 0;i < 2**num_input; i++){
let i2 = dec2bin(i)
permutations.push(bin2list(i2,num_input))
}
//draw the number fields
let connectorsEl = document.getElementById("connectors")
numberfields = ``
for(let i = 0; i< num_input; i++){
for(let level = 0; level< num_levels; level++){
let index = level * num_input + i
numberfields+=`<input id="input${index}" type="number" id="Inputs" max="${num_input-i-1}" min="${-i}" oninput="valueChange(${level},${i})" value="0">`
}
numberfields+=`<br></br>`
}
connectorsEl.innerHTML = numberfields
//drawConnections
let connectors = ``
let connectorsRenderEl = document.getElementById("connectorsRender")
for(let i = 0; i< num_input; i++){
connectors+=`<div class="row">
<div class="drop-targets">`
for(let level = 0; level< num_levels; level++){
let index = level * num_input + i
connectors+=`<div class="box" id="box${index}" ondrop="drop(event)" ondragover="allowDrop(event)" draggable="true" ondragstart="drag(event)"></div>`
}
connectors+=`</div></div>`
}
connectorsRenderEl.innerHTML = connectors
//resize canvas
let bodyRect = document.getElementById("sorting_network").getBoundingClientRect()
canvasEl.width = bodyRect.width
canvasEl.height = bodyRect.height
//reset result
let resultEl = document.getElementById("result")
resultEl.innerHTML = ``
}
//called whenever something about the sorter changes
function changeSorter(level, position, value)
{
let index = level*num_input + position
oldvalue = sorter[index]
sorter[index] = value
let valueEl = document.getElementById("input"+index)
valueEl.value = value
value = Number.parseInt(sorter[index])
let oldcorresponding = index + oldvalue
let oldcorrespondingEL = document.getElementById("input"+oldcorresponding)
if(oldvalue != 0)
{
oldcorrespondingEL.value = 0
sorter[oldcorresponding]=0
oldcorrespondingEL.style.borderColor = "White"
}
if(value >= num_input-position || value < -position)
{
valueEl.style.borderColor = "red"
}
else{ // valid input
valueEl.style.borderColor = "Green"
let corresponding = index+value
let correspondingEL = document.getElementById("input"+corresponding)
if(oldvalue != 0)
{
oldcorrespondingEL.value = 0
sorter[oldcorresponding] = 0
oldcorrespondingEL.style.borderColor = "White"
}
let CC = Number.parseInt(correspondingEL.value) + corresponding
document.getElementById("input"+ CC).value = 0
sorter[CC] = 0
document.getElementById("input"+ CC).style.borderColor = "White"
correspondingEL.value = -sorter[index]
sorter[corresponding] = -sorter[index]
correspondingEL.style.borderColor = "Green"
}
drawConnections()
calc_error()
}
//value of inputfield changes
function valueChange(level, position){
/*for(let i = 0; i< num_levels* num_input; i++){
let valueEl = document.getElementById("input"+i)
sorter[i] = valueEl.value
value = Number.parseInt(sorter[i])
if(value >= num_input-(i%num_levels)){
valueEl.style.borderColor = "red"
}
else{
valueEl.style.borderColor = "green"
let corresponding = i+value
document.getElementById("input"+corresponding).value = -sorter[i]
}
}*/
let index = level*num_input +position
let valueEl = document.getElementById("input"+index)
changeSorter(level, position, Number.parseInt(valueEl.value))
}
//calculate whether it can sort or not
function calc_error(){
let unsorting_permuts = permutations.find(function(perm){
return check_sorter_functionality(perm)===false
})
let resultEl = document.getElementById("result")
if(unsorting_permuts != undefined) //TODO
resultEl.innerHTML = `<p style="color:#FF0000";>cannot sort for example ${unsorting_permuts}</p>`
else{
resultEl.innerHTML= `<p style="color:#00AA00";>can sort everything</p>`
}
return true
}