Skip to content

Commit

Permalink
improve BitArray
Browse files Browse the repository at this point in the history
  • Loading branch information
finesoft committed Sep 13, 2023
1 parent a560fb2 commit 99aca3d
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 17 deletions.
184 changes: 172 additions & 12 deletions corant-shared/src/main/java/org/corant/shared/util/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,47 +304,165 @@ public static short toShort(byte[] bytes, boolean strict) {
return (short) (bytes[0] << 8 | bytes[1] & 0xFF);
}

public static class BitArray {
/**
* corant-shared
* <p>
* A simple bits array, no thread-safe.
*
* @author bingo 上午10:35:39
*
*/
public static class BitArray implements Cloneable {

private byte[] array = EMPTY_ARRAY;

private int size = 0;
private int length = 0;

/**
* Create a new bit array containing all the bits in the given byte array.
*
* @param bytes a byte array containing a little-endian representation of a sequence of bits to
* be used as the initial bits of the new bit array
*/
public BitArray(byte[] bytes) {
if (bytes != null) {
int len = bytes.length;
length = len << 3;
array = Arrays.copyOf(bytes, len);
size = len << 3;
}
}

public BitArray(int size, boolean in) {
if (size < 1) {
/**
* Creates a new bit array with the given bits length which containing all the bits in the given
* byte array.
*
* @param array the bytes array to be initialized
* @param length the bits length
*/
public BitArray(byte[] array, int length) {
int arrayBitLen = array.length << 3;
if (length > arrayBitLen) {
throw new IndexOutOfBoundsException("length can't > " + arrayBitLen);
}
this.array = Arrays.copyOf(array, array.length);
this.length = length;
}

/**
* Creates a bit array whose initial size is {@code length} represent bits with indices in the
* range {@code 0} through {@code length-1}. All bits are initially {@code in}.
*
* @param length the initial length of the bits in array
* @param in the initial value
*/
public BitArray(int length, boolean in) {
if (length < 1) {
throw new CorantRuntimeException("The size must be greater then 0 zero!");
}
array = new byte[(size >> 3) + ((size & 7) == 0 ? 0 : 1)];
for (int i = 0; i < size; i++) {
this.length = length;
array = new byte[(length >> 3) + ((length & 7) == 0 ? 0 : 1)];
for (int i = 0; i < length; i++) {
setBit(i, in);
}
this.size = size;
}

/**
* Obtain bit value
* Returns the underlying bytes array length
*/
public int byteLength() {
return array.length;
}

@Override
public BitArray clone() throws CloneNotSupportedException {
return new BitArray(array, length);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof BitArray)) {
return false;
}
BitArray other = (BitArray) obj;
if (!Arrays.equals(array, other.array)) {
return false;
}
if (length != other.length) {
return false;
}
return true;
}

/**
* Returns the value of the bit with the specified index. The value is {@code true} if the bit
* with the index {@code pos} is currently set in this {@code BitArray}; otherwise, the result
* is {@code false}.
*
* @param pos the position
* @return getBit
* @return the value of the bit with the specified position
* @throws IndexOutOfBoundsException if the specified pos is negative
*/
public boolean getBit(int pos) {
if (pos < 0) {
throw new IndexOutOfBoundsException("pos < 0: " + pos);
}
return (array[pos >> 3] & 1 << (pos & 7)) != 0;
}

/**
* Returns a new {@code BitArray} composed of bits from this {@code BitArray} from
* {@code fromIndex} (inclusive) to {@code toIndex} (exclusive).
*
* @param fromIndex index of the first bit to include
* @param toIndex index after the last bit to include
* @return a new {@code BitArray} from a range of this {@code BitArray}
* @throws IndexOutOfBoundsException if {@code fromIndex} is negative, or {@code toIndex} is
* negative, or {@code fromIndex} is larger than {@code toIndex}, or {@code toIndex} is
* larger than the length of this {@code BitArray}
*/
public BitArray getBits(int fromIndex, int toIndex) {
if (fromIndex < 0) {
throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
}
if (toIndex < 0) {
throw new IndexOutOfBoundsException("toIndex < 0: " + toIndex);
}
if (fromIndex > toIndex) {
throw new IndexOutOfBoundsException("fromIndex: " + fromIndex + " > toIndex: " + toIndex);
}
if (toIndex > length) {
throw new IndexOutOfBoundsException("toIndex can't > " + length);
}
BitArray ba = new BitArray(toIndex - fromIndex, false);
for (int i = 0; i < ba.length; i++) {
ba.setBit(i, getBit(i + fromIndex));
}
return ba;
}

/**
* Returns the underlying bytes array
*/
public byte[] getBytes() {
return Arrays.copyOf(array, array.length);
}

public int getInitSize() {
return size;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(array);
return prime * result + length;
}

/**
* Returns the bits length
*/
public int length() {
return length;
}

/**
Expand All @@ -354,6 +472,9 @@ public int getInitSize() {
* @param b setBit the bit to set
*/
public void setBit(int pos, boolean b) {
if (pos >= length) {
throw new IndexOutOfBoundsException("pos can't >= " + length);
}
byte b8 = array[pos >> 3];
byte posBit = (byte) (1 << (pos & 7));
if (b) {
Expand All @@ -363,5 +484,44 @@ public void setBit(int pos, boolean b) {
}
array[pos >> 3] = b8;
}

/**
* Re-initial the underlying bytes array
*
* @param data the bytes array to be initialized
*/
public void setBytes(byte[] data) {
setBytes(data, (data.length << 3));
}

/**
* Re-initial the underlying bytes array and given bits length
*
* @param data the bytes array to be initialized
* @param length the bits length
*/
public void setBytes(byte[] data, int length) {
int dataBitLen = data.length << 3;
if (length > dataBitLen) {
throw new IndexOutOfBoundsException("length can't > " + dataBitLen);
}
this.length = length;
array = new byte[data.length];
System.arraycopy(data, 0, array, 0, data.length);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder((length << 1) + 2);
sb.append('[');
String[] bits = new String[length];
for (int i = 0; i < length; i++) {
bits[i] = getBit(i) ? "1" : "0";
}
sb.append(String.join(",", bits));
sb.append(']');
return sb.toString();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void main(String... s) {
for (int i = 0; i < 36; i++) {
ba.setBit(63 - i, true);
}
for (int i = 0; i < ba.getInitSize(); i++) {
for (int i = 0; i < ba.length(); i++) {
System.out.print(ba.getBit(i) ? "1" : 0);
}
System.out.println();
Expand All @@ -67,7 +67,7 @@ public static void main(String... s) {
System.out.println(f.getEpochSecond());
Bytes.toBytes(f.getEpochSecond());
ba = Bytes.asBitArray(Bytes.toBytes(f.getEpochSecond()));
for (int i = 0; i < ba.getInitSize(); i++) {
for (int i = 0; i < ba.length(); i++) {
System.out.print(ba.getBit(i) ? "1" : 0);
}
System.out.println();
Expand All @@ -86,9 +86,10 @@ public static void main(String... s) {

@Test
public void testBitArray() {
byte[] bytes = new byte[] {127, 0};// 01111111,00000000
byte[] bytes = {127, 0};// 01111111,00000000
BitArray array = Bytes.asBitArray(bytes);
int size = array.getInitSize();
System.out.println("****\t" + array.toString() + "\t****");
int size = array.length();
for (int i = 0; i < size; i++) {
if (i < 7) {
assertTrue(array.getBit(i));
Expand All @@ -103,7 +104,7 @@ public void testBitArray() {

@Test
public void testToBytes() {
byte[] array = new byte[] {10, 32, 23, 12, 34, 23, 35, 17};
byte[] array = {10, 32, 23, 12, 34, 23, 35, 17};
assertTrue(Objects.areDeepEqual(toBytes(toLong(array)), array));
assertTrue(
Objects.areDeepEqual(toBytes(toInt(Arrays.copyOf(array, 4))), Arrays.copyOf(array, 4)));
Expand Down

0 comments on commit 99aca3d

Please sign in to comment.