diff --git a/src/client/spark_core/Collections/SortedTree.cs b/src/client/spark_core/Collections/SortedTree.cs index 884a2c3a1..00f7e1ef1 100644 --- a/src/client/spark_core/Collections/SortedTree.cs +++ b/src/client/spark_core/Collections/SortedTree.cs @@ -61,7 +61,7 @@ namespace bts.collections { if (_nursery.TryPopValue(k, out SortedList children)) { // remove orphans from _tree (orphan-nodes must reside in the main _tree until parents arrive) foreach (var n in children) - _tree.Remove(n); + _assert_remove(_tree, n); node = new Node(item, children); } else node = new Node(item, _comparer); var pk = parent(item); @@ -84,13 +84,10 @@ namespace bts.collections { if (!_index.TryGetValue(k, out Node node)) { return false; } else { - var newval = replace(node.value); - Log.assert(id(newval).Equals(k)); - _replace(node, newval); + _replace(node, replace); return true; } } - public void Remove(T item) => Remove(id(item)); @@ -185,44 +182,97 @@ namespace bts.collections { } } - void _replace(Node node, T newval) { - TKey pk = parent(newval); - TKey old_pk = parent(node.value); - if (pk.Equals(old_pk)) { - SortedList node_list; - if (pk.Equals(_root_id)) - node_list = _tree; - else if (_index.TryGetValue(pk, out Node pnode)) { - node_list = pnode.children; - } else if (!_nursery.TryGetValue(pk, out node_list)) { - throw new KeyNotFoundException($"Replace was called with a parent-key ({pk}) that was not found in _index or _nursery; item-key was ({id(newval)})."); - } - node_list.Remove(node); - node.value = newval; - node_list.Add(node); - } else { - if (old_pk.Equals(_root_id)) { - _tree.Remove(node); - } else { - if (_index.TryGetValue(old_pk, out Node pnode)) - pnode.children.Remove(node); - else { - _remove_from_nursery(old_pk, node); - } - } - node.value = newval; - if (pk.Equals(_root_id)) { + void _replace(Node node, T new_value) => + _replace(node, _ => new_value); + + void _replace(Node node, Func replace_value) { + /// STEPS (outline) + /// 1) remove node with current value + /// 2) replace node.value + /// 3) re-insert node with new value + + /// Implementation (details): + /// there are 3 scenarios in which step 1 (remove node...) may occur + /// a) node is a root node + /// - remove it from . + /// b) node is a child node who's parent is present + /// - remove it from the parent + /// c) the node is a child node who's parent is NOT present + /// - remove it from AND . + /// In each scenario, after the node is removed, the new parent will be assigned (). + /// If the parent has NOT changed, assign the node back to the same list(s), then return. + /// If the parent HAS changed, determine the list(s) in which the node needs to be inserted, insert the node, then return. + TKey old_pk = parent(node.value), new_pk; + if (old_pk.Equals(_root_id)) { + // a) + _assert_remove(_tree, node); + _assert_replace(ref node, replace_value); + new_pk = parent(node.value); + if (new_pk.Equals(old_pk)) { + // the parent has NOT changed; re-insert the node, then return. _tree.Add(node); - } else if (_index.TryGetValue(pk, out Node pnode)) { - pnode.children.Add(node); - } else { - if (!_nursery.TryGetValue(pk, out SortedList nursery_list)) { - _nursery.Add(pk, nursery_list = new SortedList(_comparer)); - } + return; + } + } else if (_index.TryGetValue(old_pk, out Node old_pnode)) { + // b) + _assert_remove(old_pnode.children, node); + _assert_replace(ref node, replace_value); + new_pk = parent(node.value); + if (new_pk.Equals(old_pk)) { + // the parent has NOT changed; re-insert the node, then return. + old_pnode.children.Add(node); + return; + } + } else if (_nursery.TryGetValue(old_pk, out SortedList nursery_list)) { + // c) + _assert_remove(nursery_list, node); + _assert_remove(_tree, node); + _assert_replace(ref node, replace_value); + new_pk = parent(node.value); + if (new_pk.Equals(old_pk)) { + // the parent has NOT changed; re-insert the node, then return. nursery_list.Add(node); _tree.Add(node); + return; + } else { + // the parent HAS changed; if the old nursery_list is empty, remove it. + if (nursery_list.Count == 0) + _nursery.Remove(old_pk); } + } else { + // error) + throw new InvalidOperationException($"Unknown parent key '{old_pk}'."); } + // the parent HAS changed. Determine the list(s) in which the node needs to inserted. + if (new_pk.Equals(_root_id)) { + _tree.Add(node); + } else if (_index.TryGetValue(new_pk, out Node new_pnode)) { + new_pnode.children.Add(node); + } else { + if (!_nursery.TryGetValue(new_pk, out SortedList new_nursery_list)) { + _nursery.Add(new_pk, new_nursery_list = new SortedList(_comparer)); + } + new_nursery_list.Add(node); + _tree.Add(node); + } + } + + /// + /// Attempts to removes the node from the list and throws an exception if it does not succeed. + /// + static void _assert_remove(SortedList list, Node node) { + if (!list.Remove(node)) + throw new InvalidOperationException("Failed to remove node."); + } + + /// + /// Replaces using the specified delegate, and throws an exception if the new value's id is different from the old value's id. + /// + void _assert_replace(ref Node node, Func replace_value) { + TKey old_key = id(node.value); + node.value = replace_value(node.value); + if (!id(node.value).Equals(old_key)) + throw new InvalidOperationException("Replace op changed Node's id."); } void _remove_from_nursery(TKey pk, Node node) {