From c88c63efadad2843d3dff9007b6b11d236455559 Mon Sep 17 00:00:00 2001 From: Peter Peterson <petersonpf@ornl.gov> Date: Thu, 19 Jul 2012 16:24:55 -0400 Subject: [PATCH] Refs #5269. Add methods to convert vectors and sets of to strings. --- .../Kernel/inc/MantidKernel/Strings.h | 6 +++ Code/Mantid/Framework/Kernel/src/Strings.cpp | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/Strings.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/Strings.h index af122ab1e00..2b00535e3fd 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/Strings.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/Strings.h @@ -6,6 +6,7 @@ //---------------------------------------------------------------------- #include "MantidKernel/DllConfig.h" #include <iostream> +#include <set> #include <sstream> #include <vector> #include <iterator> @@ -110,6 +111,11 @@ template<typename T> int convert(const char* A,T& out); /// Convert a number to a string template<typename T> std::string toString(const T value); +/// Convert a vector to a string +template<typename T> std::string toString(const std::vector<T> &value); + +/// Convert a set to a string +template<typename T> std::string toString(const std::set<T> &value); template<typename T> int setValues(const std::string& Line,const std::vector<int>& Index,std::vector<T>& Out); diff --git a/Code/Mantid/Framework/Kernel/src/Strings.cpp b/Code/Mantid/Framework/Kernel/src/Strings.cpp index 9815b0efb02..d8332d14093 100644 --- a/Code/Mantid/Framework/Kernel/src/Strings.cpp +++ b/Code/Mantid/Framework/Kernel/src/Strings.cpp @@ -681,6 +681,45 @@ std::string toString(const T value) return mess.str(); } +/** + * This assumes that the vector is sorted. + * + * @param value :: templated value (only works for integer types) to convert. + * @return A reduced string representation. + */ +template<typename T> +std::string toString(const std::vector<T> &value) +{ + std::ostringstream mess; + auto it = value.begin(); + auto last = value.end(); + T start; + T stop; + for (; it != last; ++it) + { + start = *(it); + stop = start; + for ( ; it != last; ++it) + { + if ( (stop + static_cast<T>(1)) == *(it+1) ) + stop = *(it+1); + else + break; + } + mess << start; + if (start != stop) + mess << "-" << stop; + if (it+1 != last) + mess << ","; + } + return mess.str(); +} + +template<typename T> +std::string toString(const std::set<T> &value) +{ + return toString(std::vector<T>(value.begin(), value.end())); +} //------------------------------------------------------------------------------------------------ /** @@ -1014,6 +1053,14 @@ template MANTID_KERNEL_DLL std::string toString(const size_t value); // Matches #endif template MANTID_KERNEL_DLL std::string toString(const std::string value); +// this block should generate the vector ones as well +template MANTID_KERNEL_DLL std::string toString(const std::set<int> &value); +template MANTID_KERNEL_DLL std::string toString(const std::set<int16_t> &value); +template MANTID_KERNEL_DLL std::string toString(const std::set<size_t> &value); // Matches uint64_t on Linux 64 & Win 64 +#if defined(__APPLE__) || ( defined(_WIN32) && !defined(_WIN64)) || (defined(__GNUC__) && !defined(__LP64__)) // Mac or 32-bit compiler + template MANTID_KERNEL_DLL std::string toString(const std::set<uint64_t> &value); +#endif + template MANTID_KERNEL_DLL int convPartNum(const std::string&,double&); template MANTID_KERNEL_DLL int convPartNum(const std::string&,int&); -- GitLab