1 | /* $Id: kAvlGet.h 7 2008-02-04 02:08:02Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kAvlTmpl - Templated AVL Trees, Get a Node.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 1999-2007 knut st. osmundsen <[email protected]>
|
---|
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 | /**
|
---|
36 | * Gets a node from the tree (does not remove it!)
|
---|
37 | *
|
---|
38 | * @returns Pointer to the node holding the given key.
|
---|
39 | * @param pRoot Pointer to the AVL-tree root structure.
|
---|
40 | * @param Key Key value of the node which is to be found.
|
---|
41 | */
|
---|
42 | KAVL_DECL(KAVLNODE *) KAVL_FN(Get)(KAVLROOT *pRoot, KAVLKEY Key)
|
---|
43 | {
|
---|
44 | KAVLNODE *pNode;
|
---|
45 | #ifdef KAVL_LOOKTHRU_CACHE
|
---|
46 | KAVLTREEPTR *ppEntry;
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | KAVL_READ_LOCK(pRoot);
|
---|
50 | if (pRoot->mpRoot == KAVL_NULL)
|
---|
51 | {
|
---|
52 | KAVL_READ_UNLOCK(pRoot);
|
---|
53 | return NULL;
|
---|
54 | }
|
---|
55 |
|
---|
56 | #ifdef KAVL_LOOKTHRU_CACHE
|
---|
57 | ppEntry = &pRoot->maLookthru[KAVL_LOOKTHRU_HASH(Key)];
|
---|
58 | pNode = KAVL_GET_POINTER_NULL(ppEntry);
|
---|
59 | if (!pNode || KAVL_NE(pNode->mKey, Key))
|
---|
60 | #endif
|
---|
61 | {
|
---|
62 | pNode = KAVL_GET_POINTER(&pRoot->mpRoot);
|
---|
63 | while (KAVL_NE(pNode->mKey, Key))
|
---|
64 | {
|
---|
65 | if (KAVL_G(pNode->mKey, Key))
|
---|
66 | {
|
---|
67 | if (pNode->mpLeft == KAVL_NULL)
|
---|
68 | {
|
---|
69 | KAVL_READ_UNLOCK(pRoot);
|
---|
70 | return NULL;
|
---|
71 | }
|
---|
72 | pNode = KAVL_GET_POINTER(&pNode->mpLeft);
|
---|
73 | }
|
---|
74 | else
|
---|
75 | {
|
---|
76 | if (pNode->mpRight == KAVL_NULL)
|
---|
77 | {
|
---|
78 | KAVL_READ_UNLOCK(pRoot);
|
---|
79 | return NULL;
|
---|
80 | }
|
---|
81 | pNode = KAVL_GET_POINTER(&pNode->mpRight);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | #ifdef KAVL_LOOKTHRU_CACHE
|
---|
86 | KAVL_SET_POINTER(ppEntry, pNode);
|
---|
87 | #endif
|
---|
88 | }
|
---|
89 |
|
---|
90 | KAVL_READ_UNLOCK(pRoot);
|
---|
91 | return pNode;
|
---|
92 | }
|
---|
93 |
|
---|