Wed, 25 Mar 2009 10:29:28 +0000
6400189: raw types and inference
Summary: Fixed resolution problem with raw overriding (CCC)
Reviewed-by: jjg
duke@1 | 1 | /* |
duke@1 | 2 | * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.util; |
duke@1 | 27 | |
duke@1 | 28 | /** A class for extensible, mutable bit sets. |
duke@1 | 29 | * |
duke@1 | 30 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
duke@1 | 31 | * you write code that depends on this, you do so at your own risk. |
duke@1 | 32 | * This code and its internal interfaces are subject to change or |
duke@1 | 33 | * deletion without notice.</b> |
duke@1 | 34 | */ |
duke@1 | 35 | public class Bits { |
duke@1 | 36 | |
duke@1 | 37 | |
duke@1 | 38 | private final static int wordlen = 32; |
duke@1 | 39 | private final static int wordshift = 5; |
duke@1 | 40 | private final static int wordmask = wordlen - 1; |
duke@1 | 41 | |
duke@1 | 42 | private int[] bits; |
duke@1 | 43 | |
duke@1 | 44 | /** Construct an initially empty set. |
duke@1 | 45 | */ |
duke@1 | 46 | public Bits() { |
duke@1 | 47 | this(new int[1]); |
duke@1 | 48 | } |
duke@1 | 49 | |
duke@1 | 50 | /** Construct a set consisting initially of given bit vector. |
duke@1 | 51 | */ |
duke@1 | 52 | public Bits(int[] bits) { |
duke@1 | 53 | this.bits = bits; |
duke@1 | 54 | } |
duke@1 | 55 | |
duke@1 | 56 | /** Construct a set consisting initially of given range. |
duke@1 | 57 | */ |
duke@1 | 58 | public Bits(int start, int limit) { |
duke@1 | 59 | this(); |
duke@1 | 60 | inclRange(start, limit); |
duke@1 | 61 | } |
duke@1 | 62 | |
duke@1 | 63 | private void sizeTo(int len) { |
duke@1 | 64 | if (bits.length < len) { |
duke@1 | 65 | int[] newbits = new int[len]; |
duke@1 | 66 | System.arraycopy(bits, 0, newbits, 0, bits.length); |
duke@1 | 67 | bits = newbits; |
duke@1 | 68 | } |
duke@1 | 69 | } |
duke@1 | 70 | |
duke@1 | 71 | /** This set = {}. |
duke@1 | 72 | */ |
duke@1 | 73 | public void clear() { |
duke@1 | 74 | for (int i = 0; i < bits.length; i++) bits[i] = 0; |
duke@1 | 75 | } |
duke@1 | 76 | |
duke@1 | 77 | /** Return a copy of this set. |
duke@1 | 78 | */ |
duke@1 | 79 | public Bits dup() { |
duke@1 | 80 | int[] newbits = new int[bits.length]; |
duke@1 | 81 | System.arraycopy(bits, 0, newbits, 0, bits.length); |
duke@1 | 82 | return new Bits(newbits); |
duke@1 | 83 | } |
duke@1 | 84 | |
duke@1 | 85 | /** Include x in this set. |
duke@1 | 86 | */ |
duke@1 | 87 | public void incl(int x) { |
duke@1 | 88 | assert x >= 0; |
duke@1 | 89 | sizeTo((x >>> wordshift) + 1); |
duke@1 | 90 | bits[x >>> wordshift] = bits[x >>> wordshift] | |
duke@1 | 91 | (1 << (x & wordmask)); |
duke@1 | 92 | } |
duke@1 | 93 | |
duke@1 | 94 | |
duke@1 | 95 | /** Include [start..limit) in this set. |
duke@1 | 96 | */ |
duke@1 | 97 | public void inclRange(int start, int limit) { |
duke@1 | 98 | sizeTo((limit >>> wordshift) + 1); |
duke@1 | 99 | for (int x = start; x < limit; x++) |
duke@1 | 100 | bits[x >>> wordshift] = bits[x >>> wordshift] | |
duke@1 | 101 | (1 << (x & wordmask)); |
duke@1 | 102 | } |
duke@1 | 103 | |
duke@1 | 104 | /** Exclude x from this set. |
duke@1 | 105 | */ |
duke@1 | 106 | public void excl(int x) { |
duke@1 | 107 | assert x >= 0; |
duke@1 | 108 | sizeTo((x >>> wordshift) + 1); |
duke@1 | 109 | bits[x >>> wordshift] = bits[x >>> wordshift] & |
duke@1 | 110 | ~(1 << (x & wordmask)); |
duke@1 | 111 | } |
duke@1 | 112 | |
duke@1 | 113 | /** Is x an element of this set? |
duke@1 | 114 | */ |
duke@1 | 115 | public boolean isMember(int x) { |
duke@1 | 116 | return |
duke@1 | 117 | 0 <= x && x < (bits.length << wordshift) && |
duke@1 | 118 | (bits[x >>> wordshift] & (1 << (x & wordmask))) != 0; |
duke@1 | 119 | } |
duke@1 | 120 | |
duke@1 | 121 | /** this set = this set & xs. |
duke@1 | 122 | */ |
duke@1 | 123 | public Bits andSet(Bits xs) { |
duke@1 | 124 | sizeTo(xs.bits.length); |
duke@1 | 125 | for (int i = 0; i < xs.bits.length; i++) |
duke@1 | 126 | bits[i] = bits[i] & xs.bits[i]; |
duke@1 | 127 | return this; |
duke@1 | 128 | } |
duke@1 | 129 | |
duke@1 | 130 | /** this set = this set | xs. |
duke@1 | 131 | */ |
duke@1 | 132 | public Bits orSet(Bits xs) { |
duke@1 | 133 | sizeTo(xs.bits.length); |
duke@1 | 134 | for (int i = 0; i < xs.bits.length; i++) |
duke@1 | 135 | bits[i] = bits[i] | xs.bits[i]; |
duke@1 | 136 | return this; |
duke@1 | 137 | } |
duke@1 | 138 | |
duke@1 | 139 | /** this set = this set \ xs. |
duke@1 | 140 | */ |
duke@1 | 141 | public Bits diffSet(Bits xs) { |
duke@1 | 142 | for (int i = 0; i < bits.length; i++) { |
duke@1 | 143 | if (i < xs.bits.length) { |
duke@1 | 144 | bits[i] = bits[i] & ~xs.bits[i]; |
duke@1 | 145 | } |
duke@1 | 146 | } |
duke@1 | 147 | return this; |
duke@1 | 148 | } |
duke@1 | 149 | |
duke@1 | 150 | /** this set = this set ^ xs. |
duke@1 | 151 | */ |
duke@1 | 152 | public Bits xorSet(Bits xs) { |
duke@1 | 153 | sizeTo(xs.bits.length); |
duke@1 | 154 | for (int i = 0; i < xs.bits.length; i++) |
duke@1 | 155 | bits[i] = bits[i] ^ xs.bits[i]; |
duke@1 | 156 | return this; |
duke@1 | 157 | } |
duke@1 | 158 | |
duke@1 | 159 | /** Count trailing zero bits in an int. Algorithm from "Hacker's |
duke@1 | 160 | * Delight" by Henry S. Warren Jr. (figure 5-13) |
duke@1 | 161 | */ |
duke@1 | 162 | private static int trailingZeroBits(int x) { |
duke@1 | 163 | assert wordlen == 32; |
duke@1 | 164 | if (x == 0) return 32; |
duke@1 | 165 | int n = 1; |
duke@1 | 166 | if ((x & 0xffff) == 0) { n += 16; x >>>= 16; } |
duke@1 | 167 | if ((x & 0x00ff) == 0) { n += 8; x >>>= 8; } |
duke@1 | 168 | if ((x & 0x000f) == 0) { n += 4; x >>>= 4; } |
duke@1 | 169 | if ((x & 0x0003) == 0) { n += 2; x >>>= 2; } |
duke@1 | 170 | return n - (x&1); |
duke@1 | 171 | } |
duke@1 | 172 | |
duke@1 | 173 | /** Return the index of the least bit position >= x that is set. |
duke@1 | 174 | * If none are set, returns -1. This provides a nice way to iterate |
duke@1 | 175 | * over the members of a bit set: |
duke@1 | 176 | * <pre> |
duke@1 | 177 | * for (int i = bits.nextBit(0); i>=0; i = bits.nextBit(i+1)) ... |
duke@1 | 178 | * </pre> |
duke@1 | 179 | */ |
duke@1 | 180 | public int nextBit(int x) { |
duke@1 | 181 | int windex = x >>> wordshift; |
duke@1 | 182 | if (windex >= bits.length) return -1; |
duke@1 | 183 | int word = bits[windex] & ~((1 << (x & wordmask))-1); |
duke@1 | 184 | while (true) { |
duke@1 | 185 | if (word != 0) |
duke@1 | 186 | return (windex << wordshift) + trailingZeroBits(word); |
duke@1 | 187 | windex++; |
duke@1 | 188 | if (windex >= bits.length) return -1; |
duke@1 | 189 | word = bits[windex]; |
duke@1 | 190 | } |
duke@1 | 191 | } |
duke@1 | 192 | |
duke@1 | 193 | /** a string representation of this set. |
duke@1 | 194 | */ |
duke@1 | 195 | public String toString() { |
duke@1 | 196 | char[] digits = new char[bits.length * wordlen]; |
duke@1 | 197 | for (int i = 0; i < bits.length * wordlen; i++) |
duke@1 | 198 | digits[i] = isMember(i) ? '1' : '0'; |
duke@1 | 199 | return new String(digits); |
duke@1 | 200 | } |
duke@1 | 201 | |
duke@1 | 202 | /** Test Bits.nextBit(int). */ |
duke@1 | 203 | public static void main(String[] args) { |
duke@1 | 204 | java.util.Random r = new java.util.Random(); |
duke@1 | 205 | Bits bits = new Bits(); |
duke@1 | 206 | int dupCount = 0; |
duke@1 | 207 | for (int i=0; i<125; i++) { |
duke@1 | 208 | int k; |
duke@1 | 209 | do { |
duke@1 | 210 | k = r.nextInt(250); |
duke@1 | 211 | } while (bits.isMember(k)); |
duke@1 | 212 | System.out.println("adding " + k); |
duke@1 | 213 | bits.incl(k); |
duke@1 | 214 | } |
duke@1 | 215 | int count = 0; |
duke@1 | 216 | for (int i = bits.nextBit(0); i >= 0; i = bits.nextBit(i+1)) { |
duke@1 | 217 | System.out.println("found " + i); |
duke@1 | 218 | count ++; |
duke@1 | 219 | } |
duke@1 | 220 | if (count != 125) throw new Error(); |
duke@1 | 221 | } |
duke@1 | 222 | } |