getIndexInParent method

int getIndexInParent(
  1. TKey key
)

Returns the zero-based index of key within the live sibling list of its parent (or the live root list, if key is a root). Returns -1 if key is not present or is itself pending deletion.

Live-space — not full-list-space — so the returned index directly matches positions in liveRootKeys / getLiveChildren and the input space of reorderRoots / reorderChildren.

Implementation

int getIndexInParent(TKey key) {
  if (!_hasKey(key) || _isPendingDeletion(key)) return -1;
  final parent = _parentKeyOfKey(key);
  final List<TKey> full = parent == null
      ? _roots
      : (_childListOf(parent) ?? <TKey>[]);
  int liveIndex = 0;
  for (final k in full) {
    if (k == key) return liveIndex;
    if (!_isPendingDeletion(k)) liveIndex++;
  }
  return -1;
}