Jump to content
  • Advertisement
Sign in to follow this  
jperalta

Swapping two arbitrary variables

This topic is 5045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
        }
    }


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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#.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!