/// <summary> /// Splits the iteratons into parts(spans). /// </summary> /// <param name="totalCount"></param> /// <param name="itemsPerIteration"></param> /// <returns></returns> public static List <IndexSpan> Split(int totalCount, int itemsPerIteration) { //Requirement.IsTrue(totalCount != 0, "Invalid indexes span specified."); if (totalCount == 0) { return(new List <IndexSpan>()); } List <IndexSpan> iterationSpans = new List <IndexSpan>(); // Handle case where 5total = 5 per iteration. if (totalCount == itemsPerIteration) { iterationSpans.Add(new IndexSpan(0, itemsPerIteration)); return(iterationSpans); } // Handle case where 5total = 8 per iteration if (totalCount < itemsPerIteration) { iterationSpans.Add(new IndexSpan(0, totalCount)); return(iterationSpans); } // Now create the iterations. int nextStartingIndex = 0; int count = itemsPerIteration; int totalCountProcessed = 0; int leftOver = 0; while (totalCountProcessed < totalCount) { IndexSpan span = new IndexSpan(nextStartingIndex, count); iterationSpans.Add(span); // Used to break the loop. We've finished all the items. totalCountProcessed += count; // Now calculate the next starting index. nextStartingIndex = span.EndIndex + 1; // Now determine left over items leftOver = totalCount - totalCountProcessed; if (leftOver >= itemsPerIteration) { count = itemsPerIteration; } else { count = leftOver; } } return(iterationSpans); }
/// <summary> /// Splits the iteratons into parts(spans). /// </summary> /// <param name="totalCount"></param> /// <param name="itemsPerIteration"></param> /// <returns></returns> public static List<IndexSpan> Split(int totalCount, int itemsPerIteration) { //Requirement.IsTrue(totalCount != 0, "Invalid indexes span specified."); if (totalCount == 0) return new List<IndexSpan>(); List<IndexSpan> iterationSpans = new List<IndexSpan>(); // Handle case where 5total = 5 per iteration. if (totalCount == itemsPerIteration) { iterationSpans.Add(new IndexSpan(0, itemsPerIteration)); return iterationSpans; } // Handle case where 5total = 8 per iteration if (totalCount < itemsPerIteration) { iterationSpans.Add(new IndexSpan(0, totalCount)); return iterationSpans; } // Now create the iterations. int nextStartingIndex = 0; int count = itemsPerIteration; int totalCountProcessed = 0; int leftOver = 0; while(totalCountProcessed < totalCount) { IndexSpan span = new IndexSpan(nextStartingIndex, count); iterationSpans.Add(span); // Used to break the loop. We've finished all the items. totalCountProcessed += count; // Now calculate the next starting index. nextStartingIndex = span.EndIndex + 1; // Now determine left over items leftOver = totalCount - totalCountProcessed; if (leftOver >= itemsPerIteration) count = itemsPerIteration; else count = leftOver; } return iterationSpans; }