Skip to content

Commit

Permalink
OpenHashMap.mergeX: Avoid double-hashing key
Browse files Browse the repository at this point in the history
Towards vigna#336
  • Loading branch information
mhansen committed Nov 18, 2024
1 parent 355d8eb commit 0659a61
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions drv/AbstractMap.drv
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC extends ABSTRACT_FUNCTION K
#if VALUES_INT_LONG_DOUBLE
/** {@inheritDoc}
* @implSpec This method just delegates to the interface default method,
* as the default method, but it is final, so it cannot be overridden.
* as the default method.
*/
@Override
public final VALUE_TYPE MERGE_VALUE(final KEY_GENERIC_TYPE key, final VALUE_TYPE value, final VALUE_PACKAGE.VALUE_BINARY_OPERATOR remappingFunction) {
public VALUE_TYPE MERGE_VALUE(final KEY_GENERIC_TYPE key, final VALUE_TYPE value, final VALUE_PACKAGE.VALUE_BINARY_OPERATOR remappingFunction) {
return MERGE_VALUE(key, value, (JDK_PRIMITIVE_VALUE_BINARY_OPERATOR)remappingFunction);
}
#endif
Expand Down
21 changes: 21 additions & 0 deletions drv/OpenHashMap.drv
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import VALUE_PACKAGE.VALUE_COLLECTION;
import VALUE_PACKAGE.VALUE_ABSTRACT_COLLECTION;

#if VALUES_PRIMITIVE
import VALUE_PACKAGE.VALUE_BINARY_OPERATOR;
import VALUE_PACKAGE.VALUE_ITERATOR;
import VALUE_PACKAGE.VALUE_SPLITERATOR;
import VALUE_PACKAGE.VALUE_SPLITERATORS;
Expand Down Expand Up @@ -1253,6 +1254,26 @@ public class OPEN_HASH_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENE
return value[pos] = newVal;
}

#if VALUES_INT_LONG_DOUBLE
/** {@inheritDoc} */
@Override
public VALUE_GENERIC_TYPE MERGE_VALUE(final KEY_GENERIC_TYPE k, final VALUE_GENERIC_TYPE v, VALUE_BINARY_OPERATOR remappingFunction) {
java.util.Objects.requireNonNull(remappingFunction);
REQUIRE_VALUE_NON_NULL(v)

final int pos = find(k);
if (pos < 0) {
insert(-pos - 1, k, v);
return v;
}

final VALUE_GENERIC_TYPE newValue = remappingFunction.apply(value[pos], v);

return value[pos] = newValue;
}

#endif

/** {@inheritDoc} */
@Override
public VALUE_GENERIC_TYPE merge(final KEY_GENERIC_TYPE k, final VALUE_GENERIC_TYPE v, final java.util.function.BiFunction<? super VALUE_GENERIC_CLASS, ? super VALUE_GENERIC_CLASS, ? extends VALUE_GENERIC_CLASS> remappingFunction) {
Expand Down

0 comments on commit 0659a61

Please sign in to comment.