Esempio n. 1
0
        /// <summary>
        /// Delete key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private bool delete(byte[] key, long value = -1)
        {
            //VSDebug.StopPoint(Name, "SIZE");

            int nodeID = ROOT;

            while (nodeID >= 0)
            {
                int compare = VSLib.CompareKeys(key, BTree[nodeID].Key);

                if (compare < 0)
                {
                    nodeID = BTree[nodeID].Left;
                }
                else if (compare > 0)
                {
                    nodeID = BTree[nodeID].Right;
                }
                else
                {
                    return(delete_avl_node(nodeID, value));
                }
            }
            return(false);
        }
Esempio n. 2
0
        ////////////////////////////////////////////////////////////////////////
        ///////////////////  PRIVATE METHODS ///////////////////////////////////
        ////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///  Find key
        /// </summary>
        /// <param name="key"></param>
        /// <param name="op">0 - equal; -1 - less or equal; -2 - less than; 1 - equal or more; 2 - more than</param>
        /// <returns>Index or -1</returns>
        private int find(byte[] key, int op)
        {
            int ret       = -1;
            int ret_left  = -1;
            int ret_right = -1;
            int nodeID    = ROOT;

            while (nodeID >= 0)
            {
                int compare = VSLib.CompareKeys(key, BTree[nodeID].Key);

                if (compare < 0)
                {
                    ret_left = nodeID;                  // Node Key > key
                    nodeID   = BTree[nodeID].Left;
                }
                else if (compare > 0)
                {
                    ret_right = nodeID;                 // Node key < key
                    nodeID    = BTree[nodeID].Right;
                }
                else
                {
                    ret = nodeID;
                    break;
                }
            }

            if (ret >= 0)           // Key found
            {
                if ((op == COND_EQ) | (op == COND_LE) | (op == COND_GE))
                {
                    return(ret);
                }
                else
                {
                    if (op > 0)
                    {
                        return(find_successor(ret));
                    }
                    else
                    {
                        return
                            (find_predecessor(ret));
                    }
                }
            }
            else  // Key not found
            {
                if (op == COND_EQ)
                {
                    return(ret);
                }
                else
                {
                    if ((op == COND_LE) | (op == COND_LT))
                    {
                        return(ret_right);
                    }
                    else
                    {
                        return(ret_left);
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Add key (bytes)
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        private bool insert(byte[] key, long value)
        {
            if (CURRENT_USED == CURRENT_SIZE)           // Extend if full
            {
                this.extend();
            }

            bool rc = false;

            if (ROOT < 0)                                                       // Empty tree
            {
                ROOT = create_node(key, value, -1);
                rc   = true;
            }
            else
            {
                int nodeID = ROOT;

                while (nodeID >= 0)
                {
                    int compare = VSLib.CompareKeys(key, BTree[nodeID].Key);

                    if (compare < 0)
                    {
                        if (BTree[nodeID].Left < 0)
                        {
                            // Create left node
                            BTree[nodeID].Left = create_node(key, value, nodeID);         // Add to array
                            balance_insert(nodeID, -1);
                            return(true);
                        }
                        else
                        {
                            nodeID = BTree[nodeID].Left;
                        }
                    }
                    else if (compare > 0)
                    {
                        if (BTree[nodeID].Right < 0)
                        {
                            // Create right node
                            BTree[nodeID].Right = create_node(key, value, nodeID);
                            balance_insert(nodeID, 1);
                            rc = true;
                            break;
                        }
                        else
                        {
                            nodeID = BTree[nodeID].Right;
                        }
                    }
                    else
                    { // Index value already exists
                        if (this.UNIQUE)
                        {
                            break;
                        }
                        else
                        {
                            if (add_ref(nodeID, value))          // Add reference for non-unique value
                            {
                                rc = true;
                            }
                            break;
                        }
                    }
                }
            }
            return(rc);
        }