BitSet and FastBit will always change themselves if they do some set operations, just like and, or, andNot. To avoid such cases, you should write a extended classes:

For BitSet: MyBitSet

import java.util.BitSet;

/**
 * @author Conny Gu This Class extends from Class BitSet, it won't change
 *         itself while it does some operations.
 * 
 */

public class MyBitSet extends BitSet {

	public MyBitSet() {
		super(0);
	}

	public MyBitSet(int nbits) {
		super(nbits);
	}

	/**
	 * This myand() funktion will return a BiSet and itself won't be changed.
	 * 
	 * @param b
	 * @return MyBitSet
	 */
	public MyBitSet myand(MyBitSet b) {
		MyBitSet a = (MyBitSet) this.clone();
		a.and(b);
		return a;
	}

	public MyBitSet myor(MyBitSet b) {
		MyBitSet a = (MyBitSet) this.clone();
		a.or(b);
		return a;
	}

	/**
	 * This myandNot() funktion will return a BiSet and itself won't be changed.
	 * 
	 * @param b
	 * @return MyBitSet
	 */

	public MyBitSet myandNot(MyBitSet b) {
		MyBitSet a = (MyBitSet) this.clone();
		a.andNot(b);
		return a;
	}

}

For FastBitSet: MyFastBitSet

import javolution.util.FastBitSet;

/**
 * @author Conny Gu This Class extends from Class FastBitSet, it won't change
 *         itself while it does some operations.
 * 
 */

public class MyFastBitSet extends FastBitSet {

	public MyFastBitSet() {
		super();
	}

	public MyFastBitSet(FastBitSet b) {
		super();
		this.addAll(b);

	}

	
	public MyFastBitSet myand(MyFastBitSet b) {

		MyFastBitSet a = new MyFastBitSet();

		a.addAll(this);

		a.and(b);

		return a;

	}

	public MyFastBitSet myor(MyFastBitSet b) {

		MyFastBitSet a = new MyFastBitSet();

		a.addAll(this);

		a.or(b);

		return a;

	}

	public MyFastBitSet myandNot(MyFastBitSet b) {

		MyFastBitSet a = new MyFastBitSet();

		a.addAll(this);

		a.andNot(b);

		return a;

	}

}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.