compareOfTensor method Null safety

List compareOfTensor(
  1. List list,
  2. String operator,
  3. List tensor
)

Implementation

List compareOfTensor(List list, String operator, List tensor) {
  // ignore: unnecessary_null_comparison
  if (tensor == null) {
    throw new Exception(
        "DartTensorException : tensor not found for comparision.");
  } else {
    List opratorList = ['<', '>', '>=', '<=', '==', '!='];
    List shape = getDim(list);
    List temp = [];
    if (!opratorList.contains(operator)) {
      throw new Exception(
          "DartTensorException : Unrecognised comparision operator.");
    } else {
      List flat = flatten(list);
      bool flag = true;
      List shape1 = getDim(tensor);
      List flat1 = flatten(tensor);
      if (shape.length == shape1.length) {
        for (int i = 0; i < shape.length; i++) {
          if (shape[i] != shape1[i]) {
            flag = false;
            break;
          }
        }
        if (flag) {
          if (operator == '<') {
            for (int i = 0; i < flat.length; i++) {
              temp.add(flat[i] < flat1[i] ? true : false);
            }
          } else if (operator == '>') {
            for (int i = 0; i < flat.length; i++) {
              temp.add(flat[i] > flat1[i] ? true : false);
            }
          } else if (operator == '>=') {
            for (int i = 0; i < flat.length; i++) {
              temp.add(flat[i] >= flat1[i] ? true : false);
            }
          } else if (operator == '<=') {
            for (int i = 0; i < flat.length; i++) {
              temp.add(flat[i] <= flat1[i] ? true : false);
            }
          } else if (operator == '==') {
            for (int i = 0; i < flat.length; i++) {
              temp.add(flat[i] == flat1[i] ? true : false);
            }
          } else if (operator == '!=') {
            for (int i = 0; i < flat.length; i++) {
              temp.add(flat[i] != flat1[i] ? true : false);
            }
          }
        } else {
          throw new Exception(
              "DartTensorException : Both Tensors should be of equal shape. Got tensor of shape $shape and $shape1.");
        }
      } else {
        throw new Exception(
            "DartTensorException : Both Tensors should be of equal shape. Got tensor of shape $shape and $shape1.");
      }
      temp = generate(temp, shape);
      return temp;
    }
  }
}