Revert "fix cache issue"

This reverts commit 5d4c1e2b3b.
This commit is contained in:
miliu 2019-09-29 13:58:30 -07:00
parent 294d460aae
commit 16ce0848fe
4 changed files with 34 additions and 35 deletions

View File

@ -79,7 +79,7 @@ function calculateHashString(parent, returnState) {
// can be used for both lexers and parsers. // can be used for both lexers and parsers.
function PredictionContextCache() { function PredictionContextCache() {
this.cache = new Map(); this.cache = {};
return this; return this;
} }
@ -91,16 +91,16 @@ PredictionContextCache.prototype.add = function(ctx) {
if (ctx === PredictionContext.EMPTY) { if (ctx === PredictionContext.EMPTY) {
return PredictionContext.EMPTY; return PredictionContext.EMPTY;
} }
var existing = this.cache.get(ctx) || null; var existing = this.cache[ctx] || null;
if (existing !== null) { if (existing !== null) {
return existing; return existing;
} }
this.cache.set(ctx, ctx); this.cache[ctx] = ctx;
return ctx; return ctx;
}; };
PredictionContextCache.prototype.get = function(ctx) { PredictionContextCache.prototype.get = function(ctx) {
return this.cache.get(ctx) || null; return this.cache[ctx] || null;
}; };
Object.defineProperty(PredictionContextCache.prototype, "length", { Object.defineProperty(PredictionContextCache.prototype, "length", {
@ -642,16 +642,16 @@ function mergeArrays(a, b, rootIsWildcard, mergeCache) {
// ones. // ones.
// / // /
function combineCommonParents(parents) { function combineCommonParents(parents) {
var uniqueParents = new Map(); var uniqueParents = {};
for (var p = 0; p < parents.length; p++) { for (var p = 0; p < parents.length; p++) {
var parent = parents[p]; var parent = parents[p];
if (!uniqueParents.get(parent)) { if (!(parent in uniqueParents)) {
uniqueParents.set(parent, parent); uniqueParents[parent] = parent;
} }
} }
for (var q = 0; q < parents.length; q++) { for (var q = 0; q < parents.length; q++) {
parents[q] = uniqueParents.get(parents[q]); parents[q] = uniqueParents[parents[q]];
} }
} }
@ -659,13 +659,13 @@ function getCachedPredictionContext(context, contextCache, visited) {
if (context.isEmpty()) { if (context.isEmpty()) {
return context; return context;
} }
var existing = visited.get(context) || null; var existing = visited[context] || null;
if (existing !== null) { if (existing !== null) {
return existing; return existing;
} }
existing = contextCache.get(context); existing = contextCache.get(context);
if (existing !== null) { if (existing !== null) {
visited.set(context, existing); visited[context] = existing;
return existing; return existing;
} }
var changed = false; var changed = false;
@ -685,7 +685,7 @@ function getCachedPredictionContext(context, contextCache, visited) {
} }
if (!changed) { if (!changed) {
contextCache.add(context); contextCache.add(context);
visited.set(context, context); visited[context] = context;
return context; return context;
} }
var updated = null; var updated = null;
@ -698,8 +698,8 @@ function getCachedPredictionContext(context, contextCache, visited) {
updated = new ArrayPredictionContext(parents, context.returnStates); updated = new ArrayPredictionContext(parents, context.returnStates);
} }
contextCache.add(updated); contextCache.add(updated);
visited.set(updated, updated); visited[updated] = updated;
visited.set(context, updated); visited[context] = updated;
return updated; return updated;
} }
@ -710,13 +710,13 @@ function getAllContextNodes(context, nodes, visited) {
nodes = []; nodes = [];
return getAllContextNodes(context, nodes, visited); return getAllContextNodes(context, nodes, visited);
} else if (visited === null) { } else if (visited === null) {
visited = new Map(); visited = {};
return getAllContextNodes(context, nodes, visited); return getAllContextNodes(context, nodes, visited);
} else { } else {
if (context === null || visited.get(context) !== null) { if (context === null || visited[context] !== null) {
return nodes; return nodes;
} }
visited.set(context, context); visited[context] = context;
nodes.push(context); nodes.push(context);
for (var i = 0; i < context.length; i++) { for (var i = 0; i < context.length; i++) {
getAllContextNodes(context.getParent(i), nodes, visited); getAllContextNodes(context.getParent(i), nodes, visited);

View File

@ -197,14 +197,14 @@ BitSet.prototype.toString = function () {
return "{" + this.values().join(", ") + "}"; return "{" + this.values().join(", ") + "}";
}; };
function Dictionary(hashFunction, equalsFunction) { function Map(hashFunction, equalsFunction) {
this.data = {}; this.data = {};
this.hashFunction = hashFunction || standardHashCodeFunction; this.hashFunction = hashFunction || standardHashCodeFunction;
this.equalsFunction = equalsFunction || standardEqualsFunction; this.equalsFunction = equalsFunction || standardEqualsFunction;
return this; return this;
} }
Object.defineProperty(Dictionary.prototype, "length", { Object.defineProperty(Map.prototype, "length", {
get: function () { get: function () {
var l = 0; var l = 0;
for (var hashKey in this.data) { for (var hashKey in this.data) {
@ -216,7 +216,7 @@ Object.defineProperty(Dictionary.prototype, "length", {
} }
}); });
Dictionary.prototype.put = function (key, value) { Map.prototype.put = function (key, value) {
var hashKey = "hash_" + this.hashFunction(key); var hashKey = "hash_" + this.hashFunction(key);
if (hashKey in this.data) { if (hashKey in this.data) {
var entries = this.data[hashKey]; var entries = this.data[hashKey];
@ -236,7 +236,7 @@ Dictionary.prototype.put = function (key, value) {
} }
}; };
Dictionary.prototype.containsKey = function (key) { Map.prototype.containsKey = function (key) {
var hashKey = "hash_" + this.hashFunction(key); var hashKey = "hash_" + this.hashFunction(key);
if(hashKey in this.data) { if(hashKey in this.data) {
var entries = this.data[hashKey]; var entries = this.data[hashKey];
@ -249,7 +249,7 @@ Dictionary.prototype.containsKey = function (key) {
return false; return false;
}; };
Dictionary.prototype.get = function (key) { Map.prototype.get = function (key) {
var hashKey = "hash_" + this.hashFunction(key); var hashKey = "hash_" + this.hashFunction(key);
if(hashKey in this.data) { if(hashKey in this.data) {
var entries = this.data[hashKey]; var entries = this.data[hashKey];
@ -262,7 +262,7 @@ Dictionary.prototype.get = function (key) {
return null; return null;
}; };
Dictionary.prototype.entries = function () { Map.prototype.entries = function () {
var l = []; var l = [];
for (var key in this.data) { for (var key in this.data) {
if (key.indexOf("hash_") === 0) { if (key.indexOf("hash_") === 0) {
@ -273,21 +273,21 @@ Dictionary.prototype.entries = function () {
}; };
Dictionary.prototype.getKeys = function () { Map.prototype.getKeys = function () {
return this.entries().map(function(e) { return this.entries().map(function(e) {
return e.key; return e.key;
}); });
}; };
Dictionary.prototype.getValues = function () { Map.prototype.getValues = function () {
return this.entries().map(function(e) { return this.entries().map(function(e) {
return e.value; return e.value;
}); });
}; };
Dictionary.prototype.toString = function () { Map.prototype.toString = function () {
var ss = this.entries().map(function(entry) { var ss = this.entries().map(function(entry) {
return '{' + entry.key + ':' + entry.value + '}'; return '{' + entry.key + ':' + entry.value + '}';
}); });
@ -324,7 +324,6 @@ AltDict.prototype.values = function () {
}; };
function DoubleDict() { function DoubleDict() {
this['doubleDictMap'] = this['doubleDictMap'] || new Map();
return this; return this;
} }
@ -390,17 +389,17 @@ function hashStuff() {
} }
DoubleDict.prototype.get = function (a, b) { DoubleDict.prototype.get = function (a, b) {
var d = this['doubleDictMap'].get(a) || null; var d = this[a] || null;
return d === null ? null : (d.get(b) || null); return d === null ? null : (d[b] || null);
}; };
DoubleDict.prototype.set = function (a, b, o) { DoubleDict.prototype.set = function (a, b, o) {
var d = this['doubleDictMap'].get(a) || null; var d = this[a] || null;
if (d === null) { if (d === null) {
d = new Map(); d = {};
this['doubleDictMap'].set(a, d); this[a] = d;
} }
d.set(b, o); d[b] = o;
}; };
@ -439,7 +438,7 @@ function equalArrays(a, b)
exports.Hash = Hash; exports.Hash = Hash;
exports.Set = Set; exports.Set = Set;
exports.Dictionary = Dictionary; exports.Map = Map;
exports.BitSet = BitSet; exports.BitSet = BitSet;
exports.AltDict = AltDict; exports.AltDict = AltDict;
exports.DoubleDict = DoubleDict; exports.DoubleDict = DoubleDict;

View File

@ -44,7 +44,7 @@ ATNSimulator.prototype.getCachedContext = function(context) {
if (this.sharedContextCache ===null) { if (this.sharedContextCache ===null) {
return context; return context;
} }
var visited = new Map(); var visited = {};
return getCachedPredictionContext(context, this.sharedContextCache, visited); return getCachedPredictionContext(context, this.sharedContextCache, visited);
}; };

View File

@ -10,7 +10,7 @@
// ambiguities. // ambiguities.
var Set = require('./../Utils').Set; var Set = require('./../Utils').Set;
var Map = require('./../Utils').Dictionary; var Map = require('./../Utils').Map;
var BitSet = require('./../Utils').BitSet; var BitSet = require('./../Utils').BitSet;
var AltDict = require('./../Utils').AltDict; var AltDict = require('./../Utils').AltDict;
var ATN = require('./ATN').ATN; var ATN = require('./ATN').ATN;