예제 #1
0
        /// <summary>Returns all sub-trees enumerated from left to right.</summary>
        /// <returns>Enumerated sub-trees or empty if tree is empty.</returns>
        public IEnumerable <ImTypeMap <V> > Enumerate()
        {
            if (Height == 0)
            {
                yield break;
            }

            var parents = new ImTypeMap <V> [Height];

            var node        = this;
            var parentCount = -1;

            while (node.Height != 0 || parentCount != -1)
            {
                if (node.Height != 0)
                {
                    parents[++parentCount] = node;
                    node = node.Left;
                }
                else
                {
                    node = parents[parentCount--];
                    yield return(node);

                    node = node.Right;
                }
            }
        }
예제 #2
0
 private ImTypeMap(int keyHash, Type key, V value, ImTypeMap <V> left, ImTypeMap <V> right, int height)
 {
     _keyHash = keyHash;
     Key      = key;
     Value    = value;
     Left     = left;
     Right    = right;
     Height   = height;
 }
예제 #3
0
 private ImTypeMap(int keyHash, Type key, V value, ImTypeMap <V> left, ImTypeMap <V> right)
 {
     _keyHash = keyHash;
     Key      = key;
     Value    = value;
     Left     = left;
     Right    = right;
     Height   = 1 + (left.Height > right.Height ? left.Height : right.Height);
 }
예제 #4
0
 private ImTypeMap(int keyHash, Type key, V value)
 {
     _keyHash = keyHash;
     Key      = key;
     Value    = value;
     Left     = Empty;
     Right    = Empty;
     Height   = 1;
 }
예제 #5
0
        private ImTypeMap <V> RemoveImpl(int keyHash, Type key, bool ignoreKey = false)
        {
            if (Height == 0)
            {
                return(this);
            }

            ImTypeMap <V> result;

            if (key == Key || ignoreKey) // found node
            {
                if (Height == 1)         // remove node
                {
                    return(Empty);
                }

                if (Right.IsEmpty)
                {
                    result = Left;
                }
                else if (Left.IsEmpty)
                {
                    result = Right;
                }
                else
                {
                    // we have two children, so remove the next highest node and replace this node with it.
                    var successor = Right;
                    while (!successor.Left.IsEmpty)
                    {
                        successor = successor.Left;
                    }
                    result = new ImTypeMap <V>(successor._keyHash, successor.Key, successor.Value,
                                               Left, Right.RemoveImpl(successor._keyHash, successor.Key, ignoreKey: true));
                }
            }
            else if (keyHash < _keyHash)
            {
                result = new ImTypeMap <V>(_keyHash, Key, Value, Left.RemoveImpl(keyHash, key), Right);
            }
            else
            {
                result = new ImTypeMap <V>(_keyHash, Key, Value, Left, Right.RemoveImpl(keyHash, key));
            }

            return(result.KeepBalance());
        }