Collection.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Language" content="en-us">
  5. <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
  6. <title>Collection</title>
  7. </head>
  8. <body bgcolor="#FFFFFF" link="#0000EE" text="#000000" vlink="#551A8B" alink=
  9. "#FF0000">
  10. <h1><img src="../../boost.png" alt="boost logo" width="277" align="middle"
  11. height="86"><br>
  12. Collection</h1>
  13. <h3>Description</h3>
  14. <p>A Collection is a <i>concept</i> similar to the STL <a href=
  15. "http://www.sgi.com/tech/stl/Container.html">Container</a> concept. A
  16. Collection provides iterators for accessing a range of elements and
  17. provides information about the number of elements in the Collection.
  18. However, a Collection has fewer requirements than a Container. The
  19. motivation for the Collection concept is that there are many useful
  20. Container-like types that do not meet the full requirements of Container,
  21. and many algorithms that can be written with this reduced set of
  22. requirements. To summarize the reduction in requirements:</p>
  23. <ul>
  24. <li>It is not required to "own" its elements: the lifetime of an element
  25. in a Collection does not have to match the lifetime of the Collection
  26. object, though the lifetime of the element should cover the lifetime of
  27. the Collection object.</li>
  28. <li>The semantics of copying a Collection object is not defined (it could
  29. be a deep or shallow copy or not even support copying).</li>
  30. <li>The associated reference type of a Collection does not have to be a
  31. real C++ reference.</li>
  32. </ul>Because of the reduced requirements, some care must be taken when
  33. writing code that is meant to be generic for all Collection types. In
  34. particular, a Collection object should be passed by-reference since
  35. assumptions can not be made about the behaviour of the copy constructor.
  36. <h3>Associated types</h3>
  37. <table border summary="">
  38. <tr>
  39. <td valign="top">Value type</td>
  40. <td valign="top"><tt>X::value_type</tt></td>
  41. <td valign="top">The type of the object stored in a Collection. If the
  42. Collection is <i>mutable</i> then the value type must be <a href=
  43. "http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>. Otherwise
  44. the value type must be <a href=
  45. "./CopyConstructible.html">CopyConstructible</a>.</td>
  46. </tr>
  47. <tr>
  48. <td valign="top">Iterator type</td>
  49. <td valign="top"><tt>X::iterator</tt></td>
  50. <td valign="top">The type of iterator used to iterate through a
  51. Collection's elements. The iterator's value type is expected to be the
  52. Collection's value type. A conversion from the iterator type to the
  53. const iterator type must exist. The iterator type must be an <a href=
  54. "http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.</td>
  55. </tr>
  56. <tr>
  57. <td valign="top">Const iterator type</td>
  58. <td valign="top"><tt>X::const_iterator</tt></td>
  59. <td valign="top">A type of iterator that may be used to examine, but
  60. not to modify, a Collection's elements.</td>
  61. </tr>
  62. <tr>
  63. <td valign="top">Reference type</td>
  64. <td valign="top"><tt>X::reference</tt></td>
  65. <td valign="top">A type that behaves like a reference to the
  66. Collection's value type. <a href="#n1">[1]</a></td>
  67. </tr>
  68. <tr>
  69. <td valign="top">Const reference type</td>
  70. <td valign="top"><tt>X::const_reference</tt></td>
  71. <td valign="top">A type that behaves like a const reference to the
  72. Collection's value type.</td>
  73. </tr>
  74. <tr>
  75. <td valign="top">Pointer type</td>
  76. <td valign="top"><tt>X::pointer</tt></td>
  77. <td valign="top">A type that behaves as a pointer to the Collection's
  78. value type.</td>
  79. </tr>
  80. <tr>
  81. <td valign="top">Distance type</td>
  82. <td valign="top"><tt>X::difference_type</tt></td>
  83. <td valign="top">A signed integral type used to represent the distance
  84. between two of the Collection's iterators. This type must be the same
  85. as the iterator's distance type.</td>
  86. </tr>
  87. <tr>
  88. <td valign="top">Size type</td>
  89. <td valign="top"><tt>X::size_type</tt></td>
  90. <td valign="top">An unsigned integral type that can represent any
  91. nonnegative value of the Collection's distance type.</td>
  92. </tr>
  93. </table>
  94. <h3>Notation</h3>
  95. <table summary="">
  96. <tr>
  97. <td valign="top"><tt>X</tt></td>
  98. <td valign="top">A type that is a model of Collection.</td>
  99. </tr>
  100. <tr>
  101. <td valign="top"><tt>a</tt>, <tt>b</tt></td>
  102. <td valign="top">Object of type <tt>X</tt>.</td>
  103. </tr>
  104. <tr>
  105. <td valign="top"><tt>T</tt></td>
  106. <td valign="top">The value type of <tt>X</tt>.</td>
  107. </tr>
  108. </table>
  109. <h3>Valid expressions</h3>
  110. <p>The following expressions must be valid.</p>
  111. <table border summary="">
  112. <tr>
  113. <th>Name</th>
  114. <th>Expression</th>
  115. <th>Return type</th>
  116. </tr>
  117. <tr>
  118. <td valign="top">Beginning of range</td>
  119. <td valign="top"><tt>a.begin()</tt></td>
  120. <td valign="top"><tt>iterator</tt> if <tt>a</tt> is mutable,
  121. <tt>const_iterator</tt> otherwise</td>
  122. </tr>
  123. <tr>
  124. <td valign="top">End of range</td>
  125. <td valign="top"><tt>a.end()</tt></td>
  126. <td valign="top"><tt>iterator</tt> if <tt>a</tt> is mutable,
  127. <tt>const_iterator</tt> otherwise</td>
  128. </tr>
  129. <tr>
  130. <td valign="top">Size</td>
  131. <td valign="top"><tt>a.size()</tt></td>
  132. <td valign="top"><tt>size_type</tt></td>
  133. </tr><!--
  134. <TR>
  135. <TD VAlign=top>
  136. Maximum size
  137. </TD>
  138. <TD VAlign=top>
  139. <tt>a.max_size()</tt>
  140. </TD>
  141. <TD VAlign=top>
  142. <tt>size_type</tt>
  143. </TD>
  144. </TR>
  145. -->
  146. <tr>
  147. <td valign="top">Empty Collection</td>
  148. <td valign="top"><tt>a.empty()</tt></td>
  149. <td valign="top">Convertible to <tt>bool</tt></td>
  150. </tr>
  151. <tr>
  152. <td valign="top">Swap</td>
  153. <td valign="top"><tt>a.swap(b)</tt></td>
  154. <td valign="top"><tt>void</tt></td>
  155. </tr>
  156. </table>
  157. <h3>Expression semantics</h3>
  158. <table border summary="">
  159. <tr>
  160. <th>Name</th>
  161. <th>Expression</th>
  162. <th>Semantics</th>
  163. <th>Postcondition</th>
  164. </tr>
  165. <tr>
  166. <td valign="top">Beginning of range</td>
  167. <td valign="top"><tt>a.begin()</tt></td>
  168. <td valign="top">Returns an iterator pointing to the first element in
  169. the Collection.</td>
  170. <td valign="top"><tt>a.begin()</tt> is either dereferenceable or
  171. past-the-end. It is past-the-end if and only if <tt>a.size() ==
  172. 0</tt>.</td>
  173. </tr>
  174. <tr>
  175. <td valign="top">End of range</td>
  176. <td valign="top"><tt>a.end()</tt></td>
  177. <td valign="top">Returns an iterator pointing one past the last element
  178. in the Collection.</td>
  179. <td valign="top"><tt>a.end()</tt> is past-the-end.</td>
  180. </tr>
  181. <tr>
  182. <td valign="top">Size</td>
  183. <td valign="top"><tt>a.size()</tt></td>
  184. <td valign="top">Returns the size of the Collection, that is, its
  185. number of elements.</td>
  186. <td valign="top"><tt>a.size() &gt;= 0</tt></td>
  187. </tr><!--
  188. <TR>
  189. <TD VAlign=top>
  190. Maximum size
  191. </TD>
  192. <TD VAlign=top>
  193. <tt>a.max_size()</tt>
  194. </TD>
  195. <TD VAlign=top>
  196. &nbsp;
  197. </TD>
  198. <TD VAlign=top>
  199. Returns the largest size that this Collection can ever have. <A href="#8">[8]</A>
  200. </TD>
  201. <TD VAlign=top>
  202. <tt>a.max_size() &gt;= 0 &amp;&amp; a.max_size() &gt;= a.size()</tt>
  203. </TD>
  204. </TR>
  205. -->
  206. <tr>
  207. <td valign="top">Empty Collection</td>
  208. <td valign="top"><tt>a.empty()</tt></td>
  209. <td valign="top">Equivalent to <tt>a.size() == 0</tt>. (But possibly
  210. faster.)</td>
  211. <td valign="top">&nbsp;</td>
  212. </tr>
  213. <tr>
  214. <td valign="top">Swap</td>
  215. <td valign="top"><tt>a.swap(b)</tt></td>
  216. <td valign="top">Equivalent to <tt>swap(a,b)</tt></td>
  217. <td valign="top">&nbsp;</td>
  218. </tr>
  219. </table>
  220. <h3>Complexity guarantees</h3>
  221. <p><tt>begin()</tt> and <tt>end()</tt> are amortized constant time.</p>
  222. <p><tt>size()</tt> is at most linear in the Collection's size.
  223. <tt>empty()</tt> is amortized constant time.</p>
  224. <p><tt>swap()</tt> is at most linear in the size of the two
  225. collections.</p>
  226. <h3>Invariants</h3>
  227. <table border summary="">
  228. <tr>
  229. <td valign="top">Valid range</td>
  230. <td valign="top">For any Collection <tt>a</tt>, <tt>[a.begin(),
  231. a.end())</tt> is a valid range.</td>
  232. </tr>
  233. <tr>
  234. <td valign="top">Range size</td>
  235. <td valign="top"><tt>a.size()</tt> is equal to the distance from
  236. <tt>a.begin()</tt> to <tt>a.end()</tt>.</td>
  237. </tr>
  238. <tr>
  239. <td valign="top">Completeness</td>
  240. <td valign="top">An algorithm that iterates through the range
  241. <tt>[a.begin(), a.end())</tt> will pass through every element of
  242. <tt>a</tt>.</td>
  243. </tr>
  244. </table>
  245. <h3>Models</h3>
  246. <ul>
  247. <li><tt>array</tt></li>
  248. <li><tt>array_ptr</tt></li>
  249. <li><tt>vector&lt;bool&gt;</tt></li>
  250. </ul>
  251. <h3>Collection Refinements</h3>
  252. <p>There are quite a few concepts that refine the Collection concept,
  253. similar to the concepts that refine the Container concept. Here is a brief
  254. overview of the refining concepts.</p>
  255. <h4>ForwardCollection</h4>
  256. <p>The elements are arranged in some order that does not change
  257. spontaneously from one iteration to the next. As a result, a
  258. ForwardCollection is <a href=
  259. "http://www.sgi.com/tech/stl/EqualityComparable.html">EqualityComparable</a>
  260. and <a href=
  261. "http://www.sgi.com/tech/stl/LessThanComparable.html">LessThanComparable</a>.
  262. In addition, the iterator type of a ForwardCollection is a
  263. MultiPassInputIterator which is just an InputIterator with the added
  264. requirements that the iterator can be used to make multiple passes through
  265. a range, and that if <tt>it1 == it2</tt> and <tt>it1</tt> is
  266. dereferenceable then <tt>++it1 == ++it2</tt>. The ForwardCollection also
  267. has a <tt>front()</tt> method.</p>
  268. <table border summary="">
  269. <tr>
  270. <th>Name</th>
  271. <th>Expression</th>
  272. <th>Return type</th>
  273. <th>Semantics</th>
  274. </tr>
  275. <tr>
  276. <td valign="top">Front</td>
  277. <td valign="top"><tt>a.front()</tt></td>
  278. <td valign="top"><tt>reference</tt> if <tt>a</tt> is mutable,<br>
  279. <tt>const_reference</tt> otherwise.</td>
  280. <td valign="top">Equivalent to <tt>*(a.begin())</tt>.</td>
  281. </tr>
  282. </table>
  283. <h4>ReversibleCollection</h4>
  284. <p>The container provides access to iterators that traverse in both
  285. directions (forward and reverse). The iterator type must meet all of the
  286. requirements of <a href=
  287. "http://www.sgi.com/tech/stl/BidirectionalIterator.html">BidirectionalIterator</a>
  288. except that the reference type does not have to be a real C++ reference.
  289. The ReversibleCollection adds the following requirements to those of
  290. ForwardCollection.</p>
  291. <table border summary="">
  292. <tr>
  293. <th>Name</th>
  294. <th>Expression</th>
  295. <th>Return type</th>
  296. <th>Semantics</th>
  297. </tr>
  298. <tr>
  299. <td valign="top">Beginning of range</td>
  300. <td valign="top"><tt>a.rbegin()</tt></td>
  301. <td valign="top"><tt>reverse_iterator</tt> if <tt>a</tt> is mutable,
  302. <tt>const_reverse_iterator</tt> otherwise.</td>
  303. <td valign="top">Equivalent to
  304. <tt>X::reverse_iterator(a.end())</tt>.</td>
  305. </tr>
  306. <tr>
  307. <td valign="top">End of range</td>
  308. <td valign="top"><tt>a.rend()</tt></td>
  309. <td valign="top"><tt>reverse_iterator</tt> if <tt>a</tt> is mutable,
  310. <tt>const_reverse_iterator</tt> otherwise.</td>
  311. <td valign="top">Equivalent to
  312. <tt>X::reverse_iterator(a.begin())</tt>.</td>
  313. </tr>
  314. <tr>
  315. <td valign="top">Back</td>
  316. <td valign="top"><tt>a.back()</tt></td>
  317. <td valign="top"><tt>reference</tt> if <tt>a</tt> is mutable,<br>
  318. <tt>const_reference</tt> otherwise.</td>
  319. <td valign="top">Equivalent to <tt>*(--a.end())</tt>.</td>
  320. </tr>
  321. </table>
  322. <h4>SequentialCollection</h4>
  323. <p>The elements are arranged in a strict linear order. No extra methods are
  324. required.</p>
  325. <h4>RandomAccessCollection</h4>
  326. <p>The iterators of a RandomAccessCollection satisfy all of the
  327. requirements of <a href=
  328. "http://www.sgi.com/tech/stl/RandomAccessIterator.html">RandomAccessIterator</a>
  329. except that the reference type does not have to be a real C++ reference. In
  330. addition, a RandomAccessCollection provides an element access operator.</p>
  331. <table border summary="">
  332. <tr>
  333. <th>Name</th>
  334. <th>Expression</th>
  335. <th>Return type</th>
  336. <th>Semantics</th>
  337. </tr>
  338. <tr>
  339. <td valign="top">Element Access</td>
  340. <td valign="top"><tt>a[n]</tt></td>
  341. <td valign="top"><tt>reference</tt> if <tt>a</tt> is mutable,
  342. <tt>const_reference</tt> otherwise.</td>
  343. <td valign="top">Returns the nth element of the Collection. <tt>n</tt>
  344. must be convertible to <tt>size_type</tt>. Precondition: <tt>0 &lt;= n
  345. &lt; a.size()</tt>.</td>
  346. </tr>
  347. </table>
  348. <h3>Notes</h3>
  349. <p><a name="n1" id="n1">[1]</a> The reference type does not have to be a
  350. real C++ reference. The requirements of the reference type depend on the
  351. context within which the Collection is being used. Specifically it depends
  352. on the requirements the context places on the value type of the Collection.
  353. The reference type of the Collection must meet the same requirements as the
  354. value type. In addition, the reference objects must be equivalent to the
  355. value type objects in the collection (which is trivially true if they are
  356. the same object). Also, in a mutable Collection, an assignment to the
  357. reference object must result in an assignment to the object in the
  358. Collection (again, which is trivially true if they are the same object, but
  359. non-trivial if the reference type is a proxy class).</p>
  360. <h3>See also</h3>
  361. <p><a href=
  362. "http://www.sgi.com/tech/stl/Container.html">Container</a><br></p>
  363. <hr>
  364. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  365. "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  366. height="31" width="88"></a></p>
  367. <p>Revised
  368. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->05
  369. December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38516" --></p>
  370. <table summary="">
  371. <tr valign="top">
  372. <td nowrap><i>Copyright &copy; 2000</i></td>
  373. <td><i><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy
  374. Siek</a>, Univ.of Notre Dame and C++ Library &amp; Compiler Group/SGI
  375. (<a href="mailto:jsiek@engr.sgi.com">jsiek@engr.sgi.com</a>)</i></td>
  376. </tr>
  377. </table>
  378. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  379. accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  380. copy at <a href=
  381. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  382. </body>
  383. </html>