Use TryGetValue and ContainsKey instead of Get extension method

This commit is contained in:
Sam Harwell 2013-02-27 16:20:24 -06:00
parent 25551e6985
commit efc964bdc6
9 changed files with 41 additions and 38 deletions

View File

@ -279,8 +279,8 @@ namespace Antlr4.Runtime.Atn
} }
ATNConfig config = (ATNConfig)o; ATNConfig config = (ATNConfig)o;
long configKey = GetKey(config); long configKey = GetKey(config);
ATNConfig mergedConfig = mergedConfigs.Get(configKey); ATNConfig mergedConfig;
if (mergedConfig != null && CanMerge(config, configKey, mergedConfig)) if (mergedConfigs.TryGetValue(configKey, out mergedConfig) && CanMerge(config, configKey, mergedConfig))
{ {
return mergedConfig.Contains(config); return mergedConfig.Contains(config);
} }
@ -326,8 +326,8 @@ namespace Antlr4.Runtime.Atn
} }
bool addKey; bool addKey;
long key = GetKey(e); long key = GetKey(e);
ATNConfig mergedConfig = mergedConfigs.Get(key); ATNConfig mergedConfig;
addKey = (mergedConfig == null); addKey = !mergedConfigs.TryGetValue(key, out mergedConfig);
if (mergedConfig != null && CanMerge(e, key, mergedConfig)) if (mergedConfig != null && CanMerge(e, key, mergedConfig))
{ {
mergedConfig.OuterContextDepth = Math.Max(mergedConfig.OuterContextDepth, e.OuterContextDepth mergedConfig.OuterContextDepth = Math.Max(mergedConfig.OuterContextDepth, e.OuterContextDepth
@ -639,7 +639,8 @@ namespace Antlr4.Runtime.Atn
ATNConfig config = configs[index]; ATNConfig config = configs[index];
configs.Remove(config); configs.Remove(config);
long key = GetKey(config); long key = GetKey(config);
if (mergedConfigs.Get(key) == config) ATNConfig existing;
if (mergedConfigs.TryGetValue(key, out existing) && existing == config)
{ {
mergedConfigs.Remove(key); mergedConfigs.Remove(key);
} }

View File

@ -160,8 +160,8 @@ namespace Antlr4.Runtime.Atn
{ {
throw new NotSupportedException("Appending a tree suffix is not yet supported."); throw new NotSupportedException("Appending a tree suffix is not yet supported.");
} }
PredictionContext result = visited.Get(context); PredictionContext result;
if (result == null) if (!visited.TryGetValue(context, out result))
{ {
if (context.IsEmpty) if (context.IsEmpty)
{ {

View File

@ -649,8 +649,8 @@ namespace Antlr4.Runtime.Atn
{ {
System.Diagnostics.Debug.Assert(!configs.HasSemanticContext); System.Diagnostics.Debug.Assert(!configs.HasSemanticContext);
DFAState proposed = new DFAState(configs, 0, MaxDfaEdge); DFAState proposed = new DFAState(configs, 0, MaxDfaEdge);
DFAState existing = atn.modeToDFA[mode].states.Get(proposed); DFAState existing;
if (existing != null) if (atn.modeToDFA[mode].states.TryGetValue(proposed, out existing))
{ {
return existing; return existing;
} }

View File

@ -1999,8 +1999,8 @@ namespace Antlr4.Runtime.Atn
configs.OptimizeConfigs(this); configs.OptimizeConfigs(this);
} }
DFAState proposed = CreateDFAState(configs); DFAState proposed = CreateDFAState(configs);
DFAState existing = dfa.states.Get(proposed); DFAState existing;
if (existing != null) if (dfa.states.TryGetValue(proposed, out existing))
{ {
return existing; return existing;
} }
@ -2016,8 +2016,7 @@ namespace Antlr4.Runtime.Atn
if (configs.Count < size) if (configs.Count < size)
{ {
proposed = CreateDFAState(configs); proposed = CreateDFAState(configs);
existing = dfa.states.Get(proposed); if (dfa.states.TryGetValue(proposed, out existing))
if (existing != null)
{ {
return existing; return existing;
} }

View File

@ -347,13 +347,12 @@ namespace Antlr4.Runtime.Atn
{ {
return context; return context;
} }
Antlr4.Runtime.Atn.PredictionContext existing = visited.Get(context); Antlr4.Runtime.Atn.PredictionContext existing;
if (existing != null) if (visited.TryGetValue(context, out existing))
{ {
return existing; return existing;
} }
existing = contextCache.Get(context); if (contextCache.TryGetValue(context, out existing))
if (existing != null)
{ {
visited[context] = existing; visited[context] = existing;
return existing; return existing;

View File

@ -74,8 +74,8 @@ namespace Antlr4.Runtime.Atn
{ {
return context; return context;
} }
PredictionContext result = contexts.Get(context); PredictionContext result;
if (result == null) if (!contexts.TryGetValue(context, out result))
{ {
result = context; result = context;
contexts[context] = context; contexts[context] = context;
@ -92,8 +92,8 @@ namespace Antlr4.Runtime.Atn
} }
PredictionContextCache.PredictionContextAndInt operands = new PredictionContextCache.PredictionContextAndInt PredictionContextCache.PredictionContextAndInt operands = new PredictionContextCache.PredictionContextAndInt
(context, invokingState); (context, invokingState);
PredictionContext result = childContexts.Get(operands); PredictionContext result;
if (result == null) if (!childContexts.TryGetValue(operands, out result))
{ {
result = context.GetChild(invokingState); result = context.GetChild(invokingState);
result = GetAsCached(result); result = GetAsCached(result);
@ -110,8 +110,8 @@ namespace Antlr4.Runtime.Atn
} }
PredictionContextCache.IdentityCommutativePredictionContextOperands operands = new PredictionContextCache.IdentityCommutativePredictionContextOperands operands = new
PredictionContextCache.IdentityCommutativePredictionContextOperands(x, y); PredictionContextCache.IdentityCommutativePredictionContextOperands(x, y);
PredictionContext result = joinContexts.Get(operands); PredictionContext result;
if (result != null) if (joinContexts.TryGetValue(operands, out result))
{ {
return result; return result;
} }

View File

@ -834,8 +834,8 @@ namespace Antlr4.Runtime.Atn
(); ();
foreach (ATNConfig c in configs) foreach (ATNConfig c in configs)
{ {
BitSet alts = configToAlts.Get(c); BitSet alts;
if (alts == null) if (!configToAlts.TryGetValue(c, out alts))
{ {
alts = new BitSet(); alts = new BitSet();
configToAlts[c] = alts; configToAlts[c] = alts;
@ -867,8 +867,8 @@ namespace Antlr4.Runtime.Atn
IDictionary<ATNState, BitSet> m = new Dictionary<ATNState, BitSet>(); IDictionary<ATNState, BitSet> m = new Dictionary<ATNState, BitSet>();
foreach (ATNConfig c in configs) foreach (ATNConfig c in configs)
{ {
BitSet alts = m.Get(c.State); BitSet alts;
if (alts == null) if (!m.TryGetValue(c.State, out alts))
{ {
alts = new BitSet(); alts = new BitSet();
m[c.State] = alts; m[c.State] = alts;

View File

@ -69,9 +69,8 @@ namespace Antlr4.Runtime.Misc
foreach (Tuple<RuleDependencyAttribute, ICustomAttributeProvider> dependency in dependencies) foreach (Tuple<RuleDependencyAttribute, ICustomAttributeProvider> dependency in dependencies)
{ {
Type recognizerType = dependency.Item1.Recognizer; Type recognizerType = dependency.Item1.Recognizer;
IList<Tuple<RuleDependencyAttribute, ICustomAttributeProvider>> list = recognizerDependencies.Get IList<Tuple<RuleDependencyAttribute, ICustomAttributeProvider>> list;
(recognizerType); if (!recognizerDependencies.TryGetValue(recognizerType, out list))
if (list == null)
{ {
list = new List<Tuple<RuleDependencyAttribute, ICustomAttributeProvider>>(); list = new List<Tuple<RuleDependencyAttribute, ICustomAttributeProvider>>();
recognizerDependencies[recognizerType] = list; recognizerDependencies[recognizerType] = list;

View File

@ -249,8 +249,8 @@ namespace Antlr4.Runtime
/// </remarks> /// </remarks>
public virtual void Rollback(string programName, int instructionIndex) public virtual void Rollback(string programName, int instructionIndex)
{ {
IList<TokenStreamRewriter.RewriteOperation> @is = programs.Get(programName); IList<TokenStreamRewriter.RewriteOperation> @is;
if (@is != null) if (programs.TryGetValue(programName, out @is))
{ {
programs[programName] = new List<RewriteOperation>(@is.Skip(MinTokenIndex).Take(instructionIndex - MinTokenIndex)); programs[programName] = new List<RewriteOperation>(@is.Skip(MinTokenIndex).Take(instructionIndex - MinTokenIndex));
} }
@ -406,8 +406,8 @@ namespace Antlr4.Runtime
protected internal virtual IList<TokenStreamRewriter.RewriteOperation> GetProgram protected internal virtual IList<TokenStreamRewriter.RewriteOperation> GetProgram
(string name) (string name)
{ {
IList<TokenStreamRewriter.RewriteOperation> @is = programs.Get(name); IList<TokenStreamRewriter.RewriteOperation> @is;
if (@is == null) if (!programs.TryGetValue(name, out @is))
{ {
@is = InitializeProgram(name); @is = InitializeProgram(name);
} }
@ -457,7 +457,10 @@ namespace Antlr4.Runtime
public virtual string GetText(string programName, Interval interval) public virtual string GetText(string programName, Interval interval)
{ {
IList<TokenStreamRewriter.RewriteOperation> rewrites = programs.Get(programName); IList<TokenStreamRewriter.RewriteOperation> rewrites;
if (!programs.TryGetValue(programName, out rewrites))
rewrites = null;
int start = interval.a; int start = interval.a;
int stop = interval.b; int stop = interval.b;
// ensure start/end are in range // ensure start/end are in range
@ -482,8 +485,10 @@ namespace Antlr4.Runtime
int i = start; int i = start;
while (i <= stop && i < tokens.Size) while (i <= stop && i < tokens.Size)
{ {
TokenStreamRewriter.RewriteOperation op = indexToOp.Get(i); TokenStreamRewriter.RewriteOperation op;
if (indexToOp.TryGetValue(i, out op))
indexToOp.Remove(i); indexToOp.Remove(i);
// remove so any left have index size-1 // remove so any left have index size-1
IToken t = tokens.Get(i); IToken t = tokens.Get(i);
if (op == null) if (op == null)
@ -696,7 +701,7 @@ namespace Antlr4.Runtime
continue; continue;
} }
// ignore deleted ops // ignore deleted ops
if (m.Get(op.index) != null) if (m.ContainsKey(op.index))
{ {
throw new InvalidOperationException("should only be one op per index"); throw new InvalidOperationException("should only be one op per index");
} }