From e540fa13db2f2a3884b1d46a670a2580fc3ef57d Mon Sep 17 00:00:00 2001 From: godotg Date: Fri, 23 Sep 2022 22:51:56 +0800 Subject: [PATCH] =?UTF-8?q?feat[set]:=20=E6=B7=BB=E5=8A=A0=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E6=9B=B4=E5=8A=A0=E9=AB=98=E7=9A=84=E5=8E=9F=E7=94=9F?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=9B=BA=E5=AE=9A?= =?UTF-8?q?=E5=A4=A7=E5=B0=8Fset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../protocol/collection/FixedHashSet.java | 108 +++++++++++++++++ .../collection/FixedHashSetBoolean.java | 112 ++++++++++++++++++ .../protocol/collection/FixedHashSetByte.java | 112 ++++++++++++++++++ .../collection/FixedHashSetDouble.java | 112 ++++++++++++++++++ .../collection/FixedHashSetFloat.java | 112 ++++++++++++++++++ .../protocol/collection/FixedHashSetInt.java | 112 ++++++++++++++++++ .../protocol/collection/FixedHashSetLong.java | 112 ++++++++++++++++++ .../collection/FixedHashSetShort.java | 112 ++++++++++++++++++ ...ListTest.java => FixedCollectionTest.java} | 20 +++- 9 files changed, 911 insertions(+), 1 deletion(-) create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSet.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetBoolean.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetByte.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetDouble.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetFloat.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetInt.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetLong.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetShort.java rename protocol/src/test/java/com/zfoo/protocol/collection/{ListTest.java => FixedCollectionTest.java} (74%) diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSet.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSet.java new file mode 100644 index 00000000..e29f574a --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSet.java @@ -0,0 +1,108 @@ +/* + * 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.AbstractSet; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Objects; + +/** + * @author godotg + * @version 3.0 + */ +public class FixedHashSet extends AbstractSet { + + private final Object[] array; + private final boolean[] existArray; + + public FixedHashSet(FixedSizeList list) { + var length = list.size(); + this.array = new Object[length]; + this.existArray = new boolean[length]; + var hashCollisionList = new ArrayList(); + for (var i = 0; i < length; i++) { + var ele = list.get(i); + var hash = Math.abs(Objects.hashCode(ele) % length); + if (existArray[hash]) { + hashCollisionList.add(ele); + } else { + array[hash] = ele; + existArray[hash] = true; + } + } + if (CollectionUtils.isNotEmpty(hashCollisionList)) { + for (int i = 0, j = 0; i < length; i++) { + if (hashCollisionList.size() == j) { + break; + } + if (existArray[i]) { + continue; + } + array[i] = hashCollisionList.get(j++); + existArray[i] = true; + } + } + } + + @Override + public boolean contains(Object ele) { + var hash = Math.abs(Objects.hashCode(ele) % array.length); + if (array[hash] == ele && existArray[hash]) { + return true; + } + for (var i = 0; i < array.length; i++) { + if (array[i] == ele && existArray[i]) { + return true; + } + } + return false; + } + + @Override + public int size() { + var length = 0; + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + length++; + } + } + return length; + } + + @Override + public Iterator iterator() { + var list = new ArrayList(); + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + list.add((E) array[i]); + } + } + return list.iterator(); + } + + @Override + public boolean add(E e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetBoolean.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetBoolean.java new file mode 100644 index 00000000..e0e55d43 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetBoolean.java @@ -0,0 +1,112 @@ +/* + * 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.AbstractSet; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class FixedHashSetBoolean extends AbstractSet { + + private final boolean[] array; + private final boolean[] existArray; + + public FixedHashSetBoolean(FixedSizeListBoolean list) { + var length = list.size(); + this.array = new boolean[length]; + this.existArray = new boolean[length]; + var hashCollisionList = new ArrayList(); + for (var i = 0; i < length; i++) { + var ele = list.getRaw(i); + var hash = Boolean.hashCode(ele) % length; + if (existArray[hash]) { + hashCollisionList.add(ele); + } else { + array[hash] = ele; + existArray[hash] = true; + } + } + if (CollectionUtils.isNotEmpty(hashCollisionList)) { + for (int i = 0, j = 0; i < length; i++) { + if (hashCollisionList.size() == j) { + break; + } + if (existArray[i]) { + continue; + } + array[i] = hashCollisionList.get(j++); + existArray[i] = true; + } + } + } + + @Override + public boolean contains(Object ele) { + var e = (Boolean) ele; + return contains(e.booleanValue()); + } + + public boolean contains(boolean ele) { + var hash = Boolean.hashCode(ele) % array.length; + if (array[hash] == ele && existArray[hash]) { + return true; + } + for (var i = 0; i < array.length; i++) { + if (array[i] == ele && existArray[i]) { + return true; + } + } + return false; + } + + @Override + public int size() { + var length = 0; + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + length++; + } + } + return length; + } + + @Override + public Iterator iterator() { + var list = new ArrayList(); + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + list.add(array[i]); + } + } + return list.iterator(); + } + + @Override + public boolean add(Boolean e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetByte.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetByte.java new file mode 100644 index 00000000..dae00b71 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetByte.java @@ -0,0 +1,112 @@ +/* + * 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.AbstractSet; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class FixedHashSetByte extends AbstractSet { + + private final byte[] array; + private final boolean[] existArray; + + public FixedHashSetByte(FixedSizeListByte list) { + var length = list.size(); + this.array = new byte[length]; + this.existArray = new boolean[length]; + var hashCollisionList = new ArrayList(); + for (var i = 0; i < length; i++) { + var ele = list.getRaw(i); + var hash = Math.abs(ele % length); + if (existArray[hash]) { + hashCollisionList.add(ele); + } else { + array[hash] = ele; + existArray[hash] = true; + } + } + if (CollectionUtils.isNotEmpty(hashCollisionList)) { + for (int i = 0, j = 0; i < length; i++) { + if (hashCollisionList.size() == j) { + break; + } + if (existArray[i]) { + continue; + } + array[i] = hashCollisionList.get(j++); + existArray[i] = true; + } + } + } + + @Override + public boolean contains(Object ele) { + var e = (Byte) ele; + return contains(e.byteValue()); + } + + public boolean contains(byte ele) { + var hash = Math.abs(ele % array.length); + if (array[hash] == ele && existArray[hash]) { + return true; + } + for (var i = 0; i < array.length; i++) { + if (array[i] == ele && existArray[i]) { + return true; + } + } + return false; + } + + @Override + public int size() { + var length = 0; + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + length++; + } + } + return length; + } + + @Override + public Iterator iterator() { + var list = new ArrayList(); + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + list.add(array[i]); + } + } + return list.iterator(); + } + + @Override + public boolean add(Byte e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetDouble.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetDouble.java new file mode 100644 index 00000000..266c98fb --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetDouble.java @@ -0,0 +1,112 @@ +/* + * 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.AbstractSet; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class FixedHashSetDouble extends AbstractSet { + + private final double[] array; + private final boolean[] existArray; + + public FixedHashSetDouble(FixedSizeListDouble list) { + var length = list.size(); + this.array = new double[length]; + this.existArray = new boolean[length]; + var hashCollisionList = new ArrayList(); + for (var i = 0; i < length; i++) { + var ele = list.getRaw(i); + var hash = Math.abs((int) ele % length); + if (existArray[hash]) { + hashCollisionList.add(ele); + } else { + array[hash] = ele; + existArray[hash] = true; + } + } + if (CollectionUtils.isNotEmpty(hashCollisionList)) { + for (int i = 0, j = 0; i < length; i++) { + if (hashCollisionList.size() == j) { + break; + } + if (existArray[i]) { + continue; + } + array[i] = hashCollisionList.get(j++); + existArray[i] = true; + } + } + } + + @Override + public boolean contains(Object ele) { + var e = (Double) ele; + return contains(e.doubleValue()); + } + + public boolean contains(double ele) { + var hash = Math.abs((int) ele % array.length); + if (array[hash] == ele && existArray[hash]) { + return true; + } + for (var i = 0; i < array.length; i++) { + if (array[i] == ele && existArray[i]) { + return true; + } + } + return false; + } + + @Override + public int size() { + var length = 0; + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + length++; + } + } + return length; + } + + @Override + public Iterator iterator() { + var list = new ArrayList(); + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + list.add(array[i]); + } + } + return list.iterator(); + } + + @Override + public boolean add(Double e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetFloat.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetFloat.java new file mode 100644 index 00000000..63d182f2 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetFloat.java @@ -0,0 +1,112 @@ +/* + * 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.AbstractSet; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class FixedHashSetFloat extends AbstractSet { + + private final float[] array; + private final boolean[] existArray; + + public FixedHashSetFloat(FixedSizeListFloat list) { + var length = list.size(); + this.array = new float[length]; + this.existArray = new boolean[length]; + var hashCollisionList = new ArrayList(); + for (var i = 0; i < length; i++) { + var ele = list.getRaw(i); + var hash = Math.abs((int) ele % length); + if (existArray[hash]) { + hashCollisionList.add(ele); + } else { + array[hash] = ele; + existArray[hash] = true; + } + } + if (CollectionUtils.isNotEmpty(hashCollisionList)) { + for (int i = 0, j = 0; i < length; i++) { + if (hashCollisionList.size() == j) { + break; + } + if (existArray[i]) { + continue; + } + array[i] = hashCollisionList.get(j++); + existArray[i] = true; + } + } + } + + @Override + public boolean contains(Object ele) { + var e = (Float) ele; + return contains(e.floatValue()); + } + + public boolean contains(float ele) { + var hash = Math.abs((int) ele % array.length); + if (array[hash] == ele && existArray[hash]) { + return true; + } + for (var i = 0; i < array.length; i++) { + if (array[i] == ele && existArray[i]) { + return true; + } + } + return false; + } + + @Override + public int size() { + var length = 0; + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + length++; + } + } + return length; + } + + @Override + public Iterator iterator() { + var list = new ArrayList(); + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + list.add(array[i]); + } + } + return list.iterator(); + } + + @Override + public boolean add(Float e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetInt.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetInt.java new file mode 100644 index 00000000..9a558518 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetInt.java @@ -0,0 +1,112 @@ +/* + * 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.AbstractSet; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class FixedHashSetInt extends AbstractSet { + + private final int[] array; + private final boolean[] existArray; + + public FixedHashSetInt(FixedSizeListInt list) { + var length = list.size(); + this.array = new int[length]; + this.existArray = new boolean[length]; + var hashCollisionList = new ArrayList(); + for (var i = 0; i < length; i++) { + var ele = list.getRaw(i); + var hash = Math.abs(ele % length); + if (existArray[hash]) { + hashCollisionList.add(ele); + } else { + array[hash] = ele; + existArray[hash] = true; + } + } + if (CollectionUtils.isNotEmpty(hashCollisionList)) { + for (int i = 0, j = 0; i < length; i++) { + if (hashCollisionList.size() == j) { + break; + } + if (existArray[i]) { + continue; + } + array[i] = hashCollisionList.get(j++); + existArray[i] = true; + } + } + } + + @Override + public boolean contains(Object ele) { + var e = (Integer) ele; + return contains(e.intValue()); + } + + public boolean contains(int ele) { + var hash = Math.abs(ele % array.length); + if (array[hash] == ele && existArray[hash]) { + return true; + } + for (var i = 0; i < array.length; i++) { + if (array[i] == ele && existArray[i]) { + return true; + } + } + return false; + } + + @Override + public int size() { + var length = 0; + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + length++; + } + } + return length; + } + + @Override + public Iterator iterator() { + var list = new ArrayList(); + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + list.add(array[i]); + } + } + return list.iterator(); + } + + @Override + public boolean add(Integer e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetLong.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetLong.java new file mode 100644 index 00000000..542f45a5 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetLong.java @@ -0,0 +1,112 @@ +/* + * 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.AbstractSet; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class FixedHashSetLong extends AbstractSet { + + private final long[] array; + private final boolean[] existArray; + + public FixedHashSetLong(FixedSizeListLong list) { + var length = list.size(); + this.array = new long[length]; + this.existArray = new boolean[length]; + var hashCollisionList = new ArrayList(); + for (var i = 0; i < length; i++) { + var ele = list.getRaw(i); + var hash = Math.abs((int) ele % length); + if (existArray[hash]) { + hashCollisionList.add(ele); + } else { + array[hash] = ele; + existArray[hash] = true; + } + } + if (CollectionUtils.isNotEmpty(hashCollisionList)) { + for (int i = 0, j = 0; i < length; i++) { + if (hashCollisionList.size() == j) { + break; + } + if (existArray[i]) { + continue; + } + array[i] = hashCollisionList.get(j++); + existArray[i] = true; + } + } + } + + @Override + public boolean contains(Object ele) { + var e = (Long) ele; + return contains(e.intValue()); + } + + public boolean contains(long ele) { + var hash = Math.abs((int) ele % array.length); + if (array[hash] == ele && existArray[hash]) { + return true; + } + for (var i = 0; i < array.length; i++) { + if (array[i] == ele && existArray[i]) { + return true; + } + } + return false; + } + + @Override + public int size() { + var length = 0; + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + length++; + } + } + return length; + } + + @Override + public Iterator iterator() { + var list = new ArrayList(); + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + list.add(array[i]); + } + } + return list.iterator(); + } + + @Override + public boolean add(Long e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetShort.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetShort.java new file mode 100644 index 00000000..0556e0c8 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetShort.java @@ -0,0 +1,112 @@ +/* + * 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.AbstractSet; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class FixedHashSetShort extends AbstractSet { + + private final short[] array; + private final boolean[] existArray; + + public FixedHashSetShort(FixedSizeListShort list) { + var length = list.size(); + this.array = new short[length]; + this.existArray = new boolean[length]; + var hashCollisionList = new ArrayList(); + for (var i = 0; i < length; i++) { + var ele = list.getRaw(i); + var hash = Math.abs(ele % length); + if (existArray[hash]) { + hashCollisionList.add(ele); + } else { + array[hash] = ele; + existArray[hash] = true; + } + } + if (CollectionUtils.isNotEmpty(hashCollisionList)) { + for (int i = 0, j = 0; i < length; i++) { + if (hashCollisionList.size() == j) { + break; + } + if (existArray[i]) { + continue; + } + array[i] = hashCollisionList.get(j++); + existArray[i] = true; + } + } + } + + @Override + public boolean contains(Object ele) { + var e = (Short) ele; + return contains(e.shortValue()); + } + + public boolean contains(short ele) { + var hash = Math.abs(ele % array.length); + if (array[hash] == ele && existArray[hash]) { + return true; + } + for (var i = 0; i < array.length; i++) { + if (array[i] == ele && existArray[i]) { + return true; + } + } + return false; + } + + @Override + public int size() { + var length = 0; + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + length++; + } + } + return length; + } + + @Override + public Iterator iterator() { + var list = new ArrayList(); + for (var i = 0; i < array.length; i++) { + if (existArray[i]) { + list.add(array[i]); + } + } + return list.iterator(); + } + + @Override + public boolean add(Short e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } +} diff --git a/protocol/src/test/java/com/zfoo/protocol/collection/ListTest.java b/protocol/src/test/java/com/zfoo/protocol/collection/FixedCollectionTest.java similarity index 74% rename from protocol/src/test/java/com/zfoo/protocol/collection/ListTest.java rename to protocol/src/test/java/com/zfoo/protocol/collection/FixedCollectionTest.java index c3cf8d7c..8e4d21c1 100644 --- a/protocol/src/test/java/com/zfoo/protocol/collection/ListTest.java +++ b/protocol/src/test/java/com/zfoo/protocol/collection/FixedCollectionTest.java @@ -12,13 +12,14 @@ package com.zfoo.protocol.collection; +import org.junit.Assert; import org.junit.Test; /** * @author godotg * @version 3.0 */ -public class ListTest { +public class FixedCollectionTest { @Test public void testFixedSizeListInt() { @@ -46,4 +47,21 @@ public class ListTest { } } + + @Test + public void testFixedHashInt() { + var list = new FixedSizeListInt(3); + list.set(0, 1); + list.set(1, 2); + list.set(2, 4); + var set = new FixedHashSetInt(list); + for (int i = 0; i < set.size(); i++) { + for (var ele : set) { + // test iterator + } + } + + Assert.assertTrue(set.contains(4)); + } + }