-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutil.h
95 lines (70 loc) · 2.46 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
GPU plot generator for Burst coin.
Author: Cryo
Bitcoin: 138gMBhCrNkbaiTCmUhP9HLU9xwn5QKZgD
Burst: BURST-YA29-QCEW-QXC3-BKXDL
Based on the code of the official miner and dcct's plotgen.
*/
#ifndef CRYO_UTIL_H
#define CRYO_UTIL_H
#include <string>
#include <vector>
#include <exception>
namespace cryo {
namespace util {
std::vector<std::string> split(const std::string& p_string, const std::string& p_separator);
template<typename Iterator> std::string join(Iterator& p_begin, Iterator& p_end, const std::string& p_separator);
template<typename T> std::vector<T> splitValue(T p_value, const std::vector<T>& p_units) throw (std::exception);
template<typename T> std::string formatValue(T p_value, const std::vector<T>& p_units, const std::vector<std::string>& p_labels) throw (std::exception);
}}
#include <sstream>
#include <stdexcept>
namespace cryo {
namespace util {
template<typename Iterator>
std::string join(Iterator p_begin, Iterator p_end, const std::string& p_separator) {
if(p_begin == p_end) {
return "";
}
std::ostringstream out;
out << *p_begin++;
for(; p_begin != p_end ; p_begin++) {
out << p_separator << *p_begin;
}
return out.str();
}
template<typename T>
std::vector<T> splitValue(T p_value, const std::vector<T>& p_units) throw (std::exception) {
if(p_units.size() == 0) {
throw std::runtime_error("Units vector must contain at least one element");
}
std::vector<T> parts;
typename std::vector<T>::const_iterator it(p_units.begin());
typename std::vector<T>::const_iterator end(p_units.end());
do {
parts.push_back(p_value % *it);
p_value /= *it++;
} while(it != end && p_value > 0);
if(p_value > 0) {
parts.push_back(p_value);
}
return parts;
}
template<typename T>
std::string formatValue(T p_value, const std::vector<T>& p_units, const std::vector<std::string>& p_labels) throw (std::exception) {
std::vector<T> parts(splitValue(p_value, p_units));
if(parts.size() > p_labels.size()) {
throw std::runtime_error("Not enough labels to format the specified value");
}
typename std::vector<T>::const_reverse_iterator it(parts.crbegin());
typename std::vector<T>::const_reverse_iterator end(parts.crend());
std::vector<std::string>::const_iterator label(p_labels.begin() + parts.size() - 1);
std::ostringstream out;
out << *it++ << *label;
while(it != end) {
out << " " << *it++ << *--label;
}
return out.str();
}
}}
#endif