1 57 58 package com.sun.org.apache.xerces.internal.impl.dtd.models; 59 60 61 77 public class CMStateSet 80 { 81 public CMStateSet(int bitCount) 85 { 86 fBitCount = bitCount; 88 if (fBitCount < 0) 89 throw new RuntimeException ("ImplementationMessages.VAL_CMSI"); 90 91 if (fBitCount > 64) 96 { 97 fByteCount = fBitCount / 8; 98 if (fBitCount % 8 != 0) 99 fByteCount++; 100 fByteArray = new byte[fByteCount]; 101 } 102 103 zeroBits(); 105 } 106 107 108 public String toString() 112 { 113 StringBuffer strRet = new StringBuffer (); 114 try 115 { 116 strRet.append("{"); 117 for (int index = 0; index < fBitCount; index++) 118 { 119 if (getBit(index)) 120 strRet.append(" " + index); 121 } 122 strRet.append(" }"); 123 } 124 125 catch(RuntimeException exToCatch) 126 { 127 } 132 return strRet.toString(); 133 } 134 135 136 public final void intersection(CMStateSet setToAnd) 141 { 142 if (fBitCount < 65) 143 { 144 fBits1 &= setToAnd.fBits1; 145 fBits2 &= setToAnd.fBits2; 146 } 147 else 148 { 149 for (int index = fByteCount - 1; index >= 0; index--) 150 fByteArray[index] &= setToAnd.fByteArray[index]; 151 } 152 } 153 154 public final boolean getBit(int bitToGet) 155 { 156 if (bitToGet >= fBitCount) 157 throw new RuntimeException ("ImplementationMessages.VAL_CMSI"); 158 159 if (fBitCount < 65) 160 { 161 final int mask = (0x1 << (bitToGet % 32)); 162 if (bitToGet < 32) 163 return (fBits1 & mask) != 0; 164 else 165 return (fBits2 & mask) != 0; 166 } 167 else 168 { 169 final byte mask = (byte)(0x1 << (bitToGet % 8)); 171 final int ofs = bitToGet >> 3; 172 173 return ((fByteArray[ofs] & mask) != 0); 175 } 176 } 177 178 public final boolean isEmpty() 179 { 180 if (fBitCount < 65) 181 { 182 return ((fBits1 == 0) && (fBits2 == 0)); 183 } 184 else 185 { 186 for (int index = fByteCount - 1; index >= 0; index--) 187 { 188 if (fByteArray[index] != 0) 189 return false; 190 } 191 } 192 return true; 193 } 194 195 final boolean isSameSet(CMStateSet setToCompare) 196 { 197 if (fBitCount != setToCompare.fBitCount) 198 return false; 199 200 if (fBitCount < 65) 201 { 202 return ((fBits1 == setToCompare.fBits1) 203 && (fBits2 == setToCompare.fBits2)); 204 } 205 206 for (int index = fByteCount - 1; index >= 0; index--) 207 { 208 if (fByteArray[index] != setToCompare.fByteArray[index]) 209 return false; 210 } 211 return true; 212 } 213 214 public final void union(CMStateSet setToOr) 216 { 217 if (fBitCount < 65) 218 { 219 fBits1 |= setToOr.fBits1; 220 fBits2 |= setToOr.fBits2; 221 } 222 else 223 { 224 for (int index = fByteCount - 1; index >= 0; index--) 225 fByteArray[index] |= setToOr.fByteArray[index]; 226 } 227 } 228 229 public final void setBit(int bitToSet) 230 { 231 if (bitToSet >= fBitCount) 232 throw new RuntimeException ("ImplementationMessages.VAL_CMSI"); 233 234 if (fBitCount < 65) 235 { 236 final int mask = (0x1 << (bitToSet % 32)); 237 if (bitToSet < 32) 238 { 239 fBits1 &= ~mask; 240 fBits1 |= mask; 241 } 242 else 243 { 244 fBits2 &= ~mask; 245 fBits2 |= mask; 246 } 247 } 248 else 249 { 250 final byte mask = (byte)(0x1 << (bitToSet % 8)); 252 final int ofs = bitToSet >> 3; 253 254 fByteArray[ofs] &= ~mask; 256 fByteArray[ofs] |= mask; 257 } 258 } 259 260 public final void setTo(CMStateSet srcSet) 262 { 263 if (fBitCount != srcSet.fBitCount) 265 throw new RuntimeException ("ImplementationMessages.VAL_CMSI"); 266 267 if (fBitCount < 65) 268 { 269 fBits1 = srcSet.fBits1; 270 fBits2 = srcSet.fBits2; 271 } 272 else 273 { 274 for (int index = fByteCount - 1; index >= 0; index--) 275 fByteArray[index] = srcSet.fByteArray[index]; 276 } 277 } 278 279 public final void zeroBits() 282 { 283 if (fBitCount < 65) 284 { 285 fBits1 = 0; 286 fBits2 = 0; 287 } 288 else 289 { 290 for (int index = fByteCount - 1; index >= 0; index--) 291 fByteArray[index] = 0; 292 } 293 } 294 295 296 int fBitCount; 314 int fByteCount; 315 int fBits1; 316 int fBits2; 317 byte[] fByteArray; 318 319 public boolean equals(Object o) { 320 if (!(o instanceof CMStateSet)) return false; 321 return isSameSet((CMStateSet)o); 322 } 323 324 public int hashCode() { 325 if (fBitCount < 65) 326 { 327 return fBits1+ fBits2 * 31; 328 } 329 else 330 { 331 int hash = 0; 332 for (int index = fByteCount - 1; index >= 0; index--) 333 hash = fByteArray[index] + hash * 31; 334 return hash; 335 } 336 } 337 338 }; 339 | Popular Tags |