2 // Lookup<TKey, TElement>.cs
5 // Alejandro Serrano "Serras" (trupill@yahoo.es)
6 // Marek Safar <marek.safar@gmail.com>
7 // Jb Evain <jbevain@novell.com>
8 // Eric Maupin <me@ermau.com>
10 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Collections.Generic;
37 namespace System.Linq {
39 public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, ILookup<TKey, TElement> {
41 IGrouping<TKey, TElement> nullGrouping;
42 Dictionary<TKey, IGrouping<TKey, TElement>> groups;
45 get { return (nullGrouping == null) ? groups.Count : groups.Count + 1; }
48 public IEnumerable<TElement> this [TKey key] {
50 if (key == null && nullGrouping != null)
52 else if (key != null) {
53 IGrouping<TKey, TElement> group;
54 if (groups.TryGetValue (key, out group))
58 return new TElement [0];
62 internal Lookup (Dictionary<TKey, List<TElement>> lookup, IEnumerable<TElement> nullKeyElements)
64 groups = new Dictionary<TKey, IGrouping<TKey, TElement>> (lookup.Comparer);
65 foreach (var slot in lookup)
66 groups.Add (slot.Key, new Grouping<TKey, TElement> (slot.Key, slot.Value));
68 if (nullKeyElements != null)
69 nullGrouping = new Grouping<TKey, TElement> (default (TKey), nullKeyElements);
72 public IEnumerable<TResult> ApplyResultSelector<TResult> (Func<TKey, IEnumerable<TElement>, TResult> selector)
74 if (nullGrouping != null)
75 yield return selector (nullGrouping.Key, nullGrouping);
77 foreach (var group in groups.Values)
78 yield return selector (group.Key, group);
81 public bool Contains (TKey key)
83 return (key != null) ? groups.ContainsKey (key) : nullGrouping != null;
86 public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator ()
88 if (nullGrouping != null)
89 yield return nullGrouping;
91 foreach (var g in groups.Values)
95 IEnumerator IEnumerable.GetEnumerator ()
97 return GetEnumerator ();