Esempio n. 1
0
 /// <summary>
 /// Gets the <see cref="IEqualityComparer{HashBucket}"/> to use.
 /// </summary>
 /// <param name="valueComparer">The value comparer for T.</param>
 /// <returns>The equality comparer to use.</returns>
 private static IEqualityComparer <HashBucket> GetHashBucketEqualityComparer(IEqualityComparer <T> valueComparer)
 {
     if (!ImmutableExtensions.IsValueType <T>())
     {
         return(HashBucketByRefEqualityComparer.DefaultInstance);
     }
     else if (valueComparer == EqualityComparer <T> .Default)
     {
         return(HashBucketByValueEqualityComparer.DefaultInstance);
     }
     else
     {
         return(new HashBucketByValueEqualityComparer(valueComparer));
     }
 }
Esempio n. 2
0
        public ImmutableArray <T> InsertRange(int index, IEnumerable <T> items)
        {
            var self = this;

            self.ThrowNullRefIfNotInitialized();
            Requires.Range(index >= 0 && index <= self.Length, nameof(index));
            Requires.NotNull(items, nameof(items));

            if (self.Length == 0)
            {
                return(ImmutableArray.CreateRange(items));
            }

            int count = ImmutableExtensions.GetCount(ref items);

            if (count == 0)
            {
                return(self);
            }

            T[] tmp = new T[self.Length + count];

            if (index != 0)
            {
                Array.Copy(self.array, 0, tmp, 0, index);
            }
            if (index != self.Length)
            {
                Array.Copy(self.array, index, tmp, index + count, self.Length - index);
            }

            // We want to copy over the items we need to insert.
            // Check first to see if items is a well-known collection we can call CopyTo
            // on to the array, which is an order of magnitude faster than foreach.
            // Otherwise, go to the fallback route where we manually enumerate the sequence
            // and place the items in the array one-by-one.

            if (!items.TryCopyTo(tmp, index))
            {
                int sequenceIndex = index;
                foreach (var item in items)
                {
                    tmp[sequenceIndex++] = item;
                }
            }

            return(new ImmutableArray <T>(tmp));
        }
Esempio n. 3
0
            /// <summary>
            /// Inserts the specified values at the specified index.
            /// </summary>
            /// <param name="index">The index at which to insert the value.</param>
            /// <param name="items">The elements to insert.</param>
            public void InsertRange(int index, IEnumerable <T> items)
            {
                Requires.Range(index >= 0 && index <= this.Count, nameof(index));
                Requires.NotNull(items, nameof(items));

                int count = ImmutableExtensions.GetCount(ref items);

                this.EnsureCapacity(this.Count + count);

                if (index != this.Count)
                {
                    Array.Copy(_elements, index, _elements, index + count, _count - index);
                }

                if (!items.TryCopyTo(_elements, index))
                {
                    foreach (var item in items)
                    {
                        _elements[index++] = item;
                    }
                }

                _count += count;
            }