Skip to content

Commit e310bb8

Browse files
authored
Created new projects for the samples to better isolate the code (#937)
1 parent fb2be94 commit e310bb8

File tree

4 files changed

+125
-128
lines changed

4 files changed

+125
-128
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
namespace ProgramOrderableListPartitioner
2+
{
3+
//<snippetOrderableListPartitioner>
4+
//
5+
// An orderable dynamic partitioner for lists
6+
//
7+
using System;
8+
using System.Collections;
9+
using System.Collections.Concurrent;
10+
using System.Collections.Generic;
11+
using System.Diagnostics;
12+
using System.Linq;
13+
using System.Text;
14+
using System.Threading;
15+
using System.Threading.Tasks;
16+
using System.Xml.Linq;
17+
using System.Numerics;
18+
19+
class OrderableListPartitioner<TSource> : OrderablePartitioner<TSource>
20+
{
21+
private readonly IList<TSource> m_input;
22+
23+
// Must override to return true.
24+
public override bool SupportsDynamicPartitions => true;
25+
26+
public OrderableListPartitioner(IList<TSource> input) : base(true, false, true) =>
27+
m_input = input;
28+
29+
public override IList<IEnumerator<KeyValuePair<long, TSource>>> GetOrderablePartitions(int partitionCount)
30+
{
31+
var dynamicPartitions = GetOrderableDynamicPartitions();
32+
var partitions =
33+
new IEnumerator<KeyValuePair<long, TSource>>[partitionCount];
34+
35+
for (int i = 0; i < partitionCount; i++)
36+
{
37+
partitions[i] = dynamicPartitions.GetEnumerator();
38+
}
39+
return partitions;
40+
}
41+
42+
public override IEnumerable<KeyValuePair<long, TSource>> GetOrderableDynamicPartitions() =>
43+
new ListDynamicPartitions(m_input);
44+
45+
private class ListDynamicPartitions : IEnumerable<KeyValuePair<long, TSource>>
46+
{
47+
private IList<TSource> m_input;
48+
private int m_pos = 0;
49+
50+
internal ListDynamicPartitions(IList<TSource> input) =>
51+
m_input = input;
52+
53+
public IEnumerator<KeyValuePair<long, TSource>> GetEnumerator()
54+
{
55+
while (true)
56+
{
57+
// Each task gets the next item in the list. The index is
58+
// incremented in a thread-safe manner to avoid races.
59+
int elemIndex = Interlocked.Increment(ref m_pos) - 1;
60+
61+
if (elemIndex >= m_input.Count)
62+
{
63+
yield break;
64+
}
65+
66+
yield return new KeyValuePair<long, TSource>(
67+
elemIndex, m_input[elemIndex]);
68+
}
69+
}
70+
71+
IEnumerator IEnumerable.GetEnumerator() =>
72+
((IEnumerable<KeyValuePair<long, TSource>>)this).GetEnumerator();
73+
}
74+
}
75+
76+
class ConsumerClass
77+
{
78+
static void Main()
79+
{
80+
var nums = Enumerable.Range(0, 10000).ToArray();
81+
OrderableListPartitioner<int> partitioner = new OrderableListPartitioner<int>(nums);
82+
83+
// Use with Parallel.ForEach
84+
Parallel.ForEach(partitioner, (i) => Console.WriteLine(i));
85+
86+
87+
// Use with PLINQ
88+
var query = from num in partitioner.AsParallel()
89+
where num % 2 == 0
90+
select num;
91+
92+
foreach (var v in query)
93+
Console.WriteLine(v);
94+
}
95+
}
96+
//</snippetOrderableListPartitioner>
97+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Remove="*.cs" />
10+
<Compile Include="partitioner02.cs" />
11+
</ItemGroup>
12+
13+
</Project>

snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioners.cs

Lines changed: 2 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Program
1717
static void Main(string[] args)
1818
{
1919
Consumer.Main2();
20-
TestOrderableListPartitioner();
2120
// TestDefaultRangePartitioner();
2221
//TestLoadBalancingCreateMethods();
2322
// ParallelLoopsWithPartitioner();
@@ -33,26 +32,6 @@ static void Main(string[] args)
3332
}
3433

3534

36-
static void TestOrderableListPartitioner()
37-
{
38-
var nums = Enumerable.Range(0, 10000).ToArray();
39-
OrderableListPartitioner<int> part = new OrderableListPartitioner<int>(nums);
40-
41-
// Use with Parallel.ForEach
42-
Parallel.ForEach(part, (i) => Console.WriteLine(i));
43-
44-
45-
// Use with PLINQ
46-
var query = from num in part.AsParallel()
47-
where num % 2 == 0
48-
select num;
49-
50-
foreach(var v in query)
51-
Console.WriteLine(v);
52-
53-
}
54-
55-
5635
static void TestDefaultRangePartitioner()
5736
{
5837

@@ -148,117 +127,12 @@ static void TestLoadBalancingCreateMethods()
148127
}
149128
}
150129

151-
//<snippet04>
152-
//
153-
// An orderable dynamic partitioner for lists
154-
//
155-
class OrderableListPartitioner<TSource> : OrderablePartitioner<TSource>
156-
{
157-
private readonly IList<TSource> m_input;
158-
159-
public OrderableListPartitioner(IList<TSource> input)
160-
: base(true, false, true)
161-
{
162-
m_input = input;
163-
}
164-
165-
// Must override to return true.
166-
public override bool SupportsDynamicPartitions
167-
{
168-
get
169-
{
170-
return true;
171-
}
172-
}
173-
174-
public override IList<IEnumerator<KeyValuePair<long, TSource>>>
175-
GetOrderablePartitions(int partitionCount)
176-
{
177-
var dynamicPartitions = GetOrderableDynamicPartitions();
178-
var partitions =
179-
new IEnumerator<KeyValuePair<long, TSource>>[partitionCount];
180-
181-
for (int i = 0; i < partitionCount; i++)
182-
{
183-
partitions[i] = dynamicPartitions.GetEnumerator();
184-
}
185-
return partitions;
186-
}
187-
188-
public override IEnumerable<KeyValuePair<long, TSource>>
189-
GetOrderableDynamicPartitions()
190-
{
191-
return new ListDynamicPartitions(m_input);
192-
}
193-
194-
private class ListDynamicPartitions
195-
: IEnumerable<KeyValuePair<long, TSource>>
196-
{
197-
private IList<TSource> m_input;
198-
private int m_pos = 0;
199-
200-
internal ListDynamicPartitions(IList<TSource> input)
201-
{
202-
m_input = input;
203-
}
204-
205-
public IEnumerator<KeyValuePair<long, TSource>> GetEnumerator()
206-
{
207-
while (true)
208-
{
209-
// Each task gets the next item in the list. The index is
210-
// incremented in a thread-safe manner to avoid races.
211-
int elemIndex = Interlocked.Increment(ref m_pos) - 1;
212-
213-
if (elemIndex >= m_input.Count)
214-
{
215-
yield break;
216-
}
217-
218-
yield return new KeyValuePair<long, TSource>(
219-
elemIndex, m_input[elemIndex]);
220-
}
221-
}
222-
223-
IEnumerator IEnumerable.GetEnumerator()
224-
{
225-
return
226-
((IEnumerable<KeyValuePair<long, TSource>>)this)
227-
.GetEnumerator();
228-
}
229-
}
230-
}
231-
232-
class ConsumerClass
233-
{
234-
static void Main()
235-
{
236-
var nums = Enumerable.Range(0, 10000).ToArray();
237-
OrderableListPartitioner<int> partitioner = new OrderableListPartitioner<int>(nums);
238-
239-
// Use with Parallel.ForEach
240-
Parallel.ForEach(partitioner, (i) => Console.WriteLine(i));
241-
242-
243-
// Use with PLINQ
244-
var query = from num in partitioner.AsParallel()
245-
where num % 2 == 0
246-
select num;
247-
248-
foreach (var v in query)
249-
Console.WriteLine(v);
250-
}
251-
}
252-
//</snippet04>
253-
254-
255-
256130
class PartTest2
257131
{
258132
static CancellationTokenSource cts = new CancellationTokenSource();
259133
static StringBuilder sb = new StringBuilder();
260134

261-
static void Main(string[] args)
135+
static void Main2(string[] args)
262136
{
263137
//Math.
264138
int[] sourceArray = Enumerable.Range(1, 12680).ToArray();
@@ -513,7 +387,7 @@ BigInteger Fibonacci(int x)
513387

514388
class BasicMathTest
515389
{
516-
static void Main()
390+
static void Main2()
517391
{
518392
int[] nums = Enumerable.Range(1, 10000).ToArray();
519393
// CalculatePartitions(3, 4, 4, nums.Length);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Remove="*.cs" />
10+
<Compile Include="partitioners.cs" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)