Beispiel #1
0
 public Segment(ISegmentSource <T> source)
 {
     this.source    = source ?? throw new ArgumentNullException(nameof(source));
     this.HasSource = true;
     this.Offset    = 0;
     this.Count     = source.Count;
 }
Beispiel #2
0
        public Segment(ISegmentSource <T> source, int offset, int count)
        {
            if (source == null || (uint)offset > (uint)source.Count || (uint)count > (uint)(source.Count - offset))
            {
                throw ThrowHelper.GetSegmentCtorValidationFailedException(source, offset, count);
            }

            this.source    = source;
            this.HasSource = true;
            this.Offset    = offset;
            this.Count     = count;
        }
Beispiel #3
0
        public Segment(T[] source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            this.source    = new SourceReadOnlyList(source);
            this.HasSource = true;
            this.Offset    = 0;
            this.Count     = source.Length;
        }
Beispiel #4
0
        public Segment(T[] source, int offset, int count)
        {
            // Validate arguments, check is minimal instructions with reduced branching for inlinable fast-path
            // Negative values discovered though conversion to high values when converted to unsigned
            // Failure should be rare and location determination and message is delegated to failure functions
            if (source == null || (uint)offset > (uint)source.Length || (uint)count > (uint)(source.Length - offset))
            {
                throw ThrowHelper.GetSegmentCtorValidationFailedException(source, offset, count);
            }

            this.source    = new SourceReadOnlyList(source);
            this.HasSource = true;
            this.Offset    = offset;
            this.Count     = count;
        }
Beispiel #5
0
        public static Exception GetSegmentCtorValidationFailedException <T>(ISegmentSource <T> source, int offset, int count)
        {
            if (source == null)
            {
                return(new ArgumentNullException(nameof(source)));
            }

            if (offset < 0)
            {
                return(new ArgumentOutOfRangeException(nameof(offset), "Non-negative number required."));
            }

            if (count < 0)
            {
                return(new ArgumentOutOfRangeException(nameof(count), "Non-negative number required."));
            }

            return(new ArgumentException("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."));
        }
Beispiel #6
0
 /// <summary>
 /// Creates a new segment by skipping a number of elements and then taking a number of elements from this source.
 /// </summary>
 /// <typeparam name="T">The type of elements contained in the source.</typeparam>
 /// <param name="source">The source.</param>
 /// <param name="offset">The offset in this source where the new segment begins. Must be in the range <c>[0, <paramref name="source"/>.Count]</c>.</param>
 /// <param name="count">The length of the new segment. Must be in the range <c>[0, <paramref name="source"/>.Count - <paramref name="offset"/>]</c>.</param>
 /// <returns>The new segment.</returns>
 public static Segment <T> Slice <T>(this ISegmentSource <T> source, int offset, int count)
 => source == null ? Segment <T> .Empty : new Segment <T>(source, offset, count);
 public bool Equals(ISegmentSource <char> obj)
 => obj is CharSource other && Equals(in other);
Beispiel #8
0
 /// <summary>
 /// Creates an segment referencing this source.
 /// </summary>
 /// <typeparam name="T">The type of elements contained in the source.</typeparam>
 /// <param name="source">The source.</param>
 /// <param name="offset">The offset in this source where the segment begins. Defaults to <c>0</c> (the beginning of the source). Must be in the range <c>[0, <paramref name="source"/>.Count]</c>.</returns>
 public static Segment <T> AsSegment <T>(this ISegmentSource <T> source, int offset = 0)
 => source == null ? Segment <T> .Empty : new Segment <T>(source, offset, source.Count - offset);