diff --git a/Antlr4.Runtime/Atn/ATNConfigSet.cs b/Antlr4.Runtime/Atn/ATNConfigSet.cs index 5049069fc..c41e8f648 100644 --- a/Antlr4.Runtime/Atn/ATNConfigSet.cs +++ b/Antlr4.Runtime/Atn/ATNConfigSet.cs @@ -664,45 +664,5 @@ namespace Antlr4.Runtime.Atn throw new InvalidOperationException("This ATNConfigSet is read only."); } } - - private sealed class ATNConfigSetIterator : IEnumerator - { - internal int index = -1; - - internal bool removed = false; - - public bool HasNext() - { - return this.index + 1 < this._enclosing.configs.Count; - } - - public ATNConfig Next() - { - if (!this.HasNext()) - { - throw new InvalidOperationException(); - } - this.index++; - this.removed = false; - return this._enclosing.configs[this.index]; - } - - public void Remove() - { - if (this.removed || this.index < 0 || this.index >= this._enclosing.configs.Count) - { - throw new InvalidOperationException(); - } - this._enclosing.Remove(this.index); - this.removed = true; - } - - internal ATNConfigSetIterator(ATNConfigSet _enclosing) - { - this._enclosing = _enclosing; - } - - private readonly ATNConfigSet _enclosing; - } } } diff --git a/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs b/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs index ef2c4c2b1..c14239a89 100644 --- a/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs +++ b/Antlr4.Runtime/Dfa/AbstractEdgeMap`1.cs @@ -27,7 +27,6 @@ * (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; @@ -66,51 +65,19 @@ namespace Antlr4.Runtime.Dfa public abstract Antlr4.Runtime.Dfa.AbstractEdgeMap Remove(int key); - protected internal abstract class AbstractEntrySet : AbstractSet> - { - public override bool Contains(object o) - { - if (!(o is DictionaryEntry)) - { - return false; - } - KeyValuePair entry = (KeyValuePair)o; - if (entry.Key is int) - { - int key = (int)entry.Key; - object value = entry.Value; - T existing = this._enclosing._enclosing.Get(key); - return value == existing || (existing != null && existing.Equals(value)); - } - return false; - } - - public override int Count - { - get - { - return this._enclosing._enclosing.Size(); - } - } - - internal AbstractEntrySet(AbstractEdgeMap _enclosing) - { - this._enclosing = _enclosing; - } - - private readonly AbstractEdgeMap _enclosing; - } - public abstract bool ContainsKey(int arg1); - public abstract ISet> EntrySet(); - public abstract T Get(int arg1); - public abstract bool IsEmpty(); + public abstract bool IsEmpty + { + get; + } - public abstract int Size(); + public abstract int Count + { + get; + } public abstract IDictionary ToMap(); } diff --git a/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs b/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs index 188a550e0..12da82329 100644 --- a/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs +++ b/Antlr4.Runtime/Dfa/ArrayEdgeMap`1.cs @@ -46,28 +46,37 @@ namespace Antlr4.Runtime.Dfa arrayData = (T[])new object[maxIndex - minIndex + 1]; } - public override int Size() + public override int Count { - return size; + get + { + return size; + } } - public override bool IsEmpty() + public override bool IsEmpty { - return size == 0; + get + { + return size == 0; + } } public override bool ContainsKey(int key) { - return Get(key) != null; + return this[key] != null; } - public override T Get(int key) + public override T this[int key] { - if (key < minIndex || key > maxIndex) + get { - return null; + if (key < minIndex || key > maxIndex) + { + return null; + } + return arrayData[key - minIndex]; } - return arrayData[key - minIndex]; } public override AbstractEdgeMap Put(int key, T value) @@ -98,7 +107,7 @@ namespace Antlr4.Runtime.Dfa public override AbstractEdgeMap PutAll<_T0>(IEdgeMap<_T0> m) { - if (m.IsEmpty()) + if (m.IsEmpty) { return this; } @@ -124,7 +133,7 @@ namespace Antlr4.Runtime.Dfa if (m is SingletonEdgeMap) { SingletonEdgeMap other = (SingletonEdgeMap)m; - System.Diagnostics.Debug.Assert(!other.IsEmpty()); + System.Diagnostics.Debug.Assert(!other.IsEmpty); return ((Antlr4.Runtime.Dfa.ArrayEdgeMap)Put(other.GetKey(), other.GetValue()) ); } @@ -159,7 +168,7 @@ namespace Antlr4.Runtime.Dfa public override IDictionary ToMap() { - if (IsEmpty()) + if (IsEmpty) { return Sharpen.Collections.EmptyMap(); } @@ -174,99 +183,5 @@ namespace Antlr4.Runtime.Dfa } return result; } - - public override ISet> EntrySet() - { - return new ArrayEdgeMap.EntrySet(this); - } - - private class EntrySet : AbstractEdgeMap.AbstractEntrySet - { - public override IEnumerator> GetEnumerator() - { - return new ArrayEdgeMap.EntryIterator(this); - } - - internal EntrySet(ArrayEdgeMap _enclosing) : base(_enclosing) - { - this._enclosing = _enclosing; - } - - private readonly ArrayEdgeMap _enclosing; - } - - private class EntryIterator : IEnumerator> - { - private int current; - - private int currentIndex; - - public virtual bool HasNext() - { - return this.current < this._enclosing.Size(); - } - - public virtual KeyValuePair Next() - { - if (this.current >= this._enclosing.Size()) - { - throw new InvalidOperationException(); - } - while (this._enclosing.arrayData[this.currentIndex] == null) - { - this.currentIndex++; - } - this.current++; - this.currentIndex++; - return new _KeyValuePair_193(this); - } - - private sealed class _KeyValuePair_193 : KeyValuePair - { - public _KeyValuePair_193() - { - this.key = this._enclosing._enclosing.minIndex + this._enclosing.currentIndex - 1; - this.value = this._enclosing._enclosing.arrayData[this._enclosing.currentIndex - - 1]; - } - - private readonly int key; - - private readonly T value; - - public int Key - { - get - { - return this.key; - } - } - - public T Value - { - get - { - return this.value; - } - } - - public T SetValue(T value) - { - throw new NotSupportedException("Not supported yet."); - } - } - - public virtual void Remove() - { - throw new NotSupportedException("Not supported yet."); - } - - internal EntryIterator(ArrayEdgeMap _enclosing) - { - this._enclosing = _enclosing; - } - - private readonly ArrayEdgeMap _enclosing; - } } } diff --git a/Antlr4.Runtime/Dfa/DFAState.cs b/Antlr4.Runtime/Dfa/DFAState.cs index 30261cff7..457c28f2f 100644 --- a/Antlr4.Runtime/Dfa/DFAState.cs +++ b/Antlr4.Runtime/Dfa/DFAState.cs @@ -201,7 +201,7 @@ namespace Antlr4.Runtime.Dfa { return null; } - return edges.Get(symbol); + return edges[symbol]; } public virtual void SetTarget(int symbol, DFAState target) @@ -238,7 +238,7 @@ namespace Antlr4.Runtime.Dfa { invokingState = -1; } - return contextEdges.Get(invokingState); + return contextEdges[invokingState]; } public virtual void SetContextTarget(int invokingState, DFAState target) diff --git a/Antlr4.Runtime/Dfa/IEdgeMap`1.cs b/Antlr4.Runtime/Dfa/IEdgeMap`1.cs index 1a865a698..2bcea0c03 100644 --- a/Antlr4.Runtime/Dfa/IEdgeMap`1.cs +++ b/Antlr4.Runtime/Dfa/IEdgeMap`1.cs @@ -37,14 +37,22 @@ namespace Antlr4.Runtime.Dfa /// Sam Harwell public interface IEdgeMap { - int Size(); + int Count + { + get; + } - bool IsEmpty(); + bool IsEmpty + { + get; + } bool ContainsKey(int key); - [Nullable] - T Get(int key); + T this[int key] + { + get; + } [NotNull] IEdgeMap Put(int key, T value); @@ -60,8 +68,5 @@ namespace Antlr4.Runtime.Dfa [NotNull] IDictionary ToMap(); - - [NotNull] - ISet> EntrySet(); } } diff --git a/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs b/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs index 56a31638b..539ff3378 100644 --- a/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs +++ b/Antlr4.Runtime/Dfa/SingletonEdgeMap`1.cs @@ -27,7 +27,6 @@ * (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; using System.Collections.Generic; using Antlr4.Runtime.Dfa; using Sharpen; @@ -72,14 +71,20 @@ namespace Antlr4.Runtime.Dfa return value; } - public override int Size() + public override int Count { - return value != null ? 1 : 0; + get + { + return value != null ? 1 : 0; + } } - public override bool IsEmpty() + public override bool IsEmpty { - return value == null; + get + { + return value == null; + } } public override bool ContainsKey(int key) @@ -87,13 +92,16 @@ namespace Antlr4.Runtime.Dfa return key == this.key && value != null; } - public override T Get(int key) + public override T this[int key] { - if (key == this.key) + get { - return value; + if (key == this.key) + { + return value; + } + return null; } - return null; } public override AbstractEdgeMap Put(int key, T value) @@ -142,97 +150,11 @@ namespace Antlr4.Runtime.Dfa public override IDictionary ToMap() { - if (IsEmpty()) + if (IsEmpty) { return Sharpen.Collections.EmptyMap(); } return Sharpen.Collections.SingletonMap(key, value); } - - public override ISet> EntrySet() - { - return new SingletonEdgeMap.EntrySet(this); - } - - private class EntrySet : AbstractEdgeMap.AbstractEntrySet - { - public override IEnumerator> GetEnumerator() - { - return new SingletonEdgeMap.EntryIterator(this); - } - - internal EntrySet(SingletonEdgeMap _enclosing) : base(_enclosing) - { - this._enclosing = _enclosing; - } - - private readonly SingletonEdgeMap _enclosing; - } - - private class EntryIterator : IEnumerator> - { - private int current; - - public virtual bool HasNext() - { - return this.current < this._enclosing.Size(); - } - - public virtual KeyValuePair Next() - { - if (this.current >= this._enclosing.Size()) - { - throw new InvalidOperationException(); - } - this.current++; - return new _KeyValuePair_166(this); - } - - private sealed class _KeyValuePair_166 : KeyValuePair - { - public _KeyValuePair_166() - { - this.key = this._enclosing._enclosing._enclosing.key; - this.value = this._enclosing._enclosing._enclosing.value; - } - - private readonly int key; - - private readonly T value; - - public int Key - { - get - { - return this.key; - } - } - - public T Value - { - get - { - return this.value; - } - } - - public T SetValue(T value) - { - throw new NotSupportedException("Not supported yet."); - } - } - - public virtual void Remove() - { - throw new NotSupportedException("Not supported yet."); - } - - internal EntryIterator(SingletonEdgeMap _enclosing) - { - this._enclosing = _enclosing; - } - - private readonly SingletonEdgeMap _enclosing; - } } } diff --git a/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs b/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs index 2d9578379..65abe7687 100644 --- a/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs +++ b/Antlr4.Runtime/Dfa/SparseEdgeMap`1.cs @@ -82,29 +82,38 @@ namespace Antlr4.Runtime.Dfa return keys.Length; } - public override int Size() + public override int Count { - return values.Count; + get + { + return values.Count; + } } - public override bool IsEmpty() + public override bool IsEmpty { - return values.IsEmpty(); + get + { + return values.IsEmpty(); + } } public override bool ContainsKey(int key) { - return Get(key) != null; + return this[key] != null; } - public override T Get(int key) + public override T this[int key] { - int index = System.Array.BinarySearch(keys, 0, Size(), key); - if (index < 0) + get { - return null; + int index = System.Array.BinarySearch(keys, 0, Count, key); + if (index < 0) + { + return null; + } + return values[index]; } - return values[index]; } public override AbstractEdgeMap Put(int key, T value) @@ -119,7 +128,7 @@ namespace Antlr4.Runtime.Dfa } lock (values) { - int index = System.Array.BinarySearch(keys, 0, Size(), key); + int index = System.Array.BinarySearch(keys, 0, Count, key); if (index >= 0) { // replace existing entry @@ -128,14 +137,14 @@ namespace Antlr4.Runtime.Dfa } System.Diagnostics.Debug.Assert(index < 0 && value != null); int insertIndex = -index - 1; - if (Size() < GetMaxSparseSize() && insertIndex == Size()) + if (Count < GetMaxSparseSize() && insertIndex == Count) { // stay sparse and add new entry keys[insertIndex] = key; values.AddItem(value); return this; } - int desiredSize = Size() >= GetMaxSparseSize() ? GetMaxSparseSize() * 2 : GetMaxSparseSize + int desiredSize = Count >= GetMaxSparseSize() ? GetMaxSparseSize() * 2 : GetMaxSparseSize (); int space = maxIndex - minIndex + 1; // SparseEdgeMap only uses less memory than ArrayEdgeMap up to half the size of the symbol space @@ -161,7 +170,7 @@ namespace Antlr4.Runtime.Dfa public override AbstractEdgeMap Remove(int key) { - int index = System.Array.BinarySearch(keys, 0, Size(), key); + int index = System.Array.BinarySearch(keys, 0, Count, key); if (index < 0) { return this; @@ -173,14 +182,14 @@ namespace Antlr4.Runtime.Dfa } Antlr4.Runtime.Dfa.SparseEdgeMap result = new Antlr4.Runtime.Dfa.SparseEdgeMap (this, GetMaxSparseSize()); - System.Array.Copy(result.keys, index + 1, result.keys, index, Size() - index - 1); + System.Array.Copy(result.keys, index + 1, result.keys, index, Count - index - 1); result.values.RemoveAt(index); return result; } public override AbstractEdgeMap Clear() { - if (IsEmpty()) + if (IsEmpty) { return this; } @@ -192,102 +201,16 @@ namespace Antlr4.Runtime.Dfa public override IDictionary ToMap() { - if (IsEmpty()) + if (IsEmpty) { return Sharpen.Collections.EmptyMap(); } IDictionary result = new LinkedHashMap(); - for (int i = 0; i < Size(); i++) + for (int i = 0; i < Count; i++) { result.Put(keys[i], values[i]); } return result; } - - public override ISet> EntrySet() - { - return new SparseEdgeMap.EntrySet(this); - } - - private class EntrySet : AbstractEdgeMap.AbstractEntrySet - { - public override IEnumerator> GetEnumerator() - { - return new SparseEdgeMap.EntryIterator(this); - } - - internal EntrySet(SparseEdgeMap _enclosing) : base(_enclosing) - { - this._enclosing = _enclosing; - } - - private readonly SparseEdgeMap _enclosing; - } - - private class EntryIterator : IEnumerator> - { - private int current; - - public virtual bool HasNext() - { - return this.current < this._enclosing.Size(); - } - - public virtual KeyValuePair Next() - { - if (this.current >= this._enclosing.Size()) - { - throw new InvalidOperationException(); - } - this.current++; - return new _KeyValuePair_226(this); - } - - private sealed class _KeyValuePair_226 : KeyValuePair - { - public _KeyValuePair_226() - { - this.key = this._enclosing._enclosing.keys[this._enclosing.current - 1]; - this.value = this._enclosing._enclosing.values[this._enclosing.current - 1]; - } - - private readonly int key; - - private readonly T value; - - public int Key - { - get - { - return this.key; - } - } - - public T Value - { - get - { - return this.value; - } - } - - public T SetValue(T value) - { - throw new NotSupportedException("Not supported yet."); - } - } - - public virtual void Remove() - { - throw new NotSupportedException("Not supported yet."); - } - - internal EntryIterator(SparseEdgeMap _enclosing) - { - this._enclosing = _enclosing; - } - - private readonly SparseEdgeMap _enclosing; - } } } diff --git a/reference/antlr4 b/reference/antlr4 index 7e785daa6..b106c4363 160000 --- a/reference/antlr4 +++ b/reference/antlr4 @@ -1 +1 @@ -Subproject commit 7e785daa65e15f9786218e5dd63ea5a3cecf67a8 +Subproject commit b106c4363db4aa913887021c7c2cf245ba7a5fa3