Skip to content
Snippets Groups Projects
Commit ee8a24d3 authored by Dr. Felix Tobias Schindler's avatar Dr. Felix Tobias Schindler
Browse files

[parameter] add first draft of Parameter ParameterType

parent b56201aa
No related branches found
No related tags found
No related merge requests found
......@@ -127,6 +127,10 @@ class reinterpretation_error : public Dune::Exception
{
};
class parameter_error : public Dune::Exception
{
};
class spe10_data_file_missing : public Dune::IOError
{
};
......
#include "config.h"
#include <type_traits>
#include "exceptions.hh"
#include "parameter.hh"
namespace Dune {
namespace XT {
namespace Common {
namespace internal {
// ========================
// ===== SimpleDict =====
// ========================
template <class ValueType>
SimpleDict<ValueType>::SimpleDict()
{
}
template <class ValueType>
SimpleDict<ValueType>::SimpleDict(const std::string& key, const ValueType& value)
: dict_({std::make_pair(key, value)})
{
}
template <class ValueType>
SimpleDict<ValueType>::SimpleDict(const std::vector<std::pair<std::string, ValueType>>& key_value_pairs)
{
for (const auto& key_value_pair : key_value_pairs)
dict_.emplace(key_value_pair);
}
template <class ValueType>
bool SimpleDict<ValueType>::empty() const
{
return dict_.size() == 0;
}
template <class ValueType>
bool SimpleDict<ValueType>::has_key(const std::string& key) const
{
return dict_.find(key) != dict_.end();
}
template <class ValueType>
void SimpleDict<ValueType>::set(const std::string& key, const ValueType& value, const bool overwrite)
{
if (!overwrite && has_key(key))
DUNE_THROW(Exceptions::parameter_error,
"You are trying to set a value (" << /*value <<*/ ") for the key '" << key
<< "', although a value is already set ("
// << dict_[key]
<< ") and overwrite is false!");
dict_[key] = value;
}
template <class ValueType>
const ValueType& SimpleDict<ValueType>::get(const std::string& key) const
{
const auto result = dict_.find(key);
if (result == dict_.end())
DUNE_THROW(Exceptions::parameter_error, "Key '" << key << "' does not exist!");
return result->second;
}
template <class ValueType>
bool SimpleDict<ValueType>::operator<(const SimpleDict<ValueType>& other) const
{
return dict_ < other.dict_;
}
template <class ValueType>
bool SimpleDict<ValueType>::operator==(const SimpleDict<ValueType>& other) const
{
return dict_ == other.dict_;
}
template <class ValueType>
bool SimpleDict<ValueType>::operator!=(const SimpleDict<ValueType>& other) const
{
return dict_ != other.dict_;
}
template <class ValueType>
size_t SimpleDict<ValueType>::size() const
{
return dict_.size();
}
template class SimpleDict<size_t>;
template class SimpleDict<std::vector<double>>;
} // namespace internal
// =========================
// ===== ParameterType =====
// =========================
ParameterType::ParameterType()
: BaseType()
{
}
ParameterType::ParameterType(const std::string& key, const size_t& sz)
: BaseType(key, sz)
{
}
ParameterType::ParameterType(const std::vector<std::pair<std::string, size_t>>& key_size_pairs)
: BaseType(key_size_pairs)
{
}
// std::string ParameterType::report() const
//{
// std::ostringstream ret;
// ret << "(";
// if (dict_.size() == 1) {
// const auto element = dict_.begin();
// ret << "\"" << element->first << "\", " << element->second;
// } else if (dict_.size() > 1) {
// const auto& kk = keys();
// const auto& vv = values();
// ret << "{\"";
// for (size_t ii = 0; ii < (kk.size() - 1); ++ii)
// ret << kk[ii] << "\", \"";
// ret << kk[kk.size() - 1] << "\"}, {";
// for (size_t ii = 0; ii < (vv.size() - 1); ++ii)
// ret << vv[ii] << ", ";
// ret << vv[vv.size() - 1] << "}";
// }
// ret << ")";
// return ret.str();
//}
// std::ostream& operator<<(std::ostream& oo, const ParameterType& pp)
//{
// oo << pp.report();
// return oo;
//}
// =====================
// ===== Parameter =====
// =====================
Parameter::Parameter(const double& value)
: BaseType("None", {value})
{
}
Parameter::Parameter(const std::string& key, const double& value)
: BaseType(key, {value})
{
}
Parameter::Parameter(const std::string& key, const ValueType& value)
: BaseType(key, value)
{
}
Parameter::Parameter(const std::vector<std::pair<std::string, ValueType>>& key_value_pairs)
: BaseType(key_value_pairs)
{
}
ParameterType Parameter::type() const
{
ParameterType ret;
for (const auto key_value_pairs : dict_)
ret.set(key_value_pairs.first, key_value_pairs.second.size());
return ret;
}
// std::string Parameter::report() const
//{
// std::ostringstream ret;
// ret << "(";
// if (dict_.size() == 1) {
// const auto element = dict_.begin();
// ret << "\"" << element->first << "\", ";
// const auto& second = element->second;
// if (second.size() == 1) {
// ret << second[0];
// } else {
// ret << "{";
// for (size_t ii = 0; ii < (second.size() - 1); ++ii)
// ret << second[ii] << ", ";
// ret << second[second.size() - 1] << "}";
// }
// } else if (dict_.size() > 1) {
// const auto& kk = keys();
// const auto& vv = values();
// ret << "{\"";
// for (size_t ii = 0; ii < (kk.size() - 1); ++ii)
// ret << kk[ii] << "\", \"";
// ret << kk[kk.size() - 1] << "\"}, {";
// for (size_t ii = 0; ii < (vv.size() - 1); ++ii) {
// if (vv[ii].size() == 1) {
// ret << vv[ii][0] << ", ";
// } else {
// ret << "{";
// for (size_t jj = 0; jj < (vv[ii].size() - 1); ++jj)
// ret << vv[ii][jj] << ", ";
// ret << vv[ii][vv[ii].size() - 1] << "}, ";
// }
// }
// if (vv[vv.size() - 1].size() == 1) {
// ret << vv[vv.size() - 1][0] << "}";
// } else {
// ret << "{";
// for (size_t jj = 0; jj < (vv[vv.size() - 1].size() - 1); ++jj)
// ret << vv[vv.size() - 1][jj] << ", ";
// ret << vv[vv.size() - 1][vv[vv.size() - 1].size() - 1] << "}";
// }
// }
// ret << ")";
// return ret.str();
//}
// std::ostream& operator<<(std::ostream& oo, const Parameter& pp)
//{
// oo << pp.report();
// return oo;
//}
} // namespace Common
} // namespace XT
} // namespace Dune
#ifndef DUNE_XT_COMMON_PARAMETER_HH
#define DUNE_XT_COMMON_PARAMETER_HH
#include <string>
#include <map>
#include <vector>
#include <initializer_list>
#include <sstream>
#include <ostream>
namespace Dune {
namespace XT {
namespace Common {
namespace internal {
template <class ValueType>
class SimpleDict
{
public:
SimpleDict();
SimpleDict(const std::string& key, const ValueType& value);
SimpleDict(const std::vector<std::pair<std::string, ValueType>>& key_value_pairs);
bool empty() const;
bool has_key(const std::string& key) const;
void set(const std::string& key, const ValueType& value, const bool overwrite = false);
const ValueType& get(const std::string& key) const;
bool operator<(const SimpleDict<ValueType>& other) const;
bool operator==(const SimpleDict<ValueType>& other) const;
bool operator!=(const SimpleDict<ValueType>& other) const;
size_t size() const;
// std::string report() const;
protected:
std::map<std::string, ValueType> dict_;
}; // class SimpleDict
} // namespace internal
class ParameterType : public internal::SimpleDict<size_t>
{
typedef internal::SimpleDict<size_t> BaseType;
public:
ParameterType();
ParameterType(const std::string& key, const size_t& sz);
ParameterType(const std::vector<std::pair<std::string, size_t>>& key_size_pairs);
}; // class ParameterType
// std::ostream& operator<<(std::ostream& out, const ParameterType& param_type);
class Parameter : public internal::SimpleDict<std::vector<double>>
{
typedef SimpleDict<std::vector<double>> BaseType;
typedef std::vector<double> ValueType;
public:
Parameter(const std::vector<std::pair<std::string, ValueType>>& key_value_pairs = {});
Parameter(const double& value);
Parameter(const std::string& key, const double& value);
Parameter(const std::string& key, const ValueType& value);
// Parameter(const ParameterType& param_type, const std::vector<ValueType>& values);
ParameterType type() const;
}; // class Parameter
// std::ostream& operator<<(std::ostream& out, const Parameter& mu);
} // namespace Common
} // namespace XT
} // namespace Dune
#endif // DUNE_XT_COMMON_PARAMETER_HH
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment