]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/ext/pb_ds/example/priority_queue_xref.cc
3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
43 * @file priority_queue_xref_example.cpp
44 * A basic example showing how to cross-reference priority queues and other
45 * containers for erase.
49 * This example shows how to cross-reference priority queues
50 * and other containers. I.e., using an associative container to
51 * map keys to entries in a priority queue, and using the priority
52 * queue to map entries to the associative container. The combination
53 * can be used for fast operations involving both priorities and
56 * The most useful examples of this technique are usually from the
57 * field of graph algorithms (where erasing or modifying an arbitrary
58 * entry of a priority queue is sometimes necessary), but a full-blown
59 * example would be too long. Instead, this example shows a very simple
60 * version of Dijkstra's
65 #include <ext/pb_ds/priority_queue.hpp>
66 #include <ext/pb_ds/assoc_container.hpp>
69 using namespace __gnu_pbds
;
71 // A priority queue of integers, which supports fast pushes,
72 // duplicated-int avoidance, and arbitrary-int erases.
73 class mapped_priority_queue
77 // Pushes an int into the container. If the key is already in, this
80 push(const int& r_str
);
82 // Returns a const reference to the largest int in the container.
90 // Erases the largest int in the container.
94 // Erases an arbitrary int. If the int is not in the container, this
95 // is a no-op, and the return value is false.
97 erase(const int& r_str
);
101 { return m_pq
.empty(); }
105 { return m_pq
.size(); }
108 // A priority queue of strings.
109 typedef __gnu_pbds::priority_queue
< int> pq_t
;
111 // A hash-table mapping strings to point_iterators inside the
113 typedef cc_hash_table
< int, pq_t::point_iterator
> map_t
;
120 mapped_priority_queue::
121 push(const int& r_str
)
123 // First check if the int is already in the container. If so, just return.
124 if (m_map
.find(r_str
) != m_map
.end())
127 // Push the int into the priority queue, and store a point_iterator to it.
128 pq_t::point_iterator pq_it
= m_pq
.push(r_str
);
132 // Now make the map associate the int to the point_iterator.
133 m_map
[r_str
] = pq_it
;
137 // If the above failed, we need to remove the int from the
138 // priority queue as well.
145 mapped_priority_queue::
150 // Erase the int from the map.
151 m_map
.erase(m_pq
.top());
153 // ...then from the priority queue.
158 mapped_priority_queue::
159 erase(const int& r_str
)
161 map_t::point_iterator map_it
= m_map
.find(r_str
);
163 // If the int is not in the map, this is a no-op.
164 if (map_it
== m_map
.end())
167 // Otherwise, we erase it from the priority queue.
168 m_pq
.erase(map_it
->second
);
170 // ...then from the map.
178 // Push some values into the container object.
179 mapped_priority_queue m
;
183 // The following four operations are no-ops: 2 and 1 are already in
194 // The size should be 5, since m contains the set {1, 2, 10, 11, 12}.
195 assert(m
.size() == 5);
197 // The largest value should be 12.
198 assert(m
.top() == 12);
200 // Now erase some values.
202 // Erasing 1 actually erases a value.
205 // ...but erasing 1 again is a no-op.
208 // The size should be 5, since m contains the set {2, 10, 11, 12}.
209 assert(m
.size() == 4);
211 // Now print the values in the container.
214 cout
<< m
.top() << endl
;