public static IEnumerable Take <T, R>(this ITimeBudget budget,
                                              IEnumerable <T> ts,
                                              ICollection <R> results,
                                              int steps,
                                              Predicate <T> where,
                                              Func <T, R> selector
                                              )
        {
            var iter = ts.GetEnumerator();

            return(budget.Repeat(() =>
            {
                bool hasNext = true;
                for (int i = 0; i < steps && (hasNext = iter.MoveNext()); i++)
                {
                    if (where (iter.Current))
                    {
                        results.Add(selector(iter.Current));
                    }
                }

                if (!hasNext)
                {
                    iter.Dispose();
                }
                return hasNext;
            }
                                 ));
        }
 public IEnumerable Produce(IConsumer <T> consumer, ITimeBudget budget)
 {
     if (_steps.Count == 0)
     {
         return(Enumerable.Empty <int>());
     }
     return(_steps.Peek().Produce(consumer, budget));
 }
 public static IEnumerable Take <T>(this ITimeBudget budget,
                                    IEnumerable <T> ts,
                                    ICollection <T> results,
                                    int steps,
                                    Predicate <T> where
                                    )
 {
     return(budget.Take(ts, results, steps, where, d => d));
 }
Exemplo n.º 4
0
        public IEnumerable Produce(IConsumer <TData> consumer, ITimeBudget budget)
        {
            var buffer = new List <TData>();

            foreach (var s in ConsumeCache(consumer, buffer, budget))
            {
                yield return(s);
            }
            foreach (var s in ProduceNew(consumer, buffer, budget))
            {
                yield return(s);
            }
        }
        public static IEnumerable Repeat(this ITimeBudget budget, Func <bool> f)
        {
            while (true)
            {
                while (budget.MillisRemaining > 0)
                {
                    if (!f())
                    {
                        yield break;
                    }
                }

                yield return(null);
            }
        }
Exemplo n.º 6
0
        private IEnumerable ProduceNew(IConsumer <TData> consumer, List <TData> buffer, ITimeBudget budget)
        {
            int n        = Driver.BatchSize;
            var newSteps = budget.Take(Driver.EnumerateNew(), buffer, n,
                                       Driver.FilterNew, Driver.MakeData
                                       );

            foreach (var s in PostProcess(newSteps, consumer, buffer))
            {
                yield return(s);
            }
        }
Exemplo n.º 7
0
 private IEnumerable ConsumeCache(IConsumer <TData> consumer, List <TData> buffer, ITimeBudget budget)
 {
     if (_cachedResults == null)
     {
         _cachedResults = new List <TData>();
         if (_parent != null)
         {
             int n     = Driver.BatchSize;
             var steps = budget.Take(_parent._cachedResults, buffer, n, Driver.FilterCached);
             foreach (var s in PostProcess(steps, consumer, buffer))
             {
                 yield return(s);
             }
         }
     }
     else
     {
         consumer.Consume(_cachedResults);
     }
 }