Merged with mainline at revision 128810.
[official-gcc.git] / libstdc++-v3 / testsuite / ext / pb_ds / example / basic_multiset.cc
blob064e24d3610ade999bb214fd29c3d2c15d6ef314
1 // -*- C++ -*-
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.
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.
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.
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.
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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.
42 /**
43 * @file basic_multiset_example.cpp
44 * A basic example showing how to use multisets.
48 // This example shows how to use "multisets".
50 // In this example we build a very simple priority queue that also can
51 // be queried if an entry contains (i.e., it is slightly similar to an
52 // associative container as well as a priority queue). The priority
53 // queue adapts a "multiset".
55 // (Note that there are more efficient ways for implementing this than
56 // by adapting an associative container. This is just an example for
57 // "multisets".)
59 #include <iostream>
60 #include <cassert>
61 #include <ext/pb_ds/assoc_container.hpp>
63 using namespace std;
64 using namespace __gnu_pbds;
66 // A simple priority queue that also supports an "contains" query.
67 class contains_pq
69 public:
70 // Pushes an integer.
71 void
72 push(int i);
74 // Pops the largest integer and returns it.
75 int
76 pop();
78 // Returns true iff i is contained in the container.
79 bool
80 contains(int i) const
81 { return m_tree.find(i) != m_tree.end(); }
83 // Returns true iff empty.
84 bool
85 empty() const
86 { return m_tree.empty(); }
88 private:
89 // This is the container type we adapt - a "multiset".
90 // It maps each integer to the number of times it logically appears.
91 typedef
92 tree<
93 int,
94 size_t,
95 greater<
96 int> >
97 tree_t;
99 private:
100 tree_t m_tree;
103 void
104 contains_pq::
105 push(int i)
107 // To push i, we insert to the "multiset" that i appears 0 times
108 // (which is a no-op if i already is contained), then increment the
109 // number of times i is contained by 1.
110 ++m_tree.insert(make_pair(i, 0)).first->second;
114 contains_pq::
115 pop()
117 assert(!empty());
119 // The element we need to pop must be the first one, since tree_t is
120 // an ordered container.
121 tree_t::iterator it = m_tree.begin();
123 const int i = it->first;
125 // Decrease the number of times the popped element appears in the
126 // container object. If it is 0 - we erase it.
127 if (--it->second == 0)
128 m_tree.erase(it);
130 return i;
133 int main()
135 contains_pq cpq;
137 // First we push some elements.
138 cpq.push(4);
139 cpq.push(3);
140 cpq.push(2);
141 cpq.push(1);
142 cpq.push(4);
144 // Note that logically, 4 appears 2 times, and each of 1, 2, and 3
145 // appear once.
146 assert(cpq.contains(4));
147 assert(cpq.contains(3));
148 assert(cpq.contains(2));
149 assert(cpq.contains(1));
151 // Now pop the topmost element - it should be 4.
152 assert(cpq.pop() == 4);
154 // Now logically, each of 1, 2, 3, and 4 appear once.
155 assert(cpq.contains(4));
157 // We pop the topmost element - it should be 4.
158 assert(cpq.pop() == 4);
160 // 4 should not be contained any more.
161 assert(!cpq.contains(4));
163 assert(cpq.contains(3));
164 assert(cpq.contains(2));
165 assert(cpq.contains(1));
167 assert(cpq.pop() == 3);
168 assert(cpq.pop() == 2);
169 assert(cpq.pop() == 1);
171 assert(cpq.empty());
173 return 0;