Ejemplo n.º 1
0
        public bool IsSubset(ArraySet <T> anotherSet)
        {
            if (anotherSet == null)
            {
                throw new ArgumentNullException(nameof(anotherSet));
            }

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

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

            var startIndex = 0;

            foreach (var item in this)
            {
                var index = Array.BinarySearch(anotherSet.items, startIndex, anotherSet.Count - startIndex, item, comparer);
                if (index >= 0)
                {
                    startIndex = index;
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        public ArraySet <T> Difference(ArraySet <T> anotherSet)
        {
            if (anotherSet == null)
            {
                throw new ArgumentNullException(nameof(anotherSet));
            }

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

            var differenceList = new List <T>();
            var startIndex     = 0;

            foreach (var item in this)
            {
                var index = Array.BinarySearch(anotherSet.items, startIndex, anotherSet.Count - startIndex, item, comparer);
                if (index >= 0)
                {
                    startIndex = index;
                }
                else
                {
                    differenceList.Add(item);
                    startIndex = -index - 1;
                }
            }

            var difference = new ArraySet <T>(differenceList.ToArray(), differenceList.Count, comparer);

            return(difference);
        }
Ejemplo n.º 3
0
        public ArraySet(ArraySet <T> set)
        {
            count         = set.count;
            items         = new T[set.Capacity];
            this.comparer = set.comparer;

            Array.Copy(set.items, items, set.Count);
        }
Ejemplo n.º 4
0
        private T[] GenerateUnionArray(ArraySet <T> anotherSet, out int count)
        {
            var unionArray = new T[this.Count + anotherSet.Count];
            var nextIndex  = 0;

            var thisSetIndex    = 0;
            var anotherSetIndex = 0;

            while (thisSetIndex < Count && anotherSetIndex < anotherSet.Count)
            {
                var compareResult = comparer.Compare(items[thisSetIndex], anotherSet.items[anotherSetIndex]);

                if (compareResult < 0)
                {
                    unionArray[nextIndex++] = items[thisSetIndex++];
                }
                else if (compareResult > 0)
                {
                    unionArray[nextIndex++] = anotherSet.items[anotherSetIndex++];
                }
                else
                {
                    unionArray[nextIndex++] = items[thisSetIndex];
                    thisSetIndex++;
                    anotherSetIndex++;
                }
            }

            if (thisSetIndex < Count)
            {
                var remainItems = Count - thisSetIndex;
                Array.Copy(items, thisSetIndex, unionArray, nextIndex, remainItems);
                count = nextIndex + remainItems;
                return(unionArray);
            }

            if (anotherSetIndex < anotherSet.Count)
            {
                var remainItems = anotherSet.Count - anotherSetIndex;
                Array.Copy(anotherSet.items, anotherSetIndex, unionArray, nextIndex, remainItems);
                count = nextIndex + remainItems;
                return(unionArray);
            }

            count = nextIndex;
            return(unionArray);
        }
Ejemplo n.º 5
0
        public ArraySet <T> Union(ArraySet <T> anotherSet)
        {
            if (anotherSet == null)
            {
                throw new ArgumentNullException(nameof(anotherSet));
            }

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

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

            int unionCount;
            var unionArray = GenerateUnionArray(anotherSet, out unionCount);

            var union = new ArraySet <T>(unionArray, unionCount, comparer);

            return(union);
        }