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/CardinalityStore.h>
00027 #include <vector>
00028
00029 namespace Tagcoll
00030 {
00031
00032
00033 template<class ITEM, class TAG>
00034 class HierarchyNode
00035 {
00036 protected:
00037
00038 TAG _tag;
00039 CardinalityStore<ITEM, TAG>* coll;
00040 std::vector<HierarchyNode<ITEM, TAG>*> children;
00041 OpSet<ITEM> items;
00042 HierarchyNode<ITEM, TAG>* _parent;
00043
00044 public:
00045 HierarchyNode(const TAG& tag, const CardinalityStore<ITEM, TAG>& coll)
00046 : _tag(tag), coll(new CardinalityStore<ITEM, TAG>(coll)), _parent(0) {}
00047 HierarchyNode(HierarchyNode<ITEM, TAG>* parent, const TAG& tag, const CardinalityStore<ITEM, TAG>& coll)
00048 : _tag(tag), coll(new CardinalityStore<ITEM, TAG>(coll)), _parent(parent) {}
00049 virtual ~HierarchyNode();
00050
00051 typedef typename std::vector<HierarchyNode<ITEM, TAG>*>::iterator iterator;
00052
00053
00054 const TAG& tag() const { return _tag; }
00055
00056
00057 TAG tag() { return _tag; }
00058
00059
00060 HierarchyNode<ITEM, TAG>* parent() const { return _parent; }
00061
00062
00063 virtual void expand() = 0;
00064
00065
00066 int size()
00067 {
00068 if (coll)
00069 expand();
00070 return children.size();
00071 }
00072
00073 iterator begin()
00074 {
00075 if (coll)
00076 expand();
00077 return children.begin();
00078 }
00079
00080 iterator end()
00081 {
00082 if (coll)
00083 expand();
00084 return children.end();
00085 }
00086
00087
00088 HierarchyNode<ITEM, TAG>* operator[](int idx)
00089 {
00090 if (coll)
00091 expand();
00092 return children[idx];
00093 }
00094
00095
00096 const OpSet<ITEM>& getItems()
00097 {
00098 if (coll)
00099 expand();
00100 return items;
00101 }
00102 };
00103
00104
00105
00106 template<class ITEM, class TAG>
00107 class SmartHierarchyNode : public HierarchyNode<ITEM, TAG>
00108 {
00109 protected:
00110 OpSet<ITEM> unexpandedItems;
00111
00112
00113
00114 int flattenThreshold;
00115
00116
00117 virtual void expand();
00118
00119 public:
00120 SmartHierarchyNode(const TAG& tag, const CardinalityStore<ITEM, TAG>& coll, int flattenThreshold = 0)
00121 throw () :
00122 HierarchyNode<ITEM, TAG>(tag, coll),
00123 flattenThreshold(flattenThreshold) {}
00124
00125 SmartHierarchyNode(
00126 HierarchyNode<ITEM, TAG>* parent,
00127 const TAG& tag,
00128 const CardinalityStore<ITEM, TAG>& coll,
00129 int flattenThreshold = 0)
00130 throw () :
00131 HierarchyNode<ITEM, TAG>(parent, tag, coll.getChildCollection(tag)),
00132 flattenThreshold(flattenThreshold)
00133 {
00134 OpSet<TAG> tags; tags += tag;
00135 unexpandedItems = coll.getItemsExactMatch(tags);
00136 }
00137
00138 virtual ~SmartHierarchyNode() {}
00139 };
00140
00141
00142
00143 template<class ITEM, class TAG>
00144 class CleanSmartHierarchyNode : public SmartHierarchyNode<ITEM, TAG>
00145 {
00146 protected:
00147
00148 virtual void expand();
00149
00150 TAG setTag(const TAG& tag) { return this->_tag = tag; }
00151 HierarchyNode<ITEM, TAG>* setParent(HierarchyNode<ITEM, TAG>* parent) { return this->_parent = parent; }
00152
00153 public:
00154 CleanSmartHierarchyNode(const TAG& tag, const CardinalityStore<ITEM, TAG>& coll, int flattenThreshold = 0)
00155 : SmartHierarchyNode<ITEM, TAG>(tag, coll, flattenThreshold) {}
00156 CleanSmartHierarchyNode(HierarchyNode<ITEM, TAG>* parent, const TAG& tag, const CardinalityStore<ITEM, TAG>& coll, int flattenThreshold = 0)
00157 : SmartHierarchyNode<ITEM, TAG>(parent, tag, coll, flattenThreshold) {}
00158 virtual ~CleanSmartHierarchyNode() {}
00159 };
00160
00161 };
00162
00163
00164 #endif