Skip to content

Commit 4c2c1e2

Browse files
authored
Faster TrinaryLogic::maxMin(), TrinaryLogic::lazyMaxMin()
1 parent 001d465 commit 4c2c1e2

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/TrinaryLogic.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,16 @@ public static function maxMin(self ...$operands): self
243243
if ($operands === []) {
244244
throw new ShouldNotHappenException();
245245
}
246-
$operandValues = array_column($operands, 'value');
247-
return self::create(max($operandValues) > self::MAYBE ? self::YES : min($operandValues));
246+
247+
$max = self::NO;
248+
$min = self::YES;
249+
foreach ($operands as $operand) {
250+
$max |= $operand->value;
251+
$min &= $operand->value;
252+
}
253+
$maxMin = $max === self::YES ? self::YES : $min;
254+
255+
return self::$registry[$maxMin] ??= new self($maxMin);
248256
}
249257

250258
/**
@@ -257,17 +265,17 @@ public static function lazyMaxMin(
257265
callable $callback,
258266
): self
259267
{
260-
$results = [];
268+
$min = self::YES;
261269
foreach ($objects as $object) {
262270
$result = $callback($object);
263271
if ($result->value === self::YES) {
264272
return $result;
265273
}
266274

267-
$results[] = $result;
275+
$min &= $result->value;
268276
}
269277

270-
return self::maxMin(...$results);
278+
return self::$registry[$min] ??= new self($min);
271279
}
272280

273281
public function negate(): self

0 commit comments

Comments
 (0)