getBestIndex function

int getBestIndex(
  1. int max,
  2. int length,
  3. int index
)

Return the best index to spread among the list length when limited to a max value When max is 0 or smaller than length, returns index

i.e = max=4, length=11
index=0 => 1
index=1 => 4
index=2 => 7
index=3 => 9

Implementation

int getBestIndex(int max, int length, int index) =>
    max >= length || max == 0 ? index : 1 + (index * (length / max)).round();