Skip to content

Commit

Permalink
Tidy up after merge of PR #259.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed Jun 24, 2022
1 parent 0b22b3e commit 91efd0e
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 46 deletions.
32 changes: 18 additions & 14 deletions agrona/src/main/java/org/agrona/collections/Int2IntHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public void compact()
}

/**
* Primitive specialised version of {@link #computeIfAbsent(Object, Function)}
* Primitive specialised version of {@link #computeIfAbsent(Object, Function)}.
*
* @param key to search on.
* @param mappingFunction to provide a value if the get returns null.
Expand All @@ -370,6 +370,7 @@ public int computeIfAbsent(final int key, final IntUnaryOperator mappingFunction

index = next(index, mask);
}

if (value == missingValue && (value = mappingFunction.applyAsInt(key)) != missingValue)
{
entries[index] = key;
Expand All @@ -382,7 +383,7 @@ public int computeIfAbsent(final int key, final IntUnaryOperator mappingFunction
}

/**
* Primitive specialised version of {@link java.util.Map#computeIfPresent}
* Primitive specialised version of {@link java.util.Map#computeIfPresent}.
*
* @param key to search on.
* @param remappingFunction to compute a value if a mapping is found.
Expand All @@ -405,6 +406,7 @@ public int computeIfPresent(final int key, final IntBinaryOperator remappingFunc

index = next(index, mask);
}

if (value != missingValue)
{
value = remappingFunction.applyAsInt(key, value);
Expand All @@ -415,11 +417,12 @@ public int computeIfPresent(final int key, final IntBinaryOperator remappingFunc
compactChain(index);
}
}

return value;
}

/**
* Primitive specialised version of {@link java.util.Map#compute}
* Primitive specialised version of {@link java.util.Map#compute}.
*
* @param key to search on.
* @param remappingFunction to compute a value.
Expand All @@ -442,10 +445,10 @@ public int compute(final int key, final IntBinaryOperator remappingFunction)

index = next(index, mask);
}

final int newValue = remappingFunction.applyAsInt(key, oldValue);
if (newValue != missingValue)
{
// add or replace old mapping
entries[index + 1] = newValue;
if (oldValue == missingValue)
{
Expand All @@ -460,6 +463,7 @@ else if (oldValue != missingValue)
size--;
compactChain(index);
}

return newValue;
}

Expand Down Expand Up @@ -628,7 +632,7 @@ private void compactChain(@DoNotSub int deleteKeyIndex)
}

/**
* Get the minimum value stored in the map. If the map is empty then it will return {@link #missingValue()}
* Get the minimum value stored in the map. If the map is empty then it will return {@link #missingValue()}.
*
* @return the minimum value stored in the map.
*/
Expand All @@ -652,7 +656,7 @@ public int minValue()
}

/**
* Get the maximum value stored in the map. If the map is empty then it will return {@link #missingValue()}
* Get the maximum value stored in the map. If the map is empty then it will return {@link #missingValue()}.
*
* @return the maximum value stored in the map.
*/
Expand Down Expand Up @@ -702,10 +706,10 @@ public String toString()
}

/**
* Primitive specialised version of {@link #replace(Object, Object)}
* Primitive specialised version of {@link #replace(Object, Object)}.
*
* @param key key with which the specified value is associated
* @param value value to be associated with the specified key
* @param key key with which the specified value is associated.
* @param value value to be associated with the specified key.
* @return the previous value associated with the specified key, or
* {@link #missingValue()} if there was no mapping for the key.
*/
Expand All @@ -721,12 +725,12 @@ public int replace(final int key, final int value)
}

/**
* Primitive specialised version of {@link #replace(Object, Object, Object)}
* Primitive specialised version of {@link #replace(Object, Object, Object)}.
*
* @param key key with which the specified value is associated
* @param oldValue value expected to be associated with the specified key
* @param newValue value to be associated with the specified key
* @return {@code true} if the value was replaced
* @param key key with which the specified value is associated.
* @param oldValue value expected to be associated with the specified key.
* @param newValue value to be associated with the specified key.
* @return {@code true} if the value was replaced.
*/
public boolean replace(final int key, final int oldValue, final int newValue)
{
Expand Down
20 changes: 13 additions & 7 deletions agrona/src/main/java/org/agrona/collections/Int2ObjectHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ public V computeIfAbsent(final int key, final IntFunction<? extends V> mappingFu

index = ++index & mask;
}

V value = unmapNullValue(mappedValue);

if (value == null && (value = mappingFunction.apply(key)) != null)
{
values[index] = value;
Expand All @@ -342,15 +344,16 @@ public V computeIfAbsent(final int key, final IntFunction<? extends V> mappingFu
* If the value for the specified key is present and non-null, attempts to compute a new
* mapping given the key and its current mapped value.
* <p>
* If the function returns {@code null}, the mapping is removed
* If the function returns {@code null}, the mapping is removed.
* <p>
* Primitive specialized version of {@link java.util.Map#computeIfPresent}.
*
* @param key to search on.
* @param remappingFunction to provide a value if the get returns missingValue.
* @return the new value associated with the specified key, or {@code null} if none
* @return the new value associated with the specified key, or {@code null} if none.
*/
public V computeIfPresent(final int key, final IntObjToObjFunction<? super V, ? extends V> remappingFunction)
public V computeIfPresent(
final int key, final IntObjectToObjectFunction<? super V, ? extends V> remappingFunction)
{
final int[] keys = this.keys;
final Object[] values = this.values;
Expand All @@ -367,7 +370,9 @@ public V computeIfPresent(final int key, final IntObjToObjFunction<? super V, ?

index = ++index & mask;
}

V value = unmapNullValue(mappedValue);

if (value != null)
{
value = remappingFunction.apply(key, value);
Expand All @@ -378,6 +383,7 @@ public V computeIfPresent(final int key, final IntObjToObjFunction<? super V, ?
compactChain(index);
}
}

return value;
}

Expand All @@ -392,9 +398,9 @@ public V computeIfPresent(final int key, final IntObjToObjFunction<? super V, ?
*
* @param key to search on.
* @param remappingFunction to provide a value if the get returns missingValue.
* @return the new value associated with the specified key, or {@code null} if none
* @return the new value associated with the specified key, or {@code null} if none.
*/
public V compute(final int key, final IntObjToObjFunction<? super V, ? extends V> remappingFunction)
public V compute(final int key, final IntObjectToObjectFunction<? super V, ? extends V> remappingFunction)
{
final int[] keys = this.keys;
final Object[] values = this.values;
Expand All @@ -411,12 +417,12 @@ public V compute(final int key, final IntObjToObjFunction<? super V, ? extends V

index = ++index & mask;
}

final V oldValue = unmapNullValue(mappedvalue);
final V newValue = remappingFunction.apply(key, oldValue);

if (newValue != null)
{
// add or replace old mapping
values[index] = newValue;
if (mappedvalue == null)
{
Expand All @@ -429,11 +435,11 @@ public V compute(final int key, final IntObjToObjFunction<? super V, ? extends V
}
else if (mappedvalue != null || containsKey(key))
{
// delete mapping
values[index] = null;
size--;
compactChain(index);
}

return newValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
*/
@FunctionalInterface
public interface
IntObjToObjFunction<T, R>
IntObjectToObjectFunction<T, R>
{
/**
* Applies this function to the given arguments.
*
* @param i the second function argument
* @param t the first function argument
* @return the function result
* @param i the second function argument.
* @param t the first function argument.
* @return the function result.
*/
R apply(int i, T t);
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public int computeIfAbsent(final K key, final ToIntFunction<? super K> mappingFu

index = ++index & mask;
}

if (missingValue == value && (value = mappingFunction.applyAsInt(key)) != missingValue)
{
keys[index] = key;
Expand All @@ -321,7 +322,7 @@ public int computeIfAbsent(final K key, final ToIntFunction<? super K> mappingFu
* @return the new value associated with the specified key, or missingValue if none
*/
@SuppressWarnings("overloads")
public int computeIfPresent(final K key, final ObjIntToIntFunction<? super K> remappingFunction)
public int computeIfPresent(final K key, final ObjectIntToIntFunction<? super K> remappingFunction)
{
final int missingValue = this.missingValue;
final K[] keys = this.keys;
Expand All @@ -339,6 +340,7 @@ public int computeIfPresent(final K key, final ObjIntToIntFunction<? super K> re

index = ++index & mask;
}

if (value != missingValue)
{
value = remappingFunction.apply(key, value);
Expand All @@ -350,6 +352,7 @@ public int computeIfPresent(final K key, final ObjIntToIntFunction<? super K> re
compactChain(index);
}
}

return value;
}

Expand All @@ -367,7 +370,7 @@ public int computeIfPresent(final K key, final ObjIntToIntFunction<? super K> re
* @return the new value associated with the specified key, or missingValue if none
*/
@SuppressWarnings("overloads")
public int compute(final K key, final ObjIntToIntFunction<? super K> remappingFunction)
public int compute(final K key, final ObjectIntToIntFunction<? super K> remappingFunction)
{
final int missingValue = this.missingValue;
final K[] keys = this.keys;
Expand All @@ -385,10 +388,10 @@ public int compute(final K key, final ObjIntToIntFunction<? super K> remappingFu

index = ++index & mask;
}

final int newValue = remappingFunction.apply(key, oldValue);
if (newValue != missingValue)
{
// add or replace old mapping
values[index] = newValue;
if (oldValue == missingValue)
{
Expand All @@ -401,13 +404,13 @@ public int compute(final K key, final ObjIntToIntFunction<? super K> remappingFu
}
else if (oldValue != missingValue)
{
// something to remove
keys[index] = null;
values[index] = missingValue;
--size;
compactChain(index);

}

return newValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ public int hashCode()
*/
public V computeIfAbsent(final K key, final Function<? super K, ? extends V> mappingFunction)
{

final Object[] entries = this.entries;
final int mask = entries.length - 1;
int keyIndex = Hashing.evenHash(key.hashCode(), mask);
Expand All @@ -508,7 +507,6 @@ public V computeIfAbsent(final K key, final Function<? super K, ? extends V> map
V value = unmapNullValue(mappedValue);
if (value == null && (value = mappingFunction.apply(key)) != null)
{
// insert new mapping (or update an existing mapping with NullValue)
entries[keyIndex + 1] = value;
if (mappedValue == null)
{
Expand All @@ -517,6 +515,7 @@ public V computeIfAbsent(final K key, final Function<? super K, ? extends V> map
increaseCapacity();
}
}

return value;
}

Expand All @@ -526,7 +525,6 @@ public V computeIfAbsent(final K key, final Function<? super K, ? extends V> map
@Override
public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction)
{

final Object[] entries = this.entries;
final int mask = entries.length - 1;
int keyIndex = Hashing.evenHash(key.hashCode(), mask);
Expand Down Expand Up @@ -554,6 +552,7 @@ public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ?
compactChain(keyIndex);
}
}

return value;
}

Expand All @@ -563,7 +562,6 @@ public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ?
@Override
public V compute(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction)
{

final Object[] entries = this.entries;
final int mask = entries.length - 1;
int keyIndex = Hashing.evenHash(key.hashCode(), mask);
Expand All @@ -578,12 +576,12 @@ public V compute(final K key, final BiFunction<? super K, ? super V, ? extends V

keyIndex = next(keyIndex, mask);
}

final V oldValue = unmapNullValue(mappedValue);
final V newValue = remappingFunction.apply(key, oldValue);

if (newValue != null)
{
// add or replace old mapping
entries[keyIndex + 1] = newValue;
if (mappedValue == null)
{
Expand All @@ -595,12 +593,12 @@ public V compute(final K key, final BiFunction<? super K, ? super V, ? extends V
}
else if (mappedValue != null)
{
// delete mapping
entries[keyIndex] = null;
entries[keyIndex + 1] = null;
size--;
compactChain(keyIndex);
}

return newValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
@FunctionalInterface
public interface
ObjIntToIntFunction<T>
ObjectIntToIntFunction<T>
{
/**
* Applies this function to the given arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public static void main(final String[] args) throws IOException
{
specialise(SUBSTITUTIONS, COLLECTIONS_PACKAGE, "IntIntConsumer", SRC_DIR, DST_DIR);
specialise(SUBSTITUTIONS, COLLECTIONS_PACKAGE, "IntObjConsumer", SRC_DIR, DST_DIR);
specialise(SUBSTITUTIONS, COLLECTIONS_PACKAGE, "IntObjToObjFunction", SRC_DIR, DST_DIR);
specialise(SUBSTITUTIONS, COLLECTIONS_PACKAGE, "ObjIntToIntFunction", SRC_DIR, DST_DIR);
specialise(SUBSTITUTIONS, COLLECTIONS_PACKAGE, "IntObjectToObjectFunction", SRC_DIR, DST_DIR);
specialise(SUBSTITUTIONS, COLLECTIONS_PACKAGE, "ObjectIntToIntFunction", SRC_DIR, DST_DIR);
specialise(SUBSTITUTIONS, COLLECTIONS_PACKAGE, "IntArrayList", SRC_DIR, DST_DIR);
specialise(SUBSTITUTIONS, COLLECTIONS_PACKAGE, "IntArrayQueue", SRC_DIR, DST_DIR);
specialise(SUBSTITUTIONS, COLLECTIONS_PACKAGE, "Int2IntHashMap", SRC_DIR, DST_DIR);
Expand Down
Loading

0 comments on commit 91efd0e

Please sign in to comment.