2009-12-03 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / class / System.Core / System.Linq / Lookup.cs
1 //
2 // Lookup<TKey, TElement>.cs
3 //
4 // Authors:
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>
9 //
10 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
11 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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.
30 //
31
32
33 using System;
34 using System.Collections;
35 using System.Collections.Generic;
36
37 namespace System.Linq {
38
39         public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, ILookup<TKey, TElement> {
40
41                 IGrouping<TKey, TElement> nullGrouping;
42                 Dictionary<TKey, IGrouping<TKey, TElement>> groups;
43
44                 public int Count {
45                         get { return (nullGrouping == null) ? groups.Count : groups.Count + 1; }
46                 }
47
48                 public IEnumerable<TElement> this [TKey key] {
49                         get {
50                                 if (key == null && nullGrouping != null)
51                                         return nullGrouping;
52                                 else if (key != null) {
53                                         IGrouping<TKey, TElement> group;
54                                         if (groups.TryGetValue (key, out group))
55                                                 return group;
56                                 }
57                                 
58                                 return new TElement [0];
59                         }
60                 }
61
62                 internal Lookup (Dictionary<TKey, List<TElement>> lookup, IEnumerable<TElement> nullKeyElements)
63                 {
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));
67                         
68                         if (nullKeyElements != null)
69                                 nullGrouping = new Grouping<TKey, TElement> (default (TKey), nullKeyElements);
70                 }
71
72                 public IEnumerable<TResult> ApplyResultSelector<TResult> (Func<TKey, IEnumerable<TElement>, TResult> selector)
73                 {
74                         if (nullGrouping != null)
75                                 yield return selector (nullGrouping.Key, nullGrouping);
76                         
77                         foreach (var group in groups.Values)
78                                 yield return selector (group.Key, group);
79                 }
80
81                 public bool Contains (TKey key)
82                 {       
83                         return (key != null) ? groups.ContainsKey (key) : nullGrouping != null; 
84                 }
85
86                 public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator ()
87                 {
88                         if (nullGrouping != null)
89                                 yield return nullGrouping;
90                         
91                         foreach (var g in groups.Values)
92                                 yield return g;
93                 }
94
95                 IEnumerator IEnumerable.GetEnumerator ()
96                 {
97                         return GetEnumerator ();
98                 }
99         }
100 }