Debian Codegraph @

src/App/MappedElement.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************************************
4 * *
5 * Copyright (c) 2022 Zheng, Lei (realthunder) <[email protected]> *
6 * Copyright (c) 2023 FreeCAD Project Association *
7 * *
8 * This file is part of FreeCAD. *
9 * *
10 * FreeCAD is free software: you can redistribute it and/or modify it under the terms of the *
11 * GNU Lesser General Public License as published by the Free Software Foundation, either *
12 * version 2.1 of the License, or (at your option) any later version. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; *
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
16 * See the GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License along with *
19 * FreeCAD. If not, see <https://www.gnu.org/licenses/>. *
20 * *
21 **************************************************************************************************/
22
23 #include <cstdlib>
24 #include <unordered_set>
25
26 #include "DocumentObject.h"
27 #include "MappedElement.h"
28
29 #include <algorithm>
30
31 using namespace Data;
32
33 // hextoa converts a single hex character to its integer value.
34 inline
35 int hextoa(char c)
36 {
37 constexpr int HEX_OFFSET = 10;
38 if (c >= '0' && c <= '9') {
39 return c - '0';
40 }
41
42 if (c >= 'a' && c <= 'f') {
43 return c - 'a' + HEX_OFFSET;
44 }
45
46 if (c >= 'A' && c <= 'F') {
47 return c - 'A' + HEX_OFFSET;
48 }
49
50 return -1; // invalid
51 }
52
53
54 // getHashLength returns the length of the #hexvalue without the leading '#'.
55 // If the string does not start with '#', it returns -1.
56 inline
57 int getHashLength(const MappedName& name)
58 {
59 if (name[0] != '#') {
60 return 0;
61 }
62
63 int len = name.size();
64 int i = 1; // start after the '#'
65 for (; i < len; ++i) {
66 if (!std::isxdigit(name[i])) {
67 break;
68 }
69 }
70
71 return i - 1;
72 }
73
74 // getIntLength returns the length of the integer starting at the given start
75 // index. If the string does not start with a digit or the start is past the
76 // end of the string, it returns 0.
77 inline
78 int getIntLength(const MappedName& name, int start)
79 {
80 int len = name.size();
81
82 int i = start;
83 for (; i < len; ++i) {
84 if (std::isdigit(name[i]) == 0) {
85 break; // not a digit, stop counting
86 }
87 }
88
89 return i - start; // return the length of the integer
90 }
91
92 // compareText compares the text part of two MappedNames starting at the given
93 // index. It returns -1 if left is less than right, 1 if left is greater than
94 // right, and 0 if they are equal.
95 inline
96 int compareText(const MappedName& left, const MappedName& right, int start)
97 {
98 int leftLength = left.size();
99 int rightLength = right.size();
100 int minLength = std::min(leftLength, rightLength);
101
102 for (int i = start; i < minLength; ++i) {
103 char ac = left[i];
104 char bc = right[i];
105
106 if (ac != bc) {
107 return ac < bc ? -1 : 1; // left is less than right
108 }
109 }
110
111 if (leftLength == rightLength) {
112 return 0; // they are equal
113 }
114
115 // If we reach here, it means one is a prefix of the other.
116 return leftLength < rightLength ? -1 : 1; // left is less than right
117 }
118
119 // compareHashed compares two MappedNames that could be hex values, or a
120 // combination of types. It returns:
121 // - -1 if left is less than right,
122 // - 1 if left is greater than right,
123 // - 0 if they are equal.
124 // This function handles identifiers being compared against hex values.
125 inline
126 int compareHashed(const MappedName& left, const MappedName& right)
127 {
128 int leftLength = getHashLength(left);
129 int rightLength = getHashLength(right);
130
131 if (leftLength < 1 || rightLength < 1 || // one of them is not a hex value
132 leftLength != rightLength) { // they are not the same length
133
134 // Choose the shorter one as less favors identifiers first and numbers
135 // to be smallest to largest.
136 return leftLength < rightLength ? -1 : 1;
137 }
138
139 // They are the same length and both are hex values, compare the hex digits.
140 int i = 1;
141 for (; i <= leftLength; ++i) {
142 int leftValue = hextoa(left[i]);
143 int rightValue = hextoa(right[i]);
144
145 if (leftValue != rightValue) {
146 return leftValue < rightValue ? -1 : 1;
147 }
148 }
149
150 // If we reach here, the hex values are equal, compare the text part.
151 return compareText(left, right, i);
152 }
153
154 // compareNumbers compares parts of two MappedNames that are identifiers
155 // containing numbers. It returns:
156 // - -1 if left is less than right,
157 // - 1 if left is greater than right,
158 // - 0 if they are equal.
159 inline
160 int compareNumbers(const MappedName& left, const MappedName& right, int start)
161 {
162 int leftLength = getIntLength(left, start);
163 int rightLength = getIntLength(right, start);
164
165 if (leftLength != rightLength) {
166 // The shorter number is less than the longer one.
167 return leftLength < rightLength ? -1 : 1;
168 }
169
170 int i = start;
171 for (; i < leftLength; ++i) {
172 char lc = left[i];
173 char rc = right[i];
174
175 if (lc != rc) {
176 return lc < rc ? -1 : 1;
177 }
178 }
179
180 // If we reach here, the numbers are equal, compare the text part.
181 return compareText(left, right, i);
182 }
183
184 // compareIdentifiers compares two MappedNames that are identifiers.
185 // It returns:
186 // - -1 if left is less than right,
187 // - 1 if left is greater than right,
188 // - 0 if they are equal.
189 inline
190 int compareIdentifiers(const MappedName& left, const MappedName& right)
191 {
192 int leftLength = left.size();
193 int rightLength = right.size();
194 int minLength = std::min(leftLength, rightLength);
195
196 int i = 0;
197 for (i = 0; i < minLength; ++i) {
198 char lc = left[i];
199 char rc = right[i];
200 bool ldigit = std::isdigit(lc);
201 bool rdigit = std::isdigit(rc);
202
203 if (!ldigit && !rdigit) {
204 if (lc != rc) {
205 return lc < rc ? -1 : 1; // left is less than right
206 }
207
208 continue; // equal, continue to next character
209 }
210
211 if (ldigit && rdigit) {
212 // both identifiers are the same up to this point,
213 return compareNumbers(left, right, i);
214 }
215
216 return ldigit ? -1 : 1; // left is less than right
217 }
218
219 // The identifier is the entire string, so we compare the lengths.
220 if (leftLength == rightLength) {
221 return 0; // they are equal
222 }
223
224 return leftLength < rightLength ? -1 : 1; // left is less than right
225 }
226
227 // Compare and sort the names.
228 //
229 // There are two forms of names that we compare:
230 // - Hex value: #<hex><text> like: #123:8;whatever or #aff-otherstuff;
231 // - Identifier: <id><number><text> like: Edge123:8;whatever or Edge1234:8;whatever
232 //
233 // 1. Empty names are less than any other name.
234 // 2. Identifiers are ordered before hex values.
235 // 3. If both names are hex values, compare the hex values, smaller first.
236 // 3. If both names are identifiers, compare the identifiers, lexicographically
237 // for the name and numerically for the number, smaller first.
238 // 6. If the identifiers or hex values are equal, compare the text part lexicographically.
239 // 7. If the text parts are equal but one is shorter, the shorter name is first.
240 bool ElementNameComparator::operator()(const MappedName& leftName,
241 const MappedName& rightName) const
242 {
243 int leftLength = leftName.size();
244 int rightLength = rightName.size();
245
246 if (std::min(leftLength, rightLength) == 0) {
247 return leftLength < rightLength; // empty names are less than any other name
248 }
249
250 // If at least one of the names is a hex value, we compare them as such.
251 if (leftName[0] == '#' || rightName[0] == '#') {
252 int got = compareHashed(leftName, rightName);
253 return got < 0; // if leftName is less than rightName, return true
254 }
255
256 int got = compareIdentifiers(leftName, rightName);
257
258 return got < 0; // if leftName is less than rightName or 0, return true
259 }
260
261 HistoryItem::HistoryItem(App::DocumentObject* obj, const Data::MappedName& name)
262 : obj(obj)
263 , tag(0)
264 , element(name)
265 {
266 if (obj) {
267 tag = obj->getID();
268 }
269 }