Skip to content

Commit 39ce9a1

Browse files
committed
Initial Commit
1 parent b10d461 commit 39ce9a1

File tree

67 files changed

+12948
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+12948
-0
lines changed

Properties/AssemblyInfo.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// AssemblyInfo.cs
3+
//
4+
// Author:
5+
// Jim Borden <jim.borden@couchbase.com>
6+
//
7+
// Copyright (c) 2015 Couchbase, Inc All rights reserved.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// http://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
//
21+
using System.Reflection;
22+
using System.Runtime.CompilerServices;
23+
24+
// Information about this assembly is defined by the following attributes.
25+
// Change them to the values specific to your project.
26+
27+
[assembly: AssemblyTitle("System.Threading.Tasks.Net35")]
28+
[assembly: AssemblyDescription("")]
29+
[assembly: AssemblyConfiguration("")]
30+
[assembly: AssemblyCompany("Couchbase, Inc")]
31+
[assembly: AssemblyProduct("")]
32+
[assembly: AssemblyCopyright("Couchbase, Inc")]
33+
[assembly: AssemblyTrademark("")]
34+
[assembly: AssemblyCulture("")]
35+
36+
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
37+
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
38+
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
39+
40+
[assembly: AssemblyVersion("1.0.*")]
41+
42+
// The following attributes are used to specify the signing key for the assembly,
43+
// if desired. See the Mono documentation for more information about signing.
44+
45+
//[assembly: AssemblyDelaySign(false)]
46+
//[assembly: AssemblyKeyFile("")]
47+
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
//
2+
// EnumerablePartitioner.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+
#if NET_4_0
28+
29+
using System;
30+
using System.Threading;
31+
using System.Threading.Tasks;
32+
using System.Collections.Generic;
33+
34+
namespace System.Collections.Concurrent.Partitioners
35+
{
36+
// Represent a chunk partitioner
37+
internal class EnumerablePartitioner<T> : OrderablePartitioner<T>
38+
{
39+
IEnumerable<T> source;
40+
41+
const int InitialPartitionSize = 1;
42+
const int PartitionMultiplier = 2;
43+
44+
int initialPartitionSize;
45+
int partitionMultiplier;
46+
47+
public EnumerablePartitioner (IEnumerable<T> source)
48+
: this (source, InitialPartitionSize, PartitionMultiplier)
49+
{
50+
51+
}
52+
53+
// This is used to get striped partitionning (for Take and Skip for instance
54+
public EnumerablePartitioner (IEnumerable<T> source, int initialPartitionSize, int partitionMultiplier)
55+
: base (true, false, true)
56+
{
57+
this.source = source;
58+
this.initialPartitionSize = initialPartitionSize;
59+
this.partitionMultiplier = partitionMultiplier;
60+
}
61+
62+
public override IList<IEnumerator<KeyValuePair<long, T>>> GetOrderablePartitions (int partitionCount)
63+
{
64+
if (partitionCount <= 0)
65+
throw new ArgumentOutOfRangeException ("partitionCount");
66+
67+
IEnumerator<KeyValuePair<long, T>>[] enumerators
68+
= new IEnumerator<KeyValuePair<long, T>>[partitionCount];
69+
70+
PartitionerState state = new PartitionerState ();
71+
IEnumerator<T> src = source.GetEnumerator ();
72+
bool isSimple = initialPartitionSize == 1 && partitionMultiplier == 1;
73+
74+
for (int i = 0; i < enumerators.Length; i++) {
75+
enumerators[i] = isSimple ? GetPartitionEnumeratorSimple (src, state, i == enumerators.Length - 1) : GetPartitionEnumerator (src, state);
76+
}
77+
78+
return enumerators;
79+
}
80+
81+
// This partitioner that is simpler than the general case (don't use a list) is called in the case
82+
// of initialPartitionSize == partitionMultiplier == 1
83+
IEnumerator<KeyValuePair<long, T>> GetPartitionEnumeratorSimple (IEnumerator<T> src,
84+
PartitionerState state,
85+
bool last)
86+
{
87+
long index = -1;
88+
var value = default (T);
89+
90+
try {
91+
do {
92+
lock (state.SyncLock) {
93+
if (state.Finished)
94+
break;
95+
if (state.Finished = !src.MoveNext ())
96+
break;
97+
98+
index = state.Index++;
99+
value = src.Current;
100+
}
101+
102+
yield return new KeyValuePair<long, T> (index, value);
103+
} while (!state.Finished);
104+
} finally {
105+
if (last)
106+
src.Dispose ();
107+
}
108+
}
109+
110+
IEnumerator<KeyValuePair<long, T>> GetPartitionEnumerator (IEnumerator<T> src, PartitionerState state)
111+
{
112+
int count = initialPartitionSize;
113+
List<T> list = new List<T> ();
114+
115+
while (!state.Finished) {
116+
list.Clear ();
117+
long ind = -1;
118+
119+
lock (state.SyncLock) {
120+
if (state.Finished)
121+
break;
122+
123+
ind = state.Index;
124+
125+
for (int i = 0; i < count; i++) {
126+
if (state.Finished = !src.MoveNext ()) {
127+
if (list.Count == 0)
128+
yield break;
129+
else
130+
break;
131+
}
132+
133+
list.Add (src.Current);
134+
state.Index++;
135+
}
136+
}
137+
138+
for (int i = 0; i < list.Count; i++)
139+
yield return new KeyValuePair<long, T> (ind + i, list[i]);
140+
141+
count *= partitionMultiplier;
142+
}
143+
}
144+
145+
class PartitionerState
146+
{
147+
public bool Finished;
148+
public long Index = 0;
149+
public readonly object SyncLock = new object ();
150+
}
151+
}
152+
}
153+
#endif
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//
2+
// ListPartitioner.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+
#if NET_4_0
28+
29+
using System;
30+
using System.Threading;
31+
using System.Collections.Generic;
32+
using System.Runtime.InteropServices;
33+
34+
namespace System.Collections.Concurrent.Partitioners
35+
{
36+
// Represent a Range partitioner
37+
internal class ListPartitioner<T> : OrderablePartitioner<T>
38+
{
39+
IList<T> source;
40+
41+
public ListPartitioner (IList<T> source) : base (true, true, true)
42+
{
43+
this.source = source;
44+
}
45+
46+
public override IList<IEnumerator<KeyValuePair<long, T>>> GetOrderablePartitions (int partitionCount)
47+
{
48+
if (partitionCount <= 0)
49+
throw new ArgumentOutOfRangeException ("partitionCount");
50+
51+
IEnumerator<KeyValuePair<long, T>>[] enumerators
52+
= new IEnumerator<KeyValuePair<long, T>>[partitionCount];
53+
54+
int count = source.Count / partitionCount;
55+
int extra = 0;
56+
57+
if (source.Count < partitionCount) {
58+
count = 1;
59+
} else {
60+
extra = source.Count % partitionCount;
61+
if (extra > 0)
62+
++count;
63+
}
64+
65+
int currentIndex = 0;
66+
67+
Range[] ranges = new Range[enumerators.Length];
68+
for (int i = 0; i < ranges.Length; i++) {
69+
ranges[i] = new Range (currentIndex,
70+
currentIndex + count);
71+
currentIndex += count;
72+
if (--extra == 0)
73+
--count;
74+
}
75+
76+
for (int i = 0; i < enumerators.Length; i++) {
77+
enumerators[i] = GetEnumeratorForRange (ranges, i);
78+
}
79+
80+
return enumerators;
81+
}
82+
83+
class Range
84+
{
85+
public int Actual;
86+
public readonly int LastIndex;
87+
88+
public Range (int frm, int lastIndex)
89+
{
90+
Actual = frm;
91+
LastIndex = lastIndex;
92+
}
93+
}
94+
95+
IEnumerator<KeyValuePair<long, T>> GetEnumeratorForRange (Range[] ranges, int workerIndex)
96+
{
97+
if (ranges[workerIndex].Actual >= source.Count)
98+
return GetEmpty ();
99+
100+
return GetEnumeratorForRangeInternal (ranges, workerIndex);
101+
}
102+
103+
IEnumerator<KeyValuePair<long, T>> GetEmpty ()
104+
{
105+
yield break;
106+
}
107+
108+
IEnumerator<KeyValuePair<long, T>> GetEnumeratorForRangeInternal (Range[] ranges, int workerIndex)
109+
{
110+
Range range = ranges[workerIndex];
111+
int lastIndex = range.LastIndex;
112+
int index = range.Actual;
113+
114+
for (int i = index; i < lastIndex; i = ++range.Actual) {
115+
yield return new KeyValuePair<long, T> (i, source[i]);
116+
}
117+
}
118+
}
119+
}
120+
#endif

0 commit comments

Comments
 (0)