diff options
author | Eric Maupin <ermau@mono-cvs.ximian.com> | 2009-11-12 18:04:44 +0300 |
---|---|---|
committer | Eric Maupin <ermau@mono-cvs.ximian.com> | 2009-11-12 18:04:44 +0300 |
commit | ee905d0c0340dc1cd60692d1bcee2dd3766bb05c (patch) | |
tree | d63054072e7bcb1abcaadb7dfb0a800829b18470 /mcs/class/System.Core/System.Linq/Lookup.cs | |
parent | f5628732eccfe965d221cae7617118ef8193df53 (diff) |
* System.Linq/Lookup.cs: Support null keys
* System.Linq/Enumerable.cs: Support null keys
svn path=/branches/mono-2-6/mcs/; revision=146040
Diffstat (limited to 'mcs/class/System.Core/System.Linq/Lookup.cs')
-rw-r--r-- | mcs/class/System.Core/System.Linq/Lookup.cs | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/mcs/class/System.Core/System.Linq/Lookup.cs b/mcs/class/System.Core/System.Linq/Lookup.cs index 16cac37fd97..116814feb72 100644 --- a/mcs/class/System.Core/System.Linq/Lookup.cs +++ b/mcs/class/System.Core/System.Linq/Lookup.cs @@ -5,6 +5,7 @@ // Alejandro Serrano "Serras" (trupill@yahoo.es) // Marek Safar <marek.safar@gmail.com> // Jb Evain <jbevain@novell.com> +// Eric Maupin <me@ermau.com> // // Copyright (C) 2007 Novell, Inc (http://www.novell.com) // @@ -37,48 +38,63 @@ namespace System.Linq { public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, ILookup<TKey, TElement> { + IGrouping<TKey, TElement> nullGrouping; Dictionary<TKey, IGrouping<TKey, TElement>> groups; public int Count { - get { return groups.Count; } + get { return (nullGrouping == null) ? groups.Count : groups.Count + 1; } } public IEnumerable<TElement> this [TKey key] { get { - IGrouping<TKey, TElement> group; - if (groups.TryGetValue (key, out group)) - return group; - + if (key == null && nullGrouping != null) + return nullGrouping; + else if (key != null) { + IGrouping<TKey, TElement> group; + if (groups.TryGetValue (key, out group)) + return group; + } + return new TElement [0]; } } - internal Lookup (Dictionary<TKey, List<TElement>> lookup) + internal Lookup (Dictionary<TKey, List<TElement>> lookup, IEnumerable<TElement> nullKeyElements) { groups = new Dictionary<TKey, IGrouping<TKey, TElement>> (lookup.Comparer); foreach (var slot in lookup) groups.Add (slot.Key, new Grouping<TKey, TElement> (slot.Key, slot.Value)); + + if (nullKeyElements != null) + nullGrouping = new Grouping<TKey, TElement> (default (TKey), nullKeyElements); } public IEnumerable<TResult> ApplyResultSelector<TResult> (Func<TKey, IEnumerable<TElement>, TResult> selector) { + if (nullGrouping != null) + yield return selector (nullGrouping.Key, nullGrouping); + foreach (var group in groups.Values) yield return selector (group.Key, group); } public bool Contains (TKey key) - { - return groups.ContainsKey (key); + { + return (key != null) ? groups.ContainsKey (key) : nullGrouping != null; } public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator () { - return groups.Values.GetEnumerator (); + if (nullGrouping != null) + yield return nullGrouping; + + foreach (var g in groups.Values) + yield return g; } IEnumerator IEnumerable.GetEnumerator () { - return groups.Values.GetEnumerator (); + return GetEnumerator (); } } } |