Page principale | Hiérarchie des classes | Liste par ordre alphabétique | Liste des composants | Liste des fichiers | Composants | Déclarations | Pages associées

MyTree.h

Aller à la documentation de ce fichier.
00001 /* ***********************************************************************************
00002         Writer:         Sebastien Bloc
00003         Copyright:      2004
00004         eMail:          sebastien.bloc@free.fr
00005         URL:            http://mignonsoft.free.fr
00006 
00007         This program is free software; you can redistribute it and/or
00008         modify it under the terms of the GNU General Public License
00009         as published by the Free Software Foundation; either version 2
00010         of the License, or (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         http://www.gnu.org/copyleft/gpl.html
00017 *********************************************************************************** */
00018 
00023 #ifndef INCLUDE_MY_TREE_INCLUDE
00024 #define INCLUDE_MY_TREE_INCLUDE
00025 
00026 #include "MyList.h"
00027 
00036 template <class T> 
00037 class MyTreeNode
00038 {
00039 public:
00040         T *elem;                                                        // element de la liste 
00041         MyTreeNode *parent;
00042         MyList<MyTreeNode *> childs;      // enfants 
00043 
00044 private:
00045         void Init()
00046         {
00047                 elem = NULL;
00048                 parent=NULL;
00049         }
00050 
00051 public:
00052         MyTreeNode() { Init(); }
00053 
00054         MyTreeNode(T &_elem) 
00055         {
00056                 Init();
00057                 elem = new T;
00058                 if (!elem) throw MyExceptionMemory("MyTreeNode","constructor",sizeof(T));
00059                 *elem=_elem; //operator=(T &source) de la class appelé
00060         }
00061 
00062         ~MyTreeNode() 
00063         { 
00064                 if (elem) delete elem; 
00065         }
00066 
00067         MyTreeNode<T> * operator [] (int num)
00068         {
00069                 return childs[num];
00070         }
00071 
00072         T * Elems(int num)
00073         {
00074                 return childs[num]->elem;
00075         }
00076 
00077         void operator=(MyTreeNode &source)
00078         {
00079                 if (source.elem) 
00080                 {
00081                         elem = new T;
00082                         if (!elem) throw CMyExceptionMemory("MyTreeNode","operator=",sizeof(T));
00083                         *elem=*source.elem;
00084                 }
00085                 else elem=NULL;
00086                 parent=source.parent;
00087                 childs=source.childs;
00088         }
00089 };
00090 
00139 template <class T> 
00140 class MyTree
00141 {
00142 private:
00143         MyTreeNode<T> *m_root;
00144         MyTreeNode<T> *m_current;
00145 
00146 private:
00147         void Init()
00148         {
00149                 m_root = NULL;
00150                 m_current = NULL;
00151         }
00152 
00153 public:
00154         MyTreeNode<T> * GetRoot() { return m_root; }
00155         MyTreeNode<T> * GetCurrent() { return m_current; }
00156 
00157         MyTree()
00158         {
00159                 Init();
00160         }
00161 
00162         ~MyTree()
00163         {
00164                 SuprAll();
00165         }
00166 
00167         BOOL SuprAll()
00168         {
00169                 Init();
00170                 return SuprNode(m_root,TRUE);
00171         }
00172 
00173         BOOL SuprNode(MyTreeNode<T> *node,BOOL recursif=FALSE)
00174         {
00175                 if (!node) return FALSE;
00176                 if (node->parent) // s'il y a un parent detruir son fils en question
00177                 {
00178                         BOOL isFind = FALSE;
00179                         for (node->parent->childs=0;node->parent->childs.More();node->parent->childs++)
00180                         {
00181                                 if (node->parent->childs.GetElem()==node) // j'ai trouvé le fils chez le pere
00182                                 {
00183                                         node->parent->childs.SuprCurrent();
00184                                         isFind=TRUE;
00185                                         break;
00186                                 }
00187                         }
00188                         if (!isFind) // y'a un pb, un pere mais pas de fils dans ce pere !
00189                                 throw MyExceptionMemory("MyTree","SuprNode",sizeof(MyTreeNode<T>));
00190                 }
00191                 if (recursif) // en recursif: destruction de tout ces fils
00192                 {
00193                         for (node->childs=0;node->childs.More();node->childs++)
00194                                 if (!SuprNode(node->childs.GetElem(),TRUE)) return FALSE;
00195                 }
00196                 delete node;
00197                 return TRUE;
00198         }
00199 
00200         MyTreeNode<T> *GoToChild(int numChild,MyTreeNode<T> *parent=NULL)
00201         {
00202                 MyTreeNode<T> *current = GetChild(numChild,parent);
00203                 if (current) m_current=current;
00204                 return current;
00205         }
00206 
00207         MyTreeNode<T> * GoTo(MyTreeNode<T> *node)
00208         {
00209                 MyTreeNode<T> * oldCurrent;
00210                 oldCurrent = m_current;
00211                 m_current=node;
00212                 return oldCurrent;
00213         }
00214 
00215         MyTreeNode<T> *GetChild(int numChild,MyTreeNode<T> *parent=NULL)
00216         {
00217                 if (!parent) 
00218                 {
00219                         parent = m_current;
00220                         if (!parent) return NULL;
00221                 }
00222                 return parent->childs[numChild];
00223         }
00224 
00225         MyTreeNode<T> *GoToParent(MyTreeNode<T> *parent=NULL)
00226         {
00227                 MyTreeNode<T> *current = GetParent(parent);
00228                 if (current) m_current=current;
00229                 return current;
00230         }
00231 
00232         MyTreeNode<T> *GetParent(MyTreeNode<T> *parent=NULL)
00233         {
00234                 if (!parent) 
00235                 {
00236                         parent = m_current;
00237                         if (!parent) return NULL;
00238                 }
00239                 return parent->parent;
00240         }
00241 
00242         MyTreeNode<T> * Add(T& elem,MyTreeNode<T> *parent=NULL)
00243         {
00244                 MyTreeNode<T> *node = new MyTreeNode<T>(elem); // Creation d'un nouveau noeud
00245                 if (!parent) parent = m_current;    // Attacher ce noeuf a son parent
00246                 if (parent)                      // si premier alors devient root
00247                 {
00248                         node->parent = parent;
00249                         parent->childs+=node;
00250                 }
00251                 else { m_current = m_root = node; }
00252                 return node;
00253         }
00254 };
00255 
00256 #endif

Généré le Thu Apr 8 18:58:43 2004 pour SFC par doxygen 1.3.6