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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/** \file choice.h */ // -*-c++-*-
// Copyright (C) 2009 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 CHOICE_H
#define CHOICE_H
#include <cwidget/generic/util/eassert.h>
#include <iostream>
#include <generic/util/compare3.h>
#include <boost/functional/hash.hpp>
/** \brief Represents a decision made by the resolver.
*
* This is used to keep track of which choices imply that we end up
* at a higher tier than we otherwise would. Note that only some of
* the fields are part of the "value" of the choice; id tags along
* for informational purposes only, and if the choice is of type
* install_version and is not from the dependency source, then the
* associated dependency is also present for informational purposes
* only. Comparison operators, contains(), and other operations
* ignore fields that are present for informational purposes only.
*
* \sa promotion_set
*/
template<typename PackageUniverse>
class generic_choice
{
public:
typedef typename PackageUniverse::package package;
typedef typename PackageUniverse::version version;
typedef typename PackageUniverse::dep dep;
/** \brief Indicates the type of choice represented by a
* choice object.
*/
enum type
{
/** \brief A choice to install a single package version. */
install_version,
/** \brief A choice to break a single soft dependency. */
break_soft_dep
};
private:
// The version, if any, associated with this choice.
//
// Meaningful for install_version choices.
version ver;
// The dependency, if any, associated with this choice.
//
// Meaningful for install_version choices and for break_soft_dep
// choices. This is part of the "identity" of this object (so it
// will be used in equality comparisons). Normally this is ignored
// by "contains" checks, but if one of the choices is
// dependency-scoped, it can matter.
dep d;
// True if the choice was to remove the source of the dependency.
bool from_dep_source:1;
// True if the choice *has* an attached dependency at all.
bool has_dep:1;
// The type of this choice object.
type tp:1;
/** \brief The order in which this choice was made.
*/
int id:29;
generic_choice(const version &_ver, bool _from_dep_source, bool _has_dep, const dep &_d, int _id)
: ver(_ver), d(_d), from_dep_source(_from_dep_source), has_dep(_has_dep), tp(install_version), id(_id)
{
}
generic_choice(const dep &_d, int _id)
: d(_d), from_dep_source(false), has_dep(true), tp(break_soft_dep), id(_id)
{
}
public:
generic_choice()
: from_dep_source(false), has_dep(false), tp(install_version), id(-1)
{
}
/** \brief Create a new choice that installs the given version.
*
* \param ver The version that is installed by this choice.
* \param d The dependency that caused this choice. This is
* included for informational purposes only and is
* ignored by all operations except get_dep().
* \param id An arbitrary integer associated with this choice.
* Ignored by all operations except get_id().
*/
static generic_choice make_install_version(const version &ver, const dep &d, int id)
{
return generic_choice(ver, false, true, d, id);
}
/** \brief Create a new choice that installs the given version and
* has no attached dependency.
*
* \param ver The version that is installed by this choice.
* \param d The dependency that caused this choice. This is
* included for informational purposes only and is
* ignored by all operations except get_dep().
* \param id An arbitrary integer associated with this choice.
* Ignored by all operations except get_id().
*/
static generic_choice make_install_version(const version &ver, int id)
{
return generic_choice(ver, false, false, dep(), id);
}
/** \brief Create a new choice that installs the given version to
* change the source of the given dependency.
*
* \param ver The version that is installed by this choice.
* \param d The dependency whose source was modified by installing
* ver.
* \param id An arbitrary integer associated with this choice.
* Ignored by all operations except get_id().
*/
static generic_choice make_install_version_from_dep_source(const version &ver, const dep &d, int id)
{
return generic_choice(ver, true, true, d, id);
}
/** \brief Create a new choice that leaves the given soft dependency
* unresolved.
*
* \param id An arbitrary integer associated with this choice.
* Ignored by all operations except get_id().
*/
static generic_choice make_break_soft_dep(const dep &d, int id)
{
return generic_choice(d, id);
}
/** \brief Return a choice that contains any choice with the same
* effect as this choice.
*/
generic_choice generalize() const
{
switch(tp)
{
case install_version:
// Discard the from-dep-source-ness, if any, of this choice.
return make_install_version(ver, d, id);
case break_soft_dep:
return *this;
}
eassert(!"We should never get here.");
return generic_choice();
}
/** \brief Test whether this choice "contains" another choice.
*
* This is true if the two choices are equal, or if this choice is
* "more general" than the other choice. In particular: installing
* a version, not from a dependency source, is always more general
* than installing the same version from any dependency source.
*/
bool contains(const generic_choice &other) const
{
if(tp != other.tp)
return false;
else // tp == other.tp
switch(tp)
{
case install_version:
if(ver != other.ver)
return false;
else // ver == other.ver
{
if(!from_dep_source)
return true;
else if(!other.from_dep_source)
return false;
else
return d == other.d;
}
case break_soft_dep:
return d == other.d;
}
eassert(!"We should never get here.");
return false;
}
/** \brief Compare two choices.
*
* Choices are ordered arbitrarily.
*/
int compare(const generic_choice &other) const
{
using aptitude::util::compare3;
if(tp < other.tp)
return -1;
else if(tp > other.tp)
return 1;
else // tp == other.tp
{
switch(tp)
{
case install_version:
{
const int ver_cmp = compare3(ver, other.ver);
if(ver_cmp != 0)
return ver_cmp;
else
{
if(!from_dep_source && other.from_dep_source)
return -1;
else if(from_dep_source && !other.from_dep_source)
return 1;
else if(!has_dep && other.has_dep)
return -1;
else if(has_dep && !other.has_dep)
return 1;
else if(!has_dep)
return 0;
else
return compare3(d, other.d);
}
}
case break_soft_dep:
return compare3(d, other.d);
}
}
eassert(!"We should never get here.");
return false;
};
/** \brief Compare two choices.
*
* Choices are ordered arbitrarily.
*/
bool operator<(const generic_choice &other) const
{
return compare(other) < 0;
}
bool operator==(const generic_choice &other) const
{
if(tp != other.tp)
return false;
else
{
switch(tp)
{
case install_version:
if(ver != other.ver)
return false;
if(from_dep_source != other.from_dep_source)
return false;
if(has_dep != other.has_dep)
return false;
if(!has_dep)
return true;
else
return d == other.d;
break;
case break_soft_dep:
return d == other.d;
}
}
eassert(!"We should never get here.");
}
/** \brief Create a new choice that is identical to
* this choice, except that the dependency is set to
* the given value.
*/
generic_choice copy_and_set_dep(const dep &new_dep) const
{
generic_choice rval(*this);
rval.has_dep = true;
rval.d = new_dep;
return rval;
}
/** \brief Update the ID of this choice. */
void set_id(int new_id)
{
id = new_id;
}
int get_id() const { return id; }
type get_type() const { return tp; }
const version &get_ver() const
{
eassert(tp == install_version);
return ver;
}
bool get_from_dep_source() const
{
eassert(tp == install_version);
return from_dep_source;
}
bool get_has_dep() const
{
eassert(tp == install_version);
return has_dep;
}
const dep &get_dep() const
{
return d;
}
/** \brief Compute a hash value on choices. */
std::size_t get_hash_value() const
{
std::size_t rval = 0;
boost::hash_combine(rval, tp);
switch(tp)
{
case install_version:
boost::hash_combine(rval, ver);
boost::hash_combine(rval, from_dep_source);
boost::hash_combine(rval, has_dep);
if(has_dep)
boost::hash_combine(rval, d);
break;
case break_soft_dep:
boost::hash_combine(rval, d);
break;
}
return rval;
}
};
template<typename PackageUniverse>
std::size_t hash_value(const generic_choice<PackageUniverse> &c)
{
return c.get_hash_value();
}
// Overload compare3 on choices.
namespace aptitude
{
namespace util
{
template<typename PackageUniverse>
class compare3_f<generic_choice<PackageUniverse> >
{
public:
int operator()(const generic_choice<PackageUniverse> &c1,
const generic_choice<PackageUniverse> &c2) const
{
return c1.compare(c2);
}
};
}
}
/** \brief Compares choices by their effects on the solution.
*
* e.g., two choices that install the same version will always
* compare equal.
*/
template<typename PackageUniverse>
struct generic_compare_choices_by_effects
{
int operator()(const generic_choice<PackageUniverse> &c1, const generic_choice<PackageUniverse> &c2) const
{
typedef typename PackageUniverse::version version;
typedef typename PackageUniverse::dep dep;
using aptitude::util::compare3;
if(c1.get_type() < c2.get_type())
return -1;
else if(c2.get_type() < c1.get_type())
return 1;
else
switch(c1.get_type())
{
case generic_choice<PackageUniverse>::install_version:
return compare3(c1.get_ver(), c2.get_ver());
case generic_choice<PackageUniverse>::break_soft_dep:
return compare3(c1.get_dep(), c2.get_dep());
default:
return 0;
}
}
};
template<typename PackageUniverse>
inline std::ostream &operator<<(std::ostream &out, const generic_choice<PackageUniverse> &choice)
{
switch(choice.get_type())
{
case generic_choice<PackageUniverse>::install_version:
out << "Install(" << choice.get_ver();
if(!choice.get_has_dep())
{
out << ")";
return out;
}
else if(choice.get_from_dep_source())
out << " <source: ";
else
out << " <";
return out << choice.get_dep() << ">)";
case generic_choice<PackageUniverse>::break_soft_dep:
return out << "Break(" << choice.get_dep() << ")";
default:
// We should never get here.
return out << "Error: corrupt choice object";
}
}
#endif
|