2
* Classes handling tag patches
4
* Copyright (C) 2003 Enrico Zini <enrico@debian.org>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#ifndef TAGCOLL_PATCHES_CPP
22
#define TAGCOLL_PATCHES_CPP
24
#include <tagcoll/patch.h>
27
using namespace wibble::operators;
31
template <class ITEM, class TAG>
32
void PatchList<ITEM, TAG>::addPatch(const Patch<ITEM, TAG>& patch)
34
// Filter out empty patches
35
if (patch.added.empty() && patch.removed.empty())
38
iterator i = this->find(patch.getItem());
40
insert(make_pair<ITEM, Patch<ITEM, TAG> >(patch.getItem(), patch));
42
i->second.mergeWith(patch);
45
template <class ITEM, class TAG>
46
void PatchList<ITEM, TAG>::addPatch(const PatchList<ITEM, TAG>& patches)
48
for (typename PatchList<ITEM, TAG>::const_iterator i = patches.begin();
49
i != patches.end(); i++)
53
template <class ITEM, class TAG> template<typename COLL1, typename COLL2>
54
void PatchList<ITEM, TAG>::addPatch(const COLL1& im1, const COLL2& im2)
56
for (typename COLL1::const_iterator i1 = im1.begin();
57
i1 != im1.end(); ++i1)
59
std::set<TAG> ts2 = im2.getTags(i1->first);
60
std::set<TAG> added = ts2 - i1->second;
61
std::set<TAG> removed = i1->second - ts2;
62
if (!added.empty() || !removed.empty())
63
addPatch(Patch<ITEM, TAG>(i1->first, added, removed));
67
template <class ITEM, class TAG>
68
std::set<TAG> PatchList<ITEM, TAG>::patch(const ITEM& item, const std::set<TAG>& tagset) const
70
// Find the patch record for this item
71
const_iterator p = this->find(item);
73
// If there are no patches, return the tagset unchanged
76
// There are patches: apply them:
77
return p->second.apply(tagset);
80
template <class ITEM, class TAG>
81
PatchList<ITEM, TAG> PatchList<ITEM, TAG>::getReverse() const
83
PatchList<ITEM, TAG> res;
84
for (typename PatchList<ITEM, TAG>::const_iterator i = this->begin();
85
i != this->end(); i++)
86
res.addPatch(i->second.getReverse());
93
void PatchList<ITEM>::consume(const ITEM& item, const std::set<string>& tags)
95
patches.insert(make_pair(item, tags));
98
// Output the patch list to a TagcollConsumer
100
void PatchList<ITEM>::output(TagcollConsumer<ITEM, std::string>& consumer) const
102
for (typename map< ITEM, std::set<string> >::const_iterator i = patches.begin();
103
i != patches.end(); i++)
104
if (i->second.size() == 0)
105
consumer.consume(i->first);
107
consumer.consume(i->first, i->second);
115
// vim:set ts=4 sw=4: