internal LeafNode HigherNode(uint key) { XFastNode ancestor = Bottom(key); if (ancestor == null) { return(null); } LeafNode leaf = ancestor.left as LeafNode; if (leaf != null && leaf.key > key) { return(leaf); } leaf = ancestor.right as LeafNode; if (leaf != null && leaf.key > key) { return(leaf); } leaf = ancestor.right.right as LeafNode; if (leaf != null && leaf.key > key) { return(leaf); } return(null); }
private void InsertLeafAfter(XFastNode marker, LeafNode newLeaf) { if (marker == null) { if (leafList == null) { leafList = newLeaf; newLeaf.left = newLeaf; newLeaf.right = newLeaf; } else { XFastNode rightNode = leafList; leafList.left.right = newLeaf; newLeaf.left = leafList.left; newLeaf.right = leafList; leafList.left = newLeaf; leafList = newLeaf; } } else { XFastNode rightNode = marker.right; marker.right = newLeaf; newLeaf.left = marker; newLeaf.right = rightNode; rightNode.left = newLeaf; } }
private XFastNode Bottom(uint key) { int l = 0; int h = width; XFastNode tempNode; XFastNode correctNode = null; do { int j = (l + h) / 2; uint ancestor = key >> (width - 1 - j) >> 1; if (table[j].TryGetValue(ancestor, out tempNode)) { l = j + 1; correctNode = tempNode; } else { h = j; } }while (l < h); return(correctNode); }
private LeafNode LowerNodeFromBottom(XFastNode bottom, uint key) { if (bottom == null) { return(null); } LeafNode leaf = bottom.right as LeafNode; if (leaf != null && leaf.key < key) { return(leaf); } leaf = bottom.left as LeafNode; if (leaf != null && leaf.key < key) { return(leaf); } leaf = bottom.left.left as LeafNode; if (leaf != null && leaf.key < key) { return(leaf); } return(null); }
public override bool Remove(uint key) { var bottom = Bottom(key); if (bottom == null) { return(false); } // get the leaf node in endNode LeafNode endNode = bottom.left as LeafNode; if (endNode == null || endNode.key != key) { endNode = bottom.right as LeafNode; if (endNode == null || endNode.key != key) { return(false); } } // get pointers to node elft and right from endNode XFastNode leftLeaf = endNode.left; XFastNode rightLeaf = endNode.right; // remove bottom node from the table and leaf node from the list //table[width - 1].Remove(key >> 1); RemoveLeaf(endNode); // iterate levels bool single = true; for (int i = width - 1; i >= 0; i--) { XFastNode current; uint id = key >> (width - 1 - i) >> 1; bool isFromRight = ((key >> (width - 1 - i)) & 1) == 1; table[i].TryGetValue(id, out current); // remove the node if (single) { if (isFromRight && (!(current.left is LeafNode) || (i == (width - 1) && ((LeafNode)current.left).key != key))) { current.right = leftLeaf; single = false; } else if (!isFromRight && (!(current.right is LeafNode) || (i == (width - 1) && ((LeafNode)current.right).key != key))) { current.left = rightLeaf; single = false; } else { table[i].Remove(id); } } // fix jump pointers else { if (current.left == endNode) { current.left = rightLeaf; } else if (current.right == endNode) { current.right = leftLeaf; } } } count--; version++; return(true); }
private void AddChecked(uint key, T value, bool overwrite) { // Insert node in linked list XFastNode bottom = Bottom(key); LeafNode predecessor = LowerNodeFromBottom(bottom, key); // check for overwrite LeafNode predRight; if (predecessor != null) { predRight = (LeafNode)predecessor.right; } else { predRight = leafList; } if (predRight != null && predRight.key == key) { if (!overwrite) { throw new ArgumentException(); } else { predRight.value = value; return; } } count++; version++; // merrily continue LeafNode endNode = new LeafNode() { key = key, value = value }; InsertLeafAfter(predecessor, endNode); // Fix the jump path if (bottom == null) { bottom = new XFastNode(); table[0].Add(0, bottom); bottom.left = endNode; bottom.right = endNode; } XFastNode oldNode = null; XFastNode current; for (int i = 0; i < width; i++) { uint id = key >> (width - 1 - i) >> 1; if (table[i].TryGetValue(id, out current)) { // fix the jump path LeafNode leaf = current.left as LeafNode; if (leaf != null && leaf.key > key) { current.left = endNode; } else { leaf = current.right as LeafNode; if (leaf != null && leaf.key < key) { current.right = endNode; } } } else { // insert new node current = new XFastNode() { left = endNode, right = endNode }; table[i].Add(id, current); // fix link between old and new node if ((id & 1) > 0) { oldNode.right = current; } else { oldNode.left = current; } } oldNode = current; } }
internal LeafNode LowerNode(uint key) { XFastNode ancestor = Bottom(key); return(LowerNodeFromBottom(ancestor, key)); }