java.util.BitSet — set() doesn't work as expected

后端 未结 6 1405
借酒劲吻你
借酒劲吻你 2021-01-18 01:17

Am I missing something painfully obvious? Or does just nobody in the world actually use java.util.BitSet?

The following test fails:

@Test
public voi         


        
6条回答
  •  轮回少年
    2021-01-18 01:39

    Good Casper! Your small improvement should indeed have been present in the original BitSet java def! I also suggest this (append() and concat() are useful for various usages)

    import java.util.BitSet;
    
    public class fixBitSet extends BitSet {
    
      public int fsize = 0;
    
      public void set(int k, boolean value) {
        if (k >= fsize)
          fsize = k + 1;
        super.set(k, value);
      }
    
      public void append(fixBitSet bs) {
        for (int k = 0; k < bs.fsize; k++)
          super.set(fsize + k, bs.get(k));
        fsize += bs.fsize;
      }
    
      public static fixBitSet concat(fixBitSet[] vbs) {
        final fixBitSet bs = new fixBitSet();
        for (fixBitSet xbs : vbs)
          bs.append(xbs);
        return (bs);
      }
    
    }
    

提交回复
热议问题