Skip to content

Commit 2a40116

Browse files
committed
Add Node Item class
1 parent d7f2a36 commit 2a40116

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/Item.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Danon\IntervalTree;
4+
5+
class Item
6+
{
7+
public $key;
8+
public $value;
9+
10+
public function __construct(?Interval $key, $value)
11+
{
12+
$this->key = $key;
13+
$this->value = $value;
14+
}
15+
16+
function getKey(): Interval
17+
{
18+
return $this->key;
19+
}
20+
21+
22+
function getValue()
23+
{
24+
return $this->value;
25+
}
26+
}

src/Node.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Danon\IntervalTree;
34

45
class Node
@@ -46,17 +47,16 @@ class Node
4647

4748
public function __construct($key = null, $value = null, $left = null, $right = null, $parent = null, $color = self::COLOR_BLACK)
4849
{
49-
5050
$this->left = $left;
5151
$this->right = $right;
5252
$this->parent = $parent;
5353
$this->color = $color;
5454

55-
$this->item = (object) compact('key', 'value'); // key is supposed to be instance of Interval
56-
57-
/* If not, this should by an array of two numbers */
58-
if ($key && is_array($key) && count($key) === 2) {
59-
$this->item->key = new Interval(min($key), max($key));
55+
if (is_null($key)) {
56+
$this->item = new Item($key, $value); // key is supposed to be instance of Interval
57+
} elseif ($key && is_array($key) && count($key) === 2) {
58+
$item = new Item(new Interval(min($key), max($key)), $value);
59+
$this->item = $item;
6060
}
6161

6262
$this->max = $this->item->key ? clone $this->item->key : null;
@@ -88,7 +88,7 @@ public function equalTo($otherNode)
8888
$valueEqual = true;
8989
if ($this->item->value && $otherNode->item->value) {
9090
$valueEqual = $this->item->value ? $this->item->value->equalTo($otherNode->item->value) :
91-
$this->item->value == $otherNode->item->value;
91+
$this->item->value == $otherNode->item->value;
9292
}
9393
return $this->item->key->equalTo($otherNode->item->key) && $valueEqual;
9494
}

0 commit comments

Comments
 (0)