Click here to Skip to main content
15,896,410 members
Articles / General Programming / Threads

Task Parallel Library: 4 of n

Rate me:
Please Sign up or sign in to vote.
4.93/5 (59 votes)
10 May 2011CPOL15 min read 122.4K   1.7K   126  
A look into using the Task Parallel Library
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace CustomPartitioning
{
    /// <summary>
    /// Good source for TPL partitioning can be found at 
    /// http://msdn.microsoft.com/en-us/library/dd997411.aspx
    /// </summary>
    public class SimpleCustomPartitioner<T> : Partitioner<T> 
    {
        private T[] sourceData;

        public SimpleCustomPartitioner(T[] sourceData)
        {
            this.sourceData = sourceData;
        }

        public override bool SupportsDynamicPartitions 
        {
            get 
            {
                return false;
            }
        }

        public override IList<IEnumerator<T>> GetPartitions(int partitionCount)
        {
            IList<IEnumerator<T>> partitioned = new List<IEnumerator<T>>();
            //work out how many items will go into a single partition
            int itemsPerPartition = sourceData.Length / partitionCount;
            //now create the partititions, all but the last one, which we treat as special case
            for (int i = 0; i < partitionCount - 1; i++)
            {
                partitioned.Add(GetItemsForPartition(i * itemsPerPartition, (i + 1) * itemsPerPartition));
            }
            //now create the lasst partition
            partitioned.Add(GetItemsForPartition((partitionCount - 1) * itemsPerPartition, sourceData.Length));
            return partitioned;
        }


        private IEnumerator<T> GetItemsForPartition(int start, int end)
        {
            for (int i = start; i < end; i++)
                yield return sourceData[i];
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions