internal static RBUIntNode LowerNode(RBTree tree, uint key) { RBUIntNode lowParent = null; RBUIntNode current = (RBUIntNode)tree.root; while (current != null) { if (key < current.key) { current = (RBUIntNode)current.left; } else if (key > current.key) { lowParent = current; current = (RBUIntNode)current.right; } else { break; } } // we finished walk on a node with key equal to ours if (current != null && current.left != null) { return((RBUIntNode)current.left.LastNode()); } return(lowParent); }
private void SplitIfTooLarge(XFastTrie <RBTree> .LeafNode separator) { if (separator.value.Count <= upperLimit) { return; } RBTree newTree = SplitNew(ref separator.value); cluster.Add(((RBUIntNode)newTree.LastNode()).key, newTree); }
private bool MergeSplit(ref RBTree higher, ref RBTree lower) { RBTree.Node[] array = new RBTree.Node[higher.Count + lower.Count]; lower.CopySorted(array, 0); higher.CopySorted(array, lower.Count); if (array.Length > upperLimit) { lower = RBUtils.FromSortedList(array, 0, array.Length >> 1); higher = RBUtils.FromSortedList(array, (array.Length >> 1) + 1, array.Length - 1); return(true); } else { higher = RBUtils.FromSortedList(array, 0, array.Length - 1); return(false); } }
internal static RBTree FromSortedList(RBTree.Node[] list, int start, int stop) { RBTree tree = new RBTree(new RBUIntNodeHelper()); int length = stop - start + 1; if (start == stop) { return(tree); } int maxDepth = BitHacks.Power2MSB(BitHacks.RoundToPower((uint)(length + 1))) - 1; tree.root = list[start + (length >> 1)]; tree.root.IsBlack = true; tree.root.Size = (uint)length; RBInsertChildren(tree.root, true, 1, maxDepth, list, start, start + (length >> 1) - 1); RBInsertChildren(tree.root, false, 1, maxDepth, list, start + (length >> 1) + 1, stop); return(tree); }
private void AddChecked(uint key, T value, bool overwrite) { var separator = Separator(key); if (separator == null) { // add first element RBTree newTree = new RBTree(Node.Helper); newTree.root = new RBUIntNode(key, value) { IsBlack = true }; cluster.Add(BitHacks.MaxValue(cluster.width), newTree); return; } RBUIntNode newNode = new RBUIntNode(key, value); RBUIntNode interned = (RBUIntNode)separator.value.Intern(key, newNode); if (interned != newNode) { if (overwrite) { interned.value = value; } else { throw new ArgumentException(); } } else { count++; version++; } SplitIfTooLarge(separator); }
public static RBTree.Node FirstNode(this RBTree tree) { return(FirstNode(tree.root)); }
public static RBTree.Node[] ToSortedArray(this RBTree tree) { RBTree.Node[] array = new RBTree.Node[tree.Count]; CopySorted(tree, array, 0); return(array); }
public static void CopySorted(this RBTree tree, RBTree.Node[] array, int offset) { InOrderAdd(tree.root, array, offset); }
public static RBTree.Node LastNode(this RBTree tree) { return(LastNode(tree.root)); }
// removes new RBTree from the old tree internal static RBTree SplitNew(ref RBTree tree) { RBTree.Node[] nodes = tree.ToSortedArray(); tree = RBUtils.FromSortedList(nodes, (nodes.Length >> 1), nodes.Length - 1); return(RBUtils.FromSortedList(nodes, 0, (nodes.Length >> 1) - 1)); }
public Node(uint key, T value) { tree = new RBTree(Helper); tree.Intern(key, new RBUIntNode(key, value)); }
public void Dispose() { tree = null; pennants = null; }
internal NodeEnumerator(RBTree tree, Stack <Node> init_pennants) : this(tree) { this.init_pennants = init_pennants; }
internal NodeEnumerator(RBTree tree) : this() { this.tree = tree; version = tree.version; }