BitSet Class Implementation

E

Erick Crouse

Hello Everyone,

I need a question answered concerning the BitSet class. The problem is
that the public methods which manipulate a set of bits requires a
BitSet object as both the caller and argument to the method...

private BitSet A = new BitSet(16),
private BitSet B = new BitSet(16),

// do some manipulations with BitSet... and then...

A.and(B); // Results in a different A (B is unchanged)
A.xor(B); // Again results in a different A (B is unchanged)
// Interface: public void and( BitSet bits) -
BitSet;

The problem with this is that I prefer both A and B BitSet objects
remain the same while resulting in a new BitSet object say C using an
interface similar to -> public BitSet and( BitSet A, BitSet B) -
BitSet. With the current implementation of BitSet I would need to do
some sort of clone implementation (which seems like overkill) or
instantiate two identical A objects, say A1 and A2, one of which gets
operated on and then the other remains original for other purposes (
which seems a little awkward having to instantiate identical objects ),
Can anyone suggest what they would do in this situation where the
implementation operates either operand?

Thanks a Million,

EVAC
 
D

Daniel Pitts

Erick said:
Hello Everyone,

I need a question answered concerning the BitSet class. The problem is
that the public methods which manipulate a set of bits requires a
BitSet object as both the caller and argument to the method...

private BitSet A = new BitSet(16),
private BitSet B = new BitSet(16),

// do some manipulations with BitSet... and then...

A.and(B); // Results in a different A (B is unchanged)
A.xor(B); // Again results in a different A (B is unchanged)
// Interface: public void and( BitSet bits) -
BitSet;

The problem with this is that I prefer both A and B BitSet objects
remain the same while resulting in a new BitSet object say C using an
interface similar to -> public BitSet and( BitSet A, BitSet B) -
BitSet. With the current implementation of BitSet I would need to do
some sort of clone implementation (which seems like overkill) or
instantiate two identical A objects, say A1 and A2, one of which gets
operated on and then the other remains original for other purposes (
which seems a little awkward having to instantiate identical objects ),
Can anyone suggest what they would do in this situation where the
implementation operates either operand?

Thanks a Million,

EVAC

BitSet does have a clone. There wouldn't be any way to compute a.and(b)
without either mutating a, or cloning a and then mutating the clone.

If its that important to your design, I would create a wrapper class
called ImmutableBitSet which will handle the cloning of new bitsets for
you.
 
E

Erick Crouse

Thanks Daniel.


Daniel said:
BitSet does have a clone. There wouldn't be any way to compute a.and(b)
without either mutating a, or cloning a and then mutating the clone.

If its that important to your design, I would create a wrapper class
called ImmutableBitSet which will handle the cloning of new bitsets for
you.
 
M

Mark Rafn

Erick Crouse said:
I need a question answered concerning the BitSet class. The problem is
that the public methods which manipulate a set of bits requires a
BitSet object as both the caller and argument to the method...

private BitSet A = new BitSet(16),
private BitSet B = new BitSet(16),
// do some manipulations with BitSet... and then...
A.and(B); // Results in a different A (B is unchanged)
A.xor(B); // Again results in a different A (B is unchanged)

Right. That's how it works.
The problem with this is that I prefer both A and B BitSet objects
remain the same while resulting in a new BitSet object say C using an
interface similar to -> public BitSet and( BitSet A, BitSet B) -
BitSet.

something like:
public static BitSet andBitSets(BitSet A, BitSet B) {
BitSet retval = (BitSet)A.clone();
retval.and(B);
return retval;
}
With the current implementation of BitSet I would need to do
some sort of clone implementation (which seems like overkill)

BitSet implements Clonable. You don't need to do anything but call it.
Can anyone suggest what they would do in this situation where the
implementation operates either operand?

Use clone() as intended. This works just fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top