Skip to content

Commit 26da38c

Browse files
committed
implement first version of rightward nudge hack for pointers
1 parent 7b3e675 commit 26da38c

89 files changed

Lines changed: 147 additions & 64 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎v3/js/pytutor.js‎

Lines changed: 125 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,6 @@ function ExecutionVisualizer(domRootID, dat, params) {
239239
this.sortedBreakpointsList = null; // sorted and synced with breakpointLines
240240
this.hoverBreakpoints = null; // set of breakpoints because we're HOVERING over a given line
241241

242-
this.enableTransitions = false; // EXPERIMENTAL - enable transition effects
243-
244242
this.classAttrsHidden = {}; // kludgy hack for 'show/hide attributes' for class objects
245243

246244
try_hook("end_constructor", {myViz:this});
@@ -2031,6 +2029,8 @@ ExecutionVisualizer.prototype.precomputeCurTraceLayouts = function() {
20312029
var heapPtrSrcRE = /__heap_pointer_src_/;
20322030

20332031

2032+
var rightwardNudgeHack = true; // suggested by John DeNero, toggle with global
2033+
20342034
// This is the main event here!!!
20352035
//
20362036
// The "4.0" version of renderDataStructures was refactored to be much
@@ -2040,6 +2040,10 @@ var heapPtrSrcRE = /__heap_pointer_src_/;
20402040
// function. This version was created in April 2014. For reference,
20412041
// before refactoring, this function was 1,030 lines of convoluted code!
20422042
//
2043+
// (Also added the rightward nudge hack to make tree-like structures
2044+
// look more sane without any sophisticated graph rendering code. Thanks
2045+
// to John DeNero for this suggestion all the way back in Fall 2012.)
2046+
//
20432047
// The "3.0" version of renderDataStructures renders variables in
20442048
// a stack, values in a separate heap, and draws line connectors
20452049
// to represent both stack->heap object references and, more importantly,
@@ -2101,8 +2105,19 @@ ExecutionVisualizer.prototype.renderDataStructures = function(curEntry, curTople
21012105
// use d3 to render the heap by mapping curToplevelLayout into <table class="heapRow">
21022106
// and <td class="toplevelHeapObject"> elements
21032107

2108+
// for simplicity, CLEAR this entire div every time, which totally
2109+
// gets rid of the incremental benefits of using d3 for, say,
2110+
// transitions or efficient updates. but it provides more
2111+
// deterministic and predictable output for other functions. sigh, i'm
2112+
// not really using d3 to the fullest, but oh wells!
2113+
myViz.domRoot.find('#heap')
2114+
.empty()
2115+
.html('<div id="heapHeader">Objects</div>');
2116+
2117+
21042118
var heapRows = myViz.domRootD3.select('#heap')
21052119
.selectAll('table.heapRow')
2120+
.attr('id', function(d, i){ return 'heapRow' + i; }) // add unique ID
21062121
.data(curToplevelLayout, function(objLst) {
21072122
return objLst[0]; // return first element, which is the row ID tag
21082123
});
@@ -2111,33 +2126,17 @@ ExecutionVisualizer.prototype.renderDataStructures = function(curEntry, curTople
21112126
// insert new heap rows
21122127
heapRows.enter().append('table')
21132128
//.each(function(objLst, i) {console.log('NEW ROW:', objLst, i);})
2129+
.attr('id', function(d, i){ return 'heapRow' + i; }) // add unique ID
21142130
.attr('class', 'heapRow');
21152131

21162132
// delete a heap row
21172133
var hrExit = heapRows.exit();
2118-
2119-
if (myViz.enableTransitions) {
2120-
hrExit
2121-
.style('opacity', '1')
2122-
.transition()
2123-
.style('opacity', '0')
2124-
.duration(500)
2125-
.each('end', function() {
2126-
hrExit
2127-
.each(function(d, idx) {
2128-
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
2129-
})
2130-
.remove();
2131-
myViz.redrawConnectors();
2132-
});
2133-
}
2134-
else {
2135-
hrExit
2136-
.each(function(d, idx) {
2137-
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
2138-
})
2139-
.remove();
2140-
}
2134+
hrExit
2135+
.each(function(d, idx) {
2136+
//console.log('DEL ROW:', d, idx);
2137+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
2138+
})
2139+
.remove();
21412140

21422141

21432142
// update an existing heap row
@@ -2152,20 +2151,6 @@ ExecutionVisualizer.prototype.renderDataStructures = function(curEntry, curTople
21522151
.attr('class', 'toplevelHeapObject')
21532152
.attr('id', function(d, i) {return 'toplevel_heap_object_' + d;});
21542153

2155-
if (myViz.enableTransitions) {
2156-
tlhEnter
2157-
.style('opacity', '0')
2158-
.style('border-color', 'red')
2159-
.transition()
2160-
.style('opacity', '1') /* fade in */
2161-
.duration(700)
2162-
.each('end', function() {
2163-
tlhEnter.transition()
2164-
.style('border-color', 'white') /* kill border */
2165-
.duration(300)
2166-
});
2167-
}
2168-
21692154
// remember that the enter selection is added to the update
21702155
// selection so that we can process it later ...
21712156

@@ -2183,27 +2168,12 @@ ExecutionVisualizer.prototype.renderDataStructures = function(curEntry, curTople
21832168

21842169
// delete a toplevelHeapObject
21852170
var tlhExit = toplevelHeapObjects.exit();
2171+
tlhExit
2172+
.each(function(d, idx) {
2173+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
2174+
})
2175+
.remove();
21862176

2187-
if (myViz.enableTransitions) {
2188-
tlhExit.transition()
2189-
.style('opacity', '0') /* fade out */
2190-
.duration(500)
2191-
.each('end', function() {
2192-
tlhExit
2193-
.each(function(d, idx) {
2194-
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
2195-
})
2196-
.remove();
2197-
myViz.redrawConnectors();
2198-
});
2199-
}
2200-
else {
2201-
tlhExit
2202-
.each(function(d, idx) {
2203-
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
2204-
})
2205-
.remove();
2206-
}
22072177

22082178
// Render globals and then stack frames using d3:
22092179

@@ -2603,6 +2573,102 @@ ExecutionVisualizer.prototype.renderDataStructures = function(curEntry, curTople
26032573
.remove();
26042574

26052575

2576+
// Rightward nudge hack to make tree-like structures look more sane
2577+
// without any sophisticated graph rendering code. Thanks to John
2578+
// DeNero for this suggestion in Fall 2012.
2579+
//
2580+
// This hack tries to ensure that all pointers that span different
2581+
// rows point RIGHTWARD (as much as possible), which makes tree-like
2582+
// structures look decent. e.g.,:
2583+
//
2584+
// t = [[['a', 'b'], ['c', 'd']], [[1,2], [3,4]]]
2585+
//
2586+
// Do it here since all of the divs have been rendered by now, but no
2587+
// jsPlumb arrows have been rendered yet.
2588+
if (rightwardNudgeHack) {
2589+
// Basic idea: keep a set of all nudged ROWS for each nudger row, so
2590+
// that when you get nudged, you can, in turn, nudge all of the rows
2591+
// that you've nudged. this algorithm nicely takes care of the fact
2592+
// that there might not be cycles in objects that you've nudged, but
2593+
// there are cycles in entire rows.
2594+
//
2595+
// Key: ID of .heapRow object that did the nudging
2596+
// Value: set of .heapRow ID that were (transitively) nudged by this element
2597+
// (represented as a d3.map)
2598+
var nudger_to_nudged_rows = {};
2599+
2600+
// VERY IMPORTANT to sort these connector IDs in ascending order,
2601+
// since I think they're rendered left-to-right, top-to-bottom in ID
2602+
// order, so we want to run the nudging algorithm in that same order.
2603+
var srcHeapConnectorIDs = myViz.jsPlumbManager.heapConnectionEndpointIDs.keys();
2604+
srcHeapConnectorIDs.sort();
2605+
2606+
$.each(srcHeapConnectorIDs, function(i, srcID) {
2607+
var dstID = myViz.jsPlumbManager.heapConnectionEndpointIDs.get(srcID);
2608+
2609+
var srcAnchorObject = myViz.domRoot.find('#' + srcID);
2610+
var srcHeapObject = srcAnchorObject.closest('.heapObject');
2611+
var dstHeapObject = myViz.domRoot.find('#' + dstID);
2612+
assert(dstHeapObject.attr('class') == 'heapObject');
2613+
2614+
var srcHeapRow = srcHeapObject.closest('.heapRow');
2615+
var dstHeapRow = dstHeapObject.closest('.heapRow');
2616+
2617+
var srcRowID = srcHeapRow.attr('id');
2618+
var dstRowID = dstHeapRow.attr('id');
2619+
2620+
// only consider nudging if srcID and dstID are on different rows
2621+
if (srcRowID != dstRowID) {
2622+
var srcAnchorLeft = srcAnchorObject.offset().left;
2623+
var srcHeapObjectLeft = srcHeapObject.offset().left;
2624+
var dstHeapObjectLeft = dstHeapObject.offset().left;
2625+
2626+
// if srcAnchorObject is to the RIGHT of dstHeapObject, then nudge
2627+
// dstHeapObject to the right
2628+
if (srcAnchorLeft > dstHeapObjectLeft) {
2629+
// an extra nudge of 32px matches up pretty well with the
2630+
// current CSS padding around .toplevelHeapObject
2631+
var delta = (srcAnchorLeft - dstHeapObjectLeft) + 32;
2632+
2633+
// set margin rather than padding so that arrows tips still end
2634+
// at the left edge of the element.
2635+
// whoa, set relative CSS using +=, nice!
2636+
dstHeapObject.css('margin-left', '+=' + delta);
2637+
2638+
//console.log(srcRowID, 'nudged', dstRowID, 'by', delta);
2639+
2640+
var cur_nudgee_set = nudger_to_nudged_rows[srcRowID];
2641+
if (cur_nudgee_set === undefined) {
2642+
cur_nudgee_set = d3.map();
2643+
nudger_to_nudged_rows[srcRowID] = cur_nudgee_set;
2644+
}
2645+
cur_nudgee_set.set(dstRowID, 1 /* useless value */);
2646+
2647+
// now if dstRowID itself nudged some other nodes, then nudge
2648+
// all of its nudgees by delta as well
2649+
var dst_nudgee_set = nudger_to_nudged_rows[dstRowID];
2650+
if (dst_nudgee_set) {
2651+
dst_nudgee_set.forEach(function(k, v) {
2652+
// don't nudge if it's yourself, to make cycles look
2653+
// somewhat reasonable (although still not ideal). e.g.,:
2654+
// x = [1,2]
2655+
// y = [3,x]
2656+
// x[1] = y
2657+
if (k != srcRowID) {
2658+
// nudge this entire ROW by delta as well
2659+
myViz.domRoot.find('#' + k).css('margin-left', '+=' + delta);
2660+
2661+
// then transitively add to entry for srcRowID
2662+
cur_nudgee_set.set(k, 1 /* useless value */);
2663+
}
2664+
});
2665+
}
2666+
}
2667+
}
2668+
});
2669+
}
2670+
2671+
26062672
// NB: ugh, I'm not very happy about this hack, but it seems necessary
26072673
// for embedding within sophisticated webpages such as IPython Notebook
26082674

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
x = [1,2]
2+
y = [1,2]
3+
z = [1,2]
4+
w = [1,2]
5+
6+
x[1] = y
7+
y[1] = z
8+
z[1] = w
9+
w[1] = x
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
x = [1, [2, None]]
2+
y = [1, 2]
3+
z = [1, 2]
4+
5+
x[1][0] = y # should nudge y to over the right
6+
z[1] = x # should nudge BOTH x and y over to the right
888 Bytes
-950 Bytes
6.67 KB
9.15 KB
11.9 KB
15.4 KB
16.2 KB

0 commit comments

Comments
 (0)