Skip to content

Commit

Permalink
Update ImmutableSeq
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jun 6, 2024
1 parent 5198eb8 commit 2fe8796
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import kala.tuple.Tuple;
import org.jetbrains.annotations.NotNull;

import java.io.Serial;
import java.io.Serializable;

@StaticClass
final class ImmutableMaps {
static sealed abstract class MapN<K, V> extends AbstractImmutableMap<K, V> {
Expand All @@ -30,12 +33,14 @@ public final int knownSize() {
}

@Override
public boolean isEmpty() {
public final boolean isEmpty() {
return size() == 0;
}
}

static final class Map0<K, V> extends MapN<K, V> {
static final class Map0<K, V> extends MapN<K, V> implements Serializable {
@Serial
private static final long serialVersionUID = 0L;

static final Map0<?, ?> INSTANCE = new Map0<>();

Expand All @@ -48,9 +53,16 @@ public int size() {
public @NotNull MapIterator<K, V> iterator() {
return MapIterator.empty();
}

@Serial
private Object readResolve() {
return INSTANCE;
}
}

static final class Map1<K, V> extends MapN<K, V> {
static final class Map1<K, V> extends MapN<K, V> implements Serializable {
@Serial
private static final long serialVersionUID = 0L;

private final K k0;
private final V v0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,15 @@ static <E> ImmutableSeq<E> narrow(ImmutableSeq<? extends E> seq) {
}

static <E> @NotNull ImmutableSeq<E> from(E @NotNull [] values) {
switch (values.length) {
case 0:
return ImmutableSeq.empty();
case 1:
return ImmutableSeq.of(values[0]);
case 2:
return ImmutableSeq.of(values[0], values[1]);
case 3:
return ImmutableSeq.of(values[0], values[1], values[2]);
case 4:
return ImmutableSeq.of(values[0], values[1], values[2], values[3]);
case 5:
return ImmutableSeq.of(values[0], values[1], values[2], values[3], values[4]);
}
return ImmutableVector.from(values);
return switch (values.length) {
case 0 -> ImmutableSeq.empty();
case 1 -> ImmutableSeq.of(values[0]);
case 2 -> ImmutableSeq.of(values[0], values[1]);
case 3 -> ImmutableSeq.of(values[0], values[1], values[2]);
case 4 -> ImmutableSeq.of(values[0], values[1], values[2], values[3]);
case 5 -> ImmutableSeq.of(values[0], values[1], values[2], values[3], values[4]);
default -> ImmutableVector.from(values);
};
}

static <E> @NotNull ImmutableSeq<E> from(java.util.@NotNull Collection<? extends E> values) {
Expand All @@ -115,20 +109,14 @@ static <E> ImmutableSeq<E> narrow(ImmutableSeq<? extends E> seq) {
final int length = arr.length;
//noinspection ConstantConditions
assert length == size;
switch (length) {
case 1:
return (ImmutableSeq<E>) ImmutableSeq.of(arr[0]);
case 2:
return (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1]);
case 3:
return (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2]);
case 4:
return (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2], arr[3]);
case 5:
return (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2], arr[3], arr[4]);
default:
return new ImmutableVectors.Vector1<>(arr);
}
return switch (length) {
case 1 -> (ImmutableSeq<E>) ImmutableSeq.of(arr[0]);
case 2 -> (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1]);
case 3 -> (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2]);
case 4 -> (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2], arr[3]);
case 5 -> (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2], arr[3], arr[4]);
default -> new ImmutableVectors.Vector1<>(arr);
};
}
return ImmutableVector.from(values.iterator());
}
Expand All @@ -145,20 +133,14 @@ static <E> ImmutableSeq<E> narrow(ImmutableSeq<? extends E> seq) {
Object[] arr = new Object[knownSize];
final int cn = values.copyToArray(arr);
assert cn == knownSize;
switch (knownSize) {
case 1:
return (ImmutableSeq<E>) ImmutableSeq.of(arr[0]);
case 2:
return (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1]);
case 3:
return (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2]);
case 4:
return (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2], arr[3]);
case 5:
return (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2], arr[3], arr[4]);
default:
return new ImmutableVectors.Vector1<>(arr);
}
return switch (knownSize) {
case 1 -> (ImmutableSeq<E>) ImmutableSeq.of(arr[0]);
case 2 -> (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1]);
case 3 -> (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2]);
case 4 -> (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2], arr[3]);
case 5 -> (ImmutableSeq<E>) ImmutableSeq.of(arr[0], arr[1], arr[2], arr[3], arr[4]);
default -> new ImmutableVectors.Vector1<>(arr);
};
}
return from(values.iterator());
}
Expand Down Expand Up @@ -202,38 +184,28 @@ static <E> ImmutableSeq<E> narrow(ImmutableSeq<? extends E> seq) {
if (n <= 0) {
return ImmutableSeq.empty();
}
switch (n) {
case 1:
return ImmutableSeq.of(supplier.get());
case 2:
return ImmutableSeq.of(supplier.get(), supplier.get());
case 3:
return ImmutableSeq.of(supplier.get(), supplier.get(), supplier.get());
case 4:
return ImmutableSeq.of(supplier.get(), supplier.get(), supplier.get(), supplier.get());
case 5:
return ImmutableSeq.of(supplier.get(), supplier.get(), supplier.get(), supplier.get(), supplier.get());
}
return ImmutableVector.fill(n, supplier);
return switch (n) {
case 1 -> ImmutableSeq.of(supplier.get());
case 2 -> ImmutableSeq.of(supplier.get(), supplier.get());
case 3 -> ImmutableSeq.of(supplier.get(), supplier.get(), supplier.get());
case 4 -> ImmutableSeq.of(supplier.get(), supplier.get(), supplier.get(), supplier.get());
case 5 -> ImmutableSeq.of(supplier.get(), supplier.get(), supplier.get(), supplier.get(), supplier.get());
default -> ImmutableVector.fill(n, supplier);
};
}

static <E> @NotNull ImmutableSeq<E> fill(int n, @NotNull IntFunction<? extends E> init) {
if (n <= 0) {
return ImmutableSeq.empty();
}
switch (n) {
case 1:
return ImmutableSeq.of(init.apply(0));
case 2:
return ImmutableSeq.of(init.apply(0), init.apply(1));
case 3:
return ImmutableSeq.of(init.apply(0), init.apply(1), init.apply(2));
case 4:
return ImmutableSeq.of(init.apply(0), init.apply(1), init.apply(2), init.apply(3));
case 5:
return ImmutableSeq.of(init.apply(0), init.apply(1), init.apply(2), init.apply(3), init.apply(4));
}
return ImmutableVector.fill(n, init);
return switch (n) {
case 1 -> ImmutableSeq.of(init.apply(0));
case 2 -> ImmutableSeq.of(init.apply(0), init.apply(1));
case 3 -> ImmutableSeq.of(init.apply(0), init.apply(1), init.apply(2));
case 4 -> ImmutableSeq.of(init.apply(0), init.apply(1), init.apply(2), init.apply(3));
case 5 -> ImmutableSeq.of(init.apply(0), init.apply(1), init.apply(2), init.apply(3), init.apply(4));
default -> ImmutableVector.fill(n, init);
};
}

static <E> @NotNull ImmutableSeq<E> generateUntil(@NotNull Supplier<? extends E> supplier, @NotNull Predicate<? super E> predicate) {
Expand Down

0 comments on commit 2fe8796

Please sign in to comment.