Normalize line endings.
[mono.git] / mcs / class / System.Core / System.Linq / Internal / ConcurrentLookup.cs
1 #if NET_4_0
2 //
3 // ConcurrentLookup.cs
4 //
5 // Author:
6 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 //
8 // Copyright (c) 2010 Jérémie "Garuma" Laval
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27
28 using System;
29 using System.Threading;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
33
34 namespace System.Linq
35 {
36         internal class ConcurrentLookup<TKey, TElement> : ILookup<TKey, TElement>
37         {
38                 ConcurrentDictionary<TKey, IEnumerable<TElement>> dictionary;
39
40                 private class AddSlot
41                 {
42                         TElement element;
43
44                         internal AddSlot (TElement element)
45                         {
46                                 this.element = element;
47                         }
48
49                         internal IEnumerable<TElement> AddMethod (TKey key)
50                         {
51                                 List<TElement> list = new List<TElement> ();
52                                 list.Add (element);
53
54                                 return list;
55                         }
56
57                         internal IEnumerable<TElement> UpdateMethod (TKey key, IEnumerable<TElement> old)
58                         {
59                                 ICollection<TElement> coll = (ICollection<TElement>)old;
60                                 coll.Add (element);
61
62                                 return coll;
63                         }
64                 }
65
66                 internal ConcurrentLookup (IEqualityComparer<TKey> comparer)
67                 {
68                         this.dictionary = new ConcurrentDictionary<TKey, IEnumerable<TElement>> (comparer);
69                 }
70
71                 internal void Add (TKey key, TElement element)
72                 {
73                         AddSlot slot = new AddSlot (element);
74                         dictionary.AddOrUpdate (key, slot.AddMethod, slot.UpdateMethod);
75                 }
76
77                 public bool Contains (TKey key)
78                 {
79                         return dictionary.ContainsKey (key);
80                 }
81
82                 public IEnumerable<TElement> this[TKey key] {
83                         get {
84                                 return dictionary[key];
85                         }
86                 }
87
88                 public int Count {
89                         get {
90                                 return dictionary.Count;
91                         }
92                 }
93                 
94                 public IList<TKey> Keys {
95                         get {
96                                 return (IList<TKey>)dictionary.Keys;
97                         }
98                 }
99
100                 IEnumerator IEnumerable.GetEnumerator ()
101                 {
102                         return (IEnumerator)GetEnumeratorInternal ();
103                 }
104
105                 IEnumerator<IGrouping<TKey, TElement>> IEnumerable<IGrouping<TKey, TElement>>.GetEnumerator ()
106                 {
107                         return GetEnumeratorInternal ();
108                 }
109
110                 IEnumerator<IGrouping<TKey, TElement>> GetEnumeratorInternal ()
111                 {
112                         return dictionary.Select ((pair) => new ConcurrentGrouping<TKey, TElement> (pair.Key, pair.Value)).GetEnumerator ();
113                 }
114         }
115 }
116
117 #endif