/** * Returns whether the given {@link IntArrayND} equals the given object * * @param array The array * @param object The object * @return Whether the array equals the object */ static boolean equals(IntArrayND array, Object object) { if (array == object) { return true; } if (object == null) { return false; } if (!(object instanceof IntArrayND)) { return false; } IntArrayND other = (IntArrayND) object; if (!array.getSize().equals(other.getSize())) { return false; } Stream<MutableIntTuple> coordinates = Coordinates.coordinates(array.getPreferredIterationOrder(), array.getSize()); Iterable<MutableIntTuple> iterable = () -> coordinates.iterator(); for (IntTuple coordinate : iterable) { int arrayValue = array.get(coordinate); int otherValue = other.get(coordinate); if (arrayValue != otherValue) { return false; } } return true; }
/** * Creates a formatted representation of the given array. After each dimension, a newline * character will be inserted. * * @param array The array * @param format The format for the array entries * @return The hash code */ public static String toFormattedString(IntArrayND array, String format) { if (array == null) { return "null"; } StringBuilder sb = new StringBuilder(); Iterable<MutableIntTuple> iterable = IntTupleIterables.lexicographicalIterable(array.getSize()); IntTuple previous = null; for (IntTuple coordinates : iterable) { if (previous != null) { int c = Utils.countDifferences(previous, coordinates); for (int i = 0; i < c - 1; i++) { sb.append("\n"); } } int value = array.get(coordinates); sb.append(String.format(format + ", ", value)); previous = coordinates; } return sb.toString(); }
/** * Computes the hash code of the given array. This method will return a hash code that solely * depends on the contents of the given array, regardless of its preferred iteration order. * * @param array The array * @return The hash code */ static int hashCode(IntArrayND array) { if (array == null) { return 0; } return array.stream().parallel().sum(); }
/** * Returns a default string representation of the given array. Since arrays tend to be large (and * multidimensional), this string will <b>not</b> contain the <i>contents</i> of the array, but * only its size. * * @param array The array * @return The string */ static String toString(IntArrayND array) { if (array == null) { return "null"; } return array.getClass().getSimpleName() + "[size=" + array.getSize() + "]"; }
/** * Returns the maximum value in the given array, or <code>Integer.MIN_VALUE</code> if the given * array has a size of 0. * * @param array The array * @return The maximum value */ public static int max(IntArrayND array) { return array.stream().parallel().reduce(Integer.MIN_VALUE, Math::max); }
/** * Returns the minimum value in the given array, or <code>Integer.MAX_VALUE</code> if the given * array has a size of 0. * * @param array The array * @return The minimum value */ public static int min(IntArrayND array) { return array.stream().parallel().reduce(Integer.MAX_VALUE, Math::min); }