Sharpen annotations on ATNConfigSet and EdgeMap+derived

This commit is contained in:
Sam Harwell 2013-02-19 14:32:53 -06:00
parent b0a106eaa4
commit 8ec18a03b8
8 changed files with 89 additions and 397 deletions

View File

@ -664,45 +664,5 @@ namespace Antlr4.Runtime.Atn
throw new InvalidOperationException("This ATNConfigSet is read only.");
}
}
private sealed class ATNConfigSetIterator : IEnumerator<ATNConfig>
{
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;
}
}
}

View File

@ -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<T> Remove(int key);
protected internal abstract class AbstractEntrySet : AbstractSet<KeyValuePair<int
, T>>
{
public override bool Contains(object o)
{
if (!(o is DictionaryEntry))
{
return false;
}
KeyValuePair<object, object> entry = (KeyValuePair<object, object>)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<T> _enclosing)
{
this._enclosing = _enclosing;
}
private readonly AbstractEdgeMap<T> _enclosing;
}
public abstract bool ContainsKey(int arg1);
public abstract ISet<KeyValuePair<int, T>> 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<int, T> ToMap();
}

View File

@ -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<T> Put(int key, T value)
@ -98,7 +107,7 @@ namespace Antlr4.Runtime.Dfa
public override AbstractEdgeMap<T> 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<object>)
{
SingletonEdgeMap<T> other = (SingletonEdgeMap<T>)m;
System.Diagnostics.Debug.Assert(!other.IsEmpty());
System.Diagnostics.Debug.Assert(!other.IsEmpty);
return ((Antlr4.Runtime.Dfa.ArrayEdgeMap<T>)Put(other.GetKey(), other.GetValue())
);
}
@ -159,7 +168,7 @@ namespace Antlr4.Runtime.Dfa
public override IDictionary<int, T> ToMap()
{
if (IsEmpty())
if (IsEmpty)
{
return Sharpen.Collections.EmptyMap();
}
@ -174,99 +183,5 @@ namespace Antlr4.Runtime.Dfa
}
return result;
}
public override ISet<KeyValuePair<int, T>> EntrySet()
{
return new ArrayEdgeMap.EntrySet(this);
}
private class EntrySet : AbstractEdgeMap.AbstractEntrySet
{
public override IEnumerator<KeyValuePair<int, T>> GetEnumerator()
{
return new ArrayEdgeMap.EntryIterator(this);
}
internal EntrySet(ArrayEdgeMap<T> _enclosing) : base(_enclosing)
{
this._enclosing = _enclosing;
}
private readonly ArrayEdgeMap<T> _enclosing;
}
private class EntryIterator : IEnumerator<KeyValuePair<int, T>>
{
private int current;
private int currentIndex;
public virtual bool HasNext()
{
return this.current < this._enclosing.Size();
}
public virtual KeyValuePair<int, T> 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<int, T>
{
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<T> _enclosing)
{
this._enclosing = _enclosing;
}
private readonly ArrayEdgeMap<T> _enclosing;
}
}
}

View File

@ -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)

View File

@ -37,14 +37,22 @@ namespace Antlr4.Runtime.Dfa
/// <author>Sam Harwell</author>
public interface IEdgeMap<T>
{
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<T> Put(int key, T value);
@ -60,8 +68,5 @@ namespace Antlr4.Runtime.Dfa
[NotNull]
IDictionary<int, T> ToMap();
[NotNull]
ISet<KeyValuePair<int, T>> EntrySet();
}
}

View File

@ -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<T> Put(int key, T value)
@ -142,97 +150,11 @@ namespace Antlr4.Runtime.Dfa
public override IDictionary<int, T> ToMap()
{
if (IsEmpty())
if (IsEmpty)
{
return Sharpen.Collections.EmptyMap();
}
return Sharpen.Collections.SingletonMap(key, value);
}
public override ISet<KeyValuePair<int, T>> EntrySet()
{
return new SingletonEdgeMap.EntrySet(this);
}
private class EntrySet : AbstractEdgeMap.AbstractEntrySet
{
public override IEnumerator<KeyValuePair<int, T>> GetEnumerator()
{
return new SingletonEdgeMap.EntryIterator(this);
}
internal EntrySet(SingletonEdgeMap<T> _enclosing) : base(_enclosing)
{
this._enclosing = _enclosing;
}
private readonly SingletonEdgeMap<T> _enclosing;
}
private class EntryIterator : IEnumerator<KeyValuePair<int, T>>
{
private int current;
public virtual bool HasNext()
{
return this.current < this._enclosing.Size();
}
public virtual KeyValuePair<int, T> Next()
{
if (this.current >= this._enclosing.Size())
{
throw new InvalidOperationException();
}
this.current++;
return new _KeyValuePair_166(this);
}
private sealed class _KeyValuePair_166 : KeyValuePair<int, T>
{
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<T> _enclosing)
{
this._enclosing = _enclosing;
}
private readonly SingletonEdgeMap<T> _enclosing;
}
}
}

View File

@ -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<T> 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<T> 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<T> result = new Antlr4.Runtime.Dfa.SparseEdgeMap
<T>(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<T> Clear()
{
if (IsEmpty())
if (IsEmpty)
{
return this;
}
@ -192,102 +201,16 @@ namespace Antlr4.Runtime.Dfa
public override IDictionary<int, T> ToMap()
{
if (IsEmpty())
if (IsEmpty)
{
return Sharpen.Collections.EmptyMap();
}
IDictionary<int, T> result = new LinkedHashMap<int, T>();
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<KeyValuePair<int, T>> EntrySet()
{
return new SparseEdgeMap.EntrySet(this);
}
private class EntrySet : AbstractEdgeMap.AbstractEntrySet
{
public override IEnumerator<KeyValuePair<int, T>> GetEnumerator()
{
return new SparseEdgeMap.EntryIterator(this);
}
internal EntrySet(SparseEdgeMap<T> _enclosing) : base(_enclosing)
{
this._enclosing = _enclosing;
}
private readonly SparseEdgeMap<T> _enclosing;
}
private class EntryIterator : IEnumerator<KeyValuePair<int, T>>
{
private int current;
public virtual bool HasNext()
{
return this.current < this._enclosing.Size();
}
public virtual KeyValuePair<int, T> Next()
{
if (this.current >= this._enclosing.Size())
{
throw new InvalidOperationException();
}
this.current++;
return new _KeyValuePair_226(this);
}
private sealed class _KeyValuePair_226 : KeyValuePair<int, T>
{
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<T> _enclosing)
{
this._enclosing = _enclosing;
}
private readonly SparseEdgeMap<T> _enclosing;
}
}
}

@ -1 +1 @@
Subproject commit 7e785daa65e15f9786218e5dd63ea5a3cecf67a8
Subproject commit b106c4363db4aa913887021c7c2cf245ba7a5fa3