| 1 | /* $Id: kAvlEnum.h 2 2007-11-16 16:07:14Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kAvlTmpl - Templated AVL Trees, Node Enumeration.
|
|---|
| 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 | * Enumeration control data.
|
|---|
| 39 | *
|
|---|
| 40 | * This is initialized by BeginEnum and used by GetNext to figure out what
|
|---|
| 41 | * to do next.
|
|---|
| 42 | */
|
|---|
| 43 | typedef struct KAVL_TYPE(,ENUMDATA)
|
|---|
| 44 | {
|
|---|
| 45 | KBOOL fFromLeft;
|
|---|
| 46 | KI8 cEntries;
|
|---|
| 47 | KU8 achFlags[KAVL_MAX_STACK];
|
|---|
| 48 | KAVLNODE * aEntries[KAVL_MAX_STACK];
|
|---|
| 49 | } KAVL_TYPE(,ENUMDATA), *KAVL_TYPE(P,ENUMDATA);
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Get the next node in the tree enumeration.
|
|---|
| 54 | *
|
|---|
| 55 | * The current implementation of this function willl not walk the mpList
|
|---|
| 56 | * chain like the DoWithAll function does. This may be changed later.
|
|---|
| 57 | *
|
|---|
| 58 | * @returns Pointer to the first node in the tree.
|
|---|
| 59 | * @param pEnumData Pointer to enumeration control data.
|
|---|
| 60 | */
|
|---|
| 61 | KAVL_DECL(KAVLNODE *) KAVL_FN(GetNext)(KAVL_TYPE(,ENUMDATA) *pEnumData)
|
|---|
| 62 | {
|
|---|
| 63 | if (pEnumData->fFromLeft)
|
|---|
| 64 | { /* from left */
|
|---|
| 65 | while (pEnumData->cEntries > 0)
|
|---|
| 66 | {
|
|---|
| 67 | KAVLNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1];
|
|---|
| 68 |
|
|---|
| 69 | /* left */
|
|---|
| 70 | if (pEnumData->achFlags[pEnumData->cEntries - 1] == 0)
|
|---|
| 71 | {
|
|---|
| 72 | pEnumData->achFlags[pEnumData->cEntries - 1]++;
|
|---|
| 73 | if (pNode->mpLeft != KAVL_NULL)
|
|---|
| 74 | {
|
|---|
| 75 | pEnumData->achFlags[pEnumData->cEntries] = 0; /* 0 left, 1 center, 2 right */
|
|---|
| 76 | pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
|
|---|
| 77 | continue;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /* center */
|
|---|
| 82 | if (pEnumData->achFlags[pEnumData->cEntries - 1] == 1)
|
|---|
| 83 | {
|
|---|
| 84 | pEnumData->achFlags[pEnumData->cEntries - 1]++;
|
|---|
| 85 | return pNode;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /* right */
|
|---|
| 89 | pEnumData->cEntries--;
|
|---|
| 90 | if (pNode->mpRight != KAVL_NULL)
|
|---|
| 91 | {
|
|---|
| 92 | pEnumData->achFlags[pEnumData->cEntries] = 0;
|
|---|
| 93 | pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
|
|---|
| 94 | }
|
|---|
| 95 | } /* while */
|
|---|
| 96 | }
|
|---|
| 97 | else
|
|---|
| 98 | { /* from right */
|
|---|
| 99 | while (pEnumData->cEntries > 0)
|
|---|
| 100 | {
|
|---|
| 101 | KAVLNODE *pNode = pEnumData->aEntries[pEnumData->cEntries - 1];
|
|---|
| 102 |
|
|---|
| 103 | /* right */
|
|---|
| 104 | if (pEnumData->achFlags[pEnumData->cEntries - 1] == 0)
|
|---|
| 105 | {
|
|---|
| 106 | pEnumData->achFlags[pEnumData->cEntries - 1]++;
|
|---|
| 107 | if (pNode->mpRight != KAVL_NULL)
|
|---|
| 108 | {
|
|---|
| 109 | pEnumData->achFlags[pEnumData->cEntries] = 0; /* 0 right, 1 center, 2 left */
|
|---|
| 110 | pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
|
|---|
| 111 | continue;
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /* center */
|
|---|
| 116 | if (pEnumData->achFlags[pEnumData->cEntries - 1] == 1)
|
|---|
| 117 | {
|
|---|
| 118 | pEnumData->achFlags[pEnumData->cEntries - 1]++;
|
|---|
| 119 | return pNode;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /* left */
|
|---|
| 123 | pEnumData->cEntries--;
|
|---|
| 124 | if (pNode->mpLeft != KAVL_NULL)
|
|---|
| 125 | {
|
|---|
| 126 | pEnumData->achFlags[pEnumData->cEntries] = 0;
|
|---|
| 127 | pEnumData->aEntries[pEnumData->cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
|
|---|
| 128 | }
|
|---|
| 129 | } /* while */
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | return NULL;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Starts an enumeration of all nodes in the given AVL tree.
|
|---|
| 138 | *
|
|---|
| 139 | * The current implementation of this function willl not walk the mpList
|
|---|
| 140 | * chain like the DoWithAll function does. This may be changed later.
|
|---|
| 141 | *
|
|---|
| 142 | * @returns Pointer to the first node in the enumeration.
|
|---|
| 143 | * @param ppTree Pointer to the AVL-tree root node pointer.
|
|---|
| 144 | * @param pEnumData Pointer to enumeration control data.
|
|---|
| 145 | * @param fFromLeft K_TRUE: Left to right.
|
|---|
| 146 | * K_FALSE: Right to left.
|
|---|
| 147 | */
|
|---|
| 148 | KAVL_DECL(KAVLNODE *) KAVL_FN(BeginEnum)(KAVLTREEPTR *ppTree, KAVL_TYPE(,ENUMDATA) *pEnumData, KBOOL fFromLeft)
|
|---|
| 149 | {
|
|---|
| 150 | if (*ppTree != KAVL_NULL)
|
|---|
| 151 | {
|
|---|
| 152 | pEnumData->fFromLeft = fFromLeft;
|
|---|
| 153 | pEnumData->cEntries = 1;
|
|---|
| 154 | pEnumData->aEntries[0] = KAVL_GET_POINTER(ppTree);
|
|---|
| 155 | pEnumData->achFlags[0] = 0;
|
|---|
| 156 | }
|
|---|
| 157 | else
|
|---|
| 158 | pEnumData->cEntries = 0;
|
|---|
| 159 |
|
|---|
| 160 | return KAVL_FN(GetNext)(pEnumData);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|