KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > CharSet


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4  * All rights reserved. *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License. See the file *
8  * COPYRIGHT for more information. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License along *
16  * with this program; if not, write to the Free Software Foundation, Inc., *
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18  * *
19  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

20
21 package JFlex;
22
23
24 /**
25  *
26  * @author Gerwin Klein
27  * @version JFlex 1.4.1, $Revision: 2.3 $, $Date: 2004/11/06 23:03:31 $
28  */

29 public final class CharSet {
30
31   final static int BITS = 6; // the number of bits to shift (2^6 = 64)
32
final static int MOD = (1<<BITS)-1; // modulus
33

34   long bits[];
35
36   private int numElements;
37   
38   
39   public CharSet() {
40     bits = new long[1];
41   }
42
43
44   public CharSet(int initialSize, int character) {
45     bits = new long[(initialSize >> BITS)+1];
46     add(character);
47   }
48
49
50   public void add(int character) {
51     resize(character);
52
53     if ( (bits[character >> BITS] & (1L << (character & MOD))) == 0) numElements++;
54
55     bits[character >> BITS] |= (1L << (character & MOD));
56   }
57
58
59   private int nbits2size (int nbits) {
60     return ((nbits >> BITS) + 1);
61   }
62
63
64   private void resize(int nbits) {
65     int needed = nbits2size(nbits);
66
67     if (needed < bits.length) return;
68          
69     long newbits[] = new long[Math.max(bits.length*2,needed)];
70     System.arraycopy(bits, 0, newbits, 0, bits.length);
71     
72     bits = newbits;
73   }
74
75
76   public boolean isElement(int character) {
77     int index = character >> BITS;
78     if (index >= bits.length) return false;
79     return (bits[index] & (1L << (character & MOD))) != 0;
80   }
81
82
83   public CharSetEnumerator characters() {
84     return new CharSetEnumerator(this);
85   }
86
87
88   public boolean containsElements() {
89     return numElements > 0;
90   }
91
92   public int size() {
93     return numElements;
94   }
95   
96   public String JavaDoc toString() {
97     CharSetEnumerator set = characters();
98
99     StringBuffer JavaDoc result = new StringBuffer JavaDoc("{");
100
101     if ( set.hasMoreElements() ) result.append(""+set.nextElement());
102
103     while ( set.hasMoreElements() ) {
104       int i = set.nextElement();
105       result.append( ", "+(int)i);
106     }
107
108     result.append("}");
109
110     return result.toString();
111   }
112 }
113
Popular Tags