浙江省城乡住房建设部网站,网站你们都知道,危险网站提示,辽宁电力建设监理有限公司网站概要
本文在前两篇Take源码分析的基础上#xff0c;着重分析Range参数中有倒数的情况#xff0c;即分析TakeRangeFromEndIterator的源码实现。
源码及分析
TakeRangeFromEndIterator方法用于处理Range中的开始和结束索引存在倒数的情况。该方法位于Take.cs文件中。通过yie…概要
本文在前两篇Take源码分析的基础上着重分析Range参数中有倒数的情况即分析TakeRangeFromEndIterator的源码实现。
源码及分析
TakeRangeFromEndIterator方法用于处理Range中的开始和结束索引存在倒数的情况。该方法位于Take.cs文件中。通过yield return/break的方式管理迭代过程。
TakeRangeFromEndIterator方法从整体上分为两部分一部分是TryGetNonEnumeratedCount为真的情况即souce实现了ICollection接口的情况另一部分是souce没有实现ICollection接口TryGetNonEnumeratedCount返回为False的情况。 private static IEnumerableTSource TakeRangeFromEndIteratorTSource(IEnumerableTSource source, bool isStartIndexFromEnd, int startIndex, bool isEndIndexFromEnd, int endIndex)
{if (source.TryGetNonEnumeratedCount(out int count)){startIndex CalculateStartIndex(isStartIndexFromEnd, startIndex, count);endIndex CalculateEndIndex(isEndIndexFromEnd, endIndex, count);if (startIndex endIndex){foreach (TSource element in TakeRangeIterator(source, startIndex, endIndex)){yield return element;}}yield break;}static int CalculateStartIndex(bool isStartIndexFromEnd, int startIndex, int count) Math.Max(0, isStartIndexFromEnd ? count - startIndex : startIndex);static int CalculateEndIndex(bool isEndIndexFromEnd, int endIndex, int count) Math.Min(count, isEndIndexFromEnd ? count - endIndex : endIndex);
}
该函数有4个参数开始索引是否倒数开始索引值结束索引是否为倒数结束索引值如果source实现了ICollection接口可以在不遍历souce序列的情况下直接获取序列长度则TryGetNonEnumeratedCount返回为True调用CalculateStartIndex和CalculateEndIndex内联方法获取正数的索引值假设Range是 ^3… ^1元素共10个则转换完成后是7…9即从第7个取到第9个在开始索引值小于结束索引值的前提下调TakeRangeIterator方法按照普通正数Range的方法进行处理并将结果以状态机的形式按照yield return/break方式返回。具体详见 C# Linq源码分析之Take 二
注意在TryGetNonEnumeratedCount返回为True的情况下因为可以直接取到序列的元素个数不需要进行逐个迭代和累加。因此才可以将倒数的Range转换成正数的Range。对于无法获取序列元素的情况我们看下面的代码分析。
在开始索引是倒数的情况下进行如下处理此时假设我们有如下序列我们的Range是 ^3 … ^1但是不知道序列内元素个数。 QueueTSource queue;if (isStartIndexFromEnd){// TakeLast compat: enumerator should be disposed before yielding the first element.using (IEnumeratorTSource e source.GetEnumerator()){if (!e.MoveNext()){yield break;}queue new QueueTSource();queue.Enqueue(e.Current);count 1;while (e.MoveNext()){if (count startIndex){queue.Enqueue(e.Current);count;}else{do{queue.Dequeue();queue.Enqueue(e.Current);checked { count; }} while (e.MoveNext());break;}}Debug.Assert(queue.Count Math.Min(count, startIndex));}startIndex CalculateStartIndex(isStartIndexFromEnd: true, startIndex, count);endIndex CalculateEndIndex(isEndIndexFromEnd, endIndex, count);Debug.Assert(endIndex - startIndex queue.Count);for (int rangeIndex startIndex; rangeIndex endIndex; rangeIndex){yield return queue.Dequeue();}}获取souce的迭代器e如果取一个元素失败证明Range中指定的范围在实际序列中根本取不到则通过yield break关闭状态机定义缓冲队列queue将“A”放入队列元素个数初始值设置为1迭代开始count startIndex在元素个数累加器小于启始索引的情况下每次队列增加一个元素直到count等于3此时队列元素是A, “B”, “C”;然后每次删除队首元素再添加新的元素到队尾当遍历完整个source序列后队列元素是“G” “H” “I”此种方法遍历算法只需要启始索引值结束索引值完全忽略根据迭代中获取的元素个数count计算出正数的开始和结束索引本例应该是6…8;将缓冲队列queue按照状态机的形式通过yield return方式返回因为rangeIndex endIndex;所以最后的返回值是“G” “H”没有“I”只会取到结束索引对应元素的前一个元素。
在开始索引不是倒数的情况下 进行如下处理此时假设我们有如下序列我们的Range是 3 … ^2此时我们并不清楚集合内元素的个数。
请注意在现有的情况下如果开始索引是正数结尾索引一定是倒数的。如果结尾索引是正数更加之前的代码分析只会进入C# Linq源码分析之Take 二所介绍的TakeRangeIterator方法。
假设我们使用的数据如下 else{Debug.Assert(!isStartIndexFromEnd isEndIndexFromEnd);// SkipLast compat: the enumerator should be disposed at the end of the enumeration.using IEnumeratorTSource e source.GetEnumerator();count 0;while (count startIndex e.MoveNext()){count;}if (count startIndex){queue new QueueTSource();while (e.MoveNext()){if (queue.Count endIndex){do{queue.Enqueue(e.Current);yield return queue.Dequeue();} while (e.MoveNext());break;}else{queue.Enqueue(e.Current);}}}}定义迭代器e 和 集合内初始元素个数计数器设置为0在初始化操作后count是3迭代器指向元素C如果count等于 startIndex证明Range中的取值在下面的序列中可以取到。如果不等则返回空 因为是倒数第2个所以endIndex的数值是2D和E进入缓冲队列因为缓冲队列中的长度必须和endIndex相等队列每进入一个元素到队尾然后删除队首元素并按照yield return的状态机方式返回。 最后将所有需要的元素返回。上面算法并不需要直到序列内元素的个数。