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

BLS nanali at nospam-wanadoo.fr
Thu Jul 26 16:15:38 PDT 2007


Hi Chris,
MANY THANKS.
Your help is kicking me into the right direction.

I am pretty sure that you (as nice guy and brainiac) will translate the 
the C++ implementation  within 15 minutes into D, but for me (as 
Modula/Oberon fragment) this is a challenge.
I guess that I will have a lot of more questions, (so be prepared <g>) 
For the moment I prefer to find out as much as I can by myself because 
this is my way to learn. However:
you wrote :
 > 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.

Yes, I mean private inner classes - instead of the shown structs, but 
why should I declare them *static* ? I mean the inner classes should 
belong to the class-instance. Am I wrong ?

In case that you have time enough:  Here the /templated/ B+Tree Cpp 
implementation. 
http://idlebox.net/2007/stx-btree/stx-btree-0.8/include/stx/btree.h.html

Read it ? Do you see something not doable in D ?
again, thanks for beeing so gentle and patient
Bjoern

http://idlebox.net/2007/stx-btree/stx-btree-0.8/include/stx/btree.h.html


Chris Nicholson-Sauls schrieb:
> 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