Add Collections.EmptyList and EmptyMap

This commit is contained in:
Sam Harwell 2013-02-17 21:29:26 -06:00
parent 644db625da
commit 7becde5c7b
9 changed files with 142 additions and 11 deletions

View File

@ -147,6 +147,7 @@
<Compile Include="RuleContext.cs" />
<Compile Include="Sharpen\Arrays.cs" />
<Compile Include="Sharpen\AtomicReference`1.cs" />
<Compile Include="Sharpen\Collections.cs" />
<Compile Include="Sharpen\Runtime.cs" />
<Compile Include="TokenStreamRewriter.cs" />
<Compile Include="TokenTypes.cs" />

View File

@ -412,7 +412,7 @@ namespace Antlr4.Runtime.Atn
}
if (result == null)
{
return new PrecedencePredicate[0];
return Collections.EmptyList<PrecedencePredicate>();
}
return result;
}

View File

@ -162,7 +162,7 @@ namespace Antlr4.Runtime.Dfa
{
if (IsEmpty())
{
return Sharpen.Collections.EmptyMap();
return Sharpen.Collections.EmptyMap<int, T>();
}
IDictionary<int, T> result = new LinkedHashMap<int, T>();
for (int i = 0; i < arrayData.Length; i++)

View File

@ -223,7 +223,7 @@ namespace Antlr4.Runtime.Dfa
{
if (edges == null)
{
return Sharpen.Collections.EmptyMap();
return Sharpen.Collections.EmptyMap<int, DFAState>();
}
return edges.ToMap();
}
@ -264,7 +264,7 @@ namespace Antlr4.Runtime.Dfa
{
if (contextEdges == null)
{
return Sharpen.Collections.EmptyMap();
return Sharpen.Collections.EmptyMap<int, DFAState>();
}
IDictionary<int, DFAState> map = contextEdges.ToMap();
if (map.ContainsKey(-1))

View File

@ -145,7 +145,7 @@ namespace Antlr4.Runtime.Dfa
{
if (IsEmpty())
{
return Sharpen.Collections.EmptyMap();
return Sharpen.Collections.EmptyMap<int, T>();
}
return Sharpen.Collections.SingletonMap(key, value);
}

View File

@ -195,7 +195,7 @@ namespace Antlr4.Runtime.Dfa
{
if (IsEmpty())
{
return Sharpen.Collections.EmptyMap();
return Sharpen.Collections.EmptyMap<int, T>();
}
IDictionary<int, T> result = new LinkedHashMap<int, T>();
for (int i = 0; i < Size(); i++)

View File

@ -292,7 +292,7 @@ namespace Antlr4.Runtime
{
if (children == null)
{
return new ITerminalNode[0];
return Collections.EmptyList<ITerminalNode>();
}
IList<ITerminalNode> tokens = null;
foreach (IParseTree o in children)
@ -313,7 +313,7 @@ namespace Antlr4.Runtime
}
if (tokens == null)
{
return new ITerminalNode[0];
return Collections.EmptyList<ITerminalNode>();
}
return tokens;
}
@ -327,7 +327,7 @@ namespace Antlr4.Runtime
{
if (children == null)
{
return new T[0];
return Collections.EmptyList<T>();
}
IList<T> contexts = null;
foreach (IParseTree o in children)
@ -343,7 +343,7 @@ namespace Antlr4.Runtime
}
if (contexts == null)
{
return new T[0];
return Collections.EmptyList<T>();
}
return contexts;
}

View File

@ -0,0 +1,130 @@
namespace Sharpen
{
using System;
using System.Collections.Generic;
using IEnumerable = System.Collections.IEnumerable;
using IEnumerator = System.Collections.IEnumerator;
internal static class Collections
{
public static IList<T> EmptyList<T>()
{
return EmptyListImpl<T>.Instance;
}
public static IDictionary<TKey, TValue> EmptyMap<TKey, TValue>()
{
return EmptyMapImpl<TKey, TValue>.Instance;
}
private static class EmptyListImpl<T>
{
public static readonly IList<T> Instance = new T[0];
}
private class EmptyMapImpl<TKey, TValue> : IDictionary<TKey, TValue>
{
public static readonly EmptyMapImpl<TKey, TValue> Instance =
new EmptyMapImpl<TKey, TValue>();
public void Add(TKey key, TValue value)
{
throw new InvalidOperationException("This collection is read-only.");
}
public bool ContainsKey(TKey key)
{
return false;
}
public ICollection<TKey> Keys
{
get
{
return EmptyList<TKey>();
}
}
public bool Remove(TKey key)
{
throw new InvalidOperationException("This collection is read-only.");
}
public bool TryGetValue(TKey key, out TValue value)
{
value = default(TValue);
return false;
}
public ICollection<TValue> Values
{
get
{
return EmptyList<TValue>();
}
}
public TValue this[TKey key]
{
get
{
throw new System.NotImplementedException();
}
set
{
throw new InvalidOperationException("This collection is read-only.");
}
}
public void Add(KeyValuePair<TKey, TValue> item)
{
throw new InvalidOperationException("This collection is read-only.");
}
public void Clear()
{
throw new InvalidOperationException("This collection is read-only.");
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return false;
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
}
public int Count
{
get
{
return 0;
}
}
public bool IsReadOnly
{
get
{
return true;
}
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
throw new InvalidOperationException("This collection is read-only.");
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
yield break;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
}

View File

@ -159,7 +159,7 @@ namespace Antlr4.Runtime.Tree
{
if (t.Parent == null)
{
return new ITree[0];
return Collections.EmptyList<ITree>();
}
IList<ITree> ancestors = new List<ITree>();
t = t.Parent;