1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2018 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 3, 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 COPYING3.  If not see
18 // <http://www.gnu.org/licenses/>.
19 
20 
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
22 
23 // Permission to use, copy, modify, sell, and distribute this software
24 // is hereby granted without fee, provided that the above copyright
25 // notice appears in all copies, and that both that copyright notice
26 // and this permission notice appear in supporting documentation. None
27 // of the above authors, nor IBM Haifa Research Laboratories, make any
28 // representation about the suitability of this software for any
29 // purpose. It is provided "as is" without express or implied
30 // warranty.
31 
32 /**
33  * @file basic_set_example.cpp
34  * A basic example showing how to use sets.
35  */
36 
37 /**
38  * This example shows how to use "sets". It defines a
39  * function performing a sequence of operations on
40  * a generic container. It then calls this function with some containers.
41  */
42 
43 #include <iostream>
44 #include <cassert>
45 #include <ext/pb_ds/assoc_container.hpp>
46 #include <ext/pb_ds/tag_and_trait.hpp>
47 
48 using namespace std;
49 using namespace __gnu_pbds;
50 
51 // The following function performs a sequence of operations on an
52 // associative container object storing integers.
53 template<class Cntnr>
54 void
some_op_sequence(Cntnr & r_c)55 some_op_sequence(Cntnr& r_c)
56 {
57   assert(r_c.empty());
58   assert(r_c.size() == 0);
59 
60   r_c.insert(1);
61   r_c.insert(2);
62 
63   assert(!r_c.empty());
64   assert(r_c.size() == 2);
65 
66   cout << "All value types in the container:" << endl;
67   for (typename Cntnr::const_iterator it = r_c.begin(); it != r_c.end();
68        ++it)
69     cout <<* it << " ";
70 
71   cout << endl;
72 
73   r_c.clear();
74 
75   assert(r_c.empty());
76   assert(r_c.size() == 0);
77 }
78 
main()79 int main()
80 {
81   {
82     // Perform operations on a collision-chaining hash set.
83     cc_hash_table<int, null_type> c;
84     some_op_sequence(c);
85   }
86 
87   {
88     // Perform operations on a general-probing hash set.
89     gp_hash_table<int, null_type> c;
90     some_op_sequence(c);
91   }
92 
93   {
94     // Perform operations on a red-black tree set.
95     tree<int, null_type> c;
96     some_op_sequence(c);
97   }
98 
99   {
100     // Perform operations on a splay tree set.
101     tree<
102       int,
103       null_type,
104       less<int>,
105       splay_tree_tag> c;
106     some_op_sequence(c);
107   }
108 
109   {
110     // Perform operations on a splay tree set.
111     tree<
112       int,
113       null_type,
114       less<int>,
115       ov_tree_tag> c;
116     some_op_sequence(c);
117   }
118 
119   {
120     // Perform operations on a list-update set.
121     list_update<int, null_type> c;
122     some_op_sequence(c);
123   }
124 
125   return 0;
126 }
127