From 2d20786a29d3b030402d9f636e6341d9d1c5e9df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Sat, 1 Dec 2007 18:01:15 +0000 Subject: [PATCH 01/20] Simplified mutexes for systems with no _POSIX_TIMEOUTS. [SVN r41540] --- doc/html/reference.css | 8 ++++ example/doc_splay_algorithms.cpp | 3 +- proj/vc7ide/to-do.txt | 7 ---- test/test_templates.hpp | 65 -------------------------------- 4 files changed, 9 insertions(+), 74 deletions(-) delete mode 100644 proj/vc7ide/to-do.txt delete mode 100644 test/test_templates.hpp diff --git a/doc/html/reference.css b/doc/html/reference.css index be4e64c..956d7da 100644 --- a/doc/html/reference.css +++ b/doc/html/reference.css @@ -1,3 +1,11 @@ +/*============================================================================= + 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) +=============================================================================*/ PRE.synopsis { background-color: #e0ffff; border: thin solid blue; diff --git a/example/doc_splay_algorithms.cpp b/example/doc_splay_algorithms.cpp index 5f929fa..80797db 100644 --- a/example/doc_splay_algorithms.cpp +++ b/example/doc_splay_algorithms.cpp @@ -75,5 +75,4 @@ int main() algo::erase(&header, &three); return 0; } - -//] \ No newline at end of file +//] 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/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 From 816685673e5bd456485ebf418218ec241a0e3453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Thu, 13 Dec 2007 16:49:58 +0000 Subject: [PATCH 02/20] Solved warning when using -Wall [SVN r42007] --- include/boost/intrusive/detail/utilities.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/boost/intrusive/detail/utilities.hpp b/include/boost/intrusive/detail/utilities.hpp index 2e018c9..2528aba 100644 --- a/include/boost/intrusive/detail/utilities.hpp +++ b/include/boost/intrusive/detail/utilities.hpp @@ -498,7 +498,8 @@ inline std::size_t floor_log2 (std::size_t x) inline float fast_log2 (float val) { - boost::uint32_t * const exp_ptr = reinterpret_cast (&val); + boost::uint32_t * const exp_ptr = + static_cast (static_cast(&val)); boost::uint32_t x = *exp_ptr; const int log_2 = (int)(((x >> 23) & 255) - 128); x &= ~(255 << 23); From 9f88d4a61706498f956fe345683bdaa98c966296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Sun, 20 Jan 2008 11:54:47 +0000 Subject: [PATCH 03/20] Updated Interprocess and Intrusive: -> Added linear slist to intrusive -> Updated all allocators to version 2 allocators in Interprocess -> Optimized rbtree_best_fit size overhead to 1 std:size_t. [SVN r42878] --- example/doc_list_algorithms.cpp | 2 +- example/doc_slist_algorithms.cpp | 2 +- .../intrusive/circular_list_algorithms.hpp | 109 +++++- .../intrusive/circular_slist_algorithms.hpp | 165 ++++++++- .../intrusive/detail/tree_algorithms.hpp | 4 +- include/boost/intrusive/detail/utilities.hpp | 4 +- include/boost/intrusive/intrusive_fwd.hpp | 1 + .../intrusive/linear_slist_algorithms.hpp | 324 ++++++++++++++++++ include/boost/intrusive/list.hpp | 51 +-- include/boost/intrusive/options.hpp | 27 +- include/boost/intrusive/slist.hpp | 236 ++++++------- include/boost/intrusive/slist_hook.hpp | 2 + .../vc7ide/_intrusivelib/_intrusivelib.vcproj | 3 + test/list_test.cpp | 67 ++-- test/slist_test.cpp | 169 ++++++--- 15 files changed, 888 insertions(+), 278 deletions(-) create mode 100644 include/boost/intrusive/linear_slist_algorithms.hpp diff --git a/example/doc_list_algorithms.cpp b/example/doc_list_algorithms.cpp index 0d1277d..7b4fc89 100644 --- a/example/doc_list_algorithms.cpp +++ b/example/doc_list_algorithms.cpp @@ -38,7 +38,7 @@ int main() //Create an empty doubly linked list container: //"one" will be the first node of the container - algo::init(&one); + algo::init_header(&one); assert(algo::count(&one) == 1); //Now add a new node before "one" diff --git a/example/doc_slist_algorithms.cpp b/example/doc_slist_algorithms.cpp index d13d333..d89b678 100644 --- a/example/doc_slist_algorithms.cpp +++ b/example/doc_slist_algorithms.cpp @@ -36,7 +36,7 @@ int main() //Create an empty singly linked list container: //"one" will be the first node of the container - algo::init(&one); + algo::init_header(&one); assert(algo::count(&one) == 1); //Now add a new node diff --git a/include/boost/intrusive/circular_list_algorithms.hpp b/include/boost/intrusive/circular_list_algorithms.hpp index 5e28e19..7d442b7 100644 --- a/include/boost/intrusive/circular_list_algorithms.hpp +++ b/include/boost/intrusive/circular_list_algorithms.hpp @@ -54,6 +54,27 @@ class circular_list_algorithms typedef typename NodeTraits::const_node_ptr const_node_ptr; typedef NodeTraits node_traits; + //! Effects: Constructs an non-used list element, so that + //! inited(this_node) == true + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init(node_ptr this_node) + { + NodeTraits::set_next(this_node, 0); + NodeTraits::set_previous(this_node, 0); + } + + //! Effects: Returns true is "this_node" is in a non-used state + //! as if it was initialized by the "init" function. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool inited(const_node_ptr this_node) + { return !NodeTraits::get_next(this_node); } + //! Effects: Constructs an empty list, making this_node the only //! node of the circular list: //! NodeTraits::get_next(this_node) == NodeTraits::get_previous(this_node) @@ -62,11 +83,12 @@ class circular_list_algorithms //! Complexity: Constant //! //! Throws: Nothing. - static void init(node_ptr this_node) + static void init_header(node_ptr this_node) { NodeTraits::set_next(this_node, this_node); NodeTraits::set_previous(this_node, this_node); - } + } + //! Requires: this_node must be in a circular list or be an empty circular list. //! @@ -76,8 +98,11 @@ class circular_list_algorithms //! Complexity: Constant //! //! Throws: Nothing. - static bool unique(const_node_ptr this_node) - { return NodeTraits::get_next(this_node) == this_node; } + static bool unique(const_node_ptr this_node) + { + node_ptr next = NodeTraits::get_next(this_node); + return !next || next == this_node; + } //! Requires: this_node must be in a circular list or be an empty circular list. //! @@ -107,11 +132,16 @@ class circular_list_algorithms //! Throws: Nothing. static node_ptr unlink(node_ptr this_node) { - node_ptr next(NodeTraits::get_next(this_node)); - node_ptr prev(NodeTraits::get_previous(this_node)); - NodeTraits::set_next(prev, next); - NodeTraits::set_previous(next, prev); - return next; + if(NodeTraits::get_next(this_node)){ + node_ptr next(NodeTraits::get_next(this_node)); + node_ptr prev(NodeTraits::get_previous(this_node)); + NodeTraits::set_next(prev, next); + NodeTraits::set_previous(next, prev); + return next; + } + else{ + return this_node; + } } //! Requires: b and e must be nodes of the same circular list or an empty range. @@ -229,6 +259,17 @@ class circular_list_algorithms public: static void swap_nodes(node_ptr this_node, node_ptr other_node) { + if (other_node == this_node) + return; + bool this_inited = inited(this_node); + bool other_inited = inited(other_node); + if(this_inited){ + init_header(this_node); + } + if(other_inited){ + init_header(other_node); + } + node_ptr next_this(NodeTraits::get_next(this_node)); node_ptr prev_this(NodeTraits::get_previous(this_node)); node_ptr next_other(NodeTraits::get_next(other_node)); @@ -238,6 +279,13 @@ class circular_list_algorithms swap_next(prev_this, prev_other); swap_next(this_node, other_node); swap_prev(this_node, other_node); + + if(this_inited){ + init(other_node); + } + if(other_inited){ + init(this_node); + } } //! Requires: b and e must be nodes of the same circular list or an empty range. @@ -254,8 +302,8 @@ class circular_list_algorithms { if (b != e) { node_ptr prev_p(NodeTraits::get_previous(p)); - node_ptr prev_e(NodeTraits::get_previous(e)); node_ptr prev_b(NodeTraits::get_previous(b)); + node_ptr prev_e(NodeTraits::get_previous(e)); NodeTraits::set_next(prev_e, p); NodeTraits::set_previous(p, prev_e); NodeTraits::set_next(prev_b, e); @@ -308,6 +356,47 @@ class circular_list_algorithms f = n; } } + + //! Effects: Moves the node p n positions towards the end of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of moved positions. + static void move_backwards(node_ptr p, std::size_t n) + { + //Null shift, nothing to do + if(!n) return; + node_ptr first = NodeTraits::get_next(p); + //size() == 0 or 1, nothing to do + if(first == NodeTraits::get_previous(p)) return; + unlink(p); + //Now get the new first node + while(n--){ + first = NodeTraits::get_next(first); + } + link_before(first, p); + } + + //! Effects: Moves the node p n positions towards the beginning of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of moved positions. + static void move_forward(node_ptr p, std::size_t n) + { + //Null shift, nothing to do + if(!n) return; + node_ptr last = NodeTraits::get_previous(p); + //size() == 0 or 1, nothing to do + if(last == NodeTraits::get_next(p)) return; + + unlink(p); + //Now get the new last node + while(n--){ + last = NodeTraits::get_previous(last); + } + link_after(last, p); + } }; } //namespace intrusive diff --git a/include/boost/intrusive/circular_slist_algorithms.hpp b/include/boost/intrusive/circular_slist_algorithms.hpp index 20dcafa..2768261 100644 --- a/include/boost/intrusive/circular_slist_algorithms.hpp +++ b/include/boost/intrusive/circular_slist_algorithms.hpp @@ -16,6 +16,7 @@ #include #include +#include #include namespace boost { @@ -25,7 +26,7 @@ namespace intrusive { //! forming a circular singly linked list. An empty circular list is formed by a node //! whose pointer to the next node points to itself. //! -//! circular_slist_algorithms is configured with a NodeTraits class, which capsulates the +//! 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: //! @@ -72,11 +73,13 @@ class circular_slist_algorithms //! Throws: Nothing. static node_ptr get_previous_node(node_ptr prev_init_node, node_ptr this_node) { - node_ptr p = prev_init_node; + node_ptr p = prev_init_node; for( node_ptr p_next ; this_node != (p_next = NodeTraits::get_next(p)) ; p = p_next){ - //empty + //Logic error: possible use of linear lists with + //operations only permitted with circular lists + BOOST_INTRUSIVE_INVARIANT_ASSERT(p); } return p; } @@ -115,25 +118,46 @@ class circular_slist_algorithms //! Effects: Constructs an empty list, making this_node the only //! node of the circular list: - //! NodeTraits::get_next(this_node) == NodeTraits::get_previous(this_node) - //! == this_node. + //! NodeTraits::get_next(this_node) == this_node. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init_header(node_ptr this_node) + { NodeTraits::set_next(this_node, this_node); } + + //! Effects: Constructs an non-used list element, putting the next + //! pointer to null: + //! NodeTraits::get_next(this_node) == 0 //! //! Complexity: Constant //! //! Throws: Nothing. static void init(node_ptr this_node) - { NodeTraits::set_next(this_node, this_node); } + { NodeTraits::set_next(this_node, 0); } //! Requires: this_node must be in a circular list or be an empty circular list. //! //! Effects: Returns true is "this_node" is the only node of a circular list: - //! return NodeTraits::get_next(this_node) == this_node + //! or it's a not inserted node: + //! return !NodeTraits::get_next(this_node) || NodeTraits::get_next(this_node) == this_node or //! //! Complexity: Constant //! //! Throws: Nothing. - static bool unique(const_node_ptr this_node) - { return NodeTraits::get_next(this_node) == this_node; } + static bool unique(const_node_ptr this_node) + { + node_ptr next = NodeTraits::get_next(this_node); + return !next || next == this_node; + } + + //! Effects: Returns true is "this_node" has the same state as if it was inited using "init(node_ptr)" + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool inited(const_node_ptr this_node) + { return !NodeTraits::get_next(this_node); } //! Requires: this_node must be in a circular list or be an empty circular list. //! @@ -165,7 +189,7 @@ class circular_slist_algorithms { node_ptr this_node(NodeTraits::get_next(prev_node)); NodeTraits::set_next(prev_node, NodeTraits::get_next(this_node)); - NodeTraits::set_next(this_node, this_node); + //NodeTraits::set_next(this_node, this_node); } //! Requires: nxt_node must be in a circular list or be an empty circular list. @@ -181,7 +205,7 @@ class circular_slist_algorithms unlink_after(prev_to_erase); } - //! Requires: this_node must be in a circular list or be an empty circular list. + //! Requires: this_node must be in a circular list, be an empty circular list or be inited. //! //! Effects: Unlinks the node from the circular list. //! @@ -189,7 +213,10 @@ class circular_slist_algorithms //! //! Throws: Nothing. static void unlink(node_ptr this_node) - { unlink_after(get_previous_node(this_node)); } + { + if(NodeTraits::get_next(this_node)) + unlink_after(get_previous_node(this_node)); + } //! Requires: prev_node must be a node of a circular list. //! @@ -200,8 +227,7 @@ class circular_slist_algorithms //! Throws: Nothing. static void link_after(node_ptr prev_node, node_ptr this_node) { - node_ptr this_nxt = NodeTraits::get_next(prev_node); - NodeTraits::set_next(this_node, this_nxt); + NodeTraits::set_next(this_node, NodeTraits::get_next(prev_node)); NodeTraits::set_next(prev_node, this_node); } @@ -229,6 +255,15 @@ class circular_slist_algorithms { if (other_node == this_node) return; + bool this_inited = inited(this_node); + bool other_inited = inited(other_node); + if(this_inited){ + init_header(this_node); + } + if(other_inited){ + init_header(other_node); + } + bool empty1 = unique(this_node); bool empty2 = unique(other_node); node_ptr prev_this (get_previous_node(this_node)); @@ -240,12 +275,19 @@ class circular_slist_algorithms NodeTraits::set_next(other_node, this_next); NodeTraits::set_next(empty1 ? other_node : prev_this, other_node); NodeTraits::set_next(empty2 ? this_node : prev_other, this_node); + + if(this_inited){ + init(other_node); + } + if(other_inited){ + init(this_node); + } } //! Requires: b and e must be nodes of the same circular list or an empty range. //! and p must be a node of a different circular list. //! - //! Effects: Removes the nodes from [b, e) range from their circular list and inserts + //! Effects: Removes the nodes from (b, e] range from their circular list and inserts //! them after p in p's circular list. //! //! Complexity: Constant @@ -278,6 +320,99 @@ class circular_slist_algorithms transfer_after(e, i, nxt); } } + + //! Effects: Moves the node p n positions towards the end of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements plus the number moved positions. + static void move_backwards(node_ptr p, std::size_t n) + { + //Null shift, nothing to do + if(!n) return; + node_ptr first = NodeTraits::get_next(p); + + //count() == 1 or 2, nothing to do + if(NodeTraits::get_next(first) == p) + return; + + bool end_found = false; + node_ptr new_last(0); + + //Now find the new last node according to the shift count. + //If we find p before finding the new last node + //unlink p, shortcut the search now that we know the size of the list + //and continue. + for(std::size_t i = 1; i <= n; ++i){ + new_last = first; + first = NodeTraits::get_next(first); + if(first == p){ + //Shortcut the shift with the modulo of the size of the list + n %= i; + if(!n) + return; + i = 0; + //Unlink p and continue the new first node search + first = NodeTraits::get_next(p); + unlink_after(new_last); + end_found = true; + } + } + + //If the p has not been found in the previous loop, find it + //starting in the new first node and unlink it + if(!end_found){ + unlink_after(get_previous_node(first, p)); + } + + //Now link p after the new last node + link_after(new_last, p); + } + + //! Effects: Moves the node p n positions towards the beginning of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements plus the number moved positions. + static void move_forward(node_ptr p, std::size_t n) + { + //Null shift, nothing to do + if(!n) return; + node_ptr first = node_traits::get_next(p); + + //count() == 1 or 2, nothing to do + if(node_traits::get_next(first) == p) return; + + //Iterate until p is found to know where the current last node is. + //If the shift count is less than the size of the list, we can also obtain + //the position of the new last node after the shift. + node_ptr old_last(first), next_to_it, new_last(p); + std::size_t distance = 1; + while(p != (next_to_it = node_traits::get_next(old_last))){ + if(++distance > n) + new_last = node_traits::get_next(new_last); + old_last = next_to_it; + } + //If the shift was bigger or equal than the size, obtain the equivalent + //forward shifts and find the new last node. + if(distance <= n){ + //Now find the equivalent forward shifts. + //Shortcut the shift with the modulo of the size of the list + std::size_t new_before_last_pos = (distance - (n % distance))% distance; + //If the shift is a multiple of the size there is nothing to do + if(!new_before_last_pos) return; + + for( new_last = p + ; new_before_last_pos-- + ; new_last = node_traits::get_next(new_last)){ + //empty + } + } + + //Now unlink p and link it after the new last node + unlink_after(old_last); + link_after(new_last, p); + } }; } //namespace intrusive diff --git a/include/boost/intrusive/detail/tree_algorithms.hpp b/include/boost/intrusive/detail/tree_algorithms.hpp index 1a6c045..7cf320c 100644 --- a/include/boost/intrusive/detail/tree_algorithms.hpp +++ b/include/boost/intrusive/detail/tree_algorithms.hpp @@ -137,9 +137,9 @@ class tree_algorithms { return uncast(header); } //! Requires: node is a node of the tree or an node initialized - //! by init(...). + //! by init(...) or init_node. //! - //! Effects: Returns true if the node is initialized by init(). + //! Effects: Returns true if the node is initialized by init() or init_node(). //! //! Complexity: Constant time. //! diff --git a/include/boost/intrusive/detail/utilities.hpp b/include/boost/intrusive/detail/utilities.hpp index 2528aba..e8b3bb3 100644 --- a/include/boost/intrusive/detail/utilities.hpp +++ b/include/boost/intrusive/detail/utilities.hpp @@ -498,8 +498,8 @@ inline std::size_t floor_log2 (std::size_t x) inline float fast_log2 (float val) { - boost::uint32_t * const exp_ptr = - static_cast (static_cast(&val)); + boost::uint32_t * exp_ptr = + static_cast(static_cast(&val)); boost::uint32_t x = *exp_ptr; const int log_2 = (int)(((x >> 23) & 255) - 128); x &= ~(255 << 23); diff --git a/include/boost/intrusive/intrusive_fwd.hpp b/include/boost/intrusive/intrusive_fwd.hpp index 67d50a7..65fe353 100644 --- a/include/boost/intrusive/intrusive_fwd.hpp +++ b/include/boost/intrusive/intrusive_fwd.hpp @@ -69,6 +69,7 @@ template , class O1 = none , class O2 = none , class O3 = none + , class O4 = none > class slist; diff --git a/include/boost/intrusive/linear_slist_algorithms.hpp b/include/boost/intrusive/linear_slist_algorithms.hpp new file mode 100644 index 0000000..46595e3 --- /dev/null +++ b/include/boost/intrusive/linear_slist_algorithms.hpp @@ -0,0 +1,324 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (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_LINEAR_SLIST_ALGORITHMS_HPP +#define BOOST_INTRUSIVE_LINEAR_SLIST_ALGORITHMS_HPP + +#include +#include +#include + +namespace boost { +namespace intrusive { + +//! linear_slist_algorithms provides basic algorithms to manipulate nodes +//! forming a linear singly linked list. +//! +//! linear_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 linear list +//! +//! node_ptr: A pointer to a node +//! +//! const_node_ptr: A pointer to a const node +//! +//! Static functions: +//! +//! static node_ptr get_next(const_node_ptr n); +//! +//! static void set_next(node_ptr n, node_ptr next); +template +class linear_slist_algorithms +{ + public: + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + typedef NodeTraits node_traits; + + //! Requires: this_node and prev_init_node must be in the same linear list. + //! + //! Effects: Returns the previous node of this_node in the linear list starting. + //! the search from prev_init_node. The first node checked for equality + //! is NodeTraits::get_next(prev_init_node). + //! + //! Complexity: Linear to the number of elements between prev_init_node and this_node. + //! + //! Throws: Nothing. + static node_ptr get_previous_node(node_ptr prev_init_node, node_ptr this_node) + { + node_ptr p = prev_init_node; + for( node_ptr p_next + ; this_node != (p_next = NodeTraits::get_next(p)) + ; p = p_next){ + //empty + } + return p; + } + + //! Effects: Constructs an empty list, making this_node the only + //! node of the linear list: + //! NodeTraits::get_next(this_node) == 0. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init_header(node_ptr this_node) + { NodeTraits::set_next(this_node, 0); } + + //! Effects: Constructs an non-used list element, putting the next + //! pointer to null: + //! NodeTraits::get_next(this_node) == 0 + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init(node_ptr this_node) + { NodeTraits::set_next(this_node, 0); } + + //! Requires: this_node must be in a linear list or be an empty linear list. + //! + //! Effects: Returns true is "this_node" is the only node of a linear list: + //! return NodeTraits::get_next(this_node) == this_node + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool unique(const_node_ptr this_node) + { + node_ptr next = NodeTraits::get_next(this_node); + return !next || next == this_node; + } + + //! Requires: this_node must be in a linear list or be an empty linear list. + //! + //! Effects: Returns true is "this_node" is the only node of a linear list: + //! return NodeTraits::get_next(this_node) == this_node + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool inited(const_node_ptr this_node) + { return !NodeTraits::get_next(this_node); } + + //! Requires: this_node must be in a linear list or be an empty linear list. + //! + //! Effects: Returns the number of nodes in a linear list. If the linear list + //! is empty, returns 1. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + 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); + return result; + } + + //! Requires: prev_node must be in a linear list or be an empty linear list. + //! + //! Effects: Unlinks the next node of prev_node from the linear list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void unlink_after(node_ptr prev_node) + { + node_ptr this_node(NodeTraits::get_next(prev_node)); + NodeTraits::set_next(prev_node, NodeTraits::get_next(this_node)); + } + + //! Requires: prev_node must be a node of a linear list. + //! + //! Effects: Links this_node after prev_node in the linear list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void link_after(node_ptr prev_node, node_ptr this_node) + { + NodeTraits::set_next(this_node, NodeTraits::get_next(prev_node)); + NodeTraits::set_next(prev_node, this_node); + } + + //! Requires: this_node and other_node must be nodes inserted + //! in linear lists or be empty linear lists. + //! + //! Effects: Moves all the nodes previously chained after this_node after other_node + //! and vice-versa. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void swap_trailing_nodes(node_ptr this_node, node_ptr other_node) + { + node_ptr this_nxt = NodeTraits::get_next(this_node); + node_ptr other_nxt = NodeTraits::get_next(other_node); + NodeTraits::set_next(this_node, other_nxt); + NodeTraits::set_next(other_node, this_nxt); + } + + //! Requires: b and e must be nodes of the same linear list or an empty range. + //! and p must be a node of a different linear list. + //! + //! Effects: Removes the nodes from (b, e] range from their linear list and inserts + //! them after p in p's linear list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void transfer_after(node_ptr p, node_ptr b, node_ptr e) + { + if (p != b && p != e) { + node_ptr next_b = NodeTraits::get_next(b); + node_ptr next_e = NodeTraits::get_next(e); + node_ptr next_p = NodeTraits::get_next(p); + NodeTraits::set_next(b, next_e); + NodeTraits::set_next(e, next_p); + NodeTraits::set_next(p, next_b); + } + } + + //! Effects: Reverses the order of elements in the list. + //! + //! Returns: The new first node of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: This function is linear to the contained elements. + static node_ptr reverse(node_ptr p) + { + if(!p) return 0; + node_ptr i = NodeTraits::get_next(p); + node_ptr first(p); + while(i){ + node_ptr nxti(NodeTraits::get_next(i)); + unlink_after(p); + NodeTraits::set_next(i, first); + first = i; + i = nxti; + } + return first; + } + + //! Effects: Moves the node p n positions towards the end of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements plus the number moved positions. + static node_ptr move_backwards(node_ptr p, std::size_t n) + { + //Null shift, or count() == 0 or 1, nothing to do + if(!n || !p || !NodeTraits::get_next(p)) + return p; + + node_ptr first = p; + bool end_found = false; + node_ptr new_last(0); + node_ptr old_last(0); + + //Now find the new last node according to the shift count. + //If we find 0 before finding the new last node + //unlink p, shortcut the search now that we know the size of the list + //and continue. + for(std::size_t i = 1; i <= n; ++i){ + new_last = first; + first = NodeTraits::get_next(first); + if(first == 0){ + //Shortcut the shift with the modulo of the size of the list + n %= i; + if(!n) return p; + old_last = new_last; + i = 0; + //Unlink p and continue the new first node search + first = p; + //unlink_after(new_last); + end_found = true; + } + } + + //If the p has not been found in the previous loop, find it + //starting in the new first node and unlink it + if(!end_found){ + old_last = get_previous_node(first, 0); + } + + //Now link p after the new last node + NodeTraits::set_next(old_last, p); + NodeTraits::set_next(new_last, 0); + return first; + } + + //! Effects: Moves the node p n positions towards the beginning of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of elements plus the number moved positions. + static node_ptr move_forward(node_ptr p, std::size_t n) + { + //Null shift, or count() == 0 or 1, nothing to do + if(!n || !p || !NodeTraits::get_next(p)) + return p; + + node_ptr first = p; + + //Iterate until p is found to know where the current last node is. + //If the shift count is less than the size of the list, we can also obtain + //the position of the new last node after the shift. + node_ptr old_last(first), next_to_it, new_last(p); + std::size_t distance = 1; + while(!!(next_to_it = node_traits::get_next(old_last))){ + if(distance++ > n) + new_last = node_traits::get_next(new_last); + old_last = next_to_it; + } + //If the shift was bigger or equal than the size, obtain the equivalent + //forward shifts and find the new last node. + if(distance <= n){ + //Now find the equivalent forward shifts. + //Shortcut the shift with the modulo of the size of the list + std::size_t new_before_last_pos = (distance - (n % distance))% distance; + //If the shift is a multiple of the size there is nothing to do + if(!new_before_last_pos) + return p; + + for( new_last = p + ; --new_before_last_pos + ; new_last = node_traits::get_next(new_last)){ + //empty + } + } + + //Get the first new node + node_ptr new_first = node_traits::get_next(new_last); + //Now put the old beginning after the old end + NodeTraits::set_next(old_last, p); + NodeTraits::set_next(new_last, 0); + return new_first; + } +}; + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_LINEAR_SLIST_ALGORITHMS_HPP diff --git a/include/boost/intrusive/list.hpp b/include/boost/intrusive/list.hpp index 3b337d8..77c0c47 100644 --- a/include/boost/intrusive/list.hpp +++ b/include/boost/intrusive/list.hpp @@ -208,7 +208,7 @@ class list_impl : data_(v_traits) { this->priv_size_traits().set_size(size_type(0)); - node_algorithms::init(this->get_root_node()); + node_algorithms::init_header(this->get_root_node()); } //! Requires: Dereferencing iterator must yield an lvalue of type value_type. @@ -224,7 +224,7 @@ class list_impl : data_(v_traits) { this->priv_size_traits().set_size(size_type(0)); - node_algorithms::init(this->get_root_node()); + node_algorithms::init_header(this->get_root_node()); this->insert(this->end(), b, e); } @@ -258,7 +258,7 @@ class list_impl { node_ptr to_insert = get_real_value_traits().to_node_ptr(value); if(safemode_or_autounlink) - BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(to_insert)); node_algorithms::link_before(this->get_root_node(), to_insert); this->priv_size_traits().increment(); } @@ -277,7 +277,7 @@ class list_impl { node_ptr to_insert = get_real_value_traits().to_node_ptr(value); if(safemode_or_autounlink) - BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(to_insert)); node_algorithms::link_before(node_traits::get_next(this->get_root_node()), to_insert); this->priv_size_traits().increment(); } @@ -569,21 +569,7 @@ class list_impl //! //! Note: Does not affect the validity of iterators and references. void shift_backwards(size_type n = 1) - { - //Null shift, nothing to do - if(!n) return; - node_ptr root = this->get_root_node(); - node_ptr last = node_traits::get_previous(root); - //size() == 0 or 1, nothing to do - if(last == node_traits::get_next(root)) return; - - node_algorithms::unlink(root); - //Now get the new last node - while(n--){ - last = node_traits::get_previous(last); - } - node_algorithms::link_after(last, root); - } + { node_algorithms::move_forward(this->get_root_node(), n); } //! Effects: Moves forward all the elements, so that the second //! element becomes the first, the third becomes the second... @@ -595,20 +581,7 @@ class list_impl //! //! Note: Does not affect the validity of iterators and references. void shift_forward(size_type n = 1) - { - //Null shift, nothing to do - if(!n) return; - node_ptr root = this->get_root_node(); - node_ptr first = node_traits::get_next(root); - //size() == 0 or 1, nothing to do - if(first == node_traits::get_previous(root)) return; - node_algorithms::unlink(root); - //Now get the new first node - while(n--){ - first = node_traits::get_next(first); - } - node_algorithms::link_before(first, root); - } + { node_algorithms::move_backwards(this->get_root_node(), n); } //! Effects: Erases the element pointed by i of the list. //! No destructors are called. @@ -729,7 +702,7 @@ class list_impl this->erase(this->begin(), this->end()); } else{ - node_algorithms::init(this->get_root_node()); + node_algorithms::init_header(this->get_root_node()); this->priv_size_traits().set_size(size_type(0)); } } @@ -794,7 +767,7 @@ class list_impl { node_ptr to_insert = get_real_value_traits().to_node_ptr(value); if(safemode_or_autounlink) - BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(to_insert)); node_algorithms::link_before(p.pointed_node(), to_insert); this->priv_size_traits().increment(); return iterator(to_insert, this); @@ -1234,7 +1207,7 @@ class list_impl static iterator s_iterator_to(reference value) { BOOST_STATIC_ASSERT((!stateful_value_traits)); - BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::unique(real_value_traits::to_node_ptr(value))); + BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(real_value_traits::to_node_ptr(value))); return iterator(real_value_traits::to_node_ptr(value), 0); } @@ -1252,7 +1225,7 @@ class list_impl static const_iterator s_iterator_to(const_reference value) { BOOST_STATIC_ASSERT((!stateful_value_traits)); - BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::unique(real_value_traits::to_node_ptr(const_cast (value)))); + BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(real_value_traits::to_node_ptr(const_cast (value)))); return const_iterator(real_value_traits::to_node_ptr(const_cast (value)), 0); } @@ -1267,7 +1240,7 @@ class list_impl //! Note: Iterators and references are not invalidated. iterator iterator_to(reference value) { - BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::unique(real_value_traits::to_node_ptr(value))); + BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(real_value_traits::to_node_ptr(value))); return iterator(real_value_traits::to_node_ptr(value), this); } @@ -1282,7 +1255,7 @@ class list_impl //! Note: Iterators and references are not invalidated. const_iterator iterator_to(const_reference value) const { - BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::unique(real_value_traits::to_node_ptr(const_cast (value)))); + BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(real_value_traits::to_node_ptr(const_cast (value)))); return const_iterator(real_value_traits::to_node_ptr(const_cast (value)), this); } diff --git a/include/boost/intrusive/options.hpp b/include/boost/intrusive/options.hpp index 3ced4ea..cda392e 100644 --- a/include/boost/intrusive/options.hpp +++ b/include/boost/intrusive/options.hpp @@ -318,10 +318,8 @@ struct tag /// @endcond }; -//!This option setter specifies the type of -//!a void pointer. This will instruct the hook -//!to use this type of pointer instead of the -//!default one +//!This option setter specifies the link mode +//!(normal_link, safe_link or auto_unlink) template struct link_mode { @@ -334,10 +332,8 @@ struct link_mode /// @endcond }; -//!This option setter specifies the type of -//!a void pointer. This will instruct the hook -//!to use this type of pointer instead of the -//!default one +//!This option setter specifies if the hook +//!should be optimized for size instead of for speed. template struct optimize_size { @@ -350,6 +346,20 @@ struct optimize_size /// @endcond }; +//!This option setter specifies if the list container should +//!use a linear implementation instead of a circular one. +template +struct linear +{ +/// @cond + template + struct pack : Base + { + static const bool linear = Enabled; + }; +/// @endcond +}; + //!This option setter specifies the bucket traits //!class for unordered associative containers. When this option is specified, //!instead of using the default bucket traits, a user defined holder will be defined @@ -475,6 +485,7 @@ struct hook_defaults , tag , optimize_size , store_hash + , linear >::type {}; diff --git a/include/boost/intrusive/slist.hpp b/include/boost/intrusive/slist.hpp index dadd25b..ba0240a 100644 --- a/include/boost/intrusive/slist.hpp +++ b/include/boost/intrusive/slist.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -46,12 +47,13 @@ template struct get_default_slist_hook { typedef typename T::default_slist_hook type; }; -template +template struct slistopt { typedef ValueTraits value_traits; typedef SizeType size_type; static const bool constant_time_size = ConstantTimeSize; + static const bool linear = Linear; }; template @@ -66,6 +68,7 @@ struct slist_defaults >::type > , constant_time_size + , linear , size_type >::type {}; @@ -129,10 +132,15 @@ class slist_impl ::type node_ptr; typedef typename boost::pointer_to_other ::type const_node_ptr; - typedef circular_slist_algorithms node_algorithms; + typedef typename detail::if_c + < Config::linear + , linear_slist_algorithms + , circular_slist_algorithms + >::type node_algorithms; static const bool constant_time_size = Config::constant_time_size; static const bool stateful_value_traits = detail::store_cont_ptr_on_it::value; + static const bool linear = Config::linear; /// @cond private: @@ -152,6 +160,8 @@ class slist_impl //Constant-time size is incompatible with auto-unlink hooks! BOOST_STATIC_ASSERT(!(constant_time_size && ((int)real_value_traits::link_mode == (int)auto_unlink))); + //Linear singly linked lists are incompatible with auto-unlink hooks! + BOOST_STATIC_ASSERT(!(linear && ((int)real_value_traits::link_mode == (int)auto_unlink))); node_ptr get_root_node() { return node_ptr(&data_.root_plus_size_.root_); } @@ -220,7 +230,7 @@ class slist_impl : data_(v_traits) { this->priv_size_traits().set_size(size_type(0)); - node_algorithms::init(this->get_root_node()); + node_algorithms::init_header(this->get_root_node()); } //! Requires: Dereferencing iterator must yield an lvalue of type value_type. @@ -236,7 +246,7 @@ class slist_impl : data_(v_traits) { this->priv_size_traits().set_size(size_type(0)); - node_algorithms::init(this->get_root_node()); + node_algorithms::init_header(this->get_root_node()); insert_after(before_begin(), b, e); } @@ -266,7 +276,7 @@ class slist_impl this->erase_after(this->before_begin(), this->end()); } else{ - node_algorithms::init(this->get_root_node()); + node_algorithms::init_header(this->get_root_node()); this->priv_size_traits().set_size(size_type(0)); } } @@ -299,7 +309,7 @@ class slist_impl { node_ptr to_insert = get_real_value_traits().to_node_ptr(value); if(safemode_or_autounlink) - BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(to_insert)); + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(to_insert)); node_algorithms::link_after(this->get_root_node(), to_insert); this->priv_size_traits().increment(); } @@ -385,7 +395,7 @@ class slist_impl //! //! Complexity: Constant. iterator end() - { return iterator (this->get_root_node(), this); } + { return iterator (linear ? 0 : this->get_root_node(), this); } //! Effects: Returns a const_iterator to the end of the list. //! @@ -393,7 +403,7 @@ class slist_impl //! //! Complexity: Constant. const_iterator end() const - { return const_iterator (uncast(this->get_root_node()), this); } + { return const_iterator (linear ? 0 : uncast(this->get_root_node()), this); } //! Effects: Returns a const_iterator to the end of the list. //! @@ -401,7 +411,7 @@ class slist_impl //! //! Complexity: Constant. const_iterator cend() const - { return const_iterator (uncast(this->get_root_node()), this); } + { return this->end(); } //! Effects: Returns an iterator that points to a position //! before the first element. Equivalent to "end()" @@ -410,7 +420,7 @@ class slist_impl //! //! Complexity: Constant. iterator before_begin() - { return end(); } + { return iterator(this->get_root_node(), this); } //! Effects: Returns an iterator that points to a position //! before the first element. Equivalent to "end()" @@ -419,7 +429,7 @@ class slist_impl //! //! Complexity: Constant. const_iterator before_begin() const - { return end(); } + { return const_iterator(uncast(this->get_root_node()), this); } //! Effects: Returns an iterator that points to a position //! before the first element. Equivalent to "end()" @@ -428,7 +438,7 @@ class slist_impl //! //! Complexity: Constant. const_iterator cbefore_begin() const - { return end(); } + { return this->before_begin(); } //! Precondition: end_iterator must be a valid end iterator //! of slist. @@ -487,7 +497,7 @@ class slist_impl //! Note: Does not affect the validity of iterators and references. void swap(slist_impl& other) { - node_algorithms::swap_nodes(this->get_root_node(), other.get_root_node()); + priv_swap_lists(this->get_root_node(), other.get_root_node(), detail::bool_()); if(constant_time_size){ size_type backup = this->priv_size_traits().get_size(); this->priv_size_traits().set_size(other.priv_size_traits().get_size()); @@ -506,43 +516,7 @@ class slist_impl //! Note: Iterators Does not affect the validity of iterators and references. void shift_backwards(size_type n = 1) { - //Null shift, nothing to do - if(!n) return; - node_ptr root = this->get_root_node(); - node_ptr first = node_traits::get_next(root); - - //size() == 0 or 1, nothing to do - if(node_traits::get_next(first) == root) return; - - //Iterate until the root node is found to know where the current last node is. - //If the shift count is less than the size of the list, we can also obtain - //the position of the new last node after the shift. - node_ptr old_last(first), next_to_it, new_last(root); - size_type distance = 1; - while(root != (next_to_it = node_traits::get_next(old_last))){ - if(++distance > n) - new_last = node_traits::get_next(new_last); - old_last = next_to_it; - } - //If the shift was bigger or equal than the size, obtain the equivalent - //forward shifts and find the new last node. - if(distance <= n){ - //Now find the equivalent forward shifts. - //Shorcut the shift with the modulo of the size of the list - size_type new_before_last_pos = (distance - (n % distance))% distance; - //If the shift is a multiple of the size there is nothing to do - if(!new_before_last_pos) return; - - for( new_last = root - ; new_before_last_pos-- - ; new_last = node_traits::get_next(new_last)){ - //empty - } - } - - //Now unlink the root node and link it after the new last node - node_algorithms::unlink_after(old_last); - node_algorithms::link_after(new_last, root); + priv_shift_backwards(n, detail::bool_()); } //! Effects: Moves forward all the elements, so that the second @@ -556,43 +530,7 @@ class slist_impl //! Note: Does not affect the validity of iterators and references. void shift_forward(size_type n = 1) { - //Null shift, nothing to do - if(!n) return; - node_ptr root = this->get_root_node(); - node_ptr first = node_traits::get_next(root); - - //size() == 0 or 1, nothing to do - if(node_traits::get_next(first) == root) return; - - bool end_found = false; - node_ptr new_last(0); - - //Now find the new last node according to the shift count. - //If we find the root node before finding the new last node - //unlink the root, shortcut the search now that we know the size of the list - //and continue. - for(size_type i = 1; i <= n; ++i){ - new_last = first; - first = node_traits::get_next(first); - if(first == root){ - //Shorcut the shift with the modulo of the size of the list - n %= i; - i = 0; - //Unlink the root node and continue the new first node search - first = node_traits::get_next(first); - node_algorithms::unlink_after(new_last); - end_found = true; - } - } - - //If the root node has not been found in the previous loop, find it - //starting in the new first node and unlink it - if(!end_found){ - node_algorithms::unlink_after(node_algorithms::get_previous_node(first, root)); - } - - //Now link the root node after the new last node - node_algorithms::link_after(new_last, root); + priv_shift_forward(n, detail::bool_()); } //! Requires: Disposer::operator()(pointer) shouldn't throw. @@ -643,7 +581,7 @@ class slist_impl { node_ptr n = get_real_value_traits().to_node_ptr(value); if(safemode_or_autounlink) - BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n)); + BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(n)); node_algorithms::link_after(prev_p.pointed_node(), n); this->priv_size_traits().increment(); return iterator (n, this); @@ -712,16 +650,7 @@ class slist_impl //! Note: Invalidates the iterators (but not the references) to the //! erased element. iterator erase_after(iterator prev) - { - iterator it(prev); ++it; - node_ptr to_erase(it.pointed_node()); - node_algorithms::unlink_after(prev.pointed_node()); - this->priv_size_traits().decrement(); - iterator ret(++prev); - if(safemode_or_autounlink) - node_algorithms::init(to_erase); - return ret; - } + { return this->erase_after_and_dispose(prev, detail::null_disposer()); } //! Effects: Erases the range (before_first, last) from //! the list. No destructors are called. @@ -731,18 +660,12 @@ class slist_impl //! //! Throws: Nothing. //! - //! Complexity: Lineal to the elements (last - before_first). + //! Complexity: Lineal to the elements (last - before_first + 1). //! //! Note: Invalidates the iterators (but not the references) to the //! erased element. iterator erase_after(iterator before_first, iterator last) - { - iterator first; - while(++(first = before_first) != last){ - this->erase_after(before_first); - } - return last; - } + { return this->erase_after_and_dispose(before_first, last, detail::null_disposer()); } //! Effects: Erases the element pointed by i of the list. //! No destructors are called. @@ -794,11 +717,16 @@ class slist_impl template iterator erase_after_and_dispose(iterator prev, Disposer disposer) { - iterator it(prev); ++it; + iterator it(prev); + ++it; node_ptr to_erase(it.pointed_node()); - iterator ret(this->erase_after(prev)); + ++it; + node_algorithms::unlink_after(prev.pointed_node()); + this->priv_size_traits().decrement(); + if(safemode_or_autounlink) + node_algorithms::init(to_erase); disposer(get_real_value_traits().to_value_ptr(to_erase)); - return ret; + return it; } //! Requires: Disposer::operator()(pointer) shouldn't throw. @@ -818,9 +746,10 @@ class slist_impl template iterator erase_after_and_dispose(iterator before_first, iterator last, Disposer disposer) { - iterator first; - while(++(first = before_first) != last){ - this->erase_after_and_dispose(before_first, disposer); + iterator next(before_first); + ++next; + while(next != last){ + next = this->erase_after_and_dispose(before_first, disposer); } return last; } @@ -931,7 +860,7 @@ class slist_impl iterator last_x(x.previous(x.end())); node_algorithms::transfer_after ( prev.pointed_node() - , x.end().pointed_node() + , x.before_begin().pointed_node() , last_x.pointed_node()); this->priv_size_traits().set_size(this->priv_size_traits().get_size() + x.priv_size_traits().get_size()); x.priv_size_traits().set_size(size_type(0)); @@ -1133,12 +1062,12 @@ class slist_impl (last_inserted.pointed_node(), carry.end().pointed_node()); iterator last_element(p, this); if(constant_time_size){ - counter[i].splice_after( counter[i].end(), carry + counter[i].splice_after( counter[i].before_begin(), carry , carry.before_begin(), last_element , carry.size()); } else{ - counter[i].splice_after( counter[i].end(), carry + counter[i].splice_after( counter[i].before_begin(), carry , carry.before_begin(), last_element); } if(i == fill) @@ -1153,11 +1082,11 @@ class slist_impl (last_inserted.pointed_node(), counter[--fill].end().pointed_node()); iterator last_element(p, this); if(constant_time_size){ - this->splice_after( end(), counter[fill], counter[fill].before_begin() + this->splice_after( before_begin(), counter[fill], counter[fill].before_begin() , last_element, counter[fill].size()); } else{ - this->splice_after( end(), counter[fill], counter[fill].before_begin() + this->splice_after( before_begin(), counter[fill], counter[fill].before_begin() , last_element); } } @@ -1201,14 +1130,14 @@ class slist_impl template iterator merge(slist_impl& x, Predicate p) { - iterator a(before_begin()), e(end()), ax(x.before_begin()); + iterator a(before_begin()), e(end()), ax(x.before_begin()), ex(x.end()); iterator last_inserted(e); iterator a_next; while(++(a_next = a) != e && !x.empty()) { iterator ix(ax); iterator cx; size_type n(0); - while(++(cx = ix) != ax && p(*cx, *a_next)){ + while(++(cx = ix) != ex && p(*cx, *a_next)){ ++ix; ++n; } if(ax != ix){ @@ -1235,7 +1164,7 @@ class slist_impl //! //! Note: Iterators and references are not invalidated void merge(slist_impl& x) - { this->merge(x, std::less()); } + { this->merge(x, std::less()); } //! Effects: Reverses the order of elements in the list. //! @@ -1245,7 +1174,7 @@ class slist_impl //! //! Note: Iterators and references are not invalidated void reverse() - { node_algorithms::reverse(this->get_root_node()); } + { priv_reverse(detail::bool_()); } //! Effects: Removes all the elements that compare equal to value. //! No destructors are called. @@ -1406,7 +1335,7 @@ class slist_impl static iterator s_iterator_to(reference value) { BOOST_STATIC_ASSERT((!stateful_value_traits)); - BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::unique(value_traits::to_node_ptr(value))); + //BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::inited(value_traits::to_node_ptr(value))); return iterator (value_traits::to_node_ptr(value), 0); } @@ -1424,7 +1353,7 @@ class slist_impl static const_iterator s_iterator_to(const_reference value) { BOOST_STATIC_ASSERT((!stateful_value_traits)); - BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::unique(value_traits::to_node_ptr(const_cast (value)))); + //BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::inited(value_traits::to_node_ptr(const_cast (value)))); return const_iterator (value_traits::to_node_ptr(const_cast (value)), 0); } @@ -1439,7 +1368,7 @@ class slist_impl //! Note: Iterators and references are not invalidated. iterator iterator_to(reference value) { - BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::unique(value_traits::to_node_ptr(value))); + //BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::inited(value_traits::to_node_ptr(value))); return iterator (value_traits::to_node_ptr(value), this); } @@ -1454,7 +1383,7 @@ class slist_impl //! Note: Iterators and references are not invalidated. const_iterator iterator_to(const_reference value) const { - BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::unique(value_traits::to_node_ptr(const_cast (value)))); + //BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::inited(value_traits::to_node_ptr(const_cast (value)))); return const_iterator (value_traits::to_node_ptr(const_cast (value)), this); } @@ -1487,8 +1416,54 @@ class slist_impl } private: + + void priv_reverse(detail::bool_) + { node_algorithms::reverse(this->get_root_node()); } + + void priv_reverse(detail::bool_) + { + node_ptr new_first = node_algorithms::reverse + (node_traits::get_next(this->get_root_node())); + node_traits::set_next(this->get_root_node(), new_first); + } + + void priv_shift_backwards(size_type n, detail::bool_) + { + node_algorithms::move_forward(this->get_root_node(), (std::size_t)n); + } + + void priv_shift_backwards(size_type n, detail::bool_) + { + node_ptr new_first = node_algorithms::move_forward + (node_traits::get_next(this->get_root_node()), (std::size_t)n); + node_traits::set_next(this->get_root_node(), new_first); + } + + void priv_shift_forward(size_type n, detail::bool_) + { + node_algorithms::move_backwards(this->get_root_node(), (std::size_t)n); + } + + void priv_shift_forward(size_type n, detail::bool_) + { + node_ptr new_first = node_algorithms::move_backwards + (node_traits::get_next(this->get_root_node()), (std::size_t)n); + node_traits::set_next(this->get_root_node(), new_first); + } + + //circular version + static void priv_swap_lists(node_ptr this_node, node_ptr other_node, detail::bool_) + { node_algorithms::swap_nodes(this_node, other_node); } + + //linear version + static void priv_swap_lists(node_ptr this_node, node_ptr other_node, detail::bool_) + { node_algorithms::swap_trailing_nodes(this_node, other_node); } + static slist_impl &priv_container_from_end_iterator(const const_iterator &end_iterator) { + //Obtaining the container from the end iterator is not possible with linear + //singly linked lists (because "end" is represented by the null pointer) + BOOST_STATIC_ASSERT(!linear); root_plus_size *r = detail::parent_from_member ( detail::get_pointer(end_iterator.pointed_node()), &root_plus_size::root_); data_t *d = detail::parent_from_member @@ -1620,13 +1595,13 @@ inline void swap #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED template #else -template +template #endif struct make_slist { /// @cond typedef typename pack_options - < slist_defaults, O1, O2, O3>::type packed_options; + < slist_defaults, O1, O2, O3, O4>::type packed_options; typedef typename detail::get_value_traits ::type value_traits; typedef slist_impl @@ -1635,6 +1610,7 @@ struct make_slist < value_traits , typename packed_options::size_type , packed_options::constant_time_size + , packed_options::linear > > implementation_defined; /// @endcond @@ -1643,12 +1619,12 @@ struct make_slist #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED -template +template class slist - : public make_slist::type + : public make_slist::type { typedef typename make_slist - ::type Base; + ::type Base; typedef typename Base::real_value_traits real_value_traits; //Assert if passed value traits are compatible with the type BOOST_STATIC_ASSERT((detail::is_same::value)); diff --git a/include/boost/intrusive/slist_hook.hpp b/include/boost/intrusive/slist_hook.hpp index 547ca6f..b7d5344 100644 --- a/include/boost/intrusive/slist_hook.hpp +++ b/include/boost/intrusive/slist_hook.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -31,6 +32,7 @@ struct get_slist_node_algo { typedef circular_slist_algorithms > type; }; + /// @endcond //! Helper metafunction to define a \c slist_base_hook that yields to the same diff --git a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj index 9b36d35..9b2181d 100644 --- a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj +++ b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj @@ -135,6 +135,9 @@ + + diff --git a/test/list_test.cpp b/test/list_test.cpp index 7b28289..822b408 100644 --- a/test/list_test.cpp +++ b/test/list_test.cpp @@ -63,14 +63,6 @@ void test_list::test_all(std::vector 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); + for(int s = 1; s <= num_values; ++s){ + expected_values.resize(s); + //Shift forward all possible positions 3 times + for(int i = 0; i < s*3; ++i){ + testlist.insert(testlist.begin(), &values[0], &values[0] + s); + testlist.shift_forward(i); + for(int j = 0; j < s; ++j){ + expected_values[(j + s - i%s) % s] = (j + 1); + } + TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin()); + testlist.clear(); } - 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); + //Shift backwards all possible positions + for(int i = 0; i < s*3; ++i){ + testlist.insert(testlist.begin(), &values[0], &values[0] + s); + testlist.shift_backwards(i); + for(int j = 0; j < s; ++j){ + expected_values[(j + i) % s] = (j + 1); + } + TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin()); + testlist.clear(); } - TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin()); - testlist.clear(); } } @@ -278,6 +273,28 @@ void test_list { int init_values [] = { 4, 3 }; TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } } + { + list_type testlist1 (&values[0], &values[1]); + + { int init_values [] = { 1 }; + TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } + + values[1].swap_nodes(values[2]); + + { int init_values [] = { 1 }; + TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } + + values[0].swap_nodes(values[2]); + + { int init_values [] = { 3 }; + TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } + + values[0].swap_nodes(values[2]); + + { int init_values [] = { 1 }; + TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } + } + } template diff --git a/test/slist_test.cpp b/test/slist_test.cpp index 06fc38c..698fea8 100644 --- a/test/slist_test.cpp +++ b/test/slist_test.cpp @@ -24,7 +24,7 @@ using namespace boost::intrusive; -template +template struct test_slist { typedef typename ValueTraits::value_type value_type; @@ -37,11 +37,12 @@ struct test_slist 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); + static void test_container_from_end(std::vector &, detail::bool_){} + static void test_container_from_end(std::vector &values, detail::bool_); }; -template -void test_slist +template +void test_slist ::test_all (std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -50,6 +51,7 @@ void test_slist , value_traits , size_type , constant_time_size + , linear > list_type; { list_type list(values.begin(), values.end()); @@ -66,12 +68,12 @@ void test_slist test_slow_insert (values); test_swap(values); test_clone(values); - test_container_from_end(values); + test_container_from_end(values, detail::bool_()); } //test: push_front, pop_front, front, size, empty: -template -void test_slist +template +void test_slist ::test_front_back (std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -80,6 +82,7 @@ void test_slist , value_traits , size_type , constant_time_size + , linear > list_type; list_type testlist; BOOST_TEST (testlist.empty()); @@ -101,8 +104,8 @@ void test_slist } //test: merge due to error in merge implementation: -template -void test_slist +template +void test_slist ::test_merge (std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -111,6 +114,7 @@ void test_slist , value_traits , size_type , constant_time_size + , linear > list_type; list_type testlist1, testlist2; testlist1.push_front (values[0]); @@ -124,8 +128,8 @@ void test_slist } //test: constructor, iterator, sort, reverse: -template -void test_slist +template +void test_slist ::test_sort(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -134,6 +138,7 @@ void test_slist , value_traits , size_type , constant_time_size + , linear > list_type; list_type testlist (values.begin(), values.end()); @@ -145,13 +150,13 @@ void test_slist TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); } testlist.reverse(); - { int init_values [] = { 5, 3, 1, 4, 2, }; + { 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 +template +void test_slist ::test_insert(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -160,6 +165,7 @@ void test_slist , value_traits , size_type , constant_time_size + , linear > list_type; list_type testlist; testlist.assign (&values[0] + 2, &values[0] + 5); @@ -189,8 +195,8 @@ void test_slist } //test: insert, const_iterator, erase, siterator_to: -template -void test_slist +template +void test_slist ::test_slow_insert (std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -199,6 +205,7 @@ void test_slist , value_traits , size_type , constant_time_size + , linear > list_type; list_type testlist; testlist.push_front (values[4]); @@ -232,8 +239,8 @@ void test_slist BOOST_TEST (testlist.front().value_ == 3); } -template -void test_slist +template +void test_slist ::test_shift(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -242,6 +249,7 @@ void test_slist , value_traits , size_type , constant_time_size + , linear > list_type; list_type testlist; @@ -249,33 +257,36 @@ void test_slist 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); + for(int s = 1; s <= num_values; ++s){ + expected_values.resize(s); + for(int i = 0; i < s*3; ++i){ + testlist.insert_after(testlist.before_begin(), &values[0], &values[0] + s); + testlist.shift_forward(i); + for(int j = 0; j < s; ++j){ + expected_values[(j + s - i%s) % s] = (j + 1); + } + + TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin()) + testlist.clear(); } - TEST_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin()) - testlist.clear(); - } + //Shift backwards all possible positions + for(int i = 0; i < s*3; ++i){ + testlist.insert_after(testlist.before_begin(), &values[0], &values[0] + s); + testlist.shift_backwards(i); + for(int j = 0; j < s; ++j){ + expected_values[(j + i) % s] = (j + 1); + } - //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_INTRUSIVE_SEQUENCE_EXPECTED(expected_values, testlist.begin()) - testlist.clear(); } } //test: insert_after (seq-version), swap, splice_after: -template -void test_slist +template +void test_slist ::test_swap(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -284,11 +295,12 @@ void test_slist , value_traits , size_type , constant_time_size + , linear > list_type; { list_type testlist1 (&values[0], &values[0] + 2); list_type testlist2; - testlist2.insert_after (testlist2.end(), &values[0] + 2, &values[0] + 5); + testlist2.insert_after (testlist2.before_begin(), &values[0] + 2, &values[0] + 5); testlist1.swap(testlist2); { int init_values [] = { 3, 4, 5 }; TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } @@ -299,19 +311,20 @@ void test_slist TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } BOOST_TEST (testlist1.empty()); - testlist1.splice_after (testlist1.end(), testlist2, ++testlist2.begin()); + testlist1.splice_after (testlist1.before_begin(), 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()); + testlist2.before_begin(), ++++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() ); } } + if(!list_type::linear) { list_type testlist1 (&values[0], &values[0] + 2); list_type testlist2 (&values[0] + 3, &values[0] + 5); @@ -326,10 +339,32 @@ void test_slist { int init_values [] = { 4, 3 }; TEST_INTRUSIVE_SEQUENCE( init_values, testlist2.begin() ); } } + if(!list_type::linear) + { + list_type testlist1 (&values[0], &values[1]); + + { int init_values [] = { 1 }; + TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } + + values[1].swap_nodes(values[2]); + + { int init_values [] = { 1 }; + TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } + + values[0].swap_nodes(values[2]); + + { int init_values [] = { 3 }; + TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } + + values[0].swap_nodes(values[2]); + + { int init_values [] = { 1 }; + TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } + } } -template -void test_slist +template +void test_slist ::test_clone(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -338,6 +373,7 @@ void test_slist , value_traits , size_type , constant_time_size + , linear > list_type; list_type testlist1 (&values[0], &values[0] + values.size()); @@ -349,9 +385,10 @@ void test_slist BOOST_TEST (testlist2.empty()); } -template -void test_slist - ::test_container_from_end(std::vector& values) +template +void test_slist + ::test_container_from_end(std::vector& values + ,detail::bool_) { typedef typename ValueTraits::value_type value_type; typedef slist @@ -359,6 +396,7 @@ void test_slist , value_traits , size_type , constant_time_size + , linear > list_type; list_type testlist1 (&values[0], &values[0] + values.size()); BOOST_TEST (testlist1 == list_type::container_from_end_iterator(testlist1.end())); @@ -380,6 +418,7 @@ class test_main_template < value_type , typename value_type::slist_base_hook_t >::type + , false >::test_all(data); test_slist < typename detail::get_member_value_traits < value_type @@ -388,6 +427,25 @@ class test_main_template , &value_type::slist_node_ > >::type + , false + >::test_all(data); + + //Now linear slists + test_slist < typename detail::get_base_value_traits + < value_type + , typename value_type::slist_base_hook_t + >::type + , true + >::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 + , true >::test_all(data); return 0; @@ -409,6 +467,7 @@ class test_main_template < value_type , typename value_type::slist_base_hook_t >::type + , false >::test_all(data); test_slist < typename detail::get_member_value_traits @@ -418,12 +477,14 @@ class test_main_template , &value_type::slist_node_ > >::type + , false >::test_all(data); test_slist < typename detail::get_base_value_traits < value_type , typename value_type::slist_auto_base_hook_t >::type + , false >::test_all(data); test_slist < typename detail::get_member_value_traits @@ -433,6 +494,24 @@ class test_main_template , &value_type::slist_auto_node_ > >::type + , false + >::test_all(data); + + test_slist < typename detail::get_base_value_traits + < value_type + , typename value_type::slist_base_hook_t + >::type + , true + >::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 + , true >::test_all(data); return 0; } From 703fd75c7a64150ce555e99b9f41cdc3295960b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Fri, 25 Jan 2008 23:07:51 +0000 Subject: [PATCH 04/20] 1)Fixed gcc release mode warnings. 2)Replaced throw with BOOST_RETHROW when BOOST_TRY is used. 3)Fixed issues with singly linked lists [SVN r42974] --- .../intrusive/circular_list_algorithms.hpp | 7 +- .../intrusive/circular_slist_algorithms.hpp | 273 +++++----- include/boost/intrusive/hashtable.hpp | 2 +- include/boost/intrusive/intrusive_fwd.hpp | 1 + .../intrusive/linear_slist_algorithms.hpp | 232 +++++---- include/boost/intrusive/list.hpp | 169 +++--- include/boost/intrusive/options.hpp | 14 + include/boost/intrusive/slist.hpp | 493 ++++++++++++------ .../vc7ide/_intrusivelib/_intrusivelib.vcproj | 3 + test/itestvalue.hpp | 12 +- test/list_test.cpp | 58 +++ test/slist_test.cpp | 229 ++++++-- 12 files changed, 955 insertions(+), 538 deletions(-) diff --git a/include/boost/intrusive/circular_list_algorithms.hpp b/include/boost/intrusive/circular_list_algorithms.hpp index 7d442b7..97438bf 100644 --- a/include/boost/intrusive/circular_list_algorithms.hpp +++ b/include/boost/intrusive/circular_list_algorithms.hpp @@ -154,10 +154,9 @@ class circular_list_algorithms static void unlink(node_ptr b, node_ptr e) { if (b != e) { - node_ptr prev(NodeTraits::get_previous(b)); - node_ptr next(NodeTraits::get_next(e)); - NodeTraits::set_previous(next, prev); - NodeTraits::set_next(prev, next); + node_ptr prevb(NodeTraits::get_previous(b)); + NodeTraits::set_previous(e, prevb); + NodeTraits::set_next(prevb, e); } } diff --git a/include/boost/intrusive/circular_slist_algorithms.hpp b/include/boost/intrusive/circular_slist_algorithms.hpp index 2768261..73e4590 100644 --- a/include/boost/intrusive/circular_slist_algorithms.hpp +++ b/include/boost/intrusive/circular_slist_algorithms.hpp @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -45,22 +46,98 @@ namespace intrusive { //! static void set_next(node_ptr n, node_ptr next); template class circular_slist_algorithms + /// @cond + : public detail::common_slist_algorithms + /// @endcond { + /// @cond + typedef detail::common_slist_algorithms base_t; + /// @endcond public: typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; typedef NodeTraits node_traits; - //! Requires: this_node must be in a circular list or be an empty circular list. + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Effects: Constructs an non-used list element, putting the next + //! pointer to null: + //! NodeTraits::get_next(this_node) == 0 //! - //! Effects: Returns the previous node of this_node in the circular list. - //! - //! Complexity: Linear to the number of elements in the circular list. + //! Complexity: Constant //! //! Throws: Nothing. - static node_ptr get_previous_node(node_ptr this_node) - { return get_previous_node(this_node, this_node); } + static void init(node_ptr this_node); + //! Requires: this_node must be in a circular list or be an empty circular list. + //! + //! Effects: Returns true is "this_node" is the only node of a circular list: + //! or it's a not inserted node: + //! return !NodeTraits::get_next(this_node) || NodeTraits::get_next(this_node) == this_node + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool unique(const_node_ptr this_node); + + //! Effects: Returns true is "this_node" has the same state as + //! if it was inited using "init(node_ptr)" + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool inited(const_node_ptr this_node); + + //! Requires: prev_node must be in a circular list or be an empty circular list. + //! + //! Effects: Unlinks the next node of prev_node from the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void unlink_after(node_ptr prev_node); + + //! Requires: prev_node and last_node must be in a circular list + //! or be an empty circular list. + //! + //! Effects: Unlinks the range (prev_node, last_node) from the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void unlink_after(node_ptr prev_node, node_ptr last_node); + + //! Requires: prev_node must be a node of a circular list. + //! + //! Effects: Links this_node after prev_node in the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void link_after(node_ptr prev_node, node_ptr this_node); + + //! Requires: b and e must be nodes of the same circular list or an empty range. + //! and p must be a node of a different circular list. + //! + //! Effects: Removes the nodes from (b, e] range from their circular list and inserts + //! them after p in p's circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void transfer_after(node_ptr p, node_ptr b, node_ptr e); + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Effects: Constructs an empty list, making this_node the only + //! node of the circular list: + //! NodeTraits::get_next(this_node) == this_node. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init_header(node_ptr this_node) + { NodeTraits::set_next(this_node, this_node); } //! Requires: this_node and prev_init_node must be in the same circular list. //! @@ -72,17 +149,17 @@ class circular_slist_algorithms //! //! Throws: Nothing. static node_ptr get_previous_node(node_ptr prev_init_node, node_ptr this_node) - { - node_ptr p = prev_init_node; - for( node_ptr p_next - ; this_node != (p_next = NodeTraits::get_next(p)) - ; p = p_next){ - //Logic error: possible use of linear lists with - //operations only permitted with circular lists - BOOST_INTRUSIVE_INVARIANT_ASSERT(p); - } - return p; - } + { return base_t::get_previous_node(prev_init_node, this_node); } + + //! Requires: this_node must be in a circular list or be an empty circular list. + //! + //! Effects: Returns the previous node of this_node in the circular list. + //! + //! Complexity: Linear to the number of elements in the circular list. + //! + //! Throws: Nothing. + static node_ptr get_previous_node(node_ptr this_node) + { return base_t::get_previous_node(this_node, this_node); } //! Requires: this_node must be in a circular list or be an empty circular list. //! @@ -116,49 +193,6 @@ class circular_slist_algorithms return p; } - //! Effects: Constructs an empty list, making this_node the only - //! node of the circular list: - //! NodeTraits::get_next(this_node) == this_node. - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static void init_header(node_ptr this_node) - { NodeTraits::set_next(this_node, this_node); } - - //! Effects: Constructs an non-used list element, putting the next - //! pointer to null: - //! NodeTraits::get_next(this_node) == 0 - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static void init(node_ptr this_node) - { NodeTraits::set_next(this_node, 0); } - - //! Requires: this_node must be in a circular list or be an empty circular list. - //! - //! Effects: Returns true is "this_node" is the only node of a circular list: - //! or it's a not inserted node: - //! return !NodeTraits::get_next(this_node) || NodeTraits::get_next(this_node) == this_node or - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static bool unique(const_node_ptr this_node) - { - node_ptr next = NodeTraits::get_next(this_node); - return !next || next == this_node; - } - - //! Effects: Returns true is "this_node" has the same state as if it was inited using "init(node_ptr)" - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static bool inited(const_node_ptr this_node) - { return !NodeTraits::get_next(this_node); } - //! Requires: this_node must be in a circular list or be an empty circular list. //! //! Effects: Returns the number of nodes in a circular list. If the circular list @@ -178,33 +212,6 @@ class circular_slist_algorithms return result; } - //! Requires: prev_node must be in a circular list or be an empty circular list. - //! - //! Effects: Unlinks the next node of prev_node from the circular list. - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static void unlink_after(node_ptr prev_node) - { - node_ptr this_node(NodeTraits::get_next(prev_node)); - NodeTraits::set_next(prev_node, NodeTraits::get_next(this_node)); - //NodeTraits::set_next(this_node, this_node); - } - - //! Requires: nxt_node must be in a circular list or be an empty circular list. - //! - //! Effects: Unlinks the previous node of nxt_node from the circular list. - //! - //! Complexity: Linear to the elements in the circular list. - //! - //! Throws: Nothing. - static void unlink_before(node_ptr nxt_node) - { - node_ptr prev_to_erase(get_previous_previous_node(nxt_node)); - unlink_after(prev_to_erase); - } - //! Requires: this_node must be in a circular list, be an empty circular list or be inited. //! //! Effects: Unlinks the node from the circular list. @@ -215,20 +222,7 @@ class circular_slist_algorithms static void unlink(node_ptr this_node) { if(NodeTraits::get_next(this_node)) - unlink_after(get_previous_node(this_node)); - } - - //! Requires: prev_node must be a node of a circular list. - //! - //! Effects: Links this_node after prev_node in the circular list. - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static void link_after(node_ptr prev_node, node_ptr this_node) - { - NodeTraits::set_next(this_node, NodeTraits::get_next(prev_node)); - NodeTraits::set_next(prev_node, this_node); + base_t::unlink_after(get_previous_node(this_node)); } //! Requires: nxt_node must be a node of a circular list. @@ -239,7 +233,7 @@ class circular_slist_algorithms //! //! Throws: Nothing. static void link_before (node_ptr nxt_node, node_ptr this_node) - { link_after(get_previous_node(nxt_node), this_node); } + { base_t::link_after(get_previous_node(nxt_node), this_node); } //! Requires: this_node and other_node must be nodes inserted //! in circular lists or be empty circular lists. @@ -255,17 +249,17 @@ class circular_slist_algorithms { if (other_node == this_node) return; - bool this_inited = inited(this_node); - bool other_inited = inited(other_node); + bool this_inited = base_t::inited(this_node); + bool other_inited = base_t::inited(other_node); if(this_inited){ - init_header(this_node); + base_t::init_header(this_node); } if(other_inited){ - init_header(other_node); + base_t::init_header(other_node); } - bool empty1 = unique(this_node); - bool empty2 = unique(other_node); + bool empty1 = base_t::unique(this_node); + bool empty2 = base_t::unique(other_node); node_ptr prev_this (get_previous_node(this_node)); node_ptr prev_other(get_previous_node(other_node)); @@ -277,31 +271,10 @@ class circular_slist_algorithms NodeTraits::set_next(empty2 ? this_node : prev_other, this_node); if(this_inited){ - init(other_node); + base_t::init(other_node); } if(other_inited){ - init(this_node); - } - } - - //! Requires: b and e must be nodes of the same circular list or an empty range. - //! and p must be a node of a different circular list. - //! - //! Effects: Removes the nodes from (b, e] range from their circular list and inserts - //! them after p in p's circular list. - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static void transfer_after(node_ptr p, node_ptr b, node_ptr e) - { - if (p != b && p != e) { - node_ptr next_b = NodeTraits::get_next(b); - node_ptr next_e = NodeTraits::get_next(e); - node_ptr next_p = NodeTraits::get_next(p); - NodeTraits::set_next(b, next_e); - NodeTraits::set_next(e, next_p); - NodeTraits::set_next(p, next_b); + base_t::init(this_node); } } @@ -317,24 +290,27 @@ class circular_slist_algorithms node_ptr nxt(NodeTraits::get_next(i)); if (nxt == e) break; - transfer_after(e, i, nxt); + base_t::transfer_after(e, i, nxt); } } //! Effects: Moves the node p n positions towards the end of the list. - //! + //! + //! Returns: The previous node of p after the function if there has been any movement, + //! Null if n leads to no movement. + //! //! Throws: Nothing. //! //! Complexity: Linear to the number of elements plus the number moved positions. - static void move_backwards(node_ptr p, std::size_t n) + static node_ptr move_backwards(node_ptr p, std::size_t n) { //Null shift, nothing to do - if(!n) return; + if(!n) return 0; node_ptr first = NodeTraits::get_next(p); //count() == 1 or 2, nothing to do if(NodeTraits::get_next(first) == p) - return; + return 0; bool end_found = false; node_ptr new_last(0); @@ -350,11 +326,11 @@ class circular_slist_algorithms //Shortcut the shift with the modulo of the size of the list n %= i; if(!n) - return; + return 0; i = 0; //Unlink p and continue the new first node search first = NodeTraits::get_next(p); - unlink_after(new_last); + base_t::unlink_after(new_last); end_found = true; } } @@ -362,26 +338,30 @@ class circular_slist_algorithms //If the p has not been found in the previous loop, find it //starting in the new first node and unlink it if(!end_found){ - unlink_after(get_previous_node(first, p)); + base_t::unlink_after(base_t::get_previous_node(first, p)); } //Now link p after the new last node - link_after(new_last, p); + base_t::link_after(new_last, p); + return new_last; } //! Effects: Moves the node p n positions towards the beginning of the list. //! + //! Returns: The previous node of p after the function if there has been any movement, + //! Null if n leads equals to no movement. + //! //! Throws: Nothing. //! //! Complexity: Linear to the number of elements plus the number moved positions. - static void move_forward(node_ptr p, std::size_t n) + static node_ptr move_forward(node_ptr p, std::size_t n) { //Null shift, nothing to do - if(!n) return; + if(!n) return 0; node_ptr first = node_traits::get_next(p); //count() == 1 or 2, nothing to do - if(node_traits::get_next(first) == p) return; + if(node_traits::get_next(first) == p) return 0; //Iterate until p is found to know where the current last node is. //If the shift count is less than the size of the list, we can also obtain @@ -400,7 +380,7 @@ class circular_slist_algorithms //Shortcut the shift with the modulo of the size of the list std::size_t new_before_last_pos = (distance - (n % distance))% distance; //If the shift is a multiple of the size there is nothing to do - if(!new_before_last_pos) return; + if(!new_before_last_pos) return 0; for( new_last = p ; new_before_last_pos-- @@ -410,8 +390,9 @@ class circular_slist_algorithms } //Now unlink p and link it after the new last node - unlink_after(old_last); - link_after(new_last, p); + base_t::unlink_after(old_last); + base_t::link_after(new_last, p); + return new_last; } }; diff --git a/include/boost/intrusive/hashtable.hpp b/include/boost/intrusive/hashtable.hpp index e21f883..6947427 100644 --- a/include/boost/intrusive/hashtable.hpp +++ b/include/boost/intrusive/hashtable.hpp @@ -747,7 +747,7 @@ class hashtable_impl } BOOST_INTRUSIVE_CATCH(...){ this->clear_and_dispose(disposer); - BOOST_RETHROW; + BOOST_INTRUSIVE_RETHROW; } BOOST_INTRUSIVE_CATCH_END } diff --git a/include/boost/intrusive/intrusive_fwd.hpp b/include/boost/intrusive/intrusive_fwd.hpp index 65fe353..9228bcb 100644 --- a/include/boost/intrusive/intrusive_fwd.hpp +++ b/include/boost/intrusive/intrusive_fwd.hpp @@ -70,6 +70,7 @@ template , class O2 = none , class O3 = none , class O4 = none + , class O5 = none > class slist; diff --git a/include/boost/intrusive/linear_slist_algorithms.hpp b/include/boost/intrusive/linear_slist_algorithms.hpp index 46595e3..681c13b 100644 --- a/include/boost/intrusive/linear_slist_algorithms.hpp +++ b/include/boost/intrusive/linear_slist_algorithms.hpp @@ -16,7 +16,9 @@ #include #include +#include #include +#include namespace boost { namespace intrusive { @@ -43,12 +45,99 @@ namespace intrusive { //! static void set_next(node_ptr n, node_ptr next); template class linear_slist_algorithms + /// @cond + : public detail::common_slist_algorithms + /// @endcond { + /// @cond + typedef detail::common_slist_algorithms base_t; + /// @endcond public: typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; typedef NodeTraits node_traits; + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Effects: Constructs an non-used list element, putting the next + //! pointer to null: + //! NodeTraits::get_next(this_node) == 0 + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init(node_ptr this_node); + + //! Requires: this_node must be in a circular list or be an empty circular list. + //! + //! Effects: Returns true is "this_node" is the only node of a circular list: + //! or it's a not inserted node: + //! return !NodeTraits::get_next(this_node) || NodeTraits::get_next(this_node) == this_node + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool unique(const_node_ptr this_node); + + //! Effects: Returns true is "this_node" has the same state as if + //! it was inited using "init(node_ptr)" + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static bool inited(const_node_ptr this_node); + + //! Requires: prev_node must be in a circular list or be an empty circular list. + //! + //! Effects: Unlinks the next node of prev_node from the circular list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void unlink_after(node_ptr prev_node); + + //! Requires: prev_node and last_node must be in a circular list + //! or be an empty circular list. + //! + //! Effects: Unlinks the range (prev_node, last_node) from the linear list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void unlink_after(node_ptr prev_node, node_ptr last_node); + + //! Requires: prev_node must be a node of a linear list. + //! + //! Effects: Links this_node after prev_node in the linear list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void link_after(node_ptr prev_node, node_ptr this_node); + + //! Requires: b and e must be nodes of the same linear list or an empty range. + //! and p must be a node of a different linear list. + //! + //! Effects: Removes the nodes from (b, e] range from their linear list and inserts + //! them after p in p's linear list. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void transfer_after(node_ptr p, node_ptr b, node_ptr e); + + #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + + //! Effects: Constructs an empty list, making this_node the only + //! node of the circular list: + //! NodeTraits::get_next(this_node) == this_node. + //! + //! Complexity: Constant + //! + //! Throws: Nothing. + static void init_header(node_ptr this_node) + { NodeTraits::set_next(this_node, 0); } + //! Requires: this_node and prev_init_node must be in the same linear list. //! //! Effects: Returns the previous node of this_node in the linear list starting. @@ -59,60 +148,7 @@ class linear_slist_algorithms //! //! Throws: Nothing. static node_ptr get_previous_node(node_ptr prev_init_node, node_ptr this_node) - { - node_ptr p = prev_init_node; - for( node_ptr p_next - ; this_node != (p_next = NodeTraits::get_next(p)) - ; p = p_next){ - //empty - } - return p; - } - - //! Effects: Constructs an empty list, making this_node the only - //! node of the linear list: - //! NodeTraits::get_next(this_node) == 0. - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static void init_header(node_ptr this_node) - { NodeTraits::set_next(this_node, 0); } - - //! Effects: Constructs an non-used list element, putting the next - //! pointer to null: - //! NodeTraits::get_next(this_node) == 0 - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static void init(node_ptr this_node) - { NodeTraits::set_next(this_node, 0); } - - //! Requires: this_node must be in a linear list or be an empty linear list. - //! - //! Effects: Returns true is "this_node" is the only node of a linear list: - //! return NodeTraits::get_next(this_node) == this_node - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static bool unique(const_node_ptr this_node) - { - node_ptr next = NodeTraits::get_next(this_node); - return !next || next == this_node; - } - - //! Requires: this_node must be in a linear list or be an empty linear list. - //! - //! Effects: Returns true is "this_node" is the only node of a linear list: - //! return NodeTraits::get_next(this_node) == this_node - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static bool inited(const_node_ptr this_node) - { return !NodeTraits::get_next(this_node); } + { return base_t::get_previous_node(prev_init_node, this_node); } //! Requires: this_node must be in a linear list or be an empty linear list. //! @@ -133,32 +169,6 @@ class linear_slist_algorithms return result; } - //! Requires: prev_node must be in a linear list or be an empty linear list. - //! - //! Effects: Unlinks the next node of prev_node from the linear list. - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static void unlink_after(node_ptr prev_node) - { - node_ptr this_node(NodeTraits::get_next(prev_node)); - NodeTraits::set_next(prev_node, NodeTraits::get_next(this_node)); - } - - //! Requires: prev_node must be a node of a linear list. - //! - //! Effects: Links this_node after prev_node in the linear list. - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static void link_after(node_ptr prev_node, node_ptr this_node) - { - NodeTraits::set_next(this_node, NodeTraits::get_next(prev_node)); - NodeTraits::set_next(prev_node, this_node); - } - //! Requires: this_node and other_node must be nodes inserted //! in linear lists or be empty linear lists. //! @@ -176,27 +186,6 @@ class linear_slist_algorithms NodeTraits::set_next(other_node, this_nxt); } - //! Requires: b and e must be nodes of the same linear list or an empty range. - //! and p must be a node of a different linear list. - //! - //! Effects: Removes the nodes from (b, e] range from their linear list and inserts - //! them after p in p's linear list. - //! - //! Complexity: Constant - //! - //! Throws: Nothing. - static void transfer_after(node_ptr p, node_ptr b, node_ptr e) - { - if (p != b && p != e) { - node_ptr next_b = NodeTraits::get_next(b); - node_ptr next_e = NodeTraits::get_next(e); - node_ptr next_p = NodeTraits::get_next(p); - NodeTraits::set_next(b, next_e); - NodeTraits::set_next(e, next_p); - NodeTraits::set_next(p, next_b); - } - } - //! Effects: Reverses the order of elements in the list. //! //! Returns: The new first node of the list. @@ -211,7 +200,7 @@ class linear_slist_algorithms node_ptr first(p); while(i){ node_ptr nxti(NodeTraits::get_next(i)); - unlink_after(p); + base_t::unlink_after(p); NodeTraits::set_next(i, first); first = i; i = nxti; @@ -219,16 +208,21 @@ class linear_slist_algorithms return first; } - //! Effects: Moves the node p n positions towards the end of the list. + //! Effects: Moves the first n nodes starting at p to the end of the list. + //! + //! Returns: A pair containing the new first and last node of the list or + //! if there has been any movement, a null pair if n leads to no movement. //! //! Throws: Nothing. //! //! Complexity: Linear to the number of elements plus the number moved positions. - static node_ptr move_backwards(node_ptr p, std::size_t n) + static std::pair move_first_n_backwards(node_ptr p, std::size_t n) { + std::pair ret(0, 0); //Null shift, or count() == 0 or 1, nothing to do - if(!n || !p || !NodeTraits::get_next(p)) - return p; + if(!n || !p || !NodeTraits::get_next(p)){ + return ret; + } node_ptr first = p; bool end_found = false; @@ -245,7 +239,7 @@ class linear_slist_algorithms if(first == 0){ //Shortcut the shift with the modulo of the size of the list n %= i; - if(!n) return p; + if(!n) return ret; old_last = new_last; i = 0; //Unlink p and continue the new first node search @@ -258,25 +252,31 @@ class linear_slist_algorithms //If the p has not been found in the previous loop, find it //starting in the new first node and unlink it if(!end_found){ - old_last = get_previous_node(first, 0); + old_last = base_t::get_previous_node(first, 0); } //Now link p after the new last node NodeTraits::set_next(old_last, p); NodeTraits::set_next(new_last, 0); - return first; + ret.first = first; + ret.second = new_last; + return ret; } - //! Effects: Moves the node p n positions towards the beginning of the list. + //! Effects: Moves the first n nodes starting at p to the beginning of the list. + //! + //! Returns: A pair containing the new first and last node of the list or + //! if there has been any movement, a null pair if n leads to no movement. //! //! Throws: Nothing. //! //! Complexity: Linear to the number of elements plus the number moved positions. - static node_ptr move_forward(node_ptr p, std::size_t n) + static std::pair move_first_n_forward(node_ptr p, std::size_t n) { + std::pair ret(0, 0); //Null shift, or count() == 0 or 1, nothing to do if(!n || !p || !NodeTraits::get_next(p)) - return p; + return ret; node_ptr first = p; @@ -298,7 +298,7 @@ class linear_slist_algorithms std::size_t new_before_last_pos = (distance - (n % distance))% distance; //If the shift is a multiple of the size there is nothing to do if(!new_before_last_pos) - return p; + return ret; for( new_last = p ; --new_before_last_pos @@ -308,11 +308,13 @@ class linear_slist_algorithms } //Get the first new node - node_ptr new_first = node_traits::get_next(new_last); + node_ptr new_first(node_traits::get_next(new_last)); //Now put the old beginning after the old end NodeTraits::set_next(old_last, p); NodeTraits::set_next(new_last, 0); - return new_first; + ret.first = new_first; + ret.second = new_last; + return ret; } }; diff --git a/include/boost/intrusive/list.hpp b/include/boost/intrusive/list.hpp index 77c0c47..5968dd2 100644 --- a/include/boost/intrusive/list.hpp +++ b/include/boost/intrusive/list.hpp @@ -290,14 +290,8 @@ class list_impl //! Complexity: Constant. //! //! Note: Invalidates the iterators (but not the references) to the erased element. - void pop_back() - { - node_ptr to_erase = node_traits::get_previous(this->get_root_node()); - node_algorithms::unlink(to_erase); - this->priv_size_traits().decrement(); - if(safemode_or_autounlink) - node_algorithms::init(to_erase); - } + void pop_back() + { return this->pop_back_and_dispose(detail::null_disposer()); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -329,14 +323,8 @@ class list_impl //! Complexity: Constant. //! //! Note: Invalidates the iterators (but not the references) to the erased element. - void pop_front() - { - node_ptr to_erase = node_traits::get_next(this->get_root_node()); - node_algorithms::unlink(to_erase); - this->priv_size_traits().decrement(); - if(safemode_or_autounlink) - node_algorithms::init(to_erase); - } + void pop_front() + { return this->pop_front_and_dispose(detail::null_disposer()); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -406,7 +394,7 @@ class list_impl //! //! Complexity: Constant. const_iterator begin() const - { return cbegin(); } + { return this->cbegin(); } //! Effects: Returns a const_iterator to the first element contained in the list. //! @@ -430,7 +418,7 @@ class list_impl //! //! Complexity: Constant. const_iterator end() const - { return cend(); } + { return this->cend(); } //! Effects: Returns a constant iterator to the end of the list. //! @@ -447,7 +435,7 @@ class list_impl //! //! Complexity: Constant. reverse_iterator rbegin() - { return reverse_iterator(end()); } + { return reverse_iterator(this->end()); } //! Effects: Returns a const_reverse_iterator pointing to the beginning //! of the reversed list. @@ -456,7 +444,7 @@ class list_impl //! //! Complexity: Constant. const_reverse_iterator rbegin() const - { return crbegin(); } + { return this->crbegin(); } //! Effects: Returns a const_reverse_iterator pointing to the beginning //! of the reversed list. @@ -483,7 +471,7 @@ class list_impl //! //! Complexity: Constant. const_reverse_iterator rend() const - { return crend(); } + { return this->crend(); } //! Effects: Returns a const_reverse_iterator pointing to the end //! of the reversed list. @@ -492,7 +480,7 @@ class list_impl //! //! Complexity: Constant. const_reverse_iterator crend() const - { return const_reverse_iterator(begin()); } + { return const_reverse_iterator(this->begin()); } //! Precondition: end_iterator must be a valid end iterator //! of list. @@ -503,7 +491,7 @@ class list_impl //! //! Complexity: Constant. static list_impl &container_from_end_iterator(iterator end_iterator) - { return priv_container_from_end_iterator(end_iterator); } + { return list_impl::priv_container_from_end_iterator(end_iterator); } //! Precondition: end_iterator must be a valid end const_iterator //! of list. @@ -514,7 +502,7 @@ class list_impl //! //! Complexity: Constant. static const list_impl &container_from_end_iterator(const_iterator end_iterator) - { return priv_container_from_end_iterator(end_iterator); } + { return list_impl::priv_container_from_end_iterator(end_iterator); } //! Effects: Returns the number of the elements contained in the list. //! @@ -540,7 +528,7 @@ class list_impl //! //! Note: Does not affect the validity of iterators and references. bool empty() const - { return node_algorithms::unique(this->get_root_node()); } + { return node_algorithms::unique(this->get_root_node()); } //! Effects: Swaps the elements of x and *this. //! @@ -596,16 +584,7 @@ class list_impl //! Note: Invalidates the iterators (but not the references) to the //! erased element. iterator erase(iterator i) - { - iterator erase = i; - ++i; - node_ptr to_erase = erase.pointed_node(); - node_algorithms::unlink(to_erase); - this->priv_size_traits().decrement(); - if(safemode_or_autounlink) - node_algorithms::init(to_erase); - return i; - } + { return this->erase_and_dispose(i, detail::null_disposer()); } //! Requires: first and last must be valid iterator to elements in *this. //! @@ -625,10 +604,7 @@ class list_impl iterator erase(iterator b, iterator e) { if(safemode_or_autounlink || constant_time_size){ - while(b != e){ - b = this->erase(b); - } - return b; + return this->erase_and_dispose(b, e, detail::null_disposer()); } else{ node_algorithms::unlink(b.pointed_node(), e.pointed_node()); @@ -653,14 +629,13 @@ class list_impl template iterator erase_and_dispose(iterator i, Disposer disposer) { - iterator erase = i; + node_ptr to_erase(i.pointed_node()); ++i; - node_ptr to_erase = erase.pointed_node(); node_algorithms::unlink(to_erase); this->priv_size_traits().decrement(); if(safemode_or_autounlink) node_algorithms::init(to_erase); - disposer(get_real_value_traits().to_value_ptr(to_erase)); + disposer(this->get_real_value_traits().to_value_ptr(to_erase)); return i; } @@ -681,10 +656,17 @@ class list_impl template iterator erase_and_dispose(iterator b, iterator e, Disposer disposer) { - while(b != e){ - b = this->erase_and_dispose(b, disposer); + node_ptr bp(b.pointed_node()), ep(e.pointed_node()); + node_algorithms::unlink(bp, ep); + while(bp != ep){ + node_ptr to_erase(bp); + bp = node_traits::get_next(bp); + if(safemode_or_autounlink) + node_algorithms::init(to_erase); + disposer(get_real_value_traits().to_value_ptr(to_erase)); + this->priv_size_traits().decrement(); } - return b; + return e; } //! Effects: Erases all the elements of the container. @@ -699,7 +681,7 @@ class list_impl void clear() { if(safemode_or_autounlink){ - this->erase(this->begin(), this->end()); + this->clear_and_dispose(detail::null_disposer()); } else{ node_algorithms::init_header(this->get_root_node()); @@ -720,7 +702,18 @@ class list_impl //! Note: Invalidates the iterators to the erased elements. template void clear_and_dispose(Disposer disposer) - { this->erase_and_dispose(this->begin(), this->end(), disposer); } + { + iterator it(this->begin()), itend(this->end()); + while(it != itend){ + node_ptr to_erase(it.pointed_node()); + ++it; + if(safemode_or_autounlink) + node_algorithms::init(to_erase); + disposer(get_real_value_traits().to_value_ptr(to_erase)); + } + node_algorithms::init_header(this->get_root_node()); + this->priv_size_traits().set_size(0); + } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -765,7 +758,7 @@ class list_impl //! Note: Does not affect the validity of iterators and references. iterator insert(iterator p, reference value) { - node_ptr to_insert = get_real_value_traits().to_node_ptr(value); + node_ptr to_insert = this->get_real_value_traits().to_node_ptr(value); if(safemode_or_autounlink) BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(to_insert)); node_algorithms::link_before(p.pointed_node(), to_insert); @@ -894,19 +887,10 @@ class list_impl //! list. Iterators of this list and all the references are not invalidated. void splice(iterator p, list_impl&x, iterator start, iterator end) { - if(start != end){ - if(constant_time_size){ - size_traits &thist = this->priv_size_traits(); - size_traits &xt = x.priv_size_traits(); - size_type increment = std::distance(start, end); - node_algorithms::transfer(p.pointed_node(), start.pointed_node(), end.pointed_node()); - thist.set_size(thist.get_size() + increment); - xt.set_size(xt.get_size() - increment); - } - else{ - node_algorithms::transfer(p.pointed_node(), start.pointed_node(), end.pointed_node()); - } - } + if(constant_time_size) + this->splice(p, x, start, end, std::distance(start, end)); + else + this->splice(p, x, start, end, 1);//distance is a dummy value } //! Requires: p must be a valid iterator of *this. @@ -951,7 +935,7 @@ class list_impl //! Complexity: The number of comparisons is approximately N log N, where N //! is the list's size. void sort() - { sort(std::less()); } + { this->sort(std::less()); } //! Requires: p must be a comparison function that induces a strict weak ordering //! @@ -1004,7 +988,7 @@ class list_impl //! //! Note: Iterators and references are not invalidated void merge(list_impl& x) - { merge(x, std::less()); } + { this->merge(x, std::less()); } //! Requires: p must be a comparison function that induces a strict weak //! ordering and both *this and x must be sorted according to that ordering @@ -1023,9 +1007,9 @@ class list_impl template void merge(list_impl& x, Predicate p) { - iterator e = this->end(); - iterator bx = x.begin(); - iterator ex = x.end(); + iterator e(this->end()); + iterator bx(x.begin()); + iterator ex(x.end()); for (iterator b = this->begin(); b != e; ++b) { size_type n(0); @@ -1060,7 +1044,7 @@ class list_impl //! Note: The relative order of elements that are not removed is unchanged, //! and iterators to elements that are not removed remain valid. void remove(const_reference value) - { remove_if(detail::equal_to_value(value)); } + { this->remove_if(detail::equal_to_value(value)); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -1075,7 +1059,7 @@ class list_impl //! and iterators to elements that are not removed remain valid. template void remove_and_dispose(const_reference value, Disposer disposer) - { remove_and_dispose_if(detail::equal_to_value(value), disposer); } + { this->remove_and_dispose_if(detail::equal_to_value(value), disposer); } //! Effects: Removes all the elements for which a specified //! predicate is satisfied. No destructors are called. @@ -1088,7 +1072,7 @@ class list_impl //! and iterators to elements that are not removed remain valid. template void remove_if(Pred pred) - { remove_and_dispose_if(pred, detail::null_disposer()); } + { this->remove_and_dispose_if(pred, detail::null_disposer()); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -1105,17 +1089,15 @@ class list_impl template void remove_and_dispose_if(Pred pred, Disposer disposer) { - iterator first = begin(); - iterator last = end(); - while(first != last) { - iterator next = first; - ++next; - if(pred(*first)){ - pointer p = first.operator->(); - this->erase(first); - disposer(p); + iterator cur(this->begin()); + iterator last(this->end()); + while(cur != last) { + if(pred(*cur)){ + cur = this->erase_and_dispose(cur, disposer); + } + else{ + ++cur; } - first = next; } } @@ -1129,7 +1111,7 @@ class list_impl //! Note: The relative order of elements that are not removed is unchanged, //! and iterators to elements that are not removed remain valid. void unique() - { unique_and_dispose(std::equal_to(), detail::null_disposer()); } + { this->unique_and_dispose(std::equal_to(), detail::null_disposer()); } //! Effects: Removes adjacent duplicate elements or adjacent //! elements that satisfy some binary predicate from the list. @@ -1143,7 +1125,7 @@ class list_impl //! and iterators to elements that are not removed remain valid. template void unique(BinaryPredicate pred) - { unique_and_dispose(pred, detail::null_disposer()); } + { this->unique_and_dispose(pred, detail::null_disposer()); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -1159,7 +1141,7 @@ class list_impl //! and iterators to elements that are not removed remain valid. template void unique_and_dispose(Disposer disposer) - { unique_and_dispose(std::equal_to(), disposer); } + { this->unique_and_dispose(std::equal_to(), disposer); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -1176,18 +1158,19 @@ class list_impl template void unique_and_dispose(BinaryPredicate pred, Disposer disposer) { - if(!this->empty()){ - iterator first = begin(); - iterator after = first; + iterator itend(this->end()); + iterator cur(this->begin()); + + if(cur != itend){ + iterator after(cur); ++after; - while(after != this->end()){ - if(pred(*first, *after)){ - pointer p = after.operator->(); - after = erase(after); - disposer(p); + while(after != itend){ + if(pred(*cur, *after)){ + after = this->erase_and_dispose(after, disposer); } else{ - first = after++; + cur = after; + ++after; } } } @@ -1239,7 +1222,7 @@ class list_impl //! //! Note: Iterators and references are not invalidated. iterator iterator_to(reference value) - { + { BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(real_value_traits::to_node_ptr(value))); return iterator(real_value_traits::to_node_ptr(value), this); } @@ -1254,7 +1237,7 @@ class list_impl //! //! Note: Iterators and references are not invalidated. const_iterator iterator_to(const_reference value) const - { + { BOOST_INTRUSIVE_INVARIANT_ASSERT(!node_algorithms::inited(real_value_traits::to_node_ptr(const_cast (value)))); return const_iterator(real_value_traits::to_node_ptr(const_cast (value)), this); } diff --git a/include/boost/intrusive/options.hpp b/include/boost/intrusive/options.hpp index cda392e..ab1f3dc 100644 --- a/include/boost/intrusive/options.hpp +++ b/include/boost/intrusive/options.hpp @@ -360,6 +360,20 @@ struct linear /// @endcond }; +//!This option setter specifies if the list container should +//!use a linear implementation instead of a circular one. +template +struct cache_last +{ +/// @cond + template + struct pack : Base + { + static const bool cache_last = Enabled; + }; +/// @endcond +}; + //!This option setter specifies the bucket traits //!class for unordered associative containers. When this option is specified, //!instead of using the default bucket traits, a user defined holder will be defined diff --git a/include/boost/intrusive/slist.hpp b/include/boost/intrusive/slist.hpp index ba0240a..232ab7e 100644 --- a/include/boost/intrusive/slist.hpp +++ b/include/boost/intrusive/slist.hpp @@ -28,7 +28,8 @@ #include #include #include -#include +#include //std::size_t +#include //std::pair namespace boost { namespace intrusive { @@ -47,13 +48,27 @@ template struct get_default_slist_hook { typedef typename T::default_slist_hook type; }; -template +template struct slistopt { typedef ValueTraits value_traits; typedef SizeType size_type; - static const bool constant_time_size = ConstantTimeSize; - static const bool linear = Linear; + static const bool constant_time_size = ConstantTimeSize; + static const bool linear = Linear; + static const bool cache_last = CacheLast; +}; + +template +struct root_plus_last +{ + Node root_; + NodePtr last_; +}; + +template +struct root_plus_last +{ + Node root_; }; template @@ -70,6 +85,7 @@ struct slist_defaults , constant_time_size , linear , size_type + , cache_last >::type {}; @@ -89,15 +105,15 @@ struct slist_defaults //! //! The container supports the following options: //! \c base_hook<>/member_hook<>/value_traits<>, -//! \c constant_time_size<> and \c size_type<>. +//! \c constant_time_size<>, \c size_type<>, +//! \c linear<> and \c cache_last<>. //! //! The iterators of slist are forward iterators. slist provides a static //! function called "previous" to compute the previous iterator of a given iterator. //! This function has linear complexity. To improve the usability esp. with //! the '*_after' functions, ++end() == begin() and previous(begin()) == end() -//! are defined. In addition, whenever you have an end iterator, 'after this -//! iterator' means 'at the beginning of the list'. To improve the self-documentation -//! a "before_begin()" function is defined, returning the end() iterator. +//! are defined. An new special function "before_begin()" is defined, which returns +//! an iterator that points one less the beginning of the list: ++before_begin() == begin() #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED template #else @@ -141,6 +157,7 @@ class slist_impl static const bool constant_time_size = Config::constant_time_size; static const bool stateful_value_traits = detail::store_cont_ptr_on_it::value; static const bool linear = Config::linear; + static const bool cache_last = Config::cache_last; /// @cond private: @@ -162,6 +179,14 @@ class slist_impl BOOST_STATIC_ASSERT(!(constant_time_size && ((int)real_value_traits::link_mode == (int)auto_unlink))); //Linear singly linked lists are incompatible with auto-unlink hooks! BOOST_STATIC_ASSERT(!(linear && ((int)real_value_traits::link_mode == (int)auto_unlink))); + //A list with cached last node is incompatible with auto-unlink hooks! + BOOST_STATIC_ASSERT(!(cache_last && ((int)real_value_traits::link_mode == (int)auto_unlink))); + + node_ptr get_end_node() + { return node_ptr(linear ? 0 : this->get_root_node()); } + + const_node_ptr get_end_node() const + { return const_node_ptr(linear ? 0 : this->get_root_node()); } node_ptr get_root_node() { return node_ptr(&data_.root_plus_size_.root_); } @@ -169,16 +194,49 @@ class slist_impl const_node_ptr get_root_node() const { return const_node_ptr(&data_.root_plus_size_.root_); } + node_ptr get_last_node() + { return this->get_last_node(detail::bool_()); } + + const_node_ptr get_last_node() const + { return this->get_last_node(detail::bool_()); } + + void set_last_node(node_ptr n) + { return this->set_last_node(n, detail::bool_()); } + + node_ptr get_last_node(detail::bool_) + { return node_ptr(0); } + + const_node_ptr get_last_node(detail::bool_) const + { return const_node_ptr(0); } + + void set_last_node(node_ptr, detail::bool_) + {} + + node_ptr get_last_node(detail::bool_) + { return node_ptr(data_.root_plus_size_.last_); } + + const_node_ptr get_last_node(detail::bool_) const + { return const_node_ptr(data_.root_plus_size_.last_); } + + void set_last_node(node_ptr n, detail::bool_) + { data_.root_plus_size_.last_ = n; } + static node_ptr uncast(const_node_ptr ptr) + { return node_ptr(const_cast(detail::get_pointer(ptr))); } + + void set_default_constructed_state() { - return node_ptr(const_cast(detail::get_pointer(ptr))); + node_algorithms::init_header(this->get_root_node()); + this->priv_size_traits().set_size(size_type(0)); + if(cache_last){ + this->set_last_node(this->get_root_node()); + } } struct root_plus_size : public size_traits - { - node root_; - }; + , public root_plus_last + {}; struct data_t : public slist_impl::value_traits @@ -228,10 +286,7 @@ class slist_impl //! constructor throws (this does not happen with predefined Boost.Intrusive hooks). slist_impl(const value_traits &v_traits = value_traits()) : data_(v_traits) - { - this->priv_size_traits().set_size(size_type(0)); - node_algorithms::init_header(this->get_root_node()); - } + { this->set_default_constructed_state(); } //! Requires: Dereferencing iterator must yield an lvalue of type value_type. //! @@ -245,9 +300,8 @@ class slist_impl slist_impl(Iterator b, Iterator e, const value_traits &v_traits = value_traits()) : data_(v_traits) { - this->priv_size_traits().set_size(size_type(0)); - node_algorithms::init_header(this->get_root_node()); - insert_after(before_begin(), b, e); + this->set_default_constructed_state(); + this->insert_after(this->before_begin(), b, e); } //! Effects: If it's a safe-mode @@ -273,11 +327,10 @@ class slist_impl void clear() { if(safemode_or_autounlink){ - this->erase_after(this->before_begin(), this->end()); + this->clear_and_dispose(detail::null_disposer()); } else{ - node_algorithms::init_header(this->get_root_node()); - this->priv_size_traits().set_size(size_type(0)); + this->set_default_constructed_state(); } } @@ -293,7 +346,17 @@ class slist_impl //! Note: Invalidates the iterators to the erased elements. template void clear_and_dispose(Disposer disposer) - { this->erase_after_and_dispose(this->before_begin(), this->end(), disposer); } + { + iterator it(this->begin()), itend(this->end()); + while(it != itend){ + node_ptr to_erase(it.pointed_node()); + ++it; + if(safemode_or_autounlink) + node_algorithms::init(to_erase); + disposer(get_real_value_traits().to_value_ptr(to_erase)); + } + this->set_default_constructed_state(); + } //! Requires: value must be an lvalue. //! @@ -310,10 +373,32 @@ class slist_impl node_ptr to_insert = get_real_value_traits().to_node_ptr(value); if(safemode_or_autounlink) BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(to_insert)); + if(cache_last){ + if(this->empty()){ + this->set_last_node(to_insert); + } + } node_algorithms::link_after(this->get_root_node(), to_insert); this->priv_size_traits().increment(); } + //! Requires: value must be an lvalue. + //! + //! Effects: Inserts the value in the back of the list. + //! No copy constructors are called. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + //! + //! Note: Does not affect the validity of iterators and references. + //! This function is only available is cache_last<> is true. + void push_back(reference value) + { + BOOST_STATIC_ASSERT((cache_last != 0)); + this->insert_after(iterator(this->get_last_node(), this), value); + } + //! Effects: Erases the first element of the list. //! No destructors are called. //! @@ -323,13 +408,7 @@ class slist_impl //! //! Note: Invalidates the iterators (but not the references) to the erased element. void pop_front() - { - node_ptr to_erase = node_traits::get_next(this->get_root_node()); - node_algorithms::unlink_after(this->get_root_node()); - this->priv_size_traits().decrement(); - if(safemode_or_autounlink) - node_algorithms::init(to_erase); - } + { return this->pop_front_and_dispose(detail::null_disposer()); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -345,8 +424,16 @@ class slist_impl void pop_front_and_dispose(Disposer disposer) { node_ptr to_erase = node_traits::get_next(this->get_root_node()); - this->pop_front(); + node_algorithms::unlink_after(this->get_root_node()); + this->priv_size_traits().decrement(); + if(safemode_or_autounlink) + node_algorithms::init(to_erase); disposer(get_real_value_traits().to_value_ptr(to_erase)); + if(cache_last){ + if(this->empty()){ + this->set_last_node(this->get_root_node()); + } + } } //! Effects: Returns a reference to the first element of the list. @@ -355,7 +442,7 @@ class slist_impl //! //! Complexity: Constant. reference front() - { return *get_real_value_traits().to_value_ptr(node_traits::get_next(this->get_root_node())); } + { return *this->get_real_value_traits().to_value_ptr(node_traits::get_next(this->get_root_node())); } //! Effects: Returns a const_reference to the first element of the list. //! @@ -363,7 +450,35 @@ class slist_impl //! //! Complexity: Constant. const_reference front() const - { return *get_real_value_traits().to_value_ptr(uncast(node_traits::get_next(this->get_root_node()))); } + { return *this->get_real_value_traits().to_value_ptr(uncast(node_traits::get_next(this->get_root_node()))); } + + //! Effects: Returns a reference to the last element of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + //! + //! Note: Does not affect the validity of iterators and references. + //! This function is only available is cache_last<> is true. + reference back() + { + BOOST_STATIC_ASSERT((cache_last != 0)); + return *this->get_real_value_traits().to_value_ptr(this->get_last_node()); + } + + //! Effects: Returns a const_reference to the last element of the list. + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + //! + //! Note: Does not affect the validity of iterators and references. + //! This function is only available is cache_last<> is true. + const_reference back() const + { + BOOST_STATIC_ASSERT((cache_last != 0)); + return *this->get_real_value_traits().to_value_ptr(this->get_last_node()); + } //! Effects: Returns an iterator to the first element contained in the list. //! @@ -387,7 +502,7 @@ class slist_impl //! //! Complexity: Constant. const_iterator cbegin() const - { return const_iterator (node_traits::get_next(this->get_root_node()), this); } + { return const_iterator(node_traits::get_next(this->get_root_node()), this); } //! Effects: Returns an iterator to the end of the list. //! @@ -395,7 +510,7 @@ class slist_impl //! //! Complexity: Constant. iterator end() - { return iterator (linear ? 0 : this->get_root_node(), this); } + { return iterator(this->get_end_node(), this); } //! Effects: Returns a const_iterator to the end of the list. //! @@ -403,7 +518,7 @@ class slist_impl //! //! Complexity: Constant. const_iterator end() const - { return const_iterator (linear ? 0 : uncast(this->get_root_node()), this); } + { return const_iterator(uncast(this->get_end_node()), this); } //! Effects: Returns a const_iterator to the end of the list. //! @@ -449,7 +564,7 @@ class slist_impl //! //! Complexity: Constant. static slist_impl &container_from_end_iterator(iterator end_iterator) - { return priv_container_from_end_iterator(end_iterator); } + { return slist_impl::priv_container_from_end_iterator(end_iterator); } //! Precondition: end_iterator must be a valid end const_iterator //! of slist. @@ -460,7 +575,7 @@ class slist_impl //! //! Complexity: Constant. static const slist_impl &container_from_end_iterator(const_iterator end_iterator) - { return priv_container_from_end_iterator(end_iterator); } + { return slist_impl::priv_container_from_end_iterator(end_iterator); } //! Effects: Returns the number of the elements contained in the list. //! @@ -492,12 +607,18 @@ class slist_impl //! //! Throws: Nothing. //! - //! Complexity: Linear to the number of elements of both lists. + //! Complexity: Linear to the number of elements of both lists. + //! Constant-time if linear<> and/or cache_last<> options are used. //! //! Note: Does not affect the validity of iterators and references. void swap(slist_impl& other) { - priv_swap_lists(this->get_root_node(), other.get_root_node(), detail::bool_()); + if(cache_last){ + this->priv_swap_cache_last(other); + } + else{ + this->priv_swap_lists(this->get_root_node(), other.get_root_node(), detail::bool_()); + } if(constant_time_size){ size_type backup = this->priv_size_traits().get_size(); this->priv_size_traits().set_size(other.priv_size_traits().get_size()); @@ -515,9 +636,7 @@ class slist_impl //! //! Note: Iterators Does not affect the validity of iterators and references. void shift_backwards(size_type n = 1) - { - priv_shift_backwards(n, detail::bool_()); - } + { this->priv_shift_backwards(n, detail::bool_()); } //! Effects: Moves forward all the elements, so that the second //! element becomes the first, the third becomes the second... @@ -529,9 +648,7 @@ class slist_impl //! //! Note: Does not affect the validity of iterators and references. void shift_forward(size_type n = 1) - { - priv_shift_forward(n, detail::bool_()); - } + { this->priv_shift_forward(n, detail::bool_()); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -548,18 +665,18 @@ class slist_impl //! Throws: If cloner throws. template void clone_from(const slist_impl &src, Cloner cloner, Disposer disposer) - { + { this->clear_and_dispose(disposer); BOOST_INTRUSIVE_TRY{ - iterator prev = this->before_begin(); + iterator prev(this->before_begin()); const_iterator b(src.begin()), e(src.end()); - for(; b != e; ++b, ++prev){ - this->insert_after(prev, *cloner(*b)); + for(; b != e; ++b){ + prev = this->insert_after(prev, *cloner(*b)); } } BOOST_INTRUSIVE_CATCH(...){ this->clear_and_dispose(disposer); - BOOST_RETHROW; + BOOST_INTRUSIVE_RETHROW; } BOOST_INTRUSIVE_CATCH_END } @@ -582,7 +699,11 @@ class slist_impl node_ptr n = get_real_value_traits().to_node_ptr(value); if(safemode_or_autounlink) BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::inited(n)); - node_algorithms::link_after(prev_p.pointed_node(), n); + node_ptr prev_n(prev_p.pointed_node()); + node_algorithms::link_after(prev_n, n); + if(cache_last && (this->get_last_node() == prev_n)){ + this->set_last_node(n); + } this->priv_size_traits().increment(); return iterator (n, this); } @@ -603,7 +724,7 @@ class slist_impl void insert_after(iterator prev_p, Iterator first, Iterator last) { for (; first != last; ++first) - prev_p = insert_after(prev_p, *first); + prev_p = this->insert_after(prev_p, *first); } //! Requires: value must be an lvalue and p must point to an element @@ -614,11 +735,12 @@ class slist_impl //! //! Throws: Nothing. //! - //! Complexity: Linear to the number of elements before p. + //! Complexity: Linear to the number of elements before p. + //! Constant-time if cache_last<> is true and p == end(). //! //! Note: Does not affect the validity of iterators and references. iterator insert(iterator p, reference value) - { return insert_after(this->previous(p), value); } + { return this->insert_after(this->previous(p), value); } //! Requires: Dereferencing iterator must yield //! an lvalue of type value_type and p must point to an element @@ -631,11 +753,12 @@ class slist_impl //! //! Complexity: Linear to the number of elements inserted plus linear //! to the elements before b. + //! Linear to the number of elements to insert if cache_last<> option is true and p == end(). //! //! Note: Does not affect the validity of iterators and references. template void insert(iterator p, Iterator b, Iterator e) - { return insert_after(this->previous(p), b, e); } + { return this->insert_after(this->previous(p), b, e); } //! Effects: Erases the element after the element pointed by prev of //! the list. No destructors are called. @@ -698,7 +821,7 @@ class slist_impl //! Note: Invalidates the iterators (but not the references) to the //! erased elements. iterator erase(iterator first, iterator last) - { return erase_after(this->previous(first), last); } + { return this->erase_after(this->previous(first), last); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -721,7 +844,11 @@ class slist_impl ++it; node_ptr to_erase(it.pointed_node()); ++it; - node_algorithms::unlink_after(prev.pointed_node()); + node_ptr prev_n(prev.pointed_node()); + node_algorithms::unlink_after(prev_n); + if(cache_last && (to_erase == this->get_last_node())){ + this->set_last_node(prev_n); + } this->priv_size_traits().decrement(); if(safemode_or_autounlink) node_algorithms::init(to_erase); @@ -746,10 +873,19 @@ class slist_impl template iterator erase_after_and_dispose(iterator before_first, iterator last, Disposer disposer) { - iterator next(before_first); - ++next; - while(next != last){ - next = this->erase_after_and_dispose(before_first, disposer); + node_ptr bfp(before_first.pointed_node()), lp(last.pointed_node()); + node_ptr fp(node_traits::get_next(bfp)); + node_algorithms::unlink_after(bfp, lp); + while(fp != lp){ + node_ptr to_erase(fp); + fp = node_traits::get_next(fp); + if(safemode_or_autounlink) + node_algorithms::init(to_erase); + disposer(get_real_value_traits().to_value_ptr(to_erase)); + this->priv_size_traits().decrement(); + } + if(cache_last && (node_traits::get_next(bfp) == this->get_end_node())){ + this->set_last_node(bfp); } return last; } @@ -792,7 +928,7 @@ class slist_impl //! erased elements. template iterator erase_and_dispose(iterator first, iterator last, Disposer disposer) - { return erase_after_and_dispose(this->previous(first), last, disposer); } + { return this->erase_after_and_dispose(this->previous(first), last, disposer); } //! Requires: Dereferencing iterator must yield //! an lvalue of type value_type. @@ -813,7 +949,7 @@ class slist_impl void assign(Iterator b, Iterator e) { this->clear(); - this->insert_after(before_begin(), b, e); + this->insert_after(this->before_begin(), b, e); } //! Requires: Disposer::operator()(pointer) shouldn't throw. @@ -850,18 +986,21 @@ class slist_impl //! //! Throws: Nothing. //! - //! Complexity: Linear to the elements contained in x + //! Complexity: Linear to the elements contained in x. + //! Constant-time if cache_last<> option is true. //! //! Note: Iterators of values obtained from list x now point to elements of this //! list. Iterators of this list and all the references are not invalidated. iterator splice_after(iterator prev, slist_impl &x) { if (!x.empty()){ - iterator last_x(x.previous(x.end())); - node_algorithms::transfer_after - ( prev.pointed_node() - , x.before_begin().pointed_node() - , last_x.pointed_node()); + iterator last_x(x.previous(x.end())); //<- constant time if cache_last is active + node_ptr prev_n(prev.pointed_node()); + node_ptr last_x_n(last_x.pointed_node()); + if(cache_last && node_traits::get_next(prev_n) == this->get_end_node()){ + this->set_last_node(last_x_n); + } + node_algorithms::transfer_after( prev_n, x.before_begin().pointed_node(), last_x_n); this->priv_size_traits().set_size(this->priv_size_traits().get_size() + x.priv_size_traits().get_size()); x.priv_size_traits().set_size(size_type(0)); return last_x; @@ -884,15 +1023,12 @@ class slist_impl //! //! Note: Iterators of values obtained from list x now point to elements of this //! list. Iterators of this list and all the references are not invalidated. - void splice_after(iterator prev, slist_impl &x, iterator prev_ele) + void splice_after(iterator prev_pos, slist_impl &x, iterator prev_ele) { - iterator nxt = prev_ele; - ++nxt; - if (nxt != prev && prev_ele != prev){ - node_algorithms::transfer_after - (prev.pointed_node(), prev_ele.pointed_node(), nxt.pointed_node()); - this->priv_size_traits().increment(); - x.priv_size_traits().decrement(); + iterator elem = prev_ele; + ++elem; + if (elem != prev_pos && prev_ele != prev_pos){ + this->splice_after(prev_pos, x, prev_ele, elem, 1); } } @@ -913,19 +1049,11 @@ class slist_impl //! list. Iterators of this list and all the references are not invalidated. void splice_after(iterator prev_pos, slist_impl &x, iterator before_first, iterator before_last) { - if (before_first != before_last){ - if(constant_time_size){ - size_type increment = std::distance(before_first, before_last); - node_algorithms::transfer_after - (prev_pos.pointed_node(), before_first.pointed_node(), before_last.pointed_node()); - this->priv_size_traits().set_size(this->priv_size_traits().get_size() + increment); - x.priv_size_traits().set_size(x.priv_size_traits().get_size() - increment); - } - else{ - node_algorithms::transfer_after - (prev_pos.pointed_node(), before_first.pointed_node(), before_last.pointed_node()); - } - } + if(constant_time_size) + this->splice_after(prev_pos, x, before_first, before_last, std::distance(before_first, before_last)); + else + this->priv_splice_after + (prev_pos.pointed_node(), x, before_first.pointed_node(), before_last.pointed_node()); } //! Requires: prev_pos must be a dereferenceable iterator in *this or be @@ -945,17 +1073,13 @@ class slist_impl void splice_after(iterator prev_pos, slist_impl &x, iterator before_first, iterator before_last, difference_type n) { if(n){ + BOOST_INTRUSIVE_INVARIANT_ASSERT(std::distance(before_first, before_last) == n); + this->priv_splice_after + (prev_pos.pointed_node(), x, before_first.pointed_node(), before_last.pointed_node()); if(constant_time_size){ - BOOST_INTRUSIVE_INVARIANT_ASSERT(std::distance(before_first, before_last) == n); - node_algorithms::transfer_after - (prev_pos.pointed_node(), before_first.pointed_node(), before_last.pointed_node()); this->priv_size_traits().set_size(this->priv_size_traits().get_size() + n); x.priv_size_traits().set_size(x.priv_size_traits().get_size() - n); } - else{ - node_algorithms::transfer_after - (prev_pos.pointed_node(), before_first.pointed_node(), before_last.pointed_node()); - } } } @@ -973,11 +1097,13 @@ class slist_impl //! //! Complexity: Linear to the elements contained in x plus linear to //! the elements before it. + //! Linear to the elements before it if cache_last<> option is true. + //! Constant-time if cache_last<> option is true and it == end(). //! //! Note: Iterators of values obtained from list x now point to elements of this //! list. Iterators of this list and all the references are not invalidated. iterator splice(iterator it, slist_impl &x) - { return splice_after(this->previous(it), x); } + { return this->splice_after(this->previous(it), x); } //! Requires: it p must be a valid iterator of *this. //! elem must point to an element contained in list @@ -989,11 +1115,12 @@ class slist_impl //! Throws: Nothing. //! //! Complexity: Linear to the elements before pos and before elem. + //! Linear to the elements before elem if cache_last<> option is true and pos == end(). //! //! Note: Iterators of values obtained from list x now point to elements of this //! list. Iterators of this list and all the references are not invalidated. void splice(iterator pos, slist_impl &x, iterator elem) - { return splice_after(this->previous(pos), x, this->previous(elem)); } + { return this->splice_after(this->previous(pos), x, x.previous(elem)); } //! Requires: pos must be a dereferenceable iterator in *this //! and first and last belong to x and first and last a valid range on x. @@ -1004,13 +1131,16 @@ class slist_impl //! //! Throws: Nothing. //! - //! Complexity: Linear to the sum of elements before pos, first, and last. - //! Plus linear to the number of elements transferred if constant_time_size is true. + //! Complexity: Linear to the sum of elements before pos, first, and last + //! plus linear to the number of elements transferred if constant_time_size is true. + //! Linear to the sum of elements before first, and last + //! plus linear to the number of elements transferred if constant_time_size is true + //! if cache_last<> is true and pos == end() //! //! Note: Iterators of values obtained from list x now point to elements of this //! list. Iterators of this list and all the references are not invalidated. void splice(iterator pos, slist_impl &x, iterator first, iterator last) - { return splice_after(this->previous(pos), x, this->previous(first), this->previous(last)); } + { return this->splice_after(this->previous(pos), x, x.previous(first), x.previous(last)); } //! Requires: pos must be a dereferenceable iterator in *this //! and first and last belong to x and first and last a valid range on x. @@ -1023,11 +1153,13 @@ class slist_impl //! Throws: Nothing. //! //! Complexity: Linear to the sum of elements before pos, first, and last. + //! Linear to the sum of elements before first and last + //! if cache_last<> is true and pos == end(). //! //! Note: Iterators of values obtained from list x now point to elements of this //! list. Iterators of this list and all the references are not invalidated. void splice(iterator pos, slist_impl &x, iterator first, iterator last, difference_type n) - { return splice_after(this->previous(pos), x, this->previous(first), this->previous(last), n); } + { return this->splice_after(this->previous(pos), x, x.previous(first), x.previous(last), n); } //! Effects: This function sorts the list *this according to std::less. //! The sort is stable, that is, the relative order of equivalent elements is preserved. @@ -1173,8 +1305,13 @@ class slist_impl //! Complexity: This function is linear to the contained elements. //! //! Note: Iterators and references are not invalidated - void reverse() - { priv_reverse(detail::bool_()); } + void reverse() + { + if(cache_last && !this->empty()){ + this->set_last_node(node_traits::get_next(this->get_root_node())); + } + this->priv_reverse(detail::bool_()); + } //! Effects: Removes all the elements that compare equal to value. //! No destructors are called. @@ -1187,7 +1324,7 @@ class slist_impl //! and iterators to elements that are not removed remain valid. This function is //! linear time: it performs exactly size() comparisons for equality. void remove(const_reference value) - { remove_if(detail::equal_to_value(value)); } + { this->remove_if(detail::equal_to_value(value)); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -1202,7 +1339,7 @@ class slist_impl //! and iterators to elements that are not removed remain valid. template void remove_and_dispose(const_reference value, Disposer disposer) - { remove_and_dispose_if(detail::equal_to_value(value), disposer); } + { this->remove_and_dispose_if(detail::equal_to_value(value), disposer); } //! Effects: Removes all the elements for which a specified //! predicate is satisfied. No destructors are called. @@ -1215,7 +1352,7 @@ class slist_impl //! and iterators to elements that are not removed remain valid. template void remove_if(Pred pred) - { remove_and_dispose_if(pred, detail::null_disposer()); } + { this->remove_and_dispose_if(pred, detail::null_disposer()); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -1232,18 +1369,20 @@ class slist_impl template void remove_and_dispose_if(Pred pred, Disposer disposer) { - iterator bcur(this->before_begin()), cur, e(this->end()); + iterator bcur(this->before_begin()), cur(this->begin()), e(this->end()); - while(++(cur = bcur) != e){ + while(cur != e){ if (pred(*cur)){ - pointer p = cur.operator->(); - this->erase_after(bcur); - disposer(p); + cur = this->erase_after_and_dispose(bcur, disposer); } else{ - ++bcur; + bcur = cur; + ++cur; } } + if(cache_last){ + this->set_last_node(bcur.pointed_node()); + } } //! Effects: Removes adjacent duplicate elements or adjacent @@ -1256,7 +1395,7 @@ class slist_impl //! Note: The relative order of elements that are not removed is unchanged, //! and iterators to elements that are not removed remain valid. void unique() - { unique_and_dispose(std::equal_to(), detail::null_disposer()); } + { this->unique_and_dispose(std::equal_to(), detail::null_disposer()); } //! Effects: Removes adjacent duplicate elements or adjacent //! elements that satisfy some binary predicate from the list. @@ -1270,7 +1409,7 @@ class slist_impl //! and iterators to elements that are not removed remain valid. template void unique(BinaryPredicate pred) - { unique_and_dispose(pred, detail::null_disposer()); } + { this->unique_and_dispose(pred, detail::null_disposer()); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -1286,7 +1425,7 @@ class slist_impl //! and iterators to elements that are not removed remain valid. template void unique_and_dispose(Disposer disposer) - { unique(std::equal_to(), disposer); } + { this->unique(std::equal_to(), disposer); } //! Requires: Disposer::operator()(pointer) shouldn't throw. //! @@ -1303,21 +1442,23 @@ class slist_impl template void unique_and_dispose(BinaryPredicate pred, Disposer disposer) { - iterator end_n(end()); - iterator cur(begin()); - iterator cur_next; - - if (cur != end_n) { - while(++(cur_next = cur) != end_n) { - if (pred(*cur, *cur_next)){ - pointer p = cur_next.operator->(); - this->erase_after(cur); - disposer(p); + iterator end_n(this->end()); + iterator bcur(this->begin()); + if(bcur != end_n){ + iterator cur(bcur); + ++cur; + while(cur != end_n) { + if (pred(*bcur, *cur)){ + cur = this->erase_after_and_dispose(bcur, disposer); } else{ + bcur = cur; ++cur; } } + if(cache_last){ + this->set_last_node(bcur.pointed_node()); + } } } @@ -1367,7 +1508,7 @@ class slist_impl //! //! Note: Iterators and references are not invalidated. iterator iterator_to(reference value) - { + { //BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::inited(value_traits::to_node_ptr(value))); return iterator (value_traits::to_node_ptr(value), this); } @@ -1382,7 +1523,7 @@ class slist_impl //! //! Note: Iterators and references are not invalidated. const_iterator iterator_to(const_reference value) const - { + { //BOOST_INTRUSIVE_INVARIANT_ASSERT (!node_algorithms::inited(value_traits::to_node_ptr(const_cast (value)))); return const_iterator (value_traits::to_node_ptr(const_cast (value)), this); } @@ -1393,12 +1534,16 @@ class slist_impl //! //! Throws: Nothing. //! - //! Complexity: Linear to the number of elements before i. + //! Complexity: Linear to the number of elements before i. + //! Constant if cache_last<> is true and i == end(). iterator previous(iterator i) { + if(cache_last && (i.pointed_node() == this->get_end_node())){ + return iterator(this->get_last_node(), this); + } return iterator (node_algorithms::get_previous_node - (before_begin().pointed_node(), i.pointed_node()), 0); + (this->before_begin().pointed_node(), i.pointed_node()), this); } //! Returns: The const_iterator to the element before i in the list. @@ -1408,14 +1553,30 @@ class slist_impl //! Throws: Nothing. //! //! Complexity: Linear to the number of elements before i. + //! Constant if cache_last<> is true and i == end(). const_iterator previous(const_iterator i) const { + if(cache_last && (i.pointed_node() == this->get_end_node())){ + return iterator(uncast(this->get_last_node()), this); + } return const_iterator (node_algorithms::get_previous_node - (before_begin().pointed_node(), i.pointed_node()), 0); + (this->before_begin().pointed_node(), i.pointed_node()), this); } private: + void priv_splice_after(node_ptr prev_pos_n, slist_impl &x, node_ptr before_first_n, node_ptr before_last_n) + { + if(cache_last){ + if(node_traits::get_next(prev_pos_n) == this->get_end_node()){ + this->set_last_node(before_last_n); + } + if(node_traits::get_next(before_last_n) == x.get_end_node()){ + x.set_last_node(before_first_n); + } + } + node_algorithms::transfer_after(prev_pos_n, before_first_n, before_last_n); + } void priv_reverse(detail::bool_) { node_algorithms::reverse(this->get_root_node()); } @@ -1429,26 +1590,63 @@ class slist_impl void priv_shift_backwards(size_type n, detail::bool_) { - node_algorithms::move_forward(this->get_root_node(), (std::size_t)n); + node_ptr last = node_algorithms::move_forward(this->get_root_node(), (std::size_t)n); + if(cache_last && last){ + this->set_last_node(last); + } } void priv_shift_backwards(size_type n, detail::bool_) { - node_ptr new_first = node_algorithms::move_forward - (node_traits::get_next(this->get_root_node()), (std::size_t)n); - node_traits::set_next(this->get_root_node(), new_first); + std::pair ret( + node_algorithms::move_first_n_forward + (node_traits::get_next(this->get_root_node()), (std::size_t)n)); + if(ret.first){ + node_traits::set_next(this->get_root_node(), ret.first); + if(cache_last){ + this->set_last_node(ret.second); + } + } } void priv_shift_forward(size_type n, detail::bool_) { - node_algorithms::move_backwards(this->get_root_node(), (std::size_t)n); + node_ptr last = node_algorithms::move_backwards(this->get_root_node(), (std::size_t)n); + if(cache_last && last){ + this->set_last_node(last); + } } void priv_shift_forward(size_type n, detail::bool_) { - node_ptr new_first = node_algorithms::move_backwards - (node_traits::get_next(this->get_root_node()), (std::size_t)n); - node_traits::set_next(this->get_root_node(), new_first); + std::pair ret( + node_algorithms::move_first_n_backwards + (node_traits::get_next(this->get_root_node()), (std::size_t)n)); + if(ret.first){ + node_traits::set_next(this->get_root_node(), ret.first); + if(cache_last){ + this->set_last_node(ret.second); + } + } + } + + void priv_swap_cache_last(slist_impl &other) + { + node_ptr other_last(other.get_last_node()); + node_ptr this_last(this->get_last_node()); + node_ptr other_bfirst(other.get_root_node()); + node_ptr this_bfirst(this->get_root_node()); + node_algorithms::transfer_after(this_bfirst, other_bfirst, other_last); + node_algorithms::transfer_after(other_bfirst, other_last != other_bfirst? other_last : this_bfirst, this_last); + node_ptr tmp(this->get_last_node()); + this->set_last_node(other.get_last_node()); + other.set_last_node(tmp); + if(this->get_last_node() == other_bfirst){ + this->set_last_node(this_bfirst); + } + if(other.get_last_node() == this_bfirst){ + other.set_last_node(other_bfirst); + } } //circular version @@ -1465,7 +1663,7 @@ class slist_impl //singly linked lists (because "end" is represented by the null pointer) BOOST_STATIC_ASSERT(!linear); root_plus_size *r = detail::parent_from_member - ( detail::get_pointer(end_iterator.pointed_node()), &root_plus_size::root_); + ( detail::get_pointer(end_iterator.pointed_node()), (&root_plus_size::root_)); data_t *d = detail::parent_from_member ( r, &data_t::root_plus_size_); slist_impl *s = detail::parent_from_member(d, &slist_impl::data_); @@ -1595,13 +1793,13 @@ inline void swap #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED template #else -template +template #endif struct make_slist { /// @cond typedef typename pack_options - < slist_defaults, O1, O2, O3, O4>::type packed_options; + < slist_defaults, O1, O2, O3, O4, O5>::type packed_options; typedef typename detail::get_value_traits ::type value_traits; typedef slist_impl @@ -1611,6 +1809,7 @@ struct make_slist , typename packed_options::size_type , packed_options::constant_time_size , packed_options::linear + , packed_options::cache_last > > implementation_defined; /// @endcond @@ -1619,12 +1818,12 @@ struct make_slist #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED -template +template class slist - : public make_slist::type + : public make_slist::type { typedef typename make_slist - ::type Base; + ::type Base; typedef typename Base::real_value_traits real_value_traits; //Assert if passed value traits are compatible with the type BOOST_STATIC_ASSERT((detail::is_same::value)); diff --git a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj index 9b2181d..587661c 100644 --- a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj +++ b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj @@ -216,6 +216,9 @@ + + diff --git a/test/itestvalue.hpp b/test/itestvalue.hpp index 6878158..d6338bb 100644 --- a/test/itestvalue.hpp +++ b/test/itestvalue.hpp @@ -237,7 +237,7 @@ struct testvalue // 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_; @@ -270,7 +270,7 @@ struct testvalue 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; } @@ -366,6 +366,14 @@ struct even_odd } }; +struct is_even +{ + template + bool operator() + (const testvalue& v1) const + { return (v1.value_ & 1) == 0; } +}; + } //namespace boost{ } //namespace intrusive{ diff --git a/test/list_test.cpp b/test/list_test.cpp index 822b408..3a000a9 100644 --- a/test/list_test.cpp +++ b/test/list_test.cpp @@ -31,6 +31,8 @@ struct test_list 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_remove_unique(std::vector& values); static void test_insert(std::vector& values); static void test_shift(std::vector& values); static void test_swap(std::vector& values); @@ -58,6 +60,8 @@ void test_list::test_all(std::vector { int init_values [] = { 5, 3, 1, 4, 2 }; TEST_INTRUSIVE_SEQUENCE( init_values, testlist.begin() ); } } + +//test: merge due to error in merge implementation: +template +void test_list + ::test_remove_unique (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()); + list.remove_if(is_even()); + int init_values [] = { 1, 3, 5 }; + TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() ); + } + { + std::vector values2(values); + list_type list(values.begin(), values.end()); + list.insert(list.end(), values2.begin(), values2.end()); + list.sort(); + int init_values [] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 }; + TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() ); + list.unique(); + int init_values2 [] = { 1, 2, 3, 4, 5 }; + TEST_INTRUSIVE_SEQUENCE( init_values2, list.begin() ); + } +} + +//test: merge due to error in merge implementation: +template +void test_list + ::test_merge (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, 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: assign, insert, const_iterator, const_reverse_iterator, erase, s_iterator_to: template diff --git a/test/slist_test.cpp b/test/slist_test.cpp index 698fea8..c8055c1 100644 --- a/test/slist_test.cpp +++ b/test/slist_test.cpp @@ -24,25 +24,28 @@ using namespace boost::intrusive; -template +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_all(std::vector& values); + static void test_front(std::vector& values); + static void test_back(std::vector& values, detail::bool_); + static void test_back(std::vector& values, detail::bool_); static void test_sort(std::vector& values); - static void test_merge (std::vector& values); + static void test_merge(std::vector& values); + static void test_remove_unique(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_slow_insert(std::vector& values); + static void test_clone(std::vector& values); static void test_container_from_end(std::vector &, detail::bool_){} static void test_container_from_end(std::vector &values, detail::bool_); }; -template -void test_slist +template +void test_slist ::test_all (std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -52,6 +55,7 @@ void test_slist , size_type , constant_time_size , linear + , cache_last > list_type; { list_type list(values.begin(), values.end()); @@ -60,9 +64,11 @@ void test_slist list.insert(list.end(), values.begin(), values.end()); test::test_sequence_container(list, values); } - test_front_back (values); + test_front(values); + test_back(values, detail::bool_()); test_sort(values); test_merge (values); + test_remove_unique(values); test_insert(values); test_shift(values); test_slow_insert (values); @@ -72,9 +78,9 @@ void test_slist } //test: push_front, pop_front, front, size, empty: -template -void test_slist - ::test_front_back (std::vector& values) +template +void test_slist + ::test_front(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef slist @@ -83,6 +89,7 @@ void test_slist , size_type , constant_time_size , linear + , cache_last > list_type; list_type testlist; BOOST_TEST (testlist.empty()); @@ -101,11 +108,45 @@ void test_slist testlist.pop_front(); BOOST_TEST (testlist.empty()); -} +} + +//test: push_front, pop_front, front, size, empty: +template +void test_slist + ::test_back(std::vector& values, detail::bool_) +{ + typedef typename ValueTraits::value_type value_type; + typedef slist + < value_type + , value_traits + , size_type + , constant_time_size + , linear + , cache_last + > 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_back(values[1]); + BOOST_TEST(*testlist.previous(testlist.end()) == values[1]); + BOOST_TEST (&testlist.front() == &values[0]); + BOOST_TEST (&testlist.back() == &values[1]); +} + +//test: push_front, pop_front, front, size, empty: +template +void test_slist + ::test_back(std::vector&, detail::bool_) +{} + //test: merge due to error in merge implementation: -template -void test_slist +template +void test_slist ::test_merge (std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -115,6 +156,7 @@ void test_slist , size_type , constant_time_size , linear + , cache_last > list_type; list_type testlist1, testlist2; testlist1.push_front (values[0]); @@ -127,9 +169,42 @@ void test_slist TEST_INTRUSIVE_SEQUENCE( init_values, testlist1.begin() ); } +//test: merge due to error in merge implementation: +template +void test_slist + ::test_remove_unique (std::vector& values) +{ + typedef typename ValueTraits::value_type value_type; + typedef slist + < value_type + , value_traits + , size_type + , constant_time_size + , linear + , cache_last + > list_type; + { + list_type list(values.begin(), values.end()); + list.remove_if(is_even()); + int init_values [] = { 1, 3, 5 }; + TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() ); + } + { + std::vector values2(values); + list_type list(values.begin(), values.end()); + list.insert_after(list.before_begin(), values2.begin(), values2.end()); + list.sort(); + int init_values [] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 }; + TEST_INTRUSIVE_SEQUENCE( init_values, list.begin() ); + list.unique(); + int init_values2 [] = { 1, 2, 3, 4, 5 }; + TEST_INTRUSIVE_SEQUENCE( init_values2, list.begin() ); + } +} + //test: constructor, iterator, sort, reverse: -template -void test_slist +template +void test_slist ::test_sort(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -139,6 +214,7 @@ void test_slist , size_type , constant_time_size , linear + , cache_last > list_type; list_type testlist (values.begin(), values.end()); @@ -155,8 +231,8 @@ void test_slist } //test: assign, insert_after, const_iterator, erase_after, s_iterator_to, previous: -template -void test_slist +template +void test_slist ::test_insert(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -166,6 +242,7 @@ void test_slist , size_type , constant_time_size , linear + , cache_last > list_type; list_type testlist; testlist.assign (&values[0] + 2, &values[0] + 5); @@ -195,8 +272,8 @@ void test_slist } //test: insert, const_iterator, erase, siterator_to: -template -void test_slist +template +void test_slist ::test_slow_insert (std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -206,6 +283,7 @@ void test_slist , size_type , constant_time_size , linear + , cache_last > list_type; list_type testlist; testlist.push_front (values[4]); @@ -239,8 +317,8 @@ void test_slist BOOST_TEST (testlist.front().value_ == 3); } -template -void test_slist +template +void test_slist ::test_shift(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -250,6 +328,7 @@ void test_slist , size_type , constant_time_size , linear + , cache_last > list_type; list_type testlist; @@ -285,8 +364,8 @@ void test_slist } //test: insert_after (seq-version), swap, splice_after: -template -void test_slist +template +void test_slist ::test_swap(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -296,6 +375,7 @@ void test_slist , size_type , constant_time_size , linear + , cache_last > list_type; { list_type testlist1 (&values[0], &values[0] + 2); @@ -363,8 +443,8 @@ void test_slist } } -template -void test_slist +template +void test_slist ::test_clone(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -374,6 +454,7 @@ void test_slist , size_type , constant_time_size , linear + , cache_last > list_type; list_type testlist1 (&values[0], &values[0] + values.size()); @@ -385,8 +466,8 @@ void test_slist BOOST_TEST (testlist2.empty()); } -template -void test_slist +template +void test_slist ::test_container_from_end(std::vector& values ,detail::bool_) { @@ -397,6 +478,7 @@ void test_slist , size_type , constant_time_size , linear + , cache_last > list_type; list_type testlist1 (&values[0], &values[0] + values.size()); BOOST_TEST (testlist1 == list_type::container_from_end_iterator(testlist1.end())); @@ -419,6 +501,7 @@ class test_main_template , typename value_type::slist_base_hook_t >::type , false + , false >::test_all(data); test_slist < typename detail::get_member_value_traits < value_type @@ -428,6 +511,7 @@ class test_main_template > >::type , false + , false >::test_all(data); //Now linear slists @@ -436,6 +520,46 @@ class test_main_template , typename value_type::slist_base_hook_t >::type , true + , false + >::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 + , true + , false + >::test_all(data); + + //Now the same but caching the last node + test_slist < typename detail::get_base_value_traits + < value_type + , typename value_type::slist_base_hook_t + >::type + , false + , true + >::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 + , false + , true + >::test_all(data); + + //Now linear slists + test_slist < typename detail::get_base_value_traits + < value_type + , typename value_type::slist_base_hook_t + >::type + , true + , true >::test_all(data); test_slist < typename detail::get_member_value_traits @@ -446,8 +570,8 @@ class test_main_template > >::type , true + , true >::test_all(data); - return 0; } }; @@ -468,6 +592,7 @@ class test_main_template , typename value_type::slist_base_hook_t >::type , false + , false >::test_all(data); test_slist < typename detail::get_member_value_traits @@ -478,6 +603,7 @@ class test_main_template > >::type , false + , false >::test_all(data); test_slist < typename detail::get_base_value_traits @@ -485,6 +611,7 @@ class test_main_template , typename value_type::slist_auto_base_hook_t >::type , false + , false >::test_all(data); test_slist < typename detail::get_member_value_traits @@ -495,6 +622,7 @@ class test_main_template > >::type , false + , false >::test_all(data); test_slist < typename detail::get_base_value_traits @@ -502,6 +630,27 @@ class test_main_template , typename value_type::slist_base_hook_t >::type , true + , false + >::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 + , true + , false + >::test_all(data); + + //Now cache last + test_slist < typename detail::get_base_value_traits + < value_type + , typename value_type::slist_base_hook_t + >::type + , false + , true >::test_all(data); test_slist < typename detail::get_member_value_traits @@ -511,6 +660,26 @@ class test_main_template , &value_type::slist_node_ > >::type + , false + , true + >::test_all(data); + + test_slist < typename detail::get_base_value_traits + < value_type + , typename value_type::slist_base_hook_t + >::type + , true + , true + >::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 + , true , true >::test_all(data); return 0; From a4e2dcbc76f161b02a79bd712ee23f3f05984af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Sat, 26 Jan 2008 11:52:25 +0000 Subject: [PATCH 05/20] Refactored common slist functions in a single class [SVN r42982] --- .../detail/common_slist_algorithms.hpp | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 include/boost/intrusive/detail/common_slist_algorithms.hpp diff --git a/include/boost/intrusive/detail/common_slist_algorithms.hpp b/include/boost/intrusive/detail/common_slist_algorithms.hpp new file mode 100644 index 0000000..4f4a8f1 --- /dev/null +++ b/include/boost/intrusive/detail/common_slist_algorithms.hpp @@ -0,0 +1,95 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2007-2008 +// +// 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_COMMON_SLIST_ALGORITHMS_HPP +#define BOOST_INTRUSIVE_COMMON_SLIST_ALGORITHMS_HPP + +#include +#include +#include +#include + +namespace boost { +namespace intrusive { +namespace detail { + +template +class common_slist_algorithms +{ + public: + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + typedef NodeTraits node_traits; + + static node_ptr get_previous_node(node_ptr prev_init_node, node_ptr this_node) + { + node_ptr p = prev_init_node; + for( node_ptr p_next + ; this_node != (p_next = NodeTraits::get_next(p)) + ; p = p_next){ + //Logic error: possible use of linear lists with + //operations only permitted with lists + BOOST_INTRUSIVE_INVARIANT_ASSERT(p); + } + return p; + } + + static void init_header(node_ptr this_node) + { NodeTraits::set_next(this_node, this_node); } + + static void init(node_ptr this_node) + { NodeTraits::set_next(this_node, 0); } + + static bool unique(const_node_ptr this_node) + { + node_ptr next = NodeTraits::get_next(this_node); + return !next || next == this_node; + } + + static bool inited(const_node_ptr this_node) + { return !NodeTraits::get_next(this_node); } + + static void unlink_after(node_ptr prev_node) + { + node_ptr this_node(NodeTraits::get_next(prev_node)); + NodeTraits::set_next(prev_node, NodeTraits::get_next(this_node)); + } + + static void unlink_after(node_ptr prev_node, node_ptr last_node) + { NodeTraits::set_next(prev_node, last_node); } + + static void link_after(node_ptr prev_node, node_ptr this_node) + { + NodeTraits::set_next(this_node, NodeTraits::get_next(prev_node)); + NodeTraits::set_next(prev_node, this_node); + } + + static void transfer_after(node_ptr p, node_ptr b, node_ptr e) + { + if (p != b && p != e) { + node_ptr next_b = NodeTraits::get_next(b); + node_ptr next_e = NodeTraits::get_next(e); + node_ptr next_p = NodeTraits::get_next(p); + NodeTraits::set_next(b, next_e); + NodeTraits::set_next(e, next_p); + NodeTraits::set_next(p, next_b); + } + } +}; + +} //namespace detail +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_COMMON_SLIST_ALGORITHMS_HPP From 686c081bbafe1cf100248b71e57ff49a99c70e34 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sat, 9 Feb 2008 12:29:02 +0000 Subject: [PATCH 06/20] Fix a link in the intrusive redirect. [SVN r43188] --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 8c018f9..23984f2 100644 --- a/index.html +++ b/index.html @@ -4,6 +4,6 @@ Automatic redirection failed, please go to -../../doc/html/intrusive +../../doc/html/intrusive.html From cd2a6c33683abcfb35f8b12c5dac3ceaeafe72a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Sun, 16 Mar 2008 23:55:49 +0000 Subject: [PATCH 07/20] Fixed ticket #1683 [SVN r43662] --- include/boost/intrusive/unordered_set.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/intrusive/unordered_set.hpp b/include/boost/intrusive/unordered_set.hpp index 392e83d..780fb46 100644 --- a/include/boost/intrusive/unordered_set.hpp +++ b/include/boost/intrusive/unordered_set.hpp @@ -557,7 +557,7 @@ class unordered_set_impl //! Complexity: Average case O(1), worst case O(this->size()). //! //! Throws: If hash_func or equal_func throw. - template + template size_type count(const KeyType& key, KeyHasher hash_func, KeyValueEqual equal_func) const { return table_.find(key, hash_func, equal_func) != end(); } @@ -1488,7 +1488,7 @@ class unordered_multiset_impl //! Complexity: Average case O(1), worst case O(this->size()). //! //! Throws: If the internal hasher or the equality functor throws. - template + template size_type count(const KeyType& key, KeyHasher hash_func, KeyValueEqual equal_func) const { return table_.count(key, hash_func, equal_func); } From daea35153f006e2fb6ee4aa2302bd1bd01782c0c Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sun, 27 Apr 2008 07:39:49 +0000 Subject: [PATCH 08/20] Merge in documentation fixes. Apart from the change to optional's documenation Jamfile, which I included by mistake. Fixes #1659, #1661, #1684, #1685, 1687, #1690, #1801 I wrote about this at: http://lists.boost.org/Archives/boost/2008/04/136405.php Merged revisions 44585-44806 via svnmerge from https://svn.boost.org/svn/boost/branches/doc ........ r44585 | danieljames | 2008-04-19 16:25:27 +0100 (Sat, 19 Apr 2008) | 2 lines Fix broken link to vacpp in bjam docs. Refs #1512 ........ r44586 | danieljames | 2008-04-19 16:27:36 +0100 (Sat, 19 Apr 2008) | 2 lines Fix broken link to bcpp in bjam docs. Refs #1513 ........ r44587 | danieljames | 2008-04-19 16:33:58 +0100 (Sat, 19 Apr 2008) | 2 lines DateTime documentation - Fix a link to the serialization library. Refs #1659 ........ r44588 | danieljames | 2008-04-19 16:35:36 +0100 (Sat, 19 Apr 2008) | 2 lines Fix some links in interprocess & intrusive. Refs #1661 ........ r44589 | danieljames | 2008-04-19 16:37:39 +0100 (Sat, 19 Apr 2008) | 2 lines Fix some links in the python docs. Refs #1684. ........ r44590 | danieljames | 2008-04-19 16:38:29 +0100 (Sat, 19 Apr 2008) | 2 lines Work around a quickbook bug which is affecting the python docs. Refs #1684. ........ r44591 | danieljames | 2008-04-19 16:39:34 +0100 (Sat, 19 Apr 2008) | 2 lines Fix a broken link in the numeric conversion docs. Refs #1685 ........ r44592 | danieljames | 2008-04-19 16:40:45 +0100 (Sat, 19 Apr 2008) | 2 lines Fix some links in the optional docs. Refs #1687 ........ r44593 | danieljames | 2008-04-19 16:42:09 +0100 (Sat, 19 Apr 2008) | 2 lines Fix link to the hash documentation from bimap. Refs #1690 ........ r44599 | danieljames | 2008-04-19 18:07:33 +0100 (Sat, 19 Apr 2008) | 2 lines Fix a typo in the format library. Refs #1801 ........ r44600 | danieljames | 2008-04-19 19:20:59 +0100 (Sat, 19 Apr 2008) | 1 line Initialise svnmerge. ........ r44641 | danieljames | 2008-04-20 18:59:47 +0100 (Sun, 20 Apr 2008) | 2 lines Fix the lincense url in shared container iterator documentation. ........ r44642 | danieljames | 2008-04-20 19:00:00 +0100 (Sun, 20 Apr 2008) | 2 lines Fix image link in the mpi documentation. ........ r44643 | danieljames | 2008-04-20 19:00:11 +0100 (Sun, 20 Apr 2008) | 2 lines Fix a typo in the spirit docs. ........ r44644 | danieljames | 2008-04-20 19:00:23 +0100 (Sun, 20 Apr 2008) | 2 lines Escape the slash so that quickbook doesn't think it the start of an italic section, and mess up the link. Refs #1844 ........ r44647 | danieljames | 2008-04-20 19:39:47 +0100 (Sun, 20 Apr 2008) | 2 lines Fix another typo in spirit docs. ........ [SVN r44807] --- doc/intrusive.qbk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/intrusive.qbk b/doc/intrusive.qbk index 6d9d140..3a84c3f 100644 --- a/doc/intrusive.qbk +++ b/doc/intrusive.qbk @@ -3011,7 +3011,7 @@ and also derives from `test_class`. `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. +[@../../libs/intrusive/perf/perf_list.cpp perf_list.cpp] source file. [section:performance_results_push_back Back insertion and destruction] From 7e50778757c77d2384d5ed6fe826f9d088ae3f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Sun, 27 Apr 2008 15:03:06 +0000 Subject: [PATCH 09/20] Intrusive: * Added `linear<>` and `cache_last<>` options to singly linked lists. * Added `optimize_multikey<>` option to unordered container hooks. * Optimized unordered containers when `store_hash` option is used in the hook. * Implementation changed to be exception agnostic so that it can be used in environments without exceptions. * Added `container_from_iterator` function to tree-based containers. Interprocess: * Added anonymous shared memory for UNIX systems. * Fixed file lock compilation errors [SVN r44819] --- doc/intrusive.qbk | 172 +- include/boost/intrusive/avl_set.hpp | 70 +- include/boost/intrusive/avltree.hpp | 31 + .../boost/intrusive/avltree_algorithms.hpp | 11 +- .../intrusive/circular_list_algorithms.hpp | 4 +- .../intrusive/circular_slist_algorithms.hpp | 12 +- .../boost/intrusive/detail/avltree_node.hpp | 10 +- .../detail/common_slist_algorithms.hpp | 2 +- .../intrusive/detail/ebo_functor_holder.hpp | 31 +- .../boost/intrusive/detail/hashtable_node.hpp | 44 +- include/boost/intrusive/detail/list_node.hpp | 5 +- include/boost/intrusive/detail/mpl.hpp | 20 + .../detail/no_exceptions_support.hpp | 28 - .../boost/intrusive/detail/rbtree_node.hpp | 16 +- include/boost/intrusive/detail/slist_node.hpp | 2 +- .../intrusive/detail/tree_algorithms.hpp | 175 +- include/boost/intrusive/detail/tree_node.hpp | 5 + include/boost/intrusive/detail/utilities.hpp | 83 +- include/boost/intrusive/hashtable.hpp | 1548 ++++++++++++----- include/boost/intrusive/intrusive_fwd.hpp | 6 +- .../intrusive/linear_slist_algorithms.hpp | 14 +- include/boost/intrusive/list.hpp | 18 +- include/boost/intrusive/options.hpp | 35 + .../boost/intrusive/pointer_plus_2_bits.hpp | 82 - include/boost/intrusive/pointer_plus_bit.hpp | 78 - include/boost/intrusive/pointer_plus_bits.hpp | 81 + include/boost/intrusive/rbtree.hpp | 60 +- include/boost/intrusive/rbtree_algorithms.hpp | 11 +- include/boost/intrusive/set.hpp | 85 +- include/boost/intrusive/sg_set.hpp | 68 + include/boost/intrusive/sgtree.hpp | 25 + include/boost/intrusive/sgtree_algorithms.hpp | 11 +- include/boost/intrusive/slist.hpp | 58 +- include/boost/intrusive/splay_set.hpp | 68 + include/boost/intrusive/splaytree.hpp | 25 + .../boost/intrusive/splaytree_algorithms.hpp | 64 +- include/boost/intrusive/unordered_set.hpp | 58 +- .../boost/intrusive/unordered_set_hook.hpp | 126 +- index.html | 2 +- perf/perf_list.cpp | 2 +- .../vc7ide/_intrusivelib/_intrusivelib.vcproj | 8 +- proj/vc7ide/to-do.txt | 11 + test/avl_set_test.cpp | 1 - test/generic_assoc_test.hpp | 31 + test/generic_set_test.hpp | 1 + test/itestvalue.hpp | 61 +- test/smart_ptr.hpp | 56 +- test/unordered_multiset_test.cpp | 233 ++- test/unordered_set_test.cpp | 48 +- 49 files changed, 2616 insertions(+), 1080 deletions(-) delete mode 100644 include/boost/intrusive/detail/no_exceptions_support.hpp delete mode 100644 include/boost/intrusive/pointer_plus_2_bits.hpp delete mode 100644 include/boost/intrusive/pointer_plus_bit.hpp create mode 100644 include/boost/intrusive/pointer_plus_bits.hpp create mode 100644 proj/vc7ide/to-do.txt diff --git a/doc/intrusive.qbk b/doc/intrusive.qbk index 3a84c3f..bf8a604 100644 --- a/doc/intrusive.qbk +++ b/doc/intrusive.qbk @@ -528,7 +528,25 @@ chapters: 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. +* [*set/multiset/rbtree]: A `std::set/std::multiset` like intrusive associative containers + based on red-black trees. + The size overhead is moderate for user classes (usually the size of three pointers). + Many operations have logarithmic time complexity. + +* [*avl_set/avl_multiset/avltree]: A `std::set/std::multiset` like intrusive associative + containers based on AVL trees. + The size overhead is moderate for user classes (usually the size of three pointers). + Many operations have logarithmic time complexity. + +* [*splay_set/splay_multiset/splaytree]: A `std::set/std::multiset` like intrusive associative + containers based on splay trees. Splay trees have no constant operations, but they + have some interesting caching properties. + The size overhead is moderate for user classes (usually the size of three pointers). + Many operations have logarithmic time complexity. + +* [*sg_set/sg_multiset/sgtree]: A `std::set/std::multiset` like intrusive associative + containers based on scapegoat trees. Scapegoat can be configured with the desired + balance factor to achieve the desised rebalancing frequency/search time compromise. The size overhead is moderate for user classes (usually the size of three pointers). Many operations have logarithmic time complexity. @@ -539,7 +557,7 @@ chapters: 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 +Most 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 @@ -753,7 +771,7 @@ linear time complexity, even some that usually are constant time, like 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. +constant-time functions with a slightly bigger size overhead. However, for some applications like constructing more elaborated containers, singly linked lists are essential because of their low size overhead. @@ -784,7 +802,8 @@ Like the rest of [*Boost.Intrusive] containers, 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 +[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, @@ -807,7 +826,7 @@ the section [link intrusive.usage How to use Boost.Intrusive]: template class slist; -[classref boost::intrusive::slist slist] receives the same options explained in +[classref boost::intrusive::slist slist] receives the options explained in the section [link intrusive.usage How to use Boost.Intrusive]: * [*`base_hook`] / [*`member_hook`] / @@ -819,7 +838,23 @@ the section [link intrusive.usage How to use Boost.Intrusive]: Default: `constant_time_size` * [*`size_type`]: To specify the type that will be used to store the size - of the container. Default: `size_type` + of the container. Default: `size_type`. + +[classref boost::intrusive::slist slist] can receive additional options: + +* [*`linear`]: the singly linked list is implemented as a + null-terminated list instead of a circular list. This allows `O(1)` swap, + but losses some operations like `container_from_end_iterator`. +* [*`cache_last`]: the singly linked also stores a pointer to the + last element of the singly linked list. This allows `O(1)` swap, + `splice_after(iterator, slist &)` and makes the list offer new functions + like `push_back(reference)` and `back()`. Logically, the size an empty list is + increased in `sizeof(void_pointer)` and the the cached last node pointer must + be updated in every operation, and that might incur in a slight performance impact. + +`auto_unlink` hooks are not usable if `linear` and/or `cache_last` options are +used. If `auto_unlink` hooks are used and those options are specified, a static +assertion will be raised. [endsect] @@ -1151,6 +1186,11 @@ Apart from them, these hooks offer additional options: rehashing is frequent or hashing the value is a slow operation. Default: `store_hash`. +* [*`optimize_multikey`]: This option reserves additional space in + the hook that will be used to group equal elements in unordered multisets, + improving significantly the performance when many equal values are inserted + in these containers. Default: `optimize_multikey`. + [endsect] [section:unordered_set_unordered_multiset_containers unordered_set and unordered_multiset containers] @@ -1210,7 +1250,7 @@ receive the same options explained in the section * [*`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: +And they also can receive additional options: * [*`equal`]: Equality function for the objects to be inserted in containers. Default: `equal< std::equal_to >` @@ -1228,7 +1268,14 @@ And they also can receive two additional options: 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`. + Default: `power_2_buckets`. + +* [*`cache_begin`]: Due to its internal structure, finding the first + element of an unordered container (`begin()` operation) is + amortized constant-time. It's possible to speed up `begin()` and other operations + related to it (like `clear()`) if the container caches internally the position + of the first element. This imposes the overhead of one pointer to the size + of the container. Default: `cache_begin`. [endsect] @@ -1301,13 +1348,13 @@ more frequently accessed than others, splay trees perform faster searches than e 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 +rebalanced when an 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]. +[classref boost::intrusive::multiset multiset]. 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 @@ -1565,22 +1612,22 @@ time, scapegoat trees have no additional per-node overhead compared to a regular 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 +of the root, and half on the right. An a-height-balanced tree is defined with defined with the following equation: -[*['height(tree) <= log1/α(tree.size())]] +[*['height(tree) <= log1/a(tree.size())]] -* [*['α == 1]]: A tree forming a linked list is considered balanced. -* [*['α == 0.5]]: Only a perfectly balanced binary is considered balanced. +* [*['a == 1]]: A tree forming a linked list is considered balanced. +* [*['a == 0.5]]: Only a perfectly balanced binary is considered balanced. -Scapegoat trees are loosely ['α-height-balanced] so: +Scapegoat trees are loosely ['a-height-balanced] so: -[*['height(tree) <= log1/α(tree.size()) + 1]] +[*['height(tree) <= log1/a(tree.size()) + 1]] -Scapegoat trees support any α between 0.5 and 1. If α is higher, the tree is rebalanced +Scapegoat trees support any a between 0.5 and 1. If a 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. +a values improve search times. Scapegoat-trees implemented in [*Boost.Intrusive] offer the possibility of +[*changing a 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: @@ -1591,7 +1638,7 @@ Scapegoat trees also have downsides: 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 + operations like ['log1/a]. 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: @@ -1689,9 +1736,9 @@ And they also can receive additional options: * [*`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 + the balance factor a 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 + performance. The fixed a factor that is used when this option is activated is ['1/sqrt(2) ~ 0,70711]. Default: `floating_point` [endsect] @@ -2523,6 +2570,63 @@ For a complete list of functions see [endsect] +[/ +/ +/[section:sgtree_algorithms Intrusive sg tree algorithms] +/ +/ +/[classref boost::intrusive::sgtree_algorithms sgtree_algorithms] have the same +/interface as [classref boost::intrusive::rbtree_algorithms rbtree_algorithms]. +/ +/[c++] +/ +/ template +/ struct sgtree_algorithms; +/ +/[classref boost::intrusive::sgtree_algorithms sgtree_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 sgtree +/ +/* `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_sgtree_algorithms.cpp] +/[doc_sgtree_algorithms_code] +/ +/For a complete list of functions see +/[classref boost::intrusive::sgtree_algorithms sgtree_algorithms reference]. +/ +/[endsect] +/] [endsect] [section:value_traits Containers with custom ValueTraits] @@ -3104,7 +3208,7 @@ 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)]] + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (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]] @@ -3310,18 +3414,18 @@ all the objects to be inserted in intrusive containers in containers like `std:: [endsect] -[section:disabling_exceptions Disabling exceptions support] +[section:release_notes Release Notes] -[*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. +[section:release_notes_boost_1_36_00 Boost 1.36 Release] -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. +* Added `linear<>` and `cache_last<>` options to singly linked lists. +* Added `optimize_multikey<>` option to unordered container hooks. +* Optimized unordered containers when `store_hash` option is used in the hook. +* Implementation changed to be exception agnostic so that it can be used + in environments without exceptions. +* Added `container_from_iterator` function to tree-based containers. + +[endsect] [endsect] @@ -3331,13 +3435,13 @@ exception handling in the library. * Visual 7.1/WinXP * Visual 8.0/WinXP +* Visual 9.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] diff --git a/include/boost/intrusive/avl_set.hpp b/include/boost/intrusive/avl_set.hpp index 8ff22a4..8429f13 100644 --- a/include/boost/intrusive/avl_set.hpp +++ b/include/boost/intrusive/avl_set.hpp @@ -240,7 +240,7 @@ class avl_set_impl //! Precondition: end_iterator must be a valid end const_iterator //! of avl_set. //! - //! Effects: Returns a const reference to the avl_set associated to the end iterator + //! Effects: Returns a const reference to the set associated to the end iterator //! //! Throws: Nothing. //! @@ -252,6 +252,34 @@ class avl_set_impl , &avl_set_impl::tree_); } + //! Precondition: it must be a valid iterator of set. + //! + //! Effects: Returns a reference to the set associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static avl_set_impl &container_from_iterator(iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &avl_set_impl::tree_); + } + + //! Precondition: it must be a valid const_iterator of set. + //! + //! Effects: Returns a const reference to the set associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const avl_set_impl &container_from_iterator(const_iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &avl_set_impl::tree_); + } + //! Effects: Returns the key_compare object used by the avl_set. //! //! Complexity: Constant. @@ -1085,6 +1113,12 @@ class avl_set static const avl_set &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static avl_set &container_from_iterator(iterator end_iterator) + { return static_cast(Base::container_from_iterator(end_iterator)); } + + static const avl_set &container_from_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_iterator(end_iterator)); } }; #endif @@ -1317,6 +1351,34 @@ class avl_multiset_impl , &avl_multiset_impl::tree_); } + //! Precondition: it must be a valid iterator of multiset. + //! + //! Effects: Returns a const reference to the multiset associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static avl_multiset_impl &container_from_iterator(iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &avl_multiset_impl::tree_); + } + + //! Precondition: it must be a valid const_iterator of multiset. + //! + //! Effects: Returns a const reference to the multiset associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const avl_multiset_impl &container_from_iterator(const_iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &avl_multiset_impl::tree_); + } + //! Effects: Returns the key_compare object used by the avl_multiset. //! //! Complexity: Constant. @@ -2057,6 +2119,12 @@ class avl_multiset static const avl_multiset &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static avl_multiset &container_from_iterator(iterator end_iterator) + { return static_cast(Base::container_from_iterator(end_iterator)); } + + static const avl_multiset &container_from_iterator(const_iterator end_iterator) + { return static_cast(Base::container_from_iterator(end_iterator)); } }; #endif diff --git a/include/boost/intrusive/avltree.hpp b/include/boost/intrusive/avltree.hpp index 86a2957..afd6bd4 100644 --- a/include/boost/intrusive/avltree.hpp +++ b/include/boost/intrusive/avltree.hpp @@ -387,6 +387,28 @@ class avltree_impl static const avltree_impl &container_from_end_iterator(const_iterator end_iterator) { return priv_container_from_end_iterator(end_iterator); } + //! Precondition: it must be a valid iterator + //! of rbtree. + //! + //! Effects: Returns a const reference to the tree associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static avltree_impl &container_from_iterator(iterator it) + { return priv_container_from_iterator(it); } + + //! Precondition: it must be a valid end const_iterator + //! of rbtree. + //! + //! Effects: Returns a const reference to the tree associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const avltree_impl &container_from_iterator(const_iterator it) + { return priv_container_from_iterator(it); } + //! Effects: Returns the value_compare object used by the tree. //! //! Complexity: Constant. @@ -1233,6 +1255,9 @@ class avltree_impl avltree_impl *avl = detail::parent_from_member(d, &avltree_impl::data_); return *avl; } + + static avltree_impl &priv_container_from_iterator(const const_iterator &it) + { return priv_container_from_end_iterator(it.end_iterator_from_it()); } }; #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED @@ -1426,6 +1451,12 @@ class avltree static const avltree &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static avltree &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const avltree &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } }; #endif diff --git a/include/boost/intrusive/avltree_algorithms.hpp b/include/boost/intrusive/avltree_algorithms.hpp index 94d046f..145e0fb 100644 --- a/include/boost/intrusive/avltree_algorithms.hpp +++ b/include/boost/intrusive/avltree_algorithms.hpp @@ -20,7 +20,6 @@ #include #include -#include #include #include @@ -641,6 +640,16 @@ class avltree_algorithms rebalance_after_insertion(header, new_value); } + //! Requires: "n" must be a node inserted in a tree. + //! + //! Effects: Returns a pointer to the header node of the tree. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + static node_ptr get_header(node_ptr n) + { return tree_algorithms::get_header(n); } + /// @cond private: diff --git a/include/boost/intrusive/circular_list_algorithms.hpp b/include/boost/intrusive/circular_list_algorithms.hpp index 97438bf..537b151 100644 --- a/include/boost/intrusive/circular_list_algorithms.hpp +++ b/include/boost/intrusive/circular_list_algorithms.hpp @@ -62,8 +62,8 @@ class circular_list_algorithms //! Throws: Nothing. static void init(node_ptr this_node) { - NodeTraits::set_next(this_node, 0); - NodeTraits::set_previous(this_node, 0); + NodeTraits::set_next(this_node, node_ptr(0)); + NodeTraits::set_previous(this_node, node_ptr(0)); } //! Effects: Returns true is "this_node" is in a non-used state diff --git a/include/boost/intrusive/circular_slist_algorithms.hpp b/include/boost/intrusive/circular_slist_algorithms.hpp index 73e4590..2a257e4 100644 --- a/include/boost/intrusive/circular_slist_algorithms.hpp +++ b/include/boost/intrusive/circular_slist_algorithms.hpp @@ -305,12 +305,12 @@ class circular_slist_algorithms static node_ptr move_backwards(node_ptr p, std::size_t n) { //Null shift, nothing to do - if(!n) return 0; + if(!n) return node_ptr(0); node_ptr first = NodeTraits::get_next(p); //count() == 1 or 2, nothing to do if(NodeTraits::get_next(first) == p) - return 0; + return node_ptr(0); bool end_found = false; node_ptr new_last(0); @@ -326,7 +326,7 @@ class circular_slist_algorithms //Shortcut the shift with the modulo of the size of the list n %= i; if(!n) - return 0; + return node_ptr(0); i = 0; //Unlink p and continue the new first node search first = NodeTraits::get_next(p); @@ -357,11 +357,11 @@ class circular_slist_algorithms static node_ptr move_forward(node_ptr p, std::size_t n) { //Null shift, nothing to do - if(!n) return 0; + if(!n) return node_ptr(0); node_ptr first = node_traits::get_next(p); //count() == 1 or 2, nothing to do - if(node_traits::get_next(first) == p) return 0; + if(node_traits::get_next(first) == p) return node_ptr(0); //Iterate until p is found to know where the current last node is. //If the shift count is less than the size of the list, we can also obtain @@ -380,7 +380,7 @@ class circular_slist_algorithms //Shortcut the shift with the modulo of the size of the list std::size_t new_before_last_pos = (distance - (n % distance))% distance; //If the shift is a multiple of the size there is nothing to do - if(!new_before_last_pos) return 0; + if(!new_before_last_pos) return node_ptr(0); for( new_last = p ; new_before_last_pos-- diff --git a/include/boost/intrusive/detail/avltree_node.hpp b/include/boost/intrusive/detail/avltree_node.hpp index f60f7db..92458a4 100644 --- a/include/boost/intrusive/detail/avltree_node.hpp +++ b/include/boost/intrusive/detail/avltree_node.hpp @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include namespace boost { @@ -111,7 +111,7 @@ struct compact_avltree_node_traits_impl ::type const_node_ptr; typedef typename node::balance balance; - typedef pointer_plus_2_bits ptr_bit; + typedef pointer_plus_bits ptr_bit; static node_ptr get_parent(const_node_ptr n) { return ptr_bit::get_pointer(n->parent_); } @@ -148,7 +148,7 @@ struct compact_avltree_node_traits_impl }; //Dispatches the implementation based on the boolean -template +template struct avltree_node_traits_dispatch : public default_avltree_node_traits_impl {}; @@ -164,10 +164,10 @@ struct avltree_node_traits : public avltree_node_traits_dispatch < VoidPointer , OptimizeSize && - has_pointer_plus_2_bits + max_pointer_plus_bits < VoidPointer , detail::alignment_of >::value - >::value + >::value >= 2u > {}; diff --git a/include/boost/intrusive/detail/common_slist_algorithms.hpp b/include/boost/intrusive/detail/common_slist_algorithms.hpp index 4f4a8f1..cc37a74 100644 --- a/include/boost/intrusive/detail/common_slist_algorithms.hpp +++ b/include/boost/intrusive/detail/common_slist_algorithms.hpp @@ -47,7 +47,7 @@ class common_slist_algorithms { NodeTraits::set_next(this_node, this_node); } static void init(node_ptr this_node) - { NodeTraits::set_next(this_node, 0); } + { NodeTraits::set_next(this_node, node_ptr(0)); } static bool unique(const_node_ptr this_node) { diff --git a/include/boost/intrusive/detail/ebo_functor_holder.hpp b/include/boost/intrusive/detail/ebo_functor_holder.hpp index 69c50cd..696944f 100644 --- a/include/boost/intrusive/detail/ebo_functor_holder.hpp +++ b/include/boost/intrusive/detail/ebo_functor_holder.hpp @@ -23,8 +23,15 @@ template class ebo_functor_holder_impl { public: - ebo_functor_holder_impl(){} - ebo_functor_holder_impl(const T& t):t(t){} + ebo_functor_holder_impl() + {} + ebo_functor_holder_impl(const T& t) + : t(t) + {} + template + ebo_functor_holder_impl(const Arg1& arg1, const Arg2& arg2) + : t(arg1, arg2) + {} T& get(){return t;} const T& get()const{return t;} @@ -38,8 +45,15 @@ class ebo_functor_holder_impl : public T { public: - ebo_functor_holder_impl(){} - ebo_functor_holder_impl(const T& t):T(t){} + ebo_functor_holder_impl() + {} + ebo_functor_holder_impl(const T& t) + : T(t) + {} + template + ebo_functor_holder_impl(const Arg1& arg1, const Arg2& arg2) + : T(arg1, arg2) + {} T& get(){return *this;} const T& get()const{return *this;} @@ -54,7 +68,14 @@ class ebo_functor_holder public: ebo_functor_holder(){} - ebo_functor_holder(const T& t):super(t){} + ebo_functor_holder(const T& t) + : super(t) + {} + + template + ebo_functor_holder(const Arg1& arg1, const Arg2& arg2) + : super(arg1, arg2) + {} ebo_functor_holder& operator=(const ebo_functor_holder& x) { diff --git a/include/boost/intrusive/detail/hashtable_node.hpp b/include/boost/intrusive/detail/hashtable_node.hpp index 0a8e85d..66bec59 100644 --- a/include/boost/intrusive/detail/hashtable_node.hpp +++ b/include/boost/intrusive/detail/hashtable_node.hpp @@ -50,6 +50,7 @@ const std::size_t prime_list_holder::prime_list_size template struct bucket_impl : public Slist { + typedef Slist slist_type; bucket_impl() {} @@ -69,28 +70,6 @@ struct bucket_impl : public Slist //Slist::clear(); return *this; } - - static typename Slist::difference_type get_bucket_num - ( typename Slist::const_iterator it - , const bucket_impl &first_bucket - , const bucket_impl &last_bucket) - { - typename Slist::const_iterator - first(first_bucket.cend()), last(last_bucket.cend()); - - //The end node is embedded in the singly linked list: - //iterate until we reach it. - while(!(first.pointed_node() <= it.pointed_node() && - it.pointed_node() <= last.pointed_node())){ - ++it; - } - //Now get the bucket_impl from the iterator - const bucket_impl &b = static_cast - (Slist::container_from_end_iterator(it)); - - //Now just calculate the index b has in the bucket array - return &b - &first_bucket; - } }; template @@ -133,6 +112,9 @@ class hashtable_iterator < typename Container::pointer, const Container>::type const_cont_ptr; typedef typename Container::size_type size_type; + static typename Container::node_ptr downcast_bucket(typename bucket_type::node_ptr p) + { return typename Container::node_ptr(&static_cast(*p)); } + public: typedef typename detail::add_const_if_c ::type value_type; @@ -172,7 +154,7 @@ class hashtable_iterator { return *this->operator ->(); } value_type* operator->() const - { return detail::get_pointer(this->get_real_value_traits()->to_value_ptr(slist_it_.pointed_node())); } + { return detail::get_pointer(this->get_real_value_traits()->to_value_ptr(downcast_bucket(slist_it_.pointed_node()))); } const Container *get_container() const { return detail::get_pointer(cont_); } @@ -186,17 +168,19 @@ class hashtable_iterator const Container *cont = detail::get_pointer(cont_); bucket_type* buckets = detail::get_pointer(cont->bucket_pointer()); size_type buckets_len = cont->bucket_count(); - const_siterator first(buckets[0].cend()); - const_siterator last (buckets[buckets_len].cend()); ++slist_it_; - if(first.pointed_node() <= slist_it_.pointed_node() && - slist_it_.pointed_node()<= last.pointed_node() ){ - size_type n_bucket = (size_type) - bucket_type::get_bucket_num(slist_it_, buckets[0], buckets[buckets_len]); + if(buckets[0].cend().pointed_node() <= slist_it_.pointed_node() && + slist_it_.pointed_node()<= buckets[buckets_len].cend().pointed_node() ){ + //Now get the bucket_impl from the iterator + const bucket_type &b = static_cast + (bucket_type::slist_type::container_from_end_iterator(slist_it_)); + + //Now just calculate the index b has in the bucket array + size_type n_bucket = static_cast(&b - &buckets[0]); do{ if (++n_bucket == buckets_len){ - slist_it_ = buckets->end(); + slist_it_ = (&buckets[0] + buckets_len)->end(); break; } slist_it_ = buckets[n_bucket].begin(); diff --git a/include/boost/intrusive/detail/list_node.hpp b/include/boost/intrusive/detail/list_node.hpp index ec48a6d..a4d4505 100644 --- a/include/boost/intrusive/detail/list_node.hpp +++ b/include/boost/intrusive/detail/list_node.hpp @@ -32,7 +32,8 @@ struct list_node { typedef typename boost::pointer_to_other ::type node_ptr; - node_ptr prev_, next_; + node_ptr next_; + node_ptr prev_; }; template @@ -85,7 +86,7 @@ class list_iterator typedef value_type * pointer; list_iterator() - : members_ (0, 0) + : members_ (node_ptr(0), 0) {} explicit list_iterator(node_ptr node, const Container *cont_ptr) diff --git a/include/boost/intrusive/detail/mpl.hpp b/include/boost/intrusive/detail/mpl.hpp index 5dcd75c..ed68350 100644 --- a/include/boost/intrusive/detail/mpl.hpp +++ b/include/boost/intrusive/detail/mpl.hpp @@ -13,6 +13,8 @@ #ifndef BOOST_INTRUSIVE_DETAIL_MPL_HPP #define BOOST_INTRUSIVE_DETAIL_MPL_HPP +#include + namespace boost { namespace intrusive { namespace detail { @@ -290,6 +292,24 @@ class is_empty_class static const bool value = sizeof(empty_helper_t1) == sizeof(empty_helper_t2); }; +template +struct ls_zeros +{ + static const std::size_t value = (S & std::size_t(1)) ? 0 : (1 + ls_zeros<(S>>1u)>::value); +}; + +template<> +struct ls_zeros<0> +{ + static const std::size_t value = 0; +}; + +template<> +struct ls_zeros<1> +{ + static const std::size_t value = 0; +}; + } //namespace detail } //namespace intrusive } //namespace boost diff --git a/include/boost/intrusive/detail/no_exceptions_support.hpp b/include/boost/intrusive/detail/no_exceptions_support.hpp deleted file mode 100644 index 4344684..0000000 --- a/include/boost/intrusive/detail/no_exceptions_support.hpp +++ /dev/null @@ -1,28 +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_NO_EXCEPTION_SUPPORT_HPP - -#if !(defined BOOST_INTRUSIVE_DISABLE_EXCEPTION_HANDLING) -# include -# define BOOST_INTRUSIVE_TRY BOOST_TRY -# define BOOST_INTRUSIVE_CATCH(x) BOOST_CATCH(x) -# define BOOST_INTRUSIVE_RETHROW BOOST_RETHROW -# define BOOST_INTRUSIVE_CATCH_END BOOST_CATCH_END -#else -# define BOOST_INTRUSIVE_TRY { if (true) -# define BOOST_INTRUSIVE_CATCH(x) else if (false) -# define BOOST_INTRUSIVE_RETHROW -# define BOOST_INTRUSIVE_CATCH_END } -#endif - -#endif //#ifndef BOOST_INTRUSIVE_NO_EXCEPTION_SUPPORT_HPP diff --git a/include/boost/intrusive/detail/rbtree_node.hpp b/include/boost/intrusive/detail/rbtree_node.hpp index 647c6e5..5345209 100644 --- a/include/boost/intrusive/detail/rbtree_node.hpp +++ b/include/boost/intrusive/detail/rbtree_node.hpp @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include namespace boost { @@ -110,7 +110,7 @@ struct compact_rbtree_node_traits_impl typedef typename boost::pointer_to_other ::type const_node_ptr; - typedef pointer_plus_bit ptr_bit; + typedef pointer_plus_bits ptr_bit; typedef typename node::color color; @@ -133,10 +133,10 @@ struct compact_rbtree_node_traits_impl { n->right_ = r; } static color get_color(const_node_ptr n) - { return (color)ptr_bit::get_bit(n->parent_); } + { return (color)ptr_bit::get_bits(n->parent_); } static void set_color(node_ptr n, color c) - { ptr_bit::set_bit(n->parent_, c != 0); } + { ptr_bit::set_bits(n->parent_, c != 0); } static color black() { return node::black_t; } @@ -146,7 +146,7 @@ struct compact_rbtree_node_traits_impl }; //Dispatches the implementation based on the boolean -template +template struct rbtree_node_traits_dispatch : public default_rbtree_node_traits_impl {}; @@ -161,11 +161,11 @@ template struct rbtree_node_traits : public rbtree_node_traits_dispatch < VoidPointer - , OptimizeSize && - has_pointer_plus_bit + , OptimizeSize && + (max_pointer_plus_bits < VoidPointer , detail::alignment_of >::value - >::value + >::value >= 1) > {}; diff --git a/include/boost/intrusive/detail/slist_node.hpp b/include/boost/intrusive/detail/slist_node.hpp index a5d8883..fb516ec 100644 --- a/include/boost/intrusive/detail/slist_node.hpp +++ b/include/boost/intrusive/detail/slist_node.hpp @@ -78,7 +78,7 @@ class slist_iterator typedef value_type * pointer; slist_iterator() - : members_ (0, 0) + : members_ (node_ptr(0), 0) {} explicit slist_iterator(node_ptr node, const Container *cont_ptr) diff --git a/include/boost/intrusive/detail/tree_algorithms.hpp b/include/boost/intrusive/detail/tree_algorithms.hpp index 7cf320c..3cf9786 100644 --- a/include/boost/intrusive/detail/tree_algorithms.hpp +++ b/include/boost/intrusive/detail/tree_algorithms.hpp @@ -17,7 +17,6 @@ #include #include #include -#include #include namespace boost { @@ -97,6 +96,7 @@ class tree_algorithms { /// @cond private: + typedef typename NodeTraits::node node; /// @endcond @@ -123,6 +123,26 @@ class tree_algorithms /// @cond private: + template + struct dispose_subtree_disposer + { + dispose_subtree_disposer(Disposer &disp, node_ptr subtree) + : disposer_(&disp), subtree_(subtree) + {} + + void release() + { disposer_ = 0; } + + ~dispose_subtree_disposer() + { + if(disposer_){ + dispose_subtree(subtree_, *disposer_); + } + } + Disposer *disposer_; + node_ptr subtree_; + }; + static node_ptr uncast(const_node_ptr ptr) { return node_ptr(const_cast(::boost::intrusive::detail::get_pointer(ptr))); @@ -505,9 +525,21 @@ class tree_algorithms //! Nodes: If node is inserted in a tree, this function corrupts the tree. static void init(node_ptr node) { - NodeTraits::set_parent(node, 0); - NodeTraits::set_left(node, 0); - NodeTraits::set_right(node, 0); + NodeTraits::set_parent(node, node_ptr(0)); + NodeTraits::set_left(node, node_ptr(0)); + NodeTraits::set_right(node, node_ptr(0)); + }; + + //! Effects: Returns true if node is in the same state as if called init(node) + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool inited(const_node_ptr node) + { + return !NodeTraits::get_parent(node) && + !NodeTraits::get_left(node) && + !NodeTraits::get_right(node) ; }; //! Requires: node must not be part of any tree. @@ -522,7 +554,7 @@ class tree_algorithms //! Nodes: If node is inserted in a tree, this function corrupts the tree. static void init_header(node_ptr header) { - NodeTraits::set_parent(header, 0); + NodeTraits::set_parent(header, node_ptr(0)); NodeTraits::set_left(header, header); NodeTraits::set_right(header, header); } @@ -565,7 +597,7 @@ class tree_algorithms { node_ptr leftmost = NodeTraits::get_left(header); if (leftmost == header) - return 0; + return node_ptr(0); node_ptr leftmost_parent(NodeTraits::get_parent(leftmost)); node_ptr leftmost_right (NodeTraits::get_right(leftmost)); bool is_root = leftmost_parent == header; @@ -580,12 +612,12 @@ class tree_algorithms NodeTraits::set_left(NodeTraits::get_parent(header), leftmost_right); } else if (is_root){ - NodeTraits::set_parent(header, 0); + NodeTraits::set_parent(header, node_ptr(0)); NodeTraits::set_left(header, header); NodeTraits::set_right(header, header); } else{ - NodeTraits::set_left(leftmost_parent, 0); + NodeTraits::set_left(leftmost_parent, node_ptr(0)); NodeTraits::set_left(header, leftmost_parent); } return leftmost; @@ -1143,58 +1175,54 @@ class tree_algorithms node_ptr rightmost = target_sub_root; //First set the subroot - NodeTraits::set_left(target_sub_root, 0); - NodeTraits::set_right(target_sub_root, 0); + NodeTraits::set_left(target_sub_root, node_ptr(0)); + NodeTraits::set_right(target_sub_root, node_ptr(0)); NodeTraits::set_parent(target_sub_root, target_parent); - try { - while(true) { - //First clone left nodes - if( NodeTraits::get_left(current) && - !NodeTraits::get_left(insertion_point)) { - current = NodeTraits::get_left(current); - node_ptr temp = insertion_point; - //Clone and mark as leaf - insertion_point = cloner(current); - NodeTraits::set_left (insertion_point, 0); - NodeTraits::set_right (insertion_point, 0); - //Insert left - NodeTraits::set_parent(insertion_point, temp); - NodeTraits::set_left (temp, insertion_point); - //Update leftmost - if(rightmost == target_sub_root) - leftmost = insertion_point; - } - //Then clone right nodes - else if( NodeTraits::get_right(current) && - !NodeTraits::get_right(insertion_point)){ - current = NodeTraits::get_right(current); - node_ptr temp = insertion_point; - //Clone and mark as leaf - insertion_point = cloner(current); - NodeTraits::set_left (insertion_point, 0); - NodeTraits::set_right (insertion_point, 0); - //Insert right - NodeTraits::set_parent(insertion_point, temp); - NodeTraits::set_right (temp, insertion_point); - //Update rightmost - rightmost = insertion_point; - } - //If not, go up - else if(current == source_root){ - break; - } - else{ - //Branch completed, go up searching more nodes to clone - current = NodeTraits::get_parent(current); - insertion_point = NodeTraits::get_parent(insertion_point); - } + dispose_subtree_disposer rollback(disposer, target_sub_root); + while(true) { + //First clone left nodes + if( NodeTraits::get_left(current) && + !NodeTraits::get_left(insertion_point)) { + current = NodeTraits::get_left(current); + node_ptr temp = insertion_point; + //Clone and mark as leaf + insertion_point = cloner(current); + NodeTraits::set_left (insertion_point, node_ptr(0)); + NodeTraits::set_right (insertion_point, node_ptr(0)); + //Insert left + NodeTraits::set_parent(insertion_point, temp); + NodeTraits::set_left (temp, insertion_point); + //Update leftmost + if(rightmost == target_sub_root) + leftmost = insertion_point; + } + //Then clone right nodes + else if( NodeTraits::get_right(current) && + !NodeTraits::get_right(insertion_point)){ + current = NodeTraits::get_right(current); + node_ptr temp = insertion_point; + //Clone and mark as leaf + insertion_point = cloner(current); + NodeTraits::set_left (insertion_point, node_ptr(0)); + NodeTraits::set_right (insertion_point, node_ptr(0)); + //Insert right + NodeTraits::set_parent(insertion_point, temp); + NodeTraits::set_right (temp, insertion_point); + //Update rightmost + rightmost = insertion_point; + } + //If not, go up + else if(current == source_root){ + break; + } + else{ + //Branch completed, go up searching more nodes to clone + current = NodeTraits::get_parent(current); + insertion_point = NodeTraits::get_parent(insertion_point); } } - catch(...) { - dispose_subtree(target_sub_root, disposer); - throw; - } + rollback.release(); leftmost_out = leftmost; rightmost_out = rightmost; } @@ -1321,8 +1349,8 @@ class tree_algorithms NodeTraits::set_right(header, z); } NodeTraits::set_parent(z, par); - NodeTraits::set_right(z, 0); - NodeTraits::set_left(z, 0); + NodeTraits::set_right(z, node_ptr(0)); + NodeTraits::set_left(z, node_ptr(0)); } static void erase(node_ptr header, node_ptr z) @@ -1384,7 +1412,7 @@ class tree_algorithms { std::size_t len; len = 0; - if(!old_root) return 0; + if(!old_root) return node_ptr(0); //To avoid irregularities in the algorithm (old_root can be a //left or right child or even the root of the tree) just put the @@ -1392,8 +1420,8 @@ class tree_algorithms //information to restore the original relationship after //the algorithm is applied. node_ptr super_root = NodeTraits::get_parent(old_root); - assert(super_root); - + BOOST_INTRUSIVE_INVARIANT_ASSERT(super_root); + //Get info node_ptr super_root_right_backup = NodeTraits::get_right(super_root); bool super_root_is_header = is_header(super_root); @@ -1464,7 +1492,7 @@ class tree_algorithms //information to restore the original relationship after //the algorithm is applied. node_ptr super_root = NodeTraits::get_parent(old_root); - assert(super_root); + BOOST_INTRUSIVE_INVARIANT_ASSERT(super_root); //Get info node_ptr super_root_right_backup = NodeTraits::get_right(super_root); @@ -1511,6 +1539,28 @@ class tree_algorithms return new_root; } + //! Requires: "n" must be a node inserted in a tree. + //! + //! Effects: Returns a pointer to the header node of the tree. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + static node_ptr get_root(node_ptr node) + { + BOOST_INTRUSIVE_INVARIANT_ASSERT((!inited(node))); + node_ptr x = NodeTraits::get_parent(node); + if(x){ + while(!is_header(x)){ + x = NodeTraits::get_parent(x); + } + return x; + } + else{ + return node; + } + } + private: static void erase_impl(node_ptr header, node_ptr z, data_for_rebalance &info) { @@ -1569,7 +1619,6 @@ class tree_algorithms info.x_parent = x_parent; info.y = y; } - }; } //namespace detail { diff --git a/include/boost/intrusive/detail/tree_node.hpp b/include/boost/intrusive/detail/tree_node.hpp index ea53b1c..a375cba 100644 --- a/include/boost/intrusive/detail/tree_node.hpp +++ b/include/boost/intrusive/detail/tree_node.hpp @@ -168,6 +168,11 @@ class tree_iterator return 0; } + tree_iterator end_iterator_from_it() const + { + return tree_iterator(node_algorithms::get_header(this->pointed_node()), this->get_container()); + } + private: struct members : public detail::select_constptr diff --git a/include/boost/intrusive/detail/utilities.hpp b/include/boost/intrusive/detail/utilities.hpp index e8b3bb3..f190c92 100644 --- a/include/boost/intrusive/detail/utilities.hpp +++ b/include/boost/intrusive/detail/utilities.hpp @@ -235,16 +235,9 @@ struct node_cloner node_cloner(F f, const Container *cont) : base_t(f), cont_(cont) {} - + node_ptr operator()(node_ptr p) - { - node_ptr n = cont_->get_real_value_traits().to_node_ptr - (*base_t::get()(*cont_->get_real_value_traits().to_value_ptr(p))); - //Cloned node must be in default mode if the linking mode requires it - if(safemode_or_autounlink) - BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n)); - return n; - } + { return this->operator()(*p); } node_ptr operator()(const node &to_clone) { @@ -396,16 +389,16 @@ template struct link_dispatch {}; -template -void destructor_impl(Container &cont, detail::link_dispatch) -{ (void)cont; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!cont.is_linked()); } +template +void destructor_impl(Hook &hook, detail::link_dispatch) +{ (void)hook; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!hook.is_linked()); } -template -void destructor_impl(Container &cont, detail::link_dispatch) -{ cont.unlink(); } +template +void destructor_impl(Hook &hook, detail::link_dispatch) +{ hook.unlink(); } -template -void destructor_impl(Container &, detail::link_dispatch) +template +void destructor_impl(Hook &, detail::link_dispatch) {} template @@ -548,6 +541,62 @@ inline std::size_t sqrt2_pow_2xplus1 (std::size_t x) return (value >> (pow - x)) + 1; } +template +class exception_disposer +{ + Container *cont_; + Disposer &disp_; + + exception_disposer(const exception_disposer&); + exception_disposer &operator=(const exception_disposer&); + + public: + exception_disposer(Container &cont, Disposer &disp) + : cont_(&cont), disp_(disp) + {} + + void release() + { cont_ = 0; } + + ~exception_disposer() + { + if(cont_){ + cont_->clear_and_dispose(disp_); + } + } +}; + +template +class exception_array_disposer +{ + Container *cont_; + Disposer &disp_; + typename Container::size_type &constructed_; + + exception_array_disposer(const exception_array_disposer&); + exception_array_disposer &operator=(const exception_array_disposer&); + + public: + typedef typename Container::size_type size_type; + exception_array_disposer + (Container &cont, Disposer &disp, size_type &constructed) + : cont_(&cont), disp_(disp), constructed_(constructed) + {} + + void release() + { cont_ = 0; } + + ~exception_array_disposer() + { + size_type n = constructed_; + if(cont_){ + while(n--){ + cont_[n].clear_and_dispose(disp_); + } + } + } +}; + } //namespace detail } //namespace intrusive } //namespace boost diff --git a/include/boost/intrusive/hashtable.hpp b/include/boost/intrusive/hashtable.hpp index 6947427..b621894 100644 --- a/include/boost/intrusive/hashtable.hpp +++ b/include/boost/intrusive/hashtable.hpp @@ -14,15 +14,15 @@ #include //std C++ -#include -#include -#include -#include +#include //std::equal_to +#include //std::pair +#include //std::swap, std::lower_bound, std::upper_bound +#include //std::size_t +#include //std::iterator_traits //boost #include #include #include -#include //General intrusive utilities #include #include @@ -34,12 +34,128 @@ #include #include #include +#include namespace boost { namespace intrusive { /// @cond +namespace detail { + +template +struct hash_reduced_slist_node_traits +{ + template static detail::one test(...); + template static detail::two test(typename U::reduced_slist_node_traits* = 0); + static const bool value = sizeof(test(0)) == sizeof(detail::two); +}; + +template +struct apply_reduced_slist_node_traits +{ + typedef typename NodeTraits::reduced_slist_node_traits type; +}; + +template +struct reduced_slist_node_traits +{ + typedef typename detail::eval_if_c + < hash_reduced_slist_node_traits::value + , apply_reduced_slist_node_traits + , detail::identity + >::type type; +}; + +template +struct get_slist_impl +{ + typedef trivial_value_traits trivial_traits; + + //Reducing symbol length + struct type : make_slist + < typename NodeTraits::node + , boost::intrusive::value_traits + , boost::intrusive::constant_time_size + , boost::intrusive::size_type + >::type + {}; +}; + +template +struct unordered_bucket_impl +{ + /// @cond + typedef typename detail::eval_if_c + < detail::external_value_traits_is_true + ::value + , detail::eval_value_traits + + , detail::identity + + >::type real_value_traits; + + typedef typename detail::get_node_traits + ::type node_traits; + typedef typename get_slist_impl + ::type + >::type slist_impl; + typedef detail::bucket_impl implementation_defined; + typedef implementation_defined type; +}; + +template +struct unordered_bucket_ptr_impl +{ + /// @cond + + typedef typename detail::eval_if_c + < detail::external_value_traits_is_true + ::value + , detail::eval_value_traits + + , detail::identity + + >::type real_value_traits; + typedef typename detail::get_node_traits + ::type::node_ptr node_ptr; + typedef typename unordered_bucket_impl + ::type bucket_type; + typedef typename boost::pointer_to_other + ::type implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +} //namespace detail { + +/// @endcond + +template +struct unordered_bucket +{ + /// @cond + typedef typename ValueTraitsOrHookOption:: + template pack::value_traits supposed_value_traits; + /// @endcond + typedef typename detail::unordered_bucket_impl + ::type type; +}; + +template +struct unordered_bucket_ptr +{ + /// @cond + typedef typename ValueTraitsOrHookOption:: + template pack::value_traits supposed_value_traits; + /// @endcond + typedef typename detail::unordered_bucket_ptr_impl + ::type type; +}; + +/// @cond + namespace detail{ template @@ -59,6 +175,23 @@ struct store_hash_is_true static const bool value = store_hash_bool::value > sizeof(one)*2; }; +template +struct optimize_multikey_bool +{ + template + struct two_or_three {one _[2 + Add];}; + template static one test(...); + template static two_or_three + test (detail::bool_* = 0); + static const std::size_t value = sizeof(test(0)); +}; + +template +struct optimize_multikey_is_true +{ + static const bool value = optimize_multikey_bool::value > sizeof(one)*2; +}; + template struct bucket_plus_size : public detail::size_holder @@ -92,7 +225,7 @@ struct bucket_hash_t : public detail::ebo_functor_holder bucket_plus_size bucket_plus_size_; }; -template +template struct bucket_hash_equal_t : public detail::ebo_functor_holder { typedef typename Config::equal equal; @@ -105,19 +238,41 @@ struct bucket_hash_equal_t : public detail::ebo_functor_holder bucket_hash; }; +template //cache_begin == true version +struct bucket_hash_equal_t + : public detail::ebo_functor_holder +{ + typedef typename Config::equal equal; + typedef typename Config::hash hasher; + typedef typename Config::bucket_traits bucket_traits; + typedef typename unordered_bucket_ptr_impl + ::type bucket_ptr; + + bucket_hash_equal_t(const bucket_traits &b_traits, const hasher & h, const equal &e) + : detail::ebo_functor_holder(e), bucket_hash(b_traits, h) + {} + bucket_hash_t bucket_hash; + bucket_ptr cached_begin_; +}; + template -struct data_t : public Config::value_traits +struct hashtable_data_t : public Config::value_traits { typedef typename Config::value_traits value_traits; typedef typename Config::equal equal; typedef typename Config::hash hasher; typedef typename Config::bucket_traits bucket_traits; - data_t( const bucket_traits &b_traits, const hasher & h - , const equal &e, const value_traits &val_traits) + hashtable_data_t( const bucket_traits &b_traits, const hasher & h + , const equal &e, const value_traits &val_traits) : Config::value_traits(val_traits), bucket_hash_equal_(b_traits, h, e) {} - bucket_hash_equal_t bucket_hash_equal_; + bucket_hash_equal_t bucket_hash_equal_; +}; + +struct insert_commit_data_impl +{ + std::size_t hash; }; } //namespace detail { @@ -137,12 +292,14 @@ struct get_default_uset_hook }; template < class ValueTraits + , bool UniqueKeys , class Hash , class Equal , class SizeType , bool ConstantTimeSize , class BucketTraits , bool Power2Buckets + , bool CacheBegin > struct usetopt { @@ -153,6 +310,8 @@ struct usetopt typedef BucketTraits bucket_traits; static const bool constant_time_size = ConstantTimeSize; static const bool power_2_buckets = Power2Buckets; + static const bool unique_keys = UniqueKeys; + static const bool cache_begin = CacheBegin; }; struct default_bucket_traits; @@ -174,75 +333,12 @@ struct uset_defaults , hash > , bucket_traits , power_2_buckets + , cache_begin >::type {}; -template -struct get_slist_impl -{ - typedef trivial_value_traits trivial_traits; - - //Reducing symbol length - struct type : make_slist - < typename NodeTraits::node - , boost::intrusive::value_traits - , boost::intrusive::constant_time_size - , boost::intrusive::size_type - >::type - {}; -}; - /// @endcond -template -struct unordered_bucket -{ - /// @cond - typedef typename ValueTraitsOrHookOption:: - template pack::value_traits supposed_value_traits; - - typedef typename detail::eval_if_c - < detail::external_value_traits_is_true - ::value - , detail::eval_value_traits - - , detail::identity - - >::type real_value_traits; - - typedef typename detail::get_node_traits - ::type node_traits; - typedef typename get_slist_impl - ::type slist_impl; - typedef detail::bucket_impl implementation_defined; - /// @endcond - typedef implementation_defined type; -}; - -template -struct unordered_bucket_ptr -{ - /// @cond - typedef typename ValueTraitsOrHookOption:: - template pack::value_traits supposed_value_traits; - typedef typename detail::eval_if_c - < detail::external_value_traits_is_true - ::value - , detail::eval_value_traits - - , detail::identity - - >::type real_value_traits; - typedef typename detail::get_node_traits - ::type::node_ptr node_ptr; - typedef typename unordered_bucket - ::type bucket_type; - typedef typename boost::pointer_to_other - ::type implementation_defined; - /// @endcond - typedef implementation_defined type; -}; - //! The class template hashtable is an intrusive hash table container, that //! is used to construct intrusive unordered_set and unordered_multiset containers. The //! no-throw guarantee holds only, if the Equal object and Hasher don't throw. @@ -261,7 +357,8 @@ struct unordered_bucket_ptr //! //! The container supports the following options: //! \c base_hook<>/member_hook<>/value_traits<>, -//! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<> . +//! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<> +//! \c bucket_traits<>, power_2_buckets<> and cache_begin<>. //! //! hashtable only provides forward iterators but it provides 4 iterator types: //! iterator and const_iterator to navigate through the whole container and @@ -284,13 +381,12 @@ template template #endif class hashtable_impl - : private detail::data_t + : private detail::hashtable_data_t { public: typedef typename Config::value_traits value_traits; /// @cond - static const bool external_value_traits = detail::external_value_traits_is_true::value; typedef typename detail::eval_if_c @@ -306,8 +402,10 @@ class hashtable_impl , detail::eval_bucket_traits , detail::identity >::type real_bucket_traits; - typedef typename get_slist_impl - ::type slist_impl; + typedef typename detail::get_slist_impl + ::type + >::type slist_impl; /// @endcond typedef typename real_value_traits::pointer pointer; @@ -327,7 +425,7 @@ class hashtable_impl typedef typename slist_impl::const_iterator const_siterator; typedef detail::hashtable_iterator iterator; typedef detail::hashtable_iterator const_iterator; - typedef typename real_value_traits::node_traits node_traits; + typedef typename real_value_traits::node_traits node_traits; typedef typename node_traits::node node; typedef typename boost::pointer_to_other ::type node_ptr; @@ -338,19 +436,67 @@ class hashtable_impl static const bool constant_time_size = Config::constant_time_size; static const bool stateful_value_traits = detail::store_cont_ptr_on_it::value; static const bool store_hash = detail::store_hash_is_true::value; + static const bool unique_keys = Config::unique_keys; + static const bool optimize_multikey + = detail::optimize_multikey_is_true::value && !unique_keys; + static const bool power_2_buckets = Config::power_2_buckets; + static const bool cache_begin = Config::cache_begin; /// @cond private: - typedef detail::bool_ store_hash_t; - typedef detail::size_holder size_traits; - typedef detail::data_t base_type; - typedef detail::transform_iterator - < typename slist_impl::iterator - , detail::node_to_value > local_iterator_impl; - typedef detail::transform_iterator - < typename slist_impl::iterator - , detail::node_to_value > const_local_iterator_impl; + typedef typename slist_impl::node_ptr slist_node_ptr; + typedef typename boost::pointer_to_other + ::type void_pointer; + //We'll define group traits, but these won't be instantiated if + //optimize_multikey is not true + typedef unordered_group_node_traits group_traits; + typedef circular_slist_algorithms group_algorithms; + typedef detail::bool_ store_hash_t; + typedef detail::bool_ optimize_multikey_t; + typedef detail::bool_ cache_begin_t; + typedef detail::bool_ power_2_buckets_t; + typedef detail::size_holder size_traits; + typedef detail::hashtable_data_t base_type; + + template + struct downcast_node_to_value + : public detail::node_to_value + { + typedef detail::node_to_value base_t; + typedef typename base_t::result_type result_type; + typedef typename detail::add_const_if_c + ::type &first_argument_type; + typedef typename detail::add_const_if_c + ::type &intermediate_argument_type; + + downcast_node_to_value(const hashtable_impl *cont) + : base_t(cont) + {} + + result_type operator()(first_argument_type arg) const + { return this->base_t::operator()(static_cast(arg)); } + }; + + template + struct node_cast_adaptor + : private detail::ebo_functor_holder + { + typedef detail::ebo_functor_holder base_t; + + template + node_cast_adaptor(const ConvertibleToF &c2f, const hashtable_impl *cont) + : base_t(base_t(c2f, cont)) + {} + + typename base_t::node_ptr operator()(const typename slist_impl::node &to_clone) + { return base_t::operator()(static_cast(to_clone)); } + + void operator()(typename slist_impl::node_ptr to_clone) + { base_t::operator()(node_ptr(&static_cast(*to_clone))); } + }; + + private: //noncopyable hashtable_impl (const hashtable_impl&); hashtable_impl operator =(const hashtable_impl&); @@ -362,103 +508,23 @@ class hashtable_impl //Constant-time size is incompatible with auto-unlink hooks! BOOST_STATIC_ASSERT(!(constant_time_size && ((int)real_value_traits::link_mode == (int)auto_unlink))); - static const bool power_2_buckets = Config::power_2_buckets; + template + node_cast_adaptor > + make_node_disposer(const Disposer &disposer) const + { return node_cast_adaptor >(disposer, this); } - std::size_t from_hash_to_bucket(std::size_t hash_value) const - { return from_hash_to_bucket(hash_value, detail::bool_()); } - - std::size_t from_hash_to_bucket(std::size_t hash_value, detail::bool_) const - { return hash_value % this->get_real_bucket_traits().bucket_count(); } - - std::size_t from_hash_to_bucket(std::size_t hash_value, detail::bool_) const - { return hash_value & (this->get_real_bucket_traits().bucket_count() - 1); } - - const key_equal &priv_equal() const - { return static_cast(this->bucket_hash_equal_.get()); } - - key_equal &priv_equal() - { return static_cast(this->bucket_hash_equal_.get()); } - - const real_bucket_traits &get_real_bucket_traits(detail::bool_) const - { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_.bucket_traits_; } - - const real_bucket_traits &get_real_bucket_traits(detail::bool_) const - { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_.bucket_traits_.get_bucket_traits(*this); } - - real_bucket_traits &get_real_bucket_traits(detail::bool_) - { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_.bucket_traits_; } - - real_bucket_traits &get_real_bucket_traits(detail::bool_) - { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_.bucket_traits_.get_bucket_traits(*this); } - - const real_bucket_traits &get_real_bucket_traits() const - { return this->get_real_bucket_traits(detail::bool_()); } - - real_bucket_traits &get_real_bucket_traits() - { return this->get_real_bucket_traits(detail::bool_()); } - - const hasher &priv_hasher() const - { return static_cast(this->bucket_hash_equal_.bucket_hash.get()); } - - hasher &priv_hasher() - { return static_cast(this->bucket_hash_equal_.bucket_hash.get()); } - - bucket_ptr priv_buckets() const - { return this->get_real_bucket_traits().bucket_begin(); } - - size_type priv_buckets_len() const - { return this->get_real_bucket_traits().bucket_count(); } - - static node_ptr uncast(const_node_ptr ptr) - { - return node_ptr(const_cast(detail::get_pointer(ptr))); - } - - node &from_value_to_node(value_type &v) - { return *this->get_real_value_traits().to_node_ptr(v); } - - const node &from_value_to_node(const value_type &v) const - { return *this->get_real_value_traits().to_node_ptr(v); } - - size_traits &priv_size_traits() - { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_; } - - const size_traits &priv_size_traits() const - { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_; } - - struct insert_commit_data_impl - { - size_type hash; - }; /// @endcond public: + typedef detail::insert_commit_data_impl insert_commit_data; - class local_iterator - : public local_iterator_impl - { - public: - local_iterator() - {} + typedef detail::transform_iterator + < typename slist_impl::iterator + , downcast_node_to_value > local_iterator; - local_iterator(siterator sit, const hashtable_impl *cont) - : local_iterator_impl(sit, cont) - {} - }; - - class const_local_iterator - : public const_local_iterator_impl - { - public: - const_local_iterator() - {} - - const_local_iterator(siterator sit, const hashtable_impl *cont) - : const_local_iterator_impl(sit, cont) - {} - }; - - typedef insert_commit_data_impl insert_commit_data; + typedef detail::transform_iterator + < typename slist_impl::iterator + , downcast_node_to_value > const_local_iterator; /// @cond @@ -503,7 +569,7 @@ class hashtable_impl , const value_traits &v_traits = value_traits()) : base_type(b_traits, hash_func, equal_func, v_traits) { - priv_clear_buckets(); + priv_initialize_buckets(); this->priv_size_traits().set_size(size_type(0)); BOOST_INTRUSIVE_INVARIANT_ASSERT(this->priv_buckets_len() != 0); //Check power of two bucket array if the option is activated @@ -562,7 +628,7 @@ class hashtable_impl //! //! Throws: Nothing. iterator end() - { return iterator(invalid_local_it(this->get_real_bucket_traits()), 0); } + { return iterator(priv_invalid_local_it(), 0); } //! Effects: Returns a const_iterator pointing to the end of the unordered_set. //! @@ -578,7 +644,7 @@ class hashtable_impl //! //! Throws: Nothing. const_iterator cend() const - { return const_iterator(invalid_local_it(this->get_real_bucket_traits()), 0); } + { return const_iterator(priv_invalid_local_it(), 0); } //! Effects: Returns the hasher object used by the unordered_set. //! @@ -598,8 +664,8 @@ class hashtable_impl //! Effects: Returns true is the container is empty. //! - //! Complexity: if constant_time_size is false, average constant time - //! (worst case, with empty() == true): O(this->bucket_count()). + //! Complexity: if constant-time size and cache_last options are disabled, + //! average constant time (worst case, with empty() == true: O(this->bucket_count()). //! Otherwise constant. //! //! Throws: Nothing. @@ -608,6 +674,9 @@ class hashtable_impl if(constant_time_size){ return !this->size(); } + else if(cache_begin){ + return this->begin() == this->end(); + } else{ size_type buckets_len = this->priv_buckets_len(); const bucket_type *b = detail::get_pointer(this->priv_buckets()); @@ -658,7 +727,8 @@ class hashtable_impl swap(this->priv_equal(), other.priv_equal()); swap(this->priv_hasher(), other.priv_hasher()); //These can't throw - swap(this->get_real_bucket_traits(), other.get_real_bucket_traits()); + swap(this->priv_real_bucket_traits(), other.priv_real_bucket_traits()); + priv_swap_cache(cache_begin_t(), other); if(constant_time_size){ size_type backup = this->priv_size_traits().get_size(); this->priv_size_traits().set_size(other.priv_size_traits().get_size()); @@ -698,75 +768,71 @@ class hashtable_impl const bucket_ptr src_buckets = src.priv_buckets(); const bucket_ptr dst_buckets = this->priv_buckets(); size_type constructed; - BOOST_INTRUSIVE_TRY{ - for( constructed = 0 - ; constructed < dst_bucket_count + typedef node_cast_adaptor > NodeDisposer; + typedef node_cast_adaptor > NodeCloner; + NodeDisposer node_disp(disposer, this); + + detail::exception_array_disposer + rollback(dst_buckets[0], node_disp, constructed); + for( constructed = 0 + ; constructed < dst_bucket_count + ; ++constructed){ + dst_buckets[constructed].clone_from + ( src_buckets[constructed] + , NodeCloner(cloner, this), node_disp); + } + if(src_bucket_count != dst_bucket_count){ + //Now insert the remaining ones using the modulo trick + for(//"constructed" comes from the previous loop + ; constructed < src_bucket_count ; ++constructed){ - dst_buckets[constructed].clone_from - ( src_buckets[constructed] - , detail::node_cloner(cloner, this) - , detail::node_disposer(disposer, this) - ); - } - if(src_bucket_count != dst_bucket_count){ - //Now insert the remaining ones using the modulo trick - for(//"constructed" comes from the previous loop - ; constructed < src_bucket_count - ; ++constructed){ - bucket_type &dst_b = (power_2_buckets) - ? dst_buckets[constructed & (dst_bucket_count-1)] - : dst_buckets[constructed % dst_bucket_count]; - bucket_type &src_b = src_buckets[constructed]; - for( siterator b(src_b.begin()), e(src_b.end()) - ; b != e - ; ++b){ - dst_b.push_front(*detail::node_cloner - (cloner, this)(b.pointed_node())); - } + bucket_type &dst_b = + dst_buckets[priv_hash_to_bucket(constructed, dst_bucket_count)]; + bucket_type &src_b = src_buckets[constructed]; + for( siterator b(src_b.begin()), e(src_b.end()) + ; b != e + ; ++b){ + dst_b.push_front(*(NodeCloner(cloner, this)(*b.pointed_node()))); } } } - BOOST_INTRUSIVE_CATCH(...){ - while(constructed--){ - dst_buckets[constructed].clear_and_dispose - (detail::node_disposer(disposer, this)); - } - BOOST_INTRUSIVE_RETHROW; - } - BOOST_INTRUSIVE_CATCH_END + rollback.release(); this->priv_size_traits().set_size(src.priv_size_traits().get_size()); + priv_insertion_update_cache(0u); + priv_erasure_update_cache(); } else{ //Unlike previous cloning algorithm, this can throw //if cloner, the hasher or comparison functor throw const_iterator b(src.begin()), e(src.end()); - BOOST_INTRUSIVE_TRY{ - for(; b != e; ++b){ - this->insert_equal(*cloner(*b)); - } + detail::exception_disposer + rollback(*this, disposer); + for(; b != e; ++b){ + this->insert_equal(*cloner(*b)); } - BOOST_INTRUSIVE_CATCH(...){ - this->clear_and_dispose(disposer); - BOOST_INTRUSIVE_RETHROW; - } - BOOST_INTRUSIVE_CATCH_END + rollback.release(); } } } iterator insert_equal(reference value) { - size_type bucket_num, hash_value; + size_type bucket_num; + std::size_t hash_value; + siterator prev; siterator it = this->priv_find - (value, this->priv_hasher(), this->priv_equal(), bucket_num, hash_value); + (value, this->priv_hasher(), this->priv_equal(), bucket_num, hash_value, prev); bucket_type &b = this->priv_buckets()[bucket_num]; - if(it == invalid_local_it(this->get_real_bucket_traits())){ - it = b.before_begin(); - } - node_ptr n = node_ptr(&from_value_to_node(value)); + bool found_equal = it != priv_invalid_local_it(); + node_ptr n = node_ptr(&priv_value_to_node(value)); this->priv_store_hash(n, hash_value, store_hash_t()); if(safemode_or_autounlink) BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n)); + if(!found_equal){ + it = b.before_begin(); + } + this->priv_insert_in_group(found_equal ? dcast_bucket_ptr(it.pointed_node()) : node_ptr(0), n, optimize_multikey_t()); + priv_insertion_update_cache(bucket_num); this->priv_size_traits().increment(); return iterator(b.insert_after(it, *n), this); } @@ -867,9 +933,10 @@ class hashtable_impl , insert_commit_data &commit_data) { size_type bucket_num; + siterator prev; siterator prev_pos = - this->priv_find(key, hash_func, equal_func, bucket_num, commit_data.hash); - bool success = prev_pos == invalid_local_it(this->get_real_bucket_traits()); + this->priv_find(key, hash_func, equal_func, bucket_num, commit_data.hash, prev); + bool success = prev_pos == priv_invalid_local_it(); if(success){ prev_pos = this->priv_buckets()[bucket_num].before_begin(); } @@ -897,14 +964,16 @@ class hashtable_impl //! After a successful rehashing insert_commit_data remains valid. iterator insert_unique_commit(reference value, const insert_commit_data &commit_data) { - size_type bucket_num = from_hash_to_bucket(commit_data.hash); + size_type bucket_num = priv_hash_to_bucket(commit_data.hash); bucket_type &b = this->priv_buckets()[bucket_num]; this->priv_size_traits().increment(); - node_ptr n = node_ptr(&from_value_to_node(value)); + node_ptr n = node_ptr(&priv_value_to_node(value)); this->priv_store_hash(n, commit_data.hash, store_hash_t()); if(safemode_or_autounlink) BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n)); - return iterator( b.insert_after(b.before_begin(), *n), this); + priv_insertion_update_cache(bucket_num); + this->priv_insert_in_group(node_ptr(0), n, optimize_multikey_t()); + return iterator(b.insert_after(b.before_begin(), *n), this); } //! Effects: Erases the element pointed to by i. @@ -983,12 +1052,9 @@ class hashtable_impl template void erase_and_dispose(const_iterator i, Disposer disposer) { - siterator to_erase(i.slist_it()); - bucket_ptr f(priv_buckets()), l(f + priv_buckets_len()); - bucket_type &b = this->priv_buckets()[bucket_type::get_bucket_num(to_erase, *f, *l)]; - b.erase_after_and_dispose - (b.previous(to_erase), detail::node_disposer(disposer, this)); + priv_erase(i, disposer, optimize_multikey_t()); this->priv_size_traits().decrement(); + priv_erasure_update_cache(); } //! Requires: Disposer::operator()(pointer) shouldn't throw. @@ -1009,66 +1075,26 @@ class hashtable_impl if(b == e) return; //Get the bucket number and local iterator for both iterators - bucket_ptr f(priv_buckets()), l(f + priv_buckets_len()); - size_type first_bucket_num = bucket_type::get_bucket_num(b.slist_it(), *f, *l); + siterator first_local_it(b.slist_it()); + size_type first_bucket_num = this->priv_get_bucket_num(first_local_it); siterator before_first_local_it - = priv_buckets()[first_bucket_num].previous(b.slist_it()); + = priv_get_previous(priv_buckets()[first_bucket_num], first_local_it); size_type last_bucket_num; siterator last_local_it; //For the end iterator, we will assign the end iterator //of the last bucket - if(e == end()){ + if(e == this->end()){ last_bucket_num = this->bucket_count() - 1; last_local_it = priv_buckets()[last_bucket_num].end(); } else{ last_local_it = e.slist_it(); - last_bucket_num = bucket_type::get_bucket_num(last_local_it, *f, *l); - } - - const bucket_ptr buckets = priv_buckets(); - //First erase the nodes of the first bucket - { - bucket_type &first_b = buckets[first_bucket_num]; - siterator nxt(before_first_local_it); ++nxt; - siterator end = first_b.end(); - while(nxt != end){ - nxt = first_b.erase_after_and_dispose - ( before_first_local_it - , detail::node_disposer(disposer, this)); - this->priv_size_traits().decrement(); - } - } - - //Now fully clear the intermediate buckets - for(size_type i = first_bucket_num+1; i < last_bucket_num; ++i){ - bucket_type &b = buckets[i]; - if(b.empty()) - continue; - siterator b_begin(b.before_begin()); - siterator nxt(b_begin); ++nxt; - siterator end = b.end(); - while(nxt != end){ - nxt = b.erase_after_and_dispose - (b_begin, detail::node_disposer(disposer, this)); - this->priv_size_traits().decrement(); - } - } - - //Now erase nodes from the last bucket - { - bucket_type &last_b = buckets[last_bucket_num]; - siterator b_begin(last_b.before_begin()); - siterator nxt(b_begin); ++nxt; - while(nxt != last_local_it){ - nxt = last_b.erase_after_and_dispose - (b_begin, detail::node_disposer - (disposer, this)); - this->priv_size_traits().decrement(); - } + last_bucket_num = this->priv_get_bucket_num(last_local_it); } + priv_erase_range(before_first_local_it, first_bucket_num, last_local_it, last_bucket_num, disposer); + priv_erasure_update_cache(first_bucket_num, last_bucket_num); } //! Requires: Disposer::operator()(pointer) shouldn't throw. @@ -1109,40 +1135,35 @@ class hashtable_impl size_type erase_and_dispose(const KeyType& key, KeyHasher hash_func ,KeyValueEqual equal_func, Disposer disposer) { + size_type bucket_num; + std::size_t hash; + siterator prev; + siterator it = + this->priv_find(key, hash_func, equal_func, bucket_num, hash, prev); + bool success = it != priv_invalid_local_it(); size_type count(0); - - if(constant_time_size && this->empty()){ + if(!success){ return 0; } - - bucket_type &b = this->priv_buckets()[from_hash_to_bucket(hash_func(key))]; - siterator it = b.begin(); - siterator prev = b.before_begin(); - - bool found = false; - //Find equal value - while(it != b.end()){ - const value_type &v = - *this->get_real_value_traits().to_value_ptr(it.pointed_node()); - if(equal_func(key, v)){ - found = true; - break; + else if(optimize_multikey){ + siterator last = bucket_type::s_iterator_to + (*node_traits::get_next(priv_get_last_in_group + (dcast_bucket_ptr(it.pointed_node())))); + this->priv_erase_range_impl(bucket_num, prev, last, disposer, count); + } + else{ + //If found erase all equal values + bucket_type &b = this->priv_buckets()[bucket_num]; + for(siterator end = b.end(); it != end; ++count, ++it){ + slist_node_ptr n(it.pointed_node()); + if(!equal_func(key, priv_value_from_slist_node(n))){ + break; + } + this->priv_size_traits().decrement(); } - ++prev; - ++it; - } - - if(!found) - return 0; - - //If found erase all equal values - for(siterator end = b.end(); it != end && - equal_func(key, *this->get_real_value_traits().to_value_ptr(it.pointed_node())) - ; ++count){ - it = b.erase_after_and_dispose - (prev, detail::node_disposer(disposer, this)); - this->priv_size_traits().decrement(); + b.erase_after_and_dispose(prev, it, make_node_disposer(disposer)); } + priv_erasure_update_cache(); return count; } @@ -1179,11 +1200,11 @@ class hashtable_impl size_type num_buckets = this->bucket_count(); bucket_ptr b = this->priv_buckets(); for(; num_buckets--; ++b){ - b->clear_and_dispose - (detail::node_disposer(disposer, this)); + b->clear_and_dispose(make_node_disposer(disposer)); } this->priv_size_traits().set_size(size_type(0)); } + priv_initialize_cache(); } //! Effects: Returns the number of contained elements with the given value @@ -1246,8 +1267,10 @@ class hashtable_impl template iterator find(const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) { - size_type bucket_n, hash; - siterator local_it = this->priv_find(key, hash_func, equal_func, bucket_n, hash); + size_type bucket_n; + std::size_t hash; + siterator prev; + siterator local_it = this->priv_find(key, hash_func, equal_func, bucket_n, hash, prev); return iterator(local_it, this); } @@ -1283,8 +1306,10 @@ class hashtable_impl const_iterator find (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) const { - size_type bucket_n, hash_value; - siterator sit = this->priv_find(key, hash_func, equal_func, bucket_n, hash_value); + size_type bucket_n; + std::size_t hash_value; + siterator prev; + siterator sit = this->priv_find(key, hash_func, equal_func, bucket_n, hash_value, prev); return const_iterator(sit, this); } @@ -1382,7 +1407,7 @@ class hashtable_impl //! Throws: If the internal hash function throws. iterator iterator_to(reference value) { - return iterator(bucket_type::s_iterator_to(from_value_to_node(value)), this); + return iterator(bucket_type::s_iterator_to(priv_value_to_node(value)), this); } //! Requires: value must be an lvalue and shall be in a unordered_set of @@ -1396,12 +1421,9 @@ class hashtable_impl //! Throws: If the internal hash function throws. const_iterator iterator_to(const_reference value) const { - return const_iterator(bucket_type::s_iterator_to(from_value_to_node(const_cast(value))), this); + return const_iterator(bucket_type::s_iterator_to(priv_value_to_node(const_cast(value))), this); } - - - //! Requires: value must be an lvalue and shall be in a unordered_set of //! appropriate type. Otherwise the behavior is undefined. //! @@ -1417,7 +1439,7 @@ class hashtable_impl static local_iterator s_local_iterator_to(reference value) { BOOST_STATIC_ASSERT((!stateful_value_traits)); - siterator sit = bucket_type::s_iterator_to(((hashtable_impl*)0)->from_value_to_node(value)); + siterator sit = bucket_type::s_iterator_to(((hashtable_impl*)0)->priv_value_to_node(value)); return local_iterator(sit, (hashtable_impl*)0); } @@ -1436,7 +1458,7 @@ class hashtable_impl static const_local_iterator s_local_iterator_to(const_reference value) { BOOST_STATIC_ASSERT((!stateful_value_traits)); - siterator sit = bucket_type::s_iterator_to(((hashtable_impl*)0)->from_value_to_node(const_cast(value))); + siterator sit = bucket_type::s_iterator_to(((hashtable_impl*)0)->priv_value_to_node(const_cast(value))); return const_local_iterator(sit, (hashtable_impl*)0); } @@ -1451,7 +1473,7 @@ class hashtable_impl //! Throws: Nothing. local_iterator local_iterator_to(reference value) { - siterator sit = bucket_type::s_iterator_to(this->from_value_to_node(value)); + siterator sit = bucket_type::s_iterator_to(this->priv_value_to_node(value)); return local_iterator(sit, this); } @@ -1467,7 +1489,7 @@ class hashtable_impl const_local_iterator local_iterator_to(const_reference value) const { siterator sit = bucket_type::s_iterator_to - (const_cast(this->from_value_to_node(value))); + (const_cast(this->priv_value_to_node(value))); return const_local_iterator(sit, this); } @@ -1515,7 +1537,7 @@ class hashtable_impl //! Note: the return value is in the range [0, this->bucket_count()). template size_type bucket(const KeyType& k, const KeyHasher &hash_func) const - { return from_hash_to_bucket(hash_func(k)); } + { return priv_hash_to_bucket(hash_func(k)); } //! Effects: Returns the bucket array pointer passed in the constructor //! or the last rehash function. @@ -1630,75 +1652,81 @@ class hashtable_impl size_type new_buckets_len = new_bucket_traits.bucket_count(); bucket_ptr old_buckets = this->priv_buckets(); size_type old_buckets_len = this->priv_buckets_len(); + //Check power of two bucket array if the option is activated BOOST_INTRUSIVE_INVARIANT_ASSERT (!power_2_buckets || (0 == (new_buckets_len & (new_buckets_len-1u)))); - BOOST_INTRUSIVE_TRY{ - size_type n = 0; - const bool same_buffer = old_buckets == new_buckets; - //If the new bucket length is a common factor - //of the old one we can avoid hash calculations. - const bool fast_shrink = (old_buckets_len > new_buckets_len) && - (power_2_buckets ||(old_buckets_len % new_buckets_len) == 0); - //If we are shrinking the same bucket array and it's - //is a fast shrink, just rehash the last nodes - if(same_buffer && fast_shrink){ - n = new_buckets_len; - } + size_type n = this->priv_get_cache() - this->priv_buckets(); + const bool same_buffer = old_buckets == new_buckets; + //If the new bucket length is a common factor + //of the old one we can avoid hash calculations. + const bool fast_shrink = (old_buckets_len > new_buckets_len) && + (power_2_buckets ||(old_buckets_len % new_buckets_len) == 0); + //If we are shrinking the same bucket array and it's + //is a fast shrink, just rehash the last nodes + if(same_buffer && fast_shrink){ + n = new_buckets_len; + } - //Iterate through nodes - for(; n < old_buckets_len; ++n){ - bucket_type &old_bucket = old_buckets[n]; + //Anti-exception stuff: they destroy the elements if something goes wrong + typedef detail::init_disposer NodeDisposer; + NodeDisposer node_disp; + detail::exception_array_disposer + rollback1(new_buckets[0], node_disp, new_buckets_len); + detail::exception_array_disposer + rollback2(old_buckets[0], node_disp, old_buckets_len); - if(!fast_shrink){ - siterator before_i(old_bucket.before_begin()); - siterator end(old_bucket.end()); - siterator i(old_bucket.begin()); - for(;i != end; ++i){ - const value_type &v = *this->get_real_value_traits().to_value_ptr(i.pointed_node()); - const std::size_t hash_value = this->priv_hash_when_rehashing(v, store_hash_t()); - const size_type new_n = (power_2_buckets) - ? (hash_value & (new_buckets_len-1)) : (hash_value % new_buckets_len); - //If this is a buffer expansion don't move if it's not necessary - if(same_buffer && new_n == n){ - ++before_i; - } - else{ - bucket_type &new_b = new_buckets[new_n]; - new_b.splice_after(new_b.before_begin(), old_bucket, before_i); - i = before_i; - } + //Put size in a safe value for rollback exception + size_type size_backup = this->priv_size_traits().get_size(); + this->priv_size_traits().set_size(0); + //Put cache to safe position + priv_initialize_cache(); + priv_insertion_update_cache(size_type(0u)); + + //Iterate through nodes + for(; n < old_buckets_len; ++n){ + bucket_type &old_bucket = old_buckets[n]; + + if(!fast_shrink){ + siterator before_i(old_bucket.before_begin()); + siterator end(old_bucket.end()); + siterator i(old_bucket.begin()); + for(;i != end; ++i){ + const value_type &v = priv_value_from_slist_node(i.pointed_node()); + const std::size_t hash_value = this->priv_stored_hash(v, store_hash_t()); + const size_type new_n = priv_hash_to_bucket(hash_value, new_buckets_len); + siterator last = bucket_type::s_iterator_to + (*priv_get_last_in_group(dcast_bucket_ptr(i.pointed_node()))); + if(same_buffer && new_n == n){ + before_i = last; } - } - else{ - const size_type new_n = (power_2_buckets) - ? (n & (new_buckets_len-1)) - : (n % new_buckets_len); - bucket_type &new_b = new_buckets[new_n]; - new_b.splice_after(new_b.before_begin(), old_bucket); + else{ + bucket_type &new_b = new_buckets[new_n]; + new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last); + } + i = before_i; } } + else{ + const size_type new_n = priv_hash_to_bucket(n, new_buckets_len); + bucket_type &new_b = new_buckets[new_n]; + if(!old_bucket.empty()){ + new_b.splice_after( new_b.before_begin() + , old_bucket + , old_bucket.before_begin() + , priv_get_last(old_bucket)); + } + } + } - this->get_real_bucket_traits()= new_bucket_traits; - } - BOOST_INTRUSIVE_CATCH(...){ - for(size_type n = 0; n < new_buckets_len; ++n){ - if(safemode_or_autounlink){ - new_buckets[n].clear_and_dispose - (detail::init_disposer()); - old_buckets[n].clear_and_dispose - (detail::init_disposer()); - } - else{ - new_buckets[n].clear(); - old_buckets[n].clear(); - } - } - this->priv_size_traits().set_size(size_type(0)); - BOOST_INTRUSIVE_RETHROW; - } - BOOST_INTRUSIVE_CATCH_END + this->priv_size_traits().set_size(size_backup); + this->priv_real_bucket_traits() = new_bucket_traits; + priv_initialize_cache(); + priv_insertion_update_cache(size_type(0u)); + priv_erasure_update_cache(); + rollback1.release(); + rollback2.release(); } //! Effects: Returns the nearest new bucket count optimized for @@ -1714,8 +1742,7 @@ class hashtable_impl { const std::size_t *primes = &detail::prime_list_holder<0>::prime_list[0]; const std::size_t *primes_end = primes + detail::prime_list_holder<0>::prime_list_size; - size_type const* bound = - std::lower_bound(primes, primes_end, n); + size_type const* bound = std::lower_bound(primes, primes_end, n); if(bound == primes_end) bound--; return size_type(*bound); @@ -1734,8 +1761,7 @@ class hashtable_impl { const std::size_t *primes = &detail::prime_list_holder<0>::prime_list[0]; const std::size_t *primes_end = primes + detail::prime_list_holder<0>::prime_list_size; - size_type const* bound = - std::upper_bound(primes, primes_end, n); + size_type const* bound = std::upper_bound(primes, primes_end, n); if(bound != primes_end) bound--; return size_type(*bound); @@ -1743,72 +1769,666 @@ class hashtable_impl /// @cond private: + std::size_t priv_hash_to_bucket(std::size_t hash_value) const + { return priv_hash_to_bucket(hash_value, power_2_buckets_t()); } - std::size_t priv_hash_when_rehashing(const value_type &v, detail::true_) - { return node_traits::get_hash(this->get_real_value_traits().to_node_ptr(v)); } + std::size_t priv_hash_to_bucket(std::size_t hash_value, detail::bool_) const + { return hash_value % this->priv_real_bucket_traits().bucket_count(); } - std::size_t priv_hash_when_rehashing(const value_type &v, detail::false_) - { return priv_hasher()(v); } + std::size_t priv_hash_to_bucket(std::size_t hash_value, detail::bool_) const + { return hash_value & (this->priv_real_bucket_traits().bucket_count() - 1); } - void priv_store_hash(node_ptr p, std::size_t h, detail::true_) - { return node_traits::set_hash(p, h); } + std::size_t priv_hash_to_bucket(std::size_t hash_value, std::size_t bucket_len) const + { return priv_hash_to_bucket(hash_value, bucket_len, power_2_buckets_t()); } - void priv_store_hash(node_ptr, std::size_t, detail::false_) - {} - - static siterator invalid_local_it(const real_bucket_traits &b) - { return b.bucket_begin()->end(); } + std::size_t priv_hash_to_bucket(std::size_t hash_value, std::size_t bucket_len, detail::bool_) const + { return hash_value % bucket_len; } - siterator priv_begin(size_type &bucket_num) const + std::size_t priv_hash_to_bucket(std::size_t hash_value, std::size_t bucket_len, detail::bool_) const + { return hash_value & (bucket_len - 1); } + + const key_equal &priv_equal() const + { return static_cast(this->bucket_hash_equal_.get()); } + + key_equal &priv_equal() + { return static_cast(this->bucket_hash_equal_.get()); } + + value_type &priv_value_from_slist_node(slist_node_ptr n) + { return *this->get_real_value_traits().to_value_ptr(dcast_bucket_ptr(n)); } + + const value_type &priv_value_from_slist_node(slist_node_ptr n) const + { return *this->get_real_value_traits().to_value_ptr(dcast_bucket_ptr(n)); } + + const real_bucket_traits &priv_real_bucket_traits(detail::bool_) const + { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_.bucket_traits_; } + + const real_bucket_traits &priv_real_bucket_traits(detail::bool_) const + { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_.bucket_traits_.get_bucket_traits(*this); } + + real_bucket_traits &priv_real_bucket_traits(detail::bool_) + { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_.bucket_traits_; } + + real_bucket_traits &priv_real_bucket_traits(detail::bool_) + { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_.bucket_traits_.get_bucket_traits(*this); } + + const real_bucket_traits &priv_real_bucket_traits() const + { return this->priv_real_bucket_traits(detail::bool_()); } + + real_bucket_traits &priv_real_bucket_traits() + { return this->priv_real_bucket_traits(detail::bool_()); } + + const hasher &priv_hasher() const + { return static_cast(this->bucket_hash_equal_.bucket_hash.get()); } + + hasher &priv_hasher() + { return static_cast(this->bucket_hash_equal_.bucket_hash.get()); } + + bucket_ptr priv_buckets() const + { return this->priv_real_bucket_traits().bucket_begin(); } + + size_type priv_buckets_len() const + { return this->priv_real_bucket_traits().bucket_count(); } + + static node_ptr uncast(const_node_ptr ptr) + { return node_ptr(const_cast(detail::get_pointer(ptr))); } + + node &priv_value_to_node(value_type &v) + { return *this->get_real_value_traits().to_node_ptr(v); } + + const node &priv_value_to_node(const value_type &v) const + { return *this->get_real_value_traits().to_node_ptr(v); } + + size_traits &priv_size_traits() + { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_; } + + const size_traits &priv_size_traits() const + { return this->bucket_hash_equal_.bucket_hash.bucket_plus_size_; } + + template + void priv_erase_range_impl + (size_type bucket_num, siterator before_first_it, siterator end, Disposer disposer, size_type &num_erased) { - size_type buckets_len = this->priv_buckets_len(); - for (bucket_num = 0; bucket_num < buckets_len; ++bucket_num){ - bucket_type &b = this->priv_buckets()[bucket_num]; - if(!b.empty()) - return b.begin(); + const bucket_ptr buckets = priv_buckets(); + bucket_type &b = buckets[bucket_num]; + + if(before_first_it == b.before_begin() && end == b.end()){ + priv_erase_range_impl(bucket_num, 1, disposer, num_erased); + } + else{ + num_erased = 0; + siterator to_erase(before_first_it); + ++to_erase; + slist_node_ptr end_ptr = end.pointed_node(); + while(to_erase != end){ + priv_erase_from_group(end_ptr, dcast_bucket_ptr(to_erase.pointed_node()), optimize_multikey_t()); + to_erase = b.erase_after_and_dispose(before_first_it, make_node_disposer(disposer)); + ++num_erased; + } + this->priv_size_traits().set_size(this->priv_size_traits().get_size()-num_erased); } - return invalid_local_it(this->get_real_bucket_traits()); } - void priv_clear_buckets() - { priv_clear_buckets(this->priv_buckets(), this->priv_buckets_len()); } + template + void priv_erase_range_impl + (size_type first_bucket_num, size_type num_buckets, Disposer disposer, size_type &num_erased) + { + //Now fully clear the intermediate buckets + const bucket_ptr buckets = priv_buckets(); + num_erased = 0; + for(size_type i = first_bucket_num; i < (num_buckets + first_bucket_num); ++i){ + bucket_type &b = buckets[i]; + siterator b_begin(b.before_begin()); + siterator nxt(b_begin); + ++nxt; + siterator end(b.end()); + while(nxt != end){ + priv_init_group(nxt.pointed_node(), optimize_multikey_t()); + nxt = b.erase_after_and_dispose + (b_begin, make_node_disposer(disposer)); + this->priv_size_traits().decrement(); + ++num_erased; + } + } + } - static void priv_clear_buckets(bucket_ptr buckets_ptr, size_type buckets_len) + template + void priv_erase_range( siterator before_first_it, size_type first_bucket + , siterator last_it, size_type last_bucket + , Disposer disposer) + { + size_type num_erased; + if (first_bucket == last_bucket){ + priv_erase_range_impl(first_bucket, before_first_it, last_it, disposer, num_erased); + } + else { + bucket_type *b = (&this->priv_buckets()[0]); + priv_erase_range_impl(first_bucket, before_first_it, b[first_bucket].end(), disposer, num_erased); + if(size_type n = (last_bucket - first_bucket - 1)) + priv_erase_range_impl(first_bucket + 1, n, disposer, num_erased); + priv_erase_range_impl(last_bucket, b[last_bucket].before_begin(), last_it, disposer, num_erased); + } + } + + static node_ptr dcast_bucket_ptr(typename slist_impl::node_ptr p) + { return node_ptr(&static_cast(*p)); } + + std::size_t priv_stored_hash(const value_type &v, detail::true_) + { return node_traits::get_hash(this->get_real_value_traits().to_node_ptr(v)); } + + std::size_t priv_stored_hash(const value_type &v, detail::false_) + { return priv_hasher()(v); } + + std::size_t priv_stored_hash(slist_node_ptr n, detail::true_) + { return node_traits::get_hash(dcast_bucket_ptr(n)); } + + std::size_t priv_stored_hash(slist_node_ptr, detail::false_) + { + //This code should never be reached! + BOOST_INTRUSIVE_INVARIANT_ASSERT(0); + return 0; + } + + static void priv_store_hash(node_ptr p, std::size_t h, detail::true_) + { return node_traits::set_hash(p, h); } + + static void priv_store_hash(node_ptr, std::size_t, detail::false_) + {} + + static void priv_clear_group_nodes(bucket_type &b, detail::true_) + { + siterator it(b.begin()), itend(b.end()); + while(it != itend){ + node_ptr to_erase(dcast_bucket_ptr(it.pointed_node())); + ++it; + group_algorithms::init(to_erase); + } + } + + static void priv_clear_group_nodes(bucket_type &, detail::false_) + {} + + std::size_t priv_get_bucket_num(siterator it) + { return priv_get_bucket_num_hash_dispatch(it, store_hash_t()); } + + std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::true_) + { + return this->priv_hash_to_bucket + (this->priv_stored_hash(it.pointed_node(), store_hash_t())); + } + + std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::false_) + { return priv_get_bucket_num_no_hash_store(it, optimize_multikey_t()); } + + std::size_t priv_get_bucket_num_no_hash_store( siterator it, detail::true_) + { + bucket_ptr f(priv_buckets()), l(f + priv_buckets_len() - 1); + slist_node_ptr bb = priv_get_bucket_before_begin + ( f->end().pointed_node() + , l->end().pointed_node() + , dcast_bucket_ptr(it.pointed_node())); + //Now get the bucket_impl from the iterator + const bucket_type &b = static_cast + (bucket_type::slist_type::container_from_end_iterator(bucket_type::s_iterator_to(*bb))); + //Now just calculate the index b has in the bucket array + return static_cast(&b - &*f); + } + + std::size_t priv_get_bucket_num_no_hash_store( siterator it, detail::false_) + { + bucket_ptr f(priv_buckets()), l(f + priv_buckets_len() - 1); + slist_node_ptr first_ptr(f->cend().pointed_node()) + , last_ptr(l->cend().pointed_node()); + + //The end node is embedded in the singly linked list: + //iterate until we reach it. + while(!(first_ptr <= it.pointed_node() && it.pointed_node() <= last_ptr)){ + ++it; + } + //Now get the bucket_impl from the iterator + const bucket_type &b = static_cast + (bucket_type::container_from_end_iterator(it)); + + //Now just calculate the index b has in the bucket array + return static_cast(&b - &*f); + } + + void priv_erase_from_group(slist_node_ptr end_ptr, node_ptr to_erase_ptr, detail::true_) + { + node_ptr nxt_ptr(node_traits::get_next(to_erase_ptr)); + node_ptr prev_in_group_ptr(group_traits::get_next(to_erase_ptr)); + bool last_in_group = (end_ptr == nxt_ptr) || + (group_traits::get_next(nxt_ptr) != to_erase_ptr); + bool first_in_group = node_traits::get_next(prev_in_group_ptr) != to_erase_ptr; + + if(first_in_group && last_in_group){ + group_algorithms::init(to_erase_ptr); + } + else if(first_in_group){ + group_algorithms::unlink_after(nxt_ptr); + } + else if(last_in_group){ + node_ptr first_in_group = //possible_first_in_group ? possible_first_in_group : + priv_get_first_in_group_of_last_in_group(to_erase_ptr); + group_algorithms::unlink_after(first_in_group); + //possible_first_in_group = 0; + } + else{ + group_algorithms::unlink_after(nxt_ptr); + } + } + + void priv_erase_from_group(slist_node_ptr, node_ptr, detail::false_) + {} + + void priv_init_group(slist_node_ptr n, detail::true_) + { group_algorithms::init(dcast_bucket_ptr(n)); } + + void priv_init_group(slist_node_ptr, detail::false_) + {} + + void priv_insert_in_group(node_ptr first_in_group, node_ptr n, detail::true_) + { + if(first_in_group){ + if(group_algorithms::unique(first_in_group)) + group_algorithms::link_after(first_in_group, n); + else{ + group_algorithms::link_after(node_traits::get_next(first_in_group), n); + } + } + else{ + group_algorithms::init_header(n); + } + } + + static slist_node_ptr priv_get_bucket_before_begin + (slist_node_ptr bucket_beg, slist_node_ptr bucket_end, node_ptr p) + { + //First find the last node of p's group. + //This requires checking the first node of the next group or + //the bucket node. + node_ptr prev_node = p; + node_ptr nxt(node_traits::get_next(p)); + while(!(bucket_beg <= nxt && nxt <= bucket_end) && + (group_traits::get_next(nxt) == prev_node)){ + prev_node = nxt; + nxt = node_traits::get_next(nxt); + } + + //If we've reached the bucket node just return it. + if(bucket_beg <= nxt && nxt <= bucket_end){ + return nxt; + } + + //Otherwise, iterate using group links until the bucket node + node_ptr first_node_of_group = nxt; + node_ptr last_node_group = group_traits::get_next(first_node_of_group); + slist_node_ptr possible_end = node_traits::get_next(last_node_group); + + while(!(bucket_beg <= possible_end && possible_end <= bucket_end)){ + first_node_of_group = dcast_bucket_ptr(possible_end); + last_node_group = group_traits::get_next(first_node_of_group); + possible_end = node_traits::get_next(last_node_group); + } + return possible_end; + } + + static node_ptr priv_get_prev_to_first_in_group(slist_node_ptr bucket_node, node_ptr first_in_group) + { + //Just iterate using group links and obtain the node + //before "first_in_group)" + node_ptr prev_node = dcast_bucket_ptr(bucket_node); + node_ptr nxt(node_traits::get_next(prev_node)); + while(nxt != first_in_group){ + prev_node = group_traits::get_next(nxt); + nxt = node_traits::get_next(prev_node); + } + return prev_node; + } + + static node_ptr priv_get_first_in_group_of_last_in_group(node_ptr last_in_group) + { + //Just iterate using group links and obtain the node + //before "last_in_group" + node_ptr possible_first = group_traits::get_next(last_in_group); + node_ptr possible_first_prev = group_traits::get_next(possible_first); + // The deleted node is at the end of the group, so the + // node in the group pointing to it is at the beginning + // of the group. Find that to change its pointer. + while(possible_first_prev != last_in_group){ + possible_first = possible_first_prev; + possible_first_prev = group_traits::get_next(possible_first); + } + return possible_first; + } + + void priv_insert_in_group(node_ptr, node_ptr, detail::false_) + {} + + static node_ptr priv_get_last_in_group(node_ptr first_in_group) + { return priv_get_last_in_group(first_in_group, optimize_multikey_t()); } + + static node_ptr priv_get_last_in_group(node_ptr first_in_group, detail::true_) + { return group_traits::get_next(first_in_group); } + + static node_ptr priv_get_last_in_group(node_ptr n, detail::false_) + { return n; } + + siterator priv_get_previous + (bucket_type &b, siterator i) + { return priv_get_previous(b, i, optimize_multikey_t()); } + + siterator priv_get_previous + (bucket_type &b, siterator i, detail::true_) + { + node_ptr elem(dcast_bucket_ptr(i.pointed_node())); + node_ptr prev_in_group(group_traits::get_next(elem)); + bool first_in_group = node_traits::get_next(prev_in_group) != elem; + + typename bucket_type::node &n = first_in_group + ? *priv_get_prev_to_first_in_group(b.end().pointed_node(), elem) + : *group_traits::get_next(elem) + ; + return bucket_type::s_iterator_to(n); + } + + siterator priv_get_previous + (bucket_type &b, siterator i, detail::false_) + { return b.previous(i); } + + static siterator priv_get_last(bucket_type &b) + { return priv_get_last(b, optimize_multikey_t()); } + + static siterator priv_get_last(bucket_type &b, detail::true_) + { + //First find the last node of p's group. + //This requires checking the first node of the next group or + //the bucket node. + slist_node_ptr end_ptr(b.end().pointed_node()); + node_ptr possible_end(node_traits::get_next( dcast_bucket_ptr(end_ptr))); + node_ptr last_node_group(possible_end); + + while(end_ptr != possible_end){ + last_node_group = group_traits::get_next(dcast_bucket_ptr(possible_end)); + possible_end = node_traits::get_next(last_node_group); + } + return bucket_type::s_iterator_to(*last_node_group); + } + + static siterator priv_get_last(bucket_type &b, detail::false_) + { return b.previous(b.end()); } + + siterator priv_get_previous_and_next_in_group + (siterator i, node_ptr &nxt_in_group) + { + siterator prev; + node_ptr elem(dcast_bucket_ptr(i.pointed_node())); + bucket_ptr f(priv_buckets()), l(f + priv_buckets_len() - 1); + + slist_node_ptr first_end_ptr(f->cend().pointed_node()); + slist_node_ptr last_end_ptr (l->cend().pointed_node()); + + node_ptr nxt(node_traits::get_next(elem)); + node_ptr prev_in_group(group_traits::get_next(elem)); + bool last_in_group = (first_end_ptr <= nxt && nxt <= last_end_ptr) || + (group_traits::get_next(nxt) != elem); + bool first_in_group = node_traits::get_next(prev_in_group) != elem; + + if(first_in_group){ + node_ptr start_pos; + if(last_in_group){ + start_pos = elem; + nxt_in_group = 0; + } + else{ + start_pos = prev_in_group; + nxt_in_group = node_traits::get_next(elem); + } + slist_node_ptr bucket_node; + if(store_hash){ + bucket_node = this->priv_buckets() + [this->priv_hash_to_bucket + (this->priv_stored_hash(elem, store_hash_t())) + ].before_begin().pointed_node(); + } + else{ + bucket_node = priv_get_bucket_before_begin + (first_end_ptr, last_end_ptr, start_pos); + } + prev = bucket_type::s_iterator_to + (*priv_get_prev_to_first_in_group(bucket_node, elem)); + } + else{ + if(last_in_group){ + nxt_in_group = priv_get_first_in_group_of_last_in_group(elem); + } + else{ + nxt_in_group = node_traits::get_next(elem); + } + prev = bucket_type::s_iterator_to(*group_traits::get_next(elem)); + } + return prev; + } + + template + void priv_erase(const_iterator i, Disposer disposer, detail::true_) + { + siterator elem(i.slist_it()); + node_ptr nxt_in_group; + siterator prev = priv_get_previous_and_next_in_group(elem, nxt_in_group); + bucket_type::s_erase_after_and_dispose(prev, make_node_disposer(disposer)); + if(nxt_in_group) + group_algorithms::unlink_after(nxt_in_group); + if(safemode_or_autounlink) + group_algorithms::init(dcast_bucket_ptr(elem.pointed_node())); + } + + template + void priv_erase(const_iterator i, Disposer disposer, detail::false_) + { + siterator to_erase(i.slist_it()); + bucket_type &b = this->priv_buckets()[this->priv_get_bucket_num(to_erase)]; + siterator prev(priv_get_previous(b, to_erase)); + b.erase_after_and_dispose(prev, make_node_disposer(disposer)); + } + + bucket_ptr priv_invalid_bucket() const + { + const real_bucket_traits &rbt = this->priv_real_bucket_traits(); + return rbt.bucket_begin() + rbt.bucket_count(); + } + + siterator priv_invalid_local_it() const + { return priv_invalid_bucket()->end(); } + + siterator priv_begin(size_type &bucket_num) const + { return priv_begin(bucket_num, cache_begin_t()); } + + siterator priv_begin(size_type &bucket_num, detail::bool_) const + { + size_type n = 0; + size_type buckets_len = this->priv_buckets_len(); + for (n = 0; n < buckets_len; ++n){ + bucket_type &b = this->priv_buckets()[n]; + if(!b.empty()){ + bucket_num = n; + return b.begin(); + } + } + bucket_num = n; + return priv_invalid_local_it(); + } + + siterator priv_begin(size_type &bucket_num, detail::bool_) const + { + bucket_num = this->bucket_hash_equal_.cached_begin_ - this->priv_buckets(); + if(this->bucket_hash_equal_.cached_begin_ == priv_invalid_bucket()){ + return priv_invalid_local_it(); + } + else{ + return this->bucket_hash_equal_.cached_begin_->begin(); + } + } + + void priv_initialize_cache() + { priv_initialize_cache(cache_begin_t()); } + + void priv_initialize_cache(detail::bool_) + { this->bucket_hash_equal_.cached_begin_ = priv_invalid_bucket(); } + + void priv_initialize_cache(detail::bool_) + {} + + void priv_insertion_update_cache(size_type insertion_bucket) + { priv_insertion_update_cache(insertion_bucket, cache_begin_t()); } + + void priv_insertion_update_cache(size_type insertion_bucket, detail::bool_) + { + bucket_ptr p = priv_buckets() + insertion_bucket; + if(p < this->bucket_hash_equal_.cached_begin_){ + this->bucket_hash_equal_.cached_begin_ = p; + } + } + + void priv_insertion_update_cache(size_type, detail::bool_) + {} + + void priv_erasure_update_cache(size_type first_bucket, size_type last_bucket) + { priv_erasure_update_cache(first_bucket, last_bucket, cache_begin_t()); } + + void priv_erasure_update_cache(size_type first_bucket_num, size_type last_bucket_num, detail::bool_) + { + //If the last bucket is the end, the cache must be updated + //to the last position if all + if(priv_get_cache_bucket_num() == first_bucket_num && + priv_buckets()[first_bucket_num].empty() ){ + priv_set_cache(priv_buckets() + last_bucket_num); + priv_erasure_update_cache(); + } + } + + void priv_erasure_update_cache(size_type, size_type, detail::bool_) + {} + + void priv_erasure_update_cache() + { priv_erasure_update_cache(cache_begin_t()); } + + void priv_erasure_update_cache(detail::bool_) + { + if(constant_time_size && !size()){ + priv_initialize_cache(); + } + else{ + size_type current_n = this->bucket_hash_equal_.cached_begin_ - priv_buckets(); + for( const size_type num_buckets = this->priv_buckets_len() + ; current_n < num_buckets + ; ++current_n, ++this->bucket_hash_equal_.cached_begin_){ + if(!this->bucket_hash_equal_.cached_begin_->empty()){ + return; + } + } + priv_initialize_cache(); + } + } + + void priv_erasure_update_cache(detail::bool_) + {} + + void priv_swap_cache(detail::bool_, hashtable_impl &other) + { + std::swap( this->bucket_hash_equal_.cached_begin_ + , other.bucket_hash_equal_.cached_begin_); + } + + void priv_swap_cache(detail::bool_, hashtable_impl &) + {} + + bucket_ptr priv_get_cache() + { return priv_get_cache(cache_begin_t()); } + + bucket_ptr priv_get_cache(detail::bool_) + { return this->bucket_hash_equal_.cached_begin_; } + + bucket_ptr priv_get_cache(detail::bool_) + { return this->priv_buckets(); } + + void priv_set_cache(bucket_ptr p) + { priv_set_cache(p, cache_begin_t()); } + + void priv_set_cache(bucket_ptr p, detail::bool_) + { this->bucket_hash_equal_.cached_begin_ = p; } + + void priv_set_cache(bucket_ptr, detail::bool_) + {} + + size_type priv_get_cache_bucket_num() + { return priv_get_cache_bucket_num(cache_begin_t()); } + + size_type priv_get_cache_bucket_num(detail::bool_) + { return this->bucket_hash_equal_.cached_begin_ - this->priv_buckets(); } + + size_type priv_get_cache_bucket_num(detail::bool_) + { return 0u; } + + void priv_clear_buckets() + { + this->priv_clear_buckets + ( priv_get_cache() + , this->priv_buckets_len() - (priv_get_cache() - priv_buckets())); + } + + void priv_initialize_buckets() + { + this->priv_clear_buckets + ( priv_buckets(), this->priv_buckets_len()); + } + + void priv_clear_buckets(bucket_ptr buckets_ptr, size_type buckets_len) { for(; buckets_len--; ++buckets_ptr){ if(safemode_or_autounlink){ + priv_clear_group_nodes(*buckets_ptr, optimize_multikey_t()); buckets_ptr->clear_and_dispose(detail::init_disposer()); } else{ buckets_ptr->clear(); } } + priv_initialize_cache(); } template siterator priv_find ( const KeyType &key, KeyHasher hash_func - , KeyValueEqual equal_func, size_type &bucket_number, size_type &h) const + , KeyValueEqual equal_func, size_type &bucket_number, std::size_t &h, siterator &previt) const { - bucket_number = from_hash_to_bucket((h = hash_func(key))); + bucket_number = priv_hash_to_bucket((h = hash_func(key))); if(constant_time_size && this->empty()){ - return invalid_local_it(this->get_real_bucket_traits()); + return priv_invalid_local_it(); } bucket_type &b = this->priv_buckets()[bucket_number]; - siterator it = b.begin(); + previt = b.before_begin(); + siterator it = previt; + ++it; while(it != b.end()){ - const value_type &v = - *this->get_real_value_traits().to_value_ptr(it.pointed_node()); + const value_type &v = priv_value_from_slist_node(it.pointed_node()); if(equal_func(key, v)){ return it; } + if(optimize_multikey){ + previt = bucket_type::s_iterator_to + (*priv_get_last_in_group(dcast_bucket_ptr(it.pointed_node()))); + it = previt; + } + else{ + previt = it; + } ++it; } - return invalid_local_it(this->get_real_bucket_traits()); + return priv_invalid_local_it(); } template @@ -1820,33 +2440,44 @@ class hashtable_impl , size_type &bucket_number_second , size_type &count) const { - size_type h; + std::size_t h; count = 0; + siterator prev; //Let's see if the element is present std::pair to_return - ( priv_find(key, hash_func, equal_func, bucket_number_first, h) - , invalid_local_it(this->get_real_bucket_traits())); + ( priv_find(key, hash_func, equal_func, bucket_number_first, h, prev) + , priv_invalid_local_it()); if(to_return.first == to_return.second){ bucket_number_second = bucket_number_first; return to_return; } - ++count; //If it's present, find the first that it's not equal in //the same bucket bucket_type &b = this->priv_buckets()[bucket_number_first]; siterator it = to_return.first; - ++it; - - while(it != b.end()){ - const value_type &v = - *this->get_real_value_traits().to_value_ptr(it.pointed_node()); - if(!equal_func(key, v)){ - to_return.second = it; + if(optimize_multikey){ + to_return.second = bucket_type::s_iterator_to + (*node_traits::get_next(priv_get_last_in_group + (dcast_bucket_ptr(it.pointed_node())))); + count = std::distance(it, to_return.second); + if(to_return.second != b.end()){ bucket_number_second = bucket_number_first; return to_return; } - ++it; + } + else{ ++count; + ++it; + while(it != b.end()){ + const value_type &v = priv_value_from_slist_node(it.pointed_node()); + if(!equal_func(key, v)){ + to_return.second = it; + bucket_number_second = bucket_number_first; + return to_return; + } + ++it; + ++count; + } } //If we reached the end, find the first, non-empty bucket @@ -1861,22 +2492,24 @@ class hashtable_impl } //Otherwise, return the end node - to_return.second = invalid_local_it(this->get_real_bucket_traits()); + to_return.second = priv_invalid_local_it(); return to_return; } /// @endcond }; /// @cond -template +template < class T + , bool UniqueKeys + , class O1 = none, class O2 = none + , class O3 = none, class O4 = none + , class O5 = none, class O6 = none + , class O7 = none, class O8 = none + > struct make_hashtable_opt { typedef typename pack_options - < uset_defaults, O1, O2, O3, O4, O5, O6, O7>::type packed_options; + < uset_defaults, O1, O2, O3, O4, O5, O6, O7, O8>::type packed_options; //Real value traits must be calculated from options typedef typename detail::get_value_traits @@ -1891,9 +2524,16 @@ struct make_hashtable_opt >::type real_value_traits; typedef typename packed_options::bucket_traits specified_bucket_traits; /// @endcond - //Real bucket traits must be calculated from options and calculated valute_traits - typedef typename get_slist_impl - ::type slist_impl; + + //Real bucket traits must be calculated from options and calculated value_traits + typedef typename detail::get_slist_impl + ::type + >::type slist_impl; + + typedef typename detail::reduced_slist_node_traits + ::type node_traits; + typedef typename detail::if_c< detail::is_same < specified_bucket_traits @@ -1905,12 +2545,14 @@ struct make_hashtable_opt typedef usetopt < value_traits + , UniqueKeys , typename packed_options::hash , typename packed_options::equal , typename packed_options::size_type , packed_options::constant_time_size , real_bucket_traits , packed_options::power_2_buckets + , packed_options::cache_begin > type; }; /// @endcond @@ -1923,7 +2565,7 @@ template template #endif struct make_hashtable @@ -1931,7 +2573,7 @@ struct make_hashtable /// @cond typedef hashtable_impl < typename make_hashtable_opt - ::type + ::type > implementation_defined; /// @endcond @@ -1939,12 +2581,12 @@ struct make_hashtable }; #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED -template +template class hashtable - : public make_hashtable::type + : public make_hashtable::type { typedef typename make_hashtable - ::type Base; + ::type Base; public: typedef typename Base::value_traits value_traits; diff --git a/include/boost/intrusive/intrusive_fwd.hpp b/include/boost/intrusive/intrusive_fwd.hpp index 9228bcb..c260187 100644 --- a/include/boost/intrusive/intrusive_fwd.hpp +++ b/include/boost/intrusive/intrusive_fwd.hpp @@ -283,8 +283,7 @@ template > class bs_set_member_hook; -//hash/unordered -//rbtree/set/multiset +//hashtable/unordered_set/unordered_multiset template < class T , class O1 = none @@ -294,6 +293,7 @@ template , class O5 = none , class O6 = none , class O7 = none + , class O8 = none > class hashtable; @@ -306,6 +306,7 @@ template , class O5 = none , class O6 = none , class O7 = none + , class O8 = none > class unordered_set; @@ -318,6 +319,7 @@ template , class O5 = none , class O6 = none , class O7 = none + , class O8 = none > class unordered_multiset; diff --git a/include/boost/intrusive/linear_slist_algorithms.hpp b/include/boost/intrusive/linear_slist_algorithms.hpp index 681c13b..9f1f599 100644 --- a/include/boost/intrusive/linear_slist_algorithms.hpp +++ b/include/boost/intrusive/linear_slist_algorithms.hpp @@ -136,7 +136,7 @@ class linear_slist_algorithms //! //! Throws: Nothing. static void init_header(node_ptr this_node) - { NodeTraits::set_next(this_node, 0); } + { NodeTraits::set_next(this_node, node_ptr(0)); } //! Requires: this_node and prev_init_node must be in the same linear list. //! @@ -195,7 +195,7 @@ class linear_slist_algorithms //! Complexity: This function is linear to the contained elements. static node_ptr reverse(node_ptr p) { - if(!p) return 0; + if(!p) return node_ptr(0); node_ptr i = NodeTraits::get_next(p); node_ptr first(p); while(i){ @@ -218,7 +218,7 @@ class linear_slist_algorithms //! Complexity: Linear to the number of elements plus the number moved positions. static std::pair move_first_n_backwards(node_ptr p, std::size_t n) { - std::pair ret(0, 0); + std::pair ret(node_ptr(0), node_ptr(0)); //Null shift, or count() == 0 or 1, nothing to do if(!n || !p || !NodeTraits::get_next(p)){ return ret; @@ -252,12 +252,12 @@ class linear_slist_algorithms //If the p has not been found in the previous loop, find it //starting in the new first node and unlink it if(!end_found){ - old_last = base_t::get_previous_node(first, 0); + old_last = base_t::get_previous_node(first, node_ptr(0)); } //Now link p after the new last node NodeTraits::set_next(old_last, p); - NodeTraits::set_next(new_last, 0); + NodeTraits::set_next(new_last, node_ptr(0)); ret.first = first; ret.second = new_last; return ret; @@ -273,7 +273,7 @@ class linear_slist_algorithms //! Complexity: Linear to the number of elements plus the number moved positions. static std::pair move_first_n_forward(node_ptr p, std::size_t n) { - std::pair ret(0, 0); + std::pair ret(node_ptr(0), node_ptr(0)); //Null shift, or count() == 0 or 1, nothing to do if(!n || !p || !NodeTraits::get_next(p)) return ret; @@ -311,7 +311,7 @@ class linear_slist_algorithms node_ptr new_first(node_traits::get_next(new_last)); //Now put the old beginning after the old end NodeTraits::set_next(old_last, p); - NodeTraits::set_next(new_last, 0); + NodeTraits::set_next(new_last, node_ptr(0)); ret.first = new_first; ret.second = new_last; return ret; diff --git a/include/boost/intrusive/list.hpp b/include/boost/intrusive/list.hpp index 5968dd2..ca7f7f6 100644 --- a/include/boost/intrusive/list.hpp +++ b/include/boost/intrusive/list.hpp @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include @@ -732,17 +732,13 @@ class list_impl void clone_from(const list_impl &src, Cloner cloner, Disposer disposer) { this->clear_and_dispose(disposer); - BOOST_INTRUSIVE_TRY{ - const_iterator b(src.begin()), e(src.end()); - for(; b != e; ++b){ - this->push_back(*cloner(*b)); - } + detail::exception_disposer + rollback(*this, disposer); + const_iterator b(src.begin()), e(src.end()); + for(; b != e; ++b){ + this->push_back(*cloner(*b)); } - BOOST_INTRUSIVE_CATCH(...){ - this->clear_and_dispose(disposer); - BOOST_INTRUSIVE_RETHROW; - } - BOOST_INTRUSIVE_CATCH_END + rollback.release(); } //! Requires: value must be an lvalue and p must be a valid iterator of *this. diff --git a/include/boost/intrusive/options.hpp b/include/boost/intrusive/options.hpp index ab1f3dc..2e55c58 100644 --- a/include/boost/intrusive/options.hpp +++ b/include/boost/intrusive/options.hpp @@ -406,6 +406,24 @@ struct store_hash /// @endcond }; +//!This option setter specifies if the unordered hook +//!should offer room to store another link to another node +//!with the same key. +//!Storing this link will speed up lookups and insertions on +//!unordered_multiset containers with a great number of elements +//!with the same key. +template +struct optimize_multikey +{ +/// @cond + template + struct pack : Base + { + static const bool optimize_multikey = Enabled; + }; +/// @endcond +}; + //!This option setter specifies if the bucket array will be always power of two. //!This allows using masks instead of the default modulo operation to determine //!the bucket number from the hash value, leading to better performance. @@ -423,6 +441,22 @@ struct power_2_buckets /// @endcond }; +//!This option setter specifies if the container will cache a pointer to the first +//!non-empty bucket so that begin() is always constant-time. +//!This is specially helpful when we can have containers with a few elements +//!but with big bucket arrays (that is, hashtables with low load factors). +template +struct cache_begin +{ +/// @cond + template + struct pack : Base + { + static const bool cache_begin = Enabled; + }; +/// @endcond +}; + /// @cond template @@ -500,6 +534,7 @@ struct hook_defaults , optimize_size , store_hash , linear + , optimize_multikey >::type {}; diff --git a/include/boost/intrusive/pointer_plus_2_bits.hpp b/include/boost/intrusive/pointer_plus_2_bits.hpp deleted file mode 100644 index c7d9592..0000000 --- a/include/boost/intrusive/pointer_plus_2_bits.hpp +++ /dev/null @@ -1,82 +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_POINTER_PLUS_2_BIT_HPP -#define BOOST_INTRUSIVE_POINTER_PLUS_2_BIT_HPP - -namespace boost { -namespace intrusive { - -//!This trait class is used to know if a pointer -//!can embed 2 extra bits of information if -//!it's going to be used to point to objects -//!with an alignment of "Alignment" bytes. -template -struct has_pointer_plus_2_bits -{ - static const bool value = false; -}; - -//!This is an specialization for raw pointers. -//!Raw pointers can embed two extra bits in the lower bits -//!if the alignment is multiple of 4. -template -struct has_pointer_plus_2_bits -{ - static const bool value = (N % 4u == 0); -}; - -//!This is class that is supposed to have static methods -//!to embed 2 extra bits of information in a pointer. -//! -//!This is a declaration and there is no default implementation, -//!because operations to embed bits change with every pointer type. -//! -//!An implementation that detects that a pointer type whose -//!has_pointer_plus_2_bits<>::value is non-zero can make use of these -//!operations to embed bits in the pointer. -template -struct pointer_plus_2_bits -{ - static const bool value = false; -}; - -//!This is the specialization to embed 2 extra bits of information -//!in a raw pointer. Extra bits are stored in the lower bits of the pointer. -template -struct pointer_plus_2_bits -{ - typedef T* pointer; - - static pointer get_pointer(pointer n) - { return pointer(std::size_t(n) & ~std::size_t(3u)); } - - static void set_pointer(pointer &n, pointer p) - { - assert(0 == (std::size_t(p) & std::size_t(3u))); - n = pointer(std::size_t(p) | (std::size_t(n) & std::size_t(3u))); - } - - static std::size_t get_bits(pointer n) - { return (std::size_t(n) & std::size_t(3u)); } - - static void set_bits(pointer &n, std::size_t c) - { - assert(c < 4); - n = pointer(std::size_t(get_pointer(n)) | c); - } -}; - -} //namespace intrusive -} //namespace boost - -#endif //BOOST_INTRUSIVE_POINTER_PLUS_2_BIT_HPP diff --git a/include/boost/intrusive/pointer_plus_bit.hpp b/include/boost/intrusive/pointer_plus_bit.hpp deleted file mode 100644 index 863f5f8..0000000 --- a/include/boost/intrusive/pointer_plus_bit.hpp +++ /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. -// -///////////////////////////////////////////////////////////////////////////// - -#ifndef BOOST_INTRUSIVE_POINTER_PLUS_BIT_HPP -#define BOOST_INTRUSIVE_POINTER_PLUS_BIT_HPP - -namespace boost { -namespace intrusive { - -//!This trait class is used to know if a pointer -//!can embed an extra bit of information if -//!it's going to be used to point to objects -//!with an alignment of "Alignment" bytes. -template -struct has_pointer_plus_bit -{ - static const bool value = false; -}; - -//!This is an specialization for raw pointers. -//!Raw pointers can embed an extra bit in the lower bit -//!if the alignment is multiple of 2. -template -struct has_pointer_plus_bit -{ - static const bool value = (N % 2u == 0); -}; - -//!This is class that is supposed to have static methods -//!to embed an extra bit of information in a pointer. -//!This is a declaration and there is no default implementation, -//!because operations to embed the bit change with every pointer type. -//! -//!An implementation that detects that a pointer type whose -//!has_pointer_plus_bit<>::value is non-zero can make use of these -//!operations to embed the bit in the pointer. -template -struct pointer_plus_bit -{ - static const bool value = false; -}; - -//!This is the specialization to embed an extra bit of information -//!in a raw pointer. The extra bit is stored in the lower bit of the pointer. -template -struct pointer_plus_bit -{ - typedef T* pointer; - - static pointer get_pointer(pointer n) - { return pointer(std::size_t(n) & ~std::size_t(1u)); } - - static void set_pointer(pointer &n, pointer p) - { - assert(0 == (std::size_t(p) & std::size_t(1u))); - n = pointer(std::size_t(p) | (std::size_t(n) & std::size_t(1u))); - } - - static bool get_bit(pointer n) - { return (std::size_t(n) & std::size_t(1u)) != 0; } - - static void set_bit(pointer &n, bool c) - { n = pointer(std::size_t(get_pointer(n)) | std::size_t(c)); } -}; - -} //namespace intrusive -} //namespace boost - -#endif //BOOST_INTRUSIVE_POINTER_PLUS_BIT_HPP diff --git a/include/boost/intrusive/pointer_plus_bits.hpp b/include/boost/intrusive/pointer_plus_bits.hpp new file mode 100644 index 0000000..7c77e90 --- /dev/null +++ b/include/boost/intrusive/pointer_plus_bits.hpp @@ -0,0 +1,81 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (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_POINTER_PLUS_BITS_HPP +#define BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP + +#include //ls_zeros + +namespace boost { +namespace intrusive { + +//!This trait class is used to know if a pointer +//!can embed extra bits of information if +//!it's going to be used to point to objects +//!with an alignment of "Alignment" bytes. +template +struct max_pointer_plus_bits +{ + static const std::size_t value = 0; +}; + +//!This is an specialization for raw pointers. +//!Raw pointers can embed extra bits in the lower bits +//!if the alignment is multiple of 2pow(NumBits). +template +struct max_pointer_plus_bits +{ + static const std::size_t value = detail::ls_zeros::value; +}; + +//!This is class that is supposed to have static methods +//!to embed extra bits of information in a pointer. +//!This is a declaration and there is no default implementation, +//!because operations to embed the bits change with every pointer type. +//! +//!An implementation that detects that a pointer type whose +//!has_pointer_plus_bits<>::value is non-zero can make use of these +//!operations to embed the bits in the pointer. +template +struct pointer_plus_bits; + +//!This is the specialization to embed extra bits of information +//!in a raw pointer. The extra bits are stored in the lower bit of the pointer. +template +struct pointer_plus_bits +{ + static const std::size_t Mask = ((std::size_t(1u) << NumBits) - 1); + typedef T* pointer; + + static pointer get_pointer(pointer n) + { return pointer(std::size_t(n) & ~Mask); } + + static void set_pointer(pointer &n, pointer p) + { + assert(0 == (std::size_t(p) & Mask)); + n = pointer(std::size_t(p) | (std::size_t(n) & Mask)); + } + + static std::size_t get_bits(pointer n) + { return (std::size_t(n) & Mask); } + + static void set_bits(pointer &n, std::size_t c) + { + assert(c <= Mask); + n = pointer(std::size_t(get_pointer(n)) | c); + } +}; + +} //namespace intrusive +} //namespace boost + +#endif //BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP diff --git a/include/boost/intrusive/rbtree.hpp b/include/boost/intrusive/rbtree.hpp index 1682a35..4ec26ab 100644 --- a/include/boost/intrusive/rbtree.hpp +++ b/include/boost/intrusive/rbtree.hpp @@ -379,7 +379,7 @@ class rbtree_impl //! Precondition: end_iterator must be a valid end const_iterator //! of rbtree. //! - //! Effects: Returns a const reference to the rbtree associated to the end iterator + //! Effects: Returns a const reference to the rbtree associated to the iterator //! //! Throws: Nothing. //! @@ -387,6 +387,28 @@ class rbtree_impl static const rbtree_impl &container_from_end_iterator(const_iterator end_iterator) { return priv_container_from_end_iterator(end_iterator); } + //! Precondition: it must be a valid iterator + //! of rbtree. + //! + //! Effects: Returns a const reference to the tree associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static rbtree_impl &container_from_iterator(iterator it) + { return priv_container_from_iterator(it); } + + //! Precondition: it must be a valid end const_iterator + //! of rbtree. + //! + //! Effects: Returns a const reference to the tree associated to the end iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const rbtree_impl &container_from_iterator(const_iterator it) + { return priv_container_from_iterator(it); } + //! Effects: Returns the value_compare object used by the tree. //! //! Complexity: Constant. @@ -1176,33 +1198,26 @@ class rbtree_impl static void init_node(reference value) { node_algorithms::init(value_traits::to_node_ptr(value)); } -/* - //! Effects: removes x from a tree of the appropriate type. It has no effect, - //! if x is not in such a tree. + //! Effects: removes "value" from the container. //! //! Throws: Nothing. //! - //! Complexity: Constant time. + //! Complexity: Logarithmic time. //! - //! Note: This static function is only usable with the "safe mode" - //! hook and non-constant time size lists. Otherwise, the user must use - //! the non-static "erase(reference )" member. If the user calls - //! this function with a non "safe mode" or constant time size list - //! a compilation error will be issued. - template - static void remove_node(T& value) + //! Note: This static function is only usable with non-constant + //! time size containers that have stateless comparison functors. + //! + //! If the user calls + //! this function with a constant time size container or stateful comparison + //! functor a compilation error will be issued. + static void remove_node(reference value) { - //This function is only usable for safe mode hooks and non-constant - //time lists. - //BOOST_STATIC_ASSERT((!(safemode_or_autounlink && constant_time_size))); BOOST_STATIC_ASSERT((!constant_time_size)); - BOOST_STATIC_ASSERT((boost::is_convertible::value)); node_ptr to_remove(value_traits::to_node_ptr(value)); - node_algorithms::unlink_and_rebalance(to_remove); + node_algorithms::unlink(to_remove); if(safemode_or_autounlink) node_algorithms::init(to_remove); } -*/ /// @cond private: @@ -1233,6 +1248,9 @@ class rbtree_impl rbtree_impl *rb = detail::parent_from_member(d, &rbtree_impl::data_); return *rb; } + + static rbtree_impl &priv_container_from_iterator(const const_iterator &it) + { return priv_container_from_end_iterator(it.end_iterator_from_it()); } }; #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED @@ -1426,6 +1444,12 @@ class rbtree static const rbtree &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static rbtree &container_from_it(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const rbtree &container_from_it(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } }; #endif diff --git a/include/boost/intrusive/rbtree_algorithms.hpp b/include/boost/intrusive/rbtree_algorithms.hpp index c5fae2a..8699d4c 100644 --- a/include/boost/intrusive/rbtree_algorithms.hpp +++ b/include/boost/intrusive/rbtree_algorithms.hpp @@ -54,7 +54,6 @@ #include #include -#include #include #include @@ -695,6 +694,16 @@ class rbtree_algorithms rebalance_after_insertion(header, new_value); } + //! Requires: "n" must be a node inserted in a tree. + //! + //! Effects: Returns a pointer to the header node of the tree. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + static node_ptr get_header(node_ptr n) + { return tree_algorithms::get_header(n); } + /// @cond private: diff --git a/include/boost/intrusive/set.hpp b/include/boost/intrusive/set.hpp index 03c52ab..de0b6c7 100644 --- a/include/boost/intrusive/set.hpp +++ b/include/boost/intrusive/set.hpp @@ -226,7 +226,7 @@ class set_impl //! Precondition: end_iterator must be a valid end iterator //! of set. //! - //! Effects: Returns a const reference to the set associated to the end iterator + //! Effects: Returns a reference to the set associated to the end iterator //! //! Throws: Nothing. //! @@ -253,6 +253,34 @@ class set_impl , &set_impl::tree_); } + //! Precondition: it must be a valid iterator of set. + //! + //! Effects: Returns a reference to the set associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static set_impl &container_from_iterator(iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &set_impl::tree_); + } + + //! Precondition: it must be a valid const_iterator of set. + //! + //! Effects: Returns a const reference to the set associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const set_impl &container_from_iterator(const_iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &set_impl::tree_); + } + //! Effects: Returns the key_compare object used by the set. //! //! Complexity: Constant. @@ -1086,6 +1114,12 @@ class set static const set &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static set &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const set &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } }; #endif @@ -1318,6 +1352,34 @@ class multiset_impl , &multiset_impl::tree_); } + //! Precondition: it must be a valid iterator of multiset. + //! + //! Effects: Returns a const reference to the multiset associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static multiset_impl &container_from_iterator(iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &multiset_impl::tree_); + } + + //! Precondition: it must be a valid const_iterator of multiset. + //! + //! Effects: Returns a const reference to the multiset associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static const multiset_impl &container_from_iterator(const_iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &multiset_impl::tree_); + } + //! Effects: Returns the key_compare object used by the multiset. //! //! Complexity: Constant. @@ -1932,6 +1994,21 @@ class multiset_impl void replace_node(iterator replace_this, reference with_this) { tree_.replace_node(replace_this, with_this); } + //! Effects: removes "value" from the container. + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic time. + //! + //! Note: This static function is only usable with non-constant + //! time size containers that have stateless comparison functors. + //! + //! If the user calls + //! this function with a constant time size container or stateful comparison + //! functor a compilation error will be issued. + static void remove_node(reference value) + { tree_type::remove_node(value); } + /// @cond friend bool operator==(const multiset_impl &x, const multiset_impl &y) { return x.tree_ == y.tree_; } @@ -2058,6 +2135,12 @@ class multiset static const multiset &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static multiset &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const multiset &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } }; #endif diff --git a/include/boost/intrusive/sg_set.hpp b/include/boost/intrusive/sg_set.hpp index 2ef7799..66d461d 100644 --- a/include/boost/intrusive/sg_set.hpp +++ b/include/boost/intrusive/sg_set.hpp @@ -252,6 +252,34 @@ class sg_set_impl , &sg_set_impl::tree_); } + //! Precondition: it must be a valid iterator of set. + //! + //! Effects: Returns a reference to the set associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static sg_set_impl &container_from_iterator(iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &sg_set_impl::tree_); + } + + //! Precondition: it must be a valid const_iterator of set. + //! + //! Effects: Returns a const reference to the set associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const sg_set_impl &container_from_iterator(const_iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &sg_set_impl::tree_); + } + //! Effects: Returns the key_compare object used by the sg_set. //! //! Complexity: Constant. @@ -1124,6 +1152,12 @@ class sg_set static const sg_set &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static sg_set &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const sg_set &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } }; #endif @@ -1356,6 +1390,34 @@ class sg_multiset_impl , &sg_multiset_impl::tree_); } + //! Precondition: it must be a valid iterator of multiset. + //! + //! Effects: Returns a const reference to the multiset associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static sg_multiset_impl &container_from_iterator(iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &sg_multiset_impl::tree_); + } + + //! Precondition: it must be a valid const_iterator of multiset. + //! + //! Effects: Returns a const reference to the multiset associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static const sg_multiset_impl &container_from_iterator(const_iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &sg_multiset_impl::tree_); + } + //! Effects: Returns the key_compare object used by the sg_multiset. //! //! Complexity: Constant. @@ -2135,6 +2197,12 @@ class sg_multiset static const sg_multiset &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static sg_multiset &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const sg_multiset &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } }; #endif diff --git a/include/boost/intrusive/sgtree.hpp b/include/boost/intrusive/sgtree.hpp index d62851f..44ba705 100644 --- a/include/boost/intrusive/sgtree.hpp +++ b/include/boost/intrusive/sgtree.hpp @@ -527,6 +527,28 @@ class sgtree_impl static const sgtree_impl &container_from_end_iterator(const_iterator end_iterator) { return priv_container_from_end_iterator(end_iterator); } + //! Precondition: it must be a valid iterator + //! of rbtree. + //! + //! Effects: Returns a const reference to the tree associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static sgtree_impl &container_from_iterator(iterator it) + { return priv_container_from_iterator(it); } + + //! Precondition: it must be a valid end const_iterator + //! of rbtree. + //! + //! Effects: Returns a const reference to the tree associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const sgtree_impl &container_from_iterator(const_iterator it) + { return priv_container_from_iterator(it); } + //! Effects: Returns the value_compare object used by the tree. //! //! Complexity: Constant. @@ -1442,6 +1464,9 @@ class sgtree_impl sgtree_impl *scapegoat = detail::parent_from_member(d, &sgtree_impl::data_); return *scapegoat; } + + static sgtree_impl &priv_container_from_iterator(const const_iterator &it) + { return priv_container_from_end_iterator(it.end_iterator_from_it()); } }; #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED diff --git a/include/boost/intrusive/sgtree_algorithms.hpp b/include/boost/intrusive/sgtree_algorithms.hpp index 4f2a9c7..d37f5b3 100644 --- a/include/boost/intrusive/sgtree_algorithms.hpp +++ b/include/boost/intrusive/sgtree_algorithms.hpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include @@ -640,6 +639,16 @@ class sgtree_algorithms static node_ptr rebalance_subtree(node_ptr old_root) { return tree_algorithms::rebalance_subtree(old_root); } + //! Requires: "n" must be a node inserted in a tree. + //! + //! Effects: Returns a pointer to the header node of the tree. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + static node_ptr get_header(node_ptr n) + { return tree_algorithms::get_header(n); } + /// @cond private: diff --git a/include/boost/intrusive/slist.hpp b/include/boost/intrusive/slist.hpp index 232ab7e..5a264e4 100644 --- a/include/boost/intrusive/slist.hpp +++ b/include/boost/intrusive/slist.hpp @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -25,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -183,10 +183,12 @@ class slist_impl BOOST_STATIC_ASSERT(!(cache_last && ((int)real_value_traits::link_mode == (int)auto_unlink))); node_ptr get_end_node() - { return node_ptr(linear ? 0 : this->get_root_node()); } + { return node_ptr(linear ? node_ptr(0) : this->get_root_node()); } const_node_ptr get_end_node() const - { return const_node_ptr(linear ? 0 : this->get_root_node()); } + { + return const_node_ptr + (linear ? const_node_ptr(0) : this->get_root_node()); } node_ptr get_root_node() { return node_ptr(&data_.root_plus_size_.root_); } @@ -203,13 +205,10 @@ class slist_impl void set_last_node(node_ptr n) { return this->set_last_node(n, detail::bool_()); } - node_ptr get_last_node(detail::bool_) + static node_ptr get_last_node(detail::bool_) { return node_ptr(0); } - const_node_ptr get_last_node(detail::bool_) const - { return const_node_ptr(0); } - - void set_last_node(node_ptr, detail::bool_) + static void set_last_node(node_ptr, detail::bool_) {} node_ptr get_last_node(detail::bool_) @@ -667,18 +666,14 @@ class slist_impl void clone_from(const slist_impl &src, Cloner cloner, Disposer disposer) { this->clear_and_dispose(disposer); - BOOST_INTRUSIVE_TRY{ - iterator prev(this->before_begin()); - const_iterator b(src.begin()), e(src.end()); - for(; b != e; ++b){ - prev = this->insert_after(prev, *cloner(*b)); - } + detail::exception_disposer + rollback(*this, disposer); + iterator prev(this->before_begin()); + const_iterator b(src.begin()), e(src.end()); + for(; b != e; ++b){ + prev = this->insert_after(prev, *cloner(*b)); } - BOOST_INTRUSIVE_CATCH(...){ - this->clear_and_dispose(disposer); - BOOST_INTRUSIVE_RETHROW; - } - BOOST_INTRUSIVE_CATCH_END + rollback.release(); } //! Requires: value must be an lvalue and prev_p must point to an element @@ -849,13 +844,36 @@ class slist_impl if(cache_last && (to_erase == this->get_last_node())){ this->set_last_node(prev_n); } - this->priv_size_traits().decrement(); if(safemode_or_autounlink) node_algorithms::init(to_erase); disposer(get_real_value_traits().to_value_ptr(to_erase)); + this->priv_size_traits().decrement(); return it; } + /// @cond + + template + static iterator s_erase_after_and_dispose(iterator prev, Disposer disposer) + { + BOOST_STATIC_ASSERT(((!cache_last)&&(!constant_time_size)&&(!stateful_value_traits))); + iterator it(prev); + ++it; + node_ptr to_erase(it.pointed_node()); + ++it; + node_ptr prev_n(prev.pointed_node()); + node_algorithms::unlink_after(prev_n); + if(safemode_or_autounlink) + node_algorithms::init(to_erase); + disposer(real_value_traits::to_value_ptr(to_erase)); + return it; + } + + static iterator s_erase_after(iterator prev) + { return s_erase_after_and_dispose(prev, detail::null_disposer()); } + + /// @endcond + //! Requires: Disposer::operator()(pointer) shouldn't throw. //! //! Effects: Erases the range (before_first, last) from diff --git a/include/boost/intrusive/splay_set.hpp b/include/boost/intrusive/splay_set.hpp index 82aa409..104063a 100644 --- a/include/boost/intrusive/splay_set.hpp +++ b/include/boost/intrusive/splay_set.hpp @@ -252,6 +252,34 @@ class splay_set_impl , &splay_set_impl::tree_); } + //! Precondition: it must be a valid iterator of set. + //! + //! Effects: Returns a reference to the set associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static splay_set_impl &container_from_iterator(iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &splay_set_impl::tree_); + } + + //! Precondition: it must be a valid const_iterator of set. + //! + //! Effects: Returns a const reference to the set associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const splay_set_impl &container_from_iterator(const_iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &splay_set_impl::tree_); + } + //! Effects: Returns the key_compare object used by the splay_set. //! //! Complexity: Constant. @@ -1161,6 +1189,12 @@ class splay_set static const splay_set &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static splay_set &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const splay_set &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } }; #endif @@ -1393,6 +1427,34 @@ class splay_multiset_impl , &splay_multiset_impl::tree_); } + //! Precondition: it must be a valid iterator of multiset. + //! + //! Effects: Returns a const reference to the multiset associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static splay_multiset_impl &container_from_iterator(iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &splay_multiset_impl::tree_); + } + + //! Precondition: it must be a valid const_iterator of multiset. + //! + //! Effects: Returns a const reference to the multiset associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Constant. + static const splay_multiset_impl &container_from_iterator(const_iterator it) + { + return *detail::parent_from_member + ( &tree_type::container_from_iterator(it) + , &splay_multiset_impl::tree_); + } + //! Effects: Returns the key_compare object used by the splay_multiset. //! //! Complexity: Constant. @@ -2209,6 +2271,12 @@ class splay_multiset static const splay_multiset &container_from_end_iterator(const_iterator end_iterator) { return static_cast(Base::container_from_end_iterator(end_iterator)); } + + static splay_multiset &container_from_iterator(iterator it) + { return static_cast(Base::container_from_iterator(it)); } + + static const splay_multiset &container_from_iterator(const_iterator it) + { return static_cast(Base::container_from_iterator(it)); } }; #endif diff --git a/include/boost/intrusive/splaytree.hpp b/include/boost/intrusive/splaytree.hpp index d733f50..e356963 100644 --- a/include/boost/intrusive/splaytree.hpp +++ b/include/boost/intrusive/splaytree.hpp @@ -387,6 +387,28 @@ class splaytree_impl static const splaytree_impl &container_from_end_iterator(const_iterator end_iterator) { return priv_container_from_end_iterator(end_iterator); } + //! Precondition: it must be a valid iterator + //! of rbtree. + //! + //! Effects: Returns a const reference to the tree associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static splaytree_impl &container_from_iterator(iterator it) + { return priv_container_from_iterator(it); } + + //! Precondition: it must be a valid end const_iterator + //! of rbtree. + //! + //! Effects: Returns a const reference to the tree associated to the iterator + //! + //! Throws: Nothing. + //! + //! Complexity: Logarithmic. + static const splaytree_impl &container_from_iterator(const_iterator it) + { return priv_container_from_iterator(it); } + //! Effects: Returns the value_compare object used by the tree. //! //! Complexity: Constant. @@ -1312,6 +1334,9 @@ class splaytree_impl splaytree_impl *rb = detail::parent_from_member(d, &splaytree_impl::data_); return *rb; } + + static splaytree_impl &priv_container_from_iterator(const const_iterator &it) + { return priv_container_from_end_iterator(it.end_iterator_from_it()); } }; #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED diff --git a/include/boost/intrusive/splaytree_algorithms.hpp b/include/boost/intrusive/splaytree_algorithms.hpp index 2fbcb92..f42cd3c 100644 --- a/include/boost/intrusive/splaytree_algorithms.hpp +++ b/include/boost/intrusive/splaytree_algorithms.hpp @@ -50,13 +50,48 @@ #include #include #include -#include #include #include namespace boost { namespace intrusive { +/// @cond +namespace detail { + +template +struct splaydown_rollback +{ + typedef typename NodeTraits::node_ptr node_ptr; + splaydown_rollback( const node_ptr *pcur_subtree, node_ptr header + , node_ptr leftmost , node_ptr rightmost) + : pcur_subtree_(pcur_subtree) , header_(header) + , leftmost_(leftmost) , rightmost_(rightmost) + {} + + void release() + { pcur_subtree_ = 0; } + + ~splaydown_rollback() + { + if(pcur_subtree_){ + //Exception can only be thrown by comp, but + //tree invariants still hold. *pcur_subtree is the current root + //so link it to the header. + NodeTraits::set_parent(*pcur_subtree_, header_); + NodeTraits::set_parent(header_, *pcur_subtree_); + //Recover leftmost/rightmost pointers + NodeTraits::set_left (header_, leftmost_); + NodeTraits::set_right(header_, rightmost_); + } + } + const node_ptr *pcur_subtree_; + node_ptr header_, leftmost_, rightmost_; +}; + +} //namespace detail { +/// @endcond + //! A splay tree is an implementation of a binary search tree. The tree is //! self balancing using the splay algorithm as described in //! @@ -656,7 +691,8 @@ class splaytree_algorithms node_ptr leftmost = NodeTraits::get_left(header); node_ptr rightmost = NodeTraits::get_right(header); - try{ + { + detail::splaydown_rollback rollback(&t, header, leftmost, rightmost); node_ptr null = header; node_ptr l = null; node_ptr r = null; @@ -712,18 +748,9 @@ class splaytree_algorithms } assemble(t, l, r, null); + rollback.release(); } - catch(...){ - //Exception can only be thrown by comp, but - //tree invariants still hold. t is the current root - //so link it to the header. - NodeTraits::set_parent(t, header); - NodeTraits::set_parent(header, t); - //Recover leftmost/rightmost pointers - NodeTraits::set_left (header, leftmost); - NodeTraits::set_right(header, rightmost); - throw; - } + //t is the current root NodeTraits::set_parent(header, t); NodeTraits::set_parent(t, header); @@ -755,6 +782,17 @@ class splaytree_algorithms static node_ptr rebalance_subtree(node_ptr old_root) { return tree_algorithms::rebalance_subtree(old_root); } + + //! Requires: "n" must be a node inserted in a tree. + //! + //! Effects: Returns a pointer to the header node of the tree. + //! + //! Complexity: Logarithmic. + //! + //! Throws: Nothing. + static node_ptr get_header(node_ptr n) + { return tree_algorithms::get_header(n); } + private: /// @cond diff --git a/include/boost/intrusive/unordered_set.hpp b/include/boost/intrusive/unordered_set.hpp index 780fb46..d35eef4 100644 --- a/include/boost/intrusive/unordered_set.hpp +++ b/include/boost/intrusive/unordered_set.hpp @@ -38,7 +38,8 @@ namespace intrusive { //! //! The container supports the following options: //! \c base_hook<>/member_hook<>/value_traits<>, -//! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<> . +//! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<> +//! \c bucket_traits<>, power_2_buckets<> and cache_begin<>. //! //! unordered_set only provides forward iterators but it provides 4 iterator types: //! iterator and const_iterator to navigate through the whole container and @@ -167,8 +168,8 @@ class unordered_set_impl //! Effects: Returns an iterator pointing to the beginning of the unordered_set. //! - //! Complexity: Amortized constant time. - //! Worst case (empty unordered_set): O(this->bucket_count()) + //! Complexity: Constant time if `cache_begin<>` is true. Amortized + //! constant time with worst case (empty unordered_set) O(this->bucket_count()) //! //! Throws: Nothing. iterator begin() @@ -177,8 +178,8 @@ class unordered_set_impl //! Effects: Returns a const_iterator pointing to the beginning //! of the unordered_set. //! - //! Complexity: Amortized constant time. - //! Worst case (empty unordered_set): O(this->bucket_count()) + //! Complexity: Constant time if `cache_begin<>` is true. Amortized + //! constant time with worst case (empty unordered_set) O(this->bucket_count()) //! //! Throws: Nothing. const_iterator begin() const @@ -187,8 +188,8 @@ class unordered_set_impl //! Effects: Returns a const_iterator pointing to the beginning //! of the unordered_set. //! - //! Complexity: Amortized constant time. - //! Worst case (empty unordered_set): O(this->bucket_count()) + //! Complexity: Constant time if `cache_begin<>` is true. Amortized + //! constant time with worst case (empty unordered_set) O(this->bucket_count()) //! //! Throws: Nothing. const_iterator cbegin() const @@ -236,8 +237,8 @@ class unordered_set_impl //! Effects: Returns true is the container is empty. //! - //! Complexity: if constant-time size option is disabled, average constant time - //! (worst case, with empty() == true): O(this->bucket_count()). + //! Complexity: if constant-time size and cache_last options are disabled, + //! average constant time (worst case, with empty() == true: O(this->bucket_count()). //! Otherwise constant. //! //! Throws: Nothing. @@ -959,7 +960,7 @@ template template #endif struct make_unordered_set @@ -967,19 +968,19 @@ struct make_unordered_set /// @cond typedef unordered_set_impl < typename make_hashtable_opt - ::type + ::type > implementation_defined; /// @endcond typedef implementation_defined type; }; #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED -template +template class unordered_set - : public make_unordered_set::type + : public make_unordered_set::type { typedef typename make_unordered_set - ::type Base; + ::type Base; //Assert if passed value traits are compatible with the type BOOST_STATIC_ASSERT((detail::is_same::value)); @@ -1032,7 +1033,8 @@ class unordered_set //! //! The container supports the following options: //! \c base_hook<>/member_hook<>/value_traits<>, -//! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<> . +//! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<> +//! \c bucket_traits<>, power_2_buckets<> and cache_begin<>. //! //! unordered_multiset only provides forward iterators but it provides 4 iterator types: //! iterator and const_iterator to navigate through the whole container and @@ -1161,8 +1163,8 @@ class unordered_multiset_impl //! Effects: Returns an iterator pointing to the beginning of the unordered_multiset. //! - //! Complexity: Amortized constant time. - //! Worst case (empty unordered_multiset): O(this->bucket_count()) + //! Complexity: Constant time if `cache_begin<>` is true. Amortized + //! constant time with worst case (empty unordered_set) O(this->bucket_count()) //! //! Throws: Nothing. iterator begin() @@ -1171,8 +1173,8 @@ class unordered_multiset_impl //! Effects: Returns a const_iterator pointing to the beginning //! of the unordered_multiset. //! - //! Complexity: Amortized constant time. - //! Worst case (empty unordered_multiset): O(this->bucket_count()) + //! Complexity: Constant time if `cache_begin<>` is true. Amortized + //! constant time with worst case (empty unordered_set) O(this->bucket_count()) //! //! Throws: Nothing. const_iterator begin() const @@ -1181,8 +1183,8 @@ class unordered_multiset_impl //! Effects: Returns a const_iterator pointing to the beginning //! of the unordered_multiset. //! - //! Complexity: Amortized constant time. - //! Worst case (empty unordered_multiset): O(this->bucket_count()) + //! Complexity: Constant time if `cache_begin<>` is true. Amortized + //! constant time with worst case (empty unordered_set) O(this->bucket_count()) //! //! Throws: Nothing. const_iterator cbegin() const @@ -1230,8 +1232,8 @@ class unordered_multiset_impl //! Effects: Returns true is the container is empty. //! - //! Complexity: if constant-time size option is disabled, average constant time - //! (worst case, with empty() == true): O(this->bucket_count()). + //! Complexity: if constant-time size and cache_last options are disabled, + //! average constant time (worst case, with empty() == true: O(this->bucket_count()). //! Otherwise constant. //! //! Throws: Nothing. @@ -1891,7 +1893,7 @@ template template #endif struct make_unordered_multiset @@ -1899,19 +1901,19 @@ struct make_unordered_multiset /// @cond typedef unordered_multiset_impl < typename make_hashtable_opt - ::type + ::type > implementation_defined; /// @endcond typedef implementation_defined type; }; #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED -template +template class unordered_multiset - : public make_unordered_multiset::type + : public make_unordered_multiset::type { typedef typename make_unordered_multiset - ::type Base; + ::type Base; //Assert if passed value traits are compatible with the type BOOST_STATIC_ASSERT((detail::is_same::value)); diff --git a/include/boost/intrusive/unordered_set_hook.hpp b/include/boost/intrusive/unordered_set_hook.hpp index ffac564..4920534 100644 --- a/include/boost/intrusive/unordered_set_hook.hpp +++ b/include/boost/intrusive/unordered_set_hook.hpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -26,35 +27,69 @@ namespace intrusive { /// @cond -template -struct slist_node_plus_hash +template +struct unordered_node + : public slist_node { typedef typename boost::pointer_to_other - ::type node_ptr; - node_ptr next_; + < VoidPointer + , unordered_node + >::type node_ptr; +// node_ptr next_; + node_ptr prev_in_group_; std::size_t hash_; }; -// slist_node_traits can be used with circular_slist_algorithms and supplies -// a slist_node holding the pointers needed for a singly-linked list -// it is used by slist_base_hook and slist_member_hook template -struct slist_node_traits_plus_hash +struct unordered_node + : public slist_node { - typedef slist_node_plus_hash node; + typedef typename boost::pointer_to_other + < VoidPointer + , unordered_node + >::type node_ptr; +// node_ptr next_; + node_ptr prev_in_group_; +}; + +template +struct unordered_node + : public slist_node +{ + typedef typename boost::pointer_to_other + < VoidPointer + , unordered_node + >::type node_ptr; +// node_ptr next_; + std::size_t hash_; +}; + +template +struct unordered_node_traits + : public slist_node_traits +{ + typedef slist_node_traits reduced_slist_node_traits; + typedef unordered_node node; typedef typename boost::pointer_to_other ::type node_ptr; typedef typename boost::pointer_to_other ::type const_node_ptr; - static const bool store_hash = true; + static const bool store_hash = StoreHash; + static const bool optimize_multikey = OptimizeMultiKey; static node_ptr get_next(const_node_ptr n) - { return n->next_; } + { return node_ptr(&static_cast(*n->next_)); } static void set_next(node_ptr n, node_ptr next) { n->next_ = next; } + static node_ptr get_prev_in_group(const_node_ptr n) + { return n->prev_in_group_; } + + static void set_prev_in_group(node_ptr n, node_ptr prev) + { n->prev_in_group_ = prev; } + static std::size_t get_hash(const_node_ptr n) { return n->hash_; } @@ -62,15 +97,68 @@ struct slist_node_traits_plus_hash { n->hash_ = h; } }; -template +template +struct unordered_group_node_traits +{ + typedef Node node; + typedef typename boost::pointer_to_other + ::type node_ptr; + typedef typename boost::pointer_to_other + ::type const_node_ptr; + + static node_ptr get_next(const_node_ptr n) + { return n->prev_in_group_; } + + static void set_next(node_ptr n, node_ptr next) + { n->prev_in_group_ = next; } +}; + +template +struct unordered_algorithms + : public circular_slist_algorithms +{ + typedef circular_slist_algorithms base_type; + typedef unordered_group_node_traits + < typename boost::pointer_to_other + < typename NodeTraits::node_ptr + , void + >::type + , typename NodeTraits::node + > group_traits; + typedef circular_slist_algorithms group_algorithms; + + static void init(typename base_type::node_ptr n) + { + base_type::init(n); + group_algorithms::init(n); + } + + static void init_header(typename base_type::node_ptr n) + { + base_type::init_header(n); + group_algorithms::init_header(n); + } + + static void unlink(typename base_type::node_ptr n) + { + base_type::unlink(n); + group_algorithms::unlink(n); + } +}; + +template struct get_uset_node_algo { typedef typename detail::if_c - < StoreHash - , slist_node_traits_plus_hash + < (StoreHash || OptimizeMultiKey) + , unordered_node_traits , slist_node_traits >::type node_traits_type; - typedef circular_slist_algorithms type; + typedef typename detail::if_c + < OptimizeMultiKey + , unordered_algorithms + , circular_slist_algorithms + >::type type; }; /// @endcond @@ -90,6 +178,7 @@ struct make_unordered_set_base_hook typedef detail::generic_hook < get_uset_node_algo , typename packed_options::tag , packed_options::link_mode @@ -104,7 +193,7 @@ struct make_unordered_set_base_hook //! the unordered_set/unordered_multi_set and provides an appropriate value_traits class for unordered_set/unordered_multi_set. //! //! The hook admits the following options: \c tag<>, \c void_pointer<>, -//! \c link_mode<> and \c store_hash<>. +//! \c link_mode<>, \c store_hash<> and \c optimize_multikey<>. //! //! \c tag<> defines a tag to identify the node. //! The same tag value can be used in different classes, but if a class is @@ -119,6 +208,10 @@ struct make_unordered_set_base_hook //! //! \c store_hash<> will tell the hook to store the hash of the value //! to speed up rehashings. +//! +//! \c optimize_multikey<> will tell the hook to store a link to form a group +//! with other value with the same value to speed up searches and insertions +//! in unordered_multisets with a great number of with equivalent keys. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED template #else @@ -211,6 +304,7 @@ struct make_unordered_set_member_hook typedef detail::generic_hook < get_uset_node_algo< typename packed_options::void_pointer , packed_options::store_hash + , packed_options::optimize_multikey > , member_tag , packed_options::link_mode diff --git a/index.html b/index.html index 23984f2..8c018f9 100644 --- a/index.html +++ b/index.html @@ -4,6 +4,6 @@ Automatic redirection failed, please go to -../../doc/html/intrusive.html +../../doc/html/intrusive diff --git a/perf/perf_list.cpp b/perf/perf_list.cpp index 99f3b25..5bf8012 100644 --- a/perf/perf_list.cpp +++ b/perf/perf_list.cpp @@ -23,7 +23,7 @@ using namespace boost::posix_time; //[perf_list_value_type //Iteration and element count defines const int NumIter = 100; -const int NumElements = 100000; +const int NumElements = 50000; using namespace boost::intrusive; diff --git a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj index 587661c..6dd0c3f 100644 --- a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj +++ b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj @@ -154,10 +154,7 @@ RelativePath="..\..\..\..\..\boost\intrusive\options.hpp"> - - + RelativePath="..\..\..\..\..\boost\intrusive\pointer_plus_bits.hpp"> @@ -240,9 +237,6 @@ - - diff --git a/proj/vc7ide/to-do.txt b/proj/vc7ide/to-do.txt new file mode 100644 index 0000000..7e79f70 --- /dev/null +++ b/proj/vc7ide/to-do.txt @@ -0,0 +1,11 @@ +Add resize() to list +Implement incremental hashing +Add invariants to slist and test them after every operation +Create a generic hook that will work with all containers +Take advantage of store_hash in lookups: Instead of comparing objects, just + compare hashes. This will improve equality for expensive objects. + +Improve the use of cache_begin to unordered containers: +-> Speed up rehash + + diff --git a/test/avl_set_test.cpp b/test/avl_set_test.cpp index 1f7150e..d53b56e 100644 --- a/test/avl_set_test.cpp +++ b/test/avl_set_test.cpp @@ -108,7 +108,6 @@ class test_main_template int main( int, char* [] ) { - test_main_template()(); test_main_template, false>()(); test_main_template()(); diff --git a/test/generic_assoc_test.hpp b/test/generic_assoc_test.hpp index 624fd60..5ee4720 100644 --- a/test/generic_assoc_test.hpp +++ b/test/generic_assoc_test.hpp @@ -52,8 +52,37 @@ struct test_generic_assoc 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); + static void test_container_from_iterator(std::vector& values); }; +template class ContainerDefiner> +void test_generic_assoc:: + test_container_from_iterator(std::vector& values) +{ + typedef typename ContainerDefiner + < value_type + , value_traits + , constant_time_size + >::type assoc_type; + + assoc_type testset(values.begin(), values.end()); + typedef typename assoc_type::iterator it_type; + typedef typename assoc_type::const_iterator cit_type; + typedef typename assoc_type::size_type sz_type; + sz_type sz = testset.size(); + for(it_type b(testset.begin()), e(testset.end()); b != e; ++b) + { + assoc_type &s = assoc_type::container_from_iterator(b); + const assoc_type &cs = assoc_type::container_from_iterator(cit_type(b)); + BOOST_TEST(&s == &cs); + BOOST_TEST(&s == &testset); + s.erase(b); + BOOST_TEST(testset.size() == (sz-1)); + s.insert(*b); + BOOST_TEST(testset.size() == sz); + } +} + template class ContainerDefiner> void test_generic_assoc::test_insert_erase_burst() { @@ -103,12 +132,14 @@ void test_generic_assoc::test_insert_erase_burst( template class ContainerDefiner> void test_generic_assoc::test_all(std::vector& values) { + typedef typename ValueTraits::value_type value_type; test_clone(values); test_container_from_end(values); test_splay_up(values); test_splay_down(values); test_rebalance(values); test_insert_erase_burst(); + test_container_from_iterator(values); } template class ContainerDefiner> diff --git a/test/generic_set_test.hpp b/test/generic_set_test.hpp index db14008..0498759 100644 --- a/test/generic_set_test.hpp +++ b/test/generic_set_test.hpp @@ -35,6 +35,7 @@ struct test_generic_set static void test_impl(); }; + template class ContainerDefiner> void test_generic_set::test_all() { diff --git a/test/itestvalue.hpp b/test/itestvalue.hpp index d6338bb..b45bdd8 100644 --- a/test/itestvalue.hpp +++ b/test/itestvalue.hpp @@ -126,20 +126,30 @@ template struct uset_auto_base_hook_type { typedef unordered_set_base_hook - < link_mode, void_pointer - , tag, store_hash > type; + < link_mode + , void_pointer + , tag + , store_hash + > type; }; template struct uset_member_hook_type -{ typedef unordered_set_member_hook > type; }; +{ + typedef unordered_set_member_hook + < void_pointer + , optimize_multikey + > type; +}; template struct uset_auto_member_hook_type { typedef unordered_set_member_hook < link_mode, void_pointer - , store_hash > type; + , store_hash + , optimize_multikey + > type; }; template @@ -318,6 +328,49 @@ struct testvalue slist_auto_node_.swap_nodes(other.slist_auto_node_); } + bool is_linked() const + { + //Set + return set_base_hook_t::is_linked() || + set_auto_base_hook_t::is_linked() || + set_node_.is_linked() || + set_auto_node_.is_linked() || + + //SplaySet + splay_set_base_hook_t::is_linked() || + splay_set_auto_base_hook_t::is_linked() || + splay_set_node_.is_linked() || + splay_set_auto_node_.is_linked() || + + //ScapeoatSet + bs_set_base_hook_t::is_linked() || + sg_set_node_.is_linked() || + + //AvlSet + avl_set_base_hook_t::is_linked() || + avl_set_auto_base_hook_t::is_linked() || + avl_set_node_.is_linked() || + avl_set_auto_node_.is_linked() || + + //Unordered set + unordered_set_base_hook_t::is_linked() || + unordered_set_auto_base_hook_t::is_linked() || + unordered_set_node_.is_linked() || + unordered_set_auto_node_.is_linked() || + + //List + list_base_hook_t::is_linked() || + list_auto_base_hook_t::is_linked() || + list_node_.is_linked() || + list_auto_node_.is_linked() || + + //Slist + slist_base_hook_t::is_linked() || + slist_auto_base_hook_t::is_linked() || + slist_node_.is_linked() || + slist_auto_node_.is_linked(); + } + ~testvalue() {} diff --git a/test/smart_ptr.hpp b/test/smart_ptr.hpp index 6ed0d07..dd945a8 100644 --- a/test/smart_ptr.hpp +++ b/test/smart_ptr.hpp @@ -12,8 +12,7 @@ #define BOOST_INTRUSIVE_SMART_PTR_HPP #include -#include -#include +#include #if (defined _MSC_VER) && (_MSC_VER >= 1200) # pragma once @@ -109,7 +108,7 @@ class smart_ptr public: //Public Functions //!Constructor from raw pointer (allows "0" pointer conversion). Never throws. - smart_ptr(pointer ptr = 0) + explicit smart_ptr(pointer ptr = 0) : m_ptr(ptr) {} @@ -351,67 +350,34 @@ namespace boost{ //for intrusive containers, saving space namespace intrusive { -template -struct has_pointer_plus_bit, N> +template +struct max_pointer_plus_bits, Alignment> { - static const bool value = has_pointer_plus_bit::value; + static const std::size_t value = max_pointer_plus_bits::value; }; -//Specialization -template -struct pointer_plus_bit > +template +struct pointer_plus_bits, NumBits> { typedef smart_ptr pointer; static pointer get_pointer(const pointer &n) - { return pointer_plus_bit::get_pointer(n.get()); } + { return pointer_plus_bits::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()); + pointer_plus_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()); } + { return pointer_plus_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); + pointer_plus_bits::set_bits(raw_n, c); n = raw_n; } }; diff --git a/test/unordered_multiset_test.cpp b/test/unordered_multiset_test.cpp index ceb30ba..dee32be 100644 --- a/test/unordered_multiset_test.cpp +++ b/test/unordered_multiset_test.cpp @@ -18,6 +18,7 @@ #include "smart_ptr.hpp" #include "common_functors.hpp" #include +#include //std::sort std::find #include #include #include "test_macros.hpp" @@ -27,7 +28,7 @@ using namespace boost::intrusive; static const std::size_t BucketSize = 11; -template +template struct test_unordered_multiset { typedef typename ValueTraits::value_type value_type; @@ -41,14 +42,15 @@ struct test_unordered_multiset static void test_clone(std::vector& values); }; -template -void test_unordered_multiset::test_all (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 + , cache_begin > unordered_multiset_type; { typedef typename unordered_multiset_type::bucket_traits bucket_traits; @@ -76,14 +78,15 @@ void test_unordered_multiset::test_all (std::vector -void test_unordered_multiset::test_impl() +template +void test_unordered_multiset::test_impl() { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset , constant_time_size + , cache_begin > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; @@ -106,14 +109,15 @@ void test_unordered_multiset::test_impl() } //test: constructor, iterator, clear, reverse_iterator, front, back, size: -template -void test_unordered_multiset::test_sort(std::vector& values) +template +void test_unordered_multiset::test_sort(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset , constant_time_size + , cache_begin > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; @@ -127,104 +131,187 @@ void test_unordered_multiset::test_sort(std::vector -void test_unordered_multiset::test_insert(std::vector& values) +template +void test_unordered_multiset::test_insert(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset , constant_time_size + , cache_begin > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; + typedef typename unordered_multiset_type::iterator iterator; + { + typename unordered_multiset_type::bucket_type buckets [BucketSize]; + unordered_multiset_type testset(bucket_traits(buckets, BucketSize)); - typename unordered_multiset_type::bucket_type buckets [BucketSize]; - unordered_multiset_type testset(bucket_traits(buckets, BucketSize)); + testset.insert(&values[0] + 2, &values[0] + 5); - 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() ); } - 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); - 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); - 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, 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() ); } - { 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); - 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); + } + { + //Now erase just one per loop + const int random_init[] = { 3, 2, 4, 1, 5, 2, 2 }; + const unsigned int random_size = sizeof(random_init)/sizeof(random_init[0]); + typename unordered_multiset_type::bucket_type single_bucket[1]; + for(unsigned int i = 0, max = random_size; i != max; ++i){ + std::vector data (random_size); + for (unsigned int j = 0; j < random_size; ++j) + data[j].value_ = random_init[j]; + unordered_multiset_type testset_new(bucket_traits(single_bucket, 1)); + testset_new.insert(&data[0], &data[max]); + testset_new.erase(testset_new.iterator_to(data[i])); + BOOST_TEST (testset_new.size() == (max -1)); + } + } + { + typename unordered_multiset_type::bucket_type buckets [BucketSize]; + const unsigned int NumBucketSize = BucketSize; + const unsigned int LoadFactor = 3; + const unsigned int NumIterations = NumBucketSize*LoadFactor; + std::vector random_init(NumIterations);//Preserve memory + std::vector set_tester; + set_tester.reserve(NumIterations); - //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); + //Initialize values + for (unsigned int i = 0; i < NumIterations; ++i){ + random_init[i].value_ = i*2;//(i/LoadFactor)*LoadFactor; + } + + for(unsigned int initial_pos = 0; initial_pos != (NumIterations+1); ++initial_pos){ + for(unsigned int final_pos = initial_pos; final_pos != (NumIterations+1); ++final_pos){ + + //Create intrusive container inserting values + unordered_multiset_type testset + ( &random_init[0] + , &random_init[0] + random_init.size() + , bucket_traits(buckets, NumBucketSize)); + + BOOST_TEST (testset.size() == random_init.size()); + + //Obtain the iterator range to erase + iterator it_beg_pos = testset.begin(); + for(unsigned int it_beg_pos_num = 0; it_beg_pos_num != initial_pos; ++it_beg_pos_num){ + ++it_beg_pos; + } + iterator it_end_pos(it_beg_pos); + for(unsigned int it_end_pos_num = 0; it_end_pos_num != (final_pos - initial_pos); ++it_end_pos_num){ + ++it_end_pos; + } + + //Erase the same values in both the intrusive and original vector + std::size_t erased_cnt = std::distance(it_beg_pos, it_end_pos); + + //Erase values from the intrusive container + testset.erase(it_beg_pos, it_end_pos); + + BOOST_TEST (testset.size() == (random_init.size()-(final_pos - initial_pos))); + + //Now test... + BOOST_TEST ((random_init.size() - erased_cnt) == testset.size()); + + //Create an ordered copy of the intrusive container + set_tester.insert(set_tester.end(), testset.begin(), testset.end()); + std::sort(set_tester.begin(), set_tester.end()); + { + typename std::vector::iterator it = set_tester.begin(), itend = set_tester.end(); + typename std::vector::iterator random_init_it(random_init.begin()); + for( ; it != itend; ++it){ + while(!random_init_it->is_linked()) + ++random_init_it; + BOOST_TEST(*it == *random_init_it); + ++random_init_it; + } + } + set_tester.clear(); + } + } + } } //test: insert (seq-version), swap, erase (seq-version), size: -template -void test_unordered_multiset::test_swap(std::vector& values) +template +void test_unordered_multiset::test_swap(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset , constant_time_size + , cache_begin > 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)); + { + 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); + 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 [] = { 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]); + { 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) +template +void test_unordered_multiset::test_rehash(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset , constant_time_size + , cache_begin > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; @@ -261,14 +348,15 @@ void test_unordered_multiset::test_rehash(std::vector -void test_unordered_multiset::test_find(std::vector& values) +template +void test_unordered_multiset::test_find(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset , constant_time_size + , cache_begin > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; @@ -293,8 +381,8 @@ void test_unordered_multiset::test_find(std::vector -void test_unordered_multiset +template +void test_unordered_multiset ::test_clone(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -302,6 +390,7 @@ void test_unordered_multiset , constant_time_size + , cache_begin > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; { @@ -373,6 +462,7 @@ class test_main_template < value_type , typename value_type::unordered_set_base_hook_t >::type + , true >::test_all(data); test_unordered_multiset < typename detail::get_member_value_traits @@ -382,6 +472,7 @@ class test_main_template , &value_type::unordered_set_node_ > >::type + , false >::test_all(data); return 0; @@ -404,6 +495,7 @@ class test_main_template < value_type , typename value_type::unordered_set_base_hook_t >::type + , true >::test_all(data); test_unordered_multiset < typename detail::get_member_value_traits @@ -413,12 +505,14 @@ class test_main_template , &value_type::unordered_set_node_ > >::type + , false >::test_all(data); test_unordered_multiset < typename detail::get_base_value_traits < value_type , typename value_type::unordered_set_auto_base_hook_t >::type + , true >::test_all(data); test_unordered_multiset < typename detail::get_member_value_traits @@ -428,6 +522,7 @@ class test_main_template , &value_type::unordered_set_auto_node_ > >::type + , false >::test_all(data); return 0; } diff --git a/test/unordered_set_test.cpp b/test/unordered_set_test.cpp index 3eeccf6..6fcff90 100644 --- a/test/unordered_set_test.cpp +++ b/test/unordered_set_test.cpp @@ -26,7 +26,7 @@ using namespace boost::intrusive; static const std::size_t BucketSize = 11; -template +template struct test_unordered_set { typedef typename ValueTraits::value_type value_type; @@ -40,14 +40,15 @@ struct test_unordered_set static void test_clone(std::vector& values); }; -template -void test_unordered_set::test_all(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 + , cache_begin > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; { @@ -75,14 +76,15 @@ void test_unordered_set::test_all(std::vector -void test_unordered_set::test_impl() +template +void test_unordered_set::test_impl() { typedef typename ValueTraits::value_type value_type; typedef unordered_set , constant_time_size + , cache_begin > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -103,14 +105,15 @@ void test_unordered_set::test_impl() } //test: constructor, iterator, clear, reverse_iterator, front, back, size: -template -void test_unordered_set::test_sort(std::vector& values) +template +void test_unordered_set::test_sort(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set , constant_time_size + , cache_begin > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -126,14 +129,15 @@ void test_unordered_set::test_sort(std::vector -void test_unordered_set::test_insert(std::vector& values) +template +void test_unordered_set::test_insert(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set , constant_time_size + , cache_begin > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -161,14 +165,15 @@ void test_unordered_set::test_insert(std::vector -void test_unordered_set::test_swap(std::vector& values) +template +void test_unordered_set::test_swap(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set , constant_time_size + , cache_begin > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -192,14 +197,15 @@ void test_unordered_set::test_swap(std::vector -void test_unordered_set::test_rehash(std::vector& values) +template +void test_unordered_set::test_rehash(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set , constant_time_size + , cache_begin > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -237,14 +243,15 @@ void test_unordered_set::test_rehash(std::vector -void test_unordered_set::test_find(std::vector& values) +template +void test_unordered_set::test_find(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set , constant_time_size + , cache_begin > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -267,8 +274,8 @@ void test_unordered_set::test_find(std::vector -void test_unordered_set +template +void test_unordered_set ::test_clone(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -276,6 +283,7 @@ void test_unordered_set , constant_time_size + , cache_begin > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; { @@ -347,6 +355,7 @@ class test_main_template < value_type , typename value_type::unordered_set_base_hook_t >::type + , true >::test_all(data); test_unordered_set < typename detail::get_member_value_traits < value_type @@ -355,6 +364,7 @@ class test_main_template , &value_type::unordered_set_node_ > >::type + , false >::test_all(data); return 0; @@ -377,6 +387,7 @@ class test_main_template < value_type , typename value_type::unordered_set_base_hook_t >::type + , true >::test_all(data); test_unordered_set < typename detail::get_member_value_traits @@ -386,12 +397,14 @@ class test_main_template , &value_type::unordered_set_node_ > >::type + , false >::test_all(data); test_unordered_set < typename detail::get_base_value_traits < value_type , typename value_type::unordered_set_auto_base_hook_t >::type + , true >::test_all(data); test_unordered_set < typename detail::get_member_value_traits @@ -401,6 +414,7 @@ class test_main_template , &value_type::unordered_set_auto_node_ > >::type + , false >::test_all(data); return 0; } From 57e77dc3402d86ac50f783a40a53728ec846603a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Fri, 2 May 2008 11:07:08 +0000 Subject: [PATCH 10/20] Tickets #1883, #1862, #1709 [SVN r45019] --- test/unordered_multiset_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unordered_multiset_test.cpp b/test/unordered_multiset_test.cpp index dee32be..5b09933 100644 --- a/test/unordered_multiset_test.cpp +++ b/test/unordered_multiset_test.cpp @@ -199,7 +199,7 @@ void test_unordered_multiset::test_insert(std::vector Date: Mon, 12 May 2008 19:38:37 +0000 Subject: [PATCH 11/20] Replaced non-ascii characters, ticket 1736 [SVN r45306] --- doc/Jamfile.v2 | 2 +- doc/intrusive.qbk | 4 ++-- example/Jamfile.v2 | 2 +- perf/Jamfile.v2 | 2 +- test/Jamfile.v2 | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/Jamfile.v2 b/doc/Jamfile.v2 index 823d326..9cac616 100644 --- a/doc/Jamfile.v2 +++ b/doc/Jamfile.v2 @@ -1,6 +1,6 @@ # Boost.Intrusive library documentation Jamfile # -# Copyright Ion Gaztañaga 2006. +# 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) diff --git a/doc/intrusive.qbk b/doc/intrusive.qbk index bf8a604..4a4bb9e 100644 --- a/doc/intrusive.qbk +++ b/doc/intrusive.qbk @@ -3473,10 +3473,10 @@ helpful discussions. * [*Olaf Krzikalla] for the permission to continue his great work. -* [*Joaquín M. López Muñoz] for his thorough review, help, and ideas. +* [*Joaquin M. Lopez Munoz] 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], + [*Henri Bavestrello], [*Herve Bronnimann], [*Kai Bruning], [*Kevin Sopp], [*Paul Rose], [*Pavel Vozelinek], [*Howard Hinnant], [*Olaf Krzikalla], [*Samuel Debionne], [*Stjepan Rajko], [*Thorsten Ottosen], [*Tobias Schwinger], [*Tom Brinkman] and [*Steven Watanabe] diff --git a/example/Jamfile.v2 b/example/Jamfile.v2 index 75ef95a..cfd371d 100644 --- a/example/Jamfile.v2 +++ b/example/Jamfile.v2 @@ -1,6 +1,6 @@ # Boost Intrusive Library Example Jamfile -# (C) Copyright Ion Gaztañaga 2006-2007. +# (C) Copyright Ion Gaztanaga 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) diff --git a/perf/Jamfile.v2 b/perf/Jamfile.v2 index 3c9f3d6..bb6a612 100644 --- a/perf/Jamfile.v2 +++ b/perf/Jamfile.v2 @@ -1,6 +1,6 @@ # Boost Intrusive Library Performance test Jamfile -# (C) Copyright Ion Gaztañaga 2006-2007. +# (C) Copyright Ion Gaztanaga 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) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 58db1e5..d56b50a 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -1,5 +1,5 @@ # Boost Intrusive Library Test Jamfile -# (C) Copyright Ion Gaztañaga 2006. +# (C) Copyright Ion Gaztanaga 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) From 436b5048d6665d98b754f23b3e5b12ea5fc4cddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Mon, 12 May 2008 19:43:07 +0000 Subject: [PATCH 12/20] Replaced non-ascii characters, ticket 1736 [SVN r45307] --- include/boost/intrusive/detail/ebo_functor_holder.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/intrusive/detail/ebo_functor_holder.hpp b/include/boost/intrusive/detail/ebo_functor_holder.hpp index 696944f..369d2ca 100644 --- a/include/boost/intrusive/detail/ebo_functor_holder.hpp +++ b/include/boost/intrusive/detail/ebo_functor_holder.hpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Joaquín M López Muñoz 2006-2007 +// (C) Copyright Joaquin M Lopez Munoz 2006-2007 // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at From 377a4f8a0bee52eeac56efee0d72ee9feac3eaf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Mon, 19 May 2008 20:46:22 +0000 Subject: [PATCH 13/20] x's cache_last was not updated when splicing the whole container [SVN r45565] --- include/boost/intrusive/slist.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/boost/intrusive/slist.hpp b/include/boost/intrusive/slist.hpp index 5a264e4..3527bfe 100644 --- a/include/boost/intrusive/slist.hpp +++ b/include/boost/intrusive/slist.hpp @@ -1015,8 +1015,11 @@ class slist_impl iterator last_x(x.previous(x.end())); //<- constant time if cache_last is active node_ptr prev_n(prev.pointed_node()); node_ptr last_x_n(last_x.pointed_node()); - if(cache_last && node_traits::get_next(prev_n) == this->get_end_node()){ - this->set_last_node(last_x_n); + if(cache_last){ + x.set_last_node(x.get_root_node()); + if(node_traits::get_next(prev_n) == this->get_end_node()){ + this->set_last_node(last_x_n); + } } node_algorithms::transfer_after( prev_n, x.before_begin().pointed_node(), last_x_n); this->priv_size_traits().set_size(this->priv_size_traits().get_size() + x.priv_size_traits().get_size()); From 2b0b70b225822bbfe2484f3089f1d365cdaae138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Fri, 23 May 2008 22:13:34 +0000 Subject: [PATCH 14/20] #1912: some copy edits on boost.intrusive #1932: move semantics for shared objects #1635: Incomplete include guard in boost/intrusive [SVN r45693] --- doc/intrusive.qbk | 437 +++++++++++++++---------------- proj/vc7ide/to-do.txt | 1 + test/unordered_multiset_test.cpp | 1 - 3 files changed, 219 insertions(+), 220 deletions(-) diff --git a/doc/intrusive.qbk b/doc/intrusive.qbk index 4a4bb9e..cf1f5e6 100644 --- a/doc/intrusive.qbk +++ b/doc/intrusive.qbk @@ -141,7 +141,7 @@ A non-intrusive container has some limitations: * 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 + a size overhead for each allocation to store bookkeeping information and a synchronization to protected concurrent allocation from different threads. * Only copies of objects are stored in non-intrusive containers. Hence copy @@ -160,7 +160,7 @@ Intrusive containers have some important advantages: 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 + In some situations intrusive 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 @@ -168,19 +168,19 @@ Intrusive containers have some important advantages: 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 + memory management is done with intrusive containers. Memory management usually is not a predictable 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 + maintenance information needed by the container. Hence, whenever a certain type will 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 +* In intrusive containers you don't store a copy of an object, [*but 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 @@ -194,14 +194,14 @@ Intrusive containers have also downsides: 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 + containers don't have allocation capabilities, these operations make 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. +* Analyzing the thread safety of a program that uses containers is harder with intrusive containers, because + the container might be modified indirectly without an explicit call to a container member. [table Summay of intrusive containers advantages and disadvantages [[Issue] [Intrusive] [Non-intrusive]] @@ -233,8 +233,8 @@ For a performance comparison between Intrusive and Non-intrusive containers see 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 +container. We will take a simple intrusive container, the 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: @@ -312,7 +312,7 @@ and optionally, the user can specify options. We have 3 option types: [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...) + Some options configured for the hook (the type of the pointers, link mode, etc.) will be propagated to the container. * [*`constant_time_size`]: Specifies if a constant time `size()` @@ -326,7 +326,7 @@ and optionally, the user can specify options. We have 3 option types: 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). + (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`. @@ -428,8 +428,8 @@ Now we can use the container: [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: +You can insert the same object in several intrusive containers at the same time, +using one hook per container. This is a full example using base and member hooks: [import ../example/doc_how_to_use.cpp] [doc_how_to_use_code] @@ -456,7 +456,7 @@ stored object is not bound to or managed by the container: [section:usage_when When to use?] Intrusive containers can be used for highly optimized algorithms, where speed is a crucial -issue and... +issue and: * additional memory management should be avoided. * the programmer needs to efficiently track the construction and destruction of objects. @@ -467,24 +467,24 @@ issue and... * 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 +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: +from the list iterator (`std::list::iterator`) requires 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 +(a vector's memory is guaranteed to be contiguous), and form something +like a data block, list nodes may be dispersed in the heap memory. +Hence depending on your system you might get a lot of cache misses. The same doesn't hold +for an intrusive list. Indeed, dereferencing 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: +in the same container, as shown in the following example: [import ../example/doc_window.cpp] [doc_window_code] @@ -493,7 +493,7 @@ 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. +possible or desirable. [endsect] @@ -504,13 +504,13 @@ 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 + basic operations that can be applied to a group of nodes. It's independent + from the node definition and configured using 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.]] +[[Node Traits][A class that stores basic information and operations to insert a node into 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.]] +[[Semi-Intrusive Container][Similar to an intrusive container but a semi-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.]] ] @@ -528,7 +528,7 @@ chapters: small for user classes (usually the size of two pointers). Many operations have constant time complexity. -* [*set/multiset/rbtree]: A `std::set/std::multiset` like intrusive associative containers +* [*set/multiset/rbtree]: `std::set/std::multiset` like intrusive associative containers based on red-black trees. The size overhead is moderate for user classes (usually the size of three pointers). Many operations have logarithmic time complexity. @@ -538,7 +538,7 @@ chapters: The size overhead is moderate for user classes (usually the size of three pointers). Many operations have logarithmic time complexity. -* [*splay_set/splay_multiset/splaytree]: A `std::set/std::multiset` like intrusive associative +* [*splay_set/splay_multiset/splaytree]: `std::set/std::multiset` like intrusive associative containers based on splay trees. Splay trees have no constant operations, but they have some interesting caching properties. The size overhead is moderate for user classes (usually the size of three pointers). @@ -546,27 +546,27 @@ chapters: * [*sg_set/sg_multiset/sgtree]: A `std::set/std::multiset` like intrusive associative containers based on scapegoat trees. Scapegoat can be configured with the desired - balance factor to achieve the desised rebalancing frequency/search time compromise. + balance factor to achieve the desired rebalancing frequency/search time compromise. 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: +[*Boost.Intrusive] also offers semi-intrusive containers: -* [*unordered_set/unordered_multiset]: A `std::tr1::unordered_set/std::tr1::unordered_multiset` +* [*unordered_set/unordered_multiset]: `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. + Many operations have amortized constant time complexity. Most 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 +* [*Linear time size]: The intrusive container doesn't hold a size member that is +updated with every insertion/erasure. This implies that the `size()` function doesn't have 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 +`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 +* [*Constant time size]: The intrusive container holds a size member that is 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. @@ -585,15 +585,15 @@ Apart from that, [*Boost.Intrusive] offers additional features: 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. + and can convert some constant time operations to 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. + referring 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 + be configured to use any type of pointer. 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, @@ -611,19 +611,19 @@ The safe mode is activated by default, but it can be also explicitly activated: [c++] - //Configuring explicity the safe mode + //Configuring the safe mode explicitly 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: +With the safe mode the user can detect if the object +is actually inserted in a container without any external reference. Let's review the basic features of the safe mode: -* Hooks' constructor puts the hook in a well-known default state. +* Hook's 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, +* Hook's 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 +* Every time an object is inserted in the intrusive container, the container checks if the hook is in the well-known default state. If not, an assertion is raised. @@ -631,9 +631,9 @@ is actually inserted in a container. Let's review the basic features of the safe 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. +has been inserted in a container by 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 +in a container, the hook is in the default state, and if it is inserted in a container, the hook is not in the default state. [endsect] @@ -655,7 +655,7 @@ want to redefine intrusive safe-mode assertions without modifying the global * `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`. +If any of these macros is not redefined, the assertion will default to `BOOST_ASSERT`. [endsect] @@ -670,17 +670,17 @@ If any of these macros is not redefined, the assertion will be defaul to `BOOST_ * 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. + node from the container at any time, without having any reference to the container, + if the user wants to do so. -These hooks have exactly the same size overhead as their analogue non auto-unlinking +These hooks have exactly the same size overhead as their analog 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. + without referring to the container. This auto-unlink feature is useful in certain applications but it must be used [*very carefuly]: @@ -696,11 +696,11 @@ 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, +* Every time an object is 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 +* Every time an object is erased from an intrusive container, the container puts the erased object in the well-known default state. [endsect] @@ -763,9 +763,9 @@ in constant-time size containers. [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 +it 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 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. @@ -773,7 +773,7 @@ only provides forward iterators. For most cases, a doubly linked list is preferrable because it offers more constant-time functions with a slightly bigger size overhead. However, for some applications like -constructing more elaborated containers, singly linked lists are essential +constructing more elaborate containers, singly linked lists are essential because of their low size overhead. [section:slist_hooks slist hooks] @@ -831,8 +831,8 @@ 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]. + to configure the container. (To learn about value traits go to the section + [link intrusive.value_traits Containers with custom ValueTraits].) * [*`constant_time_size`]: To activate the constant-time `size()` operation. Default: `constant_time_size` @@ -872,10 +872,10 @@ Now let's see an small example using both hooks: [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] +it imposes is 2 pointers per node. An empty, non constant-time size [classref boost::intrusive::list list] +also has 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 +and provides a bidirectional iterator. It is recommended to use [classref boost::intrusive::list list] instead of [classref boost::intrusive::slist slist] if the size overhead is acceptable: @@ -933,8 +933,8 @@ 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]. + to configure the container. (To learn about value traits go to the section + [link intrusive.value_traits Containers with custom ValueTraits].) * [*`constant_time_size`]: To activate the constant-time `size()` operation. Default: `constant_time_size` @@ -974,7 +974,7 @@ An empty, non constant-time size [classref boost::intrusive::set set], 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 +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. @@ -1056,8 +1056,8 @@ These containers receive the same options explained in the section * [*`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]. + to configure the container. (To learn about value traits go to the section + [link intrusive.value_traits Containers with custom ValueTraits].) * [*`constant_time_size`]: To activate the constant-time `size()` operation. Default: `constant_time_size` @@ -1084,17 +1084,17 @@ Now let's see an small example using both hooks and both containers: [endsect] -[section:unordered_set_unordered_multiset Pseudo-Intrusive unordered associative containers: unordered_set, unordered_multiset] +[section:unordered_set_unordered_multiset Semi-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 +are semi-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 +the contents of these semi-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. @@ -1241,8 +1241,8 @@ receive the same options explained in the section * [*`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]. + to configure the container. (To learn about value traits go to the section + [link intrusive.value_traits Containers with custom ValueTraits].) * [*`constant_time_size`]: To activate the constant-time `size()` operation. Default: `constant_time_size` @@ -1324,9 +1324,9 @@ C++ associative containers are usually based on red-black tree implementations ( 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 +Splay trees are self-adjusting binary search trees used typically 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. +accessed elements have better access times than 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: @@ -1338,19 +1338,19 @@ For more information on splay trees see [@http://en.wikipedia.org/wiki/Splay_tre 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. +An empty, non constant-time size splay container has also a 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 +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 an element is searched. This disallows const versions of search functions like `find()`, `lower_bound()`, `upper_bound()`, `equal_range()`, -`count()`... +`count()`, etc. Because of this, splay-tree based associative containers are not drop-in replacements of [classref boost::intrusive::set set]/ @@ -1423,8 +1423,8 @@ These containers receive the same options explained in the section * [*`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]. + to configure the container. (To learn about value traits go to the section + [link intrusive.value_traits Containers with custom ValueTraits].) * [*`constant_time_size`]: To activate the constant-time `size()` operation. Default: `constant_time_size` @@ -1448,10 +1448,10 @@ Intrusive splay containers can also use plain binary search tree hooks 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 +[classref boost::intrusive::sg_multiset sg_multiset]. 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 +can be inserted in some situations in an splay container but +also inserted in other compatible containers when the hook is not being used in an splay container. [classref boost::intrusive::bs_set_base_hook bs_set_base_hook] and @@ -1479,7 +1479,7 @@ containers: 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. +and because both take O(log n) time for 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. @@ -1500,7 +1500,7 @@ This size can be reduced to 3 pointers if pointers have 4 byte alignment 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). +also has a 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] @@ -1571,8 +1571,8 @@ These containers receive the same options explained in the section * [*`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]. + to configure the container. (To learn about value traits go to the section + [link intrusive.value_traits Containers with custom ValueTraits].) * [*`constant_time_size`]: To activate the constant-time `size()` operation. Default: `constant_time_size` @@ -1722,8 +1722,8 @@ These containers receive the same options explained in the section * [*`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]. + to configure the container. (To learn about value traits go to the section + [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` @@ -1786,9 +1786,9 @@ containers. However, STL and TR1 ordered and unordered simple associative contai 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`... +like `equal_range`, `lower_bound`, `upper_bound`, etc. -However, sometimes the object to be searched it's quite expensive to construct: +However, sometimes the object to be searched is quite expensive to construct: [import ../example/doc_assoc_optimized_code.cpp] [doc_assoc_optimized_code_normal_find] @@ -1805,7 +1805,7 @@ 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 +[*have enough information to find the object]. In this case, a name is enough to search `Expensive` in the container but constructing an `Expensive` might require more information that the user might not have. @@ -1862,7 +1862,7 @@ will be discarded, and this is a waste of resources. Instead of that, let's use `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 +* if the insertion is possible (there is no equivalent value) `insert_check` 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] @@ -1871,7 +1871,7 @@ in an `insert_commit_data` structure, so that `insert_commit`: 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 +`insert_check` and `insert_commit` will come in 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 @@ -1917,9 +1917,9 @@ object that will be called after the element has been erased from the container. 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 +will call the "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`... +`remove_and_dispose`, etc. 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. @@ -1946,7 +1946,7 @@ function can offer more guarantees than the manual cloning (better exception saf 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 +Apart from the container to be cloned, `clone_from` takes two function objects as arguments. For example, consider the `clone_from` member function of [classref boost::intrusive::list list]: [c++] @@ -1965,8 +1965,8 @@ This function will make `*this` a clone of `src`. Let's explain the arguments: 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. +* First it clears and disposes all the elements from *this using the disposer function object. +* After that it 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. @@ -1998,7 +1998,7 @@ and [*Boost.Intrusive]: [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 +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. @@ -2008,7 +2008,7 @@ have the following features: 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 +ADL. For example, for `boost::interprocess::offset_ptr`, `detail::get_pointer` is defined as follows: [c++] @@ -2056,8 +2056,8 @@ 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 +explained in the [link intrusive.value_traits.stateful_value_traits Stateful value traits] section; +if 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 @@ -2075,11 +2075,11 @@ 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 + unlink a node from another group of nodes, etc. For example, a circular singly linked list is a group of nodes, where each node has a pointer to the next node. [*Node Algorithms] just require a [*NodeTraits] template parameter and they can work with any [*NodeTraits] class that fulfills - the needed interface. As an example, here is a class that implements algorithms + the needed interface. As an example, here is a class that implements operations7' to manage a group of nodes forming a circular singly linked list: [c++] @@ -2116,11 +2116,11 @@ before explaining the customization options of [*Boost.Intrusive]. }; * [*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. + operations on a node within a group of nodes: + the type of the node, a function to obtain the pointer to the next node, etc. + [*Node Traits] specify the configuration information [*Node Algorithms] + need. Each type of [*Node Algorithm] expects an interface that compatible + [*Node Traits] classes must implement. As an example, this is the definition of a [*Node Traits] class that is compatible with the previously presented `my_slist_algorithms`: @@ -2149,7 +2149,7 @@ before explaining the customization options of [*Boost.Intrusive]. * [*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: + 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: @@ -2189,9 +2189,9 @@ before explaining the customization options of [*Boost.Intrusive]. * [*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 + value types that use different hooks. An intrusive container is also more elaborate than a group of nodes: it can store the number of elements to achieve constant-time - size information, it can offer debugging facilities... + size information, it can offer debugging facilities, etc. 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 @@ -2223,7 +2223,7 @@ before explaining the customization options of [*Boost.Intrusive]. // ... }; -* [*Pseudo-Intrusive Container]: A pseudo-intrusive container is similar to an +* [*Semi-Intrusive Container]: A semi-intrusive container is similar to an intrusive container, but apart from the values to be inserted in the container, it needs additional memory (for example, auxiliary arrays or indexes). @@ -2383,7 +2383,7 @@ members of the [classref boost::intrusive::rbtree_algorithms 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. +the left and right node pointers point 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 @@ -2454,7 +2454,7 @@ members of the [classref boost::intrusive::splaytree_algorithms splaytree_algori 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 left and right nodes pointers point 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 @@ -2638,7 +2638,7 @@ or implicitly configured (using hooks and their `base_hook<>`/`member_hook<>` op `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. +`ValueTraits` also stores 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 @@ -2652,7 +2652,7 @@ achieve this using [*Boost.Intrusive] predefined hooks. Now, instead of using [section:value_traits_interface ValueTraits interface] -`ValueTraits` have the following interface: +`ValueTraits` has the following interface: [c++] @@ -2678,9 +2678,9 @@ achieve this using [*Boost.Intrusive] predefined hooks. Now, instead of using Let's explain each type and function: -* [*['node_traits]]: The node configuration that it's needed by node algorithms. +* [*['node_traits]]: The node configuration that is needed by node algorithms. These node traits and algorithms are - described in the previous chapter: [link intrusive.node_algorithms Nodes Algorithms]. + described in the previous chapter: [link intrusive.node_algorithms Node Algorithms]. * If my_value_traits is meant to be used with [classref boost::intrusive::slist slist], `node_traits` should follow @@ -2708,13 +2708,13 @@ Let's explain each type and function: 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 + 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 + 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 ``. @@ -2724,32 +2724,31 @@ Let's explain each type and function: These are the possible types: * [*`normal_link`]: If this linking policy is specified in a `ValueTraits` class - as the link, containers + as the link mode, 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 + * [*`safe_link`]: If this linking policy is specified as the link mode + in a `ValueTraits` class, containers + configured with this `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 + Containers also know that 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 + These functions 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 + These functions take a pointer to a node and return a pointer to the value that contains the node. [endsect] @@ -2761,7 +2760,7 @@ 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. +the object in both a singly and a doubly linked list at the same time. This is the definition of the old node: [import ../example/doc_value_traits.cpp] @@ -2773,7 +2772,7 @@ we'll define a ValueTraits class that will configure [*Boost.Intrusive] containe [doc_value_traits_value_traits] -Defining a value traits class that just defines `value_type` as +Defining a value traits class that simply 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 @@ -2794,7 +2793,7 @@ 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. +if the user does not want to use the provided [*Boost.Intrusive] facilities. [endsect] @@ -2814,7 +2813,7 @@ we just need a node that stores two pointers: [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: +a templatized `ValueTraits` that will work for both types: [doc_advanced_value_traits_value_traits] @@ -2825,7 +2824,7 @@ due to the fact that the value traits used to define the containers provide the [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 +all possible [classref boost::intrusive::list list] containers created with predefined hooks that define the same `VoidPointer` type share the same list algorithms. @@ -2833,7 +2832,7 @@ share the same list algorithms. [section:simplifying_value_traits Simplifying value traits definition] -The previous example can be further simplified using +The previous example can be further simplified using the [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: @@ -2869,8 +2868,8 @@ to define the needed value traits classes: 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 +so that we can separate nodes and values and [*avoid modifying types to insert nodes]. +[*Boost.Intrusive] differentiates between stateful and stateless value traits by checking if the ValueTraits class is empty: * If the class is empty, a [*stateless] value traits is assumed. @@ -2880,20 +2879,20 @@ class is empty: 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: +(stateless value traits could use global variables to achieve the same goal), 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. + value traits, since accessing global resources might require syncronization primitives that + can be avoided when using internal state. * [*Flexibility]: A stateful value traits type can be configured at run-time. -* [*Run-time polimorphism]: A value traits might implement node <-> value +* [*Run-time polymorphism]: 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]. + [*A heavy node <-> value transformation will hurt 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 @@ -2914,31 +2913,31 @@ in a list guaranteeing no additional allocation apart from the initial resource [section:thread_safety Thread safety guarantees] -Intrusive containers have similar same thread-safety guarantees than STL containers. +Intrusive containers have thread safety guarantees similar to STL containers. -* Several threads can have read or write access to different instances is safe as long as inserted +* Several threads having 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 +Other functions, like checking if an object is already inserted in a container using the `is_linked()` +member of safe hooks, constitute read access on the container without having a reference to it, 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 +the thread safety of [*Boost.Intrusive] is related to the containers and also to 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: +To analyze the thread safety, consider 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 +* The auto-unlink hook's destructor and `unlink()` functions modify the container indirectly. +* The safe mode and auto-unlink hooks' `is_linked()` functions are a read access to the container. +* Inserting an object in 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] @@ -2946,12 +2945,12 @@ To analyze the thread-safety, take in care the following points: [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: +has 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 +* If a user specifies the same options in different order or specifies some options and leaves 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 up with incompatible + types. For example, the following two lists, although using the same options, do not have the same type: [c++] @@ -2969,12 +2968,12 @@ has also a couple of downsides: * 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. + may suffer 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: +To solve these issues [*Boost.Intrusive] offers some helper metafunctions that reduce symbol lengths +and create the same type if the same options (either explicitly or implicitly) are used. These also +improve compilation times. All containers and hooks have their respective `make_xxx` versions. +The previously shown example can be rewritten like this to obtain the same list type: [c++] @@ -2992,8 +2991,8 @@ Previous shown example can be rewritten like this to obtain the same list type: //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. +Produced symbol lengths and compilation times will usually be shorter and object/debug files smaller. +If you are concerned with file sizes and compilation times, this option is your best choice. [endsect] @@ -3022,26 +3021,26 @@ 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. +[*Boost.Intrusive] seeks 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 +are two-byte aligned. The option to forgo constant-time size operations can +reduce container size, and this extra size optimization is noticeable when the container is empty or contains few values. [endsect] -[section: Boost.Intrusive as basic building block] +[section: Boost.Intrusive as a 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 +[*Boost.Intrusive] can be a basic building block to build more complex containers +and this potential has motivated many design decisions. For example, the ability +to have more than one hook per user type opens the opportunity 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 +as arguments (`clone_from`, `erase_and_dispose`, `insert_check`, etc.). These +functions come in handy when implementing non-intrusive containers (for example, STL-like containers) on top of intrusive containers. [endsect] @@ -3051,9 +3050,9 @@ functions come handy when implementing non-intrusive containers [*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. +build special hooks that take advantage of an application environment. -For example, the programmer can use can customize parts of [*Boost.Intrusive] +For example, the programmer can customize parts of [*Boost.Intrusive] to manage old data structures whose definition can't be changed. [endsect] @@ -3062,25 +3061,25 @@ to manage old data structures whose definition can't be changed. [section:performance Performance] -[*Boost.Intrusive] containers offer speed improvements comparing to non-intrusive containers, -basically because: +[*Boost.Intrusive] containers offer speed improvements compared to non-intrusive containers +primarily because: -* We can minimize memory allocation/deallocation calls. -* We obtain better memory locality. +* They minimize memory allocation/deallocation calls. +* They obtain better memory locality. -This section will show some performance tests comparing some operations on +This section will show 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 +* The `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. +* The `sort` and `write access` tests will show the advantage of intrusive containers + minimizing memory accesses compared to 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 +or it can replace `std::list` when the user wants 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: @@ -3112,7 +3111,7 @@ is just a class that has a base hook ([classref boost::intrusive::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. +`test_list` objects to function objects taking pointers to them. You can find the full test code code in the [@../../libs/intrusive/perf/perf_list.cpp perf_list.cpp] source file. @@ -3120,9 +3119,9 @@ You can find the full test code code in the [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 +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 +whereas `std::list` will need to allocate memory for each object 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: @@ -3180,29 +3179,29 @@ These are the times in microseconds for each case, and the normalized time: 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 +whereas `safe_link` and `auto_unlink` intrusive containers need to put the hooks 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, +Non-intrusive containers need to make many more allocations and that's why they +lag 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. +The 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 +a good memory allocator is the reason for these excellent 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 +Values (`test_class` and `itest_class`) and lists are created as 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 +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: @@ -3260,18 +3259,18 @@ in the code: 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 +For big objects the compact pointer list wins because the reversal test doesn't need access +to values stored in another container. Since all the allocations for nodes of +this pointer list are likely to be close (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. +containers. The dispersed pointer list, as 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 +The next test measures the time needed to complete calls to the member function +`sort(Pred pred)`. Values (`test_class` and `itest_class`) and lists are created as explained in the first section. The values will be sorted in ascending and descenting order each iteration. For example, if ['l] is a list: @@ -3298,7 +3297,7 @@ For a pointer list, the function object will be adapted using `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. +another container to compare two elements. These are the results: @@ -3335,16 +3334,16 @@ These are the results: 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. +indirection that is needed to access the value is minimized because all the values +are tightly stored, improving caching. The disperse list, on the other hand, is +slower because the indirection to access values stored in the object list is +more expensive than accessing 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 +The next test measures the time needed to iterate through all the elements of a list, and increment the value of the internal `i_` member: [c++] @@ -3353,7 +3352,7 @@ increment the value of the internal `i_` member: for(; it != end; ++it) ++(it->i_); -Values (`test_class` and `itest_class`) and lists are created like explained in +Values (`test_class` and `itest_class`) and lists are created as 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: @@ -3396,9 +3395,9 @@ These are the results: [[Standard disperse pointer list] [6118 / 12453] [2.67 / 1.62]] ] -Like with the read access test, the results show that intrusive containers outperform +As 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. +The disperse list is again the slowest. [endsect] @@ -3406,7 +3405,7 @@ The disperse list is again the slowest one. 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 +when the 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`. @@ -3431,7 +3430,7 @@ all the objects to be inserted in intrusive containers in containers like `std:: [section:tested_compilers Tested compilers] -[*Boost.Intrusive] has been tested in the following compilers/platforms: +[*Boost.Intrusive] has been tested on the following compilers/platforms: * Visual 7.1/WinXP * Visual 8.0/WinXP @@ -3448,7 +3447,7 @@ all the objects to be inserted in intrusive containers in containers like `std:: [section:references References] * SGI's [@http://www.sgi.com/tech/stl/ STL Programmer's Guide]. - [*Boost.Intrusive] is based on STL concepts and interface. + [*Boost.Intrusive] is based on STL concepts and interfaces. * 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. @@ -3476,13 +3475,13 @@ helpful discussions. * [*Joaquin M. Lopez Munoz] for his thorough review, help, and ideas. * [*Cory Nelson], [*Daniel James], [*Dave Harris], [*Guillaume Melquiond], - [*Henri Bavestrello], [*Herve Bronnimann], [*Kai Bruning], [*Kevin Sopp], + [*Henri Bavestrello], [*Hervé Bronnimann], [*Kai Bruning], [*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]) +* Thanks to [*Julienne Walker] and [*The EC Team] ([@http://eternallyconfuzzled.com]) for their great algorithms. * Thanks to [*Daniel K. O.] for his AVL tree rebalancing code. diff --git a/proj/vc7ide/to-do.txt b/proj/vc7ide/to-do.txt index 7e79f70..3428e61 100644 --- a/proj/vc7ide/to-do.txt +++ b/proj/vc7ide/to-do.txt @@ -8,4 +8,5 @@ Take advantage of store_hash in lookups: Instead of comparing objects, just Improve the use of cache_begin to unordered containers: -> Speed up rehash +Add erase(iterator, iterator, difference_type) to lists to obtain constant-time erase. diff --git a/test/unordered_multiset_test.cpp b/test/unordered_multiset_test.cpp index 5b09933..dda1ad3 100644 --- a/test/unordered_multiset_test.cpp +++ b/test/unordered_multiset_test.cpp @@ -10,7 +10,6 @@ // See http://www.boost.org/libs/intrusive for documentation. // ///////////////////////////////////////////////////////////////////////////// - #include #include #include From 0ec871e2024414ccffbc8f7340db2bf9c29f253c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Fri, 23 May 2008 22:32:55 +0000 Subject: [PATCH 15/20] #1912: some copy edits on boost.intrusive #1932: move semantics for shared objects #1635: Incomplete include guard in boost/intrusive [SVN r45696] --- include/boost/intrusive/detail/utilities.hpp | 6 +- include/boost/intrusive/hashtable.hpp | 2 +- include/boost/intrusive/list.hpp | 37 ++++++- include/boost/intrusive/slist.hpp | 102 +++++++++++++++---- include/boost/intrusive/unordered_set.hpp | 4 +- 5 files changed, 127 insertions(+), 24 deletions(-) diff --git a/include/boost/intrusive/detail/utilities.hpp b/include/boost/intrusive/detail/utilities.hpp index f190c92..19b6628 100644 --- a/include/boost/intrusive/detail/utilities.hpp +++ b/include/boost/intrusive/detail/utilities.hpp @@ -391,7 +391,11 @@ struct link_dispatch template void destructor_impl(Hook &hook, detail::link_dispatch) -{ (void)hook; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!hook.is_linked()); } +{ //If this assertion raises, you might have destroyed an object + //while it was still inserted in a container that is alive. + //If so, remove the object from the container before destroying it. + (void)hook; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!hook.is_linked()); +} template void destructor_impl(Hook &hook, detail::link_dispatch) diff --git a/include/boost/intrusive/hashtable.hpp b/include/boost/intrusive/hashtable.hpp index b621894..8ae0968 100644 --- a/include/boost/intrusive/hashtable.hpp +++ b/include/boost/intrusive/hashtable.hpp @@ -343,7 +343,7 @@ struct uset_defaults //! is used to construct intrusive unordered_set and unordered_multiset containers. The //! no-throw guarantee holds only, if the Equal object and Hasher don't throw. //! -//! hashtable is a pseudo-intrusive container: each object to be stored in the +//! hashtable is a semi-intrusive container: each object to be stored in the //! container must contain a proper hook, but the container also needs //! additional auxiliary memory to work: hashtable needs a pointer to an array //! of type `bucket_type` to be passed in the constructor. This bucket array must diff --git a/include/boost/intrusive/list.hpp b/include/boost/intrusive/list.hpp index ca7f7f6..2b28923 100644 --- a/include/boost/intrusive/list.hpp +++ b/include/boost/intrusive/list.hpp @@ -586,7 +586,7 @@ class list_impl iterator erase(iterator i) { return this->erase_and_dispose(i, detail::null_disposer()); } - //! Requires: first and last must be valid iterator to elements in *this. + //! Requires: b and e must be valid iterators to elements in *this. //! //! Effects: Erases the element range pointed by b and e //! No destructors are called. @@ -596,8 +596,8 @@ class list_impl //! //! Throws: Nothing. //! - //! Complexity: Linear to the number of elements erased if it's a safe-mode - //! or auto-unlink value. Constant time otherwise. + //! Complexity: Linear to the number of erased elements if it's a safe-mode + //! or auto-unlink value, or constant-time size is enabled. Constant-time otherwise. //! //! Note: Invalidates the iterators (but not the references) to the //! erased elements. @@ -612,6 +612,37 @@ class list_impl } } + //! Requires: b and e must be valid iterators to elements in *this. + //! n must be std::distance(b, e). + //! + //! Effects: Erases the element range pointed by b and e + //! No destructors are called. + //! + //! Returns: the first element remaining beyond the removed elements, + //! or end() if no such element exists. + //! + //! Throws: Nothing. + //! + //! Complexity: Linear to the number of erased elements if it's a safe-mode + //! or auto-unlink value is enabled. Constant-time otherwise. + //! + //! Note: Invalidates the iterators (but not the references) to the + //! erased elements. + iterator erase(iterator b, iterator e, difference_type n) + { + BOOST_INTRUSIVE_INVARIANT_ASSERT(std::distance(b, e) == difference_type(n)); + if(safemode_or_autounlink || constant_time_size){ + return this->erase_and_dispose(b, e, detail::null_disposer()); + } + else{ + if(constant_time_size){ + this->priv_size_traits().set_size(this->priv_size_traits().get_size() - n); + } + node_algorithms::unlink(b.pointed_node(), e.pointed_node()); + return e; + } + } + //! Requires: Disposer::operator()(pointer) shouldn't throw. //! //! Effects: Erases the element pointed by i of the list. diff --git a/include/boost/intrusive/slist.hpp b/include/boost/intrusive/slist.hpp index 3527bfe..14c614f 100644 --- a/include/boost/intrusive/slist.hpp +++ b/include/boost/intrusive/slist.hpp @@ -778,12 +778,64 @@ class slist_impl //! //! Throws: Nothing. //! - //! Complexity: Lineal to the elements (last - before_first + 1). + //! Complexity: Linear to the number of erased elements if it's a safe-mode + //! , auto-unlink value or constant-time size is activated. Constant time otherwise. //! //! Note: Invalidates the iterators (but not the references) to the //! erased element. iterator erase_after(iterator before_first, iterator last) - { return this->erase_after_and_dispose(before_first, last, detail::null_disposer()); } + { + if(safemode_or_autounlink || constant_time_size){ + return this->erase_after_and_dispose(before_first, last, detail::null_disposer()); + } + else{ + node_ptr bfp = before_first.pointed_node(); + node_ptr lp = last.pointed_node(); + if(cache_last){ + if((lp == this->get_end_node())){ + this->set_last_node(bfp); + } + } + node_algorithms::unlink_after(bfp, lp); + return last; + } + } + + //! Effects: Erases the range (before_first, last) from + //! the list. n must be std::distance(before_first, last) - 1. + //! No destructors are called. + //! + //! Returns: the first element remaining beyond the removed elements, + //! or end() if no such element exists. + //! + //! Throws: Nothing. + //! + //! Complexity: constant-time if link_mode is normal_link. + //! Linear to the elements (last - before_first) otherwise. + //! + //! Note: Invalidates the iterators (but not the references) to the + //! erased element. + iterator erase_after(iterator before_first, iterator last, difference_type n) + { + BOOST_INTRUSIVE_INVARIANT_ASSERT(std::distance(++iterator(before_first), last) == difference_type(n)); + if(safemode_or_autounlink){ + return this->erase_after(before_first, last); + } + else{ + node_ptr bfp = before_first.pointed_node(); + node_ptr lp = last.pointed_node(); + if(cache_last){ + if((lp == this->get_end_node())){ + this->set_last_node(bfp); + } + } + node_algorithms::unlink_after(bfp, lp); + if(constant_time_size){ + this->priv_size_traits().set_size(this->priv_size_traits().get_size() - n); + } + return last; + } + } //! Effects: Erases the element pointed by i of the list. //! No destructors are called. @@ -810,14 +862,30 @@ class slist_impl //! //! Throws: Nothing. //! - //! Complexity: Linear to the number of elements erased plus linear - //! to the elements before first. + //! Complexity: Linear to the elements before last. //! //! Note: Invalidates the iterators (but not the references) to the //! erased elements. iterator erase(iterator first, iterator last) { return this->erase_after(this->previous(first), last); } + //! Effects: Erases the range [first, last) from + //! the list. n must be std::distance(first, last). + //! No destructors are called. + //! + //! Returns: the first element remaining beyond the removed elements, + //! or end() if no such element exists. + //! + //! Throws: Nothing. + //! + //! Complexity: linear to the elements before first if link_mode is normal_link + //! and constant_time_size is activated. Linear to the elements before last otherwise. + //! + //! Note: Invalidates the iterators (but not the references) to the + //! erased element. + iterator erase(iterator first, iterator last, difference_type n) + { return this->erase_after(this->previous(first), last, n); } + //! Requires: Disposer::operator()(pointer) shouldn't throw. //! //! Effects: Erases the element after the element pointed by prev of @@ -885,7 +953,7 @@ class slist_impl //! //! Throws: Nothing. //! - //! Complexity: Lineal to the elements (last - before_first). + //! Complexity: Lineal to the elements (last - before_first + 1). //! //! Note: Invalidates the iterators to the erased element. template @@ -939,7 +1007,7 @@ class slist_impl //! //! Throws: Nothing. //! - //! Complexity: Linear to the number of elements erased plus linear + //! Complexity: Linear to the number of erased elements plus linear //! to the elements before first. //! //! Note: Invalidates the iterators (but not the references) to the @@ -1047,10 +1115,7 @@ class slist_impl void splice_after(iterator prev_pos, slist_impl &x, iterator prev_ele) { iterator elem = prev_ele; - ++elem; - if (elem != prev_pos && prev_ele != prev_pos){ - this->splice_after(prev_pos, x, prev_ele, elem, 1); - } + this->splice_after(prev_pos, x, prev_ele, ++elem, 1); } //! Requires: prev_pos must be a dereferenceable iterator in *this or be @@ -1588,15 +1653,18 @@ class slist_impl private: void priv_splice_after(node_ptr prev_pos_n, slist_impl &x, node_ptr before_first_n, node_ptr before_last_n) { - if(cache_last){ - if(node_traits::get_next(prev_pos_n) == this->get_end_node()){ - this->set_last_node(before_last_n); - } - if(node_traits::get_next(before_last_n) == x.get_end_node()){ - x.set_last_node(before_first_n); + if (before_first_n != before_last_n && prev_pos_n != before_first_n && prev_pos_n != before_last_n) + { + if(cache_last){ + if(node_traits::get_next(prev_pos_n) == this->get_end_node()){ + this->set_last_node(before_last_n); + } + if(node_traits::get_next(before_last_n) == x.get_end_node()){ + x.set_last_node(before_first_n); + } } + node_algorithms::transfer_after(prev_pos_n, before_first_n, before_last_n); } - node_algorithms::transfer_after(prev_pos_n, before_first_n, before_last_n); } void priv_reverse(detail::bool_) diff --git a/include/boost/intrusive/unordered_set.hpp b/include/boost/intrusive/unordered_set.hpp index d35eef4..342e2a6 100644 --- a/include/boost/intrusive/unordered_set.hpp +++ b/include/boost/intrusive/unordered_set.hpp @@ -24,7 +24,7 @@ namespace intrusive { //! The class template unordered_set is an intrusive container, that mimics most of //! the interface of std::tr1::unordered_set as described in the C++ TR1. //! -//! unordered_set is a pseudo-intrusive container: each object to be stored in the +//! unordered_set is a semi-intrusive container: each object to be stored in the //! container must contain a proper hook, but the container also needs //! additional auxiliary memory to work: unordered_set needs a pointer to an array //! of type `bucket_type` to be passed in the constructor. This bucket array must @@ -1019,7 +1019,7 @@ class unordered_set //! The class template unordered_multiset is an intrusive container, that mimics most of //! the interface of std::tr1::unordered_multiset as described in the C++ TR1. //! -//! unordered_multiset is a pseudo-intrusive container: each object to be stored in the +//! unordered_multiset is a semi-intrusive container: each object to be stored in the //! container must contain a proper hook, but the container also needs //! additional auxiliary memory to work: unordered_multiset needs a pointer to an array //! of type `bucket_type` to be passed in the constructor. This bucket array must From 6ba7360fbaca132603c1a302b116e32a5f544120 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 17 Jun 2008 13:54:46 +0000 Subject: [PATCH 16/20] Add mime-type and eol-style properties as needed [SVN r46445] --- proj/vc7ide/Intrusive.sln | 350 +++++++++++++++++++------------------- 1 file changed, 175 insertions(+), 175 deletions(-) diff --git a/proj/vc7ide/Intrusive.sln b/proj/vc7ide/Intrusive.sln index 8767b1e..4fb0638 100644 --- a/proj/vc7ide/Intrusive.sln +++ b/proj/vc7ide/Intrusive.sln @@ -1,175 +1,175 @@ -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 +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 From 1c6d460167163cbccde14c41fe94561175baefb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Thu, 19 Jun 2008 14:29:52 +0000 Subject: [PATCH 17/20] #2019: Intrusive lists seem to return invalid references with member hooks in 64 bit mode on calling front/back [SVN r46511] --- .../boost/intrusive/detail/parent_from_member.hpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/include/boost/intrusive/detail/parent_from_member.hpp b/include/boost/intrusive/detail/parent_from_member.hpp index c566e77..c3becf1 100644 --- a/include/boost/intrusive/detail/parent_from_member.hpp +++ b/include/boost/intrusive/detail/parent_from_member.hpp @@ -16,6 +16,11 @@ #include #include +#if defined(BOOST_MSVC) || (defined (BOOST_WINDOWS) && defined(BOOST_INTEL)) +#define BOOST_INTRUSIVE_OFFSET_FROM_PTR2MEMBER_MSVC_COMPLIANT +#include +#endif + namespace boost { namespace intrusive { namespace detail { @@ -23,11 +28,10 @@ namespace detail { template inline std::size_t offset_from_pointer_to_member(const Member Parent::* ptr_to_member) { - //BOOST_STATIC_ASSERT(( sizeof(std::ptrdiff_t) == sizeof(ptr_to_member) )); //The implementation of a pointer to member is compiler dependent. - #if defined(BOOST_MSVC) || (defined (BOOST_WINDOWS) && defined(BOOST_INTEL)) + #if defined(BOOST_INTRUSIVE_OFFSET_FROM_PTR2MEMBER_MSVC_COMPLIANT) //This works with gcc, msvc, ac++, ibmcpp - return *(const std::ptrdiff_t*)(void*)&ptr_to_member; + return *(const boost::int32_t*)(void*)&ptr_to_member; #elif defined(__GNUC__) || defined(__HP_aCC) || defined(BOOST_INTEL) || defined (__IBMCPP__) || defined (__DECCXX) const Parent * const parent = 0; const char *const member = reinterpret_cast(&(parent->*ptr_to_member)); @@ -56,6 +60,10 @@ inline const Parent *parent_from_member(const Member *member, const Member Paren } //namespace intrusive { } //namespace boost { +#ifdef BOOST_INTRUSIVE_OFFSET_FROM_PTR2MEMBER_MSVC_COMPLIANT +#undef BOOST_INTRUSIVE_OFFSET_FROM_PTR2MEMBER_MSVC_COMPLIANT +#endif + #include #endif //#ifndef BOOST_INTRUSIVE_PARENT_FROM_MEMBER_HPP From a60ea9d4742cad6daafad9a041c72f1f212f3d70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Sat, 21 Jun 2008 09:04:21 +0000 Subject: [PATCH 18/20] gcc 4.3 fixes for normal and -std=c++0x modes [SVN r46571] --- include/boost/intrusive/any_hook.hpp | 320 ++++++++++++++++++ .../boost/intrusive/avltree_algorithms.hpp | 3 +- .../intrusive/circular_list_algorithms.hpp | 1 + .../intrusive/circular_slist_algorithms.hpp | 1 + .../detail/any_node_and_algorithms.hpp | 292 ++++++++++++++++ .../detail/common_slist_algorithms.hpp | 3 +- .../boost/intrusive/detail/generic_hook.hpp | 53 +-- .../boost/intrusive/detail/hashtable_node.hpp | 12 +- include/boost/intrusive/detail/list_node.hpp | 1 - .../intrusive/detail/parent_from_member.hpp | 21 +- include/boost/intrusive/detail/slist_node.hpp | 1 - .../intrusive/detail/tree_algorithms.hpp | 7 +- include/boost/intrusive/detail/utilities.hpp | 19 ++ include/boost/intrusive/hashtable.hpp | 199 +++++++---- include/boost/intrusive/intrusive_fwd.hpp | 17 + .../intrusive/linear_slist_algorithms.hpp | 1 + include/boost/intrusive/options.hpp | 94 +++-- include/boost/intrusive/rbtree_algorithms.hpp | 2 +- include/boost/intrusive/sgtree_algorithms.hpp | 2 +- .../boost/intrusive/splaytree_algorithms.hpp | 2 +- include/boost/intrusive/unordered_set.hpp | 18 +- .../boost/intrusive/unordered_set_hook.hpp | 29 +- 22 files changed, 923 insertions(+), 175 deletions(-) create mode 100644 include/boost/intrusive/any_hook.hpp create mode 100644 include/boost/intrusive/detail/any_node_and_algorithms.hpp diff --git a/include/boost/intrusive/any_hook.hpp b/include/boost/intrusive/any_hook.hpp new file mode 100644 index 0000000..148d41d --- /dev/null +++ b/include/boost/intrusive/any_hook.hpp @@ -0,0 +1,320 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2008 +// +// 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_ANY_HOOK_HPP +#define BOOST_INTRUSIVE_ANY_HOOK_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +/// @cond +template +struct get_any_node_algo +{ + typedef any_algorithms type; +}; +/// @endcond + +//! Helper metafunction to define a \c \c any_base_hook that yields to the same +//! type when the same options (either explicitly or implicitly) are used. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +struct make_any_base_hook +{ + /// @cond + typedef typename pack_options + < hook_defaults, O1, O2, O3>::type packed_options; + + typedef detail::generic_hook + < get_any_node_algo + , typename packed_options::tag + , packed_options::link_mode + , detail::AnyBaseHook + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +//! Derive a class from this hook in order to store objects of that class +//! in an intrusive container. +//! +//! The hook admits the following options: \c tag<>, \c void_pointer<> and +//! \c link_mode<>. +//! +//! \c tag<> defines a tag to identify the node. +//! The same tag value can be used in different classes, but if a class is +//! derived from more than one \c any_base_hook, then each \c any_base_hook needs its +//! unique tag. +//! +//! \c link_mode<> will specify the linking mode of the hook (\c normal_link, \c safe_link). +//! +//! \c void_pointer<> is the pointer type that will be used internally in the hook +//! and the the container configured to use this hook. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +class any_base_hook + : public make_any_base_hook::type +{ + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + public: + //! Effects: If link_mode is or \c safe_link + //! initializes the node to an unlinked state. + //! + //! Throws: Nothing. + any_base_hook(); + + //! Effects: If link_mode is or \c safe_link + //! initializes the node to an unlinked state. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing a copy-constructor + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + any_base_hook(const any_base_hook& ); + + //! Effects: Empty function. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing an assignment operator + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + any_base_hook& operator=(const any_base_hook& ); + + //! Effects: If link_mode is \c normal_link, the destructor does + //! nothing (ie. no code is generated). If link_mode is \c safe_link and the + //! object is stored in a container an assertion is raised. + //! + //! Throws: Nothing. + ~any_base_hook(); + + //! Precondition: link_mode must be \c safe_link. + //! + //! Returns: true, if the node belongs to a container, false + //! otherwise. This function can be used to test whether \c container::iterator_to + //! will return a valid iterator. + //! + //! Complexity: Constant + bool is_linked() const; + #endif +}; + +//! Helper metafunction to define a \c \c any_member_hook that yields to the same +//! type when the same options (either explicitly or implicitly) are used. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +struct make_any_member_hook +{ + /// @cond + typedef typename pack_options + < hook_defaults, O1, O2, O3>::type packed_options; + + typedef detail::generic_hook + < get_any_node_algo + , member_tag + , packed_options::link_mode + , detail::NoBaseHook + > implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + +//! Store this hook in a class to be inserted +//! in an intrusive container. +//! +//! The hook admits the following options: \c void_pointer<> and +//! \c link_mode<>. +//! +//! \c link_mode<> will specify the linking mode of the hook (\c normal_link or \c safe_link). +//! +//! \c void_pointer<> is the pointer type that will be used internally in the hook +//! and the the container configured to use this hook. +#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED +template +#else +template +#endif +class any_member_hook + : public make_any_member_hook::type +{ + #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED + public: + //! Effects: If link_mode is or \c safe_link + //! initializes the node to an unlinked state. + //! + //! Throws: Nothing. + any_member_hook(); + + //! Effects: If link_mode is or \c safe_link + //! initializes the node to an unlinked state. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing a copy-constructor + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + any_member_hook(const any_member_hook& ); + + //! Effects: Empty function. The argument is ignored. + //! + //! Throws: Nothing. + //! + //! Rationale: Providing an assignment operator + //! makes classes using the hook STL-compliant without forcing the + //! user to do some additional work. \c swap can be used to emulate + //! move-semantics. + any_member_hook& operator=(const any_member_hook& ); + + //! Effects: If link_mode is \c normal_link, the destructor does + //! nothing (ie. no code is generated). If link_mode is \c safe_link and the + //! object is stored in a container an assertion is raised. + //! + //! Throws: Nothing. + ~any_member_hook(); + + //! Precondition: link_mode must be \c safe_link. + //! + //! Returns: true, if the node belongs to a container, false + //! otherwise. This function can be used to test whether \c container::iterator_to + //! will return a valid iterator. + //! + //! Complexity: Constant + bool is_linked() const; + #endif +}; + +/// @cond + +namespace detail{ + +template +struct any_to_get_base_pointer_type +{ + typedef typename pointer_to_other + ::type type; +}; + +template +struct any_to_get_member_pointer_type +{ + typedef typename pointer_to_other + ::type type; +}; + +//!This option setter specifies that the container +//!must use the specified base hook +template class NodeTraits> +struct any_to_some_hook +{ + typedef typename BaseHook::template pack::value_traits old_value_traits; + template + struct pack : public Base + { + struct value_traits : public old_value_traits + { + static const bool is_any_hook = true; + typedef typename detail::eval_if_c + < detail::internal_base_hook_bool_is_true::value + , any_to_get_base_pointer_type + , any_to_get_member_pointer_type + >::type void_pointer; + typedef NodeTraits node_traits; + }; + }; +}; + +} //namespace detail{ + +/// @endcond + +//!This option setter specifies that +//!any hook should behave as an slist hook +template +struct any_to_slist_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + +//!This option setter specifies that +//!any hook should behave as an list hook +template +struct any_to_list_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + +//!This option setter specifies that +//!any hook should behave as a set hook +template +struct any_to_set_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + +//!This option setter specifies that +//!any hook should behave as a set hook +template +struct any_to_avl_set_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + +//!This option setter specifies that any +//!hook should behave as a set hook +template +struct any_to_bs_set_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + +//!This option setter specifies that any hook +//!should behave as an unordered set hook +template +struct any_to_unordered_set_hook +/// @cond + : public detail::any_to_some_hook +/// @endcond +{}; + + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_ANY_HOOK_HPP diff --git a/include/boost/intrusive/avltree_algorithms.hpp b/include/boost/intrusive/avltree_algorithms.hpp index 145e0fb..dc4eed4 100644 --- a/include/boost/intrusive/avltree_algorithms.hpp +++ b/include/boost/intrusive/avltree_algorithms.hpp @@ -68,6 +68,7 @@ template class avltree_algorithms { public: + typedef typename NodeTraits::node node; typedef NodeTraits node_traits; typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; @@ -75,8 +76,6 @@ class avltree_algorithms /// @cond private: - - typedef typename NodeTraits::node node; typedef detail::tree_algorithms tree_algorithms; template diff --git a/include/boost/intrusive/circular_list_algorithms.hpp b/include/boost/intrusive/circular_list_algorithms.hpp index 537b151..d48fffc 100644 --- a/include/boost/intrusive/circular_list_algorithms.hpp +++ b/include/boost/intrusive/circular_list_algorithms.hpp @@ -50,6 +50,7 @@ template class circular_list_algorithms { public: + typedef typename NodeTraits::node node; typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; typedef NodeTraits node_traits; diff --git a/include/boost/intrusive/circular_slist_algorithms.hpp b/include/boost/intrusive/circular_slist_algorithms.hpp index 2a257e4..f74a641 100644 --- a/include/boost/intrusive/circular_slist_algorithms.hpp +++ b/include/boost/intrusive/circular_slist_algorithms.hpp @@ -54,6 +54,7 @@ class circular_slist_algorithms typedef detail::common_slist_algorithms base_t; /// @endcond public: + typedef typename NodeTraits::node node; typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; typedef NodeTraits node_traits; diff --git a/include/boost/intrusive/detail/any_node_and_algorithms.hpp b/include/boost/intrusive/detail/any_node_and_algorithms.hpp new file mode 100644 index 0000000..6b75f57 --- /dev/null +++ b/include/boost/intrusive/detail/any_node_and_algorithms.hpp @@ -0,0 +1,292 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2008 +// +// 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_ANY_NODE_HPP +#define BOOST_INTRUSIVE_ANY_NODE_HPP + +#include +#include +#include +#include +#include + +namespace boost { +namespace intrusive { + +template +struct any_node +{ + typedef typename boost::pointer_to_other + ::type node_ptr; + node_ptr node_ptr_1; + node_ptr node_ptr_2; + node_ptr node_ptr_3; + std::size_t size_t_1; +}; + +template +struct any_list_node_traits +{ + typedef any_node node; + typedef typename boost::pointer_to_other + ::type node_ptr; + typedef typename boost::pointer_to_other + ::type const_node_ptr; + + static node_ptr get_next(const_node_ptr n) + { return n->node_ptr_1; } + + static void set_next(node_ptr n, node_ptr next) + { n->node_ptr_1 = next; } + + static node_ptr get_previous(const_node_ptr n) + { return n->node_ptr_2; } + + static void set_previous(node_ptr n, node_ptr prev) + { n->node_ptr_2 = prev; } +}; + + +template +struct any_slist_node_traits +{ + typedef any_node node; + typedef typename boost::pointer_to_other + ::type node_ptr; + typedef typename boost::pointer_to_other + ::type const_node_ptr; + + static node_ptr get_next(const_node_ptr n) + { return n->node_ptr_1; } + + static void set_next(node_ptr n, node_ptr next) + { n->node_ptr_1 = next; } +}; + + +template +struct any_unordered_node_traits + : public any_slist_node_traits +{ + typedef any_slist_node_traits reduced_slist_node_traits; + typedef typename reduced_slist_node_traits::node node; + typedef typename reduced_slist_node_traits::node_ptr node_ptr; + typedef typename reduced_slist_node_traits::const_node_ptr const_node_ptr; + + static const bool store_hash = true; + static const bool optimize_multikey = true; + + static node_ptr get_next(const_node_ptr n) + { return node_ptr(&static_cast(*n->node_ptr_1)); } + + static void set_next(node_ptr n, node_ptr next) + { n->node_ptr_1 = next; } + + static node_ptr get_prev_in_group(const_node_ptr n) + { return n->node_ptr_2; } + + static void set_prev_in_group(node_ptr n, node_ptr prev) + { n->node_ptr_2 = prev; } + + static std::size_t get_hash(const_node_ptr n) + { return n->size_t_1; } + + static void set_hash(node_ptr n, std::size_t h) + { n->size_t_1 = h; } +}; + + +template +struct any_rbtree_node_traits +{ + typedef any_node node; + + typedef typename boost::pointer_to_other + ::type node_ptr; + typedef typename boost::pointer_to_other + ::type const_node_ptr; + + typedef std::size_t color; + + static node_ptr get_parent(const_node_ptr n) + { return n->node_ptr_1; } + + static void set_parent(node_ptr n, node_ptr p) + { n->node_ptr_1 = p; } + + static node_ptr get_left(const_node_ptr n) + { return n->node_ptr_2; } + + static void set_left(node_ptr n, node_ptr l) + { n->node_ptr_2 = l; } + + static node_ptr get_right(const_node_ptr n) + { return n->node_ptr_3; } + + static void set_right(node_ptr n, node_ptr r) + { n->node_ptr_3 = r; } + + static color get_color(const_node_ptr n) + { return n->size_t_1; } + + static void set_color(node_ptr n, color c) + { n->size_t_1 = c; } + + static color black() + { return 0u; } + + static color red() + { return 1u; } +}; + + +template +struct any_avltree_node_traits +{ + typedef any_node node; + + typedef typename boost::pointer_to_other + ::type node_ptr; + typedef typename boost::pointer_to_other + ::type const_node_ptr; + typedef std::size_t balance; + + static node_ptr get_parent(const_node_ptr n) + { return n->node_ptr_1; } + + static void set_parent(node_ptr n, node_ptr p) + { n->node_ptr_1 = p; } + + static node_ptr get_left(const_node_ptr n) + { return n->node_ptr_2; } + + static void set_left(node_ptr n, node_ptr l) + { n->node_ptr_2 = l; } + + static node_ptr get_right(const_node_ptr n) + { return n->node_ptr_3; } + + static void set_right(node_ptr n, node_ptr r) + { n->node_ptr_3 = r; } + + static balance get_balance(const_node_ptr n) + { return n->size_t_1; } + + static void set_balance(node_ptr n, balance b) + { n->size_t_1 = b; } + + static balance negative() + { return 0u; } + + static balance zero() + { return 1u; } + + static balance positive() + { return 2u; } +}; + + +template +struct any_tree_node_traits +{ + typedef any_node node; + + typedef typename boost::pointer_to_other + ::type node_ptr; + typedef typename boost::pointer_to_other + ::type const_node_ptr; + + static node_ptr get_parent(const_node_ptr n) + { return n->node_ptr_1; } + + static void set_parent(node_ptr n, node_ptr p) + { n->node_ptr_1 = p; } + + static node_ptr get_left(const_node_ptr n) + { return n->node_ptr_2; } + + static void set_left(node_ptr n, node_ptr l) + { n->node_ptr_2 = l; } + + static node_ptr get_right(const_node_ptr n) + { return n->node_ptr_3; } + + static void set_right(node_ptr n, node_ptr r) + { n->node_ptr_3 = r; } +}; + +template +class any_node_traits +{ + public: + typedef any_node node; + typedef typename boost::pointer_to_other + ::type node_ptr; + typedef typename boost::pointer_to_other + ::type const_node_ptr; +}; + +template +class any_algorithms +{ + public: + typedef any_node node; + typedef typename boost::pointer_to_other + ::type node_ptr; + typedef typename boost::pointer_to_other + ::type const_node_ptr; + typedef any_node_traits node_traits; + + //! Requires: node must not be part of any tree. + //! + //! Effects: After the function unique(node) == true. + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + //! + //! Nodes: If node is inserted in a tree, this function corrupts the tree. + static void init(node_ptr node) + { node->node_ptr_1 = 0; }; + + //! Effects: Returns true if node is in the same state as if called init(node) + //! + //! Complexity: Constant. + //! + //! Throws: Nothing. + static bool inited(const_node_ptr node) + { return !node->node_ptr_1; }; + + static bool unique(const_node_ptr node) + { return 0 == node->node_ptr_1; } + + static void unlink(node_ptr) + { + //Auto-unlink hooks and unlink() call for safe hooks are not + //available for any hooks!!! + any_algorithms::unlink_not_available_for_any_hooks(); + } + + static void swap_nodes(node_ptr l, node_ptr r) + { + //Any nodes have no swap_nodes capability because they don't know + //what algorithm they must use from unlink them from the container + any_algorithms::swap_nodes_not_available_for_any_hooks(); + } +}; + +} //namespace intrusive +} //namespace boost + +#include + +#endif //BOOST_INTRUSIVE_ANY_NODE_HPP diff --git a/include/boost/intrusive/detail/common_slist_algorithms.hpp b/include/boost/intrusive/detail/common_slist_algorithms.hpp index cc37a74..ada51a9 100644 --- a/include/boost/intrusive/detail/common_slist_algorithms.hpp +++ b/include/boost/intrusive/detail/common_slist_algorithms.hpp @@ -26,6 +26,7 @@ template class common_slist_algorithms { public: + typedef typename NodeTraits::node node; typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; typedef NodeTraits node_traits; @@ -53,7 +54,7 @@ class common_slist_algorithms { node_ptr next = NodeTraits::get_next(this_node); return !next || next == this_node; - } + } static bool inited(const_node_ptr this_node) { return !NodeTraits::get_next(this_node); } diff --git a/include/boost/intrusive/detail/generic_hook.hpp b/include/boost/intrusive/detail/generic_hook.hpp index 87dafe2..837f928 100644 --- a/include/boost/intrusive/detail/generic_hook.hpp +++ b/include/boost/intrusive/detail/generic_hook.hpp @@ -18,6 +18,7 @@ #include #include #include +#include #include namespace boost { @@ -35,6 +36,7 @@ enum , SplaySetBaseHook , AvlSetBaseHook , BsSetBaseHook +, AnyBaseHook }; struct no_default_definer{}; @@ -70,6 +72,10 @@ template struct default_definer { typedef Hook default_bs_set_hook; }; +template +struct default_definer +{ typedef Hook default_any_hook; }; + template struct make_default_definer { @@ -90,11 +96,11 @@ struct make_node_holder typedef typename detail::if_c ::value , detail::node_holder - < typename GetNodeAlgorithms::type::node_traits::node + < typename GetNodeAlgorithms::type::node , Tag , LinkMode , HookType> - , typename GetNodeAlgorithms::type::node_traits::node + , typename GetNodeAlgorithms::type::node >::type type; }; @@ -122,38 +128,38 @@ class generic_hook , public make_node_holder::type /// @endcond { - public: /// @cond + typedef typename GetNodeAlgorithms::type node_algorithms; + typedef typename node_algorithms::node node; + typedef typename node_algorithms::node_ptr node_ptr; + typedef typename node_algorithms::const_node_ptr const_node_ptr; + + public: struct boost_intrusive_tags { static const int hook_type = HookType; static const link_mode_type link_mode = LinkMode; - typedef Tag tag; - typedef typename GetNodeAlgorithms::type node_algorithms; - typedef typename node_algorithms::node_traits 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 Tag tag; + typedef typename GetNodeAlgorithms::type::node_traits node_traits; static const bool is_base_hook = !detail::is_same::value; - enum { safemode_or_autounlink = - (int)link_mode == (int)auto_unlink || - (int)link_mode == (int)safe_link }; + static const bool safemode_or_autounlink = + (int)link_mode == (int)auto_unlink || (int)link_mode == (int)safe_link; }; + + public: /// @endcond generic_hook() { if(boost_intrusive_tags::safemode_or_autounlink){ - boost_intrusive_tags::node_algorithms::init - (static_cast(this)); + node_algorithms::init(static_cast(this)); } } generic_hook(const generic_hook& ) { if(boost_intrusive_tags::safemode_or_autounlink){ - boost_intrusive_tags::node_algorithms::init - (static_cast(this)); + node_algorithms::init(static_cast(this)); } } @@ -168,26 +174,23 @@ class generic_hook void swap_nodes(generic_hook &other) { - boost_intrusive_tags::node_algorithms::swap_nodes - ( static_cast(this) - , static_cast(&other)); + node_algorithms::swap_nodes + ( static_cast(this), static_cast(&other)); } bool is_linked() const { //is_linked() can be only used in safe-mode or auto-unlink BOOST_STATIC_ASSERT(( boost_intrusive_tags::safemode_or_autounlink )); - return !boost_intrusive_tags::node_algorithms::unique - (static_cast(this)); + return !node_algorithms::unique + (static_cast(this)); } void unlink() { BOOST_STATIC_ASSERT(( (int)boost_intrusive_tags::link_mode == (int)auto_unlink )); - boost_intrusive_tags::node_algorithms::unlink - (static_cast(this)); - boost_intrusive_tags::node_algorithms::init - (static_cast(this)); + node_algorithms::unlink(static_cast(this)); + node_algorithms::init(static_cast(this)); } }; diff --git a/include/boost/intrusive/detail/hashtable_node.hpp b/include/boost/intrusive/detail/hashtable_node.hpp index 66bec59..44a5f5a 100644 --- a/include/boost/intrusive/detail/hashtable_node.hpp +++ b/include/boost/intrusive/detail/hashtable_node.hpp @@ -104,13 +104,13 @@ class hashtable_iterator ::type > { - typedef typename Container::real_value_traits real_value_traits; - typedef typename Container::siterator siterator; - typedef typename Container::const_siterator const_siterator; - typedef typename Container::bucket_type bucket_type; + typedef typename Container::real_value_traits real_value_traits; + typedef typename Container::siterator siterator; + typedef typename Container::const_siterator const_siterator; + typedef typename Container::bucket_type bucket_type; typedef typename boost::pointer_to_other - < typename Container::pointer, const Container>::type const_cont_ptr; - typedef typename Container::size_type size_type; + < typename Container::pointer, const Container>::type const_cont_ptr; + typedef typename Container::size_type size_type; static typename Container::node_ptr downcast_bucket(typename bucket_type::node_ptr p) { return typename Container::node_ptr(&static_cast(*p)); } diff --git a/include/boost/intrusive/detail/list_node.hpp b/include/boost/intrusive/detail/list_node.hpp index a4d4505..371224e 100644 --- a/include/boost/intrusive/detail/list_node.hpp +++ b/include/boost/intrusive/detail/list_node.hpp @@ -18,7 +18,6 @@ #include #include #include -#include namespace boost { namespace intrusive { diff --git a/include/boost/intrusive/detail/parent_from_member.hpp b/include/boost/intrusive/detail/parent_from_member.hpp index c3becf1..d5fc96a 100644 --- a/include/boost/intrusive/detail/parent_from_member.hpp +++ b/include/boost/intrusive/detail/parent_from_member.hpp @@ -13,12 +13,11 @@ #define BOOST_INTRUSIVE_PARENT_FROM_MEMBER_HPP #include -#include #include #if defined(BOOST_MSVC) || (defined (BOOST_WINDOWS) && defined(BOOST_INTEL)) -#define BOOST_INTRUSIVE_OFFSET_FROM_PTR2MEMBER_MSVC_COMPLIANT -#include +#define BOOST_INTRUSIVE_MSVC_COMPLIANT_PTR_TO_MEMBER +#include #endif namespace boost { @@ -26,16 +25,18 @@ namespace intrusive { namespace detail { template -inline std::size_t offset_from_pointer_to_member(const Member Parent::* ptr_to_member) +inline std::ptrdiff_t offset_from_pointer_to_member(const Member Parent::* ptr_to_member) { //The implementation of a pointer to member is compiler dependent. - #if defined(BOOST_INTRUSIVE_OFFSET_FROM_PTR2MEMBER_MSVC_COMPLIANT) - //This works with gcc, msvc, ac++, ibmcpp + #if defined(BOOST_INTRUSIVE_MSVC_COMPLIANT_PTR_TO_MEMBER) + //msvc compliant compilers use their the first 32 bits as offset (even in 64 bit mode) return *(const boost::int32_t*)(void*)&ptr_to_member; - #elif defined(__GNUC__) || defined(__HP_aCC) || defined(BOOST_INTEL) || defined (__IBMCPP__) || defined (__DECCXX) + //This works with gcc, msvc, ac++, ibmcpp + #elif defined(__GNUC__) || defined(__HP_aCC) || defined(BOOST_INTEL) || \ + defined(__IBMCPP__) || defined(__DECCXX) const Parent * const parent = 0; const char *const member = reinterpret_cast(&(parent->*ptr_to_member)); - return std::size_t(member - reinterpret_cast(parent)); + return std::ptrdiff_t(member - reinterpret_cast(parent)); #else //This is the traditional C-front approach: __MWERKS__, __DMC__, __SUNPRO_CC return (*(const std::ptrdiff_t*)(void*)&ptr_to_member) - 1; @@ -60,8 +61,8 @@ inline const Parent *parent_from_member(const Member *member, const Member Paren } //namespace intrusive { } //namespace boost { -#ifdef BOOST_INTRUSIVE_OFFSET_FROM_PTR2MEMBER_MSVC_COMPLIANT -#undef BOOST_INTRUSIVE_OFFSET_FROM_PTR2MEMBER_MSVC_COMPLIANT +#ifdef BOOST_INTRUSIVE_MSVC_COMPLIANT_PTR_TO_MEMBER +#undef BOOST_INTRUSIVE_MSVC_COMPLIANT_PTR_TO_MEMBER #endif #include diff --git a/include/boost/intrusive/detail/slist_node.hpp b/include/boost/intrusive/detail/slist_node.hpp index fb516ec..8195e54 100644 --- a/include/boost/intrusive/detail/slist_node.hpp +++ b/include/boost/intrusive/detail/slist_node.hpp @@ -18,7 +18,6 @@ #include #include #include -#include namespace boost { namespace intrusive { diff --git a/include/boost/intrusive/detail/tree_algorithms.hpp b/include/boost/intrusive/detail/tree_algorithms.hpp index 3cf9786..65e673f 100644 --- a/include/boost/intrusive/detail/tree_algorithms.hpp +++ b/include/boost/intrusive/detail/tree_algorithms.hpp @@ -94,13 +94,8 @@ namespace detail { template class tree_algorithms { - /// @cond - private: - - typedef typename NodeTraits::node node; - /// @endcond - public: + typedef typename NodeTraits::node node; typedef NodeTraits node_traits; typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; diff --git a/include/boost/intrusive/detail/utilities.hpp b/include/boost/intrusive/detail/utilities.hpp index 19b6628..743f329 100644 --- a/include/boost/intrusive/detail/utilities.hpp +++ b/include/boost/intrusive/detail/utilities.hpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace boost { namespace intrusive { @@ -55,6 +56,24 @@ struct internal_base_hook_bool_is_true static const bool value = internal_base_hook_bool::value > sizeof(one)*2; }; +template +struct internal_any_hook_bool +{ + template + struct two_or_three {one _[2 + Add];}; + template static one test(...); + template static two_or_three + test (detail::bool_* = 0); + static const std::size_t value = sizeof(test(0)); +}; + +template +struct internal_any_hook_bool_is_true +{ + static const bool value = internal_any_hook_bool::value > sizeof(one)*2; +}; + + template struct external_value_traits_bool { diff --git a/include/boost/intrusive/hashtable.hpp b/include/boost/intrusive/hashtable.hpp index 8ae0968..2d51e79 100644 --- a/include/boost/intrusive/hashtable.hpp +++ b/include/boost/intrusive/hashtable.hpp @@ -83,9 +83,8 @@ struct get_slist_impl }; template -struct unordered_bucket_impl +struct real_from_supposed_value_traits { - /// @cond typedef typename detail::eval_if_c < detail::external_value_traits_is_true ::value @@ -93,15 +92,33 @@ struct unordered_bucket_impl , detail::identity - >::type real_value_traits; + >::type type; +}; +template +struct get_slist_impl_from_supposed_value_traits +{ + typedef typename + real_from_supposed_value_traits + < SupposedValueTraits>::type real_value_traits; typedef typename detail::get_node_traits ::type node_traits; typedef typename get_slist_impl ::type - >::type slist_impl; + >::type type; +}; + + +template +struct unordered_bucket_impl +{ + /// @cond + typedef typename + get_slist_impl_from_supposed_value_traits + ::type slist_impl; typedef detail::bucket_impl implementation_defined; + /// @endcond typedef implementation_defined type; }; @@ -109,15 +126,6 @@ template struct unordered_bucket_ptr_impl { /// @cond - - typedef typename detail::eval_if_c - < detail::external_value_traits_is_true - ::value - , detail::eval_value_traits - - , detail::identity - - >::type real_value_traits; typedef typename detail::get_node_traits ::type::node_ptr node_ptr; typedef typename unordered_bucket_impl @@ -128,36 +136,6 @@ struct unordered_bucket_ptr_impl typedef implementation_defined type; }; -} //namespace detail { - -/// @endcond - -template -struct unordered_bucket -{ - /// @cond - typedef typename ValueTraitsOrHookOption:: - template pack::value_traits supposed_value_traits; - /// @endcond - typedef typename detail::unordered_bucket_impl - ::type type; -}; - -template -struct unordered_bucket_ptr -{ - /// @cond - typedef typename ValueTraitsOrHookOption:: - template pack::value_traits supposed_value_traits; - /// @endcond - typedef typename detail::unordered_bucket_ptr_impl - ::type type; -}; - -/// @cond - -namespace detail{ - template struct store_hash_bool { @@ -275,8 +253,6 @@ struct insert_commit_data_impl std::size_t hash; }; -} //namespace detail { - template struct internal_default_uset_hook { @@ -285,6 +261,49 @@ struct internal_default_uset_hook static const bool value = sizeof(test(0)) == sizeof(detail::two); }; +} //namespace detail { + +//!This metafunction will obtain the type of a bucket +//!from the value_traits or hook option to be used with +//!a hash container. +template +struct unordered_bucket + : public detail::unordered_bucket_impl + ::value_traits + > +{}; + +//!This metafunction will obtain the type of a bucket pointer +//!from the value_traits or hook option to be used with +//!a hash container. +template +struct unordered_bucket_ptr + : public detail::unordered_bucket_ptr_impl + ::value_traits + > +{}; + +//!This metafunction will obtain the type of the default bucket traits +//!(when the user does not specify the bucket_traits<> option) from the +//!value_traits or hook option to be used with +//!a hash container. +template +struct unordered_default_bucket_traits +{ + /// @cond + typedef typename ValueTraitsOrHookOption:: + template pack::value_traits supposed_value_traits; + typedef typename detail:: + get_slist_impl_from_supposed_value_traits + ::type slist_impl; + typedef detail::bucket_traits_impl + implementation_defined; + /// @endcond + typedef implementation_defined type; +}; + template struct get_default_uset_hook { @@ -300,6 +319,7 @@ template < class ValueTraits , class BucketTraits , bool Power2Buckets , bool CacheBegin + , bool CompareHash > struct usetopt { @@ -312,6 +332,7 @@ struct usetopt static const bool power_2_buckets = Power2Buckets; static const bool unique_keys = UniqueKeys; static const bool cache_begin = CacheBegin; + static const bool compare_hash = CompareHash; }; struct default_bucket_traits; @@ -322,7 +343,7 @@ struct uset_defaults < none , base_hook < typename detail::eval_if_c - < internal_default_uset_hook::value + < detail::internal_default_uset_hook::value , get_default_uset_hook , detail::identity >::type @@ -334,6 +355,7 @@ struct uset_defaults , bucket_traits , power_2_buckets , cache_begin + , compare_hash >::type {}; @@ -441,17 +463,21 @@ class hashtable_impl = detail::optimize_multikey_is_true::value && !unique_keys; static const bool power_2_buckets = Config::power_2_buckets; static const bool cache_begin = Config::cache_begin; + static const bool compare_hash = Config::compare_hash; /// @cond private: + //Configuration error: compare_hash<> can't be specified without store_hash<> + //See documentation for more explanations + BOOST_STATIC_ASSERT((!compare_hash || store_hash)); + typedef typename slist_impl::node_ptr slist_node_ptr; typedef typename boost::pointer_to_other ::type void_pointer; //We'll define group traits, but these won't be instantiated if //optimize_multikey is not true - typedef unordered_group_node_traits group_traits; + typedef unordered_group_adapter group_traits; typedef circular_slist_algorithms group_algorithms; - typedef detail::bool_ store_hash_t; typedef detail::bool_ optimize_multikey_t; typedef detail::bool_ cache_begin_t; @@ -831,7 +857,11 @@ class hashtable_impl if(!found_equal){ it = b.before_begin(); } - this->priv_insert_in_group(found_equal ? dcast_bucket_ptr(it.pointed_node()) : node_ptr(0), n, optimize_multikey_t()); + if(optimize_multikey){ + node_ptr first_in_group = found_equal ? + dcast_bucket_ptr(it.pointed_node()) : node_ptr(0); + this->priv_insert_in_group(first_in_group, n, optimize_multikey_t()); + } priv_insertion_update_cache(bucket_num); this->priv_size_traits().increment(); return iterator(b.insert_after(it, *n), this); @@ -1136,10 +1166,10 @@ class hashtable_impl ,KeyValueEqual equal_func, Disposer disposer) { size_type bucket_num; - std::size_t hash; + std::size_t h; siterator prev; siterator it = - this->priv_find(key, hash_func, equal_func, bucket_num, hash, prev); + this->priv_find(key, hash_func, equal_func, bucket_num, h, prev); bool success = it != priv_invalid_local_it(); size_type count(0); if(!success){ @@ -1156,7 +1186,14 @@ class hashtable_impl bucket_type &b = this->priv_buckets()[bucket_num]; for(siterator end = b.end(); it != end; ++count, ++it){ slist_node_ptr n(it.pointed_node()); - if(!equal_func(key, priv_value_from_slist_node(n))){ + const value_type &v = priv_value_from_slist_node(n); + if(compare_hash){ + std::size_t vh = this->priv_stored_hash(v, store_hash_t()); + if(h != vh || !equal_func(key, v)){ + break; + } + } + else if(!equal_func(key, v)){ break; } this->priv_size_traits().decrement(); @@ -1657,7 +1694,7 @@ class hashtable_impl BOOST_INTRUSIVE_INVARIANT_ASSERT (!power_2_buckets || (0 == (new_buckets_len & (new_buckets_len-1u)))); - size_type n = this->priv_get_cache() - this->priv_buckets(); + size_type n = priv_get_cache_bucket_num(); const bool same_buffer = old_buckets == new_buckets; //If the new bucket length is a common factor //of the old one we can avoid hash calculations. @@ -1665,8 +1702,10 @@ class hashtable_impl (power_2_buckets ||(old_buckets_len % new_buckets_len) == 0); //If we are shrinking the same bucket array and it's //is a fast shrink, just rehash the last nodes - if(same_buffer && fast_shrink){ + size_type new_first_bucket_num = new_buckets_len; + if(same_buffer && fast_shrink && (n < new_buckets_len)){ n = new_buckets_len; + new_first_bucket_num = priv_get_cache_bucket_num(); } //Anti-exception stuff: they destroy the elements if something goes wrong @@ -1696,6 +1735,8 @@ class hashtable_impl const value_type &v = priv_value_from_slist_node(i.pointed_node()); const std::size_t hash_value = this->priv_stored_hash(v, store_hash_t()); const size_type new_n = priv_hash_to_bucket(hash_value, new_buckets_len); + if(cache_begin && new_n < new_first_bucket_num) + new_first_bucket_num = new_n; siterator last = bucket_type::s_iterator_to (*priv_get_last_in_group(dcast_bucket_ptr(i.pointed_node()))); if(same_buffer && new_n == n){ @@ -1710,6 +1751,8 @@ class hashtable_impl } else{ const size_type new_n = priv_hash_to_bucket(n, new_buckets_len); + if(cache_begin && new_n < new_first_bucket_num) + new_first_bucket_num = new_n; bucket_type &new_b = new_buckets[new_n]; if(!old_bucket.empty()){ new_b.splice_after( new_b.before_begin() @@ -1723,8 +1766,8 @@ class hashtable_impl this->priv_size_traits().set_size(size_backup); this->priv_real_bucket_traits() = new_bucket_traits; priv_initialize_cache(); - priv_insertion_update_cache(size_type(0u)); - priv_erasure_update_cache(); + priv_insertion_update_cache(new_first_bucket_num); + //priv_erasure_update_cache(); rollback1.release(); rollback2.release(); } @@ -1912,16 +1955,16 @@ class hashtable_impl static node_ptr dcast_bucket_ptr(typename slist_impl::node_ptr p) { return node_ptr(&static_cast(*p)); } - std::size_t priv_stored_hash(const value_type &v, detail::true_) + std::size_t priv_stored_hash(const value_type &v, detail::true_) const { return node_traits::get_hash(this->get_real_value_traits().to_node_ptr(v)); } - std::size_t priv_stored_hash(const value_type &v, detail::false_) + std::size_t priv_stored_hash(const value_type &v, detail::false_) const { return priv_hasher()(v); } - std::size_t priv_stored_hash(slist_node_ptr n, detail::true_) + std::size_t priv_stored_hash(slist_node_ptr n, detail::true_) const { return node_traits::get_hash(dcast_bucket_ptr(n)); } - std::size_t priv_stored_hash(slist_node_ptr, detail::false_) + std::size_t priv_stored_hash(slist_node_ptr, detail::false_) const { //This code should never be reached! BOOST_INTRUSIVE_INVARIANT_ASSERT(0); @@ -2414,7 +2457,13 @@ class hashtable_impl while(it != b.end()){ const value_type &v = priv_value_from_slist_node(it.pointed_node()); - if(equal_func(key, v)){ + if(compare_hash){ + std::size_t vh = this->priv_stored_hash(v, store_hash_t()); + if(h == vh && equal_func(key, v)){ + return it; + } + } + else if(equal_func(key, v)){ return it; } if(optimize_multikey){ @@ -2470,7 +2519,15 @@ class hashtable_impl ++it; while(it != b.end()){ const value_type &v = priv_value_from_slist_node(it.pointed_node()); - if(!equal_func(key, v)){ + if(compare_hash){ + std::size_t hv = this->priv_stored_hash(v, store_hash_t()); + if(hv != h || !equal_func(key, v)){ + to_return.second = it; + bucket_number_second = bucket_number_first; + return to_return; + } + } + else if(!equal_func(key, v)){ to_return.second = it; bucket_number_second = bucket_number_first; return to_return; @@ -2505,11 +2562,12 @@ template < class T , class O3 = none, class O4 = none , class O5 = none, class O6 = none , class O7 = none, class O8 = none + , class O9 = none > struct make_hashtable_opt { typedef typename pack_options - < uset_defaults, O1, O2, O3, O4, O5, O6, O7, O8>::type packed_options; + < uset_defaults, O1, O2, O3, O4, O5, O6, O7, O8, O9>::type packed_options; //Real value traits must be calculated from options typedef typename detail::get_value_traits @@ -2529,10 +2587,7 @@ struct make_hashtable_opt typedef typename detail::get_slist_impl ::type - >::type slist_impl; - - typedef typename detail::reduced_slist_node_traits - ::type node_traits; + >::type slist_impl; typedef typename detail::if_c< detail::is_same @@ -2553,6 +2608,7 @@ struct make_hashtable_opt , real_bucket_traits , packed_options::power_2_buckets , packed_options::cache_begin + , packed_options::compare_hash > type; }; /// @endcond @@ -2566,6 +2622,7 @@ template #endif struct make_hashtable @@ -2573,7 +2630,7 @@ struct make_hashtable /// @cond typedef hashtable_impl < typename make_hashtable_opt - ::type + ::type > implementation_defined; /// @endcond @@ -2581,12 +2638,12 @@ struct make_hashtable }; #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED -template +template class hashtable - : public make_hashtable::type + : public make_hashtable::type { typedef typename make_hashtable - ::type Base; + ::type Base; public: typedef typename Base::value_traits value_traits; diff --git a/include/boost/intrusive/intrusive_fwd.hpp b/include/boost/intrusive/intrusive_fwd.hpp index c260187..0642fff 100644 --- a/include/boost/intrusive/intrusive_fwd.hpp +++ b/include/boost/intrusive/intrusive_fwd.hpp @@ -294,6 +294,7 @@ template , class O6 = none , class O7 = none , class O8 = none + , class O9 = none > class hashtable; @@ -307,6 +308,7 @@ template , class O6 = none , class O7 = none , class O8 = none + , class O9 = none > class unordered_set; @@ -320,6 +322,7 @@ template , class O6 = none , class O7 = none , class O8 = none + , class O9 = none > class unordered_multiset; @@ -339,6 +342,20 @@ template > class unordered_set_member_hook; +template + < class O1 = none + , class O2 = none + , class O3 = none + > +class any_base_hook; + +template + < class O1 = none + , class O2 = none + , class O3 = none + > +class any_member_hook; + } //namespace intrusive { } //namespace boost { diff --git a/include/boost/intrusive/linear_slist_algorithms.hpp b/include/boost/intrusive/linear_slist_algorithms.hpp index 9f1f599..b52a7ba 100644 --- a/include/boost/intrusive/linear_slist_algorithms.hpp +++ b/include/boost/intrusive/linear_slist_algorithms.hpp @@ -53,6 +53,7 @@ class linear_slist_algorithms typedef detail::common_slist_algorithms base_t; /// @endcond public: + typedef typename NodeTraits::node node; typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; typedef NodeTraits node_traits; diff --git a/include/boost/intrusive/options.hpp b/include/boost/intrusive/options.hpp index 2e55c58..ab9535a 100644 --- a/include/boost/intrusive/options.hpp +++ b/include/boost/intrusive/options.hpp @@ -49,15 +49,56 @@ struct eval_bucket_traits typedef typename BucketTraits::bucket_traits type; }; +template +struct concrete_hook_base_value_traits +{ + typedef typename BaseHook::boost_intrusive_tags tags; + typedef detail::base_hook_traits + < T + , typename tags::node_traits + , tags::link_mode + , typename tags::tag + , tags::hook_type> type; +}; + +template +struct concrete_hook_base_node_traits +{ typedef typename BaseHook::boost_intrusive_tags::node_traits type; }; + +template +struct any_hook_base_value_traits +{ + typedef typename BaseHook::boost_intrusive_tags tags; + typedef detail::base_hook_traits + < T + , typename BaseHook::node_traits + , tags::link_mode + , typename tags::tag + , tags::hook_type> type; +}; + +template +struct any_hook_base_node_traits +{ typedef typename BaseHook::node_traits type; }; + template struct get_base_value_traits { - typedef detail::base_hook_traits - < T - , typename BaseHook::boost_intrusive_tags::node_traits - , BaseHook::boost_intrusive_tags::link_mode - , typename BaseHook::boost_intrusive_tags::tag - , BaseHook::boost_intrusive_tags::hook_type> type; + typedef typename detail::eval_if_c + < internal_any_hook_bool_is_true::value + , any_hook_base_value_traits + , concrete_hook_base_value_traits + >::type type; +}; + +template +struct get_base_node_traits +{ + typedef typename detail::eval_if_c + < internal_any_hook_bool_is_true::value + , any_hook_base_node_traits + , concrete_hook_base_node_traits + >::type type; }; template @@ -66,6 +107,12 @@ struct get_member_value_traits typedef typename MemberHook::member_value_traits type; }; +template +struct get_member_node_traits +{ + typedef typename MemberHook::member_value_traits::node_traits type; +}; + template struct get_value_traits { @@ -86,25 +133,12 @@ struct get_value_traits >::type type; }; -template -struct get_base_node_traits -{ - typedef typename BaseHook::boost_intrusive_tags::node_traits type; -}; - -template -struct get_member_node_traits -{ - typedef typename MemberHook::member_value_traits::node_traits type; -}; - template struct get_explicit_node_traits { typedef typename ValueTraits::node_traits type; }; - template struct get_node_traits { @@ -125,7 +159,6 @@ struct get_node_traits >::type type; }; - } //namespace detail{ @@ -258,7 +291,6 @@ template< typename Parent struct member_hook { /// @cond - typedef char Parent::* GenericPtrToMember; typedef detail::member_hook_traits < Parent , MemberHook @@ -272,6 +304,7 @@ struct member_hook /// @endcond }; + //!This option setter specifies that the container //!must use the specified base hook template @@ -457,6 +490,25 @@ struct cache_begin /// @endcond }; + +//!This option setter specifies if the container will compare the hash value +//!before comparing objects. This option can't be specified if store_hash<> +//!is not true. +//!This is specially helpful when we have containers with a high load factor. +//!and the comparison function is much more expensive that comparing already +//!stored hash values. +template +struct compare_hash +{ +/// @cond + template + struct pack : Base + { + static const bool compare_hash = Enabled; + }; +/// @endcond +}; + /// @cond template diff --git a/include/boost/intrusive/rbtree_algorithms.hpp b/include/boost/intrusive/rbtree_algorithms.hpp index 8699d4c..c408193 100644 --- a/include/boost/intrusive/rbtree_algorithms.hpp +++ b/include/boost/intrusive/rbtree_algorithms.hpp @@ -116,6 +116,7 @@ class rbtree_algorithms { public: typedef NodeTraits node_traits; + typedef typename NodeTraits::node node; typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; typedef typename NodeTraits::color color; @@ -123,7 +124,6 @@ class rbtree_algorithms /// @cond private: - typedef typename NodeTraits::node node; typedef detail::tree_algorithms tree_algorithms; template diff --git a/include/boost/intrusive/sgtree_algorithms.hpp b/include/boost/intrusive/sgtree_algorithms.hpp index d37f5b3..18c0884 100644 --- a/include/boost/intrusive/sgtree_algorithms.hpp +++ b/include/boost/intrusive/sgtree_algorithms.hpp @@ -58,6 +58,7 @@ template class sgtree_algorithms { public: + typedef typename NodeTraits::node node; typedef NodeTraits node_traits; typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; @@ -65,7 +66,6 @@ class sgtree_algorithms /// @cond private: - typedef typename NodeTraits::node node; typedef detail::tree_algorithms tree_algorithms; static node_ptr uncast(const_node_ptr ptr) diff --git a/include/boost/intrusive/splaytree_algorithms.hpp b/include/boost/intrusive/splaytree_algorithms.hpp index f42cd3c..409449f 100644 --- a/include/boost/intrusive/splaytree_algorithms.hpp +++ b/include/boost/intrusive/splaytree_algorithms.hpp @@ -130,11 +130,11 @@ class splaytree_algorithms { /// @cond private: - typedef typename NodeTraits::node node; typedef detail::tree_algorithms tree_algorithms; /// @endcond public: + typedef typename NodeTraits::node node; typedef NodeTraits node_traits; typedef typename NodeTraits::node_ptr node_ptr; typedef typename NodeTraits::const_node_ptr const_node_ptr; diff --git a/include/boost/intrusive/unordered_set.hpp b/include/boost/intrusive/unordered_set.hpp index 342e2a6..6ec56fc 100644 --- a/include/boost/intrusive/unordered_set.hpp +++ b/include/boost/intrusive/unordered_set.hpp @@ -961,6 +961,7 @@ template #endif struct make_unordered_set @@ -968,19 +969,19 @@ struct make_unordered_set /// @cond typedef unordered_set_impl < typename make_hashtable_opt - ::type + ::type > implementation_defined; /// @endcond typedef implementation_defined type; }; #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED -template +template class unordered_set - : public make_unordered_set::type + : public make_unordered_set::type { typedef typename make_unordered_set - ::type Base; + ::type Base; //Assert if passed value traits are compatible with the type BOOST_STATIC_ASSERT((detail::is_same::value)); @@ -1894,6 +1895,7 @@ template #endif struct make_unordered_multiset @@ -1901,19 +1903,19 @@ struct make_unordered_multiset /// @cond typedef unordered_multiset_impl < typename make_hashtable_opt - ::type + ::type > implementation_defined; /// @endcond typedef implementation_defined type; }; #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED -template +template class unordered_multiset - : public make_unordered_multiset::type + : public make_unordered_multiset::type { typedef typename make_unordered_multiset - ::type Base; + ::type Base; //Assert if passed value traits are compatible with the type BOOST_STATIC_ASSERT((detail::is_same::value)); diff --git a/include/boost/intrusive/unordered_set_hook.hpp b/include/boost/intrusive/unordered_set_hook.hpp index 4920534..7f6f5bd 100644 --- a/include/boost/intrusive/unordered_set_hook.hpp +++ b/include/boost/intrusive/unordered_set_hook.hpp @@ -35,7 +35,6 @@ struct unordered_node < VoidPointer , unordered_node >::type node_ptr; -// node_ptr next_; node_ptr prev_in_group_; std::size_t hash_; }; @@ -48,7 +47,6 @@ struct unordered_node < VoidPointer , unordered_node >::type node_ptr; -// node_ptr next_; node_ptr prev_in_group_; }; @@ -60,7 +58,6 @@ struct unordered_node < VoidPointer , unordered_node >::type node_ptr; -// node_ptr next_; std::size_t hash_; }; @@ -97,34 +94,26 @@ struct unordered_node_traits { n->hash_ = h; } }; -template -struct unordered_group_node_traits +template +struct unordered_group_adapter { - typedef Node node; - typedef typename boost::pointer_to_other - ::type node_ptr; - typedef typename boost::pointer_to_other - ::type const_node_ptr; + typedef typename NodeTraits::node node; + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; static node_ptr get_next(const_node_ptr n) - { return n->prev_in_group_; } + { return NodeTraits::get_prev_in_group(n); } static void set_next(node_ptr n, node_ptr next) - { n->prev_in_group_ = next; } + { NodeTraits::set_prev_in_group(n, next); } }; template struct unordered_algorithms : public circular_slist_algorithms { - typedef circular_slist_algorithms base_type; - typedef unordered_group_node_traits - < typename boost::pointer_to_other - < typename NodeTraits::node_ptr - , void - >::type - , typename NodeTraits::node - > group_traits; + typedef circular_slist_algorithms base_type; + typedef unordered_group_adapter group_traits; typedef circular_slist_algorithms group_algorithms; static void init(typename base_type::node_ptr n) From e9cdb500d6e825b71dddfd487f50f16a114f1a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Sat, 21 Jun 2008 09:06:15 +0000 Subject: [PATCH 19/20] gcc 4.3 fixes for normal and -std=c++0x modes [SVN r46573] --- doc/intrusive.qbk | 100 ++++++++++++++---- proj/vc7ide/Intrusive.sln | 8 ++ .../vc7ide/_intrusivelib/_intrusivelib.vcproj | 9 ++ proj/vc7ide/list/list.vcproj | 10 -- proj/vc7ide/multiset/multiset.vcproj | 10 -- proj/vc7ide/perf_test/perf_test.vcproj | 10 -- proj/vc7ide/set/set.vcproj | 10 -- proj/vc7ide/slist/slist.vcproj | 10 -- proj/vc7ide/to-do.txt | 43 ++++++-- .../unordered_multiset.vcproj | 10 -- .../vc7ide/unordered_set/unordered_set.vcproj | 10 -- test/common_functors.hpp | 8 ++ test/list_test.cpp | 1 + test/slist_test.cpp | 2 +- test/unordered_multiset_test.cpp | 48 ++++++--- test/unordered_set_test.cpp | 48 ++++++--- 16 files changed, 201 insertions(+), 136 deletions(-) diff --git a/doc/intrusive.qbk b/doc/intrusive.qbk index cf1f5e6..2b410ed 100644 --- a/doc/intrusive.qbk +++ b/doc/intrusive.qbk @@ -127,7 +127,7 @@ way to make user classes compatible with those containers. [section:properties_of_intrusive Properties of Boost.Intrusive containers] -Semantically, a [*Boost.Intrusive] container is similar to an STL container +Semantically, a [*Boost.Intrusive] container is similar to a 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 @@ -584,7 +584,7 @@ Apart from that, [*Boost.Intrusive] offers additional features: 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 + programming errors. It implies a slight performance overhead in some operations and can convert some constant time operations to linear time operations. * [*Auto-unlink hooks]: The hook destructor removes the object from the container @@ -596,7 +596,7 @@ Apart from that, [*Boost.Intrusive] offers additional features: be configured to use any type of pointer. 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, + [*Boost.Interprocess] defines an mart pointer compatible with shared memory, called `offset_ptr`. [*Boost.Intrusive] can be configured to use this smart pointer to allow shared memory intrusive containers. @@ -718,7 +718,7 @@ Let's see an example of an auto-unlink hook: 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: +auto-unlink hook's value_traits, you will get a static assertion: [c++] @@ -860,7 +860,7 @@ assertion will be raised. [section:slist_example Example] -Now let's see an small example using both hooks: +Now let's see a small example using both hooks: [import ../example/doc_slist.cpp] [doc_slist_code] @@ -946,7 +946,7 @@ the section [link intrusive.usage How to use Boost.Intrusive]: [section:list_example Example] -Now let's see an small example using both hooks: +Now let's see a small example using both hooks: [import ../example/doc_list.cpp] [doc_list_code] @@ -1075,7 +1075,7 @@ And they also can receive an additional option: [section:set_multiset_example Example] -Now let's see an small example using both hooks and both containers: +Now let's see a small example using both hooks and both containers: [import ../example/doc_set.cpp] [doc_set_code] @@ -1277,11 +1277,22 @@ And they also can receive additional options: of the first element. This imposes the overhead of one pointer to the size of the container. Default: `cache_begin`. +* [*`compare_hash`]: + [*Note: this option requires `store_hash` option in the hook]. + When the comparison function is expensive, + (e.g. strings with a long common predicate) sometimes (specially when the + load factor is high or we have many equivalent elements in an + [classref boost::intrusive::unordered_multiset unordered_multiset] and + no `optimize_multikey<>` is activatedin the hook) + the equality function is a performance problem. Two equal values must have + equal hashes, so comparing the hash values of two elements before using the + comparison functor can speed up some implementations. + [endsect] [section:unordered_set_unordered_multiset_example Example] -Now let's see an small example using both hooks and both containers: +Now let's see a small example using both hooks and both containers: [import ../example/doc_unordered_set.cpp] [doc_unordered_set_code] @@ -1450,9 +1461,9 @@ intrusive scapegoat containers [classref boost::intrusive::sg_set sg_set] and [classref boost::intrusive::sg_multiset sg_multiset]. A programmer might prefer using a binary search tree hook so that the same type -can be inserted in some situations in an splay container but +can be inserted in some situations in a splay container but also inserted in other compatible containers when -the hook is not being used in an splay container. +the hook is not being used in a 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 @@ -1462,7 +1473,7 @@ the same options as [classref boost::intrusive::splay_set_base_hook splay_set_ba [section:splay_set_multiset_example Example] -Now let's see an small example using both splay hooks, +Now let's see a 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] @@ -1590,7 +1601,7 @@ And they also can receive an additional option: [section:avl_set_multiset_example Example] -Now let's see an small example using both hooks and +Now let's see a small example using both hooks and [classref boost::intrusive::avl_set avl_set]/ [classref boost::intrusive::avl_multiset avl_multiset] containers: @@ -1745,7 +1756,7 @@ And they also can receive additional options: [section:sg_set_multiset_example Example] -Now let's see an small example using both hooks and +Now let's see a small example using both hooks and [classref boost::intrusive::sg_set sg_set]/ [classref boost::intrusive::sg_multiset sg_multiset] containers: @@ -1923,7 +1934,7 @@ more functions taking a disposer function object as argument, like `erase_and_di 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: +Let's see a small example: [import ../example/doc_erasing_and_disposing.cpp] [doc_erasing_and_disposing] @@ -1940,11 +1951,11 @@ that erase an element from the container. 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 +However, cloning by hand is usually more inefficient than a member cloning function and a 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`. +containers, all [*Boost.Intrusive] containers offer a special cloning function called `clone_from`. Apart from the container to be cloned, `clone_from` takes two function objects as arguments. For example, consider the `clone_from` member function of [classref boost::intrusive::list list]: @@ -1981,9 +1992,9 @@ Here's an example of `clone_from`: [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, +When a [*Boost.Intrusive] hook is configured with a 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 +hook is configured with a smart pointer (for example, an offset pointer from [*Boost.Interprocess]): [import ../example/doc_offset_ptr.cpp] @@ -2060,7 +2071,7 @@ explained in the [link intrusive.value_traits.stateful_value_traits Stateful val if 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 +Let's see a small function that shows the use of `iterator_to` and `local_iterator_to`: [import ../example/doc_iterator_from_value.cpp] @@ -2068,6 +2079,51 @@ Let's see an small function that shows the use of `iterator_to` and [endsect] +[section:any_hooks Any Hooks: A single hook for any Intrusive container] + +Sometimes, a class programmer wants to place a class in several intrusive +containers but no at the same time. In this case, the programmer might +decide to insert two hooks in the same class. + +[c++] + + class MyClass + : public list_base_hook<>, public slist_base_hook<> //... + {}; + +However, there is a more size-efficient alternative in [*Boost.Intrusive]: "any" hooks +([classref boost::intrusive::any_base_hook any_base_hook] and +[classref boost::intrusive::any_member_hook any_member_hook]. +These hooks can be used to store a type in several containers +offered by [*Boost.Intrusive] minimizing the size of the class. + +These hooks support these options: + +* [*`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. + `link_mode` is [*not] supported and `link_mode` + might offer weaker error detection in any hooks than in other hooks. + Default: `link_mode`. + +* [*`void_pointer`]: The pointer type to be used + internally in the hook and propagated to the container. + Default: `void_pointer`. + +`auto_unlink` can't be supported because the hook does not know in which type of might +be inserted container. Additionally, these hooks don't support `unlink()` and +`swap_nodes()` operations for the same reason. + +Here's an example that creates a class with two any hooks, and uses one to insert the +class in a [classref slist] and the other one in a [classref list]. + +[import ../example/doc_any_hook.cpp] +[doc_any_hook] + +[endsect] + [section:concepts Concepts explained] This section will expand the explanation of previously presented basic concepts @@ -2187,7 +2243,7 @@ before explaining the customization options of [*Boost.Intrusive]. int value_; }; -* [*Intrusive Container]: A container that offers an STL-like interface to store +* [*Intrusive Container]: A container that offers a STL-like interface to store user objects. An intrusive container can be templatized to store different value types that use different hooks. An intrusive container is also more elaborate than a group of nodes: it can store the number of elements to achieve constant-time @@ -2642,7 +2698,7 @@ used in node algorithms, since these types can be different. Apart from this, 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 +optimized for a 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 @@ -2804,7 +2860,7 @@ In the previous example, `legacy_node_traits::node` type and 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: +conversions between both types. Let's see a 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: diff --git a/proj/vc7ide/Intrusive.sln b/proj/vc7ide/Intrusive.sln index 4fb0638..0b105c0 100644 --- a/proj/vc7ide/Intrusive.sln +++ b/proj/vc7ide/Intrusive.sln @@ -79,6 +79,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sg_set", "sg_set\sg_set.vcp ProjectSection(ProjectDependencies) = postProject EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "any_hook", "any_test\any_test.vcproj", "{97B61B24-4C97-9681-50BF-243175A813B6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject Global GlobalSection(SolutionConfiguration) = preSolution Debug = Debug @@ -167,6 +171,10 @@ Global {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 + {97B61B24-4C97-9681-50BF-243175A813B6}.Debug.ActiveCfg = Debug|Win32 + {97B61B24-4C97-9681-50BF-243175A813B6}.Debug.Build.0 = Debug|Win32 + {97B61B24-4C97-9681-50BF-243175A813B6}.Release.ActiveCfg = Release|Win32 + {97B61B24-4C97-9681-50BF-243175A813B6}.Release.Build.0 = Release|Win32 EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution EndGlobalSection diff --git a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj index 6dd0c3f..b42d957 100644 --- a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj +++ b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj @@ -105,6 +105,9 @@ Name="Header Files" Filter="h;hpp;hxx;hm;inl;inc;xsd" UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + + @@ -207,6 +210,9 @@ + + @@ -316,6 +322,9 @@ + + diff --git a/proj/vc7ide/list/list.vcproj b/proj/vc7ide/list/list.vcproj index 17c3b6a..e5e510b 100644 --- a/proj/vc7ide/list/list.vcproj +++ b/proj/vc7ide/list/list.vcproj @@ -127,16 +127,6 @@ RelativePath="..\..\..\test\list_test.cpp"> - - - - diff --git a/proj/vc7ide/multiset/multiset.vcproj b/proj/vc7ide/multiset/multiset.vcproj index 4303b0c..8828607 100644 --- a/proj/vc7ide/multiset/multiset.vcproj +++ b/proj/vc7ide/multiset/multiset.vcproj @@ -121,16 +121,6 @@ RelativePath="..\..\..\test\multiset_test.cpp"> - - - - diff --git a/proj/vc7ide/perf_test/perf_test.vcproj b/proj/vc7ide/perf_test/perf_test.vcproj index e94844d..5e410b1 100644 --- a/proj/vc7ide/perf_test/perf_test.vcproj +++ b/proj/vc7ide/perf_test/perf_test.vcproj @@ -122,16 +122,6 @@ RelativePath="..\..\..\perf\perf_list.cpp"> - - - - diff --git a/proj/vc7ide/set/set.vcproj b/proj/vc7ide/set/set.vcproj index 6e96cd2..85dff59 100644 --- a/proj/vc7ide/set/set.vcproj +++ b/proj/vc7ide/set/set.vcproj @@ -121,16 +121,6 @@ RelativePath="..\..\..\test\set_test.cpp"> - - - - diff --git a/proj/vc7ide/slist/slist.vcproj b/proj/vc7ide/slist/slist.vcproj index d9138b2..3d118f5 100644 --- a/proj/vc7ide/slist/slist.vcproj +++ b/proj/vc7ide/slist/slist.vcproj @@ -121,16 +121,6 @@ RelativePath="..\..\..\test\slist_test.cpp"> - - - - diff --git a/proj/vc7ide/to-do.txt b/proj/vc7ide/to-do.txt index 3428e61..747f679 100644 --- a/proj/vc7ide/to-do.txt +++ b/proj/vc7ide/to-do.txt @@ -1,12 +1,37 @@ -Add resize() to list -Implement incremental hashing -Add invariants to slist and test them after every operation -Create a generic hook that will work with all containers -Take advantage of store_hash in lookups: Instead of comparing objects, just - compare hashes. This will improve equality for expensive objects. +Implement incremental hashing: -Improve the use of cache_begin to unordered containers: --> Speed up rehash +bucket_len is power of two +cur_idx is always at least, half of bucket_len + +find bucket from value: + size_type bucket_num = hash(val) &(bucket_len - 1); + if (bucket_num >= cur_idx) + bucket_num -= bucket_len/2; + +incremental_rehash: + + iterator _Plist, _Where; + if (load_factor > LoadFactorLimit) + { // too dense, need to grow hash table + if (cur_idx >= (bucket_len - 1)) + { // table full, request doubling + return false; + } + else if (cur_idx >= bucket_len) + bucket_len*=2; + + size_type bucket_num = cur_idx - bucket_len/2; + // rehash elements from bucket_num + ++cur_idx; + } + +insert from value: + + size_type bucket_num = hash(val) & (bucket_len - 1); + if (bucket_num >= cur_idx) + bucket_num -= bucket_len/2; + insert in bucket + +rehash: -Add erase(iterator, iterator, difference_type) to lists to obtain constant-time erase. diff --git a/proj/vc7ide/unordered_multiset/unordered_multiset.vcproj b/proj/vc7ide/unordered_multiset/unordered_multiset.vcproj index 2b9c6f3..2559e0c 100644 --- a/proj/vc7ide/unordered_multiset/unordered_multiset.vcproj +++ b/proj/vc7ide/unordered_multiset/unordered_multiset.vcproj @@ -122,16 +122,6 @@ RelativePath="..\..\..\test\unordered_multiset_test.cpp"> - - - - diff --git a/proj/vc7ide/unordered_set/unordered_set.vcproj b/proj/vc7ide/unordered_set/unordered_set.vcproj index 33c95ac..b7e9681 100644 --- a/proj/vc7ide/unordered_set/unordered_set.vcproj +++ b/proj/vc7ide/unordered_set/unordered_set.vcproj @@ -121,16 +121,6 @@ RelativePath="..\..\..\test\unordered_set_test.cpp"> - - - - diff --git a/test/common_functors.hpp b/test/common_functors.hpp index 7a6d500..d3bc3fa 100644 --- a/test/common_functors.hpp +++ b/test/common_functors.hpp @@ -41,6 +41,14 @@ class new_cloner { return new T(t); } }; +template +class new_default_factory +{ + public: + T *operator()() + { return new T(); } +}; + } //namespace test { } //namespace intrusive { } //namespace boost { diff --git a/test/list_test.cpp b/test/list_test.cpp index 3a000a9..37998be 100644 --- a/test/list_test.cpp +++ b/test/list_test.cpp @@ -229,6 +229,7 @@ void test_list TEST_INTRUSIVE_SEQUENCE( init_values, const_testlist.begin() ); } } + template void test_list ::test_shift(std::vector& values) diff --git a/test/slist_test.cpp b/test/slist_test.cpp index c8055c1..7ca0afc 100644 --- a/test/slist_test.cpp +++ b/test/slist_test.cpp @@ -315,7 +315,7 @@ void test_slist testlist.erase (++testlist.begin(), testlist.end()); BOOST_TEST (testlist.size() == 1); BOOST_TEST (testlist.front().value_ == 3); -} +} template void test_slist diff --git a/test/unordered_multiset_test.cpp b/test/unordered_multiset_test.cpp index dda1ad3..e10efd2 100644 --- a/test/unordered_multiset_test.cpp +++ b/test/unordered_multiset_test.cpp @@ -27,7 +27,7 @@ using namespace boost::intrusive; static const std::size_t BucketSize = 11; -template +template struct test_unordered_multiset { typedef typename ValueTraits::value_type value_type; @@ -41,8 +41,8 @@ struct test_unordered_multiset static void test_clone(std::vector& values); }; -template -void test_unordered_multiset::test_all (std::vector& values) +template +void test_unordered_multiset::test_all (std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset @@ -50,6 +50,7 @@ void test_unordered_multiset::test_all (std::vector , constant_time_size , cache_begin + , compare_hash > unordered_multiset_type; { typedef typename unordered_multiset_type::bucket_traits bucket_traits; @@ -77,8 +78,8 @@ void test_unordered_multiset::test_all (std::vector -void test_unordered_multiset::test_impl() +template +void test_unordered_multiset::test_impl() { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset @@ -86,6 +87,7 @@ void test_unordered_multiset::test_impl() , value_traits , constant_time_size , cache_begin + , compare_hash > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; @@ -108,8 +110,8 @@ void test_unordered_multiset::test_impl() } //test: constructor, iterator, clear, reverse_iterator, front, back, size: -template -void test_unordered_multiset::test_sort(std::vector& values) +template +void test_unordered_multiset::test_sort(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset @@ -117,6 +119,7 @@ void test_unordered_multiset::test_sort(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; @@ -130,8 +133,8 @@ void test_unordered_multiset::test_sort(std::vector -void test_unordered_multiset::test_insert(std::vector& values) +template +void test_unordered_multiset::test_insert(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset @@ -139,6 +142,7 @@ void test_unordered_multiset::test_insert(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; typedef typename unordered_multiset_type::iterator iterator; @@ -269,8 +273,8 @@ void test_unordered_multiset::test_insert(std::vector -void test_unordered_multiset::test_swap(std::vector& values) +template +void test_unordered_multiset::test_swap(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset @@ -278,6 +282,7 @@ void test_unordered_multiset::test_swap(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; typename unordered_multiset_type::bucket_type buckets [BucketSize]; @@ -302,8 +307,8 @@ void test_unordered_multiset::test_swap(std::vector -void test_unordered_multiset::test_rehash(std::vector& values) +template +void test_unordered_multiset::test_rehash(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset @@ -311,6 +316,7 @@ void test_unordered_multiset::test_rehash(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; @@ -347,8 +353,8 @@ void test_unordered_multiset::test_rehash(std::vector -void test_unordered_multiset::test_find(std::vector& values) +template +void test_unordered_multiset::test_find(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_multiset @@ -356,6 +362,7 @@ void test_unordered_multiset::test_find(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; @@ -380,8 +387,8 @@ void test_unordered_multiset::test_find(std::vector -void test_unordered_multiset +template +void test_unordered_multiset ::test_clone(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -390,6 +397,7 @@ void test_unordered_multiset , value_traits , constant_time_size , cache_begin + , compare_hash > unordered_multiset_type; typedef typename unordered_multiset_type::bucket_traits bucket_traits; { @@ -462,6 +470,7 @@ class test_main_template , typename value_type::unordered_set_base_hook_t >::type , true + , false >::test_all(data); test_unordered_multiset < typename detail::get_member_value_traits @@ -472,6 +481,7 @@ class test_main_template > >::type , false + , false >::test_all(data); return 0; @@ -495,6 +505,7 @@ class test_main_template , typename value_type::unordered_set_base_hook_t >::type , true + , false >::test_all(data); test_unordered_multiset < typename detail::get_member_value_traits @@ -505,6 +516,7 @@ class test_main_template > >::type , false + , false >::test_all(data); test_unordered_multiset < typename detail::get_base_value_traits @@ -512,6 +524,7 @@ class test_main_template , typename value_type::unordered_set_auto_base_hook_t >::type , true + , true >::test_all(data); test_unordered_multiset < typename detail::get_member_value_traits @@ -522,6 +535,7 @@ class test_main_template > >::type , false + , true >::test_all(data); return 0; } diff --git a/test/unordered_set_test.cpp b/test/unordered_set_test.cpp index 6fcff90..7702a2e 100644 --- a/test/unordered_set_test.cpp +++ b/test/unordered_set_test.cpp @@ -26,7 +26,7 @@ using namespace boost::intrusive; static const std::size_t BucketSize = 11; -template +template struct test_unordered_set { typedef typename ValueTraits::value_type value_type; @@ -40,8 +40,8 @@ struct test_unordered_set static void test_clone(std::vector& values); }; -template -void test_unordered_set::test_all(std::vector& values) +template +void test_unordered_set::test_all(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set @@ -49,6 +49,7 @@ void test_unordered_set::test_all(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; { @@ -76,8 +77,8 @@ void test_unordered_set::test_all(std::vector -void test_unordered_set::test_impl() +template +void test_unordered_set::test_impl() { typedef typename ValueTraits::value_type value_type; typedef unordered_set @@ -85,6 +86,7 @@ void test_unordered_set::test_impl() , value_traits , constant_time_size , cache_begin + , compare_hash > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -105,8 +107,8 @@ void test_unordered_set::test_impl() } //test: constructor, iterator, clear, reverse_iterator, front, back, size: -template -void test_unordered_set::test_sort(std::vector& values) +template +void test_unordered_set::test_sort(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set @@ -114,6 +116,7 @@ void test_unordered_set::test_sort(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -129,8 +132,8 @@ void test_unordered_set::test_sort(std::vector -void test_unordered_set::test_insert(std::vector& values) +template +void test_unordered_set::test_insert(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set @@ -138,6 +141,7 @@ void test_unordered_set::test_insert(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -165,8 +169,8 @@ void test_unordered_set::test_insert(std::vector -void test_unordered_set::test_swap(std::vector& values) +template +void test_unordered_set::test_swap(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set @@ -174,6 +178,7 @@ void test_unordered_set::test_swap(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -197,8 +202,8 @@ void test_unordered_set::test_swap(std::vector -void test_unordered_set::test_rehash(std::vector& values) +template +void test_unordered_set::test_rehash(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set @@ -206,6 +211,7 @@ void test_unordered_set::test_rehash(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -243,8 +249,8 @@ void test_unordered_set::test_rehash(std::vector -void test_unordered_set::test_find(std::vector& values) +template +void test_unordered_set::test_find(std::vector& values) { typedef typename ValueTraits::value_type value_type; typedef unordered_set @@ -252,6 +258,7 @@ void test_unordered_set::test_find(std::vector , constant_time_size , cache_begin + , compare_hash > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; @@ -274,8 +281,8 @@ void test_unordered_set::test_find(std::vector -void test_unordered_set +template +void test_unordered_set ::test_clone(std::vector& values) { typedef typename ValueTraits::value_type value_type; @@ -284,6 +291,7 @@ void test_unordered_set , value_traits , constant_time_size , cache_begin + , compare_hash > unordered_set_type; typedef typename unordered_set_type::bucket_traits bucket_traits; { @@ -356,6 +364,7 @@ class test_main_template , typename value_type::unordered_set_base_hook_t >::type , true + , false >::test_all(data); test_unordered_set < typename detail::get_member_value_traits < value_type @@ -365,6 +374,7 @@ class test_main_template > >::type , false + , false >::test_all(data); return 0; @@ -388,6 +398,7 @@ class test_main_template , typename value_type::unordered_set_base_hook_t >::type , true + , false >::test_all(data); test_unordered_set < typename detail::get_member_value_traits @@ -398,6 +409,7 @@ class test_main_template > >::type , false + , false >::test_all(data); test_unordered_set < typename detail::get_base_value_traits @@ -405,6 +417,7 @@ class test_main_template , typename value_type::unordered_set_auto_base_hook_t >::type , true + , true >::test_all(data); test_unordered_set < typename detail::get_member_value_traits @@ -415,6 +428,7 @@ class test_main_template > >::type , false + , true >::test_all(data); return 0; } From 883b50a8d4b1c75f71c66585f2bc4321c9b6cbbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Sat, 21 Jun 2008 09:15:00 +0000 Subject: [PATCH 20/20] gcc 4.3 fixes for normal and -std=c++0x modes [SVN r46577] --- example/doc_any_hook.cpp | 62 ++++++++++ proj/vc7ide/any_test/any_test.vcproj | 133 ++++++++++++++++++++ test/any_test.cpp | 178 +++++++++++++++++++++++++++ 3 files changed, 373 insertions(+) create mode 100644 example/doc_any_hook.cpp create mode 100644 proj/vc7ide/any_test/any_test.vcproj create mode 100644 test/any_test.cpp diff --git a/example/doc_any_hook.cpp b/example/doc_any_hook.cpp new file mode 100644 index 0000000..15220dc --- /dev/null +++ b/example/doc_any_hook.cpp @@ -0,0 +1,62 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2008 +// +// 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_any_hook +#include +#include +#include +#include + +using namespace boost::intrusive; + +class MyClass : public any_base_hook<> //Base hook +{ + int int_; + + public: + any_member_hook<> member_hook_; //Member hook + + MyClass(int i = 0) : int_(i) + {} +}; + +int main() +{ + //Define a base hook option that converts any_base_hook to a slist hook + typedef any_to_slist_hook < base_hook< any_base_hook<> > > BaseSlistOption; + typedef slist BaseSList; + + //Define a member hook option that converts any_base_hook to a list hook + typedef any_to_list_hook< member_hook + < MyClass, any_member_hook<>, &MyClass::member_hook_> > MemberListOption; + typedef list MemberList; + + //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)); } + + BaseSList base_slist; MemberList member_list; + + //Now insert them in reverse order in the slist and in order in the list + for(std::vector::iterator it(values.begin()), itend(values.end()); it != itend; ++it) + base_slist.push_front(*it), member_list.push_back(*it); + + //Now test lists + BaseSList::iterator bit(base_slist.begin()), bitend(base_slist.end()); + MemberList::reverse_iterator mrit(member_list.rbegin()), mritend(member_list.rend()); + std::vector::reverse_iterator rit(values.rbegin()), ritend(values.rend()); + + //Test the objects inserted in the base hook list + for(; rit != ritend; ++rit, ++bit, ++mrit) + if(&*bit != &*rit || &*mrit != &*rit) return 1; + return 0; +} +//] diff --git a/proj/vc7ide/any_test/any_test.vcproj b/proj/vc7ide/any_test/any_test.vcproj new file mode 100644 index 0000000..b053349 --- /dev/null +++ b/proj/vc7ide/any_test/any_test.vcproj @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/any_test.cpp b/test/any_test.cpp new file mode 100644 index 0000000..9c24c56 --- /dev/null +++ b/test/any_test.cpp @@ -0,0 +1,178 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (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 +#include +#include +#include +#include +#include +#include +#include //std::vector +#include //std::size_t + +using namespace boost::intrusive; + +class MyClass : public any_base_hook<> +{ + int int_; + + public: + //This is a member hook + any_member_hook<> member_hook_; + + MyClass(int i = 0) + : int_(i) + {} + + int get() const + { return this->int_; } + + 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 &o) + { return boost::hash()(o.get()); } +}; + +void instantiation_test() +{ + typedef member_hook< MyClass, any_member_hook<>, &MyClass::member_hook_> MemberHook; + typedef base_hook< any_base_hook<> > BaseHook; + + MyClass myclass; + { + slist < MyClass, any_to_slist_hook< BaseHook > > slist_base; + slist_base.push_front(myclass); + } + { + slist < MyClass, any_to_slist_hook< MemberHook > > slist_member; + slist_member.push_front(myclass); + } + { + list < MyClass, any_to_list_hook< BaseHook > > list_base; + list_base.push_front(myclass); + } + { + list < MyClass, any_to_list_hook< MemberHook > > list_member; + list_member.push_front(myclass); + } + { + rbtree < MyClass, any_to_set_hook< BaseHook > > rbtree_base; + rbtree_base.insert_unique(myclass); + } + { + rbtree < MyClass, any_to_set_hook< MemberHook > > rbtree_member; + rbtree_member.insert_unique(myclass); + } + { + avltree < MyClass, any_to_avl_set_hook< BaseHook > > avltree_base; + avltree_base.insert_unique(myclass); + } + { + avltree < MyClass, any_to_avl_set_hook< MemberHook > > avltree_member; + avltree_member.insert_unique(myclass); + } + { + sgtree < MyClass, any_to_bs_set_hook< BaseHook > > sgtree_base; + sgtree_base.insert_unique(myclass); + } + { + sgtree < MyClass, any_to_bs_set_hook< MemberHook > > sgtree_member; + sgtree_member.insert_unique(myclass); + } + { + splaytree < MyClass, any_to_bs_set_hook< BaseHook > > splaytree_base; + splaytree_base.insert_unique(myclass); + } + { + splaytree < MyClass, any_to_bs_set_hook< MemberHook > > splaytree_member; + splaytree_member.insert_unique(myclass); + } + typedef unordered_bucket >::type bucket_type; + typedef unordered_default_bucket_traits >::type bucket_traits; + bucket_type buckets[2]; + { + hashtable < MyClass, any_to_unordered_set_hook< BaseHook > > + hashtable_base(bucket_traits(&buckets[0], 1)); + hashtable_base.insert_unique(myclass); + } + { + hashtable < MyClass, any_to_unordered_set_hook< MemberHook > > + hashtable_member(bucket_traits(&buckets[1], 1)); + hashtable_member.insert_unique(myclass); + } +} + +bool simple_slist_test() +{ + //Define an slist that will store MyClass using the public base hook + typedef any_to_slist_hook< base_hook< any_base_hook<> > >BaseOption; + typedef slist > BaseList; + + //Define an slist that will store MyClass using the public member hook + typedef any_to_slist_hook< member_hook, &MyClass::member_hook_> > MemberOption; + typedef slist MemberList; + + 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 false; + + //Test the objects inserted in the member hook list + for(; it != itend; ++it, ++mit) + if(&*mit != &*it) return false; + } + return true; +} + +int main() +{ + if(!simple_slist_test()) + return 1; + instantiation_test(); + return 0; +} + +#include