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

TagCollection.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_TAGCOLLECTION_H 00002 #define TAGCOLL_TAGCOLLECTION_H 00003 00004 /* 00005 * Core tagged collection handling 00006 * 00007 * Copyright (C) 2003 Enrico Zini <enrico@debian.org> 00008 * 00009 * This library is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * This library 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 GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with this library; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 */ 00023 00024 #pragma interface 00025 00026 #include <string> 00027 #include <set> 00028 #include <map> 00029 #include <list> 00030 00031 #include <tagcoll/OpSet.h> 00032 #include <tagcoll/TagcollConsumer.h> 00033 #include <tagcoll/TagcollChange.h> 00034 00035 // Definition of tag implication: 00036 // tag1 is implied by tag2 if all items associated to tag2 are also 00037 // associated to tag1 00038 // Also said: 00039 // tag1 implies tag2 if all items associated to tag1 are also associated 00040 // to tag2 00041 00042 namespace Tagcoll 00043 { 00044 00045 // Container for bookmarks stored for computation 00046 template<class ITEM, class TAG> 00047 class TagCollection : public TagcollConsumer<ITEM, TAG> 00048 { 00049 protected: 00050 class TagContainer : public std::map<TAG, int> 00051 { 00052 public: 00053 void add(const TAG& tag, int card = 1) throw (); 00054 void del(const TAG& tag, int card = 1) throw (); 00055 }; 00056 00057 // Tags in this collection, with their cardinality 00058 TagContainer tags; 00059 00060 // Tag sets in this collection, with their cardinality 00061 typedef std::map<OpSet<TAG>, OpSet<ITEM> > tagsets_t; 00062 tagsets_t tagsets; 00063 00064 // Items that have no tags in this collection 00065 OpSet<ITEM> untagged; 00066 00067 // Get the list of tags that imply one of the tags in `tags' 00068 OpSet<TAG> getImplyingOneOf(const OpSet<TAG>& tags) const throw (); 00069 00070 public: 00071 // Represent a change to the TagCollection 00072 class Change : public std::map< ITEM, OpSet<TAG> > {}; 00073 00074 TagCollection() throw () {} 00075 00076 TagCollection(const TagCollection& tc) throw () 00077 : tags(tc.tags), tagsets(tc.tagsets), untagged(tc.untagged) {} 00078 00079 virtual ~TagCollection() throw () {} 00080 00081 TagCollection& operator=(const TagCollection& tc) throw () 00082 { 00083 tags = tc.tags; 00084 tagsets = tc.tagsets; 00085 untagged = tc.untagged; 00086 return *this; 00087 } 00088 00089 // Iterators support 00090 typedef typename tagsets_t::const_iterator const_iterator; 00091 typedef typename tagsets_t::iterator iterator; 00092 00093 iterator begin() throw () { return tagsets.begin(); } 00094 iterator end() throw () { return tagsets.end(); } 00095 const_iterator begin() const throw () { return tagsets.begin(); } 00096 const_iterator end() const throw () { return tagsets.end(); } 00097 00098 // Get the number of different tags in this collection 00099 int tagCount() const throw () { return tags.size(); } 00100 00101 // Get the number of different tag sets in this collection 00102 int tagsetCount() const throw () { return tagsets.size(); } 00103 00104 // Get the number of untagged items in this collection 00105 int untaggedCount() const throw () { return untagged.size(); } 00106 00107 // Get the total number of items in this collection 00108 int totalCount() const throw (); 00109 00110 // Get the set of untagged items in this collection 00111 OpSet<ITEM> getUntaggedItems() const throw () { return untagged; } 00112 00113 // Get the set of items with the given tagset 00114 OpSet<ITEM> getItemsForTagset(const OpSet<TAG>& ts) const throw (); 00115 00116 // Get the set of tags for the given item 00117 // Warning: it iterates over all tagsets to find out which one is attached 00118 // to the given item 00119 OpSet<TAG> getTagsetForItem(const ITEM& item) const throw (); 00120 00121 // Get the set of all tags in this collection 00122 OpSet<TAG> getAllTags() const throw (); 00123 00124 // Get the set of all tags in this collection that appear in tagsets 00125 // containing `ts' 00126 OpSet<TAG> getCompanionTags(const OpSet<TAG>& ts) const throw (); 00127 00128 // Get the set of all items in this collection 00129 OpSet<ITEM> getAllItems() const throw (); 00130 00131 // Get the set of all items in this collection whose tagsets contain `ts' 00132 OpSet<ITEM> getCompanionItems(const OpSet<TAG>& ts) const throw (); 00133 00134 // Get the set of all items in this collection whose tagsets contain `ts' 00135 std::map< ITEM, OpSet<TAG> > getCompanionItemsAndTagsets(const OpSet<TAG>& ts) const throw (); 00136 00137 // Get the list of tagsets related to the given one, with distance > 0 and <= maxdistance 00138 std::list< OpSet<TAG> > getRelatedTagsets(const OpSet<TAG>& ts, int maxdistance = 1) const throw (); 00139 00140 // Apply a Change to the collection; return a reverse change that can be 00141 // reused to undo the operation 00142 Change applyChange(const Change& change) throw (); 00143 TagcollChange<ITEM, TAG> applyChange(const TagcollChange<ITEM, TAG>& change) throw (); 00144 00145 // Add an untagged item to the collection 00146 void add(const ITEM& item) throw (); 00147 00148 // Add a set of untagged items to the collection 00149 void add(const OpSet<ITEM>& items) throw (); 00150 00151 // Add an item with the given tagset to the tagged collection 00152 void add(const OpSet<TAG>& tagset, const ITEM& item) throw (); 00153 00154 // Add a set of items with the given tagset to the tagged collection 00155 void add(const OpSet<TAG>& tagset, const OpSet<ITEM>& items) throw (); 00156 00157 // Add an untagged item to the collection 00158 void consume(const ITEM& item) throw () { add(item); } 00159 00160 // Add a set of untagged items to the collection 00161 void consume(const OpSet<ITEM>& items) throw () { add(items); } 00162 00163 // Add an item with the given tagset to the tagged collection 00164 void consume(const ITEM& item, const OpSet<TAG>& tagset) throw () { add(tagset, item); } 00165 00166 // Add a set of items with the given tagset to the tagged collection 00167 void consume(const OpSet<ITEM>& items, const OpSet<TAG>& tagset) throw () { add(tagset, items); } 00168 00169 // Return a tagged collection with all tagsets of this one that contain the 00170 // tag `tag', but with the tag removed 00171 TagCollection<ITEM, TAG> getChildCollection(const TAG& tag) const throw (); 00172 00173 // Return a tagged collection with all tagsets of this one that are 00174 // nonempty when stripped by the tag `tag' and all tags that imply it 00175 TagCollection<ITEM, TAG> getCollectionWithoutTags(const OpSet<TAG>& tag) const throw (); 00176 00177 // Return the tagged collection with all tagsets of this one that do not 00178 // contain the tag `tag' 00179 TagCollection<ITEM, TAG> getCollectionWithoutTagsetsHaving(const TAG& tag) const throw (); 00180 00181 // Return the tagged collection with all tagsets of this one that do not 00182 // contain the tags in `tags' 00183 TagCollection<ITEM, TAG> getCollectionWithoutTagsetsHavingAnyOf(const OpSet<TAG>& tag) const throw (); 00184 00185 // Return the tag with maximum cardinality 00186 TAG findTagWithMaxCardinalityNotIn(const OpSet<TAG>& tags, int* card = 0) const throw (); 00187 00188 // Return the list of tags that the given tag implies 00189 OpSet<TAG> getImpliedBy(const TAG& tag) const throw (); 00190 00191 // Return a collection where equivalent tags are merged. 00192 // Equivalent tags are tags which are attached to the same set of items 00193 // Merging two equivalent tags A and B is done renaming both of them in the 00194 // tag "A, B" 00195 void mergeEquivalentTags() throw (); 00196 00197 // Remove all the tags with cardinality less than `card' 00198 void removeTagsWithCardinalityLessThan(int card) throw (); 00199 00200 // Output the contents of the collection to a TagcollConsumer 00201 void output(TagcollConsumer<ITEM, TAG>& cons) const throw (); 00202 }; 00203 00204 }; 00205 00206 // vim:set ts=4 sw=4: 00207 #endif

Generated on Sun Aug 15 15:25:08 2004 for libtagcoll by doxygen 1.3.8