Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 14, 2003 The 'interchange' operator | ||||
---|---|---|---|---|
| ||||
I just heard about D, and I read that new features can still be added, so I thought I'd suggest an 'interchange' operator (for example, ><), that exhanges the two operands, such that a><b is equivalent to aux=a; a=b; b=aux. It seemed useful to me on many ocasions, it is very easy to implement, and it makes code simpler to write and read. It is also quicker (it elimnates the need for aux and, for references, it can be implemented with two xor's). So, why isn't there one yet? Anyway, I've been looking for a few minutes and couldn't find any place better to send this suggestion, so if you guys now where it should go, please forward it there. |
January 19, 2003 Re: The 'interchange' operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to bodan | a couple of us in another thread have stated a desire for a tuple construct in order to allow multiple return values from a function. the reason i mention it is that such a construct would also allow you to exchange values w/out adding a new operator:
a,b = b,a
btw, this is blatently plagerized from python (which may well have stole it from an earlier source ;-)
scott
On Tue, 14 Jan 2003 12:19:44 +0000, boda wrote:
> I just heard about D, and I read that new features can still be added, so I thought I'd suggest an 'interchange' operator (for example, ><), that exhanges the two operands, such that a><b is equivalent to aux=a; a=b; b=aux. It seemed useful to me on many ocasions, it is very easy to implement, and it makes code simpler to write and read. It is also quicker (it elimnates the need for aux and, for references, it can be implemented with two xor's). So, why isn't there one yet?
>
> Anyway, I've been looking for a few minutes and couldn't find any place better to send this suggestion, so if you guys now where it should go, please forward it there.
|
January 21, 2003 Re: The 'interchange' operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Scott Pigman | Scott Pigman wrote:
> a couple of us in another thread have stated a desire for a tuple
> construct in order to allow multiple return values from a function. the
> reason i mention it is that such a construct would also allow you to
> exchange values w/out adding a new operator:
>
> a,b = b,a
>
> btw, this is blatently plagerized from python (which may well have stole
> it from an earlier source ;-)
I believe that you can do something like that in perl with lists...
|
January 22, 2003 Re: The 'interchange' operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Scott Pigman | "Scott Pigman" <scottpig1@attbi.com> writes:
> a couple of us in another thread have stated a desire for a tuple construct in order to allow multiple return values from a function. the reason i mention it is that such a construct would also allow you to exchange values w/out adding a new operator:
>
> a,b = b,a
While this form might look elegant, it
- hides the creation of a temporary value and makes the compiler
responsible for creating one so that it would work properly
- forces you to write the expressions a and b twice (-> redundancy)
- is actually longer to type than "swap(a,b)"
-Antti
|
January 22, 2003 Re: The 'interchange' operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Antti Sykari | On Wed, 22 Jan 2003 11:46:51 +0200, Antti Sykari wrote:
> "Scott Pigman" <scottpig1@attbi.com> writes:
>
>> a couple of us in another thread have stated a desire for a tuple construct in order to allow multiple return values from a function. the reason i mention it is that such a construct would also allow you to exchange values w/out adding a new operator:
>>
>> a,b = b,a
>
> While this form might look elegant, it
>
> - hides the creation of a temporary value and makes the compiler responsible for creating one so that it would work properly - forces you to write the expressions a and b twice (-> redundancy) - is actually longer to type than "swap(a,b)"
>
> -Antti
the original post was a request for an interchange operator, which to the best of my knowledge would also create a temporary variable that the compiler would be responsible for. i was just pointing out that the tuple idea brought up elsewhere would eliminate the need for a special operator used just for swapping.
as for swap(a,b) - are functions pass-by-reference or pass-by-value? and
couldn't swap(a,b){ a,b = b,a;} be a possible implementation of it <g>?
-s
|
January 22, 2003 Re: The 'interchange' operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Scott Pigman | "Scott Pigman" <scottpig@some.where.com> escreveu na mensagem news:b0mbtg$17nr$1@digitaldaemon.com... > On Wed, 22 Jan 2003 11:46:51 +0200, Antti Sykari wrote: > > > "Scott Pigman" <scottpig1@attbi.com> writes: > > > >> a couple of us in another thread have stated a desire for a tuple construct in order to allow multiple return values from a function. the reason i mention it is that such a construct would also allow you to exchange values w/out adding a new operator: > >> > >> a,b = b,a > > > > While this form might look elegant, it > > > > - hides the creation of a temporary value and makes the compiler responsible for creating one so that it would work properly - forces you to write the expressions a and b twice (-> redundancy) - is actually longer to type than "swap(a,b)" > > > > -Antti > > > the original post was a request for an interchange operator, which to the best of my knowledge would also create a temporary variable that the compiler would be responsible for. i was just pointing out that the tuple idea brought up elsewhere would eliminate the need for a special operator used just for swapping. > > as for swap(a,b) - are functions pass-by-reference or pass-by-value? and > couldn't swap(a,b){ a,b = b,a;} be a possible implementation of it <g>? > > -s Given ints a and b, one can swap them without using a temporary variable: b = a + b; a = b - a; b = b - a; The same thing goes to anything else, because you just need to cast them back and forth to ints. So an "interchange" operator can be defined without using temporary variables. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.443 / Virus Database: 248 - Release Date: 10/1/2003 |
January 22, 2003 Re: The 'interchange' operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Yokomiso | Daniel Yokomiso wrote:
> Given ints a and b, one can swap them without using a temporary variable:
>
> b = a + b;
> a = b - a;
> b = b - a;
What's the sanity of this? This code does the following:
- load a, b into registers;
- perform stoopid math;
- save registers into variables.
Note, if the variables are already in the registers, they can be exchanged with 1 asm command. Or not exchanged at all, but accounted for at further operation and when saving them later.
If both are in memory, they have to be simply loaded and saved, and the rest is not requiered.
Obviously, there is no C operation which maps to the exchange of two variables in a normal manner, but when you use a temporary even a simple compiler (LCC!) detects it and makes the most optimal decision, which mathematical wizardry is not.
However, it would be sweet not to specify the variable explicitly. Or to be able to write an exchange elegantly and legibly in one line using tuples, which also have a number of other uses.
-i.
|
January 22, 2003 Re: The 'interchange' operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | > > Given ints a and b, one can swap them without using a temporary variable: > > > > b = a + b; > > a = b - a; > > b = b - a; > > What's the sanity of this? This code does the following: > > - load a, b into registers; > - perform stoopid math; > - save registers into variables. > the usual form is a = a ^ b; b = a ^ b; a = a ^ b; it's use is to swap to cpu registers without a 3rd or a store+load but otherwise its pointless most cpus have a swap op these days x86 has reg, mem exchange anyway (though it does lock) Arm has reg, [reg] exchange I'm sure PPC and MIPS have similar. |
January 22, 2003 Re: The 'interchange' operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | "Ilya Minkov" <midiclub@8ung.at> escreveu na mensagem news:b0mrht$1i24$1@digitaldaemon.com... > Daniel Yokomiso wrote: > > > Given ints a and b, one can swap them without using a temporary variable: > > > > b = a + b; > > a = b - a; > > b = b - a; > > What's the sanity of this? This code does the following: > > - load a, b into registers; > - perform stoopid math; > - save registers into variables. > It's not intended to be sane, fast or easy to understand, but it doesn't use ANY temporary variables. It's just to prove that it can be written without extra storage. I'm not saying that D should do this, or that this is good programming. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.443 / Virus Database: 248 - Release Date: 10/1/2003 |
January 22, 2003 Re: The 'interchange' operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Wynn | Mike Wynn wrote:
> the usual form is
> a = a ^ b;
> b = a ^ b;
> a = a ^ b;
This math is not better, IMO. Same 3 operations, all of them atomic (1 m-op), and execute in any pipe. So on intel, and all the others shouldn't be very different.
And when doing such things, originality matters more, since the *usual* way would be to swap registers with a dedicated command!
|
Copyright © 1999-2021 by the D Language Foundation