00001 #ifndef DEBTAGS_UTILS_H
00002 #define DEBTAGS_UTILS_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00141 #endif