/* * timeSymbolMaps.cpp * * Created on: Jan 20, 2010 * Author: richard */ #include #include #include #include #include #include #include "Key.h" using namespace std; using namespace boost; using namespace gtsam; template class SymbolMapExp { private: typedef map > Map; typedef vector Vec; Map values_; public: typedef pair value_type; SymbolMapExp() {} T& at(const Symbol& key) { typename Map::iterator it = values_.find(key.chr()); if(it != values_.end()) return it->second.at(key.index()); else throw invalid_argument("Key " + (string)key + " not present"); } void set(const Symbol& key, const T& value) { Vec& vec(values_[key.chr()]); //vec.reserve(10000); if(key.index() >= vec.size()) { vec.reserve(key.index()+1); vec.resize(key.index()); vec.push_back(value); } else vec[key.index()] = value; } }; template class SymbolMapBinary : public std::map { private: typedef std::map Base; public: SymbolMapBinary() : std::map() {} T& at(const Symbol& key) { typename Base::iterator it = Base::find(key); if (it == Base::end()) throw(std::invalid_argument("SymbolMap::[] invalid key: " + (std::string)key)); return it->second; } }; struct SymbolHash : public std::unary_function { std::size_t operator()(Symbol const& x) const { std::size_t seed = 0; boost::hash_combine(seed, x.chr()); boost::hash_combine(seed, x.index()); return ((size_t(x.chr()) << 24) & x.index()); } }; template class SymbolMapHash : public boost::unordered_map { public: SymbolMapHash() : boost::unordered_map(60000) {} }; struct Value { double v; Value() : v(0.0) {} Value(double vi) : v(vi) {} operator string() { return lexical_cast(v); } bool operator!=(const Value& vc) { return v != vc.v; } }; #define ELEMS 3000 #define TIMEAT 300 int main(int argc, char *argv[]) { timer tmr; // pre-allocate cout << "Generating test data ..." << endl; vector > values; for(size_t i=0; i binary; for(size_t i=0; i hash; for(size_t i=0; i experimental; for(size_t i=0; i