• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Std copy</title>
5<link rel="stylesheet" href="../../../../../../doc/src/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.Icl">
8<link rel="up" href="../examples.html" title="Examples">
9<link rel="prev" href="user_groups.html" title="User groups">
10<link rel="next" href="std_transform.html" title="Std transform">
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="user_groups.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../examples.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="std_transform.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_icl.examples.std_copy"></a><a class="link" href="std_copy.html" title="Std copy">Std copy</a>
28</h3></div></div></div>
29<p>
30        The standard algorithm <a href="http://www.cplusplus.com/reference/algorithm/copy/" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span></code></a>
31        can be used to fill interval containers from standard containers of intervals
32        or interval value pairs (segments). Because intervals do not represent <span class="emphasis"><em><span class="bold"><strong>elements</strong></span></em></span> but <span class="emphasis"><em><span class="bold"><strong>sets</strong></span></em></span>,
33        that can be empty or contain more than one element, the usage of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span></code>
34        differs from what we are familiar with using <span class="emphasis"><em>containers of elements</em></span>.
35      </p>
36<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
37<li class="listitem">
38            Use <code class="computeroutput"><span class="identifier">icl</span><span class="special">::</span><span class="identifier">inserter</span></code> from <code class="computeroutput"><span class="preprocessor">#include</span>
39            <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">icl</span><span class="special">/</span><span class="identifier">iterator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code> instead of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">inserter</span></code>
40            to call insertions on the target interval container.
41          </li>
42<li class="listitem">
43            As shown in the examples above and below this point, most of the time
44            we will not be interested to <code class="computeroutput"><span class="identifier">insert</span></code>
45            segments into <code class="computeroutput"><a class="link" href="../../boost/icl/interval_map.html" title="Class template interval_map">interval_maps</a></code>
46            but to <a class="link" href="../function_reference/addition.html" title="Addition"><span class="emphasis"><em><span class="bold"><strong>add</strong></span></em></span></a> them, in order to generate
47            the desired aggregation results. You can use <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span></code>
48            with an <code class="computeroutput"><span class="identifier">icl</span><span class="special">::</span><span class="identifier">adder</span></code> instead of an <code class="computeroutput"><span class="identifier">icl</span><span class="special">::</span><span class="identifier">inserter</span></code>
49            to achieve this.
50          </li>
51</ul></div>
52<p>
53</p>
54<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
55<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
56<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">algorithm</span><span class="special">&gt;</span>
57<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">icl</span><span class="special">/</span><span class="identifier">interval_map</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
58
59<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span>
60<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span>
61<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">icl</span><span class="special">;</span>
62
63<span class="comment">// 'make_segments' returns a vector of interval value pairs, which</span>
64<span class="comment">// are not sorted. The values are taken from the minimal example</span>
65<span class="comment">// in section 'interval combining styles'.</span>
66<span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">make_segments</span><span class="special">()</span>
67<span class="special">{</span>
68    <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">segment_vec</span><span class="special">;</span>
69    <span class="identifier">segment_vec</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">make_pair</span><span class="special">(</span><span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">right_open</span><span class="special">(</span><span class="number">2</span><span class="special">,</span><span class="number">4</span><span class="special">),</span> <span class="number">1</span><span class="special">));</span>
70    <span class="identifier">segment_vec</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">make_pair</span><span class="special">(</span><span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">right_open</span><span class="special">(</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">),</span> <span class="number">1</span><span class="special">));</span>
71    <span class="identifier">segment_vec</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">make_pair</span><span class="special">(</span><span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">right_open</span><span class="special">(</span><span class="number">1</span><span class="special">,</span><span class="number">3</span><span class="special">),</span> <span class="number">1</span><span class="special">));</span>
72    <span class="keyword">return</span> <span class="identifier">segment_vec</span><span class="special">;</span>
73<span class="special">}</span>
74
75<span class="comment">// 'show_segments' displays the source segements.</span>
76<span class="keyword">void</span> <span class="identifier">show_segments</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;&amp;</span> <span class="identifier">segments</span><span class="special">)</span>
77<span class="special">{</span>
78    <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">iter</span> <span class="special">=</span> <span class="identifier">segments</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
79    <span class="keyword">while</span><span class="special">(</span><span class="identifier">iter</span> <span class="special">!=</span> <span class="identifier">segments</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span>
80    <span class="special">{</span>
81        <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"("</span> <span class="special">&lt;&lt;</span> <span class="identifier">iter</span><span class="special">-&gt;</span><span class="identifier">first</span> <span class="special">&lt;&lt;</span> <span class="string">","</span> <span class="special">&lt;&lt;</span> <span class="identifier">iter</span><span class="special">-&gt;</span><span class="identifier">second</span> <span class="special">&lt;&lt;</span> <span class="string">")"</span><span class="special">;</span>
82        <span class="special">++</span><span class="identifier">iter</span><span class="special">;</span>
83    <span class="special">}</span>
84<span class="special">}</span>
85
86<span class="keyword">void</span> <span class="identifier">std_copy</span><span class="special">()</span>
87<span class="special">{</span>
88    <span class="comment">// So we have some segments stored in an std container.</span>
89    <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">discrete_interval</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">segments</span> <span class="special">=</span> <span class="identifier">make_segments</span><span class="special">();</span>
90    <span class="comment">// Display the input</span>
91    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"input sequence: "</span><span class="special">;</span> <span class="identifier">show_segments</span><span class="special">(</span><span class="identifier">segments</span><span class="special">);</span> <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\n\n"</span><span class="special">;</span>
92
93    <span class="comment">// We are going to 'std::copy' those segments into an interval_map:</span>
94    <span class="identifier">interval_map</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">segmap</span><span class="special">;</span>
95
96    <span class="comment">// Use an 'icl::inserter' from &lt;boost/icl/iterator.hpp&gt; to call </span>
97    <span class="comment">// insertion on the interval container.</span>
98    <span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span><span class="special">(</span><span class="identifier">segments</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">segments</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span>
99              <span class="identifier">icl</span><span class="special">::</span><span class="identifier">inserter</span><span class="special">(</span><span class="identifier">segmap</span><span class="special">,</span> <span class="identifier">segmap</span><span class="special">.</span><span class="identifier">end</span><span class="special">()));</span>
100    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"icl::inserting: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">segmap</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
101    <span class="identifier">segmap</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span>
102
103    <span class="comment">// When we are feeding data into interval_maps, most of the time we are</span>
104    <span class="comment">// intending to compute an aggregation result. So we are not interested</span>
105    <span class="comment">// the std::insert semantincs but the aggregating icl::addition semantics.</span>
106    <span class="comment">// To achieve this there is an icl::add_iterator and an icl::adder function </span>
107    <span class="comment">// provided in &lt;boost/icl/iterator.hpp&gt;.</span>
108    <span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span><span class="special">(</span><span class="identifier">segments</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">segments</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span>
109              <span class="identifier">icl</span><span class="special">::</span><span class="identifier">adder</span><span class="special">(</span><span class="identifier">segmap</span><span class="special">,</span> <span class="identifier">segmap</span><span class="special">.</span><span class="identifier">end</span><span class="special">()));</span> <span class="comment">//Aggregating associated values</span>
110    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"icl::adding   : "</span> <span class="special">&lt;&lt;</span> <span class="identifier">segmap</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
111
112    <span class="comment">// In this last case, the semantics of 'std::copy' transforms to the </span>
113    <span class="comment">// generalized addition operation, that is implemented by operator</span>
114    <span class="comment">// += or + on itl maps and sets.</span>
115<span class="special">}</span>
116
117<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
118<span class="special">{</span>
119    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"&gt;&gt;    Interval Container Library: Example std_copy.cpp    &lt;&lt;\n"</span><span class="special">;</span>
120    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"-----------------------------------------------------------\n"</span><span class="special">;</span>
121    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Using std::copy to fill an interval_map:\n\n"</span><span class="special">;</span>
122
123    <span class="identifier">std_copy</span><span class="special">();</span>
124    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
125<span class="special">}</span>
126
127<span class="comment">// Program output:</span>
128<span class="comment">/*---------------------------------------------------------
129&gt;&gt;    Interval Container Library: Example std_copy.cpp    &lt;&lt;
130-----------------------------------------------------------
131Using std::copy to fill an interval_map:
132
133input sequence: ([2,4),1)([4,5),1)([1,3),1)
134
135icl::inserting: {([1,5)-&gt;1)}
136icl::adding   : {([1,2)-&gt;1)([2,3)-&gt;2)([3,5)-&gt;1)}
137---------------------------------------------------------*/</span>
138</pre>
139<p>
140      </p>
141</div>
142<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
143<td align="left"></td>
144<td align="right"><div class="copyright-footer">Copyright © 2007-2010 Joachim
145      Faulhaber<br>Copyright © 1999-2006 Cortex Software
146      GmbH<p>
147        Distributed under the Boost Software License, Version 1.0. (See accompanying
148        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>)
149      </p>
150</div></td>
151</tr></table>
152<hr>
153<div class="spirit-nav">
154<a accesskey="p" href="user_groups.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../examples.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="std_transform.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
155</div>
156</body>
157</html>
158