Skip to content
Permalink
Browse files

Drop ordering property, add full insertion test

  • Loading branch information...
kenzierocks committed Jul 16, 2019
1 parent 5b1b5d4 commit 8adf98aa8a8f7181d46e508c15710d09f85f0934
@@ -27,8 +27,8 @@
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@@ -39,14 +39,14 @@
/**
* A space-efficient map implementation for block locations.
*/
public class OrderedBlockMap extends AbstractMap<BlockVector3, BaseBlock> {
public class BlockMap extends AbstractMap<BlockVector3, BaseBlock> {

public static OrderedBlockMap create() {
return new OrderedBlockMap();
public static BlockMap create() {
return new BlockMap();
}

public static OrderedBlockMap copyOf(Map<? extends BlockVector3, ? extends BaseBlock> source) {
return new OrderedBlockMap(source);
public static BlockMap copyOf(Map<? extends BlockVector3, ? extends BaseBlock> source) {
return new BlockMap(source);
}

private static final int WORLD_XZ_MINMAX = 30_000_000;
@@ -116,19 +116,19 @@ private static int fixSign(int h) {
return (h << FIX_SIGN_SHIFT) >> FIX_SIGN_SHIFT;
}

private final Map<Integer, Map<Integer, BaseBlock>> maps = new LinkedHashMap<>();
private final Map<Integer, Map<Integer, BaseBlock>> maps = new HashMap<>();
private Set<Entry<BlockVector3, BaseBlock>> entrySet;
private Collection<BaseBlock> values;

private OrderedBlockMap() {
private BlockMap() {
}

private OrderedBlockMap(Map<? extends BlockVector3, ? extends BaseBlock> source) {
private BlockMap(Map<? extends BlockVector3, ? extends BaseBlock> source) {
putAll(source);
}

private Map<Integer, BaseBlock> getOrCreateMap(int groupKey) {
return maps.computeIfAbsent(groupKey, k -> new LinkedHashMap<>());
return maps.computeIfAbsent(groupKey, k -> new HashMap<>());
}

private Map<Integer, BaseBlock> getOrEmptyMap(int groupKey) {
@@ -148,7 +148,7 @@ private OrderedBlockMap(Map<? extends BlockVector3, ? extends BaseBlock> source)
}
return result;
}
map = new LinkedHashMap<>();
map = new HashMap<>();
R result = func.apply(map);
if (!map.isEmpty()) {
maps.put(groupKey, map);
@@ -252,7 +252,7 @@ public BaseBlock merge(BlockVector3 key, BaseBlock value, BiFunction<? super Bas

@Override
public int size() {
return OrderedBlockMap.this.size();
return BlockMap.this.size();
}
};
}
@@ -357,9 +357,9 @@ public BaseBlock remove(Object key) {

@Override
public void putAll(Map<? extends BlockVector3, ? extends BaseBlock> m) {
if (m instanceof OrderedBlockMap) {
if (m instanceof BlockMap) {
// optimize insertions:
((OrderedBlockMap) m).maps.forEach((groupKey, map) ->
((BlockMap) m).maps.forEach((groupKey, map) ->
getOrCreateMap(groupKey).putAll(map)
);
} else {
@@ -394,7 +394,7 @@ public int size() {

@Override
public int size() {
return OrderedBlockMap.this.size();
return BlockMap.this.size();
}
};
}
@@ -406,9 +406,9 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof OrderedBlockMap) {
if (o instanceof BlockMap) {
// optimize by skipping entry translations:
return maps.equals(((OrderedBlockMap) o).maps);
return maps.equals(((BlockMap) o).maps);
}
return super.equals(o);
}
@@ -77,14 +77,14 @@ public void accept(Test test) {
return makeVectorsStream().map(Arguments::of);
}

private Stream<BlockVector3> makeVectorsStream() {
public static Stream<BlockVector3> makeVectorsStream() {
return Stream.concat(
ALWAYS_INCLUDE.stream(),
Streams.stream(generateVectors()).filter(v -> !ALWAYS_INCLUDE.contains(v))
);
}

private Iterator<BlockVector3> generateVectors() {
private static Iterator<BlockVector3> generateVectors() {
return new AbstractIterator<BlockVector3>() {

private int x = -WORLD_XZ_MINMAX + 1;
@@ -19,16 +19,16 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.AbstractMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -45,7 +45,7 @@
import static org.mockito.Mockito.when;

@DisplayName("An ordered block map")
class OrderedBlockMapTest {
class BlockMapTest {

@BeforeAll
static void setupFakePlatform() {
@@ -80,7 +80,7 @@ private static void registerBlock(String id) {
private final BaseBlock air = checkNotNull(BlockTypes.AIR).getDefaultState().toBaseBlock();
private final BaseBlock oakWood = checkNotNull(BlockTypes.OAK_WOOD).getDefaultState().toBaseBlock();

private OrderedBlockMap map = OrderedBlockMap.create();
private BlockMap map = BlockMap.create();

@BeforeEach
void setUp() {
@@ -539,4 +539,19 @@ void insertsOnMerge(BlockVector3 vec) {

}

@Test
@DisplayName("contains all inserted vectors")
void containsAllInsertedVectors() {
Set<BlockVector3> allVectors = VariedVectorsProvider.makeVectorsStream().collect(Collectors.toSet());
for (BlockVector3 vec : allVectors) {
map.put(vec, air);
}
assertEquals(allVectors.size(), map.size());
assertEquals(allVectors, map.keySet());
for (Map.Entry<BlockVector3, BaseBlock> entry : map.entrySet()) {
assertTrue(allVectors.contains(entry.getKey()));
assertEquals(air, entry.getValue());
}
}

}

0 comments on commit 8adf98a

Please sign in to comment.
You can’t perform that action at this time.