Пример #1
0
        // Removes a range of elements from this list.
        //
        public void RemoveRange <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int index, int count)
        {
            if (index < 0)
            {
                throw XExceptions.Argument.OutOfRange.NeedNonNegativeNumber(nameof(index));
            }

            if (count < 0)
            {
                throw XExceptions.Argument.OutOfRange.Count(nameof(count));
            }

            if (list.Count - index < count)
            {
                throw XExceptions.Argument.InvalidOffsetLength(nameof(index));
            }
            Contract.EndContractBlock();

            if (count <= 0)
            {
                return;
            }

            list.Count -= count;
            if (index < list.Count)
            {
                Array.Copy(list.Items, index + count, list.Items, index, list.Count - index);
            }
            Array.Clear(list.Items, list.Count, count);
            list.Version++;
        }
Пример #2
0
        public int FindIndex <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int startIndex, int count, Predicate <T> match)
        {
            if ((uint)startIndex > (uint)list.Count)
            {
                throw XExceptions.Argument.OutOfRange.Index(nameof(startIndex));
            }

            if (count < 0 || startIndex > list.Count - count)
            {
                throw XExceptions.Argument.OutOfRange.Count(nameof(count));
            }

            if (match == null)
            {
                throw XExceptions.Argument.IsNull(nameof(match));
            }

            Contract.Ensures(Contract.Result <int>() >= -1);
            Contract.Ensures(Contract.Result <int>() < startIndex + count);
            Contract.EndContractBlock();

            var endIndex = startIndex + count;

            for (var i = startIndex; i < endIndex; i++)
            {
                if (match(list.Items[i]))
                {
                    return(i);
                }
            }
            return(-1);
        }
Пример #3
0
        public Root.Code.Models.E01D.Core.Collections.Generic.List <T> GetRange <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int index, int count)
        {
            if (index < 0)
            {
                throw XExceptions.Argument.OutOfRange.NeedNonNegativeNumber(nameof(index));
            }

            if (count < 0)
            {
                throw XExceptions.Argument.OutOfRange.Count(nameof(count));
            }

            if (list.Count - index < count)
            {
                throw XExceptions.Argument.InvalidOffsetLength(nameof(index));
            }
            Contract.Ensures(Contract.Result <Root.Code.Models.E01D.Core.Collections.Generic.List <T> >() != null);
            Contract.EndContractBlock();

            var newList = Create <T>(count);

            Array.Copy(list.Items, index, newList.Items, 0, count);

            newList.Count = count;
            return(list);
        }
Пример #4
0
        // Constructs a List, copying the contents of the given collection. The
        // size and capacity of the new list will both be equal to the size of the
        // given collection.
        //
        public Root.Code.Models.E01D.Core.Collections.Generic.List <T> Create <T>(IEnumerable <T> collection)
        {
            if (collection == null)
            {
                throw XExceptions.Argument.IsNull(nameof(collection));
            }

            var list = new Root.Code.Models.E01D.Core.Collections.Generic.List <T>();

            var c = collection as Collection_I <T>;

            if (c != null && c.Count == 0)
            {
                list.Items = EmptyArray <T> .Value;
            }
            else
            {
                list.Count = 0;
                list.Items = EmptyArray <T> .Value;

                AddEnumerable(list, collection);
            }

            return(list);
        }
Пример #5
0
        // Returns the index of the last occurrence of a given value in a range of
        // this list. The list is searched backwards, starting at index
        // index and upto count elements. The elements of
        // the list are compared to the given value using the Object.Equals
        // method.
        //
        // This method uses the Array.LastIndexOf method to perform the
        // search.
        //
        public int LastIndexOf <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, T item, int index, int count)
        {
            if ((list.Count != 0) && (index < 0))
            {
                throw XExceptions.Argument.OutOfRange.NeedNonNegativeNumber(nameof(index));
            }

            if ((list.Count != 0) && (count < 0))
            {
                throw XExceptions.Argument.OutOfRange.Count(nameof(count));
            }


            if (list.Count == 0)
            {  // Special case for empty list
                return(-1);
            }

            if (index >= list.Count)
            {
                throw XExceptions.Argument.OutOfRange.BiggerThanCollection(nameof(index));
            }

            if (count > index + 1)
            {
                throw XExceptions.Argument.OutOfRange.BiggerThanCollection(nameof(index));
            }

            return(Array.LastIndexOf(list.Items, item, index, count));
        }
Пример #6
0
        public void SetCapacity <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int newCapacity)
        {
            if (newCapacity < list.Count)
            {
                throw XExceptions.Argument.OutOfRange.SmallCapacity(nameof(newCapacity));
            }

            Contract.EndContractBlock();

            if (newCapacity != list.Items.Length)
            {
                if (newCapacity > 0)
                {
                    var newItems = new T[newCapacity];

                    if (list.Count > 0)
                    {
                        Array.Copy(list.Items, 0L, newItems, 0, list.Count);
                    }
                    list.Items = newItems;
                }
                else
                {
                    list.Items = EmptyArray <T> .Value;
                }
            }
        }
Пример #7
0
        // Sorts the elements in a section of this list. The sort compares the
        // elements to each other using the given IComparer interface. If
        // comparer is null, the elements are compared to each other using
        // the IComparable interface, which in that case must be implemented by all
        // elements of the list.
        //
        // This method uses the Array.Sort method to sort the elements.
        //
        public void Sort <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int index, int count, IComparer <T> comparer)
        {
            if (index < 0)
            {
                throw XExceptions.Argument.OutOfRange.NeedNonNegativeNumber(nameof(index));
            }

            if (count < 0)
            {
                throw XExceptions.Argument.OutOfRange.Count(nameof(count));
            }

            if (list.Count - index < count)
            {
                throw XExceptions.Argument.InvalidOffsetLength(nameof(count));
            }

            Contract.EndContractBlock();

            if (count > 1)
            {
                Array.Sort(list.Items, index, count, comparer);
            }
            list.Version++;
        }
Пример #8
0
        // Inserts the elements of the given collection at a given index. If
        // required, the capacity of the list is increased to twice the previous
        // capacity or the new size, whichever is larger.  Ranges may be added
        // to the end of the list by setting index to the List's size.
        //
        public void InsertRange <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int index, IEnumerable <T> collection)
        {
            if (collection == null)
            {
                throw XExceptions.Argument.IsNull(nameof(collection));
            }

            if ((uint)index > (uint)list.Count)
            {
                throw XExceptions.Argument.OutOfRange.Index(nameof(index));
            }

            Contract.EndContractBlock();

            var c = collection as Collection_I <T>;

            if (c != null)
            {    // if collection is ICollection<T>
                var count = c.Count;
                if (count > 0)
                {
                    EnsureCapacity(list, list.Count + count);
                    if (index < list.Count)
                    {
                        Array.Copy(list.Items, index, list.Items, index + count, list.Count - index);
                    }

                    // If we're inserting a List into itself, we want to be able to deal with that.
                    if (list == c)
                    {
                        // Copy first part of list.Items to insert location
                        Array.Copy(list.Items, 0, list.Items, index, index);
                        // Copy last part of list.Items back to inserted location
                        Array.Copy(list.Items, index + count, list.Items, index * 2, list.Count - index);
                    }
                    else
                    {
                        c.CopyTo(list.Items, index);
                    }
                    list.Count += count;
                }
            }
            else if (index < list.Count)
            {
                // We're inserting a lazy enumerable. Call Insert on each of the constituent items.
                using (var en = collection.GetEnumerator())
                {
                    while (en.MoveNext())
                    {
                        Insert(list, index++, en.Current);
                    }
                }
            }
            else
            {
                // We're adding a lazy enumerable because the index is at the end of this list.
                AddEnumerable(list, collection);
            }
            list.Version++;
        }
Пример #9
0
        public Root.Code.Models.E01D.Core.Collections.Generic.List <T> Copy <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list)
        {
            var newList = Create <T>();

            list.Iterate(x => newList.Add(x));

            return(newList);
        }
Пример #10
0
 public void Add <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, T item)
 {
     if (list.Count == list.Items.Length)
     {
         EnsureCapacity(list, list.Count + 1);
     }
     list.Items[list.Count++] = item;
     list.Version++;
 }
Пример #11
0
        /// <summary>
        /// Creates a new List. The list is initially empty and has a capacity
        /// of zero. Upon adding the first element to the list the capacity is
        /// increased to _defaultCapacity, and then increased in multiples of two
        /// as required.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public Root.Code.Models.E01D.Core.Collections.Generic.List <T> Create <T>()
        {
            var list = new Root.Code.Models.E01D.Core.Collections.Generic.List <T>()
            {
                Items = EmptyArray <T> .Value
            };

            return(list);
        }
Пример #12
0
        // Returns the index of the last occurrence of a given value in a range of
        // this list. The list is searched backwards, starting at index
        // index and ending at the first element in the list. The
        // elements of the list are compared to the given value using the
        // Object.Equals method.
        //
        // This method uses the Array.LastIndexOf method to perform the
        // search.
        //
        public int LastIndexOf <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, T item, int index)
        {
            if (index >= list.Count)
            {
                throw XExceptions.Argument.OutOfRange.Index(nameof(index));
            }

            return(LastIndexOf(list, item, index, index + 1));
        }
Пример #13
0
        // Sets the capacity of this list to the size of the list. This method can
        // be used to minimize a list's memory overhead once it is known that no
        // new elements will be added to the list. To completely clear a list and
        // release all memory referenced by the list, execute the following
        // statements:
        //
        // list.Clear();
        // list.TrimExcess();
        //
        public void TrimExcess <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list)
        {
            var threshold = (int)(((double)list.Items.Length) * 0.9);

            if (list.Count < threshold)
            {
                SetCapacity(list, list.Count);
            }
        }
Пример #14
0
        public void Iterate <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, Action <T> action)
        {
            for (var i = 0; i < list.Count; i++)
            {
                var item = list.GetItem(i);

                action(item);
            }
        }
Пример #15
0
        // Clears the contents of List.
        public void Clear <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list)
        {
            if (list.Count > 0)
            {
                Array.Clear(list.Items, 0, list.Count); // Don't need to doc this but we clear the elements so that the gc can reclaim the references.

                list.Count = 0;
            }

            list.Version++;
        }
Пример #16
0
 // Returns the index of the last occurrence of a given value in a range of
 // this list. The list is searched backwards, starting at the end
 // and ending at the first element in the list. The elements of the list
 // are compared to the given value using the Object.Equals method.
 //
 // This method uses the Array.LastIndexOf method to perform the
 // search.
 //
 public int LastIndexOf <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, T item)
 {
     if (list.Count == 0)
     {  // Special case for empty list
         return(-1);
     }
     else
     {
         return(LastIndexOf(list, item, list.Count - 1, list.Count));
     }
 }
Пример #17
0
        // Contains returns true if the specified element is in the List.
        // It does a linear, O(n) search.  Equality is determined by calling
        // EqualityComparer<T>.Default.Equals().

        public bool Contains <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, T item)
        {
            // PERF: IndexOf calls Array.IndexOf, which internally
            // calls EqualityComparer<T>.Default.IndexOf, which
            // is specialized for different types. This
            // boosts performance since instead of making a
            // virtual method call each iteration of the loop,
            // via EqualityComparer<T>.Default.Equals, we
            // only make one virtual call to EqualityComparer.IndexOf.

            return(list.Count != 0 && IndexOf(list, item) != -1);
        }
Пример #18
0
        // Copies a section of this list to the given array at the given index.
        //
        // The method uses the Array.Copy method to copy the elements.
        //
        public void CopyTo <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int index, T[] array, int arrayIndex, int count)
        {
            if (list.Count - index < count)
            {
                XExceptions.Argument.InvalidOffsetLength(nameof(index));
            }

            Contract.EndContractBlock();

            // Delegate rest of error checking to Array.Copy.
            Array.Copy(list.Items, index, array, arrayIndex, count);
        }
Пример #19
0
        public void SetItem <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int index, T item)
        {
            if ((uint)index >= (uint)list.Count)
            {
                throw XExceptions.Argument.OutOfRange.Index(nameof(index));
            }
            Contract.EndContractBlock();

            list.Items[index] = item;

            list.Version++;
        }
Пример #20
0
        // Removes the element at the given index. The size of the list is
        // decreased by one.
        //
        public bool Remove <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, T item)
        {
            var index = IndexOf(list, item);

            if (index >= 0)
            {
                RemoveAt(list, index);
                return(true);
            }

            return(false);
        }
Пример #21
0
        // ToArray returns an array containing the contents of the List.
        // This requires copying the List, which is an O(n) operation.
        public T[] ToArray <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list)
        {
            if (list.Count == 0)
            {
                return(EmptyArray <T> .Value);
            }

            var array = new T[list.Count];

            Array.Copy(list.Items, 0, array, 0, list.Count);

            return(array);
        }
Пример #22
0
        public void Sort <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, Comparison <T> comparison)
        {
            if (comparison == null)
            {
                throw XExceptions.Argument.IsNull(nameof(comparison));
            }
            Contract.EndContractBlock();

            if (list.Count > 1)
            {
                XCollections.Sorting.Sort(list.Items, 0, list.Count, comparison);
            }
            list.Version++;
        }
Пример #23
0
        // Returns the index of the first occurrence of a given value in a range of
        // this list. The list is searched forwards, starting at index
        // index and upto count number of elements. The
        // elements of the list are compared to the given value using the
        // Object.Equals method.
        //
        // This method uses the Array.IndexOf method to perform the
        // search.
        //
        public int IndexOf <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, T item, int index, int count)
        {
            if (index > list.Count)
            {
                throw XExceptions.Argument.OutOfRange.Index(nameof(index));
            }

            if (count < 0 || index > list.Count - count)
            {
                throw XExceptions.Argument.OutOfRange.Count(nameof(count));
            }

            return(Array.IndexOf(list.Items, item, index, count));
        }
Пример #24
0
 // Removes the element at the given index. The size of the list is
 // decreased by one.
 //
 public void RemoveAt <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int index)
 {
     if ((uint)index >= (uint)list.Count)
     {
         throw XExceptions.Argument.OutOfRange.Index(nameof(index));
     }
     Contract.EndContractBlock();
     list.Count--;
     if (index < list.Count)
     {
         Array.Copy(list.Items, index + 1, list.Items, index, list.Count - index);
     }
     list.Items[list.Count] = default(T);
     list.Version++;
 }
Пример #25
0
        public bool TrueForAll <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, Predicate <T> match)
        {
            if (match == null)
            {
                throw XExceptions.Argument.IsNull(nameof(match));
            }
            Contract.EndContractBlock();

            for (var i = 0; i < list.Count; i++)
            {
                if (!match(list.Items[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #26
0
        public T FindLast <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, Predicate <T> match)
        {
            if (match == null)
            {
                throw XExceptions.Argument.IsNull(nameof(match));
            }
            Contract.EndContractBlock();

            for (var i = list.Count - 1; i >= 0; i--)
            {
                if (match(list.Items[i]))
                {
                    return(list.Items[i]);
                }
            }
            return(default(T));
        }
Пример #27
0
        public TOut Iterate <T, TOut>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, FuncOut <T, TOut, bool> action)
        {
            TOut result = default(TOut);

            for (var i = 0; i < list.Count; i++)
            {
                var item = list.GetItem(i);

                if (action(item, out result))
                {
                    return(result);
                }
                ;
            }

            return(result);
        }
Пример #28
0
        public int FindLastIndex <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int startIndex, int count, Predicate <T> match)
        {
            if (match == null)
            {
                throw XExceptions.Argument.IsNull(nameof(match));
            }

            Contract.Ensures(Contract.Result <int>() >= -1);
            Contract.Ensures(Contract.Result <int>() <= startIndex);
            Contract.EndContractBlock();

            if (list.Count == 0)
            {
                // Special case for 0 length List
                if (startIndex != -1)
                {
                    throw XExceptions.Argument.OutOfRange.Index(nameof(startIndex));
                }
            }
            else
            {
                // Make sure we're not out of range
                if ((uint)startIndex >= (uint)list.Count)
                {
                    throw XExceptions.Argument.OutOfRange.Index(nameof(startIndex));
                }
            }

            // 2nd have of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0.
            if (count < 0 || startIndex - count + 1 < 0)
            {
                throw XExceptions.Argument.OutOfRange.Count(nameof(startIndex));
            }

            var endIndex = startIndex - count;

            for (var i = startIndex; i > endIndex; i--)
            {
                if (match(list.Items[i]))
                {
                    return(i);
                }
            }
            return(-1);
        }
Пример #29
0
        public Root.Code.Models.E01D.Core.Collections.Generic.List <T> FindAll <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, Predicate <T> match)
        {
            if (match == null)
            {
                throw XExceptions.Argument.IsNull(nameof(match));
            }

            var newList = new Root.Code.Models.E01D.Core.Collections.Generic.List <T>();

            for (var i = 0; i < list.Count; i++)
            {
                if (match(list.Items[i]))
                {
                    newList.Add(list.Items[i]);
                }
            }
            return(newList);
        }
Пример #30
0
        // Searches a section of the list for a given element using a binary search
        // algorithm. Elements of the list are compared to the search value using
        // the given IComparer interface. If comparer is null, elements of
        // the list are compared to the search value using the IComparable
        // interface, which in that case must be implemented by all elements of the
        // list and the given search value. This method assumes that the given
        // section of the list is already sorted; if this is not the case, the
        // result will be incorrect.
        //
        // The method returns the index of the given value in the list. If the
        // list does not contain the given value, the method returns a negative
        // integer. The bitwise complement operator (~) can be applied to a
        // negative result to produce the index of the first element (if any) that
        // is larger than the given search value. This is also the index at which
        // the search value should be inserted into the list in order for the list
        // to remain sorted.
        //
        // The method uses the Array.BinarySearch method to perform the
        // search.
        //
        public int BinarySearch <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, int index, int count, T item, IComparer <T> comparer)
        {
            if (index < 0)
            {
                throw XExceptions.Argument.OutOfRange.NeedNonNegativeNumber(nameof(index));
            }

            if (count < 0)
            {
                throw XExceptions.Argument.OutOfRange.NeedNonNegativeNumber(nameof(count));
            }

            if (list.Count - index < count)
            {
                throw XExceptions.Argument.InvalidOffsetLength(nameof(count));
            }

            return(Array.BinarySearch(list.Items, index, count, item, comparer));
        }