예제 #1
0
        /// <summary>
        /// Add all the items from another collection with an enumeration order that
        /// is increasing in the items.
        /// <exception cref="ArgumentException"/> if the enumerated items turns out
        /// not to be in increasing order.
        /// </summary>
        /// <param name="items">The collection to add.</param>
        public void AddSorted(SCG.IEnumerable <T> items)
        {
            //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 = countItems(items), numAdded = 0;
            SortedArray <T> res = new SortedArray <T>(size + itemcount, _comparer);
            T lastitem          = default(T);

            T[] addedItems = new T[itemcount];

            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");
                }

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

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

            size  = j;
            array = res.array;
            raiseForAddAll(addedItems, numAdded);
        }
예제 #2
0
        /// <summary>
        /// Create a new indexed sorted collection consisting of the results of
        /// mapping all items of this list.
        /// <exception cref="ArgumentException"/> if the map is not increasing over
        /// the items of this collection (with respect to the two given comparison
        /// relations).
        /// </summary>
        /// <param name="m">The delegate definging the map.</param>
        /// <param name="c">The comparion relation to use for the result.</param>
        /// <returns>The new sorted collection.</returns>
        public IIndexedSorted <V> Map <V>(Func <T, V> m, SCG.IComparer <V> c)
        {
            SortedArray <V> res = new SortedArray <V>(size, c);

            if (size > 0)
            {
                V oldv = res.array[0] = m(array[0]), newv;

                for (int i = 1; i < size; i++)
                {
                    if (c.Compare(oldv, newv = res.array[i] = m(array[i])) >= 0)
                    {
                        throw new ArgumentException("mapper not monotonic");
                    }

                    oldv = newv;
                }
            }

            res.size = size;
            return(res);
        }
예제 #3
0
        /// <summary>
        /// Create a new indexed sorted collection consisting of the items of this
        /// indexed sorted collection satisfying a certain predicate.
        /// </summary>
        /// <param name="f">The filter delegate defining the predicate.</param>
        /// <returns>The new indexed sorted collection.</returns>
        public IIndexedSorted <T> FindAll(Func <T, bool> f)
        {
            SortedArray <T> res = new SortedArray <T>(_comparer);
            int             j = 0, rescap = res.array.Length;

            for (int i = 0; i < size; i++)
            {
                T a = array[i];

                if (f(a))
                {
                    if (j == rescap)
                    {
                        res.expand(rescap = 2 * rescap, j);
                    }

                    res.array[j++] = a;
                }
            }

            res.size = j;
            return(res);
        }
예제 #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="comparer"></param>
 /// <param name="equalityComparer"></param>
 /// <param name="capacity"></param>
 public SortedArrayDictionary(int capacity, SCG.IComparer <K> comparer, SCG.IEqualityComparer <K> equalityComparer)
     : base(comparer, equalityComparer)
 {
     pairs = sortedpairs = new SortedArray <KeyValuePair <K, V> >(capacity, new KeyValuePairComparer <K, V>(comparer));
 }