csutil/tree.h
00001 /* 00002 Copyright (C) 2000 by Norman Kraemer 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 "csextern.h" 00023 #include "array.h" 00024 00028 class csTreeNode 00029 { 00030 public: 00032 bool IsLeaf () 00033 { return children.Length () == 0; } 00034 00036 void RemoveChild (csTreeNode *child) 00037 { 00038 int idx = children.Find (child); 00039 if (idx != -1) children.DeleteIndex (idx); 00040 } 00041 00043 void AddChild (csTreeNode *child) 00044 { children.Push (child); child->parent = this; } 00045 00047 csTreeNode (csTreeNode *theParent=0) 00048 { parent=theParent; if (parent) parent->children.Push (this); } 00049 00050 virtual ~csTreeNode () 00051 { 00052 int i; 00053 for(i=children.Length ()-1; i>=0; i--) 00054 delete children.Get (i); 00055 if (parent) 00056 parent->RemoveChild (this); 00057 } 00058 00069 csTreeNode *DSF (bool (*TreeFunc)(csTreeNode *node, void* param, 00070 bool stopOnSuccess), 00071 bool (*SelBranch)(csTreeNode *node), void* param, 00072 bool stopOnSuccess) 00073 { 00074 csTreeNode *foundNode = 0; 00075 int i=0; 00076 bool dive; 00077 if (TreeFunc (this, param, stopOnSuccess)) 00078 foundNode = this; 00079 while (i<children.Length () && !(foundNode && stopOnSuccess)) 00080 { 00081 dive = (SelBranch == 0) || SelBranch (children[i]); 00082 if (dive) 00083 foundNode = (children[i])->DSF (TreeFunc, SelBranch, 00084 param, stopOnSuccess); 00085 i++; 00086 } 00087 return foundNode; 00088 } 00089 00100 csTreeNode *BSF (bool (*TreeFunc)(csTreeNode *node, void* param, 00101 bool stopOnSuccess), 00102 bool (*SelBranch)(csTreeNode *node), void* param, 00103 bool stopOnSuccess) 00104 { 00105 csTreeNode *node, *foundNode = 0; 00106 csArray<csTreeNode*> fifo; 00107 00108 fifo.Push (this); 00109 while (fifo.Length () > 0 && !(foundNode && stopOnSuccess)) 00110 { 00111 node = fifo[0]; fifo.DeleteIndex (0); 00112 if (TreeFunc (node, param, stopOnSuccess)) 00113 foundNode = node; 00114 if (!node->IsLeaf () && (SelBranch==0 || SelBranch (node))) 00115 { 00116 int i; 00117 for (i=0; i < node->children.Length (); i++ ) 00118 fifo.Push (node->children[i]); 00119 } 00120 } 00121 fifo.DeleteAll (); 00122 return foundNode; 00123 } 00124 00125 public: 00126 csTreeNode *parent; // parent node or 0 if toplevel 00127 csArray<csTreeNode*> children; // node children 00128 }; 00129 00130 #endif // __CS_CSTREENODE_H__
Generated for Crystal Space by doxygen 1.2.18