1999-04-07 16:42:40 +02:00
|
|
|
// BitSet - A vector of bits.
|
|
|
|
|
2000-05-04 17:50:34 +02:00
|
|
|
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
2000-10-29 05:44:10 +01:00
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
package java.util;
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Tom Tromey <tromey@cygnus.com>
|
|
|
|
* @date October 23, 1998.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
|
|
|
* hashCode algorithm taken from JDK 1.2 docs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public final class BitSet implements Cloneable, Serializable
|
|
|
|
{
|
|
|
|
public void and (BitSet bs)
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
int max = Math.min (bits.length, bs.bits.length);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < max; ++i)
|
|
|
|
bits[i] &= bs.bits[i];
|
|
|
|
for (; i < bits.length; ++i)
|
|
|
|
bits[i] = 0;
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public BitSet ()
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
this (64);
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public BitSet (int nbits)
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
if (nbits < 0)
|
|
|
|
throw new NegativeArraySizeException ();
|
|
|
|
int length = nbits / 64;
|
|
|
|
if (nbits % 64 != 0)
|
|
|
|
++length;
|
|
|
|
bits = new long[length];
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public void clear (int pos)
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
if (pos < 0)
|
|
|
|
throw new IndexOutOfBoundsException ();
|
|
|
|
int bit = pos % 64;
|
|
|
|
int offset = pos / 64;
|
|
|
|
ensure (offset);
|
|
|
|
bits[offset] &= ~(1L << bit);
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public Object clone ()
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
BitSet bs = new BitSet (bits.length * 64);
|
|
|
|
System.arraycopy (bits, 0, bs.bits, 0, bits.length);
|
|
|
|
return bs;
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public boolean equals (Object obj)
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
if (!(obj instanceof BitSet))
|
|
|
|
return false;
|
|
|
|
BitSet bs = (BitSet) obj;
|
|
|
|
int max = Math.min (bits.length, bs.bits.length);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < max; ++i)
|
|
|
|
if (bits[i] != bs.bits[i])
|
1999-04-07 16:42:40 +02:00
|
|
|
return false;
|
2000-10-29 05:44:10 +01:00
|
|
|
// If one is larger, check to make sure all extra bits are 0.
|
|
|
|
for (int j = i; j < bits.length; ++j)
|
|
|
|
if (bits[j] != 0)
|
|
|
|
return false;
|
|
|
|
for (int j = i; j < bs.bits.length; ++j)
|
|
|
|
if (bs.bits[j] != 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public boolean get (int pos)
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
if (pos < 0)
|
|
|
|
throw new IndexOutOfBoundsException ();
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2000-10-29 05:44:10 +01:00
|
|
|
int bit = pos % 64;
|
|
|
|
int offset = pos / 64;
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2000-10-29 05:44:10 +01:00
|
|
|
if (offset >= bits.length)
|
|
|
|
return false;
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2000-10-29 05:44:10 +01:00
|
|
|
return (bits[offset] & (1L << bit)) == 0 ? false : true;
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public int hashCode ()
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
long h = 1234;
|
|
|
|
for (int i = bits.length - 1; i >= 0; --i)
|
|
|
|
h ^= bits[i] * (i + 1);
|
|
|
|
return (int) ((h >> 32) ^ h);
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public void or (BitSet bs)
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
ensure (bs.bits.length - 1);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < bs.bits.length; ++i)
|
|
|
|
bits[i] |= bs.bits[i];
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public void set (int pos)
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
if (pos < 0)
|
|
|
|
throw new IndexOutOfBoundsException ();
|
|
|
|
int bit = pos % 64;
|
|
|
|
int offset = pos / 64;
|
|
|
|
ensure (offset);
|
|
|
|
bits[offset] |= 1L << bit;
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public int size ()
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
return bits.length * 64;
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public String toString ()
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
StringBuffer result = new StringBuffer ("{");
|
|
|
|
boolean first = true;
|
|
|
|
for (int i = 0; i < bits.length; ++i)
|
|
|
|
{
|
|
|
|
int bit = 1;
|
|
|
|
long word = bits[i];
|
|
|
|
for (int j = 0; j < 64; ++j)
|
|
|
|
{
|
|
|
|
if ((word & bit) != 0)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
result.append (", ");
|
|
|
|
result.append (64 * i + j);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
bit <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.append ("}").toString ();
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
public void xor (BitSet bs)
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
ensure (bs.bits.length - 1);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < bs.bits.length; ++i)
|
|
|
|
bits[i] ^= bs.bits[i];
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
// Make sure the vector is big enough.
|
|
|
|
private final void ensure (int lastElt)
|
2000-10-29 05:44:10 +01:00
|
|
|
{
|
|
|
|
if (lastElt + 1 > bits.length)
|
|
|
|
{
|
|
|
|
long[] nd = new long[lastElt + 1];
|
|
|
|
System.arraycopy (bits, 0, nd, 0, bits.length);
|
|
|
|
bits = nd;
|
|
|
|
}
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
// The actual bits.
|
|
|
|
private long[] bits;
|
2000-10-27 12:33:46 +02:00
|
|
|
private static final long serialVersionUID = 7997698588986878753L;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|