|
Archives
D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger
C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows
digitalmars.empire
digitalmars.DMDScript
|
D - swap
↑ ↓ ← → "Pavel Minayev" <evilone omen.ru> writes:
Since there are no templates in D, I believe there's
no place for my lovely swap()... what about a replacement?
Something built into language - which'd also make possible
for the compiler to optimize swaps (for example, generate
just one opcode if both operands are in registers). What
do you think?
As for the syntax, my suggestion would be "><":
RECT a, b;
a >< b; // swap a and b (and return new a?)
It was in Sphinx C--, if somebody still remembers it =)
↑ ↓ ← → Russell Borogove <kaleja estarcion.com> writes:
Pavel Minayev wrote:
Since there are no templates in D, I believe there's
no place for my lovely swap()... what about a replacement?
Something built into language - which'd also make possible
for the compiler to optimize swaps (for example, generate
just one opcode if both operands are in registers). What
do you think?
As for the syntax, my suggestion would be "><":
RECT a, b;
a >< b; // swap a and b (and return new a?)
It was in Sphinx C--, if somebody still remembers it =)
I'd second that proposal, if only to keep people from showing
off their 3-xor swap. :) Finally, a use for angle brackets
besides less-than and greater-than that I can accept!
↑ ↓ ← → "Sean L. Palmer" <spalmer iname.com> writes:
I agree, there's some really common basic things that programmers always end
up needing, very low level, such as:
swap(a,b) // swaps a and b
and for all numeric types:
min(val,maxv) // smaller of val and maxv
max(val,minv) // larger of val and minv
clamp(val,minv,maxv) // same as min(maxv,max(minv,val))
and for signed types:
abs(val) // absolute value of val
Alot of these should be in the standard D library. Let's please not repeat
the C min/max fiasco. The min and max #defines in <windows.h> really suck.
The problem is, without templates we have no easy way of declaring them for
all types. Consider even this simple case:
int min(int a, int b) { if (a<b) return a; return b; } // notice there are
no type conversions in this function
{
typedef int int2;
int2 x=int2(0),y=int2(1);
int2 n = min(x,y); // compile error - no overload for min(int2, int2)
}
At least all these except swap *could* be implemented in the standard
library. I guess swap could too; I suppose we could provide swap function
overloads for any class we wanted to be swappable aside from the basic
types.
If templates ever get added to the language, I sure hope either you don't
add the >< operator or that templates don't use < or > at all.
Sean
"Russell Borogove" <kaleja estarcion.com> wrote in message
news:3BF6D8CF.5DF85A8F estarcion.com...
Pavel Minayev wrote:
Since there are no templates in D, I believe there's
no place for my lovely swap()... what about a replacement?
Something built into language - which'd also make possible
for the compiler to optimize swaps (for example, generate
just one opcode if both operands are in registers). What
do you think?
As for the syntax, my suggestion would be "><":
RECT a, b;
a >< b; // swap a and b (and return new a?)
It was in Sphinx C--, if somebody still remembers it =)
I'd second that proposal, if only to keep people from showing
off their 3-xor swap. :) Finally, a use for angle brackets
besides less-than and greater-than that I can accept!
↑ ↓ ← → "Walter" <walter digitalmars.com> writes:
"Sean L. Palmer" <spalmer iname.com> wrote in message
news:9tafnl$1el2$1 digitaldaemon.com...
If templates ever get added to the language, I sure hope either you don't
add the >< operator or that templates don't use < or > at all.
Don't worry, templates will NOT use the < > to bracket parameters. <g>
↑ ↓ ← → Russell Borogove <kaleja estarcion.com> writes:
"Sean L. Palmer" wrote:
I agree, there's some really common basic things that programmers always end
up needing, very low level, such as:
swap(a,b) // swaps a and b
Outside of sorts, I rarely need this, but okay.
and for all numeric types:
min(val,maxv) // smaller of val and maxv
max(val,minv) // larger of val and minv
Yes!
clamp(val,minv,maxv) // same as min(maxv,max(minv,val))
Yes yes YES!
Anyone want to argue the merits of operators for
clamp/min/max rather than functional syntax?
a ={0..255} b + c; // same as a = clamp( b+c, 0, 255 );
a +={0..255} b; // same as a = clamp( a+b, 0, 255 );
Okay, maybe curly brackets is asking for trouble here --
square brackets? Angle brackets? Vertical bars?
a =|0..255| b+c;
a =[0..255] b+c; // looks too much like array slice
a =<0..255> b+c; // why are angle brackets so ugly?
a =(0..255) b+c; // hmmm...
a =\0..255\ b+c; // hey, not too bad
Whichever, this easily
extends to min/max:
a ={..255} b + c; // same as a = min(255,b+c);
a ={0..} d + e; // same as a = max(0,d+e);
Okay, call me crazy.
And while it probably adds more complexity to the D compiler
than anyone wants, I'd love it if the clamp function/operator
could recognize when the clamping range was 0-255 or other
ranges supported by hardware and use MMX, etc.,
automatically.
-RB
↑ ↓ ← → nicO <nicolas.boulay ifrance.com> writes:
Russell Borogove a écrit :
And while it probably adds more complexity to the D compiler
than anyone wants, I'd love it if the clamp function/operator
could recognize when the clamping range was 0-255 or other
ranges supported by hardware and use MMX, etc.,
automatically.
That's why i have propose to used instead of int, short, long :
something like : int /0..255/ i; So you could do the same thing for
every integer type. So the compiler could choose the minimun size for
array. It will be nice to add something about rounding for multimedia
soft (i have follow a discuss about integer IDCT which made bad rounding
and destroy the quality of the image).
For example, int /0..3/ tab[20000000]; will be half the size of char
tab[20000000]; so you will save 10 Mo !
If you want to use short you could make a typedef.
It will be nice to do the same thing for ENUM type. This type should not
be an integer but really a enumeration of "things" (very usefull for
state coding,...).
So the compiler could reduice the size used by an array and better
control affectation.
nicO
-RB
|
|