Swapping two arbitrary variables

Started by
14 comments, last by Nemesis2k2 19 years, 7 months ago
Is there a better way to implement this (in C#)?

    public class Utility<T>
    {
        private Utility() { }

        public static void Swap(ref T obj1, ref T obj2)
        {
            T temp = obj1;
            obj1 = obj2;
            obj2 = temp;
        }
    }


Advertisement
Here is another way...it should work.

public static void swap(ref T obj1, ret T obj2){         obj1 = obj1 ^ obj2;         obj2 = obj1 ^ obj2;         obj1 = obj1 ^ obj2;}


That way you dont have to create a temporary object. Then again, I dont know if it works for classes and objects.
Quote:Original post by visage
Here is another way...it should work.

public static void swap(ref T obj1, ret T obj2){         obj1 = obj1 ^ obj2;         obj2 = obj1 ^ obj2;         obj1 = obj1 ^ obj2;}


That way you dont have to create a temporary object. Then again, I dont know if it works for classes and objects.


That will only work for classes for which the ^ operator is a bitwise XOR. You can't be sure that this is the case though (that I know of... though maybe you could do a compile time test... hmmm).

Dwiel
Quote:Original post by visage
Here is another way...it should work.

public static void swap(ref T obj1, ret T obj2){         obj1 = obj1 ^ obj2;         obj2 = obj1 ^ obj2;         obj1 = obj1 ^ obj2;}


That way you dont have to create a temporary object. Then again, I dont know if it works for classes and objects.

Does C# allow you to cast without altering bits? In C/C++ you can, and that allows you to use the XOR trick for all data. Assuming your function is right, you'd want this:
*(int*)(&obj1) ^= *(int*)(&obj2);*(int*)(&obj2) = *(int*)(&obj1) ^ *(int*)(&obj2);*(int*)(&obj1) ^= *(int*)(&obj2);

If obj1 and obj2 are both 32-bits in size (which means 32-bit ints, 32-bit floats, or pointers) then that'll work. In C/C++. I don't think you can do this in Java, and I'm unclear on C#.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
You can do it in java, but just with the primitive types.
on a prtial side note, the XOR swap _KILLS_ the pipe
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Quote:Original post by jperalta
Is there a better way to implement this (in C#)?

*** Source Snippet Removed ***


Seeing as how you use generics, it seems to me like you are using C# 2.0. So I think it would be better if you made the class static and removed the private constructor.
Quote:Original post by silvermace
on a prtial side note, the XOR swap _KILLS_ the pipe

Indeed. It'll be faster to create a tempoary. Trust me.
The XOR method is just a hack, its slow and ugly.
Quote:Original post by silvermace
on a prtial side note, the XOR swap _KILLS_ the pipe
I have read that several times but I don't understand what you mean with it, could you please explain?
Quote:Original post by visage

Here is another way...it should work.

XOR-trick

That way you dont have to create a temporary object. Then again, I dont know if it works for classes and objects.


Bad idea. It can only work with basic types of the same size. And if you ever make the mistake of calling swap(a,a) (which, with aliasing, may happen without you noticing), you're doomed.

Quote:Original post by Eriond
I have read that several times but I don't understand what you mean with it, could you please explain?


Because you're hogging the ALU for something that doesn't need it.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement