-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGeneral.ttinclude
More file actions
74 lines (62 loc) · 1.96 KB
/
General.ttinclude
File metadata and controls
74 lines (62 loc) · 1.96 KB
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
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#+
public static string Delimit<T>(IEnumerable<T> self, string delimiter, Func<T, string> selector, bool outputTrailingDelimiter = false)
{
var builder = new StringBuilder();
bool isFirst = true;
foreach (T item in self)
{
if (isFirst)
isFirst = false;
else
builder.Append(delimiter);
builder.Append(selector(item));
}
if(outputTrailingDelimiter)
builder.Append(delimiter);
return builder.ToString();
}
public class BatchData<T> : IEnumerable<T>
{
private readonly IEnumerable<T> _Batch = null;
public BatchData(int index, int size, IEnumerable<T> batch)
{
Index = index;
Size = size;
_Batch = batch;
}
public int Index { get; private set; }
public int Size { get; private set; }
public IEnumerator<T> GetEnumerator()
{
return _Batch.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public static IEnumerable<BatchData<T>> Batch<T>(IEnumerable<T> self, int batchSize)
{
using(var enumerator = self.GetEnumerator())
{
int batchIndex = 0;
int count = 0;
T[] buffer = new T[batchSize];
while(enumerator.MoveNext())
{
buffer[count++] = enumerator.Current;
if (count == batchSize)
{
yield return new BatchData<T>(batchIndex, batchSize, buffer);
count = 0;
batchIndex++;
buffer = new T[batchSize];
}
}
if(count != 0)
yield return new BatchData<T>(batchIndex, count, buffer.Take(count));
}
}
#>