1414// </summary>
1515// ***********************************************************************
1616
17+ using System ;
18+ using System . Collections ;
19+ using System . Collections . Generic ;
20+ using System . Linq ;
21+ using DomainCommonExtensions . CommonExtensions ;
22+ using DomainCommonExtensions . DataTypeExtensions ;
23+
1724namespace DomainCommonExtensions . ArraysExtensions
1825{
26+ /// -------------------------------------------------------------------------------------------------
1927 /// <summary>
20- /// Array extensions
28+ /// Array extensions.
2129 /// </summary>
22- /// <remarks></remarks>
30+ /// =================================================================================================
2331 public static class ArrayExtensions
2432 {
33+ /// -------------------------------------------------------------------------------------------------
2534 /// <summary>
26- /// Returns the first index of the given value
35+ /// Returns the first index of the given value.
2736 /// </summary>
28- /// <param name="array">Input array</param>
29- /// <param name="value">Value to search</param>
30- /// <returns></returns>
31- /// <typeparam name="T">Type of input</typeparam>
32- /// <remarks>In case when no found any value result is -1</remarks>
33- public static int IndexOf < T > ( this T [ ] array , T value )
37+ /// <remarks>
38+ /// In case when no found any value result is -1.
39+ /// </remarks>
40+ /// <typeparam name="T">Type of input.</typeparam>
41+ /// <param name="source">Input source.</param>
42+ /// <param name="value">Value to search.</param>
43+ /// <returns>
44+ /// An int.
45+ /// </returns>
46+ /// =================================================================================================
47+ public static int IndexOf < T > ( this T [ ] source , T value )
3448 {
3549 const int index = - 1 ;
3650 try
3751 {
38- if ( array . IsNullOrEmptyEnumerable ( ) ) return index ;
52+ if ( source . IsNullOrEmptyEnumerable ( ) ) return index ;
3953
40- for ( var i = 0 ; i < array . Length ; i ++ )
41- if ( Equals ( array [ i ] , value ) )
54+ for ( var i = 0 ; i < source . Length ; i ++ )
55+ if ( Equals ( source [ i ] , value ) )
4256 return i ;
4357 }
4458 catch
@@ -48,5 +62,143 @@ public static int IndexOf<T>(this T[] array, T value)
4862
4963 return index ;
5064 }
65+
66+ /// -------------------------------------------------------------------------------------------------
67+ /// <summary>
68+ /// Append an item to source array.
69+ /// </summary>
70+ /// <typeparam name="T">Generic type parameter. Source type.</typeparam>
71+ /// <param name="source">Input source.</param>
72+ /// <param name="item">The item.</param>
73+ /// <returns>
74+ /// Returns a new T[].
75+ /// </returns>
76+ /// =================================================================================================
77+ public static T [ ] AppendItem < T > ( this T [ ] source , T item )
78+ {
79+ if ( source . IsNullOrEmptyEnumerable ( ) && item . IsNull ( ) ) return new T [ ] { } ;
80+ if ( item . IsNull ( ) ) return source ;
81+ if ( source . IsNullOrEmptyEnumerable ( ) ) return new [ ] { item } ;
82+
83+ var result = new T [ source . Length + 1 ] ;
84+ source . CopyTo ( result , 0 ) ;
85+ result [ source . Length ] = item ;
86+
87+ return result ;
88+ }
89+
90+ /// -------------------------------------------------------------------------------------------------
91+ /// <summary>
92+ /// Append an item/s to source array.
93+ /// </summary>
94+ /// <typeparam name="T">Generic type parameter. Source type.</typeparam>
95+ /// <param name="source">Input source.</param>
96+ /// <param name="items">A variable-length parameters list containing items.</param>
97+ /// <returns>
98+ /// Returns a new T[].
99+ /// </returns>
100+ /// =================================================================================================
101+ public static T [ ] AppendItem < T > ( this T [ ] source , params T [ ] items )
102+ {
103+ if ( source . IsNullOrEmptyEnumerable ( ) && items . IsNullOrEmptyEnumerable ( ) ) return new T [ ] { } ;
104+ if ( items . IsNullOrEmptyEnumerable ( ) ) return source ;
105+ if ( source . IsNullOrEmptyEnumerable ( ) ) return items ;
106+
107+ var result = new T [ source . Length + items . Length ] ;
108+ source . CopyTo ( result , 0 ) ;
109+ items . CopyTo ( result , source . Length ) ;
110+
111+ return result ;
112+ }
113+
114+ /// -------------------------------------------------------------------------------------------------
115+ /// <summary>
116+ /// Append an item to source array.
117+ /// </summary>
118+ /// <typeparam name="T">Generic type parameter.</typeparam>
119+ /// <param name="source">Input source.</param>
120+ /// <param name="index">Zero-based index of the.</param>
121+ /// <param name="item">The item.</param>
122+ /// <returns>
123+ /// Returns a new T[].
124+ /// </returns>
125+ /// =================================================================================================
126+ public static T [ ] AppendItem < T > ( this T [ ] source , int index , T item )
127+ {
128+ if ( source . IsNullOrEmptyEnumerable ( ) ) return new T [ ] { } ;
129+ if ( item . IsNull ( ) ) return source ;
130+ if ( index . IsLessZero ( ) ) return source ;
131+
132+ source [ index ] = item ;
133+
134+ return source ;
135+ }
136+
137+ /// -------------------------------------------------------------------------------------------------
138+ /// <summary>
139+ /// Append an items to source array.
140+ /// </summary>
141+ /// <typeparam name="T">Generic type parameter. Source type.</typeparam>
142+ /// <param name="source">Input source.</param>
143+ /// <param name="itemsToAppend">The items to append.</param>
144+ /// <returns>
145+ /// Returns a new T[].
146+ /// </returns>
147+ /// =================================================================================================
148+ public static T [ ] AppendIfNotExists < T > ( this T [ ] source , T [ ] itemsToAppend )
149+ {
150+ if ( source . IsNullOrEmptyEnumerable ( ) && itemsToAppend . IsNullOrEmptyEnumerable ( ) ) return new T [ ] { } ;
151+ if ( source . IsNullOrEmptyEnumerable ( ) ) return itemsToAppend ;
152+ if ( itemsToAppend . IsNullOrEmptyEnumerable ( ) ) return source ;
153+
154+ return source . AppendItem ( ( from t in itemsToAppend let existIdx = source . IndexOf ( t ) where existIdx == - 1 select t ) . ToArray ( ) ) ;
155+ }
156+
157+ /// -------------------------------------------------------------------------------------------------
158+ /// <summary>
159+ /// Removes the item from source array.
160+ /// </summary>
161+ /// <typeparam name="T">Generic type parameter. Source type.</typeparam>
162+ /// <param name="source">Input source.</param>
163+ /// <param name="item">The item.</param>
164+ /// <returns>
165+ /// Returns a new T[].
166+ /// </returns>
167+ /// =================================================================================================
168+ public static T [ ] RemoveItem < T > ( this T [ ] source , T item )
169+ {
170+ if ( source . IsNullOrEmptyEnumerable ( ) ) return new T [ ] { } ;
171+ if ( item . IsNull ( ) ) return source ;
172+
173+ var idx = source . IndexOf ( item ) ;
174+
175+ return source . RemoveAtIdx ( idx ) ;
176+ }
177+
178+ /// -------------------------------------------------------------------------------------------------
179+ /// <summary>
180+ /// A T[] extension method that removes at index.
181+ /// </summary>
182+ /// <typeparam name="T">Generic type parameter. Source type.</typeparam>
183+ /// <param name="source">Input source.</param>
184+ /// <param name="index">Zero-based index of the.</param>
185+ /// <returns>
186+ /// A T[].
187+ /// </returns>
188+ /// =================================================================================================
189+ public static T [ ] RemoveAtIdx < T > ( this T [ ] source , int index )
190+ {
191+ if ( source . IsNullOrEmptyEnumerable ( ) ) return new T [ ] { } ;
192+ if ( index . IsLessZero ( ) ) return source ;
193+
194+ T [ ] dest = new T [ source . Length - 1 ] ;
195+ if ( index > 0 )
196+ Array . Copy ( source , 0 , dest , 0 , index ) ;
197+
198+ if ( index < source . Length - 1 )
199+ Array . Copy ( source , index + 1 , dest , index , source . Length - index - 1 ) ;
200+
201+ return dest ;
202+ }
51203 }
52204}
0 commit comments