3
3
using System . Diagnostics ;
4
4
using System . Linq ;
5
5
6
- namespace KY . Core
6
+ namespace KY . Core ;
7
+
8
+ public static class ListExtension
7
9
{
8
- public static class ListExtension
10
+ [ DebuggerHidden ]
11
+ public static IList < T > AddRange < T > ( this IList < T > list , IEnumerable < T > items )
9
12
{
10
- [ DebuggerHidden ]
11
- public static IList < T > AddRange < T > ( this IList < T > list , IEnumerable < T > items )
13
+ foreach ( T item in items )
12
14
{
13
- foreach ( T item in items )
14
- {
15
- list . Add ( item ) ;
16
- }
17
- return list ;
15
+ list . Add ( item ) ;
18
16
}
17
+ return list ;
18
+ }
19
19
20
- [ DebuggerHidden ]
21
- public static List < T > YieldList < T > ( this T item )
22
- {
23
- return new List < T > ( item . Yield ( ) ) ;
24
- }
20
+ [ DebuggerHidden ]
21
+ public static List < T > YieldList < T > ( this T item )
22
+ {
23
+ return new List < T > ( item . Yield ( ) ) ;
24
+ }
25
25
26
- [ DebuggerHidden ]
27
- public static IList < T > InsertRangeReverse < T > ( this IList < T > list , int index , IEnumerable < T > items )
26
+ [ DebuggerHidden ]
27
+ public static IList < T > InsertRangeReverse < T > ( this IList < T > list , int index , IEnumerable < T > items )
28
+ {
29
+ foreach ( T item in items )
28
30
{
29
- foreach ( T item in items )
30
- {
31
- list . Insert ( index , item ) ;
32
- }
33
- return list ;
31
+ list . Insert ( index , item ) ;
34
32
}
33
+ return list ;
34
+ }
35
35
36
- [ DebuggerHidden ]
37
- public static IList < T > InsertRange < T > ( this IList < T > list , int index , IEnumerable < T > items )
36
+ [ DebuggerHidden ]
37
+ public static IList < T > InsertRange < T > ( this IList < T > list , int index , IEnumerable < T > items )
38
+ {
39
+ foreach ( T item in items )
38
40
{
39
- foreach ( T item in items )
40
- {
41
- list . Insert ( index ++ , item ) ;
42
- }
43
- return list ;
41
+ list . Insert ( index ++ , item ) ;
44
42
}
43
+ return list ;
44
+ }
45
45
46
- [ DebuggerHidden ]
47
- public static IList < T > RemoveRange < T > ( this IList < T > list , IEnumerable < T > items )
46
+ [ DebuggerHidden ]
47
+ public static IList < T > RemoveRange < T > ( this IList < T > list , IEnumerable < T > items )
48
+ {
49
+ foreach ( T item in items )
48
50
{
49
- foreach ( T item in items )
50
- {
51
- list . Remove ( item ) ;
52
- }
53
- return list ;
51
+ list . Remove ( item ) ;
54
52
}
53
+ return list ;
54
+ }
55
55
56
- [ DebuggerHidden ]
57
- public static IList < T > ReplaceRange < T > ( this IList < T > list , IEnumerable < T > items )
58
- {
59
- return list . RemoveRange ( items ) . AddRange ( items ) ;
60
- }
56
+ [ DebuggerHidden ]
57
+ public static IList < T > ReplaceRange < T > ( this IList < T > list , IEnumerable < T > items )
58
+ {
59
+ return list . RemoveRange ( items ) . AddRange ( items ) ;
60
+ }
61
61
62
- [ DebuggerHidden ]
63
- public static IList < T > Replace < T > ( this IList < T > list , T replace , T with )
62
+ [ DebuggerHidden ]
63
+ public static IList < T > Replace < T > ( this IList < T > list , T replace , T with )
64
+ {
65
+ int index = list . IndexOf ( replace ) ;
66
+ if ( index < 0 )
64
67
{
65
- int index = list . IndexOf ( replace ) ;
66
- if ( index < 0 )
67
- throw new InvalidOperationException ( "Item to replace not found in list" ) ;
68
-
69
- list [ index ] = with ;
70
- return list ;
68
+ throw new InvalidOperationException ( "Item to replace not found in list" ) ;
71
69
}
72
70
73
- [ DebuggerHidden ]
74
- public static T Min < T > ( this IList < T > source , Func < T , T > selector , T defaultValue )
75
- {
76
- if ( source == null || source . Count == 0 )
77
- return defaultValue ;
78
- return source . Min ( selector ) ;
79
- }
71
+ list [ index ] = with ;
72
+ return list ;
73
+ }
80
74
81
- [ DebuggerHidden ]
82
- public static TSource MinBy < TSource , TKey > ( this IList < TSource > source , Func < TSource , TKey > selector , TSource defaultValue )
75
+ [ DebuggerHidden ]
76
+ public static T Min < T > ( this IList < T > source , Func < T , T > selector , T defaultValue )
77
+ {
78
+ if ( source == null || source . Count == 0 )
83
79
{
84
- if ( source == null || source . Count == 0 )
85
- return defaultValue ;
86
- return source . MinBy ( selector ) ;
80
+ return defaultValue ;
87
81
}
82
+ return source . Min ( selector ) ;
83
+ }
88
84
89
- [ DebuggerHidden ]
90
- public static TKey MinValue < TSource , TKey > ( this IList < TSource > source , Func < TSource , TKey > selector , TKey defaultValue )
85
+ [ DebuggerHidden ]
86
+ public static TSource MinBy < TSource , TKey > ( this IList < TSource > source , Func < TSource , TKey > selector , TSource defaultValue )
87
+ {
88
+ if ( source == null || source . Count == 0 )
91
89
{
92
- if ( source == null || source . Count == 0 )
93
- return defaultValue ;
94
- return selector ( source . MinBy ( selector ) ) ;
90
+ return defaultValue ;
95
91
}
92
+ return source . MinBy ( selector ) ;
93
+ }
96
94
97
- [ DebuggerHidden ]
98
- public static TKey MaxValue < TSource , TKey > ( this IList < TSource > source , Func < TSource , TKey > selector , TKey defaultValue )
95
+ [ DebuggerHidden ]
96
+ public static TKey MinValue < TSource , TKey > ( this IList < TSource > source , Func < TSource , TKey > selector , TKey defaultValue )
97
+ {
98
+ if ( source == null || source . Count == 0 )
99
99
{
100
- if ( source == null || source . Count == 0 )
101
- return defaultValue ;
102
- return selector ( source . MaxBy ( selector ) ) ;
100
+ return defaultValue ;
103
101
}
102
+ return selector ( source . MinBy ( selector ) ) ;
103
+ }
104
104
105
- public static T Dequeue < T > ( this IList < T > list )
105
+ [ DebuggerHidden ]
106
+ public static TKey MaxValue < TSource , TKey > ( this IList < TSource > source , Func < TSource , TKey > selector , TKey defaultValue )
107
+ {
108
+ if ( source == null || source . Count == 0 )
106
109
{
107
- T first = list . FirstOrDefault ( ) ;
108
- list . Remove ( first ) ;
109
- return first ;
110
+ return defaultValue ;
110
111
}
112
+ return selector ( source . MaxBy ( selector ) ) ;
113
+ }
114
+
115
+ public static T Dequeue < T > ( this IList < T > list )
116
+ {
117
+ T first = list . FirstOrDefault ( ) ;
118
+ list . Remove ( first ) ;
119
+ return first ;
120
+ }
111
121
112
- public static IList < T > Unique < T > ( this IList < T > source )
122
+ public static IList < T > Unique < T > ( this IList < T > source )
123
+ {
124
+ List < T > list = new ( ) ;
125
+ foreach ( T entry in source )
113
126
{
114
- List < T > list = new List < T > ( ) ;
115
- foreach ( T entry in source )
127
+ if ( ! list . Contains ( entry ) )
116
128
{
117
- if ( ! list . Contains ( entry ) )
118
- {
119
- list . Add ( entry ) ;
120
- }
129
+ list . Add ( entry ) ;
121
130
}
122
- return list ;
123
131
}
132
+ return list ;
133
+ }
134
+
135
+ public static void AddIfNotExists < T > ( this IList < T > list , IEnumerable < T > entries )
136
+ {
137
+ entries . ForEach ( list . AddIfNotExists ) ;
138
+ }
139
+
140
+ public static void AddIfNotExists < T > ( this IList < T > list , T entry )
141
+ {
142
+ if ( list . Contains ( entry ) )
143
+ {
144
+ return ;
145
+ }
146
+ list . Add ( entry ) ;
124
147
}
125
- }
148
+ }
0 commit comments