README.md 13.4 KB
Newer Older
邹晓航 已提交
1 2
TinySTL
=======
邹晓航 已提交
3
采用C++11实现一款简易的STL标准库,既是C++STL的一个子集(裁剪了一些容器和算法)又是一个超集(增加了一些容器和算法)
邹晓航 已提交
4

邹晓航 已提交
5 6 7
目的:练习数据结构与算法和C++ Template编程

编译环境:VS2013及以上版本
邹晓航 已提交
8

邹晓航 已提交
9
##开发计划:
邹晓航 已提交
10
  * STL的几大基本组件,如string、vector、list、deque、set、map、unordered_\*
邹晓航 已提交
11 12 13 14 15 16
  * STL算法库中的大部分算法
  * circular buffer
  * bitmap
  * skip list
  * binary search tree
  * AVL tree
邹晓航 已提交
17
  * rbtree
邹晓航 已提交
18
  * segment tree
邹晓航 已提交
19 20 21 22 23 24 25 26 27 28
  * splay tree
  * rope
  * Van Emde Boas tree
  * treap
  * B-tree
  * trie
  * suffix array/tree
  * Disjoint-set data structure
  * k-d tree
  * R-tree
邹晓航 已提交
29
  * Matrix
邹晓航 已提交
30
  * Graph
邹晓航 已提交
31
  * bloom filter
邹晓航 已提交
32

邹晓航 已提交
33
##完成进度:
邹晓航 已提交
34 35 36
* STL的几大基本组件
    * type traits:100%  
    * 空间配置器:100%
邹晓航 已提交
37
    * iterator traits:100%
邹晓航 已提交
38
    * reverse_iterator:100%
邹晓航 已提交
39
    * vector:100%
邹晓航 已提交
40 41 42 43 44
    * string:100%
    * priority_queue:100%
    * stack:100%
    * deque:100%
    * queue:100%
邹晓航 已提交
45
    * pair:100%
邹晓航 已提交
46
    * list:100%
邹晓航 已提交
47
	* unordered_set:100%
邹晓航 已提交
48 49 50
* STL Algorithms:  
    * fill:100% 
    * fill_n:100% 
邹晓航 已提交
51
    * find:100%
邹晓航 已提交
52
    * is_heap:100%
邹晓航 已提交
53
    * min、max:100%
邹晓航 已提交
54 55 56 57
    * make_heap:100%
    * pop_heap:100%
    * push_heap:100%
    * sort_heap:100%
邹晓航 已提交
58
    * swap:100%
邹晓航 已提交
59 60 61
    * all_of:100%
    * any_of:100%
    * none_of:100%
邹晓航 已提交
62 63
    * find_if:100%
    * find_if_not:100%
邹晓航 已提交
64
    * adjacent_find:100%
邹晓航 已提交
65 66
    * count:100%
    * count_if:100%
邹晓航 已提交
67
    * mismatch:100%
邹晓航 已提交
68
    * equal:100%
邹晓航 已提交
69
	* is_permutation:100%
邹晓航 已提交
70
	* search:100%
邹晓航 已提交
71
	* advance:100%
邹晓航 已提交
72
	* sort:100%
邹晓航 已提交
73 74
	* generate:100%
	* distance:100%
邹晓航 已提交
75 76 77 78
* 其他组件:
    * circular_buffer:100%   
    * bitmap:100%
    * binary_search_tree:100%
邹晓航 已提交
79
    * avl_tree:100%
邹晓航 已提交
80
	* suffix_array:100%
邹晓航 已提交
81
	*directed_graph:100%
邹晓航 已提交
82

邹晓航 已提交
83
##TinySTL单元测试(原单元测试代码逐步):
邹晓航 已提交
84
  * pair:100%
邹晓航 已提交
85
  * algorithm:20%
邹晓航 已提交
86
  * vector:100%
邹晓航 已提交
87
  * string:100%
邹晓航 已提交
88
  * priority_queue:100%
邹晓航 已提交
89
  * suffix_array:100%
邹晓航 已提交
90
  * queue:100%
邹晓航 已提交
91
  * stack:100%
邹晓航 已提交
92
  * bitmap:100%
邹晓航 已提交
93
  * circular_buffer:100%
邹晓航 已提交
94
  * deque:100%
邹晓航 已提交
95
  * list:100%
邹晓航 已提交
96
  * binary_search_tree:100%
邹晓航 已提交
97
  * avl_tree:100%
邹晓航 已提交
98
  * unordered_set:100%
邹晓航 已提交
99
  *directed_graph:100%
邹晓航 已提交
100 101

#TinySTL性能测试:
邹晓航 已提交
102 103
###测试环境:Windows 7 && VS2013 && release模式
###测试结果:
邹晓航 已提交
104
####(1):vector<int>
邹晓航 已提交
105

邹晓航 已提交
106 107
    //std::vector<int> vec;
    TinySTL::vector<int> vec;
邹晓航 已提交
108 109 110 111 112 113 114
	ProfilerInstance::start();
	int i = 0;
	for (; i != 10000; ++i){
		vec.push_back(i);
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
邹晓航 已提交
115
    
邹晓航 已提交
116 117 118 119 120 121 122 123
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::vector&lt;int>|10万|2|  
|TinySTL::vector&lt;int>|100万|11|  
|TinySTL::vector&lt;int>|1000万|129|  
|std::vector&lt;int>|10万|6|  
|std::vector&lt;int>|100万|16|  
|std::vector&lt;int>|1000万|210|    
邹晓航 已提交
124
####(2):vector&lt;string>
邹晓航 已提交
125

邹晓航 已提交
126 127 128 129
    //std::vector<std::string> vec;
    TinySTL::vector<std::string> vec;
	ProfilerInstance::start();
	int i = 0;
邹晓航 已提交
130
	for (; i != 100000; ++i){
邹晓航 已提交
131 132 133 134 135
		vec.push_back(std::string("zouxiaohang"));
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
136 137 138 139 140 141 142 143
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::vector&lt;string>|10万|18|  
|TinySTL::vector&lt;string>|100万|181|  
|TinySTL::vector&lt;string>|1000万|2372|  
|std::vector&lt;string>|10万|29|  
|std::vector&lt;string>|100万|232|  
|std::vector&lt;string>|1000万|1972|
邹晓航 已提交
144
####(3):circular_buffer&lt;int, N>
邹晓航 已提交
145

邹晓航 已提交
146 147 148 149 150 151 152 153 154
    TinySTL::circular_buffer<int, 10000> cb(10000, 0);
    //boost::circular_buffer<int> cb(10000, 0);
	ProfilerInstance::start();
	for (int i = 0; i != 10000000; ++i){
		cb.push_back(i);
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
155 156 157 158 159 160 161 162
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::circular_buffer|1000万|75|  
|TinySTL::circular_buffer|10000万|604|  
|TinySTL::circular_buffer|100000万|5936|  
|boost::circular_buffer|1000万|22|  
|boost::circular_buffer|10000万|252|  
|boost::circular_buffer|100000万|2241|  
邹晓航 已提交
163
####(4):题目:利用bitmap找出str中未出现的字母  
邹晓航 已提交
164

邹晓航 已提交
165 166 167 168 169 170 171 172 173 174 175 176
    std::string str("abcdefghijklmnpqrstuvwxyz");
    TinySTL::bitmap<26> bm;
	for (auto it = str.cbegin(); it != str.cend(); ++it){
		bm.set(*it - 'a');
	}
	cout << bm << endl;
	cout << bm.size() << endl;
	for (int i = 0; i != 26; ++i){
		if (!bm.test(i))
			cout << "字母" << (char)('a' + i) << "没出现!!!" << endl;
	}
输出结果:  
邹晓航 已提交
177

邹晓航 已提交
178 179 180
    111111111111110111111111111000000
    32  
    字母o没出现!!!
邹晓航 已提交
181
    
邹晓航 已提交
182
####(5):string
邹晓航 已提交
183 184 185 186 187 188 189 190 191 192 193

    //std::string str;
    TinySTL::string str;
	ProfilerInstance::start();
	int i = 0;
	for (; i != 1000000; ++i){
		str.push_back('x');
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
194 195 196 197 198 199 200 201
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::string|100万|7|  
|TinySTL::string|1000万|39|  
|TinySTL::string|10000万|484|  
|std::string|100万|37|  
|std::string|1000万|229|  
|std::string|10000万|1965|  
邹晓航 已提交
202

邹晓航 已提交
203 204 205 206 207 208 209 210 211 212 213 214
####(6):priority_queue&lt;int>

    //std::priority_queue<int> pq;
    TinySTL::priority_queue<int> pq;
	ProfilerInstance::start();
	int i = 0;
	for (; i != 100000; ++i){
		pq.push(i);
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
215 216 217 218 219 220 221 222
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::priority_queue&lt;int>|10万|13|  
|TinySTL::priority_queue&lt;int>|100万|97|  
|TinySTL::priority_queue&lt;int>|1000万|1032|  
|std::priority_queue&lt;int>|10万|12|  
|std::priority_queue&lt;int>|100万|67|  
|std::priority_queue&lt;int>|1000万|752|  
邹晓航 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

    TinySTL::vector<int> v;
    int i = 0;
	for (; i != 100000; ++i){
		v.push_back(i);
	}
	//std::priority_queue<int> pq(v.begin(), v.end());
	TinySTL::priority_queue<int> pq(v.begin(), v.end());
	ProfilerInstance::start();
	for (i = 0; i != 100000; ++i){
		pq.pop();
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
238 239 240 241 242 243 244 245
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::priority_queue&lt;int>|10万|19|  
|TinySTL::priority_queue&lt;int>|100万|137|  
|TinySTL::priority_queue&lt;int>|1000万|1532|  
|std::priority_queue&lt;int>|10万|7|  
|std::priority_queue&lt;int>|100万|92|  
|std::priority_queue&lt;int>|1000万|1214|  
邹晓航 已提交
246

邹晓航 已提交
247
####(7):binary_search_tree&lt;string>
邹晓航 已提交
248

邹晓航 已提交
249
    ifstream f;
邹晓航 已提交
250
	//char buff[256] = { 0 };
邹晓航 已提交
251 252 253 254 255
	std::string word;
	f.open("C:\\Users\\zxh\\Desktop\\text.txt");
	TinySTL::vector<TinySTL::string> v;
	while (f.good()){
		f >> word;
邹晓航 已提交
256 257 258
		//std::copy(word.begin(), word.end(), buff);
		//v.push_back(TinySTL::string(buff, buff + word.size()));
		v.push_back(word);
邹晓航 已提交
259 260
	}
	TinySTL::binary_search_tree<TinySTL::string> sbst;
邹晓航 已提交
261
	ProfilerInstance::start();
邹晓航 已提交
262 263
	for (const auto& word : v){
		sbst.insert(word);
邹晓航 已提交
264 265 266
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
邹晓航 已提交
267
	f.close();
邹晓航 已提交
268
    
邹晓航 已提交
269 270
|container|quantity|time(ms)|  
|---------|--------|--------|  
邹晓航 已提交
271 272 273
|TinySTL::binary_search_tree&lt;string>|44067|16|  
|TinySTL::binary_search_tree&lt;string>|169664|64|  
|TinySTL::binary_search_tree&lt;string>|438230|277|   
邹晓航 已提交
274

邹晓航 已提交
275
####(8):deque&lt;int>
邹晓航 已提交
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

    //std::deque<int> dq;
    TinySTL::deque<int> dq;
	ProfilerInstance::start();
	const int max = 10000000;
	int i = 0;
	for (; i != max / 2; ++i){
		dq.push_front(i);
	}
	for (; i != max; ++i){
		dq.push_back(i);
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::deque&lt;int>|10万|15|  
|TinySTL::deque&lt;int>|100万|78|  
|TinySTL::deque&lt;int>|1000万|1186|  
|std::deque&lt;int>|10万|90|  
|std::deque&lt;int>|100万|1087|  
|std::deque&lt;int>|1000万|4835|  
#####ps:这个性能差距的原因1是内部实现的机制不同,我的deque是预先分配内存因此相同条件下占用的内存更多,而stl的deque是需要的时候再分配,更加节省内存;2是stl的deque实现了更多更灵活的插入删除操作,我只是实现了在头尾的插入和删除

邹晓航 已提交
301 302 303 304 305 306 307 308 309 310
####(9):avl_tree&lt;int> 
    TinySTL::binary_search_tree<int> bst;
    TinySTL::avl_tree<int> avlt;
	for (int i = 0; i != 10000; ++i){
		avlt.insert(i);
		bst.insert(i);
	}
	cout << "binary_search_tree height = " << bst.height() << endl;
	cout << "avl_tree height = " << avlt.height() << endl;
输出结果:  
邹晓航 已提交
311

邹晓航 已提交
312 313
    binary_search_tree height = 10000
    avl_tree height = 14
邹晓航 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334


####(10):list&lt;int>

    TinySTL::list<int> list;
    //std::list<int> list;
	const size_t max = 100000;
	ProfilerInstance::start();
	for (size_t i = 0; i != max; ++i)
		list.push_back(i);
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::list&lt;int>|10万|4|  
|TinySTL::list&lt;int>|100万|33|  
|TinySTL::list&lt;int>|1000万|286|  
|std::list&lt;int>|10万|189|  
|std::list&lt;int>|100万|1774|  
|std::list&lt;int>|1000万|17571|  
邹晓航 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371


####(11):list&lt;int>::sort()

    TinySTL::list<int> list1;
    std::list<int> list2;
	std::default_random_engine dre;
	std::uniform_int_distribution<int> id;
	const size_t max = 10000;
	for (int i = 0; i != max; ++i){
		auto n = id(dre);
		list1.push_back(n);
		list2.push_back(n);
	}
	double cost1 = 0.0, cost2 = 0.0;
	for (int i = 0; i != 100; ++i){
		ProfilerInstance::start();
		list1.sort();//TinySTL::list<int>
		ProfilerInstance::finish();
		cost1 += ProfilerInstance::millisecond();

		ProfilerInstance::start();
		list2.sort();//std::list<int>
		ProfilerInstance::finish();
		cost2 += ProfilerInstance::millisecond();
	}
	cout << "TinySTL time: " << cost1 / 100 << "ms" << endl;
	cout << "std time: " << cost2 / 100 << "ms" << endl;
    
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::list&lt;int>|1万|0.88|  
|TinySTL::list&lt;int>|10万|17.621|  
|TinySTL::list&lt;int>|100万|591.354|  
|std::list&lt;int>|1万|1.25|  
|std::list&lt;int>|10万|35.692|  
|std::list&lt;int>|100万|665.128|  
邹晓航 已提交
372 373 374 375 376 377


####(12):suffix_array

    char arr[] = { 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'b' };
    TinySTL::suffix_array sa(arr, 8);
邹晓航 已提交
378 379 380 381 382 383 384
	auto suffixArray = sa.suffixArray();
	auto rankArray = sa.rankArray();
	auto heightArray = sa.heightArray();

	TinySTL::Test::print_container(suffixArray, "suffixArray");
	TinySTL::Test::print_container(rankArray, "rankArray");
	TinySTL::Test::print_container(heightArray, "heightArray");
邹晓航 已提交
385
    
邹晓航 已提交
386
![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/suffix_array.png)
邹晓航 已提交
387 388 389 390 391 392 393 394




####(13):unordered_set&lt;int>

    TinySTL::Unordered_set<int> ust(10);
	//std::unordered_set<int> ust(10);
邹晓航 已提交
395 396 397
	const size_t insert_count = 1000000;
	const uint64_t query_count = 100000000;
	//calculate total insert time
邹晓航 已提交
398
	ProfilerInstance::start();
邹晓航 已提交
399
	for (size_t i = 0; i != insert_count; ++i){
邹晓航 已提交
400 401 402 403
		ust.insert(i);//per insert time
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
邹晓航 已提交
404 405

	//calculate total query time
邹晓航 已提交
406
	ProfilerInstance::start();
邹晓航 已提交
407
	for (uint64_t i = 0; i != query_count; ++i){
邹晓航 已提交
408
		ust.count(i);//per query time
邹晓航 已提交
409 410 411 412
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
413 414
|container|quantity|insert time(ms)|query time(ms)|    
|---------|--------|--------|--------|    
邹晓航 已提交
415 416 417 418 419
|TinySTL::unordered_set&lt;int>|1万/1亿|8|97|      
|TinySTL::unordered_set&lt;int>|10万/10亿|139|1000|    
|TinySTL::unordered_set&lt;int>|100万/100亿|1214|9546|    
|std::unordered_set&lt;int>|1万/1亿|64|101|    
|std::unordered_set&lt;int>|10万/10亿|884|953|    
邹晓航 已提交
420 421 422 423 424
|std::unordered_set&lt;int>|100万/100亿|2781|9682|   




邹晓航 已提交
425
####(14):sort
邹晓航 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

    std::random_device rd;
	const int len = 10000000;
	int arr[len];
	std::generate(std::begin(arr), std::end(arr), [&rd](){return rd(); });
	ProfilerInstance::start();
	TinySTL::sort(std::begin(arr), std::end(arr));
	//std::sort(std::begin(arr), std::end(arr));
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
|algorithm|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::sort|10万|11|  
|TinySTL::sort|100万|133|  
|TinySTL::sort|1000万|1547|  
|std::sort|10万|13|  
|std::sort|100万|147|  
邹晓航 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|std::sort|1000万|1730| 




####(15):directed_graph

    template<class Index, class Value>
	using dGraph = TinySTL::directed_graph < Index, Value > ;
	dGraph<int, int> g;
	dGraph<int, int>::nodes_set_type set1, set2, set3;
	set1.push_back(g.make_node(1, 11));
	set1.push_back(g.make_node(2, 22));
	set1.push_back(g.make_node(3, 33));
	g.add_node(g.make_node(0, 0), set1);

	set2.push_back(g.make_node(5, 55));
	set2.push_back(g.make_node(6, 66));
	set2.push_back(g.make_node(7, 77));
	g.add_node(g.make_node(1, 11), set2);

	set3.push_back(g.make_node(12, 1212));
	set3.push_back(g.make_node(13, 1313));
	set3.push_back(g.make_node(14, 1414));
	g.add_node(7, set3);

	g.make_edge(12, 2);
	g.make_edge(12, 3);
	g.make_edge(12, 0);
	std::cout << "graph after add nodes:" << std::endl;
	std::cout << g.to_string();

	auto func = [](const dGraph<int, int>::node_type& node){
		std::cout << "[" << node.first << "," << node.second << "]" << std::endl;
	};
	std::cout << "graph DFS from node(1, 11):" << std::endl;
	g.DFS(1, func);
	std::cout << "graph BFS from node(1, 11):" << std::endl;
	g.BFS(1, func);

	std::cout << "graph after delete node(7, 77):" << std::endl;
	g.delete_node(dGraph<int, int>::node_type(7, 77));
	std::cout << g.to_string();
    
![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/graph1.png)  
![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/graph_dfs.png)  
![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/graph_bfs.png)  
![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/graph2.png)