Implement some useful optional features to make this faster (#3)

* implement some useful optional features to make this faster

Implementation defined in superinterface's default method does not know about the internals of map, thus operate with less efficiency, e.g. merge() must do 2 hash lookup instead of one. Implementing these in subclass allow better performance should client code take use of these optional features.

* spotlessApply (#4)

Co-authored-by: Glease <[email protected]>
Co-authored-by: GitHub GTNH Actions <>

* use typed enum instead of int

* Spotless apply for branch feature/itemstackmap-improvements for #3 (#5)

Co-authored-by: Glease <[email protected]>
Co-authored-by: GitHub GTNH Actions <>

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
GleaseGitHub GTNH Actionsgithub-actions[bot]
authored and GitHub committed 2022-09-01 12:52:00 +02:00
1 parent 03aa402916
commit 34d8d8a53b
1 file changed
+96 -25
@@ -4,7 +4,7 @@ package com.gtnewhorizon.gtnhlib.util.map;
* Copyright (c) 2022, GTNH Team, glee8e, Code Chicken,
*
* This file is originally part of NotEnoughtItem by Code Chicken.
* It is adapted to implement the standard Map interface by glease
* It is adapted to implement the standard Map interface by glee8e
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
@@ -31,6 +31,8 @@ import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.init.Items;
@@ -130,6 +132,22 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
return map != null && map.get(stack) != null;
}
@Override
public T merge(ItemStack key, T value, BiFunction<? super T, ? super T, ? extends T> remappingFunction) {
if (key == null || key.getItem() == null || value == null || remappingFunction == null) return null;
DetailMap map = itemMap.get(key.getItem());
return itemMap.computeIfAbsent(key.getItem(), k -> new DetailMap(this.NBTSensitive))
.merge(key, value, remappingFunction);
}
@Override
public T computeIfAbsent(ItemStack key, Function<? super ItemStack, ? extends T> mappingFunction) {
if (key == null || key.getItem() == null || mappingFunction == null) return null;
DetailMap map = itemMap.get(key.getItem());
return itemMap.computeIfAbsent(key.getItem(), k -> new DetailMap(this.NBTSensitive))
.computeIfAbsent(key, mappingFunction);
}
@Override
public void clear() {
itemMap.clear();
@@ -371,22 +389,10 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
this.NBTSensitive = NBTSensitive;
}
private int getKeyType(int damage, NBTTagCompound tag) {
// 0 = MetaMap
// 1 = TagMap
// 2 = DamageMap
// 3 = WildCard
if (!NBTSensitive) {
if (isWildcard(damage)) return 3;
else return 2;
} else {
// Bitwise Operations
int i = 0;
if (isWildcard(damage)) i = 1;
if (isWildcard(tag)) i |= 2;
return i;
}
private KeyType getKeyType(int damage, NBTTagCompound tag) {
KeyType i = isWildcard(damage) ? KeyType.WildcardMeta : KeyType.NotWildcard;
if (!NBTSensitive || isWildcard(tag)) i = i.withWildcardTag();
return i;
}
public T get(ItemStack key) {
@@ -408,16 +414,16 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
public T put(ItemStack key, T value) {
try {
switch (getKeyType(actualDamage(key), key.stackTagCompound)) {
case 0:
case NotWildcard:
if (metaMap == null) metaMap = new HashMap<>();
return metaMap.put(new StackMetaKey(key), value);
case 1:
case WildcardMeta:
if (tagMap == null) tagMap = new HashMap<>();
return tagMap.put(key.stackTagCompound, value);
case 2:
case WildcardTag:
if (damageMap == null) damageMap = new HashMap<>();
return damageMap.put(actualDamage(key), value);
case 3:
case WildcardAll:
T ret = wildcard;
wildcard = value;
hasWildcard = true;
@@ -432,13 +438,13 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
public T remove(ItemStack key) {
try {
switch (getKeyType(actualDamage(key), key.stackTagCompound)) {
case 0:
case NotWildcard:
return metaMap != null ? metaMap.remove(new StackMetaKey(key)) : null;
case 1:
case WildcardMeta:
return tagMap != null ? tagMap.remove(key.stackTagCompound) : null;
case 2:
case WildcardTag:
return damageMap != null ? damageMap.remove(actualDamage(key)) : null;
case 3:
case WildcardAll:
T ret = wildcard;
wildcard = null;
hasWildcard = false;
@@ -461,6 +467,54 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
size = newSize;
}
}
public T merge(ItemStack key, T value, BiFunction<? super T, ? super T, ? extends T> remappingFunction) {
try {
switch (getKeyType(actualDamage(key), key.stackTagCompound)) {
case NotWildcard:
if (metaMap == null) metaMap = new HashMap<>();
return metaMap.merge(new StackMetaKey(key), value, remappingFunction);
case WildcardMeta:
if (tagMap == null) tagMap = new HashMap<>();
return tagMap.merge(key.stackTagCompound, value, remappingFunction);
case WildcardTag:
if (damageMap == null) damageMap = new HashMap<>();
return damageMap.merge(actualDamage(key), value, remappingFunction);
case WildcardAll:
T newValue = wildcard == null ? value : remappingFunction.apply(wildcard, value);
wildcard = newValue;
hasWildcard = newValue != null;
return newValue;
}
} finally {
updateSize();
}
return null;
}
public T computeIfAbsent(ItemStack key, Function<? super ItemStack, ? extends T> mappingFunction) {
try {
switch (getKeyType(actualDamage(key), key.stackTagCompound)) {
case NotWildcard:
if (metaMap == null) metaMap = new HashMap<>();
return metaMap.computeIfAbsent(new StackMetaKey(key), x -> mappingFunction.apply(key));
case WildcardMeta:
if (tagMap == null) tagMap = new HashMap<>();
return tagMap.computeIfAbsent(key.stackTagCompound, x -> mappingFunction.apply(key));
case WildcardTag:
if (damageMap == null) damageMap = new HashMap<>();
return damageMap.computeIfAbsent(actualDamage(key), x -> mappingFunction.apply(key));
case WildcardAll:
T newValue = wildcard == null ? mappingFunction.apply(key) : wildcard;
wildcard = newValue;
hasWildcard = newValue != null;
return newValue;
}
} finally {
updateSize();
}
return null;
}
}
private class SetView extends AbstractSet<Map.Entry<ItemStack, T>> {
@@ -496,4 +550,21 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
return size;
}
}
private enum KeyType {
NotWildcard, // MetaMap
WildcardMeta, // TagMap
WildcardTag, // DamageMap
WildcardAll, // WildCard
;
private static final KeyType[] VALUES = values();
public KeyType withWildcardMeta() {
return VALUES[ordinal() | 1];
}
public KeyType withWildcardTag() {
return VALUES[ordinal() | 2];
}
}
}