From 91efd0e4457013e7b2912cdd4c81d56e4060bda1 Mon Sep 17 00:00:00 2001 From: mjpt777 Date: Fri, 24 Jun 2022 12:25:15 +0100 Subject: [PATCH] Tidy up after merge of PR #259. --- .../agrona/collections/Int2IntHashMap.java | 32 +++++++++++-------- .../agrona/collections/Int2ObjectHashMap.java | 20 ++++++++---- ...on.java => IntObjectToObjectFunction.java} | 8 ++--- .../agrona/collections/Object2IntHashMap.java | 11 ++++--- .../collections/Object2ObjectHashMap.java | 10 +++--- ...ction.java => ObjectIntToIntFunction.java} | 2 +- .../generation/SpecialisationGenerator.java | 4 +-- .../collections/Int2IntHashMapTest.java | 4 +-- .../collections/Int2ObjectHashMapTest.java | 6 ++-- .../collections/Object2IntHashMapTest.java | 6 ++-- 10 files changed, 57 insertions(+), 46 deletions(-) rename agrona/src/main/java/org/agrona/collections/{IntObjToObjFunction.java => IntObjectToObjectFunction.java} (84%) rename agrona/src/main/java/org/agrona/collections/{ObjIntToIntFunction.java => ObjectIntToIntFunction.java} (97%) diff --git a/agrona/src/main/java/org/agrona/collections/Int2IntHashMap.java b/agrona/src/main/java/org/agrona/collections/Int2IntHashMap.java index 6a6bc5e9c..734e86c73 100644 --- a/agrona/src/main/java/org/agrona/collections/Int2IntHashMap.java +++ b/agrona/src/main/java/org/agrona/collections/Int2IntHashMap.java @@ -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. @@ -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; @@ -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. @@ -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); @@ -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. @@ -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) { @@ -460,6 +463,7 @@ else if (oldValue != missingValue) size--; compactChain(index); } + return newValue; } @@ -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. */ @@ -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. */ @@ -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. */ @@ -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) { diff --git a/agrona/src/main/java/org/agrona/collections/Int2ObjectHashMap.java b/agrona/src/main/java/org/agrona/collections/Int2ObjectHashMap.java index 452c3daa4..ab6a6eb06 100644 --- a/agrona/src/main/java/org/agrona/collections/Int2ObjectHashMap.java +++ b/agrona/src/main/java/org/agrona/collections/Int2ObjectHashMap.java @@ -321,7 +321,9 @@ public V computeIfAbsent(final int key, final IntFunction mappingFu index = ++index & mask; } + V value = unmapNullValue(mappedValue); + if (value == null && (value = mappingFunction.apply(key)) != null) { values[index] = value; @@ -342,15 +344,16 @@ public V computeIfAbsent(final int key, final IntFunction 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. *

- * If the function returns {@code null}, the mapping is removed + * If the function returns {@code null}, the mapping is removed. *

* 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 remappingFunction) + public V computeIfPresent( + final int key, final IntObjectToObjectFunction remappingFunction) { final int[] keys = this.keys; final Object[] values = this.values; @@ -367,7 +370,9 @@ public V computeIfPresent(final int key, final IntObjToObjFunction remappingFunction) + public V compute(final int key, final IntObjectToObjectFunction remappingFunction) { final int[] keys = this.keys; final Object[] values = this.values; @@ -411,12 +417,12 @@ public V compute(final int key, final IntObjToObjFunction + IntObjectToObjectFunction { /** * 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); } diff --git a/agrona/src/main/java/org/agrona/collections/Object2IntHashMap.java b/agrona/src/main/java/org/agrona/collections/Object2IntHashMap.java index 7dde3157d..96acf3155 100644 --- a/agrona/src/main/java/org/agrona/collections/Object2IntHashMap.java +++ b/agrona/src/main/java/org/agrona/collections/Object2IntHashMap.java @@ -295,6 +295,7 @@ public int computeIfAbsent(final K key, final ToIntFunction mappingFu index = ++index & mask; } + if (missingValue == value && (value = mappingFunction.applyAsInt(key)) != missingValue) { keys[index] = key; @@ -321,7 +322,7 @@ public int computeIfAbsent(final K key, final ToIntFunction mappingFu * @return the new value associated with the specified key, or missingValue if none */ @SuppressWarnings("overloads") - public int computeIfPresent(final K key, final ObjIntToIntFunction remappingFunction) + public int computeIfPresent(final K key, final ObjectIntToIntFunction remappingFunction) { final int missingValue = this.missingValue; final K[] keys = this.keys; @@ -339,6 +340,7 @@ public int computeIfPresent(final K key, final ObjIntToIntFunction re index = ++index & mask; } + if (value != missingValue) { value = remappingFunction.apply(key, value); @@ -350,6 +352,7 @@ public int computeIfPresent(final K key, final ObjIntToIntFunction re compactChain(index); } } + return value; } @@ -367,7 +370,7 @@ public int computeIfPresent(final K key, final ObjIntToIntFunction re * @return the new value associated with the specified key, or missingValue if none */ @SuppressWarnings("overloads") - public int compute(final K key, final ObjIntToIntFunction remappingFunction) + public int compute(final K key, final ObjectIntToIntFunction remappingFunction) { final int missingValue = this.missingValue; final K[] keys = this.keys; @@ -385,10 +388,10 @@ public int compute(final K key, final ObjIntToIntFunction 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) { @@ -401,13 +404,13 @@ public int compute(final K key, final ObjIntToIntFunction remappingFu } else if (oldValue != missingValue) { - // something to remove keys[index] = null; values[index] = missingValue; --size; compactChain(index); } + return newValue; } diff --git a/agrona/src/main/java/org/agrona/collections/Object2ObjectHashMap.java b/agrona/src/main/java/org/agrona/collections/Object2ObjectHashMap.java index b519360e0..b6a0663ee 100644 --- a/agrona/src/main/java/org/agrona/collections/Object2ObjectHashMap.java +++ b/agrona/src/main/java/org/agrona/collections/Object2ObjectHashMap.java @@ -489,7 +489,6 @@ public int hashCode() */ public V computeIfAbsent(final K key, final Function mappingFunction) { - final Object[] entries = this.entries; final int mask = entries.length - 1; int keyIndex = Hashing.evenHash(key.hashCode(), mask); @@ -508,7 +507,6 @@ public V computeIfAbsent(final K key, final Function 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) { @@ -517,6 +515,7 @@ public V computeIfAbsent(final K key, final Function map increaseCapacity(); } } + return value; } @@ -526,7 +525,6 @@ public V computeIfAbsent(final K key, final Function map @Override public V computeIfPresent(final K key, final BiFunction remappingFunction) { - final Object[] entries = this.entries; final int mask = entries.length - 1; int keyIndex = Hashing.evenHash(key.hashCode(), mask); @@ -554,6 +552,7 @@ public V computeIfPresent(final K key, final BiFunction remappingFunction) { - final Object[] entries = this.entries; final int mask = entries.length - 1; int keyIndex = Hashing.evenHash(key.hashCode(), mask); @@ -578,12 +576,12 @@ public V compute(final K key, final BiFunction + ObjectIntToIntFunction { /** * Applies this function to the given arguments. diff --git a/agrona/src/main/java/org/agrona/generation/SpecialisationGenerator.java b/agrona/src/main/java/org/agrona/generation/SpecialisationGenerator.java index b6caf0835..d3f204ecf 100644 --- a/agrona/src/main/java/org/agrona/generation/SpecialisationGenerator.java +++ b/agrona/src/main/java/org/agrona/generation/SpecialisationGenerator.java @@ -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); diff --git a/agrona/src/test/java/org/agrona/collections/Int2IntHashMapTest.java b/agrona/src/test/java/org/agrona/collections/Int2IntHashMapTest.java index c84d896c6..4457727c3 100644 --- a/agrona/src/test/java/org/agrona/collections/Int2IntHashMapTest.java +++ b/agrona/src/test/java/org/agrona/collections/Int2IntHashMapTest.java @@ -662,7 +662,7 @@ public void shouldNotHaveEqualHashcodeHashMapWithDifferentContents() } @Test - public void computeIfAbsentUsingImplementation() + public void shouldComputeIfAbsentUsingImplementation() { final Int2IntHashMap int2IntHashMap = new Int2IntHashMap(-1); final int key = 0; @@ -671,7 +671,7 @@ public void computeIfAbsentUsingImplementation() } @Test - public void computeIfAbsentUsingInterface() + public void shouldComputeIfAbsentUsingInterface() { final Map map = new Int2IntHashMap(-1); final int key = 0; diff --git a/agrona/src/test/java/org/agrona/collections/Int2ObjectHashMapTest.java b/agrona/src/test/java/org/agrona/collections/Int2ObjectHashMapTest.java index accfef429..7a0642ea6 100644 --- a/agrona/src/test/java/org/agrona/collections/Int2ObjectHashMapTest.java +++ b/agrona/src/test/java/org/agrona/collections/Int2ObjectHashMapTest.java @@ -573,7 +573,7 @@ protected String unmapNullValue(final Object value) } @Test - void testToArray() + void shouldToArray() { final Int2ObjectHashMap map = new Int2ObjectHashMap<>(); map.put(1, "a"); @@ -591,7 +591,7 @@ void testToArray() @Test @SuppressWarnings("rawtypes") - void testToArrayTyped() + void shouldToArrayTyped() { final Int2ObjectHashMap map = new Int2ObjectHashMap<>(); map.put(1, "a"); @@ -609,7 +609,7 @@ void testToArrayTyped() } @Test - void testToArrayWithArrayListConstructor() + void shouldToArrayWithArrayListConstructor() { final Int2ObjectHashMap map = new Int2ObjectHashMap<>(); map.put(1, "a"); diff --git a/agrona/src/test/java/org/agrona/collections/Object2IntHashMapTest.java b/agrona/src/test/java/org/agrona/collections/Object2IntHashMapTest.java index c431a9c6d..124179740 100644 --- a/agrona/src/test/java/org/agrona/collections/Object2IntHashMapTest.java +++ b/agrona/src/test/java/org/agrona/collections/Object2IntHashMapTest.java @@ -472,7 +472,7 @@ void shouldCopyConstructAndBeEqual() } @Test - void testToArray() + void shouldToArray() { final Object2IntHashMap map = new Object2IntHashMap<>(-127); map.put("a", 1); @@ -490,7 +490,7 @@ void testToArray() @Test @SuppressWarnings("rawtypes") - void testToArrayTyped() + void shouldToArrayTyped() { final Object2IntHashMap map = new Object2IntHashMap<>(-127); map.put("a", 1); @@ -508,7 +508,7 @@ void testToArrayTyped() } @Test - void testToArrayWithArrayListConstructor() + void shouldToArrayWithArrayListConstructor() { final Object2IntHashMap map = new Object2IntHashMap<>(-127); map.put("a", 1);