Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Utils.h

Go to the documentation of this file.
00001 #ifndef DEBTAGS_UTILS_H
00002 #define DEBTAGS_UTILS_H
00003 
00004 /*
00005  * Ready-made utility classes for Debtags
00006  *
00007  * Copyright (C) 2003,2004,2005  Enrico Zini <enrico@debian.org>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00024 #include <debtags/Tags.h>
00025 #include <debtags/Filters.h>
00026 #include <debtags/Status.h>
00027 #include <tagcoll/CardinalityStore.h>
00028 #include <map>
00029 
00030 
00031 namespace Debtags
00032 {
00033 
00034 template <class ITEM>
00035 class SearchHelper
00036 {
00037 protected:
00038     static const int dist = 7;
00039 
00040     class CardCompute : public std::map<Tag, int>, public Tagcoll::Consumer<ITEM, Tag>
00041     {
00042         void consumeItemUntagged(const ITEM& item) {}
00043         void consumeItem(const ITEM& item, const Tagcoll::OpSet<Tag>& tags)
00044         {
00045             for (Tagcoll::OpSet<Tag>::const_iterator i = tags.begin();
00046                     i != tags.end(); i++)
00047                 (*this)[*i]++;
00048         }
00049     public:
00050         virtual ~CardCompute() {}
00051     };
00052 
00053     Tags<ITEM>& debtags;
00054     OpSet<ITEM> startPackages;
00055     OpSet<Tag> wanted;
00056     OpSet<Tag> unwanted;
00057     OpSet<ITEM> found;
00058     OpSet<Tag> pivotTags;
00059     CardCompute cardComputer;
00060     int cards[dist];
00061     Tag tags[dist];
00062 
00063     void storeTag(const Tag& tag, int card = -1, int idx = 0);
00064 
00065 public:
00066     SearchHelper(Tags<ITEM>& debtags) : debtags(debtags) {}
00067 
00068     void clearStartPackages() { startPackages.clear(); }
00069     void addStartPackage(const ITEM& item) { startPackages += item; }
00070 
00071     void clearWanted() { wanted.clear(); }
00072     void addWanted(const Tag& tag) { wanted += tag; }
00073     const OpSet<Tag>& getWanted() const { return wanted; }
00074 
00075     void clearUnwanted() { unwanted.clear(); }
00076     void addUnwanted(const Tag& tag) { unwanted += tag; }
00077     const OpSet<Tag>& getUnwanted() const { return unwanted; }
00078 
00079     const OpSet<Tag>& getPivot() const { return pivotTags; }
00080 
00081     const OpSet<ITEM>& getFound() const { return found; }
00082 
00083     void compute();
00084 };
00085 
00086 
00096 template <class ITEM>
00097 class Specials : public Tagcoll::Consumer<ITEM, Tag>
00098 {
00099 protected:
00100     Tagcoll::CardinalityStore<ITEM, Facet> coll;
00101     TagToFacet<ITEM> tagStripper;
00102     unsigned int maxPerGroup;
00103     bool computed;
00104 
00105     virtual void consumeItemUntagged(const ITEM& item)
00106     {
00107         tagStripper.consume(item);
00108     }
00109     virtual void consumeItem(const ITEM& item, const Tagcoll::OpSet<Tag>& tags)
00110     {
00111         tagStripper.consume(item, tags);
00112     }
00113     virtual void consumeItemsUntagged(const Tagcoll::OpSet<ITEM>& items)
00114     {
00115         tagStripper.consume(items);
00116     }
00117     virtual void consumeItems(const Tagcoll::OpSet<ITEM>& items, const Tagcoll::OpSet<Tag>& tags)
00118     {
00119         tagStripper.consume(items, tags);
00120     }
00121 
00122 public:
00123     Specials(unsigned int maxPerGroup = 0) :
00124         tagStripper(coll), maxPerGroup(maxPerGroup), computed(false) {}
00125     virtual ~Specials() {}
00126     
00127     bool isComputed() const { return computed; }
00128     void compute(Status* tracker = 0);
00129     void clear()
00130     {
00131         coll = Tagcoll::CardinalityStore<ITEM, Facet>(); specials.clear();
00132         computed = false;
00133     }
00134 
00135     std::map<Facet, Tagcoll::OpSet<ITEM> > specials;
00136 };
00137 
00138 }
00139 
00140 // vim:set ts=4 sw=4:
00141 #endif

Generated on Sat Sep 10 03:47:41 2005 for libdebtags by  doxygen 1.4.4