00001 #ifndef TAGCOLL_FILTERS_H
00002 #define TAGCOLL_FILTERS_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/Consumer.h>
00027 #include <tagcoll/Filter.h>
00028
00029 #include <map>
00030 #include <string>
00031
00032 namespace Tagcoll
00033 {
00034
00041 template<typename T>
00042 class Substitutions : public Consumer<T, T>
00043 {
00044 protected:
00045 typedef std::map<T, T> changes_t;
00046 changes_t changes;
00047
00048 virtual void consumeItemUntagged(const T& item) {}
00049 virtual void consumeItem(const T& item, const OpSet<T>& tags)
00050 {
00051 for (typename OpSet<T>::const_iterator i = tags.begin();
00052 i != tags.end(); i++)
00053 changes.insert(make_pair(*i, item));
00054 }
00055 virtual void consumeItemsUntagged(const OpSet<T>& items) {}
00056
00057 public:
00058 virtual ~Substitutions() {}
00059
00061 T change(const T& v) const
00062 {
00063 typename changes_t::const_iterator i = changes.find(v);
00064
00065 return (i == changes.end()) ? v : i->second;
00066 }
00067
00069 OpSet<T> change(const OpSet<T>& values) const
00070 {
00071 OpSet<T> res;
00072 for (typename OpSet<T>::const_iterator t = values.begin();
00073 t != values.end(); t++)
00074 res += change(*t);
00075 return res;
00076 }
00077 };
00078
00094 template<typename ITEM, typename TAG>
00095 class Substitute : public Filter<ITEM, TAG>
00096 {
00097 protected:
00098 Substitutions<TAG> changes;
00099
00100 virtual void consumeItemUntagged(const ITEM& item)
00101 {
00102 this->consumer->consume(item);
00103 }
00104 virtual void consumeItem(const ITEM& item, const OpSet<TAG>& tags)
00105 {
00106 this->consumer->consume(item, changes.change(tags));
00107 }
00108 virtual void consumeItemsUntagged(const OpSet<ITEM>& items)
00109 {
00110 this->consumer->consume(items);
00111 }
00112 virtual void consumeItem(const OpSet<ITEM>& items, const OpSet<TAG>& tags)
00113 {
00114 this->consumer->consume(items, changes.change(tags));
00115 }
00116
00117 public:
00118 Substitute() {}
00119 Substitute(Consumer<ITEM, TAG>& cons) : Filter<ITEM, TAG>(cons) {}
00120 Substitute(const Substitutions<TAG>& changes) : changes(changes) {}
00121 Substitute(Consumer<ITEM, TAG>& cons, const Substitutions<TAG>& changes) :
00122 Filter<ITEM, TAG>(cons), changes(changes) {}
00123
00127 Substitutions<TAG>& substitutions() { return changes; }
00128
00132 const Substitutions<TAG>& substitutions() const { return changes; }
00133 };
00134
00141 template <typename ITEM, typename TAG>
00142 class UntaggedRemover : public Filter<ITEM, TAG>
00143 {
00144 protected:
00145 bool inverse;
00146
00147 virtual void consumeItemUntagged(const ITEM& item)
00148 {
00149 if (inverse)
00150 this->consumer->consume(item);
00151 }
00152 virtual void consumeItem(const ITEM& item, const OpSet<TAG>& tags)
00153 {
00154 if (inverse)
00155 {
00156 if (tags.empty())
00157 this->consumer->consume(item);
00158 } else {
00159 if (!tags.empty())
00160 this->consumer->consume(item, tags);
00161 }
00162 }
00163 virtual void consumeItemsUntagged(const OpSet<ITEM>& items)
00164 {
00165 if (inverse)
00166 this->consumer->consume(items);
00167 }
00168 virtual void consumeItemss(const OpSet<ITEM>& items, const OpSet<TAG>& tags)
00169 {
00170 if (inverse)
00171 {
00172 if (tags.empty())
00173 this->consumer->consume(items);
00174 } else {
00175 if (!tags.empty())
00176 this->consumer->consume(items, tags);
00177 }
00178 }
00179
00180 public:
00181 UntaggedRemover(bool inverse = false) : inverse(inverse) {}
00182 UntaggedRemover(Tagcoll::Consumer<ITEM, TAG>& cons, bool inverse = false)
00183 : Filter<ITEM, TAG>(cons), inverse(inverse) {}
00184 virtual ~UntaggedRemover() {}
00185 };
00186
00187
00191 template<typename ITEM>
00192 class UnfacetedRemover : public Filter<ITEM, std::string>
00193 {
00194 protected:
00195 std::string separator;
00196
00197 virtual void consumeItemUntagged(const ITEM& item);
00198 virtual void consumeItem(const ITEM& item, const OpSet<std::string>& tags);
00199 virtual void consumeItemsUntagged(const OpSet<ITEM>& items);
00200 virtual void consumeItems(const OpSet<ITEM>& items, const OpSet<std::string>& tags);
00201
00202 public:
00203 UnfacetedRemover() : separator("::") {}
00204 UnfacetedRemover(Consumer<ITEM, std::string>& cons)
00205 : Filter<ITEM, std::string>(cons), separator("::") {}
00206 UnfacetedRemover(const std::string& separator) : separator(separator) {}
00207 UnfacetedRemover(Consumer<ITEM, std::string>& cons, const std::string& separator)
00208 : Filter<ITEM, std::string>(cons), separator(separator) {}
00209 virtual ~UnfacetedRemover() {}
00210 };
00211
00212 };
00213
00214
00215 #endif