4 #include <boost/lexical_cast.hpp>
5 #include <boost/algorithm/string/trim.hpp>
6 #include <boost/tokenizer.hpp>
13 #include <type_traits>
23 template<
typename T,
typename U>
29 return boost::lexical_cast<T>(x);
36 AsHex(
const T& x,
const unsigned int width = 2*
sizeof(T))
38 std::ostringstream oss;
39 oss <<
"0x" << std::setfill(
'0') << std::setw(width) << std::hex << x;
47 AsBinary(
const T& x,
const unsigned int bitWidth = 8*
sizeof(T))
49 std::ostringstream oss;
51 for (
int i = bitWidth-1; i >= 0; --i)
52 oss << ((x >> i) & 1);
58 template<
class S,
class V>
62 std::ostringstream oss;
66 for (
const auto end = v.end(); it != end; ++it)
80 std::istringstream iss(str);
81 return std::vector<T>(std::istream_iterator<T>(iss), std::istream_iterator<T>());
89 boost::algorithm::trim(str);
94 std::vector<std::string>
Split(
const std::string& str,
const char*
const separators =
" ");
103 template<typename T, typename = typename std::enable_if<std::is_integral<T>::value, T>::type>
105 {
return (n > 1) ?
"s" :
""; }
108 template<
class Container,
typename T =
typename Container::value_type>
110 {
return (c.size() > 1) ?
"s" :
""; }
113 template<
typename Map>
117 const std::string& quote =
"'",
118 const std::string& separator =
", ")
120 std::ostringstream os;
122 const auto end = m.end();
124 os << quote << it->first << quote;
125 for (++it; it != end; ++it)
126 os << separator << quote << it->first << quote;
135 AsBinary(
const T& number,
const int maxBits = 8*
sizeof(T),
136 const char separator =
' ',
const int stride = 4)
138 std::ostringstream os;
139 int bit = std::min(maxBits,
int(8*
sizeof(T)));
141 T mask = (T(1) << --bit);
142 os << bool(number & mask);
143 for (--bit; bit >= 0; --bit) {
145 if (!((bit+1) % stride))
147 os << bool(number & mask);
155 std::pair<double, char>
159 const double lnt = std::log(thousand);
160 const double r = std::log(x) / lnt;
161 const int i = floor(r);
163 case 8: m =
'Y';
break;
164 case 7: m =
'Z';
break;
165 case 6: m =
'E';
break;
166 case 5: m =
'P';
break;
167 case 4: m =
'T';
break;
168 case 3: m =
'G';
break;
169 case 2: m =
'M';
break;
170 case 1: m =
'k';
break;
171 case 0: m =
'\0';
break;
172 case -1: m =
'm';
break;
173 case -2: m =
'u';
break;
174 case -3: m =
'n';
break;
175 case -4: m =
'p';
break;
176 case -5: m =
'f';
break;
177 case -6: m =
'a';
break;
178 case -7: m =
'z';
break;
179 case -8: m =
'y';
break;
180 default: m =
'?';
break;
182 const double rem = std::exp(lnt * (r - i));
183 return std::make_pair(rem, m);
187 template<
typename Geo,
typename CS>
190 Make(
const Geo&
g,
const CS& cs,
const double unit = 1,
const std::string& sep =
" ")
192 const auto& x = g.GetCoordinates(cs);
193 std::ostringstream os;
194 os << x.template get<0>()/
unit << sep << x.template get<1>()/
unit << sep << x.template get<2>();
std::string Make(const Geo &g, const CS &cs, const double unit=1, const std::string &sep=" ")
std::pair< double, char > AsHumanReadable(const T &x, const int thousand=1000)
std::string Strip(std::string str)
const char * Plural(const T n)
oss<< "0b";oss<< ((x >> i)&1);return oss.str();}template< class S, class V > std::string Join(const S &sep, const V &v)
std::string AsBinary(const T &number, const int maxBits=8 *sizeof(T), const char separator= ' ', const int stride=4)
converts integer-type numbers into a string of binary representation
T As(const U &x)
useful shorthand for boost::lexical_cast
std::string OfMapKeys(const Map &m, const std::string "e="'", const std::string &separator=", ")
std::vector< T > AsVectorOf(const std::string &str)
string OfSortedIds(vector< int > ids)
vector< string > Split(const string &str, const char *const separators)