예제 #1
0
        public RedBlackTreeSet(RedBlackTreeSet <T> anotherSet)
        {
            var sortedArray = anotherSet.ToArray();

            this.comparer = anotherSet.comparer;
            tree          = new LeftLeaningRedBlackTree <T>(sortedArray, 0, sortedArray.Length - 1, this.comparer);
        }
예제 #2
0
        private void MergeInOrderTraversal(RedBlackTreeSet <T> anotherSet, Action <T> thisHandler, Action <T> anotherHandler, Action <T> equalHandler)
        {
            var thisEnumerator    = GetEnumerator();
            var anotherEnumerator = anotherSet.GetEnumerator();

            var thisEnded    = !thisEnumerator.MoveNext();
            var anotherEnded = !anotherEnumerator.MoveNext();

            while (!thisEnded && !anotherEnded)
            {
                int compareResult = comparer.Compare(thisEnumerator.Current, anotherEnumerator.Current);
                if (compareResult < 0)
                {
                    thisHandler?.Invoke(thisEnumerator.Current);
                    thisEnded = !thisEnumerator.MoveNext();
                }
                else if (compareResult > 0)
                {
                    anotherHandler?.Invoke(anotherEnumerator.Current);
                    anotherEnded = !anotherEnumerator.MoveNext();
                }
                else
                {
                    equalHandler?.Invoke(anotherEnumerator.Current);
                    thisEnded    = !thisEnumerator.MoveNext();
                    anotherEnded = !anotherEnumerator.MoveNext();
                }
            }

            if (!thisEnded)
            {
                do
                {
                    thisHandler?.Invoke(thisEnumerator.Current);
                } while (thisEnumerator.MoveNext());
            }

            if (!anotherEnded)
            {
                do
                {
                    anotherHandler?.Invoke(anotherEnumerator.Current);
                } while (anotherEnumerator.MoveNext());
            }
        }
예제 #3
0
        public bool IsSubset(RedBlackTreeSet <T> anotherSet)
        {
            if (anotherSet == null)
            {
                throw new ArgumentNullException(nameof(anotherSet));
            }

            if (this.Empty)
            {
                return(true);
            }

            if (this.Count > anotherSet.Count)
            {
                return(false);
            }

            var thisEnumerator    = GetEnumerator();
            var anotherEnumerator = anotherSet.GetEnumerator();

            var thisEnded    = !thisEnumerator.MoveNext();
            var anotherEnded = !anotherEnumerator.MoveNext();

            while (!thisEnded && !anotherEnded)
            {
                int compareResult = comparer.Compare(thisEnumerator.Current, anotherEnumerator.Current);
                if (compareResult < 0)
                {
                    return(false);
                }
                else if (compareResult > 0)
                {
                    anotherEnded = !anotherEnumerator.MoveNext();
                }
                else
                {
                    thisEnded    = !thisEnumerator.MoveNext();
                    anotherEnded = !anotherEnumerator.MoveNext();
                }
            }

            return(thisEnded);
        }
예제 #4
0
        public RedBlackTreeSet <T> Intersect(RedBlackTreeSet <T> anotherSet)
        {
            if (anotherSet == null)
            {
                throw new ArgumentNullException(nameof(anotherSet));
            }

            if (anotherSet.Empty)
            {
                return(new RedBlackTreeSet <T>());
            }

            int intersectionCount = 0;
            var minSize           = this.Count > anotherSet.Count ? anotherSet.Count : this.Count;
            var intersectionArray = new T[minSize];

            MergeInOrderTraversal(anotherSet, null, null, item => intersectionArray[intersectionCount++] = item);
            var intersectionTree = new LeftLeaningRedBlackTree <T>(intersectionArray, 0, intersectionCount - 1, comparer);

            return(new RedBlackTreeSet <T>(intersectionTree, comparer));
        }
예제 #5
0
        public RedBlackTreeSet <T> Union(RedBlackTreeSet <T> anotherSet)
        {
            if (anotherSet == null)
            {
                throw new ArgumentNullException(nameof(anotherSet));
            }

            if (anotherSet.Empty)
            {
                return(new RedBlackTreeSet <T>(this));
            }

            int        unionCount = 0;
            var        unionArray = new T[this.Count + anotherSet.Count];
            Action <T> handler    = item => unionArray[unionCount++] = item;

            MergeInOrderTraversal(anotherSet, handler, handler, handler);
            var unionTree = new LeftLeaningRedBlackTree <T>(unionArray, 0, unionCount - 1, comparer);

            return(new RedBlackTreeSet <T>(unionTree, comparer));
        }
예제 #6
0
        public RedBlackTreeSet <T> Difference(RedBlackTreeSet <T> anotherSet)
        {
            if (anotherSet == null)
            {
                throw new ArgumentNullException(nameof(anotherSet));
            }

            if (anotherSet.Empty)
            {
                return(new RedBlackTreeSet <T>(this));
            }

            int differenceCount = 0;
            var differenceArray = new T[this.Count];

            MergeInOrderTraversal(anotherSet, item => differenceArray[differenceCount++] = item, null, null);

            var differenceTree = new LeftLeaningRedBlackTree <T>(differenceArray, 0, differenceCount - 1, comparer);

            return(new RedBlackTreeSet <T>(differenceTree, comparer));
        }