-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitwise.d
118 lines (105 loc) · 2.91 KB
/
splitwise.d
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
112
113
114
115
116
117
118
#!/usr/bin/env rdmd
import std.range;
import std.array;
import std.algorithm;
import std.stdio;
import std.traits;
/**
Iterates over a range in splits.
Allows non-filling end intervals:
[1,2,3].splitwise().array == [[1,2],[3]]
Params:
r = Range from which the minimum will be selected
pairLength =
Returns: The minimum of the passed-in values.
*/
auto splitwise(Range)(Range r, int pairLength = 2)
if(isInputRange!Range)
in{
assert(pairLength >= 1, "invalid pairLength");
}
body{
static struct Splitwise{
private Range _input;
private int _pairLength;
private ElementType!(Range)[] _els;
this (Range input, int pairLength)
{
this._input = input;
this._pairLength = pairLength;
}
private void _popSplitwise(){
// create new array -> make other mutable
if(_input.empty){
// soft catch in case we reached the end
_els = null;
}else{
_els = new ElementType!(Range)[](_pairLength);
for (int i = 0; i < _pairLength; i++){
if(_input.empty){
_els.length = i;
break;
}
ElementType!Range el = _input.front;
_els[i] = el;
_input.popFront();
}
}
}
void popFront(){
assert(!empty, "empty array");
if(_els is null){
// pop was never called before -> stuck before
_popSplitwise();
}
_popSplitwise();
}
@property bool empty(){
return _input.empty && _els is null;
}
@property auto ref front(){
assert(!empty, "r is already empty");
if (_els is null){
_popSplitwise();
}
return _els;
}
}
return Splitwise(r, pairLength);
}
///
unittest{
assert([0,1,2,3,4,5].splitwise.array == [[0, 1], [2, 3], [4, 5]]);
assert(iota(5).splitwise(2).front == [0,1]);
}
unittest{
assert([0,1,2,3,4,5].splitwise(3).array == [[0, 1, 2], [3, 4, 5]]);
assert(iota(2).splitwise(2).front == [0,1]);
assert(iota(3).splitwise(2).array == [[0,1],[2]]);
assert(iota(2).splitwise(3).front == [0,1]);
int[] d;
assert(d.splitwise.empty);
auto e = iota(5).splitwise(2);
e.popFront;
assert(e.array == [[2,3],[4]]);
}
unittest{
// test splitwise2 step by step
auto a = [0,1,2,3,4,5].splitwise;
assert(a.front == [0,1]);
a.popFront();
assert(a.front == [2,3]);
a.popFront();
assert(a.front == [4,5]);
a.popFront();
assert(a.empty);
}
unittest{
// test splitwise3 step by step
auto a = [0,1,2,3].splitwise(3);
assert(a.front == [0,1,2]);
a.popFront();
assert(a.front == [3]);
a.popFront();
assert(a.empty);
}