/src/libreoffice/starmath/inc/mathml/iterator.hxx
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | */ |
9 | | |
10 | | #pragma once |
11 | | |
12 | | #include "element.hxx" |
13 | | |
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 |
17 | | */ |
18 | | |
19 | | namespace mathml |
20 | | { |
21 | | template <typename runType> |
22 | | void SmMlIteratorBottomToTop(SmMlElement* pMlElementTree, runType aRunType, void* aData) |
23 | | { |
24 | | if (pMlElementTree == nullptr) |
25 | | return; |
26 | | |
27 | | SmMlElement* pCurrent; |
28 | | |
29 | | // Fetch the deepest element |
30 | | pCurrent = pMlElementTree; |
31 | | while (pCurrent->getSubElementsCount() != 0) |
32 | | { |
33 | | if (pCurrent->getSubElement(0) == nullptr) |
34 | | break; |
35 | | pCurrent = pCurrent->getSubElement(0); |
36 | | } |
37 | | |
38 | | do |
39 | | { |
40 | | // Fetch next element |
41 | | size_t nId = pCurrent->getSubElementId(); |
42 | | // We are back to the top. |
43 | | if (pCurrent->getParentElement() == nullptr) |
44 | | break; |
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 |
49 | | { |
50 | | // It could have sub elements |
51 | | if (pCurrent->getParentElement()->getSubElement(nId + 1) == nullptr) |
52 | | break; |
53 | | pCurrent = pCurrent->getParentElement()->getSubElement(nId + 1); |
54 | | // Fetch the deepest element |
55 | | while (pCurrent->getSubElementsCount() != 0) |
56 | | { |
57 | | if (pCurrent->getSubElement(0) == nullptr) |
58 | | break; |
59 | | pCurrent = pCurrent->getSubElement(0); |
60 | | } |
61 | | } |
62 | | |
63 | | // Just in case of, but should be forbidden |
64 | | if (pCurrent != nullptr) |
65 | | aRunType(pCurrent, aData); |
66 | | |
67 | | } while (pCurrent != nullptr); |
68 | | } |
69 | | |
70 | | template <typename runType> |
71 | | void SmMlIteratorTopToBottom(SmMlElement* pMlElementTree, runType aRunType, void* aData) |
72 | 0 | { |
73 | 0 | if (pMlElementTree == nullptr) |
74 | 0 | return; |
75 | | |
76 | 0 | SmMlElement* pCurrent; |
77 | | |
78 | | // Fetch the deepest element |
79 | 0 | pCurrent = pMlElementTree; |
80 | 0 | aRunType(pCurrent, aData); |
81 | 0 | while (pCurrent->getSubElementsCount() != 0) |
82 | 0 | { |
83 | 0 | if (pCurrent->getSubElement(0) == nullptr) |
84 | 0 | break; |
85 | 0 | pCurrent = pCurrent->getSubElement(0); |
86 | 0 | aRunType(pCurrent, aData); |
87 | 0 | } |
88 | |
|
89 | 0 | do |
90 | 0 | { |
91 | | // Fetch next element |
92 | 0 | size_t nId = pCurrent->getSubElementId(); |
93 | | // We are back to the top. |
94 | 0 | if (pCurrent->getParentElement() == nullptr) |
95 | 0 | break; |
96 | | // If this was the last, then turn back to the parent |
97 | 0 | if (nId + 1 == pCurrent->getParentElement()->getSubElementsCount()) |
98 | 0 | pCurrent = pCurrent->getParentElement(); |
99 | 0 | else // If not, next is the one near it |
100 | 0 | { |
101 | | // It could have sub elements |
102 | 0 | if (pCurrent->getParentElement()->getSubElement(nId + 1) == nullptr) |
103 | 0 | break; |
104 | 0 | pCurrent = pCurrent->getParentElement()->getSubElement(nId + 1); |
105 | 0 | aRunType(pCurrent, aData); |
106 | | // Fetch the deepest element |
107 | 0 | while (pCurrent->getSubElementsCount() != 0) |
108 | 0 | { |
109 | 0 | if (pCurrent->getSubElement(0) == nullptr) |
110 | 0 | break; |
111 | 0 | pCurrent = pCurrent->getSubElement(0); |
112 | 0 | aRunType(pCurrent, aData); |
113 | 0 | } |
114 | 0 | } |
115 | |
|
116 | 0 | } while (pCurrent != nullptr); |
117 | 0 | } |
118 | | |
119 | | void SmMlIteratorFree(SmMlElement* pMlElementTree); |
120 | | |
121 | | SmMlElement* SmMlIteratorCopy(SmMlElement* pMlElementTree); |
122 | | |
123 | | } // end namespace mathml |
124 | | |
125 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |