Use IDictionary instead of IReadOnlyDictionary before .NET 4.5

This commit is contained in:
Sam Harwell 2013-02-27 12:40:45 -06:00
parent d898160d63
commit 61c141e994
7 changed files with 61 additions and 2 deletions

View File

@ -103,7 +103,11 @@ namespace Antlr4.Runtime.Dfa
get; get;
} }
#if NET_4_5
public abstract IReadOnlyDictionary<int, T> ToMap(); public abstract IReadOnlyDictionary<int, T> ToMap();
#else
public abstract IDictionary<int, T> ToMap();
#endif
public virtual IEnumerator<KeyValuePair<int, T>> GetEnumerator() public virtual IEnumerator<KeyValuePair<int, T>> GetEnumerator()
{ {

View File

@ -168,7 +168,11 @@ namespace Antlr4.Runtime.Dfa
return this; return this;
} }
#if NET_4_5
public override IReadOnlyDictionary<int, T> ToMap() public override IReadOnlyDictionary<int, T> ToMap()
#else
public override IDictionary<int, T> ToMap()
#endif
{ {
if (IsEmpty) if (IsEmpty)
{ {
@ -183,7 +187,11 @@ namespace Antlr4.Runtime.Dfa
} }
result[i + minIndex] = arrayData[i]; result[i + minIndex] = arrayData[i];
} }
#if NET_4_5
return new ReadOnlyDictionary<int, T>(result); return new ReadOnlyDictionary<int, T>(result);
#else
return result;
#endif
} }
} }
} }

View File

@ -221,7 +221,11 @@ namespace Antlr4.Runtime.Dfa
} }
} }
#if NET_4_5
public virtual IReadOnlyDictionary<int, DFAState> EdgeMap public virtual IReadOnlyDictionary<int, DFAState> EdgeMap
#else
public virtual IDictionary<int, DFAState> EdgeMap
#endif
{ {
get get
{ {
@ -265,7 +269,11 @@ namespace Antlr4.Runtime.Dfa
} }
} }
#if NET_4_5
public virtual IReadOnlyDictionary<int, DFAState> ContextEdgeMap public virtual IReadOnlyDictionary<int, DFAState> ContextEdgeMap
#else
public virtual IDictionary<int, DFAState> ContextEdgeMap
#endif
{ {
get get
{ {
@ -273,7 +281,7 @@ namespace Antlr4.Runtime.Dfa
{ {
return Sharpen.Collections.EmptyMap<int, DFAState>(); return Sharpen.Collections.EmptyMap<int, DFAState>();
} }
IReadOnlyDictionary<int, DFAState> map = contextEdges.ToMap(); var map = contextEdges.ToMap();
if (map.ContainsKey(-1)) if (map.ContainsKey(-1))
{ {
if (map.Count == 1) if (map.Count == 1)
@ -285,7 +293,11 @@ namespace Antlr4.Runtime.Dfa
Dictionary<int, DFAState> result = map.ToDictionary(i => i.Key, i => i.Value); Dictionary<int, DFAState> result = map.ToDictionary(i => i.Key, i => i.Value);
result.Add(PredictionContext.EmptyFullStateKey, result[-1]); result.Add(PredictionContext.EmptyFullStateKey, result[-1]);
result.Remove(-1); result.Remove(-1);
#if NET_4_5
map = new ReadOnlyDictionary<int, DFAState>(new SortedDictionary<int, DFAState>(result)); map = new ReadOnlyDictionary<int, DFAState>(new SortedDictionary<int, DFAState>(result));
#else
map = new SortedDictionary<int, DFAState>(result);
#endif
} }
} }
return map; return map;

View File

@ -66,7 +66,12 @@ namespace Antlr4.Runtime.Dfa
[return: NotNull] [return: NotNull]
IEdgeMap<T> Clear(); IEdgeMap<T> Clear();
#if NET_4_5
[return: NotNull] [return: NotNull]
IReadOnlyDictionary<int, T> ToMap(); IReadOnlyDictionary<int, T> ToMap();
#else
[return: NotNull]
IDictionary<int, T> ToMap();
#endif
} }
} }

View File

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

View File

@ -201,7 +201,11 @@ namespace Antlr4.Runtime.Dfa
return result; return result;
} }
#if NET_4_5
public override IReadOnlyDictionary<int, T> ToMap() public override IReadOnlyDictionary<int, T> ToMap()
#else
public override IDictionary<int, T> ToMap()
#endif
{ {
if (IsEmpty) if (IsEmpty)
{ {
@ -212,7 +216,11 @@ namespace Antlr4.Runtime.Dfa
{ {
result[keys[i]] = values[i]; result[keys[i]] = values[i];
} }
#if NET_4_5
return new ReadOnlyDictionary<int, T>(result); return new ReadOnlyDictionary<int, T>(result);
#else
return result;
#endif
} }
} }
} }

View File

@ -39,7 +39,11 @@ namespace Sharpen
return EmptyListImpl<T>.Instance; return EmptyListImpl<T>.Instance;
} }
#if NET_4_5
public static ReadOnlyDictionary<TKey, TValue> EmptyMap<TKey, TValue>() public static ReadOnlyDictionary<TKey, TValue> EmptyMap<TKey, TValue>()
#else
public static IDictionary<TKey, TValue> EmptyMap<TKey, TValue>()
#endif
{ {
return EmptyMapImpl<TKey, TValue>.Instance; return EmptyMapImpl<TKey, TValue>.Instance;
} }
@ -49,9 +53,13 @@ namespace Sharpen
return new ReadOnlyCollection<T>(new T[] { item }); return new ReadOnlyCollection<T>(new T[] { item });
} }
#if NET_4_5
public static ReadOnlyDictionary<TKey, TValue> SingletonMap<TKey, TValue>(TKey key, TValue value) public static ReadOnlyDictionary<TKey, TValue> SingletonMap<TKey, TValue>(TKey key, TValue value)
#else
public static IDictionary<TKey, TValue> SingletonMap<TKey, TValue>(TKey key, TValue value)
#endif
{ {
return new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue> { { key, value } }); return new Dictionary<TKey, TValue> { { key, value } };
} }
private static class EmptyListImpl<T> private static class EmptyListImpl<T>
@ -61,8 +69,18 @@ namespace Sharpen
private static class EmptyMapImpl<TKey, TValue> private static class EmptyMapImpl<TKey, TValue>
{ {
#if NET_4_5
public static readonly ReadOnlyDictionary<TKey, TValue> Instance = public static readonly ReadOnlyDictionary<TKey, TValue> Instance =
new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>()); new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>());
#else
public static IDictionary<TKey, TValue> Instance
{
get
{
return new Dictionary<TKey, TValue>();
}
}
#endif
} }
} }
} }