diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-01-05 16:44:53 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-01-05 16:44:53 +0100 |
commit | e25d723262ed8702be60ade30d87c2da75fbadf2 (patch) | |
tree | fbfb8cc3327b9abab638fc513cb6fd93b69d8ab9 /vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java | |
parent | fd22e7e254528bea682a2e585f5cbb1fc625c93d (diff) |
Type DimensionSizes
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java b/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java new file mode 100644 index 00000000000..76340bb7d8f --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java @@ -0,0 +1,71 @@ +package com.yahoo.tensor; + +import java.util.Arrays; + +/** + * The sizes of a set of dimensions. + * + * @author bratseth + */ +public final class DimensionSizes { + + private final int[] sizes; + + private DimensionSizes(Builder builder) { + this.sizes = builder.sizes; + builder.sizes = null; // invalidate builder to avoid copying the array + } + + /** + * Returns the length of this in the nth dimension + * + * @throws IndexOutOfBoundsException if the index is larger than the number of dimensions in this tensor minus one + */ + public int size(int dimensionIndex) { return sizes[dimensionIndex]; } + + /** Returns the number of dimensions this provides the size of */ + public int dimensions() { return sizes.length; } + + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof DimensionSizes)) return false; + return Arrays.equals(((DimensionSizes) o).sizes, this.sizes); + } + + @Override + public int hashCode() { return Arrays.hashCode(sizes); } + + /** + * Builder of a set of dimension sizes. + * Dimensions whose size is not set before building will get size 0. + */ + public final static class Builder { + + private int[] sizes; + + public Builder(int dimensions) { + this.sizes = new int[dimensions]; + } + + public Builder set(int dimensionIndex, int size) { + sizes[dimensionIndex] = size; + return this; + } + + /** + * Returns the length of this in the nth dimension + * + * @throws IndexOutOfBoundsException if the index is larger than the number of dimensions in this tensor minus one + */ + public int size(int dimensionIndex) { return sizes[dimensionIndex]; } + + /** Returns the number of dimensions this provides the size of */ + public int dimensions() { return sizes.length; } + + /** Build this. This builder becomes invalid after calling this. */ + public DimensionSizes build() { return new DimensionSizes(this); } + + } + +} |