feat[list]: 添加性能更加高的原生基础类型的固定大小list

This commit is contained in:
godotg
2022-09-23 21:02:53 +08:00
parent 5c3c85bc2a
commit c084e16e14
8 changed files with 1141 additions and 0 deletions
@@ -0,0 +1,157 @@
/*
* Copyright (C) 2020 The zfoo Authors
* 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 com.zfoo.protocol.collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* @author godotg
* @version 3.0
*/
public class FixedSizeBooleanList implements List<Boolean> {
private final boolean[] array;
public FixedSizeBooleanList(int initialCapacity) {
this.array = new boolean[initialCapacity];
}
@Override
public int size() {
return array.length;
}
@Override
public boolean isEmpty() {
return ArrayUtils.isEmpty(array);
}
@Override
public Boolean get(int index) {
return array[index];
}
public boolean getRaw(int index) {
return array[index];
}
@Override
public Boolean set(int index, Boolean ele) {
var old = array[index];
array[index] = ele;
return old;
}
public void set(int index, boolean ele) {
array[index] = ele;
}
@Override
public boolean contains(Object ele) {
return ArrayUtils.toList(array).stream().anyMatch(it -> ele.equals(it));
}
@Override
public Object[] toArray() {
return ArrayUtils.toList(array).toArray();
}
@Override
public <T> T[] toArray(T[] arrays) {
ArrayUtils.toList(array).toArray(arrays);
return arrays;
}
@Override
public boolean containsAll(Collection<?> collection) {
return ArrayUtils.toList(array).containsAll(collection);
}
@Override
public int indexOf(Object ele) {
return ArrayUtils.toList(array).indexOf(ele);
}
@Override
public int lastIndexOf(Object ele) {
return ArrayUtils.toList(array).lastIndexOf(ele);
}
@Override
public Iterator<Boolean> iterator() {
return ArrayUtils.toList(array).iterator();
}
@Override
public ListIterator<Boolean> listIterator() {
return ArrayUtils.toList(array).listIterator();
}
@Override
public ListIterator<Boolean> listIterator(int index) {
return ArrayUtils.toList(array).listIterator(index);
}
@Override
public List<Boolean> subList(int fromIndex, int toIndex) {
return ArrayUtils.toList(array).subList(fromIndex, toIndex);
}
@Override
public boolean add(Boolean e) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, Boolean element) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends Boolean> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends Boolean> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public Boolean remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}
@@ -0,0 +1,157 @@
/*
* Copyright (C) 2020 The zfoo Authors
* 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 com.zfoo.protocol.collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* @author godotg
* @version 3.0
*/
public class FixedSizeDoubleList implements List<Double> {
private final double[] array;
public FixedSizeDoubleList(int initialCapacity) {
this.array = new double[initialCapacity];
}
@Override
public int size() {
return array.length;
}
@Override
public boolean isEmpty() {
return ArrayUtils.isEmpty(array);
}
@Override
public Double get(int index) {
return array[index];
}
public double getRaw(int index) {
return array[index];
}
@Override
public Double set(int index, Double ele) {
var old = array[index];
array[index] = ele;
return old;
}
public void set(int index, double ele) {
array[index] = ele;
}
@Override
public boolean contains(Object ele) {
return ArrayUtils.toList(array).stream().anyMatch(it -> ele.equals(it));
}
@Override
public Object[] toArray() {
return ArrayUtils.toList(array).toArray();
}
@Override
public <T> T[] toArray(T[] arrays) {
ArrayUtils.toList(array).toArray(arrays);
return arrays;
}
@Override
public boolean containsAll(Collection<?> collection) {
return ArrayUtils.toList(array).containsAll(collection);
}
@Override
public int indexOf(Object ele) {
return ArrayUtils.toList(array).indexOf(ele);
}
@Override
public int lastIndexOf(Object ele) {
return ArrayUtils.toList(array).lastIndexOf(ele);
}
@Override
public Iterator<Double> iterator() {
return ArrayUtils.toList(array).iterator();
}
@Override
public ListIterator<Double> listIterator() {
return ArrayUtils.toList(array).listIterator();
}
@Override
public ListIterator<Double> listIterator(int index) {
return ArrayUtils.toList(array).listIterator(index);
}
@Override
public List<Double> subList(int fromIndex, int toIndex) {
return ArrayUtils.toList(array).subList(fromIndex, toIndex);
}
@Override
public boolean add(Double e) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, Double element) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends Double> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends Double> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public Double remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}
@@ -0,0 +1,157 @@
/*
* Copyright (C) 2020 The zfoo Authors
* 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 com.zfoo.protocol.collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* @author godotg
* @version 3.0
*/
public class FixedSizeFloatList implements List<Float> {
private final float[] array;
public FixedSizeFloatList(int initialCapacity) {
this.array = new float[initialCapacity];
}
@Override
public int size() {
return array.length;
}
@Override
public boolean isEmpty() {
return ArrayUtils.isEmpty(array);
}
@Override
public Float get(int index) {
return array[index];
}
public float getRaw(int index) {
return array[index];
}
@Override
public Float set(int index, Float ele) {
var old = array[index];
array[index] = ele;
return old;
}
public void set(int index, float ele) {
array[index] = ele;
}
@Override
public boolean contains(Object ele) {
return ArrayUtils.toList(array).stream().anyMatch(it -> ele.equals(it));
}
@Override
public Object[] toArray() {
return ArrayUtils.toList(array).toArray();
}
@Override
public <T> T[] toArray(T[] arrays) {
ArrayUtils.toList(array).toArray(arrays);
return arrays;
}
@Override
public boolean containsAll(Collection<?> collection) {
return ArrayUtils.toList(array).containsAll(collection);
}
@Override
public int indexOf(Object ele) {
return ArrayUtils.toList(array).indexOf(ele);
}
@Override
public int lastIndexOf(Object ele) {
return ArrayUtils.toList(array).lastIndexOf(ele);
}
@Override
public Iterator<Float> iterator() {
return ArrayUtils.toList(array).iterator();
}
@Override
public ListIterator<Float> listIterator() {
return ArrayUtils.toList(array).listIterator();
}
@Override
public ListIterator<Float> listIterator(int index) {
return ArrayUtils.toList(array).listIterator(index);
}
@Override
public List<Float> subList(int fromIndex, int toIndex) {
return ArrayUtils.toList(array).subList(fromIndex, toIndex);
}
@Override
public boolean add(Float e) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, Float element) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends Float> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends Float> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public Float remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}
@@ -0,0 +1,158 @@
/*
* Copyright (C) 2020 The zfoo Authors
* 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 com.zfoo.protocol.collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* @author godotg
* @version 3.0
*/
public class FixedSizeIntList implements List<Integer> {
private final int[] array;
public FixedSizeIntList(int initialCapacity) {
this.array = new int[initialCapacity];
}
@Override
public int size() {
return array.length;
}
@Override
public boolean isEmpty() {
return ArrayUtils.isEmpty(array);
}
@Override
public boolean contains(Object ele) {
return ArrayUtils.toList(array).stream().anyMatch(it -> ele.equals(it));
}
@Override
public Integer get(int index) {
return array[index];
}
public int getRaw(int index) {
return array[index];
}
@Override
public Integer set(int index, Integer ele) {
var old = array[index];
array[index] = ele;
return old;
}
public void set(int index, int ele) {
array[index] = ele;
}
@Override
public Object[] toArray() {
return ArrayUtils.toList(array).toArray();
}
@Override
public <T> T[] toArray(T[] arrays) {
ArrayUtils.toList(array).toArray(arrays);
return arrays;
}
@Override
public boolean containsAll(Collection<?> collection) {
return ArrayUtils.toList(array).containsAll(collection);
}
@Override
public int indexOf(Object ele) {
return ArrayUtils.toList(array).indexOf(ele);
}
@Override
public int lastIndexOf(Object ele) {
return ArrayUtils.toList(array).lastIndexOf(ele);
}
@Override
public Iterator<Integer> iterator() {
return ArrayUtils.toList(array).iterator();
}
@Override
public ListIterator<Integer> listIterator() {
return ArrayUtils.toList(array).listIterator();
}
@Override
public ListIterator<Integer> listIterator(int index) {
return ArrayUtils.toList(array).listIterator(index);
}
@Override
public List<Integer> subList(int fromIndex, int toIndex) {
return ArrayUtils.toList(array).subList(fromIndex, toIndex);
}
@Override
public boolean add(Integer e) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, Integer element) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends Integer> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends Integer> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public Integer remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}
@@ -0,0 +1,157 @@
/*
* Copyright (C) 2020 The zfoo Authors
* 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 com.zfoo.protocol.collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* @author godotg
* @version 3.0
*/
public class FixedSizeLongList implements List<Long> {
private final long[] array;
public FixedSizeLongList(int initialCapacity) {
this.array = new long[initialCapacity];
}
@Override
public int size() {
return array.length;
}
@Override
public boolean isEmpty() {
return ArrayUtils.isEmpty(array);
}
@Override
public Long get(int index) {
return array[index];
}
public long getRaw(int index) {
return array[index];
}
@Override
public Long set(int index, Long ele) {
var old = array[index];
array[index] = ele;
return old;
}
public void set(int index, long ele) {
array[index] = ele;
}
@Override
public boolean contains(Object ele) {
return ArrayUtils.toList(array).stream().anyMatch(it -> ele.equals(it));
}
@Override
public Object[] toArray() {
return ArrayUtils.toList(array).toArray();
}
@Override
public <T> T[] toArray(T[] arrays) {
ArrayUtils.toList(array).toArray(arrays);
return arrays;
}
@Override
public boolean containsAll(Collection<?> collection) {
return ArrayUtils.toList(array).containsAll(collection);
}
@Override
public int indexOf(Object ele) {
return ArrayUtils.toList(array).indexOf(ele);
}
@Override
public int lastIndexOf(Object ele) {
return ArrayUtils.toList(array).lastIndexOf(ele);
}
@Override
public Iterator<Long> iterator() {
return ArrayUtils.toList(array).iterator();
}
@Override
public ListIterator<Long> listIterator() {
return ArrayUtils.toList(array).listIterator();
}
@Override
public ListIterator<Long> listIterator(int index) {
return ArrayUtils.toList(array).listIterator(index);
}
@Override
public List<Long> subList(int fromIndex, int toIndex) {
return ArrayUtils.toList(array).subList(fromIndex, toIndex);
}
@Override
public boolean add(Long e) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, Long element) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends Long> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends Long> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public Long remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}
@@ -0,0 +1,157 @@
/*
* Copyright (C) 2020 The zfoo Authors
* 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 com.zfoo.protocol.collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* @author godotg
* @version 3.0
*/
public class FixedSizeShortList implements List<Short> {
private final short[] array;
public FixedSizeShortList(int initialCapacity) {
this.array = new short[initialCapacity];
}
@Override
public int size() {
return array.length;
}
@Override
public boolean isEmpty() {
return ArrayUtils.isEmpty(array);
}
@Override
public Short get(int index) {
return array[index];
}
public short getRaw(int index) {
return array[index];
}
@Override
public Short set(int index, Short ele) {
var old = array[index];
array[index] = ele;
return old;
}
public void set(int index, short ele) {
array[index] = ele;
}
@Override
public boolean contains(Object ele) {
return ArrayUtils.toList(array).stream().anyMatch(it -> ele.equals(it));
}
@Override
public Object[] toArray() {
return ArrayUtils.toList(array).toArray();
}
@Override
public <T> T[] toArray(T[] arrays) {
ArrayUtils.toList(array).toArray(arrays);
return arrays;
}
@Override
public boolean containsAll(Collection<?> collection) {
return ArrayUtils.toList(array).containsAll(collection);
}
@Override
public int indexOf(Object ele) {
return ArrayUtils.toList(array).indexOf(ele);
}
@Override
public int lastIndexOf(Object ele) {
return ArrayUtils.toList(array).lastIndexOf(ele);
}
@Override
public Iterator<Short> iterator() {
return ArrayUtils.toList(array).iterator();
}
@Override
public ListIterator<Short> listIterator() {
return ArrayUtils.toList(array).listIterator();
}
@Override
public ListIterator<Short> listIterator(int index) {
return ArrayUtils.toList(array).listIterator(index);
}
@Override
public List<Short> subList(int fromIndex, int toIndex) {
return ArrayUtils.toList(array).subList(fromIndex, toIndex);
}
@Override
public boolean add(Short e) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, Short element) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends Short> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends Short> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public Short remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}
@@ -0,0 +1,149 @@
/*
* Copyright (C) 2020 The zfoo Authors
* 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 com.zfoo.protocol.collection;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* @author godotg
* @version 3.0
*/
public class FixedSizeStringList implements List<String> {
private final String[] array;
public FixedSizeStringList(int initialCapacity) {
this.array = new String[initialCapacity];
}
@Override
public int size() {
return array.length;
}
@Override
public boolean isEmpty() {
return ArrayUtils.isEmpty(array);
}
@Override
public String get(int index) {
return array[index];
}
@Override
public String set(int index, String ele) {
var old = array[index];
array[index] = ele;
return old;
}
@Override
public boolean contains(Object ele) {
return ArrayUtils.toList(array).stream().anyMatch(it -> ele.equals(it));
}
@Override
public Object[] toArray() {
return ArrayUtils.toList(array).toArray();
}
@Override
public <T> T[] toArray(T[] arrays) {
ArrayUtils.toList(array).toArray(arrays);
return arrays;
}
@Override
public boolean containsAll(Collection<?> collection) {
return ArrayUtils.toList(array).containsAll(collection);
}
@Override
public int indexOf(Object ele) {
return ArrayUtils.toList(array).indexOf(ele);
}
@Override
public int lastIndexOf(Object ele) {
return ArrayUtils.toList(array).lastIndexOf(ele);
}
@Override
public Iterator<String> iterator() {
return ArrayUtils.toList(array).iterator();
}
@Override
public ListIterator<String> listIterator() {
return ArrayUtils.toList(array).listIterator();
}
@Override
public ListIterator<String> listIterator(int index) {
return ArrayUtils.toList(array).listIterator(index);
}
@Override
public List<String> subList(int fromIndex, int toIndex) {
return ArrayUtils.toList(array).subList(fromIndex, toIndex);
}
@Override
public boolean add(String e) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, String element) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends String> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends String> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public String remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2020 The zfoo Authors
* 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 com.zfoo.protocol.collection;
import org.junit.Test;
/**
* @author godotg
* @version 3.0
*/
public class ListTest {
@Test
public void testFixedSizeIntList() {
var list = new FixedSizeIntList(3);
list.set(0, 1);
list.set(1, 2);
list.set(2, 3);
for (int i = 0; i < list.size(); i++) {
for (var ele : list) {
// test iterator
}
}
}
@Test
public void testFixedSizeStringList() {
var list = new FixedSizeStringList(3);
list.set(0, "1");
list.set(1, "2");
list.set(2, "3");
for (int i = 0; i < list.size(); i++) {
for (var ele : list) {
// test iterator
}
}
}
}