00001 #ifndef TAGCOLL_CARDINALITYSTORE_H
00002 #define TAGCOLL_CARDINALITYSTORE_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <tagcoll/Collection.h>
00027
00028 #include <set>
00029 #include <map>
00030 #include <list>
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 namespace Tagcoll
00041 {
00042
00051 template<class ITEM, class TAG>
00052 class CardinalityStore : public Collection<ITEM, TAG>
00053 {
00054 public:
00055 typedef std::map<OpSet<TAG>, OpSet<ITEM> > tagsets_t;
00056
00057 protected:
00058 class TagContainer : public std::map<TAG, int>
00059 {
00060 public:
00061 TagContainer() {}
00062 TagContainer(const TagContainer& tc) : std::map<TAG, int>(tc) {}
00063 ~TagContainer() {}
00064
00065 void add(const TAG& tag, int card = 1) throw ();
00066 void del(const TAG& tag, int card = 1) throw ();
00067 };
00068
00069
00070 TagContainer tags;
00071
00072
00073 tagsets_t tagsets;
00074
00078 OpSet<TAG> getImplyingOneOf(const OpSet<TAG>& tags) const;
00079
00080 void consumeItem(const ITEM& item, const OpSet<TAG>& tagset);
00081 void consumeItems(const OpSet<ITEM>& items, const OpSet<TAG>& tagset);
00082
00083 virtual OpSet<ITEM> getItemsHavingTag(const TAG& tag) const;
00084 virtual OpSet<ITEM> getItemsHavingTags(const OpSet<TAG>& tags) const;
00085 virtual OpSet<TAG> getTagsOfItem(const ITEM& item) const;
00086 virtual OpSet<TAG> getTagsOfItems(const OpSet<ITEM>& items) const;
00087
00088
00089 public:
00090 CardinalityStore() {}
00091 virtual ~CardinalityStore() {}
00092
00096 typedef typename tagsets_t::const_iterator const_iterator;
00097 typedef typename tagsets_t::iterator iterator;
00098
00099 iterator begin() { return tagsets.begin(); }
00100 iterator end() { return tagsets.end(); }
00101 const_iterator begin() const { return tagsets.begin(); }
00102 const_iterator end() const { return tagsets.end(); }
00105
00106 int itemCount() const;
00107
00109 int tagCount() const { return tags.size(); }
00110
00112 int tagsetCount() const { return tagsets.size(); }
00113
00115 OpSet<ITEM> getItemsExactMatch(const OpSet<TAG>& ts) const;
00116
00117 bool hasItem(const ITEM& item) const;
00118 bool hasTag(const TAG& tag) const { return getCardinality(tag) > 0; }
00119
00120 OpSet<TAG> getAllTags() const;
00121 OpSet<ITEM> getTaggedItems() const;
00122
00123 OpSet<TAG> getCompanionTags(const OpSet<TAG>& ts) const;
00124
00126 OpSet<ITEM> getCompanionItems(const OpSet<TAG>& ts) const;
00127
00129 std::map< ITEM, OpSet<TAG> > getCompanionItemsAndTagsets(const OpSet<TAG>& ts) const;
00130
00131 OpSet<ITEM> getRelatedItems(const OpSet<TAG>& ts, int maxdistance = 1) const;
00132
00134 std::list< OpSet<TAG> > getRelatedTagsets(const OpSet<TAG>& ts, int maxdistance = 1) const;
00135
00136 void applyChange(const PatchList<ITEM, TAG>& change);
00137
00138
00141 CardinalityStore<ITEM, TAG> getChildCollection(const TAG& tag) const;
00142
00145 CardinalityStore<ITEM, TAG> getCollectionWithoutTags(const OpSet<TAG>& tag) const;
00146
00149 CardinalityStore<ITEM, TAG> getCollectionWithoutTagsetsHaving(const TAG& tag) const;
00150
00153 CardinalityStore<ITEM, TAG> getCollectionWithoutTagsetsHavingAnyOf(const OpSet<TAG>& tag) const;
00154
00156 OpSet<TAG> getImpliedBy(const TAG& tag) const;
00157
00158 int getCardinality(const TAG& tag) const;
00159
00160
00161 TAG findTagWithMaxCardinalityNotIn(const OpSet<TAG>& tags, int* card = 0) const;
00162
00163
00164
00165
00166
00167 void mergeEquivalentTags();
00168
00169
00170 void removeTagsWithCardinalityLessThan(int card);
00171
00172 void output(Consumer<ITEM, TAG>& cons) const;
00173 void outputHavingTags(const OpSet<TAG>& ts, Consumer<ITEM, TAG>& consumer) const;
00174 };
00175
00176 };
00177
00178
00179 #endif