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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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