Fix DFA edge maps

This commit is contained in:
Sam Harwell 2013-02-19 18:47:03 -06:00
parent 4eb4194f5f
commit 00b8db4e2b
6 changed files with 48 additions and 28 deletions

View File

@ -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<T> Put(int key, T value);
public virtual Antlr4.Runtime.Dfa.AbstractEdgeMap<T> PutAll<_T0>(IEdgeMap<_T0> m)
where _T0:T
IEdgeMap<T> IEdgeMap<T>.Put(int key, T value)
{
return Put(key, value);
}
public virtual Antlr4.Runtime.Dfa.AbstractEdgeMap<T> PutAll(IEdgeMap<T> m)
{
Antlr4.Runtime.Dfa.AbstractEdgeMap<T> result = this;
foreach (KeyValuePair<int, _T0> entry in m.EntrySet())
foreach (KeyValuePair<int, T> entry in m)
{
result = result.Put(entry.Key, entry.Value);
}
return result;
}
IEdgeMap<T> IEdgeMap<T>.PutAll(IEdgeMap<T> m)
{
return PutAll(m);
}
public abstract Antlr4.Runtime.Dfa.AbstractEdgeMap<T> Clear();
IEdgeMap<T> IEdgeMap<T>.Clear()
{
return Clear();
}
public abstract Antlr4.Runtime.Dfa.AbstractEdgeMap<T> Remove(int key);
IEdgeMap<T> IEdgeMap<T>.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<int, T> ToMap();
public abstract IReadOnlyDictionary<int, T> ToMap();
public virtual IEnumerator<KeyValuePair<int, T>> GetEnumerator()
{

View File

@ -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<T>)Put(key, null));
}
public override AbstractEdgeMap<T> PutAll<_T0>(IEdgeMap<_T0> m)
public override AbstractEdgeMap<T> PutAll(IEdgeMap<T> m)
{
if (m.IsEmpty)
{
@ -167,7 +168,7 @@ namespace Antlr4.Runtime.Dfa
return this;
}
public override IDictionary<int, T> ToMap()
public override IReadOnlyDictionary<int, T> ToMap()
{
if (IsEmpty)
{
@ -182,7 +183,7 @@ namespace Antlr4.Runtime.Dfa
}
result[i + minIndex] = arrayData[i];
}
return result;
return new ReadOnlyDictionary<int, T>(result);
}
}
}

View File

@ -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<int, DFAState> EdgeMap
public virtual IReadOnlyDictionary<int, DFAState> EdgeMap
{
get
{
@ -257,7 +259,7 @@ namespace Antlr4.Runtime.Dfa
}
}
public virtual IDictionary<int, DFAState> ContextEdgeMap
public virtual IReadOnlyDictionary<int, DFAState> ContextEdgeMap
{
get
{
@ -265,26 +267,19 @@ namespace Antlr4.Runtime.Dfa
{
return Sharpen.Collections.EmptyMap<int, DFAState>();
}
IDictionary<int, DFAState> map = contextEdges.ToMap();
IReadOnlyDictionary<int, DFAState> 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<int, DFAState>(map);
map.Put(PredictionContext.EmptyFullStateKey, Sharpen.Collections.Remove(map, -1));
}
Dictionary<int, DFAState> result = map.ToDictionary(i => i.Key, i => i.Value);
result.Add(PredictionContext.EmptyFullStateKey, result[-1]);
result.Remove(-1);
map = new ReadOnlyDictionary<int, DFAState>(new SortedDictionary<int, DFAState>(result));
}
}
return map;

View File

@ -61,12 +61,12 @@ namespace Antlr4.Runtime.Dfa
IEdgeMap<T> Remove(int key);
[return: NotNull]
IEdgeMap<T> PutAll<_T0>(IEdgeMap<_T0> m) where _T0:T;
IEdgeMap<T> PutAll(IEdgeMap<T> m);
[return: NotNull]
IEdgeMap<T> Clear();
[return: NotNull]
IDictionary<int, T> ToMap();
IReadOnlyDictionary<int, T> ToMap();
}
}

View File

@ -149,7 +149,7 @@ namespace Antlr4.Runtime.Dfa
return this;
}
public override IDictionary<int, T> ToMap()
public override IReadOnlyDictionary<int, T> ToMap()
{
if (IsEmpty)
{

View File

@ -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<int, T> ToMap()
public override IReadOnlyDictionary<int, T> ToMap()
{
if (IsEmpty)
{
@ -211,7 +212,7 @@ namespace Antlr4.Runtime.Dfa
{
result[keys[i]] = values[i];
}
return result;
return new ReadOnlyDictionary<int, T>(result);
}
}
}