From 00b8db4e2b027c56adadc26dd52d588d90044ce1 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 19 Feb 2013 18:47:03 -0600 Subject: [PATCH] Fix DFA edge maps --- Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs | 33 ++++++++++++++++++++---- Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs | 7 ++--- Antlr4.Runtime/Dfa/DFAState.cs | 25 +++++++----------- Antlr4.Runtime/Dfa/IEdgeMap`1.cs | 4 +-- Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs | 2 +- Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs | 5 ++-- 6 files changed, 48 insertions(+), 28 deletions(-) diff --git a/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs b/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs index d1b756b48..0b921e1af 100644 --- a/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs +++ b/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs @@ -27,6 +27,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +using System.Collections; using System.Collections.Generic; using Antlr4.Runtime.Dfa; using Sharpen; @@ -51,24 +52,46 @@ namespace Antlr4.Runtime.Dfa public abstract Antlr4.Runtime.Dfa.AbstractEdgeMap Put(int key, T value); - public virtual Antlr4.Runtime.Dfa.AbstractEdgeMap PutAll<_T0>(IEdgeMap<_T0> m) - where _T0:T + IEdgeMap IEdgeMap.Put(int key, T value) + { + return Put(key, value); + } + + public virtual Antlr4.Runtime.Dfa.AbstractEdgeMap PutAll(IEdgeMap m) { Antlr4.Runtime.Dfa.AbstractEdgeMap result = this; - foreach (KeyValuePair entry in m.EntrySet()) + foreach (KeyValuePair entry in m) { result = result.Put(entry.Key, entry.Value); } return result; } + IEdgeMap IEdgeMap.PutAll(IEdgeMap m) + { + return PutAll(m); + } + public abstract Antlr4.Runtime.Dfa.AbstractEdgeMap Clear(); + IEdgeMap IEdgeMap.Clear() + { + return Clear(); + } + public abstract Antlr4.Runtime.Dfa.AbstractEdgeMap Remove(int key); + IEdgeMap IEdgeMap.Remove(int key) + { + return Remove(key); + } + public abstract bool ContainsKey(int arg1); - public abstract T Get(int arg1); + public abstract T this[int arg1] + { + get; + } public abstract bool IsEmpty { @@ -80,7 +103,7 @@ namespace Antlr4.Runtime.Dfa get; } - public abstract IDictionary ToMap(); + public abstract IReadOnlyDictionary ToMap(); public virtual IEnumerator> GetEnumerator() { diff --git a/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs b/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs index 5300d93d5..bc4483a57 100644 --- a/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs +++ b/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs @@ -29,6 +29,7 @@ */ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using Antlr4.Runtime.Dfa; using Sharpen; @@ -106,7 +107,7 @@ namespace Antlr4.Runtime.Dfa return ((Antlr4.Runtime.Dfa.ArrayEdgeMap)Put(key, null)); } - public override AbstractEdgeMap PutAll<_T0>(IEdgeMap<_T0> m) + public override AbstractEdgeMap PutAll(IEdgeMap m) { if (m.IsEmpty) { @@ -167,7 +168,7 @@ namespace Antlr4.Runtime.Dfa return this; } - public override IDictionary ToMap() + public override IReadOnlyDictionary ToMap() { if (IsEmpty) { @@ -182,7 +183,7 @@ namespace Antlr4.Runtime.Dfa } result[i + minIndex] = arrayData[i]; } - return result; + return new ReadOnlyDictionary(result); } } } diff --git a/Antlr4.Runtime/Dfa/DFAState.cs b/Antlr4.Runtime/Dfa/DFAState.cs index c4c4ada62..7b1067a6b 100644 --- a/Antlr4.Runtime/Dfa/DFAState.cs +++ b/Antlr4.Runtime/Dfa/DFAState.cs @@ -29,6 +29,8 @@ */ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; using System.Text; using Antlr4.Runtime.Atn; using Antlr4.Runtime.Dfa; @@ -216,7 +218,7 @@ namespace Antlr4.Runtime.Dfa } } - public virtual IDictionary EdgeMap + public virtual IReadOnlyDictionary EdgeMap { get { @@ -257,7 +259,7 @@ namespace Antlr4.Runtime.Dfa } } - public virtual IDictionary ContextEdgeMap + public virtual IReadOnlyDictionary ContextEdgeMap { get { @@ -265,26 +267,19 @@ namespace Antlr4.Runtime.Dfa { return Sharpen.Collections.EmptyMap(); } - IDictionary map = contextEdges.ToMap(); + IReadOnlyDictionary map = contextEdges.ToMap(); if (map.ContainsKey(-1)) { if (map.Count == 1) { - return Sharpen.Collections.SingletonMap(PredictionContext.EmptyFullStateKey, map. - Get(-1)); + return Sharpen.Collections.SingletonMap(PredictionContext.EmptyFullStateKey, map[-1]); } else { - try - { - map.Put(PredictionContext.EmptyFullStateKey, Sharpen.Collections.Remove(map, -1)); - } - catch (NotSupportedException) - { - // handles read only, non-singleton maps - map = new SortedDictionary(map); - map.Put(PredictionContext.EmptyFullStateKey, Sharpen.Collections.Remove(map, -1)); - } + Dictionary result = map.ToDictionary(i => i.Key, i => i.Value); + result.Add(PredictionContext.EmptyFullStateKey, result[-1]); + result.Remove(-1); + map = new ReadOnlyDictionary(new SortedDictionary(result)); } } return map; diff --git a/Antlr4.Runtime/Dfa/IEdgeMap`1.cs b/Antlr4.Runtime/Dfa/IEdgeMap`1.cs index b741ee39d..f0f24dcec 100644 --- a/Antlr4.Runtime/Dfa/IEdgeMap`1.cs +++ b/Antlr4.Runtime/Dfa/IEdgeMap`1.cs @@ -61,12 +61,12 @@ namespace Antlr4.Runtime.Dfa IEdgeMap Remove(int key); [return: NotNull] - IEdgeMap PutAll<_T0>(IEdgeMap<_T0> m) where _T0:T; + IEdgeMap PutAll(IEdgeMap m); [return: NotNull] IEdgeMap Clear(); [return: NotNull] - IDictionary ToMap(); + IReadOnlyDictionary ToMap(); } } diff --git a/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs b/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs index d4e2a146a..56601a708 100644 --- a/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs +++ b/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs @@ -149,7 +149,7 @@ namespace Antlr4.Runtime.Dfa return this; } - public override IDictionary ToMap() + public override IReadOnlyDictionary ToMap() { if (IsEmpty) { diff --git a/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs b/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs index d65567f14..5ae1d1430 100644 --- a/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs +++ b/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs @@ -29,6 +29,7 @@ */ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using Antlr4.Runtime.Dfa; using Sharpen; @@ -200,7 +201,7 @@ namespace Antlr4.Runtime.Dfa return result; } - public override IDictionary ToMap() + public override IReadOnlyDictionary ToMap() { if (IsEmpty) { @@ -211,7 +212,7 @@ namespace Antlr4.Runtime.Dfa { result[keys[i]] = values[i]; } - return result; + return new ReadOnlyDictionary(result); } } }