From 7b25c857ded357c0cb0b481dac6404c27ed0293d Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Wed, 5 Jul 2017 15:15:42 +0200 Subject: Reduce stringstream creation/imbue overhead --- src/Utilities.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/Utilities.cpp') diff --git a/src/Utilities.cpp b/src/Utilities.cpp index eb06d8b..eb34092 100644 --- a/src/Utilities.cpp +++ b/src/Utilities.cpp @@ -73,7 +73,24 @@ std::vector stringToFloatVector(const std::string &s) std::vector stringToDoubleVector(const std::string &s) { - return stringToVector(s); + std::vector out; + out.reserve(100); + std::istringstream iss; + iss.imbue(std::locale::classic()); + size_t current = 0; + size_t next; + do { + next = s.find_first_of(",;", current); + std::string t = s.substr(current, next - current); + iss.str(t); + iss.clear(); + double f; + iss >> f; + out.push_back(f); + current = next + 1; + } while (next != std::string::npos); + + return out; } template -- cgit v1.2.3 From 8d8a3d26372d9c0a784e181121fc2b360f6fee51 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Thu, 31 Aug 2017 16:47:29 +0200 Subject: Add trailing ,/; to string repr of float vector/matrix This makes it possible to differentiate between a scalar and a one-element vector, and fixes #111. --- src/Utilities.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/Utilities.cpp') diff --git a/src/Utilities.cpp b/src/Utilities.cpp index eb34092..c90e67b 100644 --- a/src/Utilities.cpp +++ b/src/Utilities.cpp @@ -77,6 +77,7 @@ std::vector stringToDoubleVector(const std::string &s) out.reserve(100); std::istringstream iss; iss.imbue(std::locale::classic()); + size_t length = s.size(); size_t current = 0; size_t next; do { @@ -88,7 +89,7 @@ std::vector stringToDoubleVector(const std::string &s) iss >> f; out.push_back(f); current = next + 1; - } while (next != std::string::npos); + } while (next != std::string::npos && current != length); return out; } @@ -97,6 +98,7 @@ template std::vector stringToVector(const std::string& s) { std::vector out; + size_t length = s.size(); size_t current = 0; size_t next; do { @@ -104,7 +106,7 @@ std::vector stringToVector(const std::string& s) std::string t = s.substr(current, next - current); out.push_back(stringTo(t)); current = next + 1; - } while (next != std::string::npos); + } while (next != std::string::npos && current != length); return out; } @@ -134,13 +136,14 @@ void splitString(std::vector &items, const std::string& s, const char *delim) { items.clear(); + size_t length = s.size(); size_t current = 0; size_t next; do { next = s.find_first_of(delim, current); items.push_back(s.substr(current, next - current)); current = next + 1; - } while (next != std::string::npos); + } while (next != std::string::npos && current != length); } } -- cgit v1.2.3 From d1e2b4f904266c94f4ebf9fc5c95d495c95b0d5c Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Tue, 17 Oct 2017 22:06:13 +0200 Subject: Improve stringToFloatVector to match stringToDoubleVector --- src/Utilities.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/Utilities.cpp') diff --git a/src/Utilities.cpp b/src/Utilities.cpp index c90e67b..2ae1b66 100644 --- a/src/Utilities.cpp +++ b/src/Utilities.cpp @@ -66,14 +66,10 @@ double stringToDouble(const std::string& s) template<> float stringTo(const std::string& s) { return stringToFloat(s); } template<> double stringTo(const std::string& s) { return stringToDouble(s); } -std::vector stringToFloatVector(const std::string &s) -{ - return stringToVector(s); -} - -std::vector stringToDoubleVector(const std::string &s) +template +std::vector stringToNumericVector(const std::string &s) { - std::vector out; + std::vector out; out.reserve(100); std::istringstream iss; iss.imbue(std::locale::classic()); @@ -85,7 +81,7 @@ std::vector stringToDoubleVector(const std::string &s) std::string t = s.substr(current, next - current); iss.str(t); iss.clear(); - double f; + T f; iss >> f; out.push_back(f); current = next + 1; @@ -94,6 +90,15 @@ std::vector stringToDoubleVector(const std::string &s) return out; } +std::vector stringToFloatVector(const std::string &s) +{ + return stringToNumericVector(s); +} +std::vector stringToDoubleVector(const std::string &s) +{ + return stringToNumericVector(s); +} + template std::vector stringToVector(const std::string& s) { -- cgit v1.2.3