Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libstdc++-v3 / testsuite / ext / pb_ds / example / basic_map.cc
1 // -*- C++ -*-
2
3 // Copyright (C) 2005-2013 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_map_example.cpp
34  * A basic example showing how to use maps.
35  */
36
37 /**
38  * This example shows how to use "maps". 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
47 using namespace std;
48 using namespace __gnu_pbds;
49
50 // The following function performs a sequence of operations on an
51 // associative container object mapping integers to characters.
52 template<class Cntnr>
53 void
54 some_op_sequence(Cntnr& r_c)
55 {
56   assert(r_c.empty());
57   assert(r_c.size() == 0);
58
59   r_c.insert(make_pair(1, 'a'));
60
61   r_c[2] = 'b';
62
63   assert(!r_c.empty());
64   assert(r_c.size() == 2);
65
66   cout << "Key 1 is mapped to " << r_c[1] << endl;
67   cout << "Key 2 is mapped to " << r_c[2] << endl;
68
69   cout << endl << "All value types in the container:" << endl;
70
71   typedef typename Cntnr::const_iterator const_iterator;
72   for (const_iterator it = r_c.begin(); it != r_c.end(); ++it)
73     cout << it->first << " -> " << it->second << endl;
74   cout << endl;
75
76   r_c.clear();
77
78   assert(r_c.empty());
79   assert(r_c.size() == 0);
80 }
81
82 int main()
83 {
84   {
85     // Perform operations on a collision-chaining hash map.
86     cc_hash_table<int, char> c;
87     some_op_sequence(c);
88   }
89
90   {
91     // Perform operations on a general-probing hash map.
92     gp_hash_table<int, char> c;
93     some_op_sequence(c);
94   }
95
96   {
97     // Perform operations on a red-black tree map.
98     tree<int, char> c;
99     some_op_sequence(c);
100   }
101
102   {
103     // Perform operations on a splay tree map.
104     tree<int, char, less<int>, splay_tree_tag> c;
105     some_op_sequence(c);
106   }
107
108   {
109     // Perform operations on an ov tree map.
110     tree<int, char, less<int>, ov_tree_tag> c;
111     some_op_sequence(c);
112   }
113
114   {
115     // Perform operations on a list-update map.
116     list_update<int, char> c;
117     some_op_sequence(c);
118   }
119 }