-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpch_util.h
195 lines (152 loc) · 3.41 KB
/
pch_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifndef __PCH_UTIL__
#define __PCH_UTIL__
#include "./pch_cpp.h"
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
template<typename T>
T bytes2T(unsigned char *bytes) {
T res = 0;
int n = sizeof(T);
memcpy(&res, bytes, n);
return res;
}
template<typename T>
auto T2bytes(T u)->std::shared_ptr<unsigned char> {
int n = sizeof(T);
auto share_buf = std::shared_ptr< unsigned char>(new (std::nothrow) unsigned char[n],
[](unsigned char* ptr){
delete [] ptr;
printf("del addr : %ld\n",ptr);
ptr = nullptr;
});
if(auto ptr_buf = share_buf.get()){
printf("new addr : %ld\n",ptr_buf);
memcpy(ptr_buf, &u, n);
}
return share_buf;
}
void printLIst(ListNode* node){
while(node){
cout << std::right << std::setw(4) << node->val<<"\t";
node = node->next;
}
cout <<endl;
}
//
//
//
//ListNode* reverse_node(ListNode* node){
//
// ListNode* prevNode = nullptr;;
// ListNode* curNode = node;
// ListNode* nextNode = nullptr;
//
// while(curNode){
// nextNode = curNode->next;
// curNode->next = prevNode;
// prevNode = curNode;
// curNode = nextNode;
//
// }
//
//
// return prevNode;
//}
template<class PodType, class Allocator>
void printVec(std::vector<PodType, Allocator>& Vec,bool toInt = false) {
size_t index = 0;
for (auto element : Vec) {
if(toInt)
cout << std::right << std::setw(4) << index << " : " << std::right << std::setw(10) << "[" <<(uint32_t)element << "]" << endl;
else
cout << std::right << std::setw(4) << index << " : " << std::right << std::setw(10) << "[" <<element << "]" << endl;
++index;
}
}
template<class Type>
std::string getTypeName() {
return typeid(Type).name();
}
template<class Type>
std::string getTypeName(Type && type) {
return typeid(Type).name();
}
#define printTypeName(f) do{cout << #f << "\t: " << getTypeName<f>() << endl;}while(0);
#define printTypeName2(f) do{cout << #f << "\t: " << getTypeName<>(f) << endl;}while(0);
//递归终止函数
template<typename T>
void format_expand(std::stringstream & ss,std::string& fmt, std::string::size_type index, T end)//递归到最后一次,调用单参数函数
{
ss.clear();
std::string str;
ss << end;
ss >> str;
for (auto i = index; i < fmt.size(); ++i) {
if (fmt.at(i) == '?') {
fmt.replace(i, 1, str);
break;
}
}
}
//int gcd(int a,int b){
//
// int temp;
// while(b != 0){
//
// temp = a % b;
// a = b;
// b = temp;
// }
//
// return a;
//}
//递归调用函数
template<typename T, class ...Args>
void format_expand(std::stringstream & ss,std::string& fmt,std::string::size_type index,T head, Args... rest)
{
ss.clear();
std::string str;
ss << head;
ss >> str;
for (auto i = index; i < fmt.size(); ++i) {
if (fmt.at(i) == '?') {
fmt.replace(i, 1, str);
print_expand(ss,fmt, i + 1, rest...);
break;
}
}
}
//调用函数
template<class ...Args>
std::string getFormatStr(std::string fmt, Args... rest)
{
std::stringstream ss;
format_expand(ss,fmt,0, rest...);
return fmt;
}
template<int N>
class aTMP{
public:
//enum:size_t { ret = N + aTMP<N-1>::ret };
static const size_t ret = N + aTMP<N-1>::ret;
};
template<>
class aTMP<0>{
public:
//enum:size_t { ret = 1 };
static const size_t ret = 1;
};
#endif __PCH_UTIL__