00001 #ifndef TAGCOLL_SMARTHIERARCHY_H
00002 #define TAGCOLL_SMARTHIERARCHY_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #pragma interface
00025
00026 #include <tagcoll/TagCollection.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 TagCollection<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 TagCollection<ITEM, TAG>& coll)
00046 throw () : _tag(tag), coll(new TagCollection<ITEM, TAG>(coll)), _parent(0) {}
00047 HierarchyNode(HierarchyNode<ITEM, TAG>* parent, const TAG& tag, const TagCollection<ITEM, TAG>& coll)
00048 throw () : _tag(tag), coll(new TagCollection<ITEM, TAG>(coll)), _parent(parent) {}
00049 virtual ~HierarchyNode() throw ();
00050
00051 typedef typename std::vector<HierarchyNode<ITEM, TAG>*>::iterator iterator;
00052
00053
00054 const TAG& tag() const throw () { return _tag; }
00055
00056
00057 TAG tag() throw () { return _tag; }
00058
00059
00060 HierarchyNode<ITEM, TAG>* parent() const throw () { return _parent; }
00061
00062
00063 virtual void expand() throw () = 0;
00064
00065
00066 int size() throw ()
00067 {
00068 if (coll)
00069 expand();
00070 return children.size();
00071 }
00072
00073 iterator begin() throw ()
00074 {
00075 if (coll)
00076 expand();
00077 return children.begin();
00078 }
00079
00080 iterator end() throw ()
00081 {
00082 if (coll)
00083 expand();
00084 return children.end();
00085 }
00086
00087
00088 HierarchyNode<ITEM, TAG>* operator[](int idx) throw ()
00089 {
00090 if (coll)
00091 expand();
00092 return children[idx];
00093 }
00094
00095
00096 const OpSet<ITEM>& getItems() throw ()
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 using HierarchyNode<ITEM, TAG>::coll;
00111 using HierarchyNode<ITEM, TAG>::items;
00112 using HierarchyNode<ITEM, TAG>::children;
00113
00114
00115
00116 int flattenThreshold;
00117
00118
00119 virtual void expand() throw ();
00120
00121 public:
00122 SmartHierarchyNode(const TAG& tag, const TagCollection<ITEM, TAG>& coll, int flattenThreshold = 0)
00123 throw () : HierarchyNode<ITEM, TAG>(tag, coll), flattenThreshold(flattenThreshold) {}
00124 SmartHierarchyNode(HierarchyNode<ITEM, TAG>* parent, const TAG& tag, const TagCollection<ITEM, TAG>& coll, int flattenThreshold = 0)
00125 throw () : HierarchyNode<ITEM, TAG>(parent, tag, coll), flattenThreshold(flattenThreshold) {}
00126 };
00127
00128
00129
00130 template<class ITEM, class TAG>
00131 class CleanSmartHierarchyNode : public SmartHierarchyNode<ITEM, TAG>
00132 {
00133 protected:
00134 using HierarchyNode<ITEM, TAG>::coll;
00135 using HierarchyNode<ITEM, TAG>::items;
00136 using HierarchyNode<ITEM, TAG>::children;
00137 using SmartHierarchyNode<ITEM, TAG>::_tag;
00138 using SmartHierarchyNode<ITEM, TAG>::_parent;
00139 using SmartHierarchyNode<ITEM, TAG>::flattenThreshold;
00140
00141
00142 virtual void expand() throw ();
00143
00144 TAG setTag(const TAG& tag) throw () { return _tag = tag; }
00145 HierarchyNode<ITEM, TAG>* setParent(HierarchyNode<ITEM, TAG>* parent) throw () { return _parent = parent; }
00146
00147 public:
00148 CleanSmartHierarchyNode(const TAG& tag, const TagCollection<ITEM, TAG>& coll, int flattenThreshold = 0)
00149 throw () : SmartHierarchyNode<ITEM, TAG>(tag, coll, flattenThreshold) {}
00150 CleanSmartHierarchyNode(HierarchyNode<ITEM, TAG>* parent, const TAG& tag, const TagCollection<ITEM, TAG>& coll, int flattenThreshold = 0)
00151 throw () : SmartHierarchyNode<ITEM, TAG>(parent, tag, coll, flattenThreshold) {}
00152 };
00153
00154
00155 template<class ITEM, class TAG>
00156 class UniqueHierarchyNode : public HierarchyNode<ITEM, TAG>
00157 {
00158 protected:
00159
00160 virtual void expand() throw () = 0;
00161
00162 public:
00163 UniqueHierarchyNode(const TAG& tag, const TagCollection<ITEM, TAG>& coll)
00164 throw () : HierarchyNode<ITEM, TAG>(tag, coll) {}
00165 UniqueHierarchyNode(HierarchyNode<ITEM, TAG>* parent, const TAG& tag, const TagCollection<ITEM, TAG>& coll)
00166 throw () : HierarchyNode<ITEM, TAG>(parent, tag, coll) {}
00167 };
00168
00169 };
00170
00171
00172 #endif