Пример #1
0
        // Constructor

        public RedBlackNode(bool isBlack, T value, IRedBlack <T> left, IRedBlack <T> right)
        {
            Debug.Assert(left != null && right != null, "Children cannot be null.");

            IsBlack = isBlack;
            Value   = value;
            Left    = left;
            Right   = right;
        }
Пример #2
0
        public static IRedBlack <T> MakeRoot <T>(this IRedBlack <T> node)
        {
            if (node.IsRedNode())
            {
                return(new RedBlackNode <T>(true, node.Value, node.Left, node.Right));
            }

            return(node);
        }
Пример #3
0
        // Private methods

        private IRedBlack <T> Balance(bool isBlack, T value, IRedBlack <T> left, IRedBlack <T> right)
        {
            if (Value.Equals(value) && Left == left && Right == right)
            {
                return(this);
            }

            return(RedBlackHelper.Balance(isBlack, value, left, right));
        }
Пример #4
0
        public ImmutableRedBlackDictionary(IComparer <TKey> keyComparer)
        {
            if (keyComparer == null)
            {
                throw new ArgumentNullException(nameof(keyComparer));
            }

            _root     = RedBlackLeaf <KeyValuePair <TKey, TValue> > .Instance;
            _comparer = new KeyComparer <TKey, TValue>(keyComparer);
        }
Пример #5
0
        public ImmutableRedBlackSet(IComparer <T> comparer)
        {
            if (comparer == null)
            {
                throw new ArgumentNullException();
            }

            _root     = RedBlackLeaf <T> .Instance;
            _comparer = comparer;
        }
Пример #6
0
        /// <summary>
        /// Balances the given subtree.
        /// </summary>
        /// <typeparam name="T">Type of values stored in the tree.</typeparam>
        /// <param name="isBlack">Is checked node black?</param>
        /// <param name="value">Value stored in the node.</param>
        /// <param name="left">New left node.</param>
        /// <param name="right">New right node.</param>
        /// <returns>Propagated node.</returns>
        public static IRedBlack <T> Balance <T>(bool isBlack, T value, IRedBlack <T> left, IRedBlack <T> right)
        {
            if (isBlack && left.IsRedNode() && left.Left.IsRedNode())
            {
                var l = Black(left.Left.Value, left.Left.Left, left.Left.Right);
                var r = Black(value, left.Right, right);

                return(Red(left.Value, l, r));
            }

            if (isBlack && left.IsRedNode() && left.Right.IsRedNode())
            {
                var l = Black(left.Value, left.Left, left.Right.Left);
                var r = Black(value, left.Right.Right, right);

                return(Red(left.Right.Value, l, r));
            }

            if (isBlack && right.IsRedNode() && right.Left.IsRedNode())
            {
                var l = Black(value, left, right.Left.Left);
                var r = Black(right.Value, right.Left.Right, right.Right);

                return(Red(right.Left.Value, l, r));
            }

            if (isBlack && right.IsRedNode() && right.Right.IsRedNode())
            {
                var l = Black(value, left, right.Left);
                var r = Black(right.Right.Value, right.Right.Left, right.Right.Right);

                return(Red(right.Value, l, r));
            }

            // Tree is already balanced.

            return(new RedBlackNode <T>(isBlack, value, left, right));
        }
Пример #7
0
        // Constructors

        public ImmutableRedBlackSet()
        {
            _root     = RedBlackLeaf <T> .Instance;
            _comparer = Comparer <T> .Default;
        }
Пример #8
0
 public static bool IsLeaf <T>(this IRedBlack <T> node)
 {
     return(node is RedBlackLeaf <T>);
 }
Пример #9
0
 public static bool IsNode <T>(this IRedBlack <T> node)
 {
     return(node is RedBlackNode <T>);
 }
Пример #10
0
 /// <summary>
 /// Checks if instance is black node.
 /// </summary>
 /// <typeparam name="T">Type contained in Red Black Tree.</typeparam>
 /// <param name="node">Checked node.</param>
 /// <returns>True, if node is not a leaf and it's black.</returns>
 public static bool IsRedNode <T>(this IRedBlack <T> node)
 {
     return(node is RedBlackNode <T> && !node.IsBlack);
 }
Пример #11
0
 private ImmutableRedBlackDictionary(IRedBlack <KeyValuePair <TKey, TValue> > root, KeyComparer <TKey, TValue> comparer)
 {
     _root     = root;
     _comparer = comparer;
 }
Пример #12
0
        // Constructors

        public ImmutableRedBlackDictionary()
        {
            _root     = RedBlackLeaf <KeyValuePair <TKey, TValue> > .Instance;
            _comparer = new KeyComparer <TKey, TValue>();
        }
Пример #13
0
        public void SetupRedBlack()
        {
            rbTree = redBlackFactory.MakeRedBlack();

            for (int i = 0; i < capacity; i++)
            {
                rbTree.Add(i, initValue);
            }
        }
Пример #14
0
 public WorkerNOTSync(IRedBlack rbTree, int nrOfOperations)
 {
     this.RedBlack = rbTree;
       this.nrOfOperations = nrOfOperations;
 }
Пример #15
0
        public static IRedBlack <T> Insert <T>(IRedBlack <T> root, T item, IComparer <T> comparer = null)
        {
            comparer = comparer ?? Comparer <T> .Default;

            return(root.Insert(item, comparer).MakeRoot());
        }
Пример #16
0
 /// <summary>
 /// Creates new red node.
 /// </summary>
 private static RedBlackNode <T> Red <T>(T value, IRedBlack <T> left, IRedBlack <T> right)
 {
     return(new RedBlackNode <T>(false, value, left, right));
 }
Пример #17
0
 private ImmutableRedBlackSet(IRedBlack <T> root, IComparer <T> comparer)
 {
     _root     = root;
     _comparer = comparer;
 }