1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Unconstrained Sets</title> 5<link rel="stylesheet" href="../../boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Bimap"> 8<link rel="up" href="../the_tutorial.html" title="The tutorial"> 9<link rel="prev" href="bimaps_with_user_defined_names.html" title="Bimaps with user defined names"> 10<link rel="next" href="additional_information.html" title="Additional information"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="bimaps_with_user_defined_names.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../the_tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="additional_information.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_bimap.the_tutorial.unconstrained_sets"></a><a class="link" href="unconstrained_sets.html" title="Unconstrained Sets">Unconstrained 28 Sets</a> 29</h3></div></div></div> 30<p> 31 Unconstrained sets allow the user to disable one of the views of a bimap. 32 Doing so makes the bimap operations execute faster and reduces memory consumption. 33 This completes the bidirectional mapping framework by including unidirectional 34 mappings as a particular case. 35 </p> 36<p> 37 Unconstrained sets are useful for the following reasons: 38 </p> 39<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 40<li class="listitem"> 41 A bimap type has stronger guarantees than its standard equivalent, and 42 includes some useful functions (replace, modify) that the standard does 43 not have. 44 </li> 45<li class="listitem"> 46 You can view the mapping as a collection of relations. 47 </li> 48<li class="listitem"> 49 Using this kind of map makes the code very extensible. If, at any moment 50 of the development, the need to perform searches from the right side 51 of the mapping arises, the only necessary change is to the <code class="computeroutput"><span class="keyword">typedef</span></code>. 52 </li> 53</ul></div> 54<p> 55 Given this bimap instance, 56 </p> 57<p> 58</p> 59<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">bimap</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">unconstrained_set_of</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">></span> <span class="identifier">bm_type</span><span class="special">;</span> 60<span class="keyword">typedef</span> <span class="identifier">bm_type</span><span class="special">::</span><span class="identifier">left_map</span> <span class="identifier">map_type</span><span class="special">;</span> 61 62<span class="identifier">bm_type</span> <span class="identifier">bm</span><span class="special">;</span> 63<span class="identifier">map_type</span> <span class="special">&</span> <span class="identifier">m</span> <span class="special">=</span> <span class="identifier">bm</span><span class="special">.</span><span class="identifier">left</span><span class="special">;</span> 64</pre> 65<p> 66 </p> 67<p> 68 or this standard map one 69 </p> 70<p> 71</p> 72<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">map</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="keyword">int</span> <span class="special">></span> <span class="identifier">map_type</span><span class="special">;</span> 73 74<span class="identifier">map_type</span> <span class="identifier">m</span><span class="special">;</span> 75</pre> 76<p> 77 </p> 78<p> 79 The following code snippet is valid 80 </p> 81<p> 82</p> 83<pre class="programlisting"><span class="identifier">m</span><span class="special">[</span><span class="string">"one"</span><span class="special">]</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> 84 85<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">m</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="string">"one"</span><span class="special">)</span> <span class="special">!=</span> <span class="identifier">m</span><span class="special">.</span><span class="identifier">end</span><span class="special">()</span> <span class="special">);</span> 86 87<span class="keyword">for</span><span class="special">(</span> <span class="identifier">map_type</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">m</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">iend</span> <span class="special">=</span> <span class="identifier">m</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span> <span class="identifier">i</span> <span class="special">!=</span> <span class="identifier">iend</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span> <span class="special">)</span> 88<span class="special">{</span> 89 <a class="co" name="boost_bimap.the_tutorial.unconstrained_sets.c0" href="unconstrained_sets.html#boost_bimap.the_tutorial.unconstrained_sets.c1"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a><span class="special">++(</span><span class="identifier">i</span><span class="special">-></span><span class="identifier">second</span><span class="special">);</span> 90<span class="special">}</span> 91 92<span class="identifier">m</span><span class="special">.</span><span class="identifier">erase</span><span class="special">(</span><span class="string">"one"</span><span class="special">);</span> 93</pre> 94<p> 95 </p> 96<div class="calloutlist"><table border="0" summary="Callout list"><tr> 97<td width="5%" valign="top" align="left"><p><a name="boost_bimap.the_tutorial.unconstrained_sets.c1"></a><a href="#boost_bimap.the_tutorial.unconstrained_sets.c0"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> 98<td valign="top" align="left"><p> 99 The right collection of the bimap is mutable so its elements can be modified 100 using iterators. 101 </p></td> 102</tr></table></div> 103<p> 104 But using a bimap has some benefits 105 </p> 106<p> 107</p> 108<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">map_type</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">const_iterator</span><span class="special">;</span> 109<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">const_iterator</span><span class="special">,</span><span class="identifier">const_iterator</span><span class="special">></span> <span class="identifier">const_range</span><span class="special">;</span> 110 111<a class="co" name="boost_bimap.the_tutorial.unconstrained_sets.c2" href="unconstrained_sets.html#boost_bimap.the_tutorial.unconstrained_sets.c3"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a><span class="identifier">const_range</span> <span class="identifier">r</span> <span class="special">=</span> <span class="identifier">m</span><span class="special">.</span><span class="identifier">range</span><span class="special">(</span> <span class="string">"one"</span> <span class="special"><=</span> <span class="identifier">_key</span><span class="special">,</span> <span class="identifier">_key</span> <span class="special"><=</span> <span class="string">"two"</span> <span class="special">);</span> 112<span class="keyword">for</span><span class="special">(</span> <span class="identifier">const_iterator</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">r</span><span class="special">.</span><span class="identifier">first</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">!=</span> <span class="identifier">r</span><span class="special">.</span><span class="identifier">second</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span> <span class="special">)</span> 113<span class="special">{</span> 114 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">i</span><span class="special">-></span><span class="identifier">first</span> <span class="special"><<</span> <span class="string">"-->"</span> <span class="special"><<</span> <span class="identifier">i</span><span class="special">-></span><span class="identifier">second</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 115<span class="special">}</span> 116 117<span class="identifier">m</span><span class="special">.</span><span class="identifier">modify_key</span><span class="special">(</span> <span class="identifier">m</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">_key</span> <span class="special">=</span> <span class="string">"1"</span> <span class="special">);</span> 118</pre> 119<p> 120 </p> 121<div class="calloutlist"><table border="0" summary="Callout list"><tr> 122<td width="5%" valign="top" align="left"><p><a name="boost_bimap.the_tutorial.unconstrained_sets.c3"></a><a href="#boost_bimap.the_tutorial.unconstrained_sets.c2"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> 123<td valign="top" align="left"><p> 124 This range is a model of BidirectionalRange, read the docs of Boost.Range 125 for more information. 126 </p></td> 127</tr></table></div> 128<p> 129 <a href="../../../../example/unconstrained_collection.cpp" target="_top">Go to source code</a> 130 </p> 131</div> 132<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 133<td align="left"></td> 134<td align="right"><div class="copyright-footer">Copyright © 2006-2012 Matias Capeletto<p> 135 Distributed under the Boost Software License, Version 1.0. (See accompanying 136 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 137 </p> 138</div></td> 139</tr></table> 140<hr> 141<div class="spirit-nav"> 142<a accesskey="p" href="bimaps_with_user_defined_names.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../the_tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="additional_information.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 143</div> 144</body> 145</html> 146