Completed
Push — master ( f37b88...0dd5a5 )
by Bingo
08:58
created

PairingHeapNode   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 3 1
A delete() 0 4 1
A __construct() 0 9 1
A getValue() 0 3 1
A decreaseKey() 0 4 1
A getOwner() 0 18 4
A setValue() 0 3 1
1
<?php
2
3
namespace BingoSoft\Heap\Tree;
4
5
use InvalidArgumentException;
6
use BingoSoft\Heap\AddressableHeapHandleInterface;
7
8
/**
9
 * Class PairingHeapNode
10
 *
11
 * @package BingoSoft\Heap\Tree
12
 */
13
class PairingHeapNode 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 PairingHeap
26
     */
27
    public $heap;
28
29
    /**
30
     * Node value
31
     *
32
     * @var mixed
33
     */
34
    public $value;
35
36
    /**
37
     * Older child
38
     *
39
     * @var PairingHeapNode
40
     */
41
    public $o_c;
42
43
    /**
44
     * Younger sibling
45
     *
46
     * @var PairingHeapNode
47
     */
48
    public $y_s;
49
50
    /**
51
     * Older sibling or parent
52
     *
53
     * @var PairingHeapNode
54
     */
55
    public $o_s;
56
57
    /**
58
     * Node key
59
     *
60
     * @var int
61
     */
62
    public $key;
63
64 23
    /**
65
     * Construct a new Pairing heap node
66 23
     *
67 23
     * @param PairingHeap $heap - heap to which the node belongs
68 23
     * @param int $key - the node key
69 23
     * @param mixed $value - value stored in the node
70 23
     */
71 23
    public function __construct(PairingHeap $heap, int $key, $value)
72 23
    {
73 23
        $this->heap = $heap;
74
        $this->key = $key;
75
        $this->value = $value;
76
        $this->hash = uniqid('', true);
77
        $this->o_c = null;
78
        $this->y_s = null;
79
        $this->o_s = null;
80 16
    }
81
82 16
    /**
83
     * Get the node key
84
     *
85
     * @return int
86
     */
87
    public function getKey(): int
88
    {
89
        return $this->key;
90 1
    }
91
    
92 1
    /**
93
     * Get the node value
94
     *
95
     * @return mixed
96
     */
97
    public function getValue()
98
    {
99
        return $this->value;
100 1
    }
101
    
102 1
    /**
103 1
     * Set the node value
104
     *
105
     * @param mixed $value - node value
106
     */
107
    public function setValue($value): void
108
    {
109
        $this->value = $value;
110 6
    }
111
112 6
    /**
113 6
     * Decrease the node key
114 5
     *
115
     * @param int $newKey - new node key
116
     */
117
    public function decreaseKey(int $newKey): void
118
    {
119 7
        $heap = $this->getOwner();
120
        $heap->decreaseKey($this, $newKey);
121 7
    }
122 7
123 5
    /**
124
     * Delete the node
125
     */
126
    public function delete(): void
127
    {
128
        $heap = $this->getOwner();
129
        $heap->delete($this);
130 12
    }
131
132 12
    /**
133
     * Get the owner heap of the node
134 2
     *
135 2
     * @return PairingHeap
136 2
     */
137
    public function getOwner(): PairingHeap
138
    {
139 2
        if ($this->heap->getOther() != $this->heap) {
140 2
            // find root
141
            $root = $this->heap;
142
            while ($root != $root->getOther()) {
143
                $root = $root->getOther();
144
            }
145 2
            // path-compression
146
            $cur = $this->heap;
147 12
            while ($cur->getOther() != $root) {
148
                $next = $cur->getOther();
149
                $cur->setOther($root);
150
                $cur = $next;
151
            }
152
            $this->heap = $root;
153
        }
154
        return $this->heap;
155
    }
156
}
157