]>
Dogcows Code - chaz/yoink/blob - src/Moof/Tree.hh
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
29 #ifndef _MOOF_TREE_HH_
30 #define _MOOF_TREE_HH_
32 #include <boost/shared_ptr.hpp>
33 #include <boost/weak_ptr.hpp>
43 typedef boost::shared_ptr
<Tree
> Ptr
;
44 typedef boost::weak_ptr
<Tree
> WeakPtr
;
52 WeakPtr lastDescendant_
;
54 inline static void init(Ptr itself
)
56 itself
->prevSibling_
= itself
;
57 itself
->lastDescendant_
= itself
;
62 explicit Tree(const T
& item
) :
67 inline void print() const
69 std::cout
<< "==================" << std::endl
;
70 std::cout
<< " Node: " << getThis() << std::endl
;
71 std::cout
<< " Parent: " << parent_
.lock() << std::endl
;
72 std::cout
<< " PrevSib: " << prevSibling_
.lock() << std::endl
;
73 std::cout
<< " Next: " << next_
<< std::endl
;
74 std::cout
<< "LastDesc: " << lastDescendant_
.lock() << std::endl
;
75 //std::cout << " Value: " << node << std::endl;
76 std::cout
<< "==================" << std::endl
;
79 inline static Ptr
createNewNode()
81 Ptr newNode
= Ptr(new Tree
<T
>());
84 inline static Ptr
createNewNode(const T
& item
)
86 Ptr newNode
= Ptr(new Tree
<T
>(item
));
91 inline Ptr
getNext() const
96 inline Ptr
getPrevious() const
98 Ptr parent
= getParent();
102 if (parent
->getNext().get() == this)
108 return prevSibling_
.lock()->getLastDescendant();
115 inline Ptr
getFirstChild() const
117 if (next_
&& next_
->getParent().get() == this)
125 inline Ptr
getLastChild() const
127 Ptr child
= getFirstChild();
131 child
= child
->prevSibling_
.lock();
137 inline Ptr
getChild(int index
) const
139 Ptr child
= getFirstChild();
141 for (int i
= 0; child
&& i
< index
; ++i
)
143 child
= child
->getNextSibling();
149 inline Ptr
getNextSibling() const
151 Ptr sibling
= getLastDescendant()->getNext();
153 if (sibling
&& sibling
->getParent() != getParent())
161 inline Ptr
getPreviousSibling() const
163 Ptr parent
= getParent();
165 if (parent
&& parent
->getNext().get() != this)
167 return prevSibling_
.lock();
173 inline Ptr
getParent() const
175 return parent_
.lock();
178 inline Ptr
getLastDescendant() const
180 return lastDescendant_
.lock();
183 inline Ptr
getThis() const
187 return next_
->getPrevious();
190 return getLastDescendant();
193 inline bool isRoot() const
195 return getParent().get() == 0;
198 inline bool isLeaf() const
200 return getLastDescendant().get() == this;
203 inline bool isDescendantOf(Ptr ancestor
) const
205 Ptr temp
= getParent();
209 if (temp
.get() == this) return true;
211 temp
= temp
->getParent();
227 void addChild(Ptr subtree
)
230 //WeakPtr prevSibling_;
232 //WeakPtr lastDescendant_;
236 Ptr firstChild
= getFirstChild();
237 Ptr lastChild
= getLastChild();
238 Ptr nextSibling
= getNextSibling();
239 Ptr lastDescendant
= getLastDescendant();
240 Ptr newLastDescendant
= subtree
->getLastDescendant();
241 Ptr parent
= getThis();
243 // 1. If parent is leaf, set parent.next to subtree.
250 // 2. Set parent.last_descendant to subtree.last_descendant.
253 while (temp
&& temp
->getLastDescendant() == lastDescendant
)
255 temp
->lastDescendant_
= newLastDescendant
;
256 temp
= temp
->getParent();
259 // 3. Set subtree.parent to parent.
261 subtree
->parent_
= parent
;
263 // 4. Set parent.first_child.prev_sibling to subtree.
267 firstChild
->prevSibling_
= subtree
;
270 // 5. Set subtree.prev_sibling to parent.last_child.
271 // 6. Set parent.last_child.last_descendant.next to subtree.
275 subtree
->prevSibling_
= lastChild
;
276 lastChild
->getLastDescendant()->next_
= subtree
;
280 subtree
->prevSibling_
= subtree
;
283 // 7. Set subtree.last_descendant.next to parent.next_sibling.
287 subtree
->getLastDescendant()->next_
= nextSibling
;
293 Ptr parent
= getParent();
294 Ptr prevSibling
= getPreviousSibling();
295 Ptr nextSibling
= getNextSibling();
296 Ptr lastDescendant
= getLastDescendant();
297 Ptr previous
= getPrevious();
299 Ptr newLastDescendant
;
301 // 1. Fix last descendant of each direct ancestor.
303 if (prevSibling
) newLastDescendant
= prevSibling
->getLastDescendant();
304 else newLastDescendant
= parent
;
307 while (temp
&& temp
->getLastDescendant() == lastDescendant
)
309 temp
->lastDescendant_
= newLastDescendant
;
310 temp
= temp
->getParent();
313 // 2. Fix next of previous.
317 previous
->next_
= nextSibling
;
320 // 3. Fix the previous sibling of next sibling.
324 nextSibling
->prevSibling_
= prevSibling
;
327 // 4. Once detached, the subtree root has no parent or siblings.
330 prevSibling_
= getThis();
338 #endif // _MOOF_TREE_HH_
340 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.050508 seconds and 4 git commands to generate.