Thread overview
Template function : use the array's one !
Feb 17, 2009
TSalm
Feb 17, 2009
BCS
Feb 17, 2009
TSalm
Feb 17, 2009
BCS
Feb 17, 2009
TSalm
Feb 18, 2009
John C
Feb 18, 2009
TSalm
February 17, 2009
Hello,

I'm searching to have a generic comparator function.

I done this :

/* ------------ CODE -------------------- */
import tango.io.Stdout;

/***
 * Not for arrays
 ***/
int compare(T)(T o1,T o2)
{
  static if  ( is( T  bar == class )  || is(  T bar == struct )  )
    return o1.opCmp(o2);

  else
    return o2 - o1;
}

/***
 * For arrays
 ***/
int compare(T:T[])(T[] o1,T[] o2)
{
  size_t minLength = ( o1.length<o2.length ? o1.length : o2.length) ;

  for (size_t i=0;i<minLength;i++)
  {
    int result = compare(o1[i],o2[i]);

    if (result != 0)
      return result;
  }

  return o2.length - o1.length ;

}

/***
 * Test
 ***/
void main()
{
  byte[] a = [10,12,13];
  byte[] b = [15,16,17];
  Stdout( compare(a,b) ).newline ;
}
/* ------------ END CODE ---------------- */



But this code return a compilation error :
test_cmp.d(12): Error: cannot implicitly convert expression (o2 - o1) of type byte[] to int
test_cmp.d(41): template instance test_cmp.compare!(byte[]) error instantiating

It seems it's the first compare function (without array in parameter) which is hook.

How can I declare this compare function to use specific code to compare arrays ?
Thanks in advance,
TSalm
February 17, 2009
Reply to TSalm,

> Hello,
> 
> I'm searching to have a generic comparator function.
> 
> I done this :
> 
[...]

> 
> How can I declare this compare function to use specific code to
> compare
> arrays ?
> Thanks in advance,
> TSalm

take a look at static if and is

http://www.digitalmars.com/d/1.0/version.html#staticif
http://www.digitalmars.com/d/1.0/expression.html#IsExpression


February 17, 2009
> take a look at static if and is
>
> http://www.digitalmars.com/d/1.0/version.html#staticif
> http://www.digitalmars.com/d/1.0/expression.html#IsExpression
>
Thanks for this links.
But I don't see anything about how to test if it's an array or not...
Is it not possible ?

February 17, 2009
Hello TSalm,

>> take a look at static if and is
>> 
>> http://www.digitalmars.com/d/1.0/version.html#staticif
>> http://www.digitalmars.com/d/1.0/expression.html#IsExpression
>> 
> Thanks for this links.
> But I don't see anything about how to test if it's an array or not...
> Is it not possible ?

look at point 5

IIRC:

static if(is(T B : B[]))
{
   // if T is an array, this is used and B is the element type
}


February 17, 2009
Le Tue, 17 Feb 2009 23:03:04 +0100, BCS <none@anon.com> a écrit:

> Hello TSalm,
>
>>> take a look at static if and is
>>>  http://www.digitalmars.com/d/1.0/version.html#staticif
>>> http://www.digitalmars.com/d/1.0/expression.html#IsExpression
>>>
>> Thanks for this links.
>> But I don't see anything about how to test if it's an array or not...
>> Is it not possible ?
>
> look at point 5
>
> IIRC:
>
> static if(is(T B : B[]))
> {
>     // if T is an array, this is used and B is the element type
> }
>
>


Incredible !
Thanks a lot BCS.
February 18, 2009
TSalm Wrote:

> Hello,
> 
> I'm searching to have a generic comparator function.
> 
> I done this :
> 
> /* ------------ CODE -------------------- */
> import tango.io.Stdout;
> 
> /***
>   * Not for arrays
>   ***/
> int compare(T)(T o1,T o2)
> {
>    static if  ( is( T  bar == class )  || is(  T bar == struct )  )
>      return o1.opCmp(o2);
> 
>    else
>      return o2 - o1;
> }
> 
> /***
>   * For arrays
>   ***/
> int compare(T:T[])(T[] o1,T[] o2)

Change this line to:
  int compare(T:T[])(T o1, T o2)

> {
>    size_t minLength = ( o1.length<o2.length ? o1.length : o2.length) ;
> 
>    for (size_t i=0;i<minLength;i++)
>    {
>      int result = compare(o1[i],o2[i]);
> 
>      if (result != 0)
>        return result;
>    }
> 
>    return o2.length - o1.length ;
> 
> }
> 
> /***
>   * Test
>   ***/
> void main()
> {
>    byte[] a = [10,12,13];
>    byte[] b = [15,16,17];
>    Stdout( compare(a,b) ).newline ;
> }
> /* ------------ END CODE ---------------- */
> 
> 
> 
> But this code return a compilation error :
> test_cmp.d(12): Error: cannot implicitly convert expression (o2 - o1) of
> type byte[] to int
> test_cmp.d(41): template instance test_cmp.compare!(byte[]) error
> instantiating
> 
> It seems it's the first compare function (without array in parameter) which is hook.
> 
> How can I declare this compare function to use specific code to compare
> arrays ?
> Thanks in advance,
> TSalm

February 18, 2009
> TSalm Wrote:
>

>> int compare(T:T[])(T[] o1,T[] o2)
>
> Change this line to:
>   int compare(T:T[])(T o1, T o2)
>

You are right.

But despite this function, at compile time, an error is return :

.\src\tsalm\tools\Generic.d(20): Error: cannot implicitly convert expression (o2 - o1) of type int[3u] to int
.\src\tsalm\tools\Generic.d(33): template instance tsalm.tools.generic.compare!(int[3u]) error instantiating
Command C:\DMD\dsss\bin\rebuild.exe returned with code 1, aborting.
Error: Command failed, aborting.