Skip to content

Commit

Permalink
Create ImmutableBitArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jul 7, 2024
1 parent 6e1c4c3 commit 98e0e35
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.collection.immutable.primitive;

import kala.NotImplementedError;
import kala.collection.base.primitive.BitArrays;
import kala.collection.base.primitive.BooleanIterator;
import kala.collection.base.primitive.BooleanTraversable;
import kala.collection.factory.primitive.BooleanCollectionFactory;
import kala.collection.primitive.BitArray;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.function.BooleanSupplier;

import static kala.collection.base.primitive.BitArrays.BITS_PRE_VALUE;

final class ImmutableBitArray extends BitArray implements ImmutableBooleanSeq {

private static final ImmutableBitArray EMPTY = new ImmutableBitArray(0, 0);

private ImmutableBitArray(long[] bitsArray, int size) {
super(bitsArray, size);
}

private ImmutableBitArray(long bits, int size) {
super(bits, size);
}

//region Static Factories

public static @NotNull BooleanCollectionFactory<?, ? extends ImmutableBitArray> factory() {
throw new NotImplementedError();
}

public static @NotNull ImmutableBitArray empty() {
return EMPTY;
}

public static @NotNull ImmutableBitArray of() {
return empty();
}

@Contract(value = "_ -> new", pure = true)
public static @NotNull ImmutableBitArray of(boolean value1) {
long bits = 0L;
bits = BitArrays.set(bits, 0, value1);
return new ImmutableBitArray(bits, 1);
}

@Contract(value = "_, _ -> new", pure = true)
public static @NotNull ImmutableBitArray of(boolean value1, boolean value2) {
long bits = 0L;
bits = BitArrays.set(bits, 0, value1);
bits = BitArrays.set(bits, 1, value2);
return new ImmutableBitArray(bits, 2);
}

@Contract(value = "_, _, _ -> new", pure = true)
public static @NotNull ImmutableBitArray of(boolean value1, boolean value2, boolean value3) {
long bits = 0L;
bits = BitArrays.set(bits, 0, value1);
bits = BitArrays.set(bits, 1, value2);
bits = BitArrays.set(bits, 2, value3);
return new ImmutableBitArray(bits, 3);
}

@Contract(value = "_, _, _, _ -> new", pure = true)
public static @NotNull ImmutableBitArray of(boolean value1, boolean value2, boolean value3, boolean value4) {
long bits = 0L;
bits = BitArrays.set(bits, 0, value1);
bits = BitArrays.set(bits, 1, value2);
bits = BitArrays.set(bits, 2, value3);
bits = BitArrays.set(bits, 3, value4);
return new ImmutableBitArray(bits, 4);
}

@Contract(value = "_, _, _, _, _ -> new", pure = true)
public static @NotNull ImmutableBitArray of(boolean value1, boolean value2, boolean value3, boolean value4, boolean value5) {
long bits = 0L;
bits = BitArrays.set(bits, 0, value1);
bits = BitArrays.set(bits, 1, value2);
bits = BitArrays.set(bits, 2, value3);
bits = BitArrays.set(bits, 3, value4);
bits = BitArrays.set(bits, 4, value5);
return new ImmutableBitArray(bits, 5);
}

@Contract(pure = true)
public static @NotNull ImmutableBitArray of(boolean... values) {
return from(values);
}

@Contract(pure = true)
public static @NotNull ImmutableBitArray from(boolean @NotNull [] values) {
final int size = values.length; // implicit null check of values

if (size == 0) {
return empty();
}

if (size < BITS_PRE_VALUE) {
long bits = 0L;
for (int i = 0; i < size; i++) {
bits = BitArrays.set(bits, i, values[i]);
}
return new ImmutableBitArray(bits, size);
}

final int fullChunkCount = size / BITS_PRE_VALUE;
final int notFullChunkLength = size % BITS_PRE_VALUE;

long[] bitsArray = new long[notFullChunkLength > 0 ? fullChunkCount + 1 : fullChunkCount];
for (int i = 0; i < fullChunkCount; i++) {
final int baseIndex = i * BITS_PRE_VALUE;
long bits = 0L;
for (int j = 0; j < BITS_PRE_VALUE; j++) {
bits = BitArrays.set(bits, j, values[baseIndex + j]);
}
bitsArray[i] = bits;
}

if (notFullChunkLength > 0) {
final int baseIndex = fullChunkCount * BITS_PRE_VALUE;
long bits = 0L;
for (int i = 0; i < notFullChunkLength; i++) {
bits = BitArrays.set(bits, i, values[baseIndex + i]);
}
bitsArray[fullChunkCount] = bits;
}

return new ImmutableBitArray(bitsArray, size);
}

public static @NotNull ImmutableBitArray from(@NotNull BooleanTraversable values) {
if (values instanceof ImmutableBitArray ba) {
return ba;
}

if (values.isEmpty()) { // implicit null check of values
return empty();
}

throw new NotImplementedError();
}


public static @NotNull ImmutableBitArray from(@NotNull BooleanIterator it) {
if (!it.hasNext()) { // implicit null check of it
return empty();
}
throw new NotImplementedError();
}

public static @NotNull ImmutableBitArray fill(int n, boolean value) {
if (n <= 0) {
return empty();
}

throw new NotImplementedError();
}

public static @NotNull ImmutableBitArray fill(int n, @NotNull BooleanSupplier supplier) {
if (n <= 0) {
return empty();
}

throw new NotImplementedError();
}

//endregion

@Override
public @NotNull String className() {
return "ImmutableBitArray";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class BitArray extends AbstractBooleanSeq implements IndexedBooleanSeq, S
//endregion

protected final long[] bitsArray;
protected final long bits;
protected long bits;
protected final int size;

protected BitArray(long[] bitsArray, int size) {
Expand All @@ -84,7 +84,7 @@ protected BitArray(long bits, int size) {
}

@Override
public @NotNull BooleanIterator iterator() {
public final @NotNull BooleanIterator iterator() {
if (bitsArray == null) {
return BitArrays.iterator(bits, 0, size);
} else {
Expand All @@ -93,12 +93,12 @@ protected BitArray(long bits, int size) {
}

@Override
public int size() {
public final int size() {
return size;
}

@Override
public boolean get(int index) {
public final boolean get(int index) {
Conditions.checkElementIndex(index, size);
if (bitsArray == null) {
return BitArrays.get(bits, index);
Expand All @@ -108,7 +108,7 @@ public boolean get(int index) {
}

@Override
public boolean @NotNull [] toArray() {
public final boolean @NotNull [] toArray() {
boolean[] res = new boolean[size];
if (bitsArray == null) {
for (int i = 0; i < size; i++) {
Expand All @@ -127,13 +127,41 @@ public boolean get(int index) {

final int notFullChunkLength = size % BITS_PRE_VALUE;
if (notFullChunkLength != 0) {
final long currentBits = bitsArray[bitsArray.length - 1];
final int baseIndex = (bitsArray.length - 1) * BITS_PRE_VALUE;
final long currentBits = bitsArray[fullChunkCount];
final int baseIndex = fullChunkCount * BITS_PRE_VALUE;
for (int i = 0; i < notFullChunkLength; i++) {
res[baseIndex + i] = BitArrays.get(currentBits, i);
}
}
}
return res;
}

@Override
public final void forEach(@NotNull BooleanConsumer action) {
if (bitsArray == null) {
for (int i = 0; i < size; i++) {
action.accept(BitArrays.get(bits, i));
}
} else {
final int fullChunkCount = size / BITS_PRE_VALUE;
for (int i = 0; i < fullChunkCount; i++) {
final long currentBits = bitsArray[i];
final int baseIndex = i * BITS_PRE_VALUE;

for (int j = 0; j < BITS_PRE_VALUE; j++) {
action.accept(BitArrays.get(currentBits, j));
}
}

final int notFullChunkLength = size % BITS_PRE_VALUE;
if (notFullChunkLength != 0) {
final long currentBits = bitsArray[fullChunkCount];
final int baseIndex = (bitsArray.length - 1) * BITS_PRE_VALUE;
for (int i = 0; i < notFullChunkLength; i++) {
action.accept(BitArrays.get(currentBits, i));
}
}
}
}
}

0 comments on commit 98e0e35

Please sign in to comment.