From efc964bdc63eac4630f58d88016042d80e8a2705 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 27 Feb 2013 16:20:24 -0600 Subject: [PATCH] Use TryGetValue and ContainsKey instead of Get extension method --- .../CSharp/Antlr4.Runtime/Atn/ATNConfigSet.cs | 11 +++++----- .../Atn/ArrayPredictionContext.cs | 4 ++-- .../Antlr4.Runtime/Atn/LexerATNSimulator.cs | 4 ++-- .../Antlr4.Runtime/Atn/ParserATNSimulator.cs | 7 +++---- .../Antlr4.Runtime/Atn/PredictionContext.cs | 7 +++---- .../Atn/PredictionContextCache.cs | 12 +++++------ .../Antlr4.Runtime/Atn/PredictionMode.cs | 8 +++---- .../Misc/RuleDependencyChecker.cs | 5 ++--- .../Antlr4.Runtime/TokenStreamRewriter.cs | 21 ++++++++++++------- 9 files changed, 41 insertions(+), 38 deletions(-) diff --git a/runtime/CSharp/Antlr4.Runtime/Atn/ATNConfigSet.cs b/runtime/CSharp/Antlr4.Runtime/Atn/ATNConfigSet.cs index f519b6565..e4abf06c1 100644 --- a/runtime/CSharp/Antlr4.Runtime/Atn/ATNConfigSet.cs +++ b/runtime/CSharp/Antlr4.Runtime/Atn/ATNConfigSet.cs @@ -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); } diff --git a/runtime/CSharp/Antlr4.Runtime/Atn/ArrayPredictionContext.cs b/runtime/CSharp/Antlr4.Runtime/Atn/ArrayPredictionContext.cs index 6437122d0..e6d79be6b 100644 --- a/runtime/CSharp/Antlr4.Runtime/Atn/ArrayPredictionContext.cs +++ b/runtime/CSharp/Antlr4.Runtime/Atn/ArrayPredictionContext.cs @@ -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) { diff --git a/runtime/CSharp/Antlr4.Runtime/Atn/LexerATNSimulator.cs b/runtime/CSharp/Antlr4.Runtime/Atn/LexerATNSimulator.cs index 3a9ff0dca..72acdcbc5 100644 --- a/runtime/CSharp/Antlr4.Runtime/Atn/LexerATNSimulator.cs +++ b/runtime/CSharp/Antlr4.Runtime/Atn/LexerATNSimulator.cs @@ -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; } diff --git a/runtime/CSharp/Antlr4.Runtime/Atn/ParserATNSimulator.cs b/runtime/CSharp/Antlr4.Runtime/Atn/ParserATNSimulator.cs index b228691d4..179a0febc 100644 --- a/runtime/CSharp/Antlr4.Runtime/Atn/ParserATNSimulator.cs +++ b/runtime/CSharp/Antlr4.Runtime/Atn/ParserATNSimulator.cs @@ -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; } diff --git a/runtime/CSharp/Antlr4.Runtime/Atn/PredictionContext.cs b/runtime/CSharp/Antlr4.Runtime/Atn/PredictionContext.cs index 70d6da46a..0eef77673 100644 --- a/runtime/CSharp/Antlr4.Runtime/Atn/PredictionContext.cs +++ b/runtime/CSharp/Antlr4.Runtime/Atn/PredictionContext.cs @@ -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; diff --git a/runtime/CSharp/Antlr4.Runtime/Atn/PredictionContextCache.cs b/runtime/CSharp/Antlr4.Runtime/Atn/PredictionContextCache.cs index 3a987f804..18c079e4c 100644 --- a/runtime/CSharp/Antlr4.Runtime/Atn/PredictionContextCache.cs +++ b/runtime/CSharp/Antlr4.Runtime/Atn/PredictionContextCache.cs @@ -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; } diff --git a/runtime/CSharp/Antlr4.Runtime/Atn/PredictionMode.cs b/runtime/CSharp/Antlr4.Runtime/Atn/PredictionMode.cs index 9fa745e60..c7c280be6 100644 --- a/runtime/CSharp/Antlr4.Runtime/Atn/PredictionMode.cs +++ b/runtime/CSharp/Antlr4.Runtime/Atn/PredictionMode.cs @@ -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 m = new Dictionary(); 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; diff --git a/runtime/CSharp/Antlr4.Runtime/Misc/RuleDependencyChecker.cs b/runtime/CSharp/Antlr4.Runtime/Misc/RuleDependencyChecker.cs index 51ae9a361..8128a1640 100644 --- a/runtime/CSharp/Antlr4.Runtime/Misc/RuleDependencyChecker.cs +++ b/runtime/CSharp/Antlr4.Runtime/Misc/RuleDependencyChecker.cs @@ -69,9 +69,8 @@ namespace Antlr4.Runtime.Misc foreach (Tuple dependency in dependencies) { Type recognizerType = dependency.Item1.Recognizer; - IList> list = recognizerDependencies.Get - (recognizerType); - if (list == null) + IList> list; + if (!recognizerDependencies.TryGetValue(recognizerType, out list)) { list = new List>(); recognizerDependencies[recognizerType] = list; diff --git a/runtime/CSharp/Antlr4.Runtime/TokenStreamRewriter.cs b/runtime/CSharp/Antlr4.Runtime/TokenStreamRewriter.cs index 4c4643a21..9824a51dc 100644 --- a/runtime/CSharp/Antlr4.Runtime/TokenStreamRewriter.cs +++ b/runtime/CSharp/Antlr4.Runtime/TokenStreamRewriter.cs @@ -249,8 +249,8 @@ namespace Antlr4.Runtime /// public virtual void Rollback(string programName, int instructionIndex) { - IList @is = programs.Get(programName); - if (@is != null) + IList @is; + if (programs.TryGetValue(programName, out @is)) { programs[programName] = new List(@is.Skip(MinTokenIndex).Take(instructionIndex - MinTokenIndex)); } @@ -406,8 +406,8 @@ namespace Antlr4.Runtime protected internal virtual IList GetProgram (string name) { - IList @is = programs.Get(name); - if (@is == null) + IList @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 rewrites = programs.Get(programName); + IList 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"); }