Added new version of compareOctalCodes() which actually compares octal codes

This commit is contained in:
ZappoMan 2013-04-25 23:42:35 -07:00
parent c014abbe6c
commit 3f158c0753
2 changed files with 40 additions and 2 deletions

View file

@ -7,6 +7,7 @@
//
#include <cmath>
#include <algorithm> // std:min
#include <cstring>
#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;
}

View file

@ -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);