00001 #ifndef TAGCOLL_SMARTHIERARCHY_H
00002 #define TAGCOLL_SMARTHIERARCHY_H
00003
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <tagcoll/coll/base.h>
00027 #include <vector>
00028 #include <set>
00029
00030 namespace tagcoll
00031 {
00032
00033
00034 template<typename COLL>
00035 class HierarchyNode
00036 {
00037 protected:
00038 typedef typename coll::coll_traits<COLL>::item_type ITEM;
00039 typedef typename coll::coll_traits<COLL>::tag_type TAG;
00040 typedef typename coll::coll_traits<COLL>::itemset_type ITEMSET;
00041 typedef typename coll::coll_traits<COLL>::tagset_type TAGSET;
00042
00043
00044 TAG _tag;
00045 COLL* coll;
00046 std::vector<HierarchyNode<COLL>*> children;
00047 ITEMSET items;
00048 HierarchyNode<COLL>* _parent;
00049
00050 public:
00051 HierarchyNode(const TAG& tag, const COLL& coll)
00052 : _tag(tag), coll(new COLL(coll)), _parent(0) {}
00053 HierarchyNode(HierarchyNode<COLL>* parent, const TAG& tag, const COLL& coll)
00054 : _tag(tag), coll(new COLL(coll)), _parent(parent) {}
00055 virtual ~HierarchyNode();
00056
00057 typedef typename std::vector<HierarchyNode<COLL>*>::iterator iterator;
00058
00059
00060 const TAG& tag() const { return _tag; }
00061
00062
00063 TAG tag() { return _tag; }
00064
00065
00066 HierarchyNode<COLL>* parent() const { return _parent; }
00067
00068
00069 virtual void expand() = 0;
00070
00071
00072 int size()
00073 {
00074 if (coll)
00075 expand();
00076 return children.size();
00077 }
00078
00079 iterator begin()
00080 {
00081 if (coll)
00082 expand();
00083 return children.begin();
00084 }
00085
00086 iterator end()
00087 {
00088 if (coll)
00089 expand();
00090 return children.end();
00091 }
00092
00093
00094 HierarchyNode<COLL>* operator[](int idx)
00095 {
00096 if (coll)
00097 expand();
00098 return children[idx];
00099 }
00100
00101
00102 const std::set<ITEM>& getItems()
00103 {
00104 if (coll)
00105 expand();
00106 return items;
00107 }
00108 };
00109
00110
00111
00112 template<typename COLL>
00113 class SmartHierarchyNode : public HierarchyNode<COLL>
00114 {
00115 protected:
00116 typedef typename HierarchyNode<COLL>::ITEM ITEM;
00117 typedef typename HierarchyNode<COLL>::TAG TAG;
00118 typedef typename HierarchyNode<COLL>::ITEMSET ITEMSET;
00119 typedef typename HierarchyNode<COLL>::TAGSET TAGSET;
00120
00121 typename HierarchyNode<COLL>::ITEMSET unexpandedItems;
00122
00123
00124
00125 int flattenThreshold;
00126
00127
00128 virtual void expand();
00129
00130 public:
00131 SmartHierarchyNode(const TAG& tag, const COLL& coll, int flattenThreshold = 0)
00132 throw () :
00133 HierarchyNode<COLL>(tag, coll),
00134 flattenThreshold(flattenThreshold) {}
00135
00136 SmartHierarchyNode(
00137 HierarchyNode<COLL>* parent,
00138 const TAG& tag,
00139 const COLL& coll,
00140 int flattenThreshold = 0)
00141 throw () :
00142 HierarchyNode<COLL>(parent, tag, coll),
00143 flattenThreshold(flattenThreshold)
00144 {
00145 std::set<TAG> tags; tags.insert(tag);
00146 unexpandedItems = coll.getItemsExactMatch(tags);
00147 }
00148
00149 virtual ~SmartHierarchyNode() {}
00150 };
00151
00152 template<typename COLL>
00153 inline HierarchyNode<COLL>* smartHierarchyNode(const typename coll::coll_traits<COLL>::tag_type& tag, const COLL& coll, int flattenThreshold)
00154 {
00155 return new SmartHierarchyNode<COLL>(tag, coll, flattenThreshold);
00156 }
00157 template<typename COLL>
00158 inline HierarchyNode<COLL>* smartHierarchyNode(HierarchyNode<COLL>* parent, const typename coll::coll_traits<COLL>::tag_type& tag, const COLL& coll, int flattenThreshold)
00159 {
00160 return new SmartHierarchyNode<COLL>(parent, tag, coll, flattenThreshold);
00161 }
00162
00163 template<typename TAG>
00164 TAG mergeTags(const TAG& tag1, const TAG& tag2)
00165 {
00166 return tag1;
00167 }
00168
00169 template<>
00170 std::string mergeTags(const std::string& tag1, const std::string& tag2);
00171
00172
00173
00174 template<typename COLL>
00175 class CleanSmartHierarchyNode : public SmartHierarchyNode<COLL>
00176 {
00177 protected:
00178 typedef typename SmartHierarchyNode<COLL>::ITEM ITEM;
00179 typedef typename SmartHierarchyNode<COLL>::TAG TAG;
00180 typedef typename SmartHierarchyNode<COLL>::ITEMSET ITEMSET;
00181 typedef typename SmartHierarchyNode<COLL>::TAGSET TAGSET;
00182
00183
00184 virtual void expand();
00185
00186 TAG setTag(const TAG& tag) { return this->_tag = tag; }
00187 HierarchyNode<COLL>* setParent(HierarchyNode<COLL>* parent) { return this->_parent = parent; }
00188
00189 public:
00190 CleanSmartHierarchyNode(const TAG& tag, const COLL& coll, int flattenThreshold = 0)
00191 : SmartHierarchyNode<COLL>(tag, coll, flattenThreshold) {}
00192 CleanSmartHierarchyNode(HierarchyNode<COLL>* parent, const TAG& tag, const COLL& coll, int flattenThreshold = 0)
00193 : SmartHierarchyNode<COLL>(parent, tag, coll, flattenThreshold) {}
00194 virtual ~CleanSmartHierarchyNode() {}
00195 };
00196
00197 template<typename COLL>
00198 inline HierarchyNode<COLL>* cleanSmartHierarchyNode(const typename coll::coll_traits<COLL>::tag_type& tag, const COLL& coll, int flattenThreshold)
00199 {
00200 return new CleanSmartHierarchyNode<COLL>(tag, coll, flattenThreshold);
00201 }
00202 template<typename COLL>
00203 inline HierarchyNode<COLL>* cleanSmartHierarchyNode(HierarchyNode<COLL>* parent, const typename coll::coll_traits<COLL>::tag_type& tag, const COLL& coll, int flattenThreshold)
00204 {
00205 return new CleanSmartHierarchyNode<COLL>(parent, tag, coll, flattenThreshold);
00206 }
00207
00208 }
00209
00210
00211 #endif