Ejemplo n.º 1
0
        public void AddSorted <U>(SCG.IEnumerable <U> items) where U : T
        {
            //Unless items have <=1 elements we would expect it to be
            //too expensive to do repeated inserts, thus:
            updatecheck();

            int j = 0, i = 0, c = -1, itemcount = EnumerableBase <U> .countItems(items);

            SortedArray <T> res = new SortedArray <T>(Size + itemcount, _comparer);
            T lastitem          = default(T);

            bool raiseAdded            = (ActiveEvents & EventTypeEnum.Added) != 0;
            CircularQueue <T> wasAdded = raiseAdded ? new CircularQueue <T>() : null;

            foreach (T item in items)
            {
                while (i < Size && (c = _comparer.Compare(array[i], item)) <= 0)
                {
                    lastitem = res.array[j++] = array[i++];
                    if (c == 0)
                    {
                        goto next;
                    }
                }

                if (j > 0 && _comparer.Compare(lastitem, item) >= 0)
                {
                    throw new ArgumentException("Argument not sorted");
                }

                lastitem = res.array[j++] = item;
next:
                c = -1;

                if (raiseAdded)
                {
                    wasAdded.Enqueue(item);
                }
            }

            while (i < Size)
            {
                res.array[j++] = array[i++];
            }

            Size  = j;
            array = res.array;

            if (raiseAdded)
            {
                foreach (T item in wasAdded)
                {
                    raiseItemsAdded(item, 1);
                }
            }
            if (((ActiveEvents & EventTypeEnum.Changed) != 0))
            {
                raiseCollectionChanged();
            }
        }
Ejemplo n.º 2
0
        public void AddAll <U>(SCG.IEnumerable <U> items) where U : T
        {
            int toadd = EnumerableBase <U> .countItems(items);

            int newsize = array.Length;

            while (newsize < Size + toadd)
            {
                newsize *= 2;
            }
            T[] newarr = new T[newsize];

            toadd = 0;
            foreach (T item in items)
            {
                newarr[Size + toadd++] = item;
            }

            Sorting.IntroSort <T>(newarr, Size, toadd, _comparer);

            int j = 0, i = 0;
            T   lastitem = default(T);

            //The following eliminates duplicates (including duplicates in input)
            //while merging the old and new collection
            for (int k = Size, klimit = Size + toadd; k < klimit; k++)
            {
                while (i < Size && _comparer.Compare(array[i], newarr[k]) <= 0)
                {
                    lastitem = newarr[j++] = array[i++];
                }

                if (j == 0 || _comparer.Compare(lastitem, newarr[k]) < 0)
                {
                    lastitem = newarr[j++] = newarr[k];
                }
            }

            while (i < Size)
            {
                newarr[j++] = array[i++];
            }

            Array.Clear(newarr, j, Size + toadd - j);
            Size  = j;
            array = newarr;
        }
Ejemplo n.º 3
0
        public void AddSorted <U>(SCG.IEnumerable <U> items) where U : T
        {
            //Unless items have <=1 elements we would expect it to be
            //too expensive to do repeated inserts, thus:
            updatecheck();

            int j = 0, i = 0, c = -1, itemcount = EnumerableBase <U> .countItems(items);

            SortedArray <T> res = new SortedArray <T>(size + itemcount, comparer);
            T lastitem          = default(T);

            foreach (T item in items)
            {
                while (i < size && (c = comparer.Compare(array[i], item)) <= 0)
                {
                    lastitem = res.array[j++] = array[i++];
                    if (c == 0)
                    {
                        goto next;
                    }
                }

                if (j > 0 && comparer.Compare(lastitem, item) >= 0)
                {
                    throw new ArgumentException("Argument not sorted");
                }

                lastitem = res.array[j++] = item;
next:
                c = -1;
            }

            while (i < size)
            {
                res.array[j++] = array[i++];
            }

            size  = j;
            array = res.array;
        }