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