FibonacciHeapNode   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 166
ccs 33
cts 36
cp 0.9167
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getOwner() 0 18 4
A decreaseKey() 0 4 1
A delete() 0 8 2
A __construct() 0 6 1
A getKey() 0 3 1
A getValue() 0 3 1
A setValue() 0 3 1
1
<?php
2
3
namespace Heap\Tree;
4
5
use InvalidArgumentException;
6
use Heap\AddressableHeapHandleInterface;
7
8
/**
9
 * Class FibonacciHeapNode
10
 *
11
 * @package Heap\Tree
12
 */
13
class FibonacciHeapNode implements AddressableHeapHandleInterface
14
{
15
    /**
16
     * The heap node unique hash
17
     *
18
     * @var string
19
     */
20
    private $hash;
21
22
    /**
23
     * Node heap
24
     *
25
     * @var FibonacciHeap
26
     */
27
    public $heap;
28
    
29
    /**
30
     * Node value
31
     *
32
     * @var mixed
33
     */
34
    public $value;
35
    
36
    /**
37
     * Parent node
38
     *
39
     * @var FibonacciHeapNode
40
     */
41
    public $parent = null;
42
    
43
    /**
44
     * Child node
45
     *
46
     * @var FibonacciHeapNode
47
     */
48
    public $child = null;
49
    
50
    /**
51
     * Previous node
52
     *
53
     * @var FibonacciHeapNode
54
     */
55
    public $prev = null;
56
    
57
    /**
58
     * Next node
59
     *
60
     * @var FibonacciHeapNode
61
     */
62
    public $next = null;
63
    
64
    /**
65
     * True if the node lost child nodes
66
     *
67
     * @var bool
68
     */
69
    public $mark = false;
70
    
71
    /**
72
     * Node key
73
     *
74
     * @var mixed
75
     */
76
    public $key;
77
    
78
    /**
79
     * Node degree
80
     *
81
     * @var int
82
     */
83
    public $degree = 0;
84
    
85
    /**
86
     * Construct a new Fibonacci heap node
87
     *
88
     * @param FibonacciHeap $heap - heap to which the node belongs
89
     * @param mixed $key - the node key
90
     * @param mixed $value - value stored in the node
91
     */
92 23
    public function __construct(FibonacciHeap $heap, $key, $value)
93
    {
94 23
        $this->heap = $heap;
95 23
        $this->key = $key;
96 23
        $this->value = $value;
97 23
        $this->hash = uniqid('', true);
98 23
    }
99
100
    /**
101
     * Get the node key
102
     *
103
     * @return mixed
104
     */
105 16
    public function getKey()
106
    {
107 16
        return $this->key;
108
    }
109
    
110
    /**
111
     * Get the node value
112
     *
113
     * @return mixed
114
     */
115 1
    public function getValue()
116
    {
117 1
        return $this->value;
118
    }
119
    
120
    /**
121
     * Set the node value
122
     *
123
     * @param mixed $value - node value
124
     */
125 1
    public function setValue($value): void
126
    {
127 1
        $this->value = $value;
128 1
    }
129
130
    /**
131
     * Decrease the node key
132
     *
133
     * @param mixed $newKey - new node key
134
     */
135 6
    public function decreaseKey($newKey): void
136
    {
137 6
        $heap = $this->getOwner();
138 6
        $heap->decreaseKey($this, $newKey);
139 5
    }
140
141
    /**
142
     * Delete the node
143
     *
144
     * @throws InvalidArgumentException
145
     */
146 7
    public function delete(): void
147
    {
148 7
        if (is_null($this->next)) {
149 3
            throw new InvalidArgumentException("Invalid handle!");
150
        }
151 5
        $heap = $this->getOwner();
152 5
        $heap->forceDecreaseKeyToMinimum($this);
153 5
        $heap->deleteMin();
154 5
    }
155
156
    /**
157
     * Get the owner heap of the node
158
     *
159
     * @return FibonacciHeap
160
     */
161 10
    public function getOwner(): FibonacciHeap
162
    {
163 10
        if ($this->heap->getOther() != $this->heap) {
164
            // find root
165 2
            $root = $this->heap;
166 2
            while ($root != $root->getOther()) {
167 2
                $root = $root->getOther();
168
            }
169
            // path-compression
170 2
            $cur = $this->heap;
171 2
            while ($cur->getOther() != $root) {
172
                $next = $cur->getOther();
173
                $cur->setOther($root);
174
                $cur = $next;
175
            }
176 2
            $this->heap = $root;
177
        }
178 10
        return $this->heap;
179
    }
180
}
181