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

View File

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

View File

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

View File

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