]>
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 assoc_container_traits_example.cpp | |
44 | * A basic example showing how to use container_traits for querying container types | |
45 | * for their behavior. | |
46 | */ | |
47 | ||
48 | /** | |
49 | * The following example shows how to use container_traits in order to print | |
50 | * out information on an associative container's behavior, e.g., its underlying | |
51 | * data structure, or whether its objects guarantee storing entries sorted | |
52 | * by key order. | |
53 | */ | |
54 | ||
55 | #include <iostream> | |
56 | #include <ext/pb_ds/assoc_container.hpp> | |
57 | #include <ext/pb_ds/tag_and_trait.hpp> | |
58 | ||
59 | using namespace std; | |
60 | using namespace pb_ds; | |
61 | using namespace pb_ds; | |
62 | ||
63 | template<class DS_Category> | |
64 | void | |
65 | print_container_category(DS_Category); | |
66 | ||
67 | template<> | |
68 | void | |
69 | print_container_category(cc_hash_tag) | |
70 | { | |
71 | cout << "Collision-chaining hash based associative-container:" << endl; | |
72 | } | |
73 | ||
74 | template<> | |
75 | void | |
76 | print_container_category(gp_hash_tag) | |
77 | { | |
78 | cout << "Probing hash based associative-container:" << endl; | |
79 | } | |
80 | ||
81 | template<> | |
82 | void | |
83 | print_container_category(rb_tree_tag) | |
84 | { | |
85 | cout << "Red-black tree associative-container:" << endl; | |
86 | } | |
87 | ||
88 | template<> | |
89 | void | |
90 | print_container_category(splay_tree_tag) | |
91 | { | |
92 | cout << "Splay tree associative-container:" << endl; | |
93 | } | |
94 | ||
95 | template<> | |
96 | void | |
97 | print_container_category(ov_tree_tag) | |
98 | { | |
99 | cout << "Ordered-vector tree associative-container:" << endl; | |
100 | } | |
101 | ||
102 | template<> | |
103 | void | |
104 | print_container_category(list_update_tag) | |
105 | { | |
106 | cout << "List-based associative-container:" << endl; | |
107 | } | |
108 | ||
109 | void | |
110 | print_erase_can_throw(bool can) | |
111 | { | |
112 | if (can) | |
113 | { | |
114 | cout << "Erase can throw" << endl; | |
115 | return; | |
116 | } | |
117 | cout << "Erase cannot throw" << endl; | |
118 | } | |
119 | ||
120 | void | |
121 | print_order_preserving(bool does) | |
122 | { | |
123 | if (does) | |
124 | { | |
125 | cout << "Preserves order" << endl; | |
126 | return; | |
127 | } | |
128 | cout << "Does not preserve order" << endl; | |
129 | } | |
130 | ||
131 | template<class Invalidation_Guarantee> | |
132 | void | |
133 | print_invalidation_guarantee(Invalidation_Guarantee); | |
134 | ||
135 | template<> | |
136 | void | |
137 | print_invalidation_guarantee(basic_invalidation_guarantee) | |
138 | { | |
139 | cout << "Guarantees only that found references, pointers, and " | |
140 | "iterators are valid as long as the container object is not " | |
141 | "modified" << endl; | |
142 | } | |
143 | ||
144 | template<> | |
145 | void | |
146 | print_invalidation_guarantee(point_invalidation_guarantee) | |
147 | { | |
148 | cout << "Guarantees that found references, pointers, and " | |
149 | "point_iterators are valid even if the container object " | |
150 | "is modified" << endl; | |
151 | } | |
152 | ||
153 | template<> | |
154 | void | |
155 | print_invalidation_guarantee(range_invalidation_guarantee) | |
156 | { | |
157 | cout << "Guarantees that iterators remain valid even if the " | |
158 | "container object is modified" << endl; | |
159 | } | |
160 | ||
161 | void | |
162 | print_reverse_iteration(bool does) | |
163 | { | |
164 | if (does) | |
165 | { | |
166 | cout << "Supports reverse iteration" << endl; | |
167 | return; | |
168 | } | |
169 | cout << "Does not support reverse iteration" << endl; | |
170 | } | |
171 | ||
172 | template<class DS_Traits> | |
173 | void | |
174 | print_container_attributes() | |
175 | { | |
176 | // First print out the data structure category. | |
177 | print_container_category(typename DS_Traits::container_category()); | |
178 | ||
179 | // Now print the attributes of the container. | |
180 | print_erase_can_throw(DS_Traits::erase_can_throw); | |
181 | print_order_preserving(DS_Traits::order_preserving); | |
182 | print_invalidation_guarantee(typename DS_Traits::invalidation_guarantee()); | |
183 | print_reverse_iteration(DS_Traits::reverse_iteration); | |
184 | ||
185 | cout << endl << endl; | |
186 | } | |
187 | ||
188 | int | |
189 | main() | |
190 | { | |
191 | { | |
192 | // Print the attributes of a collision-chaining hash table. | |
193 | typedef cc_hash_table< int, char> t; | |
194 | print_container_attributes<container_traits<t> >(); | |
195 | } | |
196 | ||
197 | { | |
198 | // Print the attributes of a (general) probing hash table. | |
199 | typedef gp_hash_table< int, char> t; | |
200 | print_container_attributes<container_traits<t> >(); | |
201 | } | |
202 | ||
203 | { | |
204 | // Print the attributes of a red-black tree. | |
205 | typedef tree< int, char> t; | |
206 | print_container_attributes<container_traits<t> >(); | |
207 | } | |
208 | ||
209 | { | |
210 | // Print the attributes of a splay tree. | |
211 | typedef | |
212 | tree< | |
213 | int, | |
214 | char, | |
215 | less<int>, | |
216 | splay_tree_tag> | |
217 | t; | |
218 | ||
219 | print_container_attributes<container_traits<t> >(); | |
220 | } | |
221 | ||
222 | { | |
223 | // Print the attributes of an ordered-vector tree. | |
224 | typedef | |
225 | tree< | |
226 | int, | |
227 | char, | |
228 | less<int>, | |
229 | ov_tree_tag> | |
230 | t; | |
231 | print_container_attributes<container_traits<t> >(); | |
232 | } | |
233 | ||
234 | { | |
235 | // Print the attributes of an list-based container. | |
236 | typedef list_update< int, char> t; | |
237 | print_container_attributes<container_traits<t> >(); | |
238 | } | |
239 | ||
240 | return 0; | |
241 | } |