do not check order sequence if option /order was not used
[mono.git] / mcs / class / System.Core / System.Linq.Parallel / ConcurrentLookup.cs
1 //
2 // ConcurrentLookup.cs
3 //
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 //
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 #if NET_4_0
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.Parallel
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