File: Blocks.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (111 lines) | stat: -rw-r--r-- 5,119 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Blocks.cs
//  
// Copyright (c) 2012 Petr Onderka
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks.Dataflow;

namespace MonoTests {
	public static class Blocks {
		public static IEnumerable<IDataflowBlock> CreateBlocksWithOptions (
			DataflowBlockOptions dataflowBlockOptions,
			ExecutionDataflowBlockOptions executionDataflowBlockOptions,
			GroupingDataflowBlockOptions groupingDataflowBlockOptions)
		{
			yield return new ActionBlock<int> (i => { }, executionDataflowBlockOptions);
			yield return new BatchBlock<double> (10, groupingDataflowBlockOptions);
			yield return new BatchedJoinBlock<dynamic, object> (
				10, groupingDataflowBlockOptions);
			yield return new BatchedJoinBlock<dynamic, object, char> (
				10, groupingDataflowBlockOptions);
			yield return new BroadcastBlock<byte> (x => x, dataflowBlockOptions);
			yield return new BufferBlock<int> (dataflowBlockOptions);
			yield return new JoinBlock<double, dynamic> (groupingDataflowBlockOptions);
			yield return new JoinBlock<object, char, byte> (
				groupingDataflowBlockOptions);
			yield return new TransformBlock<int, int> (
				i => i, executionDataflowBlockOptions);
			yield return new TransformManyBlock<double, dynamic>(
				x => new dynamic[0], executionDataflowBlockOptions);
			yield return new WriteOnceBlock<object> (x => x, dataflowBlockOptions);
		}

		public static IEnumerable<IDataflowBlock> CreateBlocks()
		{
			return CreateBlocksWithOptions (new DataflowBlockOptions (),
				new ExecutionDataflowBlockOptions (), new GroupingDataflowBlockOptions ());
		}

		public static IEnumerable<IDataflowBlock> CreateBlocksWithNameFormat(string nameFormat)
		{
			var dataflowBlockOptions = new DataflowBlockOptions { NameFormat = nameFormat };
			var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions { NameFormat = nameFormat };
			var groupingDataflowBlockOptions = new GroupingDataflowBlockOptions { NameFormat = nameFormat };

			return CreateBlocksWithOptions (dataflowBlockOptions,
				executionDataflowBlockOptions, groupingDataflowBlockOptions);
		}

		public static IEnumerable<IDataflowBlock> CreateBlocksWithCancellationToken(CancellationToken cancellationToken)
		{
			var dataflowBlockOptions = new DataflowBlockOptions { CancellationToken = cancellationToken};
			var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions { CancellationToken = cancellationToken};
			var groupingDataflowBlockOptions = new GroupingDataflowBlockOptions { CancellationToken = cancellationToken};

			return CreateBlocksWithOptions (dataflowBlockOptions,
				executionDataflowBlockOptions, groupingDataflowBlockOptions);
		}

		public static IEnumerable<IReceivableSourceBlock<T>> CreateSimpleSourceBlocks<T>()
		{
			yield return new BroadcastBlock<T> (x => x);
			yield return new BufferBlock<T> ();
			yield return new TransformBlock<T, T> (
				x => x);
			yield return new TransformManyBlock<T, T>(
				x => new T[0]);
			yield return new WriteOnceBlock<T> (x => x);
		}

		public static IEnumerable<ITargetBlock<T>> CreateTargetBlocks<T> ()
		{
			yield return new ActionBlock<T> (i => { });
			yield return new BatchBlock<T> (10);
			yield return new BatchedJoinBlock<T, T> (10).Target1;
			yield return new BatchedJoinBlock<T, T> (10).Target2;
			yield return new BatchedJoinBlock<T, T, T> (10).Target1;
			yield return new BatchedJoinBlock<T, T, T> (10).Target2;
			yield return new BatchedJoinBlock<T, T, T> (10).Target3;
			yield return new BroadcastBlock<T> (x => x);
			yield return new BufferBlock<T> ();
			yield return new JoinBlock<T, T> ().Target1;
			yield return new JoinBlock<T, T> ().Target2;
			yield return new JoinBlock<T, T, T> ().Target1;
			yield return new JoinBlock<T, T, T> ().Target2;
			yield return new JoinBlock<T, T, T> ().Target3;
			yield return new TransformBlock<T, T> (i => i);
			yield return new TransformManyBlock<T, T>(x => new T[0]);
			yield return new WriteOnceBlock<T> (x => x);
			yield return DataflowBlock.NullTarget<T> ();
		}
	}
}