Closed RealityProgrammer closed 2 years ago
System.Collections.Generic.List
void Swap(List<T> list2)
Swap all elements between 2 List<T>, mimic C++'s std::vector.swap()
List<T>
Mimic C++'s std::vector::swap, quick and easy
O(n) operation, with n is the largest size between 2 lists, unlike C++, std::vector::swap will swap 2 data pointers at constant time
var min = Math.Min(list1.Count, list2.Count); for (int i = 0; i < min; i++) { (list1[i], list2[i]) = (list2[i], list1[i]); } if (list2.Count < list1.Count) { list2.EnsureCapacity(list1.Count); for (int i = min; i < list1.Count; i++) { list2.Add(list1[i]); } list1.RemoveRange(min, list1.Count - min); } else if (list1.Count < list2.Count) { list1.EnsureCapacity(list2.Count); for (int i = min; i < list2.Count; i++) { list1.Add(list2[i]); } list2.RemoveRange(min, list1.Count - min); }
Added with 02765b8b197a3108b25c580035e753545e7f8762, defined for IList<T> rather than List<T>.
IList<T>
Type to extend
System.Collections.Generic.List
Signature
Summary
Parameters
List<T>
Benefits
Mimic C++'s std::vector::swap, quick and easy
Drawbacks
O(n) operation, with n is the largest size between 2 lists, unlike C++, std::vector::swap will swap 2 data pointers at constant time
Implementation Example
Code of Conduct