hash code and stump of map

This commit is contained in:
gnembon committed 2019-10-01 14:50:16 -03:00
1 parent b6fe264a76
commit 8b7b285503
12 files changed
+125 -5

No files matched your search

@@ -13,6 +13,7 @@ import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.GlobalPos;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
@@ -254,6 +255,14 @@ public class BlockValue extends Value
return new BlockValue(blockState, world, pos);
}
@Override
public int hashCode()
{
if (world != null && pos != null )
return GlobalPos.create(world.getDimension().getType(), pos).hashCode();
return ("b"+getString()).hashCode();
}
public BlockPos getPos()
{
return pos;
@@ -138,6 +138,13 @@ public class EntityValue extends Value
return "entity";
}
@Override
public int hashCode()
{
return entity.hashCode();
}
public static Pair<EntityType<?>, Predicate<? super Entity>> getPredicate(String who)
{
Pair<EntityType<?>, Predicate<? super Entity>> res = entityPredicates.get(who);
@@ -43,5 +43,11 @@ public class FunctionSignatureValue extends Value
}
public List<String> getGlobals() {return globals;}
@Override
public int hashCode()
{
throw new UnsupportedOperationException("function signature value cannot be used as map key");
}
}
@@ -19,4 +19,10 @@ public class GlobalValue extends Value
{
return false;
}
@Override
public int hashCode()
{
throw new UnsupportedOperationException("global value cannot be used as map key");
}
}
@@ -44,6 +44,12 @@ public abstract class LazyListValue extends AbstractListValue implements Iterato
{
return stepp > 0?(current < limit):(current > limit);
}
@Override
public String getString()
{
return String.format("[%d, %d, ..., %d]",start, start+stepp, limit);
}
};
}
@@ -145,4 +151,10 @@ public abstract class LazyListValue extends AbstractListValue implements Iterato
((LazyListValue)copy).reset();
return copy;
}
@Override
public int hashCode()
{
return ("i"+getString()).hashCode();
}
}
@@ -4,11 +4,7 @@ import carpet.script.exception.InternalExpressionException;
import net.minecraft.item.ItemStack;
import net.minecraft.util.registry.Registry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import static java.lang.Math.abs;
@@ -322,9 +318,16 @@ public class ListValue extends AbstractListValue
index = index % numitems;
return items.get((int)index);
}
@Override
public String getTypeString()
{
return "list";
}
@Override
public int hashCode()
{
return items.hashCode();
}
}
@@ -0,0 +1,34 @@
package carpet.script.value;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MapValue extends AbstractListValue
{
private Map<Value, Value> map;
public MapValue()
{
map = new HashMap<>();
}
@Override
public Iterator<Value> iterator()
{
return map.keySet().iterator();
}
@Override
public String getString()
{
return Arrays.toString(map.entrySet().toArray());
}
@Override
public boolean getBoolean()
{
return !map.isEmpty();
}
}
@@ -44,4 +44,10 @@ public class NullValue extends NumericValue // TODO check nonsingleton code
{
return "null";
}
@Override
public int hashCode()
{
return 0;
}
}
@@ -179,4 +179,13 @@ public class NumericValue extends Value
return "number";
}
@Override
public int hashCode()
{
if (Math.abs(Math.floor(value + 0.5D)-value) < epsilon) // is sufficiently close to the integer value
return Long.hashCode(getLong());
return Double.hashCode(value);
}
}
@@ -182,4 +182,12 @@ public abstract class Value implements Comparable<Value>, Cloneable
{
throw new InternalExpressionException("How did you get here? Cannot get type of an intenal type.");
}
@Override
public int hashCode()
{
String stringVal = getString();
if (stringVal.isEmpty()) return 0;
return ("s"+stringVal).hashCode();
}
}