diff --git a/src/main/java/com/jozufozu/flywheel/impl/layout/LayoutImpl.java b/src/main/java/com/jozufozu/flywheel/impl/layout/LayoutImpl.java index 0b223d1b6..b2feaac2b 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/layout/LayoutImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/layout/LayoutImpl.java @@ -1,6 +1,6 @@ package com.jozufozu.flywheel.impl.layout; -import java.util.HashMap; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -9,20 +9,27 @@ import org.jetbrains.annotations.Unmodifiable; import com.jozufozu.flywheel.api.layout.ElementType; import com.jozufozu.flywheel.api.layout.Layout; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; + final class LayoutImpl implements Layout { + @Unmodifiable private final List elements; + @Unmodifiable private final Map map; private final int byteSize; - LayoutImpl(List elements) { + LayoutImpl(@Unmodifiable List elements) { this.elements = elements; - map = new HashMap<>(); + Object2ObjectOpenHashMap map = new Object2ObjectOpenHashMap<>(); int byteSize = 0; for (Element element : this.elements) { map.put(element.name(), element.type()); byteSize += element.type().byteSize(); } + map.trim(); + + this.map = Collections.unmodifiableMap(map); this.byteSize = byteSize; } @@ -43,6 +50,52 @@ final class LayoutImpl implements Layout { return byteSize; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + elements.hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LayoutImpl other = (LayoutImpl) obj; + return elements.equals(other.elements); + } + record ElementImpl(String name, ElementType type) implements Element { + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + name.hashCode(); + result = prime * result + type.hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + ElementImpl other = (ElementImpl) obj; + return name.equals(other.name) && type.equals(other.type); + } } } diff --git a/src/main/java/com/jozufozu/flywheel/impl/layout/MatrixElementTypeImpl.java b/src/main/java/com/jozufozu/flywheel/impl/layout/MatrixElementTypeImpl.java index d70be1f2a..9c5c45b05 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/layout/MatrixElementTypeImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/layout/MatrixElementTypeImpl.java @@ -48,4 +48,29 @@ final class MatrixElementTypeImpl implements MatrixElementType { public int byteSize() { return byteSize; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + repr.hashCode(); + result = prime * result + rows; + result = prime * result + columns; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + MatrixElementTypeImpl other = (MatrixElementTypeImpl) obj; + return repr == other.repr && rows == other.rows && columns == other.columns; + } } diff --git a/src/main/java/com/jozufozu/flywheel/impl/layout/ScalarElementTypeImpl.java b/src/main/java/com/jozufozu/flywheel/impl/layout/ScalarElementTypeImpl.java index d39aaac84..a6b6d4361 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/layout/ScalarElementTypeImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/layout/ScalarElementTypeImpl.java @@ -21,4 +21,27 @@ final class ScalarElementTypeImpl implements ScalarElementType { public int byteSize() { return byteSize; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + repr.hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + ScalarElementTypeImpl other = (ScalarElementTypeImpl) obj; + return repr == other.repr; + } } diff --git a/src/main/java/com/jozufozu/flywheel/impl/layout/VectorElementTypeImpl.java b/src/main/java/com/jozufozu/flywheel/impl/layout/VectorElementTypeImpl.java index 7d9bb5fb2..5f2426a2b 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/layout/VectorElementTypeImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/layout/VectorElementTypeImpl.java @@ -36,4 +36,28 @@ final class VectorElementTypeImpl implements VectorElementType { public int byteSize() { return byteSize; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + repr.hashCode(); + result = prime * result + size; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + VectorElementTypeImpl other = (VectorElementTypeImpl) obj; + return repr == other.repr && size == other.size; + } }