001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.dtd.models;
019:
020: /**
021: * This class is a very simple bitset class. The DFA content model code needs
022: * to support a bit set, but the java BitSet class is way, way overkill. Our
023: * bitset never needs to be expanded after creation, hash itself, etc...
024: *
025: * Since the vast majority of content models will never require more than 64
026: * bits, and since allocation of anything in Java is expensive, this class
027: * provides a hybrid implementation that uses two ints for instances that use
028: * 64 bits or fewer. It has a byte array reference member which will only be
029: * used if more than 64 bits are required.
030: *
031: * Note that the code that uses this class will never perform operations
032: * on sets of different sizes, so that check does not have to be made here.
033: *
034: * @xerces.internal
035: *
036: * @version $Id: CMStateSet.java 446752 2006-09-15 21:55:19Z mrglavas $
037: */
038: // made this class public so it can be accessed by
039: // the XS content models from the schema package -neilg.
040: public class CMStateSet {
041: // -------------------------------------------------------------------
042: // Constructors
043: // -------------------------------------------------------------------
044: public CMStateSet(int bitCount) {
045: // Store the required bit count and insure its legal
046: fBitCount = bitCount;
047: if (fBitCount < 0)
048: throw new RuntimeException(
049: "ImplementationMessages.VAL_CMSI");
050:
051: //
052: // See if we need to allocate the byte array or whether we can live
053: // within the 64 bit high performance scheme.
054: //
055: if (fBitCount > 64) {
056: fByteCount = fBitCount / 8;
057: if (fBitCount % 8 != 0)
058: fByteCount++;
059: fByteArray = new byte[fByteCount];
060: }
061:
062: // Init all the bits to zero
063: zeroBits();
064: }
065:
066: // -------------------------------------------------------------------
067: // Public inherited methods
068: // -------------------------------------------------------------------
069: public String toString() {
070: StringBuffer strRet = new StringBuffer();
071: try {
072: strRet.append('{');
073: for (int index = 0; index < fBitCount; index++) {
074: if (getBit(index)) {
075: strRet.append(' ').append(index);
076: }
077: }
078: strRet.append(" }");
079: }
080:
081: catch (RuntimeException exToCatch) {
082: //
083: // We know this won't happen but we have to catch it to avoid it
084: // having to be in our 'throws' list.
085: //
086: }
087: return strRet.toString();
088: }
089:
090: // -------------------------------------------------------------------
091: // Package final methods
092: // -------------------------------------------------------------------
093: // the XS content models from the schema package -neilg.
094: public final void intersection(CMStateSet setToAnd) {
095: if (fBitCount < 65) {
096: fBits1 &= setToAnd.fBits1;
097: fBits2 &= setToAnd.fBits2;
098: } else {
099: for (int index = fByteCount - 1; index >= 0; index--)
100: fByteArray[index] &= setToAnd.fByteArray[index];
101: }
102: }
103:
104: public final boolean getBit(int bitToGet) {
105: if (bitToGet >= fBitCount)
106: throw new RuntimeException(
107: "ImplementationMessages.VAL_CMSI");
108:
109: if (fBitCount < 65) {
110: final int mask = (0x1 << (bitToGet % 32));
111: if (bitToGet < 32)
112: return (fBits1 & mask) != 0;
113: else
114: return (fBits2 & mask) != 0;
115: } else {
116: // Create the mask and byte values
117: final byte mask = (byte) (0x1 << (bitToGet % 8));
118: final int ofs = bitToGet >> 3;
119:
120: // And access the right bit and byte
121: return ((fByteArray[ofs] & mask) != 0);
122: }
123: }
124:
125: public final boolean isEmpty() {
126: if (fBitCount < 65) {
127: return ((fBits1 == 0) && (fBits2 == 0));
128: } else {
129: for (int index = fByteCount - 1; index >= 0; index--) {
130: if (fByteArray[index] != 0)
131: return false;
132: }
133: }
134: return true;
135: }
136:
137: final boolean isSameSet(CMStateSet setToCompare) {
138: if (fBitCount != setToCompare.fBitCount)
139: return false;
140:
141: if (fBitCount < 65) {
142: return ((fBits1 == setToCompare.fBits1) && (fBits2 == setToCompare.fBits2));
143: }
144:
145: for (int index = fByteCount - 1; index >= 0; index--) {
146: if (fByteArray[index] != setToCompare.fByteArray[index])
147: return false;
148: }
149: return true;
150: }
151:
152: // the XS content models from the schema package -neilg.
153: public final void union(CMStateSet setToOr) {
154: if (fBitCount < 65) {
155: fBits1 |= setToOr.fBits1;
156: fBits2 |= setToOr.fBits2;
157: } else {
158: for (int index = fByteCount - 1; index >= 0; index--)
159: fByteArray[index] |= setToOr.fByteArray[index];
160: }
161: }
162:
163: public final void setBit(int bitToSet) {
164: if (bitToSet >= fBitCount)
165: throw new RuntimeException(
166: "ImplementationMessages.VAL_CMSI");
167:
168: if (fBitCount < 65) {
169: final int mask = (0x1 << (bitToSet % 32));
170: if (bitToSet < 32) {
171: fBits1 &= ~mask;
172: fBits1 |= mask;
173: } else {
174: fBits2 &= ~mask;
175: fBits2 |= mask;
176: }
177: } else {
178: // Create the mask and byte values
179: final byte mask = (byte) (0x1 << (bitToSet % 8));
180: final int ofs = bitToSet >> 3;
181:
182: // And access the right bit and byte
183: fByteArray[ofs] &= ~mask;
184: fByteArray[ofs] |= mask;
185: }
186: }
187:
188: // the XS content models from the schema package -neilg.
189: public final void setTo(CMStateSet srcSet) {
190: // They have to be the same size
191: if (fBitCount != srcSet.fBitCount)
192: throw new RuntimeException(
193: "ImplementationMessages.VAL_CMSI");
194:
195: if (fBitCount < 65) {
196: fBits1 = srcSet.fBits1;
197: fBits2 = srcSet.fBits2;
198: } else {
199: for (int index = fByteCount - 1; index >= 0; index--)
200: fByteArray[index] = srcSet.fByteArray[index];
201: }
202: }
203:
204: // had to make this method public so it could be accessed from
205: // schema package - neilg.
206: public final void zeroBits() {
207: if (fBitCount < 65) {
208: fBits1 = 0;
209: fBits2 = 0;
210: } else {
211: for (int index = fByteCount - 1; index >= 0; index--)
212: fByteArray[index] = 0;
213: }
214: }
215:
216: // -------------------------------------------------------------------
217: // Private data members
218: //
219: // fBitCount
220: // The count of bits that the outside world wants to support,
221: // so its the max bit index plus one.
222: //
223: // fByteCount
224: // If the bit count is > 64, then we use the fByteArray member to
225: // store the bits, and this indicates its size in bytes. Otherwise
226: // its value is meaningless.
227: //
228: // fBits1
229: // fBits2
230: // When the bit count is < 64 (very common), these hold the bits.
231: // Otherwise, the fByteArray member holds htem.
232: // -------------------------------------------------------------------
233: int fBitCount;
234: int fByteCount;
235: int fBits1;
236: int fBits2;
237: byte[] fByteArray;
238:
239: /* Optimization(Jan, 2001) */
240: public boolean equals(Object o) {
241: if (!(o instanceof CMStateSet))
242: return false;
243: return isSameSet((CMStateSet) o);
244: }
245:
246: public int hashCode() {
247: if (fBitCount < 65) {
248: return fBits1 + fBits2 * 31;
249: } else {
250: int hash = 0;
251: for (int index = fByteCount - 1; index >= 0; index--)
252: hash = fByteArray[index] + hash * 31;
253: return hash;
254: }
255: }
256: /* Optimization(Jan, 2001) */
257: };
|