1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Runtime . CompilerServices ;
5+
6+ namespace VirtueSky . Misc
7+ {
8+ public partial class Common
9+ {
10+ public static void Clear < T > ( this T [ ] collection )
11+ {
12+ if ( collection == null ) throw new ArgumentNullException ( nameof ( collection ) ) ;
13+ Array . Clear ( collection , 0 , collection . Length ) ;
14+ }
15+
16+ #region IsNullOrEmpty
17+
18+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
19+ public static bool IsNullOrEmpty < T > ( this List < T > source )
20+ {
21+ return source == null || source . Count == 0 ;
22+ }
23+
24+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
25+ public static bool IsNullOrEmpty < T > ( this T [ ] source )
26+ {
27+ return source == null || source . Length == 0 ;
28+ }
29+
30+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
31+ public static bool IsNullOrEmpty < TKey , TValue > ( this Dictionary < TKey , TValue > source )
32+ {
33+ return source == null || source . Keys . Count == 0 ;
34+ }
35+
36+ #endregion
37+
38+ #region Shuffle
39+
40+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
41+ public static void Shuffle < T > ( this T [ ] source )
42+ {
43+ int n = source . Length ;
44+ while ( n > 1 )
45+ {
46+ n -- ;
47+ int k = UnityEngine . Random . Range ( 0 , n ) ;
48+ ( source [ k ] , source [ n ] ) = ( source [ n ] , source [ k ] ) ;
49+ }
50+ }
51+
52+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
53+ public static void Shuffle < T > ( this List < T > source )
54+ {
55+ int n = source . Count ;
56+ while ( n > 1 )
57+ {
58+ n -- ;
59+ int k = UnityEngine . Random . Range ( 0 , n ) ;
60+ ( source [ k ] , source [ n ] ) = ( source [ n ] , source [ k ] ) ;
61+ }
62+ }
63+
64+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
65+ public static IDictionary < T1 , T2 > Shuffle < T1 , T2 > ( this IDictionary < T1 , T2 > source )
66+ {
67+ var keys = source . Keys . ToArray ( ) ;
68+ var values = source . Values . ToArray ( ) ;
69+
70+ int n = source . Count ;
71+ while ( n > 1 )
72+ {
73+ n -- ;
74+ int k = UnityEngine . Random . Range ( 0 , n ) ;
75+ ( keys [ k ] , keys [ n ] ) = ( keys [ n ] , keys [ k ] ) ;
76+ ( values [ k ] , values [ n ] ) = ( values [ n ] , values [ k ] ) ;
77+ }
78+
79+ return MakeDictionary ( keys , values ) ;
80+ }
81+
82+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
83+ public static IDictionary < TKey , TValue > MakeDictionary < TKey , TValue > ( this TKey [ ] keys , TValue [ ] values )
84+ {
85+ if ( keys == null ) throw new ArgumentNullException ( nameof ( keys ) ) ;
86+ if ( values == null ) throw new ArgumentNullException ( nameof ( values ) ) ;
87+ if ( keys . Length != values . Length ) throw new ArgumentException ( "Size keys and size values diffirent!" ) ;
88+
89+ IDictionary < TKey , TValue > result = new Dictionary < TKey , TValue > ( ) ;
90+ for ( var i = 0 ; i < keys . Length ; i ++ )
91+ {
92+ result . Add ( keys [ i ] , values [ i ] ) ;
93+ }
94+
95+ return result ;
96+ }
97+
98+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
99+ public static IDictionary < TKey , TValue > MakeDictionary < TKey , TValue > ( this IList < TKey > keys ,
100+ IList < TValue > values )
101+ {
102+ if ( keys == null ) throw new ArgumentNullException ( nameof ( keys ) ) ;
103+ if ( values == null ) throw new ArgumentNullException ( nameof ( values ) ) ;
104+ if ( keys . Count != values . Count ) throw new ArgumentException ( "Size keys and size values diffirent!" ) ;
105+
106+ IDictionary < TKey , TValue > result = new Dictionary < TKey , TValue > ( ) ;
107+ for ( var i = 0 ; i < keys . Count ; i ++ )
108+ {
109+ result . Add ( keys [ i ] , values [ i ] ) ;
110+ }
111+
112+ return result ;
113+ }
114+
115+ #endregion
116+
117+ #region Pick random
118+
119+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
120+ public static T PickRandom < T > ( this T [ ] collection )
121+ {
122+ if ( collection == null ) throw new ArgumentNullException ( nameof ( collection ) ) ;
123+
124+ return collection . Length == 0 ? default : collection [ UnityEngine . Random . Range ( 0 , collection . Length ) ] ;
125+ }
126+
127+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
128+ public static T PickRandom < T > ( this List < T > collection )
129+ {
130+ if ( collection == null ) throw new ArgumentNullException ( nameof ( collection ) ) ;
131+
132+ return collection . Count == 0 ? default : collection [ UnityEngine . Random . Range ( 0 , collection . Count ) ] ;
133+ }
134+
135+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
136+ public static ( T , int ) PickRandomAndIndex < T > ( this T [ ] collection )
137+ {
138+ if ( collection == null ) throw new ArgumentNullException ( nameof ( collection ) ) ;
139+
140+ int index = UnityEngine . Random . Range ( 0 , collection . Length ) ;
141+ return collection . Length == 0 ? ( default , - 1 ) : ( collection [ index ] , index ) ;
142+ }
143+
144+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
145+ public static ( T , int ) PickRandomWithIndex < T > ( this List < T > collection )
146+ {
147+ if ( collection == null ) throw new ArgumentNullException ( nameof ( collection ) ) ;
148+
149+ var index = UnityEngine . Random . Range ( 0 , collection . Count ) ;
150+ return collection . Count == 0 ? ( default , - 1 ) : ( collection [ index ] , index ) ;
151+ }
152+
153+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
154+ public static List < T > PickRandomSubList < T > ( this List < T > collection , int length )
155+ {
156+ if ( collection == null ) throw new ArgumentNullException ( nameof ( collection ) ) ;
157+ var listTemp = collection . ToList ( ) ;
158+ List < T > pickList = new List < T > ( ) ;
159+ listTemp . Shuffle ( ) ;
160+ for ( int i = 0 ; i < listTemp . Count ; i ++ )
161+ {
162+ if ( i < length ) pickList . Add ( listTemp [ i ] ) ;
163+ }
164+
165+ return pickList ;
166+ }
167+
168+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
169+ public static T [ ] PickRandomSubArray < T > ( this T [ ] collection , int length )
170+ {
171+ if ( collection == null ) throw new ArgumentNullException ( nameof ( collection ) ) ;
172+ T [ ] arrayTemp = new T [ collection . Length ] ;
173+ Array . Copy ( collection , arrayTemp , collection . Length ) ;
174+ T [ ] pickArray = new T [ length <= collection . Length ? length : collection . Length ] ;
175+ arrayTemp . Shuffle ( ) ;
176+ for ( int i = 0 ; i < arrayTemp . Length ; i ++ )
177+ {
178+ if ( i < length ) pickArray [ i ] = arrayTemp [ i ] ;
179+ }
180+
181+ return pickArray ;
182+ }
183+
184+ #endregion
185+
186+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
187+ public static void Swap < T > ( this T [ ] source , int oldIndex , int newIndex )
188+ {
189+ if ( oldIndex < 0 || newIndex < 0 || oldIndex > source . Length || newIndex > source . Length )
190+ {
191+ #if UNITY_EDITOR
192+ UnityEngine . Debug . LogError ( "Index out of range!" ) ;
193+ #endif
194+ return ;
195+ }
196+
197+ if ( oldIndex == newIndex ) return ;
198+ ( source [ oldIndex ] , source [ newIndex ] ) = ( source [ newIndex ] , source [ oldIndex ] ) ;
199+ }
200+
201+
202+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
203+ public static void Swap < T > ( this List < T > source , int oldIndex , int newIndex )
204+ {
205+ if ( oldIndex < 0 || newIndex < 0 || oldIndex > source . Count || newIndex > source . Count )
206+ {
207+ #if UNITY_EDITOR
208+ UnityEngine . Debug . LogError ( "Index out of range!" ) ;
209+ #endif
210+ return ;
211+ }
212+
213+ if ( oldIndex == newIndex ) return ;
214+ ( source [ oldIndex ] , source [ newIndex ] ) = ( source [ newIndex ] , source [ oldIndex ] ) ;
215+ }
216+
217+
218+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
219+ public static T [ ] ForEach < T > ( this T [ ] source , Action < T > action )
220+ {
221+ for ( int i = source . Length - 1 ; i >= 0 ; i -- )
222+ {
223+ action ( source [ i ] ) ;
224+ }
225+
226+ return source ;
227+ }
228+
229+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
230+ public static List < T > ForEach < T > ( this List < T > source , Action < T > action )
231+ {
232+ for ( int i = source . Count - 1 ; i >= 0 ; i -- )
233+ {
234+ action ( source [ i ] ) ;
235+ }
236+
237+ return source ;
238+ }
239+
240+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
241+ public static T [ ] ForEach < T > ( this T [ ] source , Action < T , int > action )
242+ {
243+ for ( int i = source . Length - 1 ; i >= 0 ; i -- )
244+ {
245+ action ( source [ i ] , i ) ;
246+ }
247+
248+ return source ;
249+ }
250+
251+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
252+ public static List < T > ForEach < T > ( this List < T > source , Action < T , int > action )
253+ {
254+ for ( int i = source . Count - 1 ; i >= 0 ; i -- )
255+ {
256+ action ( source [ i ] , i ) ;
257+ }
258+
259+ return source ;
260+ }
261+
262+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
263+ public static void Removes < T > ( this List < T > source , List < T > entries )
264+ {
265+ for ( var i = 0 ; i < entries . Count ; i ++ )
266+ {
267+ source . Remove ( entries [ i ] ) ;
268+ }
269+ }
270+
271+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
272+ public static void Removes < T > ( this List < T > source , T [ ] entries )
273+ {
274+ foreach ( var item in entries )
275+ {
276+ source . Remove ( item ) ;
277+ }
278+ }
279+
280+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
281+ public static List < T > Adds < T > ( this List < T > source , List < T > entries )
282+ {
283+ for ( int i = 0 ; i < entries . Count ; i ++ )
284+ {
285+ source . Add ( entries [ i ] ) ;
286+ }
287+
288+ return source ;
289+ }
290+
291+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
292+ public static List < T > Adds < T > ( this List < T > source , T [ ] entries )
293+ {
294+ foreach ( var e in entries )
295+ {
296+ source . Add ( e ) ;
297+ }
298+
299+ return source ;
300+ }
301+ }
302+ }
0 commit comments