MtasAVLTree.java.html
11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasAVLTree.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> > <a href="index.source.html" class="el_package">mtas.codec.tree</a> > <span class="el_source">MtasAVLTree.java</span></div><h1>MtasAVLTree.java</h1><pre class="source lang-java linenums">package mtas.codec.tree;
import java.util.HashMap;
import mtas.codec.tree.MtasTree;
import mtas.codec.tree.MtasAVLTreeNode;
/**
* The Class MtasAVLTree.
*/
public class MtasAVLTree extends MtasTree<MtasAVLTreeNode> {
/** The index. */
private final HashMap<String, MtasAVLTreeNode> index;
/**
* Instantiates a new mtas AVL tree.
*
* @param singlePoint the single point
* @param storePrefixId the store prefix id
*/
public MtasAVLTree(boolean singlePoint, boolean storePrefixId) {
<span class="nc" id="L22"> super(singlePoint, storePrefixId);</span>
<span class="nc" id="L23"> index = new HashMap<>();</span>
<span class="nc" id="L24"> }</span>
/*
* (non-Javadoc)
*
* @see mtas.codec.tree.MtasTree#addTokenRangeEmpty(int, int)
*/
@Override
final protected void addRangeEmpty(int left, int right, int additionalId,
long additionalRef) {
<span class="nc" id="L34"> String key = left + "_" + right;</span>
<span class="nc bnc" id="L35" title="All 2 branches missed."> if (index.containsKey(key)) {</span>
// do nothing (empty...)
} else {
<span class="nc" id="L38"> addRange(left, right, additionalId, additionalRef, null, null);</span>
}
<span class="nc" id="L40"> }</span>
/*
* (non-Javadoc)
*
* @see mtas.codec.tree.MtasTree#addTokenSinglePoint(int, java.lang.Integer,
* java.lang.Long)
*/
@Override
final protected void addSinglePoint(int position, int additionalId,
long additionalRef, Integer id, Long ref) {
<span class="nc" id="L51"> addRange(position, position, additionalId, additionalRef, id, ref);</span>
<span class="nc" id="L52"> }</span>
/*
* (non-Javadoc)
*
* @see mtas.codec.tree.MtasTree#addTokenRange(int, int, java.lang.Integer,
* java.lang.Long)
*/
@Override
final protected void addRange(int left, int right, int additionalId,
long additionalRef, Integer id, Long ref) {
<span class="nc" id="L63"> String key = left + "_" + right;</span>
<span class="nc bnc" id="L64" title="All 2 branches missed."> if (index.containsKey(key)) {</span>
<span class="nc" id="L65"> index.get(key).addIdAndRef(id, ref, additionalId, additionalRef);</span>
<span class="nc" id="L66"> return;</span>
}
<span class="nc bnc" id="L68" title="All 2 branches missed."> if (root == null) {</span>
<span class="nc" id="L69"> root = new MtasAVLTreeNode(left, right, null);</span>
<span class="nc" id="L70"> root.addIdAndRef(id, ref, additionalId, additionalRef);</span>
<span class="nc" id="L71"> index.put(key, root);</span>
} else {
<span class="nc" id="L73"> MtasAVLTreeNode n = root;</span>
MtasAVLTreeNode parent;
while (true) {
<span class="nc" id="L76"> parent = n;</span>
<span class="nc bnc" id="L77" title="All 2 branches missed."> boolean goLeft = n.left > left;</span>
<span class="nc bnc" id="L78" title="All 2 branches missed."> n = goLeft ? n.leftChild : n.rightChild;</span>
<span class="nc bnc" id="L79" title="All 2 branches missed."> if (n == null) {</span>
<span class="nc bnc" id="L80" title="All 2 branches missed."> if (goLeft) {</span>
<span class="nc" id="L81"> parent.leftChild = new MtasAVLTreeNode(left, right, parent);</span>
<span class="nc" id="L82"> updateMax(parent, parent.leftChild.max);</span>
<span class="nc" id="L83"> parent.leftChild.addIdAndRef(id, ref, additionalId, additionalRef);</span>
<span class="nc" id="L84"> index.put(key, parent.leftChild);</span>
} else {
<span class="nc" id="L86"> parent.rightChild = new MtasAVLTreeNode(left, right, parent);</span>
<span class="nc" id="L87"> updateMax(parent, parent.rightChild.max);</span>
<span class="nc" id="L88"> parent.rightChild.addIdAndRef(id, ref, additionalId, additionalRef);</span>
<span class="nc" id="L89"> index.put(key, parent.rightChild);</span>
}
<span class="nc" id="L91"> rebalance(parent);</span>
<span class="nc" id="L92"> break;</span>
}
<span class="nc" id="L94"> }</span>
}
<span class="nc" id="L97"> }</span>
/**
* Update max.
*
* @param n the n
* @param max the max
*/
private void updateMax(MtasAVLTreeNode n, int max) {
<span class="nc bnc" id="L106" title="All 2 branches missed."> if (n != null) {</span>
<span class="nc bnc" id="L107" title="All 2 branches missed."> if (n.max < max) {</span>
<span class="nc" id="L108"> n.max = max;</span>
<span class="nc" id="L109"> updateMax(n.parent, max);</span>
}
}
<span class="nc" id="L112"> }</span>
/**
* Rebalance.
*
* @param n the n
*/
private void rebalance(MtasAVLTreeNode n) {
<span class="nc" id="L120"> MtasAVLTreeNode localN = n;</span>
<span class="nc" id="L121"> setBalance(localN);</span>
<span class="nc bnc" id="L122" title="All 2 branches missed."> if (localN.balance == -2) {</span>
<span class="nc bnc" id="L123" title="All 2 branches missed."> if (height(localN.leftChild.leftChild) >= height(localN.leftChild.rightChild)) {</span>
<span class="nc" id="L124"> localN = rotateRight(localN);</span>
} else {
<span class="nc" id="L126"> localN = rotateLeftThenRight(localN);</span>
}
<span class="nc bnc" id="L128" title="All 2 branches missed."> } else if (localN.balance == 2) {</span>
<span class="nc bnc" id="L129" title="All 2 branches missed."> if (height(localN.rightChild.rightChild) >= height(localN.rightChild.leftChild)) {</span>
<span class="nc" id="L130"> localN = rotateLeft(localN);</span>
} else {
<span class="nc" id="L132"> localN = rotateRightThenLeft(localN);</span>
}
}
<span class="nc bnc" id="L135" title="All 2 branches missed."> if (localN.parent != null) {</span>
<span class="nc" id="L136"> rebalance(localN.parent);</span>
} else {
<span class="nc" id="L138"> root = localN;</span>
}
<span class="nc" id="L140"> }</span>
/**
* Rotate left.
*
* @param a the a
* @return the mtas AVL tree node
*/
private MtasAVLTreeNode rotateLeft(MtasAVLTreeNode a) {
<span class="nc" id="L149"> MtasAVLTreeNode b = a.rightChild;</span>
<span class="nc" id="L150"> b.parent = a.parent;</span>
<span class="nc" id="L151"> a.rightChild = b.leftChild;</span>
<span class="nc bnc" id="L152" title="All 2 branches missed."> if (a.rightChild != null) {</span>
<span class="nc" id="L153"> a.rightChild.parent = a;</span>
}
<span class="nc" id="L155"> b.leftChild = a;</span>
<span class="nc" id="L156"> a.parent = b;</span>
<span class="nc bnc" id="L157" title="All 2 branches missed."> if (b.parent != null) {</span>
<span class="nc bnc" id="L158" title="All 4 branches missed."> if ((b.parent.rightChild != null) && b.parent.rightChild.equals(a)) {</span>
<span class="nc" id="L159"> b.parent.rightChild = b;</span>
} else {
<span class="nc" id="L161"> b.parent.leftChild = b;</span>
}
}
<span class="nc" id="L164"> setMax(a);</span>
<span class="nc" id="L165"> setMax(b);</span>
<span class="nc" id="L166"> setBalance(a, b);</span>
<span class="nc" id="L167"> return b;</span>
}
/**
* Rotate right.
*
* @param a the a
* @return the mtas AVL tree node
*/
private MtasAVLTreeNode rotateRight(MtasAVLTreeNode a) {
<span class="nc" id="L177"> MtasAVLTreeNode b = a.leftChild;</span>
<span class="nc" id="L178"> b.parent = a.parent;</span>
<span class="nc" id="L179"> a.leftChild = b.rightChild;</span>
<span class="nc bnc" id="L180" title="All 2 branches missed."> if (a.leftChild != null) {</span>
<span class="nc" id="L181"> a.leftChild.parent = a;</span>
}
<span class="nc" id="L183"> b.rightChild = a;</span>
<span class="nc" id="L184"> a.parent = b;</span>
<span class="nc bnc" id="L185" title="All 2 branches missed."> if (b.parent != null) {</span>
<span class="nc bnc" id="L186" title="All 4 branches missed."> if ((b.parent.rightChild != null) && b.parent.rightChild.equals(a)) {</span>
<span class="nc" id="L187"> b.parent.rightChild = b;</span>
} else {
<span class="nc" id="L189"> b.parent.leftChild = b;</span>
}
}
<span class="nc" id="L192"> setMax(a);</span>
<span class="nc" id="L193"> setMax(b);</span>
<span class="nc" id="L194"> setBalance(a, b);</span>
<span class="nc" id="L195"> return b;</span>
}
/**
* Rotate left then right.
*
* @param n the n
* @return the mtas AVL tree node
*/
private MtasAVLTreeNode rotateLeftThenRight(MtasAVLTreeNode n) {
<span class="nc" id="L205"> n.leftChild = rotateLeft(n.leftChild);</span>
<span class="nc" id="L206"> return rotateRight(n);</span>
}
/**
* Rotate right then left.
*
* @param n the n
* @return the mtas AVL tree node
*/
private MtasAVLTreeNode rotateRightThenLeft(MtasAVLTreeNode n) {
<span class="nc" id="L216"> n.rightChild = rotateRight(n.rightChild);</span>
<span class="nc" id="L217"> return rotateLeft(n);</span>
}
/**
* Height.
*
* @param n the n
* @return the int
*/
private int height(MtasAVLTreeNode n) {
<span class="nc bnc" id="L227" title="All 2 branches missed."> if (n == null) {</span>
<span class="nc" id="L228"> return -1;</span>
} else {
<span class="nc" id="L230"> return 1 + Math.max(height(n.leftChild), height(n.rightChild));</span>
}
}
/**
* Sets the balance.
*
* @param nodes the new balance
*/
private void setBalance(MtasAVLTreeNode... nodes) {
<span class="nc bnc" id="L240" title="All 2 branches missed."> for (MtasAVLTreeNode n : nodes) {</span>
<span class="nc" id="L241"> n.balance = height(n.rightChild) - height(n.leftChild);</span>
}
<span class="nc" id="L243"> }</span>
/**
* Sets the max.
*
* @param n the new max
*/
private void setMax(MtasAVLTreeNode n) {
<span class="nc" id="L251"> n.max = n.right;</span>
<span class="nc bnc" id="L252" title="All 2 branches missed."> if (n.leftChild != null) {</span>
<span class="nc" id="L253"> n.max = Math.max(n.max, n.leftChild.max);</span>
}
<span class="nc bnc" id="L255" title="All 2 branches missed."> if (n.rightChild != null) {</span>
<span class="nc" id="L256"> n.max = Math.max(n.max, n.rightChild.max);</span>
}
<span class="nc" id="L258"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>