1// -*- C++ -*-
2
3// Copyright (C) 2005 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
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING.  If not, write to the Free
18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19// USA.
20
21// As a special exception, you may use this file as part of a free software
22// library without restriction.  Specifically, if other files instantiate
23// templates or use macros or inline functions from this file, or you compile
24// this file and link it with other files to produce an executable, this
25// file does not by itself cause the resulting executable to be covered by
26// the GNU General Public License.  This exception does not however
27// invalidate any other reasons why the executable file might be covered by
28// the GNU General Public License.
29
30// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
31
32// Permission to use, copy, modify, sell, and distribute this software
33// is hereby granted without fee, provided that the above copyright
34// notice appears in all copies, and that both that copyright notice and
35// this permission notice appear in supporting documentation. None of
36// the above authors, nor IBM Haifa Research Laboratories, make any
37// representation about the suitability of this software for any
38// purpose. It is provided "as is" without express or implied warranty.
39
40/**
41 * @file ds_traits_example.cpp
42 * A basic example showing how to use ds_traits for querying container types
43 *	for their behavior.
44 */
45
46// For various associative containers.
47#include <ext/pb_assoc/assoc_cntnr.hpp>
48// For ds_traits.
49#include <ext/pb_assoc/ds_trait.hpp>
50// For cout, endl.
51#include <iostream>
52
53template<class DS_Category>
54void
55print_ds_category(DS_Category);
56
57template<>
58void
59print_ds_category(pb_assoc::cc_hash_ds_tag)
60{
61  std::cout << "Collision-chaining hash based associative-container:" <<
62    std::endl;
63}
64
65template<>
66void
67print_ds_category(pb_assoc::gp_hash_ds_tag)
68{
69  std::cout << "Probing hash based associative-container:" <<
70    std::endl;
71}
72
73template<>
74void
75print_ds_category(pb_assoc::rb_tree_ds_tag)
76{
77  std::cout << "Red-black tree associative-container:" <<
78    std::endl;
79}
80
81template<>
82void
83print_ds_category(pb_assoc::splay_tree_ds_tag)
84{
85  std::cout << "Splay tree associative-container:" <<
86    std::endl;
87}
88
89template<>
90void
91print_ds_category(pb_assoc::ov_tree_ds_tag)
92{
93  std::cout << "Ordered-vector tree associative-container:" <<
94    std::endl;
95}
96
97template<>
98void
99print_ds_category(pb_assoc::lu_ds_tag)
100{
101  std::cout << "List-based associative-container:" <<
102    std::endl;
103}
104
105void
106print_erase_can_throw(bool can)
107{
108  if (can)
109    {
110      std::cout << "Erase can throw" << std::endl;
111
112      return;
113    }
114
115  std::cout << "Erase cannot throw" << std::endl;
116}
117
118void
119print_order_preserving(bool does)
120{
121  if (does)
122    {
123      std::cout << "Preserves order" << std::endl;
124
125      return;
126    }
127
128  std::cout << "Does not preserve order" << std::endl;
129}
130
131void
132print_erase_iterators(bool can)
133{
134  if (can)
135    {
136      std::cout << "Can erase iterators" << std::endl;
137
138      return;
139    }
140
141  std::cout << "Cannot erase iterators" << std::endl;
142}
143
144template<class Invalidation_Guarantee>
145void
146print_invalidation_guarantee(Invalidation_Guarantee);
147
148template<>
149void
150print_invalidation_guarantee(pb_assoc::basic_invalidation_guarantee)
151{
152  std::cout << "Guarantees only that found references, pointers, and "
153    "iterators are valid as long as the container object is not "
154    "modified" << std::endl;
155}
156
157template<>
158void
159print_invalidation_guarantee(pb_assoc::find_invalidation_guarantee)
160{
161  std::cout << "Guarantees that found references, pointers, and "
162    "find_iterators are valid even if the container object "
163    "is modified" << std::endl;
164}
165
166template<>
167void
168print_invalidation_guarantee(pb_assoc::range_invalidation_guarantee)
169{
170  std::cout << "Guarantees that iterators remain valid even if the "
171    "container object is modified" << std::endl;
172}
173
174void
175print_reverse_iteration(bool does)
176{
177  if (does)
178    {
179      std::cout << "Supports reverse iteration" << std::endl;
180
181      return;
182    }
183
184  std::cout << "Does not support reverse iteration" << std::endl;
185}
186
187void
188print_split_join(bool does)
189{
190  if (does)
191    {
192      std::cout << "Supports split and join" << std::endl;
193
194      return;
195    }
196
197  std::cout << "Does not support split and join" << std::endl;
198}
199
200template<class Cntnr>
201void
202print_container_attributes()
203{
204  // First print out the data-structure category.
205
206  print_ds_category(typename Cntnr::ds_category());
207
208  // Next is the data-structure traits class of the container.
209
210  typedef pb_assoc::ds_traits< Cntnr> traits;
211
212  // Now print the attributes of the container.
213
214  print_erase_can_throw(traits::erase_can_throw);
215
216  print_order_preserving(traits::order_preserving);
217
218  print_erase_iterators(traits::erase_iterators);
219
220  print_invalidation_guarantee(typename traits::invalidation_guarantee());
221
222  print_reverse_iteration(traits::reverse_iteration);
223
224  print_split_join(traits::split_join);
225
226  std::cout << std::endl << std::endl;
227}
228
229int
230main()
231{
232  print_container_attributes<pb_assoc::cc_hash_assoc_cntnr<int, char> >();
233
234  print_container_attributes<pb_assoc::gp_hash_assoc_cntnr<int, char> >();
235
236  print_container_attributes<pb_assoc::tree_assoc_cntnr<int, char> >();
237
238  print_container_attributes<pb_assoc::tree_assoc_cntnr<
239    int,
240    char,
241    std::less<int>,
242    pb_assoc::splay_tree_ds_tag> >();
243
244  print_container_attributes<pb_assoc::tree_assoc_cntnr<
245    int,
246    char,
247    std::less<int>,
248    pb_assoc::ov_tree_ds_tag> >();
249
250  print_container_attributes<pb_assoc::lu_assoc_cntnr<int, char> >();
251
252  typedef
253    pb_assoc::lu_assoc_cntnr<
254    int,
255    pb_assoc::compound_data_type<
256    pb_assoc::gp_hash_assoc_cntnr<
257    char,
258    pb_assoc::null_data_type> > >
259    mmap_t;
260
261  print_container_attributes<mmap_t>();
262}
263