Exemplo n.º 1
0
 public void Remove(MapNode <TKey, TValue> node)
 {
     remove(node);
 }
Exemplo n.º 2
0
 public bool Insert(MapNode <TKey, TValue> newNode, bool priorityRight = true)
 {
     insert(newNode, _multi ? priorityRight : true);
     return(!newNode.Isolated);
 }
Exemplo n.º 3
0
        public MapNode <TKey, TValue> FindLast(TKey key)
        {
            MapNode <TKey, TValue> node = rbound(key);

            return(!is_nil(node) && !comp_lt(node.key, key) ? node : null);
        }
Exemplo n.º 4
0
        public MapNode <TKey, TValue> FindLeft(TKey key)
        {
            MapNode <TKey, TValue> node = rbound(key);

            return(is_nil(node) ? null : node);
        }
Exemplo n.º 5
0
        public MapNode <TKey, TValue> FindFirstLE(TKey key)
        {
            MapNode <TKey, TValue> node = rbound(key);

            return(!is_nil(node) ? node : null);
        }
Exemplo n.º 6
0
        public bool Has(TKey key)
        {
            MapNode <TKey, TValue> node = lbound(key);

            return(!is_nil(node) && !comp_lt(key, node.key));
        }
Exemplo n.º 7
0
        void remove(MapNode <TKey, TValue> where)
        {
            MapNode <TKey, TValue> erasedNode = where;

            where = next(where);
            MapNode <TKey, TValue> fixNode       = null;
            MapNode <TKey, TValue> fixNodeParent = null;
            MapNode <TKey, TValue> pNode         = erasedNode;

            if (is_nil(pNode.left))
            {
                fixNode = pNode.right;
            }
            else if (is_nil(pNode.right))
            {
                fixNode = pNode.left;
            }
            else
            {
                pNode   = where;
                fixNode = pNode.right;
            }
            if (pNode == erasedNode)
            {
                fixNodeParent = erasedNode.parent;
                if (!is_nil(fixNode))
                {
                    fixNode.parent = fixNodeParent;
                }
                if (root == erasedNode)
                {
                    root = fixNode;
                }
                else if (fixNodeParent.left == erasedNode)
                {
                    fixNodeParent.left = fixNode;
                }
                else
                {
                    fixNodeParent.right = fixNode;
                }
                if (lmost == erasedNode)
                {
                    lmost = is_nil(fixNode) ? fixNodeParent : min(fixNode);
                }
                if (rmost == erasedNode)
                {
                    rmost = is_nil(fixNode) ? fixNodeParent : max(fixNode);
                }
            }
            else
            {
                erasedNode.left.parent = pNode;
                pNode.left             = erasedNode.left;
                if (pNode == erasedNode.right)
                {
                    fixNodeParent = pNode;
                }
                else
                {
                    fixNodeParent = pNode.parent;
                    if (!is_nil(fixNode))
                    {
                        fixNode.parent = fixNodeParent;
                    }
                    fixNodeParent.left      = fixNode;
                    pNode.right             = erasedNode.right;
                    erasedNode.right.parent = pNode;
                }
                if (root == erasedNode)
                {
                    root = pNode;
                }
                else if (erasedNode.parent.left == erasedNode)
                {
                    erasedNode.parent.left = pNode;
                }
                else
                {
                    erasedNode.parent.right = pNode;
                }
                pNode.parent = erasedNode.parent;
                rb_color tcol = pNode.color;
                pNode.color      = erasedNode.color;
                erasedNode.color = tcol;
            }
            if (rb_color.black == erasedNode.color)
            {
                for (; fixNode != root && rb_color.black == fixNode.color; fixNodeParent = fixNode.parent)
                {
                    if (fixNode == fixNodeParent.left)
                    {
                        pNode = fixNodeParent.right;
                        if (rb_color.red == pNode.color)
                        {
                            pNode.color         = rb_color.black;
                            fixNodeParent.color = rb_color.red;
                            left_rotate(fixNodeParent);
                            pNode = fixNodeParent.right;
                        }
                        if (is_nil(pNode))
                        {
                            fixNode = fixNodeParent;
                        }
                        else if (rb_color.black == pNode.left.color && rb_color.black == pNode.right.color)
                        {
                            pNode.color = rb_color.red;
                            fixNode     = fixNodeParent;
                        }
                        else
                        {
                            if (rb_color.black == pNode.right.color)
                            {
                                pNode.left.color = rb_color.black;
                                pNode.color      = rb_color.red;
                                right_rotate(pNode);
                                pNode = fixNodeParent.right;
                            }
                            pNode.color         = fixNodeParent.color;
                            fixNodeParent.color = rb_color.black;
                            pNode.right.color   = rb_color.black;
                            left_rotate(fixNodeParent);
                            break;
                        }
                    }
                    else
                    {
                        pNode = fixNodeParent.left;
                        if (rb_color.red == pNode.color)
                        {
                            pNode.color         = rb_color.black;
                            fixNodeParent.color = rb_color.red;
                            right_rotate(fixNodeParent);
                            pNode = fixNodeParent.left;
                        }
                        if (is_nil(pNode))
                        {
                            fixNode = fixNodeParent;
                        }
                        else if (rb_color.black == pNode.right.color && rb_color.black == pNode.left.color)
                        {
                            pNode.color = rb_color.red;
                            fixNode     = fixNodeParent;
                        }
                        else
                        {
                            if (rb_color.black == pNode.left.color)
                            {
                                pNode.right.color = rb_color.black;
                                pNode.color       = rb_color.red;
                                left_rotate(pNode);
                                pNode = fixNodeParent.left;
                            }
                            pNode.color         = fixNodeParent.color;
                            fixNodeParent.color = rb_color.black;
                            pNode.left.color    = rb_color.black;
                            right_rotate(fixNodeParent);
                            break;
                        }
                    }
                }
                fixNode.color = rb_color.black;
            }
            erasedNode.parent = erasedNode.left = erasedNode.right = null;
            _count--;
        }
Exemplo n.º 8
0
 void insert_at(bool addLeft, MapNode <TKey, TValue> whereNode, MapNode <TKey, TValue> newNode)
 {
     newNode.parent = whereNode;
     if (whereNode == _head)
     {
         root = lmost = rmost = newNode;
     }
     else if (addLeft)
     {
         whereNode.left = newNode;
         if (whereNode == lmost)
         {
             lmost = newNode;
         }
     }
     else
     {
         whereNode.right = newNode;
         if (whereNode == rmost)
         {
             rmost = newNode;
         }
     }
     for (MapNode <TKey, TValue> pNode = newNode; rb_color.red == pNode.parent.color;)
     {
         if (pNode.parent == pNode.parent.parent.left)
         {
             whereNode = pNode.parent.parent.right;
             if (rb_color.red == whereNode.color)
             {
                 pNode.parent.color        = rb_color.black;
                 whereNode.color           = rb_color.black;
                 pNode.parent.parent.color = rb_color.red;
                 pNode = pNode.parent.parent;
             }
             else
             {
                 if (pNode == pNode.parent.right)
                 {
                     pNode = pNode.parent;
                     left_rotate(pNode);
                 }
                 pNode.parent.color        = rb_color.black;
                 pNode.parent.parent.color = rb_color.red;
                 right_rotate(pNode.parent.parent);
             }
         }
         else
         {
             whereNode = pNode.parent.parent.left;
             if (rb_color.red == whereNode.color)
             {
                 pNode.parent.color        = rb_color.black;
                 whereNode.color           = rb_color.black;
                 pNode.parent.parent.color = rb_color.red;
                 pNode = pNode.parent.parent;
             }
             else
             {
                 if (pNode == pNode.parent.left)
                 {
                     pNode = pNode.parent;
                     right_rotate(pNode);
                 }
                 pNode.parent.color        = rb_color.black;
                 pNode.parent.parent.color = rb_color.red;
                 left_rotate(pNode.parent.parent);
             }
         }
     }
     root.color = rb_color.black;
     _count++;
 }
Exemplo n.º 9
0
 static bool is_nil(MapNode <TKey, TValue> node)
 {
     return(node.nil);
 }