Beispiel #1
0
 public T this[int index, T defaultValue]
 {
     get {
         if ((uint)index < (uint)_count)
         {
             bool fail;
             var  r = _list.TryGet(_start + index, out fail);
             return(fail ? defaultValue : r);
         }
         return(defaultValue);
     }
 }
Beispiel #2
0
        static LNode Single(IListSource <LNode> e)
        {
            LNode node = e.TryGet(0, null);

            if (node == null)
            {
                throw new InvalidOperationException(Localize.Localized("ParseSingle: result was empty."));
            }
            if (e.TryGet(1, null) != null)             // don't call Count because e is typically Buffered()
            {
                throw new InvalidOperationException(Localize.Localized("ParseSingle: multiple parse results."));
            }
            return(node);
        }
Beispiel #3
0
        /// <summary>Uses list.TryGet(index) to find out if the specified index is valid.</summary>
        /// <returns>true if the specified index is valid, false if not.</returns>
        public static bool HasIndex <T>(this IListSource <T> list, int index)
        {
            bool fail;

            list.TryGet(index, out fail);
            return(!fail);
        }
Beispiel #4
0
        public TResult TryGet(int index, ref bool fail)
        {
            T t = _list.TryGet(index, ref fail);

            if (!fail)
            {
                return(_selector(t));
            }
            return(default(TResult));
        }
        public sealed override TResult TryGet(int index, out bool fail)
        {
            T t = _list.TryGet(index, out fail);

            if (!fail)
            {
                return(_selector(t));
            }
            return(default(TResult));
        }
Beispiel #6
0
        public static T FirstOrDefault <T>(this IListSource <T> list, T defaultValue)
        {
            bool fail;
            var  result = list.TryGet(0, out fail);

            if (fail)
            {
                return(defaultValue);
            }
            return(result);
        }
Beispiel #7
0
        /// <summary>Tries to get a value from the list at the specified index.</summary>
        /// <param name="index">The index to access. Valid indexes are between 0 and Count-1.</param>
        /// <returns>The retrieved value wrapped in <see cref="Maybe{T}"/>, if any.</returns>
        public static Maybe <T> TryGet <T>(this IListSource <T> list, int index)
        {
            bool fail;
            T    result = list.TryGet(index, out fail);

            if (fail)
            {
                return(Maybe <T> .NoValue);
            }
            return(new Maybe <T>(result));
        }
Beispiel #8
0
        /// <summary>Tries to get a value from the list at the specified index.</summary>
        /// <param name="index">The index to access. Valid indexes are between 0 and Count-1.</param>
        /// <param name="value">A variable that will be changed to the retrieved value. If the index is not valid, this variable is left unmodified.</param>
        /// <returns>True on success, or false if the index was not valid.</returns>
        public static bool TryGet <T>(this IListSource <T> list, int index, ref T value)
        {
            bool fail;
            T    result = list.TryGet(index, out fail);

            if (fail)
            {
                return(false);
            }
            value = result;
            return(true);
        }
Beispiel #9
0
 protected bool AdvanceAfterNextNewline(ref int index)
 {
     for (;;)
     {
         bool fail;
         char c = _source.TryGet(index, out fail);
         if (fail)
         {
             return(false);
         }
         if (c == '\r' || c == '\n')
         {
             index++;
             if (c == '\r' && _source.TryGet(index, out fail) == '\n')
             {
                 index++;
             }
             return(true);
         }
         index++;
     }
 }
Beispiel #10
0
        /// <summary>Tries to get a value from the list at the specified index.</summary>
        /// <param name="index">The index to access. Valid indexes are between 0 and Count-1.</param>
        /// <param name="defaultValue">A value to return if the index is not valid.</param>
        /// <returns>The retrieved value, or defaultValue if the index provided was not valid.</returns>
        public static T TryGet <T>(this IListSource <T> list, int index, T defaultValue)
        {
            bool fail;
            T    result = list.TryGet(index, out fail);

            if (fail)
            {
                return(defaultValue);
            }
            else
            {
                return(result);
            }
        }
Beispiel #11
0
        public Iterator <T> GetIterator()
        {
            int             i = _start, stop = i + _length;
            IListSource <T> list = _obj;

            return(delegate(ref bool ended)
            {
                if (i < stop)
                {
                    return list.TryGet(i++, ref ended);
                }
                ended = true;
                return default(T);
            });
        }
Beispiel #12
0
        /// <summary>Returns a slice without the initial elements of the list that meet the specified
        /// criteria. The word "now" is added to the name because unlike Enumerable.SkipWhile, this
        /// method scans the list immediately.</summary>
        /// <remarks>Example: new[] { 24, 28, 2, 12, 11 }.SkipNowWhile(n => n > 10) returns a slice
        /// (not a copy) of the last 2 elements.</remarks>
        public static Slice_ <T> SkipNowWhile <T>(this IListSource <T> list, Func <T, bool> predicate)
        {
            Maybe <T> value;

            for (int i = 0;; i++)
            {
                if (!(value = list.TryGet(i)).HasValue)
                {
                    return(new Slice_ <T>());
                }
                else if (!predicate(value.Value))
                {
                    return(new Slice_ <T>(list, i));
                }
            }
        }
Beispiel #13
0
 public T TryGet(int index, ref bool fail)
 {
     return(_list.TryGet(_list.Count - 1 - index, ref fail));
 }
Beispiel #14
0
 public sealed override T TryGet(int index, out bool fail)
 {
     return(_list.TryGet(_list.Count - 1 - index, out fail));
 }
Beispiel #15
0
        protected sealed override Token LT(int i)
        {
            bool fail;

            return(_tokens.TryGet(InputPosition + i, out fail));
        }
Beispiel #16
0
 public override TOut TryGet(int index, out bool fail)
 {
     return(_list.TryGet(index, out fail));
 }
Beispiel #17
0
        public static T FirstOrDefault <T>(this IListSource <T> list)
        {
            bool _;

            return(list.TryGet(0, out _));
        }
Beispiel #18
0
		static LNode Single(IListSource<LNode> e)
		{
			LNode node = e.TryGet(0, null);
			if (node == null)
				throw new InvalidOperationException(Localize.Localized("ParseSingle: result was empty."));
			if (e.TryGet(1, null) != null) // don't call Count because e is typically Buffered()
				throw new InvalidOperationException(Localize.Localized("ParseSingle: multiple parse results."));
			return node;
		}
Beispiel #19
0
 protected override Token LT(int i)
 {
     return(_tokens.TryGet(InputPosition + i, default(Token)));
 }
Beispiel #20
0
 /// <inheritdoc/>
 public T TryGet(int index, ref bool fail)
 {
     return(_list.TryGet(_offset + index, ref fail));
 }