Completed
Branch master (a3dc87)
by Bingo
02:49 queued 01:00
created

FibonacciHeapNode::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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