• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Extended functionality: Basic extensions</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../container.html" title="Chapter 9. Boost.Container">
10<link rel="prev" href="non_standard_containers.html" title="Non-standard containers">
11<link rel="next" href="configurable_containers.html" title="Extended functionality: Configurable containers">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="non_standard_containers.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../container.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="configurable_containers.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="container.extended_functionality"></a><a class="link" href="extended_functionality.html" title="Extended functionality: Basic extensions">Extended functionality:
29    Basic extensions</a>
30</h2></div></div></div>
31<div class="toc"><dl class="toc">
32<dt><span class="section"><a href="extended_functionality.html#container.extended_functionality.default_initialialization">Default
33      initialization for vector-like containers</a></span></dt>
34<dt><span class="section"><a href="extended_functionality.html#container.extended_functionality.ordered_range_insertion">Ordered
35      range insertion for associative containers (<span class="emphasis"><em>ordered_unique_range</em></span>,
36      <span class="emphasis"><em>ordered_range</em></span>) </a></span></dt>
37<dt><span class="section"><a href="extended_functionality.html#container.extended_functionality.constant_time_range_splice">Constant-time
38      range splice for <code class="computeroutput"><span class="special">(</span><span class="identifier">s</span><span class="special">)</span><span class="identifier">list</span></code></a></span></dt>
39</dl></div>
40<div class="section">
41<div class="titlepage"><div><div><h3 class="title">
42<a name="container.extended_functionality.default_initialialization"></a><a class="link" href="extended_functionality.html#container.extended_functionality.default_initialialization" title="Default initialization for vector-like containers">Default
43      initialization for vector-like containers</a>
44</h3></div></div></div>
45<p>
46        STL and most other containers value initialize new elements in common operations
47        like <code class="computeroutput"><span class="identifier">vector</span><span class="special">::</span><span class="identifier">resize</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">)</span></code> or <code class="computeroutput"><span class="keyword">explicit</span>
48        <span class="identifier">vector</span><span class="special">::</span><span class="identifier">vector</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">)</span></code>.
49      </p>
50<p>
51        In some performance-sensitive environments, where vectors are used as a replacement
52        for variable-size buffers for file or network operations, <a href="http://en.cppreference.com/w/cpp/language/value_initialization" target="_top">value
53        initialization</a> is a cost that is not negligible as elements are going
54        to be overwritten by an external source shortly after new elements are added
55        to the container.
56      </p>
57<p>
58        <span class="bold"><strong>Boost.Container</strong></span> offers two new members for
59        <code class="computeroutput"><span class="identifier">vector</span></code>, <code class="computeroutput"><span class="identifier">static_vector</span></code>
60        and <code class="computeroutput"><span class="identifier">stable_vector</span></code>: <code class="computeroutput"><span class="keyword">explicit</span> <span class="identifier">container</span><span class="special">::</span><span class="identifier">container</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">default_init_t</span><span class="special">)</span></code> and <code class="computeroutput"><span class="identifier">container</span><span class="special">::</span><span class="identifier">resize</span><span class="special">(</span><span class="identifier">size_type</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">default_init_t</span><span class="special">)</span></code>, where new elements are constructed using
61        <a href="http://en.cppreference.com/w/cpp/language/default_initialization" target="_top">default
62        initialization</a>.
63      </p>
64</div>
65<div class="section">
66<div class="titlepage"><div><div><h3 class="title">
67<a name="container.extended_functionality.ordered_range_insertion"></a><a class="link" href="extended_functionality.html#container.extended_functionality.ordered_range_insertion" title="Ordered range insertion for associative containers (ordered_unique_range, ordered_range)">Ordered
68      range insertion for associative containers (<span class="emphasis"><em>ordered_unique_range</em></span>,
69      <span class="emphasis"><em>ordered_range</em></span>) </a>
70</h3></div></div></div>
71<p>
72        When filling associative containers big performance gains can be achieved
73        if the input range to be inserted is guaranteed by the user to be ordered
74        according to the predicate. This can happen when inserting values from a
75        <code class="computeroutput"><span class="identifier">set</span></code> to a <code class="computeroutput"><span class="identifier">multiset</span></code>
76        or between different associative container families (<code class="computeroutput"><span class="special">[</span><span class="identifier">multi</span><span class="special">]</span><span class="identifier">set</span><span class="special">/</span><span class="identifier">map</span></code>
77        vs. <code class="computeroutput"><span class="identifier">flat_</span><span class="special">[</span><span class="identifier">multi</span><span class="special">]</span><span class="identifier">set</span><span class="special">/</span><span class="identifier">map</span></code>).
78      </p>
79<p>
80        <span class="bold"><strong>Boost.Container</strong></span> has some overloads for constructors
81        and insertions taking an <code class="computeroutput"><span class="identifier">ordered_unique_range_t</span></code>
82        or an <code class="computeroutput"><span class="identifier">ordered_range_t</span></code> tag
83        parameters as the first argument. When an <code class="computeroutput"><span class="identifier">ordered_unique_range_t</span></code>
84        overload is used, the user notifies the container that the input range is
85        ordered according to the container predicate and has no duplicates. When
86        an <code class="computeroutput"><span class="identifier">ordered_range_t</span></code> overload
87        is used, the user notifies the container that the input range is ordered
88        according to the container predicate but it might have duplicates. With this
89        information, the container can avoid multiple predicate calls and improve
90        insertion times.
91      </p>
92</div>
93<div class="section">
94<div class="titlepage"><div><div><h3 class="title">
95<a name="container.extended_functionality.constant_time_range_splice"></a><a class="link" href="extended_functionality.html#container.extended_functionality.constant_time_range_splice" title="Constant-time range splice for (s)list">Constant-time
96      range splice for <code class="computeroutput"><span class="special">(</span><span class="identifier">s</span><span class="special">)</span><span class="identifier">list</span></code></a>
97</h3></div></div></div>
98<p>
99        In the first C++ standard <code class="computeroutput"><span class="identifier">list</span><span class="special">::</span><span class="identifier">size</span><span class="special">()</span></code> was not required to be constant-time, and
100        that caused some controversy in the C++ community. Quoting Howard Hinnant's
101        <a href="http://howardhinnant.github.io/On_list_size.html" target="_top"><span class="emphasis"><em>On
102        List Size</em></span></a> paper:
103      </p>
104<div class="blockquote"><blockquote class="blockquote">
105<p>
106          <span class="emphasis"><em>There is a considerable debate on whether <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">size</span><span class="special">()</span></code> should be O(1) or O(N). The usual argument
107          notes that it is a tradeoff with:</em></span>
108        </p>
109<p>
110          <code class="computeroutput"><span class="identifier">splice</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">position</span><span class="special">,</span> <span class="identifier">list</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">iterator</span>
111          <span class="identifier">first</span><span class="special">,</span>
112          <span class="identifier">iterator</span> <span class="identifier">last</span><span class="special">);</span></code>
113        </p>
114<p>
115          <span class="emphasis"><em>If size() is O(1) and this != &amp;x, then this method must perform
116          a linear operation so that it can adjust the size member in each list</em></span>
117        </p>
118</blockquote></div>
119<p>
120        C++11 definitely required <code class="computeroutput"><span class="identifier">size</span><span class="special">()</span></code> to be O(1), so range splice became O(N).
121        However, Howard Hinnant's paper proposed a new <code class="computeroutput"><span class="identifier">splice</span></code>
122        overload so that even O(1) <code class="computeroutput"><span class="identifier">list</span><span class="special">:</span><span class="identifier">size</span><span class="special">()</span></code>
123        implementations could achieve O(1) range splice when the range size was known
124        to the caller:
125      </p>
126<div class="blockquote"><blockquote class="blockquote">
127<p>
128          <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">splice</span><span class="special">(</span><span class="identifier">iterator</span> <span class="identifier">position</span><span class="special">,</span> <span class="identifier">list</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">iterator</span>
129          <span class="identifier">first</span><span class="special">,</span>
130          <span class="identifier">iterator</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">size_type</span>
131          <span class="identifier">n</span><span class="special">);</span></code>
132        </p>
133<p>
134          <span class="bold"><strong>Effects</strong></span>: Inserts elements in the range
135          [first, last) before position and removes the elements from x.
136        </p>
137<p>
138          <span class="bold"><strong>Requires</strong></span>: [first, last) is a valid range
139          in x. The result is undefined if position is an iterator in the range [first,
140          last). Invalidates only the iterators and references to the spliced elements.
141          n == distance(first, last).
142        </p>
143<p>
144          <span class="bold"><strong>Throws</strong></span>: Nothing.
145        </p>
146<p>
147          <span class="bold"><strong>Complexity</strong></span>: Constant time.
148        </p>
149</blockquote></div>
150<p>
151        This new splice signature allows the client to pass the distance of the input
152        range in. This information is often available at the call site. If it is
153        passed in, then the operation is constant time, even with an O(1) size.
154      </p>
155<p>
156        <span class="bold"><strong>Boost.Container</strong></span> implements this overload
157        for <code class="computeroutput"><span class="identifier">list</span></code> and a modified version
158        of it for <code class="computeroutput"><span class="identifier">slist</span></code> (as <code class="computeroutput"><span class="identifier">slist</span><span class="special">::</span><span class="identifier">size</span><span class="special">()</span></code>
159        is also <code class="computeroutput"><span class="identifier">O</span><span class="special">(</span><span class="number">1</span><span class="special">)</span></code>).
160      </p>
161</div>
162</div>
163<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
164<td align="left"></td>
165<td align="right"><div class="copyright-footer">Copyright © 2009-2018 Ion Gaztanaga<p>
166        Distributed under the Boost Software License, Version 1.0. (See accompanying
167        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>)
168      </p>
169</div></td>
170</tr></table>
171<hr>
172<div class="spirit-nav">
173<a accesskey="p" href="non_standard_containers.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../container.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="configurable_containers.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
174</div>
175</body>
176</html>
177