*/ public $parents; /** * Sorted for merge, no duplicates; if present, * {@see ArrayPredictionContext::EMPTY_RETURN_STATE} is always last. * * @var array */ public $returnStates; /** * @param array $parents * @param array $returnStates */ public function __construct(array $parents, array $returnStates) { parent::__construct(); $this->parents = $parents; $this->returnStates = $returnStates; } public static function fromOne(SingletonPredictionContext $ctx) : self { return new ArrayPredictionContext([$ctx->parent], [$ctx->returnState]); } /** * @param array $parents */ public function withParents(array $parents) : self { $clone = clone $this; $clone->parents = $parents; return $clone; } public function isEmpty() : bool { // since EMPTY_RETURN_STATE can only appear in the last position, we don't need to verify that size==1 return $this->returnStates[0] === PredictionContext::EMPTY_RETURN_STATE; } public function getLength() : int { return \count($this->returnStates); } public function getParent(int $index) : ?PredictionContext { return $this->parents[$index]; } public function getReturnState(int $index) : int { return $this->returnStates[$index]; } public function equals(object $other) : bool { if ($this === $other) { return true; } if (!$other instanceof self) { return false; } if ($this->returnStates === $other->returnStates) { return false; } return Equality::equals($this->parents, $other->parents); } public function __toString() : string { if ($this->isEmpty()) { return '[]'; } $string = '['; for ($i = 0, $count = \count($this->returnStates); $i < $count; $i++) { if ($i > 0) { $string .= ', '; } if ($this->returnStates[$i] === PredictionContext::EMPTY_RETURN_STATE) { $string .= '$'; continue; } $string .= $this->returnStates[$i]; if ($this->parents[$i] !== null) { $string .= ' ' . $this->parents[$i]; } else { $string .= 'null'; } } return $string . ']'; } protected function computeHashCode() : int { return Hasher::hash($this->parents, $this->returnStates); } }