Replace SIZEOF_REGISTER with sizeof(mgreg_t) for consistency with sizeof(gpointer)
[mono.git] / mcs / class / corlib / System.Collections.Concurrent / OrderablePartitioner.cs
1 // 
2 // OrderablePartitioner.cs
3 //  
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2009 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 using System;
28 using System.Collections.Generic;
29
30 #if NET_4_0 || BOOTSTRAP_NET_4_0
31
32 namespace System.Collections.Concurrent
33 {
34         public abstract class OrderablePartitioner<TSource> : Partitioner<TSource>
35         {
36                 bool keysOrderedInEachPartition;
37                 bool keysOrderedAcrossPartitions;
38                 bool keysNormalized;
39                 
40                 protected OrderablePartitioner (bool keysOrderedInEachPartition,
41                                                 bool keysOrderedAcrossPartitions, 
42                                                 bool keysNormalized) : base ()
43                 {
44                         this.keysOrderedInEachPartition = keysOrderedInEachPartition;
45                         this.keysOrderedAcrossPartitions = keysOrderedAcrossPartitions;
46                         this.keysNormalized = keysNormalized;
47                 }
48                 
49                 public override IEnumerable<TSource> GetDynamicPartitions ()
50                 {
51                   foreach (KeyValuePair<long, TSource> item in GetOrderableDynamicPartitions ())
52                         yield return item.Value;
53                 }
54                 
55                 public override IList<IEnumerator<TSource>> GetPartitions (int partitionCount)
56                 {
57                         IEnumerator<TSource>[] temp = new IEnumerator<TSource>[partitionCount];
58                         IList<IEnumerator<KeyValuePair<long, TSource>>> enumerators
59                           = GetOrderablePartitions (partitionCount);
60                         
61                         for (int i = 0; i < enumerators.Count; i++)
62                                 temp[i] = new ProxyEnumerator (enumerators[i]);
63                         
64                         return temp;
65                 }
66
67                 
68                 IEnumerator<TSource> GetProxyEnumerator (IEnumerator<KeyValuePair<long, TSource>> enumerator)
69                 {
70                         while (enumerator.MoveNext ())
71                                 yield return enumerator.Current.Value;
72                 }
73                 
74                 public abstract IList<IEnumerator<KeyValuePair<long, TSource>>> GetOrderablePartitions(int partitionCount);
75                 
76                 public virtual IEnumerable<KeyValuePair<long, TSource>> GetOrderableDynamicPartitions()
77                 {
78                         if (!SupportsDynamicPartitions)
79                                 throw new NotSupportedException ();
80                         
81                         return null;
82                 }
83
84                 public bool KeysOrderedInEachPartition {
85                         get {
86                                 return keysOrderedInEachPartition;
87                         }
88                 }
89                 
90                 public bool KeysOrderedAcrossPartitions {
91                         get {
92                                 return keysOrderedAcrossPartitions;
93                         }
94                 }
95                 
96                 public bool KeysNormalized {
97                         get {
98                                 return keysNormalized;
99                         }
100                 }
101
102                 class ProxyEnumerator : IEnumerator<TSource>, IDisposable
103                 {
104                         IEnumerator<KeyValuePair<long, TSource>> internalEnumerator;
105
106                         internal ProxyEnumerator (IEnumerator<KeyValuePair<long, TSource>> enumerator)
107                         {
108                                 internalEnumerator = enumerator;
109                         }
110
111                         public void Dispose ()
112                         {
113                                 internalEnumerator.Dispose ();
114                         }
115
116                         public bool MoveNext ()
117                         {
118                                 if (!internalEnumerator.MoveNext ())
119                                         return false;
120
121                                 Current = internalEnumerator.Current.Value;
122
123                                 return true;
124                         }
125
126                         public void Reset ()
127                         {
128                                 internalEnumerator.Reset ();
129                         }
130
131                         object IEnumerator.Current {
132                                 get {
133                                         return Current;
134                                 }
135                         }
136
137                         public TSource Current {
138                                 get;
139                                 private set;
140                         }
141                 }
142         }
143 }
144 #endif