File: aptitude_resolver_cost_settings.cc

package info (click to toggle)
aptitude 0.8.11-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 40,536 kB
  • sloc: cpp: 95,205; xml: 31,928; sh: 5,142; makefile: 922; perl: 88; cs: 70; lisp: 55; sed: 16
file content (368 lines) | stat: -rw-r--r-- 11,916 bytes parent folder | download | duplicates (2)
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
/** \file aptitude_resolver_cost_settings.cc */

// Copyright (C) 2010 Daniel Burrows
// Copyright (C) 2015-2018 Manuel A. Fernandez Montecelo

// 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., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301, USA.

#include "aptitude_resolver_cost_settings.h"


#include "aptitude_resolver.h"
#include "aptitude_resolver_cost_syntax.h"

#include <boost/format.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/random_access_index.hpp>

#include <optional>

using namespace boost::multi_index;

// This code acts as a frontend that interprets the cost language for
// the resolver.  It type-checks costs to ensure that they don't go
// wrong at "runtime", it drops costs that aren't used by the final
// cost expression, and it pushes costs through to the components they
// actually modify.  For instance, if the first cost component is
// "removals + canceled-upgrades", any addition to "removals" or
// "canceled-upgrades" will actually modify that level in the
// resolver.

class aptitude_resolver_cost_settings::settings_impl
{
  /** \brief Used to track the effect of a named component.
   */
  class component_effect
  {
    int id; // The component index.
    int multiplier; // How much to multiply settings to this component
                    // by.
  public:
    component_effect(int _id, int _multiplier)
      : id(_id), multiplier(_multiplier)
    {
    }

    int get_id() const { return id; }
    int get_multiplier() const { return multiplier; }
  };

  class entry
  {
    std::string name;
    // If the type doesn't have a value, it's because the component
    // hasn't been seen in a typed context yet, so its type is
    // "bottom".
    std::optional<component_type> type;
    // The actual cost components that are modified by this entry.
    std::vector<component_effect> effects;

  public:
    entry(const std::string &_name,
          const std::optional<component_type> &_type,
          const std::vector<component_effect> &_effects)
      : name(_name),
        type(_type),
        effects(_effects)
    {
    }

    const std::string &get_name() const { return name; }
    const std::optional<component_type> &get_type() const { return type; }
    void set_type(const std::optional<component_type> &new_type)
    {
      type = new_type;
    }
    const std::vector<component_effect> &get_effects() const { return effects; }
    std::vector<component_effect> &get_effects() { return effects; }
  };

  // Modifying functor used to add an effect to an entry.
  class add_effect_f
  {
    component_effect new_effect;
  public:
    add_effect_f(const component_effect &_new_effect)
      : new_effect(_new_effect)
    {
    }

    void operator()(entry &e) const
    {
      e.get_effects().push_back(new_effect);
    }
  };

  // Combine the target value's component type with the incoming type,
  // throwing an error if they're incompatible.
  //
  // Call .assertType first before calling modify() with this modifier,
  // as the throw here would otherwise cause boost >= 1.66 to remove
  // the element rather than keeping it unmodified.
  class merge_types_f
  {
    std::optional<component_type> type;
    std::string name;

  public:
    merge_types_f(const std::optional<component_type> &_type)
      : type(_type)
    {
    }

    void operator()(entry &e) const
    {
      if(type)
        {
          if(!e.get_type())
            e.set_type(type);
          else if(type != e.get_type())
            throw CostTypeCheckFailure((boost::format(_("Conflicting types for the cost component %s."))
                                        % e.get_name()).str());
        }
    }

    void assertType(const entry &e) const
    {
      if(type && e.get_type() && type != e.get_type())
        throw CostTypeCheckFailure((boost::format(_("Conflicting types for the cost component %s."))
                                    % e.get_name()).str());
    }
  };

  // Tags for the entry holder.
  class name_t;
  class ordered_t;

  typedef multi_index_container<
    entry,
    indexed_by<
      random_access<tag<ordered_t> >,
      hashed_unique<tag<name_t>,
                    const_mem_fun<entry,
                                  const std::string &,
                                  &entry::get_name> > > >
  entry_holder;

  typedef entry_holder::index<name_t>::type by_name_index;
  typedef entry_holder::index<ordered_t>::type ordered_index;

  entry_holder entries;

  std::shared_ptr<std::vector<cost_component_structure> > settings;

public:
  explicit settings_impl(const std::shared_ptr<std::vector<cost_component_structure> > &_settings)
    : settings(_settings)
  {
    // Every time a cost component is named in the given settings
    // object, we look it up (creating it if it doesn't exist), check
    // that it has the appropriate type, and link it to the
    // corresponding entry in this object.

    by_name_index &by_name = entries.get<name_t>();
    ordered_index &ordered = entries.get<ordered_t>();

    int component_idx = 0;
    for(std::vector<cost_component_structure>::const_iterator
          settings_it = settings->begin(); settings_it != settings->end(); ++settings_it)
      {
        cost_component_structure::op op = settings_it->get_combining_op();
        std::vector<cost_component_structure::entry> &component =
          *settings_it->get_entries();

        std::optional<component_type> type;
        switch(op)
          {
          case cost_component_structure::combine_add:
            type = additive;
            break;

          case cost_component_structure::combine_max:
            type = maximized;
            break;

          case cost_component_structure::combine_none:
            type = std::optional<component_type>();
            break;

          default:
            throw CostTypeCheckFailure("Internal error: invalid cost component operation code.");
          }

        for(std::vector<cost_component_structure::entry>::const_iterator
              component_it = component.begin(); component_it != component.end(); ++component_it)
          {
            // Unpack this piece of the component.
            const std::string &name = component_it->get_name();
            int scaling_factor = component_it->get_scaling_factor();

            component_effect effect(component_idx, scaling_factor);

            by_name_index::iterator found = by_name.find(name);
            if(found == by_name.end())
              {
                std::vector<component_effect> effects;
                effects.push_back(effect);

                ordered.push_back(entry(name, type, effects));
              }
            else
              {
                merge_types_f(type).assertType(*found);
                by_name.modify(found, merge_types_f(type));
                by_name.modify(found, add_effect_f(effect));
              }
          }

        ++component_idx;
      }
  }

  component get_or_create_component(const std::string &name,
                                    component_type type)
  {
    // Look the component up by name.  If it doesn't exist, we can go
    // ahead and create it.  If it does exist, we need to check that
    // the types match.  In either case, if no "real" cost component
    // is affected by the named component, a component ID of -1 is
    // returned (indicating that the component has no real effect).

    by_name_index &by_name = entries.get<name_t>();
    by_name_index::iterator found = by_name.find(name);

    if(found == by_name.end())
      {
        by_name.insert(entry(name, type, std::vector<component_effect>()));
        return component(-1);
      }
    else
      {
        ordered_index &ordered = entries.get<ordered_t>();
        ordered_index::iterator found_ordered = entries.project<ordered_t>(found);
        component rval(found->get_effects().empty() ? -1 : found_ordered - ordered.begin());

        merge_types_f(type).assertType(*found);
        by_name.modify(found, merge_types_f(type));

        return rval;
      }
  }

  cost add_to_cost(const component &component,
                   int amt)
  {
    // Ignore irrelevant components.
    if(component.id < 0)
      return cost_limits::minimum_cost;

    // Sanity-check that the component's type is additive, then add
    // the given value to each target cost component.

    ordered_index &ordered = entries.get<ordered_t>();

    if((unsigned int)component.id >= ordered.size())
      throw CostTypeCheckFailure("Internal error: mismatch between component ID and the number of components.");

    merge_types_f(additive).assertType(*(ordered.begin() + component.id));
    ordered.modify(ordered.begin() + component.id, merge_types_f(additive));

    const entry &e = ordered[component.id];
    cost rval;
    for(std::vector<component_effect>::const_iterator it = e.get_effects().begin();
        it != e.get_effects().end(); ++it)
      {
        cost c =
          cost::make_add_to_user_level(it->get_id(), amt * it->get_multiplier());

        rval = rval + c;
      }

    return rval;
  }

  cost raise_cost(const component &component,
                  int amt)
  {
    // Ignore irrelevant components.
    if(component.id < 0)
      return cost_limits::minimum_cost;

    // Sanity-check that the component's type is maximized, then add
    // the given value to each target cost component.

    ordered_index &ordered = entries.get<ordered_t>();

    if((unsigned int)component.id >= ordered.size())
      throw CostTypeCheckFailure("Internal error: mismatch between component ID and the number of components.");

    merge_types_f(maximized).assertType(*(ordered.begin() + component.id));
    ordered.modify(ordered.begin() + component.id, merge_types_f(maximized));

    cost rval;
    const entry &e = ordered[component.id];
    for(std::vector<component_effect>::const_iterator it = e.get_effects().begin();
        it != e.get_effects().end(); ++it)
      {
        cost c =
          cost::make_advance_user_level(it->get_id(), amt * it->get_multiplier());

        rval = rval + c;
      }

    return rval;
  }

  void dump(std::ostream &out) const
  {
    dump_settings(out, settings);
  }
};

aptitude_resolver_cost_settings::aptitude_resolver_cost_settings(const std::shared_ptr<std::vector<cost_component_structure> > &settings)
  : impl(std::make_shared<settings_impl>(settings))
{
}

aptitude_resolver_cost_settings::~aptitude_resolver_cost_settings()
{
}

void aptitude_resolver_cost_settings::dump(std::ostream &out) const
{
  impl->dump(out);
}

aptitude_resolver_cost_settings::component
aptitude_resolver_cost_settings::get_or_create_component(const std::string &name,
                                                         component_type type)
{
  return impl->get_or_create_component(name, type);
}

cost aptitude_resolver_cost_settings::add_to_cost(const component &component,
                                                  int amt)
{
  return impl->add_to_cost(component, amt);
}

cost aptitude_resolver_cost_settings::raise_cost(const component &component,
                                                 int amt)
{
  return impl->raise_cost(component, amt);
}