Exemplo n.º 1
0
        /// <summary>
        /// This method will spread out the work items--like dealing cards around the table, you only get every other K'th card,
        /// where K is the number of people, when batchCount=1. (Or else you get sets of batchCount-contiguous cards).
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumerable"></param>
        /// <param name="pieceIndexRangeCollection"></param>
        /// <param name="pieceCount"></param>
        /// <param name="batchCount"></param>
        /// <param name="skipListOrNull"></param>
        /// <returns>The item and it's original index in the enumerable.</returns>
        public static IEnumerable <KeyValuePair <T, long> > DivideWork <T>(IEnumerable <T> enumerable, RangeCollection pieceIndexRangeCollection,
                                                                           long pieceCount, long batchCount, RangeCollection skipListOrNull)
        {
            if (pieceIndexRangeCollection == null)
            {
                throw new ArgumentNullException("pieceIndexRangeCollection");
            }

            Helper.CheckCondition(batchCount > 0, string.Format(CultureInfo.CurrentCulture, Properties.Resource.BatchCountCondition));
            long pieceIndex = 0;
            long batchIndex = 0;
            bool inRange    = pieceIndexRangeCollection.Contains(pieceIndex);

            foreach (var tAndRowIndex in UseSkipList(enumerable, skipListOrNull))
            {
                if (inRange)
                {
                    yield return(tAndRowIndex);
                }

                ++batchIndex;
                if (batchIndex == batchCount)
                {
                    batchIndex = 0;
                    pieceIndex = (pieceIndex + 1) % pieceCount;
                    inRange    = pieceIndexRangeCollection.Contains(pieceIndex);
                }
            }
        }
Exemplo n.º 2
0
        private static IEnumerable <KeyValuePair <T, long> > UseSkipList <T>(IEnumerable <T> enumerable, RangeCollection skipListOrNull)
        {
            long rowIndex = -1;

            foreach (T t in enumerable)
            {
                ++rowIndex;
                if (null != skipListOrNull && skipListOrNull.Contains(rowIndex))
                {
                    continue;
                }
                yield return(new KeyValuePair <T, long>(t, rowIndex));
            }
        }