diff --git a/csharp/tutorials/AsyncStreams/finished/IssuePRreport/IssuePRreport/Program.cs b/csharp/tutorials/AsyncStreams/finished/IssuePRreport/IssuePRreport/Program.cs index 0058f2dee32..b7545d62249 100644 --- a/csharp/tutorials/AsyncStreams/finished/IssuePRreport/IssuePRreport/Program.cs +++ b/csharp/tutorials/AsyncStreams/finished/IssuePRreport/IssuePRreport/Program.cs @@ -51,7 +51,7 @@ static async Task Main(string[] args) // - public_repo // Replace the 3rd parameter to the following code with your GitHub access token. var key = GetEnvVariable("GitHubKey", - "You must store you GitHub key in the 'GitHubKey' environment variable", + "You must store your GitHub key in the 'GitHubKey' environment variable", ""); var client = new GitHubClient(new Octokit.ProductHeaderValue("IssueQueryDemo")) diff --git a/csharp/tutorials/AsyncStreams/start/IssuePRreport/IssuePRreport/Program.cs b/csharp/tutorials/AsyncStreams/start/IssuePRreport/IssuePRreport/Program.cs index dadb99a1368..32a12fda866 100644 --- a/csharp/tutorials/AsyncStreams/start/IssuePRreport/IssuePRreport/Program.cs +++ b/csharp/tutorials/AsyncStreams/start/IssuePRreport/IssuePRreport/Program.cs @@ -61,7 +61,7 @@ static async Task Main(string[] args) // - public_repo // Replace the 3rd parameter to the following code with your GitHub access token. var key = GetEnvVariable("GitHubKey", - "You must store you GitHub key in the 'GitHubKey' environment variable", + "You must store your GitHub key in the 'GitHubKey' environment variable", ""); var client = new GitHubClient(new Octokit.ProductHeaderValue("IssueQueryDemo")) diff --git a/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioner02.cs b/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioner02.cs new file mode 100644 index 00000000000..b2867291462 --- /dev/null +++ b/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioner02.cs @@ -0,0 +1,97 @@ +namespace ProgramOrderableListPartitioner +{ + // + // + // An orderable dynamic partitioner for lists + // + using System; + using System.Collections; + using System.Collections.Concurrent; + using System.Collections.Generic; + using System.Diagnostics; + using System.Linq; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using System.Xml.Linq; + using System.Numerics; + + class OrderableListPartitioner : OrderablePartitioner + { + private readonly IList m_input; + + // Must override to return true. + public override bool SupportsDynamicPartitions => true; + + public OrderableListPartitioner(IList input) : base(true, false, true) => + m_input = input; + + public override IList>> GetOrderablePartitions(int partitionCount) + { + var dynamicPartitions = GetOrderableDynamicPartitions(); + var partitions = + new IEnumerator>[partitionCount]; + + for (int i = 0; i < partitionCount; i++) + { + partitions[i] = dynamicPartitions.GetEnumerator(); + } + return partitions; + } + + public override IEnumerable> GetOrderableDynamicPartitions() => + new ListDynamicPartitions(m_input); + + private class ListDynamicPartitions : IEnumerable> + { + private IList m_input; + private int m_pos = 0; + + internal ListDynamicPartitions(IList input) => + m_input = input; + + public IEnumerator> GetEnumerator() + { + while (true) + { + // Each task gets the next item in the list. The index is + // incremented in a thread-safe manner to avoid races. + int elemIndex = Interlocked.Increment(ref m_pos) - 1; + + if (elemIndex >= m_input.Count) + { + yield break; + } + + yield return new KeyValuePair( + elemIndex, m_input[elemIndex]); + } + } + + IEnumerator IEnumerable.GetEnumerator() => + ((IEnumerable>)this).GetEnumerator(); + } + } + + class ConsumerClass + { + static void Main() + { + var nums = Enumerable.Range(0, 10000).ToArray(); + OrderableListPartitioner partitioner = new OrderableListPartitioner(nums); + + // Use with Parallel.ForEach + Parallel.ForEach(partitioner, (i) => Console.WriteLine(i)); + + + // Use with PLINQ + var query = from num in partitioner.AsParallel() + where num % 2 == 0 + select num; + + foreach (var v in query) + Console.WriteLine(v); + } + } + // +} \ No newline at end of file diff --git a/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioner02.csproj b/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioner02.csproj new file mode 100644 index 00000000000..1f2eda6207f --- /dev/null +++ b/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioner02.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp3.0 + + + + + + + + diff --git a/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioners.cs b/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioners.cs index 2fd82d32934..685a86aa648 100644 --- a/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioners.cs +++ b/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioners.cs @@ -17,7 +17,6 @@ class Program static void Main(string[] args) { Consumer.Main2(); - TestOrderableListPartitioner(); // TestDefaultRangePartitioner(); //TestLoadBalancingCreateMethods(); // ParallelLoopsWithPartitioner(); @@ -33,26 +32,6 @@ static void Main(string[] args) } - static void TestOrderableListPartitioner() - { - var nums = Enumerable.Range(0, 10000).ToArray(); - OrderableListPartitioner part = new OrderableListPartitioner(nums); - - // Use with Parallel.ForEach - Parallel.ForEach(part, (i) => Console.WriteLine(i)); - - - // Use with PLINQ - var query = from num in part.AsParallel() - where num % 2 == 0 - select num; - - foreach(var v in query) - Console.WriteLine(v); - - } - - static void TestDefaultRangePartitioner() { @@ -148,117 +127,12 @@ static void TestLoadBalancingCreateMethods() } } - // - // - // An orderable dynamic partitioner for lists - // - class OrderableListPartitioner : OrderablePartitioner - { - private readonly IList m_input; - - public OrderableListPartitioner(IList input) - : base(true, false, true) - { - m_input = input; - } - - // Must override to return true. - public override bool SupportsDynamicPartitions - { - get - { - return true; - } - } - - public override IList>> - GetOrderablePartitions(int partitionCount) - { - var dynamicPartitions = GetOrderableDynamicPartitions(); - var partitions = - new IEnumerator>[partitionCount]; - - for (int i = 0; i < partitionCount; i++) - { - partitions[i] = dynamicPartitions.GetEnumerator(); - } - return partitions; - } - - public override IEnumerable> - GetOrderableDynamicPartitions() - { - return new ListDynamicPartitions(m_input); - } - - private class ListDynamicPartitions - : IEnumerable> - { - private IList m_input; - private int m_pos = 0; - - internal ListDynamicPartitions(IList input) - { - m_input = input; - } - - public IEnumerator> GetEnumerator() - { - while (true) - { - // Each task gets the next item in the list. The index is - // incremented in a thread-safe manner to avoid races. - int elemIndex = Interlocked.Increment(ref m_pos) - 1; - - if (elemIndex >= m_input.Count) - { - yield break; - } - - yield return new KeyValuePair( - elemIndex, m_input[elemIndex]); - } - } - - IEnumerator IEnumerable.GetEnumerator() - { - return - ((IEnumerable>)this) - .GetEnumerator(); - } - } - } - - class ConsumerClass - { - static void Main() - { - var nums = Enumerable.Range(0, 10000).ToArray(); - OrderableListPartitioner partitioner = new OrderableListPartitioner(nums); - - // Use with Parallel.ForEach - Parallel.ForEach(partitioner, (i) => Console.WriteLine(i)); - - - // Use with PLINQ - var query = from num in partitioner.AsParallel() - where num % 2 == 0 - select num; - - foreach (var v in query) - Console.WriteLine(v); - } - } - // - - - class PartTest2 { static CancellationTokenSource cts = new CancellationTokenSource(); static StringBuilder sb = new StringBuilder(); - static void Main(string[] args) + static void Main2(string[] args) { //Math. int[] sourceArray = Enumerable.Range(1, 12680).ToArray(); @@ -513,7 +387,7 @@ BigInteger Fibonacci(int x) class BasicMathTest { - static void Main() + static void Main2() { int[] nums = Enumerable.Range(1, 10000).ToArray(); // CalculatePartitions(3, 4, 4, nums.Length); diff --git a/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioners.csproj b/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioners.csproj new file mode 100644 index 00000000000..8e8674ba1b6 --- /dev/null +++ b/snippets/csharp/VS_Snippets_Misc/tpl_partitioners/cs/partitioners.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp3.0 + + + + + + + +