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,2004,2005  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/CardinalityStore.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     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     // Get the node tag (const version)
00054     const TAG& tag() const { return _tag; }
00055 
00056     // Get the node tag
00057     TAG tag() { return _tag; }
00058 
00059     // Get the parent of this node (0 if it is the root node)
00060     HierarchyNode<ITEM, TAG>* parent() const { return _parent; }
00061 
00062     // Expand the collection in the children of this node
00063     virtual void expand() = 0;
00064 
00065     // Get the number of child nodes
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     // Get a child node by index
00088     HierarchyNode<ITEM, TAG>* operator[](int idx)
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()
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     OpSet<ITEM> unexpandedItems;
00111 
00112     // Threshold of child items below which the child hierarchy is flattened
00113     // and they all become children of this node
00114     int flattenThreshold;
00115     
00116     // Expand the collection in the children of this node
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 // SmartHierarchyNode which also does merging of equivalent tags
00143 template<class ITEM, class TAG>
00144 class CleanSmartHierarchyNode : public SmartHierarchyNode<ITEM, TAG>
00145 {
00146 protected:
00147     // Expand the collection in the children of this node
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 // vim:set ts=4 sw=4:
00164 #endif

Generated on Mon Jun 19 18:13:39 2006 for libtagcoll by  doxygen 1.4.6