Skip to content

Commit

Permalink
Merge pull request #3 from dan-on/1.0.1
Browse files Browse the repository at this point in the history
Refactor types
  • Loading branch information
dan-on committed Oct 23, 2020
2 parents 5d4b491 + 0a7da08 commit e9e21a2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
34 changes: 19 additions & 15 deletions src/Interval.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php
namespace Danon\IntervalTree;
use InvalidArgumentException;

class Interval {
use InvalidArgumentException;

class Interval
{
public $low;
public $high;

public function __construct(int $low, int $high)
{
if($low > $high) {
if ($low > $high) {
throw new InvalidArgumentException('Low interval cannot be greater than high');
}

Expand All @@ -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;
}
Expand All @@ -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);
}

Expand All @@ -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;
}
}
30 changes: 17 additions & 13 deletions src/IntervalTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -59,9 +59,10 @@ public function getValues()

/**
* Returns array of items (<key,value> 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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions src/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e9e21a2

Please sign in to comment.