You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.3 KiB
102 lines
2.3 KiB
/**
|
|
*
|
|
*/
|
|
function Visualizer(parent) {
|
|
var top = document.createElement('div');
|
|
top.className = 'top';
|
|
parent.appendChild(top);
|
|
|
|
var bottom = document.createElement('div');
|
|
bottom.className = 'bottom';
|
|
parent.appendChild(bottom);
|
|
|
|
var controls = this.initControls();
|
|
parent.querySelector('.bottom').appendChild(controls);
|
|
|
|
var messages = this.initMessages();
|
|
parent.querySelector('.bottom').appendChild(messages);
|
|
|
|
var range = this.initRange();
|
|
parent.querySelector('.bottom').appendChild(range);
|
|
|
|
this.svg = d3.select(parent.querySelector('.top')).append('svg');
|
|
this.groups = null;
|
|
this.parent = parent;
|
|
this.sorter = null;
|
|
|
|
switch(parent.attributes['data-algorithm'].value) {
|
|
case 'quick':
|
|
this.sorter = new QuickSort();
|
|
break;
|
|
|
|
case 'merge':
|
|
this.sorter = new MergeSort();
|
|
break;
|
|
|
|
case 'selection':
|
|
this.sorter = new SelectionSort();
|
|
break;
|
|
|
|
case 'bubble':
|
|
this.sorter = new BubbleSort();
|
|
break;
|
|
|
|
case 'insertion':
|
|
this.sorter = new InsertionSort();
|
|
break;
|
|
|
|
case 'shell':
|
|
this.sorter = new ShellSort();
|
|
break;
|
|
|
|
case 'radix':
|
|
this.sorter = new RadixSort();
|
|
break;
|
|
|
|
default:
|
|
throw new Error('Unrecognized sort type.');
|
|
}
|
|
|
|
// this.instructions = null;
|
|
// this.currentInstruction = 0;
|
|
|
|
this.initItems(10);
|
|
};
|
|
|
|
// Public static properties (mutable)
|
|
Visualizer.spacerW = 5;
|
|
Visualizer.itemW = 14;
|
|
Visualizer.itemH = 50;
|
|
Visualizer.itemY = 20;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
Visualizer.prototype.followInstruction = function(index) {
|
|
if (index >= this.sorter.instructions.length) {
|
|
return;
|
|
}
|
|
|
|
var i = this.sorter.instructions[index];
|
|
console.log(i);
|
|
|
|
// NOTE interesting pattern here
|
|
if (typeof this[i[0]] === 'function') {
|
|
this[i[0]](i);
|
|
|
|
if (i[1] === true) {
|
|
setTimeout(this.followInstruction.bind(this, index + 1), 400);
|
|
}
|
|
else {
|
|
this.followInstruction(index + 1);
|
|
}
|
|
}
|
|
// TODO document this
|
|
else if (this[i[0]] !== undefined) {
|
|
console.warn(this[i[0]]);
|
|
}
|
|
else {
|
|
console.error(i);
|
|
throw new Error('Unidentified instruction.');
|
|
}
|
|
};
|
|
|