public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            TreeDictionaryKeyValuePairEnumerator <TKey, TValue> otherKeyValuePair = obj as TreeDictionaryKeyValuePairEnumerator <TKey, TValue>;

            if (otherKeyValuePair != null)
            {
                return(this.Equals(otherKeyValuePair));
            }

            TreeDictionaryKeyEnumerator <TKey, TValue> otherKey = obj as TreeDictionaryKeyEnumerator <TKey, TValue>;

            if (otherKey != null)
            {
                return(this.Equals(otherKey));
            }

            TreeDictionaryValueEnumerator <TKey, TValue> otherValue = obj as TreeDictionaryValueEnumerator <TKey, TValue>;

            if (otherValue != null)
            {
                return(this.Equals(otherValue));
            }

            return(false);
        }
 public TreeDictionaryKeyValuePairEnumerator(TreeDictionaryValueEnumerator <TKey, TValue> enumerator)
 {
     this.comparer     = enumerator.comparer;
     this.loopbackNode = enumerator.loopbackNode;
     this.node         = enumerator.node;
     this.isGoingDown  = enumerator.isGoingDown;
     this.statusBits   = enumerator.statusBits;
 }
        public bool Equals(TreeDictionaryValueEnumerator <TKey, TValue> other)
        {
            // if this node has been set to null, then it has been deleted
            // if the loopbackNode has been set to red (which is an invalid state in the red-black tree, then the entire tree
            // has been deleted via the Clear() function
            if (this.node.Parent == null || this.loopbackNode.IsRed || other.node.Parent == null || other.loopbackNode.IsRed)
            {
                throw new InvalidOperationException("This TreeDictionary node has been deleted");
            }

            if (this.loopbackNode != other.loopbackNode)
            {
                throw new ArgumentException("Both enumerators must be created from the same TreeDictionary", "other");
            }

            if ((this.statusBits & IsBeforeLowestBit) != 0)
            {
                if ((other.statusBits & IsBeforeLowestBit) != 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else if ((this.statusBits & IsBeforeLowestBit) != 0)
            {
                return(false);
            }

            if ((this.statusBits & IsAfterHighestBit) != 0)
            {
                if ((other.statusBits & IsAfterHighestBit) != 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else if ((this.statusBits & IsAfterHighestBit) != 0)
            {
                return(false);
            }

            Debug.Assert((this.statusBits & IsStartingEnumeration) == 0 && (other.statusBits & IsStartingEnumeration) == 0);

            if (this.node != other.node)
            {
                return(false);
            }

            return(true);
        }
        public int CompareTo(TreeDictionaryValueEnumerator <TKey, TValue> other)
        {
            // if this node has been set to null, then it has been deleted
            // if the loopbackNode has been set to red (which is an invalid state in the red-black tree, then the entire tree
            // has been deleted via the Clear() function
            if (this.node.Parent == null || this.loopbackNode.IsRed || other.node.Parent == null || other.loopbackNode.IsRed)
            {
                throw new InvalidOperationException("This TreeDictionary node has been deleted");
            }

            if (this.loopbackNode != other.loopbackNode)
            {
                throw new ArgumentException("Both enumerators must be created from the same TreeDictionary", "other");
            }

            if ((this.statusBits & IsBeforeLowestBit) != 0)
            {
                if ((other.statusBits & IsBeforeLowestBit) != 0)
                {
                    return(0);
                }
                else
                {
                    return(-1);
                }
            }
            else if ((other.statusBits & IsBeforeLowestBit) != 0)
            {
                return(1);
            }

            if ((this.statusBits & IsAfterHighestBit) != 0)
            {
                if ((other.statusBits & IsAfterHighestBit) != 0)
                {
                    return(0);
                }
                else
                {
                    return(1);
                }
            }
            else if ((other.statusBits & IsAfterHighestBit) != 0)
            {
                return(-1);
            }

            Debug.Assert((this.statusBits & IsStartingEnumeration) == 0 && (other.statusBits & IsStartingEnumeration) == 0);
            Debug.Assert(this.node != this.loopbackNode && this.node != other.loopbackNode && other.node != this.loopbackNode && other.node != other.loopbackNode);

            if (this.node == other.node)
            {
                return(0);
            }

            int thisHeight = comparer.Compare(this.node.Key, other.node.Key);

            if (thisHeight != 0)
            {
                return(thisHeight);
            }

            thisHeight = 0;
            TreeDictionary <TKey, TValue> .TreeNode node1 = this.node.Parent;
            TreeDictionary <TKey, TValue> .TreeNode node2 = this.loopbackNode;

            while (node1 != node2)
            {
                Debug.Assert(node1 != this.loopbackNode && node1 != other.loopbackNode);
                ++thisHeight;
                node1 = node1.Parent;
            }

            int otherHeight = 0;

            node1 = other.node.Parent;
            while (node1 != node2)
            {
                Debug.Assert(node1 != this.loopbackNode && node1 != other.loopbackNode);
                ++otherHeight;
                node1 = node1.Parent;
            }

            node1 = this.node;
            node2 = other.node;
            int diff = thisHeight - otherHeight;

            TreeDictionary <TKey, TValue> .TreeNode temp1 = null;
            TreeDictionary <TKey, TValue> .TreeNode temp2 = null;

            if (otherHeight < thisHeight)
            {
                while (diff > 0)
                {
                    Debug.Assert(node1 != this.loopbackNode && node1 != other.loopbackNode);
                    temp1 = node1;
                    node1 = node1.Parent;
                    --diff;
                }
            }
            else
            {
                while (diff < 0)
                {
                    Debug.Assert(node2 != this.loopbackNode && node2 != other.loopbackNode);
                    temp2 = node2;
                    node2 = node2.Parent;
                    ++diff;
                }
            }

            while (node1 != node2)
            {
                Debug.Assert(node1 != this.loopbackNode && node1 != other.loopbackNode && node2 != this.loopbackNode && node2 != other.loopbackNode);

                temp1 = node1;
                temp2 = node2;
                node1 = node1.Parent;
                node2 = node2.Parent;
            }

            node1 = node1.Left;
            if (temp1 == null)
            {
                if (temp2 == node1)
                {
                    return(1);
                }
                else
                {
                    return(-1);
                }
            }
            else
            {
                if (temp1 == node1)
                {
                    return(-1);
                }
                else
                {
                    return(1);
                }
            }
        }