assign/doc/multi_index_container.html
2017-09-17 09:01:40 -04:00

186 lines
28 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Boost.Assignment Documentation </title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<pre>
<span class=comment>// Boost.Assign library
//
// Copyright Thorsten Ottosen 2003-2004. 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)
//
// For more information, see http://www.boost.org/libs/assign/
//
</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>detail</span><span class=special>/</span><span class=identifier>workaround</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;
</span>
<span class=preprocessor>#if </span><span class=identifier>BOOST_WORKAROUND</span><span class=special>(</span><span class=identifier>__BORLANDC__</span><span class=special>, </span><span class=identifier>BOOST_TESTED_AT</span><span class=special>(</span><span class=number>0</span><span class=identifier>x564</span><span class=special>))</span>
###<span class=identifier>pragma </span><span class=identifier>warn </span><span class=special>-</span><span class=number>8091 </span><span class=comment>// suppress warning in Boost.Test</span>
###<span class=identifier>pragma </span><span class=identifier>warn </span><span class=special>-</span><span class=number>8057 </span><span class=comment>// unused argument argc/argv in Boost.Test</span>
<span class=preprocessor>#endif
</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>assign</span><span class=special>/</span><span class=identifier>list_of</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>assign</span><span class=special>/</span><span class=identifier>list_inserter</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>multi_index_container</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>multi_index</span><span class=special>/</span><span class=identifier>identity</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>multi_index</span><span class=special>/</span><span class=identifier>member</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>multi_index</span><span class=special>/</span><span class=identifier>ordered_index</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>multi_index</span><span class=special>/</span><span class=identifier>sequenced_index</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>test</span><span class=special>/</span><span class=identifier>unit_test</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>test</span><span class=special>/</span><span class=identifier>test_tools</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>cstddef</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>ostream</span><span class=special>&gt;</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>string</span><span class=special>&gt;
</span>
<span class=keyword>using </span><span class=keyword>namespace </span><span class=identifier>boost</span><span class=special>;</span>
<span class=keyword>using </span><span class=keyword>namespace </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>multi_index</span><span class=special>;</span>
<span class=keyword>namespace </span><span class=identifier>ba </span><span class=special>= </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>assign</span><span class=special>;
</span>
<span class=comment>//
// Define a classical multi_index_container for employees
//</span>
<span class=keyword>struct </span><span class=identifier>employee</span>
<span class=special>{
</span><span class=keyword>int </span><span class=identifier>id</span><span class=special>;
</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string </span><span class=identifier>name</span><span class=special>;
</span><span class=keyword>int </span><span class=identifier>age</span><span class=special>;
</span><span class=identifier>employee</span><span class=special>(</span><span class=keyword>int </span><span class=identifier>id_</span><span class=special>,</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string </span><span class=identifier>name_</span><span class=special>,</span><span class=keyword>int </span><span class=identifier>age_</span><span class=special>):</span><span class=identifier>id</span><span class=special>(</span><span class=identifier>id_</span><span class=special>),</span><span class=identifier>name</span><span class=special>(</span><span class=identifier>name_</span><span class=special>),</span><span class=identifier>age</span><span class=special>(</span><span class=identifier>age_</span><span class=special>){}
</span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>==(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>&amp; </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const
</span><span class=special>{
</span><span class=keyword>return </span><span class=identifier>id</span><span class=special>==</span><span class=identifier>x</span><span class=special>.</span><span class=identifier>id</span><span class=special>&amp;&amp;</span><span class=identifier>name</span><span class=special>==</span><span class=identifier>x</span><span class=special>.</span><span class=identifier>name</span><span class=special>&amp;&amp;</span><span class=identifier>age</span><span class=special>==</span><span class=identifier>x</span><span class=special>.</span><span class=identifier>age</span><span class=special>;
</span><span class=special>}
</span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>&lt;(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>&amp; </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const
</span><span class=special>{
</span><span class=keyword>return </span><span class=identifier>id</span><span class=special>&lt;</span><span class=identifier>x</span><span class=special>.</span><span class=identifier>id</span><span class=special>;
</span><span class=special>}
</span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>!=(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>&amp; </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=special>!(*</span><span class=keyword>this</span><span class=special>==</span><span class=identifier>x</span><span class=special>);}
</span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>&gt; </span><span class=special>(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>&amp; </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=identifier>x</span><span class=special>&lt;*</span><span class=keyword>this</span><span class=special>;}
</span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>&gt;=(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>&amp; </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=special>!(*</span><span class=keyword>this</span><span class=special>&lt;</span><span class=identifier>x</span><span class=special>);}
</span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>&lt;=(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>&amp; </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=special>!(</span><span class=identifier>x</span><span class=special>&lt;*</span><span class=keyword>this</span><span class=special>);}
</span><span class=keyword>struct </span><span class=identifier>comp_id
</span><span class=special>{
</span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>()(</span><span class=keyword>int </span><span class=identifier>x</span><span class=special>,</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>&amp; </span><span class=identifier>e2</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=identifier>x</span><span class=special>&lt;</span><span class=identifier>e2</span><span class=special>.</span><span class=identifier>id</span><span class=special>;}
</span><span class=keyword>bool </span><span class=keyword>operator</span><span class=special>()(</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>&amp; </span><span class=identifier>e1</span><span class=special>,</span><span class=keyword>int </span><span class=identifier>x</span><span class=special>)</span><span class=keyword>const</span><span class=special>{</span><span class=keyword>return </span><span class=identifier>e1</span><span class=special>.</span><span class=identifier>id</span><span class=special>&lt;</span><span class=identifier>x</span><span class=special>;}
</span><span class=special>};
</span><span class=keyword>friend </span><span class=identifier>std</span><span class=special>::</span><span class=identifier>ostream</span><span class=special>&amp; </span><span class=keyword>operator</span><span class=special>&lt;&lt;(</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>ostream</span><span class=special>&amp; </span><span class=identifier>os</span><span class=special>,</span><span class=keyword>const </span><span class=identifier>employee</span><span class=special>&amp; </span><span class=identifier>e</span><span class=special>)
</span><span class=special>{
</span><span class=identifier>os</span><span class=special>&lt;&lt;</span><span class=identifier>e</span><span class=special>.</span><span class=identifier>id</span><span class=special>&lt;&lt;</span><span class=string>&quot; &quot;</span><span class=special>&lt;&lt;</span><span class=identifier>e</span><span class=special>.</span><span class=identifier>name</span><span class=special>&lt;&lt;</span><span class=string>&quot; &quot;</span><span class=special>&lt;&lt;</span><span class=identifier>e</span><span class=special>.</span><span class=identifier>age</span><span class=special>&lt;&lt;</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>endl</span><span class=special>;
</span><span class=keyword>return </span><span class=identifier>os</span><span class=special>;
</span><span class=special>}</span>
<span class=special>};
</span>
<span class=keyword>struct </span><span class=identifier>name</span><span class=special>{};</span>
<span class=keyword>struct </span><span class=identifier>by_name</span><span class=special>{};</span>
<span class=keyword>struct </span><span class=identifier>age</span><span class=special>{};</span>
<span class=keyword>struct </span><span class=identifier>as_inserted</span><span class=special>{};
</span>
<span class=keyword>typedef
</span><span class=identifier>multi_index_container</span><span class=special>&lt;
</span><span class=identifier>employee</span><span class=special>,
</span><span class=identifier>indexed_by</span><span class=special>&lt;
</span><span class=identifier>ordered_unique</span><span class=special>&lt;
</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=identifier>employee</span><span class=special>&gt; </span><span class=special>&gt;,
</span><span class=identifier>ordered_non_unique</span><span class=special>&lt;
</span><span class=identifier>tag</span><span class=special>&lt;</span><span class=identifier>name</span><span class=special>,</span><span class=identifier>by_name</span><span class=special>&gt;,
</span><span class=identifier>BOOST_MULTI_INDEX_MEMBER</span><span class=special>(</span><span class=identifier>employee</span><span class=special>,</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span><span class=special>,</span><span class=identifier>name</span><span class=special>)&gt;,
</span><span class=identifier>ordered_non_unique</span><span class=special>&lt;
</span><span class=identifier>tag</span><span class=special>&lt;</span><span class=identifier>age</span><span class=special>&gt;,
</span><span class=identifier>BOOST_MULTI_INDEX_MEMBER</span><span class=special>(</span><span class=identifier>employee</span><span class=special>,</span><span class=keyword>int</span><span class=special>,</span><span class=identifier>age</span><span class=special>)&gt;,
</span><span class=identifier>sequenced</span><span class=special>&lt;
</span><span class=identifier>tag</span><span class=special>&lt;</span><span class=identifier>as_inserted</span><span class=special>&gt; </span><span class=special>&gt; </span><span class=special>&gt; </span><span class=special>&gt;
</span><span class=identifier>employee_set</span><span class=special>;
</span>
<span class=preprocessor>#if </span><span class=identifier>defined</span><span class=special>(</span><span class=identifier>BOOST_NO_MEMBER_TEMPLATES</span><span class=special>)</span>
<span class=keyword>typedef </span><span class=identifier>nth_index</span><span class=special>&lt;
</span><span class=identifier>employee_set</span><span class=special>,</span><span class=number>1</span><span class=special>&gt;::</span><span class=identifier>type </span><span class=identifier>employee_set_by_name</span><span class=special>;</span>
<span class=preprocessor>#else</span>
<span class=keyword>typedef </span><span class=identifier>employee_set</span><span class=special>::</span><span class=identifier>nth_index</span><span class=special>&lt;</span><span class=number>1</span><span class=special>&gt;::</span><span class=identifier>type </span><span class=identifier>employee_set_by_name</span><span class=special>;</span>
<span class=preprocessor>#endif
</span>
<span class=keyword>typedef </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>multi_index</span><span class=special>::</span><span class=identifier>index</span><span class=special>&lt;
</span><span class=identifier>employee_set</span><span class=special>,</span><span class=identifier>age</span><span class=special>&gt;::</span><span class=identifier>type </span><span class=identifier>employee_set_by_age</span><span class=special>;</span>
<span class=keyword>typedef </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>multi_index</span><span class=special>::</span><span class=identifier>index</span><span class=special>&lt;
</span><span class=identifier>employee_set</span><span class=special>,</span><span class=identifier>as_inserted</span><span class=special>&gt;::</span><span class=identifier>type </span><span class=identifier>employee_set_as_inserted</span><span class=special>;
</span>
<span class=comment>//
// Define a multi_index_container with a list-like index and an ordered index
//</span>
<span class=keyword>typedef </span><span class=identifier>multi_index_container</span><span class=special>&lt;
</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span><span class=special>,
</span><span class=identifier>indexed_by</span><span class=special>&lt;
</span><span class=identifier>sequenced</span><span class=special>&lt;&gt;, </span><span class=comment>// list-like index
</span><span class=identifier>ordered_non_unique</span><span class=special>&lt;</span><span class=identifier>identity</span><span class=special>&lt;</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span><span class=special>&gt; </span><span class=special>&gt; </span><span class=comment>// words by alphabetical order
</span><span class=special>&gt;</span>
<span class=special>&gt; </span><span class=identifier>text_container</span><span class=special>;
</span>
<span class=keyword>void </span><span class=identifier>test_multi_index_container</span><span class=special>()</span>
<span class=special>{
</span><span class=identifier>employee_set </span><span class=identifier>eset </span><span class=special>= </span><span class=identifier>ba</span><span class=special>::</span><span class=identifier>list_of</span><span class=special>&lt; </span><span class=identifier>employee </span><span class=special>&gt;(</span><span class=number>1</span><span class=special>,</span><span class=string>&quot;Franz&quot;</span><span class=special>,</span><span class=number>30</span><span class=special>)(</span><span class=number>2</span><span class=special>,</span><span class=string>&quot;Hanz&quot;</span><span class=special>,</span><span class=number>40</span><span class=special>)(</span><span class=number>3</span><span class=special>,</span><span class=string>&quot;Ilse&quot;</span><span class=special>,</span><span class=number>50</span><span class=special>);
</span><span class=identifier>BOOST_CHECK</span><span class=special>( </span><span class=identifier>eset</span><span class=special>.</span><span class=identifier>size</span><span class=special>() </span><span class=special>== </span><span class=number>3u </span><span class=special>);
</span><span class=comment>//
// This container is associative, hence we can use 'insert()'
//
</span><span class=identifier>ba</span><span class=special>::</span><span class=identifier>insert</span><span class=special>( </span><span class=identifier>eset </span><span class=special>)(</span><span class=number>4</span><span class=special>,</span><span class=string>&quot;Kurt&quot;</span><span class=special>,</span><span class=number>55</span><span class=special>)(</span><span class=number>5</span><span class=special>,</span><span class=string>&quot;Bjarne&quot;</span><span class=special>,</span><span class=number>77</span><span class=special>)(</span><span class=number>7</span><span class=special>,</span><span class=string>&quot;Thorsten&quot;</span><span class=special>,</span><span class=number>24</span><span class=special>);
</span><span class=identifier>BOOST_CHECK</span><span class=special>( </span><span class=identifier>eset</span><span class=special>.</span><span class=identifier>size</span><span class=special>() </span><span class=special>== </span><span class=number>6u </span><span class=special>);
</span><span class=identifier>employee_set_by_name</span><span class=special>&amp; </span><span class=identifier>name_index </span><span class=special>= </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>multi_index</span><span class=special>::</span><span class=identifier>get</span><span class=special>&lt;</span><span class=identifier>name</span><span class=special>&gt;(</span><span class=identifier>eset</span><span class=special>);
</span><span class=identifier>employee_set_by_name</span><span class=special>::</span><span class=identifier>iterator </span><span class=identifier>i </span><span class=special>= </span><span class=identifier>name_index</span><span class=special>.</span><span class=identifier>find</span><span class=special>(</span><span class=string>&quot;Ilse&quot;</span><span class=special>);
</span><span class=identifier>BOOST_CHECK</span><span class=special>( </span><span class=identifier>i</span><span class=special>-&gt;</span><span class=identifier>id </span><span class=special>== </span><span class=number>3 </span><span class=special>);
</span><span class=identifier>BOOST_CHECK</span><span class=special>( </span><span class=identifier>i</span><span class=special>-&gt;</span><span class=identifier>age </span><span class=special>== </span><span class=number>50 </span><span class=special>);
</span><span class=identifier>text_container </span><span class=identifier>text </span><span class=special>= </span><span class=identifier>ba</span><span class=special>::</span><span class=identifier>list_of</span><span class=special>&lt; </span><span class=identifier>std</span><span class=special>::</span><span class=identifier>string </span><span class=special>&gt;(</span><span class=string>&quot;Have&quot;</span><span class=special>)(</span><span class=string>&quot;you&quot;</span><span class=special>)(</span><span class=string>&quot;ever&quot;</span><span class=special>)(</span><span class=string>&quot;wondered&quot;</span><span class=special>)(</span><span class=string>&quot;how&quot;</span><span class=special>)(</span><span class=string>&quot;much&quot;</span><span class=special>)(</span><span class=string>&quot;Boost&quot;</span><span class=special>)(</span><span class=string>&quot;rocks?!&quot;</span><span class=special>);
</span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=identifier>text</span><span class=special>.</span><span class=identifier>size</span><span class=special>(), </span><span class=number>8u </span><span class=special>);
</span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=special>*</span><span class=identifier>text</span><span class=special>.</span><span class=identifier>begin</span><span class=special>(), </span><span class=string>&quot;Have&quot; </span><span class=special>);
</span><span class=comment>//
// This container is a sequence, hence we can use 'push_back()' and 'push_font()'
//
</span><span class=identifier>ba</span><span class=special>::</span><span class=identifier>push_back</span><span class=special>( </span><span class=identifier>text </span><span class=special>)(</span><span class=string>&quot;Well&quot;</span><span class=special>)(</span><span class=string>&quot;,&quot;</span><span class=special>)(</span><span class=string>&quot;A&quot;</span><span class=special>)(</span><span class=string>&quot;LOT&quot;</span><span class=special>)(</span><span class=string>&quot;,&quot;</span><span class=special>)(</span><span class=string>&quot;obviously!&quot;</span><span class=special>);
</span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=identifier>text</span><span class=special>.</span><span class=identifier>size</span><span class=special>(), </span><span class=number>14u </span><span class=special>);
</span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=special>*--</span><span class=identifier>text</span><span class=special>.</span><span class=identifier>end</span><span class=special>(), </span><span class=string>&quot;obviously!&quot; </span><span class=special>);
</span><span class=identifier>ba</span><span class=special>::</span><span class=identifier>push_front</span><span class=special>( </span><span class=identifier>text </span><span class=special>) </span><span class=special>= </span><span class=string>&quot;question:&quot;</span><span class=special>, </span><span class=string>&quot;simple&quot;</span><span class=special>, </span><span class=string>&quot;A&quot;</span><span class=special>;
</span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=identifier>text</span><span class=special>.</span><span class=identifier>size</span><span class=special>(), </span><span class=number>17u </span><span class=special>);
</span><span class=identifier>BOOST_CHECK_EQUAL</span><span class=special>( </span><span class=identifier>text</span><span class=special>.</span><span class=identifier>front</span><span class=special>(), </span><span class=string>&quot;A&quot; </span><span class=special>);</span>
<span class=special>}
</span>
<span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>test</span><span class=special>/</span><span class=identifier>included</span><span class=special>/</span><span class=identifier>unit_test_framework</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;
</span>
<span class=keyword>using </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>unit_test_framework</span><span class=special>::</span><span class=identifier>test_suite</span><span class=special>;
</span>
<span class=identifier>test_suite</span><span class=special>* </span><span class=identifier>init_unit_test_suite</span><span class=special>( </span><span class=keyword>int </span><span class=identifier>argc</span><span class=special>, </span><span class=keyword>char</span><span class=special>* </span><span class=identifier>argv</span><span class=special>[] </span><span class=special>)</span>
<span class=special>{
</span><span class=identifier>test_suite</span><span class=special>* </span><span class=identifier>test </span><span class=special>= </span><span class=identifier>BOOST_TEST_SUITE</span><span class=special>( </span><span class=string>&quot;List Test Suite&quot; </span><span class=special>);
</span><span class=identifier>test</span><span class=special>-&gt;</span><span class=identifier>add</span><span class=special>( </span><span class=identifier>BOOST_TEST_CASE</span><span class=special>( </span><span class=special>&amp;</span><span class=identifier>test_multi_index_container </span><span class=special>) </span><span class=special>);
</span><span class=keyword>return </span><span class=identifier>test</span><span class=special>;</span>
<span class=special>}
</span>
</pre>
</body>
</html>