/// <Summary>
/// Chunkhelper helps divide the sequence into several blocks.
///
/// If totalitemcount is 11 and chunksize is 5, then:
/// Chunk [0] 0, 1, 2, 3, 4
/// Chunk [1] 5, 6, 7, 8, 9
/// Chunk [2] 10
/// </Summary>
Public class chunkhelper
{
/// <Summary>
///
/// </Summary>
/// <Param name = "totalitemcount"> total projects </param>
/// <Param name = "chunksize"> block size </param>
Public chunkhelper (INT totalitemcount, int chunksize)
{
If (totalitemcount <0)
{
Throw new argumentoutofrangeexception ("itemcount", totalitemcount, "itemcount cannot be less than 0 ");
}
If (chunksize <1)
{
Throw new argumentoutofrangeexception ("chunksize", chunksize, "chunksize cannot be less than 1 ");
}
This. totalitemcount = totalitemcount;
This. chunksize = chunksize;
Int I = This. totalitemcount % This. chunksize = 0? 0: 1;
This. chunkcount = This. totalitemcount/This. chunksize + I;
}
/// <Summary>
/// Obtain the total number of projects.
/// </Summary>
Public int totalitemcount {Get; private set ;}
/// <Summary>
/// Obtain the block size.
/// </Summary>
Public int chunksize {Get; private set ;}
/// <Summary>
/// Specify the index of the project to obtain the index of the project.
/// </Summary>
/// <Param name = "itemindex"> </param>
/// <Returns> </returns>
Public int getchunkindex (INT itemindex)
{
If (itemindex <0 | itemindex> = totalitemcount)
{
Throw new argumentoutofrangeexception ();
}
Return itemindex/chunksize;
}
/// <Summary>
/// Obtain the number of parts. If totalitemcount is 0, the return value is 0 instead of 1.
/// </Summary>
Public int chunkcount {Get; private set ;}
/// <Summary>
/// Obtain the specified part information.
/// </Summary>
/// <Param name = "chunkindex"> </param>
/// <Returns> </returns>
Public chunk getchunk (INT chunkindex)
{
If (chunkindex <0 | chunkindex> = chunkcount)
{
Throw new argumentoutofrangeexception ();
}
Int startitemindex = chunkindex * chunksize;
Int itemcountinchunk = chunksize;
// If it is greater than the total number of projects, it is the last one.
If (startitemindex + itemcountinchunk> totalitemcount)
{
Itemcountinchunk = totalitemcount % chunksize;
}
Return new chunk (chunkindex, startitemindex, itemcountinchunk );
}
}
/// <Summary>
/// Chunk indicates a chunk.
/// </Summary>
Public struct chunk
{
/// <Summary>
/// Index of the block.
/// </Summary>
Public int chunkindex {Get; private set ;}
/// <Summary>
/// Index of the first item in the entire sequence.
/// </Summary>
Public int firstitemindex {Get; private set ;}
/// <Summary>
/// Number of items in the block.
/// </Summary>
Public int itemcount {Get; private set ;}
/// <Summary>
/// Instantiate chunk
/// </Summary>
/// <Param name = "chunkindex"> </param>
/// <Param name = "firstitemindex"> </param>
/// <Param name = "itemcountinchunk"> </param>
Internal chunk (INT chunkindex, int firstitemindex, int itemcountinchunk)
: This ()
{
This. chunkindex = chunkindex;
This. firstitemindex = firstitemindex;
This. itemcount = itemcountinchunk;
}
}