P
Peyman
Hi, I was reading the source code of an implementation of STL by HP
company 1994, and Silicon Graphics Computer Systems, Inc. 1996, 1997.
Which I found that for implementing a Linked List, they have used the
following two structures:
struct _List_node_base {
_List_node_base* _M_next;
_List_node_base* _M_prev;
};
template <class _Tp>
struct _List_node : public _List_node_base {
_Tp _M_data;
};
and then everywhere in the rest of the code, for example for the
iterator class, they only use the node_base, while for the list class
itself, they use node, not the node_base, and then they keep casting
these two structs to retrieve the data stored in node and so on! I
found this technique in almost all of the other data structures they've
implemented there, like the "RD_Tree" for "map". I can't understand why
they're dividing a node into two structs, while it could be only a
simple node containing both pointers and data, and then never need to
cast them, so of course faster! and easier! so I thought there must be
a good reason! Anyone knows why!?
And one more thing, what's the reason for using this much
"underscore"!!! I know it can be only a style! but is there any
specific reason!?
BTW, I know that the implementation is old fashioned but I still can
learn from it! and for more info. here is the link to this
implementation:
http://www.sgi.com/tech/stl/
company 1994, and Silicon Graphics Computer Systems, Inc. 1996, 1997.
Which I found that for implementing a Linked List, they have used the
following two structures:
struct _List_node_base {
_List_node_base* _M_next;
_List_node_base* _M_prev;
};
template <class _Tp>
struct _List_node : public _List_node_base {
_Tp _M_data;
};
and then everywhere in the rest of the code, for example for the
iterator class, they only use the node_base, while for the list class
itself, they use node, not the node_base, and then they keep casting
these two structs to retrieve the data stored in node and so on! I
found this technique in almost all of the other data structures they've
implemented there, like the "RD_Tree" for "map". I can't understand why
they're dividing a node into two structs, while it could be only a
simple node containing both pointers and data, and then never need to
cast them, so of course faster! and easier! so I thought there must be
a good reason! Anyone knows why!?
And one more thing, what's the reason for using this much
"underscore"!!! I know it can be only a style! but is there any
specific reason!?
BTW, I know that the implementation is old fashioned but I still can
learn from it! and for more info. here is the link to this
implementation:
http://www.sgi.com/tech/stl/