Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

SmartHierarchy.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_SMARTHIERARCHY_H 00002 #define TAGCOLL_SMARTHIERARCHY_H 00003 00004 /* 00005 * Auto-expanding trees and smart hierarchy interface 00006 * 00007 * Copyright (C) 2003 Enrico Zini <enrico@debian.org> 00008 * 00009 * This library is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with this library; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 */ 00023 00024 #pragma interface 00025 00026 #include <tagcoll/TagCollection.h> 00027 #include <vector> 00028 00029 namespace Tagcoll 00030 { 00031 00032 // Base class for the auto-expanding tree nodes 00033 template<class ITEM, class TAG> 00034 class HierarchyNode 00035 { 00036 protected: 00037 // Tag name 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 // Get the node tag (const version) 00054 const TAG& tag() const throw () { return _tag; } 00055 00056 // Get the node tag 00057 TAG tag() throw () { return _tag; } 00058 00059 // Get the parent of this node (0 if it is the root node) 00060 HierarchyNode<ITEM, TAG>* parent() const throw () { return _parent; } 00061 00062 // Expand the collection in the children of this node 00063 virtual void expand() throw () = 0; 00064 00065 // Get the number of child nodes 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 // Get a child node by index 00088 HierarchyNode<ITEM, TAG>* operator[](int idx) throw () 00089 { 00090 if (coll) 00091 expand(); 00092 return children[idx]; 00093 } 00094 00095 // Get the set of items present in this node 00096 const OpSet<ITEM>& getItems() throw () 00097 { 00098 if (coll) 00099 expand(); 00100 return items; 00101 } 00102 }; 00103 00104 // Hierarchy of items where information is replicated to acheive intuitive 00105 // navigability of the resulting structure 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 // Threshold of child items below which the child hierarchy is flattened 00115 // and they all become children of this node 00116 int flattenThreshold; 00117 00118 // Expand the collection in the children of this node 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 // SmartHierarchyNode which also does merging of equivalent tags 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 // Expand the collection in the children of this node 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 // Hierarchy of items where each item appears only once 00155 template<class ITEM, class TAG> 00156 class UniqueHierarchyNode : public HierarchyNode<ITEM, TAG> 00157 { 00158 protected: 00159 // Expand the collection in the children of this node 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 // vim:set ts=4 sw=4: 00172 #endif

Generated on Sun Aug 15 19:05:32 2004 for libtagcoll by doxygen 1.3.8