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

TagcollReverser.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_TAGCOLLREVERSER_H 00002 #define TAGCOLL_TAGCOLLREVERSER_H 00003 00004 /* 00005 * "Reverse" a tagged collection, associating items to tags instead of tags to 00006 * items 00007 * 00008 * Copyright (C) 2003 Enrico Zini <enrico@debian.org> 00009 * 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 */ 00024 00025 #pragma interface 00026 00027 #include <tagcoll/TagcollConsumer.h> 00028 00029 #include <string> 00030 #include <map> 00031 00032 namespace Tagcoll 00033 { 00034 00035 // TagcollConsumer that builds a tagged collection 00036 template<class ITEM = std::string, class TAG = std::string> 00037 class TagcollReverser : public TagcollConsumer<ITEM, TAG> 00038 { 00039 protected: 00040 std::map< TAG, OpSet<ITEM> > tagged; 00041 OpSet<ITEM> untagged; 00042 ITEM untagged_name; 00043 00044 public: 00045 virtual ~TagcollReverser() throw () {} 00046 00047 // Set the item name to use when reversing untagged items. If not set, 00048 // untagged items won't be included in the reversed collection 00049 void setUntaggedItemName(const ITEM& item) throw () 00050 { 00051 untagged_name = item; 00052 } 00053 00054 virtual void consume(const ITEM& item) throw () 00055 { 00056 untagged += item; 00057 } 00058 00059 virtual void consume(const ITEM& item, const OpSet<TAG>& tags) throw () 00060 { 00061 for (typename OpSet<ITEM>::const_iterator i = tags.begin(); 00062 i != tags.end(); i++) 00063 tagged[*i] += item; 00064 } 00065 00066 // Send the reversed data to a consumer 00067 void output(TagcollConsumer<TAG, ITEM>& consumer) throw () 00068 { 00069 if (untagged_name.size() > 0) 00070 if (untagged.empty()) 00071 consumer.consume(untagged_name); 00072 else 00073 consumer.consume(untagged_name, untagged); 00074 00075 for (typename std::map< TAG, OpSet<ITEM> >::const_iterator i = tagged.begin(); 00076 i != tagged.end(); i++) 00077 if (i->second.empty()) 00078 consumer.consume(i->first); 00079 else 00080 consumer.consume(i->first, i->second); 00081 } 00082 }; 00083 00084 }; 00085 00086 // vim:set ts=4 sw=4: 00087 #endif

Generated on Sun Aug 15 14:14:30 2004 for libtagcoll by doxygen 1.3.8