From b03908871987c4a1269b373fcad56939a880f6e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Fri, 4 May 2007 21:30:54 +0000 Subject: [PATCH] First inclusion [SVN r37593] --- doc/Jamfile.v2 | 40 + doc/html/boostbook.css | 538 ++++ doc/html/images/blank.png | Bin 0 -> 374 bytes doc/html/images/caution.png | Bin 0 -> 1250 bytes doc/html/images/draft.png | Bin 0 -> 17454 bytes doc/html/images/home.png | Bin 0 -> 358 bytes doc/html/images/important.png | Bin 0 -> 722 bytes doc/html/images/next.png | Bin 0 -> 336 bytes doc/html/images/note.png | Bin 0 -> 490 bytes doc/html/images/prev.png | Bin 0 -> 334 bytes doc/html/images/tip.png | Bin 0 -> 449 bytes doc/html/images/toc-blank.png | Bin 0 -> 318 bytes doc/html/images/toc-minus.png | Bin 0 -> 259 bytes doc/html/images/toc-plus.png | Bin 0 -> 264 bytes doc/html/images/up.png | Bin 0 -> 370 bytes doc/html/images/warning.png | Bin 0 -> 1241 bytes doc/html/reference.css | 5 + doc/intrusive.qbk | 2440 +++++++++++++++++ example/Jamfile.v2 | 34 + example/doc_advanced_value_traits.cpp | 103 + example/doc_assoc_optimized_code.cpp | 270 ++ example/doc_auto_unlink.cpp | 83 + example/doc_clone_from.cpp | 78 + example/doc_entity.cpp | 59 + example/doc_erasing_and_destroying.cpp | 73 + example/doc_how_to_use.cpp | 86 + example/doc_iterator_from_value.cpp | 94 + example/doc_list.cpp | 90 + example/doc_list_algorithms.cpp | 66 + example/doc_offset_ptr.cpp | 90 + example/doc_rbtree_algorithms.cpp | 82 + example/doc_set.cpp | 95 + example/doc_slist.cpp | 92 + example/doc_slist_algorithms.cpp | 60 + example/doc_unordered_set.cpp | 110 + example/doc_value_traits.cpp | 99 + example/doc_window.cpp | 85 + perf/Jamfile.v2 | 34 + perf/perf_list.cpp | 534 ++++ proj/vc7ide/Intrusive.sln | 71 + proj/vc7ide/Intrusive.vcproj | 129 + .../vc7ide/_intrusivelib/_intrusivelib.vcproj | 294 ++ proj/vc7ide/list/list.vcproj | 135 + proj/vc7ide/multiset/multiset.vcproj | 134 + proj/vc7ide/set/set.vcproj | 134 + proj/vc7ide/slist/slist.vcproj | 134 + .../unordered_multiset.vcproj | 134 + .../vc7ide/unordered_set/unordered_set.vcproj | 134 + test/Jamfile.v2 | 34 + test/common_functors.hpp | 43 + test/itestvalue.hpp | 338 +++ test/list_test.cpp | 376 +++ test/multiset_test.cpp | 317 +++ test/set_test.cpp | 315 +++ test/slist_test.cpp | 433 +++ test/smart_ptr.hpp | 355 +++ test/test_container.hpp | 227 ++ test/test_templates.hpp | 65 + test/unordered_multiset_test.cpp | 463 ++++ test/unordered_set_test.cpp | 438 +++ 60 files changed, 10043 insertions(+) create mode 100644 doc/Jamfile.v2 create mode 100644 doc/html/boostbook.css create mode 100644 doc/html/images/blank.png create mode 100644 doc/html/images/caution.png create mode 100644 doc/html/images/draft.png create mode 100644 doc/html/images/home.png create mode 100644 doc/html/images/important.png create mode 100644 doc/html/images/next.png create mode 100644 doc/html/images/note.png create mode 100644 doc/html/images/prev.png create mode 100644 doc/html/images/tip.png create mode 100644 doc/html/images/toc-blank.png create mode 100644 doc/html/images/toc-minus.png create mode 100644 doc/html/images/toc-plus.png create mode 100644 doc/html/images/up.png create mode 100644 doc/html/images/warning.png create mode 100644 doc/html/reference.css create mode 100644 doc/intrusive.qbk create mode 100644 example/Jamfile.v2 create mode 100644 example/doc_advanced_value_traits.cpp create mode 100644 example/doc_assoc_optimized_code.cpp create mode 100644 example/doc_auto_unlink.cpp create mode 100644 example/doc_clone_from.cpp create mode 100644 example/doc_entity.cpp create mode 100644 example/doc_erasing_and_destroying.cpp create mode 100644 example/doc_how_to_use.cpp create mode 100644 example/doc_iterator_from_value.cpp create mode 100644 example/doc_list.cpp create mode 100644 example/doc_list_algorithms.cpp create mode 100644 example/doc_offset_ptr.cpp create mode 100644 example/doc_rbtree_algorithms.cpp create mode 100644 example/doc_set.cpp create mode 100644 example/doc_slist.cpp create mode 100644 example/doc_slist_algorithms.cpp create mode 100644 example/doc_unordered_set.cpp create mode 100644 example/doc_value_traits.cpp create mode 100644 example/doc_window.cpp create mode 100644 perf/Jamfile.v2 create mode 100644 perf/perf_list.cpp create mode 100644 proj/vc7ide/Intrusive.sln create mode 100644 proj/vc7ide/Intrusive.vcproj create mode 100644 proj/vc7ide/_intrusivelib/_intrusivelib.vcproj create mode 100644 proj/vc7ide/list/list.vcproj create mode 100644 proj/vc7ide/multiset/multiset.vcproj create mode 100644 proj/vc7ide/set/set.vcproj create mode 100644 proj/vc7ide/slist/slist.vcproj create mode 100644 proj/vc7ide/unordered_multiset/unordered_multiset.vcproj create mode 100644 proj/vc7ide/unordered_set/unordered_set.vcproj create mode 100644 test/Jamfile.v2 create mode 100644 test/common_functors.hpp create mode 100644 test/itestvalue.hpp create mode 100644 test/list_test.cpp create mode 100644 test/multiset_test.cpp create mode 100644 test/set_test.cpp create mode 100644 test/slist_test.cpp create mode 100644 test/smart_ptr.hpp create mode 100644 test/test_container.hpp create mode 100644 test/test_templates.hpp create mode 100644 test/unordered_multiset_test.cpp create mode 100644 test/unordered_set_test.cpp diff --git a/doc/Jamfile.v2 b/doc/Jamfile.v2 new file mode 100644 index 0000000..8178ae4 --- /dev/null +++ b/doc/Jamfile.v2 @@ -0,0 +1,40 @@ +# Boost.Intrusive library documentation Jamfile +# +# Copyright Ion Gaztaņaga 2006. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) +# +# See http://www.boost.org/libs/intrusive for documentation. + + +project boost/intrusive/libs/doc ; + +import doxygen ; +import quickbook ; + +doxygen intrusive_doxygen + : + [ glob ../../../boost/intrusive/*.hpp ] + : + HIDE_UNDOC_MEMBERS=YES + HIDE_UNDOC_CLASSES=YES + EXTRACT_PRIVATE=NO + ENABLE_PREPROCESSING=YES + MACRO_EXPANSION=YES + EXPAND_ONLY_PREDEF=YES + SEARCH_INCLUDES=YES + ; + +xml intrusive_xml : intrusive.qbk ; + +boostbook standalone + : + intrusive_xml + intrusive_doxygen + : + boost.root=../../../.. + boost.libraries=../../../../libs/libraries.htm + generate.section.toc.level=3 + chunk.first.sections=1 + ; diff --git a/doc/html/boostbook.css b/doc/html/boostbook.css new file mode 100644 index 0000000..e816cce --- /dev/null +++ b/doc/html/boostbook.css @@ -0,0 +1,538 @@ +/*============================================================================= + Copyright (c) 2004 Joel de Guzman + http://spirit.sourceforge.net/ + + Use, modification and distribution is subject to the Boost Software + License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at + http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ + +/*============================================================================= + Body defaults +=============================================================================*/ + + body + { + margin: 1em; + font-family: sans-serif; + } + +/*============================================================================= + Paragraphs +=============================================================================*/ + + p + { + text-align: left; + font-size: 10pt; + line-height: 1.15; + } + +/*============================================================================= + Program listings +=============================================================================*/ + + /* Code on paragraphs */ + p tt.computeroutput + { + font-size: 10pt; + } + + pre.synopsis + { + font-size: 10pt; + margin: 1pc 4% 0pc 4%; + padding: 0.5pc 0.5pc 0.5pc 0.5pc; + } + + .programlisting, + .screen + { + font-size: 10pt; + display: block; + margin: 1pc 4% 0pc 4%; + padding: 0.5pc 0.5pc 0.5pc 0.5pc; + } + +/*============================================================================= + Headings +=============================================================================*/ + + h1, h2, h3, h4, h5, h6 + { + text-align: left; + margin: 1em 0em 0.5em 0em; + font-weight: bold; + } + + h1 { font: 140% } + h2 { font: bold 140% } + h3 { font: bold 130% } + h4 { font: bold 120% } + h5 { font: italic 110% } + h6 { font: italic 100% } + + /* Top page titles */ + title, + h1.title, + h2.title + h3.title, + h4.title, + h5.title, + h6.title, + .refentrytitle + { + font-weight: bold; + margin-bottom: 1pc; + } + + h1.title { font-size: 140% } + h2.title { font-size: 140% } + h3.title { font-size: 130% } + h4.title { font-size: 120% } + h5.title { font-size: 110% } + h6.title { font-size: 100% } + + .section h1 + { + margin: 0em 0em 0.5em 0em; + font-size: 140%; + } + + .section h2 { font-size: 140% } + .section h3 { font-size: 130% } + .section h4 { font-size: 120% } + .section h5 { font-size: 110% } + .section h6 { font-size: 100% } + + /* Code on titles */ + h1 tt.computeroutput { font-size: 140% } + h2 tt.computeroutput { font-size: 140% } + h3 tt.computeroutput { font-size: 130% } + h4 tt.computeroutput { font-size: 120% } + h5 tt.computeroutput { font-size: 110% } + h6 tt.computeroutput { font-size: 100% } + +/*============================================================================= + Author +=============================================================================*/ + + h3.author + { + font-size: 100% + } + +/*============================================================================= + Lists +=============================================================================*/ + + li + { + font-size: 10pt; + line-height: 1.3; + } + + /* Unordered lists */ + ul + { + text-align: left; + } + + /* Ordered lists */ + ol + { + text-align: left; + } + +/*============================================================================= + Links +=============================================================================*/ + + a + { + text-decoration: none; /* no underline */ + } + + a:hover + { + text-decoration: underline; + } + +/*============================================================================= + Spirit style navigation +=============================================================================*/ + + .spirit-nav + { + text-align: right; + } + + .spirit-nav a + { + color: white; + padding-left: 0.5em; + } + + .spirit-nav img + { + border-width: 0px; + } + +/*============================================================================= + Table of contents +=============================================================================*/ + + .toc + { + margin: 1pc 4% 0pc 4%; + padding: 0.1pc 1pc 0.1pc 1pc; + font-size: 10pt; + line-height: 1.15; + } + + .toc-main + { + width: 600; + text-align: center; + margin: 1pc 1pc 1pc 10%; + padding: 2pc 1pc 3pc 1pc; + line-height: 0.1; + } + + .boost-toc + { + float: right; + padding: 0.5pc; + } + +/*============================================================================= + Tables +=============================================================================*/ + + .table-title, + div.table p.title + { + margin-left: 4%; + padding-right: 0.5em; + padding-left: 0.5em; + } + + .informaltable table, + .table table + { + width: 92%; + margin-left: 4%; + margin-right: 4%; + } + + div.informaltable table, + div.table table + { + padding: 4px; + } + + /* Table Cells */ + div.informaltable table tr td, + div.table table tr td + { + padding: 0.5em; + text-align: left; + } + + div.informaltable table tr th, + div.table table tr th + { + padding: 0.5em 0.5em 0.5em 0.5em; + border: 1pt solid white; + font-size: 120%; + } + +/*============================================================================= + Blurbs +=============================================================================*/ + + div.note, + div.tip, + div.important, + div.caution, + div.warning, + p.blurb + { + font-size: 10pt; + line-height: 1.2; + display: block; + margin: 1pc 4% 0pc 4%; + padding: 0.5pc 0.5pc 0.5pc 0.5pc; + } + + p.blurb img + { + padding: 1pt; + } + +/*============================================================================= + Variable Lists +=============================================================================*/ + + span.term + { + font-weight: bold; + font-size: 10pt; + } + + div.variablelist table tbody tr td + { + text-align: left; + vertical-align: top; + padding: 0em 2em 0em 0em; + font-size: 10pt; + } + + div.variablelist table tbody tr td p + { + margin: 0em 0em 0.5em 0em; + } + + /* Make the terms in definition lists bold */ + div.variablelist dl dt + { + font-weight: bold; + font-size: 10pt; + } + + div.variablelist dl dd + { + margin: 1em 0em 1em 2em; + font-size: 10pt; + } + +/*============================================================================= + Misc +=============================================================================*/ + + /* Title of books and articles in bibliographies */ + span.title + { + font-style: italic; + } + + span.underline + { + text-decoration: underline; + } + + span.strikethrough + { + text-decoration: line-through; + } + + /* Copyright, Legal Notice */ + div div.legalnotice p + { + font-size: 8pt; + text-align: left + } + +/*============================================================================= + Colors +=============================================================================*/ + + @media screen + { + /* Links */ + a + { + color: #0C7445; + } + + a:visited + { + color: #663974; + } + + h1 a, h2 a, h3 a, h4 a, h5 a, h6 a, + h1 a:hover, h2 a:hover, h3 a:hover, h4 a:hover, h5 a:hover, h6 a:hover, + h1 a:visited, h2 a:visited, h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited + { + text-decoration: none; /* no underline */ + color: #000000; + } + + /* Syntax Highlighting */ + .keyword { color: #0000AA; } + .identifier { color: #000000; } + .special { color: #707070; } + .preprocessor { color: #402080; } + .char { color: teal; } + .comment { color: #800000; } + .string { color: teal; } + .number { color: teal; } + .white_bkd { background-color: #E8FBE9; } + .dk_grey_bkd { background-color: #A0DAAC; } + + /* Copyright, Legal Notice */ + .copyright + { + color: #666666; + font-size: small; + } + + div div.legalnotice p + { + color: #666666; + } + + /* Program listing */ + pre.synopsis + { + border: 1px solid #DCDCDC; + border-bottom: 3px solid #9D9D9D; + border-right: 3px solid #9D9D9D; + background-color: #FAFFFB; + } + + .programlisting, + .screen + { + border: 1px solid #DCDCDC; + border-bottom: 3px solid #9D9D9D; + border-right: 3px solid #9D9D9D; + background-color: #FAFFFB; + } + + /* Blurbs */ + div.note, + div.tip, + div.important, + div.caution, + div.warning, + p.blurb + { + border: 1px solid #DCDCDC; + border-bottom: 3px solid #9D9D9D; + border-right: 3px solid #9D9D9D; + background-color: #FAFFFB; + } + + /* Table of contents */ + .toc + { + border: 1px solid #DCDCDC; + border-bottom: 3px solid #9D9D9D; + border-right: 3px solid #9D9D9D; + background-color: #FAFFFB; + } + + /* Table of contents */ + .toc-main + { + border: 1px solid #DCDCDC; + border-bottom: 3px solid #9D9D9D; + border-right: 3px solid #9D9D9D; + background-color: #FAFFFB; + } + + + /* Tables */ + div.informaltable table tr td, + div.table table tr td + { + border: 1px solid #DCDCDC; + background-color: #FAFFFB; + } + + div.informaltable table tr th, + div.table table tr th + { + background-color: #E3F9E4; + border: 1px solid #DCDCDC; + } + + /* Misc */ + span.highlight + { + color: #00A000; + } + } + + @media print + { + /* Links */ + a + { + color: black; + } + + a:visited + { + color: black; + } + + .spirit-nav + { + display: none; + } + + /* Program listing */ + pre.synopsis + { + border: 1px solid gray; + background-color: #FAFFFB; + } + + .programlisting, + .screen + { + border: 1px solid gray; + background-color: #FAFFFB; + } + + /* Table of contents */ + .toc + { + border: 1px solid #DCDCDC; + border-bottom: 3px solid #9D9D9D; + border-right: 3px solid #9D9D9D; + background-color: #FAFFFB; + } + + /* Table of contents */ + .toc-main + { + border: 1px solid #DCDCDC; + border-bottom: 3px solid #9D9D9D; + border-right: 3px solid #9D9D9D; + background-color: #FAFFFB; + } + + .informaltable table, + .table table + { + border: 1px solid #DCDCDC; + border-bottom: 3px solid #9D9D9D; + border-right: 3px solid #9D9D9D; + border-collapse: collapse; + background-color: #FAFFFB; + } + + /* Tables */ + div.informaltable table tr td, + div.table table tr td + { + border: 1px solid #DCDCDC; + background-color: #FAFFFB; + } + + div.informaltable table tr th, + div.table table tr th + { + border: 1px solid #DCDCDC; + background-color: #FAFFFB; + } + + /* Misc */ + span.highlight + { + font-weight: bold; + } + } diff --git a/doc/html/images/blank.png b/doc/html/images/blank.png new file mode 100644 index 0000000000000000000000000000000000000000..764bf4f0c3bb4a09960b04b6fa9c9024bca703bc GIT binary patch literal 374 zcmeAS@N?(olHy`uVBq!ia0y~yU}ykg4kiW$2A`O3a~K#HSkfJR9T^xl_H+M9WMyDr z)b(_645^s&_M)R8g96V1g9H1Y`?|@Qa8+HmHda4(nQ?zM!!?FAj1kNm*b;aTNHv%; cj9>w=gf!;AOne?`3=9kmp00i_>zopr0Be8NW(e>Jab;j&;NV~o5MYpy zU{F+KFf?Rva$<;zVn|MA$j)XcE@r5%W@u?)XlW_#>0#*UDemd%nKFf8%9P?MQ>y38 zVVEwkZjIWyHF@jSt$X(}?A@Du?i|Cp zbLXyIW4Lzh+_h`h?%iX!chB(NJdh*`E$$X&!4}4&+z{J`|sZwzJC|^ z{$1kxcf;@BzyJTw@c+NS|Nj#IN5NBISn~R-X--a%afBxQ|J!3zMjr_SU zk_iHr)f*lf{$5^Qz}I)@3FlWvw(w~u=1P@VsTP+$RNGvxbHL-(%M6nc6`{zlU zjGQJeveps+!&Jb&mD)L@hA} z1_tL6*NBqf{Irtt#G+IN2MuLS&)mfHRNut(%;anZ6Fnn63k6F{eFF=914D)6qRirw zN{8Ia;*!i{z0_j8l+uFyyb`_S{M?DV6n8K%Fld2|%S_KpEGaEYWk@zRFt#waFg8d` zG)YZPF-fe)lBATd3a!N{b-$VA&f+n^|#(~yCI Ofx*+&&t;ucLK6T%G-N*j literal 0 HcmV?d00001 diff --git a/doc/html/images/draft.png b/doc/html/images/draft.png new file mode 100644 index 0000000000000000000000000000000000000000..0084708c9b8287c51efa6b40b8d492854191455e GIT binary patch literal 17454 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 YsPxZ;53A2HFfcH9y85}Sb4q9e0Fi|iga7~l literal 0 HcmV?d00001 diff --git a/doc/html/images/home.png b/doc/html/images/home.png new file mode 100644 index 0000000000000000000000000000000000000000..5584aacb097a80e66a5320312b6e4eb017af1a06 GIT binary patch literal 358 zcmeAS@N?(olHy`uVBq!ia0y~yU=Rjj7G?$phK4%r{|pQa%*9TgAsieWw;%dHU|?WS z3GfMV6&Dfg?(A@OuseF>a6(*+nzF*onKLh6zO-%YmOy{sw6wJU|Nk%gvq74Hfq|za z$S?Rm0x$^OKX;CSfq}EYBeIx*fm;ZK886+f`@_J%pjzS@Q4*Y=R#Ki=l*-_nm|T>f zo0^iDsNj}alvU8YOY t9}F9JU6`43jMG5vNQA&8NcoN_f;wr$vARr(hAt9lu zscC6x>EvV>6{VS+EaBwj6ciMco$ZvI98_E!l$@NLot<4>UER|o(bHqOcQ41TYc{y!?kM?_wFg)yJvXsp5^oB z9Pi&Vyniq7|3Ab3{~Z7S3p{_W`TV)z`}cpO z$(;G%_wGG* z?AW<;=dNA5cJJQ3=g*(NfB*jb_wWDz|6hCQ@I3|w2F4_BcNgdM3%p4T42R|DNig) zWpL0?*7VFxOi%SqOwUZtRxr^s(z8&owA44S&^IttNG{4OE~#|Ltt>9dOx8;+)=McZ z$j>X$OU}=oxJz*d0|SE=*tpE}yu^~yqEv=t literal 0 HcmV?d00001 diff --git a/doc/html/images/next.png b/doc/html/images/next.png new file mode 100644 index 0000000000000000000000000000000000000000..59800b4e87f60c0e3383ede2b384b9be0f5ffe8d GIT binary patch literal 336 zcmeAS@N?(olHy`uVBq!ia0y~yU=Rjj7G?$phK4%r{|pQa%*9TgAsieWw;%dHU|?WS z3GfMV6&Dfg?(8r&Hr}>%OQ656nzF)*4nJa0`Jj)#l9-t%+}PK^d+g590~2^trx_V+aGYt)W#Kgko@Q{~>i6>w}LxPb)_bi1gN;4a>^d{wcuv&t)U99M06BF~bt=lC3t1z!y3OU-_; z>3=1k>J3(3i_72APIz-IDqpqc+E%+vGv;(%KfZ!%@4A+C&xmZ75-RagX8L}A{%x+r zX<~gz))PX{awr=ezabJ<%O$qq%HpE?3}IarYhrI#g}e)4`)(-lYr?KO{@fm?UzpsD z&F7x?_G;CcbIZ>^o0GCAMe@{JfwtZgS9s0dn=t$|`IrrC3yU6#%a-U6G$wZz0 z>m`@($9HNPdGJ4#pEvb;3eT@>Ck6%v=MvY5lHmNblJdl&R0anPWlhiA#Pn3(#PrPM zYy}fNBRvZROG|wN3w;Aah2)~l;*v^-+{)sT%w)aPV!f2og8aM^z2yAdiMtecFfcG^ zfsM;d&r2*RElOoDPD(L1F;6y4H8Hg?FgGwTOER!DNJ%kHHBU4$OExhPxjgw70|Nse jNLN5&dMbmFNrjP#wt==mQ8cF^C=xwg{an^LB{Ts5w*0vf literal 0 HcmV?d00001 diff --git a/doc/html/images/prev.png b/doc/html/images/prev.png new file mode 100644 index 0000000000000000000000000000000000000000..d88a40f923e3c554125f01cd366707c60cfcad04 GIT binary patch literal 334 zcmeAS@N?(olHy`uVBq!ia0y~yU=Rjj7G?$phK4%r{|pQa%*9TgAsieWw;%dHU|?WS z3GfMV6&DdqOG`60Hr}>%%ZlYo1O0u~loc*tzSP~>;p||S5Et|R|Noh1c$yg)73T~N2spa`a*~JRJ5eh~I1}5!gYtAz;Fo=OPI2WZRmSpDVDTHL^rZN~B=o=X8 z8<-ql-^0nkz!2u?;uumfC;0|1OPoRyGxLNShYX~Tl_Wf9G1_imu)%RA9}mw<0X2^e zQioc&m}WXSvRw^OFi2qFa&lm1W^U?K=~^Ook|m{hVvche^Q6-g?(V)Vn8U=toEqFE UkjD9gfq{X+)78&qol`;+00?PtqyPW_ literal 0 HcmV?d00001 diff --git a/doc/html/images/tip.png b/doc/html/images/tip.png new file mode 100644 index 0000000000000000000000000000000000000000..5c4aab3bb3543191c360387c4af9a3cbaa051345 GIT binary patch literal 449 zcmeAS@N?(olHy`uVBq!ia0y~yV31^BV36QoU|?X-y2mNSz`($iDJ87yAgr${wU6ATeb^ycE^w7cIp z?_W?xU6%d(bb~@Shbi-aG=ETBbm~(ogOmrW&bwBIxdt+K+A3yD;V_t}R>0LF5GN)x zed{W(mq$!1ciVpPy1}W+9bm)Xrgda~bSbG?a3DdjluYV+O9SdLnMpASqX+9EZY3c7C@7#N&OTq83NAIrA4U>si~GJ7Ut&0mS)MRMrP)TsYwQg=9VeOrfC+2Nk)mu rLdL(;85kJ&K)M1F(^DCYOe>5`v<1B4~iR4S3j3^P6tl1hi%%HopDWWCg4y_C{| z{JavqiARt`YJFJnVhX)qGzOMplv!L->5yAl zT#}irms+fsQd*FoSE84kpF44v;tmD|1}(60ndy0nC8b5F42emG<`xDZ$;L?r7Ky3J=GDKJX@I;2(iM=Hp2}coT4AYeplx7Y_5oxZgQu&X%Q~lo FCIGT9P(J_w literal 0 HcmV?d00001 diff --git a/doc/html/images/up.png b/doc/html/images/up.png new file mode 100644 index 0000000000000000000000000000000000000000..17d9c3ec491ae1ba22188ce85985623c92ffa9be GIT binary patch literal 370 zcmeAS@N?(olHy`uVBq!ia0y~yU=Rjj7G?$phK4%r{|pQa%*9TgAsieWw;%dHU|?X- z3h)VW6&DdqOG|Thu-mqE%ZlYoyE{8BU%nLR@2jS)FmvY2qel)W#Kk;%^zi@x|I?Un z*fKCM@RbDl1^-6|46X<6oM2#J;4JWnEM{Qf76M_$OLy!3FfcHvmbgZg1m~xflqVLY zGWaGY7v<-srer26xMdclmgg5`7c2NiC>R+Sn6#IzInThrAO_OlT$Gwvl9`{U5R#dj z%3x@qZ(yu%U~+tY4<`cyLy@P8V@SoEspmFwHW&!FJyeg_(XezvV9WvAI|r@_>dZZG zPW6aiOT!J--9O?NG0%AP;}ge|4lDQN4=-}8`?JGwx}?mMnO)OdyQdu$nQCjPRV}jm z$u!Qa8E-cQ-r3Nz>Y(YPTd#BPEH+&8GWqfD!}4*53%dA!%#3$cIv;a~fq{X+)78&q Iol`;+0POUaApigX literal 0 HcmV?d00001 diff --git a/doc/html/images/warning.png b/doc/html/images/warning.png new file mode 100644 index 0000000000000000000000000000000000000000..1c33db8f34a8b42b373179b46a2d8d8a10e061a9 GIT binary patch literal 1241 zcmeAS@N?(olHy`uVBq!ia0y~yV2}V|4rT@hhU+WOo?>8NW(e>JaphoO5CF?5GB9W| zFc>m0I59AIF)#!%FhnshWHT@nGcZ&$Ftji*^e`|?VPKe2T|Fl#Xiikroa*YO3=B&x zEth(EEp2I8I%UezrAyZ`FswB+TsvjTRtAQxnwndCdbZA)vvujxty{P5WnkF5cJ1D+ zTaPg?91{>YCLwX`*s*gA4Ce#{&Phm|)6~4iz;I1d^V+p*_ZS%N-Mjakf#JEL;`8Uv z-!m}0=iqq%{{43bhVS3M|7T$MKMF=efJz}yVEuRs0|NtNlDE4HLkFv@2Ll7c3r`ov zkcwNmlWOyu3izvS7ejxP>R-!INP5f(XN|IS^C^Iyp?`SUk1vQO?s(K&l| zi|Nkt0@~*ymDp65*E-HED6u(s{Mfrxmah{JrgAMTIq)Du?nC5nnYTRgThA|azEdIl zD^uvV>~q(b?>`Fd;xnAbe7so1I$-&keKN}|vNNOCvX<~g{)wp7{&hR__v^cBU*Gq* zV3YS!cBPWsl#eNWc|~nAXWMOB8tQWBuXo=4>}cytyX_5F^Az{bVJ>7~U~n#RjVKAu zPb(=;EJ|f?&`{R&%uP&B^-WCAOwLv?(KFJsP_VSrH?Yt*FjPn`$}BFabjYnNF3C*R zOD)z*DJ{s)E742N&z-nSaR&nfgBIAh%=Em(lG377hGY|?B=Z!b6jL*k#Ka_13kwTN zLrZf@a|7cv1EVApQ-8txlNlHo_&~Y>64O%|j7%zwOtcNO4T_>U4H+017(8A5T-G@y GGywozG)2h( literal 0 HcmV?d00001 diff --git a/doc/html/reference.css b/doc/html/reference.css new file mode 100644 index 0000000..be4e64c --- /dev/null +++ b/doc/html/reference.css @@ -0,0 +1,5 @@ +PRE.synopsis { + background-color: #e0ffff; + border: thin solid blue; + padding: 1em +} diff --git a/doc/intrusive.qbk b/doc/intrusive.qbk new file mode 100644 index 0000000..9065b99 --- /dev/null +++ b/doc/intrusive.qbk @@ -0,0 +1,2440 @@ +[library Intrusive + [quickbook 1.3] + [version 2007-05-03] + [authors [Krzikalla, Olaf], [Gaztañaga, Ion]] + [copyright 2005 Olaf Krzikalla, 2006-2007 Ion Gaztañaga] + [purpose Intrusive containers] + [license + Distributed under the Boost Software License, Version 1.0. + (See accompanying file LICENSE_1_0.txt or copy at + [@http://www.boost.org/LICENSE_1_0.txt]) + ] +] + +[section:introduction Introduction] + +[section:introduction_presenting Presenting Boost.Intrusive] + +[*Boost.Intrusive] is a library presenting some intrusive containers to +the world of C++. Intrusive containers are special containers +that offer [link intrusive.performance better performance] +and exception safety guarantees than non-intrusive containers (like STL containers). + +The performance benefits of intrusive containers makes them ideal as a building +block to efficiently construct complex containers like multi-index containers or +to design high performance code like memory allocation algorithms. + +While intrusive containers were and are widely used in C, they +became more and more forgotten in C++ due to the presence of the standard +containers which don't support intrusive techniques.[*Boost.Intrusive] not only +reintroduces this technique to C++, but also encapsulates the implementation in +STL-like interfaces. Hence anyone familiar with standard containers can easily use +[*Boost.Intrusive]. + +[endsect] + +[section:introduction_building_intrusive Building Boost.Intrusive] + +There is no need to compile anything to use [*Boost.Intrusive], since it's +a header only library. Just include your Boost header directory in your +compiler include path. + +[endsect] + +[endsect] + +[section:intrusive_vs_nontrusive Intrusive and non-intrusive containers] + +[section:differences_intrusive_vs_nontrusive Differences between intrusive and non-intrusive containers] + +The main difference between intrusive containers and non-intrusive containers is +that in C++ non-intrusive containers store [*copies] of values passed by the user. +Containers use the `Allocator` template parameter to allocate the stored values: + +[c++] + + #include + #include + + int main() + { + std::list myclass_list; + + MyClass myclass(...); + myclass_list.push_back(myclass); + + //The stored object is different from the original object + assert(&myclass != &myclass_list.front()); + return 0; + } + + +To store the newly allocated copy of `myclass`, the container needs additional +data: `std::list` usually allocates nodes that contain pointers to the +next and previous node and the value itself. Something similar to: + +[c++] + + //A possible implementation of a std::list node + class list_node + { + list_node *next; + list_node *previous; + MyClass value; + }; + + +On the other hand, an intrusive container does not store copies of passed objects, +but it stores the objects themselves. The additional data needed to insert the object +in the container must be provided by the object itself. For example, to insert `MyClass` +in an intrusive container that implements a linked list, `MyClass` must contain the +needed ['next] and ['previous] pointers: + +[c++] + + class MyClass + { + MyClass *next; + MyClass *previous; + //Other members... + }; + + int main() + { + acme_intrusive_list list; + + MyClass myclass; + list.push_back(myclass); + + //"myclass" object is stored in the list + assert(&myclass == &list.front()); + return 0; + } + +As we can see, knowing which additional data the class should contain is not +an easy task. [*Boost.Intrusive] offers several intrusive containers and an easy +way to make user classes compatible with those containers. + +[endsect] + +[section:properties_of_intrusive Properties of Boost.Intrusive containers] + +Semantically, a [*Boost.Intrusive] container is similar to an STL container +holding pointers to objects. That is, if you have an intrusive list holding +objects of type `T`, then `std::list` would allow you to do quite the +same operations (maintaining and navigating a set of objects of type T and +types derived from it). + +A non-intrusive container has some limitations: + +* An object can only belong to one container: If you want to share an object + between two containers, you either have to store multiple copies of those + objects or you need to use containers of pointers: `std::list`. + +* The use of dynamic allocation to create copies of passed values can be a performance + and size bottleneck in some applications. Normally, dynamic allocation imposes + a size overhead for each allocation to store bookeeping information and a + synchronization to protected concurrent allocation from different threads. + +* Only copies of objects are stored in non-intrusive containers. Hence copy + or move constructors and copy or move assignment operators are required. Non-copyable + and non-movable objects can't be stored in non-intrusive containers. + +* It's not possible to store a derived object in a STL-container while + retaining its original type. + +Intrusive containers have some important advantages: + +* Operating with intrusive containers doesn't invoke any memory management at all. + The time and size overhead associated with dynamic memory can be minimized. + +* Iterating an Intrusive container needs less memory accesses than the semantically + equivalent container of pointers: iteration is faster. + +* Intrusive containers offer better exception guarantees than non-intrusive containers. + In some situation intrusives containers offer a no-throw guarantee that can't be + achieved with non-intrusive containers. + +* The computation of an iterator to an element from a pointer or reference to that element + is a constant time operation (computing the position of `T*` in a `std::list` has + linear complexity). + +* Intrusive containers offer predictability when inserting and erasing objects since no + memory managed is done with intrusive containers. Memory management usually is not a predicable + operation so complexity guarantees from non-intrusive containers are looser than the guarantees + offered by intrusive containers. + +Intrusive containers have also downsides: + +* Each type stored in an intrusive container needs additional memory holding the + maintenance information needed by the container. Hence, whenever a certain type shall + be stored in an intrusive container [*you have to change the definition of that type] + appropriately. Although this task is easy with [*Boost.Intrusive], touching the + definition of a type is sometimes a crucial issue. + +* In intrusive containers you don't store a copy of an object, [*but they rather the origina object + is linked with other objects in the container]. Objects don't need copy-constructors or assignment + operators to be stored in intrusive containers. But you have to take care of possible side effects, + whenever you change the contents of an object (this is especially important for + associative containers). + +* The user [*has to manage the lifetime of inserted objects] independently from the + containers. + +* Again you have to be [*careful]: in contrast to STL containers [*it's easy to render an + iterator invalid] without touching the intrusive container directly, because the object + can be destroyed before is erased from the container. + +* [*Boost.Intrusive] containers are [*non-copyable and non-assignable]. Since intrusive + containers don't have allocation capabilities, these operations have no sense. However, + swapping can be used to implement move-capabilities. To ease the implementation of + copy constructors and assignment operators of classes storing [*Boost.Intrusive] + containers, [*Boost.Intrusive] offers special cloning functions. See + [link intrusive.clone_from Cloning [*Boost.Intrusive] containers] section for more information. + +* Analyzing thread-safety of a program that uses containers is harder with intrusive containers, becuase + the container might be modified indirectly without an explicitly call to a container member. + +[table Summay of intrusive containers advantages and disadvantages + [[Issue] [Intrusive] [Non-intrusive]] + [[Memory management] [External] [Internal through allocator]] + [[Insertion/Erasure time] [Faster] [Slower]] + [[Memory locality] [Better] [Worse]] + [[Can hold non-copyable and non-movable objects by value] [Yes] [No]] + [[Exception guarantees] [Better] [Worse]] + [[Computation of iterator from value] [Constant] [Non-constant]] + [[Insertion/erasure predictability] [High] [Low]] + [[Memory use] [Minimal] [More than minimal]] + [[Insert objects by value retaining polymorphic behavior] [Yes] [No (slicing)]] + [[User must modify the definition of the values to insert] [Yes] [No]] + [[Containers are copyable] [No] [Yes]] + [[Inserted object's lifetime managed by] [User (more complex)] [Container (less complex)]] + [[Container invariants can be broken without using the container] [Easier] [Harder (only with containers of pointers)]] + [[Thread-safety analysis] [Harder] [Easier]] +] + +For a performance comparison between Intrusive and Non-intrusive containers see +[link intrusive.performance Performance] section. + +[endsect] + +[endsect] + +[/ +[section:quick_guide Quick guide for the impatient] + +[endsect] +] + +[section:usage How to use Boost.Intrusive] + +If you plan to use a class in an intrusive container, you have to make some decisions +influencing the class definition itself. Each class that will be used in an intrusive +container needs some appropriate data members storing the information needed by the +container. We will take a simple intrusive container, like an intrusive list +([classref boost::intrusive::list boost::intrusive::list]) for the following +examples, but all [*Boost.Intrusive] containers are very similar. To compile +the example using [classref boost::intrusive::list boost::intrusive::list], +just include: + +[c++] + + #include + +Every class to be inserted in an intrusive container, needs to contain a hook that +will offer the necessary data and resources to be insertable in the container. +With [*Boost.Intrusive] you just choose the hook to be a public base class or +a public member of the class to be inserted. + +[section:usage_base_hook Using base hooks] + +For [classref boost::intrusive::list list], you can publicly derive from +[classref boost::intrusive::list_base_hook list_base_hook]. This class takes +three template arguments: + +[c++] + + template < class Tag = tag + , linking_policy Policy = safe_link + , class VoidPointer = void *> + class list_base_hook; + +* The first template argument serves as a tag, so you can derive from more than one + [classref boost::intrusive::list_base_hook list_base_hook] and hence put an object in + multiple intrusive lists at the same time. An incomplete type can serve as a tag. + +* The second template argument controls the linking policy. [*Boost.Intrusive] currently supports + 3 policies: `normal_link`, `safe_link`, `auto_unlink`. More about these in the sections + [link intrusive.safe_hook Safe hooks] and [link intrusive.auto_unlink_hooks Auto-unlink hooks] + +* The third template argument is the pointer type to be used internally in the hook. + The default value is `void *`, which means that raw pointers will be used in the hook. + More about this in the section titled [link intrusive.using_smart_pointers Using smart pointers with Boost.Intrusive containers] + +Example: + +[c++] + + #include + + using namespace boost::intrusive; + + class Foo : public list_base_hook<> + { /**/ }; + +Once we derive our class from `list_base_hook<>` we have to obtain the `ValueTraits` +information to configure the intrusive list. `ValueTraits` tell the container +the needed information to insert the object in the container (if the hook is +a base or member object, whether is an auto-unlink hook... +To obtain the needed value traits, just use the `value_traits` subtype +[classref boost::intrusive::list_base_hook list_base_hook] +defines passing the type of the user class as an argument: + +[c++] + + typedef list_base_hook<>::value_traits + FooValueTraits; + +After that, we can define the intrusive list. The intrusive list has the following +template parameters: + +[c++] + + template < class ValueTraits + , bool ConstantTimeSize = true + , class SizeType = std::size_t> + class list; + +* The first template argument is the value traits class. Contains information about the value + to be inserted: the type, the type of hook, the type of the pointers to be used, + whether the safe mode is being used... + +* The second boolean template argument specifies if a constant time `size()` + function is demanded. This will tell the intrusive container to insert an + additional member in the intrusive container that offers this information. + +* The third template argument specifies the type that will hold + the size of the container. This type will be the type returned by `list.size()` + and the type stored in the intrusive container if `ConstantTimeSize` is requested. + The user normally will not need to change this type, but some + containers can have a `size_type` that might be different from std::size_t + (for example, STL-like containers, use the `size_type` defined by their allocator). + [*Boost.Intrusive] can be used to implement such containers specifying the + `SizeType` type. + +Example of a constant-time size intrusive list that will store Foo objects: + +[c++] + + typedef list FooList; + +Now we can just use the container: + +[c++] + + //An object to be inserted in the list + Foo foo_object; + FooList list; + + list.push_back(object); + + assert(&list.front() == &foo_object); + +[endsect] + +[section:usage_member_hook Using member hooks] + +Sometimes an 'is-a' relationship between list hooks and the list value types +is not desirable. In this case, using a member hook as a data member instead of +'disturbing' the hierarchy might be the right way: you can add a public data +member `list_member_hook<...>` to your class. +This class takes two template parameters: + +[c++] + + template < linking_policy Policy = safe_link + , class VoidPointer = void *> + class list_member_hook; + +Example: + +[c++] + + #include + + class Foo + { + public: + list_member_hook<> m_hook_; + //... + }; + +To obtain the `ValueTraits` information to configure the list, use the internal +templatized `value_traits` type and pass the class to be inserted and a pointer +to the member hook as template parameters. + +[c++] + + //Obtain ValueTraits to configure the list + typedef list_member_hook<>::value_traits + FooValueTraits; + + //This list will use the member hook + typedef list FooList; + +Now we can just use the container: + +[c++] + + //An object to be inserted in the list + Foo foo_object; + FooList list; + + list.push_back(object); + + assert(&list.front() == &foo_object); + +[endsect] + +[section:usage_both_hooks Using both hooks] + +You can insert the same object in several intrusive containers at the same time, just +using one hook for each containers. This is a full example using base and member hooks: + +[import ../example/doc_how_to_use.cpp] +[doc_how_to_use_code] + +[endsect] + +[section:usage_lifetime Object lifetime] + +Even if the interface of [classref boost::intrusive::list list] is similar to +`std::list`, its usage is a bit different: You always have to keep in mind that +you directly store objects in intrusive containers, not copies. The lifetime of a +stored object is not bound to or managed by the container: + +* When the container gets destroyed before the object, the object is not destroyed, + so you have to be careful to avoid resource leaks. + +* When the object is destroyed before the container, your program is likely to crash, + because the container contains a pointer to an non-existing object. + +[endsect] + +[endsect] + +[section:usage_when When to use?] + +Intrusive containers can be used for highly optimized algorithms, where speed is a crucial +issue and... + +* additional memory management should be avoided. +* the programmer needs to efficiently track the construction and destruction of objects. +* exception safety, especially the no-throw guarantee, is needed. +* the computation of an iterator to an element from a pointer or reference + to that element should be a constant time operation. +* it's important to achieve a well-known worst-time system response. +* localization of data (e.g. for cache hit optimization) leads to measureable effects. + +The last point is important if you have a lot of containers over a set of elements. E.g. if +you have a vector of objects (say, `std::vector`) and you also have a list +storing a subset of those objects (`std::list`), then operating on an Object +from the list iterator (`std::list::iterator`) needs two steps: + +* Access from the iterator (usually on the stack) to the list node storing a pointer to `Object`. +* Access from the pointer to `Object` to the Object stored in the vector. + +While the objects themselves are tightly packed in the memory of the vector +(vector's memory is guaranteed to be contiguous), and form something +like a data block, list nodes can stay dispersed in the heap memory. +Hence depending on your system you can get a lot of cache misses. The same doesn't hold +for an intrusive list. Indeed, dereferencing an an iterator from an intrusive list is performed in +the same two steps as described above. But the list node is already embedded in the Object, so +the memory is directly tracked from the iterator to the Object. + +It's also possible to use intrusive containers when the objects to be stored can +have different or unknown size. This allows storing base and derived objects +in the same container as shown in the following example: + +[import ../example/doc_window.cpp] +[doc_window_code] + +Due to certain properties of intrusive containers +they are often more difficult to use than their STL-counterparts. That's why you +should avoid them in public interfaces of libraries. Classes to be stored in intrusive +containers must change their implementation to store the hook and this is not always +posible or desirable. + +[endsect] + +[section:concepts_summary Concept summary] + +Here is a small summary of the basic concepts that will be used in the following +chapters: + +[variablelist Brief Concepts Summary +[[Node Algorithms][A class containing typedefs and static functions that define + basic operations that can be applied to a groups of nodes. It's independent + from the node definition, and it's configured taking a NodeTraits template + parameter that describes the node.]] +[[Node Traits][A class that stores basic information and operations to insert a node in a group of nodes.]] +[[Hook][A class that a user must add as a base class or as a member to make the user class compatible with intrusive containers.]] +[[Intrusive Container][A class that stores user classes that have the needed hooks. It takes a ValueTraits template parameter as configuration information.]] +[[Pseudo-Intrusive Container][Similar to an intrusive container but a pseudo-intrusive container needs additional memory (e.g. an auxiliary array) to work.]] +[[Value Traits][A class containing typedefs and operations to obtain the node to be used by Node Algorithms from the user class and the inverse.]] +] + +[endsect] + +[section:presenting_containers Presenting Boost.Intrusive containers] + +[*Boost.Intrusive] offers a wide range of intrusive containers: + +* [*slist]: An intrusive singly linked list. The size overhead is very small + for user classes (usually the size of one pointer) but many operations have linear + time complexity, so the user must be careful if he wants to avoid performance problems. + +* [*list]: A `std::list` like intrusive linked list. The size overhead is quite + small for user classes (usually the size of two pointers). Many operations have + constant time complexity. + +* [*set/multiset]: A `std::set/std::multiset` like intrusive associative containers. + The size overhead is moderate for user classes (usually the size of three pointers). + Many operations have logarithmic time complexity. + +[*Boost.Intrusive] also offers pseudo-intrusive containers: + +* [*unordered_set/unordered_multiset]: A `std::tr1::unordered_set/std::tr1::unordered_multiset` + like intrusive unordered associative containers. + The size overhead is moderate for user classes (an average of two pointers per element). + Many operations have an amortized constant time complexity. + +Each of these intrusive containers can be configured with constant or linear time +size: + +* [*Linear time size]: The intrusive container doesn't hold a size member that it's +updated with every insertion/erasure. This implies that the `size()` function has not constant +time complexity. On the other hand, the container is smaller, and some operations, like +`splice()` taking a range of iterators in linked lists have constant time complexity +instead of linear complexity. + +* [*Constant time size]: The intrusive container holds a size member that it's updated +with every insertion/erasure. This implies that the `size()` function has constant time +complexity. On the other hand, increases the size of the container, and some operations, +like `splice()` taking a range of iterators, have linear time complexity in linked lists. + +To make user classes compatible with these intrusive containers [*Boost.Intrusive] +offers two types of hooks for each container type: + +* [*Base hook]: The hook is stored as a public base class of the user class. + +* [*Member hook]: The hook is stored as a public member of the user class. + +Apart from that, [*Boost.Intrusive] offers additional features: + +* [*Safe mode hooks]: Hook constructor initializes the internal data to a well-known + safe state and intrusive containers check that state before inserting a value in the + container. When erasing an element from the container, the container puts the hook + in the safe state again. This allows a safer use mode and it can be used to detect + programming errors. It implies an slight performance overhead in some operations + and can convert some constant time operations in linear time operations. + +* [*Auto-unlink hooks]: The hook destructor removes the object from the container + automatically and the user can safely unlink the object from the container without + having any reference to the container. + +* [*Non-raw pointers]: If the user wants to use smart pointers instead of raw pointers, + [*Boost.Intrusive] hooks can + be configured to use any type of pointers. This configuration information is also + transmitted to the containers, so all the internal pointers used by intrusive containers + configured with these hooks will be smart pointers. As an example, + [*Boost.Interprocess] defines an smart pointer compatible with shared memory, + called `offset_ptr`. [*Boost.Intrusive] can be configured to use this smart pointer + to allow shared memory intrusive containers. + +[endsect] + +[section:safe_hook Safe hooks] + +[*Boost.Intrusive] hooks can be configured to operate in safe-link mode. +The safe mode is activated by default: + +[c++] + + template + class list_base_hook; + +Thanks to the safe-mode the user can detect without any external reference, if the object +is actually inserted in any container. Let's review the basic features of the safe-mode: + +* Hooks' constructor puts the hook in a well-known default state. + +* Hooks' destructor checks if the hook is in the well-known default state. If not, + an assertion is raised. + +* Every time an object is being inserted in the intrusive container, the container + checks if the hook is the well-known default state. If not, + an assertion is raised. + +* Every time an object is being erased from the intrusive container, the container + puts the erased object in the well-known default state. + +With these features, without any external reference the user can know if the object +has been inserted in a container calling the `is_linked()` member function. +If the object is not actually inserted +in a container, the hook is the default state and if it's inserted in a container, the +hook is not in the default state. + +[*Note:] All assertions raised by [*Boost-Intrusive] hooks and containers, are +implemented using `BOOST_ASSERT`, which can be configured by the user. See +[@http://www.boost.org/libs/utility/assert.html] for more information about +`BOOST_ASSERT`. + +(to-do example!!) + +[endsect] + +[section:auto_unlink_hooks Auto-unlink hooks] + +[section:auto_unlink_hooks_what What's an auto-unlink hook?] + +[*Boost.Intrusive] offers additional hooks with unique features: + +* When the destructor of the hook is called, the hook checks if the node is inserted + in a container. If so, the hook removes the node from the container. +* The hook has a member function called `unlink()` that can be used to unlink the + node from the container at any moment, without having any reference to the container, + if the user want to do so. + +These hooks have exactly the same size overhead as their analogue non auto-unlinking +hooks, but they have a restriction: they can only be used with +[link intrusive.presenting_containers non-constant time containers]. +There is a reason for this: + +* Auto-unlink hooks don't store any reference to the container where they are inserted. +* Only containers with non constant-time `size()` allow removing an object from the container + without using any reference to the container. + +This auto-unlink feature is useful in certain applications +but it must be used [*very carefuly]: + +* If several threads are using the same container the destructor of the auto-unlink + hook will be called without any thread synchronization so removing the object is + thread-unsafe. + +* Container contents change silently without modifying the container directly. + This can lead to surprising effects. + +These auto-unlink hooks have also safe-mode properties: + +* Hooks' constructors put the hook in a well-known default state. + +* Every time an object is being inserted in the intrusive container, the container + checks if the hook is the well-known default state. If not, + an assertion is raised. + +* Every time an object is being erased from the intrusive container, the container + puts the erased object in the well-known default state. + +[endsect] + +[section:auto_unlink_hooks_example Auto-unlink hook example] + +Let's see an example of an auto-unlink hook: + +[import ../example/doc_auto_unlink.cpp] +[doc_auto_unlink_code] + +[endsect] + +[section:auto_unlink_and_constant_time Auto-unlink hooks and containers with constant-time `size()`] + +As explained, [*Boost.Intrusive] auto-unlink hooks are incompatible with containers +that have constant-time `size()`, so if you try to define such container with an +auto-unlink hook's value_traits, you will get an static assertion: + +[c++] + + #include + + struct MyTag; + + class MyClass : public boost::intrusive::list_base_hook + {/**/}; + + boost::intrusive::list , true> bad_list; + + int main() + { + bad_list list; + return 0; + } + +leads to an error similar to: + +[pre + error : use of undefined type 'boost::STATIC_ASSERTION_FAILURE' +] + + +Pointing to code like this: + +[c++] + + //Constant-time size is incompatible with auto-unlink hooks! + BOOST_STATIC_ASSERT(!(ConstantTimeSize && ((int)ValueTraits::linking_policy == (int)auto_unlink))); + +This way, there is no way to compile a program if you try to use auto-unlink hooks +in constant-time size containers. + +[endsect] + +[endsect] + +[section:slist Intrusive singly linked list: slist] + +[classref boost::intrusive::slist slist] is the simplest intrusive container of +[*Boost.Intrusive]: a singly linked list. The memory overhead +that imposes is 1 pointer per node. The size of an empty, non constant-time size +[classref boost::intrusive::slist slist], is the size of 1 pointer. This +lightweight memory overhead comes with its drawbacks, though: many operations have +linear time complexity, even some that usually are constant time, like +[classref boost::intrusive::slist::swap swap]. [classref boost::intrusive::slist slist] +only provides forward iterators. + +For most cases, a doubly linked list is preferrable because it offers more +constant-time functions with a slightly bigger overhead. +However, for some applications like +constructing more elaborated containers, singly linked lists are essential +because of their low size overhead. + +[section:slist_hooks slist hooks] + +Like the rest of [*Boost.Intrusive] containers, [classref boost::intrusive::slist slist] has two hook types: + +[c++] + + template + class slist_base_hook; + +* [classref boost::intrusive::slist_base_hook slist_base_hook]: + the user class derives publicly from + [classref boost::intrusive::slist_base_hook slist_base_hook] to make + it [classref boost::intrusive::slist slist]-compatible. + +[c++] + + template + class slist_member_hook; + +* [classref boost::intrusive::slist_member_hook slist_member_hook]: + the user class contains a public + [classref boost::intrusive::slist_member_hook slist_member_hook] to make + it [classref boost::intrusive::slist slist]-compatible. + +[endsect] + +[section:slist_container slist container] + +[classref boost::intrusive::slist slist] receives 3 template parameters: + +[c++] + + template + class slist; + +* The first template is the value traits class. Contains information about the + value to be inserted: the type, the type of hook, the type of the pointers to + be used, whether the safe mode is being used... + +* The second boolean template argument specifies if a constant time `size()` + function is demanded. This will tell the intrusive container to insert an + additional member in the intrusive container that offers this information. + +* The third template argument specifies the type that will hold + the size of the container. This type will be the type returned by `list.size()` + and the type stored in the intrusive container if `ConstantTimeSize` is requested. + +[endsect] + +[section:slist_example Example] + +Now let's see an small example using both hooks: + +[import ../example/doc_slist.cpp] +[doc_slist_code] + +[endsect] + +[endsect] + +[section:list Intrusive doubly linked list: list] + +[classref boost::intrusive::list list] is a doubly linked list. The memory overhead +that imposes is 2 pointers per node. An empty, non constant-time size [classref boost::intrusive::list list] +has also the size of 2 pointers. [classref boost::intrusive::list list] +has many more constant-time operations than [classref boost::intrusive::slist slist] +and provides bidirectional iterator. It's recommendable to use use +[classref boost::intrusive::list list] instead of +[classref boost::intrusive::slist slist] if the size overhead is acceptable: + +[section:list_hooks list hooks] + +Like the rest of [*Boost.Intrusive] containers, +[classref boost::intrusive::list list] has two hook types: + +[c++] + + template + class list_base_hook; + +* [classref boost::intrusive::list_base_hook list_base_hook]: the user class + derives publicly from [classref boost::intrusive::list_base_hook list_base_hook] + to make it [classref boost::intrusive::list list]-compatible. + +[c++] + + template + class list_member_hook; + +* [classref boost::intrusive::list_member_hook list_member_hook]: + the user class contains a public + [classref boost::intrusive::list_member_hook list_member_hook] to make + it [classref boost::intrusive::list list]-compatible. + +[endsect] + +[section:list_container list container] + +[classref boost::intrusive::list list] receives 3 template parameters: + +[c++] + + template + class list; + +* The first template is the value traits class. Contains information about the + value to be inserted: the type, the type of hook, the type of the pointers to + be used, whether the safe mode is being used... + +* The second boolean template argument specifies if a constant time `size()` + function is demanded. This will tell the intrusive container to insert an + additional member in the intrusive container that offers this information. + +* The third template argument specifies the type that will hold + the size of the container. This type will be the type returned by `list.size()` + and the type stored in the intrusive container if `ConstantTimeSize` is requested. + +[endsect] + +[section:list_example Example] + +Now let's see an small example using both hooks: + +[import ../example/doc_list.cpp] +[doc_list_code] + +[endsect] + +[endsect] + +[section:set_multiset Intrusive associative containers: set, multiset] + +[*Boost.Intrusive] also offers associative containers that can be very useful +when creating more complex associative containers, like containers maintaining +one or more indices with different sorting semantics. +The memory overhead of these containers is usually 3 pointers and an integer. If +pointers have 2 byte alignment (which is usually true in most systems), +[*Boost.Intrusive] optimizes this overhead to 3 pointers. + +An empty, non constant-time size [classref boost::intrusive::set set] or +[classref boost::intrusive::multiset multiset] +has also the size of 3 pointers and an integer (3 pointers when optimized). +[classref boost::intrusive::set set] and +[classref boost::intrusive::multiset multiset] have logarithmic complexity in many +operations like +searches, insertions, erasures, etc... [classref boost::intrusive::set set] and +[classref boost::intrusive::multiset multiset] are the +intrusive equivalents of standard `std::set` and `std::multiset` containers. + +[section:set_multiset_hooks set and multiset hooks] + +[classref boost::intrusive::set set] and +[classref boost::intrusive::multiset multiset] share the same hooks. +This is an advantage, because the same +user type can be inserted first in a [classref boost::intrusive::multiset multiset] +and after that in [classref boost::intrusive::set set] without +changing the definition of the user class. + +[c++] + + template + class set_base_hook; + +* [classref boost::intrusive::set_base_hook set_base_hook]: + the user class derives publicly from + [classref boost::intrusive::set_base_hook set_base_hook] to make + it [classref boost::intrusive::set set]/[classref boost::intrusive::multiset multiset]-compatible. + +[c++] + + template + class set_member_hook; + +* [classref boost::intrusive::set_member_hook set_member_hook]: + the user class contains a public + [classref boost::intrusive::set_member_hook set_member_hook] to make + it [classref boost::intrusive::set set]/[classref boost::intrusive::multiset multiset]-compatible. + +[endsect] + +[section:set_multiset_containers set and multiset containers] + +[classref boost::intrusive::set set] and +[classref boost::intrusive::multiset multiset] receive 4 template parameters: + +[c++] + + template < class ValueTraits + , class Compare = std::less + , bool ConstantTimeSize = true + , class SizeType = std::size_t > + class set; + + template < class ValueTraits + , class Compare = std::less + , bool ConstantTimeSize = true + , class SizeType = std::size_t > + class multiset; + +* The first template is the value traits class. Contains information about the + value to be inserted: the type, the type of hook, the type of the pointers to + be used, whether the safe mode is being used... + +* The second template is the ordering function of the associative container. + By default, the ordering function is `std::less<...>` of the user value. + +* The third boolean template argument specifies if a constant time `size()` + function is demanded. This will tell the intrusive container to insert an + additional member in the intrusive container that offers this information. + +* The fourth template argument specifies the type that will hold + the size of the container. This type will be the type returned by `list.size()` + and the type stored in the intrusive container if `ConstantTimeSize` is requested. + +[endsect] + +[section:set_multiset_example Example] + +Now let's see an small example using both hooks and both containers: + +[import ../example/doc_set.cpp] +[doc_set_code] + +[endsect] + +[endsect] + +[section:unordered_set_unordered_multiset Pseudo-Intrusive unordered associative containers: unordered_set, unordered_multiset] + +[*Boost.Intrusive] also offers hashed containers that can be very useful develop +fast-lookup intrusive 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 +that the value_type of the container must add as a base or member. This additional +memory must be passed in the constructor of the container. + +Unlike C++ TR1 unordered associative containers (which are also hashed containers), +the contents of these pseudo-intrusive containers are not rehashed to maintain a +load factor: that would require memory management and intrusive containers don't +implement any memory management at all. However, the user can request an explicit +rehashing passing a new bucket array. + +This also offers an additional guarantee over TR1 unordered associative containers: +[*iterators are not invalidated when inserting an element] in the container. + +As with TR1 unordered associative containers, rehashing invalidates iterators, +changes ordering between elements, and changes which buckets elements appear in, +but does not invalidate pointers or references to elements. + +[*Boost.Intrusive] unordered associative containers need five arguments to be passed in +their constructors: A pointer to an array of elements whose type is called `bucket_type`, +the length of that array, the hash function to be used with the values and an +equality functor for those values: + +[c++] + + template< class ValueTraits + , class Hash = boost::hash + , class Equal = std::equal_to + , bool ConstantTimeSize = true + , class SizeType = std::size_t + > + class unordered_set + { + // ... + + typedef /*implementation defined*/ bucket_type; + typedef /*implementation defined*/ bucket_ptr; + typedef /*implementation defined*/ size_type; + + //Constructor + unordered_set ( bucket_ptr buckets + , size_type buckets_len + , const Hash &hasher = Hash() + , const Equal &equal = Equal()) ; + + // ... + }; + +Each hashed container needs [*its own bucket array]. Two hashed containers +[*can't] share the same `bucket_type` elements. The bucket array [*must] be +destroyed [*after] the container using it is destroyed, otherwise, the result +is undefined. + +[section:unordered_set_unordered_multiset_performance unordered_set and unordered_multiset performance notes] + +The size overhead for a hashed container is moderate: 1 pointer per value plus +a bucket array per container. The size of an element of the bucket array +is usually one pointer. To obtain a good performance hashed container, +the bucket length is usually the same as the number of elements that the +container contains, so a well-balanced hashed container (`bucket_count()` is +equal to `size()` ) will have an equivalent overhead of two pointers per element. + +An empty, non constant-time size [classref boost::intrusive::unordered_set unordered_set] or +[classref boost::intrusive::unordered_multiset unordered_multiset] +has also the size of `bucket_count()` pointers. + +Insertions, erasures, and searches, have amortized constant-time complexity in +hashed containers. However, some worst-case guarantees are linear. See +[classref boost::intrusive::unordered_set unordered_set] or +[classref boost::intrusive::unordered_multiset unordered_multiset] for complexity guarantees +of each operation. + +[*Be careful with non constant-time size hashed containers]: some operations, like +`empty()`, have linear complexity, unlike other [*Boost.Intrusive] containers. + +[endsect] + +[section:unordered_set_unordered_multiset_hooks unordered_set and unordered_multiset hooks] + +[classref boost::intrusive::unordered_set unordered_set] and [classref boost::intrusive::unordered_multiset unordered_multiset] share the same hooks. This is an advantage, because the same +user type can be inserted first in a [classref boost::intrusive::unordered_multiset unordered_multiset] and after that in [classref boost::intrusive::unordered_set unordered_set] without +changing the definition of the user class. + +[c++] + + template + class unordered_set_base_hook; + +* [classref boost::intrusive::unordered_set_base_hook unordered_set_base_hook]: + the user class derives publicly from + [classref boost::intrusive::unordered_set_base_hook unordered_set_base_hook] to make + it [classref boost::intrusive::unordered_set unordered_set]/[classref boost::intrusive::unordered_multiset unordered_multiset]-compatible. + +[c++] + + template + class unordered_set_member_hook; + +* [classref boost::intrusive::unordered_set_member_hook unordered_set_member_hook]: + the user class contains a public + [classref boost::intrusive::unordered_set_member_hook unordered_set_member_hook] to make + it [classref boost::intrusive::unordered_set unordered_set]/[classref boost::intrusive::unordered_multiset unordered_multiset]-compatible. + +[endsect] + +[section:unordered_set_unordered_multiset_containers unordered_set and unordered_multiset containers] + +[classref boost::intrusive::unordered_set unordered_set] and +[classref boost::intrusive::unordered_multiset unordered_multiset] receive 5 template parameters: + +[c++] + + template< class ValueTraits + , class Hash = boost::hash + , class Equal = std::equal_to + , bool ConstantTimeSize = true + , class SizeType = std::size_t + > + class unordered_set; + + template< class ValueTraits + , class Hash = boost::hash + , class Equal = std::equal_to + , bool ConstantTimeSize = true + , class SizeType = std::size_t + > + class unordered_multiset; + +* The first template is the value traits class. Contains information about the + value to be inserted: the type, the type of hook, the type of the pointers to + be used, whether the safe mode is being used... + +* The second template is the hash function of the associative container. + It takes a value_type argument and returns a std::size_t. + By default, the hash function is `boost::hash<...>` of the user value. + +* The third template is the equality function of the associative container. + By default, the equality function is `std::equal_to<...>` of the user value. + +* The fourth boolean template argument specifies if a constant time `size()` + function is demanded. This will tell the intrusive container to insert an + additional member in the intrusive container that offers this information. + [*Be careful with non constant-time size() hashed containers] since they + have a linear complexity `empty()` function. + +* The fifth template argument specifies the type that will hold + the size of the container. This type will be the type returned by `list.size()` + and the type stored in the intrusive container if `ConstantTimeSize` is requested. + +[endsect] + +[section:unordered_set_unordered_multiset_example Example] + +Now let's see an small example using both hooks and both containers: + +[import ../example/doc_unordered_set.cpp] +[doc_unordered_set_code] + +[endsect] + +[endsect] + +[section:advanced_lookups_insertions Advanced lookup and insertion functions for associative containers] + +[section:advanced_lookups Advanced lookups] + +[*Boost.Intrusive] associative containers offer the same interface as STL associative +containers. However, STL and TR1 ordered and unordered simple associative containers +(`std::set`, `std::multiset`, `std::tr1::unordered_set` and `std::tr1::unordered_multiset`) +have some inefficiencies caused by the interface: the user can only operate with `value_type` +objects. When using these containers we must use `iterator find(const value_type &value)` +to find a value. The same happens in other functions +like `equal_range`, `lower_bound`, `upper_bound`... + +However, sometimes the object to be searched it's quite expensive to construct: + +[import ../example/doc_assoc_optimized_code.cpp] +[doc_assoc_optimized_code_normal_find] + +`Expensive` is an expensive object to construct. If "key" c-string is quite long +`Expensive` has to construct a `std::string` using heap memory. Like +`Expensive`, many times the only member taking part in ordering issues is just +a small part of the class. For example, with `Expensive`, only the internal +`std::string` is needed to compare the object. + +In both containers, if we call `get_from_set/get_from_unordered_set` in a loop, we might get a performance penalty, +because we are forced to create a whole `Expensive` object to be able to find an +equivalent one. + +Sometimes this interface limitation is severe, because +we [*might not have enough information to construct the object] but we might +[*have enough information to find the object]. In this case, a name it's enough +to search `Expensive` in the container but constructing an `Expensive` +might require more information that the user might not have. + +To solve this, [classref boost::intrusive::set set]/[classref boost::intrusive::multiset multiset] +offer alternative functions, which take any type comparable with the value and a +functor that should be compatible with the +ordering function of the associative container. +[classref boost::intrusive::unordered_set unordered_set]/[classref boost::intrusive::unordered_multiset unordered_multiset] +offers functions that take any key type and compatible hash and equality functions. Now, let's see the +optimized search function: + +[doc_assoc_optimized_code_optimized_find] + +This new arbitrary key overload is also available for other functions taking +values as arguments: + +* equal_range +* lower_bound +* upper_bound +* count +* find +* erase + +Check [classref boost::intrusive::set], +[classref boost::intrusive::multiset], +[classref boost::intrusive::unordered_set], +[classref boost::intrusive::unordered_multiset] +references to know more about those functions. + +[endsect] + +[section:advanced_insertions Advanced insertions] + +A similar issue happens with insertions in simple ordered and unordered associative +containers with unique keys (`std::set` and `std::tr1::unordered_set`). In these containers, +if a value is already present, the value to be inserted is discarded. With expensive +values, if the value is already present, we can suffer efficiency problems. + +[classref boost::intrusive::set set] and [classref boost::intrusive::unordered_set unordered_set] +have insertion functions to check efficiently, without +constructing the value, if a value is present or not and if it's not present, a +function to insert it immediately without any further lookup. +For example, using the same `Expensive` class, +this function can be inefficient: + +[doc_assoc_optimized_code_normal_insert] + +If the object is already present, we are constructing an `Expensive` that +will be discarded, and this is a waste of resources. Instead of that, let's use +`insert_check` and `insert_commit` functions: + +[doc_assoc_optimized_code_optimized_insert] + +`insert_check` is similar to a normal `insert` but: + +* `insert_check` can be used with arbitrary keys +* if the insertion is possible (there is no equivalent value) collects all the needed information +in an `insert_commit_data` structure, so that `insert_commit`: + * [*does not execute] further comparisons + * can be executed with [*constant-time complexity] + * has [*no-throw guarantee]. + +These functions must be used with care, since +no other insertion or erasure must be executed between an `insert_check` and an `insert_commit` +pair. Otherwise, the behaviour is undefined. +`insert_check` and `insert_commit` will come handy +for developers programming efficient non-intrusive associative containers. +See [classref boost::intrusive::set set] +and [classref boost::intrusive::unordered_set unordered_set] reference for more information about +`insert_check` and `insert_commit`. + +With multiple ordered and unordered associative containers +([classref boost::intrusive::multiset multiset] and +[classref boost::intrusive::unordered_multiset unordered_multiset]) there's +no need for these advanced insertion functions, since insertions are always succesful. + +[endsect] + +For more information about advanced lookup and insertion functions see +[classref boost::intrusive::set set], +[classref boost::intrusive::multiset multiset], +[classref boost::intrusive::unordered_set unordered_set] and +[classref boost::intrusive::unordered_multiset unordered_multiset] references. + +[endsect] + +[section:erasing_and_destroying Erasing and destroying values from Boost.Intrusive containers] + +One of the most tedious tasks when using intrusive containers is the management of the erased elements. +When using STL containers, the container itself unlinks and destroys the contained elements, but with +intrusive containers, the user must explicitly destroy the object after erasing an element from the container. +This makes STL-like functions erasing multiple objects unhelpful: the user can't destroy every erased element. +For example, let's take the function `remove_if` from [classref boost::intrusive::list list]: + +[c++] + + template + void remove_if(Pred pred); + +How can the user destroy the elements (say, using `operator delete`) that will be erased according +to the predicate? [*Boost.Intrusive] containers offer additional functions that take a function +object that will be called after the element has been erased from the container. For example, +[classref boost::intrusive::list list] offers: + +[c++] + + template + void remove_and_destroy_if(Pred pred, Destroyer destroyer) + +This function can be used to efficiently remove and destroy elements. `remove_and_destroy_if` +will call "destroyer" function object for every removed element. [classref boost::intrusive::list list] offers +more functions taking a destroyer function object as argument, like `erase_and_destroy`, `clear_and_destroy`, +`remove_and_destroy`... Let's see an small example: + +[import ../example/doc_erasing_and_destroying.cpp] +[doc_erasing_and_destroying] + +All [*Boost.Intrusive] containers offer these "erase + destroy" additional members for all functions +that erase an element from the container. + +[endsect] + +[section:clone_from Cloning [*Boost.Intrusive] containers] + +As previously mentioned, [*Boost.Intrusive] containers are [*non-copyable and non-assignable], because +intrusive containers don't allocate memory at all. To implement a copy-constructor or assignment operator, +the user must clone one by one all the elements of the container and insert them in another intrusive container. +However, cloning by hand is usually more inefficient than a member cloning function and an specialized cloning +function can offer more guarantees than the manual cloning (better exception safety guarantees, for example). + +To ease the implementation of copy constructors and assignment operators of classes containing [*Boost.Intrusive] +containers, all [*Boost.Intrusive] containers offer an special cloning function called `clone_from`. + +Apart from the container to be cloned, `clone_from` takes two function objects as arguments. For example, the +`clone_from` member function of [classref boost::intrusive::list list]: + +[c++] + + template + void clone_from(const list &src, Cloner cloner, Destroyer destroyer); + +This function will make `*this` a clone of `src`. Let's explain the arguments: + +* The first parameter is the list to be cloned. +* The second parameter is a function object that will clone `value_type` objects and + return a pointer to the clone. It must implement the following function: + `pointer operator()(const value_type &)`. +* The second parameter is a function object that will destroy `value_type` objects. It's used first + to empty the container before cloning and to erase the elements if an exception is thrown. + +The cloning function works as follows: + +* First clears and destroys all the elements from *this using the destroyer function object. +* After that starts cloning all the elements of the source container using the cloner function object. +* If any operation in the cloning function (for example, the cloner function object) throws, all the constructed elements are destroyed using the destroyer function object. + + +Here's an example of `clone_from`: + +[import ../example/doc_clone_from.cpp] +[doc_clone_from] + +[endsect] + +[section:using_smart_pointers Using smart pointers with Boost.Intrusive containers] + +[*Boost.Intrusive] hooks can be configured to use other pointers than raw pointers. +When a [*Boost.Intrusive] hook is configured with an smart pointer as an argument, +this pointer configuration is passed to the containers. For example, if the following +hook is configured with an smart pointer (for example, an offset pointer from +[*Boost.Interprocess]): + +[import ../example/doc_offset_ptr.cpp] +[doc_offset_ptr_0] + +Any intrusive list constructed using this hook will be ready for shared memory, +because the intrusive list will also use offset pointers internally. For example, +we can create an intrusive list in shared memory combining [*Boost.Interprocess] +and [*Boost.Intrusive]: + +[doc_offset_ptr_1] + +[section:smart_pointers_requirements Requirements for smart pointers compatible with Boost.Intrusive] + +Not every smart pointer is compatible with [*Boost.Intrusive], the smart pointer must +have the following features: + +* It must support the same operations as a raw pointer, except casting. +* It must be convertible to a raw pointer and constructible from a raw pointer. +* It must have the same ownership semantics as a raw pointer. This means that + resource management smart pointers (like `boost::shared_ptr`) can't be used. + +The conversion from the smart pointer to a raw pointer must be implemented following +Boost smart pointer `get_pointer()` function. This function will be found using +ADL. For example, for `boost::interprocess::offset_ptr` `get_pointer` is defined +as follows: + +[c++] + + template + T * get_pointer(boost::interprocess::offset_ptr const & p) + { return p.get(); } + +[endsect] + +[endsect] + +[section:obtaining_iterators_from_values Obtaining iterators from values] + +[*Boost.Intrusive] offers another useful feature that's not present in STL +containers: it's possible to obtain an iterator to a value from the value itself. +This feature is implemented in [*Boost.Intrusive] containers by a +function called `iterator_to`: + +[c++] + + iterator iterator_to(reference value); + const_iterator iterator_to(const_reference value); + +For [*Boost.Intrusive] containers that have local iterators, like unordered +associative containers, we can also obtain local iterators: + +[c++] + + local_iterator local_iterator_to(reference value); + const_local_iterator local_iterator_to(const_reference value); + +For most [*Boost.Intrusive] containers +([classref boost::intrusive::list list], +[classref boost::intrusive::slist slist], +[classref boost::intrusive::set set], +[classref boost::intrusive::multiset multiset]) +`iterator_to` is an static function so we don't need a reference to the +container to obtain the iterator. For unordered associative containers +([classref boost::intrusive::unordered_set unordered_set], +[classref boost::intrusive::multiset multiset]), +`iterator_to` is not an static function, so there is need +to have a reference to the container. On the other hand, `local_iterator_to` functions +are static. + +Let's see an small function that shows the use of `iterator_to` and `local_iterator_to`: + +[import ../example/doc_iterator_from_value.cpp] +[doc_iterator_from_value] + +[endsect] + +[section:concepts Concepts explained] + +This section will expand the explanation of previously presented basic concepts +before explaining the customization options of [*Boost.Intrusive]. + +* [*Node Algorithms]: A set of static functions that implement basic operations + on a group of nodes: initialize a node, link a node to a group of nodes, + unlink a node from another group of nodes... For example, a circular + singly linked list is a group of nodes, where each node has a pointer to the + next node. [*Node Algorithms] just require a [*NodeTraits] + template parameter and they can work with any [*NodeTraits] class that fulfills + the needed interface. As an example, here is a class that implements algorithms + to manage a group of nodes forming a circular singly linked list: + +[c++] + + template + struct my_slist_algorithms + { + typedef typename NodeTraits::node_ptr node_ptr; + typedef typename NodeTraits::const_node_ptr const_node_ptr; + + //Get the previous node of "this_node" + static node_ptr get_prev_node(node_ptr this_node) + { + node_ptr p = this_node; + while (this_node != NodeTraits::get_next(p)) + p = NodeTraits::get_next(p); + return p; + } + + // number of elements in the group of nodes containing "this_node" + static std::size_t count(const_node_ptr this_node) + { + std::size_t result = 0; + const_node_ptr p = this_node; + do{ + p = NodeTraits::get_next(p); + ++result; + } while (p != this_node); + return result; + } + + // More operations + // ... + }; + +* [*Node Traits]: A class that encapsulates the basic information and + operations on a node that forms a group of nodes: + the type of the node, a function to obtain the pointer to the next node... + [*Node Traits] are the configuration information [*Node Algorithms] + need. Each type of [*Node Algorithms] expects an interface that compatible + [*Node Traits] must implement. + As an example, this is the definition of a [*Node Traits] class that + is compatible with the previously presented `my_slist_algorithms`: + +[c++] + + struct my_slist_node_traits + { + //The type of the node + struct node + { + node *next_; + }; + + typedef node * node_ptr; + typedef const node * const_node_ptr; + + //A function to obtain a pointer to the next node + static node_ptr get_next(const_node_ptr n) + { return n->next_; } + + //A function to set the pointer to the next node + static void set_next(node_ptr n, node_ptr next) + { n->next_ = next; } + }; + + +* [*Hook]: A class that the user must add as a base class or as a member to his own + class to make that class insertable in an intrusive container. Usually the hook + contains a node object, that will be used to form the group of nodes: + For example, the following class is a [*Hook] that the user can add as a base class, + to make the user class compatible with a singly linked list container: + +[c++] + + class my_slist_base_hook + //This hook contains a node, that will be used + //to link the user object in the group of nodes + : private my_slist_node_traits::node + { + typedef my_slist_node_traits::node_ptr node_ptr; + typedef my_slist_node_traits::const_node_ptr const_node_ptr; + + //Converts the generic node to the hook + static my_slist_base_hook *to_hook_ptr(node_ptr p) + { return static_cast(p); } + + //Returns the generic node stored by this hook + node_ptr to_node_ptr() + { return static_cast(this); } + + // More operations + // ... + }; + + //To make MyClass compatible with an intrusive singly linked list + //derive our class from the hook. + class MyClass + : public my_slist_base_hook + { + void set(int value); + int get() const; + + private: + int value_; + }; + +* [*Intrusive Container]: A container that offers an STL-like interface to store + user objects. An intrusive container can be templatized to store different + value types that use different hooks. An intrusive container is also more elaborated + than a group of nodes: it can store the number of elements to achieve constant-time + size information, it can offer debugging facilities... + For example, an [classref boost::intrusive::slist slist] container + (intrusive singly linked list) should + be able to hold `MyClass` objects that might have decided to store the hook + as a base class or as a member. Internally, the container will use [*Node Algorithms] + to implement its operations, and an intrusive container is configured using + a template parameter called [*ValueTraits]. [*ValueTraits] will contain + the information to convert user classes in nodes compatible with [*Node Algorithms]. + For example, this a possible [classref boost::intrusive::slist slist] implementation: + +[c++] + + template + class slist + { + public: + typedef typename ValueTraits::value_type value_type; + + //More typedefs and functions + // ... + + //Insert the value as the first element of the list + void push_front (reference value) + { + node_ptr to_insert(ValueTraits::to_node_ptr(value)); + circular_list_algorithms::link_after(to_insert, get_root_node()); + } + + // More operations + // ... + }; + +* [*Pseudo-Intrusive Container]: A pseudo-intrusive container is similar to an + intrusive container, but apart from the values to be inserted in the container, + it needs additional memory (for example, auxiliary arrays or indexes). + +* [*Value Traits]: As we can see, to make our classes intrusive-friendly we add + a simple hook as a member or base class. The hook contains a generic node + that will be inserted in a group of nodes. [*Node Algorithms] just work + with nodes and don't know anything about user classes. On the other + hand, an intrusive container needs to know how to obtain a node from a user class, + and also the inverse operation. + So we can define [*ValueTraits] as the glue between user classes and nodes + required by [*Node Algorithms]. + Let's see a possible implementation of a value traits class that glues MyClass + and the node stored in the hook: + +[c++] + + struct my_slist_derivation_value_traits + { + public: + typedef slist_node_traits node_traits; + typedef MyClass value_type; + typedef node_traits::node_ptr node_ptr; + typedef value_type* pointer; + //... + + //Converts user's value to a generic node + static node_ptr to_node_ptr(reference value) + { return static_cast(value).to_node_ptr(); } + + //Converts a generic node into user's value + static value_type *to_value_ptr(node_traits::node *n) + { static_cast(slist_base_hook::to_hook_ptr(n)); } + + // More operations + // ... + }; + +[endsect] + +[section:node_algorithms Node algorithms with custom NodeTraits] + +As explained in the [link intrusive.concepts Concepts] section, [*Boost.Intrusive] +containers are implemented using node algorithms that work on generic nodes. + +Sometimes, the use of intrusive containers is expensive for some environments +and the programmer might want to avoid all the template instantiations +related to [*Boost.Intrusive] containers. However, the user can still benefit +from [*Boost.Intrusive] using the node algorithms, because some of those algorithms, +like red-black tree algorithms, are not trivial to write. + +All node algorithm classes are +templatized by a `NodeTraits` class. This class encapsulates the needed internal +type declarations and operations to make a node compatible with node algorithms. +Each type of node algorithms has its own requirements: + +[section:circular_slist_algorithms Intrusive singly linked list algorithms] + +These algorithms are static +members of the [classref boost::intrusive::circular_slist_algorithms circular_slist_algorithms] class: + +[c++] + + template + struct circular_slist_algorithms; + +An empty list is formed by a node whose pointer to the next node points +to itself. [classref boost::intrusive::circular_slist_algorithms circular_slist_algorithms] +is configured with a NodeTraits class, which encapsulates +the information about the node to be manipulated. NodeTraits must support the +following interface: + +[*Typedefs]: + +* `node`: The type of the node that forms the circular list + +* `node_ptr`: The type of a pointer to a node (usually node*) + +* `const_node_ptr`: The type of a pointer to a const node (usually const node*) + +[*Static functions]: + +* `static node_ptr get_next(const_node_ptr n);`: + Returns a pointer to the next node stored in "n". + +* `static void set_next(node_ptr n, node_ptr next);`: + Sets the pointer to the next node stored in "n" to "next". + +Once we have a node traits configuration we can use [*Boost.Intrusive] algorithms +with our nodes: + +[import ../example/doc_slist_algorithms.cpp] +[doc_slist_algorithms_code] + +For a complete list of functions see +[classref boost::intrusive::circular_slist_algorithms circular_slist_algorithms reference]. + +[endsect] + +[section:circular_list_algorithms Intrusive doubly linked list algorithms] + +These algorithms are static +members of the [classref boost::intrusive::circular_list_algorithms circular_list_algorithms] class: + +[c++] + + template + struct circular_list_algorithms; + +An empty list is formed by a node whose pointer to the next node points +to itself. [classref boost::intrusive::circular_list_algorithms circular_list_algorithms] +is configured with a NodeTraits class, which encapsulates +the information about the node to be manipulated. NodeTraits must support the +following interface: + +[*Typedefs]: + +* `node`: The type of the node that forms the circular list + +* `node_ptr`: The type of a pointer to a node (usually node*) + +* `const_node_ptr`: The type of a pointer to a const node (usually const node*) + +[*Static functions]: + +* `static node_ptr get_next(const_node_ptr n);`: + Returns a pointer to the next node stored in "n". + +* `static void set_next(node_ptr n, node_ptr next);`: + Sets the pointer to the next node stored in "n" to "next". + +* `static node_ptr get_previous(const_node_ptr n);`: + Returns a pointer to the previous node stored in "n". + +* `static void set_previous(node_ptr n, node_ptr prev);`: + Sets the pointer to the previous node stored in "n" to "prev". + +Once we have a node traits configuration we can use [*Boost.Intrusive] algorithms +with our nodes: + +[import ../example/doc_list_algorithms.cpp] +[doc_list_algorithms_code] + +For a complete list of functions see +[classref boost::intrusive::circular_list_algorithms circular_list_algorithms reference]. + +[endsect] + +[section:rbtree_algorithms Intrusive red-black tree algorithms] + +These algorithms are static +members of the [classref boost::intrusive::rbtree_algorithms rbtree_algorithms] class: + +[c++] + + template + struct rbtree_algorithms; + + +An empty tree is formed by a node whose pointer to the parent node is null, +the pointers to the left and right nodes to itself and whose color is red. +[classref boost::intrusive::rbtree_algorithms rbtree_algorithms] +is configured with a NodeTraits class, which encapsulates +the information about the node to be manipulated. NodeTraits must support the +following interface: + +[*Typedefs]: + +* `node`: The type of the node that forms the circular rbtree + +* `node_ptr`: The type of a pointer to a node (usually node*) + +* `const_node_ptr`: The type of a pointer to a const node (usually const node*) + +* `color`: The type that can store the color of a node + +[*Static functions]: + +* `static node_ptr get_parent(const_node_ptr n);`: + Returns a pointer to the parent node stored in "n". + +* `static void set_parent(node_ptr n, node_ptr p);`: + Sets the pointer to the parent node stored in "n" to "p". + +* `static node_ptr get_left(const_node_ptr n);`: + Returns a pointer to the left node stored in "n". + +* `static void set_left(node_ptr n, node_ptr l);`: + Sets the pointer to the left node stored in "n" to "l". + +* `static node_ptr get_right(const_node_ptr n);`: + Returns a pointer to the right node stored in "n". + +* `static void set_right(node_ptr n, node_ptr r);`: + Sets the pointer to the right node stored in "n" to "r". + +* `static color get_color(const_node_ptr n);`: + Returns the color stored in "n". + +* `static void set_color(node_ptr n, color c);`: + Sets the color stored in "n" to "c". + +* `static color black();`: + Returns a value representing the black color. + +* `static color red();`: + Returns a value representing the red color. + +Once we have a node traits configuration we can use [*Boost.Intrusive] algorithms +with our nodes: + +[import ../example/doc_rbtree_algorithms.cpp] +[doc_rbtree_algorithms_code] + +For a complete rbtree of functions see +[classref boost::intrusive::rbtree_algorithms rbtree_algorithms reference]. + +[endsect] + +[endsect] + +[section:value_traits Containers with custom ValueTraits] + +As explained in the [link intrusive.concepts Concepts] section, [*Boost.Intrusive] +containers are templatized using a `ValueTraits` parameter. This parameter contains +all the information to glue the `value_type` of the containers and the node to be +used in node algorithms, since these types can be different. Apart from this, +`ValueTraits` also store information about the link policy of the values to be inserted. + +Instead of using [*Boost.Intrusive] predefined hooks +a user might want to develop customized containers, for example, using nodes that are +optimized for an specific +application or that are compatible with a a legacy ABI. A user might want +to have only two additional pointers in his class and insert the class in a doubly +linked list sometimes and in a singly linked list in other situations. You can't +achieve this using [*Boost.Intrusive] predefined hooks. + +[section:value_traits_interface ValueTraits interface] + +`ValueTraits` have the following interface: + +[c++] + + #include + #include + + struct my_value_traits + { + typedef implementation_defined node_traits; + typedef implementation_defined value_type; + typedef node_traits::node_ptr node_ptr; + typedef node_traits::const_node_ptr const_node_ptr; + typedef boost::pointer_to_other::type pointer; + typedef boost::pointer_to_other::type const_pointer; + + enum { linking_policy = some_linking_policy }; + + static node_ptr to_node_ptr (value_type &value); + static const_node_ptr to_node_ptr (const value_type &value); + static pointer to_value_ptr (node_ptr n); + static const_pointer to_value_ptr (const_node_ptr n); + }; + +Let's explain each type and function: + +* ['node_traits]: The node configuration that it's needed by node algorithms. + These node traits and algorithms are + described in the previous chapter: [link intrusive.node_algorithms Nodes Algorithms]. + + * If my_value_traits is meant to be used with [classref boost::intrusive::slist slist], + `node_traits` should follow + the interface needed by [classref boost::intrusive::circular_slist_algorithms circular_slist_algorithms]. + + * If my_value_traits is meant to be used with [classref boost::intrusive::list list], + `node_traits` should follow + the interface needed by [classref boost::intrusive::circular_list_algorithms circular_list_algorithms]. + + * If my_value_traits is meant to be used with [classref boost::intrusive::set set]/[classref boost::intrusive::multiset multiset], + `node_traits` should follow + the interface needed by [classref boost::intrusive::rbtree_algorithms rbtree_algorithms]. + + * If my_value_traits is meant to be used with [classref boost::intrusive::unordered_set unordered_set]/ + [classref boost::intrusive::unordered_multiset unordered_multiset], `node_traits` + should follow the interface needed by [classref boost::intrusive::circular_slist_algorithms circular_slist_algorithms]. + +* ['node_ptr]: A typedef for `node_traits::node_ptr`. + +* ['const_node_ptr]: A typedef for `node_traits::const_node_ptr`. + +* ['value_type]: The type that the user wants to insert in the container. This type can be + the same as `node_traits::node` but it can be different (for example, `node_traits::node` + can be a member type of `value_type`). If `value_type` and `node_traits::node` are the + same type, the `to_node_ptr` and `to_value_ptr` functions are trivial. + +* ['pointer]: The type of a pointer to a `value_type`. It must be the same pointer type + as `node_ptr`: If `node_ptr` is `node *` `pointer` must be `value_type*`. If + `node_ptr` is `smart_ptr`, `pointer` must be `smart_ptr`. + This can be generically achieved using `boost::pointer_to_other` utility from [*Boost SmartPointers] + defined in ``. + +* ['const_pointer]: The type of a pointer to a `const value_type`. It must be the same pointer type + as `node_ptr`: If `node_ptr` is `node *` `const_pointer` must be `const value_type*`. If + `node_ptr` is `smart_ptr`, `const_pointer` must be `smart_ptr` + This can be generically achieved using `boost::pointer_to_other` utility from [*Boost SmartPointers] + defined in ``. + +* ['linking_policy]: Indicates that `value_traits` needs some additional work or checks from the + container. The types are enumerations defined in the `value_traits_type.hpp` header. + These are the possible types: + + * `normal_link`: If this linking policy is specified in a `ValueTraits` class + as the linking_policy, 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 linking_policy, containers + configured with such `ValueTraits` will set the hooks + of the erased values to a default state. Containers also will + check that the hooks of the new values are default initialized. + + * `auto_unlink`: Same as "safe_link" but containers with + constant-time size features won't be + compatible with `ValueTraits` configured with this policy. + Containers also know that the a value can be silently erased from + the container without using any function provided by the containers. + +* ['static node_ptr to_node_ptr (value_type &value)] and + ['static const_node_ptr to_node_ptr (const value_type &value)]: + These function take a reference to a value_type and return a pointer to the node + to be used with node algorithms. + +* ['static pointer to_value_ptr (node_ptr n)] and + ['static const_pointer to_value_ptr (const_node_ptr n)]: + These function take a pointer to a node and return a pointer to the value + that contains the node. + +[endsect] + +[section:value_traits_example Custom ValueTraits example] + +Let's define our own `value_traits` class to be able to use [*Boost.Intrusive] +containers with an old C structure whose definition can't be changed. +That legacy type has two pointers that can be used to build singly and doubly linked +lists: in singly linked lists we only need a pointer, whereas in doubly +linked lists, we need two pointers. Since we only have two pointers, we can't insert +the object in a singly and doubly linked list at the same time. +This is the definition of the old node: + +[import ../example/doc_value_traits.cpp] +[doc_value_traits_code_legacy] + +Now we have to define a NodeTraits class that will implement the functions/typedefs +that will make the legacy node compatible with [*Boost.Intrusive] algorithms. After that, +we'll define a ValueTraits class that will configure [*Boost.Intrusive] containers: + +[doc_value_traits_value_traits] + +Now we can just define the containers that will store the legacy abi objects and write +a little test: + +[doc_value_traits_test] + +As seen, several key elements of [*Boost.Intrusive] can be reused with custom user types, +if the user does not want to use provided [*Boost.Intrusive] facilities. + +[endsect] + +[section:reusing_node_algorithms Reusing node algorithms for different values] + +In the previous example, `legacy_node_traits::node` type and +`legacy_value_traits::value_type` are the same type, but this is not necessary. It's possible +to have several `ValueTraits` defining the same `node_traits` type (and thus, the same `node_traits::node`). +This reduces the number of node algorithm instantiations, but +now `ValueTraits::to_node_ptr` and `ValueTraits::to_value_ptr` functions need to offer +conversions between both types. Let's see an small example: + +First, we'll define the node to be used in the algorithms. For a linked list, +we just need a node that stores two pointers: + +[import ../example/doc_advanced_value_traits.cpp] +[doc_advanced_value_traits_code] + +Now we'll define two different types that will be inserted in intrusive lists and +we'll define a templatized `ValueTraits` that will work for both types: + +[doc_advanced_value_traits_value_traits] + +Now define two containers. Both containers will instantiate the same list algorithms +(`circular_list_algorithms`), +due to the fact that the value traits used to define the containers provide the same `node_traits` type: + +[doc_advanced_value_traits_containers] + +All [*Boost.Intrusive] containers using predefined hooks use this technique to minimize code size: +all the possible [classref boost::intrusive::list list] containers +created with predefined hooks that define the same `VoidPointer` type +share the same list algorithms. + +[endsect] + +[endsect] + +[section:thread_safety Thread safety guarantees] + +Intrusive containers have similar same thread-safety guarantees than STL containers. + +* Serveral threads can have read or write access to different instances is safe as long as inserted + objects are different. +* Concurrent read-only access to the same container is safe. + +Some Intrusive hooks (auto-unlink hooks, for example) modify containers without +having a reference to them: this is considered a write access to the container. + +Other functions, like checking if an objects is already inserted in a containers using the `is_linked()` +member of safe hooks is a read-access to the container without having a reference to them, so no other +thread should have write access (direct or indirect) to that container. + +Since the same object can be inserted in several containers at the same time using different hooks, +the thread safety of [*Boost.Intrusive] is related to the containers and also the object whose lifetime +is manually managed by the user. + +As we can see, the analysis of the thread-safety of a program using [*Boost.Intrusive] is harder +than with non-intrusive containers. + +To analyze the thread-safety, take in care the following points: + +* Auto-unlink hook's destructor and `unlink()` functions modify the container indirectly. +* Safe mode and auto-unlink hook's `is_linked()` function is a read access to the container. +* Inserting an object in several containers that will be modified by different threads has no thread-safety + guarantee, although in most platforms it will be thread-safe without locking. + +[endsect] + +[section:design_notes Design Notes] + +When designing [*Boost.Intrusive] the following guidelines have been taken into account: + +[section: Boost.Intrusive in performance sensitive environments] + +[*Boost.Intrusive] should be a valuable tool in performance sensitive environments, +and following this guideline, [*Boost.Intrusive] has been designed to offer well +known complexity guarantees. Apart from that, some options, like optional +constant-time, have been designed to offer faster complexity guarantees in some +functions, like `slist::splice`. + +The advanced lookup and insertion functions for associative containers, taking +an arbitrary key type and predicates, were designed to avoid unnecessary object +constructions. + +[endsect] + +[section: Boost.Intrusive in space constrained environments] + +[*Boost.Intrusive] should be useful in space constrained environments, +and following this guideline [*Boost.Intrusive] separates node algorithms +and intrusive containers to avoid instantiating node algorithms for each +user type. For example, a single class of red-black algorithms will be instantiated +to implement all set and multiset containers using raw pointers. This way, +[*Boost.Intrusive] wants to avoid any code size overhead associated with templates. + +Apart from that, [*Boost.Intrusive] implements some size improvements: for example, +red-black trees embed the color bit in the parent pointer lower bit, if nodes +are two-byte aligned. The possibility to avoid constant-time size operations can +save some size on containers, and this extra size optimization is noticeable +when the container is empty or contains few values. + +[endsect] + +[section: Boost.Intrusive as basic building block] + +[*Boost.Intrusive] should be a basic building block to build more complex containers +and this guideline has motivated many design decisions. For example, the possibility +to have more than one hook per user type opens the possibility to implement multi-index +containers on top of [*Boost.Intrusive]. + +[*Boost.Intrusive] containers implement advanced functions taking function objects +as arguments (`clone_from`, `erase_and_destroy`, `insert_check`...). These +functions come handy when implementing non-intrusive containers +(for example, STL-like containers) on top of intrusive containers. + +[endsect] + +[section: Extending Boost.Intrusive] + +[*Boost.Intrusive] offers a wide range of containers but also allows the +construction of custom containers reusing [*Boost.Intrusive] elements. +The programer might want to use node algorithms directly or +build special hooks that take advantage of its application environment. + +For example, the programmer can use can customize parts of [*Boost.Intrusive] +to manage old data structures whose definition can't be changed. + +[endsect] + +[endsect] + +[section:performance Performance] + +[*Boost.Intrusive] containers offer speed improvements comparing to non-intrusive containers, +basically because: + +* We can minimize memory allocation/deallocation calls. +* We obtain better memory locality. + +This section will show some performance tests comparing some operations on +`boost::intrusive::list` and `std::list`: + +* Insertions using `push_back` and container destruction will show the + overhead associated with memory allocation/deallocation. +* `reverse` member function will show the advantages of the compact + memory representation that can be achieved with intrusive containers. +* `sort` and `write access` tests will show the advantage of intrusive containers + minimizing the memory accesses when comparing them with containers of pointers. + +Given an object of type `T`, [classref boost::intrusive::list boost::intrusive::list<...>] +can replace `std::list` to avoid memory allocation overhead, +or it can replace `std::list` when the user wants to obtain containers with +polymorphic values or wants to share values between several containers. +Because of this versatility, the performance tests will be executed for 6 different +list types: + +* 3 intrusive lists holding a class named `itest_class`, + each one with a different linking policy (`normal_link`, `safe_link`, `auto_unlink`). + The `itest_class` objects will be tightly packed in a `std::vector` object. + +* `std::list`, where `test_class` is exactly the same as `itest_class`, + but without the intrusive hook. + +* `std::list`. The list will contain pointers to `test_class` objects + tightly packed in a `std::vector` object. We'll call this configuration ['compact pointer list] + +* `std::list`. The list will contain pointers to `test_class` objects owned by a + `std::list` object. We'll call this configuration ['disperse pointer list]. + +Both `test_class` and `itest_class` are templatized classes that can be configured with +a boolean to increase the size of the object. This way, the tests can be executed with +small and big objects. Here is the first part of the testing code, which shows +the definitions of `test_class` and `itest_class` classes, and some other +utilities: + +[import ../perf/perf_list.cpp] +[perf_list_value_type] + +As we can see, `test_class` is a very simple class holding an `int`. `itest_class` +is just a class that has a base hook ([classref boost::intrusive::list_base_hook list_base_hook]) +and also derives from `test_class`. + +`func_ptr_adaptor` is just a functor adaptor to convert function objects taking +`test_list` objects to funtion objects taking pointers to them. + +You can find the full test code code in the +[@../../perf/perf_list.cpp perf_list.cpp] source file. + +[section:performance_results_push_back Back insertions and destruction] + +The first test will measure the benefits we can obtain with intrusive containers +avoiding memory allocations and deallocations . All the objects to be +inserted in intrusive containers are allocated in a single allocation call, +whereas `std::list` will need to allocate memory for every and deallocate it +for every erasure (or container destruction). + +Let's compare the code to be executed for each container type for different insertion tests: + +[perf_list_push_back_intrusive] + +For intrusive containers, all the values are created in a vector and after that +inserted in the intrusive list. + +[perf_list_push_back_stdlist] + +For a standard list, elements are pushed back using push_back(). + +[perf_list_push_back_stdptrlist] + +For a standard compact pointer list, elements are created in a vector and pushed back +in the pointer list using push_back(). + +[perf_list_push_back_disperse_stdptrlist] + +For a ['disperse pointer list], elements are created in a list and pushed back +in the pointer list using push_back(). + +These are the times in microseconds for each case, and the normalized time: + +[table Back insertion + destruction times for Visual C++ 7.1 / Windows XP + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [5000 / 22500] [1 / 1]] + [[`safe_link` intrusive list] [7812 / 32187] [1.56 / 1.43]] + [[`auto_unlink` intrusive list] [10156 / 41562] [2.03 / 1.84]] + [[Standard list] [76875 / 97500] [5.37 / 4.33]] + [[Standard compact pointer list] [76406 / 86718] [15.28 / 3.85]] + [[Standard disperse pointer list] [146562 / 175625] [29.31 / 7.80]] +] + +[table Back insertion + destruction times for GCC 4.1.1 / MinGW over Windows XP + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [4375 / 22187] [1 / 1]] + [[`safe_link` intrusive list] [7812 / 32812] [1.78 / 1.47]] + [[`auto_unlink` intrusive list] [10468 / 42031] [2.39 / 1.89]] + [[Standard list] [81250 / 98125] [18.57 / 4.42]] + [[Standard compact pointer list] [83750 / 94218] [19.14 / 4.24]] + [[Standard disperse pointer list] [155625 / 175625] [35.57 / 7.91]] +] + +[table Back insertion + destruction times for GCC 4.1.2 / Linux Kernel 2.6.18 (OpenSuse 10.2) + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [4792 / 20495] [1 / 1]] + [[`safe_link` intrusive list] [7709 / 30803] [1.60 / 1.5]] + [[`auto_unlink` intrusive list] [10180 / 41183] [2.12 / 2.0]] + [[Standard list] [17031 / 32586] [3.55 / 1.58]] + [[Standard compact pointer list] [27221 / 34823] [5.68 / 1.69]] + [[Standard disperse pointer list] [102272 / 60056] [21.34 / 2.93]] +] + +The results are logical: intrusive lists just need one allocation. The destruction +time of the `normal_link` intrusive container is trivial (complexity: `O(1)`), +whereas `safe_link` and `auto_unlink` intrusive containers need to the hook of +erased values' in the default state (complexity: `O(NumElements)`). That's why +`normal_link` intrusive list shines in this test. + +Non-intrusive containers need to make much more allocations and that's why they are +lagging behind. The `disperse pointer list` needs to make `NumElements*2` allocations, +so the result is not surprising. + +Linux test shows that standard containers are do very well against intrusive containers +with big objects. Nearly the same GCC version in MinGW performs worse, so maybe the +a good operating system memory allocator is the reason for this good results. + +[endsect] + +[section:performance_results_reversing Reversing] + +The next test measures the time needed to complete calss to the member function `reverse()`. +Values (`test_class` and `itest_class`) and lists are created like explained in the +previous section. + +Note that for pointer lists, `reverse` [*does not need to access `test_class` values +stored in another list or vector], +since this function just needs to adjust internal pointers, so in theory, all tested +lists need to perform the same operations. + +These are the results: + +[table Reverse times for Visual C++ 7.1 / Windows XP + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object) (small object / big object)]] + [[`normal_link` intrusive list] [2656 / 10625] [1 / 1.83]] + [[`safe_link` intrusive list] [2812 / 10937] [1.05 / 1.89]] + [[`auto_unlink` intrusive list] [2710 / 10781] [1.02 / 1.86]] + [[Standard list] [5781 / 14531] [2.17 / 2.51]] + [[Standard compact pointer list] [5781 / 5781] [2.17 / 1]] + [[Standard disperse pointer list] [10781 / 15781] [4.05 / 2.72]] +] + +[table Reverse times for GCC 4.1.1 / MinGW over Windows XP + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [2656 / 10781] [1 / 2.22]] + [[`safe_link` intrusive list] [2656 / 10781] [1 / 2.22]] + [[`auto_unlink` intrusive list] [2812 / 10781] [1.02 / 2.22]] + [[Standard list] [4843 / 12500] [1.82 / 2.58]] + [[Standard compact pointer list] [4843 / 4843] [1.82 / 1]] + [[Standard disperse pointer list] [9218 / 12968] [3.47 / 2.67]] +] + +[table Reverse times for GCC 4.1.2 / Linux Kernel 2.6.18 (OpenSuse 10.2) + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [2742 / 10847] [1 / 3.41]] + [[`safe_link` intrusive list] [2742 / 10847] [1 / 3.41]] + [[`auto_unlink` intrusive list] [2742 / 11027] [1 / 3.47]] + [[Standard list] [3184 / 10942] [1.16 / 3.44]] + [[Standard compact pointer list] [3207 / 3176] [1.16 / 1]] + [[Standard disperse pointer list] [5814 / 13381] [2.12 / 4.21]] +] + +For small objects the results show that the compact storage of values in intrusive +containers improve locality and reversing is faster than with standard containers, +whose values might be dispersed in memory because each value is independently +allocated. Note that the dispersed pointer list (a list of pointers to values +allocated in another list) suffers more because nodes of the pointer list +might be more dispersed, since allocations from both lists are interleaved +in the code: + +[c++] + + //Object list (holding `test_class`) + stdlist objects; + + //Pointer list (holding `test_class` pointers) + stdptrlist l; + + for(int i = 0; i < NumElements; ++i){ + //Allocation from the object list + objects.push_back(stdlist::value_type(i)); + //Allocation from the pointer list + l.push_back(&objects.back()); + } + +For big values the compact pointer list wins because when reversing does need access +to the values stored in another container. Since all the allocations for nodes of +this pointer list are likely to be near (since there is no other allocation in the +process until the pointer list is created) locality is better than with intrusive +containers. The dispersed pointer list, like with small values, has poor locality. + +[endsect] + +[section:performance_results_sorting Sorting] + +The next test measures the time needed to complete calls the member function +`sort(Pred pred)`. Values (`test_class` and `itest_class`) and lists are created like explained in the +first section. The values will be sorted in ascending and descenting order each +iteration. For example, if ['l] is a list: + +[c++] + + for(int i = 0; i < NumIter; ++i){ + if(!(i % 2)) + l.sort(std::greater()); + else + l.sort(std::less()); + } + +For a pointer list, the function object will be adapted using `func_ptr_adaptor`: + +[c++] + + for(int i = 0; i < NumIter; ++i){ + if(!(i % 2)) + l.sort(func_ptr_adaptor >()); + else + l.sort(func_ptr_adaptor >()); + } + +Note that for pointer lists, `sort` will take a function object that [*will access +`test_class` values stored in another list or vector], so pointer lists will suffer +an extra indirection: they will need to access the `test_class` values stored in +another container to compare to elements. + +These are the results: + +[table Sort times for Visual C++ 7.1 / Windows XP + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [16093 / 38906] [1 / 1]] + [[`safe_link` intrusive list] [16093 / 39062] [1 / 1]] + [[`auto_unlink` intrusive list] [16093 / 38906] [1 / 1]] + [[Standard list] [32343 / 56406] [2.0 / 1.44]] + [[Standard compact pointer list] [33593 / 46093] [2.08 / 1.18]] + [[Standard disperse pointer list] [46875 / 68593] [2.91 / 1.76]] +] + +[table Sort times for GCC 4.1.1 / MinGW over Windows XP + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [15000 / 39218] [1 / 1]] + [[`safe_link` intrusive list] [15156 / 39531] [1.01 / 1.01]] + [[`auto_unlink` intrusive list] [15156 / 39531] [1.01 / 1.01]] + [[Standard list] [34218 / 56875] [2.28 / 1.45]] + [[Standard compact pointer list] [35468 / 49218] [2.36 / 1.25]] + [[Standard disperse pointer list] [47656 / 70156] [3.17 / 1.78]] +] + +[table Sort times for GCC 4.1.2 / Linux Kernel 2.6.18 (OpenSuse 10.2) + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [18003 / 40795] [1 / 1]] + [[`safe_link` intrusive list] [18003 / 41017] [1 / 1]] + [[`auto_unlink` intrusive list] [18230 / 40941] [1.01 / 1]] + [[Standard list] [26273 / 49643] [1.45 / 1.21]] + [[Standard compact pointer list] [28540 / 43172] [1.58 / 1.05]] + [[Standard disperse pointer list] [35077 / 57638] [1.94 / 1.41]] +] + +The results show that intrusive containers are faster than standard +containers. We can see that the pointer +list holding pointers to values stored in a vector is quite fast, so the extra +indirection that needs to access the value is minimized because all the values +are tightly stored, improving cache. The disperse list, on the other hand, is +slower because the indirection to access to values stored in the object list is +more expensive than the access to values stored in a vector. + +[endsect] + +[section:performance_results_write_access Write access] + +The next test measures the time needed to iterate all the elements of a list, and +increment the value of the internal `i_` member: + +[c++] + + stdlist::iterator it(l.begin()), end(l.end()); + for(; it != end; ++it) + ++(it->i_); + +Values (`test_class` and `itest_class`) and lists are created like explained in +the first section. Note that for pointer lists, the iteration will suffer +an extra indirection: they will need to access the `test_class` values stored in +another container: + +[c++] + + stdptrlist::iterator it(l.begin()), end(l.end()); + for(; it != end; ++it) + ++((*it)->i_); + +These are the results: + +[table Write access times for Visual C++ 7.1 / Windows XP + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [2031 / 8125] [1 / 1]] + [[`safe_link` intrusive list] [2031 / 8281] [1 / 1.01]] + [[`auto_unlink` intrusive list] [2031 / 8281] [1 / 1.01]] + [[Standard list] [4218 / 10000] [2.07 / 1.23]] + [[Standard compact pointer list] [4062 / 8437] [2.0 / 1.03]] + [[Standard disperse pointer list] [8593 / 13125] [4.23 / 1.61]] +] + +[table Write access times for GCC 4.1.1 / MinGW over Windows XP + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [2343 / 8281] [1 / 1]] + [[`safe_link` intrusive list] [2500 / 8281] [1.06 / 1]] + [[`auto_unlink` intrusive list] [2500 / 8281] [1.06 / 1]] + [[Standard list] [4218 / 10781] [1.8 / 1.3]] + [[Standard compact pointer list] [3906 / 8281] [1.66 / 1]] + [[Standard disperse pointer list] [8281 / 13750] [3.53 / 1.66]] +] + +[table Write access times for GCC 4.1.2 / Linux Kernel 2.6.18 (OpenSuse 10.2) + [[Container] [Time in us/iteration (small object / big object)] [Normalized time (small object / big object)]] + [[`normal_link` intrusive list] [2286 / 8468] [1 / 1.1]] + [[`safe_link` intrusive list] [2381 / 8412] [1.04 / 1.09]] + [[`auto_unlink` intrusive list] [2301 / 8437] [1.01 / 1.1]] + [[Standard list] [3044 / 9061] [1.33 / 1.18]] + [[Standard compact pointer list] [2755 / 7660] [1.20 / 1]] + [[Standard disperse pointer list] [6118 / 12453] [2.67 / 1.62]] +] + +Like with the read access test, the results show that intrusive containers outperform +all other containers if the values are tightly packed in a vector. +The disperse list is again the slowest one. + +[endsect] + +[section:performance_results_conclusions Conclusions] + +Intrusive containers can offer performance benefits that can not be achieved with +equivalent non-intrusive containers. Memory locality improvements are noticeable +when objects to be inserted are small. Minimizing memory allocation/deallocation calls is also +an important factor and intrusive containers make this simple if the user allocates +all the objects to be inserted in intrusive containers in containers like `std::vector` or `std::deque`. + +[endsect] + +[endsect] + +[section:tested_compilers Tested compilers] + +[*Boost.Intrusive] has been tested in the following compilers/platforms: + +* Visual 7.1/WinXP +* Visual 8.0/WinXP +* GCC 4.1.1/MinGW +* Intel 9.1/WinXP +* GCC 4.1.2/Linux + +[endsect] + +[section:acknowledgments Acknowledgments] + +[*Olaf Krzikalla] would like to thank: + +* [*Markus Schaaf] for pointing out the possibility and the advantages of the derivation +approach. + +* [*Udo Steinbach] for encouragements to present this work for boost, a lot of fixes and +helpful discussions. + +* [*Jaap Suter] for the initial hint, which eventually leads to the member value_traits. + +[*Ion GaztaÃąaga] would like to thank: + +* [*Olaf Krzikalla] for the permission to continue his great work. + +* [*Joaquín M. LÃģpez MuÃąoz] for his thorough review, help, and ideas. + +* [*Cory Nelson], [*Daniel James], [*Dave Harris], [*Guillaume Melquiond], + [*Henri Bavestrello], [*HervÃĐ BrÃķnnimann], [*Kai BrÞning], [*Kevin Sopp] + [*Paul Rose], [*Pavel Vozelinek], [*Howard Hinnant], [*Olaf Krzikalla], + [*Samuel Debionne], [*Stjepan Rajko], [*Thorsten Ottosen], [*Tobias Schwinger], + [*Tom Brinkman] and [*Steven Watanabe] + for their comments and reviews in the Boost.Intrusive formal review. + +[endsect] + +[xinclude intrusive_doxygen.boostbook] + +[section:license_notices License notices] + +The internal implementation of red-black trees is based on that of SGI STL stl_tree.h file: + +Copyright (c) 1996,1997 Silicon Graphics Computer Systems, Inc. +Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Silicon Graphics makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. + +Copyright (c) 1994 Hewlett-Packard Company +Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Hewlett-Packard Company makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. + +[endsect] diff --git a/example/Jamfile.v2 b/example/Jamfile.v2 new file mode 100644 index 0000000..0e70e01 --- /dev/null +++ b/example/Jamfile.v2 @@ -0,0 +1,34 @@ +# Boost Intrusive Library Documentation test Jamfile + +# (C) Copyright Ion Gaztaņaga 2006-2007. +# Use, modification and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +# Adapted from John Maddock's TR1 Jamfile.v2 +# Copyright John Maddock 2005. +# Use, modification and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +# this rule enumerates through all the sources and invokes +# the run rule for each source, the result is a list of all +# the run rules, which we can pass on to the test_suite rule: + +rule test_all +{ + local all_rules = ; + + for local fileb in [ glob *.cpp ] + { + all_rules += [ run $(fileb) + : # additional args + : # test-files + : # requirements + ] ; + } + + return $(all_rules) ; +} + +test-suite intrusive_doc : [ test_all r ] ; \ No newline at end of file diff --git a/example/doc_advanced_value_traits.cpp b/example/doc_advanced_value_traits.cpp new file mode 100644 index 0000000..1d4b018 --- /dev/null +++ b/example/doc_advanced_value_traits.cpp @@ -0,0 +1,103 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_advanced_value_traits_code +#include +#include +#include + +//This is the node that will be used with algorithms. +struct simple_node +{ + simple_node *prev_; + simple_node *next_; +}; +//] + +//[doc_advanced_value_traits_value_traits +class base_1{}; +class base_2{}; + +struct value_1 : public base_1, public simple_node +{ int id_; }; + +struct value_2 : public base_1, public base_2, public simple_node +{ float id_; }; + +//Define the node traits. A single node_traits will be enough. +struct simple_node_traits +{ + typedef simple_node node; + typedef node * node_ptr; + typedef const node * const_node_ptr; + static node *get_next(const node *n) { return n->next_; } + static void set_next(node *n, node *next) { n->next_ = next; } + static node *get_previous(const node *n) { return n->prev_; } + static void set_previous(node *n, node *prev) { n->prev_ = prev; } +}; + +//A templatized value traits for value_1 and value_2 +template +struct value_traits +{ + typedef simple_node_traits node_traits; + typedef node_traits::node_ptr node_ptr; + typedef node_traits::const_node_ptr const_node_ptr; + typedef ValueType value_type; + typedef ValueType * pointer; + typedef const ValueType * const_pointer; + enum { linking_policy = boost::intrusive::normal_link }; + static node_ptr to_node_ptr (value_type &value) { return node_ptr(&value); } + static const_node_ptr to_node_ptr (const value_type &value) { return const_node_ptr(&value); } + static pointer to_value_ptr(node_ptr n) { return static_cast(n); } + static const_pointer to_value_ptr(const_node_ptr n) { return static_cast(n); } +}; +//] + +//[doc_advanced_value_traits_containers +//Now define two intrusive lists. Both lists will use the same algorithms: +// circular_list_algorithms +typedef boost::intrusive::list > Value1List; +typedef boost::intrusive::list > Value2List; +//] + +//[doc_advanced_value_traits_test +int main() +{ + typedef std::vector Vect1; + typedef std::vector Vect2; + + //Create values, with a different internal number + Vect1 values1; + Vect2 values2; + for(int i = 0; i < 100; ++i){ + value_1 v1; v1.id_ = i; values1.push_back(v1); + value_2 v2; v2.id_ = (float)i; values2.push_back(v2); + } + + //Create the lists with the objects + Value1List list1(values1.begin(), values1.end()); + Value2List list2(values2.begin(), values2.end()); + + //Now test both lists + Value1List::const_iterator bit1(list1.begin()), bitend1(list1.end()); + Value2List::const_iterator bit2(list2.begin()), bitend2(list2.end()); + + Vect1::const_iterator it1(values1.begin()), itend1(values1.end()); + Vect2::const_iterator it2(values2.begin()), itend2(values2.end()); + + //Test the objects inserted in our lists + for(; it1 != itend1; ++it1, ++bit1, ++it2, ++bit2){ + if(&*bit1 != &*it1 || &*bit2 != &*it2) return false; + } + return 0; +} +//] diff --git a/example/doc_assoc_optimized_code.cpp b/example/doc_assoc_optimized_code.cpp new file mode 100644 index 0000000..594c643 --- /dev/null +++ b/example/doc_assoc_optimized_code.cpp @@ -0,0 +1,270 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_assoc_optimized_code_normal_find +#include +#include +#include + +using namespace boost::intrusive; + +// Hash function for strings +struct StrHasher +{ + std::size_t operator()(const char *str) const + { + std::size_t seed = 0; + for(; *str; ++str) boost::hash_combine(seed, *str); + return seed; + } +}; + +class Expensive : public set_base_hook<>, public unordered_set_base_hook<> +{ + std::string key_; + // Other members... + + public: + Expensive(const char *key) + : key_(key) + {} //other expensive initializations... + + const std::string & get_key() const + { return key_; } + + friend bool operator < (const Expensive &a, const Expensive &b) + { return a.key_ < b.key_; } + + friend bool operator == (const Expensive &a, const Expensive &b) + { return a.key_ == b.key_; } + + friend std::size_t hash_value(const Expensive &object) + { return StrHasher()(object.get_key().c_str()); } +}; + +// A set and unordered_set that store Expensive objects +typedef set::value_traits > Set; +typedef unordered_set:: + value_traits > UnorderedSet; + +// Search functions +Expensive *get_from_set(const char* key, Set &set) +{ + Set::iterator it = set.find(Expensive(key)); + if( it == set.end() ) return 0; + return &*it; +} + +Expensive *get_from_unordered_set + (const char* key, UnorderedSet &unordered_set) +{ + UnorderedSet::iterator it = + unordered_set.find(Expensive (key)); + if( it == unordered_set.end() ) return 0; + return &*it; +} +//] + +//[doc_assoc_optimized_code_optimized_find +// These compare Expensive and a c-string +struct StrExpComp +{ + bool operator()(const char *str, const Expensive &c) const + { return std::strcmp(str, c.get_key().c_str()) < 0; } + + bool operator()(const Expensive &c, const char *str) const + { return std::strcmp(c.get_key().c_str(), str) < 0; } +}; + +struct StrExpEqual +{ + bool operator()(const char *str, const Expensive &c) const + { return std::strcmp(str, c.get_key().c_str()) == 0; } + + bool operator()(const Expensive &c, const char *str) const + { return std::strcmp(c.get_key().c_str(), str) == 0; } +}; + +// Optimized search functions +Expensive *get_from_set_optimized(const char* key, Set &set) +{ + Set::iterator it = set.find(key, StrExpComp()); + if( it == set.end() ) return 0; + return &*it; +} + +Expensive *get_from_unordered_set_optimized + (const char* key, UnorderedSet &unordered_set) +{ + UnorderedSet::iterator it = + unordered_set.find(key, StrHasher(), StrExpEqual()); + if( it == unordered_set.end() ) return 0; + return &*it; +} +//] + +//[doc_assoc_optimized_code_normal_insert +// Insertion functions +bool insert_to_set(const char* key, Set &set) +{ + Expensive *pobject = new Expensive(key); + bool success = set.insert(*pobject).second; + if(!success) delete pobject; + return success; +} + +bool insert_to_unordered_set + (const char* key, UnorderedSet &unordered_set) +{ + Expensive *pobject = new Expensive(key); + bool success = unordered_set.insert(*pobject).second; + if(!success) delete pobject; + return success; +} +//] + +//[doc_assoc_optimized_code_optimized_insert +// Optimized insertion functions +bool insert_to_set_optimized(const char* key, Set &set) +{ + Set::insert_commit_data insert_data; + bool success = set.insert_check + (key, StrExpComp(), insert_data).second; + if(success) + set.insert_commit(*new Expensive(key), insert_data); + return success; +} + +bool insert_to_unordered_set_optimized + (const char* key, UnorderedSet &unordered_set) +{ + UnorderedSet::insert_commit_data insert_data; + bool success = unordered_set.insert_check + (key, StrHasher(), StrExpEqual(), insert_data).second; + if(success) + unordered_set.insert_commit(*new Expensive(key), insert_data); + return success; +} +//] + +int main() +{ + Set set; + UnorderedSet::bucket_type buckets[10]; + UnorderedSet unordered_set(buckets, 10); + + const char * const expensive_key + = "A long string that avoids small string optimization"; + + Expensive value(expensive_key); + + if(get_from_set(expensive_key, set)){ + return 1; + } + + if(get_from_unordered_set(expensive_key, unordered_set)){ + return 1; + } + + if(get_from_set_optimized(expensive_key, set)){ + return 1; + } + + if(get_from_unordered_set_optimized(expensive_key, unordered_set)){ + return 1; + } + + Set::iterator setit = set.insert(value).first; + UnorderedSet::iterator unordered_setit = unordered_set.insert(value).first; + + if(!get_from_set(expensive_key, set)){ + return 1; + } + + if(!get_from_unordered_set(expensive_key, unordered_set)){ + return 1; + } + + if(!get_from_set_optimized(expensive_key, set)){ + return 1; + } + + if(!get_from_unordered_set_optimized(expensive_key, unordered_set)){ + return 1; + } + + set.erase(setit); + unordered_set.erase(unordered_setit); + + if(!insert_to_set(expensive_key, set)){ + return 1; + } + + if(!insert_to_unordered_set(expensive_key, unordered_set)){ + return 1; + } + + { + Expensive *ptr = &*set.begin(); + set.clear(); + delete ptr; + } + + { + Expensive *ptr = &*unordered_set.begin(); + unordered_set.clear(); + delete ptr; + } + + if(!insert_to_set_optimized(expensive_key, set)){ + return 1; + } + + if(!insert_to_unordered_set_optimized(expensive_key, unordered_set)){ + return 1; + } + + { + Expensive *ptr = &*set.begin(); + set.clear(); + delete ptr; + } + + { + Expensive *ptr = &*unordered_set.begin(); + unordered_set.clear(); + delete ptr; + } + + setit = set.insert(value).first; + unordered_setit = unordered_set.insert(value).first; + + if(insert_to_set(expensive_key, set)){ + return 1; + } + + if(insert_to_unordered_set(expensive_key, unordered_set)){ + return 1; + } + + if(insert_to_set_optimized(expensive_key, set)){ + return 1; + } + + if(insert_to_unordered_set_optimized(expensive_key, unordered_set)){ + return 1; + } + + set.erase(value); + unordered_set.erase(value); + + return 0; +} diff --git a/example/doc_auto_unlink.cpp b/example/doc_auto_unlink.cpp new file mode 100644 index 0000000..e068aec --- /dev/null +++ b/example/doc_auto_unlink.cpp @@ -0,0 +1,83 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_auto_unlink_code +#include +#include + +using namespace boost::intrusive; + +class MyClass : public list_base_hook + //This hook removes the node in the destructor +{ + int int_; + + public: + MyClass(int i = 0) : int_(i) {} + void unlink() { list_base_hook::unlink(); } + bool is_linked() { return list_base_hook::is_linked(); } + int get() const { return int_; } +}; + +//Define a list that will store MyClass +//using the public base hook +//The list can't have constant-time size! +typedef list< list_base_hook:: + value_traits, false > List; + +int main() +{ + //Create the list + List list; + { + //Create myclass and check it's linked + MyClass myclass; + assert(myclass.is_linked() == false); + + //Insert the object + list.push_back(myclass); + + //Check that we have inserted the object + assert(list.empty() == false); + assert(&list.front() == &myclass); + assert(myclass.is_linked() == true); + + //Now myclass' destructor will unlink it + //automatically + } + + //Check auto-unlink has been executed + assert(list.empty() == true); + + { + //Now test the unlink() function + + //Create myclass and check it's linked + MyClass myclass; + assert(myclass.is_linked() == false); + + //Insert the object + list.push_back(myclass); + + //Check that we have inserted the object + assert(list.empty() == false); + assert(&list.front() == &myclass); + assert(myclass.is_linked() == true); + + //Now unlink the node + myclass.unlink(); + + //Check auto-unlink has been executed + assert(list.empty() == true); + } + return 0; +} +//] diff --git a/example/doc_clone_from.cpp b/example/doc_clone_from.cpp new file mode 100644 index 0000000..f61f3c4 --- /dev/null +++ b/example/doc_clone_from.cpp @@ -0,0 +1,78 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_clone_from +#include +#include +#include + +//A class that can be inserted in an intrusive list +class my_class + : public boost::intrusive::list_base_hook<> +{ + public: + friend bool operator==(const my_class &a, const my_class &b) + { return a.int_ == b.int_; } + + int int_; + + //... +}; + +//Definition of the intrusive list +typedef boost::intrusive::list< my_class::value_traits > my_class_list; + +//Cloner object function +class new_cloner +{ + public: + my_class *operator()(const my_class &clone_this) + { return new my_class(clone_this); } +}; + +//The destroyer object function +class delete_destroyer +{ + public: + void operator()(my_class *delete_this) + { delete delete_this; } +}; + +int main() +{ + const int MaxElem = 100; + std::vector nodes(MaxElem); + + //Fill all the nodes and insert them in the list + my_class_list list; + + for(int i = 0; i < MaxElem; ++i){ + nodes[i].int_ = i; + } + list.insert(list.end(), nodes.begin(), nodes.end()); + + //Now clone "list" using "new" and "delete" object functions + my_class_list cloned_list; + cloned_list.clone_from(list, new_cloner(), delete_destroyer()); + + //Test that both are equal + if(cloned_list != list){ + std::cout << "Both lists are different" << std::endl; + } + else{ + std::cout << "Both lists are equal" << std::endl; + } + + //Don't forget to free the memory from the second list + cloned_list.clear_and_destroy(delete_destroyer()); + return 0; +} +//] diff --git a/example/doc_entity.cpp b/example/doc_entity.cpp new file mode 100644 index 0000000..efbb96c --- /dev/null +++ b/example/doc_entity.cpp @@ -0,0 +1,59 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_entity_code +#include + +//A class that can be inserted in an intrusive list +class entity + : public boost::intrusive::list_base_hook<> +{ + public: + virtual ~entity(); + //... +}; + +//"some_entity" derives from "entity" +class some_entity : public entity +{/**/}; + +//Definition of the intrusive list +typedef boost::intrusive::list< entity::value_traits > entity_list; + +//A global list +entity_list list; + +//The destructor removes itself from the list +entity::~entity() +{ list.erase(entity_list::iterator_to(*this)); } + +//Function to insert a new "some_entity" in the list +void insert_some_entity() +{ list.push_back (*new some_entity(/*...*/)); } + +//Function to clear an entity from the intrusive list +void clear_list () +{ + // entity's destructor removes itself from the list implicitly + while (!list.empty()) + delete &list.front(); +} + +int main() +{ + //Insert some new entities + insert_some_entity(); + insert_some_entity(); + //list's destructor will free objects + return 0; +} + +//] diff --git a/example/doc_erasing_and_destroying.cpp b/example/doc_erasing_and_destroying.cpp new file mode 100644 index 0000000..d6634ad --- /dev/null +++ b/example/doc_erasing_and_destroying.cpp @@ -0,0 +1,73 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_erasing_and_destroying +#include + +//A class that can be inserted in an intrusive list +class my_class + : public boost::intrusive::list_base_hook<> +{ + public: + my_class(int i) + : int_(i) + {} + + int int_; + //... +}; + +//Definition of the intrusive list +typedef boost::intrusive::list< my_class::value_traits > my_class_list; + +//The predicate function +class is_even +{ + public: + bool operator()(const my_class &c) const + { return 0 == (c.int_ % 2); } +}; + +//The destroyer object function +class delete_destroyer +{ + public: + void operator()(my_class *delete_this) + { delete delete_this; } +}; + +int main() +{ + const int MaxElem = 100; + + //Fill all the nodes and insert them in the list + my_class_list list; + + try{ + //Insert new objects in the container + for(int i = 0; i < MaxElem; ++i){ + list.push_back(*new my_class(i)); + } + + //Now use remove_and_destroy_if to erase and delete the objects + list.remove_and_destroy_if(is_even(), delete_destroyer()); + } + catch(...){ + //If something throws, make sure that all the memory is freed + list.clear_and_destroy(delete_destroyer()); + throw; + } + + //Destroy remaining elements + list.erase_and_destroy(list.begin(), list.end(), delete_destroyer()); + return 0; +} +//] diff --git a/example/doc_how_to_use.cpp b/example/doc_how_to_use.cpp new file mode 100644 index 0000000..5342b46 --- /dev/null +++ b/example/doc_how_to_use.cpp @@ -0,0 +1,86 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_how_to_use_code +#include +#include + +using namespace boost::intrusive; + +class MyClass : public list_base_hook<> +{ + int int_; + + public: + list_member_hook<> member_hook_; + + MyClass(int i) : int_(i) {} + int get() const { return int_; } +}; + +//Define a list that will store MyClass using the base hook +typedef list< list_base_hook<>::value_traits > + BaseList; + +//Define a list that will store MyClass using the member hook +typedef list< list_member_hook<>:: + value_traits + > MemberList; + +int main() +{ + typedef std::vector Vect; + typedef Vect::iterator VectIt; + typedef Vect::reverse_iterator VectRit; + + //Create several MyClass objects, each one + //with a different internal number + Vect myclassvector; + for(int i = 0; i < 100; ++i) + myclassvector.push_back(MyClass(i)); + + BaseList baselist; + MemberList memberlist; + + //Now insert them in the reverse order + //in the base hook intrusive list + for(VectIt it(myclassvector.begin()), itend(myclassvector.end()) + ; it != itend ; ++it){ + baselist.push_front(*it); + } + + //Now insert them in the same order as in vector in the + //member hook intrusive list + for(VectIt it(myclassvector.begin()), itend(myclassvector.end()) + ; it != itend; ++it){ + memberlist.push_back(*it); + } + + //Now test lists + { + BaseList::reverse_iterator rbit(baselist.rbegin()), rbitend(baselist.rend()); + MemberList::iterator mit(memberlist.begin()), mitend(memberlist.end()); + VectIt it(myclassvector.begin()), itend(myclassvector.end()); + + //Test the objects inserted in the base hook list + for(; it != itend; ++it, ++rbit){ + if(&*rbit != &*it) return 1; + } + + //Test the objects inserted in the member hook list + for(it = myclassvector.begin(); it != itend; ++it, ++mit){ + if(&*mit != &*it) return 1; + } + } + + return 0; +} +//] diff --git a/example/doc_iterator_from_value.cpp b/example/doc_iterator_from_value.cpp new file mode 100644 index 0000000..c4135ce --- /dev/null +++ b/example/doc_iterator_from_value.cpp @@ -0,0 +1,94 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_iterator_from_value +#include +#include +#include +#include + +class intrusive_data +{ + int data_id_; + public: + + int get() const { return data_id_; } + void set(int id) { data_id_ = id; } + + //This class can be inserted in an intrusive list + typedef boost::intrusive:: + list_member_hook<> list_member_hook_t; + list_member_hook_t list_hook_; + + //This class can be inserted in an intrusive unordered_set + typedef boost::intrusive:: + unordered_set_member_hook<> unordered_set_member_hook_t; + unordered_set_member_hook_t unordered_set_hook_; + + //Comparison operators + friend bool operator==(const intrusive_data &a, const intrusive_data &b) + { return a.get() == b.get(); } + + friend bool operator!=(const intrusive_data &a, const intrusive_data &b) + { return a.get() != b.get(); } +}; + +//The hash function +std::size_t hash_value(const intrusive_data &i) +{ return boost::hash()(i.get()); } + +//Definition of the intrusive list that will hold intrusive_data +typedef boost::intrusive::list< intrusive_data::list_member_hook_t:: + value_traits > list_t; + +//Definition of the intrusive unordered_set that will hold intrusive_data +typedef boost::intrusive::unordered_set< intrusive_data::unordered_set_member_hook_t:: + value_traits > unordered_set_t; + +int main() +{ + //Create MaxElem objects + const int MaxElem = 100; + std::vector nodes(MaxElem); + + //Declare the intrusive containers + list_t list; + unordered_set_t::bucket_type buckets[MaxElem]; + unordered_set_t unordered_set(buckets, MaxElem); + + //Initialize all the nodes + for(int i = 0; i < MaxElem; ++i) + nodes[i].set(i); + + //Now insert them in both intrusive containers + list.insert(list.end(), nodes.begin(), nodes.end()); + unordered_set.insert(nodes.begin(), nodes.end()); + + //Now check list::iterator_to + //which is an static member function + list_t::iterator list_it(list.begin()); + for(int i = 0; i < MaxElem; ++i, ++list_it) + if(list_t::iterator_to(nodes[i]) != list_it) + return 1; + + //Now check unordered_set::iterator_to (which is a member function) + //and unordered_set::local_current (which is an static member function) + unordered_set_t::iterator unordered_set_it(unordered_set.begin()); + for(int i = 0; i < MaxElem; ++i, ++unordered_set_it){ + if(unordered_set.iterator_to(nodes[i]) != unordered_set.find(nodes[i])) + return 1; + if(*unordered_set_t::local_iterator_to(nodes[i]) != *unordered_set.find(nodes[i])) + return 1; + } + + return 0; +} +//] diff --git a/example/doc_list.cpp b/example/doc_list.cpp new file mode 100644 index 0000000..e695bb6 --- /dev/null +++ b/example/doc_list.cpp @@ -0,0 +1,90 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_list_code +#include +#include + +using namespace boost::intrusive; + +class MyClass : public list_base_hook<> //This is a derivation hook +{ + int int_; + + public: + //This is a member hook + list_member_hook<> member_hook_; + + MyClass(int i) + : int_(i) + {} + + int get() const + { return int_; } +}; + +//Define a list that will store MyClass using the public base hook +typedef list< list_base_hook<>::value_traits > BaseList; + +//Define a list that will store MyClass using the public member hook +typedef list< list_member_hook<>::value_traits > MemberList; + +int main() +{ + typedef std::vector Vect; + typedef Vect::iterator VectIt; + typedef Vect::reverse_iterator VectRit; + + //Create several MyClass objects, each one + //with a different internal number + Vect myclassvector; + for(int i = 0; i < 100; ++i) + myclassvector.push_back(MyClass(i)); + + BaseList baselist; + MemberList memberlist; + + //Now insert them in the reverse order + //in the base hook intrusive list + for(VectIt it(myclassvector.begin()), itend(myclassvector.end()) + ; it != itend + ; ++it){ + baselist.push_front(*it); + } + + //Now insert them in the same order as in vector in the + //member hook intrusive list + for(VectIt it(myclassvector.begin()), itend(myclassvector.end()) + ; it != itend + ; ++it){ + memberlist.push_back(*it); + } + + //Now test lists + { + BaseList::reverse_iterator rbit(baselist.rbegin()), rbitend(baselist.rend()); + MemberList::iterator mit(memberlist.begin()), mitend(memberlist.end()); + VectIt it(myclassvector.begin()), itend(myclassvector.end()); + + //Test the objects inserted in the base hook list + for(; it != itend; ++it, ++rbit){ + if(&*rbit != &*it) return 1; + } + + //Test the objects inserted in the member hook list + for(it = myclassvector.begin(); it != itend; ++it, ++mit){ + if(&*mit != &*it) return 1; + } + } + + return 0; +} +//] diff --git a/example/doc_list_algorithms.cpp b/example/doc_list_algorithms.cpp new file mode 100644 index 0000000..1e63330 --- /dev/null +++ b/example/doc_list_algorithms.cpp @@ -0,0 +1,66 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_list_algorithms_code +#include +#include + +struct my_node +{ + my_node *next_, *prev_; + //other members... +}; + +//Define our own list_node_traits +struct my_list_node_traits +{ + typedef my_node node; + typedef my_node * node_ptr; + typedef const my_node * const_node_ptr; + static node_ptr get_next(const_node_ptr n) { return n->next_; } + static void set_next(node_ptr n, node_ptr next) { n->next_ = next; } + static node *get_previous(const_node_ptr n) { return n->prev_; } + static void set_previous(node_ptr n, node_ptr prev){ n->prev_ = prev; } +}; + +int main() +{ + typedef boost::intrusive::circular_list_algorithms algo; + my_node one, two, three; + + //Create an empty doubly linked list container: + //"one" will be the first node of the container + algo::init(&one); + assert(algo::count(&one) == 1); + + //Now add a new node before "one" + algo::link_before(&one, &two); + assert(algo::count(&one) == 2); + + //Now add a new node after "two" + algo::link_after(&two, &three); + assert(algo::count(&one) == 3); + + //Now unlink the node after one + algo::unlink(&three); + assert(algo::count(&one) == 2); + + //Now unlink two + algo::unlink(&two); + assert(algo::count(&one) == 1); + + //Now unlink one + algo::unlink(&one); + assert(algo::count(&one) == 1); + + return 0; +} +//] diff --git a/example/doc_offset_ptr.cpp b/example/doc_offset_ptr.cpp new file mode 100644 index 0000000..e3765a2 --- /dev/null +++ b/example/doc_offset_ptr.cpp @@ -0,0 +1,90 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_offset_ptr_0 +#include +#include + +class shared_memory_data +{ + int data_id_; + public: + + int get() const { return data_id_; } + void set(int id) { data_id_ = id; } + + //Declare the hook with an offset_ptr from Boost.Interprocess + //to make this class compatible with shared memory + typedef boost::intrusive::list_member_hook + < boost::intrusive::safe_link + , boost::interprocess::offset_ptr > member_hook_t; + member_hook_t list_hook_; +}; + +//] + +//[doc_offset_ptr_1 + +#include +#include + +//Definition of the shared memory friendly intrusive list +typedef boost::intrusive::list< shared_memory_data::member_hook_t:: + value_traits > shm_list_t; + +int main() +{ + //Now create an intrusive list in shared memory: + //nodes and the container itself must be created in shared memory + const int MaxElem = 100; + const int ShmSize = 50000; + const char *ShmName = "MySharedMemory"; + + using namespace boost::interprocess; + + //Erase all old shared memory + shared_memory_object::remove(ShmName); + managed_shared_memory shm(create_only, ShmName, ShmSize); + + //Create all nodes in shared memory using a shared memory vector + //See Boost.Interprocess documentation for more information on this + typedef allocator< shared_memory_data + , managed_shared_memory::segment_manager> shm_allocator_t; + typedef vector shm_vector_t; + shm_allocator_t shm_alloc(shm.get_segment_manager()); + shm_vector_t *pshm_vect = shm.construct(anonymous_instance)(shm_alloc); + pshm_vect->resize(MaxElem); + + //Initialize all the nodes + for(int i = 0; i < MaxElem; ++i){ + (*pshm_vect)[i].set(i); + } + + //Now create the shared memory intrusive list + shm_list_t *plist = shm.construct(anonymous_instance)(); + plist->insert(plist->end(), pshm_vect->begin(), pshm_vect->end()); + + //Check all the inserted nodes + int checker = 0; + for( shm_list_t::const_iterator it = plist->begin(), itend(plist->end()) + ; it != itend + ; ++it, ++checker){ + if(it->get() != checker){ + return false; + } + } + + //Now delete the list and after that, the nodes + shm.destroy_ptr(plist); + shm.destroy_ptr(pshm_vect); + return 0; +} +//] diff --git a/example/doc_rbtree_algorithms.cpp b/example/doc_rbtree_algorithms.cpp new file mode 100644 index 0000000..e372859 --- /dev/null +++ b/example/doc_rbtree_algorithms.cpp @@ -0,0 +1,82 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_rbtree_algorithms_code +#include +#include + +struct my_node +{ + my_node(int i = 0) + : int_(i) + {} + my_node *parent_, *left_, *right_; + int color_; + //other members + int int_; +}; + +//Define our own rbtree_node_traits +struct my_rbtree_node_traits +{ + typedef my_node node; + typedef my_node * node_ptr; + typedef const my_node * const_node_ptr; + typedef int color; + static node_ptr get_parent(const_node_ptr n) { return n->parent_; } + static void set_parent(node_ptr n, node_ptr parent){ n->parent_ = parent; } + static node_ptr get_left(const_node_ptr n) { return n->left_; } + static void set_left(node_ptr n, node_ptr left) { n->left_ = left; } + static node_ptr get_right(const_node_ptr n) { return n->right_; } + static void set_right(node_ptr n, node_ptr right) { n->right_ = right; } + static color get_color(const_node_ptr n) { return n->color_; } + static void set_color(node_ptr n, color c) { n->color_ = c; } + static color black() { return color(0); } + static color red() { return color(1); } +}; + +struct node_ptr_compare +{ + bool operator()(my_node *a, my_node *b) + { return a->int_ < b->int_; } +}; + +int main() +{ + typedef boost::intrusive::rbtree_algorithms algo; + my_node header, two(2), three(3); + + //Create an empty rbtree container: + //"header" will be the header node of the tree + algo::init_header(&header); + + //Now insert node "two" in the tree using the sorting functor + algo::insert_equal_upper_bound(&header, &two, node_ptr_compare()); + + //Now insert node "three" in the tree using the sorting functor + algo::insert_equal_lower_bound(&header, &three, node_ptr_compare()); + + //Now take the first node (the left node of the header) + my_node *n = header.left_; + assert(n == &two); + + //Now go to the next node + n = algo::next_node(n); + assert(n == &three); + + //Erase a node just using a pointer to it + algo::unlink_and_rebalance(n); + + //Erase a node using also the header (faster) + algo::erase(&header, n); + return 0; +} +//] diff --git a/example/doc_set.cpp b/example/doc_set.cpp new file mode 100644 index 0000000..a624a1e --- /dev/null +++ b/example/doc_set.cpp @@ -0,0 +1,95 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_set_code +#include +#include +#include + +using namespace boost::intrusive; + + //This is a base hook +class MyClass : public set_base_hook<> +{ + int int_; + + public: + //This is a member hook + set_member_hook<> member_hook_; + + MyClass(int i) + : int_(i) + {} + int get() const + { return int_; } + friend bool operator< (const MyClass &a, const MyClass &b) + { return a.get() < b.get(); } + friend bool operator> (const MyClass &a, const MyClass &b) + { return a.get() > b.get(); } +}; + +//Define an set that will store MyClass +//in reverse order using the public base hook +typedef set< set_base_hook<>::value_traits + , std::greater > BaseSet; + +//Define an multiset that will store MyClass +//using the public member hook +typedef multiset< set_member_hook<>:: + value_traits + , std::less > MemberIMultiset; + +int main() +{ + typedef std::vector Vect; + typedef Vect::iterator VectIt; + typedef Vect::reverse_iterator VectRit; + + //Create several MyClass objects, each one + //with a different internal number + Vect myclassvector; + for(int i = 0; i < 100; ++i) + myclassvector.push_back(MyClass(i)); + + BaseSet baseset; + MemberIMultiset membermultiset; + + //Now insert them in the reverse order + //in the base hook intrusive set + for(VectIt it(myclassvector.begin()), itend(myclassvector.end()) + ; it != itend; ++it) + baseset.insert(*it); + + //Now insert them in the same order as in vector in the + //member hook intrusive set + for(VectIt it(myclassvector.begin()), itend(myclassvector.end()) + ; it != itend; ++it) + membermultiset.insert(*it); + + //Now test sets + { + BaseSet::reverse_iterator rbit(baseset.rbegin()), rbitend(baseset.rend()); + MemberIMultiset::iterator mit(membermultiset.begin()), mitend(membermultiset.end()); + VectIt it(myclassvector.begin()), itend(myclassvector.end()); + + //Test the objects inserted in the base hook set + for(; it != itend; ++it, ++rbit){ + if(&*rbit != &*it) return 1; + } + + //Test the objects inserted in the member hook set + for(it = myclassvector.begin(); it != itend; ++it, ++mit){ + if(&*mit != &*it) return 1; + } + } + return 0; +} +//] diff --git a/example/doc_slist.cpp b/example/doc_slist.cpp new file mode 100644 index 0000000..67d4bde --- /dev/null +++ b/example/doc_slist.cpp @@ -0,0 +1,92 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_slist_code +#include +#include + +using namespace boost::intrusive; + + //This is a base hook +class MyClass : public slist_base_hook<> +{ + int int_; + + public: + //This is a member hook + slist_member_hook<> member_hook_; + + MyClass(int i) + : int_(i) + {} + + int get() const + { return int_; } +}; + +//Define an slist that will store MyClass using the public base hook +typedef slist< slist_base_hook<>::value_traits > BaseList; + +//Define an slist that will store MyClass using the public member hook +typedef slist< slist_member_hook<>::value_traits > MemberList; + +int main() +{ + typedef std::vector Vect; + typedef Vect::iterator VectIt; + typedef Vect::reverse_iterator VectRit; + + //Create several MyClass objects, each one + //with a different internal number + Vect myclassvector; + for(int i = 0; i < 100; ++i) + myclassvector.push_back(MyClass(i)); + + BaseList baselist; + MemberList memberlist; + + //Now insert them in the reverse order + //in the base hook intrusive list + for(VectIt it(myclassvector.begin()), itend(myclassvector.end()) + ; it != itend + ; ++it){ + baselist.push_front(*it); + } + + //Now insert them in the same order as in vector in the + //member hook intrusive 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(myclassvector.rbegin()), ritend(myclassvector.rend()); + VectIt it(myclassvector.begin()), itend(myclassvector.end()); + + //Test the objects inserted in the base hook list + for(; rit != ritend; ++rit, ++bit){ + if(&*bit != &*rit) return 1; + } + + //Test the objects inserted in the member hook list + for(; it != itend; ++it, ++mit){ + if(&*mit != &*it) return 1; + } + } + + return 0; +} +//] diff --git a/example/doc_slist_algorithms.cpp b/example/doc_slist_algorithms.cpp new file mode 100644 index 0000000..779b6e8 --- /dev/null +++ b/example/doc_slist_algorithms.cpp @@ -0,0 +1,60 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_slist_algorithms_code +#include +#include + +struct my_node +{ + my_node *next_; + //other members... +}; + +//Define our own slist_node_traits +struct my_slist_node_traits +{ + typedef my_node node; + typedef my_node * node_ptr; + typedef const my_node * const_node_ptr; + static node_ptr get_next(const_node_ptr n) { return n->next_; } + static void set_next(node_ptr n, node_ptr next) { n->next_ = next; } +}; + +int main() +{ + typedef boost::intrusive::circular_slist_algorithms algo; + my_node one, two, three; + + //Create an empty singly linked list container: + //"one" will be the first node of the container + algo::init(&one); + assert(algo::count(&one) == 1); + + //Now add a new node + algo::link_after(&one, &two); + assert(algo::count(&one) == 2); + + //Now add a new node after "one" + algo::link_after(&one, &three); + assert(algo::count(&one) == 3); + + //Now unlink the node after one + algo::unlink_after(&one); + assert(algo::count(&one) == 2); + + //Now unlink two + algo::unlink(&two); + assert(algo::count(&one) == 1); + + return 0; +} +//] diff --git a/example/doc_unordered_set.cpp b/example/doc_unordered_set.cpp new file mode 100644 index 0000000..c4e1c65 --- /dev/null +++ b/example/doc_unordered_set.cpp @@ -0,0 +1,110 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_unordered_set_code +#include +#include +#include +#include + +using namespace boost::intrusive; + + //This is a derivation hook +class MyClass : public unordered_set_base_hook<> +{ + int int_; + + public: + //This is a member hook + unordered_set_member_hook<> member_hook_; + + MyClass(int i) + : int_(i) + {} + + int get() const + { return int_; } + + friend bool operator== (const MyClass &a, const MyClass &b) + { return a.get() == b.get(); } + + friend bool operator> (const MyClass &a, const MyClass &b) + { return a.get() > b.get(); } +}; + +std::size_t hash_value(const MyClass &value) +{ return std::size_t(value.get()); } + +//Define an unordered_set that will store MyClass +//in reverse order using the public base hook +typedef unordered_set< unordered_set_base_hook<>:: + value_traits > BaseSet; + +//Define an unordered_multiset that will store MyClass +//using the public member hook +typedef unordered_multiset< unordered_set_member_hook<>:: + value_traits > MemberMultiSet; + +int main() +{ + typedef std::vector Vect; + typedef Vect::iterator VectIt; + typedef Vect::reverse_iterator VectRit; + + //Create a vector with 100 different MyClass objects, + //each one with a different internal number + Vect myclassvector; + for(int i = 0; i < 100; ++i) + myclassvector.push_back(MyClass(i)); + + //Create a copy of the vector + Vect myclassvector2(myclassvector); + + //Create a bucket array for base_set + BaseSet::bucket_type base_buckets[100]; + + //Create a bucket array for member_multi_set + MemberMultiSet::bucket_type member_buckets[200]; + + //Create a the unordered_set and unordered_multiset, + //taking buckets as arguments + BaseSet base_set(base_buckets, 100); + MemberMultiSet member_multi_set(member_buckets, 200); + + //Now insert myclassvector's elements in the unordered_set + for(VectIt it(myclassvector.begin()), itend(myclassvector.end()) + ; it != itend; ++it){ + base_set.insert(*it); + } + + //Now insert myclassvector's and myclassvector2's elements in the unordered_multiset + for(VectIt it(myclassvector.begin()), itend(myclassvector.end()), + it2(myclassvector2.begin()),itend2(myclassvector2.end()) + ; it != itend; ++it, ++it2){ + member_multi_set.insert(*it); + member_multi_set.insert(*it2); + } + + //Now find every element + { + VectIt it(myclassvector.begin()), itend(myclassvector.end()); + + for(; it != itend; ++it){ + //base_set should contain one element for each key + if(base_set.count(*it) != 1) return 1; + //member_multi_set should contain two elements for each key + if(member_multi_set.count(*it) != 2) return 1; + } + } + return 0; +} +//] + diff --git a/example/doc_value_traits.cpp b/example/doc_value_traits.cpp new file mode 100644 index 0000000..f091da2 --- /dev/null +++ b/example/doc_value_traits.cpp @@ -0,0 +1,99 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_value_traits_code_legacy +#include +#include +#include +#include + +//This node is the legacy type we can't modify and we want to insert in +//intrusive list and slist containers using only two pointers, since +//we know the object will never be at the same time in both lists. +struct legacy_value +{ + legacy_value *prev_; + legacy_value *next_; + int id_; +}; +//] + +//[doc_value_traits_value_traits +//Define our own NodeTraits that will configure singly and doubly linked +//list algorithms. Note that this node traits is compatible with +//circular_slist_algorithms and circular_list_algorithms. +struct legacy_node_traits +{ + typedef legacy_value node; + typedef legacy_value * node_ptr; + typedef const legacy_value * const_node_ptr; + + static node *get_next(const node *n) { return n->next_; } + static void set_next(node *n, node *next) { n->next_ = next; } + static node *get_previous(const node *n) { return n->prev_; } + static void set_previous(node *n, node *prev) { n->prev_ = prev; } +}; + +//This ValueTraits will configure list and slist. In this case, +//legacy_node_traits::node is the same as the +//legacy_value_traits::value_type so to_node_ptr/to_value_ptr +//functions are trivial. +struct legacy_value_traits +{ + typedef legacy_node_traits node_traits; + typedef node_traits::node_ptr node_ptr; + typedef node_traits::const_node_ptr const_node_ptr; + typedef legacy_value value_type; + typedef legacy_value * pointer; + typedef const legacy_value * const_pointer; + enum { linking_policy = boost::intrusive::normal_link }; + static node_ptr to_node_ptr (value_type &value) { return node_ptr(&value); } + static const_node_ptr to_node_ptr (const value_type &value) { return const_node_ptr(&value); } + static pointer to_value_ptr(node_ptr n) { return pointer(n); } + static const_pointer to_value_ptr(const_node_ptr n) { return const_pointer(n); } +}; +//] + +//[doc_value_traits_test +//Now define an intrusive list and slist that will store legacy_value objects +typedef boost::intrusive::list LegacyAbiList; +typedef boost::intrusive::slist LegacyAbiSlist; + +template +bool test_list() +{ + typedef std::vector Vect; + + //Create legacy_value objects, with a different internal number + Vect legacy_vector; + for(int i = 0; i < 100; ++i){ + legacy_value value; value.id_ = i; legacy_vector.push_back(value); + } + + //Create the list with the objects + List mylist(legacy_vector.begin(), legacy_vector.end()); + + //Now test both lists + typename List::const_iterator bit(mylist.begin()), bitend(mylist.end()); + typename Vect::const_iterator it(legacy_vector.begin()), itend(legacy_vector.end()); + + //Test the objects inserted in our list + for(; it != itend; ++it, ++bit){ + if(&*bit != &*it) return false; + } + return true; +} + +int main() +{ + return test_list() && test_list() ? 0 : 1; +} +//] diff --git a/example/doc_window.cpp b/example/doc_window.cpp new file mode 100644 index 0000000..7154c46 --- /dev/null +++ b/example/doc_window.cpp @@ -0,0 +1,85 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +//[doc_window_code +#include + +using namespace boost::intrusive; + +//An abstract class that can be inserted in an intrusive list +class Window : public list_base_hook<> +{ + public: + //This is a container those value is an abstract class: you can't do this with std::list. + typedef list< list_base_hook<>::value_traits > win_list; + + //An static intrusive list declaration + static win_list all_windows; + + //Constructor. Includes this window in the list + Window() { all_windows.push_back(*this); } + //Destructor. Removes this node from the list + virtual ~Window() { all_windows.erase(win_list::iterator_to(*this)); } + //Pure virtual function to be implemented by derived classes + virtual void Paint() = 0; +}; + +//The static intrusive list declaration +Window::win_list Window::all_windows; + +//Some Window derived classes +class FrameWindow : public Window +{ void Paint(){/**/} }; + +class EditWindow : public Window +{ void Paint(){/**/} }; + +class CanvasWindow : public Window +{ void Paint(){/**/} }; + +//A function that prints all windows stored in the intrusive list +void paint_all_windows() +{ + for(Window::win_list::iterator i(Window::all_windows.begin()) + , e(Window::all_windows.end()) + ; i != e; ++i) + i->Paint(); +} + +//... + +//A class derived from Window +class MainWindow : public Window +{ + FrameWindow frame_; //these are derived from Window too + EditWindow edit_; + CanvasWindow canvas_; + + public: + void Paint(){/**/} + //... +}; + +//Main function +int main() +{ + //When each Window class is created, is + //automatically registered in the global list + MainWindow window; + + //Paint all the windows, sub-windows and so on + paint_all_windows(); + + //All the windows are automatically unregistered + //in their destructors. + return 0; +} +//] diff --git a/perf/Jamfile.v2 b/perf/Jamfile.v2 new file mode 100644 index 0000000..3c9f3d6 --- /dev/null +++ b/perf/Jamfile.v2 @@ -0,0 +1,34 @@ +# Boost Intrusive Library Performance test Jamfile + +# (C) Copyright Ion Gaztaņaga 2006-2007. +# Use, modification and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +# Adapted from John Maddock's TR1 Jamfile.v2 +# Copyright John Maddock 2005. +# Use, modification and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +# this rule enumerates through all the sources and invokes +# the run rule for each source, the result is a list of all +# the run rules, which we can pass on to the test_suite rule: + +rule test_all +{ + local all_rules = ; + + for local fileb in [ glob *.cpp ] + { + all_rules += [ run $(fileb) + : # additional args + : # test-files + : # requirements + ] ; + } + + return $(all_rules) ; +} + +test-suite intrusive_perf : [ test_all r ] ; \ No newline at end of file diff --git a/perf/perf_list.cpp b/perf/perf_list.cpp new file mode 100644 index 0000000..730b936 --- /dev/null +++ b/perf/perf_list.cpp @@ -0,0 +1,534 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +//Includes for tests +#include +#include +#include +#include +#include + +using namespace boost::posix_time; + +//[perf_list_value_type +//Iteration and element count defines +const int NumIter = 100; +const int NumElements = 100000; + +using namespace boost::intrusive; + +template +struct filler { int dummy[10]; }; + +template <> +struct filler {}; + +template //The object for non-intrusive containers +struct test_class : private filler +{ + int i_; + test_class() {} + test_class(int i) : i_(i) {} + friend bool operator <(const test_class &l, const test_class &r) { return l.i_ < r.i_; } + friend bool operator >(const test_class &l, const test_class &r) { return l.i_ > r.i_; } +}; + +template //The object for intrusive containers +struct itest_class : public list_base_hook, public test_class +{ + itest_class() {} + itest_class(int i) : test_class(i) {} +}; + +template //Adapts functors taking values to functors taking pointers +struct func_ptr_adaptor : public FuncObj +{ + typedef typename FuncObj::first_argument_type* first_argument_type; + typedef typename FuncObj::second_argument_type* second_argument_type; + typedef typename FuncObj::result_type result_type; + result_type operator()(first_argument_type a, second_argument_type b) const + { return FuncObj::operator()(*a, *b); } +}; +//] + +template +struct get_ilist //Helps to define an intrusive list from a policy +{ + typedef list_base_hook hook; + typedef list >, false> type; +}; + +template //Helps to define an std list +struct get_list { typedef std::list > type; }; + +template //Helps to define an std pointer list +struct get_ptrlist { typedef std::list*> type; }; + + +//////////////////////////////////////////////////////////////////////// +// +// PUSH_BACK +// +//////////////////////////////////////////////////////////////////////// + +template +void test_intrusive_list_push_back() +{ + typedef typename get_ilist::type ilist; + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + //First create the elements and insert them in the intrusive list + //[perf_list_push_back_intrusive + std::vector objects(NumElements); + ilist l; + for(int i = 0; i < NumElements; ++i) + l.push_back(objects[i]); + //Elements are unlinked in ilist's destructor + //Elements are destroyed in vector's destructor + //] + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_std_list_push_back() +{ + typedef typename get_list::type stdlist; + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + //Now create the std list and insert + //[perf_list_push_back_stdlist + stdlist l; + for(int i = 0; i < NumElements; ++i) + l.push_back(typename stdlist::value_type(i)); + //Elements unlinked and destroyed in stdlist's destructor + //] + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_compact_std_ptrlist_push_back() +{ + typedef typename get_list::type stdlist; + typedef typename get_ptrlist::type stdptrlist; + //Now measure insertion time, including element creation + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + //Create elements and insert their pointer in the list + //[perf_list_push_back_stdptrlist + std::vector objects(NumElements); + stdptrlist l; + for(int i = 0; i < NumElements; ++i) + l.push_back(&objects[i]); + //Pointers to elements unlinked and destroyed in stdptrlist's destructor + //Elements destroyed in vector's destructor + //] + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_disperse_std_ptrlist_push_back() +{ + typedef typename get_list::type stdlist; + typedef typename get_ptrlist::type stdptrlist; + //Now measure insertion time, including element creation + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + //Create elements and insert their pointer in the list + //[perf_list_push_back_disperse_stdptrlist + stdlist objects; stdptrlist l; + for(int i = 0; i < NumElements; ++i){ + objects.push_back(typename stdlist::value_type(i)); + l.push_back(&objects.back()); + } + //Pointers to elements unlinked and destroyed in stdptrlist's destructor + //Elements unlinked and destroyed in stdlist's destructor + //] + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +//////////////////////////////////////////////////////////////////////// +// +// REVERSE +// +//////////////////////////////////////////////////////////////////////// + +//[perf_list_reverse +template +void test_intrusive_list_reverse() +{ + typedef typename get_ilist::type ilist; + //First create the elements + std::vector objects(NumElements); + + //Now create the intrusive list and insert data + ilist l(objects.begin(), objects.end()); + + //Now measure sorting time + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + l.reverse(); + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_std_list_reverse() +{ + typedef typename get_list::type stdlist; + + //Create the list and insert values + stdlist l; + for(int i = 0; i < NumElements; ++i) + l.push_back(typename stdlist::value_type()); + + //Now measure sorting time + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + l.reverse(); + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_compact_std_ptrlist_reverse() +{ + typedef typename get_list::type stdlist; + typedef typename get_ptrlist::type stdptrlist; + + //First create the elements + std::vector objects(NumElements); + + //Now create the std list and insert + stdptrlist l; + for(int i = 0; i < NumElements; ++i) + l.push_back(&objects[i]); + + //Now measure sorting time + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + l.reverse(); + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_disperse_std_ptrlist_reverse() +{ + typedef typename get_list::type stdlist; + typedef typename get_ptrlist::type stdptrlist; + + //First create the elements + std::list objects; + //Now create the std list and insert + stdptrlist l; + for(int i = 0; i < NumElements; ++i){ + objects.push_back(typename stdlist::value_type()); + l.push_back(&objects.back()); + } + + //Now measure sorting time + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + l.reverse(); + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} +//] + +//////////////////////////////////////////////////////////////////////// +// +// SORT +// +//////////////////////////////////////////////////////////////////////// + +//[perf_list_sort +template +void test_intrusive_list_sort() +{ + typedef typename get_ilist::type ilist; + + //First create the elements + std::vector objects(NumElements); + for(int i = 0; i < NumElements; ++i) + objects[i].i_ = i; + + //Now create the intrusive list and insert data + ilist l(objects.begin(), objects.end()); + + //Now measure sorting time + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + if(!(i % 2)){ + l.sort(std::greater()); + } + else{ + l.sort(std::less()); + } + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_std_list_sort() +{ + typedef typename get_list::type stdlist; + + //Create the list and insert values + stdlist l; + for(int i = 0; i < NumElements; ++i) + l.push_back(typename stdlist::value_type(i)); + + //Now measure sorting time + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + if(!(i % 2)){ + l.sort(std::greater()); + } + else{ + l.sort(std::less()); + } + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_compact_std_ptrlist_sort() +{ + typedef typename get_list::type stdlist; + typedef typename get_ptrlist::type stdptrlist; + + //First create the elements + std::vector objects(NumElements); + for(int i = 0; i < NumElements; ++i) + objects[i].i_ = i; + //Now create the std list and insert + stdptrlist l; + for(int i = 0; i < NumElements; ++i) + l.push_back(&objects[i]); + + //Now measure sorting time + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + if(!(i % 2)){ + l.sort(func_ptr_adaptor >()); + } + else{ + l.sort(func_ptr_adaptor >()); + } + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_disperse_std_ptrlist_sort() +{ + typedef typename get_list::type stdlist; + typedef typename get_ptrlist::type stdptrlist; + + //First create the elements and the list + std::list objects; + stdptrlist l; + for(int i = 0; i < NumElements; ++i){ + objects.push_back(typename stdlist::value_type(i)); + l.push_back(&objects.back()); + } + + //Now measure sorting time + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + if(!(i % 2)){ + l.sort(func_ptr_adaptor >()); + } + else{ + l.sort(func_ptr_adaptor >()); + } + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} +//] + +//////////////////////////////////////////////////////////////////////// +// +// WRITE ACCESS +// +//////////////////////////////////////////////////////////////////////// +//[perf_list_write_access +template +void test_intrusive_list_write_access() +{ + typedef typename get_ilist::type ilist; + + //First create the elements + std::vector objects(NumElements); + for(int i = 0; i < NumElements; ++i){ + objects[i].i_ = i; + } + + //Now create the intrusive list and insert data + ilist l(objects.begin(), objects.end()); + + //Now measure access time to the value type + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + typename ilist::iterator it(l.begin()), end(l.end()); + for(; it != end; ++it){ + ++(it->i_); + } + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_std_list_write_access() +{ + typedef typename get_list::type stdlist; + + //Create the list and insert values + stdlist l; + for(int i = 0; i < NumElements; ++i) + l.push_back(typename stdlist::value_type(i)); + + //Now measure access time to the value type + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + typename stdlist::iterator it(l.begin()), end(l.end()); + for(; it != end; ++it){ + ++(it->i_); + } + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_compact_std_ptrlist_write_access() +{ + typedef typename get_list::type stdlist; + typedef typename get_ptrlist::type stdptrlist; + + //First create the elements + std::vector objects(NumElements); + for(int i = 0; i < NumElements; ++i){ + objects[i].i_ = i; + } + + //Now create the std list and insert + stdptrlist l; + for(int i = 0; i < NumElements; ++i) + l.push_back(&objects[i]); + + //Now measure access time to the value type + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + typename stdptrlist::iterator it(l.begin()), end(l.end()); + for(; it != end; ++it){ + ++((*it)->i_); + } + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} + +template +void test_disperse_std_ptrlist_write_access() +{ + typedef typename get_list::type stdlist; + typedef typename get_ptrlist::type stdptrlist; + + //First create the elements + std::list objects; + //Now create the std list and insert + stdptrlist l; + for(int i = 0; i < NumElements; ++i){ + objects.push_back(typename stdlist::value_type(i)); + l.push_back(&objects.back()); + } + + //Now measure access time to the value type + ptime tini = microsec_clock::universal_time(); + for(int i = 0; i < NumIter; ++i){ + typename stdptrlist::iterator it(l.begin()), end(l.end()); + for(; it != end; ++it){ + ++((*it)->i_); + } + } + ptime tend = microsec_clock::universal_time(); + std::cout << "usecs/iteration: " << (tend-tini).total_microseconds()/NumIter << std::endl; +} +//] + +//////////////////////////////////////////////////////////////////////// +// +// ALL TESTS +// +//////////////////////////////////////////////////////////////////////// +template +void do_all_tests() +{ + std::cout << "Testing push back() with BigSize:" << BigSize << std::endl; + test_intrusive_list_push_back(); + test_intrusive_list_push_back(); + test_intrusive_list_push_back(); + test_std_list_push_back (); + test_compact_std_ptrlist_push_back(); + test_disperse_std_ptrlist_push_back(); + //reverse + std::cout << "Testing reverse() with BigSize:" << BigSize << std::endl; + test_intrusive_list_reverse(); + test_intrusive_list_reverse(); + test_intrusive_list_reverse(); + test_std_list_reverse(); + test_compact_std_ptrlist_reverse(); + test_disperse_std_ptrlist_reverse(); + //sort + std::cout << "Testing sort() with BigSize:" << BigSize << std::endl; + test_intrusive_list_sort(); + test_intrusive_list_sort(); + test_intrusive_list_sort(); + test_std_list_sort(); + test_compact_std_ptrlist_sort(); + test_disperse_std_ptrlist_sort(); + //write_access + std::cout << "Testing write_access() with BigSize:" << BigSize << std::endl; + test_intrusive_list_write_access(); + test_intrusive_list_write_access(); + test_intrusive_list_write_access(); + test_std_list_write_access(); + test_compact_std_ptrlist_write_access(); + test_disperse_std_ptrlist_write_access(); +} + +int main() +{ + //First pass the tests with a small size class + do_all_tests(); + //Now pass the tests with a big size class + do_all_tests(); + return 0; +} diff --git a/proj/vc7ide/Intrusive.sln b/proj/vc7ide/Intrusive.sln new file mode 100644 index 0000000..eb7626e --- /dev/null +++ b/proj/vc7ide/Intrusive.sln @@ -0,0 +1,71 @@ +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 +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 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/proj/vc7ide/Intrusive.vcproj b/proj/vc7ide/Intrusive.vcproj new file mode 100644 index 0000000..fc776c0 --- /dev/null +++ b/proj/vc7ide/Intrusive.vcproj @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj new file mode 100644 index 0000000..cf7e5e9 --- /dev/null +++ b/proj/vc7ide/_intrusivelib/_intrusivelib.vcproj @@ -0,0 +1,294 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/proj/vc7ide/list/list.vcproj b/proj/vc7ide/list/list.vcproj new file mode 100644 index 0000000..5c7c5d4 --- /dev/null +++ b/proj/vc7ide/list/list.vcproj @@ -0,0 +1,135 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/proj/vc7ide/multiset/multiset.vcproj b/proj/vc7ide/multiset/multiset.vcproj new file mode 100644 index 0000000..8413088 --- /dev/null +++ b/proj/vc7ide/multiset/multiset.vcproj @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/proj/vc7ide/set/set.vcproj b/proj/vc7ide/set/set.vcproj new file mode 100644 index 0000000..2894b6a --- /dev/null +++ b/proj/vc7ide/set/set.vcproj @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/proj/vc7ide/slist/slist.vcproj b/proj/vc7ide/slist/slist.vcproj new file mode 100644 index 0000000..9f8b663 --- /dev/null +++ b/proj/vc7ide/slist/slist.vcproj @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/proj/vc7ide/unordered_multiset/unordered_multiset.vcproj b/proj/vc7ide/unordered_multiset/unordered_multiset.vcproj new file mode 100644 index 0000000..1730eb9 --- /dev/null +++ b/proj/vc7ide/unordered_multiset/unordered_multiset.vcproj @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/proj/vc7ide/unordered_set/unordered_set.vcproj b/proj/vc7ide/unordered_set/unordered_set.vcproj new file mode 100644 index 0000000..6fa7744 --- /dev/null +++ b/proj/vc7ide/unordered_set/unordered_set.vcproj @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 new file mode 100644 index 0000000..c30b404 --- /dev/null +++ b/test/Jamfile.v2 @@ -0,0 +1,34 @@ +# Boost Intrusive Library Test Jamfile + +# (C) Copyright Ion Gaztaņaga 2006. +# Use, modification and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +# Adapted from John Maddock's TR1 Jamfile.v2 +# Copyright John Maddock 2005. +# Use, modification and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +# this rule enumerates through all the sources and invokes +# the run rule for each source, the result is a list of all +# the run rules, which we can pass on to the test_suite rule: + +rule test_all +{ + local all_rules = ; + + for local fileb in [ glob *.cpp ] + { + all_rules += [ run $(fileb) + : # additional args + : # test-files + : # requirements + ] ; + } + + return $(all_rules) ; +} + +test-suite intrusive_test : [ test_all r ] ; diff --git a/test/common_functors.hpp b/test/common_functors.hpp new file mode 100644 index 0000000..928df60 --- /dev/null +++ b/test/common_functors.hpp @@ -0,0 +1,43 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006-2007 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_TEST_COMMON_FUNCTORS_HPP +#define BOOST_INTRUSIVE_TEST_COMMON_FUNCTORS_HPP + +namespace boost { +namespace intrusive { +namespace test { + +class delete_destroyer +{ +public: + template + void operator()(Pointer p) + { + using boost::get_pointer; + delete get_pointer(p); + } +}; + +class new_cloner +{ +public: + template + Type *operator()(const Type &t) + { return new Type(t); } +}; + +} //namespace test { +} //namespace intrusive { +} //namespace boost { + +#endif diff --git a/test/itestvalue.hpp b/test/itestvalue.hpp new file mode 100644 index 0000000..04476a9 --- /dev/null +++ b/test/itestvalue.hpp @@ -0,0 +1,338 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztaņaga 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_ITESTVALUE +#define BOOST_INTRUSIVE_ITESTVALUE + +#ifdef _MSC_VER +//#pragma warning(disable : 4584) +#endif + +#include +#include +#include +#include +#include +#include +#include +#include "smart_ptr.hpp" + +namespace boost{ +namespace intrusive{ + +template +struct testvalue + : set_base_hook + , set_base_hook + , unordered_set_base_hook + , unordered_set_base_hook + , list_base_hook + , list_base_hook + , slist_base_hook + , slist_base_hook +{ + typedef set_base_hook set_auto_base_hook; + typedef set_base_hookset_base_hook; + typedef set_member_hook set_auto_member_hook; + typedef set_member_hook set_member_hook; + + typedef unordered_set_base_hook unordered_set_auto_base_hook; + typedef unordered_set_base_hook unordered_set_base_hook; + typedef unordered_set_member_hook unordered_set_auto_member_hook; + typedef unordered_set_member_hook unordered_set_member_hook; + + typedef list_base_hook list_auto_base_hook; + typedef list_base_hook list_base_hook; + typedef list_member_hook list_auto_member_hook; + typedef list_member_hook list_member_hook; + + typedef slist_base_hook slist_auto_base_hook; + typedef slist_base_hook slist_base_hook; + typedef slist_member_hook slist_auto_member_hook; + typedef slist_member_hook slist_member_hook; + + typedef typename boost::pointer_to_other + ::type pointer; + + //Set members + //pointer set_parent_, set_left_, set_right_; + //int set_color_; + set_member_hook set_node_; + set_auto_member_hook set_auto_node_; + unordered_set_member_hook unordered_set_node_; + unordered_set_auto_member_hook unordered_set_auto_node_; + + //List members + //pointer list_next_, list_prev_; + list_member_hook list_node_; + list_auto_member_hook list_auto_node_; + + //Slist members + //pointer slist_next_; + slist_member_hook slist_node_; + slist_auto_member_hook slist_auto_node_; + + int value_; + + enum{ constant_time_size = ConstantTimeSize }; + + testvalue() + {} + + testvalue(int i) + : value_(i) + {} + + testvalue (const testvalue& src) + : value_ (src.value_) + {} + + // testvalue is used in std::vector and thus prev and next + // have to be handled appropriately when copied: + + testvalue & operator= (const testvalue& src) + { + set_base_hook::operator=(src); + this->set_node_ = src.set_node_; + set_auto_base_hook::operator=(src); + this->set_auto_node_ = src.set_auto_node_; + + unordered_set_base_hook::operator=(src); + this->unordered_set_node_ = src.unordered_set_node_; + unordered_set_auto_base_hook::operator=(src); + this->unordered_set_auto_node_ = src.unordered_set_auto_node_; + + list_base_hook::operator=(src); + this->list_node_ = src.list_node_; + list_auto_base_hook::operator=(src); + this->list_auto_node_ = src.list_auto_node_; + + slist_base_hook::operator=(src); + this->slist_node_ = src.slist_node_; + slist_auto_base_hook::operator=(src); + this->slist_auto_node_ = src.slist_auto_node_; + value_ = src.value_; + return *this; + } + + void swap_nodes(testvalue &other) + { + //Set has no swapping + + //... + + //List + list_base_hook::swap_nodes(other); + list_node_.swap_nodes(other.list_node_); + list_auto_base_hook::swap_nodes(other); + list_auto_node_.swap_nodes(other.list_auto_node_); + + //Slist + slist_base_hook::swap_nodes(other); + slist_node_.swap_nodes(other.slist_node_); + slist_auto_base_hook::swap_nodes(other); + slist_auto_node_.swap_nodes(other.slist_auto_node_); + } + + ~testvalue() + {} + + bool operator< (const testvalue &other) const + { return value_ < other.value_; } + + bool operator==(const testvalue &other) const + { return value_ == other.value_; } +}; + +template +std::size_t hash_value(const testvalue &t) +{ + boost::hash hasher; + return hasher(t.value_); +} + +template +std::ostream& operator<< + (std::ostream& s, const testvalue& t) +{ return s << t.value_; } + +struct even_odd +{ + template + bool operator() + (const testvalue& v1 + ,const testvalue& v2) const + { + if ((v1.value_ & 1) == (v2.value_ & 1)) + return v1.value_ < v2.value_; + else + return v2.value_ & 1; + } +}; + +typedef testvalue value_r_f; +typedef testvalue, false> value_s_f; +typedef testvalue value_r_t; +typedef testvalue, true> value_s_t; + +//Typedefs +typedef value_r_f::set_base_hook:: + value_traits set_base_raw; + +typedef value_r_f::set_member_hook:: + value_traits set_member_raw; + +typedef value_r_f::set_auto_base_hook:: + value_traits set_auto_base_raw; + +typedef value_r_f::set_auto_member_hook:: + value_traits set_auto_member_raw; + + +typedef value_s_f::set_base_hook:: + value_traits set_base_smart; + +typedef value_s_f::set_member_hook:: + value_traits set_member_smart; + +typedef value_s_f::set_auto_base_hook:: + value_traits set_auto_base_smart; + +typedef value_s_f::set_auto_member_hook:: + value_traits set_auto_member_smart; + +typedef value_r_t::set_base_hook:: + value_traits set_base_raw_t; + +typedef value_r_t::set_member_hook:: + value_traits set_member_raw_t; + +typedef value_s_t::set_base_hook:: + value_traits set_base_smart_t; + +typedef value_s_t::set_member_hook:: + value_traits set_member_smart_t; + +//Typedefs +typedef value_r_f::unordered_set_base_hook:: + value_traits unordered_set_base_raw; + +typedef value_r_f::unordered_set_member_hook:: + value_traits unordered_set_member_raw; + +typedef value_r_f::unordered_set_auto_base_hook:: + value_traits unordered_set_auto_base_raw; + +typedef value_r_f::unordered_set_auto_member_hook:: + value_traits unordered_set_auto_member_raw; + +typedef value_s_f::unordered_set_base_hook:: + value_traits unordered_set_base_smart; + +typedef value_s_f::unordered_set_member_hook:: + value_traits unordered_set_member_smart; + +typedef value_s_f::unordered_set_auto_base_hook:: + value_traits unordered_set_auto_base_smart; + +typedef value_s_f::unordered_set_auto_member_hook:: + value_traits unordered_set_auto_member_smart; + +typedef value_r_t::unordered_set_base_hook:: + value_traits unordered_set_base_raw_t; + +typedef value_r_t::unordered_set_member_hook:: + value_traits unordered_set_member_raw_t; + +typedef value_s_t::unordered_set_base_hook:: + value_traits unordered_set_base_smart_t; + +typedef value_s_t::unordered_set_member_hook:: + value_traits unordered_set_member_smart_t; + +//Explicit instantiations +typedef value_r_f::list_base_hook:: + value_traits list_base_raw; + +typedef value_r_f::list_member_hook:: + value_traits list_member_raw; + +typedef value_r_f::list_auto_base_hook:: + value_traits list_auto_base_raw; + +typedef value_r_f::list_auto_member_hook:: + value_traits list_auto_member_raw; + +typedef value_s_f::list_base_hook:: + value_traits list_base_smart; + +typedef value_s_f::list_member_hook:: + value_traits list_member_smart; + +typedef value_s_f::list_auto_base_hook:: + value_traits list_auto_base_smart; + +typedef value_s_f::list_auto_member_hook:: + value_traits list_auto_member_smart; + +typedef value_r_t::list_base_hook:: + value_traits list_base_raw_t; + +typedef value_r_t::list_member_hook:: + value_traits list_member_raw_t; + +typedef value_s_t::list_base_hook:: + value_traits list_base_smart_t; + +typedef value_s_t::list_member_hook:: + value_traits list_member_smart_t; + +typedef value_r_f::slist_base_hook:: + value_traits slist_base_raw; + +typedef value_r_f::slist_member_hook:: + value_traits slist_member_raw; + +typedef value_r_f::slist_auto_base_hook:: + value_traits slist_auto_base_raw; + +typedef value_r_f::slist_auto_member_hook:: + value_traits slist_auto_member_raw; + +typedef value_s_f::slist_base_hook:: + value_traits slist_base_smart; + +typedef value_s_f::slist_member_hook:: + value_traits slist_member_smart; + +typedef value_s_f::slist_auto_base_hook:: + value_traits slist_auto_base_smart; + +typedef value_s_f::slist_auto_member_hook:: + value_traits slist_auto_member_smart; + +typedef value_r_t::slist_base_hook:: + value_traits slist_base_raw_t; + +typedef value_r_t::slist_member_hook:: + value_traits slist_member_raw_t; + +typedef value_s_t::slist_base_hook:: + value_traits slist_base_smart_t; + +typedef value_s_t::slist_member_hook:: + value_traits slist_member_smart_t; + +} //namespace boost{ +} //namespace intrusive{ + +#endif diff --git a/test/list_test.cpp b/test/list_test.cpp new file mode 100644 index 0000000..7e7ad03 --- /dev/null +++ b/test/list_test.cpp @@ -0,0 +1,376 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztaņaga 2006-2007. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +#include +#include +#include "itestvalue.hpp" +#include "smart_ptr.hpp" +#include "common_functors.hpp" +#include +#include + +// Boost.Test +#include "boost/test/included/test_exec_monitor.hpp" + +using namespace boost::intrusive; + +template +struct test_list +{ + typedef typename ValueTraits::value_type value_type; + static void test_all(std::vector& values); + static void test_front_back(std::vector& values); + static void test_sort(std::vector& values); + static void test_insert(std::vector& values); + static void test_shift(std::vector& values); + static void test_swap(std::vector& values); + static void test_clone(std::vector& values); +}; + +template +void test_list::test_all(std::vector& values) +{ + test_front_back(values); + test_sort(values); + test_insert(values); + test_shift(values); + test_swap(values); + test_clone(values); + + typedef boost::intrusive::list + < ValueTraits + , ValueTraits::value_type::constant_time_size + , std::size_t + > list_type; + list_type testlist(values.begin(), values.end()); + list_type testlist2; +} + +//test: push_front, pop_front, push_back, pop_back, front, back, size, empty: +template +void test_list + ::test_front_back(std::vector& values) +{ + typedef boost::intrusive::list + < ValueTraits + , ValueTraits::value_type::constant_time_size + , std::size_t + > list_type; + list_type testlist; + BOOST_CHECK (testlist.empty()); + + testlist.push_back (values[0]); + BOOST_CHECK (testlist.size() == 1); + BOOST_CHECK (&testlist.front() == &values[0]); + BOOST_CHECK (&testlist.back() == &values[0]); + + testlist.push_front (values[1]); + BOOST_CHECK (testlist.size() == 2); + BOOST_CHECK (&testlist.front() == &values[1]); + BOOST_CHECK (&testlist.back() == &values[0]); + + testlist.pop_back(); + BOOST_CHECK (testlist.size() == 1); + const list_type &const_testlist = testlist; + BOOST_CHECK (&const_testlist.front() == &values[1]); + BOOST_CHECK (&const_testlist.back() == &values[1]); + + testlist.pop_front(); + BOOST_CHECK (testlist.empty()); +} + + +//test: constructor, iterator, reverse_iterator, sort, reverse: +template +void test_list + ::test_sort(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::list + < ValueTraits + , ValueTraits::value_type::constant_time_size + , std::size_t + > list_type; + list_type testlist(values.begin(), values.end()); + + std::copy (testlist.begin(), testlist.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("12345")); + + testlist.sort (even_odd()); + std::copy (testlist.rbegin(), testlist.rend(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("53142")); + + testlist.reverse(); + //test postincrement: + for (BOOST_DEDUCED_TYPENAME list_type::iterator i = + testlist.begin(), e = testlist.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("53142")); +} + +//test: assign, insert, const_iterator, const_reverse_iterator, erase, iterator_to: +template +void test_list + ::test_insert(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::list + < ValueTraits + , ValueTraits::value_type::constant_time_size + , std::size_t + > list_type; + list_type testlist; + testlist.assign (&values[0] + 2, &values[0] + 5); + + const list_type& const_testlist = testlist; + std::copy (const_testlist.begin(), const_testlist.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("345")); + + BOOST_DEDUCED_TYPENAME list_type::iterator i = ++testlist.begin(); + BOOST_CHECK (i->value_ == 4); + + testlist.insert (i, values[0]); + std::copy (const_testlist.rbegin(), const_testlist.rend(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("5413")); + + i = testlist.iterator_to (values[4]); + BOOST_CHECK (&*i == &values[4]); + + i = testlist.erase (i); + BOOST_CHECK (i == testlist.end()); + + //test postincrement: + for (BOOST_DEDUCED_TYPENAME list_type::const_iterator i = + const_testlist.begin(), e = const_testlist.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("314")); +} + +template +void test_list + ::test_shift(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::list + < ValueTraits + , ValueTraits::value_type::constant_time_size + , std::size_t + > list_type; + list_type testlist; + + const int num_values = (int)values.size(); + std::vector expected_values(num_values); + + //Shift forward all possible positions 3 times + for(int i = 0; i < num_values*3; ++i){ + testlist.assign(values.begin(), values.end()); + testlist.shift_forward(i); + for(int j = 0; j < num_values; ++j){ + expected_values[(j + num_values - i%num_values) % num_values] = (j + 1); + } + std::copy (testlist.begin(), testlist.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())); + 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); + } + std::copy (testlist.begin(), testlist.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())); + testlist.clear(); + } +} + +//test: insert (seq-version), swap, splice, erase (seq-version): +template +void test_list + ::test_swap(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::list + list_type; + + { + list_type testlist1 (&values[0], &values[0] + 2); + list_type testlist2; + testlist2.insert (testlist2.end(), &values[0] + 2, &values[0] + 5); + testlist1.swap (testlist2); + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("345")); + std::copy (testlist2.begin(), testlist2.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("12")); + + testlist2.splice (++testlist2.begin(), testlist1); + std::copy (testlist2.begin(), testlist2.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("13452")); + BOOST_CHECK (testlist1.empty()); + + testlist1.splice (testlist1.end(), testlist2, ++(++testlist2.begin())); + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("4")); + std::copy (testlist2.begin(), testlist2.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("1352")); + + testlist1.splice (testlist1.end(), testlist2, + testlist2.begin(), ----testlist2.end()); + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("413")); + std::copy (testlist2.begin(), testlist2.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("52")); + + testlist1.erase (testlist1.iterator_to(values[0]), testlist1.end()); + BOOST_CHECK (testlist1.size() == 1); + BOOST_CHECK (&testlist1.front() == &values[3]); + } + { + boost::test_tools::output_test_stream test_seq; + list_type testlist1 (&values[0], &values[0] + 2); + list_type testlist2 (&values[0] + 3, &values[0] + 5); + + values[0].swap_nodes(values[2]); + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("32")); + + values[2].swap_nodes(values[4]); + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("52")); + std::copy (testlist2.begin(), testlist2.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("43")); + } +} + +template +void test_list + ::test_clone(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::list + list_type; + + list_type testlist1 (&values[0], &values[0] + values.size()); + list_type testlist2; + + testlist2.clone_from(testlist1, test::new_cloner(), test::delete_destroyer()); + BOOST_CHECK (testlist2 == testlist1); + testlist2.clear_and_destroy(test::delete_destroyer()); + BOOST_CHECK (testlist2.empty()); +} + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + std::vector data (5); + for (int i = 0; i < 5; ++i) + data[i].value_ = i + 1; + + test_list >::test_all(data); + + test_list >::test_all(data); + + return 0; + } +}; + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + std::vector data (5); + for (int i = 0; i < 5; ++i) + data[i].value_ = i + 1; + + test_list >::test_all(data); + + test_list >::test_all(data); + + test_list >::test_all(data); + + test_list >::test_all(data); + return 0; + } +}; + +//Explicit instantiations of non-counted classes +template class boost::intrusive::list; +template class boost::intrusive::list; +template class boost::intrusive::list; +template class boost::intrusive::list; +template class boost::intrusive::list; +template class boost::intrusive::list; +template class boost::intrusive::list; +template class boost::intrusive::list; + +//Explicit instantiation of counted classes +template class boost::intrusive::list; +template class boost::intrusive::list; +template class boost::intrusive::list; +template class boost::intrusive::list; + +int test_main( int, char* [] ) +{ + test_main_template()(); + test_main_template, false>()(); + test_main_template()(); + test_main_template, true>()(); + return 0; +} diff --git a/test/multiset_test.cpp b/test/multiset_test.cpp new file mode 100644 index 0000000..ce4367f --- /dev/null +++ b/test/multiset_test.cpp @@ -0,0 +1,317 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztaņaga 2006-2007. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#include +#include +#include "itestvalue.hpp" +#include "smart_ptr.hpp" +#include "common_functors.hpp" + +#include + +// Boost.Test +#include "boost/test/included/test_exec_monitor.hpp" + +using namespace boost::intrusive; + +template +struct test_multiset +{ + typedef typename ValueTraits::value_type value_type; + static void test_all (std::vector& values); + static void test_sort(std::vector& values); + static void test_insert(std::vector& values); + static void test_swap(std::vector& values); + static void test_find(std::vector& values); + static void test_impl(); + static void test_clone(std::vector& values); +}; + +template +void test_multiset::test_all (std::vector& values) +{ + test_sort(values); + test_insert(values); + test_swap(values); + test_find(values); + test_impl(); + test_clone(values); +} + +//test case due to an error in tree implementation: +template +void test_multiset::test_impl() +{ + typedef typename ValueTraits::value_type testvalue_t; + std::vector values (5); + for (int i = 0; i < 5; ++i) + values[i].value_ = i; + typedef multiset + + ,ValueTraits::value_type::constant_time_size, std::size_t + > multiset_type; + multiset_type testset; + for (int i = 0; i < 5; ++i) + testset.insert (values[i]); + + testset.erase (testset.iterator_to (values[0])); + testset.erase (testset.iterator_to (values[1])); + testset.insert (values[1]); + + testset.erase (testset.iterator_to (values[2])); + testset.erase (testset.iterator_to (values[3])); +} + +//test: constructor, iterator, clear, reverse_iterator, front, back, size: +template +void test_multiset::test_sort(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + + typedef multiset + + ,ValueTraits::value_type::constant_time_size, std::size_t + > multiset_type; + multiset_type testset1 (values.begin(), values.end()); + + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("122345")); + + testset1.clear(); + BOOST_CHECK (testset1.empty()); + + typedef multiset + multiset_type2; + multiset_type2 testset2 (&values[0], &values[0] + 6); + std::copy (testset2.rbegin(), testset2.rend(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("531422")); + BOOST_CHECK (testset2.begin()->value_ == 2); + BOOST_CHECK (testset2.rbegin()->value_ == 5); +} + +//test: insert, const_iterator, const_reverse_iterator, erase, iterator_to: +template +void test_multiset::test_insert(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + + typedef multiset + + ,ValueTraits::value_type::constant_time_size, std::size_t + > multiset_type; + + multiset_type testset; + testset.insert(&values[0] + 2, &values[0] + 5); + + const multiset_type& const_testset = testset; + std::copy (const_testset.begin(), const_testset.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("145")); + + BOOST_DEDUCED_TYPENAME multiset_type::iterator i = testset.begin(); + BOOST_CHECK (i->value_ == 1); + + i = testset.insert (i, values[0]); + BOOST_CHECK (&*i == &values[0]); + + std::copy (const_testset.rbegin(), const_testset.rend(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("5431")); + + i = testset.iterator_to (values[2]); + BOOST_CHECK (&*i == &values[2]); + testset.erase(i); + + //test post-increment: + for (BOOST_DEDUCED_TYPENAME multiset_type::const_iterator i = const_testset.begin(), + e = const_testset.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("135")); +} + +//test: insert (seq-version), swap, erase (seq-version), size: +template +void test_multiset::test_swap(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef multiset + + ,ValueTraits::value_type::constant_time_size, std::size_t + > multiset_type; + multiset_type testset1 (&values[0], &values[0] + 2); + multiset_type testset2; + testset2.insert (&values[0] + 2, &values[0] + 6); + testset1.swap (testset2); + + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("1245")); + //test post-increment: + for (BOOST_DEDUCED_TYPENAME multiset_type::iterator i = testset2.begin(), + e = testset2.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("23")); + + testset1.erase (testset1.iterator_to(values[5]), testset1.end()); + BOOST_CHECK (testset1.size() == 1); + BOOST_CHECK (&*testset1.begin() == &values[3]); +} + +//test: find, equal_range (lower_bound, upper_bound): +template +void test_multiset::test_find(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef multiset + + ,ValueTraits::value_type::constant_time_size, std::size_t + > multiset_type; + multiset_type testset (values.begin(), values.end()); + typedef BOOST_DEDUCED_TYPENAME multiset_type::iterator iterator; + + testvalue_t cmp_val; + cmp_val.value_ = 2; + iterator i = testset.find (cmp_val); + BOOST_CHECK (i->value_ == 2); + BOOST_CHECK ((++i)->value_ == 2); + std::pair range = testset.equal_range (cmp_val); + + BOOST_CHECK (range.first->value_ == 2); + BOOST_CHECK (range.second->value_ == 3); + BOOST_CHECK (std::distance (range.first, range.second) == 2); + + cmp_val.value_ = 7; + BOOST_CHECK (testset.find (cmp_val) == testset.end()); +} + +template +void test_multiset + ::test_clone(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + typedef multiset + + ,ValueTraits::value_type::constant_time_size, std::size_t + > multiset_type; + + multiset_type testmultiset1 (&values[0], &values[0] + values.size()); + multiset_type testmultiset2; + + testmultiset2.clone_from(testmultiset1, test::new_cloner(), test::delete_destroyer()); + BOOST_CHECK (testmultiset2 == testmultiset1); + testmultiset2.clear_and_destroy(test::delete_destroyer()); + BOOST_CHECK (testmultiset2.empty()); +} + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; + std::vector > data (6); + for (int i = 0; i < 6; ++i) + data[i].value_ = random_init[i]; + + test_multiset >::test_all(data); + + test_multiset >::test_all(data); + + return 0; + } +}; + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; + std::vector > data (6); + for (int i = 0; i < 6; ++i) + data[i].value_ = random_init[i]; + + test_multiset >::test_all(data); + + test_multiset >::test_all(data); + + test_multiset >::test_all(data); + + test_multiset >::test_all(data); + return 0; + } +}; + +//Explicit instantiations of non-counted classes +template class multiset + , false>; +template class multiset + , false>; +template class multiset + , false>; +template class multiset + , false>; +template class multiset + , false>; +template class multiset + , false>; +template class multiset + , false>; +template class multiset + , false>; + +//Explicit instantiation of counted classes +template class multiset + , true>; +template class multiset + , true>; +template class multiset + , true>; +template class multiset + , true>; + +int test_main( int, char* [] ) +{ + test_main_template()(); + test_main_template, false>()(); + test_main_template()(); + test_main_template, true>()(); + return 0; +} + + diff --git a/test/set_test.cpp b/test/set_test.cpp new file mode 100644 index 0000000..cdea5df --- /dev/null +++ b/test/set_test.cpp @@ -0,0 +1,315 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztaņaga 2006-2007. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#include +#include +#include "itestvalue.hpp" +#include "smart_ptr.hpp" +#include "common_functors.hpp" +#include + +// Boost.Test +#include "boost/test/included/test_exec_monitor.hpp" + +using namespace boost::intrusive; + +template +struct test_set +{ + typedef typename ValueTraits::value_type value_type; + static void test_all(std::vector& values); + static void test_sort(std::vector& values); + static void test_insert(std::vector& values); + static void test_swap(std::vector& values); + static void test_find(std::vector& values); + static void test_impl(); + static void test_clone(std::vector& values); +}; + +template +void test_set::test_all(std::vector& values) +{ + test_sort(values); + test_insert(values); + test_swap(values); + test_find(values); + test_impl(); + test_clone(values); +} + +//test case due to an error in tree implementation: +template +void test_set::test_impl() +{ + typedef typename ValueTraits::value_type testvalue_t; + std::vector values (5); + for (int i = 0; i < 5; ++i) + values[i].value_ = i; + + typedef boost::intrusive::set + + ,ValueTraits::value_type::constant_time_size, std::size_t + > set_type; + set_type testset; + for (int i = 0; i < 5; ++i) + testset.insert (values[i]); + + testset.erase (testset.iterator_to (values[0])); + testset.erase (testset.iterator_to (values[1])); + testset.insert (values[1]); + + testset.erase (testset.iterator_to (values[2])); + testset.erase (testset.iterator_to (values[3])); +} + +//test: constructor, iterator, clear, reverse_iterator, front, back, size: +template +void test_set::test_sort(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::set + + ,ValueTraits::value_type::constant_time_size, std::size_t + > set_type; + set_type testset1 (values.begin(), values.end()); + + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("12345")); + + testset1.clear(); + BOOST_CHECK (testset1.empty()); + + typedef boost::intrusive::set + set_type2; + set_type2 testset2 (&values[0], &values[0] + 6); + std::copy (testset2.rbegin(), testset2.rend(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("53142")); + BOOST_CHECK (testset2.begin()->value_ == 2); + BOOST_CHECK (testset2.rbegin()->value_ == 5); +} + +//test: insert, const_iterator, const_reverse_iterator, erase, iterator_to: +template +void test_set::test_insert(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::set + + ,ValueTraits::value_type::constant_time_size, std::size_t + > set_type; + set_type testset; + testset.insert(&values[0] + 2, &values[0] + 5); + + const set_type& const_testset = testset; + std::copy (const_testset.begin(), const_testset.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("145")); + + BOOST_DEDUCED_TYPENAME set_type::iterator i = testset.begin(); + BOOST_CHECK (i->value_ == 1); + + i = testset.insert (i, values[0]); + BOOST_CHECK (&*i == &values[0]); + + std::copy (const_testset.rbegin(), const_testset.rend(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("5431")); + + i = testset.iterator_to (values[2]); + BOOST_CHECK (&*i == &values[2]); + + testset.erase (i); + //test post-increment: + for (BOOST_DEDUCED_TYPENAME set_type::const_iterator i = const_testset.begin(), + e = const_testset.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("135")); +} + +//test: insert (seq-version), swap, erase (seq-version), size: +template +void test_set::test_swap(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + + typedef boost::intrusive::set + + ,ValueTraits::value_type::constant_time_size, std::size_t + > set_type; + set_type testset1 (&values[0], &values[0] + 2); + set_type testset2; + testset2.insert (&values[0] + 2, &values[0] + 6); + testset1.swap (testset2); + + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("1245")); + //test post-increment: + for (BOOST_DEDUCED_TYPENAME set_type::iterator i = testset2.begin(), + e = testset2.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("23")); + + testset1.erase (testset1.iterator_to(values[5]), testset1.end()); + BOOST_CHECK (testset1.size() == 1); + // BOOST_CHECK (&testset1.front() == &values[3]); + BOOST_CHECK (&*testset1.begin() == &values[3]); +} + +//test: find, equal_range (lower_bound, upper_bound): +template +void test_set::test_find(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + + typedef boost::intrusive::set + + ,ValueTraits::value_type::constant_time_size, std::size_t + > set_type; + set_type testset (values.begin(), values.end()); + typedef BOOST_DEDUCED_TYPENAME set_type::iterator iterator; + + testvalue_t cmp_val; + cmp_val.value_ = 2; + iterator i = testset.find (cmp_val); + BOOST_CHECK (i->value_ == 2); + BOOST_CHECK ((++i)->value_ != 2); + std::pair range = testset.equal_range (cmp_val); + + BOOST_CHECK (range.first->value_ == 2); + BOOST_CHECK (range.second->value_ == 3); + BOOST_CHECK (std::distance (range.first, range.second) == 1); + + cmp_val.value_ = 7; + BOOST_CHECK (testset.find (cmp_val) == testset.end()); +} + +template +void test_set + ::test_clone(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + typedef boost::intrusive::set + + ,ValueTraits::value_type::constant_time_size, std::size_t + > set_type; + + set_type testset1 (&values[0], &values[0] + values.size()); + set_type testset2; + + testset2.clone_from(testset1, test::new_cloner(), test::delete_destroyer()); + BOOST_CHECK (testset2 == testset1); + testset2.clear_and_destroy(test::delete_destroyer()); + BOOST_CHECK (testset2.empty()); +} + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; + std::vector > data (6); + for (int i = 0; i < 6; ++i) + data[i].value_ = random_init[i]; + + test_set >::test_all(data); + + test_set >::test_all(data); + + return 0; + } +}; + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; + std::vector > data (6); + for (int i = 0; i < 6; ++i) + data[i].value_ = random_init[i]; + + test_set >::test_all(data); + + test_set >::test_all(data); + + test_set >::test_all(data); + + test_set >::test_all(data); + return 0; + } +}; + +//Explicit instantiations of non-counted classes +template class boost::intrusive::set + , false>; +template class boost::intrusive::set + , false>; +template class boost::intrusive::set + , false>; +template class boost::intrusive::set + , false>; +template class boost::intrusive::set + , false>; +template class boost::intrusive::set + , false>; +template class boost::intrusive::set + , false>; +template class boost::intrusive::set + , false>; + +//Explicit instantiation of counted classes +template class boost::intrusive::set + , true>; +template class boost::intrusive::set + , true>; +template class boost::intrusive::set + , true>; +template class boost::intrusive::set + , true>; + +int test_main( int, char* [] ) +{ + test_main_template()(); + test_main_template, false>()(); + test_main_template()(); + test_main_template, true>()(); + return 0; +} diff --git a/test/slist_test.cpp b/test/slist_test.cpp new file mode 100644 index 0000000..5fae54d --- /dev/null +++ b/test/slist_test.cpp @@ -0,0 +1,433 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztaņaga 2006-2007. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// +#include +#include +#include "itestvalue.hpp" +#include "smart_ptr.hpp" +#include "common_functors.hpp" +#include +#include + +// Boost.Test +#include "boost/test/included/test_exec_monitor.hpp" + +using namespace boost::intrusive; + +template +struct test_slist +{ + typedef typename ValueTraits::value_type value_type; + static void test_all (std::vector& values); + static void test_front_back (std::vector& values); + static void test_sort(std::vector& values); + static void test_merge (std::vector& values); + static void test_insert(std::vector& values); + static void test_shift(std::vector& values); + static void test_swap(std::vector& values); + static void test_slow_insert (std::vector& values); + static void test_clone (std::vector& values); +}; + +template +void test_slist + ::test_all (std::vector& values) +{ + test_front_back (values); + test_sort(values); + test_merge (values); + test_insert(values); + test_shift(values); + test_slow_insert (values); + test_swap(values); + test_clone(values); +} + +//test: push_front, pop_front, front, size, empty: +template +void test_slist + ::test_front_back (std::vector& values) +{ + typedef boost::intrusive::slist + list_type; + list_type testlist; + BOOST_CHECK (testlist.empty()); + + testlist.push_front (values[0]); + BOOST_CHECK (testlist.size() == 1); + BOOST_CHECK (&testlist.front() == &values[0]); + + testlist.push_front (values[1]); + BOOST_CHECK (testlist.size() == 2); + BOOST_CHECK (&testlist.front() == &values[1]); + + testlist.pop_front(); + BOOST_CHECK (testlist.size() == 1); + BOOST_CHECK (&testlist.front() == &values[0]); + + testlist.pop_front(); + BOOST_CHECK (testlist.empty()); +} + +//test: merge due to error in merge implementation: +template +void test_slist + ::test_merge (std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::slist + 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); + + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("1345")); +} + +//test: constructor, iterator, sort, reverse: +template +void test_slist + ::test_sort(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::slist + list_type; + list_type testlist (values.begin(), values.end()); + + std::copy (testlist.begin(), testlist.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("12345")); + + testlist.sort (even_odd()); + for (BOOST_DEDUCED_TYPENAME list_type::iterator i = testlist.begin(), + e = testlist.end(); i != e; i++) //test postincrement + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("24135")); + + testlist.reverse(); + std::copy (testlist.begin(), testlist.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("53142")); +} + +//test: assign, insert_after, const_iterator, erase_after, iterator_to, previous: +template +void test_slist + ::test_insert(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::slist + list_type; + list_type testlist; + testlist.assign (&values[0] + 2, &values[0] + 5); + + const list_type& const_testlist = testlist; + std::copy (const_testlist.begin(), const_testlist.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("345")); + + BOOST_DEDUCED_TYPENAME list_type::iterator i = ++testlist.begin(); + BOOST_CHECK (i->value_ == 4); + + testlist.insert_after (i, values[0]); + for (BOOST_DEDUCED_TYPENAME list_type::const_iterator i2 = const_testlist.begin(), + e = const_testlist.end(); i2 != e; i2++) //test postincrement + test_seq << *i2; + BOOST_CHECK (test_seq.is_equal ("3415")); + + i = testlist.iterator_to (values[4]); + BOOST_CHECK (&*i == &values[4]); + i = testlist.previous (i); + BOOST_CHECK (&*i == &values[0]); + + testlist.erase_after (i); + BOOST_CHECK (&*i == &values[0]); + + std::copy (const_testlist.begin(), const_testlist.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("341")); +} + +//test: insert, const_iterator, erase, iterator_to: +template +void test_slist + ::test_slow_insert (std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::slist + list_type; + list_type testlist; + testlist.push_front (values[4]); + testlist.insert (testlist.begin(), &values[0] + 2, &values[0] + 4); + + const list_type& const_testlist = testlist; + std::copy (const_testlist.begin(), const_testlist.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("345")); + + BOOST_DEDUCED_TYPENAME list_type::iterator i = ++testlist.begin(); + BOOST_CHECK (i->value_ == 4); + + testlist.insert (i, values[0]); + std::copy (const_testlist.begin(), const_testlist.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("3145")); + + i = testlist.iterator_to (values[4]); + BOOST_CHECK (&*i == &values[4]); + + i = testlist.erase (i); + BOOST_CHECK (i == testlist.end()); + + //test postincrement: + for (BOOST_DEDUCED_TYPENAME list_type::const_iterator i = + const_testlist.begin(), e = const_testlist.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("314")); + testlist.erase (++testlist.begin(), testlist.end()); + BOOST_CHECK (testlist.size() == 1); + BOOST_CHECK (testlist.front().value_ == 3); +} + +template +void test_slist + ::test_shift(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::slist + list_type; + list_type testlist; + + const int num_values = (int)values.size(); + std::vector expected_values(num_values); + + //Shift forward all possible positions 3 times + for(int i = 0; i < num_values*3; ++i){ + testlist.assign(values.begin(), values.end()); + testlist.shift_forward(i); + for(int j = 0; j < num_values; ++j){ + expected_values[(j + num_values - i%num_values) % num_values] = (j + 1); + } + std::copy (testlist.begin(), testlist.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())); + 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); + } + std::copy (testlist.begin(), testlist.end(), + std::ostream_iterator (test_seq)); + + std::stringstream stream2; + std::copy (testlist.begin(), testlist.end(), + std::ostream_iterator (stream2)); + stream2 << std::ends; + + 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())); + testlist.clear(); + } +} + +//test: insert_after (seq-version), swap, splice_after: +template +void test_slist + ::test_swap(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::slist + list_type; + { + list_type testlist1 (&values[0], &values[0] + 2); + list_type testlist2; + testlist2.insert_after (testlist2.end(), &values[0] + 2, &values[0] + 5); + testlist1.swap(testlist2); + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("345")); + std::copy (testlist2.begin(), testlist2.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("12")); + + testlist2.splice_after (testlist2.begin(), testlist1); + std::copy (testlist2.begin(), testlist2.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("13452")); + BOOST_CHECK (testlist1.empty()); + + testlist1.splice_after (testlist1.end(), testlist2, ++testlist2.begin()); + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("4")); + std::copy (testlist2.begin(), testlist2.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("1352")); + + testlist1.splice_after (testlist1.begin(), testlist2, + testlist2.end(), ++++testlist2.begin()); + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("4135")); + std::copy (testlist2.begin(), testlist2.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("2")); + } + { + boost::test_tools::output_test_stream test_seq; + + list_type testlist1 (&values[0], &values[0] + 2); + list_type testlist2 (&values[0] + 3, &values[0] + 5); + + values[0].swap_nodes(values[2]); + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("32")); + + values[2].swap_nodes(values[4]); + std::copy (testlist1.begin(), testlist1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("52")); + std::copy (testlist2.begin(), testlist2.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("43")); + } +} + +template +void test_slist + ::test_clone(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::slist + list_type; + + list_type testlist1 (&values[0], &values[0] + values.size()); + list_type testlist2; + + testlist2.clone_from(testlist1, test::new_cloner(), test::delete_destroyer()); + BOOST_CHECK (testlist2 == testlist1); + testlist2.clear_and_destroy(test::delete_destroyer()); + BOOST_CHECK (testlist2.empty()); +} + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + std::vector data (5); + for (int i = 0; i < 5; ++i) + data[i].value_ = i + 1; + + test_slist >::test_all(data); + + test_slist >::test_all(data); + + return 0; + } +}; + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + std::vector data (5); + for (int i = 0; i < 5; ++i) + data[i].value_ = i + 1; + + test_slist >::test_all(data); + + test_slist >::test_all(data); + + test_slist >::test_all(data); + + test_slist >::test_all(data); + return 0; + } +}; + +//Explicit instantiations of non-counted classes +template class boost::intrusive::slist; +template class boost::intrusive::slist; +template class boost::intrusive::slist; +template class boost::intrusive::slist; +template class boost::intrusive::slist; +template class boost::intrusive::slist; +template class boost::intrusive::slist; +template class boost::intrusive::slist; + +//Explicit instantiation of counted classes +template class boost::intrusive::slist; +template class boost::intrusive::slist; +template class boost::intrusive::slist; +template class boost::intrusive::slist; + +int test_main(int, char* []) +{ + test_main_template()(); + test_main_template, false>()(); + test_main_template()(); + test_main_template, true>()(); + return 0; +} diff --git a/test/smart_ptr.hpp b/test/smart_ptr.hpp new file mode 100644 index 0000000..98a63d1 --- /dev/null +++ b/test/smart_ptr.hpp @@ -0,0 +1,355 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztaņaga 2006. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_SMART_PTR_HPP +#define BOOST_INTRUSIVE_SMART_PTR_HPP + +#include + +#if (defined _MSC_VER) && (_MSC_VER >= 1200) +# pragma once +#endif + +namespace boost{ +namespace intrusive{ + +namespace detail { + +struct static_cast_tag {}; +struct const_cast_tag {}; +struct dynamic_cast_tag {}; +struct reinterpret_cast_tag {}; + +} //namespace detail { + +//Empty class +struct empty_type{}; + +template +struct random_it +: public boost::iterator +{ + typedef const T* const_pointer; + typedef const T& const_reference; +}; + +template<> struct random_it +{ + typedef void * pointer; + typedef const void * const_pointer; + typedef empty_type& reference; + typedef const empty_type& const_reference; + typedef void value_type; + typedef empty_type difference_type; + typedef empty_type iterator_category; +}; + +template<> struct random_it +{ + typedef const void * pointer; + typedef const void * const_pointer; + typedef const empty_type & reference; + typedef const empty_type & const_reference; + typedef const void value_type; + typedef empty_type difference_type; + typedef empty_type iterator_category; +}; + +template<> struct random_it +{ + typedef volatile void * pointer; + typedef const volatile void * const_pointer; + typedef empty_type& reference; + typedef const empty_type& const_reference; + typedef volatile void value_type; + typedef empty_type difference_type; + typedef empty_type iterator_category; +}; + +template<> struct random_it +{ + typedef const volatile void * pointer; + typedef const volatile void * const_pointer; + typedef const empty_type & reference; + typedef const empty_type & const_reference; + typedef const volatile void value_type; + typedef empty_type difference_type; + typedef empty_type iterator_category; +}; + +} //namespace intrusive { +} //namespace boost { + + +namespace boost { +namespace intrusive { + +template +class smart_ptr +{ + typedef random_it random_it_t; + typedef smart_ptr self_t; + typedef typename random_it_t::const_pointer const_pointer_t; + typedef typename random_it_t::const_reference const_reference_t; + + void unspecified_bool_type_func() const {} + typedef void (self_t::*unspecified_bool_type)() const; + + public: + typedef typename random_it_t::pointer pointer; + typedef typename random_it_t::reference reference; + typedef typename random_it_t::value_type value_type; + typedef typename random_it_t::difference_type difference_type; + typedef typename random_it_t::iterator_category iterator_category; + + PointedType *m_ptr; + + public: //Public Functions + + /*!Constructor from raw pointer (allows "0" pointer conversion). Never throws.*/ + smart_ptr(pointer ptr = 0) + : m_ptr(ptr) + {} + + /*!Constructor from other pointer. Never throws.*/ + + template + smart_ptr(T *ptr) + : m_ptr(ptr) + {} + + /*!Constructor from other smart_ptr */ + smart_ptr(const smart_ptr& ptr) + : m_ptr(ptr.m_ptr) + {} + + /*!Constructor from other smart_ptr. If pointers of pointee types are + convertible, offset_ptrs will be convertibles. Never throws.*/ + template + smart_ptr(const smart_ptr &ptr) + : m_ptr(ptr.m_ptr) + {} + + /*!Emulates static_cast operator. Never throws. */ + template + smart_ptr(const smart_ptr & r, detail::static_cast_tag) + : m_ptr(static_cast(r.get())) + {} + + /*!Emulates const_cast operator. Never throws.*/ + template + smart_ptr(const smart_ptr & r, detail::const_cast_tag) + : m_ptr(const_cast(r.get())) + {} + + /*!Emulates dynamic_cast operator. Never throws.*/ + template + smart_ptr(const smart_ptr & r, detail::dynamic_cast_tag) + : m_ptr(dynamic_cast(r.get())) + {} + + /*!Emulates reinterpret_cast operator. Never throws.*/ + template + smart_ptr(const smart_ptr & r, detail::reinterpret_cast_tag) + : m_ptr(reinterpret_cast(r.get())) + {} + + /*!Obtains raw pointer from offset. Never throws.*/ + pointer get()const + { return m_ptr; } + + /*!Pointer-like -> operator. It can return 0 pointer. Never throws.*/ + pointer operator->() const + { return this->get(); } + + /*!Dereferencing operator, if it is a null smart_ptr behavior + is undefined. Never throws.*/ + reference operator* () const + { return *(this->get()); } + + /*!Indexing operator. Never throws.*/ + reference operator[](std::ptrdiff_t idx) const + { return this->get()[idx]; } + + /*!Assignment from pointer (saves extra conversion). Never throws.*/ + smart_ptr& operator= (pointer from) + { m_ptr = from; return *this; } + + /*!Assignment from other smart_ptr. Never throws.*/ + smart_ptr& operator= (const smart_ptr & pt) + { m_ptr = pt.m_ptr; return *this; } + + /*!Assignment from related smart_ptr. If pointers of pointee types + are assignable, offset_ptrs will be assignable. Never throws.*/ + template + smart_ptr& operator= (const smart_ptr & pt) + { m_ptr = pt.m_ptr; return *this; } + + /*!smart_ptr + std::ptrdiff_t. Never throws.*/ + smart_ptr operator+ (std::ptrdiff_t offset) const + { return smart_ptr(this->get()+offset); } + + /*!smart_ptr - std::ptrdiff_t. Never throws.*/ + smart_ptr operator- (std::ptrdiff_t offset) const + { return smart_ptr(this->get()-offset); } + + /*!smart_ptr += std::ptrdiff_t. Never throws.*/ + smart_ptr &operator+= (std::ptrdiff_t offset) + { m_ptr += offset; return *this; } + + /*!smart_ptr -= std::ptrdiff_t. Never throws.*/ + smart_ptr &operator-= (std::ptrdiff_t offset) + { m_ptr -= offset; return *this; } + + /*!++smart_ptr. Never throws.*/ + smart_ptr& operator++ (void) + { ++m_ptr; return *this; } + + /*!smart_ptr++. Never throws.*/ + smart_ptr operator++ (int) + { smart_ptr temp(*this); ++*this; return temp; } + + /*!--smart_ptr. Never throws.*/ + smart_ptr& operator-- (void) + { --m_ptr; return *this; } + + /*!smart_ptr--. Never throws.*/ + smart_ptr operator-- (int) + { smart_ptr temp(*this); --*this; return temp; } + + /*!safe bool conversion operator. Never throws.*/ + operator unspecified_bool_type() const + { return this->get()? &self_t::unspecified_bool_type_func : 0; } + + /*!Not operator. Not needed in theory, but improves portability. + Never throws.*/ + bool operator! () const + { return this->get() == 0; } +/* + friend void swap (smart_ptr &pt, smart_ptr &pt2) + { + value_type *ptr = pt.get(); + pt = pt2; + pt2 = ptr; + } +*/ +}; + +/*!smart_ptr == smart_ptr. Never throws.*/ +template +inline bool operator== (const smart_ptr &pt1, + const smart_ptr &pt2) +{ return pt1.get() == pt2.get(); } + +/*!smart_ptr != smart_ptr. Never throws.*/ +template +inline bool operator!= (const smart_ptr &pt1, + const smart_ptr &pt2) +{ return pt1.get() != pt2.get(); } + +/*!smart_ptr < smart_ptr. Never throws.*/ +template +inline bool operator< (const smart_ptr &pt1, + const smart_ptr &pt2) +{ return pt1.get() < pt2.get(); } + +/*!smart_ptr <= smart_ptr. Never throws.*/ +template +inline bool operator<= (const smart_ptr &pt1, + const smart_ptr &pt2) +{ return pt1.get() <= pt2.get(); } + +/*!smart_ptr > smart_ptr. Never throws.*/ +template +inline bool operator> (const smart_ptr &pt1, + const smart_ptr &pt2) +{ return pt1.get() > pt2.get(); } + +/*!smart_ptr >= smart_ptr. Never throws.*/ +template +inline bool operator>= (const smart_ptr &pt1, + const smart_ptr &pt2) +{ return pt1.get() >= pt2.get(); } + +/*!operator<< */ +template +inline std::basic_ostream & operator<< + (std::basic_ostream & os, smart_ptr const & p) +{ return os << p.get(); } + +/*!operator>> */ +template +inline std::basic_istream & operator>> + (std::basic_istream & os, smart_ptr & p) +{ Y * tmp; return os >> tmp; p = tmp; } + +/*!std::ptrdiff_t + smart_ptr */ +template +inline smart_ptr operator+(std::ptrdiff_t diff, const smart_ptr& right) +{ return right + diff; } + +/*!smart_ptr - smart_ptr */ +template +inline std::ptrdiff_t operator- (const smart_ptr &pt, const smart_ptr &pt2) +{ return pt.get()- pt2.get(); } + +/*!swap specialization */ +template +inline void swap (smart_ptr &pt, + smart_ptr &pt2) +{ + typename smart_ptr::value_type *ptr = pt.get(); + pt = pt2; + pt2 = ptr; +} + +/*!get_pointer() enables boost::mem_fn to recognize smart_ptr. + Never throws.*/ +template +inline T * get_pointer(smart_ptr const & p) +{ return p.get(); } + +/*!Simulation of static_cast between pointers. Never throws.*/ +template +inline smart_ptr + static_pointer_cast(smart_ptr const & r) +{ + return smart_ptr(r, detail::static_cast_tag()); +} + +/*!Simulation of const_cast between pointers. Never throws.*/ +template +inline smart_ptrconst_pointer_cast(smart_ptr const & r) +{ + return smart_ptr(r, detail::const_cast_tag()); +} + +/*!Simulation of dynamic_cast between pointers. Never throws.*/ +template +inline smart_ptr + dynamic_pointer_cast(smart_ptr const & r) +{ + return smart_ptr + (r, detail::dynamic_cast_tag()); +} + +/*!Simulation of reinterpret_cast between pointers. Never throws.*/ +template +inline smart_ptr + reinterpret_pointer_cast(smart_ptr const & r) +{ + return smart_ptr(r, detail::reinterpret_cast_tag()); +} + +} //namespace intrusive { +} //namespace boost { + +#endif //#ifndef BOOST_INTRUSIVE_SMART_PTR_HPP diff --git a/test/test_container.hpp b/test/test_container.hpp new file mode 100644 index 0000000..8f27704 --- /dev/null +++ b/test/test_container.hpp @@ -0,0 +1,227 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2006 Matias Capeletto +// Copyright (c) 2007 Ion Gaztaņaga +// +// 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) +// +////////////////////////////////////////////////////////////////////////////// + +#ifndef BOOST_INTRUSIVE_TEST_TEST_CONTAINER_HPP +#define BOOST_INTRUSIVE_TEST_TEST_CONTAINER_HPP + +// std +#include +#include +#include +// Boost.Test +//#include "boost/test/included/test_exec_monitor.hpp" + +namespace boost{ +namespace intrusive{ +namespace test { + +const int NumElem = 5; + +/* +Beginning of range a.begin() iterator if a is mutable, const_iterator otherwise [4] [7] +End of range a.end() iterator if a is mutable, const_iterator otherwise [4] +Size a.size() size_type +Maximum size a.max_size() size_type +Empty container a.empty() Convertible to bool +Swap a.swap(b) void + +Expression semantics +Semantics of an expression is defined only where it differs from, or is not defined in, Assignable, Equality Comparable, or LessThan Comparable Name Expression Precondition Semantics Postcondition +Move constructor X(a) X().size() == a.size(). X() contains a copy of each of a's elements. +Move constructor X b(a); b.size() == a.size(). b contains a copy of each of a's elements. +Move Assignment operator b = a b.size() == a.size(). b contains a copy of each of a's elements. +Destructor a.~X() Each of a's elements is destroyed, and memory allocated for them (if any) is deallocated. +Beginning of range a.begin() Returns an iterator pointing to the first element in the container. [7] a.begin() is either dereferenceable or past-the-end. It is past-the-end if and only if a.size() == 0. +End of range a.end() Returns an iterator pointing one past the last element in the container. a.end() is past-the-end. +Size a.size() Returns the size of the container, that is, its number of elements. [8] a.size() >= 0 && a.size() <= max_size() +Maximum size a.max_size() Returns the largest size that this container can ever have. [8] a.max_size() >= 0 && a.max_size() >= a.size() +Empty container a.empty() Equivalent to a.size() == 0. (But possibly faster.) +Swap a.swap(b) Equivalent to swap(a,b) [9] +*/ + +/* +expression return type operational assertion/note complexity + semantics pre/post-condition + + +X::value_type T T is + CopyConstructible +compile time +X::reference lvalue of T compile time +X::const_- +reference +const lvalue of T compile time +X::iterator iterator type +whose value +type is T +any iterator category +except output iterator. +convertible to +X::const_iterator. +compile time +X::const_- +iterator +constant iterator +type whose +value type is T +any iterator category +except output iterator +compile time +X::difference_ +type +signed integral +type +is identical to the +difference type of +X::iterator and +X::const_iterator +compile time +X::size_type unsigned +integral type +size_type can +represent any +non-negative value of +compile time + + + + + + +X u; post: u.size() == 0 constant +X(); X().size() == 0 constant +X(a); a == X(a). linear +X u(a); post: u == a linear +X u = a; Equivalent to: X u; u += a; +(&a)->X(); void note: the destructor is +applied to every +element of a; all the +memory is deallocated. +linear +a.begin(); iterator; +const_- +iterator for +constant a +constant +*/ + +template< class Container > +void test_container( Container & c ) +{ + typedef typename Container::value_type value_type; + typedef typename Container::iterator iterator; + typedef typename Container::const_iterator const_iterator; + typedef typename Container::reference reference; + typedef typename Container::const_reference const_reference; + typedef typename Container::pointer pointer; + typedef typename Container::const_pointer const_pointer; + typedef typename Container::difference_type difference_type; + typedef typename Container::size_type size_type; + + const size_type num_elem = c.size(); + BOOST_CHECK( c.empty() == (num_elem == 0) ); + { + iterator it(c.begin()), itend(c.end()); + size_type i; + for(i = 0; i < num_elem; ++i){ + ++it; + } + BOOST_CHECK( it == c.end() ); + BOOST_CHECK( c.size() == i ); + } + //Check iterator conversion + BOOST_CHECK( const_iterator(c.begin()) == c.cbegin() ); + { + const_iterator it(c.cbegin()), itend(c.cend()); + size_type i; + for(i = 0; i < num_elem; ++i){ + ++it; + } + BOOST_CHECK( it == c.cend() ); + BOOST_CHECK( c.size() == i ); + } +} +/* +template< class Container > +void test_container + ( Container & c, typename Container::size_type num_elem + , Container & c2, typename Container::size_type num_elem2) +{ + typedef typename Container::value_type value_type; + typedef typename Container::iterator iterator; + typedef typename Container::const_iterator const_iterator; + typedef typename Container::reference reference; + typedef typename Container::const_reference const_reference; + typedef typename Container::pointer pointer; + typedef typename Container::const_pointer const_pointer; + typedef typename Container::difference_type difference_type; + typedef typename Container::size_type size_type; + + //For the future: + // + //move-copy constructor + //move-assignment + + test_container_helper(c, num_elem); + test_container_helper(c2, num_elem2); + + //Test swap and test again + c.swap(c2); + test_container_helper(c, num_elem2); + test_container_helper(c2, num_elem); +}*/ + +template< class Sequence > +void test_sequence( Sequence & seq ) +{ + //First the container requirements + test_container(seq); + typedef Sequence::value_type value_type; + typedef Sequence::iterator iterator; + + Sequence::size_type old_size(seq.size()); + + //Now test sequence requirements + //insert(p, t) + Sequence::value_type one(1); + iterator one_pos = seq.insert(seq.begin(), one); + BOOST_CHECK( &*seq.begin() == &one ); + BOOST_CHECK( seq.size() == (old_size + 1) ); + + //insert(p, i, j) + value_type range[2] = { value_type(2), value_type(3) }; + iterator range_it = one_pos; ++range_it; + seq.insert(range_it, &range[0], &range[2]); + BOOST_CHECK( seq.size() == (old_size + 3) ); + range_it = ++seq.begin(); + BOOST_CHECK( &*range_it == &range[0] ); + ++range_it; + BOOST_CHECK( &*range_it == &range[1] ); + + //erase(q) + iterator it_from_erase = seq.erase(seq.begin()); + BOOST_CHECK( seq.size() == (old_size + 2) ); + BOOST_CHECK( &*it_from_erase == &range[0] ); + + //erase(q1, q2) + iterator q1(it_from_erase), q2(it_from_erase); + ++++q2; + it_from_erase = seq.erase(q1, q2); + BOOST_CHECK( seq.size() == old_size ); + //clear(), assign()??? +} + +} // namespace test{ +} // namespace intrusive{ +} // namespace boost{ + +#endif // BOOST_INTRUSIVE_TEST_TEST_CONTAINER_HPP diff --git a/test/test_templates.hpp b/test/test_templates.hpp new file mode 100644 index 0000000..9374b5a --- /dev/null +++ b/test/test_templates.hpp @@ -0,0 +1,65 @@ +#ifndef BOOST_INTRUSIVE_TEST_TEST_TEMPLATES_HPP +#define BOOST_INTRUSIVE_TEST_TEST_TEMPLATES_HPP + +#include + +namespace boost { +namespace intrusive { +namespace test { + +template +void test_shift() +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + Container test_cont; + const int NumElem = 6; + + std::vector values(NumElem); + for(int i = 0; i < NumElem; ++i){ + values[i] = i+1; + } + + const int num_values = (int)values.size(); + std::vector expected_values(num_values); + + //Shift forward all possible positions 3 times + for(int i = 0; i < num_values*3; ++i){ + test_cont.assign(values.begin(), values.end()); + test_cont.shift_forward(i); + for(int j = 0; j < num_values; ++j){ + expected_values[(j + num_values - i%num_values) % num_values] = (j + 1); + } + std::copy (test_cont.begin(), test_cont.end(), + std::ostream_iterator (test_seq)); + std::stringstream stream; + std::copy (expected_values.begin(), expected_values.end(), + std::ostream_iterator (stream)); + stream << std::ends; + BOOST_CHECK (test_seq.is_equal (stream.str().c_str())); + test_cont.clear(); + } + + //Shift backwards all possible positions + for(int i = 0; i < num_values*2; ++i){ + test_cont.assign(values.begin(), values.end()); + test_cont.shift_backwards(i); + for(int j = 0; j < num_values; ++j){ + expected_values[(j + i) % num_values] = (j + 1); + } + std::copy (test_cont.begin(), test_cont.end(), + std::ostream_iterator (test_seq)); + std::stringstream stream; + std::copy (expected_values.begin(), expected_values.end(), + std::ostream_iterator (stream)); + stream << std::ends; + BOOST_CHECK (test_seq.is_equal (stream.str().c_str())); + test_cont.clear(); + } +} + +} //namespace test { +} //namespace intrusive { +} //namespace boost { + +#endif //#ifndef BOOST_INTRUSIVE_TEST_TEST_TEMPLATES_HPP diff --git a/test/unordered_multiset_test.cpp b/test/unordered_multiset_test.cpp new file mode 100644 index 0000000..a7cc383 --- /dev/null +++ b/test/unordered_multiset_test.cpp @@ -0,0 +1,463 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztaņaga 2006-2007. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#include +#include +#include "itestvalue.hpp" +#include "smart_ptr.hpp" +#include "common_functors.hpp" + +#include + +// Boost.Test +#include "boost/test/included/test_exec_monitor.hpp" + +using namespace boost::intrusive; + +static const std::size_t BucketSize = 11; + +template +struct test_unordered_multiset +{ + typedef typename ValueTraits::value_type value_type; + static void test_all (std::vector& values); + static void test_sort(std::vector& values); + static void test_insert(std::vector& values); + static void test_swap(std::vector& values); + static void test_rehash(std::vector& values); + static void test_find(std::vector& values); + static void test_impl(); + static void test_clone(std::vector& values); +}; + +template +void test_unordered_multiset::test_all (std::vector& values) +{ + test_sort(values); + test_insert(values); + test_swap(values); + test_rehash(values); + test_find(values); + test_impl(); + test_clone(values); +} + +//test case due to an error in tree implementation: +template +void test_unordered_multiset::test_impl() +{ + typedef typename ValueTraits::value_type testvalue_t; + std::vector values (5); + for (int i = 0; i < 5; ++i) + values[i].value_ = i; + typedef boost::intrusive::unordered_multiset + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_multiset_type; + typename unordered_multiset_type::bucket_type buckets [BucketSize]; + unordered_multiset_type testset(buckets, BucketSize); + + for (int i = 0; i < 5; ++i) + testset.insert (values[i]); + + testset.erase (testset.iterator_to (values[0])); + testset.erase (testset.iterator_to (values[1])); + testset.insert (values[1]); + + testset.erase (testset.iterator_to (values[2])); + testset.erase (testset.iterator_to (values[3])); +} + +//test: constructor, iterator, clear, reverse_iterator, front, back, size: +template +void test_unordered_multiset::test_sort(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + + typedef boost::intrusive::unordered_multiset + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_multiset_type; + typename unordered_multiset_type::bucket_type buckets [BucketSize]; + unordered_multiset_type testset1(buckets, BucketSize, values.begin(), values.end()); + + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("122345")); + + testset1.clear(); + BOOST_CHECK (testset1.empty()); +} + +//test: insert, const_iterator, const_reverse_iterator, erase, iterator_to: +template +void test_unordered_multiset::test_insert(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + + typedef boost::intrusive::unordered_multiset + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_multiset_type; + typename unordered_multiset_type::bucket_type buckets [BucketSize]; + unordered_multiset_type testset(buckets, BucketSize); + typedef typename unordered_multiset_type::value_type value_type; + + testset.insert(&values[0] + 2, &values[0] + 5); + + const unordered_multiset_type& const_testset = testset; + std::copy (const_testset.begin(), const_testset.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("145")); + + BOOST_DEDUCED_TYPENAME unordered_multiset_type::iterator i = testset.begin(); + BOOST_CHECK (i->value_ == 1); + + i = testset.insert (values[0]); + BOOST_CHECK (&*i == &values[0]); + + i = testset.iterator_to (values[2]); + BOOST_CHECK (&*i == &values[2]); + testset.erase(i); + + //test post-increment: + for (BOOST_DEDUCED_TYPENAME unordered_multiset_type::const_iterator i = const_testset.begin(), + e = const_testset.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("135")); + testset.clear(); + testset.insert(&values[0], &values[0] + values.size()); + test_seq.clear(); + for (BOOST_DEDUCED_TYPENAME unordered_multiset_type::const_iterator i = const_testset.begin(), + e = const_testset.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("122345")); + BOOST_CHECK (testset.erase(1) == 1); + BOOST_CHECK (testset.erase(2) == 2); + BOOST_CHECK (testset.erase(3) == 1); + BOOST_CHECK (testset.erase(4) == 1); + BOOST_CHECK (testset.erase(5) == 1); + BOOST_CHECK (testset.empty() == true); + + //Now with a single bucket + typename unordered_multiset_type::bucket_type single_bucket[1]; + unordered_multiset_type testset2(single_bucket, 1); + testset2.insert(&values[0], &values[0] + values.size()); + BOOST_CHECK (testset2.erase(5) == 1); + BOOST_CHECK (testset2.erase(2) == 2); + BOOST_CHECK (testset2.erase(1) == 1); + BOOST_CHECK (testset2.erase(4) == 1); + BOOST_CHECK (testset2.erase(3) == 1); + BOOST_CHECK (testset2.empty() == true); +} + +//test: insert (seq-version), swap, erase (seq-version), size: +template +void test_unordered_multiset::test_swap(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::unordered_multiset + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_multiset_type; + typename unordered_multiset_type::bucket_type buckets [BucketSize]; + unordered_multiset_type testset1(buckets, BucketSize, &values[0], &values[0] + 2); + + typename unordered_multiset_type::bucket_type buckets2 [BucketSize]; + unordered_multiset_type testset2(buckets2, BucketSize); + + testset2.insert (&values[0] + 2, &values[0] + 6); + testset1.swap (testset2); + + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("1245")); + //test post-increment: + for (BOOST_DEDUCED_TYPENAME unordered_multiset_type::iterator i = testset2.begin(), + e = testset2.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("23")); + + testset1.erase (testset1.iterator_to(values[5]), testset1.end()); + BOOST_CHECK (testset1.size() == 1); + // BOOST_CHECK (&testset1.front() == &values[3]); + BOOST_CHECK (&*testset1.begin() == &values[3]); +} + +//test: rehash: +template +void test_unordered_multiset::test_rehash(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + + typedef boost::intrusive::unordered_multiset + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_multiset_type; + + typename unordered_multiset_type::bucket_type buckets1 [BucketSize]; + unordered_multiset_type testset1(buckets1, BucketSize, &values[0], &values[0] + 6); + BOOST_CHECK (testset1.size() == 6); + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("122345")); + + typename unordered_multiset_type::bucket_type buckets2 [2]; + testset1.rehash(buckets2, 2); + BOOST_CHECK (testset1.size() == 6); + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("422531")); + + + typename unordered_multiset_type::bucket_type buckets3 [BucketSize*2]; + testset1.rehash(buckets3, BucketSize*2); + BOOST_CHECK (testset1.size() == 6); + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("122345")); + + //Now rehash reducing the buckets + testset1.rehash(buckets3, 2); + BOOST_CHECK (testset1.size() == 6); + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("422531")); + + //Now rehash increasing the buckets + testset1.rehash(buckets3, BucketSize*2); + BOOST_CHECK (testset1.size() == 6); + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("122345")); +} + +//test: find, equal_range (lower_bound, upper_bound): +template +void test_unordered_multiset::test_find(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::unordered_multiset + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_multiset_type; + typename unordered_multiset_type::bucket_type buckets[BucketSize]; + unordered_multiset_type testset(buckets, BucketSize, values.begin(), values.end()); + + typedef BOOST_DEDUCED_TYPENAME unordered_multiset_type::iterator iterator; + + testvalue_t cmp_val; + cmp_val.value_ = 2; + iterator i = testset.find (cmp_val); + BOOST_CHECK (i->value_ == 2); + BOOST_CHECK ((++i)->value_ == 2); + std::pair range = testset.equal_range (cmp_val); + + BOOST_CHECK (range.first->value_ == 2); + BOOST_CHECK (range.second->value_ == 3); + BOOST_CHECK (std::distance (range.first, range.second) == 2); + + cmp_val.value_ = 7; + BOOST_CHECK (testset.find (cmp_val) == testset.end()); +} + + +template +void test_unordered_multiset + ::test_clone(std::vector& values) +{ + typedef boost::intrusive::unordered_multiset + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_multiset_type; + { + //Test with equal bucket arrays + typename unordered_multiset_type::bucket_type buckets1 [BucketSize]; + typename unordered_multiset_type::bucket_type buckets2 [BucketSize]; + unordered_multiset_type testset1 (buckets1, BucketSize, values.begin(), values.end()); + unordered_multiset_type testset2 (buckets2, BucketSize); + + testset2.clone_from(testset1, test::new_cloner(), test::delete_destroyer()); + //Ordering is not guarantee in the cloning so insert data in a set and test + std::multiset + src(testset1.begin(), testset1.end()); + std::multiset + dst(testset2.begin(), testset2.end()); + BOOST_CHECK (src == dst); + testset2.clear_and_destroy(test::delete_destroyer()); + BOOST_CHECK (testset2.empty()); + } + { + //Test with bigger source bucket arrays + typename unordered_multiset_type::bucket_type buckets1 [BucketSize*2]; + typename unordered_multiset_type::bucket_type buckets2 [BucketSize]; + unordered_multiset_type testset1 (buckets1, BucketSize*2, values.begin(), values.end()); + unordered_multiset_type testset2 (buckets2, BucketSize); + + testset2.clone_from(testset1, test::new_cloner(), test::delete_destroyer()); + //Ordering is not guarantee in the cloning so insert data in a set and test + std::multiset + src(testset1.begin(), testset1.end()); + std::multiset + dst(testset2.begin(), testset2.end()); + BOOST_CHECK (src == dst); + testset2.clear_and_destroy(test::delete_destroyer()); + BOOST_CHECK (testset2.empty()); + } + { + //Test with smaller source bucket arrays + typename unordered_multiset_type::bucket_type buckets1 [BucketSize]; + typename unordered_multiset_type::bucket_type buckets2 [BucketSize*2]; + unordered_multiset_type testset1 (buckets1, BucketSize, values.begin(), values.end()); + unordered_multiset_type testset2 (buckets2, BucketSize*2); + + testset2.clone_from(testset1, test::new_cloner(), test::delete_destroyer()); + //Ordering is not guarantee in the cloning so insert data in a set and test + std::multiset + src(testset1.begin(), testset1.end()); + std::multiset + dst(testset2.begin(), testset2.end()); + BOOST_CHECK (src == dst); + testset2.clear_and_destroy(test::delete_destroyer()); + BOOST_CHECK (testset2.empty()); + } +} + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; + std::vector > data (6); + for (int i = 0; i < 6; ++i) + data[i].value_ = random_init[i]; + + test_unordered_multiset >::test_all(data); + + test_unordered_multiset >::test_all(data); + + return 0; + } +}; + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; + std::vector > data (6); + for (int i = 0; i < 6; ++i) + data[i].value_ = random_init[i]; + + test_unordered_multiset >::test_all(data); + + test_unordered_multiset >::test_all(data); + + test_unordered_multiset >::test_all(data); + + test_unordered_multiset >::test_all(data); + return 0; + } +}; + +//Explicit instantiations of non-counted classes +template class boost::intrusive::unordered_multiset + < unordered_set_base_raw_t + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_multiset + < unordered_set_member_raw_t + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_multiset + < unordered_set_auto_base_raw + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_multiset + < unordered_set_auto_member_raw + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_multiset + < unordered_set_base_smart + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_multiset + < unordered_set_member_smart + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_multiset + < unordered_set_auto_base_smart + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_multiset + < unordered_set_auto_member_smart + , boost::hash + , std::equal_to, false>; + +//Explicit instantiation of counted classes +template class boost::intrusive::unordered_multiset + < unordered_set_base_raw_t + , boost::hash + , std::equal_to, true>; +template class boost::intrusive::unordered_multiset + < unordered_set_member_raw_t + , boost::hash + , std::equal_to, true>; +template class boost::intrusive::unordered_multiset + < unordered_set_base_smart_t + , boost::hash + , std::equal_to, true>; +template class boost::intrusive::unordered_multiset + < unordered_set_member_smart_t + , boost::hash + , std::equal_to, true>; + +int test_main( int, char* [] ) +{ + test_main_template()(); + test_main_template, false>()(); + test_main_template()(); + test_main_template, true>()(); + return 0; +} diff --git a/test/unordered_set_test.cpp b/test/unordered_set_test.cpp new file mode 100644 index 0000000..4469f61 --- /dev/null +++ b/test/unordered_set_test.cpp @@ -0,0 +1,438 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Olaf Krzikalla 2004-2006. +// (C) Copyright Ion Gaztaņaga 2006-2007. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/libs/intrusive for documentation. +// +///////////////////////////////////////////////////////////////////////////// + +#include +#include +#include "itestvalue.hpp" +#include "smart_ptr.hpp" +#include "common_functors.hpp" +#include +#include + +// Boost.Test +#include "boost/test/included/test_exec_monitor.hpp" + +using namespace boost::intrusive; + +static const std::size_t BucketSize = 11; + +template +struct test_unordered_set +{ + typedef typename ValueTraits::value_type value_type; + static void test_all(std::vector& values); + static void test_sort(std::vector& values); + static void test_insert(std::vector& values); + static void test_swap(std::vector& values); + static void test_rehash(std::vector& values); + static void test_find(std::vector& values); + static void test_impl(); + static void test_clone(std::vector& values); +}; + +template +void test_unordered_set::test_all(std::vector& values) +{ + test_sort(values); + test_insert(values); + test_swap(values); + test_rehash(values); + test_find(values); + test_impl(); + test_clone(values); +} + +//test case due to an error in tree implementation: +template +void test_unordered_set::test_impl() +{ + typedef typename ValueTraits::value_type testvalue_t; + std::vector values (5); + for (int i = 0; i < 5; ++i) + values[i].value_ = i; + + typedef boost::intrusive::unordered_set + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_set_type; + typename unordered_set_type::bucket_type buckets [BucketSize]; + unordered_set_type testset(buckets, BucketSize); + for (int i = 0; i < 5; ++i) + testset.insert (values[i]); + + testset.erase (testset.iterator_to (values[0])); + testset.erase (testset.iterator_to (values[1])); + testset.insert (values[1]); + + testset.erase (testset.iterator_to (values[2])); + testset.erase (testset.iterator_to (values[3])); +} + +//test: constructor, iterator, clear, reverse_iterator, front, back, size: +template +void test_unordered_set::test_sort(std::vector& values) +{ + + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::unordered_set + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_set_type; + typename unordered_set_type::bucket_type buckets [BucketSize]; + unordered_set_type testset1(buckets, BucketSize, values.begin(), values.end()); + + BOOST_CHECK (5 == std::distance(testset1.begin(), testset1.end())); + + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("12345")); + + testset1.clear(); + BOOST_CHECK (testset1.empty()); +} + +//test: insert, const_iterator, const_reverse_iterator, erase, iterator_to: +template +void test_unordered_set::test_insert(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + typedef boost::intrusive::unordered_set + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_set_type; + typename unordered_set_type::bucket_type buckets [BucketSize]; + unordered_set_type testset(buckets, BucketSize); + testset.insert(&values[0] + 2, &values[0] + 5); + + const unordered_set_type& const_testset = testset; + std::copy (const_testset.begin(), const_testset.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("145")); + + BOOST_DEDUCED_TYPENAME unordered_set_type::iterator i = testset.begin(); + BOOST_CHECK (i->value_ == 1); + + i = testset.insert(values[0]).first; + BOOST_CHECK (&*i == &values[0]); + + i = testset.iterator_to (values[2]); + BOOST_CHECK (&*i == &values[2]); + + testset.erase (i); + //test post-increment: + for (BOOST_DEDUCED_TYPENAME unordered_set_type::const_iterator i = const_testset.begin(), + e = const_testset.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("135")); +} + +//test: insert (seq-version), swap, erase (seq-version), size: +template +void test_unordered_set::test_swap(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + + typedef boost::intrusive::unordered_set + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_set_type; + + typename unordered_set_type::bucket_type buckets1 [BucketSize]; + unordered_set_type testset1(buckets1, BucketSize, &values[0], &values[0] + 2); + + typename unordered_set_type::bucket_type buckets2 [BucketSize]; + unordered_set_type testset2(buckets2, BucketSize); + + testset2.insert (&values[0] + 2, &values[0] + 6); + testset1.swap (testset2); + + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("1245")); + //test post-increment: + for (BOOST_DEDUCED_TYPENAME unordered_set_type::iterator i = testset2.begin(), + e = testset2.end(); i != e; i++) + test_seq << *i; + BOOST_CHECK (test_seq.is_equal ("23")); + + testset1.erase (testset1.iterator_to(values[5]), testset1.end()); + BOOST_CHECK (testset1.size() == 1); + // BOOST_CHECK (&testset1.front() == &values[3]); + BOOST_CHECK (&*testset1.begin() == &values[3]); +} + +//test: rehash: +template +void test_unordered_set::test_rehash(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + + typedef boost::intrusive::unordered_set + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_set_type; + + typename unordered_set_type::bucket_type buckets1 [BucketSize]; + unordered_set_type testset1(buckets1, BucketSize, &values[0], &values[0] + 6); + BOOST_CHECK (testset1.size() == 5); + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("12345")); + + typename unordered_set_type::bucket_type buckets2 [2]; + testset1.rehash(buckets2, 2); + BOOST_CHECK (testset1.size() == 5); + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("42531")); + + typename unordered_set_type::bucket_type buckets3 [BucketSize*2]; + testset1.rehash(buckets3, BucketSize*2); + BOOST_CHECK (testset1.size() == 5); + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("12345")); + + //Now rehash reducing the buckets + testset1.rehash(buckets3, 2); + BOOST_CHECK (testset1.size() == 5); + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("42531")); + + //Now rehash increasing the buckets + testset1.rehash(buckets3, BucketSize*2); + BOOST_CHECK (testset1.size() == 5); + std::copy (testset1.begin(), testset1.end(), + std::ostream_iterator (test_seq)); + BOOST_CHECK (test_seq.is_equal ("12345")); +} + + +//test: find, equal_range (lower_bound, upper_bound): +template +void test_unordered_set::test_find(std::vector& values) +{ + typedef typename ValueTraits::value_type testvalue_t; + boost::test_tools::output_test_stream test_seq; + + typedef boost::intrusive::unordered_set + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_set_type; + typename unordered_set_type::bucket_type buckets [BucketSize]; + unordered_set_type testset (buckets, BucketSize, values.begin(), values.end()); + typedef BOOST_DEDUCED_TYPENAME unordered_set_type::iterator iterator; + + testvalue_t cmp_val; + cmp_val.value_ = 2; + iterator i = testset.find (cmp_val); + BOOST_CHECK (i->value_ == 2); + BOOST_CHECK ((++i)->value_ != 2); + std::pair range = testset.equal_range (cmp_val); + + BOOST_CHECK (range.first->value_ == 2); + BOOST_CHECK (range.second->value_ == 3); + BOOST_CHECK (std::distance (range.first, range.second) == 1); + + cmp_val.value_ = 7; + BOOST_CHECK (testset.find (cmp_val) == testset.end()); +} + +template +void test_unordered_set + ::test_clone(std::vector& values) +{ + typedef boost::intrusive::unordered_set + + ,std::equal_to + ,ValueTraits::value_type::constant_time_size, std::size_t + > unordered_set_type; + { + //Test with equal bucket arrays + typename unordered_set_type::bucket_type buckets1 [BucketSize]; + typename unordered_set_type::bucket_type buckets2 [BucketSize]; + unordered_set_type testset1 (buckets1, BucketSize, values.begin(), values.end()); + unordered_set_type testset2 (buckets2, BucketSize); + + testset2.clone_from(testset1, test::new_cloner(), test::delete_destroyer()); + //Ordering is not guarantee in the cloning so insert data in a set and test + std::set + src(testset1.begin(), testset1.end()); + std::set + dst(testset2.begin(), testset2.end()); + BOOST_CHECK (src == dst ); + testset2.clear_and_destroy(test::delete_destroyer()); + BOOST_CHECK (testset2.empty()); + } + { + //Test with bigger source bucket arrays + typename unordered_set_type::bucket_type buckets1 [BucketSize*2]; + typename unordered_set_type::bucket_type buckets2 [BucketSize]; + unordered_set_type testset1 (buckets1, BucketSize*2, values.begin(), values.end()); + unordered_set_type testset2 (buckets2, BucketSize); + + testset2.clone_from(testset1, test::new_cloner(), test::delete_destroyer()); + //Ordering is not guarantee in the cloning so insert data in a set and test + std::set + src(testset1.begin(), testset1.end()); + std::set + dst(testset2.begin(), testset2.end()); + BOOST_CHECK (src == dst ); + testset2.clear_and_destroy(test::delete_destroyer()); + BOOST_CHECK (testset2.empty()); + } + { + //Test with smaller source bucket arrays + typename unordered_set_type::bucket_type buckets1 [BucketSize]; + typename unordered_set_type::bucket_type buckets2 [BucketSize*2]; + unordered_set_type testset1 (buckets1, BucketSize, values.begin(), values.end()); + unordered_set_type testset2 (buckets2, BucketSize*2); + + testset2.clone_from(testset1, test::new_cloner(), test::delete_destroyer()); + //Ordering is not guarantee in the cloning so insert data in a set and test + std::set + src(testset1.begin(), testset1.end()); + std::set + dst(testset2.begin(), testset2.end()); + BOOST_CHECK (src == dst ); + testset2.clear_and_destroy(test::delete_destroyer()); + BOOST_CHECK (testset2.empty()); + } +} + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; + std::vector > data (6); + for (int i = 0; i < 6; ++i) + data[i].value_ = random_init[i]; + + test_unordered_set >::test_all(data); + + test_unordered_set >::test_all(data); + + return 0; + } +}; + +template +class test_main_template +{ + public: + int operator()() + { + typedef testvalue testvalue_t; + static const int random_init[6] = { 3, 2, 4, 1, 5, 2 }; + std::vector > data (6); + for (int i = 0; i < 6; ++i) + data[i].value_ = random_init[i]; + + test_unordered_set >::test_all(data); + + test_unordered_set >::test_all(data); + + test_unordered_set >::test_all(data); + + test_unordered_set >::test_all(data); + return 0; + } +}; + +template class boost::intrusive::unordered_set + < unordered_set_base_raw_t + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_set + < unordered_set_member_raw_t + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_set + < unordered_set_auto_base_raw + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_set + < unordered_set_auto_member_raw + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_set + < unordered_set_base_smart + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_set + < unordered_set_member_smart + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_set + < unordered_set_auto_base_smart + , boost::hash + , std::equal_to, false>; +template class boost::intrusive::unordered_set + < unordered_set_auto_member_smart + , boost::hash + , std::equal_to, false>; + +//Explicit instantiation of counted classes +template class boost::intrusive::unordered_set + < unordered_set_base_raw_t + , boost::hash + , std::equal_to, true>; +template class boost::intrusive::unordered_set + < unordered_set_member_raw_t + , boost::hash + , std::equal_to, true>; +template class boost::intrusive::unordered_set + < unordered_set_base_smart_t + , boost::hash + , std::equal_to, true>; +template class boost::intrusive::unordered_set + < unordered_set_member_smart_t + , boost::hash + , std::equal_to, true>; + +int test_main( int, char* [] ) +{ + test_main_template()(); + test_main_template, false>()(); + test_main_template()(); + test_main_template, true>()(); + return 0; +}