001// Copyright (c) Choreo contributors
002
003package choreo.util;
004
005import java.util.function.BiFunction;
006
007/** A Choreo Internal utility class for array operations. */
008public class ChoreoArrayUtil {
009  /**
010   * Checks two <code>double</code> arrays for equality with the given function. This returns true
011   * if:
012   *
013   * <ul>
014   *   <li>Either both arrays are null, or
015   *   <li>Neither is null, the arrays are the same length, and the given function returns true for
016   *       all same-index pairs of elements in the arrays.
017   * </ul>
018   *
019   * @param arr1 The first array
020   * @param arr2 The second array
021   * @param check The function to compare elements.
022   * @return Whether the arrays are equal.
023   */
024  public static boolean zipEquals(
025      double[] arr1, double[] arr2, BiFunction<Double, Double, Boolean> check) {
026    if (arr1 == null && arr2 == null) {
027      return true;
028    }
029    if (arr1 != null && arr2 == null) {
030      return false;
031    }
032    if (arr1 == null && arr2 != null) {
033      return false;
034    }
035    // arr1 and arr2 both not null
036    if (arr1.length != arr2.length) {
037      return false;
038    }
039    for (int i = 0; i < arr1.length; ++i) {
040      if (!check.apply(arr1[i], arr2[i])) {
041        return false;
042      }
043    }
044    return true;
045  }
046
047  /** Factory class. */
048  private ChoreoArrayUtil() {}
049}