www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template function : use the array's one !

reply TSalm <TSalm free.fr> writes:
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
Feb 17 2009
next sibling parent reply BCS <ao pathlink.com> writes:
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
Feb 17 2009
parent reply TSalm <TSalm free.fr> writes:
 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 ?
Feb 17 2009
parent reply BCS <none anon.com> writes:
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 }
Feb 17 2009
parent TSalm <TSalm free.fr> writes:
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.
Feb 17 2009
prev sibling parent reply John C <johnch_atms hotmail.com> writes:
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
Feb 18 2009
parent TSalm <TSalm free.fr> writes:
 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.
Feb 18 2009