From aeb2474d55331685329ed2e74b77929e1a5137c9 Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Sat, 9 Jun 2012 18:19:45 +0000 Subject: [PATCH] Made a cpp file that makes the header cleaner, and avoids superheavy includes. --- gtsam/nonlinear/Symbol.cpp | 73 ++++++++++++++++++++++++++++++++++++++ gtsam/nonlinear/Symbol.h | 62 ++++++++++---------------------- 2 files changed, 91 insertions(+), 44 deletions(-) create mode 100644 gtsam/nonlinear/Symbol.cpp diff --git a/gtsam/nonlinear/Symbol.cpp b/gtsam/nonlinear/Symbol.cpp new file mode 100644 index 000000000..a161e2fd7 --- /dev/null +++ b/gtsam/nonlinear/Symbol.cpp @@ -0,0 +1,73 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file Symbol.cpp + * @date June 9, 2012 + * @author: Frank Dellaert + * @author: Richard Roberts + */ + +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +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::max()) + << indexBits; + static const Key indexMask = ~chrMask; + + 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; + } + + 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; + } + + Symbol::operator std::string() const { + return str(boost::format("%c%d") % c_ % j_); + } + + boost::function Symbol::ChrTest(unsigned char c) { + namespace bl = boost::lambda; + return bl::bind(&Symbol::chr, bl::bind(bl::constructor(), bl::_1)) + == c; + } + +} // namespace gtsam + diff --git a/gtsam/nonlinear/Symbol.h b/gtsam/nonlinear/Symbol.h index 03fd4aa1f..b4bda4f1a 100644 --- a/gtsam/nonlinear/Symbol.h +++ b/gtsam/nonlinear/Symbol.h @@ -19,17 +19,7 @@ #pragma once #include - -#include -#include #include -#include -#include -#include -#include - -#include -#include namespace gtsam { @@ -45,6 +35,7 @@ protected: size_t j_; public: + /** Default constructor */ Symbol() : c_(0), j_(0) { @@ -61,39 +52,19 @@ public: } /** Constructor that decodes an integer Key */ - Symbol(Key key) { - const size_t keyBits = sizeof(Key) * 8; - const size_t chrBits = sizeof(unsigned char) * 8; - const size_t indexBits = keyBits - chrBits; - const Key chrMask = Key(std::numeric_limits::max()) << indexBits; - const Key indexMask = ~chrMask; - c_ = (unsigned char)((key & chrMask) >> indexBits); - j_ = key & indexMask; - } + Symbol(Key key); /** return Key (integer) representation */ - Key key() const { - const size_t keyBits = sizeof(Key) * 8; - const size_t chrBits = sizeof(unsigned char) * 8; - const size_t indexBits = keyBits - chrBits; - const Key chrMask = Key(std::numeric_limits::max()) << indexBits; - const Key indexMask = ~chrMask; - if(j_ > indexMask) - throw std::invalid_argument("Symbol index is too large"); - Key key = (Key(c_) << indexBits) | j_; - return key; - } + Key key() const; /** Cast to integer */ operator Key() const { return key(); } - // Testable Requirements - void print(const std::string& s = "") const { - std::cout << s << (std::string) (*this) << std::endl; - } - bool equals(const Symbol& expected, double tol = 0.0) const { - return (*this) == expected; - } + /// Print + void print(const std::string& s = "") const; + + /// Check equality + bool equals(const Symbol& expected, double tol = 0.0) const; /** Retrieve key character */ unsigned char chr() const { @@ -106,23 +77,29 @@ public: } /** Create a string from the key */ - operator std::string() const { - return str(boost::format("%c%d") % c_ % j_); - } + operator std::string() const; /** Comparison for use in maps */ bool operator<(const Symbol& comp) const { return c_ < comp.c_ || (comp.c_ == c_ && j_ < comp.j_); } + + /** Comparison for use in maps */ bool operator==(const Symbol& comp) const { return comp.c_ == c_ && comp.j_ == j_; } + + /** Comparison for use in maps */ bool operator==(Key comp) const { return comp == (Key)(*this); } + + /** Comparison for use in maps */ bool operator!=(const Symbol& comp) const { return comp.c_ != c_ || comp.j_ != j_; } + + /** Comparison for use in maps */ bool operator!=(Key comp) const { return comp != (Key)(*this); } @@ -132,10 +109,7 @@ public: * Values::filter() function to retrieve all key-value pairs with the * requested character. */ - static boost::function ChrTest(unsigned char c) { - namespace bl = boost::lambda; - return bl::bind(&Symbol::chr, bl::bind(bl::constructor(), bl::_1)) == c; - } + static boost::function ChrTest(unsigned char c); private: