C #: Good / Best Swap Method Implementation

I read this post about shuffling a map and in many shuffling and sorting algorithms you need to swap two elements in a list or array. But what does a good and effective Swap method look like?

Let say for a T[] and for a List<T> . What is the best way to implement a method that changes two elements in these two?

 Swap(ref cards[i], ref cards[n]); // How is Swap implemented? 
+10
sorting c # shuffle
Feb 16 '09 at 9:29
source share
5 answers

Well, the code you posted ( ref cards[n] ) can only work with an array (and not with a list) - but you would just use (where foo and bar are two values):

 static void Swap(ref int foo, ref int bar) { int tmp = foo; foo = bar; bar = tmp; } 

Or perhaps (if you want atomic):

 Interlocked.Exchange(ref foo, ref bar); 

Personally, I don’t think I would bother with the swap method, but just do it directly; this means that you can use (either for a list or for an array):

 int tmp = cards[n]; cards[n] = cards[i]; cards[i] = tmp; 

If you really wanted to write a swap method that worked in an array of list or , you would need to do something like:

 static void Swap(IList<int> list, int indexA, int indexB) { int tmp = list[indexA]; list[indexA] = list[indexB]; list[indexB] = tmp; } 

(it would be trivial to do this general), however, the original version of "inline" (ie not the method) working on the array will be faster.

+21
Feb 16 '09 at 9:34
source share

Using:

 void swap(int &a, int &b) { // &a != &b // a == b OK a ^= b; b ^= a; a ^= b; return; } 

I did not understand that I was in the C # section. This is C ++ code, but it should have the same basic idea. I believe X is also XOR in C #. It looks like instead of & you might need "ref" (?). I'm not sure.

+5
Jan 03 '09 at 22:17
source share

A good exchange is one where you do not change the content. In C / C ++, it will be like replacing pointers instead of replacing content. This exchange style is fast and comes with some exclusion guarantee. Unfortunately, my C # is too rusty for me to put it in code. For simple data types, this style does not give you much. But once you're used to and dealing with larger (and more complex) objects, it can save your life.

+3
Feb 16 '09 at 9:37
source share

How about this? This is a general implementation of the swap method. Jit will create a compiled version of closed types ONLY for you, so you don’t have to worry about performance!

 /// <summary> /// Swap two elements /// Generic implementation by LMF /// </summary> public static void Swap<T>(ref T itemLeft, ref T itemRight) { T dummyItem = itemRight; itemLeft = itemRight; itemRight = dummyItem; } 

NTN Lorenzo

+3
Dec 20 '13 at 3:41
source share

For some people, replacement can also be done using extension methods (.NET 3.0 and later).

In general, it seems there is no way to say that the extension methods "this" are ref, so you need to return it and override the old value.

 public static class GeneralExtensions { public static T SwapWith<T>(this T current, ref T other) { T tmpOther = other; other = current; return tmpOther; } } 

This extension method can be used as follows:

 int val1 = 10; int val2 = 20; val1 = val1.SwapWith(ref val2); 
0
Jan 20 '14 at 15:16
source share



All Articles