From 3f158c075395819f56b2dfc84cfb70eb20a577bc Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 25 Apr 2013 23:42:35 -0700 Subject: [PATCH] Added new version of compareOctalCodes() which actually compares octal codes --- libraries/shared/src/OctalCode.cpp | 38 +++++++++++++++++++++++++++++- libraries/shared/src/OctalCode.h | 4 +++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/libraries/shared/src/OctalCode.cpp b/libraries/shared/src/OctalCode.cpp index 2e9fadb0f6..62768569f1 100644 --- a/libraries/shared/src/OctalCode.cpp +++ b/libraries/shared/src/OctalCode.cpp @@ -7,6 +7,7 @@ // #include +#include // std:min #include #include "SharedUtil.h" #include "OctalCode.h" @@ -134,7 +135,7 @@ float * firstVertexForCode(unsigned char * octalCode) { return firstVertex; } -OctalTreeDepth compareOctalCodes(unsigned char* codeA, unsigned char* codeB) { +OctalTreeDepth compareOctalCodesDepth(unsigned char* codeA, unsigned char* codeB) { if (!codeA || !codeB) { return ILLEGAL_CODE; } @@ -156,3 +157,38 @@ OctalTreeDepth compareOctalCodes(unsigned char* codeA, unsigned char* codeB) { return result; } +OctalTreeDepth compareOctalCodes(unsigned char* codeA, unsigned char* codeB) { + if (!codeA || !codeB) { + return ILLEGAL_CODE; + } + + OctalTreeDepth result = LESS_THAN; // assume it's shallower + + int numberOfBytes = std::min(bytesRequiredForCodeLength(*codeA),bytesRequiredForCodeLength(*codeB)); + int compare = memcmp(codeA,codeB,numberOfBytes); + + if (compare < 0) { + result = LESS_THAN; + } else if (compare > 0) { + result = GREATER_THAN; + } else { + int codeLenthA = numberOfThreeBitSectionsInCode(codeA); + int codeLenthB = numberOfThreeBitSectionsInCode(codeB); + + if (codeLenthA == codeLenthB) { + // if the memcmp matched exactly, and they were the same length, + // then these must be the same code! + result = EXACT_MATCH; + } else { + // if the memcmp matched exactly, but they aren't the same length, + // then they have a matching common parent, but they aren't the same + if (codeLenthA < codeLenthB) { + result = LESS_THAN; + } else { + result = GREATER_THAN; + } + } + } + return result; +} + diff --git a/libraries/shared/src/OctalCode.h b/libraries/shared/src/OctalCode.h index 65c2fcca91..be939f2495 100644 --- a/libraries/shared/src/OctalCode.h +++ b/libraries/shared/src/OctalCode.h @@ -28,7 +28,9 @@ typedef enum { EQUAL_DEPTH, EXACT_MATCH, DEEPER, - ILLEGAL_CODE + ILLEGAL_CODE, + GREATER_THAN, + LESS_THAN } OctalTreeDepth; OctalTreeDepth compareOctalCodes(unsigned char* code1, unsigned char* code2);