Templates lots of newbie qs.

Daniel Keep daniel.keep.lists at gmail.com
Thu Mar 8 07:39:49 PST 2007


Chris Warwick wrote:
> Ok, never used templates before but i think i have the basic idea. Just have 
> a few questions and would welcome any corrections / sugestions on way's to 
> improve this, my first template.. (surprisingly had very little trouble 
> writing this and it compiling, not sure about working tho')
> 
> class vector(T)
> {
> 
>     // fitems will be intialized to default right? So no need for a default 
> constructor?
> 
>     T[] fitems;

All types in D are initialised to a "sane" default if not given an
explicit initialiser.

So, yes.

>     this(T[] source)
>     {
>         fitems = source.dup;
>     }
> 
>     // Am i right in thinking that vector!(int) is a distinct type from T[], 
> so how do you pass
>     // a template instance of the same time to the constructor? Somthing 
> like this???
>     // compiles but im not sure if its right..
> 
>     this(vector!(T) source)
>     {
>         fitems = source.fitems.dup;
>     }

Yup, that looks good.

>     void append(T item)
>     {
>         fitems ~= item;
>     }
> 
>     bool remove(T item)    // returns true if item removed
>     {
>         int i = indexOf(item);
>         if (i >= 0)
>         {
>             fitems[i..fitems.length-1] = fitems[i+1..fitems.length];
>             fitems.length = fitems.length-1;
>             return true;
>         }
>         else
>         {
>             return false;
>         }
>     }
> 
>     int indexOf(T item)    // returns -1 if not found
>     {
>         for (int i = 0; i < fitems.length; i++)
>         {
>             if (fitems[i] == item) { return i; }
>         }
>         return -1;
>     }
> 
>     int length()
>     {
>         return fitems.length;
>     }
> 
>     // Do i have the opIndex / opIndexAssign correct?
>     // Is there anyway to localy control whether bounds checking is on, so i 
> can turn it
>     // on or off for just this module?
> 
>     T opIndex(int i)
>     {
>         return fitems[i];
>     }
> 
>     void opIndexAssign(T item, int index)
>     {
>         fitems[index] = item;
>     }

Yup; looks mostly right.  You can't locally control bounds-checking.
Bounds checking is disabled by throwing the -release compiler switch.

Incidentally, you should use size_t for indices; it is an unsigned
integer type that spans the address-space.

> }
> 
> And are class templates value types or referances types?

They are reference types, just like un-templated classes.

> How well optimized is stuff like this? Will the opIndex/Assign optimize down 
> to similar speed as using a basic array?

Probably not since your opIndex and opIndexAssign are virtual methods;
they can be overridden in subclasses, which makes them difficult to inline.

Best bet?  Write a simple benchmark, compile with -release -inline, and
change opIndex/opIndexAssign to "final" methods, if you aren't going to
want to override those methods.

> For basic array stuff like this would it be better to use the "Functions as 
> Array Properties" feature? Can that be templatized? Can you writed a generic 
> function that takes an array as the first param.. Somthing like this...
> 
> template ArrayProps(T)
> {
>      int indexOf(T[] arr, T item)
>     {
>         for (int i = 0; i < arr.length; i++)
>         {
>             if (arr[i] == item) { return i; }
>         }
>         return -1;
>     }
> }
> 
> ArrayPros(int);

This line above is wrong.  It should be:

mixin ArrayProps!(int);

> int[] foo = [0,1,2,3,4];
> int ibetthisdontwork = foo.indexOf(3);

Apart from that, it should work fine.  Has anyone tried something like
this lately:

int indexOf(T)(inout T[] arr, T item)
{
    foreach( i, e ; arr )
        if( e == item ) return i;
    return -1;
}

> many thanks,
> 
> chris 

No problem; hope this helps.

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/


More information about the Digitalmars-d-learn mailing list