FastMap.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package malte0811.ferritecore;
  2. import net.minecraft.state.Property;
  3. import javax.annotation.Nullable;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.List;
  7. import java.util.Map;
  8. public class FastMap<Value> {
  9. private final List<Key<?>> keys;
  10. private final List<Property<?>> rawKeys;
  11. private final List<Value> values;
  12. public FastMap(Collection<Property<?>> properties, Map<Map<Property<?>, Comparable<?>>, Value> valuesMap) {
  13. List<Key<?>> keys = new ArrayList<>(properties.size());
  14. int factorUpTo = 1;
  15. for (Property<?> prop : properties) {
  16. keys.add(new Key<>(prop, factorUpTo));
  17. factorUpTo *= prop.getAllowedValues().size();
  18. }
  19. this.keys = keys;
  20. List<Value> valuesList = new ArrayList<>(factorUpTo);
  21. for (int i = 0; i < factorUpTo; ++i) {
  22. valuesList.add(null);
  23. }
  24. for (Map.Entry<Map<Property<?>, Comparable<?>>, Value> state : valuesMap.entrySet()) {
  25. valuesList.set(indexOf(state.getKey()), state.getValue());
  26. }
  27. this.values = valuesList;
  28. this.rawKeys = new ArrayList<>(properties);
  29. }
  30. @Nullable
  31. public <T extends Comparable<T>>
  32. Value with(int last, Property<T> prop, T value) {
  33. final Key<T> keyToChange = indexOf(prop);
  34. if (keyToChange == null) {
  35. return null;
  36. }
  37. int newIndex = keyToChange.replaceIn(last, value);
  38. if (newIndex < 0) {
  39. return null;
  40. }
  41. return values.get(newIndex);
  42. }
  43. @Nullable
  44. private <T extends Comparable<T>>
  45. Key<T> indexOf(Property<T> prop) {
  46. for (Key<?> key : keys) {
  47. if (key.getProperty() == prop) {
  48. return (Key<T>) key;
  49. }
  50. }
  51. return null;
  52. }
  53. public int indexOf(Map<Property<?>, Comparable<?>> state) {
  54. int id = 0;
  55. for (Key<?> k : keys) {
  56. id += k.toPartialMapIndex(state.get(k.getProperty()));
  57. }
  58. return id;
  59. }
  60. @Nullable
  61. public <T extends Comparable<T>>
  62. T getValue(int stateIndex, Property<T> property) {
  63. final Key<T> propId = indexOf(property);
  64. if (propId == null) {
  65. return null;
  66. }
  67. return propId.getValue(stateIndex);
  68. }
  69. public Collection<Property<?>> getProperties() {
  70. return rawKeys;
  71. }
  72. private static class Key<T extends Comparable<T>> {
  73. private final Property<T> property;
  74. private final List<T> values;
  75. private final int mapFactor;
  76. private Key(Property<T> property, int mapFactor) {
  77. this.property = property;
  78. this.values = new ArrayList<>(property.getAllowedValues());
  79. this.mapFactor = mapFactor;
  80. }
  81. public int toPartialMapIndex(Comparable<?> value) {
  82. return mapFactor * getInternalIndex(value);
  83. }
  84. private int getInternalIndex(Comparable<?> value) {
  85. int result = values.indexOf(value);
  86. if (result >= 0) {
  87. return result;
  88. } else {
  89. throw new IllegalStateException("Unknown value: "+value+" in "+property);
  90. }
  91. }
  92. public T getValue(int mapIndex) {
  93. int index = (mapIndex / mapFactor) % values.size();
  94. return values.get(index);
  95. }
  96. public int replaceIn(int mapIndex, T newValue) {
  97. final int lowerData = mapIndex % mapFactor;
  98. final int upperFactor = mapFactor * values.size();
  99. final int upperData = mapIndex - mapIndex % upperFactor;
  100. int internalIndex = getInternalIndex(newValue);
  101. if (internalIndex < 0) {
  102. return -1;
  103. } else {
  104. return lowerData + mapFactor * internalIndex + upperData;
  105. }
  106. }
  107. public Property<T> getProperty() {
  108. return property;
  109. }
  110. }
  111. }