1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
12 #include "element.hxx"
14 /** The purpose of this iterator is to be able to iterate threw an infinite element tree
15 * infinite -> as much as your memory can hold
16 * No call-backs that will end up in out of stack
21 template <typename runType
>
22 void SmMlIteratorBottomToTop(SmMlElement
* pMlElementTree
, runType aRunType
, void* aData
)
24 if (pMlElementTree
== nullptr)
27 SmMlElement
* pCurrent
;
29 // Fetch the deepest element
30 pCurrent
= pMlElementTree
;
31 while (pCurrent
->getSubElementsCount() != 0)
33 if (pCurrent
->getSubElement(0) == nullptr)
35 pCurrent
= pCurrent
->getSubElement(0);
41 size_t nId
= pCurrent
->getSubElementId();
42 // We are back to the top.
43 if (pCurrent
->getParentElement() == nullptr)
45 // If this was the last, then turn back to the parent
46 if (nId
+ 1 == pCurrent
->getParentElement()->getSubElementsCount())
47 pCurrent
= pCurrent
->getParentElement();
48 else // If not, next is the one near it
50 // It could have sub elements
51 if (pCurrent
->getParentElement()->getSubElement(nId
+ 1) == nullptr)
53 pCurrent
= pCurrent
->getParentElement()->getSubElement(nId
+ 1);
54 // Fetch the deepest element
55 while (pCurrent
->getSubElementsCount() != 0)
57 if (pCurrent
->getSubElement(0) == nullptr)
59 pCurrent
= pCurrent
->getSubElement(0);
63 // Just in case of, but should be forbidden
64 if (pCurrent
!= nullptr)
65 aRunType(pCurrent
, aData
);
67 } while (pCurrent
!= nullptr);
70 template <typename runType
>
71 void SmMlIteratorTopToBottom(SmMlElement
* pMlElementTree
, runType aRunType
, void* aData
)
73 if (pMlElementTree
== nullptr)
76 SmMlElement
* pCurrent
;
78 // Fetch the deepest element
79 pCurrent
= pMlElementTree
;
80 aRunType(pCurrent
, aData
);
81 while (pCurrent
->getSubElementsCount() != 0)
83 if (pCurrent
->getSubElement(0) == nullptr)
85 pCurrent
= pCurrent
->getSubElement(0);
86 aRunType(pCurrent
, aData
);
92 size_t nId
= pCurrent
->getSubElementId();
93 // We are back to the top.
94 if (pCurrent
->getParentElement() == nullptr)
96 // If this was the last, then turn back to the parent
97 if (nId
+ 1 == pCurrent
->getParentElement()->getSubElementsCount())
98 pCurrent
= pCurrent
->getParentElement();
99 else // If not, next is the one near it
101 // It could have sub elements
102 if (pCurrent
->getParentElement()->getSubElement(nId
+ 1) == nullptr)
104 pCurrent
= pCurrent
->getParentElement()->getSubElement(nId
+ 1);
105 aRunType(pCurrent
, aData
);
106 // Fetch the deepest element
107 while (pCurrent
->getSubElementsCount() != 0)
109 if (pCurrent
->getSubElement(0) == nullptr)
111 pCurrent
= pCurrent
->getSubElement(0);
112 aRunType(pCurrent
, aData
);
116 } while (pCurrent
!= nullptr);
119 void SmMlIteratorFree(SmMlElement
* pMlElementTree
);
121 SmMlElement
* SmMlIteratorCopy(SmMlElement
* pMlElementTree
);
123 } // end namespace mathml
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */