1 /* BitSet.java -- A vector of bits.
2 Copyright (C) 1998, 1999, 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 import gnu
.java
.lang
.CPStringBuilder
;
42 import java
.io
.Serializable
;
44 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
45 * hashCode algorithm taken from JDK 1.2 docs.
49 * This class can be thought of in two ways. You can see it as a
50 * vector of bits or as a set of non-negative integers. The name
51 * <code>BitSet</code> is a bit misleading.
53 * It is implemented by a bit vector, but its equally possible to see
54 * it as set of non-negative integer; each integer in the set is
55 * represented by a set bit at the corresponding index. The size of
56 * this structure is determined by the highest integer in the set.
58 * You can union, intersect and build (symmetric) remainders, by
59 * invoking the logical operations and, or, andNot, resp. xor.
61 * This implementation is NOT synchronized against concurrent access from
62 * multiple threads. Specifically, if one thread is reading from a bitset
63 * while another thread is simultaneously modifying it, the results are
66 * @author Jochen Hoenicke
67 * @author Tom Tromey (tromey@cygnus.com)
68 * @author Eric Blake (ebb9@email.byu.edu)
69 * @status updated to 1.4
71 public class BitSet
implements Cloneable
, Serializable
74 * Compatible with JDK 1.0.
76 private static final long serialVersionUID
= 7997698588986878753L;
81 private static final int LONG_MASK
= 0x3f;
85 * @serial the i'th bit is in bits[i/64] at position i%64 (where position
86 * 0 is the least significant).
91 * Create a new empty bit set. All bits are initially false.
99 * Create a new empty bit set, with a given size. This
100 * constructor reserves enough space to represent the integers
101 * from <code>0</code> to <code>nbits-1</code>.
103 * @param nbits the initial size of the bit set
104 * @throws NegativeArraySizeException if nbits < 0
106 public BitSet(int nbits
)
109 throw new NegativeArraySizeException();
111 int length
= nbits
>>> 6;
112 if ((nbits
& LONG_MASK
) != 0)
114 bits
= new long[length
];
118 * Performs the logical AND operation on this bit set and the
119 * given <code>set</code>. This means it builds the intersection
120 * of the two sets. The result is stored into this bit set.
122 * @param bs the second bit set
123 * @throws NullPointerException if bs is null
125 public void and(BitSet bs
)
127 int max
= Math
.min(bits
.length
, bs
.bits
.length
);
129 for (i
= 0; i
< max
; ++i
)
130 bits
[i
] &= bs
.bits
[i
];
131 while (i
< bits
.length
)
136 * Performs the logical AND operation on this bit set and the
137 * complement of the given <code>bs</code>. This means it
138 * selects every element in the first set, that isn't in the
139 * second set. The result is stored into this bit set and is
140 * effectively the set difference of the two.
142 * @param bs the second bit set
143 * @throws NullPointerException if bs is null
146 public void andNot(BitSet bs
)
148 int i
= Math
.min(bits
.length
, bs
.bits
.length
);
150 bits
[i
] &= ~bs
.bits
[i
];
154 * Returns the number of bits set to true.
156 * @return the number of true bits
159 public int cardinality()
162 for (int i
= bits
.length
- 1; i
>= 0; i
--)
165 // Take care of common cases.
174 // Successively collapse alternating bit groups into a sum.
175 a
= ((a
>> 1) & 0x5555555555555555L
) + (a
& 0x5555555555555555L
);
176 a
= ((a
>> 2) & 0x3333333333333333L
) + (a
& 0x3333333333333333L
);
177 int b
= (int) ((a
>>> 32) + a
);
178 b
= ((b
>> 4) & 0x0f0f0f0f) + (b
& 0x0f0f0f0f);
179 b
= ((b
>> 8) & 0x00ff00ff) + (b
& 0x00ff00ff);
180 card
+= ((b
>> 16) & 0x0000ffff) + (b
& 0x0000ffff);
186 * Sets all bits in the set to false.
192 Arrays
.fill(bits
, 0);
196 * Removes the integer <code>pos</code> from this set. That is
197 * the corresponding bit is cleared. If the index is not in the set,
198 * this method does nothing.
200 * @param pos a non-negative integer
201 * @throws IndexOutOfBoundsException if pos < 0
203 public void clear(int pos
)
205 int offset
= pos
>> 6;
207 // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
208 // so we'll just let that be our exception.
209 bits
[offset
] &= ~
(1L << pos
);
213 * Sets the bits between from (inclusive) and to (exclusive) to false.
215 * @param from the start range (inclusive)
216 * @param to the end range (exclusive)
217 * @throws IndexOutOfBoundsException if from < 0 || to < 0 ||
221 public void clear(int from
, int to
)
223 if (from
< 0 || from
> to
)
224 throw new IndexOutOfBoundsException();
227 int lo_offset
= from
>>> 6;
228 int hi_offset
= to
>>> 6;
230 if (lo_offset
== hi_offset
)
232 bits
[hi_offset
] &= ((1L << from
) - 1) | (-1L << to
);
236 bits
[lo_offset
] &= (1L << from
) - 1;
237 bits
[hi_offset
] &= -1L << to
;
238 for (int i
= lo_offset
+ 1; i
< hi_offset
; i
++)
243 * Create a clone of this bit set, that is an instance of the same
244 * class and contains the same elements. But it doesn't change when
245 * this bit set changes.
247 * @return the clone of this object.
249 public Object
clone()
253 BitSet bs
= (BitSet
) super.clone();
254 bs
.bits
= (long[]) bits
.clone();
257 catch (CloneNotSupportedException e
)
259 // Impossible to get here.
265 * Returns true if the <code>obj</code> is a bit set that contains
266 * exactly the same elements as this bit set, otherwise false.
268 * @param obj the object to compare to
269 * @return true if obj equals this bit set
271 public boolean equals(Object obj
)
273 if (!(obj
instanceof BitSet
))
275 BitSet bs
= (BitSet
) obj
;
276 int max
= Math
.min(bits
.length
, bs
.bits
.length
);
278 for (i
= 0; i
< max
; ++i
)
279 if (bits
[i
] != bs
.bits
[i
])
281 // If one is larger, check to make sure all extra bits are 0.
282 for (int j
= i
; j
< bits
.length
; ++j
)
285 for (int j
= i
; j
< bs
.bits
.length
; ++j
)
292 * Sets the bit at the index to the opposite value.
294 * @param index the index of the bit
295 * @throws IndexOutOfBoundsException if index is negative
298 public void flip(int index
)
300 int offset
= index
>> 6;
302 // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
303 // so we'll just let that be our exception.
304 bits
[offset
] ^
= 1L << index
;
308 * Sets a range of bits to the opposite value.
310 * @param from the low index (inclusive)
311 * @param to the high index (exclusive)
312 * @throws IndexOutOfBoundsException if from > to || from < 0 ||
316 public void flip(int from
, int to
)
318 if (from
< 0 || from
> to
)
319 throw new IndexOutOfBoundsException();
322 int lo_offset
= from
>>> 6;
323 int hi_offset
= to
>>> 6;
325 if (lo_offset
== hi_offset
)
327 bits
[hi_offset
] ^
= (-1L << from
) & ((1L << to
) - 1);
331 bits
[lo_offset
] ^
= -1L << from
;
332 bits
[hi_offset
] ^
= (1L << to
) - 1;
333 for (int i
= lo_offset
+ 1; i
< hi_offset
; i
++)
338 * Returns true if the integer <code>bitIndex</code> is in this bit
339 * set, otherwise false.
341 * @param pos a non-negative integer
342 * @return the value of the bit at the specified position
343 * @throws IndexOutOfBoundsException if the pos is negative
345 public boolean get(int pos
)
347 int offset
= pos
>> 6;
348 if (offset
>= bits
.length
)
350 // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
351 // so we'll just let that be our exception.
352 return (bits
[offset
] & (1L << pos
)) != 0;
356 * Returns a new <code>BitSet</code> composed of a range of bits from
359 * @param from the low index (inclusive)
360 * @param to the high index (exclusive)
361 * @throws IndexOutOfBoundsException if from > to || from < 0 ||
365 public BitSet
get(int from
, int to
)
367 if (from
< 0 || from
> to
)
368 throw new IndexOutOfBoundsException();
369 BitSet bs
= new BitSet(to
- from
);
370 int lo_offset
= from
>>> 6;
371 if (lo_offset
>= bits
.length
|| to
== from
)
374 int lo_bit
= from
& LONG_MASK
;
375 int hi_offset
= to
>>> 6;
378 int len
= Math
.min(hi_offset
- lo_offset
+ 1, bits
.length
- lo_offset
);
379 System
.arraycopy(bits
, lo_offset
, bs
.bits
, 0, len
);
380 if (hi_offset
< bits
.length
)
381 bs
.bits
[hi_offset
- lo_offset
] &= (1L << to
) - 1;
385 int len
= Math
.min(hi_offset
, bits
.length
- 1);
386 int reverse
= 64 - lo_bit
;
388 for (i
= 0; lo_offset
< len
; lo_offset
++, i
++)
389 bs
.bits
[i
] = ((bits
[lo_offset
] >>> lo_bit
)
390 | (bits
[lo_offset
+ 1] << reverse
));
391 if ((to
& LONG_MASK
) > lo_bit
)
392 bs
.bits
[i
++] = bits
[lo_offset
] >>> lo_bit
;
393 if (hi_offset
< bits
.length
)
394 bs
.bits
[i
- 1] &= (1L << (to
- from
)) - 1;
399 * Returns a hash code value for this bit set. The hash code of
400 * two bit sets containing the same integers is identical. The algorithm
401 * used to compute it is as follows:
403 * Suppose the bits in the BitSet were to be stored in an array of
404 * long integers called <code>bits</code>, in such a manner that
405 * bit <code>k</code> is set in the BitSet (for non-negative values
406 * of <code>k</code>) if and only if
408 * <code>((k/64) < bits.length)
409 * && ((bits[k/64] & (1L << (bit % 64))) != 0)
412 * Then the following definition of the hashCode method
413 * would be a correct implementation of the actual algorithm:
416 <pre>public int hashCode()
419 for (int i = bits.length-1; i >= 0; i--)
421 h ^= bits[i] * (i + 1);
424 return (int)((h >> 32) ^ h);
427 * Note that the hash code values changes, if the set is changed.
429 * @return the hash code value for this bit set.
431 public int hashCode()
434 for (int i
= bits
.length
; i
> 0; )
436 return (int) ((h
>> 32) ^ h
);
440 * Returns true if the specified BitSet and this one share at least one
443 * @param set the set to check for intersection
444 * @return true if the sets intersect
445 * @throws NullPointerException if set is null
448 public boolean intersects(BitSet set
)
450 int i
= Math
.min(bits
.length
, set
.bits
.length
);
452 if ((bits
[i
] & set
.bits
[i
]) != 0)
458 * Returns true if this set contains no true bits.
460 * @return true if all bits are false
463 public boolean isEmpty()
465 for (int i
= bits
.length
- 1; i
>= 0; i
--)
472 * Returns the logical number of bits actually used by this bit
473 * set. It returns the index of the highest set bit plus one.
474 * Note that this method doesn't return the number of set bits.
476 * @return the index of the highest set bit plus one.
480 // Set i to highest index that contains a non-zero value.
482 for (i
= bits
.length
- 1; i
>= 0 && bits
[i
] == 0; --i
)
485 // if i < 0 all bits are cleared.
489 // Now determine the exact length.
491 int len
= (i
+ 1) * 64;
492 // b >= 0 checks if the highest bit is zero.
503 * Returns the index of the next false bit, from the specified bit
506 * @param from the start location
507 * @return the first false bit
508 * @throws IndexOutOfBoundsException if from is negative
511 public int nextClearBit(int from
)
513 int offset
= from
>> 6;
514 long mask
= 1L << from
;
515 while (offset
< bits
.length
)
517 // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
518 // so we'll just let that be our exception.
519 long h
= bits
[offset
];
535 * Returns the index of the next true bit, from the specified bit
536 * (inclusive). If there is none, -1 is returned. You can iterate over
537 * all true bits with this loop:<br>
539 <pre>for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1))
544 * @param from the start location
545 * @return the first true bit, or -1
546 * @throws IndexOutOfBoundsException if from is negative
549 public int nextSetBit(int from
)
551 int offset
= from
>> 6;
552 long mask
= 1L << from
;
553 while (offset
< bits
.length
)
555 // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
556 // so we'll just let that be our exception.
557 long h
= bits
[offset
];
573 * Performs the logical OR operation on this bit set and the
574 * given <code>set</code>. This means it builds the union
575 * of the two sets. The result is stored into this bit set, which
576 * grows as necessary.
578 * @param bs the second bit set
579 * @throws NullPointerException if bs is null
581 public void or(BitSet bs
)
583 ensure(bs
.bits
.length
- 1);
584 for (int i
= bs
.bits
.length
- 1; i
>= 0; i
--)
585 bits
[i
] |= bs
.bits
[i
];
589 * Add the integer <code>bitIndex</code> to this set. That is
590 * the corresponding bit is set to true. If the index was already in
591 * the set, this method does nothing. The size of this structure
592 * is automatically increased as necessary.
594 * @param pos a non-negative integer.
595 * @throws IndexOutOfBoundsException if pos is negative
597 public void set(int pos
)
599 int offset
= pos
>> 6;
601 // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
602 // so we'll just let that be our exception.
603 bits
[offset
] |= 1L << pos
;
607 * Sets the bit at the given index to the specified value. The size of
608 * this structure is automatically increased as necessary.
610 * @param index the position to set
611 * @param value the value to set it to
612 * @throws IndexOutOfBoundsException if index is negative
615 public void set(int index
, boolean value
)
624 * Sets the bits between from (inclusive) and to (exclusive) to true.
626 * @param from the start range (inclusive)
627 * @param to the end range (exclusive)
628 * @throws IndexOutOfBoundsException if from < 0 || from > to ||
632 public void set(int from
, int to
)
634 if (from
< 0 || from
> to
)
635 throw new IndexOutOfBoundsException();
638 int lo_offset
= from
>>> 6;
639 int hi_offset
= to
>>> 6;
641 if (lo_offset
== hi_offset
)
643 bits
[hi_offset
] |= (-1L << from
) & ((1L << to
) - 1);
647 bits
[lo_offset
] |= -1L << from
;
648 bits
[hi_offset
] |= (1L << to
) - 1;
649 for (int i
= lo_offset
+ 1; i
< hi_offset
; i
++)
654 * Sets the bits between from (inclusive) and to (exclusive) to the
657 * @param from the start range (inclusive)
658 * @param to the end range (exclusive)
659 * @param value the value to set it to
660 * @throws IndexOutOfBoundsException if from < 0 || from > to ||
664 public void set(int from
, int to
, boolean value
)
673 * Returns the number of bits actually used by this bit set. Note
674 * that this method doesn't return the number of set bits, and that
675 * future requests for larger bits will make this automatically grow.
677 * @return the number of bits currently used.
681 return bits
.length
* 64;
685 * Returns the string representation of this bit set. This
686 * consists of a comma separated list of the integers in this set
687 * surrounded by curly braces. There is a space after each comma.
688 * A sample string is thus "{1, 3, 53}".
689 * @return the string representation.
691 public String
toString()
693 CPStringBuilder r
= new CPStringBuilder("{");
694 boolean first
= true;
695 for (int i
= 0; i
< bits
.length
; ++i
)
701 for (int j
= 0; j
< 64; ++j
)
703 if ((word
& bit
) != 0)
707 r
.append(64 * i
+ j
);
713 return r
.append("}").toString();
717 * Performs the logical XOR operation on this bit set and the
718 * given <code>set</code>. This means it builds the symmetric
719 * remainder of the two sets (the elements that are in one set,
720 * but not in the other). The result is stored into this bit set,
721 * which grows as necessary.
723 * @param bs the second bit set
724 * @throws NullPointerException if bs is null
726 public void xor(BitSet bs
)
728 ensure(bs
.bits
.length
- 1);
729 for (int i
= bs
.bits
.length
- 1; i
>= 0; i
--)
730 bits
[i
] ^
= bs
.bits
[i
];
734 * Make sure the vector is big enough.
736 * @param lastElt the size needed for the bits array
738 private void ensure(int lastElt
)
740 if (lastElt
>= bits
.length
)
742 long[] nd
= new long[lastElt
+ 1];
743 System
.arraycopy(bits
, 0, nd
, 0, bits
.length
);
748 // This is used by EnumSet for efficiency.
749 final boolean containsAll(BitSet other
)
751 for (int i
= other
.bits
.length
- 1; i
>= 0; i
--)
753 if ((bits
[i
] & other
.bits
[i
]) != other
.bits
[i
])