Wavelet and Image class library  1.3.2
NTree.hh
Go to the documentation of this file.
00001 /*
00002  * class NTree
00003  *
00004  * $Date$
00005  * $Revision$
00006  *
00007  */
00008 
00009 #ifndef NTREE_HH__
00010 #define NTREE_HH__
00011 
00012 #include "WImage/miscdefs.h"
00013 #include <stdexcept>
00014 
00015 #ifndef _WIN32_WCE
00016 #include <iostream>
00017 #endif
00018 
00019 
00028 template < class Type > class 
00029 NTree 
00030 {
00031 public:
00037   NTree (int nChildren, NTree < Type > *parent = NULL, int position = -1);
00044   NTree (int nChildren, Type &data, 
00045          int position = -1, NTree < Type > *parent = NULL);
00047   ~NTree (void);
00050   inline bool isRoot (void) const { return m_parent == NULL; }
00053   inline int position (void) const { return m_position; }
00056   inline int aryness (void) const { return m_aryness; }
00059   inline bool hasLeftSibling (void) const { 
00060     return !isRoot () && m_position > 0 
00061       && m_parent->hasChildAt (m_position - 1); 
00062   }
00065   inline bool hasRightSibling (void) const { 
00066     return !isRoot () && m_position < m_aryness - 1 
00067       && m_parent->hasChildAt (m_position + 1); 
00068   }
00072   inline bool hasChildAt (int pos) const { 
00073     return indexOK (pos) && m_children[pos] != NULL; 
00074   }
00077   inline bool hasChildren (void) const {
00078     int i; for (i = 0; i < m_aryness && !hasChildAt (i); i++) { }
00079     return i != m_aryness;
00080   }
00083   int card (void) const;
00088   bool equals (const NTree &tree) const;
00091   NTree < Type > *clone (void) const; 
00094   inline void destroyAt (int pos) { DELETE (m_children[pos]); }
00101   void appendAt (int pos, NTree < Type > *tree); 
00106   void appendAt (int pos, const Type &data); 
00109   inline Type &data (void) { return m_data; }
00114   inline NTree < Type > &childAt (int pos) { 
00115     if (!indexOK (pos) || m_children[pos] == NULL)
00116       {
00117         throw std::invalid_argument ("childAt: no child at this position");
00118       }
00119     return *m_children[pos];
00120   } 
00124   NTree < Type > &leftSibling (void) {
00125     if (!hasLeftSibling ())
00126       {
00127         throw std::invalid_argument ("leftSibling: no left sibling");
00128       }
00129     return *m_parent->m_children[m_position - 1];
00130   }
00134   NTree < Type > &rightSibling (void) {
00135     if (!hasRightSibling ())
00136       {
00137         throw std::invalid_argument ("rightSibling: no right sibling");
00138       }
00139     return *m_parent->m_children[m_position + 1];
00140   }
00141   
00142 protected:
00147   void init (int nChildren, int position, NTree < Type > *parent);
00150   void appendNGenerations (int levels); 
00154   inline bool indexOK (int pos) const { return pos >= 0 && pos < m_aryness; }
00161   void copyLeaves (NTree < Type > &dst, const NTree < Type > &src) const;
00162   
00164   NTree < Type > **m_children;
00165 
00167   NTree < Type > *m_parent;
00168 
00170   Type m_data;
00171 
00173   int m_aryness;
00175   int m_position;
00176 }; /* class NTree */
00177 
00178 #include "../NTree.cc"
00179 
00182 #endif /* NTREE_HH__ */
00183 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines