[flexcoders] Re: swap two variables without temporary variable

2007-08-30 Thread Reid Priedhorsky
carlos m wrote:
 Hey Reid,
 
 Don't think there is a built-in function that will do that for
 you--not that I am aware of anyway--but there is definitely a way of
 implementing it! Check out this blog post by the awesome guys at
 polygonal:
 http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/ .
 Specifically, check out the section labeled Swap integers without
 temporary variables using XOR.
 
 Lot's of goodies on that blog!
 
  Cliffnotes: 
  var t:int = a;
  a = b;
  b = t;
 
 //equals: 
  a ^= b;
  b ^= a;
  a ^= b;HTH!

Hi Carlos,

Thanks for the note -- I suppose I should have been more clear. I meant, 
how to swap two variables in one line: it's not about speed but rather 
clarity. :)

Reid




RE: [flexcoders] Re: swap two variables without temporary variable

2007-08-30 Thread Gordon Smith
AS3 does not have references to ints. ints re always passed by value, so
you cannot write a method like swap(a, b) to swap them. Just use a temp
var.
 
- Gordon



From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Reid Priedhorsky
Sent: Thursday, August 30, 2007 9:41 AM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Re: swap two variables without temporary variable



carlos m wrote:
 Hey Reid,
 
 Don't think there is a built-in function that will do that for
 you--not that I am aware of anyway--but there is definitely a way of
 implementing it! Check out this blog post by the awesome guys at
 polygonal:
 http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/
http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/  .
 Specifically, check out the section labeled Swap integers without
 temporary variables using XOR.
 
 Lot's of goodies on that blog!
 
 Cliffnotes: 
 var t:int = a;
 a = b;
 b = t;
 
 //equals: 
 a ^= b;
 b ^= a;
 a ^= b;HTH!

Hi Carlos,

Thanks for the note -- I suppose I should have been more clear. I meant,

how to swap two variables in one line: it's not about speed but rather 
clarity. :)

Reid



 


[flexcoders] Re: swap two variables without temporary variable

2007-08-30 Thread Doug Lowder
What about:
...
swap(this, a, b);
...

private function swap(o: *, propnameA: String, propnameB: String) : 
void {
  // pseudocode for the swap
  temp = o[propnameA];
  o[propnameA] = o[propnameB];
  o[propnameB] = temp;
}

I guess it might depend on access to the variables within the object, 
but shouldn't that always work for o == this?

Doug

--- In flexcoders@yahoogroups.com, Gordon Smith [EMAIL PROTECTED] wrote:

 AS3 does not have references to ints. ints re always passed by 
value, so
 you cannot write a method like swap(a, b) to swap them. Just use a 
temp
 var.
  
 - Gordon
 
 
 
 From: flexcoders@yahoogroups.com 
[mailto:[EMAIL PROTECTED] On
 Behalf Of Reid Priedhorsky
 Sent: Thursday, August 30, 2007 9:41 AM
 To: flexcoders@yahoogroups.com
 Subject: [flexcoders] Re: swap two variables without temporary 
variable
 
 
 
 carlos m wrote:
  Hey Reid,
  
  Don't think there is a built-in function that will do that for
  you--not that I am aware of anyway--but there is definitely a way 
of
  implementing it! Check out this blog post by the awesome guys at
  polygonal:
  http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/
 http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-
math/  .
  Specifically, check out the section labeled Swap integers without
  temporary variables using XOR.
  
  Lot's of goodies on that blog!
  
  Cliffnotes: 
  var t:int = a;
  a = b;
  b = t;
  
  //equals: 
  a ^= b;
  b ^= a;
  a ^= b;HTH!
 
 Hi Carlos,
 
 Thanks for the note -- I suppose I should have been more clear. I 
meant,
 
 how to swap two variables in one line: it's not about speed but 
rather 
 clarity. :)
 
 Reid