/CS/migrated/branches/R0_90/include/csutil/cstreend.h

# · C++ Header · 113 lines · 66 code · 13 blank · 34 comment · 21 complexity · d347fbd01121fe4932f4e5137e65a61a MD5 · raw file

  1. /*
  2. Copyright (C) 2000 by Norman Krämer
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this library; if not, write to the Free
  13. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef __CS_CSTREENODE_H__
  16. #define __CS_CSTREENODE_H__
  17. #include "csutil/csvector.h"
  18. /**
  19. * A generic tree class.
  20. */
  21. class csTreeNode
  22. {
  23. public:
  24. bool IsLeaf ()
  25. { return children.Length () == 0; }
  26. void RemoveChild (csTreeNode *child)
  27. { int idx = children.Find (child); if (idx != -1) children.Delete (idx); }
  28. void AddChild (csTreeNode *child)
  29. { children.Push (child); child->parent = this; }
  30. csTreeNode (csTreeNode *theParent=NULL)
  31. { parent=theParent; if (parent) parent->children.Push (this); }
  32. virtual ~csTreeNode ()
  33. {
  34. int i;
  35. for(i=children.Length ()-1; i>=0; i--)
  36. delete (csTreeNode*)children.Get (i);
  37. if (parent)
  38. parent->RemoveChild (this);
  39. }
  40. /**
  41. * Execute a function on this node and its children. Do this in "DepthSearchFirst" order, that is check a childs children
  42. * before testing the next direct child.
  43. * Returns the last node where TreeFunc resulted in TRUE. If stopOnSuccess is true, then execution is stoped after first
  44. * successful execution of TreeFunc.
  45. * SelBranch lets you decide which children to select for further investugation. NULL means all children.
  46. */
  47. csTreeNode *DSF (bool (*TreeFunc)(csTreeNode *node, csSome param, bool stopOnSuccess),
  48. bool (*SelBranch)(csTreeNode *node), csSome param, bool stopOnSuccess)
  49. {
  50. csTreeNode *foundNode = NULL;
  51. int i=0;
  52. bool dive;
  53. if (TreeFunc (this, param, stopOnSuccess))
  54. foundNode = this;
  55. while (i<children.Length () && !(foundNode && stopOnSuccess))
  56. {
  57. dive = (SelBranch == NULL) || SelBranch ((csTreeNode*)children.Get (i));
  58. if (dive)
  59. foundNode = ((csTreeNode*)children.Get (i))->DSF (TreeFunc, SelBranch,
  60. param, stopOnSuccess);
  61. i++;
  62. }
  63. return foundNode;
  64. }
  65. /**
  66. * Execute a function on this node and its children. Do this in "BreadthSearchFirst" order, that is check first all
  67. * direct children before diving into subchildren.
  68. * Returns the last node where TreeFunc resulted in TRUE. If stopOnSuccess is true, then execution is stoped after first
  69. * successful execution of TreeFunc.
  70. * SelBranch lets you decide which children to select for further investugation. NULL means all children.
  71. */
  72. csTreeNode *BSF (bool (*TreeFunc)(csTreeNode *node, csSome param, bool stopOnSuccess),
  73. bool (*SelBranch)(csTreeNode *node), csSome param, bool stopOnSuccess)
  74. {
  75. csTreeNode *node, *foundNode = NULL;
  76. csVector fifo;
  77. fifo.Push (this);
  78. while (fifo.Length () > 0 && !(foundNode && stopOnSuccess))
  79. {
  80. node = (csTreeNode*)fifo.Get (0); fifo.Delete (0);
  81. if (TreeFunc (node, param, stopOnSuccess))
  82. foundNode = node;
  83. if (!node->IsLeaf () && (SelBranch==NULL || SelBranch (node)))
  84. {
  85. int i;
  86. for (i=0; i < node->children.Length (); i++ )
  87. fifo.Push (node->children.Get (i));
  88. }
  89. }
  90. fifo.DeleteAll ();
  91. return foundNode;
  92. }
  93. public:
  94. csTreeNode *parent; // parent node or NULL if toplevel
  95. csVector children; // node children
  96. };
  97. #endif // __CS_CSTREENODE_H__