remove method

T? remove(
  1. OccurrenceAddress address
)

Removes and returns the value stored at address, if any.

Implementation

T? remove(OccurrenceAddress address) {
  final path = _validatedPath(address);
  final nodes = <_OccurrenceTrieNode<T>>[_root];
  var node = _root;
  for (final index in path) {
    final child = node.children[index];
    if (child == null) {
      return null;
    }
    nodes.add(child);
    node = child;
  }

  final previous = node.value;
  if (previous == null) {
    return null;
  }
  node.value = null;
  for (var index = path.length - 1; index >= 0; index--) {
    final child = nodes[index + 1];
    if (!child.isEmpty) {
      break;
    }
    nodes[index].children.remove(path[index]);
  }
  return previous;
}