aboutsummaryrefslogtreecommitdiff
path: root/paludis/resolver/resolutions_by_resolvent.cc
blob: 7ab0398ff6557985212f488ff11613d51111d10d (plain)
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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2010 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis 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; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/resolver/resolutions_by_resolvent.hh>
#include <paludis/resolver/resolvent.hh>
#include <paludis/resolver/resolution.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/stringify.hh>
#include <paludis/serialise-impl.hh>
#include <list>
#include <unordered_map>

using namespace paludis;
using namespace paludis::resolver;

// we need iterators to remain valid after an insert, but we also need fast
// lookups.
typedef std::list<std::shared_ptr<Resolution> > ResolutionList;
typedef std::unordered_map<Resolvent, ResolutionList::const_iterator, Hash<Resolvent> > ResolutionListIndex;

namespace paludis
{
    template <>
    struct Imp<ResolutionsByResolvent>
    {
        ResolutionList resolution_list;
        ResolutionListIndex resolution_list_index;
    };

    template <>
    struct WrappedForwardIteratorTraits<ResolutionsByResolvent::ConstIteratorTag>
    {
        typedef std::list<std::shared_ptr<Resolution> >::const_iterator UnderlyingIterator;
    };
}

ResolutionsByResolvent::ResolutionsByResolvent() :
    Pimp<ResolutionsByResolvent>()
{
}

ResolutionsByResolvent::~ResolutionsByResolvent()
{
}

ResolutionsByResolvent::ConstIterator
ResolutionsByResolvent::begin() const
{
    return ConstIterator(_imp->resolution_list.begin());
}

ResolutionsByResolvent::ConstIterator
ResolutionsByResolvent::end() const
{
    return ConstIterator(_imp->resolution_list.end());
}

ResolutionsByResolvent::ConstIterator
ResolutionsByResolvent::find(const Resolvent & r) const
{
    ResolutionListIndex::const_iterator x(_imp->resolution_list_index.find(r));
    if (x == _imp->resolution_list_index.end())
        return end();
    else
        return ConstIterator(x->second);
}

ResolutionsByResolvent::ConstIterator
ResolutionsByResolvent::insert_new(const std::shared_ptr<Resolution> & r)
{
    ResolutionList::iterator i(_imp->resolution_list.insert(_imp->resolution_list.end(), r));
    if (! _imp->resolution_list_index.insert(std::make_pair(r->resolvent(), i)).second)
    {
        _imp->resolution_list.erase(i);
        throw InternalError(PALUDIS_HERE, "duplicate resolution");
    }
    return ConstIterator(i);
}

void
ResolutionsByResolvent::serialise(Serialiser & s) const
{
    s.object("ResolutionsByResolvent")
        .member(SerialiserFlags<serialise::container>(), "items", _imp->resolution_list)
        ;
}

const std::shared_ptr<ResolutionsByResolvent>
ResolutionsByResolvent::deserialise(Deserialisation & d)
{
    Deserialisator v(d, "ResolutionsByResolvent");
    Deserialisator vv(*v.find_remove_member("items"), "c");
    std::shared_ptr<ResolutionsByResolvent> result(std::make_shared<ResolutionsByResolvent>());
    for (int n(1), n_end(vv.member<int>("count") + 1) ; n != n_end ; ++n)
        result->insert_new(vv.member<std::shared_ptr<Resolution> >(stringify(n)));
    return result;
}

template class WrappedForwardIterator<ResolutionsByResolvent::ConstIteratorTag, const std::shared_ptr<Resolution> >;