Skip to content

Commit

Permalink
Add Node Item class
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-on committed Nov 14, 2020
1 parent d7f2a36 commit 2a40116
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
26 changes: 26 additions & 0 deletions src/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Danon\IntervalTree;

class Item
{
public $key;
public $value;

public function __construct(?Interval $key, $value)
{
$this->key = $key;
$this->value = $value;
}

function getKey(): Interval
{
return $this->key;
}


function getValue()
{
return $this->value;
}
}
14 changes: 7 additions & 7 deletions src/Node.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Danon\IntervalTree;

class Node
Expand Down Expand Up @@ -46,17 +47,16 @@ class Node

public function __construct($key = null, $value = null, $left = null, $right = null, $parent = null, $color = self::COLOR_BLACK)
{

$this->left = $left;
$this->right = $right;
$this->parent = $parent;
$this->color = $color;

$this->item = (object) compact('key', 'value'); // key is supposed to be instance of Interval

/* If not, this should by an array of two numbers */
if ($key && is_array($key) && count($key) === 2) {
$this->item->key = new Interval(min($key), max($key));
if (is_null($key)) {
$this->item = new Item($key, $value); // key is supposed to be instance of Interval
} elseif ($key && is_array($key) && count($key) === 2) {
$item = new Item(new Interval(min($key), max($key)), $value);
$this->item = $item;
}

$this->max = $this->item->key ? clone $this->item->key : null;
Expand Down Expand Up @@ -88,7 +88,7 @@ public function equalTo($otherNode)
$valueEqual = true;
if ($this->item->value && $otherNode->item->value) {
$valueEqual = $this->item->value ? $this->item->value->equalTo($otherNode->item->value) :
$this->item->value == $otherNode->item->value;
$this->item->value == $otherNode->item->value;
}
return $this->item->key->equalTo($otherNode->item->key) && $valueEqual;
}
Expand Down

0 comments on commit 2a40116

Please sign in to comment.