Commit 84e47120 authored by yurys@chromium.org's avatar yurys@chromium.org

Extract item<->index conversion from iterator into a separate interface

BUG=None

Review URL: https://codereview.chromium.org/200783007

git-svn-id: svn://svn.chromium.org/blink/trunk@169832 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent d2e396f6
......@@ -142,59 +142,150 @@ WebInspector.HeapSnapshotItemIterator.prototype = {
hasNext: function() { },
/**
* @return {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge}
*/
item: function() { },
next: function() { }
};
/**
* @interface
*/
WebInspector.HeapSnapshotItemIndexProvider = function() { }
WebInspector.HeapSnapshotItemIndexProvider.prototype = {
/**
* @param {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge} item
* @return {number}
*/
index: function() { },
indexForItem: function(item) { },
/**
* @param {number} newIndex
* @return {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge}
*/
setIndex: function(newIndex) { },
itemForIndex: function(newIndex) { },
};
/**
* @constructor
* @implements {WebInspector.HeapSnapshotItemIndexProvider}
* @param {!WebInspector.HeapSnapshot} snapshot
*/
WebInspector.HeapSnapshotNodeIndexProvider = function(snapshot)
{
this._node = snapshot.createNode();
}
WebInspector.HeapSnapshotNodeIndexProvider.prototype = {
/**
* @return {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge}
* @param {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge} item
* @return {number}
*/
item: function() { },
indexForItem: function(item)
{
var node = /** @type {!WebInspector.HeapSnapshotNode} */ (item);
return node.nodeIndex;
},
next: function() { }
/**
* @param {number} index
* @return {!WebInspector.HeapSnapshotNode}
*/
itemForIndex: function(index)
{
this._node.nodeIndex = index;
return this._node;
}
};
/**
* @constructor
* @implements {WebInspector.HeapSnapshotItemIterator}
* @param {!WebInspector.HeapSnapshotNode} node
* @implements {WebInspector.HeapSnapshotItemIndexProvider}
* @param {!WebInspector.HeapSnapshot} snapshot
*/
WebInspector.HeapSnapshotEdgeIterator = function(node)
WebInspector.HeapSnapshotEdgeIndexProvider = function(snapshot)
{
this._sourceNode = node;
this.edge = node._snapshot.createEdge(node._edgeIndexesStart());
this._edge = snapshot.createEdge(0);
}
WebInspector.HeapSnapshotEdgeIterator.prototype = {
WebInspector.HeapSnapshotEdgeIndexProvider.prototype = {
/**
* @return {boolean}
* @param {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge} item
* @return {number}
*/
hasNext: function()
indexForItem: function(item)
{
return this.edge.edgeIndex < this._sourceNode._edgeIndexesEnd();
var edge = /** @type {!WebInspector.HeapSnapshotEdge} */ (item);
return edge.edgeIndex;
},
/**
* @param {number} index
* @return {!WebInspector.HeapSnapshotEdge}
*/
itemForIndex: function(index)
{
this._edge.edgeIndex = index;
return this._edge;
}
};
/**
* @constructor
* @implements {WebInspector.HeapSnapshotItemIndexProvider}
* @param {!WebInspector.HeapSnapshot} snapshot
*/
WebInspector.HeapSnapshotRetainerEdgeIndexProvider = function(snapshot)
{
this._retainerEdge = snapshot.createRetainingEdge(0);
}
WebInspector.HeapSnapshotRetainerEdgeIndexProvider.prototype = {
/**
* @param {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge} item
* @return {number}
*/
index: function()
indexForItem: function(item)
{
return this.edge.edgeIndex;
var edge = /** @type {!WebInspector.HeapSnapshotRetainerEdge} */ (item);
return edge.retainerIndex();
},
/**
* @param {number} newIndex
* @param {number} index
* @return {!WebInspector.HeapSnapshotRetainerEdge}
*/
setIndex: function(newIndex)
itemForIndex: function(index)
{
this.edge.edgeIndex = newIndex;
this._retainerEdge.setRetainerIndex(index);
return this._retainerEdge;
}
};
/**
* @constructor
* @implements {WebInspector.HeapSnapshotItemIterator}
* @param {!WebInspector.HeapSnapshotNode} node
*/
WebInspector.HeapSnapshotEdgeIterator = function(node)
{
this._sourceNode = node;
this.edge = node._snapshot.createEdge(node._edgeIndexesStart());
}
WebInspector.HeapSnapshotEdgeIterator.prototype = {
/**
* @return {boolean}
*/
hasNext: function()
{
return this.edge.edgeIndex < this._sourceNode._edgeIndexesEnd();
},
/**
......@@ -372,22 +463,6 @@ WebInspector.HeapSnapshotRetainerEdgeIterator.prototype = {
return this.retainer.retainerIndex() < this._retainersEnd;
},
/**
* @return {number}
*/
index: function()
{
return this.retainer.retainerIndex();
},
/**
* @param {number} newIndex
*/
setIndex: function(newIndex)
{
this.retainer.setRetainerIndex(newIndex);
},
/**
* @return {!WebInspector.HeapSnapshotRetainerEdge}
*/
......@@ -409,7 +484,6 @@ WebInspector.HeapSnapshotRetainerEdgeIterator.prototype = {
WebInspector.HeapSnapshotNode = function(snapshot, nodeIndex)
{
this._snapshot = snapshot;
this._firstNodeIndex = nodeIndex;
this.nodeIndex = nodeIndex;
}
......@@ -635,22 +709,6 @@ WebInspector.HeapSnapshotNodeIterator.prototype = {
return this.node.nodeIndex < this._nodesLength;
},
/**
* @return {number}
*/
index: function()
{
return /** @type{number} */ (this.node.nodeIndex);
},
/**
* @param {number} newIndex
*/
setIndex: function(newIndex)
{
this.node.nodeIndex = newIndex;
},
/**
* @return {!WebInspector.HeapSnapshotNode}
*/
......@@ -669,15 +727,14 @@ WebInspector.HeapSnapshotNodeIterator.prototype = {
/**
* @constructor
* @implements {WebInspector.HeapSnapshotItemIterator}
* @param {!WebInspector.HeapSnapshotItemIterator} iterator
* @param {!WebInspector.HeapSnapshotItemIndexProvider} itemProvider
* @param {!Array.<number>|!Uint32Array} indexes
*/
WebInspector.HeapSnapshotIndexRangeIterator = function(iterator, indexes)
WebInspector.HeapSnapshotIndexRangeIterator = function(itemProvider, indexes)
{
this._iterator = iterator;
this._itemProvider = itemProvider;
this._indexes = indexes;
this._position = 0;
this._forcedIndex = -1;
}
WebInspector.HeapSnapshotIndexRangeIterator.prototype = {
......@@ -689,31 +746,13 @@ WebInspector.HeapSnapshotIndexRangeIterator.prototype = {
return this._position < this._indexes.length
},
/**
* @return {number}
*/
index: function()
{
if (this._forcedIndex !== -1)
return this._forcedIndex;
return this._indexes[this._position];
},
/**
* @param {number} newIndex
*/
setIndex: function(newIndex)
{
this._forcedIndex = newIndex;
},
/**
* @return {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge}
*/
item: function()
{
this._iterator.setIndex(this.index());
return this._iterator.item();
var index = this._indexes[this._position];
return this._itemProvider.itemForIndex(index);
},
next: function()
......@@ -744,22 +783,6 @@ WebInspector.HeapSnapshotFilteredIterator.prototype = {
return this._iterator.hasNext();
},
/**
* @return {number}
*/
index: function()
{
return this._iterator.index();
},
/**
* @param {number} newIndex
*/
setIndex: function(newIndex)
{
this._iterator.setIndex(newIndex);
},
/**
* @return {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge}
*/
......@@ -1900,7 +1923,8 @@ WebInspector.HeapSnapshot.prototype = {
{
var node = this.createNode(nodeIndex);
var filter = this.containmentEdgesFilter(showHiddenData);
return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.edges());
var indexProvider = new WebInspector.HeapSnapshotEdgeIndexProvider(this);
return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.edges(), indexProvider);
},
/**
......@@ -1910,7 +1934,8 @@ WebInspector.HeapSnapshot.prototype = {
createEdgesProviderForTest: function(nodeIndex, filter)
{
var node = this.createNode(nodeIndex);
return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.edges());
var indexProvider = new WebInspector.HeapSnapshotEdgeIndexProvider(this);
return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.edges(), indexProvider);
},
/**
......@@ -1940,7 +1965,8 @@ WebInspector.HeapSnapshot.prototype = {
{
var node = this.createNode(nodeIndex);
var filter = this.retainingEdgesFilter(showHiddenData);
return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.retainers());
var indexProvider = new WebInspector.HeapSnapshotRetainerEdgeIndexProvider(this);
return new WebInspector.HeapSnapshotEdgesProvider(this, filter, node.retainers(), indexProvider);
},
/**
......@@ -2024,10 +2050,12 @@ WebInspector.HeapSnapshot.prototype = {
/**
* @constructor
* @param {!WebInspector.HeapSnapshotItemIterator} iterator
* @param {!WebInspector.HeapSnapshotItemIndexProvider} indexProvider
*/
WebInspector.HeapSnapshotItemProvider = function(iterator)
WebInspector.HeapSnapshotItemProvider = function(iterator, indexProvider)
{
this._iterator = iterator;
this._indexProvider = indexProvider;
this._isEmpty = !iterator.hasNext();
/** @type {?Array.<number>} */
this._iterationOrder = null;
......@@ -2043,7 +2071,7 @@ WebInspector.HeapSnapshotItemProvider.prototype = {
return;
this._iterationOrder = [];
for (var iterator = this._iterator; iterator.hasNext(); iterator.next())
this._iterationOrder.push(iterator.index());
this._iterationOrder.push(this._indexProvider.indexForItem(iterator.item()));
},
/**
......@@ -2078,8 +2106,9 @@ WebInspector.HeapSnapshotItemProvider.prototype = {
var result = new Array(count);
var iterator = this._iterator;
for (var i = 0 ; i < count; ++i) {
iterator.setIndex(this._iterationOrder[position++]);
result[i] = iterator.item().serialize();
var itemIndex = this._iterationOrder[position++];
var item = this._indexProvider.itemForIndex(itemIndex);
result[i] = item.serialize();
}
return new WebInspector.HeapSnapshotCommon.ItemsRange(begin, end, this._iterationOrder.length, result);
},
......@@ -2095,13 +2124,14 @@ WebInspector.HeapSnapshotItemProvider.prototype = {
/**
* @constructor
* @extends {WebInspector.HeapSnapshotItemProvider}
* @param {!WebInspector.HeapSnapshotItemIndexProvider} indexProvider
*/
WebInspector.HeapSnapshotEdgesProvider = function(snapshot, filter, edgesIter)
WebInspector.HeapSnapshotEdgesProvider = function(snapshot, filter, edgesIter, indexProvider)
{
this.snapshot = snapshot;
if (filter)
edgesIter = new WebInspector.HeapSnapshotFilteredIterator(edgesIter, filter);
WebInspector.HeapSnapshotItemProvider.call(this, edgesIter);
WebInspector.HeapSnapshotItemProvider.call(this, edgesIter, indexProvider);
}
WebInspector.HeapSnapshotEdgesProvider.prototype = {
......@@ -2199,10 +2229,11 @@ WebInspector.HeapSnapshotEdgesProvider.prototype = {
WebInspector.HeapSnapshotNodesProvider = function(snapshot, filter, nodeIndexes)
{
this.snapshot = snapshot;
var it = new WebInspector.HeapSnapshotIndexRangeIterator(snapshot._allNodes(), nodeIndexes);
var indexProvider = new WebInspector.HeapSnapshotNodeIndexProvider(snapshot);
var it = new WebInspector.HeapSnapshotIndexRangeIterator(indexProvider, nodeIndexes);
if (filter)
it = new WebInspector.HeapSnapshotFilteredIterator(it, filter);
WebInspector.HeapSnapshotItemProvider.call(this, it);
WebInspector.HeapSnapshotItemProvider.call(this, it, indexProvider);
}
WebInspector.HeapSnapshotNodesProvider.prototype = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment