Skip to content

Commit

Permalink
Omit overlapping instances from matches
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstjules committed Mar 23, 2017
1 parent da7451c commit 106a079
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Inspector extends EventEmitter {
* @fires Inspector#match
*/
_analyze() {
var key, match, i, groups;
var key, match, i, groups, nodeArrays;

var sortedKeys = Object.keys(this._map)
.filter(key => this._map[key].length >= this._minInstances)
Expand All @@ -142,8 +142,11 @@ class Inspector extends EventEmitter {
continue;
}

nodeArrays = this._map[key].slice(0);
this._omitOverlappingInstances(nodeArrays);

// groups will be of type Node[][][]
groups = [this._map[key].slice(0)];
groups = [nodeArrays];
if (this._identifiers) {
groups = this._groupByMatchingIdentifiers(groups);
}
Expand All @@ -162,6 +165,36 @@ class Inspector extends EventEmitter {
}
}

/**
* Removes overlapping instances from a group of node arrays. That is,
* if one instance has nodes abcd, and another has bcde, then bcde will
* be removed from the array.
*
* @private
*
* @param {Node[][]} nodeArrays
*/
_omitOverlappingInstances(nodeArrays) {
var set = new Set();

var hasOverlap = (nodes) => {
return nodes.some(node => set.has(node));
};

var addNodes = (nodes) => {
nodes.forEach(node => set.add(node));
};

for (var i = 0; i < nodeArrays.length; i++) {
if (hasOverlap(nodeArrays[i])) {
nodeArrays.splice(i--, 1);
continue;
} else {
addNodes(nodeArrays[i]);
}
}
}

/**
* Iterates over the multi-dimensional array of nodes, and returns a new
* array grouping them based on matching identifiers.
Expand Down Expand Up @@ -224,6 +257,7 @@ class Inspector extends EventEmitter {
return nodeArrays.some(array => array.indexOf(node) !== -1)
});
};

var isComplete = (nodes) => {
return (!nodeUtils.typesMatch(nodes) || alreadyIncluded(nodes)) ||
(this._identifiers && !nodeUtils.identifiersMatch(nodes)) ||
Expand Down

0 comments on commit 106a079

Please sign in to comment.