1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/** \file dynamic_set_union.h */ // -*-c++-*-
// Copyright (C) 2010 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#ifndef DYNAMIC_SET_UNION_H
#define DYNAMIC_SET_UNION_H
#include "dynamic_set.h"
#include <boost/enable_shared_from_this.hpp>
#include <boost/make_shared.hpp>
#include <boost/unordered_map.hpp>
#include <sigc++/signal.h>
namespace aptitude
{
namespace util
{
/** \brief A read-only dynamic set representing the union of one
* or more dynamic sets.
*/
template<typename T>
class dynamic_set_union
: public dynamic_set<T>,
public boost::enable_shared_from_this<dynamic_set_union<T> >
{
typedef boost::unordered_map<T, int> value_counts_t;
// Counts how many times each element occurs. By assumption,
// individual sets can contain an element only once, so this
// effectively counts the number of individual sets containing
// the element.
value_counts_t value_counts;
// Keeps sets contained in this union alive, and also stores the
// connections binding each set to this object's handlers.
typedef boost::unordered_multimap<boost::shared_ptr<dynamic_set<T> >,
sigc::connection>
contained_sets_t;
// Maintains pointers to the individual sets this contains.
contained_sets_t contained_sets;
sigc::signal<void, T> signal_inserted;
sigc::signal<void, T> signal_removed;
void handle_inserted(const T &t);
void handle_removed(const T &t);
class set_enumerator;
public:
dynamic_set_union();
static boost::shared_ptr<dynamic_set_union> create();
void insert_set(const boost::shared_ptr<dynamic_set<T> > &set);
void remove_set(const boost::shared_ptr<dynamic_set<T> > &set);
std::size_t size();
boost::shared_ptr<enumerator<T> > enumerate();
sigc::connection connect_inserted(const sigc::slot<void, T> &slot);
sigc::connection connect_removed(const sigc::slot<void, T> &slot);
};
template<typename T>
dynamic_set_union<T>::dynamic_set_union()
{
}
template<typename T>
boost::shared_ptr<dynamic_set_union<T> >
dynamic_set_union<T>::create()
{
return boost::make_shared<dynamic_set_union<T> >();
}
template<typename T>
void dynamic_set_union<T>::insert_set(const boost::shared_ptr<dynamic_set<T> > &set)
{
if(contained_sets.find(set) == contained_sets.end())
{
for(boost::shared_ptr<enumerator<T> > e = set->enumerate();
e->advance(); )
handle_inserted(e->get_current());
sigc::connection inserted_connection =
set->connect_inserted(sigc::mem_fun(*this, &dynamic_set_union::handle_inserted));
sigc::connection removed_connection =
set->connect_removed(sigc::mem_fun(*this, &dynamic_set_union::handle_removed));
contained_sets.insert(std::make_pair(set, inserted_connection));
contained_sets.insert(std::make_pair(set, removed_connection));
}
}
template<typename T>
void dynamic_set_union<T>::remove_set(const boost::shared_ptr<dynamic_set<T> > &set)
{
// Paranoia: defensively take an extra reference in case "set"
// is somehow being kept alive by our reference (which should be
// impossible).
const boost::shared_ptr<dynamic_set<T> > set_copy = set;
typedef typename contained_sets_t::iterator contained_iterator;
const std::pair<contained_iterator, contained_iterator> found
= contained_sets.equal_range(set);
if(found.first != found.second)
{
for(boost::shared_ptr<enumerator<T> > e = set->enumerate();
e->advance(); )
handle_removed(e->get_current());
for(contained_iterator it = found.first; it != found.second; ++it)
it->second.disconnect();
contained_sets.erase(found.first, found.second);
}
}
template<typename T>
std::size_t dynamic_set_union<T>::size()
{
return value_counts.size();
}
template<typename T>
boost::shared_ptr<enumerator<T> > dynamic_set_union<T>::enumerate()
{
return boost::make_shared<set_enumerator>(this->shared_from_this(),
value_counts.begin(),
value_counts.end());
}
template<typename T>
sigc::connection
dynamic_set_union<T>::connect_inserted(const sigc::slot<void, T> &slot)
{
return signal_inserted.connect(slot);
}
template<typename T>
sigc::connection
dynamic_set_union<T>::connect_removed(const sigc::slot<void, T> &slot)
{
return signal_removed.connect(slot);
}
template<typename T>
void dynamic_set_union<T>::handle_inserted(const T &t)
{
typename value_counts_t::iterator found =
value_counts.find(t);
if(found != value_counts.end())
++found->second;
else
{
value_counts.insert(std::make_pair(t, 1));
signal_inserted(t);
}
}
template<typename T>
void dynamic_set_union<T>::handle_removed(const T &t)
{
typename value_counts_t::iterator found =
value_counts.find(t);
if(found != value_counts.end())
{
--found->second;
if(found->second == 0)
{
// Copy the value to send it to the signal.
const T value = found->first;
value_counts.erase(found);
signal_removed(value);
}
}
}
template<typename T>
class dynamic_set_union<T>::set_enumerator
: public enumerator<T>
{
public:
typedef
typename dynamic_set_union<T>::value_counts_t::const_iterator
values_iterator;
private:
boost::shared_ptr<dynamic_set<T> > parent;
values_iterator begin, end;
bool is_first;
public:
set_enumerator(const boost::shared_ptr<dynamic_set<T> > &_parent,
const values_iterator &_begin,
const values_iterator &_end);
T get_current();
bool advance();
};
template<typename T>
dynamic_set_union<T>::set_enumerator::set_enumerator(const boost::shared_ptr<dynamic_set<T> > &_parent,
const values_iterator &_begin,
const values_iterator &_end)
: parent(_parent),
begin(_begin),
end(_end),
is_first(true)
{
}
template<typename T>
T dynamic_set_union<T>::set_enumerator::get_current()
{
return begin->first;
}
template<typename T>
bool dynamic_set_union<T>::set_enumerator::advance()
{
if(is_first)
is_first = false;
else
++begin;
return begin != end;
}
}
}
#endif // DYNAMIC_SET_UNION_H
|