Commit 67235967 authored by Tobias WEBER's avatar Tobias WEBER

simplified string conversion

parent 875e9a68
......@@ -32,12 +32,50 @@
namespace m {
// ----------------------------------------------------------------------------
// helpers
// ----------------------------------------------------------------------------
// constants
template<typename T> constexpr T pi = T(M_PI);
template<typename T> T golden = T(0.5) + std::sqrt(T(5))/T(2);
/**
* converts a string to a scalar value
*/
template<class t_scalar=double, class t_str=std::string>
t_scalar stoval(const t_str& str)
{
if constexpr(std::is_same_v<t_scalar, float>)
return std::stof(str);
else if constexpr(std::is_same_v<t_scalar, double>)
return std::stod(str);
else if constexpr(std::is_same_v<t_scalar, long double>)
return std::stold(str);
else if constexpr(std::is_same_v<t_scalar, int>)
return std::stoi(str);
// else if constexpr(std::is_same_v<t_scalar, unsigned int>)
// return std::stoui(str);
else if constexpr(std::is_same_v<t_scalar, long>)
return std::stol(str);
else if constexpr(std::is_same_v<t_scalar, unsigned long>)
return std::stoul(str);
else if constexpr(std::is_same_v<t_scalar, long long>)
return std::stoll(str);
else if constexpr(std::is_same_v<t_scalar, unsigned long long>)
return std::stoull(str);
else
{
t_scalar val{};
std::istringstream{str} >> val;
return val;
}
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// concepts
// ----------------------------------------------------------------------------
......@@ -1056,10 +1094,7 @@ requires m::is_basic_vec<t_vec> && m::is_dyn_vec<t_vec>
for(auto& tok : vecstr)
{
boost::trim(tok);
std::istringstream istr(tok);
typename t_vec::value_type c{};
istr >> c;
typename t_vec::value_type c = m::stoval<typename t_vec::value_type>(tok);
vec.emplace_back(std::move(c));
}
......
......@@ -124,12 +124,12 @@ load_cif(const std::string& filename, t_real eps=1e-6)
// lattice
t_real a{}, b{}, c{}, alpha{}, beta{}, gamma{};
if(auto val = block.find_values("_cell_length_a"); val.length()) std::istringstream{val[0]} >> a;
if(auto val = block.find_values("_cell_length_b"); val.length()) std::istringstream{val[0]} >> b;
if(auto val = block.find_values("_cell_length_c"); val.length()) std::istringstream{val[0]} >> c;
if(auto val = block.find_values("_cell_angle_alpha"); val.length()) std::istringstream{val[0]} >> alpha;
if(auto val = block.find_values("_cell_angle_beta"); val.length()) std::istringstream{val[0]} >> beta;
if(auto val = block.find_values("_cell_angle_gamma"); val.length()) std::istringstream{val[0]} >> gamma;
if(auto val = block.find_values("_cell_length_a"); val.length()) a = m::stoval<t_real>(val[0]);
if(auto val = block.find_values("_cell_length_b"); val.length()) b = m::stoval<t_real>(val[0]);
if(auto val = block.find_values("_cell_length_c"); val.length()) c = m::stoval<t_real>(val[0]);
if(auto val = block.find_values("_cell_angle_alpha"); val.length()) alpha = m::stoval<t_real>(val[0]);
if(auto val = block.find_values("_cell_angle_beta"); val.length()) beta = m::stoval<t_real>(val[0]);
if(auto val = block.find_values("_cell_angle_gamma"); val.length()) gamma = m::stoval<t_real>(val[0]);
Lattice<t_real> latt{.a=a, .b=b, .c=c, .alpha=alpha, .beta=beta, .gamma=gamma};
......@@ -143,10 +143,9 @@ load_cif(const std::string& filename, t_real eps=1e-6)
{
atomnames.push_back(tabAtoms[row][0]);
t_real x{}, y{}, z{};
std::istringstream{tabAtoms[row][1]} >> x;
std::istringstream{tabAtoms[row][2]} >> y;
std::istringstream{tabAtoms[row][3]} >> z;
const t_real x = m::stoval<t_real>(tabAtoms[row][1]);
const t_real y = m::stoval<t_real>(tabAtoms[row][2]);
const t_real z = m::stoval<t_real>(tabAtoms[row][3]);
atoms.emplace_back(t_vec{{x, y, z}});
}
......
......@@ -26,6 +26,8 @@ using t_mat_cplx = m::mat<t_cplx, std::vector>;
int main()
{
//std::cout << m::stoval<unsigned int>("123") << std::endl;
auto x = m::create<t_vec>({1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
auto y = m::create<t_vec>({5, 5, 7, 9, 9.5, 10.5, 10.5, 12, 13.5, 14});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment