Completed
Branch master (49eb62)
by Bingo
02:52 queued 56s
created

FibonacciHeapNode::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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