Skip to content

Commit

Permalink
add missing copy constructors on maps (real-logic#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-anthony authored Mar 22, 2023
1 parent 787e444 commit 08d056a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
16 changes: 16 additions & 0 deletions agrona/src/main/java/org/agrona/collections/Int2IntHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public Int2IntHashMap(
capacity(findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, initialCapacity)));
}

/**
* Copy construct a new map from an existing one.
*
* @param mapToCopy for construction.
*/
public Int2IntHashMap(final Int2IntHashMap mapToCopy)
{
this.loadFactor = mapToCopy.loadFactor;
this.resizeThreshold = mapToCopy.resizeThreshold;
this.size = mapToCopy.size;
this.shouldAvoidAllocation = mapToCopy.shouldAvoidAllocation;
this.missingValue = mapToCopy.missingValue;

entries = mapToCopy.entries.clone();
}

/**
* The value to be used as a null marker in the map.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public Object2NullableObjectHashMap(
super(initialCapacity, loadFactor, shouldAvoidAllocation);
}

/**
* Copy construct a new map from an existing one.
*
* @param mapToCopy for construction.
*/
public Object2NullableObjectHashMap(final Object2ObjectHashMap<K, V> mapToCopy)
{
super(mapToCopy);
}

protected Object mapNullValue(final Object value)
{
return value == null ? NullReference.INSTANCE : value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ public Object2ObjectHashMap(final int initialCapacity, final float loadFactor, f
capacity(findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, initialCapacity)));
}

/**
* Copy construct a new map from an existing one.
*
* @param mapToCopy for construction.
*/
public Object2ObjectHashMap(final Object2ObjectHashMap<K, V> mapToCopy)
{
this.loadFactor = mapToCopy.loadFactor;
this.resizeThreshold = mapToCopy.resizeThreshold;
this.size = mapToCopy.size;
this.shouldAvoidAllocation = mapToCopy.shouldAvoidAllocation;

entries = mapToCopy.entries.clone();
}

/**
* Get the load factor applied for resize operations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,21 @@ void shouldIterateEntriesBySpecialisedTypeAndSetValue()
assertEquals(expected, map);
}

@Test
void shouldCopyConstructAndBeEqual()
{
final int[] testEntries = { 3, 1, 19, 7, 11, 12, 7 };

final Int2IntHashMap map = new Int2IntHashMap(Integer.MIN_VALUE);
for (final int testEntry : testEntries)
{
map.put(testEntry, testEntry + 1);
}

final Int2IntHashMap mapCopy = new Int2IntHashMap(map);
assertThat(mapCopy, is(map));
}

@Test
void shouldToArray()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class Object2ObjectHashMapTest
{
Expand Down Expand Up @@ -189,4 +192,20 @@ void shouldForEachKeys()

assertEquals(copyTwo, copyOne);
}

@Test
void shouldCopyConstructAndBeEqual()
{
final int[] testEntries = { 3, 1, 19, 7, 11, 12, 7 };

final Object2ObjectHashMap<String, Integer> map = new Object2ObjectHashMap<>();
for (final int testEntry : testEntries)
{
map.put(String.valueOf(testEntry), testEntry);
}

final Object2ObjectHashMap<String, Integer> mapCopy = new Object2ObjectHashMap<>(map);
assertThat(mapCopy, is(map));
}

}

0 comments on commit 08d056a

Please sign in to comment.