author | vromero |
Tue, 09 Sep 2014 10:43:06 -0700 | |
changeset 26537 | 026471c1a12b |
parent 25874 | 83c19f00452c |
child 29054 | 310b8028d7df |
permissions | -rw-r--r-- |
10 | 1 |
/* |
22689
570f79791f52
8028267: sun.tools.javac.util.Bits contains a main-method with test code
alundblad
parents:
19941
diff
changeset
|
2 |
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
14049
3207422a0f9b
7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
13844
diff
changeset
|
28 |
import java.util.Arrays; |
3207422a0f9b
7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
13844
diff
changeset
|
29 |
|
10 | 30 |
/** A class for extensible, mutable bit sets. |
31 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
32 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
33 |
* If you write code that depends on this, you do so at your own risk. |
10 | 34 |
* This code and its internal interfaces are subject to change or |
35 |
* deletion without notice.</b> |
|
36 |
*/ |
|
37 |
public class Bits { |
|
38 |
||
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
39 |
// ____________ reset _________ |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
40 |
// / UNKNOWN \ <-------- / UNINIT \ |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
41 |
// \____________/ | \_________/ |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
42 |
// | | | |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
43 |
// |assign | | any |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
44 |
// | ___________ | |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
45 |
// ------> / NORMAL \ <---- |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
46 |
// \___________/ | |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
47 |
// | | |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
48 |
// | | |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
49 |
// ----------- |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
50 |
// any |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
51 |
protected enum BitsState { |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
52 |
/* A Bits instance is in UNKNOWN state if it has been explicitly reset. |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
53 |
* It is possible to get to this state from any other by calling the |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
54 |
* reset method. An instance in the UNKNOWN state can pass to the |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
55 |
* NORMAL state after being assigned another Bits instance. |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
56 |
* |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
57 |
* Bits instances are final fields in Flow so the UNKNOWN state models |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
58 |
* the null assignment. |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
59 |
*/ |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
60 |
UNKNOWN, |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
61 |
/* A Bits instance is in UNINIT when it is created with the default |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
62 |
* constructor but it isn't explicitly reset. The main objective of this |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
63 |
* internal state is to save some memory. |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
64 |
*/ |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
65 |
UNINIT, |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
66 |
/* The normal state is reached after creating a Bits instance from an |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
67 |
* existing one or after applying any operation to an instance on UNINIT |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
68 |
* or NORMAL state. From this state a bits instance can pass to the |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
69 |
* UNKNOWN state by calling the reset method. |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
70 |
*/ |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
71 |
NORMAL; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
72 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
73 |
static BitsState getState(int[] someBits, boolean reset) { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
74 |
if (reset) { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
75 |
return UNKNOWN; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
76 |
} else { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
77 |
if (someBits != unassignedBits) { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
78 |
return NORMAL; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
79 |
} else { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
80 |
return UNINIT; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
81 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
82 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
83 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
84 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
85 |
} |
10 | 86 |
|
26537
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
87 |
public enum BitsOpKind { |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
88 |
INIT, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
89 |
CLEAR, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
90 |
INCL_BIT, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
91 |
EXCL_BIT, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
92 |
ASSIGN, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
93 |
AND_SET, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
94 |
OR_SET, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
95 |
DIFF_SET, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
96 |
XOR_SET, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
97 |
INCL_RANGE, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
98 |
EXCL_RANGE, |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
99 |
} |
026471c1a12b
8042347: javac, Gen.LVTAssignAnalyzer should be refactored, it shouldn't be a static class
vromero
parents:
25874
diff
changeset
|
100 |
|
10 | 101 |
private final static int wordlen = 32; |
102 |
private final static int wordshift = 5; |
|
103 |
private final static int wordmask = wordlen - 1; |
|
104 |
||
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
105 |
public int[] bits = null; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
106 |
// This field will store last version of bits after every change. |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
107 |
private static final int[] unassignedBits = new int[0]; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
108 |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
109 |
protected BitsState currentState; |
10 | 110 |
|
111 |
/** Construct an initially empty set. |
|
112 |
*/ |
|
113 |
public Bits() { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
114 |
this(false); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
115 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
116 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
117 |
public Bits(Bits someBits) { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
118 |
this(someBits.dup().bits, BitsState.getState(someBits.bits, false)); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
119 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
120 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
121 |
public Bits(boolean reset) { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
122 |
this(unassignedBits, BitsState.getState(unassignedBits, reset)); |
10 | 123 |
} |
124 |
||
125 |
/** Construct a set consisting initially of given bit vector. |
|
126 |
*/ |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
127 |
protected Bits(int[] bits, BitsState initState) { |
10 | 128 |
this.bits = bits; |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
129 |
this.currentState = initState; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
130 |
switch (initState) { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
131 |
case UNKNOWN: |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
132 |
this.bits = null; |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
133 |
break; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
134 |
case NORMAL: |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
135 |
Assert.check(bits != unassignedBits); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
136 |
break; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
137 |
} |
10 | 138 |
} |
139 |
||
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
140 |
protected void sizeTo(int len) { |
10 | 141 |
if (bits.length < len) { |
14049
3207422a0f9b
7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
13844
diff
changeset
|
142 |
bits = Arrays.copyOf(bits, len); |
10 | 143 |
} |
144 |
} |
|
145 |
||
146 |
/** This set = {}. |
|
147 |
*/ |
|
148 |
public void clear() { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
149 |
Assert.check(currentState != BitsState.UNKNOWN); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
150 |
for (int i = 0; i < bits.length; i++) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
151 |
bits[i] = 0; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
152 |
} |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
153 |
currentState = BitsState.NORMAL; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
154 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
155 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
156 |
public void reset() { |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
157 |
internalReset(); |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
158 |
} |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
159 |
|
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
160 |
protected void internalReset() { |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
161 |
bits = null; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
162 |
currentState = BitsState.UNKNOWN; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
163 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
164 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
165 |
public boolean isReset() { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
166 |
return currentState == BitsState.UNKNOWN; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
167 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
168 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
169 |
public Bits assign(Bits someBits) { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
170 |
bits = someBits.dup().bits; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
171 |
currentState = BitsState.NORMAL; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
172 |
return this; |
10 | 173 |
} |
174 |
||
175 |
/** Return a copy of this set. |
|
176 |
*/ |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
177 |
public Bits dup() { |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
178 |
Assert.check(currentState != BitsState.UNKNOWN); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
179 |
Bits tmp = new Bits(); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
180 |
tmp.bits = dupBits(); |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
181 |
currentState = BitsState.NORMAL; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
182 |
return tmp; |
10 | 183 |
} |
184 |
||
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
185 |
protected int[] dupBits() { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
186 |
int [] result; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
187 |
if (currentState != BitsState.NORMAL) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
188 |
result = bits; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
189 |
} else { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
190 |
result = new int[bits.length]; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
191 |
System.arraycopy(bits, 0, result, 0, bits.length); |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
192 |
} |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
193 |
return result; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
194 |
} |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
195 |
|
10 | 196 |
/** Include x in this set. |
197 |
*/ |
|
198 |
public void incl(int x) { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
199 |
Assert.check(currentState != BitsState.UNKNOWN); |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
22689
diff
changeset
|
200 |
Assert.check(x >= 0); |
10 | 201 |
sizeTo((x >>> wordshift) + 1); |
202 |
bits[x >>> wordshift] = bits[x >>> wordshift] | |
|
203 |
(1 << (x & wordmask)); |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
204 |
currentState = BitsState.NORMAL; |
10 | 205 |
} |
206 |
||
207 |
||
208 |
/** Include [start..limit) in this set. |
|
209 |
*/ |
|
210 |
public void inclRange(int start, int limit) { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
211 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 212 |
sizeTo((limit >>> wordshift) + 1); |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
213 |
for (int x = start; x < limit; x++) { |
10 | 214 |
bits[x >>> wordshift] = bits[x >>> wordshift] | |
215 |
(1 << (x & wordmask)); |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
216 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
217 |
currentState = BitsState.NORMAL; |
10 | 218 |
} |
219 |
||
7621
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
220 |
/** Exclude [start...end] from this set. |
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
221 |
*/ |
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
222 |
public void excludeFrom(int start) { |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
223 |
Assert.check(currentState != BitsState.UNKNOWN); |
7621
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
224 |
Bits temp = new Bits(); |
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
225 |
temp.sizeTo(bits.length); |
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
226 |
temp.inclRange(0, start); |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
227 |
internalAndSet(temp); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
228 |
currentState = BitsState.NORMAL; |
7621
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
229 |
} |
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
230 |
|
10 | 231 |
/** Exclude x from this set. |
232 |
*/ |
|
233 |
public void excl(int x) { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
234 |
Assert.check(currentState != BitsState.UNKNOWN); |
8032 | 235 |
Assert.check(x >= 0); |
10 | 236 |
sizeTo((x >>> wordshift) + 1); |
237 |
bits[x >>> wordshift] = bits[x >>> wordshift] & |
|
238 |
~(1 << (x & wordmask)); |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
239 |
currentState = BitsState.NORMAL; |
10 | 240 |
} |
241 |
||
242 |
/** Is x an element of this set? |
|
243 |
*/ |
|
244 |
public boolean isMember(int x) { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
245 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 246 |
return |
247 |
0 <= x && x < (bits.length << wordshift) && |
|
248 |
(bits[x >>> wordshift] & (1 << (x & wordmask))) != 0; |
|
249 |
} |
|
250 |
||
13844 | 251 |
/** {@literal this set = this set & xs}. |
10 | 252 |
*/ |
253 |
public Bits andSet(Bits xs) { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
254 |
Assert.check(currentState != BitsState.UNKNOWN); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
255 |
internalAndSet(xs); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
256 |
currentState = BitsState.NORMAL; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
257 |
return this; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
258 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
259 |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
260 |
protected void internalAndSet(Bits xs) { |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
261 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 262 |
sizeTo(xs.bits.length); |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
263 |
for (int i = 0; i < xs.bits.length; i++) { |
10 | 264 |
bits[i] = bits[i] & xs.bits[i]; |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
265 |
} |
10 | 266 |
} |
267 |
||
268 |
/** this set = this set | xs. |
|
269 |
*/ |
|
270 |
public Bits orSet(Bits xs) { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
271 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 272 |
sizeTo(xs.bits.length); |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
273 |
for (int i = 0; i < xs.bits.length; i++) { |
10 | 274 |
bits[i] = bits[i] | xs.bits[i]; |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
275 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
276 |
currentState = BitsState.NORMAL; |
10 | 277 |
return this; |
278 |
} |
|
279 |
||
280 |
/** this set = this set \ xs. |
|
281 |
*/ |
|
282 |
public Bits diffSet(Bits xs) { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
283 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 284 |
for (int i = 0; i < bits.length; i++) { |
285 |
if (i < xs.bits.length) { |
|
286 |
bits[i] = bits[i] & ~xs.bits[i]; |
|
287 |
} |
|
288 |
} |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
289 |
currentState = BitsState.NORMAL; |
10 | 290 |
return this; |
291 |
} |
|
292 |
||
293 |
/** this set = this set ^ xs. |
|
294 |
*/ |
|
295 |
public Bits xorSet(Bits xs) { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
296 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 297 |
sizeTo(xs.bits.length); |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
298 |
for (int i = 0; i < xs.bits.length; i++) { |
10 | 299 |
bits[i] = bits[i] ^ xs.bits[i]; |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
300 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
301 |
currentState = BitsState.NORMAL; |
10 | 302 |
return this; |
303 |
} |
|
304 |
||
305 |
/** Count trailing zero bits in an int. Algorithm from "Hacker's |
|
306 |
* Delight" by Henry S. Warren Jr. (figure 5-13) |
|
307 |
*/ |
|
308 |
private static int trailingZeroBits(int x) { |
|
8032 | 309 |
Assert.check(wordlen == 32); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
310 |
if (x == 0) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
311 |
return 32; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
312 |
} |
10 | 313 |
int n = 1; |
314 |
if ((x & 0xffff) == 0) { n += 16; x >>>= 16; } |
|
315 |
if ((x & 0x00ff) == 0) { n += 8; x >>>= 8; } |
|
316 |
if ((x & 0x000f) == 0) { n += 4; x >>>= 4; } |
|
317 |
if ((x & 0x0003) == 0) { n += 2; x >>>= 2; } |
|
318 |
return n - (x&1); |
|
319 |
} |
|
320 |
||
13844 | 321 |
/** Return the index of the least bit position ≥ x that is set. |
10 | 322 |
* If none are set, returns -1. This provides a nice way to iterate |
323 |
* over the members of a bit set: |
|
13844 | 324 |
* <pre>{@code |
10 | 325 |
* for (int i = bits.nextBit(0); i>=0; i = bits.nextBit(i+1)) ... |
13844 | 326 |
* }</pre> |
10 | 327 |
*/ |
328 |
public int nextBit(int x) { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
329 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 330 |
int windex = x >>> wordshift; |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
331 |
if (windex >= bits.length) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
332 |
return -1; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
333 |
} |
10 | 334 |
int word = bits[windex] & ~((1 << (x & wordmask))-1); |
335 |
while (true) { |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
336 |
if (word != 0) { |
10 | 337 |
return (windex << wordshift) + trailingZeroBits(word); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
338 |
} |
10 | 339 |
windex++; |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
340 |
if (windex >= bits.length) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
341 |
return -1; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
342 |
} |
10 | 343 |
word = bits[windex]; |
344 |
} |
|
345 |
} |
|
346 |
||
347 |
/** a string representation of this set. |
|
348 |
*/ |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
349 |
@Override |
10 | 350 |
public String toString() { |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
351 |
if (bits != null && bits.length > 0) { |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
352 |
char[] digits = new char[bits.length * wordlen]; |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
353 |
for (int i = 0; i < bits.length * wordlen; i++) { |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
354 |
digits[i] = isMember(i) ? '1' : '0'; |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
355 |
} |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
356 |
return new String(digits); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
357 |
} else { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
358 |
return "[]"; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
359 |
} |
10 | 360 |
} |
361 |
||
362 |
} |