source: trunk/include/k/kAvlTmpl/kAvlDoWithAll.h@ 4

Last change on this file since 4 was 2, checked in by bird, 18 years ago

Imported http://svn.netlabs.org/repos/libc/trunk/kStuff, revision 3612.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.2 KB
Line 
1/* $Id: kAvlDoWithAll.h 2 2007-11-16 16:07:14Z bird $ */
2/** @file
3 * kAvlTmpl - Templated AVL Trees, The Callback Iterator.
4 */
5
6/*
7 * Copyright (c) 1999-2007 knut st. osmundsen <bird-src-spam@anduin.net>
8 *
9 * This file is part of kStuff.
10 *
11 * kStuff is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * kStuff is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with kStuff; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 *
26 * As a special exception, since this is a source file and not a header
27 * file, you are granted permission to #include this file as you wish
28 * without this in itself causing the resulting program or whatever to be
29 * covered by the LGPL license. This exception does not however invalidate
30 * any other reasons why the resulting program/whatever should not be
31 * covered the LGPL or GPL.
32 */
33
34/*******************************************************************************
35* Structures and Typedefs *
36*******************************************************************************/
37/**
38 * Stack used by DoWithAll to avoid recusive function calls.
39 */
40typedef struct
41{
42 unsigned cEntries;
43 KAVLNODE *aEntries[KAVL_MAX_STACK];
44 char achFlags[KAVL_MAX_STACK];
45} KAVL_INT(STACK2);
46
47
48/**
49 * Iterates thru all nodes in the given tree.
50 *
51 * @returns 0 on success. Return from callback on failure.
52 * @param ppTree Pointer to the AVL-tree root node pointer.
53 * @param fFromLeft K_TRUE: Left to right.
54 * K_FALSE: Right to left.
55 * @param pfnCallBack Pointer to callback function.
56 * @param pvUser User parameter passed on to the callback function.
57 */
58KAVL_DECL(int) KAVL_FN(DoWithAll)(KAVLTREEPTR *ppTree, KBOOL fFromLeft, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
59{
60 KAVL_INT(STACK2) AVLStack;
61 KAVLNODE *pNode;
62#ifdef KAVL_EQUAL_ALLOWED
63 KAVLNODE *pEqual;
64#endif
65 int rc;
66
67 if (*ppTree == KAVL_NULL)
68 return 0;
69
70 AVLStack.cEntries = 1;
71 AVLStack.achFlags[0] = 0;
72 AVLStack.aEntries[0] = KAVL_GET_POINTER(ppTree);
73
74 if (fFromLeft)
75 { /* from left */
76 while (AVLStack.cEntries > 0)
77 {
78 pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
79
80 /* left */
81 if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
82 {
83 if (pNode->mpLeft != KAVL_NULL)
84 {
85 AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
86 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
87 continue;
88 }
89 }
90
91 /* center */
92 rc = pfnCallBack(pNode, pvUser);
93 if (rc)
94 return rc;
95#ifdef KAVL_EQUAL_ALLOWED
96 if (pNode->mpList != KAVL_NULL)
97 for (pEqual = KAVL_GET_POINTER(&pNode->mpList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->mpList))
98 {
99 rc = pfnCallBack(pEqual, pvUser);
100 if (rc)
101 return rc;
102 }
103#endif
104
105 /* right */
106 AVLStack.cEntries--;
107 if (pNode->mpRight != KAVL_NULL)
108 {
109 AVLStack.achFlags[AVLStack.cEntries] = 0;
110 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
111 }
112 } /* while */
113 }
114 else
115 { /* from right */
116 while (AVLStack.cEntries > 0)
117 {
118 pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
119
120 /* right */
121 if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
122 {
123 if (pNode->mpRight != KAVL_NULL)
124 {
125 AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
126 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
127 continue;
128 }
129 }
130
131 /* center */
132 rc = pfnCallBack(pNode, pvUser);
133 if (rc)
134 return rc;
135#ifdef KAVL_EQUAL_ALLOWED
136 if (pNode->mpList != KAVL_NULL)
137 for (pEqual = KAVL_GET_POINTER(&pNode->mpList); pEqual; pEqual = KAVL_GET_POINTER_NULL(&pEqual->pList))
138 {
139 rc = pfnCallBack(pEqual, pvUser);
140 if (rc)
141 return rc;
142 }
143#endif
144
145 /* left */
146 AVLStack.cEntries--;
147 if (pNode->mpLeft != KAVL_NULL)
148 {
149 AVLStack.achFlags[AVLStack.cEntries] = 0;
150 AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
151 }
152 } /* while */
153 }
154
155 return 0;
156}
157
Note: See TracBrowser for help on using the repository browser.