]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Inplace and infix operators</title> | |
5 | <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.74.0"> | |
7 | <link rel="home" href="../../index.html" title="Chapter 1. Boost.Icl"> | |
8 | <link rel="up" href="../implementation.html" title="Implementation"> | |
9 | <link rel="prev" href="complexity.html" title="Complexity"> | |
10 | <link rel="next" href="../function_reference.html" title="Function Reference"> | |
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="../../../../../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="complexity.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../implementation.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="../function_reference.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
24 | </div> | |
25 | <div class="section boost_icl_implementation_inplace_and_infix_operators" lang="en"> | |
26 | <div class="titlepage"><div><div><h3 class="title"> | |
27 | <a name="boost_icl.implementation.inplace_and_infix_operators"></a><a class="link" href="inplace_and_infix_operators.html" title="Inplace and infix operators">Inplace | |
28 | and infix operators</a> | |
29 | </h3></div></div></div> | |
30 | <p> | |
31 | For the major operations <span class="emphasis"><em>addition, subtraction, intersection</em></span> | |
32 | of <span class="bold"><strong>icl</strong></span> containers and for <span class="emphasis"><em>symmetric | |
33 | difference</em></span> inplace <code class="computeroutput"><span class="keyword">operator</span></code>s | |
34 | <code class="computeroutput"><span class="special">+=</span> <span class="special">|=,</span> | |
35 | <span class="special">-=,</span> <span class="special">&=</span></code> | |
36 | and <code class="computeroutput"><span class="special">^=</span></code> are provided. | |
37 | </p> | |
38 | <p> | |
39 | For every <span class="emphasis"><em><span class="bold"><strong>inplace</strong></span></em></span> operator | |
40 | ||
41 | </p> | |
42 | <pre class="programlisting"><span class="identifier">T</span><span class="special">&</span> <span class="keyword">operator</span> <span class="identifier">o</span><span class="special">=</span> <span class="special">(</span><span class="identifier">T</span><span class="special">&</span> <span class="identifier">object</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">P</span><span class="special">&</span> <span class="identifier">operand</span><span class="special">)</span> | |
43 | </pre> | |
44 | <p> | |
45 | the <span class="bold"><strong>icl</strong></span> provides corresponding <span class="emphasis"><em><span class="bold"><strong>infix</strong></span></em></span> operators. | |
46 | </p> | |
47 | <pre class="programlisting"><span class="identifier">T</span> <span class="keyword">operator</span> <span class="identifier">o</span> <span class="special">(</span><span class="identifier">T</span> <span class="identifier">object</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">P</span><span class="special">&</span> <span class="identifier">operand</span><span class="special">){</span> <span class="keyword">return</span> <span class="identifier">object</span> <span class="identifier">o</span><span class="special">=</span> <span class="identifier">operand</span><span class="special">;</span> <span class="special">}</span> | |
48 | <span class="identifier">T</span> <span class="keyword">operator</span> <span class="identifier">o</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">P</span><span class="special">&</span> <span class="identifier">operand</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">object</span><span class="special">){</span> <span class="keyword">return</span> <span class="identifier">object</span> <span class="identifier">o</span><span class="special">=</span> <span class="identifier">operand</span><span class="special">;</span> <span class="special">}</span> | |
49 | </pre> | |
50 | <p> | |
51 | </p> | |
52 | <p> | |
53 | From this implementation of the infix <code class="computeroutput"><span class="keyword">operator</span> | |
54 | <span class="identifier">o</span></code> the compiler will hopefully use | |
55 | return value optimization <a href="http://en.wikipedia.org/wiki/Return_value_optimization" target="_top">(RVO)</a> | |
56 | creating no temporary object and performing one copy of the first argument | |
57 | <code class="computeroutput"><span class="identifier">object</span></code>. | |
58 | </p> | |
59 | <div class="caution"><table border="0" summary="Caution"> | |
60 | <tr> | |
61 | <td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../doc/src/images/caution.png"></td> | |
62 | <th align="left">Caution</th> | |
63 | </tr> | |
64 | <tr><td align="left" valign="top"><p> | |
65 | Compared to the <span class="emphasis"><em>inplace</em></span> <code class="computeroutput"><span class="keyword">operator</span> | |
66 | <span class="identifier">o</span><span class="special">=</span></code> | |
67 | every use of an <span class="emphasis"><em>infix</em></span> <code class="computeroutput"><span class="keyword">operator</span> | |
68 | <span class="identifier">o</span></code> requires <span class="emphasis"><em><span class="bold"><strong>one extra copy</strong></span></em></span> of the first argument | |
69 | <code class="computeroutput"><span class="identifier">object</span></code> that passes a container. | |
70 | </p></td></tr> | |
71 | </table></div> | |
72 | <p> | |
73 | Use infix operators only, if | |
74 | </p> | |
75 | <div class="itemizedlist"><ul type="disc"> | |
76 | <li> | |
77 | efficiency is not crucial, e.g. the containers copied are small. | |
78 | </li> | |
79 | <li> | |
80 | a concise and short notation is more important than efficiency in your | |
81 | context. | |
82 | </li> | |
83 | <li> | |
84 | you need the result of operator <code class="computeroutput"><span class="identifier">o</span><span class="special">=</span></code> as a copy anyway. | |
85 | </li> | |
86 | </ul></div> | |
87 | <a name="boost_icl.implementation.inplace_and_infix_operators.time_complexity_of_infix__code__phrase_role__identifier__operators__phrase___phrase_role__identifier__o__phrase___code_"></a><h6> | |
88 | <a name="id1133408"></a> | |
89 | <a class="link" href="inplace_and_infix_operators.html#boost_icl.implementation.inplace_and_infix_operators.time_complexity_of_infix__code__phrase_role__identifier__operators__phrase___phrase_role__identifier__o__phrase___code_">Time | |
90 | Complexity of infix <code class="computeroutput"><span class="identifier">operators</span> <span class="identifier">o</span></code></a> | |
91 | </h6> | |
92 | <p> | |
93 | The time complexity of all infix operators of the <span class="bold"><strong>icl</strong></span> | |
94 | is biased by the extra copy of the <code class="computeroutput"><span class="identifier">object</span></code> | |
95 | argument. So all infix <code class="computeroutput"><span class="identifier">operators</span> | |
96 | <span class="identifier">o</span></code> are at least <span class="emphasis"><em>linear</em></span> | |
97 | in <code class="computeroutput"><span class="identifier">n</span> <span class="special">=</span> | |
98 | <span class="identifier">object</span><span class="special">.</span><span class="identifier">iterative_size</span><span class="special">()</span></code>. | |
99 | Taking this into account, the complexities of all infix operators can be | |
100 | determined from the corresponding inplace <code class="computeroutput"><span class="identifier">operators</span> | |
101 | <span class="identifier">o</span><span class="special">=</span></code> | |
102 | they depend on. | |
103 | </p> | |
104 | </div> | |
105 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
106 | <td align="left"></td> | |
107 | <td align="right"><div class="copyright-footer">Copyright © 2007 -2010 Joachim Faulhaber<br>Copyright © 1999 -2006 Cortex Software GmbH<p> | |
108 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
109 | 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>) | |
110 | </p> | |
111 | </div></td> | |
112 | </tr></table> | |
113 | <hr> | |
114 | <div class="spirit-nav"> | |
115 | <a accesskey="p" href="complexity.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../implementation.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="../function_reference.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
116 | </div> | |
117 | </body> | |
118 | </html> |