Showing the changes to libraries/shared/src/OctalCode.cpp (1 of 6 files). Show all
Unified · Split
| @@ -257,3 +257,42 @@ unsigned char* rebaseOctalCode(unsigned char* originalOctalCode, unsigned char* · 256 unmodified lines | |||
| 257 | return newCode; | 257 | return newCode; |
| 258 | } | 258 | } |
| 259 | 259 | ||
| 260 | bool isAncestorOf(unsigned char* possibleAncestor, unsigned char* possibleDescendent, int descendentsChild) { | ||
| 261 | if (!possibleAncestor || !possibleDescendent) { | ||
| 262 | return false; | ||
| 263 | } | ||
| 264 | |||
| 265 | int ancestorCodeLength = numberOfThreeBitSectionsInCode(possibleAncestor); | ||
| 266 | if (ancestorCodeLength == 0) { | ||
| 267 | return true; // this is the root, it's the anscestor of all | ||
| 268 | } | ||
| 269 | |||
| 270 | int descendentCodeLength = numberOfThreeBitSectionsInCode(possibleDescendent); | ||
| 271 | |||
| 272 | // if the caller also include a child, then our descendent length is actually one extra! | ||
| 273 | if (descendentsChild != CHECK_NODE_ONLY) { | ||
| 274 | descendentCodeLength++; | ||
| 275 | } | ||
| 276 | |||
| 277 | if (ancestorCodeLength > descendentCodeLength) { | ||
| 278 | return false; // if the descendent is shorter, it can't be a descendent | ||
| 279 | } | ||
| 280 | |||
| 281 | // compare the sections for the ancestor to the descendent | ||
| 282 | for (int section = 0; section < ancestorCodeLength; section++) { | ||
| 283 | char sectionValueAncestor = getOctalCodeSectionValue(possibleAncestor, section); | ||
| 284 | char sectionValueDescendent; | ||
| 285 | if (ancestorCodeLength <= descendentCodeLength) { | ||
| 286 | sectionValueDescendent = getOctalCodeSectionValue(possibleDescendent, section); | ||
| 287 | } else { | ||
| 288 | assert(descendentsChild != CHECK_NODE_ONLY); | ||
| 289 | sectionValueDescendent = descendentsChild; | ||
| 290 | } | ||
| 291 | if (sectionValueAncestor != sectionValueDescendent) { | ||
| 292 | return false; // first non-match, means they don't match | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | // they all match, so we are an ancestor | ||
| 297 | return true; | ||
| 298 | } | ||