Skip to content

Commit

Permalink
Fix MutableHashMap and MutableHashSet
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jan 27, 2025
1 parent aa58315 commit 3d2bb1b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package kala.collection.internal.hash;

import kala.function.Hasher;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.Serial;
Expand All @@ -42,7 +43,7 @@ public abstract class HashBase<K, N extends HashNode<K, N>> implements Serializa

protected transient int contentSize = 0;

protected HashBase(Hasher<? super K> hasher, int initialCapacity, double loadFactor) {
protected HashBase(@NotNull Hasher<? super K> hasher, int initialCapacity, double loadFactor) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,4 @@ private HashUtils() {
public static int tableSizeFor(int capacity) {
return Integer.min(Integer.highestOneBit(Integer.max(capacity - 1, 4)) * 2, 1 << 30);
}

public static int improveHash(int originalHash) {
return originalHash ^ (originalHash >>> 16);
}

public static int unimproveHash(int improvedHash) {
return improveHash(improvedHash);
}

public static int computeHash(Object o) {
return o == null ? 0 : improveHash(o.hashCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
import java.util.function.Supplier;
import java.util.stream.Collector;

import static kala.collection.internal.hash.HashUtils.computeHash;

@SuppressWarnings("unchecked")
@Debug.Renderer(hasChildren = "isNotEmpty()", childrenArray = "toArray()")
public final class MutableHashMap<K, V> extends HashBase<K, MutableHashMap.Node<K, V>> implements MutableMap<K, V>, Cloneable, Serializable {
Expand All @@ -65,15 +63,15 @@ public MutableHashMap(int initialCapacity, double loadFactor) {
this(Hasher.optimizedHasher(), initialCapacity, loadFactor);
}

public MutableHashMap(Hasher<? super K> hasher) {
public MutableHashMap(@NotNull Hasher<? super K> hasher) {
this(hasher, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}

public MutableHashMap(Hasher<? super K> hasher, int initialCapacity) {
public MutableHashMap(@NotNull Hasher<? super K> hasher, int initialCapacity) {
this(hasher, initialCapacity, DEFAULT_LOAD_FACTOR);
}

public MutableHashMap(Hasher<? super K> hasher, int initialCapacity, double loadFactor) {
public MutableHashMap(@NotNull Hasher<? super K> hasher, int initialCapacity, double loadFactor) {
super(hasher, initialCapacity, loadFactor);
}

Expand Down Expand Up @@ -529,7 +527,7 @@ public void set(K key, V value) {
if (contentSize + 1 >= threshold) {
growTable(table.length * 2);
}
final int hash = computeHash(key);
final int hash = hasher.hash(key);
final int idx = index(hash);
set0(key, value, hash, idx);
}
Expand All @@ -539,7 +537,7 @@ public void set(K key, V value) {
if (contentSize + 1 >= threshold) {
growTable(table.length * 2);
}
final int hash = computeHash(key);
final int hash = hasher.hash(key);
final int idx = index(hash);
return put0(key, value, hash, idx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.Serial;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
Expand All @@ -34,6 +35,7 @@

@SuppressWarnings("unchecked")
public final class MutableHashSet<E> extends HashBase<E, MutableHashSet.Node<E>> implements MutableSet<E>, Serializable {
@Serial
private static final long serialVersionUID = 2267952928135789371L;
private static final MutableHashSet.Factory<?> FACTORY = new Factory<>();

Expand Down Expand Up @@ -236,7 +238,7 @@ private boolean add(E value, int hash) {
final Node<E> old = n;
Node<E> prev = null;
while ((n != null) && n.hash <= hash) {
if (n.hash == hash && hasher.test(value, n.key)) {
if (n.hash == hash && hasher.equals(value, n.key)) {
return false;
}
prev = n;
Expand All @@ -257,12 +259,12 @@ public boolean add(E value) {
if (contentSize + 1 >= threshold) {
growTable(table.length * 2);
}
return add(value, HashUtils.computeHash(value));
return add(value, hasher.hash(value));
}

@Override
public boolean remove(Object value) {
return removeNode((E) value, HashUtils.computeHash(value)) != null;
return removeNode((E) value, hasher.hash((E) value)) != null;
}

//endregion
Expand Down Expand Up @@ -331,6 +333,7 @@ private void readObject(java.io.ObjectInputStream in)
}
}

@Serial
private void writeObject(java.io.ObjectOutputStream out)
throws IOException {
out.writeInt(contentSize);
Expand Down

0 comments on commit 3d2bb1b

Please sign in to comment.