From 0a7da0815b40f2b8ab1ce83b061e10a332c921ea Mon Sep 17 00:00:00 2001 From: dan1 Date: Fri, 23 Oct 2020 20:56:31 +0500 Subject: [PATCH] refactor types --- src/Interval.php | 34 +++++++++++++++++++--------------- src/IntervalTree.php | 30 +++++++++++++++++------------- src/Node.php | 13 +++++++++++++ 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/Interval.php b/src/Interval.php index e7467f3..8c4093d 100644 --- a/src/Interval.php +++ b/src/Interval.php @@ -1,15 +1,16 @@ $high) { + if ($low > $high) { throw new InvalidArgumentException('Low interval cannot be greater than high'); } @@ -23,7 +24,7 @@ public function lessThan(Interval $otherInterval) $this->low == $otherInterval->low && $this->high < $otherInterval->high; } - public function equalTo(Interval $otherInterval) + public function equalTo(Interval $otherInterval) { return $this->low == $otherInterval->low && $this->high == $otherInterval->high; } @@ -49,19 +50,20 @@ public function merge(Interval $otherInterval) /** * Returns how key should return */ - public function output() { + public function output() + { return [$this->low, $this->high]; } - - /** * Function returns maximum between two comparable values - * @param interval1 - * @param interval2 - * @returns {Interval} + * + * @param Interval $interval1 + * @param Interval $interval2 + * @return Interval */ - public static function comparableMax($interval1, $interval2) { + public static function comparableMax($interval1, $interval2): self + { return $interval1->merge($interval2); } @@ -72,11 +74,13 @@ public function getMax() /** * Predicate returns true if first value less than second value - * @param val1 - * @param val2 - * @returns {boolean} + * + * @param $val1 + * @param $val2 + * @return bool */ - public static function comparableLessThan($val1, $val2) { + public static function comparableLessThan($val1, $val2): bool + { return $val1 < $val2; } } \ No newline at end of file diff --git a/src/IntervalTree.php b/src/IntervalTree.php index b0ce34e..661c908 100644 --- a/src/IntervalTree.php +++ b/src/IntervalTree.php @@ -46,9 +46,9 @@ public function getKeys(): array /** * Return array of values in the ascending keys order - * @returns {Array} + * @return array */ - public function getValues() + public function getValues(): array { $res = []; $this->treeWalk($this->root, function ($node) use (&$res) { @@ -59,9 +59,10 @@ public function getValues() /** * Returns array of items ( pairs) in the ascended keys order - * @returns {Array} + * + * @return array */ - public function getItems() + public function getItems(): array { $res = []; $this->treeWalk($this->root, function ($node) use (&$res) { @@ -144,11 +145,12 @@ public function insert(array $key, $value = null) /** * Returns true if item {key,value} exist in the tree + * * @param key - interval correspondent to keys stored in the tree * @param value - value object to be checked - * @returns {boolean} - true if item {key, value} exist in the tree, false otherwise + * @return bool - true if item {key, value} exist in the tree, false otherwise */ - public function exist($key, $value) + public function exist($key, $value): bool { $searchNode = new Node($key, $value); return $this->treeSearch($this->root, $searchNode) ? true : false; @@ -158,9 +160,9 @@ public function exist($key, $value) * Remove entry {key, value} from the tree * @param key - interval correspondent to keys stored in the tree * @param value - - value object - * @returns {boolean} - true if item {key, value} deleted, false if not found + * @return bool - true if item {key, value} deleted, false if not found */ - public function remove($key, $value) + public function remove($key, $value): bool { $searchNode = new Node($key, $value); $deleteNode = $this->treeSearch($this->root, $searchNode); @@ -175,13 +177,16 @@ public function remove($key, $value) * Method calls a callback function with two parameters (key, value) * @param visitor(key,value) - function to be called for each tree item */ - function foreach ($visitor) { + public function foreach($visitor) + { $this->treeWalk($this->root, function ($node) { return $visitor($node->item->key, $node->item->value); }); } - /** Value Mapper. Walk through every node and map node value to another value + /** + * Value Mapper. Walk through every node and map node value to another value + * * @param callback(value, key) - function to be called for each tree item */ public function map($callback) @@ -320,13 +325,12 @@ public function treeDelete($deleteNode) $this->recalcMax($fixNode); // update max property upward from fix_node to root - // COPY DATA !!! - // Delete_node becomes cut_node, it means that we cannot hold reference + // deleteNode becomes cutNode, it means that we cannot hold reference // to node in outer structure and we will have to delete by key, additional search need if ($cutNode !== $deleteNode) { $deleteNode->copyData($cutNode); $deleteNode->updateMax(); // update max property of the cut node at the new place - $this->recalcMax($deleteNode); // update max property upward from delete_node to root + $this->recalcMax($deleteNode); // update max property upward from deleteNode to root } if ( /*fix_node !== this.nil_node && */$cutNode->color === Node::COLOR_BLACK) { diff --git a/src/Node.php b/src/Node.php index a21aed9..5025393 100644 --- a/src/Node.php +++ b/src/Node.php @@ -9,24 +9,37 @@ class Node /** * Reference to left child node + * + * @var Node */ public $left; /** * Reference to right child node + * + * @var Node */ public $right; /** * Reference to parent node + * + * @var Node */ public $parent; /** * Color of node (BLACK or RED) + * + * @var int */ public $color; + /** + * Key and value + * + * @var object + */ public $item; public $max;