Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

CTree.h

Go to the documentation of this file.
00001 /*
00002  * CTree.h
00003  * $Id: CTree.h,v 1.7 2001/09/28 11:06:08 mjanich Exp $
00004  *
00005  * Copyright (C) 2001 Markus Janich
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 
00023 
00024 
00025 #ifndef CTREE_H
00026 #define CTREE_H
00027 
00028 
00029 // Own
00031 #include "CList.h"
00032 
00033 
00034 // System
00036 #include <iostream.h>
00037 
00038 
00039 // forward declarations
00041 class CTreeTraverserBase;
00042 
00043 
00044 
00047 class CTreeNode {
00048 public:
00049 
00050   enum Where { Front, End }; 
00053   CTreeNode() 
00054     : m_pcParent(0)
00055     {
00056 #ifdef DEBUG_TREE
00057       cerr << "called CTreeNode<NodeDataType>::CTreeNode()" << endl;
00058 #endif
00059       /* nothing to do */};
00060 
00061 
00063   CTreeNode(const CTreeNode &cSource);
00064 
00066   virtual ~CTreeNode();
00067 
00068 
00071   virtual CTreeNode *append(CTreeNode *pcNode, Where w=End) {
00072     return append(this, pcNode, w);
00073   };
00074 
00078   virtual CTreeNode *append(CTreeNode *pcWhere,
00079                             CTreeNode *pcAppend, Where w=End) {
00080     if (pcWhere) {
00081       switch (w) {
00082       case End: 
00083         pcWhere->m_cChildrenList.insertAsLast(pcAppend);
00084         break;
00085       case Front:
00086         pcWhere->m_cChildrenList.insertAsFirst(pcAppend);
00087         break;
00088       }
00089       pcAppend->m_pcParent = pcWhere;
00090 
00091       return pcAppend;
00092     }
00093     return 0;
00094   };
00095 
00099   virtual CTreeNode *insert(CTreeNode *pcWhere,
00100                             CTreeNode *pcInsert);
00101 
00102 
00103   // FIXME: Should be removed later, because of conflicts with traversers
00105   /** Remove the specified node from the tree. */
00106   virtual void remove(CTreeNode *pcRemove);
00107 
00109   virtual void remove(CTreeTraverserBase *pcTraverser);
00110 
00111 
00113   virtual CTreeNode *getParent() const { return m_pcParent; };
00114 
00116   virtual int numChildren() const {
00117     return m_cChildrenList.getNumObjects();
00118   };
00119 
00121   virtual const CList<CTreeNode> &getChildrenList() const {
00122     return m_cChildrenList;
00123   }
00124 
00126   virtual CTreeNode &operator=(const CTreeNode &cSource);
00127 
00131   virtual CTreeNode &operator[](int i) const;
00132 
00134   virtual bool isEqual(const CTreeNode *pcNode) const; 
00135 
00137   virtual void printTree(ostream &out=cout) const;
00138 
00139   friend ostream& operator<<(ostream &out, CTreeNode *pcTreeNode);
00140 
00141 
00142 protected:
00143 
00145   virtual void print(ostream &out) const;
00146 
00147 
00149   // DATA  //
00151 
00152   CTreeNode *m_pcParent;
00153   CList<CTreeNode> m_cChildrenList;
00154 };
00155 
00156 
00157 
00161 
00162 
00163 
00167 class CTreeTraverserBase {
00168   friend class CTreeNode;
00169 
00170 public:
00171 
00172   CTreeTraverserBase() {};
00173   CTreeTraverserBase(CTreeNode *pcNode) {};
00174   virtual ~CTreeTraverserBase() {};
00175 
00176   virtual bool atStart() = 0;
00177   virtual bool atEnd() = 0;
00178 
00179   virtual const CTreeNode *operator++() = 0;
00180   virtual const CTreeNode *operator++(int dummy) = 0;
00181 
00182   //virtual const CTreeNode *operator--() = 0;
00183   //virtual const CTreeNode *operator--(int dummy) = 0;
00184 
00185   virtual CTreeNode *operator*() = 0;
00186 
00187 
00188 protected:
00189 
00190   virtual CTreeNode *getCurrentNode() const = 0;
00191   virtual void removeCurrentNode() = 0;
00192 };
00193 
00194 
00195 
00200 
00201 // FIXME:: They better should do their work directly
00202 //         on the data of the tree instead of making
00203 //         a list
00204 
00207 class CDepthFirstTraverser : public CTreeTraverserBase {
00208 public:
00209 
00210   CDepthFirstTraverser(CTreeNode *pcNode);
00211   virtual ~CDepthFirstTraverser() {};
00212 
00213   virtual bool atStart();
00214   virtual bool atEnd();
00215 
00216   virtual const CTreeNode *operator++();
00217   virtual const CTreeNode *operator++(int dummy);
00218 
00219   //virtual const CTreeNode *operator--();
00220   //virtual const CTreeNode *operator--(int dummy);
00221 
00222   virtual CTreeNode *operator*() {
00223     return getCurrentNode();
00224   }
00225 
00226 
00227 protected:
00228 
00229   virtual CTreeNode *getCurrentNode() const;
00230   virtual void removeCurrentNode();
00231 
00232 
00233 private:
00234 
00235   void parseSubTree(CTreeNode *pcNode);
00236 
00237   CList<CTreeNode> m_cNodeList;
00238   CListContainer<CTreeNode> *m_pcCurrentNode;
00239   bool m_fAtEnd, m_fAtStart;
00240   int m_nLastOp;  // 0: ++, 1: --
00241 };
00242 
00243 
00244 
00247 class CBreathFirstTraverser : public CTreeTraverserBase {
00248 public:
00249 
00250   CBreathFirstTraverser(CTreeNode *pcNode);
00251   virtual ~CBreathFirstTraverser() {};
00252 
00253   virtual bool atStart();
00254   virtual bool atEnd();
00255 
00256   virtual const CTreeNode *operator++();
00257   virtual const CTreeNode *operator++(int dummy);
00258 
00259   //virtual const CTreeNode *operator--();
00260   //virtual const CTreeNode *operator--(int dummy);
00261 
00262   virtual CTreeNode *operator*() {
00263     return getCurrentNode();
00264   }
00265 
00266 
00267 protected:
00268 
00269   virtual CTreeNode *getCurrentNode() const;
00270   virtual void removeCurrentNode();
00271 
00272 
00273 private:
00274 
00275   CList<CTreeNode> m_cNodeList;
00276   CListContainer<CTreeNode> *m_pcCurrentNode;
00277   bool m_fAtEnd, m_fAtStart;
00278   int m_nLastOp;  // 0: ++, 1: --
00279 };
00280 
00281 
00282 #endif // CTREE_H

Generated at Thu Oct 4 17:17:25 2001 for QGLViewer by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001