This repository has been archived by the owner on Jan 24, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change: Clean up list packing patches, use fixed array type
- Loading branch information
1 parent
cefcac6
commit 4d891e6
Showing
9 changed files
with
392 additions
and
74 deletions.
There are no files selected for viewing
11 changes: 7 additions & 4 deletions
11
...common/collections/CompactCollectors.java → .../common/collections/CollectionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
package me.jellysquid.mods.hydrogen.common.collections; | ||
|
||
import it.unimi.dsi.fastutil.objects.ObjectArrayList; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collector; | ||
import java.util.stream.Collectors; | ||
|
||
public class CompactCollectors { | ||
public class CollectionHelper { | ||
public static <T> List<T> fixed(List<T> src) { | ||
return new FixedArrayList<>(src); | ||
} | ||
|
||
public static <T> Collector<T, ?, List<T>> toSizedList(int size) { | ||
return Collectors.toCollection(() -> new ObjectArrayList<>(size)); | ||
return Collectors.toCollection(() -> new ArrayList<>(size)); | ||
} | ||
} |
153 changes: 153 additions & 0 deletions
153
src/main/java/me/jellysquid/mods/hydrogen/common/collections/FixedArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package me.jellysquid.mods.hydrogen.common.collections; | ||
|
||
import com.google.common.collect.Iterators; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
|
||
import java.util.*; | ||
|
||
public class FixedArrayList<T> implements List<T> { | ||
private final T[] array; | ||
|
||
@SuppressWarnings("unchecked") | ||
public FixedArrayList(List<T> list) { | ||
this(list.toArray((T[]) new Object[0])); | ||
} | ||
|
||
public FixedArrayList(T[] array) { | ||
this.array = array; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return this.array.length; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return this.array.length == 0; | ||
} | ||
|
||
@Override | ||
public boolean contains(Object o) { | ||
return ArrayUtils.contains(this.array, o); | ||
} | ||
|
||
@Override | ||
public Iterator<T> iterator() { | ||
return Iterators.forArray(this.array); | ||
} | ||
|
||
@Override | ||
public Object[] toArray() { | ||
return this.array.clone(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T1> T1[] toArray(T1[] dst) { | ||
T[] src = this.array; | ||
|
||
if (dst.length < src.length) { | ||
return (T1[]) Arrays.copyOf(src, src.length, dst.getClass()); | ||
} | ||
|
||
System.arraycopy(src, 0, dst, 0, src.length); | ||
|
||
if (dst.length > src.length) { | ||
dst[src.length] = null; | ||
} | ||
|
||
return dst; | ||
} | ||
|
||
@Override | ||
public boolean add(T t) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean remove(Object o) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean containsAll(Collection<?> c) { | ||
for (Object o : c) { | ||
if (!ArrayUtils.contains(this.array, o)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public boolean addAll(Collection<? extends T> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean addAll(int index, Collection<? extends T> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean removeAll(Collection<?> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean retainAll(Collection<?> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public T get(int index) { | ||
return this.array[index]; | ||
} | ||
|
||
@Override | ||
public T set(int index, T element) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void add(int index, T element) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public T remove(int index) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int indexOf(Object o) { | ||
return ArrayUtils.indexOf(this.array, o); | ||
} | ||
|
||
@Override | ||
public int lastIndexOf(Object o) { | ||
return ArrayUtils.lastIndexOf(this.array, o); | ||
} | ||
|
||
@Override | ||
public ListIterator<T> listIterator() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public ListIterator<T> listIterator(int index) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public List<T> subList(int fromIndex, int toIndex) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
210 changes: 210 additions & 0 deletions
210
src/main/java/me/jellysquid/mods/hydrogen/common/collections/ImmutablePairArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
package me.jellysquid.mods.hydrogen.common.collections; | ||
|
||
import com.google.common.collect.AbstractIterator; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.ListIterator; | ||
|
||
public class ImmutablePairArrayList<V1, V2> implements List<Pair<V1, V2>> { | ||
private final V1[] v1; | ||
private final V2[] v2; | ||
private final int size; | ||
|
||
private final MutablePair<V1, V2> pair = new MutablePair<>(); | ||
|
||
@SuppressWarnings("unchecked") | ||
public ImmutablePairArrayList(List<Pair<V1, V2>> src) { | ||
this.size = src.size(); | ||
|
||
this.v1 = (V1[]) new Object[this.size]; | ||
this.v2 = (V2[]) new Object[this.size]; | ||
|
||
for (int i = 0; i < this.size; i++) { | ||
Pair<V1, V2> pair = src.get(i); | ||
|
||
this.v1[i] = pair.getKey(); | ||
this.v2[i] = pair.getValue(); | ||
} | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return this.size; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return this.size() == 0; | ||
} | ||
|
||
@Override | ||
public boolean contains(Object o) { | ||
if (!(o instanceof Pair)) { | ||
return false; | ||
} | ||
|
||
Pair<?, ?> pair = (Pair<?, ?>) o; | ||
|
||
for (int i = 0; i < this.size; i++) { | ||
if (this.v1[i] == pair.getLeft() && this.v2[i] == pair.getRight()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public Iterator<Pair<V1, V2>> iterator() { | ||
return new AbstractIterator<Pair<V1, V2>>() { | ||
private final V1[] v1 = ImmutablePairArrayList.this.v1; | ||
private final V2[] v2 = ImmutablePairArrayList.this.v2; | ||
private final int limit = ImmutablePairArrayList.this.size; | ||
|
||
private final MutablePair<V1, V2> tmp = new MutablePair<>(); | ||
|
||
private int index = 0; | ||
|
||
@Override | ||
protected Pair<V1, V2> computeNext() { | ||
if (this.index >= this.limit) { | ||
return this.endOfData(); | ||
} | ||
|
||
this.tmp.left = this.v1[this.index]; | ||
this.tmp.right = this.v2[this.index]; | ||
|
||
this.index++; | ||
|
||
return this.tmp; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Object[] toArray() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public <T> T[] toArray(T[] a) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean add(Pair<V1, V2> v1V2Pair) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean remove(Object o) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean containsAll(Collection<?> collection) { | ||
for (Object obj : collection) { | ||
if (!this.contains(obj)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public boolean addAll(Collection<? extends Pair<V1, V2>> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean addAll(int index, Collection<? extends Pair<V1, V2>> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean removeAll(Collection<?> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean retainAll(Collection<?> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Pair<V1, V2> get(int index) { | ||
this.pair.left = this.v1[index]; | ||
this.pair.right = this.v2[index]; | ||
|
||
return this.pair; | ||
} | ||
|
||
@Override | ||
public Pair<V1, V2> set(int index, Pair<V1, V2> element) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void add(int index, Pair<V1, V2> element) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Pair<V1, V2> remove(int index) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int indexOf(Object o) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int lastIndexOf(Object o) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public ListIterator<Pair<V1, V2>> listIterator() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public ListIterator<Pair<V1, V2>> listIterator(int index) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public List<Pair<V1, V2>> subList(int fromIndex, int toIndex) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
private static class MutablePair<V1, V2> extends Pair<V1, V2> { | ||
private V1 left; | ||
private V2 right; | ||
|
||
@Override | ||
public V1 getLeft() { | ||
return this.left; | ||
} | ||
|
||
@Override | ||
public V2 getRight() { | ||
return this.right; | ||
} | ||
|
||
@Override | ||
public V2 setValue(V2 value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
} |
Oops, something went wrong.