Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/gc/NonbranchyTree.java
41161 views
1
/*
2
* Copyright (c) 2003, 2020, 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.gc;
25
26
import java.io.*;
27
import java.util.*;
28
29
import nsk.share.test.ExecutionController;
30
import nsk.share.test.LocalRandom;
31
32
/**
33
* <tt>NonbranchyTree</tt> defines a tree structure. Each node of the tree
34
* always has one son. A node may have the second son with probability
35
* <tt>branchiness</tt>.
36
*/
37
public class NonbranchyTree {
38
39
/** Minimal size of each node (in bytes) */
40
public final static int MIN_NODE_SIZE = 20;
41
private Node root;
42
private int numberOfNodes;
43
private float branchiness;
44
private int size;
45
private ExecutionController controller;
46
47
/**
48
* Creates a new tree with number of nodes not more than
49
* <tt>numberOfNodes</tt>. The implementation uses recursion to build the
50
* tree, so if <tt>StackOverflowError</tt> or <tt>OutOfMemoryError</tt> is
51
* thrown, the recursion is stopped and the method finishes building of the
52
* tree. Each node consists of <tt>byte[]</tt> of length <tt>size</tt>.
53
*
54
* @param numberOfNodes maximum number of nodes for the tree.
55
* @param branchiness probability for each node to have the second son.
56
* @param size number of bytes to store in a node.
57
*
58
* @throws <i>IllegalArgumentException</i> if <tt>numberOfNodes</tt> is
59
* less than 1; or <tt>branchiness</tt> is greater than 1, or less
60
* or equal than 0; or <tt>size</tt> is less than 1.
61
*
62
*/
63
public NonbranchyTree(int numberOfNodes, float branchiness, int size) {
64
this(numberOfNodes, branchiness, size, null);
65
initTree();
66
}
67
68
public NonbranchyTree(int numberOfNodes, float branchiness, int size, ExecutionController controller) {
69
this.numberOfNodes = numberOfNodes;
70
this.branchiness = branchiness;
71
this.size = size;
72
this.controller = controller;
73
initTree();
74
}
75
76
private void initTree() {
77
if (numberOfNodes < 1) {
78
throw new IllegalArgumentException("Illegal number of nodes: "
79
+ numberOfNodes + ", must be at "
80
+ "least 1.");
81
}
82
if ( (branchiness >= 1) || (branchiness <= 0) ) {
83
throw new IllegalArgumentException("Illegal value of branchiness: "
84
+ numberOfNodes + ", must be at "
85
+ "greater than 0 and less than "
86
+ " 1.");
87
}
88
if (size < 1) {
89
throw new IllegalArgumentException("Illegal size of nodes: "
90
+ size + ", must be at least 1.");
91
}
92
// ensure that LocalRandom is loaded and has enough memory
93
LocalRandom.nextBoolean();
94
root = createTree(numberOfNodes, size);
95
}
96
97
// Create a new tree with specified number of nodes and size of each node
98
private Node createTree(int numberOfNodes, int size) {
99
// Make sure we respect the controller and stop test after
100
// given time.
101
if (controller != null && !controller.continueExecution()) {
102
return null;
103
}
104
105
Node node = new Node(size);
106
try {
107
if (numberOfNodes == 0) {
108
// No more nodes need to be built
109
return null;
110
} else if (numberOfNodes == 1) {
111
return node;
112
} else if (numberOfNodes == 2) {
113
node.left = createTree(1, size);
114
return node;
115
} else {
116
// Create a few nodes
117
if (makeRightNode()) {
118
// The node will have two sons
119
int leftNodes = 1 + LocalRandom.nextInt(numberOfNodes - 2);
120
int rightNodes = numberOfNodes - 1 - leftNodes;
121
122
node.left = createTree(leftNodes, size);
123
node.right = createTree(rightNodes, size);
124
} else {
125
// The node will have just one son
126
Node leftTree = createTree(numberOfNodes - 1, size);
127
node.left = leftTree;
128
}
129
return node;
130
} // if
131
} catch(StackOverflowError e) {
132
// No more memory for such long tree
133
return node;
134
} catch(OutOfMemoryError e) {
135
// No more memory for such long tree
136
return node;
137
} // try
138
} // createTree()
139
140
// Define the "branchiness" of the tree
141
private boolean makeRightNode() {
142
return (LocalRandom.nextFloat() < branchiness);
143
}
144
145
/**
146
* Bends the tree. A son of a leaf of the tree is set to the root node.
147
*
148
*/
149
public void bend() {
150
bend(root);
151
}
152
153
// Bend the tree: make a reference from a leat of the tree to the specified
154
// node
155
private void bend(Node markedNode) {
156
Node node = root;
157
158
while ( (node.left != null) || (node.right != null) )
159
node = node.left;
160
node.right = markedNode;
161
}
162
163
/**
164
* Prints the whole tree from the root to the defined PrintStream.
165
*
166
* @param out PrintStream to print the tree in
167
*
168
*/
169
public void print(PrintStream out) {
170
print(out, root);
171
}
172
173
// Print the sub-tree from the specified node and down
174
private void print(PrintStream out, Node node) {
175
node.print(out);
176
if (node.left != null)
177
print(out, node.left);
178
if (node.right != null)
179
print(out, node.right);
180
}
181
}
182
183
// The class defines a node of a tree
184
class Node {
185
Node left;
186
Node right;
187
byte[] core;
188
189
Node(int size) {
190
left = null;
191
right = null;
192
core = new byte[size];
193
194
// Initizlize the core array
195
for (int i = 0; i < size; i++)
196
core[i] = (byte) i;
197
}
198
199
// Print the node info
200
void print(PrintStream out) {
201
out.println("node = " + this + " (" + left + ", " + right + ")");
202
}
203
}
204
205