1 16 17 package org.apache.xerces.impl.dtd.models; 18 19 20 38 public class CMStateSet 41 { 42 public CMStateSet(int bitCount) 46 { 47 fBitCount = bitCount; 49 if (fBitCount < 0) 50 throw new RuntimeException ("ImplementationMessages.VAL_CMSI"); 51 52 if (fBitCount > 64) 57 { 58 fByteCount = fBitCount / 8; 59 if (fBitCount % 8 != 0) 60 fByteCount++; 61 fByteArray = new byte[fByteCount]; 62 } 63 64 zeroBits(); 66 } 67 68 69 public String toString() 73 { 74 StringBuffer strRet = new StringBuffer (); 75 try 76 { 77 strRet.append("{"); 78 for (int index = 0; index < fBitCount; index++) 79 { 80 if (getBit(index)) 81 strRet.append(" " + index); 82 } 83 strRet.append(" }"); 84 } 85 86 catch(RuntimeException exToCatch) 87 { 88 } 93 return strRet.toString(); 94 } 95 96 97 public final void intersection(CMStateSet setToAnd) 102 { 103 if (fBitCount < 65) 104 { 105 fBits1 &= setToAnd.fBits1; 106 fBits2 &= setToAnd.fBits2; 107 } 108 else 109 { 110 for (int index = fByteCount - 1; index >= 0; index--) 111 fByteArray[index] &= setToAnd.fByteArray[index]; 112 } 113 } 114 115 public final boolean getBit(int bitToGet) 116 { 117 if (bitToGet >= fBitCount) 118 throw new RuntimeException ("ImplementationMessages.VAL_CMSI"); 119 120 if (fBitCount < 65) 121 { 122 final int mask = (0x1 << (bitToGet % 32)); 123 if (bitToGet < 32) 124 return (fBits1 & mask) != 0; 125 else 126 return (fBits2 & mask) != 0; 127 } 128 else 129 { 130 final byte mask = (byte)(0x1 << (bitToGet % 8)); 132 final int ofs = bitToGet >> 3; 133 134 return ((fByteArray[ofs] & mask) != 0); 136 } 137 } 138 139 public final boolean isEmpty() 140 { 141 if (fBitCount < 65) 142 { 143 return ((fBits1 == 0) && (fBits2 == 0)); 144 } 145 else 146 { 147 for (int index = fByteCount - 1; index >= 0; index--) 148 { 149 if (fByteArray[index] != 0) 150 return false; 151 } 152 } 153 return true; 154 } 155 156 final boolean isSameSet(CMStateSet setToCompare) 157 { 158 if (fBitCount != setToCompare.fBitCount) 159 return false; 160 161 if (fBitCount < 65) 162 { 163 return ((fBits1 == setToCompare.fBits1) 164 && (fBits2 == setToCompare.fBits2)); 165 } 166 167 for (int index = fByteCount - 1; index >= 0; index--) 168 { 169 if (fByteArray[index] != setToCompare.fByteArray[index]) 170 return false; 171 } 172 return true; 173 } 174 175 public final void union(CMStateSet setToOr) 177 { 178 if (fBitCount < 65) 179 { 180 fBits1 |= setToOr.fBits1; 181 fBits2 |= setToOr.fBits2; 182 } 183 else 184 { 185 for (int index = fByteCount - 1; index >= 0; index--) 186 fByteArray[index] |= setToOr.fByteArray[index]; 187 } 188 } 189 190 public final void setBit(int bitToSet) 191 { 192 if (bitToSet >= fBitCount) 193 throw new RuntimeException ("ImplementationMessages.VAL_CMSI"); 194 195 if (fBitCount < 65) 196 { 197 final int mask = (0x1 << (bitToSet % 32)); 198 if (bitToSet < 32) 199 { 200 fBits1 &= ~mask; 201 fBits1 |= mask; 202 } 203 else 204 { 205 fBits2 &= ~mask; 206 fBits2 |= mask; 207 } 208 } 209 else 210 { 211 final byte mask = (byte)(0x1 << (bitToSet % 8)); 213 final int ofs = bitToSet >> 3; 214 215 fByteArray[ofs] &= ~mask; 217 fByteArray[ofs] |= mask; 218 } 219 } 220 221 public final void setTo(CMStateSet srcSet) 223 { 224 if (fBitCount != srcSet.fBitCount) 226 throw new RuntimeException ("ImplementationMessages.VAL_CMSI"); 227 228 if (fBitCount < 65) 229 { 230 fBits1 = srcSet.fBits1; 231 fBits2 = srcSet.fBits2; 232 } 233 else 234 { 235 for (int index = fByteCount - 1; index >= 0; index--) 236 fByteArray[index] = srcSet.fByteArray[index]; 237 } 238 } 239 240 public final void zeroBits() 243 { 244 if (fBitCount < 65) 245 { 246 fBits1 = 0; 247 fBits2 = 0; 248 } 249 else 250 { 251 for (int index = fByteCount - 1; index >= 0; index--) 252 fByteArray[index] = 0; 253 } 254 } 255 256 257 int fBitCount; 275 int fByteCount; 276 int fBits1; 277 int fBits2; 278 byte[] fByteArray; 279 280 public boolean equals(Object o) { 281 if (!(o instanceof CMStateSet)) return false; 282 return isSameSet((CMStateSet)o); 283 } 284 285 public int hashCode() { 286 if (fBitCount < 65) 287 { 288 return fBits1+ fBits2 * 31; 289 } 290 else 291 { 292 int hash = 0; 293 for (int index = fByteCount - 1; index >= 0; index--) 294 hash = fByteArray[index] + hash * 31; 295 return hash; 296 } 297 } 298 299 }; 300 | Popular Tags |