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

CardinalityStore.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_CARDINALITYSTORE_H
00002 #define TAGCOLL_CARDINALITYSTORE_H
00003 
00004 /* \file
00005  * In-memory collection keeping a fast-access track of tag cardinalities.
00006  */
00007 
00008 /*
00009  * Copyright (C) 2003,2004,2005  Enrico Zini <enrico@debian.org>
00010  *
00011  * This library is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2.1 of the License, or (at your option) any later version.
00015  *
00016  * This library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this library; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00024  */
00025 
00026 #include <tagcoll/Collection.h>
00027 
00028 #include <set>
00029 #include <map>
00030 #include <list>
00031 
00032 
00033 // Definition of tag implication:
00034 //   tag1 is implied by tag2 if all items associated to tag2 are also
00035 //   associated to tag1
00036 // Also said:
00037 //   tag1 implies tag2 if all items associated to tag1 are also associated
00038 //   to tag2
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     // Tags in this collection, with their cardinality
00070     TagContainer tags;
00071     
00072     // Tag sets in this collection, with their item set
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     // Return the tag with maximum cardinality
00161     TAG findTagWithMaxCardinalityNotIn(const OpSet<TAG>& tags, int* card = 0) const;
00162         
00163     // Return a collection where equivalent tags are merged.
00164     // Equivalent tags are tags which are attached to the same set of items
00165     // Merging two equivalent tags A and B is done renaming both of them in the
00166     // tag "A, B"
00167     void mergeEquivalentTags();
00168 
00169     // Remove all the tags with cardinality less than `card'
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 // vim:set ts=4 sw=4:
00179 #endif

Generated on Wed Sep 21 05:22:09 2005 for libtagcoll by  doxygen 1.4.4