EnsureCapacity() public static method

If the given LongBitSet is large enough to hold {@code numBits}, returns the given bits, otherwise returns a new LongBitSet which can hold the requested number of bits.

NOTE: the returned bitset reuses the underlying {@code long[]} of the given {@code bits} if possible. Also, calling #length() on the returned bits may return a value greater than {@code numBits}.

public static EnsureCapacity ( LongBitSet bits, long numBits ) : LongBitSet
bits LongBitSet
numBits long
return LongBitSet
Exemplo n.º 1
0
        public virtual void TestEnsureCapacity()
        {
            LongBitSet bits = new LongBitSet(5);

            bits.Set(1);
            bits.Set(4);

            LongBitSet newBits = LongBitSet.EnsureCapacity(bits, 8); // grow within the word

            Assert.IsTrue(newBits.Get(1));
            Assert.IsTrue(newBits.Get(4));
            newBits.Clear(1);
            // we align to 64-bits, so even though it shouldn't have, it re-allocated a long[1]
            Assert.IsTrue(bits.Get(1));
            Assert.IsFalse(newBits.Get(1));

            newBits.Set(1);
            newBits = LongBitSet.EnsureCapacity(newBits, newBits.Length() - 2); // reuse
            Assert.IsTrue(newBits.Get(1));

            bits.Set(1);
            newBits = LongBitSet.EnsureCapacity(bits, 72); // grow beyond one word
            Assert.IsTrue(newBits.Get(1));
            Assert.IsTrue(newBits.Get(4));
            newBits.Clear(1);
            // we grew the long[], so it's not shared
            Assert.IsTrue(bits.Get(1));
            Assert.IsFalse(newBits.Get(1));
        }