Auxiliary classes chunkhelper that divide sequences into several blocks

Source: Internet
Author: User
/// <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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.