annotate agent/src/share/classes/sun/jvm/hotspot/utilities/IntervalTree.java @ 12226:7944aba7ba41

8015107: NPG: Use consistent naming for metaspace concepts Reviewed-by: coleenp, mgerdin, hseigel
author ehelin
date Mon, 12 Aug 2013 17:37:02 +0200
parents c18cbe5936b8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.utilities;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 /** Derived from the example in Section 15.3 of CLR. */
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import java.io.PrintStream;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import java.util.ArrayList;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import java.util.Comparator;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import java.util.List;
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 public class IntervalTree extends RBTree {
a61af66fc99e Initial load
duke
parents:
diff changeset
35 private Comparator endpointComparator;
a61af66fc99e Initial load
duke
parents:
diff changeset
36
a61af66fc99e Initial load
duke
parents:
diff changeset
37 /** This constructor takes only one comparator: one which operates
a61af66fc99e Initial load
duke
parents:
diff changeset
38 upon the endpoints of the Intervals this tree will store. It
a61af66fc99e Initial load
duke
parents:
diff changeset
39 constructs an internal "interval comparator" out of this one. */
a61af66fc99e Initial load
duke
parents:
diff changeset
40 public IntervalTree(Comparator endpointComparator) {
a61af66fc99e Initial load
duke
parents:
diff changeset
41 super(new IntervalComparator(endpointComparator));
a61af66fc99e Initial load
duke
parents:
diff changeset
42 this.endpointComparator = endpointComparator;
a61af66fc99e Initial load
duke
parents:
diff changeset
43 }
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 public void insert(Interval interval, Object data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
46 IntervalNode node = new IntervalNode(interval, endpointComparator, data);
a61af66fc99e Initial load
duke
parents:
diff changeset
47 insertNode(node);
a61af66fc99e Initial load
duke
parents:
diff changeset
48 }
a61af66fc99e Initial load
duke
parents:
diff changeset
49
a61af66fc99e Initial load
duke
parents:
diff changeset
50 /** Returns a List<IntervalNode> indicating which nodes'
a61af66fc99e Initial load
duke
parents:
diff changeset
51 intervals were intersected by the given query interval. It is
a61af66fc99e Initial load
duke
parents:
diff changeset
52 guaranteed that these nodes will be returned sorted by
a61af66fc99e Initial load
duke
parents:
diff changeset
53 increasing low endpoint. */
a61af66fc99e Initial load
duke
parents:
diff changeset
54 public List findAllNodesIntersecting(Interval interval) {
a61af66fc99e Initial load
duke
parents:
diff changeset
55 List retList = new ArrayList();
a61af66fc99e Initial load
duke
parents:
diff changeset
56 searchForIntersectingNodesFrom((IntervalNode) getRoot(), interval, retList);
a61af66fc99e Initial load
duke
parents:
diff changeset
57 return retList;
a61af66fc99e Initial load
duke
parents:
diff changeset
58 }
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 public void print() {
a61af66fc99e Initial load
duke
parents:
diff changeset
61 printOn(System.out);
a61af66fc99e Initial load
duke
parents:
diff changeset
62 }
a61af66fc99e Initial load
duke
parents:
diff changeset
63
a61af66fc99e Initial load
duke
parents:
diff changeset
64 public void printOn(PrintStream tty) {
a61af66fc99e Initial load
duke
parents:
diff changeset
65 printFromNode(getRoot(), tty, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
66 }
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 //----------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
69 // Overridden internal functionality
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 protected Object getNodeValue(RBNode node) {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 return ((IntervalNode) node).getInterval();
a61af66fc99e Initial load
duke
parents:
diff changeset
73 }
a61af66fc99e Initial load
duke
parents:
diff changeset
74
a61af66fc99e Initial load
duke
parents:
diff changeset
75 protected void verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
76 super.verify();
a61af66fc99e Initial load
duke
parents:
diff changeset
77 verifyFromNode(getRoot());
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 //----------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
81 // Internals only below this point
a61af66fc99e Initial load
duke
parents:
diff changeset
82 //
a61af66fc99e Initial load
duke
parents:
diff changeset
83
a61af66fc99e Initial load
duke
parents:
diff changeset
84 private void verifyFromNode(RBNode node) {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 if (node == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 // We already know that the red/black structure has been verified.
a61af66fc99e Initial load
duke
parents:
diff changeset
90 // What we need to find out is whether this node has been updated
a61af66fc99e Initial load
duke
parents:
diff changeset
91 // properly -- i.e., whether its notion of the maximum endpoint is
a61af66fc99e Initial load
duke
parents:
diff changeset
92 // correct.
a61af66fc99e Initial load
duke
parents:
diff changeset
93 IntervalNode intNode = (IntervalNode) node;
a61af66fc99e Initial load
duke
parents:
diff changeset
94 if (!intNode.getMaxEndpoint().equals(intNode.computeMaxEndpoint())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 if (DEBUGGING && VERBOSE) {
a61af66fc99e Initial load
duke
parents:
diff changeset
96 print();
a61af66fc99e Initial load
duke
parents:
diff changeset
97 }
a61af66fc99e Initial load
duke
parents:
diff changeset
98 throw new RuntimeException("Node's max endpoint was not updated properly");
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100 if (!intNode.getMinEndpoint().equals(intNode.computeMinEndpoint())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
101 if (DEBUGGING && VERBOSE) {
a61af66fc99e Initial load
duke
parents:
diff changeset
102 print();
a61af66fc99e Initial load
duke
parents:
diff changeset
103 }
a61af66fc99e Initial load
duke
parents:
diff changeset
104 throw new RuntimeException("Node's min endpoint was not updated properly");
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106
a61af66fc99e Initial load
duke
parents:
diff changeset
107 verifyFromNode(node.getLeft());
a61af66fc99e Initial load
duke
parents:
diff changeset
108 verifyFromNode(node.getRight());
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 static class IntervalComparator implements Comparator {
a61af66fc99e Initial load
duke
parents:
diff changeset
112 private Comparator endpointComparator;
a61af66fc99e Initial load
duke
parents:
diff changeset
113
a61af66fc99e Initial load
duke
parents:
diff changeset
114 public IntervalComparator(Comparator endpointComparator) {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 this.endpointComparator = endpointComparator;
a61af66fc99e Initial load
duke
parents:
diff changeset
116 }
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 public int compare(Object o1, Object o2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 Interval i1 = (Interval) o1;
a61af66fc99e Initial load
duke
parents:
diff changeset
120 Interval i2 = (Interval) o2;
a61af66fc99e Initial load
duke
parents:
diff changeset
121 return endpointComparator.compare(i1.getLowEndpoint(), i2.getLowEndpoint());
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123 }
a61af66fc99e Initial load
duke
parents:
diff changeset
124
a61af66fc99e Initial load
duke
parents:
diff changeset
125 private void searchForIntersectingNodesFrom(IntervalNode node,
a61af66fc99e Initial load
duke
parents:
diff changeset
126 Interval interval,
a61af66fc99e Initial load
duke
parents:
diff changeset
127 List resultList) {
a61af66fc99e Initial load
duke
parents:
diff changeset
128 if (node == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
130 }
a61af66fc99e Initial load
duke
parents:
diff changeset
131
a61af66fc99e Initial load
duke
parents:
diff changeset
132 // Inorder traversal (very important to guarantee sorted order)
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134 // Check to see whether we have to traverse the left subtree
a61af66fc99e Initial load
duke
parents:
diff changeset
135 IntervalNode left = (IntervalNode) node.getLeft();
a61af66fc99e Initial load
duke
parents:
diff changeset
136 if ((left != null) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
137 (endpointComparator.compare(left.getMaxEndpoint(),
a61af66fc99e Initial load
duke
parents:
diff changeset
138 interval.getLowEndpoint()) > 0)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 searchForIntersectingNodesFrom(left, interval, resultList);
a61af66fc99e Initial load
duke
parents:
diff changeset
140 }
a61af66fc99e Initial load
duke
parents:
diff changeset
141
a61af66fc99e Initial load
duke
parents:
diff changeset
142 // Check for intersection with current node
a61af66fc99e Initial load
duke
parents:
diff changeset
143 if (node.getInterval().overlaps(interval, endpointComparator)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 resultList.add(node);
a61af66fc99e Initial load
duke
parents:
diff changeset
145 }
a61af66fc99e Initial load
duke
parents:
diff changeset
146
a61af66fc99e Initial load
duke
parents:
diff changeset
147 // Check to see whether we have to traverse the left subtree
a61af66fc99e Initial load
duke
parents:
diff changeset
148 IntervalNode right = (IntervalNode) node.getRight();
a61af66fc99e Initial load
duke
parents:
diff changeset
149 if ((right != null) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
150 (endpointComparator.compare(interval.getHighEndpoint(),
a61af66fc99e Initial load
duke
parents:
diff changeset
151 right.getMinEndpoint()) > 0)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
152 searchForIntersectingNodesFrom(right, interval, resultList);
a61af66fc99e Initial load
duke
parents:
diff changeset
153 }
a61af66fc99e Initial load
duke
parents:
diff changeset
154 }
a61af66fc99e Initial load
duke
parents:
diff changeset
155
a61af66fc99e Initial load
duke
parents:
diff changeset
156 /** Debugging */
a61af66fc99e Initial load
duke
parents:
diff changeset
157 private void printFromNode(RBNode node, PrintStream tty, int indentDepth) {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 for (int i = 0; i < indentDepth; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 tty.print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
160 }
a61af66fc99e Initial load
duke
parents:
diff changeset
161
a61af66fc99e Initial load
duke
parents:
diff changeset
162 tty.print("-");
a61af66fc99e Initial load
duke
parents:
diff changeset
163 if (node == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
164 tty.println();
a61af66fc99e Initial load
duke
parents:
diff changeset
165 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
166 }
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168 tty.println(" " + node +
a61af66fc99e Initial load
duke
parents:
diff changeset
169 " (min " + ((IntervalNode) node).getMinEndpoint() +
a61af66fc99e Initial load
duke
parents:
diff changeset
170 ", max " + ((IntervalNode) node).getMaxEndpoint() + ")" +
a61af66fc99e Initial load
duke
parents:
diff changeset
171 ((node.getColor() == RBColor.RED) ? " (red)" : " (black)"));
a61af66fc99e Initial load
duke
parents:
diff changeset
172 if (node.getLeft() != null) printFromNode(node.getLeft(), tty, indentDepth + 2);
a61af66fc99e Initial load
duke
parents:
diff changeset
173 if (node.getRight() != null) printFromNode(node.getRight(), tty, indentDepth + 2);
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175 }