Exemple #1
0
        /// <summary>
        ///     Creates an array from the contents of this builder.
        /// </summary>
        /// <remarks>
        ///     Do not call this method twice on the same builder.
        /// </remarks>
        public T[] ToArray()
        {
            if (Count == 0)
            {
                return(ArrayEx.Empty <T>());
            }

            Debug.Assert(Buffer != null); // Nonzero _count should imply this

            var result = Buffer;

            if (Count < result.Length)
            {
                // Avoid a bit of overhead (method call, some branches, extra code-gen)
                // which would be incurred by using Array.Resize
                result = new T[Count];
                Array.Copy(Buffer, 0, result, 0, Count);
            }

#if DEBUG
            // Try to prevent callers from using the ArrayBuilder after ToArray, if _count != 0.
            Count  = -1;
            Buffer = null;
#endif

            return(result);
        }
Exemple #2
0
 /// <summary>
 ///     Initializes the <see cref="ArrayBuilder{T}" /> with a specified capacity.
 /// </summary>
 /// <param name="capacity">The capacity of the array to allocate.</param>
 public ArrayBuilder(int capacity)
     : this()
 {
     Debug.Assert(capacity >= 0);
     if (capacity > 0)
     {
         Buffer = new T[capacity];
     }
     else
     {
         Buffer = ArrayEx.Empty <T>();
     }
 }
Exemple #3
0
        /// <summary>
        ///     Creates an array from the contents of this builder.
        /// </summary>
        /// <remarks>
        ///     Do not call this method twice on the same builder.
        /// </remarks>
        public T[] ToArray()
        {
            if (Count == 0)
            {
                return(ArrayEx.Empty <T>());
            }

            if (Count == Buffer.Length)
            {
                return(Buffer);
            }

            // Avoid a bit of overhead (method call, some branches, extra code-gen)
            // which would be incurred by using Array.Resize
            var result = new T[Count];

            Array.Copy(Buffer, 0, result, 0, Count);
            return(result);
        }
Exemple #4
0
        protected HashSet(SerializationInfo info, StreamingContext context)
            : this()
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            No.Op(context);
            var dictionary = (info.GetValue("dictionary", typeof(KeyValuePair <T, object?>[])) as KeyValuePair <T, object?>[]) ?? ArrayEx.Empty <KeyValuePair <T, object?> >();
            var comparer   = info.GetValue("comparer", typeof(IEqualityComparer <T>)) ?? EqualityComparer <T> .Default;

            _wrapped = new NullAwareDictionary <T, object?>(dictionary, comparer);
        }