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