patternjavaMinor
Bit string builder for Java
Viewed 0 times
bitbuilderjavaforstring
Problem
I have this Java class for building bit strings. The set of methods it provides resembles that of
See what I have:
BitStringBuilder.java:
```
package net.coderodde.util;
import java.util.BitSet;
/**
* This class implements a simple bit string builder.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (May 25, 2016)
*/
public class BitStringBuilder {
/**
* The minimum number of bits to commit in the constructor.
*/
private static final int MINIMUM_BIT_COMMIT = 64;
private static final int BITS_PER_WORD = 64;
/**
* The actual bit storage.
*/
private long[] bits;
/**
* The size of bits this builder holds.
*/
private int size;
/**
* Constructs a new empty bit string builder.
*/
public BitStringBuilder() {
this(MINIMUM_BIT_COMMIT);
}
/**
* Constructs a new empty bit string builder capable holding
* {@code bitCommit} bits immediately after construction.
*
* @param bitCommit the number of bits to reserve.
*/
public BitStringBuilder(int bitCommit) {
bitCommit = Math.max(bitCommit, MINIMUM_BIT_COMMIT);
final int longs = bitCommit / BITS_PER_WORD +
(bitCommit % BITS_PER_WORD != 0 ? 1 : 0);
this.bits = new long[longs];
}
public int size() {
return this.size;
}
public boolean isEmpty() {
return this.size == 0;
}
/**
* Appends the bit string described by {@code bitset} to the end of this
* builder.
*
* @param bitset the {@link java.util.BitSet} holding the bits.
* @param numBits the number of bits to consider in {@code bitset}.
*/
public void append(final BitSet bitset, final int numBits) {
if (numBits toIndex) {
throw new IllegalArgumentException(
"'fromIndex' (" + fromIndex + ") is larger than 'toIndex' (" +
toIndex + ")!");
}
java.lang.StringBuilder.See what I have:
BitStringBuilder.java:
```
package net.coderodde.util;
import java.util.BitSet;
/**
* This class implements a simple bit string builder.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (May 25, 2016)
*/
public class BitStringBuilder {
/**
* The minimum number of bits to commit in the constructor.
*/
private static final int MINIMUM_BIT_COMMIT = 64;
private static final int BITS_PER_WORD = 64;
/**
* The actual bit storage.
*/
private long[] bits;
/**
* The size of bits this builder holds.
*/
private int size;
/**
* Constructs a new empty bit string builder.
*/
public BitStringBuilder() {
this(MINIMUM_BIT_COMMIT);
}
/**
* Constructs a new empty bit string builder capable holding
* {@code bitCommit} bits immediately after construction.
*
* @param bitCommit the number of bits to reserve.
*/
public BitStringBuilder(int bitCommit) {
bitCommit = Math.max(bitCommit, MINIMUM_BIT_COMMIT);
final int longs = bitCommit / BITS_PER_WORD +
(bitCommit % BITS_PER_WORD != 0 ? 1 : 0);
this.bits = new long[longs];
}
public int size() {
return this.size;
}
public boolean isEmpty() {
return this.size == 0;
}
/**
* Appends the bit string described by {@code bitset} to the end of this
* builder.
*
* @param bitset the {@link java.util.BitSet} holding the bits.
* @param numBits the number of bits to consider in {@code bitset}.
*/
public void append(final BitSet bitset, final int numBits) {
if (numBits toIndex) {
throw new IllegalArgumentException(
"'fromIndex' (" + fromIndex + ") is larger than 'toIndex' (" +
toIndex + ")!");
}
Solution
It looks like you want to optimize something but you did not define what are your performance requirements. Your tests do not justify your implementation.
Why the state of builder is stored in array of longs? In
This approach increases complexity of the whole class without known benefits.
I recommend to use
Use array of longs when
Why the state of builder is stored in array of longs? In
toString method you rewrite everything to StringBuilder and build the String.This approach increases complexity of the whole class without known benefits.
I recommend to use
StringBuilder or Bitset instead of array of longs. Write an adapter that implements your expected interface and delegates calls to the StringBuilder.Use array of longs when
StringBuilder or Bitset are not enough for you, but tell us why it is not enough. Write tests that prove this.Context
StackExchange Code Review Q#129298, answer score: 4
Revisions (0)
No revisions yet.