IntegerRange constructor

IntegerRange([
  1. int? a,
  2. int? b,
  3. int? c
])

Creates a virtual range of numbers containing an arithmetic progressions of integer values.

The constructor called without any arguments returns the empty range. For example, IntegerRange() yields <int>[].

The constructor called with one argument returns the range of all numbers up to, but excluding the end. For example, IntegerRange(3) yields <int>[0, 1, 2].

The constructor called with two arguments returns the range between the two numbers (including the start, but excluding the end). For example, IntegerRange(3, 6) yields <int>[3, 4, 5].

The constructor called with three arguments returns the range between the first two numbers (including the start, but excluding the end) and the step value. For example, IntegerRange(1, 7, 2) yields <int>[1, 3, 5].

Implementation

factory IntegerRange([int? a, int? b, int? c]) {
  var start = 0;
  var end = 0;
  var step = 1;
  if (c != null) {
    start = a!;
    end = b!;
    step = c;
  } else if (b != null) {
    start = a!;
    end = b;
    step = start <= end ? 1 : -1;
  } else if (a != null) {
    end = a;
  }
  if (start < end) {
    if (step == 1) {
      return IntegerRange._(start, end, step, end - start);
    } else if (step > 1) {
      return IntegerRange._(
          start, end, step, (end - start + step - 1) ~/ step);
    }
  } else if (start > end) {
    if (step == -1) {
      return IntegerRange._(start, end, step, start - end);
    } else if (step < -1) {
      return IntegerRange._(
          start, end, step, (start - end - step - 1) ~/ -step);
    }
  } else {
    if (step != 0) {
      return IntegerRange._(start, end, step, 0);
    }
  }
  throw ArgumentError.value(
      step, 'step', 'Invalid step size for range $start..$end');
}