~ubuntu-branches/ubuntu/vivid/aptitude/vivid

« back to all changes in this revision

Viewing changes to .pc/0001-Fix-build-with-g-4.7.patch/src/generic/problemresolver/incremental_expression.h

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2012-06-29 01:48:31 UTC
  • Revision ID: package-import@ubuntu.com-20120629014831-7yxujv9gswyo2hac
Tags: 0.6.6-1ubuntu2
Fix FTBFS with gcc-4.7 (LP: #1007969)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** \file incremental_expression.h */   // -*-c++-*-
 
2
 
 
3
//   Copyright (C) 2009-2010 Daniel Burrows
 
4
//
 
5
//   This program is free software; you can redistribute it and/or
 
6
//   modify it under the terms of the GNU General Public License as
 
7
//   published by the Free Software Foundation; either version 2 of
 
8
//   the License, or (at your option) any later version.
 
9
//
 
10
//   This program is distributed in the hope that it will be useful,
 
11
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
//   General Public License for more details.
 
14
//
 
15
//   You should have received a copy of the GNU General Public License
 
16
//   along with this program; see the file COPYING.  If not, write to
 
17
//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
//   Boston, MA 02111-1307, USA.
 
19
 
 
20
 
 
21
#ifndef BOOL_EXPRESSION_H
 
22
#define BOOL_EXPRESSION_H
 
23
 
 
24
#include <cwidget/generic/util/eassert.h>
 
25
#include <cwidget/generic/util/ref_ptr.h>
 
26
 
 
27
#include <generic/util/compare3.h>
 
28
#include <generic/util/refcounted_base.h>
 
29
 
 
30
#include <algorithm>
 
31
#include <set>
 
32
 
 
33
#include <ostream>
 
34
 
 
35
// A system of incrementally computed expressions stored as a DAG.
 
36
// NOT THREADSAFE (the weak-reference system would utterly break in
 
37
// the presence of threads without a lot of expensive locking, and
 
38
// inside the resolver we don't need it).
 
39
//
 
40
// \todo Support delayed propagation of updates? (basically using a
 
41
// queue -- this would avoid excessive recursion, and also avoid
 
42
// having an unresponsive UI if there's a lot of stuff to update)
 
43
// Maybe have a propagate() method on a generic (non-templated) base
 
44
// class and have the user pass in a deque of pointers to that class
 
45
// when signaling updates or modifying variable values.  propagate()
 
46
// would also accept a deque, to totally flatten out the computation.
 
47
 
 
48
template<typename T>
 
49
class expression;
 
50
 
 
51
template<typename T>
 
52
class expression_weak_ref;
 
53
 
 
54
// Generic base class so arbitrary incoming weak references can be
 
55
// invalidated by the expression type.
 
56
class expression_weak_ref_generic
 
57
{
 
58
  // This should be a friend of the weak_ref below, but I don't
 
59
  // remember how to make friends and templates get along; making its
 
60
  // private parts protected instead.
 
61
protected:
 
62
  bool valid;
 
63
 
 
64
  expression_weak_ref_generic(bool _valid)
 
65
    : valid(_valid)
 
66
  {
 
67
  }
 
68
 
 
69
public:
 
70
  expression_weak_ref_generic()
 
71
    : valid(false)
 
72
  {
 
73
  }
 
74
 
 
75
  bool get_valid() const
 
76
  {
 
77
    return valid;
 
78
  }
 
79
 
 
80
  // This should be private, but it has to be exposed to a templated
 
81
  // class (expression<T>).
 
82
  void invalidate()
 
83
  {
 
84
    valid = false;
 
85
  }
 
86
};
 
87
 
 
88
/** \brief A weak reference to something derived from "expression".
 
89
 *
 
90
 *  \tparam T   The type that is encapsulated (should inherit from "expression").
 
91
 */
 
92
template<typename T>
 
93
class expression_weak_ref : public expression_weak_ref_generic
 
94
{
 
95
  T *expr;
 
96
 
 
97
public:
 
98
  expression_weak_ref()
 
99
    : expr(NULL)
 
100
  {
 
101
  }
 
102
 
 
103
  expression_weak_ref(T *_expr);
 
104
  expression_weak_ref(const cwidget::util::ref_ptr<T> &_expr);
 
105
 
 
106
  expression_weak_ref(const expression_weak_ref &other);
 
107
 
 
108
  expression_weak_ref &operator=(const expression_weak_ref &other);
 
109
 
 
110
  T *get_value() const
 
111
  {
 
112
    eassert(valid);
 
113
 
 
114
    return expr;
 
115
  }
 
116
 
 
117
  bool operator==(const expression_weak_ref &other) const
 
118
  {
 
119
    return expr == other.expr;
 
120
  }
 
121
 
 
122
  bool operator==(const T *other) const
 
123
  {
 
124
    return expr == other;
 
125
  }
 
126
 
 
127
  int compare(const expression_weak_ref &other) const
 
128
  {
 
129
    return aptitude::util::compare3(expr, other.expr);
 
130
  }
 
131
 
 
132
  bool operator<(const expression_weak_ref &other) const
 
133
  {
 
134
    return expr < other.expr;
 
135
  }
 
136
 
 
137
  ~expression_weak_ref();
 
138
};
 
139
 
 
140
namespace aptitude
 
141
{
 
142
  namespace util
 
143
  {
 
144
    template<typename T>
 
145
    class compare3_f<expression_weak_ref<T> >
 
146
    {
 
147
    public:
 
148
      int operator()(const expression_weak_ref<T> &r1,
 
149
                     const expression_weak_ref<T> &r2) const
 
150
      {
 
151
        return r1.compare(r2);
 
152
      }
 
153
    };
 
154
  }
 
155
}
 
156
 
 
157
template<typename T>
 
158
class expression_container;
 
159
 
 
160
/** \brief An expression whose value can be computed incrementally
 
161
 *  and updated in its parents.
 
162
 *
 
163
 *  \typeparam T   The type of value computed by this expression;
 
164
 *                 should be copy-constructable and equality-comparable.
 
165
 */
 
166
template<typename T>
 
167
class expression : public aptitude::util::refcounted_base_not_threadsafe
 
168
{
 
169
  // Weak references to parents.
 
170
  std::set<expression_weak_ref<expression_container<T> > > parents;
 
171
 
 
172
  // Incoming weak references.
 
173
  std::set<expression_weak_ref_generic *> weak_refs;
 
174
 
 
175
  // These two routines should be private, but they need to be exposed
 
176
  // to a templated class (expression_weak_ref<T>).
 
177
public:
 
178
  void add_weak_ref(expression_weak_ref_generic *ref)
 
179
  {
 
180
    weak_refs.insert(ref);
 
181
  }
 
182
 
 
183
  void remove_weak_ref(expression_weak_ref_generic *ref)
 
184
  {
 
185
    weak_refs.erase(ref);
 
186
  }
 
187
 
 
188
protected:
 
189
  void signal_value_changed(T old_value, T new_value)
 
190
  {
 
191
    cwidget::util::ref_ptr<expression> self(this);
 
192
 
 
193
    // Strip out dead references as we go, to save a bit of space.
 
194
    typename std::set<expression_weak_ref<expression_container<T> > >::iterator
 
195
      curr = parents.begin();
 
196
 
 
197
    while(curr != parents.end())
 
198
      {
 
199
        typename std::set<expression_weak_ref<expression_container<T> > >::iterator
 
200
          next = curr;
 
201
        ++next;
 
202
 
 
203
        if(curr->get_valid())
 
204
          curr->get_value()->child_modified(self, old_value, new_value);
 
205
        else
 
206
          parents.erase(curr);
 
207
 
 
208
        curr = next;
 
209
      }
 
210
  }
 
211
 
 
212
public:
 
213
  virtual ~expression()
 
214
  {
 
215
    for(typename std::set<expression_weak_ref_generic *>::const_iterator it =
 
216
          weak_refs.begin(); it != weak_refs.end(); ++it)
 
217
      (*it)->invalidate();
 
218
 
 
219
    weak_refs.clear(); // Not strictly necessary, but avoids potential
 
220
                       // surprises.
 
221
  }
 
222
 
 
223
  // Add a weak reference to the given expression; its
 
224
  // child_modified() routine will be invoked when this child's value
 
225
  // changes.
 
226
  void add_parent(expression_container<T> *parent)
 
227
  {
 
228
    if(parent != NULL)
 
229
      parents.insert(parent);
 
230
  }
 
231
 
 
232
private:
 
233
  class parent_equals_weak_ref
 
234
  {
 
235
    const expression_container<T> *parent;
 
236
 
 
237
  public:
 
238
    parent_equals_weak_ref(const expression_container<T> *_parent)
 
239
      : parent(_parent)
 
240
    {
 
241
    }
 
242
 
 
243
    bool operator()(const expression_weak_ref<expression_container<T> > &other) const
 
244
    {
 
245
      return other == parent;
 
246
    }
 
247
  };
 
248
 
 
249
public:
 
250
  void remove_parent(expression_container<T> *parent)
 
251
  {
 
252
    parents.erase(expression_weak_ref<expression_container<T> >(parent));
 
253
  }
 
254
 
 
255
  virtual T get_value() = 0;
 
256
  virtual void dump(std::ostream &out) = 0;
 
257
};
 
258
 
 
259
/** \brief Base class for expressions that can contain other
 
260
 *  expressions as children.
 
261
 */
 
262
template<typename T>
 
263
class expression_container : public expression<T>
 
264
{
 
265
public:
 
266
  /** \brief Invoked when the value of a child has changed from
 
267
   * old_value to new_value.
 
268
   *
 
269
   *  When this method is called, the value really has changed (i.e.,
 
270
   *  old_value does not equal new_value).
 
271
   */
 
272
  virtual void child_modified(const cwidget::util::ref_ptr<expression<T> > &child,
 
273
                              T old_value,
 
274
                              T new_value) = 0;
 
275
};
 
276
 
 
277
/** \brief Base class for objects that have a single sub-expression. */
 
278
template<typename T>
 
279
class expression_box : public expression_container<T>
 
280
{
 
281
  cwidget::util::ref_ptr<expression<T> > child;
 
282
 
 
283
public:
 
284
  expression_box() : child() { }
 
285
 
 
286
  expression_box(const cwidget::util::ref_ptr<expression<T> > &_child)
 
287
    : child(_child)
 
288
  {
 
289
    if(child.valid())
 
290
      child->add_parent(this);
 
291
  }
 
292
 
 
293
  expression_box(const expression_box &other)
 
294
    : child(other.child)
 
295
  {
 
296
    if(child.valid())
 
297
      child->add_parent(this);
 
298
  }
 
299
 
 
300
  ~expression_box()
 
301
  {
 
302
    if(child.valid())
 
303
      child->remove_parent(this);
 
304
  }
 
305
 
 
306
  void set_child(const cwidget::util::ref_ptr<expression<T> > &new_child)
 
307
  {
 
308
    if(child.valid())
 
309
      child->remove_parent(this);
 
310
    child = new_child;
 
311
    if(new_child.valid())
 
312
      new_child->add_parent(this);
 
313
  }
 
314
 
 
315
  /** \brief Set the child of this box to the child of the other box.
 
316
   */
 
317
  expression_box &operator=(const expression_box &other)
 
318
  {
 
319
    set_child(other.child);
 
320
    return *this;
 
321
  }
 
322
 
 
323
  const cwidget::util::ref_ptr<expression<T> > &get_child() const
 
324
  {
 
325
    return child;
 
326
  }
 
327
 
 
328
  /** \brief Returns the child's value, or a default-constructed T if
 
329
   *  there is no child.
 
330
   */
 
331
  T get_value()
 
332
  {
 
333
    if(child.valid())
 
334
      return child->get_value();
 
335
    else
 
336
      return T();
 
337
  }
 
338
 
 
339
  /** \brief Write this expression to the given stream.
 
340
   *
 
341
   *  The default implementation writes nothing if the child is
 
342
   *  invalid, and writes the child if the child is valid.
 
343
   */
 
344
  void dump(std::ostream &out)
 
345
  {
 
346
    if(get_child().valid())
 
347
      get_child()->dump(out);
 
348
  }
 
349
};
 
350
 
 
351
/** \brief Base class for expressions that trivially wrap their
 
352
 *  subexpression.
 
353
 */
 
354
template<typename T>
 
355
class expression_wrapper : public expression_box<T>
 
356
{
 
357
public:
 
358
  expression_wrapper() : expression_box<T>() { }
 
359
  expression_wrapper(const cwidget::util::ref_ptr<expression<T> > &child)
 
360
    : expression_box<T>(child)
 
361
  {
 
362
  }
 
363
  expression_wrapper(const expression_wrapper &other)
 
364
    : expression_box<T>(other)
 
365
  {
 
366
  }
 
367
 
 
368
  expression_wrapper &operator=(const expression_wrapper &other)
 
369
  {
 
370
    set_child(other.get_child());
 
371
    return *this;
 
372
  }
 
373
 
 
374
  /** \brief Invoked by the default implementation of child_modified.
 
375
   *
 
376
   *  This method's default implementation does nothing.
 
377
   */
 
378
  virtual void changed(T new_value)
 
379
  {
 
380
  }
 
381
 
 
382
  void child_modified(const cwidget::util::ref_ptr<expression<T> > &child,
 
383
                      T old_value,
 
384
                      T new_value)
 
385
  {
 
386
    changed(new_value);
 
387
  }                   
 
388
};
 
389
 
 
390
/** \brief Base class for N-ary containers that support adding and
 
391
 *  removing children.
 
392
 */
 
393
template<typename T>
 
394
class expression_container_base : public expression_container<T>
 
395
{
 
396
  // Strong references to children.
 
397
  std::vector<cwidget::util::ref_ptr<expression<T> > > children;
 
398
 
 
399
public:
 
400
  template<typename Iter>
 
401
  expression_container_base(Iter begin, Iter end)
 
402
    : children(begin, end)
 
403
  {
 
404
    for(typename std::vector<cwidget::util::ref_ptr<expression<T> > >::const_iterator
 
405
          it = get_children().begin(); it != get_children().end(); ++it)
 
406
      if(it->valid())
 
407
        (*it)->add_parent(this);
 
408
  }
 
409
 
 
410
  const std::vector<cwidget::util::ref_ptr<expression<T> > > &get_children() const
 
411
  {
 
412
    return children;
 
413
  }
 
414
 
 
415
  /** \brief Add a new child to this container. */
 
416
  virtual void add_child(const cwidget::util::ref_ptr<expression<T> > &new_child)
 
417
  {
 
418
    children.push_back(new_child);
 
419
  }
 
420
 
 
421
  /** \brief Remove one copy of a child from this container. */
 
422
  virtual void remove_child(const cwidget::util::ref_ptr<expression<T> > &child)
 
423
  {
 
424
    if(child.valid())
 
425
      child->remove_parent(this);
 
426
 
 
427
    typename std::vector<cwidget::util::ref_ptr<expression<T> > >::iterator
 
428
      found = std::find(children.begin(), children.end(), child);
 
429
 
 
430
    if(found != children.end())
 
431
      children.erase(found);
 
432
  }
 
433
 
 
434
  virtual std::string get_name() = 0;
 
435
 
 
436
  void dump(std::ostream &out)
 
437
  {
 
438
    out << get_name() << "(";
 
439
    for(typename std::vector<cwidget::util::ref_ptr<expression<T> > >::const_iterator
 
440
          it = get_children().begin(); it != get_children().end(); ++it)
 
441
      {
 
442
        if(it != get_children().begin())
 
443
          out << ", ";
 
444
 
 
445
        if(it->valid())
 
446
          (*it)->dump(out);
 
447
        else
 
448
          out << "(null)";
 
449
      }
 
450
    out << ")";
 
451
  }
 
452
};
 
453
 
 
454
template<typename T>
 
455
expression_weak_ref<T>::expression_weak_ref(T *_expr)
 
456
  : expression_weak_ref_generic(true), expr(_expr)
 
457
{
 
458
  expr->add_weak_ref(this);
 
459
}
 
460
 
 
461
template<typename T>
 
462
expression_weak_ref<T>::expression_weak_ref(const cwidget::util::ref_ptr<T> &_expr)
 
463
  : expression_weak_ref_generic(true), expr(_expr.unsafe_get_ref())
 
464
{
 
465
  expr->add_weak_ref(this);
 
466
}
 
467
 
 
468
template<typename T>
 
469
expression_weak_ref<T>::expression_weak_ref(const expression_weak_ref<T> &other)
 
470
  : expression_weak_ref_generic(other.get_valid()), expr(other.expr)
 
471
{
 
472
  if(valid)
 
473
    expr->add_weak_ref(this);
 
474
}
 
475
 
 
476
template<typename T>
 
477
expression_weak_ref<T> &expression_weak_ref<T>::operator=(const expression_weak_ref<T> &other)
 
478
{
 
479
  if(valid)
 
480
    expr->remove_weak_ref(this);
 
481
 
 
482
  valid = other.valid;
 
483
  expr = other.expr;
 
484
 
 
485
  if(valid)
 
486
    expr->add_weak_ref(this);
 
487
 
 
488
  return *this;
 
489
}
 
490
 
 
491
template<typename T>
 
492
expression_weak_ref<T>::~expression_weak_ref()
 
493
{
 
494
  if(valid)
 
495
    expr->remove_weak_ref(this);
 
496
}
 
497
 
 
498
/** \brief Represents a variable in the expression language.
 
499
 *
 
500
 *  Variables can be modified arbitrarily; changes are immediately
 
501
 *  propagated to parent expressions.
 
502
 *
 
503
 *  It would be nice if the user could attach names for better
 
504
 *  printing of expressions, but that would take a lot of memory.
 
505
 */
 
506
template<typename T>
 
507
class var_e : public expression<T>
 
508
{
 
509
  T value;
 
510
 
 
511
protected:
 
512
  var_e(T _value) : value(_value)
 
513
  {
 
514
  }
 
515
 
 
516
public:
 
517
  static cwidget::util::ref_ptr<var_e> create(T value)
 
518
  {
 
519
    return new var_e(value);
 
520
  }
 
521
 
 
522
  T get_value()
 
523
  {
 
524
    return value;
 
525
  }
 
526
 
 
527
  /** \brief Set the value of this expression to new_value,
 
528
   *  and propagate it to our parent if necessary.
 
529
   */
 
530
  void set_value(T new_value)
 
531
  {
 
532
    if(value != new_value)
 
533
      {
 
534
        T old_value = value;
 
535
        value = new_value;
 
536
        signal_value_changed(old_value, new_value);
 
537
      }
 
538
  }
 
539
 
 
540
  void dump(std::ostream &out)
 
541
  {
 
542
    out << "v" << this;
 
543
  }
 
544
};
 
545
 
 
546
/** \brief Boolean-specific expressions. */
 
547
 
 
548
// @{
 
549
 
 
550
// Base class for Boolean expressions that use the number of their
 
551
// sub-parts that are "true" to determine their own value.
 
552
class counting_bool_e : public expression_container_base<bool>
 
553
{
 
554
  std::vector<cwidget::util::ref_ptr<expression<bool> > >::size_type num_true;
 
555
 
 
556
  // Invoked from the constructor; counts how many of the children are
 
557
  // true.
 
558
  void init_num_true();
 
559
public:
 
560
  template<typename Iter>
 
561
  counting_bool_e(Iter begin, Iter end)
 
562
    : expression_container_base<bool>(begin, end), num_true(0)
 
563
  {
 
564
    init_num_true();
 
565
  }
 
566
 
 
567
  std::vector<cwidget::util::ref_ptr<expression<bool> > >::size_type
 
568
  get_num_true() const
 
569
  {
 
570
    return num_true;
 
571
  }
 
572
 
 
573
  void child_modified(const cwidget::util::ref_ptr<expression<bool> > &child,
 
574
                      bool old_value,
 
575
                      bool new_value);
 
576
 
 
577
  void add_child(const cwidget::util::ref_ptr<expression<bool> > &new_child);
 
578
  void remove_child(const cwidget::util::ref_ptr<expression<bool> > &child);
 
579
};
 
580
 
 
581
class and_e : public counting_bool_e
 
582
{
 
583
  template<typename Iter>
 
584
  and_e(Iter begin, Iter end)
 
585
    : counting_bool_e(begin, end)
 
586
  {
 
587
  }
 
588
 
 
589
public:
 
590
  template<typename Iter>
 
591
  static cwidget::util::ref_ptr<and_e>
 
592
  create(Iter begin, Iter end)
 
593
  {
 
594
    return new and_e(begin, end);
 
595
  }
 
596
 
 
597
  static cwidget::util::ref_ptr<and_e>
 
598
  create(const cwidget::util::ref_ptr<expression<bool> > &e1,
 
599
         const cwidget::util::ref_ptr<expression<bool> > &e2)
 
600
  {
 
601
    cwidget::util::ref_ptr<expression<bool> > tmp[2];
 
602
    tmp[0] = e1;
 
603
    tmp[1] = e2;
 
604
 
 
605
    return create(tmp, tmp + 2);
 
606
  }
 
607
 
 
608
  bool get_value();
 
609
  std::string get_name();
 
610
};
 
611
 
 
612
class or_e : public counting_bool_e
 
613
{
 
614
  template<typename Iter>
 
615
  or_e(Iter begin, Iter end)
 
616
    : counting_bool_e(begin, end)
 
617
  {
 
618
  }
 
619
 
 
620
public:
 
621
  template<typename Iter>
 
622
  static cwidget::util::ref_ptr<or_e>
 
623
  create(Iter begin, Iter end)
 
624
  {
 
625
    return new or_e(begin, end);
 
626
  }
 
627
 
 
628
  static cwidget::util::ref_ptr<or_e>
 
629
  create(const cwidget::util::ref_ptr<expression<bool> > &e1,
 
630
         const cwidget::util::ref_ptr<expression<bool> > &e2)
 
631
  {
 
632
    cwidget::util::ref_ptr<expression<bool> > tmp[2];
 
633
    tmp[0] = e1;
 
634
    tmp[1] = e2;
 
635
 
 
636
    return create(tmp, tmp + 2);
 
637
  }
 
638
 
 
639
  bool get_value();
 
640
  std::string get_name();
 
641
};
 
642
 
 
643
class not_e : public expression_box<bool>
 
644
{
 
645
  not_e(const cwidget::util::ref_ptr<expression<bool> > &_child)
 
646
    : expression_box<bool>(_child)
 
647
  {
 
648
  }
 
649
 
 
650
public:
 
651
  static cwidget::util::ref_ptr<not_e>
 
652
  create(const cwidget::util::ref_ptr<expression<bool> > &child)
 
653
  {
 
654
    return new not_e(child);
 
655
  }
 
656
 
 
657
  void child_modified(const cwidget::util::ref_ptr<expression<bool> > &child,
 
658
                      bool old_value,
 
659
                      bool new_value);
 
660
  bool get_value();
 
661
  void dump(std::ostream &out);
 
662
};
 
663
 
 
664
template<typename T>
 
665
std::ostream &operator<<(std::ostream &out,
 
666
                         const cwidget::util::ref_ptr<expression<T> > &o)
 
667
{
 
668
  if(o.valid())
 
669
    o->dump(out);
 
670
  else
 
671
    out << "(null)";
 
672
 
 
673
  return out;
 
674
}
 
675
 
 
676
// @}
 
677
 
 
678
#endif