feat[set]: 基于netty的高性能map实现set

This commit is contained in:
godotg
2022-09-24 18:05:55 +08:00
parent 4a6f89c7d6
commit 18652f366d
5 changed files with 301 additions and 0 deletions
@@ -35,6 +35,7 @@ public abstract class ArrayUtils {
public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
public static final char[] EMPTY_CHAR_ARRAY = new char[0];
public static final Object EMPTY_OBJECT = new Object();
/**
* length
@@ -0,0 +1,75 @@
/*
* 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 io.netty.util.collection.ByteObjectHashMap;
import java.util.AbstractSet;
import java.util.Iterator;
/**
* @author godotg
* @version 3.0
*/
public class HashByteSet extends AbstractSet<Byte> {
private final ByteObjectHashMap<Object> map;
public HashByteSet() {
map = new ByteObjectHashMap<>();
}
public HashByteSet(int initialCapacity) {
map = new ByteObjectHashMap<>(initialCapacity);
}
@Override
public Iterator<Byte> iterator() {
return map.keySet().iterator();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean contains(Object o) {
return map.containsKey(o);
}
@Override
public boolean add(Byte e) {
return map.put(e, ArrayUtils.EMPTY_OBJECT) == null;
}
public boolean add(byte e) {
return map.put(e, ArrayUtils.EMPTY_OBJECT) == null;
}
@Override
public boolean remove(Object o) {
return map.remove(o) == ArrayUtils.EMPTY_OBJECT;
}
@Override
public void clear() {
map.clear();
}
}
@@ -0,0 +1,75 @@
/*
* 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 io.netty.util.collection.IntObjectHashMap;
import java.util.AbstractSet;
import java.util.Iterator;
/**
* @author godotg
* @version 3.0
*/
public class HashIntSet extends AbstractSet<Integer> {
private final IntObjectHashMap<Object> map;
public HashIntSet() {
map = new IntObjectHashMap<>();
}
public HashIntSet(int initialCapacity) {
map = new IntObjectHashMap<>(initialCapacity);
}
@Override
public Iterator<Integer> iterator() {
return map.keySet().iterator();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean contains(Object o) {
return map.containsKey(o);
}
@Override
public boolean add(Integer e) {
return map.put(e, ArrayUtils.EMPTY_OBJECT) == null;
}
public boolean add(int e) {
return map.put(e, ArrayUtils.EMPTY_OBJECT) == null;
}
@Override
public boolean remove(Object o) {
return map.remove(o) == ArrayUtils.EMPTY_OBJECT;
}
@Override
public void clear() {
map.clear();
}
}
@@ -0,0 +1,75 @@
/*
* 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 io.netty.util.collection.LongObjectHashMap;
import java.util.AbstractSet;
import java.util.Iterator;
/**
* @author godotg
* @version 3.0
*/
public class HashLongSet extends AbstractSet<Long> {
private final LongObjectHashMap<Object> map;
public HashLongSet() {
map = new LongObjectHashMap<>();
}
public HashLongSet(int initialCapacity) {
map = new LongObjectHashMap<>(initialCapacity);
}
@Override
public Iterator<Long> iterator() {
return map.keySet().iterator();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean contains(Object o) {
return map.containsKey(o);
}
@Override
public boolean add(Long e) {
return map.put(e, ArrayUtils.EMPTY_OBJECT) == null;
}
public boolean add(long e) {
return map.put(e, ArrayUtils.EMPTY_OBJECT) == null;
}
@Override
public boolean remove(Object o) {
return map.remove(o) == ArrayUtils.EMPTY_OBJECT;
}
@Override
public void clear() {
map.clear();
}
}
@@ -0,0 +1,75 @@
/*
* 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 io.netty.util.collection.ShortObjectHashMap;
import java.util.AbstractSet;
import java.util.Iterator;
/**
* @author godotg
* @version 3.0
*/
public class HashShortSet extends AbstractSet<Short> {
private final ShortObjectHashMap<Object> map;
public HashShortSet() {
map = new ShortObjectHashMap<>();
}
public HashShortSet(int initialCapacity) {
map = new ShortObjectHashMap<>(initialCapacity);
}
@Override
public Iterator<Short> iterator() {
return map.keySet().iterator();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean contains(Object o) {
return map.containsKey(o);
}
@Override
public boolean add(Short e) {
return map.put(e, ArrayUtils.EMPTY_OBJECT) == null;
}
public boolean add(short e) {
return map.put(e, ArrayUtils.EMPTY_OBJECT) == null;
}
@Override
public boolean remove(Object o) {
return map.remove(o) == ArrayUtils.EMPTY_OBJECT;
}
@Override
public void clear() {
map.clear();
}
}