BitSet Class Implementation

Discussion in 'Java' started by Erick Crouse, Nov 16, 2006.

  1. Erick Crouse

    Erick Crouse Guest

    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
    Erick Crouse, Nov 16, 2006
    #1
    1. Advertising

  2. Erick Crouse

    Daniel Pitts Guest

    Erick Crouse wrote:
    > 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.
    Daniel Pitts, Nov 17, 2006
    #2
    1. Advertising

  3. Erick Crouse

    Erick Crouse Guest

    Thanks Daniel.


    Daniel Pitts wrote:
    > Erick Crouse wrote:
    > > 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.
    Erick Crouse, Nov 20, 2006
    #3
  4. Erick Crouse

    Mark Rafn Guest

    Erick Crouse <> wrote:
    >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.
    --
    Mark Rafn <http://www.dagon.net/>
    Mark Rafn, Nov 20, 2006
    #4
  5. Erick Crouse

    Daniel Pitts Guest

    Erick Crouse wrote:
    > Thanks Daniel.


    As Andrew Thompson might say, your future lack of top posting will be
    thanks enough.

    You're welcome,
    Daniel.
    Daniel Pitts, Nov 20, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sarath
    Replies:
    4
    Views:
    437
  2. arnuld
    Replies:
    2
    Views:
    443
    Ian Collins
    Jul 20, 2007
  3. arnuld
    Replies:
    5
    Views:
    483
    Bo Persson
    Jul 20, 2007
  4. Replies:
    5
    Views:
    396
    Salt_Peter
    Jan 5, 2008
  5. Ninds
    Replies:
    14
    Views:
    703
    W Karas
    Dec 3, 2012
Loading...

Share This Page