#ifndef _utl_ParameterStorage_h_ #define _utl_ParameterStorage_h_ /** \file Template class allowing to store parameters and their covariances using enums to address them with a general interface. \author Tim Huege */ #include #include #include #include #include namespace utl { /** \class ParameterStorage ParameterStorage.h "utl/ParameterStorage.h" \brief Template class allowing to store parameters and their covariances using enums to address them with a general interface. \param IndexEnum The enum class defining the list of allowed parameters. \param ParameterType The data type of the parameters to be stored, usually double. \param ParameterCovarianceType The data type of the covariances to be stored, usually double. \author Tim Huege \date 03 Mar 2011 \ingroup stl */ template class ParameterStorage { public: typedef std::map ParameterMap; typedef std::map, ParameterCovarianceType> ParameterCovarianceMap; typedef std::vector EnumVector; /// determine whether a parameter has been set bool HasParameter(const IndexEnum param) const { return fParameterMap.find(param) != fParameterMap.end(); } /// get the value of a parameter, throw exception if parameter has not been set const ParameterType& GetParameter(const IndexEnum param) const { const typename ParameterMap::const_iterator it = fParameterMap.find(param); if (it != fParameterMap.end()) return it->second; else { std::stringstream sstr; sstr << "Tried to get an unset parameter (" << param << ") from ParameterStorage."; throw NonExistentComponentException(sstr.str().c_str()); } } /// set the value of a parameter void SetParameter(const IndexEnum param, const ParameterType value) { fParameterMap[param] = value; } /// determine whether a parameter covariance has been set bool HasParameterCovariance(const IndexEnum param1, const IndexEnum param2) const { const std::pair covarianceKey = GenCovarianceKey(param1, param2); return fParameterCovarianceMap.find(covarianceKey) != fParameterCovarianceMap.end(); } /// get the value of a parameter covariance, throw exception if parameter covariance is not set const ParameterCovarianceType& GetParameterCovariance(const IndexEnum param1, const IndexEnum param2) const { const std::pair covarianceKey = GenCovarianceKey(param1, param2); const typename ParameterCovarianceMap::const_iterator it = fParameterCovarianceMap.find(covarianceKey); if (it != fParameterCovarianceMap.end()) return it->second; else { std::stringstream sstr; sstr << "Tried to get an unset parameter covariance (" << covarianceKey.first << ", " << covarianceKey.second << ") from ParameterStorage."; throw NonExistentComponentException(sstr.str().c_str()); } } /// set the value of a parameter void SetParameterCovariance(const IndexEnum param1, const IndexEnum param2, const ParameterCovarianceType value) { const std::pair covarianceKey = GenCovarianceKey(param1, param2); fParameterCovarianceMap[covarianceKey] = value; } /// get a std::vector containing the enums for which parameters have been set std::vector GetEnumVector() const { std::vector enumVector; enumVector.reserve(fParameterMap.size()); typename std::map::const_iterator it; for( it = fParameterMap.begin(); it != fParameterMap.end(); ++it ) { enumVector.push_back(it->first); } return enumVector; } /// get a std::vector containing the enum-pairs for which covariances have been set std::vector< std::pair > GetCovarianceEnumVector() const { std::vector< std::pair > enumVector; enumVector.reserve(fParameterCovarianceMap.size()); typename ParameterCovarianceMap::const_iterator it; for(it = fParameterCovarianceMap.begin(); it != fParameterCovarianceMap.end(); ++it) { enumVector.push_back(it->first); } return enumVector; } private: /// helper function to create the key for the covariance map from the parameters using sorting std::pair GenCovarianceKey(const IndexEnum param1, const IndexEnum param2) const { if (long(param1) < long(param2)) return std::make_pair(param1, param2); else return std::make_pair(param2, param1); } /// the map containing the stored parameters of type ParameterType ParameterMap fParameterMap; /// the map containing the stored parameter covariances of type ParameterCovarianceType ParameterCovarianceMap fParameterCovarianceMap; }; } // utl #endif