SPARK-1903 front end crash when sorting
This commit is contained in:
1 parent
65c4c23b22
commit
2ea95e7938
1 file changed
+88
-38
@@ -61,7 +61,7 @@ namespace bts.collections {
|
||||
if (_nursery.TryPopValue(k, out SortedList<Node> 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> 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<T, T> 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 <see cref="_tree"/>.
|
||||
/// b) node is a child node who's parent is present
|
||||
/// - remove it from the parent <see cref="Node.children"/>
|
||||
/// c) the node is a child node who's parent is NOT present
|
||||
/// - remove it from <see cref="_nursery"/> AND <see cref="_tree"/>.
|
||||
/// In each scenario, after the node is removed, the new parent will be assigned (<see cref="new_pk"/>).
|
||||
/// 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<Node> nursery_list)) {
|
||||
_nursery.Add(pk, nursery_list = new SortedList<Node>(_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<Node> 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<Node> new_nursery_list)) {
|
||||
_nursery.Add(new_pk, new_nursery_list = new SortedList<Node>(_comparer));
|
||||
}
|
||||
new_nursery_list.Add(node);
|
||||
_tree.Add(node);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to removes the node from the list <see cref="SortedList{T}.Remove"/> and throws an exception if it does not succeed.
|
||||
/// </summary>
|
||||
static void _assert_remove(SortedList<Node> list, Node node) {
|
||||
if (!list.Remove(node))
|
||||
throw new InvalidOperationException("Failed to remove node.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces <see cref="Node.value"/> using the specified delegate, and throws an exception if the new value's id is different from the old value's id.
|
||||
/// </summary>
|
||||
void _assert_replace(ref Node node, Func<T, T> 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) {
|
||||
|
||||
Reference in new issue
Block a user