-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscy_test.h
307 lines (288 loc) · 6.6 KB
/
scy_test.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/************************************************
* filename : scy_test.h
* author : songcy
* email : [email protected]
* last modified : 2014-10-01 11:09
* description :
************************************************/
#ifndef _SCY_TEST_H_
#define _SCY_TEST_H_
#include<iostream>
#include<cstdlib>
#include<vector>
#include<sys/time.h>
#include<climits>
#include<algorithm>
#include<string>
namespace scy_test
{
/***************************
* judge wether the array is sorted
***************************/
template<typename T>
bool IsSorted(std::vector<T> tv)
{
for(auto i = tv.begin()+1; i != tv.end(); ++i)
{
if(*i < *(i-1))
{
#ifndef NOTEST
std::cout<< *i <<" < " <<*(i-1)<<std::endl;
std::cout<<" unsorted ! "<<std::endl;
#endif
return false;
}
}
return true;
}
/***************************
* print info
***************************/
inline void Print(const char * str)
{
#ifndef NOTEST
std::cout<<str<<std::endl;
#endif
}
template<typename T>
inline void Print(T &t)
{
#ifndef NOTEST
std::cout<< t <<std::endl;
#endif
}
template<typename T>
inline void Print(T t[], size_t len)
{
#ifndef NOTEST
for(size_t i = 0; i < len; ++i)
std::cout<<t[i]<<" ";
std::cout<<std::endl;
#endif
}
template<typename T>
inline void Print(std::vector<T> iv)
{
#ifndef NOTEST
for(auto i = iv.begin(); i != iv.end(); ++i)
std::cout<<*i<<" ";
std::cout<<std::endl;
#endif
}
/***************************
* record function's running time
* example:
* {
* timer t("quicksort");
* funtion code...
* }
***************************/
class timer
{
public:
timer(const char *info_):
starttime(0),
endtime(0),
info(info_)
{
gettimeofday(&tv,NULL);
starttime = tv.tv_sec * 1000 + tv.tv_usec/1000;
std::cout<<"--------------------------"<<std::endl;
std::cout<<"- \" "<<info<<" \""<<std::endl;
}
~timer()
{
gettimeofday(&tv,NULL);
endtime = tv.tv_sec*1000 + tv.tv_usec/1000;
std::cout<<"- running time : "<<endtime-starttime<<" ms "<<std::endl;
std::cout<<"--------------------------"<<std::endl;
}
private:
struct timeval tv; //to create time in 0.001s
time_t starttime;
time_t endtime;
std::string info;
};
/***************************
* create
* unsorted/nearsorted/sorted/rsorted
* array for sort functions
***************************/
template<typename T>
class RandomArray
{
public:
RandomArray(size_t size, int mval = INT_MAX):
unsortedvec(std::vector<T>(size)),
nearlysortedvec(std::vector<T>(size)),
// tlist(list<T>(size)),
size(size),
max_value(mval)
{
// t = new T[size];
srand((unsigned)time(NULL));
createDataRandomly();
}
RandomArray():
unsortedvec(std::vector<T>(defaultsize)),
nearlysortedvec(std::vector<T>(defaultsize)),
// tlist(list<T>(1)),
size(defaultsize),
max_value(INT_MAX)
{
// t = new T[1];
srand((unsigned int)time(NULL));
createDataRandomly();
}
~RandomArray() { }
// not return & to protect datas' order in class
std::vector<T> getUnsortedArray() { return unsortedvec; }
std::vector<T> getNearlysortedArray(){ return nearlysortedvec;}
std::vector<T> getSortedArray(){ return sortedvec;}
std::vector<T> getRsortedArray(){ return sortedvec;}
private:
void createDataRandomly()
{
for(size_t i = 0; i < size; ++i)
{
unsortedvec[i] = rand() % max_value;
/*
std::cout<<tvec[i]<<" -------log info";
std::cout<<"\n";
*/
}
sortedvec = unsortedvec;
std::sort(sortedvec.begin(), sortedvec.end());
rsortedvec = sortedvec;
std::reverse(rsortedvec.begin(), rsortedvec.end());
int extend_value = 0;
int scope = 200; //random in range (a ,a+200)
for(int i = 0; i < size ; ++i)
{
nearlysortedvec[i] = (rand() % max_value ) % scope + extend_value;
if( i % 30 == 0)
extend_value += scope;
}
}
RandomArray(const RandomArray &);
RandomArray& operator=(const RandomArray &);
std::vector<T> unsortedvec;
std::vector<T> nearlysortedvec;
std::vector<T> sortedvec;
std::vector<T> rsortedvec;
// T * t;
// list<T> tlist;
size_t size;
const int max_value;
static const int defaultsize = 10000;
// static unsigned randseed;
};
/**********************************************
* TODO:create binary tree
***********************************************/
template<typename T>
class Node
{
public:
Node *left;
Node *right;
T val;
};
template<typename T>
class BinaryTree
{
public:
void traverseTree(void (*fun)());
size_t size()
{
return node_count;
}
BinaryTree(size_t nodenum, int max):
node_count(nodenum),
maxval(max),
nodevec(0)
{
createRandomBinaryTree();
}
~BinaryTree()
{
for(int i = 0; i < nodevec.size(); ++i)
delete nodevec[i];
}
Node<T>* getRoot()
{
return root;
}
void printBinaryTree()
{
int count = 0;
traverseTree(root, count);
}
private:
void createRandomBinaryTree()
{
int count = 0;
RandomArray<int> randarray(node_count, maxval);
std::vector<int> ivec = randarray.getUnsortedArray();
root = new Node<T>;
root->left = root->right = NULL;
root->val = ivec[0];
createRandomBinaryTreeChild(root, ivec, 1);
}
void createRandomBinaryTreeChild(Node<T> *node, std::vector<int>& ivec, int index)
{
std::cout<<"index: "<<index<<std::endl;
if(index >= ivec.size()-1)
return;
int ranleft = rand()%2;
int ranright = rand()%2;
if(ranleft == 0)
{
Node<T> *newnode = new Node<T>;
nodevec.push_back(newnode);
newnode->val = ivec[index];
newnode->left = NULL;
newnode->right = NULL;
node->left = newnode;
createRandomBinaryTreeChild(node->left, ivec, index+1);
index++;
}
if(ranright == 0)
{
Node<T> *newnode = new Node<T>;
nodevec.push_back(newnode);
newnode->val = ivec[index];
newnode->left = NULL;
newnode->right = NULL;
node->right = newnode;
createRandomBinaryTreeChild(node->right, ivec, index+1);
}
}
void traverseTree(Node<T>* node,int count)
{
if(node == NULL)
return;
if(node->left)
std::cout<<node->left->val<<"<-";
else
std::cout<<"NULL<-";
std::cout<<node->val;
if(node->right)
std::cout<<"->"<<node->right->val;
else
std::cout<<"->NULL";
std::cout<<"\t";
printf("deep = %d\n",count);
count++;
traverseTree(node->left,count);
traverseTree(node->right,count);
}
// void createSortedBinaryTree(); //TODO
// void createFullBinaryTree(); //TODO
std::vector< Node<T>* > nodevec;
size_t node_count;
int maxval;
Node<T> *root;
};
}
#endif