1
- // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
-
3
- using System ;
4
- using System . Collections . Generic ;
5
- using System . Linq ;
6
-
7
- namespace CommandLine . Infrastructure
8
- {
9
- static class EnumerableExtensions
10
- {
11
- public static int IndexOf < TSource > ( this IEnumerable < TSource > source , Func < TSource , bool > predicate )
12
- {
13
- var index = - 1 ;
14
- foreach ( var item in source )
15
- {
16
- index ++ ;
17
- if ( predicate ( item ) )
18
- {
19
- break ;
20
- }
21
- }
22
- return index ;
23
- }
24
-
25
- public static object ToUntypedArray ( this IEnumerable < object > value , Type type )
26
- {
27
- var array = Array . CreateInstance ( type , value . Count ( ) ) ;
28
- value . ToArray ( ) . CopyTo ( array , 0 ) ;
29
- return array ;
30
- }
31
-
32
- public static bool Empty < TSource > ( this IEnumerable < TSource > source )
33
- {
34
- return ! source . Any ( ) ;
35
- }
36
-
37
- /// <summary>
38
- /// Breaks a collection into groups of a specified size.
39
- /// </summary>
40
- /// <param name="source">A collection of <typeparam name="T"/>.</param>
41
- /// <param name="groupSize">The number of items each group shall contain.</param>
42
- /// <returns>An enumeration of T[].</returns>
43
- /// <remarks>An incomplete group at the end of the source collection will be silently dropped.</remarks>
44
- public static IEnumerable < T [ ] > Group < T > ( this IEnumerable < T > source , int groupSize )
45
- {
46
- if ( groupSize < 1 )
47
- {
48
- throw new ArgumentOutOfRangeException ( nameof ( groupSize ) ) ;
49
- }
50
-
51
- T [ ] group = new T [ groupSize ] ;
52
- int groupIndex = 0 ;
53
-
54
- foreach ( var item in source )
55
- {
56
- group [ groupIndex ++ ] = item ;
57
-
58
- if ( groupIndex == groupSize )
59
- {
60
- yield return group ;
61
-
62
- group = new T [ groupSize ] ;
63
- groupIndex = 0 ;
64
- }
65
- }
66
- }
67
- }
1
+ // Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
+
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+ using System . Linq ;
6
+
7
+ namespace CommandLine . Infrastructure
8
+ {
9
+ static class EnumerableExtensions
10
+ {
11
+ public static int IndexOf < TSource > ( this IEnumerable < TSource > source , Func < TSource , bool > predicate )
12
+ {
13
+ var index = - 1 ;
14
+ foreach ( var item in source )
15
+ {
16
+ index ++ ;
17
+ if ( predicate ( item ) )
18
+ {
19
+ break ;
20
+ }
21
+ }
22
+ return index ;
23
+ }
24
+
25
+ public static object ToUntypedArray ( this IEnumerable < object > value , Type type )
26
+ {
27
+ var array = Array . CreateInstance ( type , value . Count ( ) ) ;
28
+ value . ToArray ( ) . CopyTo ( array , 0 ) ;
29
+ return array ;
30
+ }
31
+
32
+ public static bool Empty < TSource > ( this IEnumerable < TSource > source )
33
+ {
34
+ return ! source . Any ( ) ;
35
+ }
36
+
37
+ /// <summary>
38
+ /// Breaks a collection into groups of a specified size.
39
+ /// </summary>
40
+ /// <param name="source">A collection of <typeparam name="T"/>.</param>
41
+ /// <param name="groupSize">The number of items each group shall contain.</param>
42
+ /// <returns>An enumeration of T[].</returns>
43
+ /// <remarks>An incomplete group at the end of the source collection will be silently dropped.</remarks>
44
+ public static IEnumerable < T [ ] > Group < T > ( this IEnumerable < T > source , int groupSize )
45
+ {
46
+ if ( groupSize < 1 )
47
+ {
48
+ throw new ArgumentOutOfRangeException ( nameof ( groupSize ) ) ;
49
+ }
50
+
51
+ T [ ] group = new T [ groupSize ] ;
52
+ int groupIndex = 0 ;
53
+
54
+ foreach ( var item in source )
55
+ {
56
+ group [ groupIndex ++ ] = item ;
57
+
58
+ if ( groupIndex == groupSize )
59
+ {
60
+ yield return group ;
61
+
62
+ group = new T [ groupSize ] ;
63
+ groupIndex = 0 ;
64
+ }
65
+ }
66
+ }
67
+ }
68
68
}
0 commit comments