]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
3 | // Copyright (C) 2005, 2006 Free Software Foundation, Inc. | |
4 | // | |
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 | |
9 | // version. | |
10 | ||
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. | |
15 | ||
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. | |
20 | ||
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 | |
29 | // Public License. | |
30 | ||
31 | // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. | |
32 | ||
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 | |
40 | // warranty. | |
41 | ||
42 | /** | |
43 | * @file native_priority_queue.hpp | |
44 | * Contains an adapter to Dinkumware/SGI tree tables | |
45 | */ | |
46 | ||
47 | #ifndef PB_DS_NATIVE_PRIORITY_QUEUE_HPP | |
48 | #define PB_DS_NATIVE_PRIORITY_QUEUE_HPP | |
49 | ||
4569a895 AT |
50 | #include <string> |
51 | #include <vector> | |
52 | #include <queue> | |
53 | #include <deque> | |
382a1351 BK |
54 | #include <ext/pb_ds/detail/standard_policies.hpp> |
55 | #include <ext/pb_ds/detail/type_utils.hpp> | |
56 | #include <io/xml.hpp> | |
4569a895 AT |
57 | |
58 | namespace pb_ds | |
59 | { | |
4569a895 AT |
60 | namespace test |
61 | { | |
4569a895 AT |
62 | namespace detail |
63 | { | |
382a1351 | 64 | template<typename Value_Type, bool Vector, typename Allocator> |
4569a895 AT |
65 | struct base_seq |
66 | { | |
382a1351 BK |
67 | private: |
68 | typedef typename Allocator::template rebind<Value_Type> value_rebind; | |
69 | ||
70 | public: | |
71 | typedef std::vector<Value_Type, typename value_rebind::other> type; | |
4569a895 AT |
72 | }; |
73 | ||
382a1351 BK |
74 | template<typename Value_Type, typename Allocator> |
75 | struct base_seq<Value_Type, false, Allocator> | |
4569a895 | 76 | { |
382a1351 BK |
77 | private: |
78 | typedef typename Allocator::template rebind<Value_Type> value_rebind; | |
4569a895 | 79 | |
382a1351 BK |
80 | public: |
81 | typedef std::deque<Value_Type, typename value_rebind::other> type; | |
82 | }; | |
4569a895 AT |
83 | } // namespace detail |
84 | ||
382a1351 BK |
85 | struct native_pq_tag |
86 | { }; | |
87 | ||
88 | #define PB_DS_CLASS_C_DEC \ | |
89 | native_priority_queue<Value_Type, Vector, Cmp_Fn, Allocator> | |
4569a895 | 90 | |
382a1351 BK |
91 | #define PB_DS_BASE_C_DEC \ |
92 | std::priority_queue<Value_Type, typename detail::base_seq<Value_Type, Vector, Allocator>::type, Cmp_Fn> | |
4569a895 AT |
93 | |
94 | template<typename Value_Type, | |
95 | bool Vector, | |
382a1351 BK |
96 | typename Cmp_Fn = std::less<Value_Type>, |
97 | typename Allocator = std::allocator<char> > | |
4569a895 AT |
98 | class native_priority_queue : public PB_DS_BASE_C_DEC |
99 | { | |
100 | private: | |
101 | typedef PB_DS_BASE_C_DEC base_type; | |
382a1351 | 102 | typedef typename Allocator::template rebind<Value_Type> value_rebind; |
4569a895 AT |
103 | |
104 | public: | |
105 | typedef Value_Type value_type; | |
382a1351 | 106 | typedef typename value_rebind::other::const_reference const_reference; |
4569a895 | 107 | typedef native_pq_tag container_category; |
4569a895 AT |
108 | typedef Cmp_Fn cmp_fn; |
109 | ||
4569a895 AT |
110 | native_priority_queue() : base_type() |
111 | { } | |
112 | ||
113 | template<typename It> | |
114 | native_priority_queue(It f, It l) : base_type(f, l) | |
115 | { } | |
116 | ||
117 | static std::string | |
118 | name() | |
119 | { | |
120 | if (Vector) | |
121 | return ("n_pq_vector"); | |
4569a895 AT |
122 | return ("n_pq_deque"); |
123 | } | |
124 | ||
125 | static std::string | |
126 | desc() | |
127 | { | |
128 | if (Vector) | |
382a1351 BK |
129 | return make_xml_tag("type", "value", "std::priority_queue_vector"); |
130 | return make_xml_tag("type", "value", "std::priority_queue_deque"); | |
4569a895 AT |
131 | } |
132 | ||
133 | void | |
134 | clear() | |
382a1351 | 135 | { *static_cast<base_type*>(this) = base_type(); } |
4569a895 AT |
136 | |
137 | void | |
138 | erase(const_reference r_val) | |
139 | { | |
140 | base_type tmp; | |
4569a895 AT |
141 | Cmp_Fn cmp; |
142 | ||
143 | while (cmp(base_type::top(), r_val) || cmp(r_val, base_type::top())) | |
144 | { | |
145 | tmp.push(base_type::top()); | |
4569a895 AT |
146 | base_type::pop(); |
147 | } | |
148 | ||
149 | if (!base_type::empty()) | |
150 | { | |
151 | base_type::pop(); | |
4569a895 AT |
152 | while (!base_type::empty()) |
153 | { | |
154 | tmp.push(base_type::top()); | |
4569a895 AT |
155 | base_type::pop(); |
156 | } | |
157 | } | |
382a1351 | 158 | *static_cast<base_type* >(this) = tmp; |
4569a895 AT |
159 | } |
160 | ||
161 | template<typename Pred> | |
162 | size_t | |
163 | erase_if(Pred pred) | |
164 | { | |
165 | base_type tmp; | |
4569a895 | 166 | std::size_t ersd = 0; |
4569a895 AT |
167 | while (!base_type::empty()) |
168 | { | |
169 | if (!pred(base_type::top())) | |
170 | tmp.push(base_type::top()); | |
171 | else | |
172 | ++ersd; | |
4569a895 AT |
173 | base_type::pop(); |
174 | } | |
175 | ||
382a1351 | 176 | *static_cast<base_type*>(this) = tmp; |
4569a895 AT |
177 | return ersd; |
178 | } | |
179 | ||
180 | template<typename Pred> | |
181 | void | |
182 | split(Pred pred, PB_DS_CLASS_C_DEC& other) | |
183 | { | |
184 | base_type tmp; | |
4569a895 | 185 | other.clear(); |
4569a895 AT |
186 | while (!base_type::empty()) |
187 | { | |
188 | if (!pred(base_type::top())) | |
189 | tmp.push(base_type::top()); | |
190 | else | |
191 | other.push(base_type::top()); | |
4569a895 AT |
192 | base_type::pop(); |
193 | } | |
382a1351 | 194 | *static_cast<base_type*>(this) = tmp; |
4569a895 AT |
195 | } |
196 | ||
197 | void | |
198 | modify(const_reference r_old, const_reference r_new) | |
199 | { | |
200 | erase(r_old); | |
4569a895 AT |
201 | push(r_new); |
202 | } | |
203 | ||
204 | void | |
205 | join(PB_DS_CLASS_C_DEC& other) | |
206 | { | |
207 | std::vector<value_type> a_tmp; | |
4569a895 AT |
208 | while (!base_type::empty()) |
209 | { | |
210 | a_tmp.push_back(base_type::top()); | |
4569a895 AT |
211 | base_type::pop(); |
212 | } | |
213 | ||
214 | while (!other.empty()) | |
215 | { | |
216 | a_tmp.push_back(other.top()); | |
4569a895 AT |
217 | other.pop(); |
218 | } | |
219 | ||
382a1351 | 220 | *static_cast<base_type*>(this) = base_type(a_tmp.begin(), a_tmp.end()); |
4569a895 AT |
221 | } |
222 | ||
223 | Cmp_Fn | |
224 | get_cmp_fn() const | |
382a1351 | 225 | { return Cmp_Fn(); } |
4569a895 AT |
226 | }; |
227 | ||
228 | #undef PB_DS_BASE_C_DEC | |
4569a895 AT |
229 | #undef PB_DS_CLASS_C_DEC |
230 | ||
231 | } // namespace test | |
4569a895 AT |
232 | } // namespace pb_ds |
233 | ||
382a1351 | 234 | #endif |