コード例 #1
0
ファイル: BTree.cs プロジェクト: Sphere10/Framework
        public void Add(K key, V value)
        {
            if (this._root == null)
            {
                this._root = new BTreeNode(this.order, key, value);
                this.Count++;
            }
            else
            {
                BTreeNode parentNode  = null;
                BTreeNode currentNode = this._root;

                while (!currentNode.IsLeaf || !currentNode.HasRoom)
                {
                    if (!currentNode.HasRoom)
                    {
                        this.PushSidesDown(currentNode);
                        currentNode = this.AttachSingleNodeToParent(currentNode, parentNode);
                    }

                    BTreeNodeRecord closest = currentNode.ClosestRecord(key);

                    if (key.Equals(closest.Item.Key))
                    {
                        closest.Item = new KeyValuePair <K, V>(key, value);
                        return;
                    }
                    else
                    {
                        parentNode = currentNode;

                        if (key.CompareTo(closest.Item.Key) < 0 && closest.Left != null)
                        {
                            currentNode = closest.Left;
                        }
                        else
                        {
                            currentNode = closest.Right;
                        }
                    }
                }

                if (currentNode.ContainsKey(key))
                {
                    currentNode.ClosestRecord(key).Item = new KeyValuePair <K, V>(key, value);
                }
                else
                {
                    currentNode.Records.Add(new BTreeNodeRecord(key, value));
                    currentNode.Records.Sort();
                    this.Count++;
                }
            }
        }
コード例 #2
0
ファイル: BTree.cs プロジェクト: Sphere10/Framework
        private BTreeNodeRecord FindRecord(BTreeNode start, K key, int depthRestriction, bool exactMatch)
        {
            if (this._root == null)
            {
                return(null);
            }

            BTreeNode containingNode = this.FindNode(start, key, depthRestriction, exactMatch);

            if (exactMatch && containingNode == null)
            {
                return(null);
            }

            BTreeNodeRecord found = containingNode.ClosestRecord(key);

            if (!exactMatch)
            {
                return(found);
            }

            if (found.Item.Key.Equals(key))
            {
                return(found);
            }

            return(null);
        }
コード例 #3
0
ファイル: BTree.cs プロジェクト: Sphere10/Framework
        private void RemoveLeaf(K key)
        {
            BTreeNode       foundNode   = this.FindNode(this._root, key, -1, true);
            BTreeNodeRecord foundRecord = foundNode.ClosestRecord(key);

            foundNode.Records.Remove(foundRecord);
            this.Count--;
        }
コード例 #4
0
ファイル: BTree.cs プロジェクト: Sphere10/Framework
        public bool Remove(K key)
        {
            if (this._root == null)
            {
                return(false);
            }

            BTreeNode foundNode = this.FindNode(this._root, key, -1, true);

            if (foundNode == null)
            {
                return(false);
            }

            if (foundNode.ClosestRecord(key).Item.Key.Equals(key))
            {
                this.FixPathSingles(key);

                foundNode = this.FindNode(this._root, key, -1, true);

                if (foundNode.IsLeaf)
                {
                    if (foundNode.Records.Count > 1)
                    {
                        this.RemoveLeaf(key);
                    }
                    else
                    {
                        this._root = null;
                        this.Count = 0;
                    }
                }
                else
                {
                    this.RemoveInternal(key);
                }

                return(true);
            }

            return(false);
        }
コード例 #5
0
ファイル: BTree.cs プロジェクト: Sphere10/Framework
        private BTreeNode FindNode(BTreeNode start, K key, int depthRestriction, bool exactMatch)
        {
            if (this._root == null)
            {
                return(null);
            }

            BTreeNode currentNode = start;

            int depthTraversed = 0;

            while (currentNode != null)
            {
                BTreeNodeRecord currentRecord = currentNode.ClosestRecord(key);

                if (currentRecord.Item.Key.Equals(key))
                {
                    return(currentNode);
                }
                else if (depthTraversed == depthRestriction)
                {
                    if (exactMatch)
                    {
                        return(null);
                    }
                    else
                    {
                        return(currentNode);
                    }
                }
                else
                {
                    depthTraversed++;

                    if (key.CompareTo(currentRecord.Item.Key) < 0 && currentRecord.Left != null)
                    {
                        currentNode = currentRecord.Left;
                    }
                    else if (currentRecord.Right != null)
                    {
                        currentNode = currentRecord.Right;
                    }
                    else
                    {
                        if (exactMatch)
                        {
                            return(null);
                        }
                        else
                        {
                            return(currentNode);
                        }
                    }
                }
            }

            if (exactMatch)
            {
                return(null);
            }
            else
            {
                return(currentNode);
            }
        }