1- namespace DotNetExtensions . Collections ;
1+ namespace DotNetExtensions . Collections ;
22
33/// <summary>
44/// Provides extension methods for working with enumerable sequences.
55/// </summary>
6- public static partial class EnumerableExtensions
7- {
8- /// <summary>
9- /// Returns a sequence of consecutive pairs of elements from the source sequence.
10- /// </summary>
11- /// <typeparam name="T">The type of elements in the source sequence.</typeparam>
12- /// <param name="source">The source sequence.</param>
13- /// <returns>
14- /// A sequence of tuples containing consecutive pairs of elements from the source.
15- /// </returns>
16- /// <exception cref="ArgumentNullException">
17- /// Thrown if the <paramref name="source"/> is <c>null</c>.
18- /// </exception>
19- public static IEnumerable < ( T First , T Second ) > Pairwise < T > ( this IEnumerable < T > source )
20- {
21- ArgumentNullException . ThrowIfNull ( source ) ;
22-
23- return PairwiseInternal ( source ) ;
24- }
25-
26- private static IEnumerable < ( T First , T Second ) > PairwiseInternal < T > ( IEnumerable < T > source )
27- {
28- using var enumerator = source . GetEnumerator ( ) ;
29-
30- // Buffer the first element
31- if ( ! enumerator . MoveNext ( ) ) yield break ;
32- var previous = enumerator . Current ;
33-
34- while ( enumerator . MoveNext ( ) )
35- {
36- var current = enumerator . Current ;
37- yield return ( previous , current ) ;
38- previous = current ;
39- }
40- }
41-
42- /// <summary>
43- /// Returns all overlapping sub-arrays of the specified length from the source sequence.
44- /// </summary>
45- /// <typeparam name="T">The type of elements in the source sequence.</typeparam>
46- /// <param name="source">The source sequence.</param>
47- /// <param name="length">The length of each subarray.</param>
48- /// <returns>
49- /// A sequence of sub-arrays of the specified length, each represented as an array of type <typeparamref name="T"/>.
50- /// </returns>
51- /// <exception cref="ArgumentNullException">
52- /// Thrown if the <paramref name="source"/> is <c>null</c>.
53- /// </exception>
54- /// <exception cref="ArgumentOutOfRangeException">
55- /// Thrown if the <paramref name="length"/> is negative.
56- /// </exception>
57- public static IEnumerable < T [ ] > Adjacent < T > ( this IEnumerable < T > source , int length )
58- {
59- ArgumentNullException . ThrowIfNull ( source ) ;
60- ArgumentOutOfRangeException . ThrowIfNegative ( length ) ;
61-
62- return AdjacentInternal ( source , length ) ;
63- }
64-
65- private static IEnumerable < T [ ] > AdjacentInternal < T > ( IEnumerable < T > source , int length )
66- {
67- var array = source as T [ ] ?? source . ToArray ( ) ;
68- if ( length > array . Length )
69- yield break ;
70-
71- for ( var i = 0 ; i <= array . Length - length ; i ++ )
72- {
73- var segment = new T [ length ] ;
74- Array . Copy ( array , i , segment , 0 , length ) ;
75- yield return segment ;
76- }
77- }
78-
79- /// <summary>
80- /// Returns a new sequence that skips the element at the specified index.
81- /// </summary>
82- /// <typeparam name="T">The type of elements in the source sequence.</typeparam>
83- /// <param name="source">The source sequence.</param>
84- /// <param name="index">The index of the element to skip.</param>
85- /// <returns>
86- /// A new sequence without the element at the specified index.
87- /// </returns>
88- /// <exception cref="ArgumentNullException">
89- /// Thrown if the <paramref name="source"/> is <c>null</c>.
90- /// </exception>
91- /// <exception cref="ArgumentOutOfRangeException">
92- /// Thrown if the <paramref name="index"/> is negative.
93- /// </exception>
94- public static IEnumerable < T > SkipAt < T > ( this IEnumerable < T > source , int index )
95- {
96- ArgumentNullException . ThrowIfNull ( source ) ;
97- ArgumentOutOfRangeException . ThrowIfNegative ( index ) ;
98-
99- return SkipAtInternal ( source , index ) ;
100- }
101-
102- private static IEnumerable < T > SkipAtInternal < T > ( IEnumerable < T > source , int index )
103- {
104- using var enumerator = source . GetEnumerator ( ) ;
105- var i = 0 ;
106-
107- while ( enumerator . MoveNext ( ) )
108- {
109- if ( i ++ == index ) continue ;
110- yield return enumerator . Current ;
111- }
112- }
113- }
6+ public static partial class EnumerableExtensions ;
0 commit comments