diff --git a/doc/Jamfile.v2 b/doc/Jamfile.v2 deleted file mode 100644 index 823d326..0000000 --- a/doc/Jamfile.v2 +++ /dev/null @@ -1,55 +0,0 @@ -# Boost.Intrusive library documentation Jamfile -# -# Copyright Ion Gaztaņaga 2006. -# Distributed under the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -# -# See http://www.boost.org/libs/intrusive for documentation. - - -import doxygen ; -import quickbook ; - -doxygen autodoc - : - [ glob ../../../boost/intrusive/*.hpp ] - : - HIDE_UNDOC_MEMBERS=YES - HIDE_UNDOC_MEMBERS=YES - HIDE_UNDOC_CLASSES=YES - EXTRACT_PRIVATE=NO - ENABLE_PREPROCESSING=YES - MACRO_EXPANSION=YES - "PREDEFINED=BOOST_INTRUSIVE_DOXYGEN_INVOKED \\ - "list_impl=list" \\ - "slist_impl=slist" \\ - "set_impl=set" \\ - "multiset_impl=multiset" \\ - "rbtree_impl=rbtree" \\ - "unordered_set_impl=unordered_set" \\ - "unordered_multiset_impl=unordered_multiset" \\ - "hashtable_impl=hashtable" \\ - "splay_set_impl=splay_set" \\ - "splay_multiset_impl=splay_multiset" \\ - "splaytree_impl=splaytree" \\ - "sg_set_impl=sg_set" \\ - "sg_multiset_impl=sg_multiset" \\ - "sgtree_impl=sgtree" \\ - "avl_set_impl=avl_set" \\ - "avl_multiset_impl=avl_multiset" \\ - "avltree_impl=avltree"" - ; - -xml intrusive : intrusive.qbk ; - -boostbook standalone - : - intrusive - : - boost.root=../../../.. - boost.libraries=../../../../libs/libraries.htm - generate.section.toc.level=3 - chunk.first.sections=1 - autodoc - ; diff --git a/doc/html/boostbook.css b/doc/html/boostbook.css deleted file mode 100644 index e816cce..0000000 --- a/doc/html/boostbook.css +++ /dev/null @@ -1,538 +0,0 @@ -/*============================================================================= - Copyright (c) 2004 Joel de Guzman - http://spirit.sourceforge.net/ - - Use, modification and distribution is subject to the Boost Software - License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) -=============================================================================*/ - -/*============================================================================= - Body defaults -=============================================================================*/ - - body - { - margin: 1em; - font-family: sans-serif; - } - -/*============================================================================= - Paragraphs -=============================================================================*/ - - p - { - text-align: left; - font-size: 10pt; - line-height: 1.15; - } - -/*============================================================================= - Program listings -=============================================================================*/ - - /* Code on paragraphs */ - p tt.computeroutput - { - font-size: 10pt; - } - - pre.synopsis - { - font-size: 10pt; - margin: 1pc 4% 0pc 4%; - padding: 0.5pc 0.5pc 0.5pc 0.5pc; - } - - .programlisting, - .screen - { - font-size: 10pt; - display: block; - margin: 1pc 4% 0pc 4%; - padding: 0.5pc 0.5pc 0.5pc 0.5pc; - } - -/*============================================================================= - Headings -=============================================================================*/ - - h1, h2, h3, h4, h5, h6 - { - text-align: left; - margin: 1em 0em 0.5em 0em; - font-weight: bold; - } - - h1 { font: 140% } - h2 { font: bold 140% } - h3 { font: bold 130% } - h4 { font: bold 120% } - h5 { font: italic 110% } - h6 { font: italic 100% } - - /* Top page titles */ - title, - h1.title, - h2.title - h3.title, - h4.title, - h5.title, - h6.title, - .refentrytitle - { - font-weight: bold; - margin-bottom: 1pc; - } - - h1.title { font-size: 140% } - h2.title { font-size: 140% } - h3.title { font-size: 130% } - h4.title { font-size: 120% } - h5.title { font-size: 110% } - h6.title { font-size: 100% } - - .section h1 - { - margin: 0em 0em 0.5em 0em; - font-size: 140%; - } - - .section h2 { font-size: 140% } - .section h3 { font-size: 130% } - .section h4 { font-size: 120% } - .section h5 { font-size: 110% } - .section h6 { font-size: 100% } - - /* Code on titles */ - h1 tt.computeroutput { font-size: 140% } - h2 tt.computeroutput { font-size: 140% } - h3 tt.computeroutput { font-size: 130% } - h4 tt.computeroutput { font-size: 120% } - h5 tt.computeroutput { font-size: 110% } - h6 tt.computeroutput { font-size: 100% } - -/*============================================================================= - Author -=============================================================================*/ - - h3.author - { - font-size: 100% - } - -/*============================================================================= - Lists -=============================================================================*/ - - li - { - font-size: 10pt; - line-height: 1.3; - } - - /* Unordered lists */ - ul - { - text-align: left; - } - - /* Ordered lists */ - ol - { - text-align: left; - } - -/*============================================================================= - Links -=============================================================================*/ - - a - { - text-decoration: none; /* no underline */ - } - - a:hover - { - text-decoration: underline; - } - -/*============================================================================= - Spirit style navigation -=============================================================================*/ - - .spirit-nav - { - text-align: right; - } - - .spirit-nav a - { - color: white; - padding-left: 0.5em; - } - - .spirit-nav img - { - border-width: 0px; - } - -/*============================================================================= - Table of contents -=============================================================================*/ - - .toc - { - margin: 1pc 4% 0pc 4%; - padding: 0.1pc 1pc 0.1pc 1pc; - font-size: 10pt; - line-height: 1.15; - } - - .toc-main - { - width: 600; - text-align: center; - margin: 1pc 1pc 1pc 10%; - padding: 2pc 1pc 3pc 1pc; - line-height: 0.1; - } - - .boost-toc - { - float: right; - padding: 0.5pc; - } - -/*============================================================================= - Tables -=============================================================================*/ - - .table-title, - div.table p.title - { - margin-left: 4%; - padding-right: 0.5em; - padding-left: 0.5em; - } - - .informaltable table, - .table table - { - width: 92%; - margin-left: 4%; - margin-right: 4%; - } - - div.informaltable table, - div.table table - { - padding: 4px; - } - - /* Table Cells */ - div.informaltable table tr td, - div.table table tr td - { - padding: 0.5em; - text-align: left; - } - - div.informaltable table tr th, - div.table table tr th - { - padding: 0.5em 0.5em 0.5em 0.5em; - border: 1pt solid white; - font-size: 120%; - } - -/*============================================================================= - Blurbs -=============================================================================*/ - - div.note, - div.tip, - div.important, - div.caution, - div.warning, - p.blurb - { - font-size: 10pt; - line-height: 1.2; - display: block; - margin: 1pc 4% 0pc 4%; - padding: 0.5pc 0.5pc 0.5pc 0.5pc; - } - - p.blurb img - { - padding: 1pt; - } - -/*============================================================================= - Variable Lists -=============================================================================*/ - - span.term - { - font-weight: bold; - font-size: 10pt; - } - - div.variablelist table tbody tr td - { - text-align: left; - vertical-align: top; - padding: 0em 2em 0em 0em; - font-size: 10pt; - } - - div.variablelist table tbody tr td p - { - margin: 0em 0em 0.5em 0em; - } - - /* Make the terms in definition lists bold */ - div.variablelist dl dt - { - font-weight: bold; - font-size: 10pt; - } - - div.variablelist dl dd - { - margin: 1em 0em 1em 2em; - font-size: 10pt; - } - -/*============================================================================= - Misc -=============================================================================*/ - - /* Title of books and articles in bibliographies */ - span.title - { - font-style: italic; - } - - span.underline - { - text-decoration: underline; - } - - span.strikethrough - { - text-decoration: line-through; - } - - /* Copyright, Legal Notice */ - div div.legalnotice p - { - font-size: 8pt; - text-align: left - } - -/*============================================================================= - Colors -=============================================================================*/ - - @media screen - { - /* Links */ - a - { - color: #0C7445; - } - - a:visited - { - color: #663974; - } - - h1 a, h2 a, h3 a, h4 a, h5 a, h6 a, - h1 a:hover, h2 a:hover, h3 a:hover, h4 a:hover, h5 a:hover, h6 a:hover, - h1 a:visited, h2 a:visited, h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited - { - text-decoration: none; /* no underline */ - color: #000000; - } - - /* Syntax Highlighting */ - .keyword { color: #0000AA; } - .identifier { color: #000000; } - .special { color: #707070; } - .preprocessor { color: #402080; } - .char { color: teal; } - .comment { color: #800000; } - .string { color: teal; } - .number { color: teal; } - .white_bkd { background-color: #E8FBE9; } - .dk_grey_bkd { background-color: #A0DAAC; } - - /* Copyright, Legal Notice */ - .copyright - { - color: #666666; - font-size: small; - } - - div div.legalnotice p - { - color: #666666; - } - - /* Program listing */ - pre.synopsis - { - border: 1px solid #DCDCDC; - border-bottom: 3px solid #9D9D9D; - border-right: 3px solid #9D9D9D; - background-color: #FAFFFB; - } - - .programlisting, - .screen - { - border: 1px solid #DCDCDC; - border-bottom: 3px solid #9D9D9D; - border-right: 3px solid #9D9D9D; - background-color: #FAFFFB; - } - - /* Blurbs */ - div.note, - div.tip, - div.important, - div.caution, - div.warning, - p.blurb - { - border: 1px solid #DCDCDC; - border-bottom: 3px solid #9D9D9D; - border-right: 3px solid #9D9D9D; - background-color: #FAFFFB; - } - - /* Table of contents */ - .toc - { - border: 1px solid #DCDCDC; - border-bottom: 3px solid #9D9D9D; - border-right: 3px solid #9D9D9D; - background-color: #FAFFFB; - } - - /* Table of contents */ - .toc-main - { - border: 1px solid #DCDCDC; - border-bottom: 3px solid #9D9D9D; - border-right: 3px solid #9D9D9D; - background-color: #FAFFFB; - } - - - /* Tables */ - div.informaltable table tr td, - div.table table tr td - { - border: 1px solid #DCDCDC; - background-color: #FAFFFB; - } - - div.informaltable table tr th, - div.table table tr th - { - background-color: #E3F9E4; - border: 1px solid #DCDCDC; - } - - /* Misc */ - span.highlight - { - color: #00A000; - } - } - - @media print - { - /* Links */ - a - { - color: black; - } - - a:visited - { - color: black; - } - - .spirit-nav - { - display: none; - } - - /* Program listing */ - pre.synopsis - { - border: 1px solid gray; - background-color: #FAFFFB; - } - - .programlisting, - .screen - { - border: 1px solid gray; - background-color: #FAFFFB; - } - - /* Table of contents */ - .toc - { - border: 1px solid #DCDCDC; - border-bottom: 3px solid #9D9D9D; - border-right: 3px solid #9D9D9D; - background-color: #FAFFFB; - } - - /* Table of contents */ - .toc-main - { - border: 1px solid #DCDCDC; - border-bottom: 3px solid #9D9D9D; - border-right: 3px solid #9D9D9D; - background-color: #FAFFFB; - } - - .informaltable table, - .table table - { - border: 1px solid #DCDCDC; - border-bottom: 3px solid #9D9D9D; - border-right: 3px solid #9D9D9D; - border-collapse: collapse; - background-color: #FAFFFB; - } - - /* Tables */ - div.informaltable table tr td, - div.table table tr td - { - border: 1px solid #DCDCDC; - background-color: #FAFFFB; - } - - div.informaltable table tr th, - div.table table tr th - { - border: 1px solid #DCDCDC; - background-color: #FAFFFB; - } - - /* Misc */ - span.highlight - { - font-weight: bold; - } - } diff --git a/doc/html/images/blank.png b/doc/html/images/blank.png deleted file mode 100644 index c387d42..0000000 Binary files a/doc/html/images/blank.png and /dev/null differ diff --git a/doc/html/images/caution.png b/doc/html/images/caution.png deleted file mode 100644 index b056bcc..0000000 Binary files a/doc/html/images/caution.png and /dev/null differ diff --git a/doc/html/images/draft.png b/doc/html/images/draft.png deleted file mode 100644 index c99e091..0000000 Binary files a/doc/html/images/draft.png and /dev/null differ diff --git a/doc/html/images/home.png b/doc/html/images/home.png deleted file mode 100644 index 04cc7bd..0000000 Binary files a/doc/html/images/home.png and /dev/null differ diff --git a/doc/html/images/important.png b/doc/html/images/important.png deleted file mode 100644 index e257957..0000000 Binary files a/doc/html/images/important.png and /dev/null differ diff --git a/doc/html/images/next.png b/doc/html/images/next.png deleted file mode 100644 index f9f1081..0000000 Binary files a/doc/html/images/next.png and /dev/null differ diff --git a/doc/html/images/note.png b/doc/html/images/note.png deleted file mode 100644 index 28195ae..0000000 Binary files a/doc/html/images/note.png and /dev/null differ diff --git a/doc/html/images/prev.png b/doc/html/images/prev.png deleted file mode 100644 index 22b8ac2..0000000 Binary files a/doc/html/images/prev.png and /dev/null differ diff --git a/doc/html/images/tip.png b/doc/html/images/tip.png deleted file mode 100644 index 07ef20f..0000000 Binary files a/doc/html/images/tip.png and /dev/null differ diff --git a/doc/html/images/toc-blank.png b/doc/html/images/toc-blank.png deleted file mode 100644 index 75b24d6..0000000 Binary files a/doc/html/images/toc-blank.png and /dev/null differ diff --git a/doc/html/images/toc-minus.png b/doc/html/images/toc-minus.png deleted file mode 100644 index 7a8a274..0000000 Binary files a/doc/html/images/toc-minus.png and /dev/null differ diff --git a/doc/html/images/toc-plus.png b/doc/html/images/toc-plus.png deleted file mode 100644 index 29fada2..0000000 Binary files a/doc/html/images/toc-plus.png and /dev/null differ diff --git a/doc/html/images/up.png b/doc/html/images/up.png deleted file mode 100644 index 17d9c3e..0000000 Binary files a/doc/html/images/up.png and /dev/null differ diff --git a/doc/html/images/warning.png b/doc/html/images/warning.png deleted file mode 100644 index 74fc1ba..0000000 Binary files a/doc/html/images/warning.png and /dev/null differ diff --git a/doc/html/reference.css b/doc/html/reference.css deleted file mode 100644 index be4e64c..0000000 --- a/doc/html/reference.css +++ /dev/null @@ -1,5 +0,0 @@ -PRE.synopsis { - background-color: #e0ffff; - border: thin solid blue; - padding: 1em -} diff --git a/doc/intrusive.qbk b/doc/intrusive.qbk deleted file mode 100644 index 6d9d140..0000000 --- a/doc/intrusive.qbk +++ /dev/null @@ -1,3418 +0,0 @@ -[/ - / Copyright (c) 2007 Ion Gaztanaga - / - / Distributed under the Boost Software License, Version 1.0. (See accompanying - / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - /] - -[library Boost.Intrusive - [quickbook 1.3] - [authors [Krzikalla, Olaf], [Gaztañaga, Ion]] - [copyright 2005 Olaf Krzikalla, 2006-2007 Ion Gaztañaga] - [id intrusive] - [dirname intrusive] - [purpose Intrusive containers] - [license - Distributed under the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - [@http://www.boost.org/LICENSE_1_0.txt]) - ] -] - -[section:introduction Introduction] - -[section:introduction_presenting Presenting Boost.Intrusive] - -[*Boost.Intrusive] is a library presenting some intrusive containers to -the world of C++. Intrusive containers are special containers -that offer [link intrusive.performance better performance] -and exception safety guarantees than non-intrusive containers (like STL containers). - -The performance benefits of intrusive containers makes them ideal as a building -block to efficiently construct complex containers like multi-index containers or -to design high performance code like memory allocation algorithms. - -While intrusive containers were and are widely used in C, they -became more and more forgotten in C++ due to the presence of the standard -containers which don't support intrusive techniques.[*Boost.Intrusive] not only -reintroduces this technique to C++, but also encapsulates the implementation in -STL-like interfaces. Hence anyone familiar with standard containers can easily use -[*Boost.Intrusive]. - -[endsect] - -[section:introduction_building_intrusive Building Boost.Intrusive] - -There is no need to compile anything to use [*Boost.Intrusive], since it's -a header only library. Just include your Boost header directory in your -compiler include path. - -[endsect] - -[endsect] - -[section:intrusive_vs_nontrusive Intrusive and non-intrusive containers] - -[section:differences_intrusive_vs_nontrusive Differences between intrusive and non-intrusive containers] - -The main difference between intrusive containers and non-intrusive containers is -that in C++ non-intrusive containers store [*copies] of values passed by the user. -Containers use the `Allocator` template parameter to allocate the stored values: - -[c++] - - #include - #include - - int main() - { - std::list myclass_list; - - MyClass myclass(...); - myclass_list.push_back(myclass); - - //The stored object is different from the original object - assert(&myclass != &myclass_list.front()); - return 0; - } - - -To store the newly allocated copy of `myclass`, the container needs additional -data: `std::list` usually allocates nodes that contain pointers to the -next and previous node and the value itself. Something similar to: - -[c++] - - //A possible implementation of a std::list node - class list_node - { - list_node *next; - list_node *previous; - MyClass value; - }; - - -On the other hand, an intrusive container does not store copies of passed objects, -but it stores the objects themselves. The additional data needed to insert the object -in the container must be provided by the object itself. For example, to insert `MyClass` -in an intrusive container that implements a linked list, `MyClass` must contain the -needed ['next] and ['previous] pointers: - -[c++] - - class MyClass - { - MyClass *next; - MyClass *previous; - //Other members... - }; - - int main() - { - acme_intrusive_list list; - - MyClass myclass; - list.push_back(myclass); - - //"myclass" object is stored in the list - assert(&myclass == &list.front()); - return 0; - } - -As we can see, knowing which additional data the class should contain is not -an easy task. [*Boost.Intrusive] offers several intrusive containers and an easy -way to make user classes compatible with those containers. - -[endsect] - -[section:properties_of_intrusive Properties of Boost.Intrusive containers] - -Semantically, a [*Boost.Intrusive] container is similar to an STL container -holding pointers to objects. That is, if you have an intrusive list holding -objects of type `T`, then `std::list` would allow you to do quite the -same operations (maintaining and navigating a set of objects of type T and -types derived from it). - -A non-intrusive container has some limitations: - -* An object can only belong to one container: If you want to share an object - between two containers, you either have to store multiple copies of those - objects or you need to use containers of pointers: `std::list`. - -* The use of dynamic allocation to create copies of passed values can be a performance - and size bottleneck in some applications. Normally, dynamic allocation imposes - a size overhead for each allocation to store bookeeping information and a - synchronization to protected concurrent allocation from different threads. - -* Only copies of objects are stored in non-intrusive containers. Hence copy - or move constructors and copy or move assignment operators are required. Non-copyable - and non-movable objects can't be stored in non-intrusive containers. - -* It's not possible to store a derived object in a STL-container while - retaining its original type. - -Intrusive containers have some important advantages: - -* Operating with intrusive containers doesn't invoke any memory management at all. - The time and size overhead associated with dynamic memory can be minimized. - -* Iterating an Intrusive container needs less memory accesses than the semantically - equivalent container of pointers: iteration is faster. - -* Intrusive containers offer better exception guarantees than non-intrusive containers. - In some situation intrusives containers offer a no-throw guarantee that can't be - achieved with non-intrusive containers. - -* The computation of an iterator to an element from a pointer or reference to that element - is a constant time operation (computing the position of `T*` in a `std::list` has - linear complexity). - -* Intrusive containers offer predictability when inserting and erasing objects since no - memory managed is done with intrusive containers. Memory management usually is not a predicable - operation so complexity guarantees from non-intrusive containers are looser than the guarantees - offered by intrusive containers. - -Intrusive containers have also downsides: - -* Each type stored in an intrusive container needs additional memory holding the - maintenance information needed by the container. Hence, whenever a certain type shall - be stored in an intrusive container [*you have to change the definition of that type] - appropriately. Although this task is easy with [*Boost.Intrusive], touching the - definition of a type is sometimes a crucial issue. - -* In intrusive containers you don't store a copy of an object, [*but they rather the original object - is linked with other objects in the container]. Objects don't need copy-constructors or assignment - operators to be stored in intrusive containers. But you have to take care of possible side effects, - whenever you change the contents of an object (this is especially important for - associative containers). - -* The user [*has to manage the lifetime of inserted objects] independently from the - containers. - -* Again you have to be [*careful]: in contrast to STL containers [*it's easy to render an - iterator invalid] without touching the intrusive container directly, because the object - can be disposed before is erased from the container. - -* [*Boost.Intrusive] containers are [*non-copyable and non-assignable]. Since intrusive - containers don't have allocation capabilities, these operations have no sense. However, - swapping can be used to implement move-capabilities. To ease the implementation of - copy constructors and assignment operators of classes storing [*Boost.Intrusive] - containers, [*Boost.Intrusive] offers special cloning functions. See - [link intrusive.clone_from Cloning [*Boost.Intrusive] containers] section for more information. - -* Analyzing thread-safety of a program that uses containers is harder with intrusive containers, becuase - the container might be modified indirectly without an explicitly call to a container member. - -[table Summay of intrusive containers advantages and disadvantages - [[Issue] [Intrusive] [Non-intrusive]] - [[Memory management] [External] [Internal through allocator]] - [[Insertion/Erasure time] [Faster] [Slower]] - [[Memory locality] [Better] [Worse]] - [[Can hold non-copyable and non-movable objects by value] [Yes] [No]] - [[Exception guarantees] [Better] [Worse]] - [[Computation of iterator from value] [Constant] [Non-constant]] - [[Insertion/erasure predictability] [High] [Low]] - [[Memory use] [Minimal] [More than minimal]] - [[Insert objects by value retaining polymorphic behavior] [Yes] [No (slicing)]] - [[User must modify the definition of the values to insert] [Yes] [No]] - [[Containers are copyable] [No] [Yes]] - [[Inserted object's lifetime managed by] [User (more complex)] [Container (less complex)]] - [[Container invariants can be broken without using the container] [Easier] [Harder (only with containers of pointers)]] - [[Thread-safety analysis] [Harder] [Easier]] -] - -For a performance comparison between Intrusive and Non-intrusive containers see -[link intrusive.performance Performance] section. - -[endsect] - -[endsect] - -[section:usage How to use Boost.Intrusive] - -If you plan to insert a class in an intrusive container, you have to make some decisions -influencing the class definition itself. Each class that will be used in an intrusive -container needs some appropriate data members storing the information needed by the -container. We will take a simple intrusive container, like an intrusive list -([classref boost::intrusive::list boost::intrusive::list]) for the following -examples, but all [*Boost.Intrusive] containers are very similar. To compile -the example using [classref boost::intrusive::list boost::intrusive::list], -just include: - -[c++] - - #include - -Every class to be inserted in an intrusive container, needs to contain a hook that -will offer the necessary data and resources to be insertable in the container. -With [*Boost.Intrusive] you just choose the hook to be a public base class or -a public member of the class to be inserted. - -[section:usage_base_hook Using base hooks] - -For [classref boost::intrusive::list list], you can publicly derive from -[classref boost::intrusive::list_base_hook list_base_hook]. - -[c++] - - template - class list_base_hook; - -The class can take several options. [*Boost.Intrusive] classes receive arguments in the -form `option_name`. You can specify the following options: - -* [*`tag`]: this argument serves as a tag, so you can derive from more than one - [classref boost::intrusive::list_base_hook list_base_hook] and hence put an object in - multiple intrusive lists at the same time. An incomplete type can serve as a tag. - If you specify two base hooks, you [*must] specify a different - tag for each one. Example: `list_base_hook< tag >`. If no tag is specified - a default one will be used (more on default tags later). - -* [*`link_mode`]: The second template argument controls the - linking policy. [*Boost.Intrusive] currently supports - 3 modes: `normal_link`, `safe_link` and `auto_unlink`. By default, `safe_link` - mode is used. More about these in sections - [link intrusive.safe_hook Safe hooks] and [link intrusive.auto_unlink_hooks Auto-unlink hooks]. - Example: `list_base_hook< link_mode >` - -* [*`void_pointer`]: this option is the pointer type to be used - internally in the hook. The default value is `void *`, which means that raw pointers - will be used in the hook. More about this in the section titled - [link intrusive.using_smart_pointers Using smart pointers with Boost.Intrusive containers]. - Example: `list_base_hook< void_pointer< my_smart_ptr >` - -For the following examples, let's forget the options and use the default values: - -[c++] - - #include - - using namespace boost::intrusive; - - class Foo - //Base hook with default tag, raw pointers and safe_link mode - : public list_base_hook<> - { /**/ }; - -After that, we can define the intrusive list: - -[c++] - - template - class list; - -`list` receives the type to be inserted in the container (`T`) as the first parameter -and optionally, the user can specify options. We have 3 option types: - -* [*`base_hook`] / [*`member_hook`] / - [*`value_traits`]: All these options specify the relationship - between the type `T` to be inserted in the list and the hook (since we can - have several hooks in the same `T` type). `member_hook` will be explained - a bit later and `value_traits` will be explained in the - [link intrusive.value_traits Containers with custom ValueTraits] section. - [*If no option is specified, the container will be configured to use the base - hook with the default tag]. - Some options configured for the hook (the type of the pointers, link mode...) - will be propagated to the container. - -* [*`constant_time_size`]: Specifies if a constant time `size()` - function is demanded for the container. This will instruct the intrusive - container to store an additional member to keep track of the current size of the - container. By default, contant-time size is activated. - -* [*`size_type`]: Specifies a type that can hold - the size of the container. This type will be the type returned by `list.size()` - and the type stored in the intrusive container if `constant_time_size` - is requested. - The user normally will not need to change this type, but some - containers can have a `size_type` that might be different from `std::size_t` - (for example, STL-like containers, use the `size_type` defined by their allocator). - [*Boost.Intrusive] can be used to implement such containers specifying the - the type of the size. By default the type is `std::size_t`. - -Example of a constant-time size intrusive list that will store Foo objects, using -the base hook with the default tag: - -[c++] - - typedef list FooList; - -Example of a intrusive list with non constant-time size that will store Foo objects: - -[c++] - - typedef list > FooList; - -Remember that the user must specify the base hook if the base hook has no default tag -(e.g: if more than one base hook is used): - -[c++] - - #include - - using namespace boost::intrusive; - - struct my_tag; - - typedef list_base_hook< tag > BaseHook; - class Foo : public BaseHook - { /**/ }; - - typedef list< Foo, base_hook > FooList; - -Once the list is defined, we can use it: - -[c++] - - //An object to be inserted in the list - Foo foo_object; - FooList list; - - list.push_back(object); - - assert(&list.front() == &foo_object); - -[endsect] - -[section:usage_member_hook Using member hooks] - -Sometimes an 'is-a' relationship between list hooks and the list value types -is not desirable. In this case, using a member hook as a data member instead of -'disturbing' the hierarchy might be the right way: you can add a public data -member `list_member_hook<...>` to your class. -This class can be configured with the same options as `list_base_hook` -except the option `tag`: - -[c++] - - template - class list_member_hook; - -Example: - -[c++] - - #include - - class Foo - { - public: - list_member_hook<> hook_; - //... - }; - -When member hooks are used, the `member_hook` option is used to configure the -list: - -[c++] - - //This option will configure "list" to use the member hook - typedef member_hook, &Foo::hook_> MemberHookOption; - - //This list will use the member hook - typedef list FooList; - -Now we can use the container: - -[c++] - - //An object to be inserted in the list - Foo foo_object; - FooList list; - - list.push_back(object); - - assert(&list.front() == &foo_object); - -[endsect] - -[section:usage_both_hooks Using both hooks] - -You can insert the same object in several intrusive containers at the same time, just -using one hook for each container. This is a full example using base and member hooks: - -[import ../example/doc_how_to_use.cpp] -[doc_how_to_use_code] - -[endsect] - -[section:usage_lifetime Object lifetime] - -Even if the interface of [classref boost::intrusive::list list] is similar to -`std::list`, its usage is a bit different: You always have to keep in mind that -you directly store objects in intrusive containers, not copies. The lifetime of a -stored object is not bound to or managed by the container: - -* When the container gets destroyed before the object, the object is not destroyed, - so you have to be careful to avoid resource leaks. - -* When the object is destroyed before the container, your program is likely to crash, - because the container contains a pointer to an non-existing object. - -[endsect] - -[endsect] - -[section:usage_when When to use?] - -Intrusive containers can be used for highly optimized algorithms, where speed is a crucial -issue and... - -* additional memory management should be avoided. -* the programmer needs to efficiently track the construction and destruction of objects. -* exception safety, especially the no-throw guarantee, is needed. -* the computation of an iterator to an element from a pointer or reference - to that element should be a constant time operation. -* it's important to achieve a well-known worst-time system response. -* localization of data (e.g. for cache hit optimization) leads to measureable effects. - -The last point is important if you have a lot of containers over a set of elements. E.g. if -you have a vector of objects (say, `std::vector`) and you also have a list -storing a subset of those objects (`std::list`), then operating on an Object -from the list iterator (`std::list::iterator`) needs two steps: - -* Access from the iterator (usually on the stack) to the list node storing a pointer to `Object`. -* Access from the pointer to `Object` to the Object stored in the vector. - -While the objects themselves are tightly packed in the memory of the vector -(vector's memory is guaranteed to be contiguous), and form something -like a data block, list nodes can stay dispersed in the heap memory. -Hence depending on your system you can get a lot of cache misses. The same doesn't hold -for an intrusive list. Indeed, dereferencing an an iterator from an intrusive list is performed in -the same two steps as described above. But the list node is already embedded in the Object, so -the memory is directly tracked from the iterator to the Object. - -It's also possible to use intrusive containers when the objects to be stored can -have different or unknown size. This allows storing base and derived objects -in the same container as shown in the following example: - -[import ../example/doc_window.cpp] -[doc_window_code] - -Due to certain properties of intrusive containers -they are often more difficult to use than their STL-counterparts. That's why you -should avoid them in public interfaces of libraries. Classes to be stored in intrusive -containers must change their implementation to store the hook and this is not always -posible or desirable. - -[endsect] - -[section:concepts_summary Concept summary] - -Here is a small summary of the basic concepts that will be used in the following -chapters: - -[variablelist Brief Concepts Summary -[[Node Algorithms][A class containing typedefs and static functions that define - basic operations that can be applied to a groups of nodes. It's independent - from the node definition, and it's configured taking a NodeTraits template - parameter that describes the node.]] -[[Node Traits][A class that stores basic information and operations to insert a node in a group of nodes.]] -[[Hook][A class that a user must add as a base class or as a member to make the user class compatible with intrusive containers.]] -[[Intrusive Container][A class that stores user classes that have the needed hooks. It takes a ValueTraits template parameter as configuration information.]] -[[Pseudo-Intrusive Container][Similar to an intrusive container but a pseudo-intrusive container needs additional memory (e.g. an auxiliary array) to work.]] -[[Value Traits][A class containing typedefs and operations to obtain the node to be used by Node Algorithms from the user class and the inverse.]] -] - -[endsect] - -[section:presenting_containers Presenting Boost.Intrusive containers] - -[*Boost.Intrusive] offers a wide range of intrusive containers: - -* [*slist]: An intrusive singly linked list. The size overhead is very small - for user classes (usually the size of one pointer) but many operations have linear - time complexity, so the user must be careful if he wants to avoid performance problems. - -* [*list]: A `std::list` like intrusive linked list. The size overhead is quite - small for user classes (usually the size of two pointers). Many operations have - constant time complexity. - -* [*set/multiset]: A `std::set/std::multiset` like intrusive associative containers. - The size overhead is moderate for user classes (usually the size of three pointers). - Many operations have logarithmic time complexity. - -[*Boost.Intrusive] also offers pseudo-intrusive containers: - -* [*unordered_set/unordered_multiset]: A `std::tr1::unordered_set/std::tr1::unordered_multiset` - like intrusive unordered associative containers. - The size overhead is moderate for user classes (an average of two pointers per element). - Many operations have an amortized constant time complexity. - -Each of these intrusive containers can be configured with constant or linear time -size: - -* [*Linear time size]: The intrusive container doesn't hold a size member that it's -updated with every insertion/erasure. This implies that the `size()` function has not constant -time complexity. On the other hand, the container is smaller, and some operations, like -`splice()` taking a range of iterators in linked lists have constant time complexity -instead of linear complexity. - -* [*Constant time size]: The intrusive container holds a size member that it's updated -with every insertion/erasure. This implies that the `size()` function has constant time -complexity. On the other hand, increases the size of the container, and some operations, -like `splice()` taking a range of iterators, have linear time complexity in linked lists. - -To make user classes compatible with these intrusive containers [*Boost.Intrusive] -offers two types of hooks for each container type: - -* [*Base hook]: The hook is stored as a public base class of the user class. - -* [*Member hook]: The hook is stored as a public member of the user class. - -Apart from that, [*Boost.Intrusive] offers additional features: - -* [*Safe mode hooks]: Hook constructor initializes the internal data to a well-known - safe state and intrusive containers check that state before inserting a value in the - container. When erasing an element from the container, the container puts the hook - in the safe state again. This allows a safer use mode and it can be used to detect - programming errors. It implies an slight performance overhead in some operations - and can convert some constant time operations in linear time operations. - -* [*Auto-unlink hooks]: The hook destructor removes the object from the container - automatically and the user can safely unlink the object from the container without - having any reference to the container. - -* [*Non-raw pointers]: If the user wants to use smart pointers instead of raw pointers, - [*Boost.Intrusive] hooks can - be configured to use any type of pointers. This configuration information is also - transmitted to the containers, so all the internal pointers used by intrusive containers - configured with these hooks will be smart pointers. As an example, - [*Boost.Interprocess] defines an smart pointer compatible with shared memory, - called `offset_ptr`. [*Boost.Intrusive] can be configured to use this smart pointer - to allow shared memory intrusive containers. - -[endsect] - -[section:safe_hook Safe hooks] - -[section:features Features of the safe mode] - -[*Boost.Intrusive] hooks can be configured to operate in safe-link mode. -The safe mode is activated by default, but it can be also explicitly activated: - -[c++] - - //Configuring explicity the safe mode - class Foo : public list_base_hook< link_mode > - {}; - -Thanks to the safe-mode the user can detect without any external reference, if the object -is actually inserted in a container. Let's review the basic features of the safe-mode: - -* Hooks' constructor puts the hook in a well-known default state. - -* Hooks' destructor checks if the hook is in the well-known default state. If not, - an assertion is raised. - -* Every time an object is being inserted in the intrusive container, the container - checks if the hook is in the well-known default state. If not, - an assertion is raised. - -* Every time an object is being erased from the intrusive container, the container - puts the erased object in the well-known default state. - -With these features, without any external reference the user can know if the object -has been inserted in a container calling the `is_linked()` member function. -If the object is not actually inserted -in a container, the hook is in the default state and if it's inserted in a container, the -hook is not in the default state. - -[endsect] - -[section:configuring Configuring safe-mode assertions] - -By default, all safe-mode assertions raised by [*Boost-Intrusive] hooks -and containers in are implemented using `BOOST_ASSERT`, which can be configured by -the user. See [@http://www.boost.org/libs/utility/assert.html] for more -information about `BOOST_ASSERT`. - -`BOOST_ASSERT` is globally configured, so the user might -want to redefine intrusive safe-mode assertions without modifying the global -`BOOST_ASSERT`. This can be achieved redefining the following macros: - -* `BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT`: This assertion will be - used in insertion functions of the intrusive containers to check that - the hook of the value to be inserted is default constructed. -* `BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT`: This assertion will be - used in hooks' destructors to check that the hook is in a default state. - -If any of these macros is not redefined, the assertion will be defaul to `BOOST_ASSERT`. - -[endsect] - -[endsect] - -[section:auto_unlink_hooks Auto-unlink hooks] - -[section:auto_unlink_hooks_what What's an auto-unlink hook?] - -[*Boost.Intrusive] offers additional hooks with unique features: - -* When the destructor of the hook is called, the hook checks if the node is inserted - in a container. If so, the hook removes the node from the container. -* The hook has a member function called `unlink()` that can be used to unlink the - node from the container at any moment, without having any reference to the container, - if the user want to do so. - -These hooks have exactly the same size overhead as their analogue non auto-unlinking -hooks, but they have a restriction: they can only be used with -[link intrusive.presenting_containers non-constant time containers]. -There is a reason for this: - -* Auto-unlink hooks don't store any reference to the container where they are inserted. -* Only containers with non constant-time `size()` allow removing an object from the container - without using any reference to the container. - -This auto-unlink feature is useful in certain applications -but it must be used [*very carefuly]: - -* If several threads are using the same container the destructor of the auto-unlink - hook will be called without any thread synchronization so removing the object is - thread-unsafe. - -* Container contents change silently without modifying the container directly. - This can lead to surprising effects. - -These auto-unlink hooks have also safe-mode properties: - -* Hooks' constructors put the hook in a well-known default state. - -* Every time an object is being inserted in the intrusive container, the container - checks if the hook is the well-known default state. If not, - an assertion is raised. - -* Every time an object is being erased from the intrusive container, the container - puts the erased object in the well-known default state. - -[endsect] - -[section:auto_unlink_hooks_example Auto-unlink hook example] - -Let's see an example of an auto-unlink hook: - -[import ../example/doc_auto_unlink.cpp] -[doc_auto_unlink_code] - -[endsect] - -[section:auto_unlink_and_constant_time Auto-unlink hooks and containers with constant-time `size()`] - -As explained, [*Boost.Intrusive] auto-unlink hooks are incompatible with containers -that have constant-time `size()`, so if you try to define such container with an -auto-unlink hook's value_traits, you will get an static assertion: - -[c++] - - #include - - using boost::intrusive; - - struct MyTag; - - class MyClass : public list_base_hook< link_mode > - {/**/}; - - list > bad_list; - - int main() - { - bad_list list; - return 0; - } - -leads to an error similar to: - -[pre - error : use of undefined type 'boost::STATIC_ASSERTION_FAILURE' -] - -Pointing to code like this: - -[c++] - - //Constant-time size is incompatible with auto-unlink hooks! - BOOST_STATIC_ASSERT(!(constant_time_size && ((int)value_traits::link_mode == (int)auto_unlink))); - -This way, there is no way to compile a program if you try to use auto-unlink hooks -in constant-time size containers. - -[endsect] - -[endsect] - -[section:slist Intrusive singly linked list: slist] - -[classref boost::intrusive::slist slist] is the simplest intrusive container of -[*Boost.Intrusive]: a singly linked list. The memory overhead -that imposes is 1 pointer per node. The size of an empty, non constant-time size -[classref boost::intrusive::slist slist], is the size of 1 pointer. This -lightweight memory overhead comes with its drawbacks, though: many operations have -linear time complexity, even some that usually are constant time, like -[classref boost::intrusive::slist::swap swap]. [classref boost::intrusive::slist slist] -only provides forward iterators. - -For most cases, a doubly linked list is preferrable because it offers more -constant-time functions with a slightly bigger overhead. -However, for some applications like -constructing more elaborated containers, singly linked lists are essential -because of their low size overhead. - -[section:slist_hooks slist hooks] - -Like the rest of [*Boost.Intrusive] containers, -[classref boost::intrusive::slist slist] has two hook types: - -[c++] - - template - class slist_base_hook; - -* [classref boost::intrusive::slist_base_hook slist_base_hook]: - the user class derives publicly from - [classref boost::intrusive::slist_base_hook slist_base_hook] to make - it [classref boost::intrusive::slist slist]-compatible. - -[c++] - - template - class slist_member_hook; - -* [classref boost::intrusive::slist_member_hook slist_member_hook]: - the user class contains a public - [classref boost::intrusive::slist_member_hook slist_member_hook] to make - it [classref boost::intrusive::slist slist]-compatible. - -[classref boost::intrusive::slist_base_hook slist_base_hook] and -[classref boost::intrusive::slist_member_hook slist_member_hook] receive the same options explained in -the section [link intrusive.usage How to use Boost.Intrusive]: - -* [*`tag`] (for base hooks only): This argument serves as a tag, - so you can derive from more than one slist hook. - Default: `tag`. - -* [*`link_mode`]: The linking policy. - Default: `link_mode`. - -* [*`void_pointer`]: The pointer type to be used - internally in the hook and propagated to the container. - Default: `void_pointer`. - -[endsect] - -[section:slist_container slist container] - -[c++] - - template - class slist; - -[classref boost::intrusive::slist slist] receives the same options explained in -the section [link intrusive.usage How to use Boost.Intrusive]: - -* [*`base_hook`] / [*`member_hook`] / - [*`value_traits`]: To specify the hook type or value traits used - to configure the container (to know about value traits go to the section - titled [link intrusive.value_traits Containers with custom ValueTraits]. - -* [*`constant_time_size`]: To activate the constant-time `size()` operation. - Default: `constant_time_size` - -* [*`size_type`]: To specify the type that will be used to store the size - of the container. Default: `size_type` - -[endsect] - -[section:slist_example Example] - -Now let's see an small example using both hooks: - -[import ../example/doc_slist.cpp] -[doc_slist_code] - -[endsect] - -[endsect] - -[section:list Intrusive doubly linked list: list] - -[classref boost::intrusive::list list] is a doubly linked list. The memory overhead -that imposes is 2 pointers per node. An empty, non constant-time size [classref boost::intrusive::list list] -has also the size of 2 pointers. [classref boost::intrusive::list list] -has many more constant-time operations than [classref boost::intrusive::slist slist] -and provides bidirectional iterator. It's recommendable to use use -[classref boost::intrusive::list list] instead of -[classref boost::intrusive::slist slist] if the size overhead is acceptable: - -[section:list_hooks list hooks] - -Like the rest of [*Boost.Intrusive] containers, -[classref boost::intrusive::list list] has two hook types: - -[c++] - - template - class list_base_hook; - -* [classref boost::intrusive::list_base_hook list_base_hook]: the user class - derives publicly from [classref boost::intrusive::list_base_hook list_base_hook] - to make it [classref boost::intrusive::list list]-compatible. - -[c++] - - template - class list_member_hook; - -* [classref boost::intrusive::list_member_hook list_member_hook]: - the user class contains a public - [classref boost::intrusive::list_member_hook list_member_hook] to make - it [classref boost::intrusive::list list]-compatible. - -[classref boost::intrusive::list_base_hook list_base_hook] and -[classref boost::intrusive::list_member_hook list_member_hook] receive -the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive]: - -* [*`tag`] (for base hooks only): This argument serves as a tag, - so you can derive from more than one list hook. - Default: `tag`. - -* [*`link_mode`]: The linking policy. - Default: `link_mode`. - -* [*`void_pointer`]: The pointer type to be used - internally in the hook and propagated to the container. - Default: `void_pointer`. - -[endsect] - -[section:list_container list container] - -[c++] - - template - class list; - -[classref boost::intrusive::list list] receives the same options explained in -the section [link intrusive.usage How to use Boost.Intrusive]: - -* [*`base_hook`] / [*`member_hook`] / - [*`value_traits`]: To specify the hook type or value traits used - to configure the container (to know about value traits go to the section - titled [link intrusive.value_traits Containers with custom ValueTraits]. - -* [*`constant_time_size`]: To activate the constant-time `size()` operation. - Default: `constant_time_size` - -* [*`size_type`]: To specify the type that will be used to store the size - of the container. Default: `size_type` - -[endsect] - -[section:list_example Example] - -Now let's see an small example using both hooks: - -[import ../example/doc_list.cpp] -[doc_list_code] - -[endsect] - -[endsect] - -[section:set_multiset Intrusive associative containers: set, multiset, rbtree] - -[*Boost.Intrusive] also offers associative containers that can be very useful -when creating more complex associative containers, like containers maintaining -one or more indices with different sorting semantics. Boost.Intrusive associative -containers, like most STL associative container implemenatations are based on -red-black trees. - -The memory overhead of these containers is usually 3 pointers and a bit (with -alignment issues, this means 3 pointers and an integer). -This size can be reduced to 3 pointers if pointers have even alignment -(which is usually true in most systems). - -An empty, non constant-time size [classref boost::intrusive::set set], -[classref boost::intrusive::multiset multiset] or -[classref boost::intrusive::rbtree rbtree] -has also the size of 3 pointers and an integer (3 pointers when optimized for size). -These containers have logarithmic complexity in many -operations like -searches, insertions, erasures, etc... [classref boost::intrusive::set set] and -[classref boost::intrusive::multiset multiset] are the -intrusive equivalents of standard `std::set` and `std::multiset` containers. - -[classref boost::intrusive::rbtree rbtree] is a superset of -[classref boost::intrusive::set set] and -[classref boost::intrusive::multiset multiset] containers that offers -functions to insert unique and multiple keys. - -[section:set_multiset_hooks set, multiset and rbtree hooks] - -[classref boost::intrusive::set set], -[classref boost::intrusive::multiset multiset] and -[classref boost::intrusive::rbtree rbtree] share the same hooks. -This is an advantage, because the same -user type can be inserted first in a [classref boost::intrusive::multiset multiset] -and after that in [classref boost::intrusive::set set] without -changing the definition of the user class. - -[c++] - - template - class set_base_hook; - -* [classref boost::intrusive::set_base_hook set_base_hook]: - the user class derives publicly from - [classref boost::intrusive::set_base_hook set_base_hook] to make - it [classref boost::intrusive::set set]/[classref boost::intrusive::multiset multiset]-compatible. - -[c++] - - template - class set_member_hook; - -* [classref boost::intrusive::set_member_hook set_member_hook]: - the user class contains a public - [classref boost::intrusive::set_member_hook set_member_hook] to make - it [classref boost::intrusive::set set]/[classref boost::intrusive::multiset multiset]-compatible. - -[classref boost::intrusive::set_base_hook set_base_hook] and -[classref boost::intrusive::set_member_hook set_member_hook] receive -the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive] plus a size optimization option: - -* [*`tag`] (for base hooks only): This argument serves as a tag, - so you can derive from more than one base hook. - Default: `tag`. - -* [*`link_mode`]: The linking policy. - Default: `link_mode`. - -* [*`void_pointer`]: The pointer type to be used - internally in the hook and propagated to the container. - Default: `void_pointer`. - -* [*`optimize_size`]: The hook will be optimized for size - instead of speed. The hook will embed the color bit of the red-black - tree node in the parent pointer if pointer alignment is even. - Optimizing the size will reduce speed performance a bit since masking - operations will be needed to access parent pointer and color attributes. - Default: `optimize_size`. - -[endsect] - -[section:set_multiset_containers set, multiset and rbtree containers] - -[c++] - - template - class set; - - template - class multiset; - - template - class rbtree; - -These containers receive the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive]: - -* [*`base_hook`] / [*`member_hook`] / - [*`value_traits`]: To specify the hook type or value traits used - to configure the container (to know about value traits go to the section - titled [link intrusive.value_traits Containers with custom ValueTraits]. - -* [*`constant_time_size`]: To activate the constant-time `size()` operation. - Default: `constant_time_size` - -* [*`size_type`]: To specify the type that will be used to store the size - of the container. Default: `size_type` - -And they also can receive an additional option: - -* [*`compare`]: Comparison function for the objects to be inserted - in containers. The comparison functor must induce a strict weak ordering. - Default: `compare< std::less >` - -[endsect] - -[section:set_multiset_example Example] - -Now let's see an small example using both hooks and both containers: - -[import ../example/doc_set.cpp] -[doc_set_code] - -[endsect] - -[endsect] - -[section:unordered_set_unordered_multiset Pseudo-Intrusive unordered associative containers: unordered_set, unordered_multiset] - -[*Boost.Intrusive] also offers hashed containers that can be very useful to implement -fast-lookup containers. These containers -([classref boost::intrusive::unordered_set unordered_set] and [classref boost::intrusive::unordered_multiset unordered_multiset]) -are pseudo-intrusive containers: they need additional memory apart from the hook -stored in the `value_type`. This additional -memory must be passed in the constructor of the container. - -Unlike C++ TR1 unordered associative containers (which are also hashed containers), -the contents of these pseudo-intrusive containers are not rehashed to maintain a -load factor: that would require memory management and intrusive containers don't -implement any memory management at all. However, the user can request an explicit -rehashing passing a new bucket array. -This also offers an additional guarantee over TR1 unordered associative containers: -[*iterators are not invalidated when inserting an element] in the container. - -As with TR1 unordered associative containers, rehashing invalidates iterators, -changes ordering between elements and changes which buckets elements appear in, -but does not invalidate pointers or references to elements. - -Apart from expected hash and equality function objects, [*Boost.Intrusive] unordered -associative containers' constructors take an argument specifying an auxiliary -bucket vector to be used by the container. - -[section:unordered_set_unordered_multiset_performance unordered_set and unordered_multiset performance notes] - -The size overhead for a hashed container is moderate: 1 pointer per value plus -a bucket array per container. The size of an element of the bucket array -is usually one pointer. To obtain a good performance hashed container, -the bucket length is usually the same as the number of elements that the -container contains, so a well-balanced hashed container (`bucket_count()` is -equal to `size()` ) will have an equivalent overhead of two pointers per element. - -An empty, non constant-time size [classref boost::intrusive::unordered_set unordered_set] or -[classref boost::intrusive::unordered_multiset unordered_multiset] -has also the size of `bucket_count()` pointers. - -Insertions, erasures, and searches, have amortized constant-time complexity in -hashed containers. However, some worst-case guarantees are linear. See -[classref boost::intrusive::unordered_set unordered_set] or -[classref boost::intrusive::unordered_multiset unordered_multiset] for complexity guarantees -of each operation. - -[*Be careful with non constant-time size hashed containers]: some operations, like -`empty()`, have linear complexity, unlike other [*Boost.Intrusive] containers. - -[endsect] - -[section:unordered_set_unordered_multiset_hooks unordered_set and unordered_multiset hooks] - -[classref boost::intrusive::unordered_set unordered_set] and [classref boost::intrusive::unordered_multiset unordered_multiset] share the same hooks. This is an advantage, because the same -user type can be inserted first in a [classref boost::intrusive::unordered_multiset unordered_multiset] and after that in [classref boost::intrusive::unordered_set unordered_set] without -changing the definition of the user class. - -[c++] - - template - class unordered_set_base_hook; - -* [classref boost::intrusive::unordered_set_base_hook unordered_set_base_hook]: - the user class derives publicly from - [classref boost::intrusive::unordered_set_base_hook unordered_set_base_hook] to make - it [classref boost::intrusive::unordered_set unordered_set]/[classref boost::intrusive::unordered_multiset unordered_multiset]-compatible. - -[c++] - - template - class unordered_set_member_hook; - -* [classref boost::intrusive::unordered_set_member_hook unordered_set_member_hook]: - the user class contains a public - [classref boost::intrusive::unordered_set_member_hook unordered_set_member_hook] to make - it [classref boost::intrusive::unordered_set unordered_set]/[classref boost::intrusive::unordered_multiset unordered_multiset]-compatible. - -[classref boost::intrusive::unordered_set_base_hook unordered_set_base_hook] and -[classref boost::intrusive::unordered_set_member_hook unordered_set_member_hook] receive -the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive]: - -* [*`tag`] (for base hooks only): This argument serves as a tag, - so you can derive from more than one base hook. - Default: `tag`. - -* [*`link_mode`]: The linking policy. - Default: `link_mode`. - -* [*`void_pointer`]: The pointer type to be used - internally in the hook and propagated to the container. - Default: `void_pointer`. - -Apart from them, these hooks offer additional options: - -* [*`store_hash`]: This option reserves additional space in - the hook to store the hash value of the object once it's introduced in the - container. When this option is used, the unordered container will store - the calculated hash value in the hook and rehashing operations won't need - to recalculate the hash of the value. - This option will improve the perfomance of unordered containers when - rehashing is frequent or hashing the value is a slow operation. - Default: `store_hash`. - -[endsect] - -[section:unordered_set_unordered_multiset_containers unordered_set and unordered_multiset containers] - -[c++] - - template - class unordered_set; - - template - class unordered_multiset; - -As mentioned, unordered containers need an auxiliary array to work. [*Boost.Intrusive] -unordered containers receive this auxiliary array packed in a type called `bucket_traits` -(which can be also customized by a container option). All unordered containers receive -a `bucket_traits` object in their constructors. The default `bucket_traits` class -is initialized with a pointer to an array of buckets and its size: - -[c++] - - #include - - using namespace boost::intrusive; - - struct MyClass : public unordered_set_base_hook<> - {}; - - typedef unordered_set::bucket_type bucket_type; - typedef unordered_set::bucket_traits bucket_traits; - - int main() - { - bucket_type buckets[100]; - unordered_set uset(bucket_traits(buckets, 100)); - return 0; - } - -Each hashed container needs [*its own bucket traits], that is, [*its own -bucket vector]. Two hashed containers -[*can't] share the same `bucket_type` elements. The bucket array [*must] be -destroyed [*after] the container using it is destroyed, otherwise, the result -is undefined. - -[classref boost::intrusive::unordered_set unordered_set] and -[classref boost::intrusive::unordered_multiset unordered_multiset] -receive the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive]: - -* [*`base_hook`] / [*`member_hook`] / - [*`value_traits`]: To specify the hook type or value traits used - to configure the container (to know about value traits go to the section - titled [link intrusive.value_traits Containers with custom ValueTraits]. - -* [*`constant_time_size`]: To activate the constant-time `size()` operation. - Default: `constant_time_size` - -* [*`size_type`]: To specify the type that will be used to store the size - of the container. Default: `size_type` - -And they also can receive two additional options: - -* [*`equal`]: Equality function for the objects to be inserted - in containers. Default: `equal< std::equal_to >` - -* [*`hash`]: Hash function to be used in the container. - Default: `hash< boost::hash >` - -* [*`bucket_traits`]: A type that wraps the bucket vector to - be used by the unordered container. Default: a type initialized by the address - and size of a bucket array and stores both variables internally. - -* [*`power_2_buckets`]: The user guarantees that only bucket arrays - with power of two length will be used. The container will then use masks instead of modulo - operations to obtain the bucket number from the hash value. Masks are faster than - modulo operations and for some applications modulo operations can impose - a considerable overhead. In debug mode an assertion will be raised if the user - provides a bucket length that is not power of two. - Default: `constant_time_size`. - -[endsect] - -[section:unordered_set_unordered_multiset_example Example] - -Now let's see an small example using both hooks and both containers: - -[import ../example/doc_unordered_set.cpp] -[doc_unordered_set_code] - -[endsect] - -[section:custom_bucket_traits Custom bucket traits] - -Instead of using the default `bucket_traits` class to store the bucket array, a user -can define his own class to store the bucket array using the [*['bucket_traits<>]] -option. A user-defined bucket-traits must fulfill the following interface: - -[c++] - - class my_bucket_traits - { - bucket_ptr bucket_begin(); - const_bucket_ptr bucket_begin() const; - std::size_t bucket_count() const; - }; - - -The following bucket traits just stores a pointer to the bucket -array but the size is a compile-time constant. Note the use of the auxiliary -[classref boost::intrusive::unordered_bucket unordered_bucket] and -[classref boost::intrusive::unordered_bucket_ptr unordered_bucket_ptr] -utilities to obtain the type of the bucket and its pointer before defining -the unordered container: - -[import ../example/doc_bucket_traits.cpp] -[doc_bucket_traits] - -[endsect] - -[endsect] - -[section:splay_set_multiset Intrusive splay tree based associative containers: splay_set, splay_multiset and , splay_tree] - -C++ associative containers are usually based on red-black tree implementations (e.g.: STL, -Boost.Intrusive associative containers). However, there are other interesting data -structures that offer some advantages (and also disadvantages). - -Splay trees are self-adjusting binary search trees used tipically in caches, memory -allocators and other applications, because splay trees have a "caching effect": recently -accessed elements have better access times that elements accessed less frequently. -For more information on splay trees see [@http://en.wikipedia.org/wiki/Splay_tree Wikipedia entry]. - -[*Boost.Intrusive] offers 3 containers based on splay trees: -[classref boost::intrusive::splay_set splay_set], -[classref boost::intrusive::splay_multiset splay_multiset] and -[classref boost::intrusive::splaytree splaytree]. The first two are similar to -[classref boost::intrusive::set set] or -[classref boost::intrusive::multiset multiset] and the latter is a generalization -that offers functions both to insert unique and multiple keys. - -The memory overhead of these containers with Boost.Intrusive hooks is usually 3 pointers. -An empty, non constant-time size splay container has also the size of 3 pointers. - -[section:splay_set_multiset_disadvantages Advantages and disadvantages of splay tree based containers] - -Splay tree based intrusive containers have logarithmic complexity in many -operations like searches, insertions, erasures, etc... but if some elements are -more frequently accessed than others, splay trees perform faster searches than equivalent -balanced binary trees (such as red-black trees). - -The caching effect offered by splay trees comes with a cost: the tree must be -rebalanced when a element is searched. This disallows const versions of search -functions like `find()`, `lower_bound()`, `upper_bound()`, `equal_range()`, -`count()`... - -Because of this, splay-tree based associative containers are not drop-in -replacements of [classref boost::intrusive::set set]/ -[classref boost::intrusive::splay_set splay_set]. - -Apart from this, if element searches are randomized, the tree will be rebalanced -without taking advantage of the cache effect, so splay trees can offer worse -performance than other balanced trees for some search patterns. - -[endsect] - -[section:splay_set_multiset_hooks splay_set, splay_multiset and splaytree hooks] - -[classref boost::intrusive::splay_set splay_set], -[classref boost::intrusive::splay_multiset splay_multiset] and -[classref boost::intrusive::splaytree splaytree] -share the same hooks. - -[c++] - - template - class splay_set_base_hook; - -* [classref boost::intrusive::splay_set_base_hook splay_set_base_hook]: - the user class derives publicly from this class to make - it compatible with splay tree based containers. - -[c++] - - template - class splay_set_member_hook; - -* [classref boost::intrusive::set_member_hook set_member_hook]: - the user class contains a public member of this class to make - it compatible with splay tree based containers. - -[classref boost::intrusive::splay_set_base_hook splay_set_base_hook] and -[classref boost::intrusive::splay_set_member_hook splay_set_member_hook] receive -the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive]: - -* [*`tag`] (for base hooks only): This argument serves as a tag, - so you can derive from more than one base hook. - Default: `tag`. - -* [*`link_mode`]: The linking policy. - Default: `link_mode`. - -* [*`void_pointer`]: The pointer type to be used - internally in the hook and propagated to the container. - Default: `void_pointer`. - -[endsect] - -[section:set_multiset_containers splay_set, splay_multiset and splaytree containers] - -[c++] - - template - class splay_set; - - template - class splay_multiset; - - template - class splaytree; - -These containers receive the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive]: - -* [*`base_hook`] / [*`member_hook`] / - [*`value_traits`]: To specify the hook type or value traits used - to configure the container (to know about value traits go to the section - titled [link intrusive.value_traits Containers with custom ValueTraits]. - -* [*`constant_time_size`]: To activate the constant-time `size()` operation. - Default: `constant_time_size` - -* [*`size_type`]: To specify the type that will be used to store the size - of the container. Default: `size_type` - -And they also can receive an additional option: - -* [*`compare`]: Comparison function for the objects to be inserted - in containers. The comparison functor must induce a strict weak ordering. - Default: `compare< std::less >` - -[endsect] - -[section:splay_set_bst_hook Splay trees with BST hooks] - -Intrusive splay containers can also use plain binary search tree hooks -[classref boost::intrusive::bs_set_base_hook bs_set_base_hook] and -[classref boost::intrusive::bs_set_base_hook bs_set_base_hook]. -These hooks can be used by other intrusive containers like -intrusive scapegoat containers -[classref boost::intrusive::sg_set sg_set] and -[classref boost::intrusive::sg_multiset sg_multiset] so a programmer -might prefer using a binary search tree hook so that the same type -can be introduced in some situations in an splay container but that -can also be introduced in other compatible containers as well when -the hook is not being used in an splay container. - -[classref boost::intrusive::bs_set_base_hook bs_set_base_hook] and -[classref boost::intrusive::bs_set_base_hook bs_set_member_hook] admit -the same options as [classref boost::intrusive::splay_set_base_hook splay_set_base_hook]. - -[endsect] - -[section:splay_set_multiset_example Example] - -Now let's see an small example using both splay hooks, -binary search tree hooks and -[classref boost::intrusive::splay_set splay_set]/ -[classref boost::intrusive::splay_multiset splay_multiset] -containers: - -[import ../example/doc_splay_set.cpp] -[doc_splay_set_code] - -[endsect] - -[endsect] - -[section:avl_set_multiset Intrusive avl tree based associative containers: avl_set, avl_multiset and avltree] - -Similar to red-black trees, AVL trees are balanced binary trees. -AVL trees are often compared with red-black trees because they support the same set of operations -and because red-black trees also take O(log n) time for the basic operations. -AVL trees are more rigidly balanced than Red-Black trees, leading to slower insertion and -removal but faster retrieval, so AVL trees perform better -than red-black trees for lookup-intensive applications. - -[*Boost.Intrusive] offers 3 containers based on avl trees: -[classref boost::intrusive::avl_set avl_set], -[classref boost::intrusive::avl_multiset avl_multiset] and -[classref boost::intrusive::avltree avltree]. The first two are similar to -[classref boost::intrusive::set set] or -[classref boost::intrusive::multiset multiset] and the latter is a generalization -that offers functions both to insert unique and multiple keys. - -The memory overhead of these containers with Boost.Intrusive hooks is usually 3 -pointers and 2 bits (due to alignment, this usually means 3 pointers plus an integer). -This size can be reduced to 3 pointers if pointers have 4 byte alignment -(which is usually true in 32 bit systems). - -An empty, non constant-time size [classref boost::intrusive::avl_set avl_set], -[classref boost::intrusive::avl_multiset avl_multiset] or -[classref boost::intrusive::avltree avltree] -has also the size of 3 pointers and an integer (3 pointers when optimized for size). - -[section:avl_set_multiset_hooks avl_set, avl_multiset and avltree hooks] - -[classref boost::intrusive::avl_set avl_set], -[classref boost::intrusive::avl_multiset avl_multiset] and -[classref boost::intrusive::avltree avltree] -share the same hooks. - -[c++] - - template - class avl_set_base_hook; - -* [classref boost::intrusive::avl_set_base_hook avl_set_base_hook]: - the user class derives publicly from this class to make - it compatible with avl tree based containers. - -[c++] - - template - class avl_set_member_hook; - -* [classref boost::intrusive::set_member_hook set_member_hook]: - the user class contains a public member of this class to make - it compatible with avl tree based containers. - -[classref boost::intrusive::avl_set_base_hook avl_set_base_hook] and -[classref boost::intrusive::avl_set_member_hook avl_set_member_hook] receive -the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive] plus an option to optimize -the size of the node: - -* [*`tag`] (for base hooks only): This argument serves as a tag, - so you can derive from more than one base hook. - Default: `tag`. - -* [*`link_mode`]: The linking policy. - Default: `link_mode`. - -* [*`void_pointer`]: The pointer type to be used - internally in the hook and propagated to the container. - Default: `void_pointer`. - -* [*`optimize_size`]: The hook will be optimized for size - instead of speed. The hook will embed the balance bits of the AVL - tree node in the parent pointer if pointer alignment is multiple of 4. - Optimizing the size will reduce speed performance a bit since masking - operations will be needed to access parent pointer and balance factor attributes. - Default: `optimize_size`. - -[endsect] - -[section:set_multiset_containers avl_set, avl_multiset and avltree containers] - -[c++] - - template - class avl_set; - - template - class avl_multiset; - - template - class avltree; - -These containers receive the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive]: - -* [*`base_hook`] / [*`member_hook`] / - [*`value_traits`]: To specify the hook type or value traits used - to configure the container (to know about value traits go to the section - titled [link intrusive.value_traits Containers with custom ValueTraits]. - -* [*`constant_time_size`]: To activate the constant-time `size()` operation. - Default: `constant_time_size` - -* [*`size_type`]: To specify the type that will be used to store the size - of the container. Default: `size_type` - -And they also can receive an additional option: - -* [*`compare`]: Comparison function for the objects to be inserted - in containers. The comparison functor must induce a strict weak ordering. - Default: `compare< std::less >` - -[endsect] - -[section:avl_set_multiset_example Example] - -Now let's see an small example using both hooks and -[classref boost::intrusive::avl_set avl_set]/ -[classref boost::intrusive::avl_multiset avl_multiset] -containers: - -[import ../example/doc_avl_set.cpp] -[doc_avl_set_code] - -[endsect] - -[endsect] - - -[section:sg_set_multiset Intrusive scapegoat tree based associative containers: sg_set, sg_multiset and sgtree] - -A scapegoat tree is a self-balancing binary search tree, that provides worst-case O(log n) -lookup time, and O(log n) amortized insertion and deletion time. -Unlike other self-balancing binary search trees that provide worst case O(log n) lookup -time, scapegoat trees have no additional per-node overhead compared to a regular binary -search tree. - -A binary search tree is said to be weight balanced if half the nodes are on the left -of the root, and half on the right. An Îą-height-balanced tree is defined with defined -with the following equation: - -[*['height(tree) <= log1/Îą(tree.size())]] - -* [*['Îą == 1]]: A tree forming a linked list is considered balanced. -* [*['Îą == 0.5]]: Only a perfectly balanced binary is considered balanced. - -Scapegoat trees are loosely ['Îą-height-balanced] so: - -[*['height(tree) <= log1/Îą(tree.size()) + 1]] - -Scapegoat trees support any Îą between 0.5 and 1. If Îą is higher, the tree is rebalanced -less often, obtaining quicker insertions but slower searches. Lower -Îą values improve search times. Scapegoat-trees implemented in [*Boost.Intrusive] offer the possibility of -[*changing Îą at run-time] taking advantage of the flexibility of scapegoat trees. -For more information on scapegoat trees see [@http://en.wikipedia.org/wiki/Scapegoat_tree Wikipedia entry]. - -Scapegoat trees also have downsides: - -* They need additional storage of data on the - root (the size of the tree, for example) to achieve logarithmic complexity operations - so it's not possible to offer `auto_unlink` hooks. The size of an empty scapegoat - tree is also considerably increased. - -* The operations needed to determine if the tree is unbalanced require floating-point - operations like ['log1/Îą]. If the system has no floating point operations (like some - embedded systems), scapegoat tree operations might become slow. - -[*Boost.Intrusive] offers 3 containers based on scapegoat trees: -[classref boost::intrusive::sg_set sg_set], -[classref boost::intrusive::sg_multiset sg_multiset] and -[classref boost::intrusive::sgtree sgtree]. The first two are similar to -[classref boost::intrusive::set set] or -[classref boost::intrusive::multiset multiset] and the latter is a generalization -that offers functions both to insert unique and multiple keys. - -The memory overhead of these containers with Boost.Intrusive hooks is 3 -pointers. - -An empty, [classref boost::intrusive::sg_set sg_set], -[classref boost::intrusive::sg_multiset sg_multiset] or -[classref boost::intrusive::sgtree sgtree] -has also the size of 3 pointers, two integers and two floating point values -(equivalent to the size of 7 pointers on most systems). - -[section:sg_set_multiset_hooks Using binary search tree hooks: bs_set_base_hook and bs_set_member_hook] - -[classref boost::intrusive::sg_set sg_set], -[classref boost::intrusive::sg_multiset sg_multiset] and -[classref boost::intrusive::sgtree sgtree] don't use their -own hooks but plain binary search tree hooks. This has many advantages -since binary search tree hooks can also be used to insert values in -splay containers. - -[c++] - - template - class bs_set_base_hook; - -* [classref boost::intrusive::bs_set_base_hook bs_set_base_hook]: - the user class derives publicly from this class to make - it compatible with scapegoat tree based containers. - -[c++] - - template - class bs_set_member_hook; - -* [classref boost::intrusive::set_member_hook set_member_hook]: - the user class contains a public member of this class to make - it compatible with scapegoat tree based containers. - -[classref boost::intrusive::bs_set_base_hook bs_set_base_hook] and -[classref boost::intrusive::bs_set_member_hook bs_set_member_hook] receive -the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive] plus an option to optimize -the size of the node: - -* [*`tag`] (for base hooks only): This argument serves as a tag, - so you can derive from more than one base hook. - Default: `tag`. - -* [*`link_mode`]: The linking policy. - Default: `link_mode`. - -* [*`void_pointer`]: The pointer type to be used - internally in the hook and propagated to the container. - Default: `void_pointer`. - -[endsect] - -[section:sg_set_multiset_containers sg_set, sg_multiset and sgtree containers] - -[c++] - - template - class sg_set; - - template - class sg_multiset; - - template - class sgtree; - -These containers receive the same options explained in the section -[link intrusive.usage How to use Boost.Intrusive]: - -* [*`base_hook`] / [*`member_hook`] / - [*`value_traits`]: To specify the hook type or value traits used - to configure the container (to know about value traits go to the section - titled [link intrusive.value_traits Containers with custom ValueTraits]. - -* [*`size_type`]: To specify the type that will be used to store the size - of the container. Default: `size_type` - -And they also can receive additional options: - -* [*`compare`]: Comparison function for the objects to be inserted - in containers. The comparison functor must induce a strict weak ordering. - Default: `compare< std::less >` - -* [*`floating_point`]: - When this option is deactivated, the scapegoat tree loses the ability to change - the balance factor Îą at run-time, but the size of an empty container is reduced - and no floating point operations are performed, normally increasing container - performance. The fixed Îą factor that is used when this option is activated - is ['1/sqrt(2) ~ 0,70711]. Default: `floating_point` - -[endsect] - -[section:sg_set_multiset_example Example] - -Now let's see an small example using both hooks and -[classref boost::intrusive::sg_set sg_set]/ -[classref boost::intrusive::sg_multiset sg_multiset] -containers: - -[import ../example/doc_sg_set.cpp] -[doc_sg_set_code] - -[endsect] - -[endsect] - - - - - - - - - - - - - - - - - - - - -[section:advanced_lookups_insertions Advanced lookup and insertion functions for associative containers] - -[section:advanced_lookups Advanced lookups] - -[*Boost.Intrusive] associative containers offer the same interface as STL associative -containers. However, STL and TR1 ordered and unordered simple associative containers -(`std::set`, `std::multiset`, `std::tr1::unordered_set` and `std::tr1::unordered_multiset`) -have some inefficiencies caused by the interface: the user can only operate with `value_type` -objects. When using these containers we must use `iterator find(const value_type &value)` -to find a value. The same happens in other functions -like `equal_range`, `lower_bound`, `upper_bound`... - -However, sometimes the object to be searched it's quite expensive to construct: - -[import ../example/doc_assoc_optimized_code.cpp] -[doc_assoc_optimized_code_normal_find] - -`Expensive` is an expensive object to construct. If "key" c-string is quite long -`Expensive` has to construct a `std::string` using heap memory. Like -`Expensive`, many times the only member taking part in ordering issues is just -a small part of the class. For example, with `Expensive`, only the internal -`std::string` is needed to compare the object. - -In both containers, if we call `get_from_set/get_from_unordered_set` in a loop, we might get a performance penalty, -because we are forced to create a whole `Expensive` object to be able to find an -equivalent one. - -Sometimes this interface limitation is severe, because -we [*might not have enough information to construct the object] but we might -[*have enough information to find the object]. In this case, a name it's enough -to search `Expensive` in the container but constructing an `Expensive` -might require more information that the user might not have. - -To solve this, [classref boost::intrusive::set set]/[classref boost::intrusive::multiset multiset] -offer alternative functions, which take any type comparable with the value and a -functor that should be compatible with the -ordering function of the associative container. -[classref boost::intrusive::unordered_set unordered_set]/[classref boost::intrusive::unordered_multiset unordered_multiset] -offers functions that take any key type and compatible hash and equality functions. Now, let's see the -optimized search function: - -[doc_assoc_optimized_code_optimized_find] - -This new arbitrary key overload is also available for other functions taking -values as arguments: - -* equal_range -* lower_bound -* upper_bound -* count -* find -* erase - -Check [classref boost::intrusive::set set], -[classref boost::intrusive::multiset multiset], -[classref boost::intrusive::unordered_set unordered_set], -[classref boost::intrusive::unordered_multiset unordered_multiset] -references to know more about those functions. - -[endsect] - -[section:advanced_insertions Advanced insertions] - -A similar issue happens with insertions in simple ordered and unordered associative -containers with unique keys (`std::set` and `std::tr1::unordered_set`). In these containers, -if a value is already present, the value to be inserted is discarded. With expensive -values, if the value is already present, we can suffer efficiency problems. - -[classref boost::intrusive::set set] and [classref boost::intrusive::unordered_set unordered_set] -have insertion functions to check efficiently, without -constructing the value, if a value is present or not and if it's not present, a -function to insert it immediately without any further lookup. -For example, using the same `Expensive` class, -this function can be inefficient: - -[doc_assoc_optimized_code_normal_insert] - -If the object is already present, we are constructing an `Expensive` that -will be discarded, and this is a waste of resources. Instead of that, let's use -`insert_check` and `insert_commit` functions: - -[doc_assoc_optimized_code_optimized_insert] - -`insert_check` is similar to a normal `insert` but: - -* `insert_check` can be used with arbitrary keys -* if the insertion is possible (there is no equivalent value) collects all the needed information -in an `insert_commit_data` structure, so that `insert_commit`: - * [*does not execute] further comparisons - * can be executed with [*constant-time complexity] - * has [*no-throw guarantee]. - -These functions must be used with care, since -no other insertion or erasure must be executed between an `insert_check` and an `insert_commit` -pair. Otherwise, the behaviour is undefined. -`insert_check` and `insert_commit` will come handy -for developers programming efficient non-intrusive associative containers. -See [classref boost::intrusive::set set] -and [classref boost::intrusive::unordered_set unordered_set] reference for more information about -`insert_check` and `insert_commit`. - -With multiple ordered and unordered associative containers -([classref boost::intrusive::multiset multiset] and -[classref boost::intrusive::unordered_multiset unordered_multiset]) there's -no need for these advanced insertion functions, since insertions are always succesful. - -[endsect] - -For more information about advanced lookup and insertion functions see -[classref boost::intrusive::set set], -[classref boost::intrusive::multiset multiset], -[classref boost::intrusive::unordered_set unordered_set] and -[classref boost::intrusive::unordered_multiset unordered_multiset] references. - -[endsect] - -[section:erasing_and_disposing Erasing and disposing values from Boost.Intrusive containers] - -One of the most tedious tasks when using intrusive containers is the management of the erased elements. -When using STL containers, the container itself unlinks and destroys the contained elements, but with -intrusive containers, the user must explicitly destroy the object after erasing an element from the container. -This makes STL-like functions erasing multiple objects unhelpful: the user can't destroy every erased element. -For example, let's take the function `remove_if` from [classref boost::intrusive::list list]: - -[c++] - - template - void remove_if(Pred pred); - -How can the user destroy the elements (say, using `operator delete`) that will be erased according -to the predicate? [*Boost.Intrusive] containers offer additional functions that take a function -object that will be called after the element has been erased from the container. For example, -[classref boost::intrusive::list list] offers: - -[c++] - - template - void remove_and_dispose_if(Pred pred, Disposer disposer) - -With this function the user can efficiently remove and destroy elements if the disposer -function destroys an object: `remove_and_dispose_if` -will call "disposer" function object for every removed element. [classref boost::intrusive::list list] offers -more functions taking a disposer function object as argument, like `erase_and_dispose`, `clear_and_dispose`, -`remove_and_dispose`... - -Note that the disposing function does not need to just destroy the object. It can -implement any other operation like inserting the remove object in another container. -Let's see an small example: - -[import ../example/doc_erasing_and_disposing.cpp] -[doc_erasing_and_disposing] - -All [*Boost.Intrusive] containers offer these "erase + dispose" additional members for all functions -that erase an element from the container. - - - -[endsect] - -[section:clone_from Cloning [*Boost.Intrusive] containers] - -As previously mentioned, [*Boost.Intrusive] containers are [*non-copyable and non-assignable], because -intrusive containers don't allocate memory at all. To implement a copy-constructor or assignment operator, -the user must clone one by one all the elements of the container and insert them in another intrusive container. -However, cloning by hand is usually more inefficient than a member cloning function and an specialized cloning -function can offer more guarantees than the manual cloning (better exception safety guarantees, for example). - -To ease the implementation of copy constructors and assignment operators of classes containing [*Boost.Intrusive] -containers, all [*Boost.Intrusive] containers offer an special cloning function called `clone_from`. - -Apart from the container to be cloned, `clone_from` takes two function objects as arguments. For example, the -`clone_from` member function of [classref boost::intrusive::list list]: - -[c++] - - template - void clone_from(const list &src, Cloner cloner, Disposer disposer); - -This function will make `*this` a clone of `src`. Let's explain the arguments: - -* The first parameter is the list to be cloned. -* The second parameter is a function object that will clone `value_type` objects and - return a pointer to the clone. It must implement the following function: - `pointer operator()(const value_type &)`. -* The second parameter is a function object that will dispose `value_type` objects. It's used first - to empty the container before cloning and to dispose the elements if an exception is thrown. - -The cloning function works as follows: - -* First clears and disposes all the elements from *this using the disposer function object. -* After that starts cloning all the elements of the source container using the cloner function object. -* If any operation in the cloning function (for example, the cloner function object) throws, - all the constructed elements are disposed using the disposer function object. - - -Here's an example of `clone_from`: - -[import ../example/doc_clone_from.cpp] -[doc_clone_from] - -[endsect] - -[section:using_smart_pointers Using smart pointers with Boost.Intrusive containers] - -[*Boost.Intrusive] hooks can be configured to use other pointers than raw pointers. -When a [*Boost.Intrusive] hook is configured with an smart pointer as an argument, -this pointer configuration is passed to the containers. For example, if the following -hook is configured with an smart pointer (for example, an offset pointer from -[*Boost.Interprocess]): - -[import ../example/doc_offset_ptr.cpp] -[doc_offset_ptr_0] - -Any intrusive list constructed using this hook will be ready for shared memory, -because the intrusive list will also use offset pointers internally. For example, -we can create an intrusive list in shared memory combining [*Boost.Interprocess] -and [*Boost.Intrusive]: - -[doc_offset_ptr_1] - -[section:smart_pointers_requirements Requirements for smart pointers compatible with Boost.Intrusive] - -Not every smart pointer is compatible with [*Boost.Intrusive], the smart pointer must -have the following features: - -* It must support the same operations as a raw pointer, except casting. -* It must be convertible to a raw pointer and constructible from a raw pointer. -* It must have the same ownership semantics as a raw pointer. This means that - resource management smart pointers (like `boost::shared_ptr`) can't be used. - -The conversion from the smart pointer to a raw pointer must be implemented following -Boost smart pointer `detail::get_pointer()` function. This function will be found using -ADL. For example, for `boost::interprocess::offset_ptr` `detail::get_pointer` is defined -as follows: - -[c++] - - template - T * detail::get_pointer(boost::interprocess::offset_ptr const & p) - { return p.get(); } - -[endsect] - -[endsect] - -[section:obtaining_iterators_from_values Obtaining iterators from values] - -[*Boost.Intrusive] offers another useful feature that's not present in STL -containers: it's possible to obtain an iterator to a value from the value itself. -This feature is implemented in [*Boost.Intrusive] containers by a -function called `iterator_to`: - -[c++] - - iterator iterator_to(reference value); - const_iterator iterator_to(const_reference value); - -For [*Boost.Intrusive] containers that have local iterators, like unordered -associative containers, we can also obtain local iterators: - -[c++] - - local_iterator local_iterator_to(reference value); - const_local_iterator local_iterator_to(const_reference value) const; - -For most [*Boost.Intrusive] containers -([classref boost::intrusive::list list], -[classref boost::intrusive::slist slist], -[classref boost::intrusive::set set], -[classref boost::intrusive::multiset multiset]) we have an alternative -static `s_iterator_to` function. - -For unordered associative containers -([classref boost::intrusive::unordered_set unordered_set], -[classref boost::intrusive::multiset multiset]), -`iterator_to` has no static alternative function. -On the other hand, `local_iterator_to` functions -have their `s_local_iterator_to` static alternatives. - -Alternative static functions are available under certain circunstances -explained in the [link intrusive.value_traits.stateful_value_traits Stateful value traits] section, -but the programmer uses hooks provided by [*Boost.Intrusive], those functions -will be available. - -Let's see an small function that shows the use of `iterator_to` and -`local_iterator_to`: - -[import ../example/doc_iterator_from_value.cpp] -[doc_iterator_from_value] - -[endsect] - -[section:concepts Concepts explained] - -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... 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 algorithms - to manage a group of nodes forming a circular singly linked list: - -[c++] - - template - struct my_slist_algorithms - { - typedef typename NodeTraits::node_ptr node_ptr; - typedef typename NodeTraits::const_node_ptr const_node_ptr; - - //Get the previous node of "this_node" - static node_ptr get_prev_node(node_ptr this_node) - { - node_ptr p = this_node; - while (this_node != NodeTraits::get_next(p)) - p = NodeTraits::get_next(p); - return p; - } - - // number of elements in the group of nodes containing "this_node" - static std::size_t count(const_node_ptr this_node) - { - std::size_t result = 0; - const_node_ptr p = this_node; - do{ - p = NodeTraits::get_next(p); - ++result; - } while (p != this_node); - return result; - } - - // More operations - // ... - }; - -* [*Node Traits]: A class that encapsulates the basic information and - operations on a node that forms a group of nodes: - the type of the node, a function to obtain the pointer to the next node... - [*Node Traits] are the configuration information [*Node Algorithms] - need. Each type of [*Node Algorithms] expects an interface that compatible - [*Node Traits] must implement. - As an example, this is the definition of a [*Node Traits] class that - is compatible with the previously presented `my_slist_algorithms`: - -[c++] - - struct my_slist_node_traits - { - //The type of the node - struct node - { - node *next_; - }; - - typedef node * node_ptr; - typedef const node * const_node_ptr; - - //A function to obtain a pointer to the next node - static node_ptr get_next(const_node_ptr n) - { return n->next_; } - - //A function to set the pointer to the next node - static void set_next(node_ptr n, node_ptr next) - { 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: - -[c++] - - class my_slist_base_hook - //This hook contains a node, that will be used - //to link the user object in the group of nodes - : private my_slist_node_traits::node - { - typedef my_slist_node_traits::node_ptr node_ptr; - typedef my_slist_node_traits::const_node_ptr const_node_ptr; - - //Converts the generic node to the hook - static my_slist_base_hook *to_hook_ptr(node_ptr p) - { return static_cast(p); } - - //Returns the generic node stored by this hook - node_ptr to_node_ptr() - { return static_cast(this); } - - // More operations - // ... - }; - - //To make MyClass compatible with an intrusive singly linked list - //derive our class from the hook. - class MyClass - : public my_slist_base_hook - { - void set(int value); - int get() const; - - private: - int value_; - }; - -* [*Intrusive Container]: A container that offers an 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 elaborated - than a group of nodes: it can store the number of elements to achieve constant-time - size information, it can offer debugging facilities... - For example, an [classref boost::intrusive::slist 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 [classref boost::intrusive::slist slist] implementation: - -[c++] - - template - class slist - { - public: - typedef typename ValueTraits::value_type value_type; - - //More typedefs and functions - // ... - - //Insert the value as the first element of the list - void push_front (reference value) - { - node_ptr to_insert(ValueTraits::to_node_ptr(value)); - circular_list_algorithms::link_after(to_insert, get_root_node()); - } - - // More operations - // ... - }; - -* [*Pseudo-Intrusive Container]: A pseudo-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: - -[c++] - - struct my_slist_derivation_value_traits - { - public: - typedef slist_node_traits node_traits; - typedef MyClass value_type; - typedef node_traits::node_ptr node_ptr; - typedef value_type* pointer; - //... - - //Converts user's value to a generic node - static node_ptr to_node_ptr(reference value) - { return static_cast(value).to_node_ptr(); } - - //Converts a generic node into user's value - static value_type *to_value_ptr(node_traits::node *n) - { static_cast(slist_base_hook::to_hook_ptr(n)); } - - // More operations - // ... - }; - -[endsect] - -[section:node_algorithms Node algorithms with custom NodeTraits] - -As explained in the [link intrusive.concepts Concepts] section, [*Boost.Intrusive] -containers are implemented using node algorithms that work on generic nodes. - -Sometimes, the use of intrusive containers is expensive for some environments -and the programmer might want to avoid all the template instantiations -related to [*Boost.Intrusive] containers. However, the user can still benefit -from [*Boost.Intrusive] using the node algorithms, because some of those algorithms, -like red-black tree algorithms, are not trivial to write. - -All node algorithm classes are -templatized by a `NodeTraits` class. This class encapsulates the needed internal -type declarations and operations to make a node compatible with node algorithms. -Each type of node algorithms has its own requirements: - -[section:circular_slist_algorithms Intrusive singly linked list algorithms] - -These algorithms are static -members of the [classref boost::intrusive::circular_slist_algorithms circular_slist_algorithms] class: - -[c++] - - template - struct circular_slist_algorithms; - -An empty list is formed by a node whose pointer to the next node points -to itself. [classref boost::intrusive::circular_slist_algorithms circular_slist_algorithms] -is configured with a NodeTraits class, which encapsulates -the information about the node to be manipulated. NodeTraits must support the -following interface: - -[*Typedefs]: - -* `node`: The type of the node that forms the circular list - -* `node_ptr`: The type of a pointer to a node (usually node*) - -* `const_node_ptr`: The type of a pointer to a const node (usually const node*) - -[*Static functions]: - -* `static node_ptr get_next(const_node_ptr n);`: - Returns a pointer to the next node stored in "n". - -* `static void set_next(node_ptr n, node_ptr next);`: - Sets the pointer to the next node stored in "n" to "next". - -Once we have a node traits configuration we can use [*Boost.Intrusive] algorithms -with our nodes: - -[import ../example/doc_slist_algorithms.cpp] -[doc_slist_algorithms_code] - -For a complete list of functions see -[classref boost::intrusive::circular_slist_algorithms circular_slist_algorithms reference]. - -[endsect] - -[section:circular_list_algorithms Intrusive doubly linked list algorithms] - -These algorithms are static -members of the [classref boost::intrusive::circular_list_algorithms circular_list_algorithms] class: - -[c++] - - template - struct circular_list_algorithms; - -An empty list is formed by a node whose pointer to the next node points -to itself. [classref boost::intrusive::circular_list_algorithms circular_list_algorithms] -is configured with a NodeTraits class, which encapsulates -the information about the node to be manipulated. NodeTraits must support the -following interface: - -[*Typedefs]: - -* `node`: The type of the node that forms the circular list - -* `node_ptr`: The type of a pointer to a node (usually node*) - -* `const_node_ptr`: The type of a pointer to a const node (usually const node*) - -[*Static functions]: - -* `static node_ptr get_next(const_node_ptr n);`: - Returns a pointer to the next node stored in "n". - -* `static void set_next(node_ptr n, node_ptr next);`: - Sets the pointer to the next node stored in "n" to "next". - -* `static node_ptr get_previous(const_node_ptr n);`: - Returns a pointer to the previous node stored in "n". - -* `static void set_previous(node_ptr n, node_ptr prev);`: - Sets the pointer to the previous node stored in "n" to "prev". - -Once we have a node traits configuration we can use [*Boost.Intrusive] algorithms -with our nodes: - -[import ../example/doc_list_algorithms.cpp] -[doc_list_algorithms_code] - -For a complete list of functions see -[classref boost::intrusive::circular_list_algorithms circular_list_algorithms reference]. - -[endsect] - -[section:rbtree_algorithms Intrusive red-black tree algorithms] - -These algorithms are static -members of the [classref boost::intrusive::rbtree_algorithms rbtree_algorithms] class: - -[c++] - - template - struct rbtree_algorithms; - - -An empty tree is formed by a node whose pointer to the parent node is null, -the pointers to the left and right nodes to itself and whose color is red. -[classref boost::intrusive::rbtree_algorithms rbtree_algorithms] -is configured with a NodeTraits class, which encapsulates -the information about the node to be manipulated. NodeTraits must support the -following interface: - -[*Typedefs]: - -* `node`: The type of the node that forms the circular rbtree - -* `node_ptr`: The type of a pointer to a node (usually node*) - -* `const_node_ptr`: The type of a pointer to a const node (usually const node*) - -* `color`: The type that can store the color of a node - -[*Static functions]: - -* `static node_ptr get_parent(const_node_ptr n);`: - Returns a pointer to the parent node stored in "n". - -* `static void set_parent(node_ptr n, node_ptr p);`: - Sets the pointer to the parent node stored in "n" to "p". - -* `static node_ptr get_left(const_node_ptr n);`: - Returns a pointer to the left node stored in "n". - -* `static void set_left(node_ptr n, node_ptr l);`: - Sets the pointer to the left node stored in "n" to "l". - -* `static node_ptr get_right(const_node_ptr n);`: - Returns a pointer to the right node stored in "n". - -* `static void set_right(node_ptr n, node_ptr r);`: - Sets the pointer to the right node stored in "n" to "r". - -* `static color get_color(const_node_ptr n);`: - Returns the color stored in "n". - -* `static void set_color(node_ptr n, color c);`: - Sets the color stored in "n" to "c". - -* `static color black();`: - Returns a value representing the black color. - -* `static color red();`: - Returns a value representing the red color. - -Once we have a node traits configuration we can use [*Boost.Intrusive] algorithms -with our nodes: - -[import ../example/doc_rbtree_algorithms.cpp] -[doc_rbtree_algorithms_code] - -For a complete list of functions see -[classref boost::intrusive::rbtree_algorithms rbtree_algorithms reference]. - -[endsect] - -[section:splaytree_algorithms Intrusive splay tree algorithms] - -These algorithms are static -members of the [classref boost::intrusive::splaytree_algorithms splaytree_algorithms] class: - -[c++] - - template - struct splaytree_algorithms; - - -An empty tree is formed by a node whose pointer to the parent node is null, -the pointers to the left and right nodes to itself. -[classref boost::intrusive::splaytree_algorithms splaytree_algorithms] -is configured with a NodeTraits class, which encapsulates -the information about the node to be manipulated. NodeTraits must support the -following interface: - -[*Typedefs]: - -* `node`: The type of the node that forms the circular splaytree - -* `node_ptr`: The type of a pointer to a node (usually node*) - -* `const_node_ptr`: The type of a pointer to a const node (usually const node*) - -[*Static functions]: - -* `static node_ptr get_parent(const_node_ptr n);`: - Returns a pointer to the parent node stored in "n". - -* `static void set_parent(node_ptr n, node_ptr p);`: - Sets the pointer to the parent node stored in "n" to "p". - -* `static node_ptr get_left(const_node_ptr n);`: - Returns a pointer to the left node stored in "n". - -* `static void set_left(node_ptr n, node_ptr l);`: - Sets the pointer to the left node stored in "n" to "l". - -* `static node_ptr get_right(const_node_ptr n);`: - Returns a pointer to the right node stored in "n". - -* `static void set_right(node_ptr n, node_ptr r);`: - Sets the pointer to the right node stored in "n" to "r". - -Once we have a node traits configuration we can use [*Boost.Intrusive] algorithms -with our nodes: - -[import ../example/doc_splaytree_algorithms.cpp] -[doc_splaytree_algorithms_code] - -For a complete list of functions see -[classref boost::intrusive::splaytree_algorithms splaytree_algorithms reference]. - -[endsect] - -[section:avltree_algorithms Intrusive avl tree algorithms] - -[classref boost::intrusive::avltree_algorithms avltree_algorithms] have the same -interface as [classref boost::intrusive::rbtree_algorithms rbtree_algorithms]. - -[c++] - - template - struct avltree_algorithms; - -[classref boost::intrusive::avltree_algorithms avltree_algorithms] -is configured with a NodeTraits class, which encapsulates -the information about the node to be manipulated. NodeTraits must support the -following interface: - -[*Typedefs]: - -* `node`: The type of the node that forms the circular avltree - -* `node_ptr`: The type of a pointer to a node (usually node*) - -* `const_node_ptr`: The type of a pointer to a const node (usually const node*) - -* `balance`: A type that can represent 3 balance types (usually an integer) - -[*Static functions]: - -* `static node_ptr get_parent(const_node_ptr n);`: - Returns a pointer to the parent node stored in "n". - -* `static void set_parent(node_ptr n, node_ptr p);`: - Sets the pointer to the parent node stored in "n" to "p". - -* `static node_ptr get_left(const_node_ptr n);`: - Returns a pointer to the left node stored in "n". - -* `static void set_left(node_ptr n, node_ptr l);`: - Sets the pointer to the left node stored in "n" to "l". - -* `static node_ptr get_right(const_node_ptr n);`: - Returns a pointer to the right node stored in "n". - -* `static void set_right(node_ptr n, node_ptr r);`: - Sets the pointer to the right node stored in "n" to "r". - -* `static balance get_balance(const_node_ptr n);`: - Returns the balance factor stored in "n". - -* `static void set_balance(node_ptr n, balance b);`: - Sets the balance factor stored in "n" to "b". - -* `static balance negative();`: - Returns a value representing a negative balance factor. - -* `static balance zero();`: - Returns a value representing a zero balance factor. - -* `static balance positive();`: - Returns a value representing a positive balance factor. - -Once we have a node traits configuration we can use [*Boost.Intrusive] algorithms -with our nodes: - -[import ../example/doc_avltree_algorithms.cpp] -[doc_avltree_algorithms_code] - -For a complete list of functions see -[classref boost::intrusive::avltree_algorithms avltree_algorithms reference]. - -[endsect] - -[endsect] - -[section:value_traits Containers with custom ValueTraits] - -As explained in the [link intrusive.concepts Concepts] section, [*Boost.Intrusive] -containers need a `ValueTraits` class to perform transformations between nodes and -user values. `ValueTraits` can be explicitly configured (using the `value_traits<>` option) -or implicitly configured (using hooks and their `base_hook<>`/`member_hook<>` options). -`ValueTraits` contains -all the information to glue the `value_type` of the containers and the node to be -used in node algorithms, since these types can be different. Apart from this, -`ValueTraits` also store information about the link policy of the values to be inserted. - -Instead of using [*Boost.Intrusive] predefined hooks -a user might want to develop customized containers, for example, using nodes that are -optimized for an specific -application or that are compatible with a a legacy ABI. A user might want -to have only two additional pointers in his class and insert the class in a doubly -linked list sometimes and in a singly linked list in other situations. You can't -achieve this using [*Boost.Intrusive] predefined hooks. Now, instead of using -`base_hook<...>` or `member_hook<...>` options the user will specify the -`value_traits<...>` options. Let's see how we can do this: - -[section:value_traits_interface ValueTraits interface] - -`ValueTraits` have the following interface: - -[c++] - - #include - #include - - struct my_value_traits - { - typedef implementation_defined node_traits; - typedef implementation_defined value_type; - typedef node_traits::node_ptr node_ptr; - typedef node_traits::const_node_ptr const_node_ptr; - typedef boost::pointer_to_other::type pointer; - typedef boost::pointer_to_other::type const_pointer; - - static const link_mode_type link_mode = some_linking_policy; - - static node_ptr to_node_ptr (value_type &value); - static const_node_ptr to_node_ptr (const value_type &value); - static pointer to_value_ptr (node_ptr n); - static const_pointer to_value_ptr (const_node_ptr n); - }; - -Let's explain each type and function: - -* [*['node_traits]]: The node configuration that it's needed by node algorithms. - These node traits and algorithms are - described in the previous chapter: [link intrusive.node_algorithms Nodes Algorithms]. - - * If my_value_traits is meant to be used with [classref boost::intrusive::slist slist], - `node_traits` should follow - the interface needed by [classref boost::intrusive::circular_slist_algorithms circular_slist_algorithms]. - - * If my_value_traits is meant to be used with [classref boost::intrusive::list list], - `node_traits` should follow - the interface needed by [classref boost::intrusive::circular_list_algorithms circular_list_algorithms]. - - * If my_value_traits is meant to be used with [classref boost::intrusive::set set]/[classref boost::intrusive::multiset multiset], - `node_traits` should follow - the interface needed by [classref boost::intrusive::rbtree_algorithms rbtree_algorithms]. - - * If my_value_traits is meant to be used with [classref boost::intrusive::unordered_set unordered_set]/ - [classref boost::intrusive::unordered_multiset unordered_multiset], `node_traits` - should follow the interface needed by [classref boost::intrusive::circular_slist_algorithms circular_slist_algorithms]. - -* [*['node_ptr]]: A typedef for `node_traits::node_ptr`. - -* [*['const_node_ptr]]: A typedef for `node_traits::const_node_ptr`. - -* [*['value_type]]: The type that the user wants to insert in the container. This type can be - the same as `node_traits::node` but it can be different (for example, `node_traits::node` - can be a member type of `value_type`). If `value_type` and `node_traits::node` are the - same type, the `to_node_ptr` and `to_value_ptr` functions are trivial. - -* [*['pointer]]: The type of a pointer to a `value_type`. It must be the same pointer type - as `node_ptr`: If `node_ptr` is `node *` `pointer` must be `value_type*`. If - `node_ptr` is `smart_ptr`, `pointer` must be `smart_ptr`. - This can be generically achieved using `boost::pointer_to_other` utility from [*Boost SmartPointers] - defined in ``. - -* [*['const_pointer]]: The type of a pointer to a `const value_type`. It must be the same pointer type - as `node_ptr`: If `node_ptr` is `node *` `const_pointer` must be `const value_type*`. If - `node_ptr` is `smart_ptr`, `const_pointer` must be `smart_ptr` - This can be generically achieved using `boost::pointer_to_other` utility from [*Boost SmartPointers] - defined in ``. - -* [*['link_mode]]: Indicates that `value_traits` needs some additional work or checks from the - container. The types are enumerations defined in the `link_mode.hpp` header. - These are the possible types: - - * [*`normal_link`]: If this linking policy is specified in a `ValueTraits` class - as the link, containers - configured with such `ValueTraits` won't set the hooks - of the erased values to a default state. Containers also won't - check that the hooks of the new values are default initialized. - normal_link, - - * [*`safe_link`]: If this linking policy is specified in a `ValueTraits` class - as the link, containers - configured with such `ValueTraits` will set the hooks - of the erased values to a default state. Containers also will - check that the hooks of the new values are default initialized. - - * [*`auto_unlink`]: Same as "safe_link" but containers with - constant-time size features won't be - compatible with `ValueTraits` configured with this policy. - Containers also know that the a value can be silently erased from - the container without using any function provided by the containers. - -* [*['static node_ptr to_node_ptr (value_type &value)]] and - [*['static const_node_ptr to_node_ptr (const value_type &value)]]: - These function take a reference to a value_type and return a pointer to the node - to be used with node algorithms. - -* [*['static pointer to_value_ptr (node_ptr n)]] and - [*['static const_pointer to_value_ptr (const_node_ptr n)]]: - These function take a pointer to a node and return a pointer to the value - that contains the node. - -[endsect] - -[section:value_traits_example Custom ValueTraits example] - -Let's define our own `value_traits` class to be able to use [*Boost.Intrusive] -containers with an old C structure whose definition can't be changed. -That legacy type has two pointers that can be used to build singly and doubly linked -lists: in singly linked lists we only need a pointer, whereas in doubly -linked lists, we need two pointers. Since we only have two pointers, we can't insert -the object in a singly and doubly linked list at the same time. -This is the definition of the old node: - -[import ../example/doc_value_traits.cpp] -[doc_value_traits_code_legacy] - -Now we have to define a NodeTraits class that will implement the functions/typedefs -that will make the legacy node compatible with [*Boost.Intrusive] algorithms. After that, -we'll define a ValueTraits class that will configure [*Boost.Intrusive] containers: - -[doc_value_traits_value_traits] - -Defining a value traits class that just defines `value_type` as -`legacy_node_traits::node` is a common approach when defining customized -intrusive containers, so [*Boost.Intrusive] offers a templatized -[classref boost::intrusive::trivial_value_traits trivial_value_traits] class -that does exactly what we want: - -[c++] - - #include - - //Now we can define legacy_value_traits just with a single line - using namespace boost::intrusive; - typedef trivial_value_traits legacy_value_traits; - - -Now we can just define the containers that will store the legacy abi objects and write -a little test: - -[doc_value_traits_test] - -As seen, several key elements of [*Boost.Intrusive] can be reused with custom user types, -if the user does not want to use provided [*Boost.Intrusive] facilities. - -[endsect] - -[section:reusing_node_algorithms Reusing node algorithms for different values] - -In the previous example, `legacy_node_traits::node` type and -`legacy_value_traits::value_type` are the same type, but this is not necessary. It's possible -to have several `ValueTraits` defining the same `node_traits` type (and thus, the same `node_traits::node`). -This reduces the number of node algorithm instantiations, but -now `ValueTraits::to_node_ptr` and `ValueTraits::to_value_ptr` functions need to offer -conversions between both types. Let's see an small example: - -First, we'll define the node to be used in the algorithms. For a linked list, -we just need a node that stores two pointers: - -[import ../example/doc_advanced_value_traits.cpp] -[doc_advanced_value_traits_code] - -Now we'll define two different types that will be inserted in intrusive lists and -we'll define a templatized `ValueTraits` that will work for both types: - -[doc_advanced_value_traits_value_traits] - -Now define two containers. Both containers will instantiate the same list algorithms -(`circular_list_algorithms`), -due to the fact that the value traits used to define the containers provide the same `node_traits` type: - -[doc_advanced_value_traits_containers] - -All [*Boost.Intrusive] containers using predefined hooks use this technique to minimize code size: -all the possible [classref boost::intrusive::list list] containers -created with predefined hooks that define the same `VoidPointer` type -share the same list algorithms. - -[endsect] - -[section:simplifying_value_traits Simplifying value traits definition] - -The previous example can be further simplified using -[classref boost::intrusive::derivation_value_traits derivation_value_traits] -class to define a value traits class with a value that stores the -`simple_node` as a base class: - -[c++] - - #include - - //value_1, value_2, simple_node and simple_node_traits are defined - //as in the previous example... - //... - - using namespace boost::intrusive; - - //Now define the needed value traits using - typedef derivation_value_traits ValueTraits1; - typedef derivation_value_traits ValueTraits2; - - //Now define the containers - typedef list > Value1List; - typedef list > Value2List; - -We can even choose to store `simple_node` as a member of `value_1` and `value_2` -classes and use [classref boost::intrusive::member_value_traits member_value_traits] -to define the needed value traits classes: - -[import ../example/doc_advanced_value_traits2.cpp] -[doc_advanced_value_traits2_value_traits] - -[endsect] - -[section:stateful_value_traits Stateful value traits] - -Until now all shown custom value traits are stateless, that is, [*the transformation between nodes -and values is implemented in terms of static functions]. It's possible to use [*stateful] value traits -so that we can even separate nodes and values and [*avoid modifying types to insert nodes]. -[*Boost.Intrusive] differentiates between stateful and stateless value traits checking if the ValueTraits -class is empty: - -* If the class is empty, a [*stateless] value traits is assumed. - Node <-> Value transformations must be static functions. -* If the class is not empty, a [*stateful] value traits is assumed. - Node <-> Value transformations must be non-static functions. - -Using stateful value traits it's possible to create containers of non-copyable/moveble objects [*without modifying] -the definition of the class to be inserted. This interesting property is achieved without using global variables -(stateless value traits could use global variables to achieve the same property), so: - -* [*Thread-safety guarantees]: Better thread-safety guarantees can be achieved with stateful - value traits, since accessing to global resources might require syncronization primitives that - can be avoided when using the internal state. -* [*Flexibility]: A stateful value traits type can be configured at run-time. -* [*Run-time polimorphism]: A value traits might implement node <-> value - transformations as virtual functions. A single container type could be - configured at run-time to use different node <-> value relatioships. - -Stateful value traits have many advantages but also some downsides: - -* [*Performance]: Value traits operations should be very efficient since they are basic operations used by containers. - [*A heavy node <-> value transformation can downgrade intrusive containers' performance]. -* [*Exception guarantees]: The stateful ValueTraits must maintain no-throw guarantees, otherwise, the - container invariants won't be preserved. -* [*Static functions]: Some static functions offered by intrusive containers are not - available because node <-> value transformations are not static. -* [*Bigger iterators]: The size of some iterators is increased because the iterator - needs to store a pointer to the stateful value traits to implement node to value - tranformations (e.g. `operator*()` and `operator->()`). - -An easy and useful example of stateful value traits is when an array of values can be indirectly introduced -in a list guaranteeing no additional allocation apart from the initial resource reservation: - -[import ../example/doc_stateful_value_traits.cpp] -[doc_stateful_value_traits] - -[endsect] - -[endsect] - -[section:thread_safety Thread safety guarantees] - -Intrusive containers have similar same thread-safety guarantees than STL containers. - -* Several threads can have read or write access to different instances is safe as long as inserted - objects are different. -* Concurrent read-only access to the same container is safe. - -Some Intrusive hooks (auto-unlink hooks, for example) modify containers without -having a reference to them: this is considered a write access to the container. - -Other functions, like checking if an objects is already inserted in a containers using the `is_linked()` -member of safe hooks is a read-access to the container without having a reference to them, so no other -thread should have write access (direct or indirect) to that container. - -Since the same object can be inserted in several containers at the same time using different hooks, -the thread safety of [*Boost.Intrusive] is related to the containers and also the object whose lifetime -is manually managed by the user. - -As we can see, the analysis of the thread-safety of a program using [*Boost.Intrusive] is harder -than with non-intrusive containers. - -To analyze the thread-safety, take in care the following points: - -* Auto-unlink hook's destructor and `unlink()` functions modify the container indirectly. -* Safe mode and auto-unlink hook's `is_linked()` function is a read access to the container. -* Inserting an object in several containers that will be modified by different threads has no thread-safety - guarantee, although in most platforms it will be thread-safe without locking. - -[endsect] - -[section:obtaining_same_type_reducing_space Obtaining the same types and reducing symbol length] - -The flexible option specification mechanism used by [*Boost.Intrusive] for hooks and containers -has also a couple of downsides: - -* If a user specifies the same options in different order or specifies some options and lefts the - rest as defaults the type of the created container/hook will be different. Sometimes - this is annoying, because two programmers specifying the same options might end with incompatible - types. For example, the following two lists, although they're using the same options, have not - the same type: - -[c++] - - #include - - using namespace boost::intrusive; - - //Explicitly specify constant-time size and size type - typedef list, size_type List1; - - //Implicitly specify constant-time size and size type - typedef list List2; - -* Option specifiers lead to long template symbols for classes and functions. Option specifiers themselves - are verbose and without variadic templates, several default template parameters are assigned for - non-specified options. Object and debugging information files can grow and compilation times - might suffer a bit if long names are produced. - -To solve these issues [*Boost.Intrusive] offers some helper metafunctions that that reduce symbol lengths -and create the same type if the same options (either explicitly or implicitly) are used. This also -improves compilation times. All containers and hooks have their respective `make_xxx` versions. -Previous shown example can be rewritten like this to obtain the same list type: - -[c++] - - #include - - using namespace boost::intrusive; - - #include - - using namespace boost::intrusive; - - //Explicitly specify constant-time size and size type - typedef make_list, size_type::type List1; - - //Implicitly specify constant-time size and size type - typedef make_list::type List2; - -Produced symbol lengths and compilation times are usually shorter and object/debug files are smaller. -If you are a programmer concerned with file sizes and compilation times, this option is your choice. - -[endsect] - -[section:design_notes Design Notes] - -When designing [*Boost.Intrusive] the following guidelines have been taken into account: - -[section: Boost.Intrusive in performance sensitive environments] - -[*Boost.Intrusive] should be a valuable tool in performance sensitive environments, -and following this guideline, [*Boost.Intrusive] has been designed to offer well -known complexity guarantees. Apart from that, some options, like optional -constant-time, have been designed to offer faster complexity guarantees in some -functions, like `slist::splice`. - -The advanced lookup and insertion functions for associative containers, taking -an arbitrary key type and predicates, were designed to avoid unnecessary object -constructions. - -[endsect] - -[section: Boost.Intrusive in space constrained environments] - -[*Boost.Intrusive] should be useful in space constrained environments, -and following this guideline [*Boost.Intrusive] separates node algorithms -and intrusive containers to avoid instantiating node algorithms for each -user type. For example, a single class of red-black algorithms will be instantiated -to implement all set and multiset containers using raw pointers. This way, -[*Boost.Intrusive] wants to avoid any code size overhead associated with templates. - -Apart from that, [*Boost.Intrusive] implements some size improvements: for example, -red-black trees embed the color bit in the parent pointer lower bit, if nodes -are two-byte aligned. The possibility to avoid constant-time size operations can -save some size on containers, and this extra size optimization is noticeable -when the container is empty or contains few values. - -[endsect] - -[section: Boost.Intrusive as basic building block] - -[*Boost.Intrusive] should be a basic building block to build more complex containers -and this guideline has motivated many design decisions. For example, the possibility -to have more than one hook per user type opens the possibility to implement multi-index -containers on top of [*Boost.Intrusive]. - -[*Boost.Intrusive] containers implement advanced functions taking function objects -as arguments (`clone_from`, `erase_and_dispose`, `insert_check`...). These -functions come handy when implementing non-intrusive containers -(for example, STL-like containers) on top of intrusive containers. - -[endsect] - -[section: Extending Boost.Intrusive] - -[*Boost.Intrusive] offers a wide range of containers but also allows the -construction of custom containers reusing [*Boost.Intrusive] elements. -The programer might want to use node algorithms directly or -build special hooks that take advantage of its application environment. - -For example, the programmer can use can customize parts of [*Boost.Intrusive] -to manage old data structures whose definition can't be changed. - -[endsect] - -[endsect] - -[section:performance Performance] - -[*Boost.Intrusive] containers offer speed improvements comparing to non-intrusive containers, -basically because: - -* We can minimize memory allocation/deallocation calls. -* We obtain better memory locality. - -This section will show some performance tests comparing some operations on -`boost::intrusive::list` and `std::list`: - -* Insertions using `push_back` and container destruction will show the - overhead associated with memory allocation/deallocation. -* `reverse` member function will show the advantages of the compact - memory representation that can be achieved with intrusive containers. -* `sort` and `write access` tests will show the advantage of intrusive containers - minimizing the memory accesses when comparing them with containers of pointers. - -Given an object of type `T`, [classref boost::intrusive::list boost::intrusive::list] -can replace `std::list` to avoid memory allocation overhead, -or it can replace `std::list` when the user wants to obtain containers with -polymorphic values or wants to share values between several containers. -Because of this versatility, the performance tests will be executed for 6 different -list types: - -* 3 intrusive lists holding a class named `itest_class`, - each one with a different linking policy (`normal_link`, `safe_link`, `auto_unlink`). - The `itest_class` objects will be tightly packed in a `std::vector` object. - -* `std::list`, where `test_class` is exactly the same as `itest_class`, - but without the intrusive hook. - -* `std::list`. The list will contain pointers to `test_class` objects - tightly packed in a `std::vector` object. We'll call this configuration ['compact pointer list] - -* `std::list`. The list will contain pointers to `test_class` objects owned by a - `std::list` object. We'll call this configuration ['disperse pointer list]. - -Both `test_class` and `itest_class` are templatized classes that can be configured with -a boolean to increase the size of the object. This way, the tests can be executed with -small and big objects. Here is the first part of the testing code, which shows -the definitions of `test_class` and `itest_class` classes, and some other -utilities: - -[import ../perf/perf_list.cpp] -[perf_list_value_type] - -As we can see, `test_class` is a very simple class holding an `int`. `itest_class` -is just a class that has a base hook ([classref boost::intrusive::list_base_hook list_base_hook]) -and also derives from `test_class`. - -`func_ptr_adaptor` is just a functor adaptor to convert function objects taking -`test_list` objects to funtion objects taking pointers to them. - -You can find the full test code code in the -[@../../perf/perf_list.cpp perf_list.cpp] source file. - -[section:performance_results_push_back Back insertion and destruction] - -The first test will measure the benefits we can obtain with intrusive containers -avoiding memory allocations and deallocations . All the objects to be -inserted in intrusive containers are allocated in a single allocation call, -whereas `std::list` will need to allocate memory for every and deallocate it -for every erasure (or container destruction). - -Let's compare the code to be executed for each container type for different insertion tests: - -[perf_list_push_back_intrusive] - -For intrusive containers, all the values are created in a vector and after that -inserted in the intrusive list. - -[perf_list_push_back_stdlist] - -For a standard list, elements are pushed back using push_back(). - -[perf_list_push_back_stdptrlist] - -For a standard compact pointer list, elements are created in a vector and pushed back -in the pointer list using push_back(). - -[perf_list_push_back_disperse_stdptrlist] - -For a ['disperse pointer list], elements are created in a list and pushed back -in the pointer list using push_back(). - -These are the times in microseconds for each case, and the normalized time: - -[table Back insertion + destruction times for Visual C++ 7.1 / Windows XP - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [5000 / 22500] [1 / 1]] - [[`safe_link` intrusive list] [7812 / 32187] [1.56 / 1.43]] - [[`auto_unlink` intrusive list] [10156 / 41562] [2.03 / 1.84]] - [[Standard list] [76875 / 97500] [5.37 / 4.33]] - [[Standard compact pointer list] [76406 / 86718] [15.28 / 3.85]] - [[Standard disperse pointer list] [146562 / 175625] [29.31 / 7.80]] -] - -[table Back insertion + destruction times for GCC 4.1.1 / MinGW over Windows XP - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [4375 / 22187] [1 / 1]] - [[`safe_link` intrusive list] [7812 / 32812] [1.78 / 1.47]] - [[`auto_unlink` intrusive list] [10468 / 42031] [2.39 / 1.89]] - [[Standard list] [81250 / 98125] [18.57 / 4.42]] - [[Standard compact pointer list] [83750 / 94218] [19.14 / 4.24]] - [[Standard disperse pointer list] [155625 / 175625] [35.57 / 7.91]] -] - -[table Back insertion + destruction times for GCC 4.1.2 / Linux Kernel 2.6.18 (OpenSuse 10.2) - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [4792 / 20495] [1 / 1]] - [[`safe_link` intrusive list] [7709 / 30803] [1.60 / 1.5]] - [[`auto_unlink` intrusive list] [10180 / 41183] [2.12 / 2.0]] - [[Standard list] [17031 / 32586] [3.55 / 1.58]] - [[Standard compact pointer list] [27221 / 34823] [5.68 / 1.69]] - [[Standard disperse pointer list] [102272 / 60056] [21.34 / 2.93]] -] - -The results are logical: intrusive lists just need one allocation. The destruction -time of the `normal_link` intrusive container is trivial (complexity: `O(1)`), -whereas `safe_link` and `auto_unlink` intrusive containers need to put the hook of -erased values' in the default state (complexity: `O(NumElements)`). That's why -`normal_link` intrusive list shines in this test. - -Non-intrusive containers need to make much more allocations and that's why they are -lagging behind. The `disperse pointer list` needs to make `NumElements*2` allocations, -so the result is not surprising. - -Linux test shows that standard containers perform very well against intrusive containers -with big objects. Nearly the same GCC version in MinGW performs worse, so maybe the -a good memory allocator is the reason for these excelent results. - -[endsect] - -[section:performance_results_reversing Reversing] - -The next test measures the time needed to complete calls to the member function `reverse()`. -Values (`test_class` and `itest_class`) and lists are created like explained in the -previous section. - -Note that for pointer lists, `reverse` [*does not need to access `test_class` values -stored in another list or vector], -since this function just needs to adjust internal pointers, so in theory, all tested -lists need to perform the same operations. - -These are the results: - -[table Reverse times for Visual C++ 7.1 / Windows XP - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object) (small object / big object)]] - [[`normal_link` intrusive list] [2656 / 10625] [1 / 1.83]] - [[`safe_link` intrusive list] [2812 / 10937] [1.05 / 1.89]] - [[`auto_unlink` intrusive list] [2710 / 10781] [1.02 / 1.86]] - [[Standard list] [5781 / 14531] [2.17 / 2.51]] - [[Standard compact pointer list] [5781 / 5781] [2.17 / 1]] - [[Standard disperse pointer list] [10781 / 15781] [4.05 / 2.72]] -] - -[table Reverse times for GCC 4.1.1 / MinGW over Windows XP - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [2656 / 10781] [1 / 2.22]] - [[`safe_link` intrusive list] [2656 / 10781] [1 / 2.22]] - [[`auto_unlink` intrusive list] [2812 / 10781] [1.02 / 2.22]] - [[Standard list] [4843 / 12500] [1.82 / 2.58]] - [[Standard compact pointer list] [4843 / 4843] [1.82 / 1]] - [[Standard disperse pointer list] [9218 / 12968] [3.47 / 2.67]] -] - -[table Reverse times for GCC 4.1.2 / Linux Kernel 2.6.18 (OpenSuse 10.2) - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [2742 / 10847] [1 / 3.41]] - [[`safe_link` intrusive list] [2742 / 10847] [1 / 3.41]] - [[`auto_unlink` intrusive list] [2742 / 11027] [1 / 3.47]] - [[Standard list] [3184 / 10942] [1.16 / 3.44]] - [[Standard compact pointer list] [3207 / 3176] [1.16 / 1]] - [[Standard disperse pointer list] [5814 / 13381] [2.12 / 4.21]] -] - -For small objects the results show that the compact storage of values in intrusive -containers improve locality and reversing is faster than with standard containers, -whose values might be dispersed in memory because each value is independently -allocated. Note that the dispersed pointer list (a list of pointers to values -allocated in another list) suffers more because nodes of the pointer list -might be more dispersed, since allocations from both lists are interleaved -in the code: - -[c++] - - //Object list (holding `test_class`) - stdlist objects; - - //Pointer list (holding `test_class` pointers) - stdptrlist l; - - for(int i = 0; i < NumElements; ++i){ - //Allocation from the object list - objects.push_back(stdlist::value_type(i)); - //Allocation from the pointer list - l.push_back(&objects.back()); - } - -For big values the compact pointer list wins because when reversing doesn't need access -to the values stored in another container. Since all the allocations for nodes of -this pointer list are likely to be near (since there is no other allocation in the -process until the pointer list is created) locality is better than with intrusive -containers. The dispersed pointer list, like with small values, has poor locality. - -[endsect] - -[section:performance_results_sorting Sorting] - -The next test measures the time needed to complete calls the member function -`sort(Pred pred)`. Values (`test_class` and `itest_class`) and lists are created like explained in the -first section. The values will be sorted in ascending and descenting order each -iteration. For example, if ['l] is a list: - -[c++] - - for(int i = 0; i < NumIter; ++i){ - if(!(i % 2)) - l.sort(std::greater()); - else - l.sort(std::less()); - } - -For a pointer list, the function object will be adapted using `func_ptr_adaptor`: - -[c++] - - for(int i = 0; i < NumIter; ++i){ - if(!(i % 2)) - l.sort(func_ptr_adaptor >()); - else - l.sort(func_ptr_adaptor >()); - } - -Note that for pointer lists, `sort` will take a function object that [*will access -`test_class` values stored in another list or vector], so pointer lists will suffer -an extra indirection: they will need to access the `test_class` values stored in -another container to compare to elements. - -These are the results: - -[table Sort times for Visual C++ 7.1 / Windows XP - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [16093 / 38906] [1 / 1]] - [[`safe_link` intrusive list] [16093 / 39062] [1 / 1]] - [[`auto_unlink` intrusive list] [16093 / 38906] [1 / 1]] - [[Standard list] [32343 / 56406] [2.0 / 1.44]] - [[Standard compact pointer list] [33593 / 46093] [2.08 / 1.18]] - [[Standard disperse pointer list] [46875 / 68593] [2.91 / 1.76]] -] - -[table Sort times for GCC 4.1.1 / MinGW over Windows XP - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [15000 / 39218] [1 / 1]] - [[`safe_link` intrusive list] [15156 / 39531] [1.01 / 1.01]] - [[`auto_unlink` intrusive list] [15156 / 39531] [1.01 / 1.01]] - [[Standard list] [34218 / 56875] [2.28 / 1.45]] - [[Standard compact pointer list] [35468 / 49218] [2.36 / 1.25]] - [[Standard disperse pointer list] [47656 / 70156] [3.17 / 1.78]] -] - -[table Sort times for GCC 4.1.2 / Linux Kernel 2.6.18 (OpenSuse 10.2) - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [18003 / 40795] [1 / 1]] - [[`safe_link` intrusive list] [18003 / 41017] [1 / 1]] - [[`auto_unlink` intrusive list] [18230 / 40941] [1.01 / 1]] - [[Standard list] [26273 / 49643] [1.45 / 1.21]] - [[Standard compact pointer list] [28540 / 43172] [1.58 / 1.05]] - [[Standard disperse pointer list] [35077 / 57638] [1.94 / 1.41]] -] - -The results show that intrusive containers are faster than standard -containers. We can see that the pointer -list holding pointers to values stored in a vector is quite fast, so the extra -indirection that needs to access the value is minimized because all the values -are tightly stored, improving cache. The disperse list, on the other hand, is -slower because the indirection to access to values stored in the object list is -more expensive than the access to values stored in a vector. - -[endsect] - -[section:performance_results_write_access Write access] - -The next test measures the time needed to iterate all the elements of a list, and -increment the value of the internal `i_` member: - -[c++] - - stdlist::iterator it(l.begin()), end(l.end()); - for(; it != end; ++it) - ++(it->i_); - -Values (`test_class` and `itest_class`) and lists are created like explained in -the first section. Note that for pointer lists, the iteration will suffer -an extra indirection: they will need to access the `test_class` values stored in -another container: - -[c++] - - stdptrlist::iterator it(l.begin()), end(l.end()); - for(; it != end; ++it) - ++((*it)->i_); - -These are the results: - -[table Write access times for Visual C++ 7.1 / Windows XP - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [2031 / 8125] [1 / 1]] - [[`safe_link` intrusive list] [2031 / 8281] [1 / 1.01]] - [[`auto_unlink` intrusive list] [2031 / 8281] [1 / 1.01]] - [[Standard list] [4218 / 10000] [2.07 / 1.23]] - [[Standard compact pointer list] [4062 / 8437] [2.0 / 1.03]] - [[Standard disperse pointer list] [8593 / 13125] [4.23 / 1.61]] -] - -[table Write access times for GCC 4.1.1 / MinGW over Windows XP - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [2343 / 8281] [1 / 1]] - [[`safe_link` intrusive list] [2500 / 8281] [1.06 / 1]] - [[`auto_unlink` intrusive list] [2500 / 8281] [1.06 / 1]] - [[Standard list] [4218 / 10781] [1.8 / 1.3]] - [[Standard compact pointer list] [3906 / 8281] [1.66 / 1]] - [[Standard disperse pointer list] [8281 / 13750] [3.53 / 1.66]] -] - -[table Write access times for GCC 4.1.2 / Linux Kernel 2.6.18 (OpenSuse 10.2) - [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] - [[`normal_link` intrusive list] [2286 / 8468] [1 / 1.1]] - [[`safe_link` intrusive list] [2381 / 8412] [1.04 / 1.09]] - [[`auto_unlink` intrusive list] [2301 / 8437] [1.01 / 1.1]] - [[Standard list] [3044 / 9061] [1.33 / 1.18]] - [[Standard compact pointer list] [2755 / 7660] [1.20 / 1]] - [[Standard disperse pointer list] [6118 / 12453] [2.67 / 1.62]] -] - -Like with the read access test, the results show that intrusive containers outperform -all other containers if the values are tightly packed in a vector. -The disperse list is again the slowest one. - -[endsect] - -[section:performance_results_conclusions Conclusions] - -Intrusive containers can offer performance benefits that can not be achieved with -equivalent non-intrusive containers. Memory locality improvements are noticeable -when objects to be inserted are small. Minimizing memory allocation/deallocation calls is also -an important factor and intrusive containers make this simple if the user allocates -all the objects to be inserted in intrusive containers in containers like `std::vector` or `std::deque`. - -[endsect] - -[endsect] - -[section:disabling_exceptions Disabling exceptions support] - -[*Boost.Intrusive] might be useful in environments where exceptions are not available -or recommendable (like embedded or real-time systems). [*Boost.Intrusive] uses the -global Boost mechanism to disable exception handling, so that if the compiler -configuration disables exceptions, `BOOST_NO_EXCEPTIONS` is defined and exception -handling is disabled. - -This mechanism is a global mechanism to disable exceptions. If for any reason, -the user wants to disable exception handling [*only] in [*Boost.Intrusive], -`BOOST_INTRUSIVE_DISABLE_EXCEPTION_HANDLING` can be defined to disable -exception handling in the library. - -[endsect] - -[section:tested_compilers Tested compilers] - -[*Boost.Intrusive] has been tested in the following compilers/platforms: - -* Visual 7.1/WinXP -* Visual 8.0/WinXP -* GCC 4.1.1/MinGW -* GCC 3.4.4/Cygwin -* Intel 9.1/WinXP -* GCC 4.1.2/Linux -* GCC 3.4.3/Solaris 11 -* GCC 4.0/Mac Os 10.4.1 -* SunCC 5.8/Solaris 11 - -[endsect] - -[section:references References] - -* SGI's [@http://www.sgi.com/tech/stl/ STL Programmer's Guide]. - [*Boost.Intrusive] is based on STL concepts and interface. - -* Dr. Dobb's, September 1, 2005: [@http://www.ddj.com/architect/184402007 ['Implementing Splay Trees in C++] ]. - [*Boost.Intrusive] splay containers code is based on this article. - -* Olaf's original intrusive container library: [@http://freenet-homepage.de/turtle++/intrusive.html ['STL-like intrusive containers] ]. - -[endsect] - -[section:acknowledgments Acknowledgements] - -[*Olaf Krzikalla] would like to thank: - -* [*Markus Schaaf] for pointing out the possibility and the advantages of the derivation -approach. - -* [*Udo Steinbach] for encouragements to present this work for boost, a lot of fixes and -helpful discussions. - -* [*Jaap Suter] for the initial hint, which eventually lead to the member value_traits. - -[*Ion Gaztanaga] would like to thank: - -* [*Olaf Krzikalla] for the permission to continue his great work. - -* [*Joaquín M. LÃģpez MuÃąoz] for his thorough review, help, and ideas. - -* [*Cory Nelson], [*Daniel James], [*Dave Harris], [*Guillaume Melquiond], - [*Henri Bavestrello], [*HervÃĐ BrÃķnnimann], [*Kai BrÞning], [*Kevin Sopp], - [*Paul Rose], [*Pavel Vozelinek], [*Howard Hinnant], [*Olaf Krzikalla], - [*Samuel Debionne], [*Stjepan Rajko], [*Thorsten Ottosen], [*Tobias Schwinger], - [*Tom Brinkman] and [*Steven Watanabe] - for their comments and reviews in the Boost.Intrusive formal review. - -* Thanks to of [*Julienne Walker] and [*The EC Team] ([@http://eternallyconfuzzled.com]) - for their great algorithms. - -* Thanks to [*Daniel K. O.] for his AVL tree rebalancing code. - -* Thanks to [*Ralf Mattethat] for his splay tree article and code. - -* Special thanks to [*Steven Watanabe] and [*Tobias Schwinger] for their - invaluable suggestions and improvements. - -[endsect] - -[xinclude autodoc.xml] - -[section:license_notices License notices] - -Most of the internal implementation of red-black trees is based on that of SGI STL stl_tree.h file: - -['Copyright (c) 1996,1997 Silicon Graphics Computer Systems, Inc. -Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Silicon Graphics makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.] - -['Copyright (c) 1994 Hewlett-Packard Company -Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Hewlett-Packard Company makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.] - -The tree destruction algorithm is based on Julienne Walker and The EC Team code: - -['This code is in the public domain. Anyone may -use it or change it in any way that they see -fit. The author assumes no responsibility for -damages incurred through use of the original -code or any variations thereof.] - -['It is requested, but not required, that due -credit is given to the original author and -anyone who has modified the code through -a header comment, such as this one.] - -[endsect] diff --git a/example/Jamfile.v2 b/example/Jamfile.v2 deleted file mode 100644 index 75ef95a..0000000 --- a/example/Jamfile.v2 +++ /dev/null @@ -1,34 +0,0 @@ -# Boost Intrusive Library Example Jamfile - -# (C) Copyright Ion Gaztaņaga 2006-2007. -# Use, modification and distribution are subject to the -# Boost Software License, Version 1.0. (See accompanying file -# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -# Adapted from John Maddock's TR1 Jamfile.v2 -# Copyright John Maddock 2005. -# Use, modification and distribution are subject to the -# Boost Software License, Version 1.0. (See accompanying file -# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -# this rule enumerates through all the sources and invokes -# the run rule for each source, the result is a list of all -# the run rules, which we can pass on to the test_suite rule: - -rule test_all -{ - local all_rules = ; - - for local fileb in [ glob *.cpp ] - { - all_rules += [ run $(fileb) /boost/thread//boost_thread - : # additional args - : # test-files - : # requirements - ] ; - } - - return $(all_rules) ; -} - -test-suite intrusive_example : [ test_all r ] : multi ; \ No newline at end of file diff --git a/example/doc_advanced_value_traits.cpp b/example/doc_advanced_value_traits.cpp deleted file mode 100644 index f4caafb..0000000 --- a/example/doc_advanced_value_traits.cpp +++ /dev/null @@ -1,105 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_advanced_value_traits_code -#include -#include -#include - -//This is the node that will be used with algorithms. -struct simple_node -{ - simple_node *prev_; - simple_node *next_; -}; -//] - -//[doc_advanced_value_traits_value_traits -class base_1{}; -class base_2{}; - -struct value_1 : public base_1, public simple_node -{ int id_; }; - -struct value_2 : public base_1, public base_2, public simple_node -{ float id_; }; - -//Define the node traits. A single node_traits will be enough. -struct simple_node_traits -{ - typedef simple_node node; - typedef node * node_ptr; - typedef const node * const_node_ptr; - static node *get_next(const node *n) { return n->next_; } - static void set_next(node *n, node *next) { n->next_ = next; } - static node *get_previous(const node *n) { return n->prev_; } - static void set_previous(node *n, node *prev) { n->prev_ = prev; } -}; - -//A templatized value traits for value_1 and value_2 -template -struct simple_value_traits -{ - typedef simple_node_traits node_traits; - typedef node_traits::node_ptr node_ptr; - typedef node_traits::const_node_ptr const_node_ptr; - typedef ValueType value_type; - typedef ValueType * pointer; - typedef const ValueType * const_pointer; - static const boost::intrusive::link_mode_type link_mode = boost::intrusive::normal_link; - static node_ptr to_node_ptr (value_type &value) { return node_ptr(&value); } - static const_node_ptr to_node_ptr (const value_type &value) { return const_node_ptr(&value); } - static pointer to_value_ptr(node_ptr n) { return static_cast(n); } - static const_pointer to_value_ptr(const_node_ptr n) { return static_cast(n); } -}; -//] - -//[doc_advanced_value_traits_containers -//Now define two intrusive lists. Both lists will use the same algorithms: -// circular_list_algorithms - -using namespace boost::intrusive; -typedef list > > Value1List; -typedef list > > Value2List; -//] - -//[doc_advanced_value_traits_test -int main() -{ - typedef std::vector Vect1; - typedef std::vector Vect2; - - //Create values, with a different internal number - Vect1 values1; - Vect2 values2; - for(int i = 0; i < 100; ++i){ - value_1 v1; v1.id_ = i; values1.push_back(v1); - value_2 v2; v2.id_ = (float)i; values2.push_back(v2); - } - - //Create the lists with the objects - Value1List list1(values1.begin(), values1.end()); - Value2List list2(values2.begin(), values2.end()); - - //Now test both lists - Value1List::const_iterator bit1(list1.begin()), bitend1(list1.end()); - Value2List::const_iterator bit2(list2.begin()), bitend2(list2.end()); - - Vect1::const_iterator it1(values1.begin()), itend1(values1.end()); - Vect2::const_iterator it2(values2.begin()), itend2(values2.end()); - - //Test the objects inserted in our lists - for(; it1 != itend1; ++it1, ++bit1, ++it2, ++bit2){ - if(&*bit1 != &*it1 || &*bit2 != &*it2) return false; - } - return 0; -} -//] diff --git a/example/doc_advanced_value_traits2.cpp b/example/doc_advanced_value_traits2.cpp deleted file mode 100644 index 6367af0..0000000 --- a/example/doc_advanced_value_traits2.cpp +++ /dev/null @@ -1,95 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include -#include - -struct simple_node -{ - simple_node *prev_; - simple_node *next_; -}; - -//Define the node traits. A single node_traits will be enough. -struct simple_node_traits -{ - typedef simple_node node; - typedef node * node_ptr; - typedef const node * const_node_ptr; - static node *get_next(const node *n) { return n->next_; } - static void set_next(node *n, node *next) { n->next_ = next; } - static node *get_previous(const node *n) { return n->prev_; } - static void set_previous(node *n, node *prev) { n->prev_ = prev; } -}; - -//[doc_advanced_value_traits2_value_traits -class base_1{}; -class base_2{}; - -struct value_1 : public base_1, public simple_node -{ - int id_; - simple_node node_; -}; - -struct value_2 : public base_1, public base_2, public simple_node -{ - simple_node node_; - float id_; -}; - -using namespace boost::intrusive; - -typedef member_value_traits - ValueTraits1; -typedef member_value_traits - ValueTraits2; - -//Now define two intrusive lists. Both lists will use the same algorithms: -// circular_list_algorithms -typedef list > Value1List; -typedef list > Value2List; -//] - -//[doc_advanced_value_traits2_test -int main() -{ - typedef std::vector Vect1; - typedef std::vector Vect2; - - //Create values, with a different internal number - Vect1 values1; - Vect2 values2; - for(int i = 0; i < 100; ++i){ - value_1 v1; v1.id_ = i; values1.push_back(v1); - value_2 v2; v2.id_ = (float)i; values2.push_back(v2); - } - - //Create the lists with the objects - Value1List list1(values1.begin(), values1.end()); - Value2List list2(values2.begin(), values2.end()); - - //Now test both lists - Value1List::const_iterator bit1(list1.begin()), bitend1(list1.end()); - Value2List::const_iterator bit2(list2.begin()), bitend2(list2.end()); - - Vect1::const_iterator it1(values1.begin()), itend1(values1.end()); - Vect2::const_iterator it2(values2.begin()), itend2(values2.end()); - - //Test the objects inserted in our lists - for(; it1 != itend1; ++it1, ++bit1, ++it2, ++bit2){ - if(&*bit1 != &*it1 || &*bit2 != &*it2) return false; - } - return 0; -} -//] diff --git a/example/doc_assoc_optimized_code.cpp b/example/doc_assoc_optimized_code.cpp deleted file mode 100644 index 8af780c..0000000 --- a/example/doc_assoc_optimized_code.cpp +++ /dev/null @@ -1,260 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_assoc_optimized_code_normal_find -#include -#include -#include - -using namespace boost::intrusive; - -// Hash function for strings -struct StrHasher -{ - std::size_t operator()(const char *str) const - { - std::size_t seed = 0; - for(; *str; ++str) boost::hash_combine(seed, *str); - return seed; - } -}; - -class Expensive : public set_base_hook<>, public unordered_set_base_hook<> -{ - std::string key_; - // Other members... - - public: - Expensive(const char *key) - : key_(key) - {} //other expensive initializations... - - const std::string & get_key() const - { return key_; } - - friend bool operator < (const Expensive &a, const Expensive &b) - { return a.key_ < b.key_; } - - friend bool operator == (const Expensive &a, const Expensive &b) - { return a.key_ == b.key_; } - - friend std::size_t hash_value(const Expensive &object) - { return StrHasher()(object.get_key().c_str()); } -}; - -// A set and unordered_set that store Expensive objects -typedef set Set; -typedef unordered_set UnorderedSet; - -// Search functions -Expensive *get_from_set(const char* key, Set &set_object) -{ - Set::iterator it = set_object.find(Expensive(key)); - if( it == set_object.end() ) return 0; - return &*it; -} - -Expensive *get_from_uset(const char* key, UnorderedSet &uset_object) -{ - UnorderedSet::iterator it = uset_object.find(Expensive (key)); - if( it == uset_object.end() ) return 0; - return &*it; -} -//] - -//[doc_assoc_optimized_code_optimized_find -// These compare Expensive and a c-string -struct StrExpComp -{ - bool operator()(const char *str, const Expensive &c) const - { return std::strcmp(str, c.get_key().c_str()) < 0; } - - bool operator()(const Expensive &c, const char *str) const - { return std::strcmp(c.get_key().c_str(), str) < 0; } -}; - -struct StrExpEqual -{ - bool operator()(const char *str, const Expensive &c) const - { return std::strcmp(str, c.get_key().c_str()) == 0; } - - bool operator()(const Expensive &c, const char *str) const - { return std::strcmp(c.get_key().c_str(), str) == 0; } -}; - -// Optimized search functions -Expensive *get_from_set_optimized(const char* key, Set &set_object) -{ - Set::iterator it = set_object.find(key, StrExpComp()); - if( it == set_object.end() ) return 0; - return &*it; -} - -Expensive *get_from_uset_optimized(const char* key, UnorderedSet &uset_object) -{ - UnorderedSet::iterator it = uset_object.find(key, StrHasher(), StrExpEqual()); - if( it == uset_object.end() ) return 0; - return &*it; -} -//] - -//[doc_assoc_optimized_code_normal_insert -// Insertion functions -bool insert_to_set(const char* key, Set &set_object) -{ - Expensive *pobject = new Expensive(key); - bool success = set_object.insert(*pobject).second; - if(!success) delete pobject; - return success; -} - -bool insert_to_uset(const char* key, UnorderedSet &uset_object) -{ - Expensive *pobject = new Expensive(key); - bool success = uset_object.insert(*pobject).second; - if(!success) delete pobject; - return success; -} -//] - -//[doc_assoc_optimized_code_optimized_insert -// Optimized insertion functions -bool insert_to_set_optimized(const char* key, Set &set_object) -{ - Set::insert_commit_data insert_data; - bool success = set_object.insert_check(key, StrExpComp(), insert_data).second; - if(success) set_object.insert_commit(*new Expensive(key), insert_data); - return success; -} - -bool insert_to_uset_optimized(const char* key, UnorderedSet &uset_object) -{ - UnorderedSet::insert_commit_data insert_data; - bool success = uset_object.insert_check - (key, StrHasher(), StrExpEqual(), insert_data).second; - if(success) uset_object.insert_commit(*new Expensive(key), insert_data); - return success; -} -//] - -int main() -{ - Set set; - UnorderedSet::bucket_type buckets[10]; - UnorderedSet unordered_set(UnorderedSet::bucket_traits(buckets, 10)); - - const char * const expensive_key - = "A long string that avoids small string optimization"; - - Expensive value(expensive_key); - - if(get_from_set(expensive_key, set)){ - return 1; - } - - if(get_from_uset(expensive_key, unordered_set)){ - return 1; - } - - if(get_from_set_optimized(expensive_key, set)){ - return 1; - } - - if(get_from_uset_optimized(expensive_key, unordered_set)){ - return 1; - } - - Set::iterator setit = set.insert(value).first; - UnorderedSet::iterator unordered_setit = unordered_set.insert(value).first; - - if(!get_from_set(expensive_key, set)){ - return 1; - } - - if(!get_from_uset(expensive_key, unordered_set)){ - return 1; - } - - if(!get_from_set_optimized(expensive_key, set)){ - return 1; - } - - if(!get_from_uset_optimized(expensive_key, unordered_set)){ - return 1; - } - - set.erase(setit); - unordered_set.erase(unordered_setit); - - if(!insert_to_set(expensive_key, set)){ - return 1; - } - - if(!insert_to_uset(expensive_key, unordered_set)){ - return 1; - } - - { - Expensive *ptr = &*set.begin(); - set.clear(); - delete ptr; - } - - { - Expensive *ptr = &*unordered_set.begin(); - unordered_set.clear(); - delete ptr; - } - - if(!insert_to_set_optimized(expensive_key, set)){ - return 1; - } - - if(!insert_to_uset_optimized(expensive_key, unordered_set)){ - return 1; - } - - { - Expensive *ptr = &*set.begin(); - set.clear(); - delete ptr; - } - - { - Expensive *ptr = &*unordered_set.begin(); - unordered_set.clear(); - delete ptr; - } - - setit = set.insert(value).first; - unordered_setit = unordered_set.insert(value).first; - - if(insert_to_set(expensive_key, set)){ - return 1; - } - - if(insert_to_uset(expensive_key, unordered_set)){ - return 1; - } - - if(insert_to_set_optimized(expensive_key, set)){ - return 1; - } - - if(insert_to_uset_optimized(expensive_key, unordered_set)){ - return 1; - } - - set.erase(value); - unordered_set.erase(value); - - return 0; -} diff --git a/example/doc_auto_unlink.cpp b/example/doc_auto_unlink.cpp deleted file mode 100644 index 848b973..0000000 --- a/example/doc_auto_unlink.cpp +++ /dev/null @@ -1,82 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_auto_unlink_code -#include -#include - -using namespace boost::intrusive; - -typedef list_base_hook > auto_unlink_hook; - -class MyClass : public auto_unlink_hook - //This hook removes the node in the destructor -{ - int int_; - - public: - MyClass(int i = 0) : int_(i) {} - void unlink() { auto_unlink_hook::unlink(); } - bool is_linked() { return auto_unlink_hook::is_linked(); } -}; - -//Define a list that will store values using the base hook -//The list can't have constant-time size! -typedef list< MyClass, constant_time_size > List; - -int main() -{ - //Create the list - List l; - { - //Create myclass and check it's linked - MyClass myclass; - assert(myclass.is_linked() == false); - - //Insert the object - l.push_back(myclass); - - //Check that we have inserted the object - assert(l.empty() == false); - assert(&l.front() == &myclass); - assert(myclass.is_linked() == true); - - //Now myclass' destructor will unlink it - //automatically - } - - //Check auto-unlink has been executed - assert(l.empty() == true); - - { - //Now test the unlink() function - - //Create myclass and check it's linked - MyClass myclass; - assert(myclass.is_linked() == false); - - //Insert the object - l.push_back(myclass); - - //Check that we have inserted the object - assert(l.empty() == false); - assert(&l.front() == &myclass); - assert(myclass.is_linked() == true); - - //Now unlink the node - myclass.unlink(); - - //Check auto-unlink has been executed - assert(l.empty() == true); - } - return 0; -} -//] diff --git a/example/doc_avl_set.cpp b/example/doc_avl_set.cpp deleted file mode 100644 index 33a061c..0000000 --- a/example/doc_avl_set.cpp +++ /dev/null @@ -1,86 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_avl_set_code -#include -#include -#include -#include - -using namespace boost::intrusive; - - //This is a base hook optimized for size -class MyClass : public avl_set_base_hook > -{ - int int_; - - public: - //This is a member hook - avl_set_member_hook<> member_hook_; - - MyClass(int i) - : int_(i) - {} - friend bool operator< (const MyClass &a, const MyClass &b) - { return a.int_ < b.int_; } - friend bool operator> (const MyClass &a, const MyClass &b) - { return a.int_ > b.int_; } - friend bool operator== (const MyClass &a, const MyClass &b) - { return a.int_ < b.int_; } -}; - -//Define an avl_set using the base hook that will store values in reverse order -typedef avl_set< MyClass, compare > > BaseSet; - -//Define an multiset using the member hook -typedef member_hook, &MyClass::member_hook_> MemberOption; -typedef avl_multiset< MyClass, MemberOption> MemberMultiset; - -int main() -{ - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create several MyClass objects, each one with a different value - std::vector values; - for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); - - BaseSet baseset; - MemberMultiset membermultiset; - - //Check that size optimization is activated in the base hook - assert(sizeof(avl_set_base_hook >) == 3*sizeof(void*)); - //Check that size optimization is deactivated in the member hook - assert(sizeof(avl_set_member_hook<>) > 3*sizeof(void*)); - - //Now insert them in the sets - for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){ - baseset.insert(*it); - membermultiset.insert(*it); - } - - //Now test avl_sets - { - BaseSet::reverse_iterator rbit(baseset.rbegin()), rbitend(baseset.rend()); - MemberMultiset::iterator mit(membermultiset.begin()), mitend(membermultiset.end()); - VectIt it(values.begin()), itend(values.end()); - - //Test the objects inserted in the base hook avl_set - for(; it != itend; ++it, ++rbit) - if(&*rbit != &*it) return 1; - - //Test the objects inserted in the member hook avl_set - for(it = values.begin(); it != itend; ++it, ++mit) - if(&*mit != &*it) return 1; - } - return 0; -} -//] diff --git a/example/doc_avltree_algorithms.cpp b/example/doc_avltree_algorithms.cpp deleted file mode 100644 index 9aa47b2..0000000 --- a/example/doc_avltree_algorithms.cpp +++ /dev/null @@ -1,85 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_avltree_algorithms_code -#include -#include - -struct my_node -{ - my_node(int i = 0) - : int_(i) - {} - my_node *parent_, *left_, *right_; - int balance_; - //other members - int int_; -}; - -//Define our own avltree_node_traits -struct my_avltree_node_traits -{ - typedef my_node node; - typedef my_node * node_ptr; - typedef const my_node * const_node_ptr; - typedef int balance; - - static node_ptr get_parent(const_node_ptr n) { return n->parent_; } - static void set_parent(node_ptr n, node_ptr parent){ n->parent_ = parent; } - static node_ptr get_left(const_node_ptr n) { return n->left_; } - static void set_left(node_ptr n, node_ptr left) { n->left_ = left; } - static node_ptr get_right(const_node_ptr n) { return n->right_; } - static void set_right(node_ptr n, node_ptr right) { n->right_ = right; } - static balance get_balance(const_node_ptr n) { return n->balance_; } - static void set_balance(node_ptr n, balance b) { n->balance_ = b; } - static balance negative() { return -1; } - static balance zero() { return 0; } - static balance positive() { return 1; } -}; - -struct node_ptr_compare -{ - bool operator()(my_node *a, my_node *b) - { return a->int_ < b->int_; } -}; - -int main() -{ - typedef boost::intrusive::avltree_algorithms algo; - my_node header, two(2), three(3); - - //Create an empty avltree container: - //"header" will be the header node of the tree - algo::init_header(&header); - - //Now insert node "two" in the tree using the sorting functor - algo::insert_equal_upper_bound(&header, &two, node_ptr_compare()); - - //Now insert node "three" in the tree using the sorting functor - algo::insert_equal_lower_bound(&header, &three, node_ptr_compare()); - - //Now take the first node (the left node of the header) - my_node *n = header.left_; - assert(n == &two); - - //Now go to the next node - n = algo::next_node(n); - assert(n == &three); - - //Erase a node just using a pointer to it - algo::unlink(&two); - - //Erase a node using also the header (faster) - algo::erase(&header, &three); - return 0; -} - -//] diff --git a/example/doc_bucket_traits.cpp b/example/doc_bucket_traits.cpp deleted file mode 100644 index c03c125..0000000 --- a/example/doc_bucket_traits.cpp +++ /dev/null @@ -1,83 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_bucket_traits -#include -#include -#include - -using namespace boost::intrusive; - -//A class to be inserted in an unordered_set -class MyClass : public unordered_set_base_hook<> -{ - int int_; - - public: - MyClass(int i = 0) : int_(i) - {} - - friend bool operator==(const MyClass &l, const MyClass &r) - { return l.int_ == r.int_; } - friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } -}; - -//Define the base hook option -typedef base_hook< unordered_set_base_hook<> > BaseHookOption; - -//Obtain the types of the bucket and the bucket pointer -typedef unordered_bucket::type BucketType; -typedef unordered_bucket_ptr::type BucketPtr; - -//The custom bucket traits. -class custom_bucket_traits -{ - public: - static const int NumBuckets = 100; - - custom_bucket_traits(BucketPtr buckets) - : buckets_(buckets) - {} - - //Functions to be implemented by custom bucket traits - BucketPtr bucket_begin() const { return buckets_; } - std::size_t bucket_count() const { return NumBuckets;} - - private: - BucketPtr buckets_; -}; - -//Define the container using the custom bucket traits -typedef unordered_set > BucketTraitsUset; - -int main() -{ - typedef std::vector::iterator VectIt; - std::vector values; - - //Fill values - for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); - - //Now create the bucket array and the custom bucket traits object - BucketType buckets[custom_bucket_traits::NumBuckets]; - custom_bucket_traits btraits(buckets); - - //Now create the unordered set - BucketTraitsUset uset(btraits); - - //Insert the values in the unordered set - for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it) - uset.insert(*it); - - return 0; -} -//] diff --git a/example/doc_clone_from.cpp b/example/doc_clone_from.cpp deleted file mode 100644 index 94c0668..0000000 --- a/example/doc_clone_from.cpp +++ /dev/null @@ -1,74 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_clone_from -#include -#include -#include - -using namespace boost::intrusive; - -//A class that can be inserted in an intrusive list -class my_class : public list_base_hook<> -{ - public: - friend bool operator==(const my_class &a, const my_class &b) - { return a.int_ == b.int_; } - - int int_; - - //... -}; - -//Definition of the intrusive list -typedef list my_class_list; - -//Cloner object function -struct new_cloner -{ - my_class *operator()(const my_class &clone_this) - { return new my_class(clone_this); } -}; - -//The disposer object function -struct delete_disposer -{ - void operator()(my_class *delete_this) - { delete delete_this; } -}; - -int main() -{ - const int MaxElem = 100; - std::vector nodes(MaxElem); - - //Fill all the nodes and insert them in the list - my_class_list list; - - for(int i = 0; i < MaxElem; ++i) nodes[i].int_ = i; - - list.insert(list.end(), nodes.begin(), nodes.end()); - - //Now clone "list" using "new" and "delete" object functions - my_class_list cloned_list; - cloned_list.clone_from(list, new_cloner(), delete_disposer()); - - //Test that both are equal - if(cloned_list != list) - std::cout << "Both lists are different" << std::endl; - else - std::cout << "Both lists are equal" << std::endl; - - //Don't forget to free the memory from the second list - cloned_list.clear_and_dispose(delete_disposer()); - return 0; -} -//] diff --git a/example/doc_entity.cpp b/example/doc_entity.cpp deleted file mode 100644 index 0e2fc54..0000000 --- a/example/doc_entity.cpp +++ /dev/null @@ -1,60 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_entity_code -#include - -using namespace boost::intrusive; - -//A class that can be inserted in an intrusive list -class entity : public list_base_hook<> -{ - public: - virtual ~entity(); - //... -}; - -//"some_entity" derives from "entity" -class some_entity : public entity -{/**/}; - -//Definition of the intrusive list -typedef list entity_list; - -//A global list -entity_list global_list; - -//The destructor removes itself from the global list -entity::~entity() -{ global_list.erase(entity_list::s_iterator_to(*this)); } - -//Function to insert a new "some_entity" in the global list -void insert_some_entity() -{ global_list.push_back (*new some_entity(/*...*/)); } - -//Function to clear an entity from the intrusive global list -void clear_list () -{ - // entity's destructor removes itself from the global list implicitly - while (!global_list.empty()) - delete &global_list.front(); -} - -int main() -{ - //Insert some new entities - insert_some_entity(); - insert_some_entity(); - //global_list's destructor will free objects - return 0; -} - -//] diff --git a/example/doc_erasing_and_disposing.cpp b/example/doc_erasing_and_disposing.cpp deleted file mode 100644 index 51d737f..0000000 --- a/example/doc_erasing_and_disposing.cpp +++ /dev/null @@ -1,70 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_erasing_and_disposing -#include - -using namespace boost::intrusive; - -//A class that can be inserted in an intrusive list -class my_class : public list_base_hook<> -{ - public: - my_class(int i) - : int_(i) - {} - - int int_; - //... -}; - -//Definition of the intrusive list -typedef list my_class_list; - -//The predicate function -struct is_even -{ - bool operator()(const my_class &c) const - { return 0 == (c.int_ % 2); } -}; - -//The disposer object function -struct delete_disposer -{ - void operator()(my_class *delete_this) - { delete delete_this; } -}; - -int main() -{ - const int MaxElem = 100; - - //Fill all the nodes and insert them in the list - my_class_list list; - - try{ - //Insert new objects in the container - for(int i = 0; i < MaxElem; ++i) list.push_back(*new my_class(i)); - - //Now use remove_and_dispose_if to erase and delete the objects - list.remove_and_dispose_if(is_even(), delete_disposer()); - } - catch(...){ - //If something throws, make sure that all the memory is freed - list.clear_and_dispose(delete_disposer()); - throw; - } - - //Dispose remaining elements - list.erase_and_dispose(list.begin(), list.end(), delete_disposer()); - return 0; -} -//] diff --git a/example/doc_external_value_traits.cpp b/example/doc_external_value_traits.cpp deleted file mode 100644 index 80d366e..0000000 --- a/example/doc_external_value_traits.cpp +++ /dev/null @@ -1,129 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_external_value_traits -#include -#include - -using namespace boost::intrusive; - -//This type is not modifiable so we can't store hooks or custom nodes -typedef int identifier_t; - -//This value traits will associate elements from an array of identifiers with -//elements of an array of nodes. The element i of the value array will use the -//node i of the node array: -class external_traits -{ - //Non-copyable - external_traits(const external_traits &); - external_traits& operator=(const external_traits &); - - public: - typedef list_node_traits node_traits; - typedef node_traits::node node; - typedef node * node_ptr; - typedef const node * const_node_ptr; - typedef identifier_t value_type; - typedef identifier_t * pointer; - typedef const identifier_t * const_pointer; - static const link_mode_type link_mode = normal_link; - - external_traits(pointer ids, std::size_t NumElements) - : ids_(ids), nodes_(NumElements) - {} - - ///Note: non static functions! - node_ptr to_node_ptr (value_type &value) - { return &this->nodes_[0] + (&value - this->ids_); } - const_node_ptr to_node_ptr (const value_type &value) const - { return &this->nodes_[0] + (&value - this->ids_); } - pointer to_value_ptr(node_ptr n) - { return this->ids_ + (n - &this->nodes_[0]); } - const_pointer to_value_ptr(const_node_ptr n) const - { return this->ids_ + (n - &this->nodes_[0]); } - - private: - pointer ids_; - //This is an array of nodes that is necessary to form the linked list - std::vector::node> nodes_; -}; - -//This is the value traits class that will be stored in the container -//and that will lead to the external traits using the address -//of the container. -struct internal_traits -{ - static const bool external_value_traits = true; - typedef external_traits value_traits; - - template - value_traits &get_value_traits(Container &cont); - - template - const value_traits &get_value_traits(const Container &cont) const; -}; - -//The intrusive list that will use external value traits -typedef list > List; - -class data_holder - : public List -{ - public: - data_holder(identifier_t *ids, std::size_t NumElements) - : List() - , external_traits_(ids, NumElements) - {} - external_traits external_traits_; -}; - -template -internal_traits::value_traits &internal_traits::get_value_traits(Container &cont) -{ return static_cast(cont).external_traits_; } - -template -const internal_traits::value_traits &internal_traits::get_value_traits(const Container &cont) const -{ return static_cast(cont).external_traits_; } - -int main() -{ - const int NumElements = 100; - - //This is an array of ids that we want to "store" - identifier_t ids [NumElements]; - - //Initialize id objects, each one with a different number - for(int i = 0; i != NumElements; ++i) ids[i] = i; - - //The data holding the list and the external traits - data_holder data(ids, NumElements); - - //This list will store ids without modifying identifier_t instances - //Stateful value traits must be explicitly passed in the constructor. - List &my_list = data; - - //Insert ids in reverse order in the list - for(identifier_t * it(&ids[0]), *itend(&ids[NumElements]); it != itend; ++it) - my_list.push_front(*it); - - //Now test lists - List::const_iterator list_it (my_list.cbegin()); - identifier_t *it_val(&ids[NumElements]-1), *it_rbeg_val(&ids[0] -1); - - //Test the objects inserted in the base hook list - for(; it_val != it_rbeg_val; --it_val, ++list_it){ - if(&*list_it != &*it_val) return 1; - } - - return 0; -} -//] diff --git a/example/doc_how_to_use.cpp b/example/doc_how_to_use.cpp deleted file mode 100644 index 8284bd0..0000000 --- a/example/doc_how_to_use.cpp +++ /dev/null @@ -1,75 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_how_to_use_code -#include -#include - -using namespace boost::intrusive; - -class MyClass : public list_base_hook<> -{ - int int_; - - public: - list_member_hook<> member_hook_; - - MyClass(int i) : int_(i) {} -}; - -//Define a list that will store MyClass using the base hook -typedef list BaseList; - -//Define a list that will store MyClass using the member hook -typedef member_hook - < MyClass, list_member_hook<>, &MyClass::member_hook_> MemberOption; -typedef list MemberList; - -int main() -{ - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create several MyClass objects, each one with a different value - std::vector 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; -} -//] diff --git a/example/doc_iterator_from_value.cpp b/example/doc_iterator_from_value.cpp deleted file mode 100644 index b212a49..0000000 --- a/example/doc_iterator_from_value.cpp +++ /dev/null @@ -1,97 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_iterator_from_value -#include -#include -#include -#include - -using namespace boost::intrusive; - -class intrusive_data -{ - int data_id_; - public: - - void set(int id) { data_id_ = id; } - - //This class can be inserted in an intrusive list - list_member_hook<> list_hook_; - - //This class can be inserted in an intrusive unordered_set - unordered_set_member_hook<> unordered_set_hook_; - - //Comparison operators - friend bool operator==(const intrusive_data &a, const intrusive_data &b) - { return a.data_id_ == b.data_id_; } - - friend bool operator!=(const intrusive_data &a, const intrusive_data &b) - { return a.data_id_ != b.data_id_; } - - //The hash function - friend std::size_t hash_value(const intrusive_data &i) - { return boost::hash()(i.data_id_); } -}; - -//Definition of the intrusive list that will hold intrusive_data -typedef member_hook - , &intrusive_data::list_hook_> MemberListOption; -typedef list list_t; - -//Definition of the intrusive unordered_set that will hold intrusive_data -typedef member_hook - < intrusive_data, unordered_set_member_hook<> - , &intrusive_data::unordered_set_hook_> MemberUsetOption; -typedef boost::intrusive::unordered_set - < intrusive_data, MemberUsetOption> unordered_set_t; - -int main() -{ - //Create MaxElem objects - const int MaxElem = 100; - std::vector nodes(MaxElem); - - //Declare the intrusive containers - list_t list; - unordered_set_t::bucket_type buckets[MaxElem]; - unordered_set_t unordered_set - (unordered_set_t::bucket_traits(buckets, MaxElem)); - - //Initialize all the nodes - for(int i = 0; i < MaxElem; ++i) nodes[i].set(i); - - //Now insert them in both intrusive containers - list.insert(list.end(), nodes.begin(), nodes.end()); - unordered_set.insert(nodes.begin(), nodes.end()); - - //Now check the iterator_to function - list_t::iterator list_it(list.begin()); - for(int i = 0; i < MaxElem; ++i, ++list_it) - if(list.iterator_to(nodes[i]) != list_it || - list_t::s_iterator_to(nodes[i]) != list_it) - return 1; - - //Now check unordered_set::s_iterator_to (which is a member function) - //and unordered_set::s_local_iterator_to (which is an static member function) - unordered_set_t::iterator unordered_set_it(unordered_set.begin()); - for(int i = 0; i < MaxElem; ++i){ - unordered_set_it = unordered_set.find(nodes[i]); - if(unordered_set.iterator_to(nodes[i]) != unordered_set_it) - return 1; - if(*unordered_set.local_iterator_to(nodes[i]) != *unordered_set_it || - *unordered_set_t::s_local_iterator_to(nodes[i]) != *unordered_set_it ) - return 1; - } - - return 0; -} -//] diff --git a/example/doc_list.cpp b/example/doc_list.cpp deleted file mode 100644 index 7137fcc..0000000 --- a/example/doc_list.cpp +++ /dev/null @@ -1,76 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_list_code -#include -#include - -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 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::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create several MyClass objects, each one with a different value - std::vector 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; -} -//] diff --git a/example/doc_list_algorithms.cpp b/example/doc_list_algorithms.cpp deleted file mode 100644 index 0d1277d..0000000 --- a/example/doc_list_algorithms.cpp +++ /dev/null @@ -1,66 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_list_algorithms_code -#include -#include - -struct my_node -{ - my_node *next_, *prev_; - //other members... -}; - -//Define our own list_node_traits -struct my_list_node_traits -{ - typedef my_node node; - typedef my_node * node_ptr; - typedef const my_node * const_node_ptr; - static node_ptr get_next(const_node_ptr n) { return n->next_; } - static void set_next(node_ptr n, node_ptr next) { n->next_ = next; } - static node *get_previous(const_node_ptr n) { return n->prev_; } - static void set_previous(node_ptr n, node_ptr prev){ n->prev_ = prev; } -}; - -int main() -{ - typedef boost::intrusive::circular_list_algorithms algo; - my_node one, two, three; - - //Create an empty doubly linked list container: - //"one" will be the first node of the container - algo::init(&one); - assert(algo::count(&one) == 1); - - //Now add a new node before "one" - algo::link_before(&one, &two); - assert(algo::count(&one) == 2); - - //Now add a new node after "two" - algo::link_after(&two, &three); - assert(algo::count(&one) == 3); - - //Now unlink the node after one - algo::unlink(&three); - assert(algo::count(&one) == 2); - - //Now unlink two - algo::unlink(&two); - assert(algo::count(&one) == 1); - - //Now unlink one - algo::unlink(&one); - assert(algo::count(&one) == 1); - - return 0; -} -//] diff --git a/example/doc_offset_ptr.cpp b/example/doc_offset_ptr.cpp deleted file mode 100644 index 1d7e87e..0000000 --- a/example/doc_offset_ptr.cpp +++ /dev/null @@ -1,100 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// - -//This is needed to allow concurrent test execution in -//several platforms. The shared memory must be unique -//for each process... -#include -#include - -const char *get_shared_memory_name() -{ - std::stringstream s; - s << "process_" << boost::interprocess::detail::get_current_process_id(); - static std::string str = s.str(); - return str.c_str(); -} - -//[doc_offset_ptr_0 -#include -#include - -using namespace boost::intrusive; -namespace ip = boost::interprocess; - -class shared_memory_data - //Declare the hook with an offset_ptr from Boost.Interprocess - //to make this class compatible with shared memory - : public list_base_hook< void_pointer< ip::offset_ptr > > -{ - int data_id_; - public: - - int get() const { return data_id_; } - void set(int id) { data_id_ = id; } -}; -//] - -//[doc_offset_ptr_1 -#include -#include -#include -#include - -//Definition of the shared memory friendly intrusive list -typedef ip::list shm_list_t; - -int main() -{ - //Now create an intrusive list in shared memory: - //nodes and the container itself must be created in shared memory - const int MaxElem = 100; - const int ShmSize = 50000; - const char *ShmName = get_shared_memory_name(); - { - //Erase all old shared memory - ip::shared_memory_object::remove(ShmName); - ip::managed_shared_memory shm(ip::create_only, ShmName, ShmSize); - - //Create all nodes in shared memory using a shared memory vector - //See Boost.Interprocess documentation for more information on this - typedef ip::allocator - < shared_memory_data, ip::managed_shared_memory::segment_manager> - shm_allocator_t; - typedef ip::vector shm_vector_t; - shm_allocator_t shm_alloc(shm.get_segment_manager()); - shm_vector_t *pshm_vect = - shm.construct(ip::anonymous_instance)(shm_alloc); - pshm_vect->resize(MaxElem); - - //Initialize all the nodes - for(int i = 0; i < MaxElem; ++i) (*pshm_vect)[i].set(i); - - //Now create the shared memory intrusive list - shm_list_t *plist = shm.construct(ip::anonymous_instance)(); - plist->insert(plist->end(), pshm_vect->begin(), pshm_vect->end()); - - //Check all the inserted nodes - int checker = 0; - for( shm_list_t::const_iterator it = plist->begin(), itend(plist->end()) - ; it != itend; ++it, ++checker){ - if(it->get() != checker) return false; - } - - //Now delete the list and after that, the nodes - shm.destroy_ptr(plist); - shm.destroy_ptr(pshm_vect); - } - ip::shared_memory_object::remove(ShmName); - return 0; -} -//] diff --git a/example/doc_rbtree_algorithms.cpp b/example/doc_rbtree_algorithms.cpp deleted file mode 100644 index 6eb25fb..0000000 --- a/example/doc_rbtree_algorithms.cpp +++ /dev/null @@ -1,83 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_rbtree_algorithms_code -#include -#include - -struct my_node -{ - my_node(int i = 0) - : int_(i) - {} - my_node *parent_, *left_, *right_; - int color_; - //other members - int int_; -}; - -//Define our own rbtree_node_traits -struct my_rbtree_node_traits -{ - typedef my_node node; - typedef my_node * node_ptr; - typedef const my_node * const_node_ptr; - typedef int color; - static node_ptr get_parent(const_node_ptr n) { return n->parent_; } - static void set_parent(node_ptr n, node_ptr parent){ n->parent_ = parent; } - static node_ptr get_left(const_node_ptr n) { return n->left_; } - static void set_left(node_ptr n, node_ptr left) { n->left_ = left; } - static node_ptr get_right(const_node_ptr n) { return n->right_; } - static void set_right(node_ptr n, node_ptr right) { n->right_ = right; } - static color get_color(const_node_ptr n) { return n->color_; } - static void set_color(node_ptr n, color c) { n->color_ = c; } - static color black() { return color(0); } - static color red() { return color(1); } -}; - -struct node_ptr_compare -{ - bool operator()(my_node *a, my_node *b) - { return a->int_ < b->int_; } -}; - -int main() -{ - typedef boost::intrusive::rbtree_algorithms algo; - my_node header, two(2), three(3); - - //Create an empty rbtree container: - //"header" will be the header node of the tree - algo::init_header(&header); - - //Now insert node "two" in the tree using the sorting functor - algo::insert_equal_upper_bound(&header, &two, node_ptr_compare()); - - //Now insert node "three" in the tree using the sorting functor - algo::insert_equal_lower_bound(&header, &three, node_ptr_compare()); - - //Now take the first node (the left node of the header) - my_node *n = header.left_; - assert(n == &two); - - //Now go to the next node - n = algo::next_node(n); - assert(n == &three); - - //Erase a node just using a pointer to it - algo::unlink(&two); - - //Erase a node using also the header (faster) - algo::erase(&header, &three); - return 0; -} - -//] diff --git a/example/doc_set.cpp b/example/doc_set.cpp deleted file mode 100644 index d9d5d47..0000000 --- a/example/doc_set.cpp +++ /dev/null @@ -1,86 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_set_code -#include -#include -#include -#include - -using namespace boost::intrusive; - - //This is a base hook optimized for size -class MyClass : public set_base_hook > -{ - int int_; - - public: - //This is a member hook - set_member_hook<> member_hook_; - - MyClass(int i) - : int_(i) - {} - friend bool operator< (const MyClass &a, const MyClass &b) - { return a.int_ < b.int_; } - friend bool operator> (const MyClass &a, const MyClass &b) - { return a.int_ > b.int_; } - friend bool operator== (const MyClass &a, const MyClass &b) - { return a.int_ < b.int_; } -}; - -//Define a set using the base hook that will store values in reverse order -typedef set< MyClass, compare > > BaseSet; - -//Define an multiset using the member hook -typedef member_hook, &MyClass::member_hook_> MemberOption; -typedef multiset< MyClass, MemberOption> MemberMultiset; - -int main() -{ - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create several MyClass objects, each one with a different value - std::vector values; - for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); - - BaseSet baseset; - MemberMultiset membermultiset; - - //Check that size optimization is activated in the base hook - assert(sizeof(set_base_hook >) == 3*sizeof(void*)); - //Check that size optimization is deactivated in the member hook - assert(sizeof(set_member_hook<>) > 3*sizeof(void*)); - - //Now insert them in the reverse order in the base hook set - for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){ - baseset.insert(*it); - membermultiset.insert(*it); - } - - //Now test sets - { - BaseSet::reverse_iterator rbit(baseset.rbegin()), rbitend(baseset.rend()); - MemberMultiset::iterator mit(membermultiset.begin()), mitend(membermultiset.end()); - VectIt it(values.begin()), itend(values.end()); - - //Test the objects inserted in the base hook set - for(; it != itend; ++it, ++rbit) - if(&*rbit != &*it) return 1; - - //Test the objects inserted in the member hook set - for(it = values.begin(); it != itend; ++it, ++mit) - if(&*mit != &*it) return 1; - } - return 0; -} -//] diff --git a/example/doc_sg_set.cpp b/example/doc_sg_set.cpp deleted file mode 100644 index 2c6eec5..0000000 --- a/example/doc_sg_set.cpp +++ /dev/null @@ -1,85 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_sg_set_code -#include -#include -#include -#include - -using namespace boost::intrusive; - -class MyClass : public bs_set_base_hook<> -{ - int int_; - - public: - //This is a member hook - bs_set_member_hook<> member_hook_; - - MyClass(int i) - : int_(i) - {} - friend bool operator< (const MyClass &a, const MyClass &b) - { return a.int_ < b.int_; } - friend bool operator> (const MyClass &a, const MyClass &b) - { return a.int_ > b.int_; } - friend bool operator== (const MyClass &a, const MyClass &b) - { return a.int_ < b.int_; } -}; - -//Define an sg_set using the base hook that will store values in reverse order -//and won't execute floating point operations. -typedef sg_set - < MyClass, compare >, floating_point > BaseSet; - -//Define an multiset using the member hook -typedef member_hook, &MyClass::member_hook_> MemberOption; -typedef sg_multiset< MyClass, MemberOption> MemberMultiset; - -int main() -{ - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create several MyClass objects, each one with a different value - std::vector values; - for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); - - BaseSet baseset; - MemberMultiset membermultiset; - - //Now insert them in the reverse order in the base hook sg_set - for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){ - baseset.insert(*it); - membermultiset.insert(*it); - } - - //Change balance factor - membermultiset.balance_factor(0.9f); - - //Now test sg_sets - { - BaseSet::reverse_iterator rbit(baseset.rbegin()), rbitend(baseset.rend()); - MemberMultiset::iterator mit(membermultiset.begin()), mitend(membermultiset.end()); - VectIt it(values.begin()), itend(values.end()); - - //Test the objects inserted in the base hook sg_set - for(; it != itend; ++it, ++rbit) - if(&*rbit != &*it) return 1; - - //Test the objects inserted in the member hook sg_set - for(it = values.begin(); it != itend; ++it, ++mit) - if(&*mit != &*it) return 1; - } - return 0; -} -//] diff --git a/example/doc_slist.cpp b/example/doc_slist.cpp deleted file mode 100644 index f8164bd..0000000 --- a/example/doc_slist.cpp +++ /dev/null @@ -1,79 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_slist_code -#include -#include - -using namespace boost::intrusive; - - //This is a base hook -class MyClass : public slist_base_hook<> -{ - int int_; - - public: - //This is a member hook - slist_member_hook<> member_hook_; - - MyClass(int i) - : int_(i) - {} -}; - -//Define an slist that will store MyClass using the public base hook -typedef slist BaseList; - -//Define an slist that will store MyClass using the public member hook -typedef member_hook, &MyClass::member_hook_> MemberOption; -typedef slist MemberList; - -int main() -{ - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create several MyClass objects, each one with a different value - std::vector 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(BaseList::iterator it(baselist.begin()), itend(baselist.end()) - ; it != itend; ++it){ - memberlist.push_front(*it); - } - - //Now test lists - { - BaseList::iterator bit(baselist.begin()), bitend(baselist.end()); - MemberList::iterator mit(memberlist.begin()), mitend(memberlist.end()); - VectRit rit(values.rbegin()), ritend(values.rend()); - VectIt it(values.begin()), itend(values.end()); - - //Test the objects inserted in the base hook list - for(; rit != ritend; ++rit, ++bit) - if(&*bit != &*rit) return 1; - - //Test the objects inserted in the member hook list - for(; it != itend; ++it, ++mit) - if(&*mit != &*it) return 1; - } - - return 0; -} -//] diff --git a/example/doc_slist_algorithms.cpp b/example/doc_slist_algorithms.cpp deleted file mode 100644 index d13d333..0000000 --- a/example/doc_slist_algorithms.cpp +++ /dev/null @@ -1,60 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_slist_algorithms_code -#include -#include - -struct my_node -{ - my_node *next_; - //other members... -}; - -//Define our own slist_node_traits -struct my_slist_node_traits -{ - typedef my_node node; - typedef my_node * node_ptr; - typedef const my_node * const_node_ptr; - static node_ptr get_next(const_node_ptr n) { return n->next_; } - static void set_next(node_ptr n, node_ptr next) { n->next_ = next; } -}; - -int main() -{ - typedef boost::intrusive::circular_slist_algorithms algo; - my_node one, two, three; - - //Create an empty singly linked list container: - //"one" will be the first node of the container - algo::init(&one); - assert(algo::count(&one) == 1); - - //Now add a new node - algo::link_after(&one, &two); - assert(algo::count(&one) == 2); - - //Now add a new node after "one" - algo::link_after(&one, &three); - assert(algo::count(&one) == 3); - - //Now unlink the node after one - algo::unlink_after(&one); - assert(algo::count(&one) == 2); - - //Now unlink two - algo::unlink(&two); - assert(algo::count(&one) == 1); - - return 0; -} -//] diff --git a/example/doc_splay_algorithms.cpp b/example/doc_splay_algorithms.cpp deleted file mode 100644 index 5f929fa..0000000 --- a/example/doc_splay_algorithms.cpp +++ /dev/null @@ -1,79 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_splaytree_algorithms_code -#include -#include - -struct my_node -{ - my_node(int i = 0) - : int_(i) - {} - my_node *parent_, *left_, *right_; - int color_; - //other members - int int_; -}; - -//Define our own splaytree_node_traits -struct my_splaytree_node_traits -{ - typedef my_node node; - typedef my_node * node_ptr; - typedef const my_node * const_node_ptr; - - static node_ptr get_parent(const_node_ptr n) { return n->parent_; } - static void set_parent(node_ptr n, node_ptr parent){ n->parent_ = parent; } - static node_ptr get_left(const_node_ptr n) { return n->left_; } - static void set_left(node_ptr n, node_ptr left) { n->left_ = left; } - static node_ptr get_right(const_node_ptr n) { return n->right_; } - static void set_right(node_ptr n, node_ptr right) { n->right_ = right; } -}; - -struct node_ptr_compare -{ - bool operator()(my_node *a, my_node *b) - { return a->int_ < b->int_; } -}; - -int main() -{ - typedef boost::intrusive::splaytree_algorithms algo; - my_node header, two(2), three(3); - - //Create an empty splaytree container: - //"header" will be the header node of the tree - algo::init_header(&header); - - //Now insert node "two" in the tree using the sorting functor - algo::insert_equal_upper_bound(&header, &two, node_ptr_compare()); - - //Now insert node "three" in the tree using the sorting functor - algo::insert_equal_lower_bound(&header, &three, node_ptr_compare()); - - //Now take the first node (the left node of the header) - my_node *n = header.left_; - assert(n == &two); - - //Now go to the next node - n = algo::next_node(n); - assert(n == &three); - - //Erase a node just using a pointer to it - algo::unlink(&two); - - //Erase a node using also the header (faster) - algo::erase(&header, &three); - return 0; -} - -//] \ No newline at end of file diff --git a/example/doc_splay_set.cpp b/example/doc_splay_set.cpp deleted file mode 100644 index b82f48a..0000000 --- a/example/doc_splay_set.cpp +++ /dev/null @@ -1,93 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_splay_set_code -#include -#include -#include -#include - -using namespace boost::intrusive; - -class MyClass - : public splay_set_base_hook<> //This is an splay tree base hook - , public bs_set_base_hook<> //This is a binary search tree base hook - -{ - int int_; - - public: - //This is a member hook - splay_set_member_hook<> member_hook_; - - MyClass(int i) - : int_(i) - {} - friend bool operator< (const MyClass &a, const MyClass &b) - { return a.int_ < b.int_; } - friend bool operator> (const MyClass &a, const MyClass &b) - { return a.int_ > b.int_; } - friend bool operator== (const MyClass &a, const MyClass &b) - { return a.int_ < b.int_; } -}; - -//Define a set using the base hook that will store values in reverse order -typedef splay_set< MyClass, compare > > BaseSplaySet; - -//Define a set using the binary search tree hook -typedef splay_set< MyClass, base_hook > > BaseBsSplaySet; - -//Define an multiset using the member hook -typedef member_hook, &MyClass::member_hook_> MemberOption; -typedef splay_multiset< MyClass, MemberOption> MemberSplayMultiset; - -int main() -{ - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create several MyClass objects, each one with a different value - std::vector values; - for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); - - BaseSplaySet baseset; - BaseBsSplaySet bsbaseset; - MemberSplayMultiset membermultiset; - - - //Insert values in the container - for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){ - baseset.insert(*it); - bsbaseset.insert(*it); - membermultiset.insert(*it); - } - - //Now test sets - { - BaseSplaySet::reverse_iterator rbit(baseset.rbegin()), rbitend(baseset.rend()); - BaseBsSplaySet::iterator bsit(bsbaseset.begin()), bsitend(bsbaseset.end()); - MemberSplayMultiset::iterator mit(membermultiset.begin()), mitend(membermultiset.end()); - VectIt it(values.begin()), itend(values.end()); - - //Test the objects inserted in the base hook set - for(; it != itend; ++it, ++rbit){ - if(&*rbit != &*it) return 1; - } - - //Test the objects inserted in member and binary search hook sets - for(it = values.begin(); it != itend; ++it, ++bsit, ++mit){ - if(&*bsit != &*it) return 1; - if(&*mit != &*it) return 1; - } - } - return 0; -} -//] diff --git a/example/doc_splaytree_algorithms.cpp b/example/doc_splaytree_algorithms.cpp deleted file mode 100644 index f6c7f6f..0000000 --- a/example/doc_splaytree_algorithms.cpp +++ /dev/null @@ -1,78 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_splaytree_algorithms_code -#include -#include - -struct my_node -{ - my_node(int i = 0) - : int_(i) - {} - my_node *parent_, *left_, *right_; - //other members - int int_; -}; - -//Define our own splaytree_node_traits -struct my_splaytree_node_traits -{ - typedef my_node node; - typedef my_node * node_ptr; - typedef const my_node * const_node_ptr; - - static node_ptr get_parent(const_node_ptr n) { return n->parent_; } - static void set_parent(node_ptr n, node_ptr parent){ n->parent_ = parent; } - static node_ptr get_left(const_node_ptr n) { return n->left_; } - static void set_left(node_ptr n, node_ptr left) { n->left_ = left; } - static node_ptr get_right(const_node_ptr n) { return n->right_; } - static void set_right(node_ptr n, node_ptr right) { n->right_ = right; } -}; - -struct node_ptr_compare -{ - bool operator()(my_node *a, my_node *b) - { return a->int_ < b->int_; } -}; - -int main() -{ - typedef boost::intrusive::splaytree_algorithms algo; - my_node header, two(2), three(3); - - //Create an empty splaytree container: - //"header" will be the header node of the tree - algo::init_header(&header); - - //Now insert node "two" in the tree using the sorting functor - algo::insert_equal_upper_bound(&header, &two, node_ptr_compare()); - - //Now insert node "three" in the tree using the sorting functor - algo::insert_equal_lower_bound(&header, &three, node_ptr_compare()); - - //Now take the first node (the left node of the header) - my_node *n = header.left_; - assert(n == &two); - - //Now go to the next node - n = algo::next_node(n); - assert(n == &three); - - //Erase a node just using a pointer to it - algo::unlink(&two); - - //Erase a node using also the header (faster) - algo::erase(&header, &three); - return 0; -} - -//] diff --git a/example/doc_stateful_value_traits.cpp b/example/doc_stateful_value_traits.cpp deleted file mode 100644 index 96d16f3..0000000 --- a/example/doc_stateful_value_traits.cpp +++ /dev/null @@ -1,87 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_stateful_value_traits -#include - -using namespace boost::intrusive; - -//This type is not modifiable so we can't store hooks or custom nodes -typedef int identifier_t; - -//This value traits will associate elements from an array of identifiers with -//elements of an array of nodes. The element i of the value array will use the -//node i of the node array: -struct stateful_value_traits -{ - typedef list_node_traits node_traits; - typedef node_traits::node node; - typedef node * node_ptr; - typedef const node * const_node_ptr; - typedef identifier_t value_type; - typedef identifier_t * pointer; - typedef const identifier_t * const_pointer; - static const link_mode_type link_mode = normal_link; - - stateful_value_traits(pointer ids, node_ptr node_array) - : ids_(ids), nodes_(node_array) - {} - - ///Note: non static functions! - node_ptr to_node_ptr (value_type &value) - { return this->nodes_ + (&value - this->ids_); } - const_node_ptr to_node_ptr (const value_type &value) const - { return this->nodes_ + (&value - this->ids_); } - pointer to_value_ptr(node_ptr n) - { return this->ids_ + (n - this->nodes_); } - const_pointer to_value_ptr(const_node_ptr n) const - { return this->ids_ + (n - this->nodes_); } - - private: - pointer ids_; - node_ptr nodes_; -}; - -int main() -{ - const int NumElements = 100; - - //This is an array of ids that we want to "store" - identifier_t ids [NumElements]; - - //This is an array of nodes that is necessary to form the linked list - list_node_traits::node nodes [NumElements]; - - //Initialize id objects, each one with a different number - for(int i = 0; i != NumElements; ++i) ids[i] = i; - - //Define a list that will "link" identifiers using external nodes - typedef list > List; - - //This list will store ids without modifying identifier_t instances - //Stateful value traits must be explicitly passed in the constructor. - List my_list (stateful_value_traits (ids, nodes)); - - //Insert ids in reverse order in the list - for(identifier_t * it(&ids[0]), *itend(&ids[NumElements]); it != itend; ++it) - my_list.push_front(*it); - - //Now test lists - List::const_iterator list_it (my_list.cbegin()); - identifier_t *it_val(&ids[NumElements-1]), *it_rbeg_val(&ids[0]-1); - - //Test the objects inserted in the base hook list - for(; it_val != it_rbeg_val; --it_val, ++list_it) - if(&*list_it != &*it_val) return 1; - - return 0; -} -//] diff --git a/example/doc_unordered_set.cpp b/example/doc_unordered_set.cpp deleted file mode 100644 index 531ef10..0000000 --- a/example/doc_unordered_set.cpp +++ /dev/null @@ -1,94 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_unordered_set_code -#include -#include -#include -#include - -using namespace boost::intrusive; - -class MyClass : public unordered_set_base_hook<> -{ //This is a derivation hook - int int_; - - public: - unordered_set_member_hook<> member_hook_; //This is a member hook - - MyClass(int i) - : int_(i) - {} - - friend bool operator== (const MyClass &a, const MyClass &b) - { return a.int_ == b.int_; } - - friend std::size_t hash_value(const MyClass &value) - { return std::size_t(value.int_); } -}; - -//Define an unordered_set that will store MyClass objects using the base hook -typedef unordered_set BaseSet; - -//Define an unordered_multiset that will store MyClass using the member hook -typedef member_hook, &MyClass::member_hook_> - MemberOption; -typedef unordered_multiset< MyClass, MemberOption> MemberMultiSet; - -int main() -{ - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create a vector with 100 different MyClass objects - std::vector values; - for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); - - //Create a copy of the vector - std::vector values2(values); - - //Create a bucket array for base_set - BaseSet::bucket_type base_buckets[100]; - - //Create a bucket array for member_multi_set - MemberMultiSet::bucket_type member_buckets[200]; - - //Create unordered containers taking buckets as arguments - BaseSet base_set(BaseSet::bucket_traits(base_buckets, 100)); - MemberMultiSet member_multi_set - (MemberMultiSet::bucket_traits(member_buckets, 200)); - - //Now insert values's elements in the unordered_set - for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it) - base_set.insert(*it); - - //Now insert values's and values2's elements in the unordered_multiset - for(VectIt it(values.begin()), itend(values.end()), - it2(values2.begin()),itend2(values2.end()) - ; it != itend; ++it, ++it2){ - member_multi_set.insert(*it); - member_multi_set.insert(*it2); - } - - //Now find every element - { - VectIt it(values.begin()), itend(values.end()); - - for(; it != itend; ++it){ - //base_set should contain one element for each key - if(base_set.count(*it) != 1) return 1; - //member_multi_set should contain two elements for each key - if(member_multi_set.count(*it) != 2) return 1; - } - } - return 0; -} -//] diff --git a/example/doc_value_traits.cpp b/example/doc_value_traits.cpp deleted file mode 100644 index e50e23b..0000000 --- a/example/doc_value_traits.cpp +++ /dev/null @@ -1,103 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_value_traits_code_legacy -#include -#include -#include -#include - -//This node is the legacy type we can't modify and we want to insert in -//intrusive list and slist containers using only two pointers, since -//we know the object will never be at the same time in both lists. -struct legacy_value -{ - legacy_value *prev_; - legacy_value *next_; - int id_; -}; -//] - -//[doc_value_traits_value_traits -//Define our own NodeTraits that will configure singly and doubly linked -//list algorithms. Note that this node traits is compatible with -//circular_slist_algorithms and circular_list_algorithms. - -namespace bi = boost::intrusive; - -struct legacy_node_traits -{ - typedef legacy_value node; - typedef legacy_value * node_ptr; - typedef const legacy_value * const_node_ptr; - - static node *get_next(const node *n) { return n->next_; } - static void set_next(node *n, node *next) { n->next_ = next; } - static node *get_previous(const node *n) { return n->prev_; } - static void set_previous(node *n, node *prev) { n->prev_ = prev; } -}; - -//This ValueTraits will configure list and slist. In this case, -//legacy_node_traits::node is the same as the -//legacy_value_traits::value_type so to_node_ptr/to_value_ptr -//functions are trivial. -struct legacy_value_traits -{ - typedef legacy_node_traits node_traits; - typedef node_traits::node_ptr node_ptr; - typedef node_traits::const_node_ptr const_node_ptr; - typedef legacy_value value_type; - typedef legacy_value * pointer; - typedef const legacy_value * const_pointer; - static const bi::link_mode_type link_mode = bi::normal_link; - static node_ptr to_node_ptr (value_type &value) { return node_ptr(&value); } - static const_node_ptr to_node_ptr (const value_type &value) { return const_node_ptr(&value); } - static pointer to_value_ptr(node_ptr n) { return pointer(n); } - static const_pointer to_value_ptr(const_node_ptr n) { return const_pointer(n); } -}; - -//] - -//[doc_value_traits_test -//Now define an intrusive list and slist that will store legacy_value objects -typedef bi::value_traits ValueTraitsOption; -typedef bi::list LegacyAbiList; -typedef bi::slist LegacyAbiSlist; - -template -bool test_list() -{ - typedef std::vector Vect; - - //Create legacy_value objects, with a different internal number - Vect legacy_vector; - for(int i = 0; i < 100; ++i){ - legacy_value value; value.id_ = i; legacy_vector.push_back(value); - } - - //Create the list with the objects - List mylist(legacy_vector.begin(), legacy_vector.end()); - - //Now test both lists - typename List::const_iterator bit(mylist.begin()), bitend(mylist.end()); - typename Vect::const_iterator it(legacy_vector.begin()), itend(legacy_vector.end()); - - //Test the objects inserted in our list - for(; it != itend; ++it, ++bit) - if(&*bit != &*it) return false; - return true; -} - -int main() -{ - return test_list() && test_list() ? 0 : 1; -} -//] diff --git a/example/doc_window.cpp b/example/doc_window.cpp deleted file mode 100644 index 9f49e9e..0000000 --- a/example/doc_window.cpp +++ /dev/null @@ -1,83 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -//[doc_window_code -#include - -using namespace boost::intrusive; - -//An abstract class that can be inserted in an intrusive list -class Window : public list_base_hook<> -{ - public: - //This is a container those value is an abstract class: you can't do this with std::list. - typedef list win_list; - - //An static intrusive list declaration - static win_list all_windows; - - //Constructor. Includes this window in the list - Window() { all_windows.push_back(*this); } - //Destructor. Removes this node from the list - virtual ~Window() { all_windows.erase(win_list::s_iterator_to(*this)); } - //Pure virtual function to be implemented by derived classes - virtual void Paint() = 0; -}; - -//The static intrusive list declaration -Window::win_list Window::all_windows; - -//Some Window derived classes -class FrameWindow : public Window -{ void Paint(){/**/} }; - -class EditWindow : public Window -{ void Paint(){/**/} }; - -class CanvasWindow : public Window -{ void Paint(){/**/} }; - -//A function that prints all windows stored in the intrusive list -void paint_all_windows() -{ - for(Window::win_list::iterator i(Window::all_windows.begin()) - , e(Window::all_windows.end()) - ; i != e; ++i) - i->Paint(); -} - -//... - -//A class derived from Window -class MainWindow : public Window -{ - FrameWindow frame_; //these are derived from Window too - EditWindow edit_; - CanvasWindow canvas_; - - public: - void Paint(){/**/} - //... -}; - -//Main function -int main() -{ - //When a Window class is created, is automatically registered in the global list - MainWindow window; - - //Paint all the windows, sub-windows and so on - paint_all_windows(); - - //All the windows are automatically unregistered in their destructors. - return 0; -} -//] diff --git a/index.html b/index.html deleted file mode 100644 index 8c018f9..0000000 --- a/index.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - - -Automatic redirection failed, please go to -../../doc/html/intrusive - - diff --git a/perf/Jamfile.v2 b/perf/Jamfile.v2 deleted file mode 100644 index 3c9f3d6..0000000 --- a/perf/Jamfile.v2 +++ /dev/null @@ -1,34 +0,0 @@ -# Boost Intrusive Library Performance test Jamfile - -# (C) Copyright Ion Gaztaņaga 2006-2007. -# Use, modification and distribution are subject to the -# Boost Software License, Version 1.0. (See accompanying file -# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -# Adapted from John Maddock's TR1 Jamfile.v2 -# Copyright John Maddock 2005. -# Use, modification and distribution are subject to the -# Boost Software License, Version 1.0. (See accompanying file -# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -# this rule enumerates through all the sources and invokes -# the run rule for each source, the result is a list of all -# the run rules, which we can pass on to the test_suite rule: - -rule test_all -{ - local all_rules = ; - - for local fileb in [ glob *.cpp ] - { - all_rules += [ run $(fileb) - : # additional args - : # test-files - : # requirements - ] ; - } - - return $(all_rules) ; -} - -test-suite intrusive_perf : [ test_all r ] ; \ No newline at end of file diff --git a/perf/perf_list.cpp b/perf/perf_list.cpp deleted file mode 100644 index 99f3b25..0000000 --- a/perf/perf_list.cpp +++ /dev/null @@ -1,547 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// - -//Includes for tests -#include -#include -#include -#include -#include -#include - -using namespace boost::posix_time; - -//[perf_list_value_type -//Iteration and element count defines -const int NumIter = 100; -const int NumElements = 100000; - -using namespace boost::intrusive; - -template struct filler { int dummy[10]; }; -template <> struct filler {}; - -template //The object for non-intrusive containers -struct test_class : private filler -{ - int i_; - test_class() {} - test_class(int i) : i_(i) {} - friend bool operator <(const test_class &l, const test_class &r) { return l.i_ < r.i_; } - friend bool operator >(const test_class &l, const test_class &r) { return l.i_ > r.i_; } -}; - -template -struct itest_class //The object for intrusive containers - : public list_base_hook >, public test_class -{ - itest_class() {} - itest_class(int i) : test_class(i) {} -}; - -template //Adapts functors taking values to functors taking pointers -struct func_ptr_adaptor : public FuncObj -{ - typedef typename FuncObj::first_argument_type* first_argument_type; - typedef typename FuncObj::second_argument_type* second_argument_type; - typedef typename FuncObj::result_type result_type; - result_type operator()(first_argument_type a, second_argument_type b) const - { return FuncObj::operator()(*a, *b); } -}; -//] - -template -struct get_ilist //Helps to define an intrusive list from a policy -{ - typedef list, constant_time_size > type; -}; - -template //Helps to define an std list -struct get_list { typedef std::list > type; }; - -template //Helps to define an std pointer list -struct get_ptrlist { typedef std::list*> type; }; - -//////////////////////////////////////////////////////////////////////// -// -// PUSH_BACK -// -//////////////////////////////////////////////////////////////////////// - -template -void test_intrusive_list_push_back() -{ - typedef typename get_ilist::type ilist; - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - //First create the elements and insert them in the intrusive list - //[perf_list_push_back_intrusive - std::vector objects(NumElements); - ilist l; - for(int i = 0; i < NumElements; ++i) - l.push_back(objects[i]); - //Elements are unlinked in ilist's destructor - //Elements are destroyed in vector's destructor - //] - } - ptime tend = microsec_clock::universal_time(); - std::cout << "link_mode: " << LinkMode << ", usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_std_list_push_back() -{ - typedef typename get_list::type stdlist; - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - //Now create the std list and insert - //[perf_list_push_back_stdlist - stdlist l; - for(int i = 0; i < NumElements; ++i) - l.push_back(typename stdlist::value_type(i)); - //Elements unlinked and destroyed in stdlist's destructor - //] - } - ptime tend = microsec_clock::universal_time(); - std::cout << "std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_compact_std_ptrlist_push_back() -{ - typedef typename get_list::type stdlist; - typedef typename get_ptrlist::type stdptrlist; - //Now measure insertion time, including element creation - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - //Create elements and insert their pointer in the list - //[perf_list_push_back_stdptrlist - std::vector objects(NumElements); - stdptrlist l; - for(int i = 0; i < NumElements; ++i) - l.push_back(&objects[i]); - //Pointers to elements unlinked and destroyed in stdptrlist's destructor - //Elements destroyed in vector's destructor - //] - } - ptime tend = microsec_clock::universal_time(); - std::cout << "compact std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_disperse_std_ptrlist_push_back() -{ - typedef typename get_list::type stdlist; - typedef typename get_ptrlist::type stdptrlist; - //Now measure insertion time, including element creation - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - //Create elements and insert their pointer in the list - //[perf_list_push_back_disperse_stdptrlist - stdlist objects; stdptrlist l; - for(int i = 0; i < NumElements; ++i){ - objects.push_back(typename stdlist::value_type(i)); - l.push_back(&objects.back()); - } - //Pointers to elements unlinked and destroyed in stdptrlist's destructor - //Elements unlinked and destroyed in stdlist's destructor - //] - } - ptime tend = microsec_clock::universal_time(); - std::cout << "disperse std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -//////////////////////////////////////////////////////////////////////// -// -// REVERSE -// -//////////////////////////////////////////////////////////////////////// - -//[perf_list_reverse -template -void test_intrusive_list_reverse() -{ - typedef typename get_ilist::type ilist; - //First create the elements - std::vector objects(NumElements); - - //Now create the intrusive list and insert data - ilist l(objects.begin(), objects.end()); - - //Now measure sorting time - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - l.reverse(); - } - ptime tend = microsec_clock::universal_time(); - std::cout << "link_mode: " << LinkMode << ", usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_std_list_reverse() -{ - typedef typename get_list::type stdlist; - - //Create the list and insert values - stdlist l; - for(int i = 0; i < NumElements; ++i) - l.push_back(typename stdlist::value_type()); - - //Now measure sorting time - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - l.reverse(); - } - ptime tend = microsec_clock::universal_time(); - std::cout << "std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_compact_std_ptrlist_reverse() -{ - typedef typename get_list::type stdlist; - typedef typename get_ptrlist::type stdptrlist; - - //First create the elements - std::vector objects(NumElements); - - //Now create the std list and insert - stdptrlist l; - for(int i = 0; i < NumElements; ++i) - l.push_back(&objects[i]); - - //Now measure sorting time - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - l.reverse(); - } - ptime tend = microsec_clock::universal_time(); - std::cout << "compact std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_disperse_std_ptrlist_reverse() -{ - typedef typename get_list::type stdlist; - typedef typename get_ptrlist::type stdptrlist; - - //First create the elements - std::list objects; - //Now create the std list and insert - stdptrlist l; - for(int i = 0; i < NumElements; ++i){ - objects.push_back(typename stdlist::value_type()); - l.push_back(&objects.back()); - } - - //Now measure sorting time - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - l.reverse(); - } - ptime tend = microsec_clock::universal_time(); - std::cout << "disperse std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} -//] - -//////////////////////////////////////////////////////////////////////// -// -// SORT -// -//////////////////////////////////////////////////////////////////////// - -//[perf_list_sort -template -void test_intrusive_list_sort() -{ - typedef typename get_ilist::type ilist; - - //First create the elements - std::vector objects(NumElements); - for(int i = 0; i < NumElements; ++i) - objects[i].i_ = i; - - //Now create the intrusive list and insert data - ilist l(objects.begin(), objects.end()); - - //Now measure sorting time - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - if(!(i % 2)){ - l.sort(std::greater()); - } - else{ - l.sort(std::less()); - } - } - ptime tend = microsec_clock::universal_time(); - std::cout << "link_mode: " << LinkMode << ", usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_std_list_sort() -{ - typedef typename get_list::type stdlist; - - //Create the list and insert values - stdlist l; - for(int i = 0; i < NumElements; ++i) - l.push_back(typename stdlist::value_type(i)); - - //Now measure sorting time - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - if(!(i % 2)){ - l.sort(std::greater()); - } - else{ - l.sort(std::less()); - } - } - ptime tend = microsec_clock::universal_time(); - std::cout << "std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_compact_std_ptrlist_sort() -{ - typedef typename get_list::type stdlist; - typedef typename get_ptrlist::type stdptrlist; - - //First create the elements - std::vector objects(NumElements); - for(int i = 0; i < NumElements; ++i) - objects[i].i_ = i; - //Now create the std list and insert - stdptrlist l; - for(int i = 0; i < NumElements; ++i) - l.push_back(&objects[i]); - - //Now measure sorting time - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - if(!(i % 2)){ - l.sort(func_ptr_adaptor >()); - } - else{ - l.sort(func_ptr_adaptor >()); - } - } - ptime tend = microsec_clock::universal_time(); - std::cout << "compact std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_disperse_std_ptrlist_sort() -{ - typedef typename get_list::type stdlist; - typedef typename get_ptrlist::type stdptrlist; - - //First create the elements and the list - std::list objects; - stdptrlist l; - for(int i = 0; i < NumElements; ++i){ - objects.push_back(typename stdlist::value_type(i)); - l.push_back(&objects.back()); - } - - //Now measure sorting time - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - if(!(i % 2)){ - l.sort(func_ptr_adaptor >()); - } - else{ - l.sort(func_ptr_adaptor >()); - } - } - ptime tend = microsec_clock::universal_time(); - std::cout << "disperse std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} -//] - -//////////////////////////////////////////////////////////////////////// -// -// WRITE ACCESS -// -//////////////////////////////////////////////////////////////////////// -//[perf_list_write_access -template -void test_intrusive_list_write_access() -{ - typedef typename get_ilist::type ilist; - - //First create the elements - std::vector objects(NumElements); - for(int i = 0; i < NumElements; ++i){ - objects[i].i_ = i; - } - - //Now create the intrusive list and insert data - ilist l(objects.begin(), objects.end()); - - //Now measure access time to the value type - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - typename ilist::iterator it(l.begin()), end(l.end()); - for(; it != end; ++it){ - ++(it->i_); - } - } - ptime tend = microsec_clock::universal_time(); - std::cout << "link_mode: " << LinkMode << ", usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_std_list_write_access() -{ - typedef typename get_list::type stdlist; - - //Create the list and insert values - stdlist l; - for(int i = 0; i < NumElements; ++i) - l.push_back(typename stdlist::value_type(i)); - - //Now measure access time to the value type - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - typename stdlist::iterator it(l.begin()), end(l.end()); - for(; it != end; ++it){ - ++(it->i_); - } - } - ptime tend = microsec_clock::universal_time(); - std::cout << "std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_compact_std_ptrlist_write_access() -{ - typedef typename get_list::type stdlist; - typedef typename get_ptrlist::type stdptrlist; - - //First create the elements - std::vector objects(NumElements); - for(int i = 0; i < NumElements; ++i){ - objects[i].i_ = i; - } - - //Now create the std list and insert - stdptrlist l; - for(int i = 0; i < NumElements; ++i) - l.push_back(&objects[i]); - - //Now measure access time to the value type - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - typename stdptrlist::iterator it(l.begin()), end(l.end()); - for(; it != end; ++it){ - ++((*it)->i_); - } - } - ptime tend = microsec_clock::universal_time(); - std::cout << "compact std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} - -template -void test_disperse_std_ptrlist_write_access() -{ - typedef typename get_list::type stdlist; - typedef typename get_ptrlist::type stdptrlist; - - //First create the elements - std::list objects; - //Now create the std list and insert - stdptrlist l; - for(int i = 0; i < NumElements; ++i){ - objects.push_back(typename stdlist::value_type(i)); - l.push_back(&objects.back()); - } - - //Now measure access time to the value type - ptime tini = microsec_clock::universal_time(); - for(int i = 0; i < NumIter; ++i){ - typename stdptrlist::iterator it(l.begin()), end(l.end()); - for(; it != end; ++it){ - ++((*it)->i_); - } - } - ptime tend = microsec_clock::universal_time(); - std::cout << "disperse std::list usecs/iteration: " - << (tend-tini).total_microseconds()/NumIter << std::endl; -} -//] - -//////////////////////////////////////////////////////////////////////// -// -// ALL TESTS -// -//////////////////////////////////////////////////////////////////////// -template -void do_all_tests() -{ - std::cout << "\n\nTesting push back() with BigSize:" << BigSize << std::endl; - test_intrusive_list_push_back(); - test_intrusive_list_push_back(); - test_intrusive_list_push_back(); - test_std_list_push_back (); - test_compact_std_ptrlist_push_back(); - test_disperse_std_ptrlist_push_back(); - //reverse - std::cout << "\n\nTesting reverse() with BigSize:" << BigSize << std::endl; - test_intrusive_list_reverse(); - test_intrusive_list_reverse(); - test_intrusive_list_reverse(); - test_std_list_reverse(); - test_compact_std_ptrlist_reverse(); - test_disperse_std_ptrlist_reverse(); - //sort - std::cout << "\n\nTesting sort() with BigSize:" << BigSize << std::endl; - test_intrusive_list_sort(); - test_intrusive_list_sort(); - test_intrusive_list_sort(); - test_std_list_sort(); - test_compact_std_ptrlist_sort(); - test_disperse_std_ptrlist_sort(); - //write_access - std::cout << "\n\nTesting write_access() with BigSize:" << BigSize << std::endl; - test_intrusive_list_write_access(); - test_intrusive_list_write_access(); - test_intrusive_list_write_access(); - test_std_list_write_access(); - test_compact_std_ptrlist_write_access(); - test_disperse_std_ptrlist_write_access(); -} - -int main() -{ - //First pass the tests with a small size class - do_all_tests(); - //Now pass the tests with a big size class - do_all_tests(); - return 0; -} diff --git a/proj/vc7ide/Intrusive.sln b/proj/vc7ide/Intrusive.sln deleted file mode 100644 index 8767b1e..0000000 --- a/proj/vc7ide/Intrusive.sln +++ /dev/null @@ -1,175 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 8.00 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "list", "list\list.vcproj", "{977B61B4-9968-497C-9F0B-24A8145473B8}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "slist", "slist\slist.vcproj", "{5A02061D-3728-4C49-AFC8-0130C1F161C0}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "multiset", "multiset\multiset.vcproj", "{961F0E06-C092-4AF7-ABC5-2A49999F3B79}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_intrusivelib", "_intrusivelib\_intrusivelib.vcproj", "{90F3C5BD-8E6C-4629-BC71-A1009EC88059}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "set", "set\set.vcproj", "{960E01F6-92C1-F74A-BCA5-2A9F3B994979}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "unordered_set", "unordered_set\unordered_set.vcproj", "{90E701E6-2C91-F4A7-BA6C-A9F3B0949279}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "unordered_multiset", "unordered_multiset\unordered_multiset.vcproj", "{9101EE76-BB6C-2C91-F4B7-A9F3B9490279}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "perf_test", "perf_test\perf_test.vcproj", "{910E70E6-2C91-AA67-BF4C-A9C74A309927}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "default_hook", "default_hook\default_hook.vcproj", "{761A79B4-9968-CB81-F02B-2A4497345475}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stateful_value_traits", "stateful_value_traits\stateful_value_traits.vcproj", "{9571A7B4-9968-B9C1-17FB-134547B46975}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "virtual_base", "virtual_base\virtual_base.vcproj", "{3579B1A4-9894-02AB-CB81-297B46154345}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "make_functions", "make_functions\make_functions.vcproj", "{7679B41B-F2B4-9176-CB81-35449467B435}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "custom_bucket_traits", "custom_bucket_traits\custom_bucket_traits.vcproj", "{31C77B84-0B2C-9481-CB81-27A149F33825}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "external_value_traits", "external_value_traits\external_value_traits.vcproj", "{97B69A72-B9D3-7389-17FB-74612F4A9543}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "splay_multiset", "splay_multiset\splay_multiset.vcproj", "{01E70176-B6C5-BF47-2C91-A949077BA323}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "splay_set", "splay_set\splay_set.vcproj", "{1E6909E7-C971-F24A-6C7B-A92094B71B59}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "avl_multiset", "avl_multiset\avl_multiset.vcproj", "{0AE70176-5B8C-4BC7-392C-A4A312B07893}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "avl_set", "avl_set\avl_set.vcproj", "{16909EE7-24AF-97C1-C76B-204B971BA959}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sg_multiset", "sg_multiset\sg_multiset.vcproj", "{07022E76-6CB5-92C1-B47F-A10772A79B43}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sg_set", "sg_set\sg_set.vcproj", "{1690A9E7-DB57-971C-F24A-09B752A942F7}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Global - GlobalSection(SolutionConfiguration) = preSolution - Debug = Debug - Release = Release - EndGlobalSection - GlobalSection(ProjectDependencies) = postSolution - EndGlobalSection - GlobalSection(ProjectConfiguration) = postSolution - {977B61B4-9968-497C-9F0B-24A8145473B8}.Debug.ActiveCfg = Debug|Win32 - {977B61B4-9968-497C-9F0B-24A8145473B8}.Debug.Build.0 = Debug|Win32 - {977B61B4-9968-497C-9F0B-24A8145473B8}.Release.ActiveCfg = Release|Win32 - {977B61B4-9968-497C-9F0B-24A8145473B8}.Release.Build.0 = Release|Win32 - {5A02061D-3728-4C49-AFC8-0130C1F161C0}.Debug.ActiveCfg = Debug|Win32 - {5A02061D-3728-4C49-AFC8-0130C1F161C0}.Debug.Build.0 = Debug|Win32 - {5A02061D-3728-4C49-AFC8-0130C1F161C0}.Release.ActiveCfg = Release|Win32 - {5A02061D-3728-4C49-AFC8-0130C1F161C0}.Release.Build.0 = Release|Win32 - {961F0E06-C092-4AF7-ABC5-2A49999F3B79}.Debug.ActiveCfg = Debug|Win32 - {961F0E06-C092-4AF7-ABC5-2A49999F3B79}.Debug.Build.0 = Debug|Win32 - {961F0E06-C092-4AF7-ABC5-2A49999F3B79}.Release.ActiveCfg = Release|Win32 - {961F0E06-C092-4AF7-ABC5-2A49999F3B79}.Release.Build.0 = Release|Win32 - {90F3C5BD-8E6C-4629-BC71-A1009EC88059}.Debug.ActiveCfg = Debug|Win32 - {90F3C5BD-8E6C-4629-BC71-A1009EC88059}.Debug.Build.0 = Debug|Win32 - {90F3C5BD-8E6C-4629-BC71-A1009EC88059}.Release.ActiveCfg = Release|Win32 - {90F3C5BD-8E6C-4629-BC71-A1009EC88059}.Release.Build.0 = Release|Win32 - {960E01F6-92C1-F74A-BCA5-2A9F3B994979}.Debug.ActiveCfg = Debug|Win32 - {960E01F6-92C1-F74A-BCA5-2A9F3B994979}.Debug.Build.0 = Debug|Win32 - {960E01F6-92C1-F74A-BCA5-2A9F3B994979}.Release.ActiveCfg = Release|Win32 - {960E01F6-92C1-F74A-BCA5-2A9F3B994979}.Release.Build.0 = Release|Win32 - {90E701E6-2C91-F4A7-BA6C-A9F3B0949279}.Debug.ActiveCfg = Debug|Win32 - {90E701E6-2C91-F4A7-BA6C-A9F3B0949279}.Debug.Build.0 = Debug|Win32 - {90E701E6-2C91-F4A7-BA6C-A9F3B0949279}.Release.ActiveCfg = Release|Win32 - {90E701E6-2C91-F4A7-BA6C-A9F3B0949279}.Release.Build.0 = Release|Win32 - {9101EE76-BB6C-2C91-F4B7-A9F3B9490279}.Debug.ActiveCfg = Debug|Win32 - {9101EE76-BB6C-2C91-F4B7-A9F3B9490279}.Debug.Build.0 = Debug|Win32 - {9101EE76-BB6C-2C91-F4B7-A9F3B9490279}.Release.ActiveCfg = Release|Win32 - {9101EE76-BB6C-2C91-F4B7-A9F3B9490279}.Release.Build.0 = Release|Win32 - {910E70E6-2C91-AA67-BF4C-A9C74A309927}.Debug.ActiveCfg = Debug|Win32 - {910E70E6-2C91-AA67-BF4C-A9C74A309927}.Debug.Build.0 = Debug|Win32 - {910E70E6-2C91-AA67-BF4C-A9C74A309927}.Release.ActiveCfg = Release|Win32 - {910E70E6-2C91-AA67-BF4C-A9C74A309927}.Release.Build.0 = Release|Win32 - {761A79B4-9968-CB81-F02B-2A4497345475}.Debug.ActiveCfg = Debug|Win32 - {761A79B4-9968-CB81-F02B-2A4497345475}.Debug.Build.0 = Debug|Win32 - {761A79B4-9968-CB81-F02B-2A4497345475}.Release.ActiveCfg = Release|Win32 - {761A79B4-9968-CB81-F02B-2A4497345475}.Release.Build.0 = Release|Win32 - {9571A7B4-9968-B9C1-17FB-134547B46975}.Debug.ActiveCfg = Debug|Win32 - {9571A7B4-9968-B9C1-17FB-134547B46975}.Debug.Build.0 = Debug|Win32 - {9571A7B4-9968-B9C1-17FB-134547B46975}.Release.ActiveCfg = Release|Win32 - {9571A7B4-9968-B9C1-17FB-134547B46975}.Release.Build.0 = Release|Win32 - {3579B1A4-9894-02AB-CB81-297B46154345}.Debug.ActiveCfg = Debug|Win32 - {3579B1A4-9894-02AB-CB81-297B46154345}.Debug.Build.0 = Debug|Win32 - {3579B1A4-9894-02AB-CB81-297B46154345}.Release.ActiveCfg = Release|Win32 - {3579B1A4-9894-02AB-CB81-297B46154345}.Release.Build.0 = Release|Win32 - {7679B41B-F2B4-9176-CB81-35449467B435}.Debug.ActiveCfg = Debug|Win32 - {7679B41B-F2B4-9176-CB81-35449467B435}.Debug.Build.0 = Debug|Win32 - {7679B41B-F2B4-9176-CB81-35449467B435}.Release.ActiveCfg = Release|Win32 - {7679B41B-F2B4-9176-CB81-35449467B435}.Release.Build.0 = Release|Win32 - {31C77B84-0B2C-9481-CB81-27A149F33825}.Debug.ActiveCfg = Debug|Win32 - {31C77B84-0B2C-9481-CB81-27A149F33825}.Debug.Build.0 = Debug|Win32 - {31C77B84-0B2C-9481-CB81-27A149F33825}.Release.ActiveCfg = Release|Win32 - {31C77B84-0B2C-9481-CB81-27A149F33825}.Release.Build.0 = Release|Win32 - {97B69A72-B9D3-7389-17FB-74612F4A9543}.Debug.ActiveCfg = Debug|Win32 - {97B69A72-B9D3-7389-17FB-74612F4A9543}.Debug.Build.0 = Debug|Win32 - {97B69A72-B9D3-7389-17FB-74612F4A9543}.Release.ActiveCfg = Release|Win32 - {97B69A72-B9D3-7389-17FB-74612F4A9543}.Release.Build.0 = Release|Win32 - {01E70176-B6C5-BF47-2C91-A949077BA323}.Debug.ActiveCfg = Debug|Win32 - {01E70176-B6C5-BF47-2C91-A949077BA323}.Debug.Build.0 = Debug|Win32 - {01E70176-B6C5-BF47-2C91-A949077BA323}.Release.ActiveCfg = Release|Win32 - {01E70176-B6C5-BF47-2C91-A949077BA323}.Release.Build.0 = Release|Win32 - {1E6909E7-C971-F24A-6C7B-A92094B71B59}.Debug.ActiveCfg = Debug|Win32 - {1E6909E7-C971-F24A-6C7B-A92094B71B59}.Debug.Build.0 = Debug|Win32 - {1E6909E7-C971-F24A-6C7B-A92094B71B59}.Release.ActiveCfg = Release|Win32 - {1E6909E7-C971-F24A-6C7B-A92094B71B59}.Release.Build.0 = Release|Win32 - {0AE70176-5B8C-4BC7-392C-A4A312B07893}.Debug.ActiveCfg = Debug|Win32 - {0AE70176-5B8C-4BC7-392C-A4A312B07893}.Debug.Build.0 = Debug|Win32 - {0AE70176-5B8C-4BC7-392C-A4A312B07893}.Release.ActiveCfg = Release|Win32 - {0AE70176-5B8C-4BC7-392C-A4A312B07893}.Release.Build.0 = Release|Win32 - {16909EE7-24AF-97C1-C76B-204B971BA959}.Debug.ActiveCfg = Debug|Win32 - {16909EE7-24AF-97C1-C76B-204B971BA959}.Debug.Build.0 = Debug|Win32 - {16909EE7-24AF-97C1-C76B-204B971BA959}.Release.ActiveCfg = Release|Win32 - {16909EE7-24AF-97C1-C76B-204B971BA959}.Release.Build.0 = Release|Win32 - {07022E76-6CB5-92C1-B47F-A10772A79B43}.Debug.ActiveCfg = Debug|Win32 - {07022E76-6CB5-92C1-B47F-A10772A79B43}.Debug.Build.0 = Debug|Win32 - {07022E76-6CB5-92C1-B47F-A10772A79B43}.Release.ActiveCfg = Release|Win32 - {07022E76-6CB5-92C1-B47F-A10772A79B43}.Release.Build.0 = Release|Win32 - {1690A9E7-DB57-971C-F24A-09B752A942F7}.Debug.ActiveCfg = Debug|Win32 - {1690A9E7-DB57-971C-F24A-09B752A942F7}.Debug.Build.0 = Debug|Win32 - {1690A9E7-DB57-971C-F24A-09B752A942F7}.Release.ActiveCfg = Release|Win32 - {1690A9E7-DB57-971C-F24A-09B752A942F7}.Release.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - EndGlobalSection - GlobalSection(ExtensibilityAddIns) = postSolution - EndGlobalSection -EndGlobal diff --git a/proj/vc7ide/Intrusive.vcproj b/proj/vc7ide/Intrusive.vcproj deleted file mode 100644 index fc776c0..0000000 --- a/proj/vc7ide/Intrusive.vcproj +++ /dev/null @@ -1,129 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj deleted file mode 100644 index 9b36d35..0000000 --- a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj +++ /dev/null @@ -1,423 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/avl_multiset/avl_multiset.vcproj b/proj/vc7ide/avl_multiset/avl_multiset.vcproj deleted file mode 100644 index a2d2cb1..0000000 --- a/proj/vc7ide/avl_multiset/avl_multiset.vcproj +++ /dev/null @@ -1,128 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/avl_set/avl_set.vcproj b/proj/vc7ide/avl_set/avl_set.vcproj deleted file mode 100644 index e5df0b2..0000000 --- a/proj/vc7ide/avl_set/avl_set.vcproj +++ /dev/null @@ -1,127 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/custom_bucket_traits/custom_bucket_traits.vcproj b/proj/vc7ide/custom_bucket_traits/custom_bucket_traits.vcproj deleted file mode 100644 index a396174..0000000 --- a/proj/vc7ide/custom_bucket_traits/custom_bucket_traits.vcproj +++ /dev/null @@ -1,133 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/default_hook/default_hook.vcproj b/proj/vc7ide/default_hook/default_hook.vcproj deleted file mode 100644 index 9c26fb7..0000000 --- a/proj/vc7ide/default_hook/default_hook.vcproj +++ /dev/null @@ -1,133 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/external_value_traits/external_value_traits.vcproj b/proj/vc7ide/external_value_traits/external_value_traits.vcproj deleted file mode 100644 index 87ebd51..0000000 --- a/proj/vc7ide/external_value_traits/external_value_traits.vcproj +++ /dev/null @@ -1,133 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/list/list.vcproj b/proj/vc7ide/list/list.vcproj deleted file mode 100644 index 17c3b6a..0000000 --- a/proj/vc7ide/list/list.vcproj +++ /dev/null @@ -1,143 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/make_functions/make_functions.vcproj b/proj/vc7ide/make_functions/make_functions.vcproj deleted file mode 100644 index cf926ce..0000000 --- a/proj/vc7ide/make_functions/make_functions.vcproj +++ /dev/null @@ -1,133 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/multiset/multiset.vcproj b/proj/vc7ide/multiset/multiset.vcproj deleted file mode 100644 index 4303b0c..0000000 --- a/proj/vc7ide/multiset/multiset.vcproj +++ /dev/null @@ -1,137 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/perf_test/perf_test.vcproj b/proj/vc7ide/perf_test/perf_test.vcproj deleted file mode 100644 index e94844d..0000000 --- a/proj/vc7ide/perf_test/perf_test.vcproj +++ /dev/null @@ -1,138 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/set/set.vcproj b/proj/vc7ide/set/set.vcproj deleted file mode 100644 index 6e96cd2..0000000 --- a/proj/vc7ide/set/set.vcproj +++ /dev/null @@ -1,137 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/sg_multiset/sg_multiset.vcproj b/proj/vc7ide/sg_multiset/sg_multiset.vcproj deleted file mode 100644 index fab36dc..0000000 --- a/proj/vc7ide/sg_multiset/sg_multiset.vcproj +++ /dev/null @@ -1,128 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/sg_set/sg_set.vcproj b/proj/vc7ide/sg_set/sg_set.vcproj deleted file mode 100644 index 222fffc..0000000 --- a/proj/vc7ide/sg_set/sg_set.vcproj +++ /dev/null @@ -1,127 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/slist/slist.vcproj b/proj/vc7ide/slist/slist.vcproj deleted file mode 100644 index d9138b2..0000000 --- a/proj/vc7ide/slist/slist.vcproj +++ /dev/null @@ -1,137 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/splay_multiset/splay_multiset.vcproj b/proj/vc7ide/splay_multiset/splay_multiset.vcproj deleted file mode 100644 index 8239a8f..0000000 --- a/proj/vc7ide/splay_multiset/splay_multiset.vcproj +++ /dev/null @@ -1,128 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/splay_set/splay_set.vcproj b/proj/vc7ide/splay_set/splay_set.vcproj deleted file mode 100644 index 63a9d43..0000000 --- a/proj/vc7ide/splay_set/splay_set.vcproj +++ /dev/null @@ -1,127 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/stateful_value_traits/stateful_value_traits.vcproj b/proj/vc7ide/stateful_value_traits/stateful_value_traits.vcproj deleted file mode 100644 index 72cbe3b..0000000 --- a/proj/vc7ide/stateful_value_traits/stateful_value_traits.vcproj +++ /dev/null @@ -1,133 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/to-do.txt b/proj/vc7ide/to-do.txt deleted file mode 100644 index ae66bad..0000000 --- a/proj/vc7ide/to-do.txt +++ /dev/null @@ -1,7 +0,0 @@ -add includes needed for the snippets -add section explaining splice(...,n) -Faltaría, en mi opinión, una guía de qué headers incluyen a cuáles. P.ej., -los *_hook.hpp típicamente están incluidos por el header del contenedor asociado, etc. -Add resize() to list -Optimize rehash for when shrinking: there is no need to hash the values. -Optimize store_hash to work with the same node and type traits as store_hash diff --git a/proj/vc7ide/unordered_multiset/unordered_multiset.vcproj b/proj/vc7ide/unordered_multiset/unordered_multiset.vcproj deleted file mode 100644 index 2b9c6f3..0000000 --- a/proj/vc7ide/unordered_multiset/unordered_multiset.vcproj +++ /dev/null @@ -1,138 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/unordered_set/unordered_set.vcproj b/proj/vc7ide/unordered_set/unordered_set.vcproj deleted file mode 100644 index 33c95ac..0000000 --- a/proj/vc7ide/unordered_set/unordered_set.vcproj +++ /dev/null @@ -1,137 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proj/vc7ide/virtual_base/virtual_base.vcproj b/proj/vc7ide/virtual_base/virtual_base.vcproj deleted file mode 100644 index b5f6f66..0000000 --- a/proj/vc7ide/virtual_base/virtual_base.vcproj +++ /dev/null @@ -1,133 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 deleted file mode 100644 index 58db1e5..0000000 --- a/test/Jamfile.v2 +++ /dev/null @@ -1,33 +0,0 @@ -# Boost Intrusive Library Test Jamfile -# (C) Copyright Ion Gaztaņaga 2006. -# Use, modification and distribution are subject to the -# Boost Software License, Version 1.0. (See accompanying file -# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -# Adapted from John Maddock's TR1 Jamfile.v2 -# Copyright John Maddock 2005. -# Use, modification and distribution are subject to the -# Boost Software License, Version 1.0. (See accompanying file -# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -# this rule enumerates through all the sources and invokes -# the run rule for each source, the result is a list of all -# the run rules, which we can pass on to the test_suite rule: - -rule test_all -{ - local all_rules = ; - - for local fileb in [ glob *.cpp ] - { - all_rules += [ run $(fileb) - : # additional args - : # test-files - : # requirements - ] ; - } - - return $(all_rules) ; -} - -test-suite intrusive_test : [ test_all r ] : multi ; \ No newline at end of file diff --git a/test/avl_multiset_test.cpp b/test/avl_multiset_test.cpp deleted file mode 100644 index dcaf774..0000000 --- a/test/avl_multiset_test.cpp +++ /dev/null @@ -1,118 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "generic_multiset_test.hpp" - -template< class ValueType - , class Option1 = boost::intrusive::none - , class Option2 = boost::intrusive::none - , class Option3 = boost::intrusive::none - > -struct GetContainer -{ - typedef boost::intrusive::avl_multiset - < ValueType - , Option1 - , Option2 - , Option3 - > type; -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::avl_set_base_hook_t - >::type - , GetContainer - >::test_all(); - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::avl_set_member_hook_t - , &value_type::avl_set_node_ - > - >::type - , GetContainer - >::test_all(); - return 0; - } -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::avl_set_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::avl_set_member_hook_t - , &value_type::avl_set_node_ - > - >::type - , GetContainer - >::test_all(); - - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::avl_set_auto_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::avl_set_auto_member_hook_t - , &value_type::avl_set_auto_node_ - > - >::type - , GetContainer - >::test_all(); - return 0; - } -}; - -int main( int, char* [] ) -{ - test_main_template()(); - test_main_template, false>()(); - test_main_template()(); - test_main_template, true>()(); - return boost::report_errors(); -} - -#include diff --git a/test/avl_set_test.cpp b/test/avl_set_test.cpp deleted file mode 100644 index 1f7150e..0000000 --- a/test/avl_set_test.cpp +++ /dev/null @@ -1,119 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "generic_set_test.hpp" - -template< class ValueType - , class Option1 = boost::intrusive::none - , class Option2 = boost::intrusive::none - , class Option3 = boost::intrusive::none - > -struct GetContainer -{ - typedef boost::intrusive::avl_set - < ValueType - , Option1 - , Option2 - , Option3 - > type; -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::avl_set_base_hook_t - >::type - , GetContainer - >::test_all(); - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::avl_set_member_hook_t - , &value_type::avl_set_node_ - > - >::type - , GetContainer - >::test_all(); - return 0; - } -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::avl_set_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::avl_set_member_hook_t - , &value_type::avl_set_node_ - > - >::type - , GetContainer - >::test_all(); - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::avl_set_auto_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::avl_set_auto_member_hook_t - , &value_type::avl_set_auto_node_ - > - >::type - , GetContainer - >::test_all(); - - return 0; - } -}; - -int main( int, char* [] ) -{ - - test_main_template()(); - test_main_template, false>()(); - test_main_template()(); - test_main_template, true>()(); - return boost::report_errors(); -} - -#include diff --git a/test/common_functors.hpp b/test/common_functors.hpp deleted file mode 100644 index 7a6d500..0000000 --- a/test/common_functors.hpp +++ /dev/null @@ -1,48 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// - -#ifndef BOOST_INTRUSIVE_TEST_COMMON_FUNCTORS_HPP -#define BOOST_INTRUSIVE_TEST_COMMON_FUNCTORS_HPP - -#include -#include - -namespace boost { -namespace intrusive { -namespace test { - -template -class delete_disposer -{ - public: - template - void operator()(Pointer p) - { - typedef typename std::iterator_traits::value_type value_type; - BOOST_INTRUSIVE_INVARIANT_ASSERT(( detail::is_same::value )); - delete detail::get_pointer(p); - } -}; - -template -class new_cloner -{ - public: - T *operator()(const T &t) - { return new T(t); } -}; - -} //namespace test { -} //namespace intrusive { -} //namespace boost { - -#endif diff --git a/test/custom_bucket_traits_test.cpp b/test/custom_bucket_traits_test.cpp deleted file mode 100644 index 7539b9c..0000000 --- a/test/custom_bucket_traits_test.cpp +++ /dev/null @@ -1,127 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include -#include - -using namespace boost::intrusive; - -class MyClass : public unordered_set_base_hook<> -{ - int int_; - - public: - MyClass(int i = 0) : int_(i) - {} - unordered_set_member_hook<> member_hook_; - - friend bool operator==(const MyClass &l, const MyClass &r) - { return l.int_ == r.int_; } - - friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } -}; - -struct uset_value_traits -{ - typedef slist_node_traits node_traits; - typedef node_traits::node_ptr node_ptr; - typedef node_traits::const_node_ptr const_node_ptr; - typedef MyClass value_type; - typedef MyClass * pointer; - typedef const MyClass * const_pointer; - static const link_mode_type link_mode = normal_link; - - static node_ptr to_node_ptr (value_type &value) - { return node_ptr(&value); } - static const_node_ptr to_node_ptr (const value_type &value) - { return const_node_ptr(&value); } - static pointer to_value_ptr(node_ptr n) - { return static_cast(n); } - static const_pointer to_value_ptr(const_node_ptr n) - { return static_cast(n); } -}; - -//Base -typedef base_hook< unordered_set_base_hook<> > BaseHook; -typedef unordered_bucket::type BaseBucketType; -typedef unordered_set BaseUset; -//Member -typedef member_hook - < MyClass, unordered_set_member_hook<> - , &MyClass::member_hook_ > MemberHook; -typedef unordered_bucket::type MemberBucketType; -typedef unordered_set MemberUset; -//Explicit -typedef value_traits< uset_value_traits > Traits; -typedef unordered_bucket::type TraitsBucketType; -typedef unordered_set TraitsUset; - -struct uset_bucket_traits -{ - //Power of two bucket length - static const std::size_t NumBuckets = 128; - - uset_bucket_traits(BaseBucketType *buckets) - : buckets_(buckets) - {} - - uset_bucket_traits(const uset_bucket_traits &other) - : buckets_(other.buckets_) - {} - - BaseBucketType * bucket_begin() const - { return buckets_; } - - std::size_t bucket_count() const - { return NumBuckets; } - - BaseBucketType *buckets_; -}; - -typedef unordered_set - , power_2_buckets > - BucketTraitsUset; - -int main() -{ - if(!detail::is_same::value) - return 1; - if(!detail::is_same::value) - return 1; - if(!detail::is_same::value) - return 1; - if(!detail::is_same::value) - return 1; - if(!detail::is_same::value) - return 1; - - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - std::vector values; - - for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); - - BaseBucketType buckets[uset_bucket_traits::NumBuckets]; - uset_bucket_traits btraits(buckets); - BucketTraitsUset uset(btraits); - - for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it) - uset.insert(*it); - - for( VectRit it(values.rbegin()), itend(values.rend()); it != itend; ++it){ - if(uset.find(*it) == uset.cend()) return 1; - } - - return 0; -} diff --git a/test/default_hook_test.cpp b/test/default_hook_test.cpp deleted file mode 100644 index 938095f..0000000 --- a/test/default_hook_test.cpp +++ /dev/null @@ -1,97 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include -#include -#include "smart_ptr.hpp" -#include - -using namespace boost::intrusive; - -class MyClass - -: public list_base_hook - < void_pointer >, link_mode > -, public slist_base_hook - < void_pointer >, link_mode > -, public set_base_hook - < void_pointer >, link_mode > -, public unordered_set_base_hook - < void_pointer >, link_mode > -{ - int int_; - - public: - MyClass(int i) - : int_(i) - {} - - friend bool operator<(const MyClass &l, const MyClass &r) - { return l.int_ < r.int_; } - - friend bool operator==(const MyClass &l, const MyClass &r) - { return l.int_ == r.int_; } - - friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } -}; - -//Define a list that will store MyClass using the public base hook -typedef list List; -typedef slist Slist; -typedef set Set; -typedef unordered_set USet; - -int main() -{ - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create several MyClass objects, each one with a different value - std::vector values; - for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); - - USet::bucket_type buckets[100]; - - List my_list; - Slist my_slist; - Set my_set; - USet my_uset(USet::bucket_traits(buckets, 100)); - - //Now insert them in the reverse order - //in the base hook intrusive list - for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){ - my_list.push_front(*it); - my_slist.push_front(*it); - my_set.insert(*it); - my_uset.insert(*it); - } - - //Now test lists - { - List::const_iterator list_it(my_list.cbegin()); - Slist::const_iterator slist_it(my_slist.cbegin()); - Set::const_reverse_iterator set_rit(my_set.crbegin()); - VectRit vect_it(values.rbegin()), vect_itend(values.rend()); - - //Test the objects inserted in the base hook list - for(; vect_it != vect_itend; ++vect_it, ++list_it, ++slist_it, ++set_rit){ - if(&*list_it != &*vect_it) return 1; - if(&*slist_it != &*vect_it) return 1; - if(&*set_rit != &*vect_it) return 1; - if(my_uset.find(*set_rit) == my_uset.cend()) return 1; - } - } - - return 0; -} diff --git a/test/external_value_traits_test.cpp b/test/external_value_traits_test.cpp deleted file mode 100644 index fa380e2..0000000 --- a/test/external_value_traits_test.cpp +++ /dev/null @@ -1,239 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include -#include -#include -#include -#include - -using namespace boost::intrusive; - -class MyClass -{ - public: - int int_; - - MyClass(int i = 0) - : int_(i) - {} - friend bool operator > (const MyClass &l, const MyClass &r) - { return l.int_ > r.int_; } - friend bool operator == (const MyClass &l, const MyClass &r) - { return l.int_ == r.int_; } - friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } -}; - -const int NumElements = 100; - -template -struct external_traits -{ - typedef NodeTraits node_traits; - typedef typename node_traits::node node; - typedef typename node_traits::node_ptr node_ptr; - typedef typename node_traits::const_node_ptr const_node_ptr; - typedef MyClass value_type; - typedef typename boost::pointer_to_other - ::type pointer; - typedef typename boost::pointer_to_other - ::type const_pointer; - static const link_mode_type link_mode = normal_link; - - external_traits(pointer values, std::size_t NumElem) - : values_(values), node_array_(NumElem) - {} - - node_ptr to_node_ptr (value_type &value) - { return (&node_array_[0]) + (&value - values_); } - - const_node_ptr to_node_ptr (const value_type &value) const - { return &node_array_[0] + (&value - values_); } - - pointer to_value_ptr(node_ptr n) - { return values_ + (n - &node_array_[0]); } - - const_pointer to_value_ptr(const_node_ptr n) const - { return values_ + (n - &node_array_[0]); } - - pointer values_; - std::vector node_array_; -}; - -template -struct value_traits_proxy; - -template -struct traits_holder - : public T -{}; - -typedef value_traits_proxy > list_value_traits_proxy; -typedef value_traits_proxy > slist_value_traits_proxy; -typedef value_traits_proxy > rbtree_value_traits_proxy; -typedef value_traits_proxy > > hash_value_traits_proxy; - -struct uset_bucket_traits -{ - private: - typedef unordered_bucket > > > >::type bucket_type; - - //Non-copyable - uset_bucket_traits(const uset_bucket_traits &other); - uset_bucket_traits & operator=(const uset_bucket_traits &other); - - public: - static const std::size_t NumBuckets = 100; - - uset_bucket_traits(){} - - bucket_type * bucket_begin() const - { return buckets_; } - - std::size_t bucket_count() const - { return NumBuckets; } - - mutable bucket_type buckets_[NumBuckets]; -}; - -struct bucket_traits_proxy -{ - static const bool external_bucket_traits = true; - typedef uset_bucket_traits bucket_traits; - - template - bucket_traits &get_bucket_traits(Container &cont); - - template - const bucket_traits &get_bucket_traits(const Container &cont) const; -}; - -//Define a list that will store MyClass using the external hook -typedef list > List; -//Define a slist that will store MyClass using the external hook -typedef slist > Slist; -//Define a rbtree that will store MyClass using the external hook -typedef rbtree< MyClass - , value_traits - , compare > > Rbtree; -//Define a hashtable that will store MyClass using the external hook -typedef hashtable< MyClass - , value_traits - , bucket_traits - > Hash; - -template -struct value_traits_proxy -{ - static const bool external_value_traits = true; - typedef external_traits value_traits; - - template - const value_traits &get_value_traits(const Container &cont) const; - - template - value_traits &get_value_traits(Container &cont); -}; - -struct ContainerHolder - : public uset_bucket_traits - , public List - , public external_traits > - , public Slist - , public external_traits > - , public Rbtree - , public external_traits > - , public Hash - , public external_traits > > -{ - static const std::size_t NumBucket = 100; - ContainerHolder(MyClass *values, std::size_t num_elem) - : uset_bucket_traits() - , List() - , external_traits >(values, num_elem) - , Slist() - , external_traits >(values, num_elem) - , Rbtree() - , external_traits >(values, num_elem) - , Hash(Hash::bucket_traits()) - , external_traits > >(values, num_elem) - {} -}; - -template -template -typename value_traits_proxy::value_traits & - value_traits_proxy::get_value_traits(Container &cont) -{ return static_cast(static_cast(cont)); } - -template -template -const typename value_traits_proxy::value_traits & - value_traits_proxy::get_value_traits(const Container &cont) const -{ return static_cast(static_cast(cont)); } - -template -typename bucket_traits_proxy::bucket_traits & - bucket_traits_proxy::get_bucket_traits(Container &cont) -{ return static_cast(static_cast(cont)); } - -template -const typename bucket_traits_proxy::bucket_traits & - bucket_traits_proxy::get_bucket_traits(const Container &cont) const -{ return static_cast(static_cast(cont)); } - -int main() -{ - MyClass values [NumElements]; - //Create several MyClass objects, each one with a different value - for(int i = 0; i < NumElements; ++i) - values[i].int_ = i; - - ContainerHolder cont_holder(values, NumElements); - List &my_list = static_cast (cont_holder); - Slist &my_slist = static_cast (cont_holder); - Rbtree &my_rbtree = static_cast (cont_holder); - Hash &my_hash = static_cast (cont_holder); - - //Now insert them in containers - for(MyClass * it(&values[0]), *itend(&values[NumElements]) - ; it != itend; ++it){ - my_list.push_front(*it); - my_slist.push_front(*it); - my_rbtree.insert_unique(*it); - my_hash.insert_unique(*it); - } - - //Now test containers - { - List::const_iterator list_it (my_list.cbegin()); - Slist::const_iterator slist_it (my_slist.cbegin()); - Rbtree::const_iterator rbtree_it (my_rbtree.cbegin()); - Hash::const_iterator hash_it (my_hash.cbegin()); - MyClass *it_val(&values[NumElements] - 1), *it_rbeg_val(&values[0]-1); - - //Test inserted objects - for(; it_val != it_rbeg_val; --it_val, ++list_it, ++slist_it, ++rbtree_it){ - if(&*list_it != &*it_val) return 1; - if(&*slist_it != &*it_val) return 1; - if(&*rbtree_it != &*it_val) return 1; - hash_it = my_hash.find(*it_val); - if(hash_it == my_hash.cend() || &*hash_it != &*it_val) - return 1; - } - } - - return 0; -} diff --git a/test/generic_assoc_test.hpp b/test/generic_assoc_test.hpp deleted file mode 100644 index 624fd60..0000000 --- a/test/generic_assoc_test.hpp +++ /dev/null @@ -1,328 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include //vector -#include //sort, random_shuffle -#include -#include "common_functors.hpp" -#include -#include -#include "test_macros.hpp" -#include "test_container.hpp" - -namespace boost{ -namespace intrusive{ -namespace test{ - -template -struct has_splay -{ - static const bool value = false; -}; - -template -struct has_rebalance -{ - static const bool value = false; -}; - -template class ContainerDefiner> -struct test_generic_assoc -{ - typedef typename ValueTraits::value_type value_type; - static void test_all(std::vector& values); - static void test_clone(std::vector& values); - static void test_insert_erase_burst(); - static void test_container_from_end(std::vector& values); - static void test_splay_up(std::vector& values); - static void test_splay_up(std::vector& values, boost::intrusive::detail::true_type); - static void test_splay_up(std::vector& values, boost::intrusive::detail::false_type); - static void test_splay_down(std::vector& values); - static void test_splay_down(std::vector& values, boost::intrusive::detail::true_type); - static void test_splay_down(std::vector& values, boost::intrusive::detail::false_type); - static void test_rebalance(std::vector& values); - static void test_rebalance(std::vector& values, boost::intrusive::detail::true_type); - static void test_rebalance(std::vector& values, boost::intrusive::detail::false_type); -}; - -template class ContainerDefiner> -void test_generic_assoc::test_insert_erase_burst() -{ - typedef typename ValueTraits::value_type value_type; - - std::vector values; - const int MaxValues = 100; - for(int i = 0; i != MaxValues; ++i){ - values.push_back(value_type(i)); - } - - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type assoc_type; - typedef typename assoc_type::iterator iterator; - - //First ordered insertions - assoc_type testset (&values[0], &values[0] + values.size()); - TEST_INTRUSIVE_SEQUENCE_EXPECTED(testset, testset.begin()); - - //Ordered erasure - iterator it(testset.begin()), itend(testset.end()); - for(int i = 0; it != itend; ++i){ - BOOST_TEST(&*it == &values[i]); - it = testset.erase(it); - } - - BOOST_TEST(testset.empty()); - - //Now random insertions - std::random_shuffle(values.begin(), values.end()); - testset.insert(&values[0], &values[0] + values.size()); - std::sort(values.begin(), values.end()); - TEST_INTRUSIVE_SEQUENCE_EXPECTED(testset, testset.begin()); - - //Random erasure - std::random_shuffle(values.begin(), values.end()); - for(int i = 0; i != MaxValues; ++i){ - it = testset.erase(testset.iterator_to(values[i])); - } - - BOOST_TEST(testset.empty()); -} - -template class ContainerDefiner> -void test_generic_assoc::test_all(std::vector& values) -{ - test_clone(values); - test_container_from_end(values); - test_splay_up(values); - test_splay_down(values); - test_rebalance(values); - test_insert_erase_burst(); -} - -template class ContainerDefiner> -void test_generic_assoc - ::test_clone(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type assoc_type; - - assoc_type testset1 (&values[0], &values[0] + values.size()); - assoc_type testset2; - - testset2.clone_from(testset1, test::new_cloner(), test::delete_disposer()); - BOOST_TEST (testset2 == testset1); - testset2.clear_and_dispose(test::delete_disposer()); - BOOST_TEST (testset2.empty()); -} - -template class ContainerDefiner> -void test_generic_assoc - ::test_container_from_end(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type assoc_type; - assoc_type testset (&values[0], &values[0] + values.size()); - BOOST_TEST (testset == assoc_type::container_from_end_iterator(testset.end())); - BOOST_TEST (testset == assoc_type::container_from_end_iterator(testset.cend())); -} - -template class ContainerDefiner> -void test_generic_assoc::test_splay_up -(std::vector& values, boost::intrusive::detail::true_type) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type assoc_type; - typedef typename assoc_type::iterator iterator; - typedef std::vector orig_set_t; - std::size_t num_values; - orig_set_t original_testset; - { - assoc_type testset (values.begin(), values.end()); - num_values = testset.size(); - original_testset.insert(original_testset.end(), testset.begin(), testset.end()); - } - - for(std::size_t i = 0; i != num_values; ++i){ - assoc_type testset (values.begin(), values.end()); - { - iterator it = testset.begin(); - for(std::size_t j = 0; j != i; ++j, ++it){} - testset.splay_up(it); - } - BOOST_TEST (testset.size() == num_values); - iterator it = testset.begin(); - for( typename orig_set_t::const_iterator origit = original_testset.begin() - , origitend = original_testset.end() - ; origit != origitend - ; ++origit, ++it){ - BOOST_TEST(*origit == *it); - } - } -} - -template class ContainerDefiner> -void test_generic_assoc::test_splay_up -(std::vector&, boost::intrusive::detail::false_type) -{} - -template class ContainerDefiner> -void test_generic_assoc::test_splay_up -(std::vector& values) -{ - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type assoc_type; - typedef typename detail::remove_const::type Type; - typedef detail::bool_::value> enabler; - test_splay_up(values, enabler()); -} - -template class ContainerDefiner> -void test_generic_assoc::test_splay_down -(std::vector& values, boost::intrusive::detail::true_type) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type assoc_type; - typedef typename assoc_type::iterator iterator; - typedef std::vector orig_set_t; - std::size_t num_values; - orig_set_t original_testset; - { - assoc_type testset (values.begin(), values.end()); - num_values = testset.size(); - original_testset.insert(original_testset.end(), testset.begin(), testset.end()); - } - - for(std::size_t i = 0; i != num_values; ++i){ - assoc_type testset (values.begin(), values.end()); - BOOST_TEST(testset.size() == num_values); - { - iterator it = testset.begin(); - for(std::size_t j = 0; j != i; ++j, ++it){} - BOOST_TEST(*it == *testset.splay_down(*it)); - } - BOOST_TEST (testset.size() == num_values); - iterator it = testset.begin(); - for( typename orig_set_t::const_iterator origit = original_testset.begin() - , origitend = original_testset.end() - ; origit != origitend - ; ++origit, ++it){ - BOOST_TEST(*origit == *it); - } - } -} - -template class ContainerDefiner> -void test_generic_assoc::test_splay_down -(std::vector&, boost::intrusive::detail::false_type) -{} - -template class ContainerDefiner> -void test_generic_assoc::test_splay_down -(std::vector& values) -{ - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type assoc_type; - typedef typename detail::remove_const::type Type; - typedef detail::bool_::value> enabler; - test_splay_down(values, enabler()); -} - -template class ContainerDefiner> -void test_generic_assoc::test_rebalance -(std::vector& values, boost::intrusive::detail::true_type) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type assoc_type; - typedef std::vector orig_set_t; - typedef typename orig_set_t::iterator iterator_t; - std::size_t num_values; - orig_set_t original_testset; - { - assoc_type testset (values.begin(), values.end()); - num_values = testset.size(); - original_testset.insert(original_testset.end(), testset.begin(), testset.end()); - } - { - assoc_type testset(values.begin(), values.end()); - testset.rebalance(); - iterator_t it = original_testset.begin(); - TEST_INTRUSIVE_SEQUENCE_EXPECTED(original_testset, testset.begin()); - } - - { - std::size_t numdata; - { - assoc_type testset(values.begin(), values.end()); - numdata = testset.size(); - } - - for(int i = 0; i != (int)numdata; ++i){ - assoc_type testset(values.begin(), values.end()); - typename assoc_type::iterator it = testset.begin(); - for(int j = 0; j != i; ++j) ++it; - testset.rebalance_subtree(it); - TEST_INTRUSIVE_SEQUENCE_EXPECTED(original_testset, testset.begin()); - } - } -} - -template class ContainerDefiner> -void test_generic_assoc::test_rebalance -(std::vector&, boost::intrusive::detail::false_type) -{} - -template class ContainerDefiner> -void test_generic_assoc::test_rebalance -(std::vector& values) -{ - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type assoc_type; - typedef typename detail::remove_const::type Type; - typedef detail::bool_::value> enabler; - test_rebalance(values, enabler()); -} - -}}} //namespace boost::intrusive::test - -#include diff --git a/test/generic_multiset_test.hpp b/test/generic_multiset_test.hpp deleted file mode 100644 index 0df4e72..0000000 --- a/test/generic_multiset_test.hpp +++ /dev/null @@ -1,227 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include "common_functors.hpp" -#include -#include -#include "test_macros.hpp" -#include "test_container.hpp" -#include "generic_assoc_test.hpp" - -namespace boost{ -namespace intrusive{ -namespace test{ - -template class ContainerDefiner> -struct test_generic_multiset -{ - typedef typename ValueTraits::value_type value_type; - static void test_all (); - static void test_sort(std::vector& values); - static void test_insert(std::vector& values); - static void test_swap(std::vector& values); - static void test_find(std::vector& values); - static void test_impl(); -}; - -template class ContainerDefiner> -void test_generic_multiset::test_all () -{ - typedef typename ValueTraits::value_type value_type; - static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; - std::vector values (6); - for (int i = 0; i < 6; ++i) - values[i].value_ = random_init[i]; - - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type multiset_type; - { - multiset_type testset(values.begin(), values.end()); - test::test_container(testset); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_common_unordered_and_associative_container(testset, values); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_associative_container(testset, values); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_non_unique_container(testset, values); - } - test_sort(values); - test_insert(values); - test_swap(values); - test_find(values); - test_impl(); - test_generic_assoc::test_all(values); -} - -//test case due to an error in tree implementation: -template class ContainerDefiner> -void test_generic_multiset::test_impl() -{ - typedef typename ValueTraits::value_type value_type; - std::vector values (5); - for (int i = 0; i < 5; ++i) - values[i].value_ = i; - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type multiset_type; - - multiset_type testset; - for (int i = 0; i < 5; ++i) - testset.insert (values[i]); - - testset.erase (testset.iterator_to (values[0])); - testset.erase (testset.iterator_to (values[1])); - testset.insert (values[1]); - - testset.erase (testset.iterator_to (values[2])); - testset.erase (testset.iterator_to (values[3])); -} - -//test: constructor, iterator, clear, reverse_iterator, front, back, size: -template class ContainerDefiner> -void test_generic_multiset::test_sort(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type multiset_type; - - multiset_type testset1 (values.begin(), values.end()); - { int init_values [] = { 1, 2, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - testset1.clear(); - BOOST_TEST (testset1.empty()); - - typedef typename ContainerDefiner - - , value_traits - , constant_time_size - >::type multiset_type2; - multiset_type2 testset2 (&values[0], &values[0] + 6); - { int init_values [] = { 5, 3, 1, 4, 2, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset2.rbegin() ); } - - BOOST_TEST (testset2.begin()->value_ == 2); - BOOST_TEST (testset2.rbegin()->value_ == 5); -} - -//test: insert, const_iterator, const_reverse_iterator, erase, iterator_to: -template class ContainerDefiner> -void test_generic_multiset::test_insert(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , size_type - , constant_time_size - >::type multiset_type; - - multiset_type testset; - testset.insert(&values[0] + 2, &values[0] + 5); - { int init_values [] = { 1, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); } - - typename multiset_type::iterator i = testset.begin(); - BOOST_TEST (i->value_ == 1); - - i = testset.insert (i, values[0]); - BOOST_TEST (&*i == &values[0]); - - { int init_values [] = { 5, 4, 3, 1 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset.rbegin() ); } - - i = testset.iterator_to (values[2]); - BOOST_TEST (&*i == &values[2]); - - i = multiset_type::s_iterator_to (values[2]); - BOOST_TEST (&*i == &values[2]); - - testset.erase(i); - - { int init_values [] = { 1, 3, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); } -} - -//test: insert (seq-version), swap, erase (seq-version), size: -template class ContainerDefiner> -void test_generic_multiset::test_swap(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , size_type - , constant_time_size - >::type multiset_type; - multiset_type testset1 (&values[0], &values[0] + 2); - multiset_type testset2; - testset2.insert (&values[0] + 2, &values[0] + 6); - testset1.swap (testset2); - - { int init_values [] = { 1, 2, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - { int init_values [] = { 2, 3 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); } - - testset1.erase (testset1.iterator_to(values[5]), testset1.end()); - BOOST_TEST (testset1.size() == 1); - BOOST_TEST (&*testset1.begin() == &values[3]); -} - -//test: find, equal_range (lower_bound, upper_bound): -template class ContainerDefiner> -void test_generic_multiset::test_find(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , size_type - , constant_time_size - >::type multiset_type; - multiset_type testset (values.begin(), values.end()); - typedef typename multiset_type::iterator iterator; - - value_type cmp_val; - cmp_val.value_ = 2; - iterator i = testset.find (cmp_val); - BOOST_TEST (i->value_ == 2); - BOOST_TEST ((++i)->value_ == 2); - std::pair range = testset.equal_range (cmp_val); - - BOOST_TEST (range.first->value_ == 2); - BOOST_TEST (range.second->value_ == 3); - BOOST_TEST (std::distance (range.first, range.second) == 2); - - cmp_val.value_ = 7; - BOOST_TEST (testset.find (cmp_val) == testset.end()); -} - -}}} //namespace boost::intrusive::test - -#include diff --git a/test/generic_set_test.hpp b/test/generic_set_test.hpp deleted file mode 100644 index db14008..0000000 --- a/test/generic_set_test.hpp +++ /dev/null @@ -1,225 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include "common_functors.hpp" -#include -#include -#include "test_macros.hpp" -#include "test_container.hpp" -#include "generic_assoc_test.hpp" - -namespace boost{ -namespace intrusive{ -namespace test{ - -template class ContainerDefiner> -struct test_generic_set -{ - typedef typename ValueTraits::value_type value_type; - static void test_all(); - static void test_sort(std::vector& values); - static void test_insert(std::vector& values); - static void test_swap(std::vector& values); - static void test_find(std::vector& values); - static void test_impl(); -}; - -template class ContainerDefiner> -void test_generic_set::test_all() -{ - typedef typename ValueTraits::value_type value_type; - static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; - std::vector values (6); - for (int i = 0; i < 6; ++i) - values[i].value_ = random_init[i]; - - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type set_type; - { - set_type testset(values.begin(), values.end()); - test::test_container(testset); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_common_unordered_and_associative_container(testset, values); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_associative_container(testset, values); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_unique_container(testset, values); - } - test_sort(values); - test_insert(values); - test_swap(values); - test_find(values); - test_impl(); - test_generic_assoc::test_all(values); -} - -//test case due to an error in tree implementation: -template class ContainerDefiner> -void test_generic_set::test_impl() -{ - typedef typename ValueTraits::value_type value_type; - std::vector values (5); - for (int i = 0; i < 5; ++i) - values[i].value_ = i; - - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type set_type; - set_type testset; - for (int i = 0; i < 5; ++i) - testset.insert (values[i]); - - testset.erase (testset.iterator_to (values[0])); - testset.erase (testset.iterator_to (values[1])); - testset.insert (values[1]); - - testset.erase (testset.iterator_to (values[2])); - testset.erase (testset.iterator_to (values[3])); -} - -//test: constructor, iterator, clear, reverse_iterator, front, back, size: -template class ContainerDefiner> -void test_generic_set::test_sort(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type set_type; - set_type testset1 (values.begin(), values.end()); - { int init_values [] = { 1, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - testset1.clear(); - BOOST_TEST (testset1.empty()); - - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , compare - , value_traits - , constant_time_size - >::type set_type2; - set_type2 testset2 (&values[0], &values[0] + 6); - { int init_values [] = { 5, 3, 1, 4, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset2.rbegin() ); } - BOOST_TEST (testset2.begin()->value_ == 2); - BOOST_TEST (testset2.rbegin()->value_ == 5); -} - -//test: insert, const_iterator, const_reverse_iterator, erase, s_iterator_to: -template class ContainerDefiner> -void test_generic_set::test_insert(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type set_type; - set_type testset; - testset.insert(&values[0] + 2, &values[0] + 5); - - const set_type& const_testset = testset; - { int init_values [] = { 1, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testset.begin() ); } - - typename set_type::iterator i = testset.begin(); - BOOST_TEST (i->value_ == 1); - - i = testset.insert (i, values[0]); - BOOST_TEST (&*i == &values[0]); - - { int init_values [] = { 5, 4, 3, 1 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset.rbegin() ); } - - i = testset.iterator_to (values[2]); - BOOST_TEST (&*i == &values[2]); - - i = set_type::s_iterator_to(values[2]); - BOOST_TEST (&*i == &values[2]); - - testset.erase (i); - { int init_values [] = { 1, 3, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); } -} - -//test: insert (seq-version), swap, erase (seq-version), size: -template class ContainerDefiner> -void test_generic_set::test_swap(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type set_type; - set_type testset1 (&values[0], &values[0] + 2); - set_type testset2; - testset2.insert (&values[0] + 2, &values[0] + 6); - testset1.swap (testset2); - - { int init_values [] = { 1, 2, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - { int init_values [] = { 2, 3 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); } - - testset1.erase (testset1.iterator_to(values[5]), testset1.end()); - BOOST_TEST (testset1.size() == 1); - // BOOST_TEST (&testset1.front() == &values[3]); - BOOST_TEST (&*testset1.begin() == &values[3]); -} - -//test: find, equal_range (lower_bound, upper_bound): -template class ContainerDefiner> -void test_generic_set::test_find(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef typename ContainerDefiner - < value_type - , value_traits - , constant_time_size - >::type set_type; - set_type testset (values.begin(), values.end()); - typedef typename set_type::iterator iterator; - - value_type cmp_val; - cmp_val.value_ = 2; - iterator i = testset.find (cmp_val); - BOOST_TEST (i->value_ == 2); - BOOST_TEST ((++i)->value_ != 2); - std::pair range = testset.equal_range (cmp_val); - - BOOST_TEST (range.first->value_ == 2); - BOOST_TEST (range.second->value_ == 3); - BOOST_TEST (std::distance (range.first, range.second) == 1); - - cmp_val.value_ = 7; - BOOST_TEST (testset.find (cmp_val) == testset.end()); -} - -}}} //namespace boost::intrusive::test - -#include diff --git a/test/itestvalue.hpp b/test/itestvalue.hpp deleted file mode 100644 index 6878158..0000000 --- a/test/itestvalue.hpp +++ /dev/null @@ -1,372 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#ifndef BOOST_INTRUSIVE_DETAIL_ITESTVALUE_HPP -#define BOOST_INTRUSIVE_DETAIL_ITESTVALUE_HPP - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "smart_ptr.hpp" - -namespace boost{ -namespace intrusive{ - -struct my_tag; - -template -struct set_base_hook_type -{ typedef set_base_hook > type; }; - -template -struct set_auto_base_hook_type -{ typedef set_base_hook, void_pointer, tag, optimize_size > type; }; - -template -struct set_member_hook_type -{ typedef set_member_hook, optimize_size > type; }; - -template -struct set_auto_member_hook_type -{ typedef set_member_hook, void_pointer > type; }; - -template -struct splay_set_base_hook_type -{ typedef splay_set_base_hook > type; }; - -template -struct splay_set_auto_base_hook_type -{ typedef splay_set_base_hook, void_pointer, tag > type; }; - -template -struct splay_set_member_hook_type -{ typedef splay_set_member_hook > type; }; - -template -struct splay_set_auto_member_hook_type -{ typedef splay_set_member_hook, void_pointer > type; }; - -template -struct bs_set_base_hook_type -{ typedef bs_set_base_hook > type; }; - -template -struct bs_set_member_hook_type -{ typedef bs_set_member_hook > type; }; - -template -struct avl_set_base_hook_type -{ typedef avl_set_base_hook > type; }; - -template -struct avl_set_auto_base_hook_type -{ typedef avl_set_base_hook, void_pointer, tag, optimize_size > type; }; - -template -struct avl_set_member_hook_type -{ typedef avl_set_member_hook, optimize_size > type; }; - -template -struct avl_set_auto_member_hook_type -{ typedef avl_set_member_hook, void_pointer > type; }; - -template -struct list_base_hook_type -{ typedef list_base_hook > type; }; - -template -struct list_auto_base_hook_type -{ typedef list_base_hook, void_pointer, tag > type; }; - -template -struct list_member_hook_type -{ typedef list_member_hook > type; }; - -template -struct list_auto_member_hook_type -{ typedef list_member_hook, void_pointer > type; }; - -template -struct slist_base_hook_type -{ typedef slist_base_hook > type; }; - -template -struct slist_auto_base_hook_type -{ typedef slist_base_hook, void_pointer, tag > type; }; - -template -struct slist_member_hook_type -{ typedef slist_member_hook > type; }; - -template -struct slist_auto_member_hook_type -{ typedef slist_member_hook, void_pointer > type; }; - -template -struct uset_base_hook_type -{ typedef unordered_set_base_hook > type; }; - -template -struct uset_auto_base_hook_type -{ - typedef unordered_set_base_hook - < link_mode, void_pointer - , tag, store_hash > type; -}; - -template -struct uset_member_hook_type -{ typedef unordered_set_member_hook > type; }; - -template -struct uset_auto_member_hook_type -{ - typedef unordered_set_member_hook - < link_mode, void_pointer - , store_hash > type; -}; - -template -struct testvalue - : set_base_hook_type::type - , set_auto_base_hook_type::type - , splay_set_base_hook_type::type - , splay_set_auto_base_hook_type::type - , bs_set_base_hook_type::type - , avl_set_base_hook_type::type - , avl_set_auto_base_hook_type::type - , list_base_hook_type::type - , list_auto_base_hook_type::type - , slist_base_hook_type::type - , slist_auto_base_hook_type::type - , uset_base_hook_type::type - , uset_auto_base_hook_type::type -{ - typedef typename set_auto_base_hook_type::type set_auto_base_hook_t; - typedef typename set_base_hook_type::type set_base_hook_t; - typedef typename set_auto_member_hook_type::type set_auto_member_hook_t; - typedef typename set_member_hook_type::type set_member_hook_t; - - typedef typename splay_set_auto_base_hook_type::type splay_set_auto_base_hook_t; - typedef typename splay_set_base_hook_type::type splay_set_base_hook_t; - typedef typename splay_set_auto_member_hook_type::type splay_set_auto_member_hook_t; - typedef typename splay_set_member_hook_type::type splay_set_member_hook_t; - - typedef typename bs_set_base_hook_type::type bs_set_base_hook_t; - typedef typename bs_set_member_hook_type::type bs_set_member_hook_t; - - typedef typename avl_set_auto_base_hook_type::type avl_set_auto_base_hook_t; - typedef typename avl_set_base_hook_type::type avl_set_base_hook_t; - typedef typename avl_set_auto_member_hook_type::type avl_set_auto_member_hook_t; - typedef typename avl_set_member_hook_type::type avl_set_member_hook_t; - - typedef typename uset_auto_base_hook_type::type unordered_set_auto_base_hook_t; - typedef typename uset_base_hook_type::type unordered_set_base_hook_t; - typedef typename uset_auto_member_hook_type::type unordered_set_auto_member_hook_t; - typedef typename uset_member_hook_type::type unordered_set_member_hook_t; - - typedef typename list_auto_base_hook_type::type list_auto_base_hook_t; - typedef typename list_base_hook_type::type list_base_hook_t; - typedef typename list_auto_member_hook_type::type list_auto_member_hook_t; - typedef typename list_member_hook_type::type list_member_hook_t; - - typedef typename slist_auto_base_hook_type::type slist_auto_base_hook_t; - typedef typename slist_base_hook_type::type slist_base_hook_t; - typedef typename slist_auto_member_hook_type::type slist_auto_member_hook_t; - typedef typename slist_member_hook_type::type slist_member_hook_t; - - //Set members - set_member_hook_t set_node_; - set_auto_member_hook_t set_auto_node_; - - //SplaySet members - splay_set_member_hook_t splay_set_node_; - splay_set_auto_member_hook_t splay_set_auto_node_; - - //ScapegoatSet members - bs_set_member_hook_t sg_set_node_; - - //AvlSet members - avl_set_member_hook_t avl_set_node_; - avl_set_auto_member_hook_t avl_set_auto_node_; - - //Unordered set members - unordered_set_member_hook_t unordered_set_node_; - unordered_set_auto_member_hook_t unordered_set_auto_node_; - - //List members - list_member_hook_t list_node_; - list_auto_member_hook_t list_auto_node_; - - //Slist members - slist_member_hook_t slist_node_; - slist_auto_member_hook_t slist_auto_node_; - - int value_; - - static const bool constant_time_size = ConstantTimeSize; - - testvalue() - {} - - testvalue(int i) - : value_(i) - {} - - testvalue (const testvalue& src) - : value_ (src.value_) - {} - - // testvalue is used in std::vector and thus prev and next - // have to be handled appropriately when copied: - - testvalue & operator= (const testvalue& src) - { - set_base_hook_t::operator=(src); - set_auto_base_hook_t::operator=(src); - this->set_node_ = src.set_node_; - this->set_auto_node_ = src.set_auto_node_; - - splay_set_base_hook_t::operator=(src); - splay_set_auto_base_hook_t::operator=(src); - this->splay_set_node_ = src.splay_set_node_; - this->splay_set_auto_node_ = src.splay_set_auto_node_; - - bs_set_base_hook_t::operator=(src); - this->sg_set_node_ = src.sg_set_node_; - - avl_set_base_hook_t::operator=(src); - avl_set_auto_base_hook_t::operator=(src); - this->avl_set_node_ = src.avl_set_node_; - this->avl_set_auto_node_ = src.avl_set_auto_node_; - - unordered_set_base_hook_t::operator=(src); - unordered_set_auto_base_hook_t::operator=(src); - this->unordered_set_node_ = src.unordered_set_node_; - this->unordered_set_auto_node_ = src.unordered_set_auto_node_; - - list_base_hook_t::operator=(src); - list_auto_base_hook_t::operator=(src); - this->list_node_ = src.list_node_; - this->list_auto_node_ = src.list_auto_node_; - - slist_base_hook_t::operator=(src); - slist_auto_base_hook_t::operator=(src); - this->slist_node_ = src.slist_node_; - this->slist_auto_node_ = src.slist_auto_node_; - - value_ = src.value_; - return *this; - } - - void swap_nodes(testvalue &other) - { - //Set - set_base_hook_t::swap_nodes(other); - set_auto_base_hook_t::swap_nodes(other); - set_node_.swap_nodes(other.set_node_); - set_auto_node_.swap_nodes(other.set_auto_node_); - - //SplaySet - splay_set_base_hook_t::swap_nodes(other); - splay_set_auto_base_hook_t::swap_nodes(other); - splay_set_node_.swap_nodes(other.splay_set_node_); - splay_set_auto_node_.swap_nodes(other.splay_set_auto_node_); - - //ScapeoatSet - bs_set_base_hook_t::swap_nodes(other); - sg_set_node_.swap_nodes(other.sg_set_node_); - - //AvlSet - avl_set_base_hook_t::swap_nodes(other); - avl_set_auto_base_hook_t::swap_nodes(other); - avl_set_node_.swap_nodes(other.avl_set_node_); - avl_set_auto_node_.swap_nodes(other.avl_set_auto_node_); - - //Unordered set - unordered_set_base_hook_t::swap_nodes(other); - unordered_set_auto_base_hook_t::swap_nodes(other); - unordered_set_node_.swap_nodes(other.unordered_set_node_); - unordered_set_auto_node_.swap_nodes(other.unordered_set_auto_node_); - - //List - list_base_hook_t::swap_nodes(other); - list_auto_base_hook_t::swap_nodes(other); - list_node_.swap_nodes(other.list_node_); - list_auto_node_.swap_nodes(other.list_auto_node_); - - //Slist - slist_base_hook_t::swap_nodes(other); - slist_auto_base_hook_t::swap_nodes(other); - slist_node_.swap_nodes(other.slist_node_); - slist_auto_node_.swap_nodes(other.slist_auto_node_); - } - - ~testvalue() - {} - - bool operator< (const testvalue &other) const - { return value_ < other.value_; } - - bool operator==(const testvalue &other) const - { return value_ == other.value_; } - - friend bool operator< (int other1, const testvalue &other2) - { return other1 < other2.value_; } - - friend bool operator< (const testvalue &other1, int other2) - { return other1.value_ < other2; } - - friend bool operator== (int other1, const testvalue &other2) - { return other1 == other2.value_; } - - friend bool operator== (const testvalue &other1, int other2) - { return other1.value_ == other2; } -}; - -template -std::size_t hash_value(const testvalue &t) -{ - boost::hash hasher; - return hasher(t.value_); -} - -template -std::ostream& operator<< - (std::ostream& s, const testvalue& t) -{ return s << t.value_; } - -struct even_odd -{ - template - bool operator() - (const testvalue& v1 - ,const testvalue& v2) const - { - if ((v1.value_ & 1) == (v2.value_ & 1)) - return v1.value_ < v2.value_; - else - return v2.value_ & 1; - } -}; - -} //namespace boost{ -} //namespace intrusive{ - -#endif diff --git a/test/list_test.cpp b/test/list_test.cpp deleted file mode 100644 index 7b28289..0000000 --- a/test/list_test.cpp +++ /dev/null @@ -1,414 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// - -#include -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "common_functors.hpp" -#include -#include -#include "test_macros.hpp" -#include "test_container.hpp" - -using namespace boost::intrusive; - -template -struct test_list -{ - typedef typename ValueTraits::value_type value_type; - static void test_all(std::vector& values); - static void test_front_back(std::vector& values); - static void test_sort(std::vector& values); - static void test_insert(std::vector& values); - static void test_shift(std::vector& values); - static void test_swap(std::vector& values); - static void test_clone(std::vector& values); - static void test_container_from_end(std::vector& values); -}; - -template -void test_list::test_all(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef list - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - { - list_type list(values.begin(), values.end()); - test::test_container(list); - list.clear(); - list.insert(list.end(), values.begin(), values.end()); - test::test_sequence_container(list, values); - } - - test_front_back(values); - test_sort(values); - test_insert(values); - test_shift(values); - test_swap(values); - test_clone(values); - test_container_from_end(values); -/* - const char *list_name = typeid(list_type).name(); - std::cout << list_name << std::endl << strlen(list_name) << std::endl; - const char *value_t = typeid(typename list_type::value_traits).name(); - std::cout << value_t << std::endl << strlen(value_t) << std::endl; - const char *list_it_name = typeid(typename list_type::iterator).name(); - std::cout << list_it_name << std::endl << strlen(list_it_name ) << std::endl; -*/ -} - -//test: push_front, pop_front, push_back, pop_back, front, back, size, empty: -template -void test_list - ::test_front_back(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef list - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist; - BOOST_TEST (testlist.empty()); - - testlist.push_back (values[0]); - BOOST_TEST (testlist.size() == 1); - BOOST_TEST (&testlist.front() == &values[0]); - BOOST_TEST (&testlist.back() == &values[0]); - - testlist.push_front (values[1]); - BOOST_TEST (testlist.size() == 2); - BOOST_TEST (&testlist.front() == &values[1]); - BOOST_TEST (&testlist.back() == &values[0]); - - testlist.pop_back(); - BOOST_TEST (testlist.size() == 1); - const list_type &const_testlist = testlist; - BOOST_TEST (&const_testlist.front() == &values[1]); - BOOST_TEST (&const_testlist.back() == &values[1]); - - testlist.pop_front(); - BOOST_TEST (testlist.empty()); -} - - -//test: constructor, iterator, reverse_iterator, sort, reverse: -template -void test_list - ::test_sort(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef list - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist(values.begin(), values.end()); - - { int init_values [] = { 1, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); } - - testlist.sort (even_odd()); - { int init_values [] = { 5, 3, 1, 4, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist.rbegin() ); } - - testlist.reverse(); - { int init_values [] = { 5, 3, 1, 4, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); } -} - -//test: assign, insert, const_iterator, const_reverse_iterator, erase, s_iterator_to: -template -void test_list - ::test_insert(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef list - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist; - testlist.assign (&values[0] + 2, &values[0] + 5); - - const list_type& const_testlist = testlist; - { int init_values [] = { 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); } - - typename list_type::iterator i = ++testlist.begin(); - BOOST_TEST (i->value_ == 4); - - { - typename list_type::const_iterator ci = typename list_type::iterator(); - //typename list_type::iterator i = typename list_type::const_iterator(); - } - - testlist.insert (i, values[0]); - { int init_values [] = { 5, 4, 1, 3 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.rbegin() ); } - - i = testlist.iterator_to (values[4]); - BOOST_TEST (&*i == &values[4]); - - i = list_type::s_iterator_to (values[4]); - BOOST_TEST (&*i == &values[4]); - - i = testlist.erase (i); - BOOST_TEST (i == testlist.end()); - - { int init_values [] = { 3, 1, 4 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); } -} - -template -void test_list - ::test_shift(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef list - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist; - const int num_values = (int)values.size(); - std::vector expected_values(num_values); - - //Shift forward all possible positions 3 times - for(int i = 0; i < num_values*3; ++i){ - testlist.assign(values.begin(), values.end()); - testlist.shift_forward(i); - for(int j = 0; j < num_values; ++j){ - expected_values[(j + num_values - i%num_values) % num_values] = (j + 1); - } - TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin()); - testlist.clear(); - } - - //Shift backwards all possible positions - for(int i = 0; i < num_values*3; ++i){ - testlist.assign(values.begin(), values.end()); - testlist.shift_backwards(i); - for(int j = 0; j < num_values; ++j){ - expected_values[(j + i) % num_values] = (j + 1); - } - TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin()); - testlist.clear(); - } -} - -//test: insert (seq-version), swap, splice, erase (seq-version): -template -void test_list - ::test_swap(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef list - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - { - list_type testlist1 (&values[0], &values[0] + 2); - list_type testlist2; - testlist2.insert (testlist2.end(), &values[0] + 2, &values[0] + 5); - testlist1.swap (testlist2); - - { int init_values [] = { 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } - { int init_values [] = { 1, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } - - testlist2.splice (++testlist2.begin(), testlist1); - { int init_values [] = { 1, 3, 4, 5, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } - - BOOST_TEST (testlist1.empty()); - - testlist1.splice (testlist1.end(), testlist2, ++(++testlist2.begin())); - { int init_values [] = { 4 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } - - { int init_values [] = { 1, 3, 5, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } - - testlist1.splice (testlist1.end(), testlist2, - testlist2.begin(), ----testlist2.end()); - { int init_values [] = { 4, 1, 3 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } - { int init_values [] = { 5, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } - - testlist1.erase (testlist1.iterator_to(values[0]), testlist1.end()); - BOOST_TEST (testlist1.size() == 1); - BOOST_TEST (&testlist1.front() == &values[3]); - } - { - list_type testlist1 (&values[0], &values[0] + 2); - list_type testlist2 (&values[0] + 3, &values[0] + 5); - - values[0].swap_nodes(values[2]); - { int init_values [] = { 3, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } - - values[2].swap_nodes(values[4]); - { int init_values [] = { 5, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } - { int init_values [] = { 4, 3 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } - } -} - -template -void test_list - ::test_container_from_end(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef list - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist1 (&values[0], &values[0] + values.size()); - BOOST_TEST (testlist1 == list_type::container_from_end_iterator(testlist1.end())); - BOOST_TEST (testlist1 == list_type::container_from_end_iterator(testlist1.cend())); -} - - -template -void test_list - ::test_clone(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef list - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist1 (&values[0], &values[0] + values.size()); - list_type testlist2; - - testlist2.clone_from(testlist1, test::new_cloner(), test::delete_disposer()); - BOOST_TEST (testlist2 == testlist1); - testlist2.clear_and_dispose(test::delete_disposer()); - BOOST_TEST (testlist2.empty()); -} - -template -class test_main_template -{ - public: - int operator()() - { - typedef testvalue value_type; - std::vector data (5); - for (int i = 0; i < 5; ++i) - data[i].value_ = i + 1; - - test_list < typename detail::get_base_value_traits - < value_type - , typename value_type::list_base_hook_t - >::type - >::test_all(data); - test_list < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::list_member_hook_t - , &value_type::list_node_ - > - >::type - >::test_all(data); - return 0; - } -}; - -template -class test_main_template -{ - public: - int operator()() - { - typedef testvalue value_type; - std::vector data (5); - for (int i = 0; i < 5; ++i) - data[i].value_ = i + 1; - - test_list < typename detail::get_base_value_traits - < value_type - , typename value_type::list_base_hook_t - >::type - >::test_all(data); - - test_list < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::list_member_hook_t - , &value_type::list_node_ - > - >::type - >::test_all(data); -/* - test_list - , safe_link> - >::test_all(data); -*/ - test_list < typename detail::get_base_value_traits - < value_type - , typename value_type::list_auto_base_hook_t - >::type - >::test_all(data); - - test_list < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::list_auto_member_hook_t - , &value_type::list_auto_node_ - > - >::type - >::test_all(data); -/* - test_list - , auto_unlink> - >::test_all(data); -*/ - return 0; - } -}; - -int main( int, char* [] ) -{ - test_main_template()(); - test_main_template, false>()(); - test_main_template()(); - test_main_template, true>()(); - - return boost::report_errors(); -} -#include diff --git a/test/make_functions_test.cpp b/test/make_functions_test.cpp deleted file mode 100644 index bf40afb..0000000 --- a/test/make_functions_test.cpp +++ /dev/null @@ -1,147 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include -#include -#include -#include "smart_ptr.hpp" -#include - -using namespace boost::intrusive; - -class MyClass -: public make_list_base_hook - < void_pointer >, link_mode >::type -, public make_slist_base_hook - < void_pointer >, link_mode >::type -, public make_set_base_hook - < void_pointer >, link_mode >::type -, public make_unordered_set_base_hook - < void_pointer >, link_mode >::type -{ - int int_; - - public: - MyClass(int i) - : int_(i) - {} - - friend bool operator<(const MyClass &l, const MyClass &r) - { return l.int_ < r.int_; } - - friend bool operator==(const MyClass &l, const MyClass &r) - { return l.int_ == r.int_; } - - friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } -}; - -//Define a list that will store MyClass using the public base hook -typedef make_list::type List; -typedef make_slist::type Slist; -typedef make_set::type Set; -typedef make_unordered_set::type USet; - -int main() -{ - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create several MyClass objects, each one with a different value - std::vector values; - for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); - - USet::bucket_type buckets[100]; - - List my_list; - Slist my_slist; - Set my_set; - USet my_uset(USet::bucket_traits(buckets, 100)); - - //Now insert them in containers - for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){ - my_list.push_front(*it); - my_slist.push_front(*it); - my_set.insert(*it); - my_uset.insert(*it); - } - - //Now test lists - { - List::const_iterator list_it(my_list.cbegin()); - Slist::const_iterator slist_it(my_slist.cbegin()); - Set::const_reverse_iterator set_rit(my_set.crbegin()); - VectRit vect_it(values.rbegin()), vect_itend(values.rend()); - - //Test the objects inserted in the base hook list - for(; vect_it != vect_itend; ++vect_it, ++list_it, ++slist_it, ++set_rit){ - if(&*list_it != &*vect_it) return 1; - if(&*slist_it != &*vect_it) return 1; - if(&*set_rit != &*vect_it) return 1; - if(my_uset.find(*set_rit) == my_uset.cend()) return 1; - } - } - - //Check defined types and implicitly defined types are equal - if(detail::is_same, link_mode >::type - ,make_list_base_hook<>::type - >::value == false){ - return 1; - } - - if(detail::is_same, link_mode >::type - ,make_slist_base_hook<>::type - >::value == false){ - return 1; - } - - if(detail::is_same, link_mode >::type - ,make_set_base_hook<>::type - >::value == false){ - return 1; - } - - if(detail::is_same, link_mode >::type - ,make_unordered_set_base_hook<>::type - >::value == false){ - return 1; - } - - //Check defined types and implicitly defined types are unequal - if(detail::is_same, link_mode >::type - ,make_list_base_hook<>::type - >::value == true){ - return 1; - } - - if(detail::is_same, link_mode >::type - ,make_slist_base_hook<>::type - >::value == true){ - return 1; - } - - if(detail::is_same, link_mode >::type - ,make_set_base_hook<>::type - >::value == true){ - return 1; - } - - if(detail::is_same, link_mode >::type - ,make_unordered_set_base_hook<>::type - >::value == true){ - return 1; - } - - - return 0; -} diff --git a/test/multiset_test.cpp b/test/multiset_test.cpp deleted file mode 100644 index 180ee62..0000000 --- a/test/multiset_test.cpp +++ /dev/null @@ -1,117 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "generic_multiset_test.hpp" - -template< class ValueType - , class Option1 = boost::intrusive::none - , class Option2 = boost::intrusive::none - , class Option3 = boost::intrusive::none - > -struct GetContainer -{ - typedef boost::intrusive::multiset - < ValueType - , Option1 - , Option2 - , Option3 - > type; -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::set_base_hook_t - >::type - , GetContainer - >::test_all(); - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::set_member_hook_t - , &value_type::set_node_ - > - >::type - , GetContainer - >::test_all(); - return 0; - } -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::set_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::set_member_hook_t - , &value_type::set_node_ - > - >::type - , GetContainer - >::test_all(); - - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::set_auto_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::set_auto_member_hook_t - , &value_type::set_auto_node_ - > - >::type - , GetContainer - >::test_all(); - return 0; - } -}; - -int main( int, char* [] ) -{ - test_main_template()(); - test_main_template, false>()(); - test_main_template()(); - test_main_template, true>()(); - return boost::report_errors(); -} - -#include diff --git a/test/set_test.cpp b/test/set_test.cpp deleted file mode 100644 index a6b214e..0000000 --- a/test/set_test.cpp +++ /dev/null @@ -1,118 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "generic_set_test.hpp" - -template< class ValueType - , class Option1 = boost::intrusive::none - , class Option2 = boost::intrusive::none - , class Option3 = boost::intrusive::none - > -struct GetContainer -{ - typedef boost::intrusive::set - < ValueType - , Option1 - , Option2 - , Option3 - > type; -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::set_base_hook_t - >::type - , GetContainer - >::test_all(); - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::set_member_hook_t - , &value_type::set_node_ - > - >::type - , GetContainer - >::test_all(); - return 0; - } -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::set_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::set_member_hook_t - , &value_type::set_node_ - > - >::type - , GetContainer - >::test_all(); - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::set_auto_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::set_auto_member_hook_t - , &value_type::set_auto_node_ - > - >::type - , GetContainer - >::test_all(); - - return 0; - } -}; - -int main( int, char* [] ) -{ - - test_main_template()(); - test_main_template, false>()(); - test_main_template()(); - test_main_template, true>()(); - return boost::report_errors(); -} -#include diff --git a/test/sg_multiset_test.cpp b/test/sg_multiset_test.cpp deleted file mode 100644 index 48f15f4..0000000 --- a/test/sg_multiset_test.cpp +++ /dev/null @@ -1,108 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "generic_multiset_test.hpp" - -namespace boost { namespace intrusive { namespace test { - -template -struct has_rebalance > -{ - static const bool value = true; -}; - -}}} - -template< class ValueType - , class Option1 = boost::intrusive::none - , class Option2 = boost::intrusive::none - , class Option3 = boost::intrusive::none - > -struct GetContainer -{ - typedef boost::intrusive::sg_multiset - < ValueType - , Option1 - , Option2 - , Option3 - > type; -}; - -template< class ValueType - , class Option1 = boost::intrusive::none - , class Option2 = boost::intrusive::none - , class Option3 = boost::intrusive::none - > -struct GetContainerFixedAlpha -{ - typedef boost::intrusive::sg_multiset - < ValueType - , Option1 - , Option2 - , Option3 - , boost::intrusive::floating_point - > type; -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::bs_set_base_hook_t - >::type - , GetContainer - >::test_all(); - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::bs_set_member_hook_t - , &value_type::sg_set_node_ - > - >::type - , GetContainer - >::test_all(); - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::bs_set_base_hook_t - >::type - , GetContainerFixedAlpha - >::test_all(); - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::bs_set_member_hook_t - , &value_type::sg_set_node_ - > - >::type - , GetContainerFixedAlpha - >::test_all(); - return 0; - } -}; - -int main( int, char* [] ) -{ - test_main_template()(); - test_main_template >()(); - return boost::report_errors(); -} diff --git a/test/sg_set_test.cpp b/test/sg_set_test.cpp deleted file mode 100644 index 9d85e32..0000000 --- a/test/sg_set_test.cpp +++ /dev/null @@ -1,109 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "generic_set_test.hpp" - -namespace boost { namespace intrusive { namespace test { - -template -struct has_rebalance > -{ - static const bool value = true; -}; - -}}} - -template< class ValueType - , class Option1 = boost::intrusive::none - , class Option2 = boost::intrusive::none - , class Option3 = boost::intrusive::none - > -struct GetContainer -{ - typedef boost::intrusive::sg_set - < ValueType - , Option1 - , Option2 - , Option3 - > type; -}; - -template< class ValueType - , class Option1 = boost::intrusive::none - , class Option2 = boost::intrusive::none - , class Option3 = boost::intrusive::none - > -struct GetContainerFixedAlpha -{ - typedef boost::intrusive::sg_set - < ValueType - , Option1 - , Option2 - , Option3 - , boost::intrusive::floating_point - > type; -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::bs_set_base_hook_t - >::type - , GetContainer - >::test_all(); - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::bs_set_member_hook_t - , &value_type::sg_set_node_ - > - >::type - , GetContainer - >::test_all(); - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::bs_set_base_hook_t - >::type - , GetContainerFixedAlpha - >::test_all(); - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::bs_set_member_hook_t - , &value_type::sg_set_node_ - > - >::type - , GetContainerFixedAlpha - >::test_all(); - return 0; - } -}; - -int main( int, char* [] ) -{ - test_main_template()(); - test_main_template >()(); - return boost::report_errors(); -} -#include diff --git a/test/slist_test.cpp b/test/slist_test.cpp deleted file mode 100644 index 06fc38c..0000000 --- a/test/slist_test.cpp +++ /dev/null @@ -1,449 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// - -#include -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "common_functors.hpp" -#include -#include -#include "test_macros.hpp" -#include "test_container.hpp" - -using namespace boost::intrusive; - -template -struct test_slist -{ - typedef typename ValueTraits::value_type value_type; - static void test_all (std::vector& values); - static void test_front_back (std::vector& values); - static void test_sort(std::vector& values); - static void test_merge (std::vector& values); - static void test_insert(std::vector& values); - static void test_shift(std::vector& values); - static void test_swap(std::vector& values); - static void test_slow_insert (std::vector& values); - static void test_clone (std::vector& values); - static void test_container_from_end(std::vector &values); -}; - -template -void test_slist - ::test_all (std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef slist - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - { - list_type list(values.begin(), values.end()); - test::test_container(list); - list.clear(); - list.insert(list.end(), values.begin(), values.end()); - test::test_sequence_container(list, values); - } - test_front_back (values); - test_sort(values); - test_merge (values); - test_insert(values); - test_shift(values); - test_slow_insert (values); - test_swap(values); - test_clone(values); - test_container_from_end(values); -} - -//test: push_front, pop_front, front, size, empty: -template -void test_slist - ::test_front_back (std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef slist - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist; - BOOST_TEST (testlist.empty()); - - testlist.push_front (values[0]); - BOOST_TEST (testlist.size() == 1); - BOOST_TEST (&testlist.front() == &values[0]); - - testlist.push_front (values[1]); - BOOST_TEST (testlist.size() == 2); - BOOST_TEST (&testlist.front() == &values[1]); - - testlist.pop_front(); - BOOST_TEST (testlist.size() == 1); - BOOST_TEST (&testlist.front() == &values[0]); - - testlist.pop_front(); - BOOST_TEST (testlist.empty()); -} - -//test: merge due to error in merge implementation: -template -void test_slist - ::test_merge (std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef slist - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist1, testlist2; - testlist1.push_front (values[0]); - testlist2.push_front (values[4]); - testlist2.push_front (values[3]); - testlist2.push_front (values[2]); - testlist1.merge (testlist2); - - int init_values [] = { 1, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); -} - -//test: constructor, iterator, sort, reverse: -template -void test_slist - ::test_sort(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef slist - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist (values.begin(), values.end()); - - { int init_values [] = { 1, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); } - - testlist.sort (even_odd()); - { int init_values [] = { 2, 4, 1, 3, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); } - - testlist.reverse(); - { int init_values [] = { 5, 3, 1, 4, 2, }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); } -} - -//test: assign, insert_after, const_iterator, erase_after, s_iterator_to, previous: -template -void test_slist - ::test_insert(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef slist - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist; - testlist.assign (&values[0] + 2, &values[0] + 5); - - const list_type& const_testlist = testlist; - { int init_values [] = { 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); } - - typename list_type::iterator i = ++testlist.begin(); - BOOST_TEST (i->value_ == 4); - - testlist.insert_after (i, values[0]); - { int init_values [] = { 3, 4, 1, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); } - - i = testlist.iterator_to (values[4]); - BOOST_TEST (&*i == &values[4]); - i = list_type::s_iterator_to (values[4]); - BOOST_TEST (&*i == &values[4]); - i = testlist.previous (i); - BOOST_TEST (&*i == &values[0]); - - testlist.erase_after (i); - BOOST_TEST (&*i == &values[0]); - { int init_values [] = { 3, 4, 1 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); } -} - -//test: insert, const_iterator, erase, siterator_to: -template -void test_slist - ::test_slow_insert (std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef slist - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist; - testlist.push_front (values[4]); - testlist.insert (testlist.begin(), &values[0] + 2, &values[0] + 4); - - const list_type& const_testlist = testlist; - { int init_values [] = { 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); } - - typename list_type::iterator i = ++testlist.begin(); - BOOST_TEST (i->value_ == 4); - - testlist.insert (i, values[0]); - { int init_values [] = { 3, 1, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); } - - i = testlist.iterator_to (values[4]); - BOOST_TEST (&*i == &values[4]); - - i = list_type::s_iterator_to (values[4]); - BOOST_TEST (&*i == &values[4]); - - i = testlist.erase (i); - BOOST_TEST (i == testlist.end()); - - { int init_values [] = { 3, 1, 4 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); } - - testlist.erase (++testlist.begin(), testlist.end()); - BOOST_TEST (testlist.size() == 1); - BOOST_TEST (testlist.front().value_ == 3); -} - -template -void test_slist - ::test_shift(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef slist - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist; - - const int num_values = (int)values.size(); - std::vector expected_values(num_values); - - //Shift forward all possible positions 3 times - for(int i = 0; i < num_values*3; ++i){ - testlist.assign(values.begin(), values.end()); - testlist.shift_forward(i); - for(int j = 0; j < num_values; ++j){ - expected_values[(j + num_values - i%num_values) % num_values] = (j + 1); - } - - TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin()) - testlist.clear(); - } - - //Shift backwards all possible positions - for(int i = 0; i < num_values*3; ++i){ - testlist.assign(values.begin(), values.end()); - testlist.shift_backwards(i); - for(int j = 0; j < num_values; ++j){ - expected_values[(j + i) % num_values] = (j + 1); - } - - TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin()) - testlist.clear(); - } -} - -//test: insert_after (seq-version), swap, splice_after: -template -void test_slist - ::test_swap(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef slist - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - { - list_type testlist1 (&values[0], &values[0] + 2); - list_type testlist2; - testlist2.insert_after (testlist2.end(), &values[0] + 2, &values[0] + 5); - testlist1.swap(testlist2); - { int init_values [] = { 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } - { int init_values [] = { 1, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } - testlist2.splice_after (testlist2.begin(), testlist1); - { int init_values [] = { 1, 3, 4, 5, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } - BOOST_TEST (testlist1.empty()); - - testlist1.splice_after (testlist1.end(), testlist2, ++testlist2.begin()); - { int init_values [] = { 4 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } - { int init_values [] = { 1, 3, 5, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } - - testlist1.splice_after (testlist1.begin(), testlist2, - testlist2.end(), ++++testlist2.begin()); - { int init_values [] = { 4, 1, 3, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } - { int init_values [] = { 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } - } - { - list_type testlist1 (&values[0], &values[0] + 2); - list_type testlist2 (&values[0] + 3, &values[0] + 5); - - values[0].swap_nodes(values[2]); - { int init_values [] = { 3, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } - - values[2].swap_nodes(values[4]); - { int init_values [] = { 5, 2 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } - { int init_values [] = { 4, 3 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } - } -} - -template -void test_slist - ::test_clone(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef slist - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - - list_type testlist1 (&values[0], &values[0] + values.size()); - list_type testlist2; - - testlist2.clone_from(testlist1, test::new_cloner(), test::delete_disposer()); - BOOST_TEST (testlist2 == testlist1); - testlist2.clear_and_dispose(test::delete_disposer()); - BOOST_TEST (testlist2.empty()); -} - -template -void test_slist - ::test_container_from_end(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef slist - < value_type - , value_traits - , size_type - , constant_time_size - > list_type; - list_type testlist1 (&values[0], &values[0] + values.size()); - BOOST_TEST (testlist1 == list_type::container_from_end_iterator(testlist1.end())); - BOOST_TEST (testlist1 == list_type::container_from_end_iterator(testlist1.cend())); -} - -template -class test_main_template -{ - public: - int operator()() - { - typedef testvalue value_type; - std::vector data (5); - for (int i = 0; i < 5; ++i) - data[i].value_ = i + 1; - - test_slist < typename detail::get_base_value_traits - < value_type - , typename value_type::slist_base_hook_t - >::type - >::test_all(data); - test_slist < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::slist_member_hook_t - , &value_type::slist_node_ - > - >::type - >::test_all(data); - - return 0; - } -}; - -template -class test_main_template -{ - public: - int operator()() - { - typedef testvalue value_type; - std::vector data (5); - for (int i = 0; i < 5; ++i) - data[i].value_ = i + 1; - - test_slist < typename detail::get_base_value_traits - < value_type - , typename value_type::slist_base_hook_t - >::type - >::test_all(data); - - test_slist < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::slist_member_hook_t - , &value_type::slist_node_ - > - >::type - >::test_all(data); - - test_slist < typename detail::get_base_value_traits - < value_type - , typename value_type::slist_auto_base_hook_t - >::type - >::test_all(data); - - test_slist < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::slist_auto_member_hook_t - , &value_type::slist_auto_node_ - > - >::type - >::test_all(data); - return 0; - } -}; - -int main(int, char* []) -{ - test_main_template()(); - test_main_template, false>()(); - test_main_template()(); - test_main_template, true>()(); - return boost::report_errors(); -} -#include diff --git a/test/smart_ptr.hpp b/test/smart_ptr.hpp deleted file mode 100644 index 6ed0d07..0000000 --- a/test/smart_ptr.hpp +++ /dev/null @@ -1,422 +0,0 @@ -////////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006. Distributed under the Boost -// Software License, Version 1.0. (See accompanying file -// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -////////////////////////////////////////////////////////////////////////////// - -#ifndef BOOST_INTRUSIVE_SMART_PTR_HPP -#define BOOST_INTRUSIVE_SMART_PTR_HPP - -#include -#include -#include - -#if (defined _MSC_VER) && (_MSC_VER >= 1200) -# pragma once -#endif - -namespace boost{ -namespace intrusive{ - -namespace detail { - -struct static_cast_tag {}; -struct const_cast_tag {}; -struct dynamic_cast_tag {}; -struct reinterpret_cast_tag {}; - -} //namespace detail { - -//Empty class -struct empty_type{}; - -template -struct random_it -: public boost::iterator -{ - typedef const T* const_pointer; - typedef const T& const_reference; -}; - -template<> struct random_it -{ - typedef const void * const_pointer; - typedef empty_type& reference; - typedef const empty_type& const_reference; - typedef empty_type difference_type; - typedef empty_type iterator_category; -}; - -template<> struct random_it -{ - typedef const void * const_pointer; - typedef const empty_type & reference; - typedef const empty_type & const_reference; - typedef empty_type difference_type; - typedef empty_type iterator_category; -}; - -template<> struct random_it -{ - typedef const volatile void * const_pointer; - typedef empty_type& reference; - typedef const empty_type& const_reference; - typedef empty_type difference_type; - typedef empty_type iterator_category; -}; - -template<> struct random_it -{ - typedef const volatile void * const_pointer; - typedef const empty_type & reference; - typedef const empty_type & const_reference; - typedef empty_type difference_type; - typedef empty_type iterator_category; -}; - -} //namespace intrusive { -} //namespace boost { - - -namespace boost { -namespace intrusive { - -template -class smart_ptr -{ - typedef random_it random_it_t; - typedef smart_ptr self_t; - typedef typename random_it_t::const_pointer const_pointer_t; - typedef typename random_it_t::const_reference const_reference_t; - - void unspecified_bool_type_func() const {} - typedef void (self_t::*unspecified_bool_type)() const; - - public: - typedef PointedType * pointer; - typedef typename random_it_t::reference reference; - typedef PointedType value_type; - typedef typename random_it_t::difference_type difference_type; - typedef typename random_it_t::iterator_category iterator_category; - - PointedType *m_ptr; - - public: //Public Functions - - //!Constructor from raw pointer (allows "0" pointer conversion). Never throws. - smart_ptr(pointer ptr = 0) - : m_ptr(ptr) - {} - - //!Constructor from other pointer. Never throws. - template - smart_ptr(T *ptr) - : m_ptr(ptr) - {} - - //!Constructor from other smart_ptr - smart_ptr(const smart_ptr& ptr) - : m_ptr(ptr.m_ptr) - {} - - //!Constructor from other smart_ptr. If pointers of pointee types are - //!convertible, offset_ptrs will be convertibles. Never throws. - template - smart_ptr(const smart_ptr &ptr) - : m_ptr(ptr.m_ptr) - {} - - //!Emulates static_cast operator. Never throws. - template - smart_ptr(const smart_ptr & r, detail::static_cast_tag) - : m_ptr(static_cast(r.get())) - {} - - //!Emulates const_cast operator. Never throws. - template - smart_ptr(const smart_ptr & r, detail::const_cast_tag) - : m_ptr(const_cast(r.get())) - {} - - //!Emulates dynamic_cast operator. Never throws. - template - smart_ptr(const smart_ptr & r, detail::dynamic_cast_tag) - : m_ptr(dynamic_cast(r.get())) - {} - - //!Emulates reinterpret_cast operator. Never throws. - template - smart_ptr(const smart_ptr & r, detail::reinterpret_cast_tag) - : m_ptr(reinterpret_cast(r.get())) - {} - - //!Obtains raw pointer from offset. Never throws. - pointer get() const - { return m_ptr; } - - //!Pointer-like -> operator. It can return 0 pointer. Never throws. - pointer operator->() const - { return this->get(); } - - //!Dereferencing operator, if it is a null smart_ptr behavior - //! is undefined. Never throws. - reference operator* () const - { return *(this->get()); } - - //!Indexing operator. Never throws. - reference operator[](std::ptrdiff_t idx) const - { return this->get()[idx]; } - - //!Assignment from pointer (saves extra conversion). Never throws. - smart_ptr& operator= (pointer from) - { m_ptr = from; return *this; } - - //!Assignment from other smart_ptr. Never throws. - smart_ptr& operator= (const smart_ptr & pt) - { m_ptr = pt.m_ptr; return *this; } - - //!Assignment from related smart_ptr. If pointers of pointee types - //! are assignable, offset_ptrs will be assignable. Never throws. - template - smart_ptr& operator= (const smart_ptr & pt) - { m_ptr = pt.m_ptr; return *this; } - - //!smart_ptr + std::ptrdiff_t. Never throws. - smart_ptr operator+ (std::ptrdiff_t offset) const - { return smart_ptr(this->get()+offset); } - - //!smart_ptr - std::ptrdiff_t. Never throws. - smart_ptr operator- (std::ptrdiff_t offset) const - { return smart_ptr(this->get()-offset); } - - //!smart_ptr += std::ptrdiff_t. Never throws. - smart_ptr &operator+= (std::ptrdiff_t offset) - { m_ptr += offset; return *this; } - - //!smart_ptr -= std::ptrdiff_t. Never throws. - smart_ptr &operator-= (std::ptrdiff_t offset) - { m_ptr -= offset; return *this; } - - //!++smart_ptr. Never throws. - smart_ptr& operator++ (void) - { ++m_ptr; return *this; } - - //!smart_ptr++. Never throws. - smart_ptr operator++ (int) - { smart_ptr temp(*this); ++*this; return temp; } - - //!--smart_ptr. Never throws. - smart_ptr& operator-- (void) - { --m_ptr; return *this; } - - //!smart_ptr--. Never throws. - smart_ptr operator-- (int) - { smart_ptr temp(*this); --*this; return temp; } - - //!safe bool conversion operator. Never throws. - operator unspecified_bool_type() const - { return this->get()? &self_t::unspecified_bool_type_func : 0; } - - //!Not operator. Not needed in theory, but improves portability. - //!Never throws. - bool operator! () const - { return this->get() == 0; } -/* - friend void swap (smart_ptr &pt, smart_ptr &pt2) - { - value_type *ptr = pt.get(); - pt = pt2; - pt2 = ptr; - } -*/ -}; - -//!smart_ptr == smart_ptr. Never throws. -template -inline bool operator== (const smart_ptr &pt1, - const smart_ptr &pt2) -{ return pt1.get() == pt2.get(); } - -//!smart_ptr != smart_ptr. Never throws. -template -inline bool operator!= (const smart_ptr &pt1, - const smart_ptr &pt2) -{ return pt1.get() != pt2.get(); } - -//!smart_ptr < smart_ptr. Never throws. -template -inline bool operator< (const smart_ptr &pt1, - const smart_ptr &pt2) -{ return pt1.get() < pt2.get(); } - -//!smart_ptr <= smart_ptr. Never throws. -template -inline bool operator<= (const smart_ptr &pt1, - const smart_ptr &pt2) -{ return pt1.get() <= pt2.get(); } - -//!smart_ptr > smart_ptr. Never throws. -template -inline bool operator> (const smart_ptr &pt1, - const smart_ptr &pt2) -{ return pt1.get() > pt2.get(); } - -//!smart_ptr >= smart_ptr. Never throws. -template -inline bool operator>= (const smart_ptr &pt1, - const smart_ptr &pt2) -{ return pt1.get() >= pt2.get(); } - -//!operator<< -template -inline std::basic_ostream & operator<< - (std::basic_ostream & os, smart_ptr const & p) -{ return os << p.get(); } - -//!operator>> -template -inline std::basic_istream & operator>> - (std::basic_istream & os, smart_ptr & p) -{ Y * tmp; return os >> tmp; p = tmp; } - -//!std::ptrdiff_t + smart_ptr -template -inline smart_ptr operator+(std::ptrdiff_t diff, const smart_ptr& right) -{ return right + diff; } - -//!smart_ptr - smart_ptr -template -inline std::ptrdiff_t operator- (const smart_ptr &pt, const smart_ptr &pt2) -{ return pt.get()- pt2.get(); } - -//!swap specialization -template -inline void swap (smart_ptr &pt, - smart_ptr &pt2) -{ - typename smart_ptr::value_type *ptr = pt.get(); - pt = pt2; - pt2 = ptr; -} - -//!detail::get_pointer() enables boost::mem_fn to recognize smart_ptr. -//!Never throws. -template -inline T* get_pointer(const smart_ptr & p) -{ return p.get(); } - -//!Simulation of static_cast between pointers. Never throws. -template -inline smart_ptr - static_pointer_cast(smart_ptr const & r) -{ - return smart_ptr(r, detail::static_cast_tag()); -} - -//!Simulation of const_cast between pointers. Never throws. -template -inline smart_ptrconst_pointer_cast(smart_ptr const & r) -{ - return smart_ptr(r, detail::const_cast_tag()); -} - -//!Simulation of dynamic_cast between pointers. Never throws. -template -inline smart_ptr - dynamic_pointer_cast(smart_ptr const & r) -{ - return smart_ptr - (r, detail::dynamic_cast_tag()); -} - -//!Simulation of reinterpret_cast between pointers. Never throws. -template -inline smart_ptr - reinterpret_pointer_cast(smart_ptr const & r) -{ - return smart_ptr(r, detail::reinterpret_cast_tag()); -} - -} //namespace intrusive { -} //namespace boost { - -namespace boost{ - -//This is to support embedding a bit in the pointer -//for intrusive containers, saving space -namespace intrusive { - -template -struct has_pointer_plus_bit, N> -{ - static const bool value = has_pointer_plus_bit::value; -}; - -//Specialization -template -struct pointer_plus_bit > -{ - typedef smart_ptr pointer; - - static pointer get_pointer(const pointer &n) - { return pointer_plus_bit::get_pointer(n.get()); } - - static void set_pointer(pointer &n, pointer p) - { - T *raw_n = n.get(); - pointer_plus_bit::set_pointer(raw_n, p.get()); - n = raw_n; - } - - static bool get_bit(const pointer &n) - { return pointer_plus_bit::get_bit(n.get()); } - - static void set_bit(pointer &n, bool c) - { - T *raw_n = n.get(); - pointer_plus_bit::set_bit(raw_n, c); - n = raw_n; - } -}; - -template -struct has_pointer_plus_2_bits, N> -{ - static const bool value = has_pointer_plus_2_bits::value; -}; - -template -struct pointer_plus_2_bits > -{ - typedef smart_ptr pointer; - - static pointer get_pointer(const pointer &n) - { return pointer_plus_2_bits::get_pointer(n.get()); } - - static void set_pointer(pointer &n, pointer p) - { - T *raw_n = n.get(); - pointer_plus_2_bits::set_pointer(raw_n, p.get()); - n = raw_n; - } - - static std::size_t get_bits(const pointer &n) - { return pointer_plus_2_bits::get_bits(n.get()); } - - static void set_bits(pointer &n, std::size_t c) - { - T *raw_n = n.get(); - pointer_plus_2_bits::set_bits(raw_n, c); - n = raw_n; - } -}; - -} //namespace intrusive -} //namespace boost{ - -#endif //#ifndef BOOST_INTRUSIVE_SMART_PTR_HPP diff --git a/test/splay_multiset_test.cpp b/test/splay_multiset_test.cpp deleted file mode 100644 index a8601ac..0000000 --- a/test/splay_multiset_test.cpp +++ /dev/null @@ -1,138 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "generic_multiset_test.hpp" - -namespace boost { namespace intrusive { namespace test { - -template -struct has_const_overloads > -{ - static const bool value = false; -}; - -template -struct has_splay > -{ - static const bool value = true; -}; - -template -struct has_rebalance > -{ - static const bool value = true; -}; - -}}} - -template< class ValueType - , class Option1 = boost::intrusive::none - , class Option2 = boost::intrusive::none - , class Option3 = boost::intrusive::none - > -struct GetContainer -{ - typedef boost::intrusive::splay_multiset - < ValueType - , Option1 - , Option2 - , Option3 - > type; -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::splay_set_base_hook_t - >::type - , GetContainer - >::test_all(); - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::splay_set_member_hook_t - , &value_type::splay_set_node_ - > - >::type - , GetContainer - >::test_all(); - return 0; - } -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::splay_set_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::splay_set_member_hook_t - , &value_type::splay_set_node_ - > - >::type - , GetContainer - >::test_all(); - - test::test_generic_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::splay_set_auto_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::splay_set_auto_member_hook_t - , &value_type::splay_set_auto_node_ - > - >::type - , GetContainer - >::test_all(); - return 0; - } -}; - -int main( int, char* [] ) -{ - test_main_template()(); - test_main_template, false>()(); - test_main_template()(); - test_main_template, true>()(); - return boost::report_errors(); -} diff --git a/test/splay_set_test.cpp b/test/splay_set_test.cpp deleted file mode 100644 index b18b4e7..0000000 --- a/test/splay_set_test.cpp +++ /dev/null @@ -1,138 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "generic_set_test.hpp" - -namespace boost { namespace intrusive { namespace test { - -template -struct has_const_overloads > -{ - static const bool value = false; -}; - -template -struct has_splay > -{ - static const bool value = true; -}; - -template -struct has_rebalance > -{ - static const bool value = true; -}; - -}}} - -template< class ValueType - , class Option1 = boost::intrusive::none - , class Option2 = boost::intrusive::none - , class Option3 = boost::intrusive::none - > -struct GetContainer -{ - typedef boost::intrusive::splay_set - < ValueType - , Option1 - , Option2 - , Option3 - > type; -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::splay_set_base_hook_t - >::type - , GetContainer - >::test_all(); - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::splay_set_member_hook_t - , &value_type::splay_set_node_ - > - >::type - , GetContainer - >::test_all(); - return 0; - } -}; - -template -class test_main_template -{ - public: - int operator()() - { - using namespace boost::intrusive; - typedef testvalue value_type; - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::splay_set_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::splay_set_member_hook_t - , &value_type::splay_set_node_ - > - >::type - , GetContainer - >::test_all(); - - test::test_generic_set < typename detail::get_base_value_traits - < value_type - , typename value_type::splay_set_auto_base_hook_t - >::type - , GetContainer - >::test_all(); - - test::test_generic_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::splay_set_auto_member_hook_t - , &value_type::splay_set_auto_node_ - > - >::type - , GetContainer - >::test_all(); - - return 0; - } -}; - -int main( int, char* [] ) -{ - test_main_template()(); - test_main_template, false>()(); - test_main_template()(); - test_main_template, true>()(); - return boost::report_errors(); -} -#include diff --git a/test/stateful_value_traits_test.cpp b/test/stateful_value_traits_test.cpp deleted file mode 100644 index aca8cb3..0000000 --- a/test/stateful_value_traits_test.cpp +++ /dev/null @@ -1,147 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include -#include -#include -#include -#include - -using namespace boost::intrusive; - -class MyClass -{ - public: - int int_; - - MyClass(int i = 0) - : int_(i) - {} - - friend bool operator<(const MyClass &l, const MyClass &r) - { return l.int_ < r.int_; } - - friend bool operator==(const MyClass &l, const MyClass &r) - { return l.int_ == r.int_; } - - friend std::size_t hash_value(const MyClass &v) - { return boost::hash_value(v.int_); } -}; - -template -struct stateful_value_traits -{ - typedef NodeTraits node_traits; - typedef typename node_traits::node node; - typedef typename node_traits::node_ptr node_ptr; - typedef typename node_traits::const_node_ptr const_node_ptr; - typedef T value_type; - typedef typename boost::pointer_to_other - ::type pointer; - typedef typename boost::pointer_to_other - ::type const_pointer; - static const link_mode_type link_mode = normal_link; - - stateful_value_traits(pointer values, node_ptr node_array) - : values_(values), node_array_(node_array) - {} - - node_ptr to_node_ptr (value_type &value) - { return node_array_ + (&value - values_); } - - const_node_ptr to_node_ptr (const value_type &value) const - { return node_array_ + (&value - values_); } - - pointer to_value_ptr(node_ptr n) - { return values_ + (n - node_array_); } - - const_pointer to_value_ptr(const_node_ptr n) const - { return values_ + (n - node_array_); } - - pointer values_; - node_ptr node_array_; -}; - -//Define a list that will store MyClass using the external hook -typedef stateful_value_traits< MyClass, list_node_traits > list_traits; -typedef list > List; - -//Define a slist that will store MyClass using the external hook -typedef stateful_value_traits< MyClass, slist_node_traits > slist_traits; -typedef slist > Slist; - -//Define a set that will store MyClass using the external hook -typedef stateful_value_traits< MyClass, rbtree_node_traits > rbtree_traits; -typedef set > Set; - -//uset uses the same traits as slist -typedef unordered_set > Uset; - - -typedef list_traits::node list_node_t; -typedef slist_traits::node slist_node_t; -typedef rbtree_traits::node rbtree_node_t; - -const int NumElements = 100; - -MyClass values [NumElements]; -list_node_t list_hook_array [NumElements]; -slist_node_t slist_hook_array [NumElements]; -rbtree_node_t rbtree_hook_array [NumElements]; -slist_node_t uset_hook_array [NumElements]; - -int main() -{ - //Create several MyClass objects, each one with a different value - for(int i = 0; i < NumElements; ++i) - values[i].int_ = i; - - Uset::bucket_type buckets[NumElements]; - - List my_list (list_traits (values, list_hook_array)); - Slist my_slist(slist_traits(values, slist_hook_array)); - Set my_set (std::less(), rbtree_traits(values, rbtree_hook_array)); - Uset my_uset ( Uset::bucket_traits(buckets, NumElements) - , boost::hash() - , std::equal_to() - , slist_traits(values, uset_hook_array) - ); - - //Now insert them in containers - for(MyClass * it(&values[0]), *itend(&values[NumElements]) - ; it != itend - ; ++it){ - my_list.push_front(*it); - my_slist.push_front(*it); - my_set.insert(*it); - my_uset.insert(*it); - } - - //Now test lists - { - List::const_iterator list_it (my_list.cbegin()); - Slist::const_iterator slist_it(my_slist.cbegin()); - Set::const_reverse_iterator set_rit(my_set.crbegin()); - MyClass *it_val(&values[NumElements-1]), *it_rbeg_val(&values[0]-1); - - //Test the objects inserted in the base hook list - for(; it_val != it_rbeg_val; --it_val, ++list_it, ++slist_it, ++set_rit){ - if(&*list_it != &*it_val) return 1; - if(&*slist_it != &*it_val) return 1; - if(&*set_rit != &*it_val) return 1; - if(my_uset.find(*it_val) == my_uset.cend()) return 1; - } - } - - return 0; -} diff --git a/test/test_container.hpp b/test/test_container.hpp deleted file mode 100644 index 9ae000a..0000000 --- a/test/test_container.hpp +++ /dev/null @@ -1,301 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// - -#ifndef BOOST_INTRUSIVE_TEST_CONTAINER_HPP -#define BOOST_INTRUSIVE_TEST_CONTAINER_HPP - -#include -#include - -namespace boost { -namespace intrusive { -namespace test { - -template -struct has_const_overloads -{ - static const bool value = true; -}; - -template< class Container > -void test_container( Container & c ) -{ - typedef typename Container::value_type value_type; - typedef typename Container::iterator iterator; - typedef typename Container::const_iterator const_iterator; - typedef typename Container::reference reference; - typedef typename Container::const_reference const_reference; - typedef typename Container::pointer pointer; - typedef typename Container::const_pointer const_pointer; - typedef typename Container::difference_type difference_type; - typedef typename Container::size_type size_type; - typedef typename Container::difference_type difference_type; - typedef typename Container::size_type size_type; - typedef typename Container::value_traits value_traits; - - const size_type num_elem = c.size(); - BOOST_TEST( c.empty() == (num_elem == 0) ); - { - iterator it(c.begin()), itend(c.end()); - size_type i; - for(i = 0; i < num_elem; ++i){ - ++it; - } - BOOST_TEST( it == c.end() ); - BOOST_TEST( c.size() == i ); - } - - //Check iterator conversion - BOOST_TEST( const_iterator(c.begin()) == c.cbegin() ); - { - const_iterator it(c.cbegin()), itend(c.cend()); - size_type i; - for(i = 0; i < num_elem; ++i){ - ++it; - } - BOOST_TEST( it == c.cend() ); - BOOST_TEST( c.size() == i ); - } -} - - -template< class Container, class Data > -void test_sequence_container(Container & c, Data & d) -{ - assert( d.size() > 2 ); - - c.clear(); - - BOOST_TEST( c.size() == 0 ); - BOOST_TEST( c.empty() ); - - { - typename Data::iterator i = d.begin(); - c.insert( c.begin(), *i ); - c.insert( c.end(), *(++i) ); - } - - BOOST_TEST( c.size() == 2 ); - BOOST_TEST( !c.empty() ); - - c.erase( c.begin() ); - - BOOST_TEST( c.size() == 1 ); - - { - typename Data::iterator i = d.begin(); - ++++i; - c.insert( c.begin(), *(i) ); - } - - c.erase( c.begin(), c.end() ); - - BOOST_TEST( c.empty() ); - - c.insert( c.begin(), *d.begin() ); - - BOOST_TEST( c.size() == 1 ); - - BOOST_TEST( c.begin() != c.end() ); - - c.erase( c.begin() ); - - c.assign(d.begin(), d.end()); - - BOOST_TEST( c.size() == d.size() ); - - c.clear(); - - BOOST_TEST( c.size() == 0 ); - BOOST_TEST( c.empty() ); -} - -template< class Container, class Data > -void test_common_unordered_and_associative_container(Container & c, Data & d) -{ - typedef typename Container::size_type size_type; - - assert( d.size() > 2 ); - - c.clear(); - c.insert(d.begin(), d.end()); - - for( typename Data::const_iterator di = d.begin(), de = d.end(); - di != de; ++di ) - { - BOOST_TEST( c.find(*di) != c.end() ); - } - - typename Data::const_iterator db = d.begin(); - typename Data::const_iterator da = db++; - - size_type old_size = c.size(); - - c.erase(*da); - - BOOST_TEST( c.size() == old_size-1 ); - - BOOST_TEST( c.count(*da) == 0 ); - BOOST_TEST( c.count(*db) != 0 ); - - BOOST_TEST( c.find(*da) == c.end() ); - BOOST_TEST( c.find(*db) != c.end() ); - - BOOST_TEST( c.equal_range(*db).first != c.end() ); - - c.clear(); - - BOOST_TEST( c.equal_range(*da).first == c.end() ); -} - -template< class Container, class Data > -void test_associative_container_invariants(Container & c, Data & d, boost::intrusive::detail::true_type) -{ - typedef typename Container::const_iterator const_iterator; - for( typename Data::const_iterator di = d.begin(), de = d.end(); - di != de; ++di) - { - const_iterator ci = c.find(*di); - BOOST_TEST( ci != c.end() ); - BOOST_TEST( ! c.value_comp()(*ci, *di) ); - const_iterator cil = c.lower_bound(*di); - const_iterator ciu = c.upper_bound(*di); - std::pair er = c.equal_range(*di); - BOOST_TEST( cil == er.first ); - BOOST_TEST( ciu == er.second ); - if(ciu != c.end()){ - BOOST_TEST( c.value_comp()(*cil, *ciu) ); - } - if(c.count(*di) > 1){ - const_iterator ci_next = cil; ++ci_next; - for( ; ci_next != ciu; ++cil, ++ci_next){ - BOOST_TEST( !c.value_comp()(*ci_next, *cil) ); - } - } - } -} - -template< class Container, class Data > -void test_associative_container_invariants(Container &, Data &, boost::intrusive::detail::false_type) -{} - -template< class Container, class Data > -void test_associative_container_invariants(Container & c, Data & d) -{ - using namespace boost::intrusive; - typedef typename detail::remove_const::type Type; - typedef detail::bool_::value> enabler; - test_associative_container_invariants(c, d, enabler()); -} - -template< class Container, class Data > -void test_associative_container(Container & c, Data & d) -{ - typedef typename Container::const_iterator const_iterator; - assert( d.size() > 2 ); - - c.clear(); - c.insert(d.begin(),d.end()); - - test_associative_container_invariants(c, d); - - const Container & cr = c; - - test_associative_container_invariants(cr, d); -} - -template< class Container, class Data > -void test_unordered_associative_container_invariants(Container & c, Data & d, boost::intrusive::detail::true_type) -{ - typedef typename Container::size_type size_type; - typedef typename Container::const_iterator const_iterator; - - for( typename Data::const_iterator di = d.begin(), de = d.end() ; - di != de ; ++di ){ - const_iterator i = c.find(*di); - size_type nb = c.bucket(*i); - size_type bucket_elem = std::distance(c.begin(nb), c.end(nb)); - BOOST_TEST( bucket_elem == c.bucket_size(nb) ); - BOOST_TEST( &*c.local_iterator_to(*c.find(*di)) == &*i ); - std::pair er = c.equal_range(*di); - size_type cnt = std::distance(er.first, er.second); - BOOST_TEST( cnt == c.count(*di)); - if(cnt > 1) - for(const_iterator n = er.first, i = n++, e = er.second; n != e; ++i, ++n){ - BOOST_TEST( c.key_eq()(*i, *n) ); - BOOST_TEST( c.hash_function()(*i) == c.hash_function()(*n) ); - } - } - - size_type blen = c.bucket_count(); - size_type total_objects = 0; - for(size_type i = 0; i < blen; ++i){ - total_objects += c.bucket_size(i); - } - BOOST_TEST( total_objects == c.size() ); -} - -template< class Container, class Data > -void test_unordered_associative_container_invariants(Container &, Data &, boost::intrusive::detail::false_type) -{} - -template< class Container, class Data > -void test_unordered_associative_container_invariants(Container & c, Data & d) -{ - using namespace boost::intrusive; - typedef typename detail::remove_const::type Type; - typedef detail::bool_::value> enabler; - test_unordered_associative_container_invariants(c, d, enabler()); -} - -template< class Container, class Data > -void test_unordered_associative_container(Container & c, Data & d) -{ - c.clear(); - c.insert( d.begin(), d.end() ); - - test_unordered_associative_container_invariants(c, d); - - const Container & cr = c; - - test_unordered_associative_container_invariants(cr, d); -} - -template< class Container, class Data > -void test_unique_container(Container & c, Data & d) -{ - typedef typename Container::value_type value_type; - c.clear(); - c.insert(d.begin(),d.end()); - typename Container::size_type old_size = c.size(); - value_type v(*d.begin()); - c.insert(v); - BOOST_TEST( c.size() == old_size ); - c.clear(); -} - -template< class Container, class Data > -void test_non_unique_container(Container & c, Data & d) -{ - typedef typename Container::value_type value_type; - c.clear(); - c.insert(d.begin(),d.end()); - typename Container::size_type old_size = c.size(); - value_type v(*d.begin()); - c.insert(v); - BOOST_TEST( c.size() == (old_size+1) ); - c.clear(); -} - -}}} - -#endif //#ifndef BOOST_INTRUSIVE_TEST_CONTAINER_HPP diff --git a/test/test_macros.hpp b/test/test_macros.hpp deleted file mode 100644 index d612b9f..0000000 --- a/test/test_macros.hpp +++ /dev/null @@ -1,29 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2006-2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// - -#ifndef BOOST_INTRUSIVE_TEST_TEST_MACROS_HPP -#define BOOST_INTRUSIVE_TEST_TEST_MACROS_HPP - -#define TEST_INTRUSIVE_SEQUENCE( INTVALUES, ITERATOR )\ -{ \ - const int init_values_size = sizeof(INTVALUES)/sizeof(INTVALUES[0]); \ - std::vector expected; \ - expected.assign(&INTVALUES[0], &INTVALUES[0] + init_values_size); \ - BOOST_TEST (std::equal(expected.begin(), expected.end(), ITERATOR) ); \ -} - -#define TEST_INTRUSIVE_SEQUENCE_EXPECTED( EXPECTEDVECTOR, ITERATOR )\ -{ \ - BOOST_TEST (std::equal(EXPECTEDVECTOR.begin(), EXPECTEDVECTOR.end(), ITERATOR) ); \ -} - -#endif diff --git a/test/test_templates.hpp b/test/test_templates.hpp deleted file mode 100644 index 9374b5a..0000000 --- a/test/test_templates.hpp +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef BOOST_INTRUSIVE_TEST_TEST_TEMPLATES_HPP -#define BOOST_INTRUSIVE_TEST_TEST_TEMPLATES_HPP - -#include - -namespace boost { -namespace intrusive { -namespace test { - -template -void test_shift() -{ - typedef typename ValueTraits::value_type testvalue_t; - boost::test_tools::output_test_stream test_seq; - Container test_cont; - const int NumElem = 6; - - std::vector values(NumElem); - for(int i = 0; i < NumElem; ++i){ - values[i] = i+1; - } - - const int num_values = (int)values.size(); - std::vector expected_values(num_values); - - //Shift forward all possible positions 3 times - for(int i = 0; i < num_values*3; ++i){ - test_cont.assign(values.begin(), values.end()); - test_cont.shift_forward(i); - for(int j = 0; j < num_values; ++j){ - expected_values[(j + num_values - i%num_values) % num_values] = (j + 1); - } - std::copy (test_cont.begin(), test_cont.end(), - std::ostream_iterator (test_seq)); - std::stringstream stream; - std::copy (expected_values.begin(), expected_values.end(), - std::ostream_iterator (stream)); - stream << std::ends; - BOOST_CHECK (test_seq.is_equal (stream.str().c_str())); - test_cont.clear(); - } - - //Shift backwards all possible positions - for(int i = 0; i < num_values*2; ++i){ - test_cont.assign(values.begin(), values.end()); - test_cont.shift_backwards(i); - for(int j = 0; j < num_values; ++j){ - expected_values[(j + i) % num_values] = (j + 1); - } - std::copy (test_cont.begin(), test_cont.end(), - std::ostream_iterator (test_seq)); - std::stringstream stream; - std::copy (expected_values.begin(), expected_values.end(), - std::ostream_iterator (stream)); - stream << std::ends; - BOOST_CHECK (test_seq.is_equal (stream.str().c_str())); - test_cont.clear(); - } -} - -} //namespace test { -} //namespace intrusive { -} //namespace boost { - -#endif //#ifndef BOOST_INTRUSIVE_TEST_TEST_TEMPLATES_HPP diff --git a/test/unordered_multiset_test.cpp b/test/unordered_multiset_test.cpp deleted file mode 100644 index ceb30ba..0000000 --- a/test/unordered_multiset_test.cpp +++ /dev/null @@ -1,445 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// - -#include -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "common_functors.hpp" -#include -#include -#include -#include "test_macros.hpp" -#include "test_container.hpp" - -using namespace boost::intrusive; - -static const std::size_t BucketSize = 11; - -template -struct test_unordered_multiset -{ - typedef typename ValueTraits::value_type value_type; - static void test_all (std::vector& values); - static void test_sort(std::vector& values); - static void test_insert(std::vector& values); - static void test_swap(std::vector& values); - static void test_rehash(std::vector& values); - static void test_find(std::vector& values); - static void test_impl(); - static void test_clone(std::vector& values); -}; - -template -void test_unordered_multiset::test_all (std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_multiset - - , constant_time_size - > unordered_multiset_type; - { - typedef typename unordered_multiset_type::bucket_traits bucket_traits; - typename unordered_multiset_type::bucket_type buckets [BucketSize]; - unordered_multiset_type testset(bucket_traits(buckets, BucketSize)); - testset.insert(values.begin(), values.end()); - test::test_container(testset); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_common_unordered_and_associative_container(testset, values); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_unordered_associative_container(testset, values); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_non_unique_container(testset, values); - } - test_sort(values); - test_insert(values); - test_swap(values); - test_rehash(values); - test_find(values); - test_impl(); - test_clone(values); -} - -//test case due to an error in tree implementation: -template -void test_unordered_multiset::test_impl() -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_multiset - - , constant_time_size - > unordered_multiset_type; - typedef typename unordered_multiset_type::bucket_traits bucket_traits; - - std::vector values (5); - for (int i = 0; i < 5; ++i) - values[i].value_ = i; - - typename unordered_multiset_type::bucket_type buckets [BucketSize]; - unordered_multiset_type testset(bucket_traits(buckets, BucketSize)); - - for (int i = 0; i < 5; ++i) - testset.insert (values[i]); - - testset.erase (testset.iterator_to (values[0])); - testset.erase (testset.iterator_to (values[1])); - testset.insert (values[1]); - - testset.erase (testset.iterator_to (values[2])); - testset.erase (testset.iterator_to (values[3])); -} - -//test: constructor, iterator, clear, reverse_iterator, front, back, size: -template -void test_unordered_multiset::test_sort(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_multiset - - , constant_time_size - > unordered_multiset_type; - typedef typename unordered_multiset_type::bucket_traits bucket_traits; - - typename unordered_multiset_type::bucket_type buckets [BucketSize]; - unordered_multiset_type testset1(values.begin(), values.end(), bucket_traits(buckets, BucketSize)); - - { int init_values [] = { 1, 2, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - testset1.clear(); - BOOST_TEST (testset1.empty()); -} - -//test: insert, const_iterator, const_reverse_iterator, erase, iterator_to: -template -void test_unordered_multiset::test_insert(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_multiset - - , constant_time_size - > unordered_multiset_type; - typedef typename unordered_multiset_type::bucket_traits bucket_traits; - - typename unordered_multiset_type::bucket_type buckets [BucketSize]; - unordered_multiset_type testset(bucket_traits(buckets, BucketSize)); - - testset.insert(&values[0] + 2, &values[0] + 5); - - const unordered_multiset_type& const_testset = testset; - { int init_values [] = { 1, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testset.begin() ); } - - typename unordered_multiset_type::iterator i = testset.begin(); - BOOST_TEST (i->value_ == 1); - - i = testset.insert (values[0]); - BOOST_TEST (&*i == &values[0]); - - i = testset.iterator_to (values[2]); - BOOST_TEST (&*i == &values[2]); - testset.erase(i); - - { int init_values [] = { 1, 3, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testset.begin() ); } - testset.clear(); - testset.insert(&values[0], &values[0] + values.size()); - - { int init_values [] = { 1, 2, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testset.begin() ); } - - BOOST_TEST (testset.erase(1) == 1); - BOOST_TEST (testset.erase(2) == 2); - BOOST_TEST (testset.erase(3) == 1); - BOOST_TEST (testset.erase(4) == 1); - BOOST_TEST (testset.erase(5) == 1); - BOOST_TEST (testset.empty() == true); - - //Now with a single bucket - typename unordered_multiset_type::bucket_type single_bucket[1]; - unordered_multiset_type testset2(bucket_traits(single_bucket, 1)); - testset2.insert(&values[0], &values[0] + values.size()); - BOOST_TEST (testset2.erase(5) == 1); - BOOST_TEST (testset2.erase(2) == 2); - BOOST_TEST (testset2.erase(1) == 1); - BOOST_TEST (testset2.erase(4) == 1); - BOOST_TEST (testset2.erase(3) == 1); - BOOST_TEST (testset2.empty() == true); -} - -//test: insert (seq-version), swap, erase (seq-version), size: -template -void test_unordered_multiset::test_swap(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_multiset - - , constant_time_size - > unordered_multiset_type; - typedef typename unordered_multiset_type::bucket_traits bucket_traits; - - typename unordered_multiset_type::bucket_type buckets [BucketSize]; - typename unordered_multiset_type::bucket_type buckets2 [BucketSize]; - unordered_multiset_type testset1(&values[0], &values[0] + 2, bucket_traits(buckets, BucketSize)); - unordered_multiset_type testset2(bucket_traits(buckets2, BucketSize)); - - testset2.insert (&values[0] + 2, &values[0] + 6); - testset1.swap (testset2); - - { int init_values [] = { 1, 2, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - { int init_values [] = { 2, 3 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); } - - testset1.erase (testset1.iterator_to(values[5]), testset1.end()); - BOOST_TEST (testset1.size() == 1); - // BOOST_TEST (&testset1.front() == &values[3]); - BOOST_TEST (&*testset1.begin() == &values[3]); -} - -//test: rehash: -template -void test_unordered_multiset::test_rehash(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_multiset - - , constant_time_size - > unordered_multiset_type; - typedef typename unordered_multiset_type::bucket_traits bucket_traits; - - typename unordered_multiset_type::bucket_type buckets1 [BucketSize]; - typename unordered_multiset_type::bucket_type buckets2 [2]; - typename unordered_multiset_type::bucket_type buckets3 [BucketSize*2]; - - unordered_multiset_type testset1(&values[0], &values[0] + 6, bucket_traits(buckets1, BucketSize)); - BOOST_TEST (testset1.size() == 6); - { int init_values [] = { 1, 2, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - testset1.rehash(bucket_traits(buckets2, 2)); - BOOST_TEST (testset1.size() == 6); - { int init_values [] = { 4, 2, 2, 5, 3, 1 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - testset1.rehash(bucket_traits(buckets3, BucketSize*2)); - BOOST_TEST (testset1.size() == 6); - { int init_values [] = { 1, 2, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - //Now rehash reducing the buckets - testset1.rehash(bucket_traits(buckets3, 2)); - BOOST_TEST (testset1.size() == 6); - { int init_values [] = { 4, 2, 2, 5, 3, 1 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - //Now rehash increasing the buckets - testset1.rehash(bucket_traits(buckets3, BucketSize*2)); - BOOST_TEST (testset1.size() == 6); - { int init_values [] = { 1, 2, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } -} - -//test: find, equal_range (lower_bound, upper_bound): -template -void test_unordered_multiset::test_find(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_multiset - - , constant_time_size - > unordered_multiset_type; - typedef typename unordered_multiset_type::bucket_traits bucket_traits; - - typename unordered_multiset_type::bucket_type buckets[BucketSize]; - unordered_multiset_type testset(values.begin(), values.end(), bucket_traits(buckets, BucketSize)); - - typedef typename unordered_multiset_type::iterator iterator; - - value_type cmp_val; - cmp_val.value_ = 2; - iterator i = testset.find (cmp_val); - BOOST_TEST (i->value_ == 2); - BOOST_TEST ((++i)->value_ == 2); - std::pair range = testset.equal_range (cmp_val); - - BOOST_TEST (range.first->value_ == 2); - BOOST_TEST (range.second->value_ == 3); - BOOST_TEST (std::distance (range.first, range.second) == 2); - - cmp_val.value_ = 7; - BOOST_TEST (testset.find (cmp_val) == testset.end()); -} - - -template -void test_unordered_multiset - ::test_clone(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_multiset - - , constant_time_size - > unordered_multiset_type; - typedef typename unordered_multiset_type::bucket_traits bucket_traits; - { - //Test with equal bucket arrays - typename unordered_multiset_type::bucket_type buckets1 [BucketSize]; - typename unordered_multiset_type::bucket_type buckets2 [BucketSize]; - unordered_multiset_type testset1 (values.begin(), values.end(), bucket_traits(buckets1, BucketSize)); - unordered_multiset_type testset2 (bucket_traits(buckets2, BucketSize)); - - testset2.clone_from(testset1, test::new_cloner(), test::delete_disposer()); - //Ordering is not guarantee in the cloning so insert data in a set and test - std::multiset - src(testset1.begin(), testset1.end()); - std::multiset - dst(testset2.begin(), testset2.end()); - BOOST_TEST (src.size() == dst.size() && std::equal(src.begin(), src.end(), dst.begin())); - testset2.clear_and_dispose(test::delete_disposer()); - BOOST_TEST (testset2.empty()); - } - { - //Test with bigger source bucket arrays - typename unordered_multiset_type::bucket_type buckets1 [BucketSize*2]; - typename unordered_multiset_type::bucket_type buckets2 [BucketSize]; - unordered_multiset_type testset1 (values.begin(), values.end(), bucket_traits(buckets1, BucketSize*2)); - unordered_multiset_type testset2 (bucket_traits(buckets2, BucketSize)); - - testset2.clone_from(testset1, test::new_cloner(), test::delete_disposer()); - //Ordering is not guarantee in the cloning so insert data in a set and test - std::multiset - src(testset1.begin(), testset1.end()); - std::multiset - dst(testset2.begin(), testset2.end()); - BOOST_TEST (src.size() == dst.size() && std::equal(src.begin(), src.end(), dst.begin())); - testset2.clear_and_dispose(test::delete_disposer()); - BOOST_TEST (testset2.empty()); - } - { - //Test with smaller source bucket arrays - typename unordered_multiset_type::bucket_type buckets1 [BucketSize]; - typename unordered_multiset_type::bucket_type buckets2 [BucketSize*2]; - unordered_multiset_type testset1 (values.begin(), values.end(), bucket_traits(buckets1, BucketSize)); - unordered_multiset_type testset2 (bucket_traits(buckets2, BucketSize*2)); - - testset2.clone_from(testset1, test::new_cloner(), test::delete_disposer()); - //Ordering is not guarantee in the cloning so insert data in a set and test - std::multiset - src(testset1.begin(), testset1.end()); - std::multiset - dst(testset2.begin(), testset2.end()); - BOOST_TEST (src.size() == dst.size() && std::equal(src.begin(), src.end(), dst.begin())); - testset2.clear_and_dispose(test::delete_disposer()); - BOOST_TEST (testset2.empty()); - } -} - -template -class test_main_template -{ - public: - int operator()() - { - typedef testvalue value_type; - static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; - std::vector > data (6); - for (int i = 0; i < 6; ++i) - data[i].value_ = random_init[i]; - - test_unordered_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::unordered_set_base_hook_t - >::type - >::test_all(data); - - test_unordered_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::unordered_set_member_hook_t - , &value_type::unordered_set_node_ - > - >::type - >::test_all(data); - - return 0; - } -}; - -template -class test_main_template -{ - public: - int operator()() - { - typedef testvalue value_type; - static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; - std::vector > data (6); - for (int i = 0; i < 6; ++i) - data[i].value_ = random_init[i]; - - test_unordered_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::unordered_set_base_hook_t - >::type - >::test_all(data); - - test_unordered_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::unordered_set_member_hook_t - , &value_type::unordered_set_node_ - > - >::type - >::test_all(data); - - test_unordered_multiset < typename detail::get_base_value_traits - < value_type - , typename value_type::unordered_set_auto_base_hook_t - >::type - >::test_all(data); - - test_unordered_multiset < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::unordered_set_auto_member_hook_t - , &value_type::unordered_set_auto_node_ - > - >::type - >::test_all(data); - return 0; - } -}; - -int main( int, char* [] ) -{ - test_main_template()(); - test_main_template, false>()(); - test_main_template()(); - test_main_template, true>()(); - return boost::report_errors(); -} - -#include diff --git a/test/unordered_set_test.cpp b/test/unordered_set_test.cpp deleted file mode 100644 index 3eeccf6..0000000 --- a/test/unordered_set_test.cpp +++ /dev/null @@ -1,417 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Olaf Krzikalla 2004-2006. -// (C) Copyright Ion Gaztanaga 2006-2007. -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include -#include "itestvalue.hpp" -#include "smart_ptr.hpp" -#include "common_functors.hpp" -#include -#include -#include -#include "test_macros.hpp" -#include "test_container.hpp" - -using namespace boost::intrusive; - -static const std::size_t BucketSize = 11; - -template -struct test_unordered_set -{ - typedef typename ValueTraits::value_type value_type; - static void test_all(std::vector& values); - static void test_sort(std::vector& values); - static void test_insert(std::vector& values); - static void test_swap(std::vector& values); - static void test_rehash(std::vector& values); - static void test_find(std::vector& values); - static void test_impl(); - static void test_clone(std::vector& values); -}; - -template -void test_unordered_set::test_all(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_set - - , constant_time_size - > unordered_set_type; - typedef typename unordered_set_type::bucket_traits bucket_traits; - { - typename unordered_set_type::bucket_type buckets [BucketSize]; - unordered_set_type testset(bucket_traits(buckets, BucketSize)); - testset.insert(values.begin(), values.end()); - test::test_container(testset); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_common_unordered_and_associative_container(testset, values); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_unordered_associative_container(testset, values); - testset.clear(); - testset.insert(values.begin(), values.end()); - test::test_unique_container(testset, values); - } - test_sort(values); - test_insert(values); - test_swap(values); - test_rehash(values); - test_find(values); - test_impl(); - test_clone(values); -} - -//test case due to an error in tree implementation: -template -void test_unordered_set::test_impl() -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_set - - , constant_time_size - > unordered_set_type; - typedef typename unordered_set_type::bucket_traits bucket_traits; - - std::vector values (5); - for (int i = 0; i < 5; ++i) - values[i].value_ = i; - - typename unordered_set_type::bucket_type buckets [BucketSize]; - unordered_set_type testset(bucket_traits(buckets, BucketSize)); - for (int i = 0; i < 5; ++i) - testset.insert (values[i]); - - testset.erase (testset.iterator_to (values[0])); - testset.erase (testset.iterator_to (values[1])); - testset.insert (values[1]); - testset.erase (testset.iterator_to (values[2])); - testset.erase (testset.iterator_to (values[3])); -} - -//test: constructor, iterator, clear, reverse_iterator, front, back, size: -template -void test_unordered_set::test_sort(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_set - - , constant_time_size - > unordered_set_type; - typedef typename unordered_set_type::bucket_traits bucket_traits; - - typename unordered_set_type::bucket_type buckets [BucketSize]; - unordered_set_type testset1(values.begin(), values.end(), bucket_traits(buckets, BucketSize)); - - BOOST_TEST (5 == std::distance(testset1.begin(), testset1.end())); - { int init_values [] = { 1, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - testset1.clear(); - BOOST_TEST (testset1.empty()); -} - -//test: insert, const_iterator, const_reverse_iterator, erase, iterator_to: -template -void test_unordered_set::test_insert(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_set - - , constant_time_size - > unordered_set_type; - typedef typename unordered_set_type::bucket_traits bucket_traits; - - typename unordered_set_type::bucket_type buckets [BucketSize]; - unordered_set_type testset(bucket_traits(buckets, BucketSize)); - testset.insert(&values[0] + 2, &values[0] + 5); - - const unordered_set_type& const_testset = testset; - { int init_values [] = { 1, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testset.begin() ); } - - typename unordered_set_type::iterator i = testset.begin(); - BOOST_TEST (i->value_ == 1); - - i = testset.insert(values[0]).first; - BOOST_TEST (&*i == &values[0]); - - i = testset.iterator_to (values[2]); - BOOST_TEST (&*i == &values[2]); - - testset.erase (i); - - { int init_values [] = { 1, 3, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, const_testset.begin() ); } -} - -//test: insert (seq-version), swap, erase (seq-version), size: -template -void test_unordered_set::test_swap(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_set - - , constant_time_size - > unordered_set_type; - typedef typename unordered_set_type::bucket_traits bucket_traits; - - typename unordered_set_type::bucket_type buckets1 [BucketSize]; - typename unordered_set_type::bucket_type buckets2 [BucketSize]; - unordered_set_type testset1(&values[0], &values[0] + 2, bucket_traits(buckets1, BucketSize)); - unordered_set_type testset2(bucket_traits(buckets2, BucketSize)); - - testset2.insert (&values[0] + 2, &values[0] + 6); - testset1.swap (testset2); - - { int init_values [] = { 1, 2, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - { int init_values [] = { 2, 3 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); } - - testset1.erase (testset1.iterator_to(values[5]), testset1.end()); - BOOST_TEST (testset1.size() == 1); - // BOOST_TEST (&testset1.front() == &values[3]); - BOOST_TEST (&*testset1.begin() == &values[3]); -} - -//test: rehash: -template -void test_unordered_set::test_rehash(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_set - - , constant_time_size - > unordered_set_type; - typedef typename unordered_set_type::bucket_traits bucket_traits; - - typename unordered_set_type::bucket_type buckets1 [BucketSize]; - typename unordered_set_type::bucket_type buckets2 [2]; - typename unordered_set_type::bucket_type buckets3 [BucketSize*2]; - - unordered_set_type testset1(&values[0], &values[0] + 6, bucket_traits(buckets1, BucketSize)); - BOOST_TEST (testset1.size() == 5); - { int init_values [] = { 1, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - testset1.rehash(bucket_traits(buckets2, 2)); - BOOST_TEST (testset1.size() == 5); - { int init_values [] = { 4, 2, 5, 3, 1 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - testset1.rehash(bucket_traits(buckets3, BucketSize*2)); - BOOST_TEST (testset1.size() == 5); - { int init_values [] = { 1, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - //Now rehash reducing the buckets - testset1.rehash(bucket_traits(buckets3, 2)); - BOOST_TEST (testset1.size() == 5); - { int init_values [] = { 4, 2, 5, 3, 1 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } - - //Now rehash increasing the buckets - testset1.rehash(bucket_traits(buckets3, BucketSize*2)); - BOOST_TEST (testset1.size() == 5); - { int init_values [] = { 1, 2, 3, 4, 5 }; - TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); } -} - - -//test: find, equal_range (lower_bound, upper_bound): -template -void test_unordered_set::test_find(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_set - - , constant_time_size - > unordered_set_type; - typedef typename unordered_set_type::bucket_traits bucket_traits; - - typename unordered_set_type::bucket_type buckets [BucketSize]; - unordered_set_type testset (values.begin(), values.end(), bucket_traits(buckets, BucketSize)); - typedef typename unordered_set_type::iterator iterator; - - value_type cmp_val; - cmp_val.value_ = 2; - iterator i = testset.find (cmp_val); - BOOST_TEST (i->value_ == 2); - BOOST_TEST ((++i)->value_ != 2); - std::pair range = testset.equal_range (cmp_val); - - BOOST_TEST (range.first->value_ == 2); - BOOST_TEST (range.second->value_ == 3); - BOOST_TEST (std::distance (range.first, range.second) == 1); - - cmp_val.value_ = 7; - BOOST_TEST (testset.find (cmp_val) == testset.end()); -} - -template -void test_unordered_set - ::test_clone(std::vector& values) -{ - typedef typename ValueTraits::value_type value_type; - typedef unordered_set - - , constant_time_size - > unordered_set_type; - typedef typename unordered_set_type::bucket_traits bucket_traits; - { - //Test with equal bucket arrays - typename unordered_set_type::bucket_type buckets1 [BucketSize]; - typename unordered_set_type::bucket_type buckets2 [BucketSize]; - unordered_set_type testset1 (values.begin(), values.end(), bucket_traits(buckets1, BucketSize)); - unordered_set_type testset2 (bucket_traits(buckets2, BucketSize)); - - testset2.clone_from(testset1, test::new_cloner(), test::delete_disposer()); - //Ordering is not guarantee in the cloning so insert data in a set and test - std::set - src(testset1.begin(), testset1.end()); - std::set - dst(testset2.begin(), testset2.end()); - BOOST_TEST (src.size() == dst.size() && std::equal(src.begin(), src.end(), dst.begin())); - testset2.clear_and_dispose(test::delete_disposer()); - BOOST_TEST (testset2.empty()); - } - { - //Test with bigger source bucket arrays - typename unordered_set_type::bucket_type buckets1 [BucketSize*2]; - typename unordered_set_type::bucket_type buckets2 [BucketSize]; - unordered_set_type testset1 (values.begin(), values.end(), bucket_traits(buckets1, BucketSize*2)); - unordered_set_type testset2 (bucket_traits(buckets2, BucketSize)); - - testset2.clone_from(testset1, test::new_cloner(), test::delete_disposer()); - //Ordering is not guarantee in the cloning so insert data in a set and test - std::set - src(testset1.begin(), testset1.end()); - std::set - dst(testset2.begin(), testset2.end()); - BOOST_TEST (src.size() == dst.size() && std::equal(src.begin(), src.end(), dst.begin())); - testset2.clear_and_dispose(test::delete_disposer()); - BOOST_TEST (testset2.empty()); - } - { - //Test with smaller source bucket arrays - typename unordered_set_type::bucket_type buckets1 [BucketSize]; - typename unordered_set_type::bucket_type buckets2 [BucketSize*2]; - unordered_set_type testset1 (values.begin(), values.end(), bucket_traits(buckets1, BucketSize)); - unordered_set_type testset2 (bucket_traits(buckets2, BucketSize*2)); - - testset2.clone_from(testset1, test::new_cloner(), test::delete_disposer()); - //Ordering is not guarantee in the cloning so insert data in a set and test - std::set - src(testset1.begin(), testset1.end()); - std::set - dst(testset2.begin(), testset2.end()); - BOOST_TEST (src.size() == dst.size() && std::equal(src.begin(), src.end(), dst.begin())); - testset2.clear_and_dispose(test::delete_disposer()); - BOOST_TEST (testset2.empty()); - } -} - -template -class test_main_template -{ - public: - int operator()() - { - typedef testvalue value_type; - static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; - std::vector > data (6); - for (int i = 0; i < 6; ++i) - data[i].value_ = random_init[i]; - - test_unordered_set < typename detail::get_base_value_traits - < value_type - , typename value_type::unordered_set_base_hook_t - >::type - >::test_all(data); - test_unordered_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::unordered_set_member_hook_t - , &value_type::unordered_set_node_ - > - >::type - >::test_all(data); - - return 0; - } -}; - -template -class test_main_template -{ - public: - int operator()() - { - typedef testvalue value_type; - static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; - std::vector > data (6); - for (int i = 0; i < 6; ++i) - data[i].value_ = random_init[i]; - - test_unordered_set < typename detail::get_base_value_traits - < value_type - , typename value_type::unordered_set_base_hook_t - >::type - >::test_all(data); - - test_unordered_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::unordered_set_member_hook_t - , &value_type::unordered_set_node_ - > - >::type - >::test_all(data); - - test_unordered_set < typename detail::get_base_value_traits - < value_type - , typename value_type::unordered_set_auto_base_hook_t - >::type - >::test_all(data); - - test_unordered_set < typename detail::get_member_value_traits - < value_type - , member_hook< value_type - , typename value_type::unordered_set_auto_member_hook_t - , &value_type::unordered_set_auto_node_ - > - >::type - >::test_all(data); - return 0; - } -}; - -int main( int, char* [] ) -{ - test_main_template()(); - test_main_template, false>()(); - test_main_template()(); - test_main_template, true>()(); - return boost::report_errors(); -} -#include diff --git a/test/virtual_base_test.cpp b/test/virtual_base_test.cpp deleted file mode 100644 index 14bd885..0000000 --- a/test/virtual_base_test.cpp +++ /dev/null @@ -1,85 +0,0 @@ -///////////////////////////////////////////////////////////////////////////// -// -// (C) Copyright Ion Gaztanaga 2007 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org/libs/intrusive for documentation. -// -///////////////////////////////////////////////////////////////////////////// -#include -#include -#include -#include -#include - -using namespace boost::intrusive; - -struct VirtualBase -{ - virtual ~VirtualBase(){} -}; - -struct VirtualBase2 -{ - virtual ~VirtualBase2(){} -}; - -struct VirtualBase3 -{ -}; - -class NonVirtualBase - : public virtual VirtualBase - , public virtual VirtualBase2 -{ - public: - int dummy[10]; -}; - -class MyClass - : public NonVirtualBase - , public virtual VirtualBase3 -{ - int int_; - public: - list_member_hook<> list_hook_; - MyClass(int i = 0) - : int_(i) - {} -}; - -//Define a list that will store MyClass using the public base hook -typedef member_hook< MyClass, list_member_hook<>, &MyClass::list_hook_ > MemberHook; -typedef list List; - -int main() -{ - typedef std::vector::iterator VectIt; - typedef std::vector::reverse_iterator VectRit; - - //Create several MyClass objects, each one with a different value - std::vector values; - for(int i = 0; i < 100; ++i) values.push_back(MyClass(i)); - - List my_list; - - //Now insert them in the reverse order - //in the base hook intrusive list - for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it) - my_list.push_front(*it); - - //Now test lists - { - List::const_iterator list_it(my_list.cbegin()); - VectRit vect_it(values.rbegin()), vect_itend(values.rend()); - - //Test the objects inserted in the base hook list - for(; vect_it != vect_itend; ++vect_it, ++list_it) - if(&*list_it != &*vect_it) return 1; - } - - return 0; -}