/// <summary> /// 令位置前进指定字符数组的一部分。 /// </summary> /// <param name="chars">要前进的字符数组。</param> /// <param name="index">要前进的字符的索引。</param> /// <param name="count">要前进的字符的长度。</param> /// <exception cref="ArgumentNullException"><paramref name="chars"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 或 /// <paramref name="count"/> 小于零。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 和 <paramref name="count"/> /// 不表示 <paramref name="chars"/> 中的有效范围。</exception> public void Forward(char[] chars, int index, int count) { if (chars == null) { throw CommonExceptions.ArgumentNull("chars"); } if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (count < 0) { throw CommonExceptions.ArgumentNegative("count", count); } if (index + count > chars.Length) { throw CommonExceptions.ArgumentOutOfRange("count", count); } Contract.EndContractBlock(); if (count == 1) { Forward(chars[index]); } else if (count > 1) { ForwardInternal(chars, index, count); } }
/// <summary> /// 初始化 <see cref="BitList"/> 类的新实例,该实例可拥有指定的初始容量。 /// </summary> /// <param name="capacity">新 <see cref="BitList"/> 最初可以存储的元素数。</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> 小于 <c>0</c>。</exception> public BitList(int capacity) : base(null) { if (capacity < 0) { throw CommonExceptions.ArgumentNegative(nameof(capacity), capacity); } Contract.EndContractBlock(); _items = new uint[(capacity >> IndexShift) + 1]; this._capacity = _items.Length << IndexShift; }
/// <summary> /// 加载指定索引的参数,并转换为指定类型。 /// </summary> /// <param name="il">IL 指令生成器。</param> /// <param name="index">要加载的参数索引。</param> /// <param name="paramType">要加载的参数类型。</param> /// <param name="targetType">要转换到的类型。</param> /// <remarks>会根据 <paramref name="index"/> 的值,选择最合适的 IL 指令。</remarks> /// <exception cref="ArgumentNullException"><paramref name="il"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="paramType"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="targetType"/> 为 <c>null</c>。</exception> public static void EmitLoadArg(this ILGenerator il, int index, Type paramType, Type targetType) { CommonExceptions.CheckArgumentNull(il, "il"); if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } CommonExceptions.CheckArgumentNull(paramType, "paramType"); CommonExceptions.CheckArgumentNull(targetType, "targetType"); Contract.EndContractBlock(); EmitLoadArg(il, index, paramType, targetType, true); }
/// <summary> /// 移除 <see cref="ListBase{T}"/> 的指定索引处的元素。 /// </summary> /// <param name="index">要移除的元素的从零开始的索引。</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <see cref="ListBase{T}"/> 中的有效索引。</exception> public void RemoveAt(int index) { if (index < 0) { throw CommonExceptions.ArgumentNegative(nameof(index), index); } if (index > Count) { throw CommonExceptions.ArgumentOutOfRange(nameof(index), index); } Contract.EndContractBlock(); RemoveItem(index); }
/// <summary> /// 将指定长度的值添加到 <see cref="BitList"/> 的末尾。 /// </summary> /// <param name="length">要添加的值的长度。</param> /// <param name="value">要添加的值。</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> 小于 <c>0</c>。</exception> public void AddRange(int length, bool value) { if (length < 0) { throw CommonExceptions.ArgumentNegative(nameof(length), length); } Contract.EndContractBlock(); var cnt = _count; _count += length; EnsureCapacity(_count + UnitSize); FillInternal(cnt, length, value); }
/// <summary> /// 移除 <see cref="ListBase{T}"/> 的指定索引处的元素。 /// </summary> /// <param name="index">要移除的元素的从零开始的索引。</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <see cref="ListBase{T}"/> 中的有效索引。</exception> void IList.RemoveAt(int index) { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > this.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); this.RemoveItem(index); }
/// <summary> /// 将元素插入 <see cref="ListBase{T}"/> 的指定索引处。 /// </summary> /// <param name="index">从零开始的索引,应在该位置插入 <paramref name="item"/>。</param> /// <param name="item">要插入到 <see cref="ListBase{T}"/> 中的对象。</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <see cref="ListBase{T}"/> 中的有效索引。</exception> public void Insert(int index, T item) { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > this.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); this.InsertItem(index, item); }
/// <summary> /// 将指定长度的值添加到 <see cref="BitList"/> 的末尾。 /// </summary> /// <param name="length">要添加的值的长度。</param> /// <param name="value">要添加的值。</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> 小于 <c>0</c>。</exception> public void AddRange(int length, bool value) { if (length < 0) { throw CommonExceptions.ArgumentNegative("length", length); } Contract.EndContractBlock(); int cnt = this.count; this.count += length; EnsureCapacity(this.count + UnitSize); this.FillInternal(cnt, length, value); }
/// <summary> /// 回退 <paramref name="count"/> 个字符,只有之前的数据未被丢弃时才可以进行回退。 /// <c>Unget(1)</c> 与 <see cref="Unget()"/> 等价。 /// </summary> /// <param name="count">要回退的字符个数。</param> /// <returns>实际回退的字符个数,小于等于 <paramref name="count"/>。</returns> /// <exception cref="ObjectDisposedException">当前 <see cref="SourceReader"/> 已关闭。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> 小于 <c>0</c>。</exception> public int Unget(int count) { if (count < 0) { throw CommonExceptions.ArgumentNegative("count", count); } Contract.EndContractBlock(); CheckDisposed(); if (count == 0) { return(0); } if (count == 1) { return(this.Unget() ? 1 : 0); } int backCount = 0; while (true) { if (current == first) { int charCnt = index - firstIndex; if (count > charCnt) { backCount += charCnt; index = firstIndex; } else { backCount += count; index -= count; } break; } if (count > index) { backCount += index; count -= index; SwitchPrevBuffer(); } else { backCount += count; index -= count; break; } } globalIndex -= backCount; return(backCount); }
/// <summary> /// 将指定集合中的元素插入 <see cref="BitList"/> 的指定索引处。 /// </summary> /// <param name="index">应在此处插入新元素的从零开始的索引。</param> /// <param name="collection">一个集合,应将其元素插入到 /// <see cref="BitList"/> 中。</param> /// <exception cref="ArgumentNullException"><paramref name="collection"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 大于 <see cref="Count"/>。</exception> public void InsertRange(int index, IEnumerable <bool> collection) { CommonExceptions.CheckArgumentNull(collection, "collection"); if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > this.count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); IList <uint> uintList; int length = 0; BitList bList = collection as BitList; if (bList != null) { length = bList.count; uintList = bList.items; } else { uintList = new List <uint>(); uint value = 0U; int j = 0; foreach (bool b in collection) { if (b) { value |= 1U << j; } j++; length++; if (j == UnitSize) { uintList.Add(value); value = 0U; j = 0; } } if (j > 0) { uintList.Add(value); } } this.InsertRange(index, length, uintList); }
/// <summary> /// 将指定集合中的元素插入 <see cref="BitList"/> 的指定索引处。 /// </summary> /// <param name="index">应在此处插入新元素的从零开始的索引。</param> /// <param name="collection">一个集合,应将其元素插入到 /// <see cref="BitList"/> 中,其中每个整数表示 32 个连续位。</param> /// <exception cref="ArgumentNullException"><paramref name="collection"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 大于 <see cref="Count"/>。</exception> public void InsertRange(int index, IEnumerable <uint> collection) { CommonExceptions.CheckArgumentNull(collection, "collection"); if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > this.count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); IList <uint> uintList = collection as IList <uint> ?? new List <uint>(collection); this.InsertRange(index, uintList.Count << IndexShift, uintList); }
/// <summary> /// 获取指定索引处的元素。 /// </summary> /// <param name="index">要获取的元素从零开始的索引。</param> /// <value>指定索引处的元素。</value> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <see cref="ListQueue{T}"/> 中的有效索引。</exception> public T this[int index] { get { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index >= this.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); return(getElement(this, index)); } }
/// <summary> /// 获取指定索引处的元素。 /// </summary> /// <param name="index">要获取的元素从零开始的索引。</param> /// <value>指定索引处的元素。</value> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <see cref="ListStack{T}"/> 中的有效索引。</exception> public T this[int index] { get { if (index < 0) { throw CommonExceptions.ArgumentNegative(nameof(index), index); } if (index >= Count) { throw CommonExceptions.ArgumentOutOfRange(nameof(index), index); } Contract.EndContractBlock(); return(getArrayField(this)[index]); } }
/// <summary> /// 从特定的 <see cref="Array"/> 索引处开始,将指定集合 /// 的元素复制到一个 <see cref="Array"/> 中。 /// </summary> /// <param name="source">要复制元素的集合。</param> /// <param name="array">从 <paramref name="source"/> 复制的元素的目标位置的一维 /// <see cref="Array"/>。<paramref name="array"/> 必须具有从零开始的索引。</param> /// <param name="index"><paramref name="array"/> 中从零开始的索引,在此处开始复制。</param> /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentException"><paramref name="array"/> 是多维的。</exception> /// <exception cref="ArgumentException"><paramref name="source"/> /// 中的元素数目大于从 <paramref name="index"/> 到目标 <paramref name="array"/> /// 末尾之间的可用空间。</exception> /// <exception cref="ArgumentException"><paramref name="source"/> /// 的类型无法自动转换为目标 <paramref name="array"/> 的类型。</exception> public static void CopyTo <T>(ICollection <T> source, Array array, int index) { Contract.Requires(source != null); if (array == null) { throw CommonExceptions.ArgumentNull("array"); } if (array.Rank != 1) { throw CommonExceptions.ArrayRankMultiDimNotSupported(); } if (array.GetLowerBound(0) != 0) { throw CommonExceptions.ArrayNonZeroLowerBound("array"); } if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (array.Length - index < source.Count) { throw CommonExceptions.ArrayTooSmall("array"); } Contract.EndContractBlock(); T[] arr = array as T[]; if (arr != null) { foreach (T obj in source) { arr[index++] = obj; } } else { try { foreach (T obj in source) { array.SetValue(obj, index++); } } catch (InvalidCastException ex) { throw CommonExceptions.InvalidArrayType(ex); } } }
/// <summary> /// 填充指定范围中的元素。 /// </summary> /// <param name="index">要填充的范围的从零开始的起始索引。</param> /// <param name="length">要填充的范围内的元素数。</param> /// <param name="value">要填充的值。</param> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="index"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="length"/> 小于 <c>0</c>。</exception> /// <exception cref="System.ArgumentException"> /// <paramref name="index"/> 和 <paramref name="length"/> /// 不表示 <see cref="BitList"/> 中元素的有效范围。</exception> public void Fill(int index, int length, bool value) { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (length < 0) { throw CommonExceptions.ArgumentNegative("length", length); } if (index + length > this.count) { throw CommonExceptions.ArgumentOutOfRange("length", length); } Contract.EndContractBlock(); this.FillInternal(index, length, value); }
/// <summary> /// 使用给定的数组初始化 <see cref="ArrayAdapter{T}"/> 类的新实例。 /// 它分割指定数组中指定的元素范围。 /// </summary> /// <param name="array">初始化使用的数组。</param> /// <param name="offset">相应范围中第一个元素的从零开始的索引。</param> /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> 不是 /// <paramref name="array"/> 中的有效索引。</exception> public ArrayAdapter(T[] array, int offset) : base(array) { CommonExceptions.CheckArgumentNull(array, nameof(array)); if (offset < 0) { throw CommonExceptions.ArgumentNegative(nameof(offset), offset); } if (offset > array.Length) { throw CommonExceptions.ArgumentOutOfRange(nameof(offset), offset); } Contract.EndContractBlock(); this.array = array; this.offset = offset; count = array.Length - offset; }
/// <summary> /// 获取或设置指定索引处的元素。 /// </summary> /// <param name="index">要获取或设置的元素从零开始的索引。</param> /// <value>指定索引处的元素。</value> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <see cref="ReadOnlyList{T}"/> 中的有效索引。</exception> /// <exception cref="NotSupportedException">设置指定索引处的元素。</exception> T IList <T> .this[int index] { get { if (index < 0) { throw CommonExceptions.ArgumentNegative(nameof(index), index); } if (index > Count) { throw CommonExceptions.ArgumentOutOfRange(nameof(index), index); } Contract.EndContractBlock(); return(GetItemAt(index)); } set { throw CommonExceptions.MethodNotSupported(); } }
/// <summary> /// 加载指定索引的参数地址。 /// </summary> /// <param name="il">IL 指令生成器。</param> /// <param name="index">要加载的参数索引。</param> /// <remarks>会根据 <paramref name="index"/> 的值,选择最合适的 IL 指令。</remarks> /// <exception cref="ArgumentNullException"><paramref name="il"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 小于 <c>0</c>。</exception> public static void EmitLoadArgAddress(this ILGenerator il, int index) { CommonExceptions.CheckArgumentNull(il, "il"); if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } Contract.EndContractBlock(); if (index <= Byte.MaxValue) { il.Emit(OpCodes.Ldarga_S, (byte)index); } else { il.Emit(OpCodes.Ldarga, index); } }
/// <summary> /// 将指定集合的元素插入到当前列表的指定索引处。 /// </summary> /// <typeparam name="T">列表中元素的类型。</typeparam> /// <param name="list">要插入到的列表。</param> /// <param name="index">从零开始的索引,在此处开始插入新元素。</param> /// <param name="collection">要插入的元素集合。</param> /// <exception cref="ArgumentNullException"><paramref name="list"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="collection"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <paramref name="list"/> 中的有效索引。</exception> public static void InsertRange <T>(this IList <T> list, int index, IEnumerable <T> collection) { CommonExceptions.CheckArgumentNull(list, "list"); CommonExceptions.CheckArgumentNull(collection, "collection"); if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index >= list.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); foreach (T item in collection) { list.Insert(index++, item); } }
/// <summary> /// 使用索引、行和列初始化 <see cref="SourcePosition"/> 结构的新实例。 /// </summary> /// <param name="idx">从零开始的索引。</param> /// <param name="line">所在的行。</param> /// <param name="col">所在的列。</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="idx"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="line"/> 或 <paramref name="col"/> /// 小于 <c>1</c>。</exception> public SourcePosition(int idx, int line, int col) { if (idx < 0) { throw CommonExceptions.ArgumentNegative("idx", idx); } if (line < 1) { throw CommonExceptions.ArgumentOutOfRange("line", line); } if (col < 1) { throw CommonExceptions.ArgumentOutOfRange("col", col); } this.index = idx; this.line = line; this.col = col; }
/// <summary> /// 使用默认的比较器,在排序 <see cref="IList{T}"/> 的某个元素范围中搜索值。 /// </summary> /// <typeparam name="T">列表元素的类型。</typeparam> /// <param name="list">要搜索的从零开始的排序 <see cref="IList{T}"/>。</param> /// <param name="index">要搜索的范围的起始索引。</param> /// <param name="length">要搜索的范围的长度。</param> /// <param name="value">要搜索的对象。</param> /// <returns>如果找到 <paramref name="value"/>,则为指定 <paramref name="list"/> /// 中的指定 <paramref name="value"/> 的索引。如果找不到 <paramref name="value"/> /// 且 <paramref name="value"/> 小于 <paramref name="list"/> 中的一个或多个元素, /// 则为一个负数,该负数是大于 <paramref name="value"/> 的第一个元素的索引的按位求补。 /// 如果找不到 <paramref name="value"/> 且 <paramref name="value"/> 大于 <paramref name="list"/> /// 中的任何元素,则为一个负数,该负数是(最后一个元素的索引加 1)的按位求补。</returns> /// <exception cref="ArgumentNullException"><paramref name="list"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 或 /// <paramref name="length"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentException"><paramref name="index"/> 和 <paramref name="length"/> /// 不指定 <paramref name="list"/> 中的有效范围。</exception> /// <exception cref="InvalidOperationException"><typeparamref name="T"/> /// 类型没有实现 <see cref="IComparable{T}"/> 泛型接口。</exception>> public static int BinarySearch <T>(this IList <T> list, int index, int length, T value) { CommonExceptions.CheckArgumentNull(list, "list"); if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (length < 0) { throw CommonExceptions.ArgumentNegative("length", length); } if (index + length > list.Count) { throw CommonExceptions.InvalidOffsetLength(); } Contract.Ensures((Contract.Result <int>() >= 0 && Contract.Result <int>() <= list.Count) || (Contract.Result <int>() < 0 && ~Contract.Result <int>() <= list.Count + 1)); return(BinarySearchInternal(list, index, length, value, null)); }
/// <summary> /// 将对象插入 <see cref="ListBase{T}"/> 的指定索引处。 /// </summary> /// <param name="index">从零开始的索引,应在该位置插入 <paramref name="value"/>。</param> /// <param name="value">要插入到 <see cref="ListBase{T}"/> 中的对象。</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <see cref="ListBase{T}"/> 中的有效索引。</exception> /// <exception cref="ArgumentException"><paramref name="value"/> 不能赋值给 /// <typeparamref name="T"/> 类型。</exception> void IList.Insert(int index, object value) { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > this.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); try { this.InsertItem(index, (T)value); } catch (InvalidCastException) { throw CommonExceptions.ArgumentWrongType("value", value, typeof(T)); } }
/// <summary> /// 获取或设置指定索引处的元素。 /// </summary> /// <param name="index">要获得或设置的元素从零开始的索引。</param> /// <value>指定索引处的元素。</value> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <see cref="ReadOnlyList{T}"/> 中的有效索引。</exception> /// <exception cref="NotSupportedException">设置指定索引处的元素。</exception> object IList.this[int index] { get { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > this.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); return(this.GetItemAt(index)); } set { throw CommonExceptions.MethodNotSupported(); } }
/// <summary> /// 使用指定的 <see cref="IComparer{T}"/> 泛型接口,在排序 <see cref="IList{T}"/> 的某个元素范围中搜索键值。 /// </summary> /// <typeparam name="TElement">列表元素的类型。</typeparam> /// <typeparam name="TKey">要查找的键的类型。</typeparam> /// <param name="list">要搜索的从零开始的排序 <see cref="IList{T}"/>。</param> /// <param name="index">要搜索的范围的起始索引。</param> /// <param name="length">要搜索的范围的长度。</param> /// <param name="value">要搜索的键值。</param> /// <param name="keySelector">用于从元素中提取键的函数。</param> /// <param name="comparer">比较元素时要使用的 <see cref="IComparer{T}"/> 实现。</param> /// <returns>如果找到 <paramref name="value"/>,则为指定 <paramref name="list"/> /// 中的指定 <paramref name="value"/> 的索引。如果找不到 <paramref name="value"/> /// 且 <paramref name="value"/> 小于 <paramref name="list"/> 中的一个或多个元素, /// 则为一个负数,该负数是大于 <paramref name="value"/> 的第一个元素的索引的按位求补。 /// 如果找不到 <paramref name="value"/> 且 <paramref name="value"/> 大于 <paramref name="list"/> /// 中的任何元素,则为一个负数,该负数是(最后一个元素的索引加 1)的按位求补。</returns> /// <exception cref="ArgumentNullException"><paramref name="list"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="keySelector"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 或 /// <paramref name="length"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentException"><paramref name="index"/> 和 <paramref name="length"/> /// 不指定 <paramref name="list"/> 中的有效范围。</exception> /// <exception cref="InvalidOperationException"><paramref name="comparer"/> 为 <c>null</c>, /// 且 <typeparamref name="TKey"/> 类型没有实现 <see cref="IComparable{T}"/> 泛型接口。</exception> public static int BinarySearch <TElement, TKey>(this IList <TElement> list, int index, int length, TKey value, Func <TElement, TKey> keySelector, IComparer <TKey> comparer) { CommonExceptions.CheckArgumentNull(list, "list"); CommonExceptions.CheckArgumentNull(keySelector, "keySelector"); if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (length < 0) { throw CommonExceptions.ArgumentNegative("length", length); } if (index + length > list.Count) { throw CommonExceptions.InvalidOffsetLength(); } Contract.Ensures((Contract.Result <int>() >= 0 && Contract.Result <int>() <= list.Count) || (Contract.Result <int>() < 0 && ~Contract.Result <int>() <= list.Count + 1)); return(BinarySearchInternal(list, index, length, value, keySelector, comparer)); }
/// <summary> /// 使用给定的数组初始化 <see cref="ArrayAdapter{T}"/> 类的新实例。 /// 它分割指定数组中指定的元素范围。 /// </summary> /// <param name="array">初始化使用的数组。</param> /// <param name="offset">相应范围中第一个元素的从零开始的索引。</param> /// <param name="count">范围中的元素数。</param> /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> 或 /// <paramref name="count"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentException"><paramref name="offset"/> 和 <paramref name="count"/> /// 不指定 <paramref name="array"/> 中的有效范围。</exception> public ArrayAdapter(T[] array, int offset, int count) : base(array) { CommonExceptions.CheckArgumentNull(array, nameof(array)); if (offset < 0) { throw CommonExceptions.ArgumentNegative(nameof(offset), offset); } if (count < 0) { throw CommonExceptions.ArgumentNegative(nameof(count), count); } if (offset + count > array.Length) { throw CommonExceptions.InvalidOffsetLength(); } Contract.EndContractBlock(); this.array = array; this.offset = offset; this.count = count; }
/// <summary> /// 加载指定索引的参数,并转换为指定类型。 /// </summary> /// <param name="il">IL 指令生成器。</param> /// <param name="index">要加载的参数索引。</param> /// <param name="paramType">要加载的参数类型。</param> /// <param name="targetType">要转换到的类型。</param> /// <param name="isChecked">是否执行溢出检查。</param> /// <remarks>会根据 <paramref name="index"/> 的值,选择最合适的 IL 指令。</remarks> /// <exception cref="ArgumentNullException"><paramref name="il"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="paramType"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="targetType"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><paramref name="paramType"/> 包含泛型参数。</exception> /// <exception cref="ArgumentException"><paramref name="targetType"/> 包含泛型参数。</exception> public static void EmitLoadArg(this ILGenerator il, int index, Type paramType, Type targetType, bool isChecked) { CommonExceptions.CheckArgumentNull(il, "il"); if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } CommonExceptions.CheckArgumentNull(paramType, "paramType"); CommonExceptions.CheckArgumentNull(targetType, "targetType"); Contract.EndContractBlock(); if (paramType.ContainsGenericParameters) { throw CommonExceptions.TypeContainsGenericParameters(paramType); } if (targetType.ContainsGenericParameters) { throw CommonExceptions.TypeContainsGenericParameters(targetType); } if (paramType == targetType) { il.EmitLoadArg(index); return; } Converter converter = il.GetConversion(paramType, targetType, ConversionType.UserDefined); if (converter == null) { throw CommonExceptions.InvalidCast(paramType, targetType); } if (converter.PassByAddr) { il.EmitLoadArgAddress(index); } else { il.EmitLoadArg(index); } converter.Emit(isChecked); }
/// <summary> /// 读取输入字符序列中的字符块,并将字符位置提升 <paramref name="count"/>。 /// </summary> /// <param name="buffer">读取字符的缓冲区。</param> /// <param name="index">缓存区中的起始索引。</param> /// <param name="count">要读取的字符数。</param> /// <returns>读入缓冲区的总字符数。如果当前没有足够多的可用字符,则总字符数可能会少于所请求的字符数; /// 若已到达字符序列的结尾,则总字符数为零。</returns> /// <exception cref="ArgumentNullException"><paramref name="buffer"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 或 /// <paramref name="count"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentException"><paramref name="index"/> 和 <paramref name="count"/> /// 不指定 <paramref name="buffer"/> 中的有效范围。</exception> public override int Read(char[] buffer, int index, int count) { if (buffer == null) { throw CommonExceptions.ArgumentNull("buffer"); } if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (count < 0) { throw CommonExceptions.ArgumentNegative("count", count); } if (index + count > buffer.Length) { throw CommonExceptions.InvalidOffsetLength(); } Contract.EndContractBlock(); CheckDisposed(); if (count == 0 || this.nextChar == -1) { return(count); } int end = index + count; for (int i = index; i < end; i++) { buffer[i] = this.chars.Current; if (!this.chars.MoveNext()) { this.nextChar = -1; return(i - index); } } this.nextChar = this.chars.Current; return(count); }
/// <summary> /// 使用内部正则表达式和重复次数初始化 <see cref="RepeatExp"/> 类的新实例。 /// </summary> /// <param name="innerExp">包含的内部正则表达式。</param> /// <param name="minTimes">内部正则表达式的最少重复次数。</param> /// <param name="maxTimes">内部正则表达式的最多重复次数。</param> internal RepeatExp(Regex innerExp, int minTimes, int maxTimes) { if (minTimes < 0) { CommonExceptions.ArgumentNegative("minTimes", minTimes); } if (maxTimes < 0) { CommonExceptions.ArgumentNegative("maxTimes", maxTimes); } if (minTimes > maxTimes) { CommonExceptions.ReversedArgument("minTimes", "maxTimes"); } if (innerExp == null) { throw CommonExceptions.ArgumentNull("innerExp"); } CheckRegex(innerExp); this.innerExp = innerExp; this.minTimes = minTimes; this.maxTimes = maxTimes; }
/// <summary> /// 从特定的 <see cref="Array"/> 索引处开始,将指定集合 /// 的元素复制到一个 <see cref="Array"/> 中。 /// </summary> /// <param name="source">要复制元素的集合。</param> /// <param name="array">从 <paramref name="source"/> 复制的元素的目标位置的一维 /// <see cref="Array"/>。<paramref name="array"/> 必须具有从零开始的索引。</param> /// <param name="index"><paramref name="array"/> 中从零开始的索引,在此处开始复制。</param> /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 小于 <c>0</c>。</exception> /// <exception cref="ArgumentException"><paramref name="array"/> 是多维的。</exception> /// <exception cref="ArgumentException"><paramref name="source"/> /// 中的元素数目大于从 <paramref name="index"/> 到目标 <paramref name="array"/> /// 末尾之间的可用空间。</exception> /// <exception cref="ArgumentException"><paramref name="source"/> /// 的类型无法自动转换为目标 <paramref name="array"/> 的类型。</exception> public static void CopyTo <T>(ICollection <T> source, Array array, int index) { Contract.Requires(source != null); CommonExceptions.CheckSimplyArray(array, nameof(array)); if (index < 0) { throw CommonExceptions.ArgumentNegative(nameof(index), index); } if (array.Length - index < source.Count) { throw CommonExceptions.ArrayTooSmall(nameof(array)); } Contract.EndContractBlock(); var arr = array as T[]; if (arr != null) { foreach (var obj in source) { arr[index++] = obj; } } else { try { foreach (var obj in source) { array.SetValue(obj, index++); } } catch (InvalidCastException ex) { throw CommonExceptions.InvalidElementType(ex); } } }
/// <summary> /// 获取或设置指定索引处的元素。 /// </summary> /// <param name="index">要获得或设置的元素从零开始的索引。</param> /// <value>指定索引处的元素。</value> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 不是 /// <see cref="ListBase{T}"/> 中的有效索引。</exception> /// <exception cref="ArgumentException">设置属性,且 <paramref name="value"/> 不能赋值给 /// <typeparamref name="T"/> 类型。</exception> object IList.this[int index] { get { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > this.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); return(this.GetItemAt(index)); } set { if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index > this.Count) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.EndContractBlock(); try { this.SetItemAt(index, (T)value); } catch (InvalidCastException) { throw CommonExceptions.ArgumentWrongType("value", value, typeof(T)); } } }