有没有一个IEumable实现只迭代一次它的源代码(例如LINQ)? [英] Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?

查看:24
本文介绍了有没有一个IEumable实现只迭代一次它的源代码(例如LINQ)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供的items是LINQ表达式的结果:

var items = from item in ItemsSource.RetrieveItems()
            where ...

假设生成每一项都需要一些不可忽视的时间。

可以使用两种操作模式:

  1. 使用foreach将允许开始处理集合开头的项,而不是最终可用的项。但是,如果我们希望以后再次处理相同的集合,则必须复制并保存它:

    var storedItems = new List<Item>();
    foreach(var item in items)
    {
        Process(item);
        storedItems.Add(item);
    }
    
    // Later
    foreach(var item in storedItems)
    {
        ProcessMore(item);
    }
    

    因为如果我们刚刚创建foreach(... in items),则ItemsSource.RetrieveItems()将再次被调用。

  2. 我们可以直接使用.ToList(),但这将迫使我们等待检索最后一个项目,然后才能开始处理第一个项目。

问题:是否有一个IEnumerable实现将像常规LINQ查询结果一样第一次迭代,但将在进程中实现,以便第二次foreach将迭代存储的值?

推荐答案

一个有趣的挑战,所以我必须提供我自己的解决方案。有趣的是,我现在的解决方案是版本3。版本2是我根据Servy的反馈进行的简化。然后我意识到我的解决方案有很大的缺陷。如果缓存的可枚举数的第一个枚举没有完成,则不会执行任何缓存。许多像FirstTake这样的LINQ扩展只能枚举足够的可枚举数来完成工作,我不得不更新到版本3才能使用缓存。

问题是关于不涉及并发访问的可枚举数的后续枚举。尽管如此,我还是决定让我的解决方案线程安全。它增加了一些复杂性和一些开销,但应该允许在所有方案中使用该解决方案。

public static class EnumerableExtensions {

  public static IEnumerable<T> Cached<T>(this IEnumerable<T> source) {
    if (source == null)
      throw new ArgumentNullException("source");
    return new CachedEnumerable<T>(source);
  }

}

class CachedEnumerable<T> : IEnumerable<T> {

  readonly Object gate = new Object();

  readonly IEnumerable<T> source;

  readonly List<T> cache = new List<T>();

  IEnumerator<T> enumerator;

  bool isCacheComplete;

  public CachedEnumerable(IEnumerable<T> source) {
    this.source = source;
  }

  public IEnumerator<T> GetEnumerator() {
    lock (this.gate) {
      if (this.isCacheComplete)
        return this.cache.GetEnumerator();
      if (this.enumerator == null)
        this.enumerator = source.GetEnumerator();
    }
    return GetCacheBuildingEnumerator();
  }

  public IEnumerator<T> GetCacheBuildingEnumerator() {
    var index = 0;
    T item;
    while (TryGetItem(index, out item)) {
      yield return item;
      index += 1;
    }
  }

  bool TryGetItem(Int32 index, out T item) {
    lock (this.gate) {
      if (!IsItemInCache(index)) {
        // The iteration may have completed while waiting for the lock.
        if (this.isCacheComplete) {
          item = default(T);
          return false;
        }
        if (!this.enumerator.MoveNext()) {
          item = default(T);
          this.isCacheComplete = true;
          this.enumerator.Dispose();
          return false;
        }
        this.cache.Add(this.enumerator.Current);
      }
      item = this.cache[index];
      return true;
    }
  }

  bool IsItemInCache(Int32 index) {
    return index < this.cache.Count;
  }

  IEnumerator IEnumerable.GetEnumerator() {
    return GetEnumerator();
  }

}

扩展名的用法如下(sequenceIEnumerable<T>):

var cachedSequence = sequence.Cached();

// Pulling 2 items from the sequence.
foreach (var item in cachedSequence.Take(2))
  // ...

// Pulling 2 items from the cache and the rest from the source.
foreach (var item in cachedSequence)
  // ...

// Pulling all items from the cache.
foreach (var item in cachedSequence)
  // ...

如果只枚举了部分可枚举对象(例如cachedSequence.Take(2).ToList()),则会有轻微泄漏。将释放ToList使用的枚举数,但不释放基础源枚举数。这是因为前两个项被缓存,如果请求后续项,源枚举器将保持活动状态。在这种情况下,仅当可以进行垃圾收集时才清理源枚举数(这将与可能的大缓存同时进行)。

这篇关于有没有一个IEumable实现只迭代一次它的源代码(例如LINQ)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆