author | vromero |
Sat, 14 Sep 2013 19:04:47 +0100 | |
changeset 19941 | 8b91e8eb2d20 |
parent 17282 | c6964ad7df63 |
child 22689 | 570f79791f52 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
2 |
* Copyright (c) 1999, 2013, 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 |
|
87 |
private final static int wordlen = 32; |
|
88 |
private final static int wordshift = 5; |
|
89 |
private final static int wordmask = wordlen - 1; |
|
90 |
||
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
91 |
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
|
92 |
// 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
|
93 |
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
|
94 |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
95 |
protected BitsState currentState; |
10 | 96 |
|
97 |
/** Construct an initially empty set. |
|
98 |
*/ |
|
99 |
public Bits() { |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
100 |
this(false); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
101 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
102 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
103 |
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
|
104 |
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
|
105 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
106 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
107 |
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
|
108 |
this(unassignedBits, BitsState.getState(unassignedBits, reset)); |
10 | 109 |
} |
110 |
||
111 |
/** Construct a set consisting initially of given bit vector. |
|
112 |
*/ |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
113 |
protected Bits(int[] bits, BitsState initState) { |
10 | 114 |
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
|
115 |
this.currentState = initState; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
116 |
switch (initState) { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
117 |
case UNKNOWN: |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
118 |
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
|
119 |
break; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
120 |
case NORMAL: |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
121 |
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
|
122 |
break; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
123 |
} |
10 | 124 |
} |
125 |
||
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
126 |
protected void sizeTo(int len) { |
10 | 127 |
if (bits.length < len) { |
14049
3207422a0f9b
7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
13844
diff
changeset
|
128 |
bits = Arrays.copyOf(bits, len); |
10 | 129 |
} |
130 |
} |
|
131 |
||
132 |
/** This set = {}. |
|
133 |
*/ |
|
134 |
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
|
135 |
Assert.check(currentState != BitsState.UNKNOWN); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
136 |
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
|
137 |
bits[i] = 0; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
138 |
} |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
139 |
currentState = BitsState.NORMAL; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
140 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
141 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
142 |
public void reset() { |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
143 |
internalReset(); |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
144 |
} |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
145 |
|
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
146 |
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
|
147 |
bits = null; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
148 |
currentState = BitsState.UNKNOWN; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
149 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
150 |
|
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
151 |
public boolean isReset() { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
152 |
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
|
153 |
} |
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 |
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
|
156 |
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
|
157 |
currentState = BitsState.NORMAL; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
158 |
return this; |
10 | 159 |
} |
160 |
||
161 |
/** Return a copy of this set. |
|
162 |
*/ |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
163 |
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
|
164 |
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
|
165 |
Bits tmp = new Bits(); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
166 |
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
|
167 |
currentState = BitsState.NORMAL; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
168 |
return tmp; |
10 | 169 |
} |
170 |
||
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
171 |
protected int[] dupBits() { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
172 |
int [] result; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
173 |
if (currentState != BitsState.NORMAL) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
174 |
result = bits; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
175 |
} else { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
176 |
result = new int[bits.length]; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
177 |
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
|
178 |
} |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
179 |
return result; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
180 |
} |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
181 |
|
10 | 182 |
/** Include x in this set. |
183 |
*/ |
|
184 |
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
|
185 |
Assert.check(currentState != BitsState.UNKNOWN); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
186 |
Assert.check(x >= 0, "Value of x " + x); |
10 | 187 |
sizeTo((x >>> wordshift) + 1); |
188 |
bits[x >>> wordshift] = bits[x >>> wordshift] | |
|
189 |
(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
|
190 |
currentState = BitsState.NORMAL; |
10 | 191 |
} |
192 |
||
193 |
||
194 |
/** Include [start..limit) in this set. |
|
195 |
*/ |
|
196 |
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
|
197 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 198 |
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
|
199 |
for (int x = start; x < limit; x++) { |
10 | 200 |
bits[x >>> wordshift] = bits[x >>> wordshift] | |
201 |
(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
|
202 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
203 |
currentState = BitsState.NORMAL; |
10 | 204 |
} |
205 |
||
7621
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
206 |
/** Exclude [start...end] from this set. |
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
207 |
*/ |
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
208 |
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
|
209 |
Assert.check(currentState != BitsState.UNKNOWN); |
7621
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
210 |
Bits temp = new Bits(); |
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
211 |
temp.sizeTo(bits.length); |
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
212 |
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
|
213 |
internalAndSet(temp); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
214 |
currentState = BitsState.NORMAL; |
7621
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
215 |
} |
9f2901d1f92f
7003744: Compiler error concerning final variables
mcimadamore
parents:
5847
diff
changeset
|
216 |
|
10 | 217 |
/** Exclude x from this set. |
218 |
*/ |
|
219 |
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
|
220 |
Assert.check(currentState != BitsState.UNKNOWN); |
8032 | 221 |
Assert.check(x >= 0); |
10 | 222 |
sizeTo((x >>> wordshift) + 1); |
223 |
bits[x >>> wordshift] = bits[x >>> wordshift] & |
|
224 |
~(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
|
225 |
currentState = BitsState.NORMAL; |
10 | 226 |
} |
227 |
||
228 |
/** Is x an element of this set? |
|
229 |
*/ |
|
230 |
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
|
231 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 232 |
return |
233 |
0 <= x && x < (bits.length << wordshift) && |
|
234 |
(bits[x >>> wordshift] & (1 << (x & wordmask))) != 0; |
|
235 |
} |
|
236 |
||
13844 | 237 |
/** {@literal this set = this set & xs}. |
10 | 238 |
*/ |
239 |
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
|
240 |
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
|
241 |
internalAndSet(xs); |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
242 |
currentState = BitsState.NORMAL; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
243 |
return this; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
244 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
245 |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
246 |
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
|
247 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 248 |
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
|
249 |
for (int i = 0; i < xs.bits.length; i++) { |
10 | 250 |
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
|
251 |
} |
10 | 252 |
} |
253 |
||
254 |
/** this set = this set | xs. |
|
255 |
*/ |
|
256 |
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
|
257 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 258 |
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
|
259 |
for (int i = 0; i < xs.bits.length; i++) { |
10 | 260 |
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
|
261 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
262 |
currentState = BitsState.NORMAL; |
10 | 263 |
return this; |
264 |
} |
|
265 |
||
266 |
/** this set = this set \ xs. |
|
267 |
*/ |
|
268 |
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
|
269 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 270 |
for (int i = 0; i < bits.length; i++) { |
271 |
if (i < xs.bits.length) { |
|
272 |
bits[i] = bits[i] & ~xs.bits[i]; |
|
273 |
} |
|
274 |
} |
|
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
275 |
currentState = BitsState.NORMAL; |
10 | 276 |
return this; |
277 |
} |
|
278 |
||
279 |
/** this set = this set ^ xs. |
|
280 |
*/ |
|
281 |
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
|
282 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 283 |
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
|
284 |
for (int i = 0; i < xs.bits.length; i++) { |
10 | 285 |
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
|
286 |
} |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
287 |
currentState = BitsState.NORMAL; |
10 | 288 |
return this; |
289 |
} |
|
290 |
||
291 |
/** Count trailing zero bits in an int. Algorithm from "Hacker's |
|
292 |
* Delight" by Henry S. Warren Jr. (figure 5-13) |
|
293 |
*/ |
|
294 |
private static int trailingZeroBits(int x) { |
|
8032 | 295 |
Assert.check(wordlen == 32); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
296 |
if (x == 0) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
297 |
return 32; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
298 |
} |
10 | 299 |
int n = 1; |
300 |
if ((x & 0xffff) == 0) { n += 16; x >>>= 16; } |
|
301 |
if ((x & 0x00ff) == 0) { n += 8; x >>>= 8; } |
|
302 |
if ((x & 0x000f) == 0) { n += 4; x >>>= 4; } |
|
303 |
if ((x & 0x0003) == 0) { n += 2; x >>>= 2; } |
|
304 |
return n - (x&1); |
|
305 |
} |
|
306 |
||
13844 | 307 |
/** Return the index of the least bit position ≥ x that is set. |
10 | 308 |
* If none are set, returns -1. This provides a nice way to iterate |
309 |
* over the members of a bit set: |
|
13844 | 310 |
* <pre>{@code |
10 | 311 |
* for (int i = bits.nextBit(0); i>=0; i = bits.nextBit(i+1)) ... |
13844 | 312 |
* }</pre> |
10 | 313 |
*/ |
314 |
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
|
315 |
Assert.check(currentState != BitsState.UNKNOWN); |
10 | 316 |
int windex = x >>> wordshift; |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
317 |
if (windex >= bits.length) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
318 |
return -1; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
319 |
} |
10 | 320 |
int word = bits[windex] & ~((1 << (x & wordmask))-1); |
321 |
while (true) { |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
322 |
if (word != 0) { |
10 | 323 |
return (windex << wordshift) + trailingZeroBits(word); |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
324 |
} |
10 | 325 |
windex++; |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
326 |
if (windex >= bits.length) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
327 |
return -1; |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
328 |
} |
10 | 329 |
word = bits[windex]; |
330 |
} |
|
331 |
} |
|
332 |
||
333 |
/** a string representation of this set. |
|
334 |
*/ |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
335 |
@Override |
10 | 336 |
public String toString() { |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
337 |
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
|
338 |
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
|
339 |
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
|
340 |
digits[i] = isMember(i) ? '1' : '0'; |
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
341 |
} |
17282
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
342 |
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
|
343 |
} else { |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
344 |
return "[]"; |
c6964ad7df63
8008562: javac, a refactoring to Bits is necessary in order to provide a change history
vromero
parents:
14049
diff
changeset
|
345 |
} |
10 | 346 |
} |
347 |
||
348 |
/** Test Bits.nextBit(int). */ |
|
349 |
public static void main(String[] args) { |
|
350 |
java.util.Random r = new java.util.Random(); |
|
351 |
Bits bits = new Bits(); |
|
352 |
for (int i=0; i<125; i++) { |
|
353 |
int k; |
|
354 |
do { |
|
355 |
k = r.nextInt(250); |
|
356 |
} while (bits.isMember(k)); |
|
357 |
System.out.println("adding " + k); |
|
358 |
bits.incl(k); |
|
359 |
} |
|
360 |
int count = 0; |
|
361 |
for (int i = bits.nextBit(0); i >= 0; i = bits.nextBit(i+1)) { |
|
362 |
System.out.println("found " + i); |
|
363 |
count ++; |
|
364 |
} |
|
19941
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
365 |
if (count != 125) { |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
366 |
throw new Error(); |
8b91e8eb2d20
7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
17282
diff
changeset
|
367 |
} |
10 | 368 |
} |
369 |
} |