@@ -29,11 +29,11 @@ public static class ExtendedList
2929 /// <summary>
3030 /// Check if a List is Null or just Empty
3131 /// </summary>
32- /// <typeparam name="TValue">Generic data Type</typeparam>
32+ /// <typeparam name="TValue">Generic Object Type</typeparam>
3333 /// <param name="lst">List we want to check</param>
3434 /// <returns>Empty or not</returns>
3535 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
36- public static bool IsNullOrEmpty < TValue > ( [ NotNullWhen ( false ) ] this ICollection < TValue > lst )
36+ public static bool IsNullOrEmpty < TValue > ( [ NotNullWhen ( false ) ] this ICollection < TValue > ? lst )
3737 {
3838 if ( lst == null )
3939 {
@@ -46,7 +46,7 @@ public static bool IsNullOrEmpty<TValue>([NotNullWhen(false)] this ICollection<T
4646 /// <summary>
4747 /// Add Element at the first Entry
4848 /// </summary>
49- /// <typeparam name="TValue">Generic data Type</typeparam>
49+ /// <typeparam name="TValue">Generic Object Type</typeparam>
5050 /// <param name="lst">List we want to add to</param>
5151 /// <param name="item">item we will replace or add</param>
5252 public static void AddFirst < TValue > ( this List < TValue > lst , TValue item )
@@ -57,7 +57,23 @@ public static void AddFirst<TValue>(this List<TValue> lst, TValue item)
5757 }
5858
5959 /// <summary>
60- /// Removes the fast.
60+ /// Only works with equal and Implemented IEquality Interface
61+ /// </summary>
62+ /// <typeparam name="TValue">Generic Object Type</typeparam>
63+ /// <param name="lst">List we want to add to</param>
64+ /// <param name="item">item we will replace or add</param>
65+ /// <returns>if [true] item was added, else [false]</returns>
66+ public static bool AddDistinct < TValue > ( this List < TValue > lst , TValue item )
67+ {
68+ if ( lst . Contains ( item ) ) return false ;
69+
70+ lst . Add ( item ) ;
71+ return true ;
72+ }
73+
74+ /// <summary>
75+ /// Removes element from ist fast.
76+ /// Destroys order though.
6177 /// </summary>
6278 /// <typeparam name="TValue">Generic data Type</typeparam>
6379 /// <param name="list">The list.</param>
@@ -76,28 +92,24 @@ public static void RemoveFast<TValue>(this List<TValue> list, TValue item)
7692 list . RemoveAt ( lastIndex ) ;
7793 }
7894
79-
8095 /// <summary>
81- /// Only works with equal and Implemented IEquality Interface
96+ /// Removes element at index fast by swapping with the last element.
97+ /// Destroys order.
8298 /// </summary>
83- /// <typeparam name="TValue">Generic data Type</typeparam>
84- /// <param name="lst">List we want to add to</param>
85- /// <param name="item">item we will replace or add</param>
86- /// <returns>if [true] item was added, else [false]</returns>
87- public static bool AddDistinct < TValue > ( this List < TValue > lst , TValue item )
99+ /// <typeparam name="TValue">The type of the value.</typeparam>
100+ /// <param name="list">The list.</param>
101+ /// <param name="index">The index.</param>
102+ public static void RemoveAtFast < TValue > ( this List < TValue > list , int index )
88103 {
89- var hashSet = new HashSet < TValue > ( lst ) ;
104+ if ( index < 0 || index >= list . Count ) return ;
90105
91- // Check if the item already exists in the HashSet
92- if ( hashSet . Contains ( item ) )
93- {
94- return false ; // Item already exists, no need to add
95- }
106+ int lastIndex = list . Count - 1 ;
96107
97- // Add the item to the list since it doesn't already exist
98- lst . Add ( item ) ;
108+ // Move the last element into the slot of the element to remove
109+ list [ index ] = list [ lastIndex ] ;
99110
100- return true ;
111+ // Remove the now-redundant last element
112+ list . RemoveAt ( lastIndex ) ;
101113 }
102114
103115 /// <summary>
@@ -106,7 +118,7 @@ public static bool AddDistinct<TValue>(this List<TValue> lst, TValue item)
106118 /// <typeparam name="TValue">The type of the value.</typeparam>
107119 /// <param name="lst">The List.</param>
108120 /// <returns>A Dictionary from a list with int as the key</returns>
109- public static Dictionary < int , TValue > ToDictionary < TValue > ( this IEnumerable < TValue > lst )
121+ public static Dictionary < int , TValue > ? ToDictionary < TValue > ( this IEnumerable < TValue > lst )
110122 {
111123 var index = 0 ;
112124 return lst ? . ToDictionary ( _ => index ++ ) ;
@@ -115,7 +127,7 @@ public static Dictionary<int, TValue> ToDictionary<TValue>(this IEnumerable<TVal
115127 /// <summary>
116128 /// Add contents of another sequence to the base list, ensuring no duplicates
117129 /// </summary>
118- /// <typeparam name="TValue">Generic data Type</typeparam>
130+ /// <typeparam name="TValue">Generic Object Type</typeparam>
119131 /// <param name="lst">Base list we add to</param>
120132 /// <param name="range">Sequence with elements we want to add</param>
121133 /// <param name="invert">optional parameter invert result</param>
@@ -137,7 +149,7 @@ public static void Union<TValue>(this List<TValue> lst, IEnumerable<TValue> rang
137149 /// <summary>
138150 /// Add contents of another sequence to the base list, ensuring no duplicates
139151 /// </summary>
140- /// <typeparam name="TValue">Generic data Type</typeparam>
152+ /// <typeparam name="TValue">Generic Object Type</typeparam>
141153 /// <param name="lst">Base list we add to</param>
142154 /// <param name="range">Sequence with elements we want to add</param>
143155 /// <param name="invert">If true, removes elements instead of adding them</param>
@@ -161,7 +173,7 @@ public static void Difference<TValue>(this List<TValue> lst, IEnumerable<TValue>
161173 /// <summary>
162174 /// Keep only elements present in both the base list and another sequence
163175 /// </summary>
164- /// <typeparam name="TValue">Generic data Type</typeparam>
176+ /// <typeparam name="TValue">Generic Object Type</typeparam>
165177 /// <param name="lst">Base list to filter</param>
166178 /// <param name="range">Sequence with elements to retain</param>
167179 /// <param name="invert">If true, keeps elements not present in both sequences</param>
@@ -186,7 +198,7 @@ public static void Intersection<TValue>(this List<TValue> lst, IEnumerable<TValu
186198 /// <summary>
187199 /// Keep only elements that are in either the base list or another sequence but not in both
188200 /// </summary>
189- /// <typeparam name="TValue">Generic data Type</typeparam>
201+ /// <typeparam name="TValue">Generic Object Type</typeparam>
190202 /// <param name="lst">Base list to modify</param>
191203 /// <param name="range">Sequence with elements to compare</param>
192204 /// <param name="invert">If true, keeps elements that are in both sequences</param>
@@ -214,10 +226,10 @@ public static void SymmetricDifference<TValue>(this List<TValue> lst, IEnumerabl
214226 /// Try to Clone a List
215227 /// Here we abuse the IEnumerable ToList Method
216228 /// </summary>
217- /// <typeparam name="TValue">Generic data Type</typeparam>
229+ /// <typeparam name="TValue">Generic Object Type</typeparam>
218230 /// <param name="lst">IEnumerable</param>
219231 /// <returns>Clone of the Input IEnumerable</returns>
220- public static List < TValue > Clone < TValue > ( this IEnumerable < TValue > lst )
232+ public static List < TValue > ? Clone < TValue > ( this IEnumerable < TValue > ? lst )
221233 {
222234 return lst ? . ToList ( ) ;
223235 }
@@ -277,7 +289,7 @@ public static bool Equal<TValue>(this List<TValue> lst, List<TValue> compare, En
277289 /// <summary>
278290 /// Chunks a list by a certain amount.
279291 /// </summary>
280- /// <typeparam name="TValue">Generic data Type</typeparam>
292+ /// <typeparam name="TValue">Generic Object Type</typeparam>
281293 /// <param name="source">The source.</param>
282294 /// <param name="chunkSize">Size of the chunk.</param>
283295 /// <returns>List split into chunks</returns>
@@ -324,8 +336,8 @@ public static void Shuffle<T>(this IList<T> lst)
324336 /// <returns>
325337 /// Dictionary with an conversion from the attribute Id as Key
326338 /// </returns>
327- public static Dictionary < TId , TValue > ToDictionaryId < TValue , TId > ( this IList < TValue > lst )
328- where TValue : IIdHandling < TId >
339+ public static Dictionary < TId , TValue > ToDictionaryId < TValue , TId > ( this IEnumerable < TValue > lst )
340+ where TValue : IIdHandling < TId > where TId : notnull
329341 {
330342 var dct = new Dictionary < TId , TValue > ( ) ;
331343
0 commit comments