This section will expand the explanation of previously presented basic concepts
before explaining the customization options of Boost.Intrusive.

Node Algorithms: A set of static functions
that implement basic operations on a group of nodes: initialize a node,
link_mode_type a node to a group of nodes, unlink a node from another group
of nodes, etc. For example, a circular singly linked list is a group of
nodes, where each node has a pointer to the next node. Node
Algorithms just require a NodeTraits
template parameter and they can work with any NodeTraits
class that fulfills the needed interface. As an example, here is a class
that implements operations7' to manage a group of nodes forming a circular
singly linked list:

template<classNodeTraits>structmy_slist_algorithms{typedeftypenameNodeTraits::node_ptrnode_ptr;typedeftypenameNodeTraits::const_node_ptrconst_node_ptr;//Get the previous node of "this_node"staticnode_ptrget_prev_node(node_ptrthis_node){node_ptrp=this_node;while(this_node!=NodeTraits::get_next(p))p=NodeTraits::get_next(p);returnp;}// number of elements in the group of nodes containing "this_node"staticstd::size_tcount(const_node_ptrthis_node){std::size_tresult=0;const_node_ptrp=this_node;do{p=NodeTraits::get_next(p);++result;}while(p!=this_node);returnresult;}// More operations// ...};

Node Traits: A class that encapsulates
the basic information and operations on a node within a group of nodes:
the type of the node, a function to obtain the pointer to the next node,
etc. Node Traits specify the configuration
information Node Algorithms need. Each
type of Node Algorithm expects an interface
that compatible Node Traits classes must
implement. As an example, this is the definition of a Node
Traits class that is compatible with the previously presented
my_slist_algorithms:

structmy_slist_node_traits{//The type of the nodestructnode{node*next_;};typedefnode*node_ptr;typedefconstnode*const_node_ptr;//A function to obtain a pointer to the next nodestaticnode_ptrget_next(const_node_ptrn){returnn->next_;}//A function to set the pointer to the next nodestaticvoidset_next(node_ptrn,node_ptrnext){n->next_=next;}};

Hook: A class that the user must add as
a base class or as a member to his own class to make that class insertable
in an intrusive container. Usually the hook contains a node object that
will be used to form the group of nodes: For example, the following class
is a Hook that the user can add as a base
class, to make the user class compatible with a singly linked list container:

classmy_slist_base_hook//This hook contains a node, that will be used//to link the user object in the group of nodes:privatemy_slist_node_traits::node{typedefmy_slist_node_traits::node_ptrnode_ptr;typedefmy_slist_node_traits::const_node_ptrconst_node_ptr;//Converts the generic node to the hookstaticmy_slist_base_hook*to_hook_ptr(node_ptrp){returnstatic_cast<my_slist_base_hook*>(p);}//Returns the generic node stored by this hooknode_ptrto_node_ptr(){returnstatic_cast<node*const>(this);}// More operations// ...};//To make MyClass compatible with an intrusive singly linked list//derive our class from the hook.classMyClass:publicmy_slist_base_hook{voidset(intvalue);intget()const;private:intvalue_;};

Intrusive Container: A container that
offers a STL-like interface to store user objects. An intrusive container
can be templatized to store different value types that use different hooks.
An intrusive container is also more elaborate than a group of nodes: it
can store the number of elements to achieve constant-time size information,
it can offer debugging facilities, etc. For example, an slist
container (intrusive singly linked list) should be able to hold MyClass objects that might have decided
to store the hook as a base class or as a member. Internally, the container
will use Node Algorithms to implement
its operations, and an intrusive container is configured using a template
parameter called ValueTraits. ValueTraits will contain the information to convert
user classes in nodes compatible with Node Algorithms.
For example, this a possible slist
implementation:

template<classValueTraits,...>classslist{public:typedeftypenameValueTraits::value_typevalue_type;//More typedefs and functions// ...//Insert the value as the first element of the listvoidpush_front(referencevalue){node_ptrto_insert(ValueTraits::to_node_ptr(value));circular_list_algorithms::link_after(to_insert,get_root_node());}// More operations// ...};

Semi-Intrusive Container: A semi-intrusive
container is similar to an intrusive container, but apart from the values
to be inserted in the container, it needs additional memory (for example,
auxiliary arrays or indexes).

Value Traits: As we can see, to make our
classes intrusive-friendly we add a simple hook as a member or base class.
The hook contains a generic node that will be inserted in a group of nodes.
Node Algorithms just work with nodes and
don't know anything about user classes. On the other hand, an intrusive
container needs to know how to obtain a node from a user class, and also
the inverse operation. So we can define ValueTraits
as the glue between user classes and nodes required by Node
Algorithms. Let's see a possible implementation of a value traits
class that glues MyClass and the node stored in the hook:

structmy_slist_derivation_value_traits{public:typedefslist_node_traitsnode_traits;typedefMyClassvalue_type;typedefnode_traits::node_ptrnode_ptr;typedefvalue_type*pointer;//...//Converts user's value to a generic nodestaticnode_ptrto_node_ptr(referencevalue){returnstatic_cast<slist_base_hook&>(value).to_node_ptr();}//Converts a generic node into user's valuestaticvalue_type*to_value_ptr(node_traits::node*n){static_cast<value_type*>(slist_base_hook::to_hook_ptr(n));}// More operations// ...};