1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
25 #include <tools/link.hxx>
26 #include <rsctree.hxx>
31 pLeft
= pRight
= NULL
;
38 void BiNode::EnumNodes( Link
<> aLink
) const
41 Left()->EnumNodes( aLink
);
42 aLink
.Call( const_cast<BiNode
*>(this) );
44 Right()->EnumNodes( aLink
);
47 BiNode
* BiNode::ChangeDLListBTree( BiNode
* pList
)
55 while( pList
->Left() )
56 pList
= pList
->Left();
59 for( nEle
= 0; pTmp
->Right(); nEle
++ )
65 for( i
= 0; i
< (nEle
/ 2); i
++ )
67 pMiddle
= pMiddle
->Right();
74 if( NULL
!= (pTmp
= pMiddle
->Left()) ) // rechten Zeiger auf Null
75 pTmp
->pRight
= (BiNode
*)0;
77 // linken Zeiger auf Null
78 BiNode
* pRightNode
= pMiddle
->Right();
80 pRightNode
->pLeft
= (BiNode
*)0;
82 pMiddle
->pLeft
= ChangeDLListBTree( pList
);
83 pMiddle
->pRight
= ChangeDLListBTree( pRightNode
);
90 BiNode
* BiNode::ChangeBTreeDLList()
93 BiNode
* pLL_RN
; // linke Liste rechter Knoten
97 pList
= Right()->ChangeBTreeDLList();
104 pLL_RN
= pList
= Left()->ChangeBTreeDLList();
106 while( pLL_RN
->Right() )
107 pLL_RN
= pLL_RN
->Right();
110 pLL_RN
->pRight
= this;
115 NameNode
* NameNode::Remove( NameNode
* pRemove
)
117 NameNode
* pRoot
= this;
118 NameNode
* pParent
= SearchParent( pRemove
);
122 if( pParent
->Left() &&
123 (EQUAL
== pRemove
->Compare( pParent
->Left() ) ) )
125 pParent
->pLeft
= pRemove
->Left();
126 if( pRemove
->Right() )
127 pParent
->Insert( pRemove
->Right() );
129 else if( pParent
->Right() &&
130 (EQUAL
== pRemove
->Compare( pParent
->Right() ) ) )
132 pParent
->pRight
= pRemove
->Right();
133 if( pRemove
->Left() )
134 pParent
->Insert( pRemove
->Left() );
137 else if( EQUAL
== this->Compare( pRemove
) )
143 Right()->Insert( Left() );
150 pRemove
->pLeft
= pRemove
->pRight
= NULL
;
156 COMPARE
NameNode::Compare( const NameNode
* pCompare
) const
158 if( reinterpret_cast<sal_uIntPtr
>(this) < reinterpret_cast<sal_uIntPtr
>(pCompare
) )
160 else if( reinterpret_cast<sal_uIntPtr
>(this) > reinterpret_cast<sal_uIntPtr
>(pCompare
) )
166 COMPARE
NameNode::Compare( const void * pCompare
) const
168 if( reinterpret_cast<sal_uIntPtr
>(this) < reinterpret_cast<sal_uIntPtr
>(pCompare
) )
170 else if( reinterpret_cast<sal_uIntPtr
>(this) > reinterpret_cast<sal_uIntPtr
>(pCompare
) )
176 // search for a parent node.
177 // return a pointer to the parent node if found.
178 // otherwise return 0.
179 NameNode
* NameNode::SearchParent( const NameNode
* pSearch
) const
181 int nCmp
= Compare( pSearch
);
183 if( nCmp
== GREATER
)
187 if( Left()->Compare( pSearch
) == EQUAL
)
188 return const_cast<NameNode
*>(this);
189 return Left()->SearchParent( pSearch
);
192 else if( nCmp
== LESS
)
196 if( Right()->Compare( pSearch
) == EQUAL
)
197 return const_cast<NameNode
*>(this);
198 return Right()->SearchParent( pSearch
);
201 return (NameNode
*)NULL
;
204 // search for a node.
205 // return a pointer to the node if found.
206 // otherwise return 0.
207 NameNode
* NameNode::Search( const NameNode
* pSearch
) const
209 int nCmp
= Compare( pSearch
);
211 if( nCmp
== GREATER
)
214 return Left()->Search( pSearch
);
216 else if( nCmp
== LESS
)
219 return Right()->Search( pSearch
);
222 return const_cast<NameNode
*>(this);
227 // search for a node.
228 // return a pointer to the node if found.
229 // otherwise return 0.
230 NameNode
* NameNode::Search( const void * pSearch
) const
232 int nCmp
= Compare( pSearch
);
234 if( nCmp
== GREATER
)
237 return Left()->Search( pSearch
);
239 else if( nCmp
== LESS
)
242 return Right()->Search( pSearch
);
245 return const_cast<NameNode
*>(this);
250 // Ein Knoten wird in den Baum eingefuegt
251 // Gibt es einen Knoten mit dem gleichen Namen, dann return false
252 // sonst return true. Der Knoten wird auf jeden Fall eingefuegt.
254 bool NameNode::Insert( NameNode
* pTN
, sal_uInt32
* pnDepth
)
257 int nCmp
= Compare( pTN
);
260 if( nCmp
== GREATER
)
263 bRet
= Left()->Insert( pTN
, pnDepth
);
270 bRet
= Right()->Insert( pTN
, pnDepth
);
280 // insert a node in the tree.
281 // if the node with the same name is in, return false and no insert.
282 // if not return true.
283 bool NameNode::Insert( NameNode
* pTN
)
285 sal_uInt32 nDepth
= 0;
288 bRet
= Insert( pTN
, &nDepth
);
294 pLeft
= ChangeDLListBTree( Left()->ChangeBTreeDLList() );
296 pRight
= ChangeDLListBTree( Right()->ChangeBTreeDLList() );
303 void NameNode::OrderTree()
305 NameNode
* pTmpLeft
= static_cast<NameNode
*>(Left());
306 NameNode
* pTmpRight
= static_cast<NameNode
*>(Right());
310 SubOrderTree( pTmpLeft
);
311 SubOrderTree( pTmpRight
);
314 void NameNode::SubOrderTree( NameNode
* pOrderNode
)
318 NameNode
* pTmpLeft
= static_cast<NameNode
*>(pOrderNode
->Left());
319 NameNode
* pTmpRight
= static_cast<NameNode
*>(pOrderNode
->Right());
320 pOrderNode
->pLeft
= NULL
;
321 pOrderNode
->pRight
= NULL
;
322 Insert( pOrderNode
);
323 SubOrderTree( pTmpLeft
);
324 SubOrderTree( pTmpRight
);
328 IdNode
* IdNode::Search( sal_uInt32 nTypeName
) const
330 return static_cast<IdNode
*>(NameNode::Search( (const void *)&nTypeName
));
333 COMPARE
IdNode::Compare( const NameNode
* pSearch
) const
335 if( GetId() < (sal_uInt32
)(static_cast<const IdNode
*>(pSearch
)->GetId()) )
337 else if( GetId() > (sal_uInt32
)(static_cast<const IdNode
*>(pSearch
)->GetId()) )
343 // pSearch ist ein Zeiger auf sal_uInt32
344 COMPARE
IdNode::Compare( const void * pSearch
) const
346 if( GetId() < *static_cast<const sal_uInt32
*>(pSearch
) )
348 else if( GetId() > *static_cast<const sal_uInt32
*>(pSearch
) )
354 sal_uInt32
IdNode::GetId() const
359 StringNode
* StringNode::Search( const char * pSearch
) const
361 return static_cast<StringNode
*>(NameNode::Search( (const void *)pSearch
));
364 COMPARE
StringNode::Compare( const NameNode
* pSearch
) const
366 int nCmp
= strcmp( m_aName
.getStr(),
367 static_cast<const StringNode
*>(pSearch
)->m_aName
.getStr() );
376 // pSearch ist ein Zeiger auf const char *
377 COMPARE
StringNode::Compare( const void * pSearch
) const
379 int nCmp
= strcmp( m_aName
.getStr(), static_cast<const char *>(pSearch
) );
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */