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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
package bjc.utils.data.experimental;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* A lazy implementation of a pair
*
* @author ben
*
* @param <LeftType>
* The type on the left side of the pair
* @param <RightType>
* The type on the right side of the pair
*/
public class LazyPair<LeftType, RightType>
implements IPair<LeftType, RightType> {
private static class HalfBoundLazyPair<OldType, NewLeft, NewRight>
implements IPair<NewLeft, NewRight> {
private Supplier<OldType> oldSupplier;
private Function<OldType, IPair<NewLeft, NewRight>> binder;
private IPair<NewLeft, NewRight> boundPair;
private boolean pairBound;
public HalfBoundLazyPair(Supplier<OldType> oldSupp,
Function<OldType, IPair<NewLeft, NewRight>> bindr) {
oldSupplier = oldSupp;
binder = bindr;
}
@Override
public <BoundLeft> IPair<BoundLeft, NewRight> bindLeft(
Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) {
Supplier<NewLeft> leftSupp = () -> {
IPair<NewLeft, NewRight> newPair = boundPair;
if (!pairBound) {
newPair = binder.apply(oldSupplier.get());
}
return newPair.getLeft();
};
return new HalfBoundLazyPair<>(leftSupp, leftBinder);
}
@Override
public <BoundRight> IPair<NewLeft, BoundRight> bindRight(
Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) {
Supplier<NewRight> rightSupp = () -> {
IPair<NewLeft, NewRight> newPair = boundPair;
if (!pairBound) {
newPair = binder.apply(oldSupplier.get());
}
return newPair.getRight();
};
return new HalfBoundLazyPair<>(rightSupp, rightBinder);
}
@Override
public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind(
BiFunction<NewLeft, NewRight, IPair<BoundLeft, BoundRight>> bindr) {
IHolder<IPair<NewLeft, NewRight>> newPair = new Identity<>(
boundPair);
IHolder<Boolean> newPairMade = new Identity<>(pairBound);
Supplier<NewLeft> leftSupp = () -> {
if (!newPairMade.getValue()) {
newPair.replace(binder.apply(oldSupplier.get()));
newPairMade.replace(true);
}
return newPair.unwrap((pair) -> pair.getLeft());
};
Supplier<NewRight> rightSupp = () -> {
if (!newPairMade.getValue()) {
newPair.replace(binder.apply(oldSupplier.get()));
newPairMade.replace(true);
}
return newPair.unwrap((pair) -> pair.getRight());
};
return new BoundLazyPair<>(leftSupp, rightSupp, bindr);
}
@Override
public <MergedType> MergedType merge(
BiFunction<NewLeft, NewRight, MergedType> merger) {
if (!pairBound) {
boundPair = binder.apply(oldSupplier.get());
pairBound = true;
}
return boundPair.merge(merger);
}
}
private static class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight>
implements IPair<NewLeft, NewRight> {
private Supplier<OldLeft> leftSupplier;
private Supplier<OldRight> rightSupplier;
private BiFunction<OldLeft, OldRight, IPair<NewLeft, NewRight>> binder;
private IPair<NewLeft, NewRight> boundPair;
private boolean pairBound;
public BoundLazyPair(Supplier<OldLeft> leftSupp,
Supplier<OldRight> rightSupp,
BiFunction<OldLeft, OldRight, IPair<NewLeft, NewRight>> bindr) {
leftSupplier = leftSupp;
rightSupplier = rightSupp;
binder = bindr;
}
@Override
public <BoundLeft> IPair<BoundLeft, NewRight> bindLeft(
Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) {
Supplier<NewLeft> leftSupp = () -> {
IPair<NewLeft, NewRight> newPair = boundPair;
if (!pairBound) {
newPair = binder.apply(leftSupplier.get(),
rightSupplier.get());
}
return newPair.getLeft();
};
return new HalfBoundLazyPair<>(leftSupp, leftBinder);
}
@Override
public <BoundRight> IPair<NewLeft, BoundRight> bindRight(
Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) {
Supplier<NewRight> rightSupp = () -> {
IPair<NewLeft, NewRight> newPair = boundPair;
if (!pairBound) {
newPair = binder.apply(leftSupplier.get(),
rightSupplier.get());
}
return newPair.getRight();
};
return new HalfBoundLazyPair<>(rightSupp, rightBinder);
}
@Override
public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind(
BiFunction<NewLeft, NewRight, IPair<BoundLeft, BoundRight>> bindr) {
IHolder<IPair<NewLeft, NewRight>> newPair = new Identity<>(
boundPair);
IHolder<Boolean> newPairMade = new Identity<>(pairBound);
return new BoundLazyPair<>(() -> {
if (!newPairMade.getValue()) {
newPair.replace(binder.apply(leftSupplier.get(),
rightSupplier.get()));
newPairMade.replace(false);
}
return newPair.unwrap((pair) -> pair.getLeft());
}, () -> {
if (!newPairMade.getValue()) {
newPair.replace(binder.apply(leftSupplier.get(),
rightSupplier.get()));
newPairMade.replace(false);
}
return newPair.unwrap((pair) -> pair.getRight());
}, bindr);
}
@Override
public <MergedType> MergedType merge(
BiFunction<NewLeft, NewRight, MergedType> merger) {
if (!pairBound) {
boundPair = binder.apply(leftSupplier.get(),
rightSupplier.get());
pairBound = true;
}
return boundPair.merge(merger);
}
}
private LeftType leftValue;
private RightType rightValue;
private Supplier<LeftType> leftSupplier;
private Supplier<RightType> rightSupplier;
private boolean leftMaterialized;
private boolean rightMaterialized;
/**
* Create a new lazy pair, using the set value s
*
* @param leftVal
* The value for the left side of the pair
* @param rightVal
* The value for the right side of the pair
*/
public LazyPair(LeftType leftVal, RightType rightVal) {
leftValue = leftVal;
rightValue = rightVal;
leftMaterialized = true;
rightMaterialized = true;
}
/**
* Create a new lazy pair from the given value sources
*
* @param leftSupp
* The source for a value on the left side of the pair
* @param rightSupp
* The source for a value on the right side of the pair
*/
public LazyPair(Supplier<LeftType> leftSupp,
Supplier<RightType> rightSupp) {
leftSupplier = leftSupp;
rightSupplier = rightSupp;
leftMaterialized = false;
rightMaterialized = false;
}
@Override
public <BoundLeft> IPair<BoundLeft, RightType> bindLeft(
Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) {
Supplier<LeftType> leftSupp = () -> {
if (leftMaterialized) {
return leftValue;
}
return leftSupplier.get();
};
return new HalfBoundLazyPair<>(leftSupp, leftBinder);
}
@Override
public <BoundRight> IPair<LeftType, BoundRight> bindRight(
Function<RightType, IPair<LeftType, BoundRight>> rightBinder) {
Supplier<RightType> rightSupp = () -> {
if (rightMaterialized) {
return rightValue;
}
return rightSupplier.get();
};
return new HalfBoundLazyPair<>(rightSupp, rightBinder);
}
@Override
public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind(
BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) {
return new BoundLazyPair<>(leftSupplier, rightSupplier, binder);
}
@Override
public <MergedType> MergedType merge(
BiFunction<LeftType, RightType, MergedType> merger) {
if (!leftMaterialized) {
leftValue = leftSupplier.get();
leftMaterialized = true;
}
if (!rightMaterialized) {
rightValue = rightSupplier.get();
rightMaterialized = true;
}
return merger.apply(leftValue, rightValue);
}
@Override
public LeftType getLeft() {
if (!leftMaterialized) {
leftValue = leftSupplier.get();
leftMaterialized = true;
}
return leftValue;
}
@Override
public RightType getRight() {
if (!rightMaterialized) {
rightValue = rightSupplier.get();
rightMaterialized = true;
}
return rightValue;
}
}
|