This shows you the differences between two versions of the page.
algorithm:queue_array-based [2016/04/02 23:36] will created |
algorithm:queue_array-based [2016/04/03 00:06] will |
||
---|---|---|---|
Line 20: | Line 20: | ||
return null; | return null; | ||
} | } | ||
- | // If the left stack is empty reverse the right stack onto it. | + | // If the left stack is empty copy right 'slice()' and |
- | this.left = this.right.reverse(); | + | // reverse the order of the elements. |
+ | this.left = this.right.slice().reverse(); | ||
this.right.length = 0; | this.right.length = 0; | ||
} | } | ||
Line 51: | Line 52: | ||
======= Tests ======= | ======= Tests ======= | ||
<syntax js> | <syntax js> | ||
- | /* Unit tests… | ||
function testA() { | function testA() { | ||
- | if (!ok) { | + | var q = new Queue(); |
- | throw "Not ok." | + | q.enqueue(1); |
- | } | + | q.enqueue(2); |
+ | q.enqueue(3); | ||
+ | |||
+ | assert(q.dequeue() === 1); | ||
+ | assert(q.dequeue() === 2); | ||
+ | assert(q.dequeue() === 3); | ||
+ | assert(q.dequeue() === null, "End of queue."); | ||
} | } | ||
- | */ | + | |
- | </syntax> | + | function testB() { |
+ | var q = new Queue(); | ||
+ | q.enqueue(1); | ||
+ | q.enqueue(2); | ||
+ | |||
+ | assert(q.dequeue() === 1); | ||
+ | |||
+ | q.enqueue(3); | ||
+ | q.enqueue(4); | ||
+ | |||
+ | assert(q.dequeue() === 2); | ||
+ | |||
+ | q.enqueue(5); | ||
+ | |||
+ | assert(q.dequeue() === 3); | ||
+ | assert(q.dequeue() === 4); | ||
+ | assert(q.dequeue() === 5); | ||
+ | assert(q.dequeue() === null, "End of queue."); | ||
+ | }</syntax> | ||
======= Options ======= | ======= Options ======= | ||
Line 99: | Line 123: | ||
| | ||
returnText = svg.append("text") | returnText = svg.append("text") | ||
- | .attr("dx", "0") | + | .attr("dx", "-10") |
.attr("dy", "40"); | .attr("dy", "40"); | ||
+ | leftLabel = svg.append("text").text("Left") | ||
+ | .attr("dx", "-10") | ||
+ | .attr("dy", "0"); | ||
+ | rightLabel = svg.append("text").text("Right") | ||
+ | .attr("dx", "-10") | ||
+ | .attr("dy", "0"); | ||
} | } | ||
Line 109: | Line 139: | ||
return {"popped": poppedMarker}; | return {"popped": poppedMarker}; | ||
} | } | ||
- | |||
- | function updateD3(x, clean, duration) { | ||
- | var stack = x.lookupInScope("queue"); | ||
- | var poppedValue = x.lookupInScope("popped"); | ||
- | | ||
- | var slots = svg.selectAll(".slot") | ||
- | .data(stack.array); | ||
| | ||
+ | function drawArray(slots, name, yoffset) { | ||
slots.exit().remove(); | slots.exit().remove(); | ||
| | ||
Line 122: | Line 146: | ||
var g = slots.enter() | var g = slots.enter() | ||
.append("g") | .append("g") | ||
- | .attr("class", "slot"); | + | .attr("class", "slot "+name); |
| | ||
g.append("rect") | g.append("rect") | ||
Line 138: | Line 162: | ||
g.attr("transform", function(d, i) { | g.attr("transform", function(d, i) { | ||
var y = Math.floor(i/10); | var y = Math.floor(i/10); | ||
- | return "translate(" + (i%10*30) + "," + y*40 +")"; | + | return "translate(" + (i%10*30) + "," + (yoffset+y*40) +")"; |
}); | }); | ||
+ | |||
+ | return yoffset + Math.max(Math.floor(slots.length/10),1) * 40; | ||
+ | } | ||
+ | |||
+ | function updateD3(x, clean, duration) { | ||
+ | var stack = x.lookupInScope("queue"); | ||
+ | var poppedValue = x.lookupInScope("popped"); | ||
| | ||
- | returnText.text(function(){return poppedValue !== poppedMarker? "Popped: "+ poppedValue : ""}); | + | var slots = svg.selectAll(".slot.left") |
- | returnText.attr("transform", function() { | + | .data(stack.left); |
- | var y = Math.floor(stack.array.length/10); | + | var slotsR = svg.selectAll(".slot.right") |
- | return "translate(0," + y*40 +")"; | + | .data(stack.right); |
- | }); | + | |
+ | var y = drawArray(slots, "left", 20); | ||
+ | rightLabel.attr("dy", y) | ||
+ | y = drawArray(slotsR, "right", y+20); | ||
+ | |||
+ | |||
+ | returnText.text(function(){return poppedValue !== poppedMarker? "Popped: "+ poppedValue : ""}); | ||
+ | returnText.attr("dy", y); | ||
} | } | ||