SmartHierarchy.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_SMARTHIERARCHY_H
00002 #define TAGCOLL_SMARTHIERARCHY_H
00003 
00008 /* 
00009  * Copyright (C) 2003--2006  Enrico Zini <enrico@debian.org>
00010  *
00011  * This library is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2.1 of the License, or (at your option) any later version.
00015  *
00016  * This library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this library; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00024  */
00025 
00026 #include <tagcoll/coll/base.h>
00027 #include <vector>
00028 #include <set>
00029 
00030 namespace tagcoll
00031 {
00032 
00033 // Base class for the auto-expanding tree nodes
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     // Tag name
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     // Get the node tag (const version)
00060     const TAG& tag() const { return _tag; }
00061 
00062     // Get the node tag
00063     TAG tag() { return _tag; }
00064 
00065     // Get the parent of this node (0 if it is the root node)
00066     HierarchyNode<COLL>* parent() const { return _parent; }
00067 
00068     // Expand the collection in the children of this node
00069     virtual void expand() = 0;
00070 
00071     // Get the number of child nodes
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     // Get a child node by index
00094     HierarchyNode<COLL>* operator[](int idx)
00095     {
00096         if (coll)
00097             expand();
00098         return children[idx];
00099     }
00100 
00101     // Get the set of items present in this node
00102     const std::set<ITEM>& getItems()
00103     {
00104         if (coll)
00105             expand();
00106         return items;
00107     }
00108 };
00109 
00110 // Hierarchy of items where information is replicated to acheive intuitive
00111 // navigability of the resulting structure
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     // Threshold of child items below which the child hierarchy is flattened
00124     // and they all become children of this node
00125     int flattenThreshold;
00126     
00127     // Expand the collection in the children of this node
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 // SmartHierarchyNode which also does merging of equivalent tags
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     // Expand the collection in the children of this node
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 // vim:set ts=4 sw=4:
00211 #endif

Generated on Sat Dec 9 04:13:10 2006 for libtagcoll by  doxygen 1.5.1