Table of Contents

Class PartitionerEnumerable<T>

Namespace
Cuemon.Collections.Generic
Assembly
Cuemon.Core.dll

Exposes the enumerator, which supports iteration in partitions over a collection of a specified type. Implements the IEnumerable<T>

public class PartitionerEnumerable<T> : IEnumerable<T>, IEnumerable

Type Parameters

T

The type of objects to enumerate.

Inheritance
PartitionerEnumerable<T>
Implements
Derived

Examples

The following example demonstrates how to use to iterate over a sequence in fixed-size partitions. It wraps a sequence of 1000 integers with a partition size of 100, then processes partitions sequentially using ToList while tracking the remaining partitions via HasPartitions. A smaller string partitioner shows the same behavior with an uneven final batch. The example outputs partition counts and content to the console, verifying that batching works correctly.

using System;
using System.Linq;
using Cuemon.Collections.Generic;

namespace MyApp.Examples;

public class PartitionerEnumerableExample
{
    public void Demonstrate()
    {
        // Create a sequence of 1000 numbers
        var numbers = Enumerable.Range(1, 1000);

        // Wrap in a partitioner with partition size of 100
        var partitioner = new PartitionerEnumerable<int>(numbers, partitionSize: 100);

        Console.WriteLine(partitioner.PartitionSize); // 100
        Console.WriteLine(partitioner.HasPartitions); // True
        Console.WriteLine(partitioner.IteratedCount); // 0

        // Process the first partition (items 1-100)
        var firstBatch = partitioner.ToList();
        Console.WriteLine(firstBatch.Count);  // 100
        Console.WriteLine(firstBatch[0]);     // 1
        Console.WriteLine(firstBatch[99]);    // 100

        Console.WriteLine(partitioner.IteratedCount); // 1
        Console.WriteLine(partitioner.HasPartitions); // True

        // Process the remaining partitions
        while (partitioner.HasPartitions)
        {
            var batch = partitioner.ToList();
            Console.WriteLine($"Batch {partitioner.IteratedCount}: {batch.Count} items");
        // After exhausting the sequence:
        Console.WriteLine(partitioner.HasPartitions); // False
        Console.WriteLine(partitioner.IteratedCount); // 10 (1000/100)

        // A partitioner can be iterated multiple times (each iteration
        // advances through the source sequence)
        var words = new PartitionerEnumerable<string>(
            new[] { "a", "b", "c", "d", "e", "f", "g", "h" },
            partitionSize: 3);

        while (words.HasPartitions)
        {
            var chunk = words.ToList();
            Console.WriteLine(string.Join(",", chunk));
        // Output:
        //   a,b,c
        //   d,e,f
        //   g,h

}}}
}

Constructors

PartitionerEnumerable(IEnumerable<T>, int)

Initializes a new instance of the PartitionerEnumerable<T> class.

public PartitionerEnumerable(IEnumerable<T> source, int partitionSize = 128)

Parameters

source IEnumerable<T>

The sequence to iterate in partitions.

partitionSize int

The size of the partitions.

Exceptions

ArgumentOutOfRangeException

partitionSize is lower than 0.

Properties

HasPartitions

Gets a value indicating whether this instance has partitions remaining to be iterated.

public bool HasPartitions { get; }

Property Value

bool

true if this instance has partitions remaining to be iterated; otherwise, false.

IteratedCount

Gets the number of times the this instance was iterated.

public int IteratedCount { get; }

Property Value

int

The number of times the this instance was iterated.

Origin

Gets the sequence that this instance was constructed with.

protected IEnumerable<T> Origin { get; }

Property Value

IEnumerable<T>

The sequence that this instance was constructed with.

PartitionSize

Gets the number of elements per partition.

public int PartitionSize { get; }

Property Value

int

The number of elements per partition.

Methods

GetEnumerator()

Returns an enumerator that iterates through the partition of the collection.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

An enumerator that can be used to iterate through the partition of the collection.

See Also