Exemplo n.º 1
0
 /// <summary>
 /// Returns the max value between the two ints
 /// </summary>
 public int Max(int x, int y)
 {
     return(_math.Max(x, y));
 }
Exemplo n.º 2
0
        private IList <T> SliceImpl <T>(IList <T> list, int start, int end, bool allowOverflow, int?pstep)
        {
            if (!allowOverflow)
            {
                if (start < -list.Count)
                {
                    throw new IndexOutOfRangeException(
                              "start index value must be greater than or equal to -list.Count unless allowOverflow option is used, actual value was " +
                              start);
                }

                if (start >= list.Count)
                {
                    throw new IndexOutOfRangeException(
                              "start index value must be less than list.Count unless allowOverflow option is used, actual value was " +
                              start);
                }

                if (end < -list.Count)
                {
                    throw new IndexOutOfRangeException(
                              "end index value must be greater than or equal to -list.Count unless allowOverflow option is used, actual value was " +
                              end);
                }


                if (end >= list.Count)
                {
                    throw new IndexOutOfRangeException(
                              "end index value must be less than list.Count unless allowOverflow option is used, actual value was " +
                              end);
                }

                if ((start < 0 && end >= 0) || (start >= 0 && end < 0))
                {
                    throw new InvalidOperationException(
                              "When using a negative index both values must be negative, actual values were " + start +
                              " for start and " + end + " for end");
                }
            }

            int count = list.Count;
            var len   = (end - start);

            T[] returning;

            //creates an offset for handling negative indexes
            int offset = ((_math.Max(_math.Abs(start), _math.Abs(end)) / count) + 1) * count;

            if (pstep.HasValue && pstep.Value == 0)
            {
                throw new InvalidOperationException("Cannot have a zero value step");
            }

            int step = 1;

            //when the len is negative we are going backwards
            if (len < 0)
            {
                if (pstep.HasValue)
                {
                    //if we are going backwards and a value was passed
                    //the value has to be negative or we're going to
                    //throw an invalid operation exception
                    if (pstep.Value >= 0)
                    {
                        throw new InvalidOperationException("Cannot have a backwards slice with a forward (postive) step, value passed " + pstep.Value);
                    }

                    step = -pstep.Value;
                }

                if (len % step == 0)
                {
                    len /= step;
                }
                else
                {
                    len = (len / step) - 1;
                }

                len = -len;

                returning = new T[len];


                int j = 0;
                for (int i = start; i > end; i -= step)
                {
                    returning[j] = list[(i + offset) % count];
                    j++;
                }
            }
            else if (len > 0)
            {
                if (pstep.HasValue)
                {
                    //if we are going forwards and the passed step is negative
                    //will throw an exception
                    if (pstep.Value <= 0)
                    {
                        throw new InvalidOperationException("Cannot have a forward slice with a backwards (negative) step, value passed " + pstep.Value);
                    }

                    step = pstep.Value;
                }

                if (len % step == 0)
                {
                    len /= step;
                }
                else
                {
                    len = (len / step) + 1;
                }

                returning = new T[len];

                if (step < 0)
                {
                    var t = start;
                    start = end;
                    end   = t;
                    step  = -step;
                }

                int j = 0;
                for (int i = start; i < end; i += step)
                {
                    returning[j] = list[(i + offset) % count];
                    j++;
                }
            }
            else
            {
                returning = new T[] { };
            }

            return(returning);
        }