C++/ D C struct2 D inner classes, C++ templates/D templates

Chris Nicholson-Sauls ibisbasenji at gmail.com
Thu Jul 26 13:34:26 PDT 2007


BLS wrote:
> Hi :
> it is about translating a C++ B+Tree algoritm into D.
> Question No1 <g>
> #include <algorithm>
> #include <functional>
> etc. and so on
> 
> *The C++* source looks like :
> template <typename _Key, typename _Data,
>           typename _Value = std::pair<_Key, _Data>,
>           typename _Compare = std::less<_Key>,
>           typename _Traits = btree_default_map_traits<_Key, _Data>,
>           bool _Duplicates = false>
> 
> class bptree
> {
> public:
>     typedef _Key                        key_type;
> 
>   //etc, and so on
> 
> private :
>   struct node
>   {
>     //contains somethink like
>     inline void initialize(const unsigned short l)
>     {
>       level = l;
>       slotuse = 0;
>     }
>   struct inner_node : public node
>   {
>   }
> 
> 
> }
> 
> 
> *IN D*, I would like to use adaptor classes instead of structs -> struct 
> inheritance
> 
> class BplusTtree(_Key, _Data, and so on, bool _Duplicates=false)
> {
>   class node(){}
>   // and
>   class inner_node : node{}
> 
> }
> 
> Is this legal ?

If you mean just "are inner classes" legal, then yes.  In the way you 
show above, the class will belong to a given class instance/object.  If 
you prefix them with 'static' they will belong to the class (aka 
template instance, in this case).

If you meant something else in particular... then I'm not sure what you 
meant.  :)

> No2 :
> The code contains std::pairs, (for key, value pairs)
> What is the fasted (execution-speed) way to implement this in D ?

Associative array.  I'm not sure if there's any difference between AA's 
in D/1.x versus D/2.x, so I'll link to both.
http://digitalmars.com/d/1.0/arrays.html#associative
http://digitalmars.com/d/arrays.html#associative

> No3 :
> C++ *prepare_cast, static_cast* just cast(whatever) in D ?

Yes, just cast(whatever).

> Sorry for beeing so ignorant but I just have basic C++ knowhow, and I am 
>  still learning about D templates. Many thanks in advance
> Bjoern
> 
> 
> 

No apologies necessary.

-- Chris Nicholson-Sauls


More information about the Digitalmars-d-learn mailing list