upsert method

dynamic upsert(
  1. dynamic insertIdx,
  2. dynamic val, [
  3. UpsertFn? fn
])

Implementation

upsert(insertIdx, val, [UpsertFn? fn]) {
  _magnitude = 0;
  int position = positionForIndex(insertIdx);

  if (elements.length > position && elements[position] == insertIdx) {
    if (fn == null) {
      throw Exception('fn required for this scenario');
    }
    elements[position + 1] = fn(elements[position + 1], val);
  } else {
    //elements.splice(position, 0, insertIdx, val);
    elements.insertAll(position, [insertIdx, val]);
  }
}