public void Add(IPatriciaTreeItem item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } this._items.Add(item); this.AddNode(this._root, item); }
protected Node(int index, string key, int bit, IPatriciaTreeItem data) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (0 > bit) { throw new ArgumentOutOfRangeException(nameof(bit)); } this._index = index; this._key = key; this._bit = bit; this._item = data; }
private void AddNode(PatriciaTree.Node node, IPatriciaTreeItem item) { if (node == null) { this._root = (PatriciaTree.Node) new PatriciaTree.Leaf(this._nodes.Count, item); this._nodes.Add((PatriciaTree.INode) this._root); } else { int start = node.Parent == null ? 0 : node.Parent.Bit; if (node is PatriciaTree.BranchNode) { int bit = PatriciaTree.BitHelper.CompareBit(node.Key, item.Key, start, node.Bit); if (0 <= bit) { PatriciaTree.Leaf leaf = new PatriciaTree.Leaf(this._nodes.Count, item); this._nodes.Add((PatriciaTree.INode)leaf); this.Branch(node, (PatriciaTree.Node)leaf, bit); } else { this.AddNode(PatriciaTree.BitHelper.GetBit(item.Key, node.Bit) ? node.Right : node.Left, item); } } else { if (!(node is PatriciaTree.Leaf)) { throw new Exception("invalid node type."); } int bit = PatriciaTree.BitHelper.CompareBit(node.Key, item.Key, start); if (0 >= bit) { throw new ArgumentException(nameof(item)); } PatriciaTree.Leaf leaf = new PatriciaTree.Leaf(this._nodes.Count, item); this._nodes.Add((PatriciaTree.INode)leaf); this.Branch(node, (PatriciaTree.Node)leaf, bit); } } }
public bool Remove(IPatriciaTreeItem item) { return(this.Remove(item.Key)); }
public Leaf(int index, IPatriciaTreeItem item) : base(index, item.Key, PatriciaTree.BitHelper.GetBitCount(item.Key), item) { }