KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dtd > models > CMStateSet


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.dtd.models;
18
19
20 /**
21  * This class is a very simple bitset class. The DFA content model code needs
22  * to support a bit set, but the java BitSet class is way, way overkill. Our
23  * bitset never needs to be expanded after creation, hash itself, etc...
24  *
25  * Since the vast majority of content models will never require more than 64
26  * bits, and since allocation of anything in Java is expensive, this class
27  * provides a hybrid implementation that uses two ints for instances that use
28  * 64 bits or fewer. It has a byte array reference member which will only be
29  * used if more than 64 bits are required.
30  *
31  * Note that the code that uses this class will never perform operations
32  * on sets of different sizes, so that check does not have to be made here.
33  *
34  * @xerces.internal
35  *
36  * @version $Id: CMStateSet.java,v 1.5 2004/10/04 22:00:42 mrglavas Exp $
37  */

38 // made this class public so it can be accessed by
39
// the XS content models from the schema package -neilg.
40
public class CMStateSet
41 {
42     // -------------------------------------------------------------------
43
// Constructors
44
// -------------------------------------------------------------------
45
public CMStateSet(int bitCount)
46     {
47         // Store the required bit count and insure its legal
48
fBitCount = bitCount;
49         if (fBitCount < 0)
50             throw new RuntimeException JavaDoc("ImplementationMessages.VAL_CMSI");
51
52         //
53
// See if we need to allocate the byte array or whether we can live
54
// within the 64 bit high performance scheme.
55
//
56
if (fBitCount > 64)
57         {
58             fByteCount = fBitCount / 8;
59             if (fBitCount % 8 != 0)
60                 fByteCount++;
61             fByteArray = new byte[fByteCount];
62         }
63
64         // Init all the bits to zero
65
zeroBits();
66     }
67
68
69     // -------------------------------------------------------------------
70
// Public inherited methods
71
// -------------------------------------------------------------------
72
public String JavaDoc toString()
73     {
74         StringBuffer JavaDoc strRet = new StringBuffer JavaDoc();
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 JavaDoc exToCatch)
87         {
88             //
89
// We know this won't happen but we have to catch it to avoid it
90
// having to be in our 'throws' list.
91
//
92
}
93         return strRet.toString();
94     }
95
96
97     // -------------------------------------------------------------------
98
// Package final methods
99
// -------------------------------------------------------------------
100
// the XS content models from the schema package -neilg.
101
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 JavaDoc("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             // Create the mask and byte values
131
final byte mask = (byte)(0x1 << (bitToGet % 8));
132             final int ofs = bitToGet >> 3;
133
134             // And access the right bit and byte
135
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 // the XS content models from the schema package -neilg.
176
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 JavaDoc("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             // Create the mask and byte values
212
final byte mask = (byte)(0x1 << (bitToSet % 8));
213             final int ofs = bitToSet >> 3;
214
215             // And access the right bit and byte
216
fByteArray[ofs] &= ~mask;
217             fByteArray[ofs] |= mask;
218         }
219     }
220
221 // the XS content models from the schema package -neilg.
222
public final void setTo(CMStateSet srcSet)
223     {
224         // They have to be the same size
225
if (fBitCount != srcSet.fBitCount)
226             throw new RuntimeException JavaDoc("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     // had to make this method public so it could be accessed from
241
// schema package - neilg.
242
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     // -------------------------------------------------------------------
258
// Private data members
259
//
260
// fBitCount
261
// The count of bits that the outside world wants to support,
262
// so its the max bit index plus one.
263
//
264
// fByteCount
265
// If the bit count is > 64, then we use the fByteArray member to
266
// store the bits, and this indicates its size in bytes. Otherwise
267
// its value is meaningless.
268
//
269
// fBits1
270
// fBits2
271
// When the bit count is < 64 (very common), these hold the bits.
272
// Otherwise, the fByteArray member holds htem.
273
// -------------------------------------------------------------------
274
int fBitCount;
275     int fByteCount;
276     int fBits1;
277     int fBits2;
278     byte[] fByteArray;
279     /* Optimization(Jan, 2001) */
280     public boolean equals(Object JavaDoc 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    /* Optimization(Jan, 2001) */
299 };
300
Popular Tags