integer max, min?

downs default_357-line at yahoo.de
Tue Oct 2 08:31:20 PDT 2007


BLS wrote:
> Paolo Invernizzi schrieb:
>> Silly question...
>>
>> Where are in Phobos the max and min for integers?
>>
>> Thanks, Paolo
> Have a look at :
> 
> http://www.digitalmars.com/d/property.html
> 
> HTH Bjoern
Or if you mean the other max/min ...

template supertype(T...) {
  static assert (T.length);
  static if (T.length==1)
    alias T[0] supertype;
  else static if (T.length==2)
    alias typeof(T[0].init+T[1].init) supertype;
  else
    alias supertype!(
      typeof(T[0].init+T[1].init),
      T[2..$]
    ) supertype;
}

supertype!(T) max(T...)(T t) {
  static assert(T.length);
  supertype!(T) res=t[0];
  foreach (v; t[1..$]) if (v>res) res=v;
  return res;
}

supertype!(T) min(T...)(T t) {
  static assert(T.length);
  supertype!(T) res=t[0];
  foreach (v; t[1..$]) if (v<res) res=v;
  return res;
}

That should work. Have fun!
 --downs


More information about the Digitalmars-d-learn mailing list