Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Intrusive doubly linked list: list 介入式雙鏈表:list

list hooks  list 鉤子
list container  list 容器
Example 例子

list is a doubly linked list. The memory overhead it imposes is 2 pointers per node. An empty, non constant-time size list also has the size of 2 pointers. list has many more constant-time operations than slist and provides a bidirectional iterator. It is recommended to use list instead of slist if the size overhead is acceptable:
list 是一個雙鏈表。它所帶來的內存開銷是每節點2個指針。一個空的、不帶常量時間 size 的 list 的大小為2個指針。list 具有比 slist 更多的常量時間操作,且提供雙向迭代器。如果空間代價可以接受,建議使用 list 而不是 slist。 

Like the rest of Boost.Intrusive containers, list has two hook types:
和其它 Boost.Intrusive 容器一樣,list 有兩種鉤子:

template <class ...Options>
class list_base_hook;

template <class ...Options>
class list_member_hook;

list_base_hook and list_member_hook receive the same options explained in the section How to use Boost.Intrusive:
list_base_hook
list_member_hook 接受相同的選項,在 如何使用 Boost.Intrusive 一節中說明:

  • tag<class Tag> (for base hooks only): This argument serves as a tag, so you can derive from more than one list hook. Default: tag<default_tag>.
    tag<class Tag> (只用於基類鉤子):該參數作為一個標記,你可以派生自多個 slist 鉤子。缺省值:tag<default_tag>.
  • link_mode<link_mode_type LinkMode>: The linking policy. Default: link_mode<safe_link>.
    link_mode<link_mode_type LinkMode>: 鏈接策略。缺省值:link_mode<safe_link>.
  • void_pointer<class VoidPointer>: The pointer type to be used internally in the hook and propagated to the container. Default: void_pointer<void*>.
    void_pointer<class VoidPointer>: 在鉤子內部使用並被傳遞給容器的指針類型。缺省值:void_pointer<void*>.

template <class T, class ...Options>
class list;

list receives the same options explained in the section How to use Boost.Intrusive:
list 接受以下選項,具體解釋在 如何使用 Boost.Intrusive 一節中: 

  • base_hook<class Hook> / member_hook<class T, class Hook, Hook T::* PtrToMember> / value_traits<class ValueTraits>: To specify the hook type or value traits used to configure the container. (To learn about value traits go to the section Containers with custom ValueTraits.)
    base_hook<class Hook> / member_hook<class T, class Hook, Hook T::* PtrToMember> / value_traits<class ValueTraits>: 指定用於配置容器的鉤子類型或 value traits。(要學習有關 value traits 的知識,請見 帶定制化 ValueTraits 的容器)
  • constant_time_size<bool Enabled>: To activate the constant-time size() operation. Default: constant_time_size<true>
    constant_time_size<bool Enabled>: 激活常量時間的 size() 操作。缺省值:constant_time_size<true>
  • size_type<bool Enabled>: To specify the type that will be used to store the size of the container. Default: size_type<std::size_t>.
    size_type<bool Enabled>: 指定用於保存容器大小的類型。缺省值:size_type<std::size_t>.

Now let's see a small example using both hooks:
現在我們來看一個使用兩種鉤子的小例子:

#include <boost/intrusive/list.hpp>
#include <vector>

using namespace boost::intrusive;

class MyClass : public list_base_hook<>   //This is a derivation hook 這是一個派生鉤子
{ int int_; public: //This is a member hook 這是一成員鉤子
list_member_hook<> member_hook_; MyClass(int i) : int_(i) {} }; //Define a list that will store MyClass using the public base hook
typedef list<MyClass> BaseList; //Define a list that will store MyClass using the public member hook
typedef list< MyClass , member_hook< MyClass, list_member_hook<>, &MyClass::member_hook_>
> MemberList; int main() { typedef std::vector<MyClass>::iterator VectIt; typedef std::vector<MyClass>::reverse_iterator VectRit; //Create several MyClass objects, each one with a different value
std::vector<MyClass> values; for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); BaseList baselist; MemberList memberlist; //Now insert them in the reverse order in the base hook list
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it) baselist.push_front(*it); //Now insert them in the same order as in vector in the member hook list
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it) memberlist.push_back(*it); //Now test lists
{ BaseList::reverse_iterator rbit(baselist.rbegin()), rbitend(baselist.rend()); MemberList::iterator mit(memberlist.begin()), mitend(memberlist.end()); VectIt it(values.begin()), itend(values.end()); //Test the objects inserted in the base hook list
for(; it != itend; ++it, ++rbit) if(&*rbit != &*it) return 1; //Test the objects inserted in the member hook list
for(it = values.begin(); it != itend; ++it, ++mit) if(&*mit != &*it) return 1; } return 0; }


PrevUpHomeNext