private void SplitLinkedNodeAndAddNewKeyNode(int start, int end, int value, int matchingCharCount) { //Splits "this" into two LinkedNodes and adds new KeyNode for current suffix var newLinkedNode = new LinkedNode(Start + matchingCharCount, End) { Children = Children }; Children = new LinkedList <IChildNode>(); Children.AddLast(newLinkedNode); End = Start + matchingCharCount; var newKeyNode = new KeyNode(start + matchingCharCount, end, value); Children.AddLast(newKeyNode); }
private void SplitKeyNodeAndAddBothToNewLinkedNode(string text, KeyNode node, int start, int end, int value) { //Count matching chars var count = CountMatchingChars(text, start, end, node.Start); //Replace node with new LinkedNode with lenth og the count of charmatches and add two KeyNodes. //One for node and one for the current value var newLinkedNode = new LinkedNode(node.Start, node.Start + count); var newKeyNode1 = new KeyNode(node.Start + count, node.End, node.Value); var newKeyNode2 = new KeyNode(start + count, end, value); newLinkedNode.Children.AddLast(newKeyNode1); newLinkedNode.Children.AddLast(newKeyNode2); Children.Remove(node); Children.AddFirst(newLinkedNode); }
//Original value is set when calling this method from a KeyNode, to not lose the value. public override void Add(Key key, int value) { var matchingChars = key.CountMatchingCharacters(Key); var newKeyStartIndex = MathClamp(key.StartIndex + matchingChars, key.StartIndex, key.LastIndex); // The new key we want to insert is either the full string or a substring. Subtracting the matching characters of the already // existing node, and beginning the new string where the match ends. var newKey = new Key(key.WordKey, newKeyStartIndex, key.LastIndex, key.lowerWord); if (Key.Length == 1 && newKey.Length == 1 && Key.StartsWithSameCharacter(newKey)) { if (More is null) { More = new KeyNode(newKey, value); return; } var temp = More; var prev = (Node)this; while (temp != null) { prev = temp; temp = temp.More; } if (temp != null) { return; } prev.More = new KeyNode(newKey, value); } else { if (Next.Key.StartsWithSameCharacter(newKey)) { Key.LastIndex = MathClamp(Key.StartIndex + matchingChars - 1, Key.StartIndex, Key.LastIndex); if (Next is LinkedNode linkedNode) { linkedNode.Add(newKey, value); } else if (Next is KeyNode keyNode) { Next = keyNode.Add(newKey, value); } return; } if (Next.More is null) { Key.LastIndex = MathClamp(Key.StartIndex + matchingChars - 1, Key.StartIndex, Key.LastIndex); Next.More = new KeyNode(newKey, value); } else { var temp = Next.More; var prev = Next; while (temp != null) { if (temp.Key.StartsWithSameCharacter(newKey)) { Key.LastIndex = MathClamp(Key.StartIndex + matchingChars - 1, Key.StartIndex, Key.LastIndex); if (temp is LinkedNode linkedNode) { linkedNode.Add(newKey, value); } else if (temp is KeyNode keyNode) { prev.More = keyNode.Add(newKey, value); } return; } prev = temp; temp = temp.More; } if (temp != null) { return; } Key.LastIndex = MathClamp(Key.StartIndex + matchingChars - 1, Key.StartIndex, Key.LastIndex); prev.More = new KeyNode(newKey, value); } } }