diff --git a/cpp/Key.h b/cpp/Key.h index b12793ab7..c641f9a20 100644 --- a/cpp/Key.h +++ b/cpp/Key.h @@ -111,13 +111,13 @@ namespace gtsam { /** * Encoding two numbers into a single size_t for conversion to Symbol - * Stores the label in the upper bits of the index - * - * STILL IN TESTING - DO NOT USE!! + * Stores the label in the upper bytes of the index */ - size_t encode() { - //short label = label_; - return this->j_; // + (label << 32); + size_t encode() const { + short label = (short) label_; //bound size of label to 2 bytes + size_t shift = (sizeof(size_t)-sizeof(short)) * 8; + size_t modifier = ((size_t) label) << shift; + return this->j_ + modifier; } @@ -184,7 +184,7 @@ namespace gtsam { /** Casting constructor from TypedLabeledSymbol */ template - Symbol(const TypedLabeledSymbol& symbol): c_(C), j_(symbol.encode()) {} + Symbol(const TypedLabeledSymbol& symbol): c_(C), j_(symbol.encode()) {} /** "Magic" key casting constructor from string */ #ifdef GTSAM_MAGIC_KEY diff --git a/cpp/testKey.cpp b/cpp/testKey.cpp index 867cc2dd2..0c64ebbd4 100644 --- a/cpp/testKey.cpp +++ b/cpp/testKey.cpp @@ -54,27 +54,29 @@ TEST ( TypedLabledSymbol, basic_operations ) { CHECK(key5 < key6); } -/* ************************************************************************* * +/* ************************************************************************* */ TEST ( TypedLabledSymbol, encoding ) { typedef TypedLabeledSymbol RobotKey; - cout << "short : " << sizeof(short) << " size_t: " << sizeof(size_t) << endl; - cout << "unsigned int : " << sizeof(unsigned int) << endl; - RobotKey key1(37, 'A'); - size_t index = key1.index(); - size_t modifier = 65; - modifier = modifier << sizeof(size_t) * 4; - index += modifier; - cout << "index: " << index << " modifier: " << modifier << endl; + // Note: calculations done in test due to possible differences between machines + // take the upper two bytes for the label + short label = key1.label(); + // find the shift necessary + size_t shift = (sizeof(size_t)-sizeof(short)) * 8; + size_t modifier = label; + modifier = modifier << shift; + size_t index = key1.index() + modifier; -// short encoded = 65; -// size_t modifier = encoded << 32; -// size_t index = 37 + encoded; -// Symbol act(key1), exp('x', index); -// CHECK(assert_equal(exp, act)); + // check index encoding + Symbol act1(key1), exp('x', index); + CHECK(assert_equal(exp, act1)); + + // check casting + Symbol act2 = (Symbol) key1; + CHECK(assert_equal(exp, act2)); }