Fixed Symbol problem - related to using std::numeric_limits::max() statically on keys created outside of functions

release/4.3a0
Alex Cunningham 2012-06-09 19:43:14 +00:00
parent f45c236585
commit 9e26b32daa
3 changed files with 43 additions and 34 deletions

View File

@ -703,6 +703,14 @@
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="testKey.run" path="build/gtsam/nonlinear" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments>-j5</buildArguments>
<buildTarget>testKey.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="schedulingExample.run" path="build/gtsam_unstable/discrete" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments>-j5</buildArguments>

View File

@ -16,8 +16,6 @@
* @author: Richard Roberts
*/
#pragma once
#include <gtsam/nonlinear/Symbol.h>
#include <boost/mpl/char.hpp>
@ -27,47 +25,50 @@
#include <boost/lambda/construct.hpp>
#include <boost/lambda/lambda.hpp>
#include <limits.h>
#include <list>
#include <iostream>
namespace gtsam {
static const size_t keyBits = sizeof(Key) * 8;
static const size_t chrBits = sizeof(unsigned char) * 8;
static const size_t indexBits = keyBits - chrBits;
static const Key chrMask = Key(std::numeric_limits<unsigned char>::max())
<< indexBits;
static const Key indexMask = ~chrMask;
static const size_t keyBits = sizeof(Key) * 8;
static const size_t chrBits = sizeof(unsigned char) * 8;
static const size_t indexBits = keyBits - chrBits;
static const Key chrMask = Key(UCHAR_MAX) << indexBits; // For some reason, std::numeric_limits<unsigned char>::max() fails
static const Key indexMask = ~chrMask;
Symbol::Symbol(Key key) {
c_ = (unsigned char) ((key & chrMask) >> indexBits);
j_ = key & indexMask;
}
Symbol::Symbol(Key key) {
c_ = (unsigned char) ((key & chrMask) >> indexBits);
j_ = key & indexMask;
}
Key Symbol::key() const {
if (j_ > indexMask)
throw std::invalid_argument("Symbol index is too large");
Key key = (Key(c_) << indexBits) | j_;
return key;
}
Key Symbol::key() const {
if (j_ > indexMask) {
boost::format msg("Symbol index is too large, j=%d, indexMask=%d");
msg % j_ % indexMask;
throw std::invalid_argument(msg.str());
}
Key key = (Key(c_) << indexBits) | j_;
return key;
}
void Symbol::print(const std::string& s) const {
std::cout << s << (std::string) (*this) << std::endl;
}
void Symbol::print(const std::string& s) const {
std::cout << s << (std::string) (*this) << std::endl;
}
bool Symbol::equals(const Symbol& expected, double tol) const {
return (*this) == expected;
}
bool Symbol::equals(const Symbol& expected, double tol) const {
return (*this) == expected;
}
Symbol::operator std::string() const {
return str(boost::format("%c%d") % c_ % j_);
}
Symbol::operator std::string() const {
return str(boost::format("%c%d") % c_ % j_);
}
boost::function<bool(Key)> Symbol::ChrTest(unsigned char c) {
namespace bl = boost::lambda;
return bl::bind(&Symbol::chr, bl::bind(bl::constructor<Symbol>(), bl::_1))
== c;
}
boost::function<bool(Key)> Symbol::ChrTest(unsigned char c) {
namespace bl = boost::lambda;
return bl::bind(&Symbol::chr, bl::bind(bl::constructor<Symbol>(), bl::_1))
== c;
}
} // namespace gtsam

View File

@ -21,11 +21,11 @@ using namespace boost::assign;
#include <gtsam/base/Testable.h>
#include <gtsam/base/TestableAssertions.h>
#include <gtsam/nonlinear/Symbol.h>
using namespace std;
using namespace gtsam;
Key aKey = gtsam::symbol_shorthand::X(4); // FIXME: throws index too large exception in Symbol.key()
Key aKey = gtsam::symbol_shorthand::X(4);
/* ************************************************************************* */
TEST(Key, KeySymbolConversion) {