Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

cstreend.h

00001 /*
00002     Copyright (C) 2000 by Norman Krämer
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_CSTREENODE_H__
00020 #define __CS_CSTREENODE_H__
00021 
00022 #include "csutil/csvector.h"
00023 
00027 class csTreeNode
00028 {
00029  public:
00030 
00031   bool IsLeaf ()
00032   { return children.Length () == 0; }
00033 
00034   void RemoveChild (csTreeNode *child)
00035   { int idx = children.Find (child); if (idx != -1) children.Delete (idx); }
00036 
00037   void AddChild (csTreeNode *child)
00038   { children.Push (child); child->parent = this; }
00039 
00040   csTreeNode (csTreeNode *theParent=NULL)
00041   { parent=theParent; if (parent) parent->children.Push (this); }
00042 
00043   virtual ~csTreeNode ()
00044   {
00045         int i;
00046     for(i=children.Length ()-1; i>=0; i--)
00047       delete (csTreeNode*)children.Get (i);
00048     if (parent)
00049       parent->RemoveChild (this);
00050   }
00051 
00059   csTreeNode *DSF (bool (*TreeFunc)(csTreeNode *node, csSome param, bool stopOnSuccess),
00060                    bool (*SelBranch)(csTreeNode *node), csSome param, bool stopOnSuccess)
00061   {
00062     csTreeNode *foundNode = NULL;
00063     int i=0;
00064     bool dive;
00065     if (TreeFunc (this, param, stopOnSuccess))
00066       foundNode = this;
00067     while (i<children.Length () && !(foundNode && stopOnSuccess))
00068     {
00069       dive = (SelBranch == NULL) || SelBranch ((csTreeNode*)children.Get (i));
00070       if (dive)
00071         foundNode = ((csTreeNode*)children.Get (i))->DSF (TreeFunc, SelBranch,
00072                                                           param, stopOnSuccess);
00073         i++;
00074     }
00075     return foundNode;
00076   }
00077 
00085   csTreeNode *BSF (bool (*TreeFunc)(csTreeNode *node, csSome param, bool stopOnSuccess),
00086                    bool (*SelBranch)(csTreeNode *node), csSome param, bool stopOnSuccess)
00087   {
00088     csTreeNode *node, *foundNode = NULL;
00089     csVector fifo;
00090 
00091     fifo.Push (this);
00092     while (fifo.Length () > 0 && !(foundNode && stopOnSuccess))
00093     {
00094       node = (csTreeNode*)fifo.Get (0); fifo.Delete (0);
00095       if (TreeFunc (node, param, stopOnSuccess))
00096         foundNode = node;
00097       if (!node->IsLeaf () && (SelBranch==NULL || SelBranch (node)))
00098           {
00099                 int i;
00100         for (i=0; i < node->children.Length (); i++ )
00101           fifo.Push (node->children.Get (i));
00102           }
00103     }
00104     fifo.DeleteAll ();
00105     return foundNode;
00106   }
00107 
00108  public:
00109   csTreeNode *parent; // parent node or NULL if toplevel
00110   csVector children; // node children
00111 };
00112 
00113 #endif // __CS_CSTREENODE_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000