PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/libstdc++-v3/doc/html/ext/pb_ds/tutorial.html

https://bitbucket.org/pizzafactory/pf-gcc
HTML | 670 lines | 560 code | 110 blank | 0 comment | 0 complexity | 8b5a72e41e8f149dacc261fff859590a MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta name="generator" content=
  6. "HTML Tidy for Linux/x86 (vers 12 April 2005), see www.w3.org" />
  7. <title>Tutorial</title>
  8. <meta http-equiv="Content-Type" content=
  9. "text/html; charset=us-ascii" />
  10. </head>
  11. <body>
  12. <div id="page">
  13. <h1>Short Tutorial</h1>
  14. <p>Following is a short tutorial illustrating the main points
  15. of <tt>pb_ds</tt>. <a href="concepts.html">Concepts</a>
  16. describes and summarizes some concepts.</p>
  17. <h2><a name="assoc_main" id="assoc_main">Associative
  18. Containers</a></h2>
  19. <h3><a name="assoc_basic" id="assoc_basic">Basic Use</a></h3>
  20. <p>For the most part, <tt>pb_ds</tt>'s containers have the same
  21. interface as the STL's, except for the names used for the
  22. container classes themselves. For example, this shows basic
  23. operations on a collision-chaining hash-based container:</p>
  24. <pre>
  25. <a href=
  26. "cc_hash_table.html">cc_hash_table</a>&lt;<b>int</b>, <b>char</b>&gt; c;
  27. c[2] = 'b';
  28. assert(c.find(1) == c.end());
  29. </pre>
  30. <p>The container is called <a href=
  31. "cc_hash_table.html"><tt>cc_hash_table</tt></a> as
  32. opposed to <tt>unordered_map</tt>, since "unordered map" does
  33. not necessarily mean a hash-based map (as the STL implicitly
  34. implies). For example, list-based associative containers, which
  35. are very useful for the construction of "multimaps" (see
  36. <a href=
  37. "assoc_performance_tests.html#msc">Associative-Container
  38. Performance Tests::Observations::Mapping-Semantics
  39. Considerations</a>), are also unordered. It is also not called
  40. <tt>hash_map</tt> since there are more ways than one to
  41. implement hash tables.</p>
  42. <p>This snippet shows a red-black tree based container:</p>
  43. <pre>
  44. <a href=
  45. "tree.html">tree</a>&lt;<b>int</b>, <b>char</b>&gt; c;
  46. c[2] = 'b';
  47. assert(c.find(2) != c.end());
  48. </pre>
  49. <p>The container is called <a href=
  50. "tree.html"><tt>tree</tt></a>
  51. as opposed to <tt>map</tt>, since "map" doesn't say that
  52. much.</p>
  53. <p>Most of the STL's familiar methods are unchanged.
  54. <i>E.g.</i>, <tt>being</tt>, <tt>end</tt>, <tt>size</tt>,
  55. <tt>empty</tt>, and <tt>clear</tt>, do just the same as is
  56. customary. <a href=
  57. "assoc_examples.html#basic_usage">Associative-Container
  58. Examples::Basic use</a>, and especially <a href=
  59. "http://gcc.gnu.org/viewcvs/*checkout*/trunk/libstdc%2B%2B-v3/testsuite/ext/pb_ds/example/basic_map.cc"><tt>basic_map.cc</tt></a>,
  60. show examples of this.</p>
  61. <p>This isn't to say that things are exactly as one would expect,
  62. given the container requirments and interfaces in the C++
  63. standard.</p>
  64. <p>The names of containers' policies and policy accessors are
  65. different than those of the STL. For example, if <tt>C</tt> is
  66. some type of hash-based container, then</p>
  67. <pre>
  68. C::hash_fn
  69. </pre>gives the type of its hash functor, and if <tt>c</tt> is some
  70. hash-based container object, then
  71. <pre>
  72. c.get_hash_fn()
  73. </pre>
  74. <p>will return a reference to its hash-functor object.</p>
  75. <p>Similarly, if <tt>C</tt> is some type of tree-based
  76. container, then</p>
  77. <pre>
  78. C::cmp_fn
  79. </pre>gives the type of its comparison functor, and if <tt>c</tt>
  80. is some tree-based container object, then
  81. <pre>
  82. c.get_cmp_fn()
  83. </pre>
  84. <p>will return a reference to its comparison-functor
  85. object.</p>
  86. <p>It would be nice to give names consistent with those in the
  87. existing C++ standard (inclusive of TR1). Unfortunately, these
  88. standard containers don't consistently name types and
  89. methods. For example, <tt>std::tr1::unordered_map</tt> uses
  90. <tt>hasher</tt> for the hash functor, but <tt>std::map</tt> uses
  91. <tt>key_compare</tt> for the comparison functor. Also, we could
  92. not find an accessor for <tt>std::tr1::unordered_map</tt>'s hash
  93. functor, but <tt>std::map</tt> uses <tt>compare</tt> for accessing
  94. the comparison functor.</p>
  95. <p>Instead, <tt>pb_ds</tt> attempts to be internally consistent, and
  96. uses standard-derived terminology if possible.
  97. </p>
  98. <p>Another source of difference is in scope: <tt>pb_ds</tt>
  99. contains more types of associative containers than the STL, and
  100. more opportunities to configure these new containers, since
  101. different types of associative containers are useful in different
  102. settings (see <a href=
  103. "assoc_performance_tests.html#dss_family_choice">Associative-Container
  104. Performance Tests::Observations::Underlying Data-Structure
  105. Families</a>).</p>
  106. <p><tt>pb_ds</tt> contains different classes for hash-based containers,
  107. tree-based containers, trie-based containers, and list-based
  108. containers. <a href=
  109. "interface.html#containers_assoc">Inteface::Containers::Associative
  110. Containers</a> lists the containers. <a href=
  111. "hash_based_containers.html">Design::Associative
  112. Containers::Hash-Based Containers</a>, <a href=
  113. "tree_based_containers.html">Design::Associative
  114. Containers::Tree-Based Containers</a>, <a href=
  115. "trie_based_containers.html">Design::Associative
  116. Containers::Trie-Based Containers</a>, and <a href=
  117. "lu_based_containers.html">Design::Associative
  118. Containers::List-Based Containers</a>, explain some more about
  119. these types of containers, respectively.</p>
  120. <p>Since associative containers share parts of their interface,
  121. they are organized as a class hierarchy; it is shown in Figure
  122. <a href="#cd">Class hierarchy</a>.</p>
  123. <h6 class="c1"><a name="cd" id="cd"><img src="container_cd.png" alt=
  124. "no image" /></a></h6>
  125. <h6 class="c1">Class hierarchy.</h6>
  126. <p>Each type or method is defined in the most-common ancestor
  127. in which it makes sense:
  128. <a href=
  129. "http://gcc.gnu.org/viewcvs/*checkout*/trunk/libstdc%2B%2B-v3/testsuite/ext/pb_ds/example/basic_map.cc"><tt>basic_map.cc</tt></a>
  130. shows an example of most of the associative-container
  131. types.</p>
  132. <p>For example, all associative containers support iteration.
  133. Consequently, <a href=
  134. "container_base.html"><tt>container_base</tt></a> has the
  135. interface:</p>
  136. <pre>
  137. <b>template</b>&lt;...&gt;
  138. <b>class</b> <a href="container_base.html">container_base</a>
  139. {
  140. ...
  141. <b>public</b>:
  142. ...
  143. const_iterator
  144. begin() <b>const</b>;
  145. iterator
  146. begin();
  147. const_iterator
  148. end() <b>const</b>;
  149. iterator
  150. end();
  151. ...
  152. };
  153. </pre>
  154. <p>and so all associative containers inherent this method.
  155. Conversely, both collision-chaining and (general) probing
  156. hash-based associative containers have a hash functor, so
  157. <a href=
  158. "basic_hash_table.html"><tt>basic_hash_table</tt></a>
  159. has the interface:</p>
  160. <pre>
  161. <b>template</b>&lt;...&gt;
  162. <b>class</b> <a href="basic_hash_table.html">basic_hash_table</a> : <b>public</b> <a href="container_base.html">container_base</a>
  163. {
  164. ...
  165. <b>public</b>:
  166. ...
  167. const hash_fn&amp;
  168. get_hash_fn() const;
  169. hash_fn&amp;
  170. get_hash_fn();
  171. ...
  172. };
  173. </pre>
  174. <p>and so all hash-based associative containers inherit the
  175. same hash-functor accessor methods.</p>
  176. <p>This is discussed further in <a href=
  177. "ds_gen.html">Design::Associative Containers::Data-Structure
  178. Genericity</a>.</p>
  179. <h3><a name="assoc_policies" id="assoc_policies">Configuring
  180. Associative Containers</a></h3>
  181. <p>In general, each of <tt>pb_ds</tt>'s containers is
  182. parametrized by more policies than those of the STL's. For
  183. example, the STL's hash-based container is parametrized as
  184. follows:</p>
  185. <pre>
  186. <b>template</b>&lt;
  187. <b>typename</b> Key,
  188. <b>typename</b> Mapped,
  189. <b>typename</b> Hash,
  190. <b>typename</b> Pred,
  191. <b>typename</b> Allocator,
  192. <b>bool</b> Cache_Hashe_Code&gt;
  193. <b>class</b> unordered_map;
  194. </pre>
  195. <p>and so can be configured by key type, mapped type, a functor
  196. that translates keys to unsigned integral types, an equivalence
  197. predicate, an allocator, and an indicator whether to store hash
  198. values with each entry. <tt>pb_ds</tt>'s collision-chaining
  199. hash-based container is parametrized as</p>
  200. <pre>
  201. <b>template</b>&lt;
  202. <b>typename</b> Key,
  203. <b>typename</b> Mapped,
  204. <b>typename</b> Hash_Fn,
  205. <b>typename</b> Eq_Fn,
  206. <b>typename</b> Comb_Hash_Fn,
  207. <b>typename</b> Resize_Policy
  208. <b>bool</b> Store_Hash
  209. <b>typename</b> Allocator&gt;
  210. <b>class</b> <a href=
  211. "cc_hash_table.html">cc_hash_table</a>;
  212. </pre>
  213. <p>and so can be configured by the first four types of
  214. <tt>std::tr1::unordered_map</tt>, then a policy for translating
  215. the key-hash result into a position within the table, then a
  216. policy by which the table resizes, an indicator whether to
  217. store hash values with each entry, and an allocator (which is
  218. typically the last template parameter in STL containers).</p>
  219. <p>Nearly all policy parameters have default values, so this
  220. need not be considered for casual use. It is important to note,
  221. however, that hash-based containers' policies can dramatically
  222. alter their performance in different settings, and that
  223. tree-based containers' policies can make them useful for other
  224. purposes than just look-up.</p>
  225. <p><a href="hash_based_containers.html">Design::Associative
  226. Containers::Hash-Based Containers</a>, <a href=
  227. "tree_based_containers.html">Design::Associative
  228. Containers::Tree-Based Containers</a>, <a href=
  229. "trie_based_containers.html">Design::Associative
  230. Containers::Trie-Based Containers</a>, and <a href=
  231. "lu_based_containers.html">Design::Associative
  232. Containers::List-Based Containers</a>, explain some more about
  233. configuring hash based, tree based, trie based, and list base
  234. containers, respectively. <a href=
  235. "interface.html#ds_policy_classes">Interface::Container Policy
  236. Classes</a> shows the different policy classes for configuring
  237. associative containers. <a href=
  238. "assoc_examples.html#hash_based">Examples::Hash-Based
  239. Containers</a>, <a href=
  240. "assoc_examples.html#tree_like_based">Examples::Tree-Like-Based
  241. Containers</a>, and <a href=
  242. "assoc_examples.html#trie_based">Examples::Trie-Based
  243. Containers</a> show examples for this.</p>
  244. <h3><a name="assoc_ds_gen" id="assoc_ds_gen">Determining
  245. Containers' Attributes</a></h3>
  246. <p>Associative-containers' underlying data structures obviously
  247. affect their performance; Unfortunately, they can also affect
  248. their interface. When manipulating generically associative
  249. containers, it is often useful to be able to statically
  250. determine what they can support and what the cannot. (This was
  251. discussed in <a href=
  252. "motivation.html#assoc_ds_genericity">Motivation::Associative
  253. Containers::Data-Structure Genericity</a>.)</p>
  254. <p>Happily, the STL provides a good solution to a similar
  255. problem - that of the different behavior of iterators. If
  256. <tt>It</tt> is an iterator, then</p>
  257. <pre>
  258. <b>typename</b> std::iterator_traits&lt;It&gt;::iterator_category
  259. </pre>
  260. <p>is one of a small number of pre-defined
  261. <tt><b>struct</b></tt>s, and,</p>
  262. <pre>
  263. <b>typename</b> std::iterator_traits&lt;It&gt;::value_type
  264. </pre>
  265. <p>is the value type to which the iterator "points".</p>
  266. <p>Similarly, in <tt>pb_ds</tt>, if <tt>C</tt> is an
  267. associative container, then</p>
  268. <pre>
  269. <b>typename</b> <a href=
  270. "assoc_container_traits.html"><tt>container_traits</tt></a>&lt;C&gt;::container_category
  271. </pre>is one of a small number of pre-defined
  272. <tt><b>struct</b></tt>s, each one corresponding to a class in
  273. Figure <a href="#cd">Class hierarchy</a>. These tags are listed in
  274. <a href="interface.html#ds_ts_assoc">Interface::Associative
  275. Containers::Data-Structure Tags and Traits::Data-Structure
  276. Tags::Associative-Containers</a>; <a href="ds_gen.html#container_traits">
  277. Design::Associative Containers::Data-Structure Tags and
  278. Traits</a> explains this further; <a href=
  279. "ds_gen.html#tag_cd">Design::Associative
  280. Containers::Data-Structure Tags and Traits::Data-structure tag
  281. class hierarchy</a> shows a class diagram.
  282. <p>In most cases, however, the exact underlying data structure
  283. is not really important, but only one of its attributes:
  284. whether it guarantees storing elements by key order, for
  285. example. For this one can use</p>
  286. <pre>
  287. <b>typename</b> <a href=
  288. "assoc_container_traits.html"><tt>container_traits</tt></a>&lt;C&gt;::order_preserving
  289. </pre>
  290. <p>This is described further in <a href=
  291. "ds_gen.html">Design::Data-Structure Genericity</a>; <a href=
  292. "http://gcc.gnu.org/viewcvs/*checkout*/trunk/libstdc%2B%2B-v3/testsuite/ext/pb_ds/example/assoc_container_traits.cc"><tt>assoc_container_traits.cc</tt></a>
  293. shows an example of querying containers' attributes.</p>
  294. <h3><a name="assoc_find_range" id="assoc_find_range">Point-Type
  295. and Range-Type Methods and Iterators</a></h3>(This subsection
  296. addresses points from <a href=
  297. "motivation.html#assoc_diff_it">Motivation::Associative
  298. Containers::Differentiating between Iterator Types</a>.)
  299. <p><tt>pb_ds</tt> differentiates between two types of methods
  300. and iterators: point-type, and range-type. For example,
  301. <tt>find</tt> and <tt>insert</tt> are point-type methods, since
  302. they each deal with a specific element; their returned
  303. iterators are point-type iterators. <tt>begin</tt> and
  304. <tt>end</tt> are range-type methods, since they are not used to
  305. find a specific element, but rather to go over all elements in
  306. a container object; their returned iterators are range-type
  307. iterators.</p>
  308. <p>Most containers store elements in an order that is
  309. determined by their interface. Correspondingly, it is fine that
  310. their point-type iterators are synonymous with their range-type
  311. iterators. For example, in the following snippet</p>
  312. <pre>
  313. std::for_each(c.find(1), c.find(5), foo);
  314. </pre>two point-type iterators (returned by <tt>find</tt>) are used
  315. for a range-type purpose - going over all elements whose key is
  316. between 1 and 5.
  317. <p>Conversely, the above snippet makes no sense for
  318. self-organizing containers - ones that order (and reorder)
  319. their elements by implementation. It would be nice to have a
  320. uniform iterator system that would allow the above snippet to
  321. compile only if it made sense.</p>
  322. <p>This could trivially be done by specializing
  323. <tt>std::for_each</tt> for the case of iterators returned by
  324. <tt>std::tr1::unordered_map</tt>, but this would only solve the
  325. problem for one algorithm and one container. Fundamentally, the
  326. problem is that one can loop using a self-organizing
  327. container's point-type iterators.</p>
  328. <p><tt>pb_ds</tt>'s containers define two families of
  329. iterators: <tt>const_point_iterator</tt> and
  330. <tt>point_iterator</tt> are the iterator types returned by
  331. point-type methods; <tt>const_iterator</tt> and
  332. <tt>iterator</tt> are the iterator types returned by range-type
  333. methods.</p>
  334. <pre>
  335. <b>class</b> <i>&lt;- some container -&gt;</i>
  336. {
  337. <b>public</b>:
  338. ...
  339. <b>typedef</b> <i>&lt;- something -&gt;</i> const_iterator;
  340. <b>typedef</b> <i>&lt;- something -&gt;</i> iterator;
  341. <b>typedef</b> <i>&lt;- something -&gt;</i> const_point_iterator;
  342. <b>typedef</b> <i>&lt;- something -&gt;</i> point_iterator;
  343. ...
  344. <b>public</b>:
  345. ...
  346. const_iterator begin () <b>const</b>;
  347. iterator begin();
  348. const_point_iterator find(...) <b>const</b>;
  349. point_iterator find(...);
  350. };
  351. </pre>
  352. <p><a href="ds_gen.html#find_range">Design::Associative
  353. Containers::Data-Structure Genericity::Point-Type and
  354. Range-Type Methods and Iterators</a> discusses the relationship
  355. between point-type and range-type iterators in general; for
  356. containers whose interface defines sequence order, however, it
  357. is very simple: point-type and range-type iterators are exactly
  358. the same, which means that the above snippet will compile if it
  359. is used for an order-preserving associative container.</p>
  360. <p>For self-organizing containers, however, (hash-based
  361. containers as a special example), the preceding snippet will
  362. not compile, because their point-type iterators do not support
  363. <tt><b>operator</b>++</tt>.</p>
  364. <p>In any case, both for order-preserving and self-organizing
  365. containers, the following snippet will compile:</p>
  366. <pre>
  367. <b>typename</b> Cntnr::point_iterator it = c.find(2);
  368. </pre>
  369. <p>because a range-type iterator can always be converted to a
  370. point-type iterator.</p>
  371. <p><a href="ds_gen.html#find_range">Design::Associative
  372. Containers::Data-Structure Genericity::Point-Type and
  373. Range-Type Methods and Iterators</a> discusses this
  374. further.</p>
  375. <p><a href=
  376. "motivation.html#assoc_diff_it">Motivation::Associative
  377. Containers::Differentiating between Iterator Types</a> also
  378. raised the point that a container's iterators might have
  379. different invalidation rules concerning their de-referencing
  380. abilities and movement abilities. This now corresponds exactly
  381. to the question of whether point-type and range-type iterators
  382. are valid. As explained in <a href="#assoc_ds_gen">Determining
  383. Containers' Attributes</a>, <a href=
  384. "assoc_container_traits.html"><tt>container_traits</tt></a> allows
  385. querying a container for its data structure attributes. The
  386. iterator-invalidation guarantees are certainly a property of
  387. the underlying data structure, and so</p>
  388. <pre>
  389. <a href=
  390. "assoc_container_traits.html">container_traits</a>&lt;C&gt;::invalidation_guarantee
  391. </pre>
  392. <p>gives one of three pre-determined types that answer this
  393. query. This is explained further in <a href=
  394. "ds_gen.html#find_range">Design::Associative
  395. Containers::Data-Structure Genericity::Point-Type and
  396. Range-Type Methods and Iterators</a>.</p>
  397. <h3><a name="assoc_ms" id="assoc_ms">Distinguishing between Maps and Sets</a></h3>
  398. <p>Anyone familiar with the STL knows that there are four kinds
  399. of associative containers: maps, sets, multimaps, and
  400. multisets. <a href="#assoc_basic">Basic Use</a> discussed how
  401. to use maps, <i>i.e.</i> containers that associate each key to
  402. some data.</p>
  403. <p>Sets are associative containers that simply store keys -
  404. they do not map them to anything. In the STL, each map class
  405. has a corresponding set class. <i>E.g.</i>,
  406. <tt>std::map&lt;<b>int</b>, <b>char</b>&gt;</tt> maps each
  407. <tt><b>int</b></tt> to a <tt><b>char</b></tt>, but
  408. <tt>std::set&lt;<b>int</b>, <b>char</b>&gt;</tt> simply stores
  409. <tt><b>int</b></tt>s. In <tt>pb_ds</tt>, however, there are no
  410. distinct classes for maps and sets. Instead, an associative
  411. container's <tt>Mapped</tt> template parameter is a policy: if
  412. it is instantiated by <a href=
  413. "null_mapped_type.html"><tt>null_mapped_type</tt></a>, then it
  414. is a "set"; otherwise, it is a "map". <i>E.g.</i>,</p>
  415. <pre>
  416. <a href="cc_hash_table.html">cc_hash_table</a>&lt;<b>int</b>, <b>char</b>&gt;
  417. </pre>is a "map" mapping each <tt><b>int</b></tt> value to a <tt>
  418. <b>char</b></tt>, but
  419. <pre>
  420. <a href="cc_hash_table.html">cc_hash_table</a>&lt;<b>int</b>, <a href="null_mapped_type.html">null_mapped_type</a>&gt;
  421. </pre>is a type that uniquely stores <tt><b>int</b></tt> values.
  422. <p>Once the <tt>Mapped</tt> template parameter is instantiated
  423. by <a href="null_mapped_type.html">null_mapped_type</a>, then
  424. the "set" acts very similarly to the STL's sets - it does not
  425. map each key to a distinct <a href=
  426. "null_mapped_type.html">null_mapped_type</a> object. Also,
  427. , the container's <tt>value_type</tt> is essentially
  428. its <tt>key_type</tt> - just as with the STL's sets. For a simple example, see <a href=
  429. "http://gcc.gnu.org/viewcvs/*checkout*/trunk/libstdc%2B%2B-v3/testsuite/ext/pb_ds/example/basic_set.cc"><tt>basic_set.cc</tt></a>
  430. .</p>
  431. <p>The STL's multimaps and multisets allow, respectively,
  432. non-uniquely mapping keys and non-uniquely storing keys. As
  433. discussed in <a href=
  434. "motivation.html#assoc_mapping_semantics">Motivation::Associative
  435. Containers::Alternative to Multiple Equivalent Keys</a>, the
  436. reasons why this might be necessary are 1) that a key might be
  437. decomposed into a primary key and a secondary key, 2) that a
  438. key might appear more than once, or 3) any arbitrary
  439. combination of 1)s and 2)s. Correspondingly,
  440. one should use 1) "maps" mapping primary keys to secondary
  441. keys, 2) "maps" mapping keys to size types, or 3) any arbitrary
  442. combination of 1)s and 2)s. Thus, for example, an
  443. <tt>std::multiset&lt;<b>int</b>&gt;</tt> might be used to store
  444. multiple instances of integers, but using <tt>pb_ds</tt>'s
  445. containers, one might use</p>
  446. <pre>
  447. <a href=
  448. "tree.html">tree</a>&lt;<b>int</b>, size_t&gt;
  449. </pre><i>i.e.</i>, a "map" of <tt><b>int</b></tt>s to
  450. <tt>size_t</tt>s.
  451. <p><a href="assoc_examples.html#mmaps">Associative-Container
  452. Examples::"Multimaps" and "Multisets"</a> shows some simple
  453. examples.</p>
  454. <p>These "multimaps" and "multisets" might be confusing to
  455. anyone familiar with the STL's <tt>std::multimap</tt> and
  456. <tt>std::multiset</tt>, because there is no clear
  457. correspondence between the two. For example, in some cases
  458. where one uses <tt>std::multiset</tt> in the STL, one might use
  459. in <tt>pb_ds</tt> a "multimap" of "multisets" - <i>i.e.</i>, a
  460. container that maps primary keys each to an associative
  461. container that maps each secondary key to the number of times
  462. it occurs.</p>
  463. <p>When one uses a "multimap," one should choose with care the
  464. type of container used for secondary keys. This is further
  465. explained in <a href=
  466. "assoc_performance_tests.html#msc">Associative-Container
  467. Performance Tests::Observations::Mapping-Semantics
  468. Considerations</a>.</p>
  469. <hr>
  470. <h2><a name="pq" id="pq">Priority Queues</a></h2>
  471. <h3><a name="pq_basic" id="pq_basic">Basic Use</a></h3>
  472. <p><tt>pb_ds</tt>'s priority_queue container is
  473. similar to the STL's in interface. For example:</p>
  474. <pre>
  475. <a href=
  476. "priority_queue.html">priority_queue</a>&lt;<b>int</b>&gt; p;
  477. p.push(2);
  478. p.push(4);
  479. p.push(1);
  480. assert(p.top() == 4);
  481. p.pop();
  482. assert(p.top() == 2);
  483. assert(p.size() == 2);
  484. assert(!p.empty());
  485. </pre>
  486. <h3><a name="pq_policies" id="pq_policies">Configuring Priority
  487. Queues</a></h3>
  488. <p>As opposed to associative containers, priority queues have
  489. relatively few configuration options. The priority queue is
  490. parametrized as follows:</p>
  491. <pre>
  492. <b>template</b>&lt;
  493. <b>typename</b> Value_Type,
  494. <b>typename</b> Cmp_Fn,
  495. <b>typename</b> Tag,
  496. <b>typename</b> Allocator&gt;
  497. <b>class</b> <a href="priority_queue.html">priority_queue</a>;
  498. </pre>
  499. <p>The <tt>Value_Type</tt>, <tt>Cmp_Fn</tt>, and
  500. <tt>Allocator</tt> parameters are the container's value type,
  501. comparison-functor type, and allocator type, respectively;
  502. these are very similar to the STL's priority queue. The
  503. <tt>Tag</tt> parameter is different: there are a number of
  504. pre-defined tag types corresponding to binary heaps, binomial
  505. heaps, <i>etc.</i>, and <tt>Tag</tt> should be instantiated
  506. by one of them. <a href=
  507. "interface.html#ds_ts_pq">Interface::Data-Structure Tags and
  508. Traits::Data Structure Tags::Priority-Queues</a> lists the
  509. possible types, <a href="pq_design.html">Priority-Queue
  510. Design</a> explains this further, and <a href=
  511. "http://gcc.gnu.org/viewcvs/*checkout*/trunk/libstdc%2B%2B-v3/testsuite/ext/pb_ds/example/basic_priority_queue.cc"><tt>basic_priority_queue.cc</tt></a>
  512. shows an example.</p>
  513. <p>Note that as opposed to the STL's priority queue, <a href=
  514. "priority_queue.html"><tt>priority_queue</tt></a> is not a
  515. sequence-adapter; it is a regular container.</p>
  516. <h3><a name="pq_ds_more_ops" id="pq_ds_more_ops">Supporting
  517. More Operations</a></h3>
  518. <p><a href="priority_queue.html"><tt>priority_queue</tt></a>'s
  519. <tt>push</tt> method returns a point-type iterator, which can
  520. be used for modifying or erasing arbitrary values. For
  521. example:</p>
  522. <pre>
  523. <a href=
  524. "priority_queue.html">priority_queue</a>&lt;<b>int</b>&gt; p;
  525. <a href=
  526. "priority_queue.html">priority_queue</a>&lt;<b>int</b>&gt;::point_iterator it = p.push(3);
  527. p.modify(it, 4);
  528. </pre>
  529. <p>These types of operations are necessary for making priority
  530. queues useful for different applications, especially graph
  531. applications. <a href="pq_examples.html#xref">Priority-Queue
  532. Examples::Cross-Referencing</a> gives some examples.</p>
  533. <h3><a name="pq_ds_gen" id="pq_ds_gen">Determining Container
  534. Attributes</a></h3>
  535. <p>Similarly to <a href=
  536. "assoc_container_traits.html"><tt>container_traits</tt></a> (described
  537. in <a href="#assoc_ds_gen">Associative Containers::Determining
  538. Containers' Attributes</a>), <a href=
  539. "pq_container_traits.html"><tt>container_traits</tt></a> can be used to
  540. statically determine priority-queues' attributes:</p>
  541. <pre>
  542. <a href=
  543. "pq_container_traits.html">container_traits</a>&lt;C&gt;::container_category
  544. </pre>is one of a small number of predefined tag structures that
  545. identifies the underlying data structure, and
  546. <pre>
  547. <a href=
  548. "pq_container_traits.html">container_traits</a>&lt;C&gt;::invalidation_guarantee
  549. </pre>
  550. <p>is its invalidation guarantee. Invalidation guarantees are
  551. especially important regarding priority queues, since in
  552. <tt>pb_ds</tt>'s design, iterators are practically the only way
  553. to manipulate them.</p>
  554. <p><a href="pq_design.html#pq_traits">Design::Priority
  555. Queues::Traits</a> discusses this further. <a href=
  556. "pq_examples.html#generics">Priority-Queue
  557. Examples::Generics</a> shows an example.</p>
  558. </div>
  559. </body>
  560. </html>