Version 4.6.0
This commit is contained in:
1 parent
f79dcf067a
commit
517a5e1f70
2036 files changed
+110041
-26162
No files matched your search
+40
-61
@@ -1,87 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Utils;
|
||||
|
||||
/**
|
||||
* Class PairSet
|
||||
* @package GraphQL\Utils
|
||||
* A way to keep track of pairs of things when the ordering of the pair does
|
||||
* not matter. We do this by maintaining a sort of double adjacency sets.
|
||||
*/
|
||||
class PairSet
|
||||
{
|
||||
/**
|
||||
* @var \SplObjectStorage<any, Set<any>>
|
||||
*/
|
||||
/** @var bool[][] */
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $wrappers = [];
|
||||
|
||||
/**
|
||||
* PairSet constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->data = new \SplObjectStorage(); // SplObject hash instead?
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $a
|
||||
* @param $b
|
||||
* @return null|object
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @param bool $areMutuallyExclusive
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has($a, $b)
|
||||
public function has($a, $b, $areMutuallyExclusive)
|
||||
{
|
||||
$a = $this->toObj($a);
|
||||
$b = $this->toObj($b);
|
||||
|
||||
/** @var \SplObjectStorage $first */
|
||||
$first = isset($this->data[$a]) ? $this->data[$a] : null;
|
||||
return isset($first, $first[$b]) ? $first[$b] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $a
|
||||
* @param $b
|
||||
*/
|
||||
public function add($a, $b)
|
||||
{
|
||||
$this->pairSetAdd($a, $b);
|
||||
$this->pairSetAdd($b, $a);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $var
|
||||
* @return mixed
|
||||
*/
|
||||
private function toObj($var)
|
||||
{
|
||||
// SplObjectStorage expects objects, so wrapping non-objects to objects
|
||||
if (is_object($var)) {
|
||||
return $var;
|
||||
$first = $this->data[$a] ?? null;
|
||||
$result = $first && isset($first[$b]) ? $first[$b] : null;
|
||||
if ($result === null) {
|
||||
return false;
|
||||
}
|
||||
if (!isset($this->wrappers[$var])) {
|
||||
$tmp = new \stdClass();
|
||||
$tmp->_internal = $var;
|
||||
$this->wrappers[$var] = $tmp;
|
||||
// areMutuallyExclusive being false is a superset of being true,
|
||||
// hence if we want to know if this PairSet "has" these two with no
|
||||
// exclusivity, we have to ensure it was added as such.
|
||||
if ($areMutuallyExclusive === false) {
|
||||
return $result === false;
|
||||
}
|
||||
return $this->wrappers[$var];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $a
|
||||
* @param $b
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @param bool $areMutuallyExclusive
|
||||
*/
|
||||
private function pairSetAdd($a, $b)
|
||||
public function add($a, $b, $areMutuallyExclusive)
|
||||
{
|
||||
$a = $this->toObj($a);
|
||||
$b = $this->toObj($b);
|
||||
$set = isset($this->data[$a]) ? $this->data[$a] : null;
|
||||
$this->pairSetAdd($a, $b, $areMutuallyExclusive);
|
||||
$this->pairSetAdd($b, $a, $areMutuallyExclusive);
|
||||
}
|
||||
|
||||
if (!isset($set)) {
|
||||
$set = new \SplObjectStorage();
|
||||
$this->data[$a] = $set;
|
||||
}
|
||||
$set[$b] = true;
|
||||
/**
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @param bool $areMutuallyExclusive
|
||||
*/
|
||||
private function pairSetAdd($a, $b, $areMutuallyExclusive)
|
||||
{
|
||||
$this->data[$a] = $this->data[$a] ?? [];
|
||||
$this->data[$a][$b] = $areMutuallyExclusive;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user