Exemplo n.º 1
0
        /// <summary>
        ///     Returns a sub-array from the given input array, based on the indexes
        ///     returned by the provided range instance.
        ///     For example, if the Range elements are [3,2,1], then this method
        ///     will return an array consisting of the elements 3,2,1 from the
        ///     input array.
        ///     If the input array is null, this method returns null.
        ///     If the input range is null or empty, this method returns an empty array.
        ///     If the elements of the range are outside the array bounds, it throws
        ///     an ArgumentOutOfRangeException
        /// </summary>
        public QArray <T> Slice(QRange range)
        {
            if (range == null)
            {
                // Debug.Assert(false, $"Trying to slice an array with a null range");
                return(new QArray <T>());
            }

            if (range.IsEmpty)
            {
                return(new QArray <T>());
            }
            long rangeCount = 1 + (range.End - range.Start) / range.Step;
            long rangeEnd   = range.Start + (rangeCount - 1) * range.Step;

            // Make sure the slice fits
            if ((range.Start >= Length) || (range.Start < 0) || (rangeEnd >= Length) || (rangeEnd < 0))
            {
                throw new ArgumentOutOfRangeException();
            }

            if (range.Start == range.End)
            {
                return(new QArray <T>(this[range.Start]));
            }

            long newStart = ConvertIndex(range.Start);
            long newStep  = range.Step * step;
            long newCount = rangeCount;

            QArray <T> result = new QArray <T>(this);

            result.start  = newStart;
            result.step   = newStep;
            result.Length = newCount;

            return(result);
        }
Exemplo n.º 2
0
 IQArray <T> IQArray <T> .Slice(QRange range) => Slice(range);