From 4a6f89c7d6b44a01cac90f625f2b530a20378cee Mon Sep 17 00:00:00 2001 From: godotg Date: Sat, 24 Sep 2022 17:42:37 +0800 Subject: [PATCH] =?UTF-8?q?del[set]:=20=E7=A7=BB=E9=99=A4=E4=B8=8D?= =?UTF-8?q?=E5=90=88=E9=80=82=E7=9A=84set=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zfoo/protocol/buffer/ByteBufUtils.java | 56 +++++++-- .../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 ------------------ 9 files changed, 48 insertions(+), 900 deletions(-) delete mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSet.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetBoolean.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetByte.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetDouble.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetFloat.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetInt.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetLong.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetShort.java diff --git a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java index 88f890f5..44b23fe9 100644 --- a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java @@ -430,7 +430,12 @@ public abstract class ByteBufUtils { } public static Set readPacketSet(ByteBuf byteBuf, IProtocolRegistration protocolRegistration) { - return new FixedHashSet((FixedSizeList) readPacketList(byteBuf, protocolRegistration)); + var length = readInt(byteBuf); + var set = (Set) CollectionUtils.newFixedSet(length); + for (var i = 0; i < length; i++) { + set.add((IPacket) protocolRegistration.read(byteBuf)); + } + return set; } public static void writeIntIntMap(ByteBuf byteBuf, Map map) { @@ -760,7 +765,12 @@ public abstract class ByteBufUtils { } public static Set readBooleanSet(ByteBuf byteBuf) { - return new FixedHashSetBoolean((FixedSizeListBoolean) readBooleanList(byteBuf)); + var length = readInt(byteBuf); + var set = (Set) CollectionUtils.newFixedSet(length); + for (var i = 0; i < length; i++) { + set.add(readBooleanBox(byteBuf)); + } + return set; } //---------------------------------byte-------------------------------------- @@ -833,7 +843,12 @@ public abstract class ByteBufUtils { } public static Set readByteSet(ByteBuf byteBuf) { - return new FixedHashSetByte((FixedSizeListByte) readByteList(byteBuf)); + var length = readInt(byteBuf); + var set = (Set) CollectionUtils.newFixedSet(length); + for (var i = 0; i < length; i++) { + set.add(readByteBox(byteBuf)); + } + return set; } //---------------------------------short-------------------------------------- @@ -914,7 +929,12 @@ public abstract class ByteBufUtils { } public static Set readShortSet(ByteBuf byteBuf) { - return new FixedHashSetShort((FixedSizeListShort) readShortList(byteBuf)); + var length = readInt(byteBuf); + var set = (Set) CollectionUtils.newFixedSet(length); + for (var i = 0; i < length; i++) { + set.add(readShortBox(byteBuf)); + } + return set; } @@ -988,7 +1008,12 @@ public abstract class ByteBufUtils { } public static Set readIntSet(ByteBuf byteBuf) { - return new FixedHashSetInt((FixedSizeListInt) readIntList(byteBuf)); + var length = readInt(byteBuf); + var set = (Set) CollectionUtils.newFixedSet(length); + for (var i = 0; i < length; i++) { + set.add(readIntBox(byteBuf)); + } + return set; } @@ -1062,7 +1087,12 @@ public abstract class ByteBufUtils { } public static Set readLongSet(ByteBuf byteBuf) { - return new FixedHashSetLong((FixedSizeListLong) readLongList(byteBuf)); + var length = readInt(byteBuf); + var set = (Set) CollectionUtils.newFixedSet(length); + for (var i = 0; i < length; i++) { + set.add(readLongBox(byteBuf)); + } + return set; } //---------------------------------float-------------------------------------- @@ -1143,7 +1173,12 @@ public abstract class ByteBufUtils { } public static Set readFloatSet(ByteBuf byteBuf) { - return new FixedHashSetFloat((FixedSizeListFloat) readFloatList(byteBuf)); + var length = readInt(byteBuf); + var set = (Set) CollectionUtils.newFixedSet(length); + for (var i = 0; i < length; i++) { + set.add(readFloatBox(byteBuf)); + } + return set; } //---------------------------------double-------------------------------------- @@ -1224,7 +1259,12 @@ public abstract class ByteBufUtils { } public static Set readDoubleSet(ByteBuf byteBuf) { - return new FixedHashSetDouble((FixedSizeListDouble) readDoubleList(byteBuf)); + var length = readInt(byteBuf); + var set = (Set) CollectionUtils.newFixedSet(length); + for (var i = 0; i < length; i++) { + set.add(readDoubleBox(byteBuf)); + } + return set; } //---------------------------------string-------------------------------------- diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSet.java b/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSet.java deleted file mode 100644 index e29f574a..00000000 --- a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSet.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * 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 deleted file mode 100644 index e0e55d43..00000000 --- a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetBoolean.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 deleted file mode 100644 index dae00b71..00000000 --- a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetByte.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 deleted file mode 100644 index 266c98fb..00000000 --- a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetDouble.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 deleted file mode 100644 index 63d182f2..00000000 --- a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetFloat.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 deleted file mode 100644 index 9a558518..00000000 --- a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetInt.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 deleted file mode 100644 index 542f45a5..00000000 --- a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetLong.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 deleted file mode 100644 index 0556e0c8..00000000 --- a/protocol/src/main/java/com/zfoo/protocol/collection/FixedHashSetShort.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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(); - } -}