Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/TreeNodesDenotation.java
40948 views
1
/*
2
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package nsk.share;
25
26
import java.util.*;
27
28
/**
29
* This denotation provides naming and indexing for nodes
30
* of a binary, or ternary, or <tt>n</tt>-ary tree.
31
*
32
* <p>Here, <tt>n</tt> would be the length of a symbols
33
* string used as an alphabeth for nodes naming. For a
34
* binary tree, <tt>n=2</tt>, and an aplhabeth could be
35
* the <tt>"LR"</tt> string. This implies the following
36
* naming for tree nodes:
37
* <pre>
38
* (empty)
39
* / \
40
* L R
41
* / \ / \
42
* LL LR RL RR
43
* / \ / \ / \ / \
44
* </pre>
45
*
46
* <p>Anyway, the tree root node is named with the empty
47
* string <tt>""</tt> and is indexed with 2-zeroes array
48
* <tt>{0,0}</tt>.
49
*
50
* <p>Index for a tree node is 2-elements <tt>int[]</tt>
51
* array. The 1st element is the node's level in a tree.
52
* The 2nd element is the item's number among all nodes of
53
* that level; provided that node items are enumerated from
54
* <tt>0</tt> to <tt>n</tt><sup>level</sup><tt>-1</tt>.
55
* Given a level, lexicographic order is assumed for the
56
* nodes of the same level.
57
*
58
* <p>For example: given the above sample tree, the node
59
* <tt>"L"</tt> has the index <tt>{1,0}</tt>, while the
60
* node <tt>"RL"</tt> has the index <tt>{2,2}</tt>.
61
*
62
* <p>In general case, ordering of characters used for nodes
63
* naming is implied by the given alphabeth. This may differ
64
* from the ``natural'' ordering. For example, if alphabeth
65
* is <tt>"ZYX...CBA"</tt>, then ordering for nodes would be
66
* opposite to ``natural''.
67
*/
68
public class TreeNodesDenotation extends Denotation {
69
/**
70
* Symbols to denote tree nodes.
71
*
72
* @see #TreeNodeDenotation(String)
73
*/
74
private String alphabeth;
75
76
/**
77
* Standard denotation for a binary tree; alphabeth
78
* is <tt>"LR"</tt>.
79
*
80
* @see #TreeNodesDenotation(String)
81
*/
82
public TreeNodesDenotation() {
83
this("LR");
84
}
85
86
/**
87
* Denotation for nodes of a tree.
88
*
89
* <p>Each tree node is marked with a string of symbols
90
* from the given <tt>alphabeth</tt>. A string length
91
* equals to the node's level. The root node is always
92
* denoted with the empty string.
93
*
94
* <p>For example, an <tt>alphabeth</tt> for a binary
95
* tree could be <tt>"LR"</tt>, or <tt>"01"</tt>, or
96
* any 2-symbols string. However, <tt>"lL"</tt> or
97
* <tt>"rR"</tt> would be illegal because of collision
98
* between upper- and lower- case letters.
99
*
100
* <p>In general case, it is illegal for <tt>alphabeth</tt>
101
* to contain two or several copies of the same symbol.
102
* This constructor deems lower- and upper-case variants
103
* of the same letter are the same symbol.
104
*
105
* @throws IllegalArgumentException If the <tt>alphabeth</tt>
106
* looks illegal.
107
*/
108
public TreeNodesDenotation(String alphabeth) {
109
if (alphabeth.length() == 0)
110
throw new IllegalArgumentException("empty alphabeth");
111
// Check for lower- to upper- case collision:
112
this.alphabeth = alphabeth.toUpperCase();
113
int length = this.alphabeth.length();
114
Set<Character> pool = new HashSet<Character>(); // still empty
115
for (int i=0; i<length; i++)
116
pool.add(Character.valueOf(this.alphabeth.charAt(i)));
117
if (pool.size() != length)
118
throw new IllegalArgumentException("collision: " + alphabeth);
119
}
120
121
/**
122
* Check if the <tt>name</tt> is legal, and return the
123
* numeric index for the tree node denoted by the given
124
* <tt>name</tt>.
125
*
126
* @throws IllegalArgumentException If the <tt>name</tt>
127
* is illegal.
128
*/
129
public int[] indexFor(String name) {
130
int level = name.length();
131
int factor = alphabeth.length();
132
long item = 0;
133
for (int i=0; i<level; i++) {
134
char symbol = Character.toUpperCase(name.charAt(i));
135
int position = alphabeth.indexOf(symbol);
136
if (position < 0)
137
throw new IllegalArgumentException("unknown symbol: " + name);
138
item = item*factor + position;
139
if (item < 0 || item > Integer.MAX_VALUE)
140
throw new IllegalArgumentException("too long name: " + name);
141
};
142
int[] index = new int [] { level, (int)item };
143
return index;
144
}
145
146
/**
147
* Check if the <tt>index[]</tt> is legal, and return
148
* a symbolic name for the tree node denoted by the
149
* given <tt>index[]</tt>.
150
*
151
* @throws IllegalArgumentException If the <tt>index[]</tt>
152
* is illegal.
153
*/
154
public String nameFor(int[] index) {
155
if (index.length != 2)
156
throw new IllegalArgumentException(
157
"index dimention: " + index.length);
158
StringBuffer name = new StringBuffer(); // still empty
159
int level = index[0];
160
int item = index[1];
161
if (level < 0 || item < 0)
162
throw new IllegalArgumentException(
163
"negative index: " + level + ", " + item);
164
int factor = alphabeth.length();
165
for (int i=0; i<level; i++) {
166
int k = item % factor;
167
name.append(alphabeth.charAt(k));
168
item = item / factor;
169
};
170
if (item != 0)
171
throw new IllegalArgumentException(
172
"out of range: {"+ index[0] + "," + index[1] + "}");
173
return new String(name.reverse());
174
}
175
}
176
177