private void ContinueIndexingAtChild( int itemId, byte fieldId, IReadOnlyList <TokenLocation> locations, ReadOnlyMemory <char> remainingTokenText, int remainingTextSplitPosition) { var indexChar = remainingTokenText.Span[remainingTextSplitPosition]; this.EnsureMutatedChildNodesCreated(); if (!this.MutatedChildNodes !.TryGetValue(indexChar, out var childNode)) { if (this.original != null && this.original.ChildNodes.TryGetValue(indexChar, out var originalChildNode)) { // the original had an unmutated child node that matched the index character - mutate it now childNode = new IndexNodeMutation(this.depth + 1, originalChildNode, this.indexNodeFactory); } else { // This is a novel branch in the index childNode = new IndexNodeMutation(this); } // Track the mutated node this.MutatedChildNodes.Add(indexChar, childNode); } childNode.Index(itemId, fieldId, locations, remainingTokenText.Slice(remainingTextSplitPosition + 1)); }
private bool TryRemove(IndexNode node, int itemId, int nodeDepth, [NotNullWhen(true)] out IndexNodeMutation?mutatedNode) { mutatedNode = null; if (node.HasChildNodes) { // Work through the child nodes and recursively determine whether removals are needed from // them. If they are, then this instance will also become mutated. foreach (var child in node.ChildNodes) { if (this.TryRemove(child.Value, itemId, nodeDepth + 1, out var mutatedChild)) { if (mutatedNode == null) { mutatedNode = new IndexNodeMutation(nodeDepth, node, this.indexNodeFactory); mutatedNode.EnsureMutatedChildNodesCreated(); } mutatedNode.MutatedChildNodes !.Add(child.Key, mutatedChild); } } } if (node.HasMatches) { // Removing an item from the nodes current matches will return the same dictionary // if the item didn't exist - this removes the need for an extra Exists check var mutatedMatches = node.Matches.Remove(itemId); if (mutatedMatches != node.Matches) { if (mutatedNode == null) { mutatedNode = new IndexNodeMutation(nodeDepth, node, this.indexNodeFactory); } mutatedNode.EnsureMutatedMatchesCreated(); mutatedNode.MutatedMatches !.Remove(itemId); } } return(mutatedNode != null); }
private IndexNodeMutation(IndexNodeMutation parent) : this(parent.depth + 1, parent.indexNodeFactory) { }
public IndexMutation(IndexNode root, IIndexNodeFactory indexNodeFactory) { this.root = new IndexNodeMutation(0, root, indexNodeFactory); }