├── .gitattributes ├── .gitignore ├── README.md ├── TinySTL.sln └── TinySTL ├── AVLTree.h ├── Algorithm.h ├── Alloc.h ├── Allocator.h ├── BinarySearchTree.h ├── Bitmap.h ├── COWPtr.h ├── CircularBuffer.h ├── Construct.h ├── Deque.h ├── Detail ├── AVLTree.impl.h ├── Alloc.cpp ├── BinarySearchTree.impl.h ├── Bitmap.impl.h ├── COWPtr.impl.h ├── CircularBuffer.impl.h ├── Deque.impl.h ├── Graph.impl.h ├── List.impl.h ├── Ref.h ├── String.cpp ├── TrieTree.cpp ├── Unordered_set.impl.h └── Vector.impl.h ├── Functional.h ├── Graph.h ├── Iterator.h ├── List.h ├── Memory.h ├── Profiler ├── Profiler.cpp └── Profiler.h ├── Queue.h ├── ReverseIterator.h ├── ScreenShots ├── graph1.png ├── graph2.png ├── graph_bfs.png ├── graph_dfs.png ├── suffix_array.png └── trie_tree.png ├── Stack.h ├── String.h ├── SuffixArray.h ├── Test ├── AVLTreeTest.cpp ├── AVLTreeTest.h ├── AlgorithmTest.cpp ├── AlgorithmTest.h ├── BinarySearchTreeTest.cpp ├── BinarySearchTreeTest.h ├── BitmapTest.cpp ├── BitmapTest.h ├── COWPtrTest.cpp ├── COWPtrTest.h ├── CircularBufferTest.cpp ├── CircularBufferTest.h ├── DequeTest.cpp ├── DequeTest.h ├── GraphTest.cpp ├── GraphTest.h ├── ListTest.cpp ├── ListTest.h ├── PairTest.cpp ├── PairTest.h ├── PriorityQueueTest.cpp ├── PriorityQueueTest.h ├── QueueTest.cpp ├── QueueTest.h ├── RefTest.cpp ├── RefTest.h ├── SharedPtrTest.cpp ├── SharedPtrTest.h ├── StackTest.cpp ├── StackTest.h ├── StringTest.cpp ├── StringTest.h ├── SuffixArrayTest.cpp ├── SuffixArrayTest.h ├── TestUtil.h ├── TrieTreeTest.cpp ├── TrieTreeTest.h ├── TypeTraitsTest.cpp ├── TypeTraitsTest.h ├── UFSetTest.cpp ├── UFSetTest.h ├── UniquePtrTest.cpp ├── UniquePtrTest.h ├── Unordered_setTest.cpp ├── Unordered_setTest.h ├── VectorTest.cpp └── VectorTest.h ├── TestData ├── string_test.txt └── trie_tree_test.txt ├── TinySTL.vcxproj ├── TinySTL.vcxproj.filters ├── TrieTree.h ├── TypeTraits.h ├── UFSet.h ├── UninitializedFunctions.h ├── Unordered_set.h ├── Utility.h ├── Vector.h └── main.cpp /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | 11 | [Dd]ebug/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | [Bb]in/ 16 | [Oo]bj/ 17 | 18 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets 19 | !packages/*/build/ 20 | 21 | # MSTest test Results 22 | [Tt]est[Rr]esult*/ 23 | [Bb]uild[Ll]og.* 24 | 25 | *_i.c 26 | *_p.c 27 | *.ilk 28 | *.meta 29 | *.obj 30 | *.pch 31 | *.pdb 32 | *.pgc 33 | *.pgd 34 | *.rsp 35 | *.sbr 36 | *.tlb 37 | *.tli 38 | *.tlh 39 | *.tmp 40 | *.tmp_proj 41 | *.log 42 | *.vspscc 43 | *.vssscc 44 | .builds 45 | *.pidb 46 | *.log 47 | *.scc 48 | 49 | # Visual C++ cache files 50 | ipch/ 51 | *.aps 52 | *.ncb 53 | *.opensdf 54 | *.sdf 55 | *.cachefile 56 | 57 | # Visual Studio profiler 58 | *.psess 59 | *.vsp 60 | *.vspx 61 | 62 | # Guidance Automation Toolkit 63 | *.gpState 64 | 65 | # ReSharper is a .NET coding add-in 66 | _ReSharper*/ 67 | *.[Rr]e[Ss]harper 68 | 69 | # TeamCity is a build add-in 70 | _TeamCity* 71 | 72 | # DotCover is a Code Coverage Tool 73 | *.dotCover 74 | 75 | # NCrunch 76 | *.ncrunch* 77 | .*crunch*.local.xml 78 | 79 | # Installshield output folder 80 | [Ee]xpress/ 81 | 82 | # DocProject is a documentation generator add-in 83 | DocProject/buildhelp/ 84 | DocProject/Help/*.HxT 85 | DocProject/Help/*.HxC 86 | DocProject/Help/*.hhc 87 | DocProject/Help/*.hhk 88 | DocProject/Help/*.hhp 89 | DocProject/Help/Html2 90 | DocProject/Help/html 91 | 92 | # Click-Once directory 93 | publish/ 94 | 95 | # Publish Web Output 96 | *.Publish.xml 97 | 98 | # NuGet Packages Directory 99 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 100 | #packages/ 101 | 102 | # Windows Azure Build Output 103 | csx 104 | *.build.csdef 105 | 106 | # Windows Store app package directory 107 | AppPackages/ 108 | 109 | # Others 110 | sql/ 111 | *.Cache 112 | ClientBin/ 113 | [Ss]tyle[Cc]op.* 114 | ~$* 115 | *~ 116 | *.dbmdl 117 | *.[Pp]ublish.xml 118 | *.pfx 119 | *.publishsettings 120 | 121 | # RIA/Silverlight projects 122 | Generated_Code/ 123 | 124 | # Backup & report files from converting an old project file to a newer 125 | # Visual Studio version. Backup files are not needed, because we have git ;-) 126 | _UpgradeReport_Files/ 127 | Backup*/ 128 | UpgradeLog*.XML 129 | UpgradeLog*.htm 130 | 131 | # SQL Server files 132 | App_Data/*.mdf 133 | App_Data/*.ldf 134 | 135 | 136 | #LightSwitch generated files 137 | GeneratedArtifacts/ 138 | _Pvt_Extensions/ 139 | ModelManifest.xml 140 | 141 | # ========================= 142 | # Windows detritus 143 | # ========================= 144 | 145 | # Windows image file caches 146 | Thumbs.db 147 | ehthumbs.db 148 | 149 | # Folder config file 150 | Desktop.ini 151 | 152 | # Recycle Bin used on file shares 153 | $RECYCLE.BIN/ 154 | 155 | # Mac desktop service store files 156 | .DS_Store 157 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TinySTL 2 | ======= 3 | 采用C++11实现一款简易的STL标准库,既是C++STL的一个子集(裁剪了一些容器和算法)又是一个超集(增加了一些容器和算法) 4 | 5 | 目的:练习数据结构与算法和C++ Template编程 6 | 7 | 编译环境:VS2013及以上版本 8 | 9 | ##开发计划: 10 | * STL的几大基本组件,如string、vector、list、deque、set、map、unordered_\*等 11 | * STL算法库中的大部分算法 12 | * circular buffer 13 | * bitmap 14 | * skip list 15 | * binary search tree 16 | * AVL tree 17 | * rbtree 18 | * segment tree 19 | * splay tree 20 | * rope 21 | * Van Emde Boas tree 22 | * treap 23 | * B-tree 24 | * trie 25 | * suffix array/tree 26 | * Disjoint-set data structure 27 | * k-d tree 28 | * R-tree 29 | * Matrix 30 | * Graph 31 | * bloom filter 32 | 33 | ##完成进度: 34 | * STL的几大基本组件 35 | * type traits:100% 36 | * 空间配置器:100% 37 | * iterator traits:100% 38 | * reverse_iterator:100% 39 | * vector:100% 40 | * string:100% 41 | * priority_queue:100% 42 | * stack:100% 43 | * deque:100% 44 | * queue:100% 45 | * pair:100% 46 | * list:100% 47 | * unordered_set:100% 48 | * unique_ptr:100% 49 | * shared_ptr:100% 50 | * cow_ptr:100% 51 | * STL Algorithms: 52 | * fill:100% 53 | * fill_n:100% 54 | * find:100% 55 | * is_heap:100% 56 | * min、max:100% 57 | * make_heap:100% 58 | * pop_heap:100% 59 | * push_heap:100% 60 | * sort_heap:100% 61 | * swap:100% 62 | * all_of:100% 63 | * any_of:100% 64 | * none_of:100% 65 | * find_if:100% 66 | * find_if_not:100% 67 | * adjacent_find:100% 68 | * count:100% 69 | * count_if:100% 70 | * mismatch:100% 71 | * equal:100% 72 | * is_permutation:100% 73 | * search:100% 74 | * advance:100% 75 | * sort:100% 76 | * generate:100% 77 | * generate_n:100% 78 | * distance:100% 79 | * copy:100% 80 | * 其他组件: 81 | * circular_buffer:100% 82 | * bitmap:100% 83 | * binary_search_tree:100% 84 | * avl_tree:100% 85 | * suffix_array:100% 86 | * directed_graph:100% 87 | * trie tree:100% 88 | * Disjoint-set data structure:100% 89 | 90 | ##TinySTL单元测试(原单元测试代码逐步): 91 | * pair:100% 92 | * algorithm:20% 93 | * vector:100% 94 | * string:100% 95 | * priority_queue:100% 96 | * suffix_array:100% 97 | * queue:100% 98 | * stack:100% 99 | * bitmap:100% 100 | * circular_buffer:100% 101 | * deque:100% 102 | * list:100% 103 | * binary_search_tree:100% 104 | * avl_tree:100% 105 | * unordered_set:100% 106 | * directed_graph:100% 107 | * trie tree:100% 108 | * unique_ptr:100% 109 | * shared_ptr:100% 110 | * Disjoint-set data structure:100% 111 | 112 | #TinySTL性能测试: 113 | ###测试环境:Windows 7 && VS2013 && release模式 114 | ###测试结果: 115 | ####(1):vector<int> 116 | 117 | //std::vector vec; 118 | TinySTL::vector vec; 119 | ProfilerInstance::start(); 120 | int i = 0; 121 | for (; i != 10000; ++i){ 122 | vec.push_back(i); 123 | } 124 | ProfilerInstance::finish(); 125 | ProfilerInstance::dumpDuringTime(); 126 | 127 | |container|quantity|time(ms)| 128 | |---------|--------|--------| 129 | |TinySTL::vector<int>|10万|2| 130 | |TinySTL::vector<int>|100万|11| 131 | |TinySTL::vector<int>|1000万|129| 132 | |std::vector<int>|10万|6| 133 | |std::vector<int>|100万|16| 134 | |std::vector<int>|1000万|210| 135 | ####(2):vector<string> 136 | 137 | //std::vector vec; 138 | TinySTL::vector vec; 139 | ProfilerInstance::start(); 140 | int i = 0; 141 | for (; i != 100000; ++i){ 142 | vec.push_back(std::string("zouxiaohang")); 143 | } 144 | ProfilerInstance::finish(); 145 | ProfilerInstance::dumpDuringTime(); 146 | 147 | |container|quantity|time(ms)| 148 | |---------|--------|--------| 149 | |TinySTL::vector<string>|10万|18| 150 | |TinySTL::vector<string>|100万|181| 151 | |TinySTL::vector<string>|1000万|2372| 152 | |std::vector<string>|10万|29| 153 | |std::vector<string>|100万|232| 154 | |std::vector<string>|1000万|1972| 155 | ####(3):circular_buffer<int, N> 156 | 157 | TinySTL::circular_buffer cb(10000, 0); 158 | //boost::circular_buffer cb(10000, 0); 159 | ProfilerInstance::start(); 160 | for (int i = 0; i != 10000000; ++i){ 161 | cb.push_back(i); 162 | } 163 | ProfilerInstance::finish(); 164 | ProfilerInstance::dumpDuringTime(); 165 | 166 | |container|quantity|time(ms)| 167 | |---------|--------|--------| 168 | |TinySTL::circular_buffer|1000万|75| 169 | |TinySTL::circular_buffer|10000万|604| 170 | |TinySTL::circular_buffer|100000万|5936| 171 | |boost::circular_buffer|1000万|22| 172 | |boost::circular_buffer|10000万|252| 173 | |boost::circular_buffer|100000万|2241| 174 | ####(4):题目:利用bitmap找出str中未出现的字母 175 | 176 | std::string str("abcdefghijklmnpqrstuvwxyz"); 177 | TinySTL::bitmap<26> bm; 178 | for (auto it = str.cbegin(); it != str.cend(); ++it){ 179 | bm.set(*it - 'a'); 180 | } 181 | cout << bm << endl; 182 | cout << bm.size() << endl; 183 | for (int i = 0; i != 26; ++i){ 184 | if (!bm.test(i)) 185 | cout << "字母" << (char)('a' + i) << "没出现!!!" << endl; 186 | } 187 | 输出结果: 188 | 189 | 111111111111110111111111111000000 190 | 32 191 | 字母o没出现!!! 192 | 193 | ####(5):string 194 | 195 | //std::string str; 196 | TinySTL::string str; 197 | ProfilerInstance::start(); 198 | int i = 0; 199 | for (; i != 1000000; ++i){ 200 | str.push_back('x'); 201 | } 202 | ProfilerInstance::finish(); 203 | ProfilerInstance::dumpDuringTime(); 204 | 205 | |container|quantity|time(ms)| 206 | |---------|--------|--------| 207 | |TinySTL::string|100万|7| 208 | |TinySTL::string|1000万|39| 209 | |TinySTL::string|10000万|484| 210 | |std::string|100万|37| 211 | |std::string|1000万|229| 212 | |std::string|10000万|1965| 213 | 214 | ####(6):priority_queue<int> 215 | 216 | //std::priority_queue pq; 217 | TinySTL::priority_queue pq; 218 | ProfilerInstance::start(); 219 | int i = 0; 220 | for (; i != 100000; ++i){ 221 | pq.push(i); 222 | } 223 | ProfilerInstance::finish(); 224 | ProfilerInstance::dumpDuringTime(); 225 | 226 | |container|quantity|time(ms)| 227 | |---------|--------|--------| 228 | |TinySTL::priority_queue<int>|10万|13| 229 | |TinySTL::priority_queue<int>|100万|97| 230 | |TinySTL::priority_queue<int>|1000万|1032| 231 | |std::priority_queue<int>|10万|12| 232 | |std::priority_queue<int>|100万|67| 233 | |std::priority_queue<int>|1000万|752| 234 | 235 | TinySTL::vector v; 236 | int i = 0; 237 | for (; i != 100000; ++i){ 238 | v.push_back(i); 239 | } 240 | //std::priority_queue pq(v.begin(), v.end()); 241 | TinySTL::priority_queue pq(v.begin(), v.end()); 242 | ProfilerInstance::start(); 243 | for (i = 0; i != 100000; ++i){ 244 | pq.pop(); 245 | } 246 | ProfilerInstance::finish(); 247 | ProfilerInstance::dumpDuringTime(); 248 | 249 | |container|quantity|time(ms)| 250 | |---------|--------|--------| 251 | |TinySTL::priority_queue<int>|10万|19| 252 | |TinySTL::priority_queue<int>|100万|137| 253 | |TinySTL::priority_queue<int>|1000万|1532| 254 | |std::priority_queue<int>|10万|7| 255 | |std::priority_queue<int>|100万|92| 256 | |std::priority_queue<int>|1000万|1214| 257 | 258 | ####(7):binary_search_tree<string> 259 | 260 | ifstream f; 261 | //char buff[256] = { 0 }; 262 | std::string word; 263 | f.open("C:\\Users\\zxh\\Desktop\\text.txt"); 264 | TinySTL::vector v; 265 | while (f.good()){ 266 | f >> word; 267 | //std::copy(word.begin(), word.end(), buff); 268 | //v.push_back(TinySTL::string(buff, buff + word.size())); 269 | v.push_back(word); 270 | } 271 | TinySTL::binary_search_tree sbst; 272 | ProfilerInstance::start(); 273 | for (const auto& word : v){ 274 | sbst.insert(word); 275 | } 276 | ProfilerInstance::finish(); 277 | ProfilerInstance::dumpDuringTime(); 278 | f.close(); 279 | 280 | |container|quantity|time(ms)| 281 | |---------|--------|--------| 282 | |TinySTL::binary_search_tree<string>|44067|16| 283 | |TinySTL::binary_search_tree<string>|169664|64| 284 | |TinySTL::binary_search_tree<string>|438230|277| 285 | 286 | ####(8):deque<int> 287 | 288 | //std::deque dq; 289 | TinySTL::deque dq; 290 | ProfilerInstance::start(); 291 | const int max = 10000000; 292 | int i = 0; 293 | for (; i != max / 2; ++i){ 294 | dq.push_front(i); 295 | } 296 | for (; i != max; ++i){ 297 | dq.push_back(i); 298 | } 299 | ProfilerInstance::finish(); 300 | ProfilerInstance::dumpDuringTime(); 301 | 302 | |container|quantity|time(ms)| 303 | |---------|--------|--------| 304 | |TinySTL::deque<int>|10万|15| 305 | |TinySTL::deque<int>|100万|78| 306 | |TinySTL::deque<int>|1000万|1186| 307 | |std::deque<int>|10万|90| 308 | |std::deque<int>|100万|1087| 309 | |std::deque<int>|1000万|4835| 310 | #####ps:这个性能差距的原因1是内部实现的机制不同,我的deque是预先分配内存因此相同条件下占用的内存更多,而stl的deque是需要的时候再分配,更加节省内存;2是stl的deque实现了更多更灵活的插入删除操作,我只是实现了在头尾的插入和删除 311 | 312 | ####(9):avl_tree<int> 313 | TinySTL::binary_search_tree bst; 314 | TinySTL::avl_tree avlt; 315 | for (int i = 0; i != 10000; ++i){ 316 | avlt.insert(i); 317 | bst.insert(i); 318 | } 319 | cout << "binary_search_tree height = " << bst.height() << endl; 320 | cout << "avl_tree height = " << avlt.height() << endl; 321 | 输出结果: 322 | 323 | binary_search_tree height = 10000 324 | avl_tree height = 14 325 | 326 | 327 | ####(10):list<int> 328 | 329 | TinySTL::list list; 330 | //std::list list; 331 | const size_t max = 100000; 332 | ProfilerInstance::start(); 333 | for (size_t i = 0; i != max; ++i) 334 | list.push_back(i); 335 | ProfilerInstance::finish(); 336 | ProfilerInstance::dumpDuringTime(); 337 | 338 | |container|quantity|time(ms)| 339 | |---------|--------|--------| 340 | |TinySTL::list<int>|10万|4| 341 | |TinySTL::list<int>|100万|33| 342 | |TinySTL::list<int>|1000万|286| 343 | |std::list<int>|10万|189| 344 | |std::list<int>|100万|1774| 345 | |std::list<int>|1000万|17571| 346 | 347 | 348 | ####(11):list<int>::sort() 349 | 350 | TinySTL::list list1; 351 | std::list list2; 352 | std::default_random_engine dre; 353 | std::uniform_int_distribution id; 354 | const size_t max = 10000; 355 | for (int i = 0; i != max; ++i){ 356 | auto n = id(dre); 357 | list1.push_back(n); 358 | list2.push_back(n); 359 | } 360 | double cost1 = 0.0, cost2 = 0.0; 361 | for (int i = 0; i != 100; ++i){ 362 | ProfilerInstance::start(); 363 | list1.sort();//TinySTL::list 364 | ProfilerInstance::finish(); 365 | cost1 += ProfilerInstance::millisecond(); 366 | 367 | ProfilerInstance::start(); 368 | list2.sort();//std::list 369 | ProfilerInstance::finish(); 370 | cost2 += ProfilerInstance::millisecond(); 371 | } 372 | cout << "TinySTL time: " << cost1 / 100 << "ms" << endl; 373 | cout << "std time: " << cost2 / 100 << "ms" << endl; 374 | 375 | |container|quantity|time(ms)| 376 | |---------|--------|--------| 377 | |TinySTL::list<int>|1万|0.88| 378 | |TinySTL::list<int>|10万|17.621| 379 | |TinySTL::list<int>|100万|591.354| 380 | |std::list<int>|1万|1.25| 381 | |std::list<int>|10万|35.692| 382 | |std::list<int>|100万|665.128| 383 | 384 | 385 | ####(12):suffix_array 386 | 387 | char arr[] = { 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'b' }; 388 | TinySTL::suffix_array sa(arr, 8); 389 | auto suffixArray = sa.suffixArray(); 390 | auto rankArray = sa.rankArray(); 391 | auto heightArray = sa.heightArray(); 392 | 393 | TinySTL::Test::print_container(suffixArray, "suffixArray"); 394 | TinySTL::Test::print_container(rankArray, "rankArray"); 395 | TinySTL::Test::print_container(heightArray, "heightArray"); 396 | 397 | ![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/suffix_array.png) 398 | 399 | 400 | 401 | 402 | ####(13):unordered_set<int> 403 | 404 | TinySTL::Unordered_set ust(10); 405 | //std::unordered_set ust(10); 406 | const size_t insert_count = 1000000; 407 | const uint64_t query_count = 100000000; 408 | //calculate total insert time 409 | ProfilerInstance::start(); 410 | for (size_t i = 0; i != insert_count; ++i){ 411 | ust.insert(i);//per insert time 412 | } 413 | ProfilerInstance::finish(); 414 | ProfilerInstance::dumpDuringTime(); 415 | 416 | //calculate total query time 417 | ProfilerInstance::start(); 418 | for (uint64_t i = 0; i != query_count; ++i){ 419 | ust.count(i);//per query time 420 | } 421 | ProfilerInstance::finish(); 422 | ProfilerInstance::dumpDuringTime(); 423 | 424 | |container|quantity|insert time(ms)|query time(ms)| 425 | |---------|--------|--------|--------| 426 | |TinySTL::unordered_set<int>|1万/1亿|8|97| 427 | |TinySTL::unordered_set<int>|10万/10亿|139|1000| 428 | |TinySTL::unordered_set<int>|100万/100亿|1214|9546| 429 | |std::unordered_set<int>|1万/1亿|64|101| 430 | |std::unordered_set<int>|10万/10亿|884|953| 431 | |std::unordered_set<int>|100万/100亿|2781|9682| 432 | 433 | 434 | 435 | 436 | ####(14):sort 437 | 438 | std::random_device rd; 439 | const int len = 10000000; 440 | int arr[len]; 441 | std::generate(std::begin(arr), std::end(arr), [&rd](){return rd(); }); 442 | ProfilerInstance::start(); 443 | TinySTL::sort(std::begin(arr), std::end(arr)); 444 | //std::sort(std::begin(arr), std::end(arr)); 445 | ProfilerInstance::finish(); 446 | ProfilerInstance::dumpDuringTime(); 447 | 448 | |algorithm|quantity|time(ms)| 449 | |---------|--------|--------| 450 | |TinySTL::sort|10万|11| 451 | |TinySTL::sort|100万|133| 452 | |TinySTL::sort|1000万|1547| 453 | |std::sort|10万|13| 454 | |std::sort|100万|147| 455 | |std::sort|1000万|1730| 456 | 457 | 458 | 459 | 460 | ####(15):directed_graph 461 | 462 | template 463 | using dGraph = TinySTL::directed_graph < Index, Value > ; 464 | dGraph g; 465 | dGraph::nodes_set_type set1, set2, set3; 466 | set1.push_back(g.make_node(1, 11)); 467 | set1.push_back(g.make_node(2, 22)); 468 | set1.push_back(g.make_node(3, 33)); 469 | g.add_node(g.make_node(0, 0), set1); 470 | 471 | set2.push_back(g.make_node(5, 55)); 472 | set2.push_back(g.make_node(6, 66)); 473 | set2.push_back(g.make_node(7, 77)); 474 | g.add_node(g.make_node(1, 11), set2); 475 | 476 | set3.push_back(g.make_node(12, 1212)); 477 | set3.push_back(g.make_node(13, 1313)); 478 | set3.push_back(g.make_node(14, 1414)); 479 | g.add_node(7, set3); 480 | 481 | g.make_edge(12, 2); 482 | g.make_edge(12, 3); 483 | g.make_edge(12, 0); 484 | std::cout << "graph after add nodes:" << std::endl; 485 | std::cout << g.to_string(); 486 | 487 | auto func = [](const dGraph::node_type& node){ 488 | std::cout << "[" << node.first << "," << node.second << "]" << std::endl; 489 | }; 490 | std::cout << "graph DFS from node(1, 11):" << std::endl; 491 | g.DFS(1, func); 492 | std::cout << "graph BFS from node(1, 11):" << std::endl; 493 | g.BFS(1, func); 494 | 495 | std::cout << "graph after delete node(7, 77):" << std::endl; 496 | g.delete_node(dGraph::node_type(7, 77)); 497 | std::cout << g.to_string(); 498 | 499 | ![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/graph1.png) 500 | ![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/graph_dfs.png) 501 | ![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/graph_bfs.png) 502 | ![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/graph2.png) 503 | 504 | 505 | 506 | 507 | 508 | ####(16):trie tree 509 | 510 | TinySTL::trie_tree t; 511 | std::ifstream in; 512 | in.open("C:\\Users\\zxh\\Desktop\\trie_tree_test.txt"); 513 | std::vector v; 514 | std::string s; 515 | while (in){ 516 | in >> s; 517 | v.push_back(s); 518 | } 519 | ProfilerInstance::start(); 520 | for (const auto& str : v){ 521 | t.insert(TinySTL::string(str.data())); 522 | } 523 | ProfilerInstance::finish(); 524 | std::cout << "build trie tree costs " << ProfilerInstance::millisecond() << "ms" << std::endl; 525 | 526 | ProfilerInstance::start(); 527 | auto res = t.get_word_by_prefix("v"); 528 | ProfilerInstance::finish(); 529 | std::cout << "get word by prefix \"v\" costs " << ProfilerInstance::millisecond() << "ms" << std::endl; 530 | auto i = 0; 531 | for (const auto& str : res){ 532 | ++i; 533 | if (i % 10 == 0) std::cout << std::endl; 534 | std::cout << str << " "; 535 | } 536 | std::cout << std::endl; 537 | 538 | ![image](https://raw.githubusercontent.com/zouxiaohang/TinySTL/master/TinySTL/ScreenShots/trie_tree.png) 539 | 540 | 541 | 542 | ####(17):shared_ptr 543 | 544 | shared_ptr sp1(new string("hello")); 545 | assert(sp1.use_count() == 1); 546 | assert(*sp1 == "hello"); 547 | sp1->append(" world"); 548 | assert(*sp1 == "hello world"); 549 | 550 | { 551 | auto sp2 = sp1; 552 | assert(sp1.use_count() == 2); 553 | } 554 | assert(sp1.use_count() == 1); 555 | 556 | 557 | 558 | ####(18):unique_ptr 559 | 560 | auto up = make_unique(10, '0'); 561 | up->append("111"); 562 | assert(*up == "0000000000111"); 563 | 564 | up.release(); 565 | assert(up == nullptr); 566 | 567 | up.reset(new string("hello")); 568 | assert(*up == "hello"); 569 | 570 | 571 | 572 | ####(19):cow_ptr 573 | 574 | cow_ptr cp1 = make_cow("zouxiaohang"); 575 | auto cp2 = cp1, cp3 = cp1; 576 | assert(cp1 == cp2 && cp2 == cp3); 577 | assert(*cp1 == *cp2 && *cp2 == *cp3 && *cp3 == "zouxiaohang"); 578 | 579 | string s = *cp2;//read 580 | assert(s == "zouxiaohang"); 581 | assert(cp1 == cp2 && cp2 == cp3); 582 | assert(*cp1 == *cp2 && *cp2 == *cp3 && *cp3 == "zouxiaohang"); 583 | 584 | *cp2 = ("C++");//write 585 | assert(*cp1 == *cp3 && *cp3 == "zouxiaohang"); 586 | assert(*cp2 == "C++"); 587 | 588 | 589 | 590 | 591 | ####(19):union-find set 592 | 593 | uf_set<10> uf; 594 | uf.Union(0, 1); 595 | uf.Union(2, 3); 596 | uf.Union(3, 1); 597 | assert(uf.Find(0) == uf.Find(2)); 598 | 599 | 600 | 601 | -------------------------------------------------------------------------------- /TinySTL.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Express 2013 for Windows Desktop 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TinySTL", "TinySTL\TinySTL.vcxproj", "{8B728502-8B31-4983-B9C3-13D8CCA52181}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {8B728502-8B31-4983-B9C3-13D8CCA52181}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {8B728502-8B31-4983-B9C3-13D8CCA52181}.Debug|Win32.Build.0 = Debug|Win32 16 | {8B728502-8B31-4983-B9C3-13D8CCA52181}.Release|Win32.ActiveCfg = Release|Win32 17 | {8B728502-8B31-4983-B9C3-13D8CCA52181}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /TinySTL/AVLTree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/AVLTree.h -------------------------------------------------------------------------------- /TinySTL/Algorithm.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Algorithm.h -------------------------------------------------------------------------------- /TinySTL/Alloc.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Alloc.h -------------------------------------------------------------------------------- /TinySTL/Allocator.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Allocator.h -------------------------------------------------------------------------------- /TinySTL/BinarySearchTree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/BinarySearchTree.h -------------------------------------------------------------------------------- /TinySTL/Bitmap.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Bitmap.h -------------------------------------------------------------------------------- /TinySTL/COWPtr.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/COWPtr.h -------------------------------------------------------------------------------- /TinySTL/CircularBuffer.h: -------------------------------------------------------------------------------- 1 | #ifndef _CIRCULAR_BUFFER_H_ 2 | #define _CIRCULAR_BUFFER_H_ 3 | 4 | #include "Allocator.h" 5 | #include "Iterator.h" 6 | #include "UninitializedFunctions.h" 7 | 8 | #include 9 | 10 | namespace TinySTL{ 11 | template 12 | class circular_buffer; 13 | namespace Detail{ 14 | //the iterator of circular buffer 15 | template> 16 | class cb_iter:iterator{//bidirectional iterator 17 | private: 18 | typedef ::TinySTL::circular_buffer cb; 19 | typedef cb *cbPtr; 20 | 21 | T *ptr_; 22 | int index_; 23 | cbPtr container_; 24 | public: 25 | cb_iter() :ptr_(0), index_(0), container_(0){} 26 | cb_iter(T *ptr, cbPtr container) : 27 | ptr_(ptr), index_(ptr - container->start_), container_(container){} 28 | cb_iter(const cb_iter& cit) : 29 | ptr_(cit.ptr_), index_(cit.index_), container_(cit.container_){} 30 | cb_iter& operator = (const cb_iter& cit); 31 | public: 32 | operator T*(){ return ptr_; } 33 | T& operator *(){ return *ptr_; } 34 | T *operator ->(){ return &(operator*()); } 35 | 36 | cb_iter& operator ++(); 37 | cb_iter operator ++(int); 38 | cb_iter& operator --(); 39 | cb_iter operator --(int); 40 | bool operator == (const cb_iter& it)const; 41 | bool operator != (const cb_iter& it)const; 42 | private: 43 | void setIndex_(int index){ index_ = index; } 44 | void setPtr_(T *ptr){ ptr_ = ptr; } 45 | int nextIndex(int index){ return (++index) % N; } 46 | int prevIndex(int index); 47 | public: 48 | template 49 | friend cb_iter operator +(const cb_iter& cit, std::ptrdiff_t i); 50 | template 51 | friend cb_iter operator -(const cb_iter& cit, std::ptrdiff_t i); 52 | }; 53 | }//end of Detail namespace 54 | 55 | //circular buffer 56 | template> 57 | class circular_buffer{ 58 | template 59 | friend class ::TinySTL::Detail::cb_iter; 60 | public: 61 | typedef T value_type; 62 | typedef Detail::cb_iter iterator; 63 | typedef iterator pointer; 64 | typedef T& reference; 65 | typedef int size_type; 66 | typedef ptrdiff_t difference_type; 67 | private: 68 | T *start_; 69 | T *finish_; 70 | int indexOfHead;//the first position 71 | int indexOfTail;//the last position 72 | size_type size_; 73 | 74 | typedef Alloc dataAllocator; 75 | public: 76 | explicit circular_buffer(const int& n, const value_type& val = value_type()); 77 | template 78 | circular_buffer(InputIterator first, InputIterator last); 79 | circular_buffer(const circular_buffer& cb); 80 | circular_buffer& operator = (const circular_buffer& cb); 81 | circular_buffer& operator = (circular_buffer&& cb); 82 | circular_buffer(circular_buffer&& cb); 83 | ~circular_buffer(); 84 | 85 | bool full(){ return size_ == N; } 86 | bool empty(){ return size_ == 0; } 87 | difference_type capacity(){ return finish_ - start_; } 88 | size_type size(){ return size_; } 89 | void clear(); 90 | 91 | iterator first(){ return iterator(start_ + indexOfHead, this); } 92 | iterator last(){ return iterator(start_ + indexOfTail, this); } 93 | 94 | reference operator [](size_type i){ return *(start_ + i); } 95 | reference front(){ return *(start_ + indexOfHead); } 96 | reference back(){ return *(start_ + indexOfTail); } 97 | void push_back(const T& val); 98 | void pop_front(); 99 | 100 | bool operator == (circular_buffer& cb); 101 | bool operator != (circular_buffer& cb); 102 | 103 | Alloc get_allocator(){ return dataAllocator; } 104 | private: 105 | void allocateAndFillN(const int& n, const value_type& val); 106 | template 107 | void allocateAndCopy(InputIterator first, InputIterator last); 108 | int nextIndex(int index){ return (index + 1) % N; } 109 | void copyAllMembers(const circular_buffer& cb); 110 | void zeroCircular(circular_buffer& cb); 111 | void clone(const circular_buffer& cb); 112 | public: 113 | template 114 | friend std::ostream& operator <<(std::ostream& os, circular_buffer& cb); 115 | };//end of circular buffer 116 | } 117 | 118 | #include "Detail\CircularBuffer.impl.h" 119 | #endif -------------------------------------------------------------------------------- /TinySTL/Construct.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONSTRUCT_H_ 2 | #define _CONSTRUCT_H_ 3 | 4 | #include 5 | 6 | #include "TypeTraits.h" 7 | 8 | namespace TinySTL{ 9 | 10 | template 11 | inline void construct(T1 *ptr1, const T2& value){ 12 | new(ptr1) T1(value); 13 | } 14 | 15 | template 16 | inline void destroy(T *ptr){ 17 | ptr->~T(); 18 | } 19 | 20 | template 21 | inline void _destroy(ForwardIterator first, ForwardIterator last, _true_type){} 22 | 23 | template 24 | inline void _destroy(ForwardIterator first, ForwardIterator last, _false_type){ 25 | for (; first != last; ++first){ 26 | destroy(&*first); 27 | } 28 | } 29 | 30 | template 31 | inline void destroy(ForwardIterator first, ForwardIterator last){ 32 | typedef typename _type_traits::is_POD_type is_POD_type; 33 | _destroy(first, last, is_POD_type()); 34 | } 35 | 36 | } 37 | 38 | #endif -------------------------------------------------------------------------------- /TinySTL/Deque.h: -------------------------------------------------------------------------------- 1 | #ifndef _DEQUE_H_ 2 | #define _DEQUE_H_ 3 | 4 | #include "Allocator.h" 5 | #include "Iterator.h" 6 | #include "ReverseIterator.h" 7 | #include "Utility.h" 8 | 9 | namespace TinySTL{ 10 | template> 11 | class deque; 12 | namespace Detail{ 13 | //class of deque iterator 14 | template 15 | class dq_iter :public iterator{ 16 | private: 17 | template 18 | friend class ::TinySTL::deque; 19 | private: 20 | //typedef TinySTL::deque* cntrPtr; 21 | typedef const ::TinySTL::deque* cntrPtr; 22 | size_t mapIndex_; 23 | T *cur_; 24 | cntrPtr container_; 25 | public: 26 | dq_iter() :mapIndex_(-1), cur_(0), container_(0){} 27 | dq_iter(size_t index, T *ptr, cntrPtr container) 28 | :mapIndex_(index), cur_(ptr), container_(container){} 29 | dq_iter(const dq_iter& it) 30 | :mapIndex_(it.mapIndex_), cur_(it.cur_), container_(it.container_){} 31 | dq_iter& operator = (const dq_iter& it); 32 | void swap(dq_iter& it); 33 | reference operator *(){ return *cur_; } 34 | const reference operator *()const{ return *cur_; } 35 | pointer operator ->(){ return &(operator*()); } 36 | const pointer operator ->()const{ return &(operator*()); } 37 | dq_iter& operator ++(); 38 | dq_iter operator ++(int); 39 | dq_iter& operator --(); 40 | dq_iter operator --(int); 41 | bool operator ==(const dq_iter& it)const; 42 | bool operator !=(const dq_iter& it)const; 43 | private: 44 | T *getBuckTail(size_t mapIndex)const; 45 | T *getBuckHead(size_t mapIndex)const; 46 | size_t getBuckSize()const; 47 | public: 48 | template 49 | friend dq_iter operator + (const dq_iter& it, typename dq_iter::difference_type n); 50 | template 51 | friend dq_iter operator + (typename dq_iter::difference_type n, const dq_iter& it); 52 | template 53 | friend dq_iter operator - (const dq_iter& it, typename dq_iter::difference_type n); 54 | template 55 | friend dq_iter operator - (typename dq_iter::difference_type n, const dq_iter& it); 56 | template 57 | friend typename dq_iter::difference_type operator - (const dq_iter& it1, const dq_iter& it2); 58 | template 59 | friend void swap(dq_iter& lhs, dq_iter& rhs); 60 | }; 61 | }// end of Detail namespace 62 | 63 | //class of deque 64 | template 65 | class deque{ 66 | private: 67 | template 68 | friend class ::TinySTL::Detail::dq_iter; 69 | public: 70 | typedef T value_type; 71 | typedef Detail::dq_iter iterator; 72 | typedef Detail::dq_iter const_iterator; 73 | typedef T& reference; 74 | typedef const reference const_reference; 75 | typedef size_t size_type; 76 | typedef ptrdiff_t difference_type; 77 | typedef Alloc allocator_type; 78 | private: 79 | typedef Alloc dataAllocator; 80 | enum class EBucksSize{BUCKSIZE = 64}; 81 | private: 82 | iterator beg_, end_; 83 | size_t mapSize_; 84 | T **map_; 85 | public: 86 | deque(); 87 | explicit deque(size_type n, const value_type& val = value_type()); 88 | template 89 | deque(InputIterator first, InputIterator last); 90 | deque(const deque& x); 91 | 92 | ~deque(); 93 | 94 | deque& operator= (const deque& x); 95 | deque& operator= (deque&& x); 96 | 97 | iterator begin(); 98 | iterator end(); 99 | iterator begin()const; 100 | iterator end()const; 101 | public: 102 | size_type size() const{ return end() - begin(); } 103 | bool empty() const{ return begin() == end(); } 104 | 105 | reference operator[] (size_type n); 106 | reference front(); 107 | reference back(); 108 | const_reference operator[] (size_type n) const; 109 | const_reference front() const; 110 | const_reference back() const; 111 | 112 | void push_back(const value_type& val); 113 | void push_front(const value_type& val); 114 | void pop_back(); 115 | void pop_front(); 116 | void swap(deque& x); 117 | void clear(); 118 | private: 119 | T *getANewBuck(); 120 | T** getANewMap(const size_t size); 121 | size_t getNewMapSize(const size_t size); 122 | size_t getBuckSize()const; 123 | void init(); 124 | bool back_full()const; 125 | bool front_full()const; 126 | void deque_aux(size_t n, const value_type& val, std::true_type); 127 | template 128 | void deque_aux(Iterator first, Iterator last, std::false_type); 129 | void reallocateAndCopy(); 130 | public: 131 | template 132 | friend bool operator== (const deque& lhs, const deque& rhs); 133 | template 134 | friend bool operator!= (const deque& lhs, const deque& rhs); 135 | template 136 | friend void swap(deque& x, deque& y); 137 | };//end of deque 138 | } 139 | 140 | #include "Detail\Deque.impl.h" 141 | #endif -------------------------------------------------------------------------------- /TinySTL/Detail/AVLTree.impl.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Detail/AVLTree.impl.h -------------------------------------------------------------------------------- /TinySTL/Detail/Alloc.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Detail/Alloc.cpp -------------------------------------------------------------------------------- /TinySTL/Detail/BinarySearchTree.impl.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Detail/BinarySearchTree.impl.h -------------------------------------------------------------------------------- /TinySTL/Detail/Bitmap.impl.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Detail/Bitmap.impl.h -------------------------------------------------------------------------------- /TinySTL/Detail/COWPtr.impl.h: -------------------------------------------------------------------------------- 1 | #ifndef _COWPTR_IMPL_H_ 2 | #define _COWPTR_IMPL_H_ 3 | 4 | namespace TinySTL{ 5 | namespace Detail{ 6 | template 7 | const T& proxy::operator *()const{ 8 | return *(cp_->ptr_); 9 | } 10 | template 11 | T& proxy::operator *(){ 12 | auto t = *(cp_->ptr_); 13 | cp_->ptr_ = make_shared(t); 14 | return *(cp_->ptr_); 15 | } 16 | template 17 | const T *proxy::operator ->()const{ 18 | return cp_->ptr_.operator->(); 19 | } 20 | template 21 | T *proxy::operator ->(){ 22 | auto t = *(cp_->ptr_); 23 | cp_->ptr_ = make_shared(t); 24 | return cp_->ptr_.operator->(); 25 | } 26 | template 27 | cow_ptr& proxy::operator = (const T& val){ 28 | cp_->ptr_ = make_shared(val); 29 | return *cp_; 30 | } 31 | template 32 | proxy::operator T()const{ return *(cp_->ptr_); } 33 | } 34 | template 35 | cow_ptr::cow_ptr(T *p = nullptr) :ptr_(p){} 36 | template 37 | template 38 | cow_ptr::cow_ptr(T *p, D d) : ptr_(p, d){} 39 | template 40 | cow_ptr::cow_ptr(const cow_ptr& cp){ 41 | ptr_ = cp.ptr_; 42 | } 43 | template 44 | cow_ptr& cow_ptr::operator = (const cow_ptr& cp){ 45 | if (this != &cp){ 46 | ptr_.decrease_ref(); 47 | ptr_ = cp.ptr_; 48 | } 49 | return *this; 50 | } 51 | template 52 | typename cow_ptr::element_type *cow_ptr::get(){ 53 | return ptr_.get(); 54 | } 55 | template 56 | const typename cow_ptr::element_type *cow_ptr::get()const{ 57 | return ptr_.get(); 58 | } 59 | template 60 | cow_ptr::operator bool()const{ 61 | return ptr_ != nullptr; 62 | } 63 | template 64 | const typename cow_ptr::proxy cow_ptr::operator *()const{ 65 | return proxy(const_cast(this)); 66 | } 67 | template 68 | typename cow_ptr::proxy cow_ptr::operator *(){ 69 | return proxy(this); 70 | } 71 | template 72 | const typename cow_ptr::proxy cow_ptr::operator ->()const{ 73 | return proxy(const_cast(this)); 74 | } 75 | template 76 | typename cow_ptr::proxy cow_ptr::operator ->(){ 77 | return proxy(this); 78 | } 79 | 80 | template 81 | bool operator == (const cow_ptr& cp1, const cow_ptr& cp2){ 82 | return cp1.ptr_ == cp2.ptr_; 83 | } 84 | template 85 | bool operator == (const cow_ptr& cp, nullptr_t p){ 86 | return cp.ptr_ == p; 87 | } 88 | template 89 | bool operator == (nullptr_t p, const cow_ptr& cp){ 90 | return cp == p; 91 | } 92 | template 93 | bool operator != (const cow_ptr& cp1, const cow_ptr& cp2){ 94 | return !(cp1 == cp2); 95 | } 96 | template 97 | bool operator != (const cow_ptr& cp, nullptr_t p){ 98 | return !(cp == p); 99 | } 100 | template 101 | bool operator != (nullptr_t p, const cow_ptr& cp){ 102 | return !(cp == p); 103 | } 104 | } 105 | 106 | #endif -------------------------------------------------------------------------------- /TinySTL/Detail/CircularBuffer.impl.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Detail/CircularBuffer.impl.h -------------------------------------------------------------------------------- /TinySTL/Detail/Deque.impl.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Detail/Deque.impl.h -------------------------------------------------------------------------------- /TinySTL/Detail/Graph.impl.h: -------------------------------------------------------------------------------- 1 | #include "../Graph.h" 2 | 3 | namespace TinySTL{ 4 | namespace Detail{ 5 | template 6 | typename graph::node_type 7 | graph::make_node(const Index& index, const Value& val){ 8 | return node_type(index, val); 9 | } 10 | template 11 | typename const graph::node_type& 12 | graph::get_node(const Index& index){ 13 | for (auto& pair : nodes_){ 14 | if (equal_func(pair.first.first, index)) 15 | return pair.first; 16 | } 17 | static node_type empty_node; 18 | return empty_node; 19 | } 20 | template 21 | bool graph::is_contained(const Index& index){ 22 | for (auto& pair : nodes_){ 23 | if (equal_func(pair.first.first, index)) 24 | return true; 25 | } 26 | return false; 27 | } 28 | template 29 | typename graph::nodes_set_type 30 | graph::empty_node_set(){ 31 | return nodes_set_type(); 32 | } 33 | template 34 | bool graph::empty()const{ 35 | return nodes_.empty(); 36 | } 37 | template 38 | typename graph::inner_iterator 39 | graph::begin(const Index& index){ 40 | for (auto& pair : nodes_){ 41 | if (equal_func(pair.first.first, index)) 42 | return inner_iterator(this, (pair.second).begin()); 43 | } 44 | return end(index); 45 | } 46 | template 47 | typename graph::inner_iterator 48 | graph::end(const Index& index){ 49 | for (auto& pair : nodes_){ 50 | if (equal_func(pair.first.first, index)) 51 | return inner_iterator(this, (pair.second).end()); 52 | } 53 | return inner_iterator(); 54 | } 55 | template 56 | typename graph::iterator graph::begin(){ 57 | return iterator(this, nodes_.begin()); 58 | } 59 | template 60 | typename graph::iterator graph::end(){ 61 | return iterator(this, nodes_.end()); 62 | } 63 | template 64 | size_t graph::size()const{ 65 | return size_; 66 | } 67 | template 68 | typename graph::nodes_set_type 69 | graph::adjacent_nodes(const Index& index){ 70 | nodes_set_type s; 71 | for (auto it = begin(index); it != end(index); ++it){ 72 | s.push_back(*it); 73 | } 74 | return s; 75 | } 76 | template 77 | typename graph::nodes_set_type 78 | graph::adjacent_nodes(const node_type& n){ 79 | return adjacent_nodes(n.first); 80 | } 81 | template 82 | void graph::_DFS(node_type& node, 83 | visiter_func_type func, Unordered_set, EqualFunc>& visited){ 84 | auto nodes = adjacent_nodes(node.first); 85 | func(node); 86 | visited.insert(node.first); 87 | for (auto& n : nodes){ 88 | if (visited.count(n.first) == 0)//has not visited 89 | _DFS(n, func, visited); 90 | } 91 | } 92 | template 93 | void graph::DFS(const Index& index, visiter_func_type func){ 94 | node_type start = (get_node(index)); 95 | Unordered_set, EqualFunc> visited(7); 96 | _DFS(start, func, visited); 97 | } 98 | template 99 | void graph::_BFS(node_type& node, 100 | visiter_func_type func, Unordered_set, EqualFunc>& visited){ 101 | auto nodes = adjacent_nodes(node.first); 102 | func(node); 103 | visited.insert(node.first); 104 | do{ 105 | nodes_set_type temp; 106 | for (auto it = nodes.begin(); it != nodes.end(); ++it){ 107 | if (visited.count(it->first) == 0){//has not visited 108 | func(*it); 109 | visited.insert(it->first); 110 | auto s = adjacent_nodes(it->first); 111 | temp.insert(temp.end(), s.begin(), s.end()); 112 | } 113 | } 114 | nodes = temp; 115 | } while (!nodes.empty()); 116 | } 117 | template 118 | void graph::BFS(const Index& index, visiter_func_type func){ 119 | node_type start = (get_node(index)); 120 | Unordered_set, EqualFunc> visited(7); 121 | _BFS(start, func, visited); 122 | } 123 | template 124 | string graph::to_string(){ 125 | string str; 126 | std::ostringstream oss; 127 | for (auto oit = begin(); oit != end(); ++oit){ 128 | 129 | oss << "[" << oit->first << "," << oit->second << "]" << ":"; 130 | auto eit = end(oit->first); 131 | for (auto iit = begin(oit->first); iit != eit; ++iit){ 132 | oss << "[" << iit->first << "," << iit->second << "]" << "->"; 133 | } 134 | oss << "[nil]" << std::endl << std::setw(4) << "|" << std::endl; 135 | } 136 | oss << "[nil]" << std::endl; 137 | str.append(oss.str().c_str()); 138 | return str; 139 | } 140 | template 141 | typename graph::equal_func_type 142 | graph::get_equal_func()const{ 143 | return equal_func; 144 | } 145 | //******************************************************************************** 146 | template 147 | inner_iterator& inner_iterator::operator ++(){ 148 | ++inner_it_; 149 | return *this; 150 | } 151 | template 152 | const inner_iterator inner_iterator::operator ++(int){ 153 | auto temp = *this; 154 | ++*this; 155 | return temp; 156 | } 157 | template 158 | bool operator ==(const inner_iterator& lhs, 159 | const inner_iterator& rhs){ 160 | return lhs.container_ == rhs.container_ && lhs.inner_it_ == rhs.inner_it_; 161 | } 162 | template 163 | bool operator !=(const inner_iterator& lhs, 164 | const inner_iterator& rhs){ 165 | return !(lhs == rhs); 166 | } 167 | //********************************************************************************* 168 | template 169 | outter_iterator& outter_iterator::operator ++(){ 170 | ++outter_it_; 171 | return *this; 172 | } 173 | template 174 | const outter_iterator outter_iterator::operator ++(int){ 175 | auto temp = *this; 176 | ++*this; 177 | return temp; 178 | } 179 | template 180 | bool operator ==(const outter_iterator& lhs, 181 | const outter_iterator& rhs){ 182 | return lhs.container_ == rhs.container_ && lhs.outter_it_ == rhs.outter_it_; 183 | } 184 | template 185 | bool operator !=(const outter_iterator& lhs, 186 | const outter_iterator& rhs){ 187 | return !(lhs == rhs); 188 | } 189 | }//end of Detail 190 | 191 | template 192 | directed_graph::directed_graph():graph(){} 193 | template 194 | void directed_graph::add_node_helper(const Index& index, const nodes_set_type& nodes){ 195 | if (nodes.empty()) 196 | return; 197 | //find node n's list 198 | list* l; 199 | for (auto& pair : nodes_){ 200 | if (equal_func(pair.first.first, index)) 201 | l = &(pair.second); 202 | } 203 | for (const auto& item : nodes){ 204 | l->push_front(item); 205 | if (!is_contained(item.first)){ 206 | add_node(item, empty_node_set()); 207 | } 208 | } 209 | } 210 | template 211 | void directed_graph::add_node(const node_type& n, const nodes_set_type& nodes){ 212 | if (!is_contained(n.first)){ 213 | nodes_.push_front(make_pair(n, list())); 214 | ++size_; 215 | } 216 | add_node_helper(n.first, nodes); 217 | } 218 | template 219 | void directed_graph::add_node(const Index& index, const nodes_set_type& nodes){ 220 | add_node_helper(index, nodes); 221 | } 222 | template 223 | void directed_graph::delete_node(const Index& index){ 224 | for (auto oit = nodes_.begin(); oit != nodes_.end();){ 225 | auto& l = oit->second; 226 | if (equal_func((oit->first).first, index)){ 227 | oit = nodes_.erase(oit); 228 | }else{ 229 | for (auto iit = l.begin(); iit != l.end();){ 230 | if (equal_func(iit->first, index)) 231 | iit = l.erase(iit); 232 | else 233 | ++iit; 234 | } 235 | ++oit; 236 | } 237 | } 238 | } 239 | template 240 | void directed_graph::delete_node(const node_type& item){ 241 | delete_node(item.first); 242 | } 243 | template 244 | void directed_graph::make_edge(const Index& index1, const Index& index2){ 245 | auto node1 = get_node(index1), node2 = get_node(index2); 246 | for (auto it = nodes_.begin(); it != nodes_.end(); ++it){ 247 | if (equal_func((it->first).first, index1)) 248 | (it->second).push_front(node2); 249 | } 250 | } 251 | } -------------------------------------------------------------------------------- /TinySTL/Detail/List.impl.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Detail/List.impl.h -------------------------------------------------------------------------------- /TinySTL/Detail/Ref.h: -------------------------------------------------------------------------------- 1 | #ifndef _REF_H_ 2 | #define _REF_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace TinySTL{ 9 | namespace Detail{ 10 | template 11 | struct _default_delete{ 12 | void operator ()(T* ptr){ if (ptr) delete ptr; } 13 | }; 14 | 15 | template 16 | struct ref_t{ 17 | using deleter_type = std::function < void(T*) >; 18 | 19 | std::atomic ncount_; 20 | T *data_; 21 | deleter_type deleter_; 22 | 23 | explicit ref_t(T *p = nullptr, deleter_type pfunc = deleter_type(_default_delete())) 24 | : ncount_(0), data_(p), deleter_(pfunc){ 25 | if (data_) 26 | ncount_ = 1; 27 | } 28 | ref_t(const ref_t&) = delete; 29 | ref_t& operator = (const ref_t&) = delete; 30 | 31 | ~ref_t(){ 32 | --ncount_; 33 | if (ncount_ == 0) 34 | deleter_(data_); 35 | } 36 | 37 | size_t count()const{ return ncount_.load(); } 38 | T *get_data()const{ return data_; } 39 | 40 | ref_t& operator ++(){ 41 | ++ncount_; 42 | return *this; 43 | } 44 | ref_t operator ++(int){ 45 | auto t = *this; 46 | ++*this; 47 | return t; 48 | } 49 | ref_t& operator --(){ 50 | --ncount_; 51 | return *this; 52 | } 53 | ref_t operator --(int){ 54 | auto t = *this; 55 | --*this; 56 | return t; 57 | } 58 | }; 59 | template 60 | bool operator ==(const ref_t& lhs, const ref_t& rhs){ 61 | return lhs.get_data() == rhs.get_data(); 62 | } 63 | template 64 | bool operator !=(const ref_t& lhs, const ref_t& rhs){ 65 | return !(lhs == rhs); 66 | } 67 | } 68 | } 69 | 70 | #endif -------------------------------------------------------------------------------- /TinySTL/Detail/String.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Detail/String.cpp -------------------------------------------------------------------------------- /TinySTL/Detail/TrieTree.cpp: -------------------------------------------------------------------------------- 1 | #include "../TrieTree.h" 2 | 3 | namespace TinySTL{ 4 | trie_tree::trie_tree() :root_(new trie_node), size_(0){} 5 | trie_tree::~trie_tree(){ 6 | if (root_){ 7 | root_->map_childs.clear(); 8 | delete root_; 9 | } 10 | } 11 | bool trie_tree::empty()const{ 12 | return size() == 0; 13 | } 14 | trie_tree::size_type trie_tree::size()const{ 15 | return size_; 16 | } 17 | bool trie_tree::is_existed(const string& word)const{ 18 | if (word.empty()) 19 | return false; 20 | auto root = get_root(); 21 | auto res = root->map_childs.find(word[0]); 22 | if (res == root->map_childs.end())//not found 23 | return false; 24 | else 25 | return _is_existed(word, res->second); 26 | } 27 | bool trie_tree::_is_existed(const string& word, const node_ptr& up)const{ 28 | if (word.size() == 1) 29 | return up->is_a_word; 30 | char ch = word[1]; 31 | auto res = up->map_childs.find(ch); 32 | if (res == up->map_childs.end())//not found 33 | return false; 34 | else 35 | return _is_existed(word.substr(1), res->second); 36 | } 37 | trie_tree::node_ptr trie_tree::make_node(char ch, bool is_a_word){ 38 | return std::make_unique(ch, is_a_word); 39 | } 40 | bool trie_tree::insert(const string& word){ 41 | if (is_existed(word)) 42 | return true; 43 | if (word.empty()) 44 | return false; 45 | char ch = word[0]; 46 | auto root = get_root(); 47 | auto res = root->map_childs.find(ch); 48 | if (res != root->map_childs.end()){ 49 | return _insert(word.substr(1), res->second); 50 | }else{ 51 | auto is_a_word = (word.size() == 1 ? true : false); 52 | auto node = make_node(ch, is_a_word); 53 | root->map_childs[ch] = std::move(node); 54 | return _insert(word.substr(1), root->map_childs[ch]); 55 | } 56 | } 57 | bool trie_tree::_insert(const string& word, const node_ptr& up){ 58 | if (word.empty()){ 59 | ++size_; 60 | up->is_a_word = true; 61 | return true; 62 | } 63 | char ch = word[0]; 64 | auto res = up->map_childs.find(ch); 65 | if (res != up->map_childs.end()){ 66 | return _insert(word.substr(1), res->second); 67 | }else{ 68 | auto is_a_word = (word.size() == 1 ? true : false); 69 | auto node = make_node(ch, is_a_word); 70 | up->map_childs[ch] = std::move(node); 71 | return _insert(word.substr(1), up->map_childs[ch]); 72 | } 73 | } 74 | void trie_tree::print_tree(std::ostream& os)const{ 75 | auto root = get_root(); 76 | if (root == NULL) 77 | os << "the trie_tree is empty!" << std::endl; 78 | for (auto cit = root->map_childs.cbegin(); cit != root->map_childs.cend(); ++cit) 79 | _print_tree(os, cit->second, string()); 80 | } 81 | void trie_tree::_print_tree(std::ostream& os, const node_ptr& up, string word)const{ 82 | word += up->data; 83 | if (up->is_a_word) 84 | os << word << std::endl; 85 | for (auto cit = up->map_childs.cbegin(); cit != up->map_childs.cend(); ++cit){ 86 | _print_tree(os, cit->second, word); 87 | } 88 | } 89 | vector trie_tree::get_word_by_prefix(const string& prefix)const{ 90 | vector words; 91 | auto root = get_root(); 92 | if (root == NULL || prefix.size() == 0) 93 | return words; 94 | char ch = prefix[0]; 95 | auto res = root->map_childs.find(ch); 96 | if (res != root->map_childs.end()) 97 | _get_word_by_prefix(prefix, res->second, prefix, words); 98 | return words; 99 | } 100 | void trie_tree::_get_word_by_prefix(const string& prefix, const node_ptr& up, 101 | const string& real_prefix, vector& words)const{ 102 | if (prefix.size() == 1){ 103 | if (up->is_a_word) 104 | words.push_back(real_prefix); 105 | for (auto cit = up->map_childs.cbegin(); cit != up->map_childs.cend(); ++cit){ 106 | __get_word_by_prefix(cit->second, string(), real_prefix, words); 107 | } 108 | }else{ 109 | char ch = prefix[1]; 110 | auto res = up->map_childs.find(ch); 111 | if (res != up->map_childs.end()){ 112 | _get_word_by_prefix(prefix.substr(1), res->second, real_prefix, words); 113 | } 114 | } 115 | } 116 | void trie_tree::__get_word_by_prefix(const node_ptr& up, string& word, const string& prefix, vector& words)const{ 117 | word += up->data; 118 | if (up->is_a_word) 119 | words.push_back(prefix + word); 120 | for (auto cit = up->map_childs.cbegin(); cit != up->map_childs.cend(); ++cit){ 121 | __get_word_by_prefix(cit->second, string(word), prefix, words); 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /TinySTL/Detail/Unordered_set.impl.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Detail/Unordered_set.impl.h -------------------------------------------------------------------------------- /TinySTL/Detail/Vector.impl.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Detail/Vector.impl.h -------------------------------------------------------------------------------- /TinySTL/Functional.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTIONAL_H_ 2 | #define _FUNCTIONAL_H_ 3 | 4 | namespace TinySTL{ 5 | //********** [less] **************** 6 | template 7 | struct less{ 8 | typedef T first_argument_type; 9 | typedef T second_argument_type; 10 | typedef bool result_type; 11 | 12 | result_type operator()(const first_argument_type& x, const second_argument_type& y){ 13 | return x < y; 14 | } 15 | }; 16 | //********** [equal_to] **************** 17 | template 18 | struct equal_to{ 19 | typedef T first_argument_type; 20 | typedef T second_argument_type; 21 | typedef bool result_type; 22 | 23 | result_type operator()(const first_argument_type& x, const second_argument_type& y){ 24 | return x == y; 25 | } 26 | }; 27 | } 28 | #endif -------------------------------------------------------------------------------- /TinySTL/Graph.h: -------------------------------------------------------------------------------- 1 | #ifndef _GRAPH_H_ 2 | #define _GRAPH_H_ 3 | 4 | #include "Iterator.h" 5 | #include "List.h" 6 | #include "String.h" 7 | #include "Unordered_set.h" 8 | #include "Utility.h" 9 | #include "Vector.h" 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | namespace TinySTL{ 16 | namespace Detail{ 17 | template 18 | class inner_iterator; 19 | template 20 | class outter_iterator; 21 | 22 | template> 23 | class graph{//base class 24 | public: 25 | friend class inner_iterator < Index, Value, EqualFunc >; 26 | friend class outter_iterator < Index, Value, EqualFunc > ; 27 | public: 28 | typedef Index index_type; 29 | typedef Value value_type; 30 | typedef EqualFunc equal_func_type; 31 | typedef pair node_type; 32 | typedef vector nodes_set_type; 33 | typedef std::function visiter_func_type; 34 | typedef outter_iterator iterator; 35 | typedef inner_iterator inner_iterator; 36 | public: 37 | graph() :size_(0){}; 38 | virtual ~graph(){}; 39 | 40 | //node can be not in the graph 41 | virtual void add_node(const node_type& item, const nodes_set_type& nodes) = 0; 42 | //node of the index must in the graph 43 | virtual void add_node(const Index& index, const nodes_set_type& nodes) = 0; 44 | virtual void make_edge(const Index& index1, const Index& index2) = 0; 45 | 46 | virtual void delete_node(const node_type& item) = 0; 47 | virtual void delete_node(const Index& index) = 0; 48 | 49 | void DFS(const Index& index, visiter_func_type func); 50 | void BFS(const Index& index, visiter_func_type func); 51 | 52 | node_type make_node(const Index& index, const Value& val); 53 | const node_type& get_node(const Index& index); 54 | 55 | bool is_contained(const Index& index); 56 | inline static nodes_set_type empty_node_set(); 57 | nodes_set_type adjacent_nodes(const Index& index); 58 | nodes_set_type adjacent_nodes(const node_type& n); 59 | 60 | bool empty()const; 61 | size_t size()const; 62 | inner_iterator begin(const Index& index); 63 | inner_iterator end(const Index& index); 64 | iterator begin(); 65 | iterator end(); 66 | 67 | equal_func_type get_equal_func()const; 68 | string to_string(); 69 | protected: 70 | void _DFS(node_type& node, visiter_func_type func, Unordered_set, EqualFunc>& visited); 71 | void _BFS(node_type& node, visiter_func_type func, Unordered_set, EqualFunc>& visited); 72 | protected: 73 | list>> nodes_; 74 | equal_func_type equal_func; 75 | size_t size_; 76 | }; 77 | 78 | template> 79 | class inner_iterator :public iterator::node_type>{ 81 | public: 82 | friend class graph < Index, Value, EqualFunc > ; 83 | typedef graph* cntrPtr; 84 | typedef graph graph_type; 85 | typedef typename list::iterator inner_it_type; 86 | public: 87 | explicit inner_iterator(cntrPtr c = nullptr, inner_it_type iit = inner_it_type()) 88 | :container_(c), inner_it_(iit){} 89 | 90 | inner_iterator& operator ++(); 91 | const inner_iterator operator ++(int); 92 | 93 | typename graph_type::node_type& operator*(){ return *inner_it_; } 94 | typename graph_type::node_type* operator ->(){ return &(operator*()); } 95 | private: 96 | cntrPtr container_; 97 | inner_it_type inner_it_; 98 | public: 99 | template 100 | friend bool operator ==(const inner_iterator& lhs, 101 | const inner_iterator& rhs); 102 | template 103 | friend bool operator !=(const inner_iterator& lhs, 104 | const inner_iterator& rhs); 105 | }; 106 | template> 107 | class outter_iterator :public iterator::node_type>{ 109 | public: 110 | friend class graph < Index, Value, EqualFunc >; 111 | typedef graph* cntrPtr; 112 | typedef graph graph_type; 113 | typedef typename list>>::iterator outter_it_type; 114 | private: 115 | cntrPtr container_; 116 | outter_it_type outter_it_; 117 | public: 118 | explicit outter_iterator(cntrPtr c = nullptr, outter_it_type oit = outter_it_type()) 119 | :container_(c), outter_it_(oit){} 120 | 121 | outter_iterator& operator ++(); 122 | const outter_iterator operator ++(int); 123 | 124 | typename graph_type::node_type& operator*(){ return outter_it_->first; } 125 | typename graph_type::node_type* operator ->(){ return &(operator*()); } 126 | public: 127 | template 128 | friend bool operator ==(const outter_iterator& lhs, 129 | const outter_iterator& rhs); 130 | template 131 | friend bool operator !=(const outter_iterator& lhs, 132 | const outter_iterator& rhs); 133 | }; 134 | }//end of namespace Detail 135 | 136 | //directed graph 137 | template> 138 | class directed_graph :public Detail::graph < Index, Value, EqualFunc > { 139 | public: 140 | directed_graph(); 141 | ~directed_graph(){} 142 | //node n -> every node_type in the nodes set 143 | void add_node(const node_type& n, const nodes_set_type& nodes) override final; 144 | void add_node(const Index& index, const nodes_set_type& nodes) override final; 145 | //node index1 -> node index2 146 | void make_edge(const Index& index1, const Index& index2) override final; 147 | 148 | void delete_node(const node_type& item) override final; 149 | void delete_node(const Index& index) override final; 150 | private: 151 | void add_node_helper(const Index& index, const nodes_set_type& nodes); 152 | }; 153 | } 154 | 155 | #include "Detail\Graph.impl.h" 156 | #endif -------------------------------------------------------------------------------- /TinySTL/Iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef _ITERATOR_H_ 2 | #define _ITERATOR_H_ 3 | 4 | namespace TinySTL{ 5 | 6 | struct input_iterator_tag{}; 7 | struct output_iterator_tag{}; 8 | struct forward_iterator_tag :public input_iterator_tag {}; 9 | struct bidirectional_iterator_tag :public forward_iterator_tag {}; 10 | struct random_access_iterator_tag :public bidirectional_iterator_tag {}; 11 | 12 | template struct input_iterator 13 | { 14 | typedef input_iterator_tag iterator_category; 15 | typedef T value_type; 16 | typedef Distance difference_type; 17 | typedef T* pointer; 18 | typedef T& reference; 19 | }; 20 | struct output_iterator 21 | { 22 | typedef output_iterator_tag iterator_category; 23 | typedef void value_type; 24 | typedef void difference_type; 25 | typedef void pointer; 26 | typedef void reference; 27 | }; 28 | template struct forward_iterator 29 | { 30 | typedef forward_iterator_tag iterator_category; 31 | typedef T value_type; 32 | typedef Distance difference_type; 33 | typedef T* pointer; 34 | typedef T& reference; 35 | }; 36 | template struct bidirectional_iterator 37 | { 38 | typedef bidirectional_iterator_tag iterator_category; 39 | typedef T value_type; 40 | typedef Distance difference_type; 41 | typedef T* pointer; 42 | typedef T& reference; 43 | }; 44 | template struct random_access_iterator 45 | { 46 | typedef random_access_iterator_tag iterator_category; 47 | typedef T value_type; 48 | typedef Distance difference_type; 49 | typedef T* pointer; 50 | typedef T& reference; 51 | }; 52 | 53 | template 55 | struct iterator 56 | { 57 | typedef Category iterator_category; 58 | typedef T value_type; 59 | typedef Distance difference_type; 60 | typedef Pointer pointer; 61 | typedef Reference reference; 62 | }; 63 | 64 | template 65 | struct iterator_traits 66 | { 67 | typedef typename Iterator::iterator_category iterator_category; 68 | typedef typename Iterator::value_type value_type; 69 | typedef typename Iterator::difference_type difference_type; 70 | typedef typename Iterator::pointer pointer; 71 | typedef typename Iterator::reference reference; 72 | }; 73 | template 74 | struct iterator_traits 75 | { 76 | typedef random_access_iterator_tag iterator_category; 77 | typedef T value_type; 78 | typedef ptrdiff_t difference_type; 79 | typedef T* pointer; 80 | typedef T& reference; 81 | }; 82 | template 83 | struct iterator_traits 84 | { 85 | typedef random_access_iterator_tag iterator_category; 86 | typedef T value_type; 87 | typedef ptrdiff_t difference_type; 88 | typedef const T* pointer; 89 | typedef const T& reference; 90 | }; 91 | 92 | template 93 | inline typename iterator_traits::iterator_category 94 | iterator_category(const Iterator& It){ 95 | typedef typename iterator_traits::iterator_category category; 96 | return category(); 97 | } 98 | template 99 | inline typename iterator_traits::value_type* 100 | value_type(const Iterator& It){ 101 | return static_cast::value_type*>(0); 102 | } 103 | template 104 | inline typename iterator_traits::difference_type* 105 | difference_type(const Iterator& It){ 106 | return static_cast::difference_type*>(0); 107 | } 108 | } 109 | 110 | #endif -------------------------------------------------------------------------------- /TinySTL/List.h: -------------------------------------------------------------------------------- 1 | #ifndef _LIST_H_ 2 | #define _LIST_H_ 3 | 4 | #include "Allocator.h" 5 | #include "Iterator.h" 6 | #include "ReverseIterator.h" 7 | #include "UninitializedFunctions.h" 8 | 9 | #include 10 | 11 | namespace TinySTL{ 12 | template 13 | class list; 14 | namespace Detail{ 15 | //the class of node 16 | template 17 | struct node{ 18 | T data; 19 | node *prev; 20 | node *next; 21 | list *container; 22 | node(const T& d, node *p, node *n, list *c): 23 | data(d), prev(p), next(n), container(c){} 24 | bool operator ==(const node& n){ 25 | return data == n.data && prev == n.prev && next == n.next && container == n.container; 26 | } 27 | }; 28 | //the class of list iterator 29 | template 30 | struct listIterator :public iterator{ 31 | template 32 | friend class list; 33 | public: 34 | typedef node* nodePtr; 35 | nodePtr p; 36 | public: 37 | explicit listIterator(nodePtr ptr = nullptr) :p(ptr){} 38 | 39 | listIterator& operator++(); 40 | listIterator operator++(int); 41 | listIterator& operator --(); 42 | listIterator operator --(int); 43 | T& operator *(){ return p->data; } 44 | T* operator ->(){ return &(operator*()); } 45 | 46 | template 47 | friend bool operator ==(const listIterator& lhs, const listIterator& rhs); 48 | template 49 | friend bool operator !=(const listIterator& lhs, const listIterator& rhs); 50 | }; 51 | }//end of namespace 52 | 53 | 54 | //the class of list 55 | template 56 | class list{ 57 | template 58 | friend struct listIterator; 59 | private: 60 | typedef allocator> nodeAllocator; 61 | typedef Detail::node *nodePtr; 62 | public: 63 | typedef T value_type; 64 | typedef Detail::listIterator iterator; 65 | typedef Detail::listIterator const_iterator; 66 | typedef reverse_iterator_t reverse_iterator; 67 | typedef T& reference; 68 | typedef size_t size_type; 69 | private: 70 | iterator head; 71 | iterator tail; 72 | public: 73 | list(); 74 | explicit list(size_type n, const value_type& val = value_type()); 75 | template 76 | list(InputIterator first, InputIterator last); 77 | list(const list& l); 78 | list& operator = (const list& l); 79 | ~list(); 80 | 81 | bool empty()const{ return head == tail; } 82 | size_type size()const; 83 | reference front(){ return (head.p->data); } 84 | reference back(){ return (tail.p->prev->data); } 85 | 86 | void push_front(const value_type& val); 87 | void pop_front(); 88 | void push_back(const value_type& val); 89 | void pop_back(); 90 | 91 | iterator begin(); 92 | iterator end(); 93 | const_iterator begin()const; 94 | const_iterator end()const; 95 | reverse_iterator rbegin(); 96 | reverse_iterator rend(); 97 | 98 | iterator insert(iterator position, const value_type& val); 99 | void insert(iterator position, size_type n, const value_type& val); 100 | template 101 | void insert(iterator position, InputIterator first, InputIterator last); 102 | iterator erase(iterator position); 103 | iterator erase(iterator first, iterator last); 104 | void swap(list& x); 105 | void clear(); 106 | void splice(iterator position, list& x); 107 | void splice(iterator position, list& x, iterator i); 108 | void splice(iterator position, list& x, iterator first, iterator last); 109 | void remove(const value_type& val); 110 | template 111 | void remove_if(Predicate pred); 112 | void unique(); 113 | template 114 | void unique(BinaryPredicate binary_pred); 115 | void merge(list& x); 116 | template 117 | void merge(list& x, Compare comp); 118 | void sort(); 119 | template 120 | void sort(Compare comp); 121 | void reverse(); 122 | private: 123 | void ctorAux(size_type n, const value_type& val, std::true_type); 124 | template 125 | void ctorAux(InputIterator first, InputIterator last, std::false_type); 126 | nodePtr newNode(const T& val = T()); 127 | void deleteNode(nodePtr p); 128 | void insert_aux(iterator position, size_type n, const T& val, std::true_type); 129 | template 130 | void insert_aux(iterator position, InputIterator first, InputIterator last, std::false_type); 131 | const_iterator changeIteratorToConstIterator(iterator& it)const; 132 | public: 133 | template 134 | friend void swap(list& x, list& y); 135 | template 136 | friend bool operator== (const list& lhs, const list& rhs); 137 | template 138 | friend bool operator!= (const list& lhs, const list& rhs); 139 | };//end of List 140 | } 141 | 142 | #include "Detail\List.impl.h" 143 | #endif -------------------------------------------------------------------------------- /TinySTL/Memory.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Memory.h -------------------------------------------------------------------------------- /TinySTL/Profiler/Profiler.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Profiler/Profiler.cpp -------------------------------------------------------------------------------- /TinySTL/Profiler/Profiler.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Profiler/Profiler.h -------------------------------------------------------------------------------- /TinySTL/Queue.h: -------------------------------------------------------------------------------- 1 | #ifndef _QUEUE_H_ 2 | #define _QUEUE_H_ 3 | 4 | #include "Deque.h" 5 | #include "Functional.h" 6 | #include "Vector.h" 7 | 8 | namespace TinySTL{ 9 | //class of queue 10 | template> 11 | class queue{ 12 | public: 13 | typedef T value_type; 14 | typedef Container container_type; 15 | typedef typename Container::reference reference; 16 | typedef typename Container::const_reference const_reference; 17 | typedef typename Container::size_type size_type; 18 | private: 19 | Container container_; 20 | public: 21 | queue(){} 22 | explicit queue(const container_type& ctnr) :container_(ctnr){} 23 | 24 | bool empty() const{ return container_.empty(); } 25 | size_type size() const{ return container_.size(); } 26 | reference& front(){ return container_.front(); } 27 | const_reference& front() const{ return container_.front(); } 28 | reference& back(){ return container_.back(); } 29 | const_reference& back() const{ return container_.back(); } 30 | void push(const value_type& val){ container_.push_back(val); } 31 | void pop(){ container_.pop_front(); } 32 | void swap(queue& x){ container_.swap(x.container_); } 33 | public: 34 | template 35 | friend bool operator== (const queue& lhs, const queue& rhs); 36 | template 37 | friend bool operator!= (const queue& lhs, const queue& rhs); 38 | template 39 | friend void swap(queue& x, queue& y); 40 | }; 41 | template 42 | bool operator== (const queue& lhs, const queue& rhs){ 43 | return lhs.container_ == rhs.container_; 44 | } 45 | template 46 | bool operator!= (const queue& lhs, const queue& rhs){ 47 | return lhs.container_ != rhs.container_; 48 | } 49 | template 50 | void swap(queue& x, queue& y){ 51 | TinySTL::swap(x.container_, y.container_); 52 | } 53 | //class of priority_queue 54 | template , 55 | class Compare = TinySTL::less> 56 | class priority_queue{ 57 | public: 58 | typedef T value_type; 59 | typedef Container container_type; 60 | typedef typename Container::reference reference; 61 | typedef typename Container::const_reference const_reference; 62 | typedef typename Container::size_type size_type; 63 | private: 64 | container_type container_; 65 | Compare compare_; 66 | public: 67 | explicit priority_queue(const Compare& comp = Compare(), 68 | const Container& ctnr = Container()) 69 | : container_(ctnr), compare_(comp){} 70 | template 71 | priority_queue(InputIterator first, InputIterator last, 72 | const Compare& comp = Compare(), 73 | const Container& ctnr = Container()) 74 | : container_(ctnr), compare_(comp){ 75 | container_.insert(container_.end(), first, last); 76 | TinySTL::make_heap(container_.begin(), container_.end()); 77 | } 78 | bool empty() const{ 79 | return container_.empty(); 80 | } 81 | size_type size() const{ 82 | return container_.size(); 83 | } 84 | reference top() { 85 | return container_.front(); 86 | } 87 | void push(const value_type& val){ 88 | container_.push_back(val); 89 | TinySTL::push_heap(container_.begin(), container_.end(), compare_); 90 | } 91 | void pop(){ 92 | TinySTL::pop_heap(container_.begin(), container_.end(), compare_); 93 | container_.pop_back(); 94 | } 95 | void swap(priority_queue& x){ 96 | TinySTL::swap(container_, x.container_); 97 | TinySTL::swap(compare_, x.compare_); 98 | } 99 | public: 100 | template 101 | friend void swap(priority_queue& x, priority_queue& y); 102 | }; 103 | template 104 | void swap(priority_queue& x, priority_queue& y){ 105 | x.swap(y); 106 | } 107 | } 108 | 109 | #endif -------------------------------------------------------------------------------- /TinySTL/ReverseIterator.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/ReverseIterator.h -------------------------------------------------------------------------------- /TinySTL/ScreenShots/graph1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/ScreenShots/graph1.png -------------------------------------------------------------------------------- /TinySTL/ScreenShots/graph2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/ScreenShots/graph2.png -------------------------------------------------------------------------------- /TinySTL/ScreenShots/graph_bfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/ScreenShots/graph_bfs.png -------------------------------------------------------------------------------- /TinySTL/ScreenShots/graph_dfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/ScreenShots/graph_dfs.png -------------------------------------------------------------------------------- /TinySTL/ScreenShots/suffix_array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/ScreenShots/suffix_array.png -------------------------------------------------------------------------------- /TinySTL/ScreenShots/trie_tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/ScreenShots/trie_tree.png -------------------------------------------------------------------------------- /TinySTL/Stack.h: -------------------------------------------------------------------------------- 1 | #ifndef _STACK_H_ 2 | #define _STACK_H_ 3 | 4 | #include "Vector.h" 5 | 6 | namespace TinySTL{ 7 | //class of stack 8 | template> 9 | class stack{ 10 | public: 11 | typedef typename Container::value_type value_type; 12 | typedef typename Container::reference reference; 13 | typedef typename Container::size_type size_type; 14 | typedef Container container_type; 15 | private: 16 | container_type container_; 17 | public: 18 | explicit stack(const container_type& ctnr = container_type()) :container_(ctnr){} 19 | 20 | bool empty() const{ return container_.empty(); } 21 | size_type size() const{ return container_.size(); } 22 | value_type& top(){ return (container_.back()); } 23 | const value_type& top() const{ return (container_.back()); } 24 | void push(const value_type& val){ container_.push_back(val); } 25 | void pop(){ container_.pop_back(); } 26 | void swap(stack& x){ TinySTL::swap(container_, x.container_); } 27 | public: 28 | template 29 | friend bool operator== (const stack& lhs, const stack& rhs); 30 | template 31 | friend bool operator!= (const stack& lhs, const stack& rhs); 32 | //template 33 | //friend bool operator< (const stack& lhs, const stack& rhs); 34 | //template 35 | //friend bool operator<= (const stack& lhs, const stack& rhs); 36 | //template 37 | //friend bool operator> (const stack& lhs, const stack& rhs); 38 | //template 39 | //friend bool operator>= (const stack& lhs, const stack& rhs); 40 | template 41 | friend void swap(stack& x, stack& y); 42 | }; 43 | template 44 | bool operator== (const stack& lhs, const stack& rhs){ 45 | return lhs.container_ == rhs.container_; 46 | } 47 | template 48 | bool operator!= (const stack& lhs, const stack& rhs){ 49 | return lhs.container_ != rhs.container_; 50 | } 51 | template 52 | void swap(stack& x, stack& y){ 53 | x.swap(y); 54 | } 55 | //template 56 | //bool operator< (const stack& lhs, const stack& rhs){ 57 | // return lhs.container_ < rhs.container_; 58 | //} 59 | //template 60 | //bool operator<= (const stack& lhs, const stack& rhs){ 61 | // return lhs.container_ <= rhs.container_; 62 | //} 63 | //template 64 | //bool operator>(const stack& lhs, const stack& rhs){ 65 | // return lhs.container_ > rhs.container_; 66 | //} 67 | //template 68 | //bool operator>= (const stack& lhs, const stack& rhs){ 69 | // return lhs.container_ >= rhs.container_; 70 | //} 71 | } 72 | #endif -------------------------------------------------------------------------------- /TinySTL/String.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/String.h -------------------------------------------------------------------------------- /TinySTL/SuffixArray.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/SuffixArray.h -------------------------------------------------------------------------------- /TinySTL/Test/AVLTreeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "AVLTreeTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace AVLTreeTest{ 5 | template 6 | bool container_equal(const Container1& con1, const Container2& con2){ 7 | auto first1 = con1.cbegin(), last1 = con1.cend(); 8 | auto first2 = con2.cbegin(), last2 = con2.cend(); 9 | for (; first1 != last1 && first2 != last2; ++first1, ++first2){ 10 | if (*first1 != *first2) 11 | return false; 12 | } 13 | return (first1 == last1 && first2 == last2); 14 | } 15 | 16 | 17 | void testCase1(){ 18 | tsAVL avl; 19 | assert(avl.empty()); 20 | assert(avl.size() == 0); 21 | 22 | avl.insert("0"); 23 | assert(!avl.empty()); 24 | assert(avl.size() == 1); 25 | assert(*avl.root() == "0"); 26 | } 27 | void testCase2(){ 28 | tsAVL avl; 29 | for (auto i = 0; i != 10000; ++i){ 30 | avl.insert(i); 31 | } 32 | assert(avl.height() == 14); 33 | } 34 | void testCase3(){ 35 | tsAVL avl; 36 | for (auto i = 0; i != 10; ++i){ 37 | avl.insert(i); 38 | } 39 | assert(*avl.find(5) == 5); 40 | assert(*avl.find_min() == 0); 41 | assert(*avl.find_max() == 9); 42 | } 43 | void testCase4(){ 44 | tsAVL avl; 45 | std::vector v; 46 | std::random_device rd; 47 | 48 | for (auto i = 0; i != 100; ++i){ 49 | auto r = rd() % 65536; 50 | avl.insert(r); 51 | v.push_back(r); 52 | } 53 | std::sort(v.begin(), v.end()); 54 | v.erase(std::unique(v.begin(), v.end()), v.end()); 55 | assert(container_equal(avl, v)); 56 | 57 | for (auto i = 0; i != 20; ++i){ 58 | avl.erase(*avl.cbegin()); 59 | v.erase(v.begin()); 60 | assert(container_equal(avl, v)); 61 | } 62 | 63 | tsAVL avl1; 64 | avl1.insert(v.begin(), v.end()); 65 | assert(container_equal(avl1, v)); 66 | } 67 | 68 | void testAllCases(){ 69 | testCase1(); 70 | testCase2(); 71 | testCase3(); 72 | testCase4(); 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /TinySTL/Test/AVLTreeTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _AVL_TREE_TEST_H_ 2 | #define _AVL_TREE_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../AVLTree.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace TinySTL{ 15 | namespace AVLTreeTest{ 16 | template 17 | using tsAVL = TinySTL::avl_tree < T > ; 18 | 19 | void testCase1(); 20 | void testCase2(); 21 | void testCase3(); 22 | void testCase4(); 23 | 24 | 25 | void testAllCases(); 26 | } 27 | } 28 | 29 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/AlgorithmTest.cpp: -------------------------------------------------------------------------------- 1 | #include "AlgorithmTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace AlgorithmTest{ 5 | void testFill(){ 6 | std::vector v1(8), v2(8); 7 | std::fill(v1.begin(), v1.begin() + 4, 5); //5 5 5 5 0 0 0 0 8 | std::fill(v1.begin() + 3, v1.end() - 2, 8); //5 5 5 8 8 8 0 0 9 | TinySTL::fill(v2.begin(), v2.begin() + 4, 5); //5 5 5 5 0 0 0 0 10 | TinySTL::fill(v2.begin() + 3, v2.end() - 2, 8); //5 5 5 8 8 8 0 0 11 | 12 | assert(TinySTL::Test::container_equal(v1, v2)); 13 | } 14 | void testFillN(){ 15 | std::vector v1(8, 10), v2(8, 10); 16 | std::fill_n(v1.begin(), 4, 20); //20 20 20 20 10 10 10 10 17 | std::fill_n(v1.begin() + 3, 3, 33); //20 20 20 33 33 33 10 10 18 | TinySTL::fill_n(v2.begin(), 4, 20); //20 20 20 20 10 10 10 10 19 | TinySTL::fill_n(v2.begin() + 3, 3, 33); //20 20 20 33 33 33 10 10 20 | 21 | assert(TinySTL::Test::container_equal(v1, v2)); 22 | } 23 | void testMinMax(){ 24 | assert(TinySTL::min(1, 2) == 1); 25 | assert(TinySTL::min(2, 1) == 1); 26 | assert(TinySTL::min('a', 'z') == 'a'); 27 | assert(TinySTL::min(3.14, 2.72) == 2.72); 28 | 29 | assert(TinySTL::max(1, 2) == 2); 30 | assert(TinySTL::max(2, 1) == 2); 31 | assert(TinySTL::max('a', 'z') == 'z'); 32 | assert(TinySTL::max(3.14, 2.73) == 3.14); 33 | } 34 | void testHeapAlgorithm(){ 35 | int myints[] = { 10, 20, 30, 5, 15 }; 36 | std::vector v1(myints, myints + 5); 37 | std::vector v2(myints, myints + 5); 38 | 39 | std::make_heap(v1.begin(), v1.end()); 40 | TinySTL::make_heap(v2.begin(), v2.end()); 41 | assert(TinySTL::Test::container_equal(v1, v2)); 42 | 43 | std::pop_heap(v1.begin(), v1.end()); v1.pop_back(); 44 | TinySTL::pop_heap(v2.begin(), v2.end()); v2.pop_back(); 45 | assert(TinySTL::Test::container_equal(v1, v2)); 46 | 47 | v1.push_back(99); std::push_heap(v1.begin(), v1.end()); 48 | v2.push_back(99); TinySTL::push_heap(v2.begin(), v2.end()); 49 | assert(TinySTL::Test::container_equal(v1, v2)); 50 | 51 | std::sort_heap(v1.begin(), v1.end()); 52 | TinySTL::sort_heap(v2.begin(), v2.end()); 53 | assert(TinySTL::Test::container_equal(v1, v2)); 54 | } 55 | void testIsHeap(){ 56 | std::vector v1{ 9, 5, 2, 6, 4, 1, 3, 8, 7 }; 57 | std::vector v2{ 9, 5, 2, 6, 4, 1, 3, 8, 7 }; 58 | 59 | if (!std::is_heap(v1.begin(), v1.end())) 60 | std::make_heap(v1.begin(), v1.end()); 61 | if (!TinySTL::is_heap(v2.begin(), v2.end())) 62 | TinySTL::make_heap(v2.begin(), v2.end()); 63 | 64 | assert(TinySTL::Test::container_equal(v1, v2)); 65 | } 66 | void testAllOf(){ 67 | std::array foo = { 3, 5, 7, 11, 13, 17, 19, 23 }; 68 | assert(TinySTL::all_of(foo.begin(), foo.end(), [](int i){return i % 2; })); 69 | } 70 | void testNoneOf(){ 71 | std::array foo = { 1, 2, 4, 8, 16, 32, 64, 128 }; 72 | assert(TinySTL::none_of(foo.begin(), foo.end(), [](int i){return i < 0; })); 73 | } 74 | void testAnyOf(){ 75 | std::array foo = { 0, 1, -1, 3, -3, 5, -5 }; 76 | assert(std::any_of(foo.begin(), foo.end(), [](int i){return i < 0; })); 77 | } 78 | void testForEach(){ 79 | std::vector myvector{ 10, 20, 30 }; 80 | std::vector temp{ 11, 21, 31 }; 81 | TinySTL::for_each(myvector.begin(), myvector.end(), [&myvector](int& i){ 82 | ++i; 83 | }); 84 | 85 | assert(TinySTL::Test::container_equal(myvector, temp)); 86 | } 87 | void testFind(){ 88 | std::vector v{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 89 | assert(TinySTL::find(v.begin(), v.end(), 5) != v.end()); 90 | assert(TinySTL::find(v.begin(), v.end(), 10) == v.end()); 91 | 92 | assert(TinySTL::find_if(v.begin(), v.end(), [](int i){return i < 0; }) == v.end()); 93 | assert(TinySTL::find_if_not(v.begin(), v.end(), [](int i){return i < 0; }) != v.end()); 94 | } 95 | void testFindEnd(){ 96 | int myints[] = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 }; 97 | std::vector v(myints, myints + 10); 98 | int needle1[] = { 1, 2, 3 }; 99 | auto it = TinySTL::find_end(v.begin(), v.end(), needle1, needle1 + 3); 100 | assert(it == v.begin() + 5); 101 | 102 | int needle2[] = { 4, 5, 1 }; 103 | it = TinySTL::find_end(v.begin(), v.end(), needle2, needle2 + 3, [](int i, int j){return i == j; }); 104 | assert(it == v.begin() + 3); 105 | } 106 | void testFindFirstOf(){ 107 | int mychars[] = { 'a', 'b', 'c', 'A', 'B', 'C' }; 108 | std::vector v(mychars, mychars + 6); 109 | int needle[] = { 'A', 'B', 'C' }; 110 | auto it = TinySTL::find_first_of(v.begin(), v.end(), needle, needle + 3); 111 | assert(*it == 'A'); 112 | 113 | it = TinySTL::find_first_of(v.begin(), v.end(), needle, needle + 3, 114 | [](char ch1, char ch2){return std::tolower(ch1) == std::tolower(ch2); }); 115 | assert(*it == 'a'); 116 | } 117 | void testAdjacentFind(){ 118 | int myints[] = { 5, 20, 5, 30, 30, 20, 10, 10, 20 }; 119 | std::vector v(myints, myints + 8); 120 | auto it = TinySTL::adjacent_find(v.begin(), v.end()); 121 | assert(*it == 30); 122 | 123 | it = TinySTL::adjacent_find(++it, v.end(), [](int i, int j){return i == j; }); 124 | assert(*it == 10); 125 | } 126 | void testCount(){ 127 | int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; // 8 elements 128 | int mycount = TinySTL::count(myints, myints + 8, 10); 129 | assert(mycount == 3); 130 | 131 | mycount = TinySTL::count_if(myints, myints + 8, [](int i){return i % 2 == 0; }); 132 | assert(mycount == 8); 133 | } 134 | void testMismatch(){ 135 | std::vector v; 136 | for (int i = 1; i<6; i++) v.push_back(i * 10); //10 20 30 40 50 137 | int myints[] = { 10, 20, 80, 320, 1024 }; 138 | TinySTL::pair::iterator, int*> mypair; 139 | mypair = TinySTL::mismatch(v.begin(), v.end(), myints); 140 | assert(*mypair.first == 30 && *mypair.second == 80); 141 | 142 | ++mypair.first; ++mypair.second; 143 | mypair = TinySTL::mismatch(mypair.first, v.end(), mypair.second, [](int i, int j){return i == j; }); 144 | } 145 | void testEqual(){ 146 | int myints[] = { 20, 40, 60, 80, 100 }; 147 | std::vectorv(myints, myints + 5); //20 40 60 80 100 148 | assert(TinySTL::equal(v.begin(), v.end(), myints)); 149 | 150 | v[3] = 81; 151 | assert(!TinySTL::equal(v.begin(), v.end(), myints, [](int i, int j){return i == j; })); 152 | } 153 | void testIsPermutation(){ 154 | std::array foo = { 1, 2, 3, 4, 5 }; 155 | std::array bar = { 3, 1, 4, 5, 2 }; 156 | 157 | assert(TinySTL::is_permutation(foo.begin(), foo.end(), bar.begin())); 158 | } 159 | void testSearch(){ 160 | std::vector v; 161 | for (int i = 1; i<10; i++) v.push_back(i * 10); 162 | int needle1[] = { 40, 50, 60, 70 }; 163 | auto it = TinySTL::search(v.begin(), v.end(), needle1, needle1 + 4); 164 | assert(it == v.begin() + 3); 165 | 166 | int needle2[] = { 20, 30, 50 }; 167 | it = std::search(v.begin(), v.end(), needle2, needle2 + 3, [](int i, int j){return i == j; }); 168 | assert(it == v.end()); 169 | } 170 | void testAdvance(){ 171 | TinySTL::vector v; 172 | TinySTL::list l; 173 | TinySTL::binary_search_tree bst; 174 | for (auto i = 0; i != 10; ++i){ 175 | v.push_back(i); 176 | l.push_back(i); 177 | bst.insert(i); 178 | } 179 | auto vit = v.begin(); 180 | auto lit = l.begin(); 181 | auto bit = bst.cbegin(); 182 | 183 | TinySTL::advance(vit, 5); 184 | TinySTL::advance(lit, 5); 185 | TinySTL::advance(bit, 5); 186 | assert(*vit == 5 && *lit == 5 && *bit == 5); 187 | 188 | TinySTL::advance(vit, -5); 189 | TinySTL::advance(lit, -5); 190 | assert(*vit == 0 && *lit == 0); 191 | } 192 | void testSort(){ 193 | int arr1[1] = { 0 }; 194 | TinySTL::sort(std::begin(arr1), std::end(arr1)); 195 | assert(std::is_sorted(std::begin(arr1), std::end(arr1))); 196 | 197 | int arr2[2] = { 1, 0 }; 198 | TinySTL::sort(std::begin(arr2), std::end(arr2)); 199 | assert(std::is_sorted(std::begin(arr2), std::end(arr2))); 200 | 201 | int arr3[3] = { 2, 1, 3 }; 202 | TinySTL::sort(std::begin(arr3), std::end(arr3)); 203 | assert(std::is_sorted(std::begin(arr3), std::end(arr3))); 204 | 205 | int arr4[100]; 206 | std::random_device rd; 207 | for (auto i = 0; i != 10; ++i){ 208 | for (auto& n : arr4){ 209 | n = rd() % 65536; 210 | } 211 | TinySTL::sort(std::begin(arr4), std::end(arr4)); 212 | assert(std::is_sorted(std::begin(arr4), std::end(arr4))); 213 | } 214 | } 215 | void testGenerate(){ 216 | int arr1[100], arr2[100]; 217 | auto f = [](int i){ return i; }; 218 | for (auto i = 0; i != 100; ++i){ 219 | auto func = std::bind(f, i); 220 | TinySTL::generate(std::begin(arr1), std::end(arr1), func); 221 | std::generate(std::begin(arr2), std::end(arr2), func); 222 | } 223 | assert(TinySTL::Test::container_equal(arr1, arr2)); 224 | 225 | int n1 = 0, n2 = 0; 226 | auto gen1 = [&n1](){return n1++; }; 227 | auto gen2 = [&n2](){return n2++; }; 228 | int arr3[100], arr4[100]; 229 | TinySTL::generate_n(arr3, 100, gen1); 230 | std::generate_n(arr4, 100, gen2); 231 | assert(TinySTL::Test::container_equal(arr3, arr4)); 232 | } 233 | void testDistance(){ 234 | TinySTL::list l(10, 0); 235 | TinySTL::vector v(10, 0); 236 | 237 | auto lit = l.begin(); 238 | TinySTL::advance(lit, 5); 239 | auto vit = v.begin(); 240 | TinySTL::advance(vit, 5); 241 | 242 | assert(TinySTL::distance(l.begin(), lit) == 5); 243 | assert(TinySTL::distance(v.begin(), vit) == 5); 244 | } 245 | void testCopy(){ 246 | char arr1[] = "hello", res1[6] = { 0 }; 247 | TinySTL::copy(std::begin(arr1), std::end(arr1), res1); 248 | assert(TinySTL::Test::container_equal(arr1, res1)); 249 | 250 | wchar_t arr2[] = L"hello", res2[6] = { 0 }; 251 | TinySTL::copy(std::begin(arr2), std::end(arr2), res2); 252 | assert(TinySTL::Test::container_equal(arr2, res2)); 253 | 254 | int arr3[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, res3[10] = { 0 }; 255 | TinySTL::copy(std::begin(arr3), std::end(arr3), res3); 256 | assert(TinySTL::Test::container_equal(arr3, res3)); 257 | 258 | std::string arr4[3] = { "1", "2", "3" }, res4[3]; 259 | TinySTL::copy(std::begin(arr4), std::end(arr4), res4); 260 | assert(TinySTL::Test::container_equal(arr4, res4)); 261 | } 262 | 263 | void testAllCases(){ 264 | testFill(); 265 | testFillN(); 266 | testMinMax(); 267 | testHeapAlgorithm(); 268 | testIsHeap(); 269 | testAllOf(); 270 | testNoneOf(); 271 | testAnyOf(); 272 | testForEach(); 273 | testFind(); 274 | testFindEnd(); 275 | testFindFirstOf(); 276 | testAdjacentFind(); 277 | testCount(); 278 | testMismatch(); 279 | testEqual(); 280 | testIsPermutation(); 281 | testSearch(); 282 | testAdvance(); 283 | testSort(); 284 | testGenerate(); 285 | testDistance(); 286 | testCopy(); 287 | } 288 | } 289 | } -------------------------------------------------------------------------------- /TinySTL/Test/AlgorithmTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _ALGORITHM_TEST_H_ 2 | #define _ALGORITHM_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../Algorithm.h" 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "../BinarySearchTree.h" 19 | #include "../List.h" 20 | #include "../Vector.h" 21 | 22 | 23 | namespace TinySTL{ 24 | namespace AlgorithmTest{ 25 | void testFill(); 26 | void testFillN(); 27 | void testMinMax(); 28 | void testHeapAlgorithm(); 29 | void testIsHeap(); 30 | void testAllOf(); 31 | void testNoneOf(); 32 | void testAnyOf(); 33 | void testForEach(); 34 | void testFind(); 35 | void testFindEnd(); 36 | void testFindFirstOf(); 37 | void testAdjacentFind(); 38 | void testCount(); 39 | void testMismatch(); 40 | void testEqual(); 41 | void testIsPermutation(); 42 | void testSearch(); 43 | void testAdvance(); 44 | void testSort(); 45 | void testGenerate(); 46 | void testDistance(); 47 | void testCopy(); 48 | 49 | void testAllCases(); 50 | } 51 | } 52 | 53 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/BinarySearchTreeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "BinarySearchTreeTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace BinarySearchTreeTest{ 5 | template 6 | bool container_equal(const Container1& con1, const Container2& con2){ 7 | auto first1 = con1.cbegin(), last1 = con1.cend(); 8 | auto first2 = con2.cbegin(), last2 = con2.cend(); 9 | for (; first1 != last1 && first2 != last2; ++first1, ++first2){ 10 | if (*first1 != *first2) 11 | return false; 12 | } 13 | return (first1 == last1 && first2 == last2); 14 | } 15 | 16 | 17 | void testCase1(){ 18 | tsBst bst; 19 | 20 | assert(bst.empty()); 21 | assert(bst.size() == 0); 22 | 23 | bst.insert("1"); 24 | assert(!bst.empty()); 25 | assert(bst.size() == 1); 26 | } 27 | void testCase2(){ 28 | tsBst bst; 29 | 30 | for (auto i = 0; i != 100; ++i) 31 | bst.insert(i); 32 | assert(bst.height() == 100); 33 | } 34 | void testCase3(){ 35 | tsBst bst; 36 | std::vector v; 37 | std::random_device rd; 38 | 39 | for (auto i = 0; i != 100; ++i){ 40 | auto r = rd() % 65536; 41 | bst.insert(r); 42 | v.push_back(r); 43 | } 44 | std::sort(v.begin(), v.end()); 45 | v.erase(std::unique(v.begin(), v.end()), v.end()); 46 | assert(container_equal(bst, v)); 47 | 48 | for (auto i = 0; i != 20; ++i){ 49 | bst.erase(*bst.cbegin()); 50 | v.erase(v.begin()); 51 | assert(container_equal(bst, v)); 52 | } 53 | 54 | tsBst bst1; 55 | bst1.insert(v.begin(), v.end()); 56 | assert(container_equal(bst1, v)); 57 | } 58 | void testCase4(){ 59 | tsBst bst; 60 | for (auto i = 0; i != 10; ++i) 61 | bst.insert(i); 62 | 63 | assert(*bst.find(5) == 5); 64 | assert(*bst.find_min() == 0); 65 | assert(*bst.find_max() == 9); 66 | } 67 | 68 | 69 | void testAllCases(){ 70 | testCase1(); 71 | testCase2(); 72 | testCase3(); 73 | testCase4(); 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /TinySTL/Test/BinarySearchTreeTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _BINARY_SEARCH_TREE_TEST_H_ 2 | #define _BINARY_SEARCH_TREE_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../BinarySearchTree.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace TinySTL{ 15 | namespace BinarySearchTreeTest{ 16 | template 17 | using tsBst = TinySTL::binary_search_tree < T > ; 18 | 19 | void testCase1(); 20 | void testCase2(); 21 | void testCase3(); 22 | void testCase4(); 23 | 24 | void testAllCases(); 25 | } 26 | } 27 | 28 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/BitmapTest.cpp: -------------------------------------------------------------------------------- 1 | #include "BitmapTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace BitmapTest{ 5 | void testCase1(){ 6 | bitmap<1> bt1; 7 | assert(bt1.size() == 8); 8 | 9 | bitmap<7> bt2; 10 | assert(bt2.size() == 8); 11 | 12 | bitmap<127> bt3; 13 | assert(bt3.size() == 128); 14 | } 15 | void testCase2(){ 16 | bitmap<8> bt1, bt2; 17 | bt1.set(); 18 | assert(bt1.to_string() == "11111111"); 19 | bt1.reset(); 20 | assert(bt1.to_string() == "00000000"); 21 | 22 | bt2.set(0); bt2.set(2); bt2.set(4); 23 | assert(bt2.to_string() == "10101000"); 24 | bt2.reset(0); bt2.reset(2); bt2.reset(4); 25 | assert(bt2.to_string() == "00000000"); 26 | } 27 | void testCase3(){ 28 | bitmap<8> bt; 29 | bt.flip(); 30 | assert(bt.to_string() == "11111111"); 31 | 32 | bt.flip(0); 33 | assert(bt.to_string() == "01111111"); 34 | } 35 | void testCase4(){ 36 | bitmap<8> bt; 37 | bt.set(); 38 | assert(bt.count() == 8); 39 | 40 | bt.flip(); 41 | assert(bt.count() == 0); 42 | 43 | bt.set(0); 44 | assert(bt.count() == 1); 45 | } 46 | void testCase5(){ 47 | bitmap<8> bt; 48 | assert(!bt.test(0)); 49 | 50 | bt.set(0); 51 | assert(bt.test(0)); 52 | } 53 | void testCase6(){ 54 | bitmap<8> bt; 55 | assert(!bt.any()); 56 | assert(bt.none()); 57 | assert(!bt.all()); 58 | 59 | bt.set(0); 60 | assert(bt.any()); 61 | assert(!bt.none()); 62 | assert(!bt.all()); 63 | 64 | bt.set(); 65 | assert(bt.any()); 66 | assert(!bt.none()); 67 | assert(bt.all()); 68 | 69 | bt.reset(); 70 | assert(!bt.any()); 71 | assert(bt.none()); 72 | assert(!bt.all()); 73 | } 74 | void testCase7(){ 75 | bitmap<8> bt; 76 | bt.set(0); bt.set(2); bt.set(4); bt.set(6); 77 | assert(bt.to_string() == TinySTL::string("10101010")); 78 | } 79 | 80 | 81 | void testAllCases(){ 82 | testCase1(); 83 | testCase2(); 84 | testCase3(); 85 | testCase4(); 86 | testCase5(); 87 | testCase6(); 88 | testCase7(); 89 | } 90 | } 91 | } -------------------------------------------------------------------------------- /TinySTL/Test/BitmapTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _BITMAP_TEST_H_ 2 | #define _BITMAP_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../Bitmap.h" 7 | 8 | #include 9 | #include 10 | 11 | namespace TinySTL{ 12 | namespace BitmapTest{ 13 | using std::cout; 14 | using std::endl; 15 | 16 | void testCase1(); 17 | void testCase2(); 18 | void testCase3(); 19 | void testCase4(); 20 | void testCase5(); 21 | void testCase6(); 22 | void testCase7(); 23 | 24 | void testAllCases(); 25 | } 26 | } 27 | 28 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/COWPtrTest.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Test/COWPtrTest.cpp -------------------------------------------------------------------------------- /TinySTL/Test/COWPtrTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _COWPTR_TEST_H_ 2 | #define _COWPTR_TEST_H_ 3 | 4 | #include "../COWPtr.h" 5 | 6 | #include 7 | 8 | namespace TinySTL{ 9 | namespace COWPtrTest{ 10 | void testCase1(); 11 | 12 | void testAllCases(); 13 | } 14 | } 15 | 16 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/CircularBufferTest.cpp: -------------------------------------------------------------------------------- 1 | #include "CircularBufferTest.h" 2 | 3 | #include 4 | 5 | namespace TinySTL{ 6 | namespace CircularBufferTest{ 7 | void testCase1(){ 8 | tsCB cb1(10, 1); 9 | for (auto i = 0; i != 10; ++i) 10 | assert(cb1[i] == 1); 11 | 12 | int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 13 | tsCB cb2(std::begin(arr), std::end(arr)); 14 | for (auto i = 0; i != 10; ++i) 15 | assert(cb2[i] == i); 16 | 17 | auto cb3(cb2); 18 | assert(circular_buffer_equal(cb2, cb3)); 19 | 20 | auto cb4(std::move(cb2));//cb2 clear 21 | assert(circular_buffer_equal(cb3, cb4)); 22 | 23 | auto cb5 = cb3; 24 | assert(circular_buffer_equal(cb3, cb5)); 25 | 26 | auto cb6 = std::move(cb3);//cb3 clear 27 | assert(circular_buffer_equal(cb5, cb6)); 28 | } 29 | void testCase2(){ 30 | tsCB cb(1); 31 | assert(cb.size() == 1); 32 | cb.pop_front(); 33 | assert(!cb.full()); 34 | assert(cb.size() == 0); 35 | assert(cb.empty()); 36 | 37 | cb.push_back(1), cb.push_back(2); 38 | assert(cb.full()); 39 | assert(!cb.empty()); 40 | assert(cb.size() == 2); 41 | } 42 | void testCase3(){ 43 | tsCB cb(3); 44 | cb[0] = "one", cb[1] = "two", cb[2] = "three"; 45 | 46 | assert(*(cb.first()) == "one" && *(cb.last()) == "three"); 47 | 48 | assert(cb.front() == "one" && cb.back() == "three"); 49 | } 50 | void testCase4(){ 51 | tsCB cb(1); 52 | assert(cb.front() == std::string()); 53 | 54 | cb.push_back("zxh"); cb.push_back("jwl"); 55 | assert(cb.back() == "jwl"); 56 | cb.pop_front(); 57 | assert(cb.front() == "zxh"); 58 | } 59 | void testCase5(){ 60 | tsCB cb1(3), cb2(3); 61 | assert(cb1 == cb2); 62 | assert(!(cb1 != cb2)); 63 | 64 | cb1[0] = -1; 65 | assert(!(cb1 == cb2)); 66 | assert(cb1 != cb2); 67 | } 68 | void testCase6(){ 69 | std::string arr[] = { "1", "2", "3" }; 70 | tsCB cb(std::begin(arr), std::end(arr)); 71 | 72 | std::ostringstream os; 73 | os << cb; 74 | assert(os.str() == "(1, 2, 3)"); 75 | } 76 | 77 | 78 | void testAllCases(){ 79 | testCase1(); 80 | testCase2(); 81 | testCase3(); 82 | testCase4(); 83 | testCase5(); 84 | testCase6(); 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /TinySTL/Test/CircularBufferTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _CIRCULAR_BUFFER_TEST_H_ 2 | #define _CIRCULAR_BUFFER_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../CircularBuffer.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | namespace TinySTL{ 13 | namespace CircularBufferTest{ 14 | template 15 | using tsCB = TinySTL::circular_buffer; 16 | 17 | template 18 | bool circular_buffer_equal(tsCB& cb1, tsCB cb2){ 19 | auto it1 = cb1.first(), it2 = cb2.first(); 20 | for (; it1 != cb1.last() && it2 != cb1.last(); ++it1, ++it2){ 21 | if (*it1 != *it2) 22 | return false; 23 | } 24 | return (it1 == cb1.last() && it2 == cb2.last() && (*(cb1.last()) == *(cb2.last()))); 25 | } 26 | 27 | void testCase1(); 28 | void testCase2(); 29 | void testCase3(); 30 | void testCase4(); 31 | void testCase5(); 32 | void testCase6(); 33 | 34 | void testAllCases(); 35 | } 36 | } 37 | 38 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/DequeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "DequeTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace DequeTest{ 5 | void testCase1(){ 6 | stdDQ dq1(10, 0); 7 | tsDQ dq2(10, 0); 8 | assert(TinySTL::Test::container_equal(dq1, dq2)); 9 | 10 | int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 11 | stdDQ dq3(std::begin(arr), std::end(arr)); 12 | tsDQ dq4(std::begin(arr), std::end(arr)); 13 | assert(TinySTL::Test::container_equal(dq3, dq4)); 14 | 15 | auto dq5(dq1); 16 | auto dq6(dq2); 17 | assert(TinySTL::Test::container_equal(dq5, dq6)); 18 | 19 | auto dq7 = dq3; 20 | auto dq8 = dq4; 21 | assert(TinySTL::Test::container_equal(dq7, dq8)); 22 | 23 | auto dq9 = std::move(dq7); 24 | auto dq10 = std::move(dq8); 25 | assert(TinySTL::Test::container_equal(dq9, dq10)); 26 | } 27 | void testCase2(){ 28 | tsDQ dq1; 29 | assert(dq1.empty()); 30 | assert(dq1.size() == 0); 31 | 32 | tsDQ dq2(10, 0); 33 | assert(!dq2.empty()); 34 | assert(dq2.size() == 10); 35 | } 36 | void testCase3(){ 37 | stdDQ dq1(10, "10"); 38 | tsDQ dq2(10, "10"); 39 | 40 | dq1[0] = "0"; dq1[9] = "9"; 41 | dq2[0] = "0"; dq2[9] = "9"; 42 | 43 | assert(dq1.front() == dq2.front()); 44 | assert(dq1.back() == dq2.back()); 45 | } 46 | void testCase4(){ 47 | stdDQ dq1; 48 | tsDQ dq2; 49 | 50 | for (auto i = 0; i != 10; ++i){ 51 | dq1.push_back(i); 52 | dq2.push_back(i); 53 | } 54 | assert(TinySTL::Test::container_equal(dq1, dq2)); 55 | 56 | for (auto i = 10; i != 20; ++i){ 57 | dq1.push_front(i); 58 | dq2.push_front(i); 59 | } 60 | assert(TinySTL::Test::container_equal(dq1, dq2)); 61 | 62 | for (auto i = 0; i != 5; ++i){ 63 | dq1.pop_back(); 64 | dq2.pop_back(); 65 | } 66 | assert(TinySTL::Test::container_equal(dq1, dq2)); 67 | 68 | for (auto i = 0; i != 5; ++i){ 69 | dq1.pop_front(); 70 | dq2.pop_front(); 71 | } 72 | assert(TinySTL::Test::container_equal(dq1, dq2)); 73 | } 74 | void testCase5(){ 75 | int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 76 | tsDQ foo(arr, arr + 3), bar(arr + 3, arr + 10); 77 | 78 | assert(foo.size() == 3 && bar.size() == 7); 79 | foo.swap(bar); 80 | assert(foo.size() == 7 && bar.size() == 3); 81 | TinySTL::swap(foo, bar); 82 | assert(foo.size() == 3 && bar.size() == 7); 83 | } 84 | void testCase6(){ 85 | int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 86 | tsDQ foo1(arr, arr + 3), bar(arr + 3, arr + 10); 87 | 88 | assert(foo1 != bar); 89 | auto foo2 = bar; 90 | assert(foo2 == bar); 91 | } 92 | 93 | 94 | void testAllCases(){ 95 | testCase1(); 96 | testCase2(); 97 | testCase3(); 98 | testCase4(); 99 | testCase5(); 100 | testCase6(); 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /TinySTL/Test/DequeTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _DEQUE_TEST_H_ 2 | #define _DEQUE_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../Deque.h" 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | namespace TinySTL{ 13 | namespace DequeTest{ 14 | template 15 | using stdDQ = std::deque < T > ; 16 | template 17 | using tsDQ = TinySTL::deque < T > ; 18 | 19 | void testCase1(); 20 | void testCase2(); 21 | void testCase3(); 22 | void testCase4(); 23 | void testCase5(); 24 | void testCase6(); 25 | 26 | void testAllCases(); 27 | } 28 | } 29 | 30 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/GraphTest.cpp: -------------------------------------------------------------------------------- 1 | #include "GraphTest.h" 2 | 3 | #include 4 | #include 5 | 6 | namespace TinySTL{ 7 | namespace GraphTest{ 8 | void testCase1(){ 9 | dGraph g; 10 | assert(g.empty()); 11 | assert(g.size() == 0); 12 | 13 | g.add_node(g.make_node(0, 0), g.empty_node_set()); 14 | assert(!g.empty()); 15 | assert(g.size() == 1); 16 | assert(g.is_contained(0)); 17 | 18 | auto node = g.get_node(0); 19 | assert(node.first == 0 && node.second == 0); 20 | } 21 | void testCase2(){ 22 | dGraph g; 23 | dGraph::nodes_set_type set1, set2, set3; 24 | set1.push_back(g.make_node(1, 11)); 25 | set1.push_back(g.make_node(2, 22)); 26 | set1.push_back(g.make_node(3, 33)); 27 | g.add_node(g.make_node(0, 0), set1); 28 | 29 | set2.push_back(g.make_node(5, 55)); 30 | set2.push_back(g.make_node(6, 66)); 31 | set2.push_back(g.make_node(7, 77)); 32 | g.add_node(g.make_node(1, 11), set2); 33 | 34 | set3.push_back(g.make_node(12, 1212)); 35 | set3.push_back(g.make_node(13, 1313)); 36 | set3.push_back(g.make_node(14, 1414)); 37 | g.add_node(7, set3); 38 | 39 | g.make_edge(12, 2); 40 | g.make_edge(12, 3); 41 | g.make_edge(12, 0); 42 | std::ostringstream os; 43 | os << g.to_string(); 44 | std::string res(R"([14,1414]:[nil] 45 | | 46 | [13,1313]:[nil] 47 | | 48 | [12,1212]:[0,0]->[3,33]->[2,22]->[nil] 49 | | 50 | [7,77]:[14,1414]->[13,1313]->[12,1212]->[nil] 51 | | 52 | [6,66]:[nil] 53 | | 54 | [5,55]:[nil] 55 | | 56 | [3,33]:[nil] 57 | | 58 | [2,22]:[nil] 59 | | 60 | [1,11]:[7,77]->[6,66]->[5,55]->[nil] 61 | | 62 | [0,0]:[3,33]->[2,22]->[1,11]->[nil] 63 | | 64 | [nil] 65 | )"); 66 | assert(os.str() == res); 67 | 68 | std::ostringstream os1, os2; 69 | auto func1 = [&os1](const dGraph::node_type& node){ 70 | os1 << "[" << node.first << "," << node.second << "]"; 71 | }; 72 | auto func2 = [&os2](const dGraph::node_type& node){ 73 | os2 << "[" << node.first << "," << node.second << "]"; 74 | }; 75 | res = R"([1,11][7,77][14,1414][13,1313][12,1212][0,0][3,33][2,22][6,66][5,55])"; 76 | g.DFS(1, func1); 77 | assert(os1.str() == res); 78 | 79 | res = R"([1,11][7,77][6,66][5,55][14,1414][13,1313][12,1212][0,0][3,33][2,22])"; 80 | g.BFS(1, func2); 81 | assert(os2.str() == res); 82 | 83 | std::ostringstream os3; 84 | g.delete_node(dGraph::node_type(7, 77)); 85 | os3 << g.to_string(); 86 | res = R"([14,1414]:[nil] 87 | | 88 | [13,1313]:[nil] 89 | | 90 | [12,1212]:[0,0]->[3,33]->[2,22]->[nil] 91 | | 92 | [6,66]:[nil] 93 | | 94 | [5,55]:[nil] 95 | | 96 | [3,33]:[nil] 97 | | 98 | [2,22]:[nil] 99 | | 100 | [1,11]:[6,66]->[5,55]->[nil] 101 | | 102 | [0,0]:[3,33]->[2,22]->[1,11]->[nil] 103 | | 104 | [nil] 105 | )"; 106 | assert(os3.str() == res); 107 | } 108 | void testCase3(){ 109 | dGraph g; 110 | dGraph::nodes_set_type set1, set2; 111 | set1.push_back(g.make_node(1, 11)); 112 | set1.push_back(g.make_node(2, 22)); 113 | set1.push_back(g.make_node(3, 33)); 114 | g.add_node(g.make_node(0, 0), set1); 115 | 116 | auto nodes = g.adjacent_nodes(0); 117 | std::reverse(set1.begin(), set1.end()); 118 | assert(TinySTL::Test::container_equal(nodes, set1)); 119 | 120 | for (auto it = g.begin(0); it != g.end(0); ++it){ 121 | set2.push_back(*it); 122 | } 123 | assert(TinySTL::Test::container_equal(nodes, set2)); 124 | } 125 | void testCase4(){ 126 | dGraph g; 127 | dGraph::nodes_set_type set; 128 | for (auto i = 0; i != 10; ++i){ 129 | auto node = g.make_node(i, i); 130 | g.add_node(node, g.empty_node_set()); 131 | set.push_back(node); 132 | } 133 | std::reverse(set.begin(), set.end()); 134 | auto it2 = set.begin(); 135 | for (auto it1 = g.begin(); it1 != g.end() && it2 != set.end(); ++it1, ++it2){ 136 | assert(*it1 == *it2); 137 | } 138 | } 139 | 140 | void testAllCases(){ 141 | testCase1(); 142 | testCase2(); 143 | testCase3(); 144 | testCase4(); 145 | } 146 | } 147 | } -------------------------------------------------------------------------------- /TinySTL/Test/GraphTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _GRAPH_TEST_H_ 2 | #define _GRAPH_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../Graph.h" 7 | 8 | #include 9 | 10 | namespace TinySTL{ 11 | namespace GraphTest{ 12 | template 13 | using dGraph = TinySTL::directed_graph < Index, Value > ; 14 | 15 | void testCase1(); 16 | void testCase2(); 17 | void testCase3(); 18 | void testCase4(); 19 | 20 | void testAllCases(); 21 | } 22 | } 23 | 24 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/ListTest.cpp: -------------------------------------------------------------------------------- 1 | #include "ListTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace ListTest{ 5 | void testCase1(){ 6 | stdL l1(10, 0); 7 | tsL l2(10, 0); 8 | assert(TinySTL::Test::container_equal(l1, l2)); 9 | 10 | int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 11 | stdL l3(std::begin(arr), std::end(arr)); 12 | tsL l4(std::begin(arr), std::end(arr)); 13 | assert(TinySTL::Test::container_equal(l3, l4)); 14 | 15 | auto l5(l1); 16 | auto l6(l2); 17 | assert(TinySTL::Test::container_equal(l5, l6)); 18 | 19 | auto l7 = l1; 20 | auto l8 = l2; 21 | assert(TinySTL::Test::container_equal(l7, l8)); 22 | } 23 | void testCase2(){ 24 | tsL l1; 25 | assert(l1.empty()); 26 | assert(l1.size() == 0); 27 | 28 | std::string arr[] = { "1", "2", "3" }; 29 | tsL l2(std::begin(arr), std::end(arr)); 30 | assert(!l2.empty()); 31 | assert(l2.size() == 3); 32 | } 33 | void testCase3(){ 34 | std::string arr[] = { "1", "2", "3" }; 35 | tsL l(std::begin(arr), std::end(arr)); 36 | assert(l.back() == arr[2]); 37 | assert(l.front() == arr[0]); 38 | 39 | l.front() = "front"; 40 | l.back() = "back"; 41 | assert(l.back() == "back"); 42 | assert(l.front() == "front"); 43 | } 44 | void testCase4(){ 45 | stdL l1; 46 | tsL l2; 47 | for (auto i = 0; i != 10; ++i){ 48 | l1.push_front(i); 49 | l2.push_front(i); 50 | } 51 | assert(TinySTL::Test::container_equal(l1, l2)); 52 | for (auto i = 0; i != 10; ++i){ 53 | l1.push_back(i); 54 | l2.push_back(i); 55 | } 56 | assert(TinySTL::Test::container_equal(l1, l2)); 57 | for (auto i = 0; i != 5; ++i){ 58 | l1.pop_back(); 59 | l2.pop_back(); 60 | } 61 | assert(TinySTL::Test::container_equal(l1, l2)); 62 | for (auto i = 0; i != 5; ++i){ 63 | l1.pop_front(); 64 | l2.pop_front(); 65 | } 66 | assert(TinySTL::Test::container_equal(l1, l2)); 67 | } 68 | void testCase5(){ 69 | stdL l1; 70 | tsL l2; 71 | 72 | for (auto i = 0; i != 10; ++i){ 73 | l1.push_back(i); 74 | l2.push_back(i); 75 | } 76 | auto rit1 = l1.rbegin(); 77 | auto rit2 = l2.rbegin(); 78 | for (; rit1 != l1.rend() && rit2 != l2.rend(); ++rit1, ++rit2){ 79 | assert(*rit1 == *rit2); 80 | } 81 | assert(rit1 == l1.rend() && rit2 == l2.rend()); 82 | } 83 | void testCase6(){ 84 | stdL l1; 85 | tsL l2; 86 | 87 | l1.insert(l1.end(), 10, -1); 88 | l2.insert(l2.end(), 10, -1); 89 | assert(TinySTL::Test::container_equal(l1, l2)); 90 | 91 | auto it1 = l1.begin(); 92 | auto it2 = l2.begin(); 93 | for (auto i = l1.size() / 2 + 1; i != 0; --i){ 94 | ++it1; 95 | ++it2; 96 | } 97 | l1.insert(it1, 1); 98 | l2.insert(it2, 1); 99 | assert(TinySTL::Test::container_equal(l1, l2)); 100 | 101 | int arr[] = { 1, 2, 3, 4, 5 }; 102 | it1 = l1.end(); 103 | it2 = l2.end(); 104 | l1.insert(it1, std::begin(arr), std::end(arr)); 105 | l2.insert(it2, std::begin(arr), std::end(arr)); 106 | assert(TinySTL::Test::container_equal(l1, l2)); 107 | } 108 | void testCase7(){ 109 | stdL l1; 110 | tsL l2; 111 | for (auto i = 0; i != 100; ++i){ 112 | l1.push_back(i); 113 | l2.push_back(i); 114 | } 115 | 116 | l1.erase(l1.begin()); l1.erase(--l1.end()); 117 | l2.erase(l2.begin()); l2.erase(--l2.end()); 118 | assert(TinySTL::Test::container_equal(l1, l2)); 119 | 120 | l1.erase(l1.begin(), l1.end()); 121 | l2.erase(l2.begin(), l2.end()); 122 | assert(TinySTL::Test::container_equal(l1, l2)); 123 | } 124 | void testCase8(){ 125 | tsL l1, l2; 126 | l1.push_back(1); l1.push_back(1); l1.push_back(1); 127 | l2.push_back(2); l2.push_back(2); 128 | 129 | l1.swap(l2); 130 | assert(l1.size() == 2 && l2.size() == 3); 131 | TinySTL::swap(l1, l2); 132 | assert(l1.size() == 3 && l2.size() == 2); 133 | } 134 | void testCase9(){ 135 | std::random_device rd; 136 | stdL l1; 137 | tsL l2; 138 | for (auto i = 0; i != 100; ++i){ 139 | auto ret = rd() % 65536; 140 | l1.push_back(ret); 141 | l2.push_back(ret); 142 | } 143 | 144 | l1.sort(); 145 | l2.sort(); 146 | assert(TinySTL::Test::container_equal(l1, l2)); 147 | 148 | l1.sort(std::greater()); 149 | l2.sort(std::greater()); 150 | assert(TinySTL::Test::container_equal(l1, l2)); 151 | } 152 | void testCase10(){ 153 | int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 154 | stdL l1(std::begin(arr), std::end(arr)); 155 | tsL l2(std::begin(arr), std::end(arr)); 156 | 157 | l1.reverse(); 158 | l2.reverse(); 159 | assert(TinySTL::Test::container_equal(l1, l2)); 160 | } 161 | void testCase11(){ 162 | int arr1[] = { 0, 1, 3, 5, 9 }, arr2[] = { 2, 4, 6, 7, 8 }; 163 | stdL first1(std::begin(arr1), std::end(arr1)), second1(std::begin(arr2), std::end(arr2)); 164 | tsL first2(std::begin(arr1), std::end(arr1)), second2(std::begin(arr2), std::end(arr2)); 165 | 166 | first1.merge(second1); 167 | first2.merge(second2); 168 | assert(TinySTL::Test::container_equal(first1, first2)); 169 | } 170 | void testCase12(){ 171 | int arr[] = { 0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7, 8, 8, 9, 11 }; 172 | stdL l1(std::begin(arr), std::end(arr)); 173 | tsL l2(std::begin(arr), std::end(arr)); 174 | 175 | l1.unique(); 176 | l2.unique(); 177 | assert(TinySTL::Test::container_equal(l1, l2)); 178 | } 179 | void testCase13(){ 180 | int arr[] = { 17, 89, 7, 14, 89, 0, 1, 4 }; 181 | stdL l1(std::begin(arr), std::end(arr)); 182 | tsL l2(std::begin(arr), std::end(arr)); 183 | 184 | l1.remove(89); 185 | l2.remove(89); 186 | assert(TinySTL::Test::container_equal(l1, l2)); 187 | 188 | auto func = [](int n){return n % 2 == 0; }; 189 | l1.remove_if(func); 190 | l2.remove_if(func); 191 | assert(TinySTL::Test::container_equal(l1, l2)); 192 | } 193 | void testCase14(){ 194 | stdL l1(10, 0), l3(10, 1); 195 | tsL l2(10, 0), l4(10, 1); 196 | 197 | l1.splice(l1.begin(), l3); 198 | l2.splice(l2.begin(), l4); 199 | assert(TinySTL::Test::container_equal(l1, l2)); 200 | 201 | auto l5 = l1; 202 | auto l6 = l2; 203 | l1.splice(l1.end(), l5, l5.begin()); 204 | l2.splice(l2.end(), l6, l6.begin()); 205 | assert(TinySTL::Test::container_equal(l1, l2)); 206 | 207 | auto it1 = l1.begin(); 208 | auto it2 = l2.begin(); 209 | for (auto i = 0; i != l1.size() / 2; ++i){ 210 | ++it1; 211 | ++it2; 212 | } 213 | l1.splice(it1, l5, l5.begin(), l5.end()); 214 | l2.splice(it2, l6, l6.begin(), l6.end()); 215 | assert(TinySTL::Test::container_equal(l1, l2)); 216 | } 217 | void testCase15(){ 218 | tsL l1(10, 0), l2(10, 1), l3(10, 0); 219 | 220 | assert(l1 == l3); 221 | assert(l1 != l2); 222 | } 223 | 224 | 225 | void testAllCases(){ 226 | testCase1(); 227 | testCase2(); 228 | testCase3(); 229 | testCase4(); 230 | testCase5(); 231 | testCase6(); 232 | testCase7(); 233 | testCase8(); 234 | testCase9(); 235 | testCase10(); 236 | testCase11(); 237 | testCase12(); 238 | testCase13(); 239 | testCase14(); 240 | testCase15(); 241 | } 242 | } 243 | } -------------------------------------------------------------------------------- /TinySTL/Test/ListTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _LIST_TEST_H_ 2 | #define _LIST_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../List.h" 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace TinySTL{ 15 | namespace ListTest{ 16 | template 17 | using stdL = std::list < T > ; 18 | template 19 | using tsL = TinySTL::list < T > ; 20 | 21 | void testCase1(); 22 | void testCase2(); 23 | void testCase3(); 24 | void testCase4(); 25 | void testCase5(); 26 | void testCase6(); 27 | void testCase7(); 28 | void testCase8(); 29 | void testCase9(); 30 | void testCase10(); 31 | void testCase11(); 32 | void testCase12(); 33 | void testCase13(); 34 | void testCase14(); 35 | void testCase15(); 36 | 37 | void testAllCases(); 38 | } 39 | } 40 | 41 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/PairTest.cpp: -------------------------------------------------------------------------------- 1 | #include "PairTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace PairTest{ 5 | template 6 | static inline bool container_equal(const Container1& pair1, const Container2& pair2){ 7 | return (pair1.first == pair2.first && pair1.second == pair2.second); 8 | } 9 | void testCase1(){ 10 | stdPair p1(5, 5); 11 | tsPair p2(5, 5); 12 | assert(container_equal(p1, p2)); 13 | } 14 | void testCase2(){ 15 | stdPair p1(stdPair(0, 0)); 16 | tsPair p2(tsPair(0, 0)); 17 | assert(container_equal(p1, p2)); 18 | } 19 | void testCase3(){ 20 | stdPair temp1 = std::make_pair(std::string("zxh"), std::string("zxh")); 21 | stdPair p1 = temp1; 22 | 23 | tsPair temp2 = TinySTL::make_pair(std::string("zxh"), std::string("zxh")); 24 | tsPair p2 = temp2; 25 | 26 | assert(container_equal(p1, p2)); 27 | } 28 | void testCase4(){ 29 | TinySTL::pair foo(10, 'z'); 30 | TinySTL::pair bar(90, 'a'); 31 | 32 | assert(!(foo == bar)); 33 | assert(foo != bar); 34 | assert(foo < bar); 35 | assert(!(foo > bar)); 36 | assert(foo <= bar); 37 | assert(!(foo >= bar)); 38 | } 39 | void testCase5(){ 40 | TinySTL::pair foo(10, 'z'); 41 | TinySTL::pair bar(90, 'a'); 42 | 43 | foo.swap(bar); 44 | 45 | assert(foo.first == 90 && foo.second == 'a'); 46 | assert(bar.first == 10 && bar.second == 'z'); 47 | } 48 | 49 | 50 | void testAllCases(){ 51 | testCase1(); 52 | testCase2(); 53 | testCase3(); 54 | testCase4(); 55 | testCase5(); 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /TinySTL/Test/PairTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _PAIR_TEST_H_ 2 | #define _PAIR_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../Utility.h" 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | namespace TinySTL{ 14 | namespace PairTest{ 15 | template 16 | using stdPair = std::pair < T, T > ; 17 | template 18 | using tsPair = TinySTL::pair < T, T > ; 19 | 20 | void testCase1(); 21 | void testCase2(); 22 | void testCase3(); 23 | void testCase4(); 24 | void testCase5(); 25 | 26 | void testAllCases(); 27 | } 28 | } 29 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/PriorityQueueTest.cpp: -------------------------------------------------------------------------------- 1 | #include "PriorityQueueTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace PriorityQueueTest{ 5 | void testCase1(){ 6 | int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, -1, -2, -3 }; 7 | stdPQ pq1(std::begin(arr), std::end(arr)); 8 | tsPQ pq2(std::begin(arr), std::end(arr)); 9 | 10 | while (!pq1.empty() && !pq2.empty()){ 11 | assert(pq1.top() == pq2.top()); 12 | pq1.pop(); pq2.pop(); 13 | } 14 | assert(pq1.empty() && pq2.empty()); 15 | } 16 | void testCase2(){ 17 | tsPQ pq; 18 | assert(pq.empty()); 19 | 20 | pq.push("zxh"); 21 | assert(!pq.empty()); 22 | } 23 | void testCase3(){ 24 | tsPQ pq; 25 | auto i = 1; 26 | for (; i != 10; ++i){ 27 | pq.push(i); 28 | assert(pq.size() == i); 29 | } 30 | for (i = pq.size(); i != 0; --i){ 31 | pq.pop(); 32 | assert(pq.size() == (i - 1)); 33 | } 34 | } 35 | void testCase4(){ 36 | stdPQ pq1; 37 | tsPQ pq2; 38 | 39 | pq1.push(30); 40 | pq1.push(100); 41 | pq1.push(25); 42 | pq1.push(40); 43 | 44 | pq2.push(30); 45 | pq2.push(100); 46 | pq2.push(25); 47 | pq2.push(40); 48 | 49 | while (!pq1.empty() && !pq2.empty()){ 50 | assert(pq1.top() == pq2.top()); 51 | pq1.pop(); 52 | pq2.pop(); 53 | } 54 | } 55 | void testCase5(){ 56 | tsPQ foo, bar; 57 | foo.push(15); foo.push(30); foo.push(10); 58 | bar.push(101); bar.push(202); 59 | 60 | assert(foo.size() == 3 && bar.size() == 2); 61 | foo.swap(bar); 62 | assert(foo.size() == 2 && bar.size() == 3); 63 | 64 | TinySTL::swap(foo, bar); 65 | assert(foo.size() == 3 && bar.size() == 2); 66 | } 67 | 68 | void testAllCases(){ 69 | testCase1(); 70 | testCase2(); 71 | testCase3(); 72 | testCase4(); 73 | testCase5(); 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /TinySTL/Test/PriorityQueueTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _PRIORITY_QUEUE_TEST_H_ 2 | #define _PRIORITY_QUEUE_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../Queue.h" 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | namespace TinySTL{ 14 | namespace PriorityQueueTest{ 15 | template 16 | using stdPQ = std::priority_queue < T > ; 17 | template 18 | using tsPQ = TinySTL::priority_queue < T > ; 19 | 20 | void testCase1(); 21 | void testCase2(); 22 | void testCase3(); 23 | void testCase4(); 24 | void testCase5(); 25 | 26 | void testAllCases(); 27 | } 28 | } 29 | 30 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/QueueTest.cpp: -------------------------------------------------------------------------------- 1 | #include "QueueTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace QueueTest{ 5 | void testCase1(){ 6 | stdQ q1; 7 | tsQ q2; 8 | 9 | for (auto i = 0; i != 10; ++i){ 10 | q1.push(i); 11 | q2.push(i); 12 | } 13 | for (auto i = 0; i != 10; ++i){ 14 | assert(q1.front() == q2.front()); 15 | q1.pop(); 16 | q2.pop(); 17 | } 18 | } 19 | void testCase2(){ 20 | tsQ q1; 21 | for (auto i = 0; i != 10; ++i) 22 | q1.push(i); 23 | auto q2(q1); 24 | assert(q1 == q2); 25 | assert(!(q1 != q2)); 26 | } 27 | void testCase3(){ 28 | tsQ q; 29 | assert(q.empty()); 30 | assert(q.size() == 0); 31 | 32 | q.push(10); 33 | q.push(11); 34 | assert(!q.empty()); 35 | assert(q.size() == 2); 36 | } 37 | void testCase4(){ 38 | tsQ q; 39 | q.push("front"); 40 | q.push("back"); 41 | 42 | assert(q.front() == "front"); 43 | assert(q.back() == "back"); 44 | } 45 | void testCase5(){ 46 | tsQ q1, q2; 47 | 48 | q1.push(1); q1.push(2); q1.push(3); 49 | q2.push(1); q2.push(2); 50 | 51 | assert(q1.size() == 3 && q2.size() == 2); 52 | q1.swap(q2); 53 | assert(q1.size() == 2 && q2.size() == 3); 54 | TinySTL::swap(q1, q2); 55 | assert(q1.size() == 3 && q2.size() == 2); 56 | } 57 | 58 | void testAllCases(){ 59 | testCase1(); 60 | testCase2(); 61 | testCase3(); 62 | testCase4(); 63 | testCase5(); 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /TinySTL/Test/QueueTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _QUEUE_TEST_H_ 2 | #define _QUEUE_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../Queue.h" 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | namespace TinySTL{ 13 | namespace QueueTest{ 14 | template 15 | using stdQ = std::queue < T > ; 16 | template 17 | using tsQ = TinySTL::queue < T > ; 18 | 19 | void testCase1(); 20 | void testCase2(); 21 | void testCase3(); 22 | void testCase4(); 23 | void testCase5(); 24 | 25 | void testAllCases(); 26 | } 27 | } 28 | 29 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/RefTest.cpp: -------------------------------------------------------------------------------- 1 | #include "RefTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace RefTest{ 5 | void testCaseRef(){ 6 | ref_t r1; 7 | assert(r1.count() == 0); 8 | assert(r1.get_data() == nullptr); 9 | 10 | int *p = new int(0); 11 | ref_t r2(p); 12 | assert(r2.count() == 1); 13 | assert(r2.get_data() != nullptr); 14 | 15 | ++r2; 16 | assert(r2.count() == 2); 17 | 18 | --r2; 19 | assert(r2.count() == 1); 20 | } 21 | 22 | void testAllCases(){ 23 | testCaseRef(); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /TinySTL/Test/RefTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _REF_TEST_H_ 2 | #define _REF_TEST_H_ 3 | 4 | #include "../Detail/Ref.h" 5 | 6 | #include 7 | 8 | namespace TinySTL{ 9 | namespace RefTest{ 10 | template 11 | using ref_t = TinySTL::Detail::ref_t < T > ; 12 | 13 | void testCaseRef(); 14 | void testAllCases(); 15 | } 16 | } 17 | 18 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/SharedPtrTest.cpp: -------------------------------------------------------------------------------- 1 | #include "SharedPtrTest.h" 2 | 3 | #include "../String.h" 4 | 5 | namespace TinySTL{ 6 | namespace SharedPtrTest{ 7 | void testCase1(){ 8 | shared_ptr sp1(new int(10)); 9 | assert(*(sp1.get()) == 10); 10 | 11 | shared_ptr sp2(new int(1), default_delete()); 12 | assert(sp2.use_count() == 1); 13 | 14 | auto sp3(sp2); 15 | assert(sp3.use_count() == 2); 16 | { 17 | auto sp4 = sp2; 18 | assert(sp4.use_count() == 3 && sp3.use_count() == sp4.use_count()); 19 | 20 | assert(sp2.get() == sp3.get() && sp2.get() == sp4.get()); 21 | assert(sp2 == sp3 && !(sp2 != sp4)); 22 | } 23 | assert(sp3.use_count() == 2); 24 | 25 | shared_ptr sp5(new string("hello")); 26 | assert(*sp5 == "hello"); 27 | sp5->append(" world"); 28 | assert(*sp5 == "hello world"); 29 | 30 | auto sp6 = make_shared(10, '0'); 31 | assert(*sp6 == "0000000000"); 32 | 33 | shared_ptr spp; 34 | assert(spp == nullptr); 35 | assert(!(spp != nullptr)); 36 | } 37 | 38 | void testAllCases(){ 39 | testCase1(); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /TinySTL/Test/SharedPtrTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _SHARED_PTR_TEST_H_ 2 | #define _SHARED_PTR_TEST_H_ 3 | 4 | #include "../Memory.h" 5 | 6 | #include 7 | 8 | namespace TinySTL{ 9 | namespace SharedPtrTest{ 10 | void testCase1(); 11 | 12 | void testAllCases(); 13 | } 14 | } 15 | 16 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/StackTest.cpp: -------------------------------------------------------------------------------- 1 | #include "StackTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace StackTest{ 5 | void testCase1(){ 6 | stdSt st1; 7 | tsSt st2; 8 | 9 | for (auto i = 0; i != 10; ++i){ 10 | st1.push(i); 11 | st2.push(i); 12 | } 13 | for (auto i = 0; i != 10; ++i){ 14 | assert(st1.top() == st2.top()); 15 | st1.pop(); 16 | st2.pop(); 17 | } 18 | } 19 | void testCase2(){ 20 | tsSt st; 21 | assert(st.empty()); 22 | assert(st.size() == 0); 23 | 24 | st.push("one"); 25 | st.push("two"); 26 | assert(!st.empty()); 27 | assert(st.size() == 2); 28 | } 29 | void testCase3(){ 30 | tsSt st1; 31 | for (auto i = 0; i != 5; ++i) 32 | st1.push(i); 33 | auto st2(st1); 34 | assert(st1 == st2); 35 | assert(!(st1 != st2)); 36 | } 37 | void testCase4(){ 38 | tsSt st1, st2; 39 | st1.push(1); st1.push(2); st1.push(3); 40 | st2.push(1); st2.push(2); 41 | assert(st1.size() == 3 && st2.size() == 2); 42 | st1.swap(st2); 43 | assert(st1.size() == 2 && st2.size() == 3); 44 | TinySTL::swap(st1, st2); 45 | assert(st1.size() == 3 && st2.size() == 2); 46 | } 47 | 48 | void testAllCases(){ 49 | testCase1(); 50 | testCase2(); 51 | testCase3(); 52 | testCase4(); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /TinySTL/Test/StackTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _STACK_TEST_H_ 2 | #define _STACK_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../Stack.h" 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | namespace TinySTL{ 13 | namespace StackTest{ 14 | template 15 | using stdSt = std::stack < T > ; 16 | template 17 | using tsSt = TinySTL::stack < T > ; 18 | 19 | void testCase1(); 20 | void testCase2(); 21 | void testCase3(); 22 | void testCase4(); 23 | 24 | void testAllCases(); 25 | } 26 | } 27 | 28 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/StringTest.cpp: -------------------------------------------------------------------------------- 1 | #include "StringTest.h" 2 | 3 | #include 4 | 5 | namespace TinySTL{ 6 | namespace StringTest{ 7 | 8 | void testCase1(){ 9 | const char *ptr = "hello world"; 10 | 11 | stdStr s1(ptr); 12 | tsStr s2(ptr); 13 | assert(TinySTL::Test::container_equal(s1, s2)); 14 | 15 | stdStr s3(ptr, 5); 16 | tsStr s4(ptr, 5); 17 | assert(TinySTL::Test::container_equal(s3, s4)); 18 | 19 | stdStr s5(10, 'z'); 20 | tsStr s6(10, 'z'); 21 | assert(TinySTL::Test::container_equal(s5, s6)); 22 | 23 | char arr[] = "zouxiaohang love cpp"; 24 | stdStr s7(std::begin(arr), std::end(arr)); 25 | tsStr s8(std::begin(arr), std::end(arr)); 26 | assert(TinySTL::Test::container_equal(s7, s8)); 27 | } 28 | void testCase2(){ 29 | stdStr temp1("hello, world"); 30 | tsStr temp2("hello, world"); 31 | 32 | stdStr s1(temp1); 33 | tsStr s2(temp2); 34 | assert(TinySTL::Test::container_equal(s1, s2)); 35 | 36 | stdStr s3(std::move(s1)); 37 | tsStr s4(std::move(s2)); 38 | assert(TinySTL::Test::container_equal(s3, s4)); 39 | 40 | stdStr s5(temp1, 1); 41 | tsStr s6(temp2, 1); 42 | assert(TinySTL::Test::container_equal(s5, s6)); 43 | 44 | stdStr s7(temp1, 0, 5); 45 | tsStr s8(temp2, 0, 5); 46 | assert(TinySTL::Test::container_equal(s7, s8)); 47 | } 48 | void testCase3(){ 49 | stdStr t1("hello, world"); 50 | tsStr t2("hello, world"); 51 | 52 | stdStr s1; s1 = 'a'; 53 | tsStr s2; s2 = 'a'; 54 | assert(TinySTL::Test::container_equal(s1, s2)); 55 | 56 | stdStr s3; s3 = "hello"; 57 | tsStr s4; s4 = "hello"; 58 | assert(TinySTL::Test::container_equal(s3, s4)); 59 | 60 | stdStr s5; s5 = t1; 61 | tsStr s6; s6 = t2; 62 | assert(TinySTL::Test::container_equal(s5, s6)); 63 | 64 | stdStr s7; s7 = std::move(t1); 65 | tsStr s8; s8 = std::move(t2); 66 | assert(TinySTL::Test::container_equal(s7, s8)); 67 | } 68 | void testCase4(){ 69 | tsStr str("Test string"); 70 | stdStr s(str.begin(), str.end()); 71 | auto i = 0; 72 | for (tsStr::iterator it = str.begin(); it != str.end(); ++it, ++i){ 73 | assert(*it == s[i]); 74 | } 75 | 76 | i = s.size() - 1; 77 | for (tsStr::reverse_iterator it = str.rbegin(); it != str.rend(); ++it, --i){ 78 | assert(*it == s[i]); 79 | } 80 | } 81 | void testCase5(){ 82 | tsStr s; 83 | assert(s.size() == 0); 84 | assert(s.length() == 0); 85 | 86 | s = "hello, world"; 87 | assert(s.size() == 12); 88 | assert(s.size() == 12); 89 | } 90 | void testCase6(){ 91 | stdStr s1("hello, world"); 92 | tsStr s2("hello, world"); 93 | 94 | s1.resize(5); 95 | s2.resize(5); 96 | assert(TinySTL::Test::container_equal(s1, s2)); 97 | 98 | s1.resize(20, 'z'); 99 | s2.resize(20, 'z'); 100 | assert(TinySTL::Test::container_equal(s1, s2)); 101 | 102 | s1.resize(6, 'a'); 103 | s2.resize(6, 'a'); 104 | assert(TinySTL::Test::container_equal(s1, s2)); 105 | 106 | s1.resize(100); 107 | s2.resize(100); 108 | assert(TinySTL::Test::container_equal(s1, s2)); 109 | } 110 | void testCase7(){ 111 | tsStr s; 112 | s.reserve(10); 113 | assert(s.capacity() == 10); 114 | } 115 | void testCase8(){ 116 | tsStr s; 117 | assert(s.empty()); 118 | 119 | s = "hello, world"; 120 | assert(!s.empty()); 121 | 122 | s.clear(); 123 | assert(s.empty()); 124 | } 125 | void testCase9(){ 126 | tsStr s; 127 | s.resize(10); 128 | for (auto i = 0; i != s.size(); ++i) 129 | s[i] = 'a' + i; 130 | assert(s == "abcdefghij"); 131 | 132 | s.back() = 'Z'; 133 | s.front() = 'A'; 134 | assert(s == "AbcdefghiZ"); 135 | } 136 | void testCase10(){ 137 | stdStr s1; 138 | tsStr s2; 139 | for (auto i = 0; i != 10; ++i){ 140 | s1.push_back('a' + i); 141 | s2.push_back('a' + i); 142 | } 143 | assert(TinySTL::Test::container_equal(s1, s2)); 144 | } 145 | void testCase11(){ 146 | stdStr s1; 147 | tsStr s2; 148 | 149 | s1.insert(s1.begin(), 'A'); 150 | s2.insert(s2.begin(), 'A'); 151 | assert(TinySTL::Test::container_equal(s1, s2)); 152 | 153 | s1.insert(s1.end(), 2, 'Z'); 154 | s2.insert(s2.end(), 2, 'Z'); 155 | assert(TinySTL::Test::container_equal(s1, s2)); 156 | 157 | size_t n = 2; 158 | s1.insert(2, 10, '@'); 159 | s2.insert(2, 10, '@'); 160 | assert(TinySTL::Test::container_equal(s1, s2)); 161 | 162 | s1.insert(0, "hello, world"); 163 | s2.insert(0, "hello, world"); 164 | assert(TinySTL::Test::container_equal(s1, s2)); 165 | 166 | s1.insert(s1.size() - 1, "zouxiaohang", 3); 167 | s2.insert(s2.size() - 1, "zouxiaohang", 3); 168 | assert(TinySTL::Test::container_equal(s1, s2)); 169 | 170 | stdStr s3; 171 | tsStr s4; 172 | 173 | s3.insert(s3.begin(), s1.begin(), s1.end()); 174 | s4.insert(s4.begin(), s2.begin(), s2.end()); 175 | assert(TinySTL::Test::container_equal(s3, s4)); 176 | 177 | s3.insert(1, s1); 178 | s4.insert(1, s2); 179 | assert(TinySTL::Test::container_equal(s3, s4)); 180 | 181 | stdStr t1("zouxiaoHANG"); 182 | tsStr t2("zouxiaoHANG"); 183 | s3.insert(s3.size(), t1, 7, t1.size() - 7); 184 | s4.insert(s4.size(), t2, 7, t2.size() - 7); 185 | assert(TinySTL::Test::container_equal(s3, s4)); 186 | } 187 | void testCase12(){ 188 | stdStr s1; 189 | tsStr s2; 190 | 191 | s1.append(stdStr("abc")); 192 | s2.append(tsStr("abc")); 193 | assert(TinySTL::Test::container_equal(s1, s2)); 194 | 195 | s1.append(stdStr("123456789"), 1, 3); 196 | s2.append(tsStr("123456789"), 1, 3); 197 | assert(TinySTL::Test::container_equal(s1, s2)); 198 | 199 | s1.append("hello"); 200 | s2.append("hello"); 201 | assert(TinySTL::Test::container_equal(s1, s2)); 202 | 203 | s1.append("world", 0, 5); 204 | s2.append("world", 0, 5); 205 | assert(TinySTL::Test::container_equal(s1, s2)); 206 | 207 | s1.append(10, 'A'); 208 | s2.append(10, 'A'); 209 | assert(TinySTL::Test::container_equal(s1, s2)); 210 | 211 | stdStr s3; s3.append(s1.begin(), s1.end()); 212 | tsStr s4; s4.append(s2.begin(), s2.end()); 213 | assert(TinySTL::Test::container_equal(s3, s4)); 214 | } 215 | void testCase13(){ 216 | stdStr s1; 217 | tsStr s2; 218 | 219 | s1 += 'A'; 220 | s2 += 'A'; 221 | assert(TinySTL::Test::container_equal(s1, s2)); 222 | 223 | s1 += "hello"; 224 | s2 += "hello"; 225 | assert(TinySTL::Test::container_equal(s1, s2)); 226 | 227 | s1 += stdStr("world"); 228 | s2 += tsStr("world"); 229 | assert(TinySTL::Test::container_equal(s1, s2)); 230 | } 231 | void testCase14(){ 232 | stdStr s1("hello world"); 233 | tsStr s2("hello world"); 234 | 235 | s1.pop_back(); 236 | s2.pop_back(); 237 | assert(TinySTL::Test::container_equal(s1, s2)); 238 | } 239 | void testCase15(){ 240 | stdStr s1("hello world"); 241 | tsStr s2("hello world"); 242 | 243 | s1.erase(s1.begin() + 1); 244 | s2.erase(s2.begin() + 1); 245 | assert(TinySTL::Test::container_equal(s1, s2)); 246 | 247 | s1.erase(2, s1.size() - 4); 248 | s2.erase(2, s2.size() - 4); 249 | assert(TinySTL::Test::container_equal(s1, s2)); 250 | 251 | s1.erase(s1.begin(), s1.end()); 252 | s2.erase(s2.begin(), s2.end()); 253 | assert(TinySTL::Test::container_equal(s1, s2)); 254 | } 255 | void testCase16(){ 256 | stdStr s1("zouxiaohang"), t1("I Love C++"); 257 | tsStr s2("zouxiaohang"), t2("I Love C++"); 258 | 259 | s1.replace(0, 3, t1); 260 | s2.replace(0, 3, t2); 261 | assert(TinySTL::Test::container_equal(s1, s2)); 262 | 263 | s1.replace(s1.begin(), s1.begin() + s1.size() / 2, t1); 264 | s2.replace(s2.begin(), s2.begin() + s2.size() / 2, t2); 265 | assert(TinySTL::Test::container_equal(s1, s2)); 266 | 267 | s1.replace(0, s1.size(), t1, 0, t1.size()); 268 | s2.replace(0, s2.size(), t2, 0, t2.size()); 269 | assert(TinySTL::Test::container_equal(s1, s2)); 270 | 271 | s1.replace(0, s1.size(), "123456789"); 272 | s2.replace(0, s2.size(), "123456789"); 273 | assert(TinySTL::Test::container_equal(s1, s2)); 274 | 275 | s1.replace(s1.begin(), s1.end(), stdStr("hubei")); 276 | s2.replace(s2.begin(), s2.end(), tsStr("hubei")); 277 | assert(TinySTL::Test::container_equal(s1, s2)); 278 | 279 | s1.replace(0, s1.size(), "wuhan", 5); 280 | s2.replace(0, s2.size(), "wuhan", 5); 281 | assert(TinySTL::Test::container_equal(s1, s2)); 282 | 283 | s1.replace(s1.begin(), s1.end(), "hongshanqu", 10); 284 | s2.replace(s2.begin(), s2.end(), "hongshanqu", 10); 285 | assert(TinySTL::Test::container_equal(s1, s2)); 286 | 287 | s1.replace(0, s1.size(), 10, 'Z'); 288 | s2.replace(0, s2.size(), 10, 'Z'); 289 | assert(TinySTL::Test::container_equal(s1, s2)); 290 | 291 | s1.replace(s1.begin(), s1.end(), 10, 'A'); 292 | s2.replace(s2.begin(), s2.end(), 10, 'A'); 293 | assert(TinySTL::Test::container_equal(s1, s2)); 294 | 295 | s1.replace(s1.begin(), s1.end(), t1.begin(), t1.end()); 296 | s2.replace(s2.begin(), s2.end(), t2.begin(), t2.end()); 297 | assert(TinySTL::Test::container_equal(s1, s2)); 298 | } 299 | void testCase17(){ 300 | tsStr buyer("money"); 301 | tsStr seller("goods"); 302 | 303 | seller.swap(buyer); 304 | assert(seller == "money"); 305 | assert(buyer == "goods"); 306 | } 307 | void testCase18(){ 308 | char buffer[20]; 309 | tsStr str("Test string..."); 310 | std::size_t length = str.copy(buffer, 6, 5); 311 | buffer[length] = '\0'; 312 | std::equal(std::begin(buffer), std::end(buffer), str.begin()); 313 | } 314 | void testCase19(){ 315 | tsStr str("There are two needles in this haystack with needles."); 316 | tsStr str2("needle"); 317 | 318 | auto found = str.find(str2); 319 | assert(found == 14); 320 | 321 | found = str.find("needles are small", found + 1, 6); 322 | assert(found == 44); 323 | 324 | found = str.find(tsStr("wuhan")); 325 | assert(found == tsStr::npos); 326 | 327 | found = str.find("haystack"); 328 | assert(found == 30); 329 | 330 | found = str.find('.'); 331 | assert(found == 51); 332 | 333 | str.replace(str.find(str2), str2.length(), "preposition"); 334 | assert(TinySTL::Test::container_equal(str, 335 | tsStr("There are two prepositions in this haystack with needles."))); 336 | } 337 | void testCase20(){ 338 | tsStr str("The sixth sick sheik's sixth sheep's sick."); 339 | tsStr key("sixth"); 340 | 341 | auto found = str.rfind(key); 342 | assert(found == 23); 343 | 344 | found = str.rfind(key, 24); 345 | assert(found == 23); 346 | 347 | found = str.rfind('.'); 348 | assert(found == str.size() - 1); 349 | 350 | found = str.rfind("The"); 351 | assert(found == 0); 352 | 353 | found = str.rfind("sick111", 10, 4); 354 | assert(found == 10); 355 | } 356 | void testCase21(){ 357 | tsStr str("Please, replace the vowels in this sentence by asterisks."); 358 | tsStr key("aeiou"); 359 | const char *arr = "aeiou"; 360 | 361 | auto found = str.find_first_of(arr); 362 | assert(found == 2); 363 | 364 | found = str.find_first_of(arr, found + 1); 365 | assert(found == 3); 366 | 367 | found = str.find_first_of(arr, found + 1, 1); 368 | assert(found == 12); 369 | 370 | found = str.find_first_of(key, found + 1); 371 | assert(found == 14); 372 | 373 | found = str.find_first_of('v', found + 1); 374 | assert(found == 20); 375 | } 376 | void testCase22(){ 377 | tsStr str("1234567890098765432112345678900"); 378 | 379 | auto found = str.find_last_of('6'); 380 | assert(found == 25); 381 | 382 | found = str.find_last_of('6', found - 1); 383 | assert(found == 14); 384 | 385 | found = str.find_last_of("01", 11, 2); 386 | assert(found == 10); 387 | 388 | found = str.find_last_of(tsStr("#1"), 19); 389 | assert(found == 19); 390 | } 391 | void testCase23(){ 392 | tsStr str("look for non-alphabetic characters..."); 393 | 394 | auto found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz "); 395 | assert(found == 12); 396 | } 397 | void testCase24(){ 398 | tsStr str("12345678900987654321"); 399 | 400 | auto found = str.find_last_not_of("023456789", str.size() - 2); 401 | assert(found == 0); 402 | } 403 | void testCase25(){ 404 | tsStr str = "We think in generalities, but we live in details."; 405 | 406 | auto s = str.substr(3, 5); 407 | assert(TinySTL::Test::container_equal(s, tsStr("think"))); 408 | } 409 | void testCase26(){ 410 | tsStr str1("green apple"); 411 | tsStr str2("red apple"); 412 | 413 | assert(str1.compare(str2) != 0); 414 | assert(str1.compare(6, 5, "apple") == 0); 415 | assert(str2.compare(str2.size() - 5, 5, "apple") == 0); 416 | assert(str1.compare(6, 5, str2, 4, 5) == 0); 417 | } 418 | void testCase27(){ 419 | tsStr firstlevel("com"); 420 | tsStr secondlevel("cplusplus"); 421 | tsStr scheme("http://"); 422 | 423 | auto hostname = "www." + secondlevel + '.' + firstlevel; 424 | auto url = scheme + hostname; 425 | 426 | assert(TinySTL::Test::container_equal(url, tsStr("http://www.cplusplus.com"))); 427 | } 428 | void testCase28(){ 429 | tsStr foo = "alpha"; 430 | tsStr bar = "beta"; 431 | 432 | assert(!(foo == bar)); 433 | assert(foo != bar); 434 | assert(foo < bar); 435 | assert(!(foo > bar)); 436 | assert(foo <= bar); 437 | assert(!(foo >= bar)); 438 | } 439 | void testCase29(){ 440 | tsStr name; 441 | std::ifstream in(".\\TestData\\string_test.txt"); 442 | 443 | if (in){ 444 | in >> name; 445 | assert(name == "zouxiaohang"); 446 | in.close(); 447 | } 448 | } 449 | 450 | void testAllCases(){ 451 | testCase1(); 452 | testCase2(); 453 | testCase3(); 454 | testCase4(); 455 | testCase5(); 456 | testCase6(); 457 | testCase7(); 458 | testCase8(); 459 | testCase9(); 460 | testCase10(); 461 | testCase11(); 462 | testCase12(); 463 | testCase13(); 464 | testCase14(); 465 | testCase15(); 466 | testCase16(); 467 | testCase17(); 468 | testCase18(); 469 | testCase19(); 470 | testCase20(); 471 | testCase21(); 472 | testCase22(); 473 | testCase23(); 474 | testCase24(); 475 | testCase25(); 476 | testCase26(); 477 | testCase27(); 478 | testCase28(); 479 | testCase29(); 480 | } 481 | } 482 | } -------------------------------------------------------------------------------- /TinySTL/Test/StringTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _STRING_TEST_H_ 2 | #define _STRING_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../String.h" 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | namespace TinySTL{ 13 | namespace StringTest{ 14 | using stdStr = std::string; 15 | using tsStr = TinySTL::string; 16 | 17 | void testCase1(); 18 | void testCase2(); 19 | void testCase3(); 20 | void testCase4(); 21 | void testCase5(); 22 | void testCase6(); 23 | void testCase7(); 24 | void testCase8(); 25 | void testCase9(); 26 | void testCase10(); 27 | void testCase11(); 28 | void testCase12(); 29 | void testCase13(); 30 | void testCase14(); 31 | void testCase15(); 32 | void testCase16(); 33 | void testCase17(); 34 | void testCase18(); 35 | void testCase19(); 36 | void testCase20(); 37 | void testCase21(); 38 | void testCase22(); 39 | void testCase23(); 40 | void testCase24(); 41 | void testCase25(); 42 | void testCase26(); 43 | void testCase27(); 44 | void testCase28(); 45 | void testCase29(); 46 | 47 | void testAllCases(); 48 | } 49 | } 50 | 51 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/SuffixArrayTest.cpp: -------------------------------------------------------------------------------- 1 | #include "SuffixArrayTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace SuffixArrayTest{ 5 | void testCase(){ 6 | //char arr[] = { 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'b' }; 7 | std::string str("aabaaaab"); 8 | 9 | //TinySTL::suffix_array sa(arr, 8); 10 | TinySTL::suffix_array sa(str.data(), str.size()); 11 | auto sa1 = sa.suffixArray(); 12 | auto sa2 = TinySTL::suffix_array::array_type{3, 4, 5, 0, 6, 1, 7, 2}; 13 | assert(TinySTL::Test::container_equal(sa1, sa2)); 14 | 15 | auto ra1 = sa.rankArray(); 16 | auto ra2 = TinySTL::suffix_array::array_type{ 3, 5, 7, 0, 1, 2, 4, 6 }; 17 | assert(TinySTL::Test::container_equal(ra1, ra2)); 18 | 19 | auto ha1 = sa.heightArray(); 20 | auto ha2 = TinySTL::suffix_array::array_type{ 3, 2, 3, 1, 2, 0, 1 }; 21 | assert(TinySTL::Test::container_equal(ha1, ha2)); 22 | } 23 | 24 | 25 | void testAllCases(){ 26 | testCase(); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /TinySTL/Test/SuffixArrayTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _SUFFIX_ARRAY_TEST_H_ 2 | #define _SUFFIX_ARRAY_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../SuffixArray.h" 7 | 8 | #include 9 | #include 10 | 11 | namespace TinySTL{ 12 | namespace SuffixArrayTest{ 13 | void testCase(); 14 | 15 | void testAllCases(); 16 | } 17 | } 18 | 19 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/TestUtil.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Test/TestUtil.h -------------------------------------------------------------------------------- /TinySTL/Test/TrieTreeTest.cpp: -------------------------------------------------------------------------------- 1 | #include "TrieTreeTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace TrieTreeTest{ 5 | void testCase1(){ 6 | trie_tree t; 7 | t.insert("abc"); 8 | t.insert("def"); 9 | 10 | assert(t.is_existed("abc")); 11 | assert(!t.is_existed("a")); 12 | } 13 | void testCase2(){ 14 | trie_tree t; 15 | string arr[] = { "ab", "abbreviation", "abide", "abolish", "abstract"}; 16 | for (const auto& str : arr){ 17 | t.insert(str); 18 | } 19 | t.insert("action"); 20 | 21 | auto v = t.get_word_by_prefix("ab"); 22 | assert(TinySTL::Test::container_equal(v, arr)); 23 | } 24 | void testCase3(){ 25 | trie_tree t; 26 | string arr[] = { "a", "ab", "abc", "d", "a", "abc" }; 27 | 28 | assert(t.empty()); 29 | assert(t.size() == 0); 30 | 31 | for (const auto& str : arr){ 32 | t.insert(str); 33 | } 34 | assert(!t.empty()); 35 | assert(t.size() == 4); 36 | } 37 | 38 | void testAllCases(){ 39 | testCase1(); 40 | testCase2(); 41 | testCase3(); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /TinySTL/Test/TrieTreeTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRIE_TREE_TEST_H_ 2 | #define _TRIE_TREE_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../TrieTree.h" 7 | 8 | #include 9 | 10 | namespace TinySTL{ 11 | namespace TrieTreeTest{ 12 | void testCase1(); 13 | void testCase2(); 14 | void testCase3(); 15 | 16 | void testAllCases(); 17 | } 18 | } 19 | 20 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/TypeTraitsTest.cpp: -------------------------------------------------------------------------------- 1 | #include "TypeTraitsTest.h" 2 | #include 3 | 4 | namespace TinySTL{ 5 | namespace TypeTraitsTest{ 6 | void testCase1(){ 7 | } 8 | 9 | void testAllCases(){ 10 | testCase1(); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /TinySTL/Test/TypeTraitsTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _TYPE_TRAITS_TEST_H_ 2 | #define _TYPE_TRAITS_TEST_H_ 3 | 4 | #include "../TypeTraits.h" 5 | 6 | namespace TinySTL{ 7 | namespace TypeTraitsTest{ 8 | void testAllCases(); 9 | } 10 | } 11 | 12 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/UFSetTest.cpp: -------------------------------------------------------------------------------- 1 | #include "UFSetTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace UFSetTest{ 5 | void testCase1(){ 6 | uf_set<10> uf; 7 | uf.Union(0, 1); 8 | uf.Union(2, 3); 9 | uf.Union(3, 1); 10 | assert(uf.Find(0) == uf.Find(2)); 11 | } 12 | 13 | void testAllCases(){ 14 | testCase1(); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /TinySTL/Test/UFSetTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _UF_SET_TEST_H_ 2 | #define _UF_SET_TEST_H_ 3 | 4 | #include "../UFSet.h" 5 | #include 6 | 7 | namespace TinySTL{ 8 | namespace UFSetTest{ 9 | void testCase1(); 10 | 11 | void testAllCases(); 12 | } 13 | } 14 | 15 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/UniquePtrTest.cpp: -------------------------------------------------------------------------------- 1 | #include "UniquePtrTest.h" 2 | 3 | #include "../String.h" 4 | 5 | namespace { 6 | template 7 | struct Del{ 8 | void operator()(T *p){ delete p; } 9 | }; 10 | } 11 | 12 | namespace TinySTL{ 13 | namespace UniquePtrTest{ 14 | void testCase1(){ 15 | unique_ptr up1; 16 | assert(up1 == nullptr); 17 | 18 | unique_ptr up2(new int(5)); 19 | assert(up2 != nullptr); 20 | assert(*up2 == 5); 21 | 22 | unique_ptr> up3(new string("zouxiaohang"), Del()); 23 | assert(up3 != nullptr); 24 | assert(*up3 == "zouxiaohang"); 25 | 26 | auto up4(std::move(up2)); 27 | assert(*up4 == 5); 28 | assert(up2 == nullptr); 29 | 30 | unique_ptr> up5; 31 | up5 = std::move(up3); 32 | assert(*up5 == "zouxiaohang"); 33 | assert(up3 == nullptr); 34 | 35 | auto up6 = make_unique(6, 'z'); 36 | assert(*up6 == "zzzzzz"); 37 | } 38 | void testCase2(){ 39 | auto up = make_unique(10, '0'); 40 | up->append("111"); 41 | assert(*up == "0000000000111"); 42 | 43 | up.release(); 44 | assert(up == nullptr); 45 | 46 | up.reset(new string("hello")); 47 | assert(*up == "hello"); 48 | } 49 | void testCase3(){ 50 | auto up1 = make_unique(2, '0'); 51 | auto up2 = make_unique(2, '1'); 52 | 53 | up1.swap(up2); 54 | assert(*up1 == "11" && *up2 == "00"); 55 | 56 | swap(up1, up2); 57 | assert(*up1 == "00" && *up2 == "11"); 58 | } 59 | 60 | void testAllCases(){ 61 | testCase1(); 62 | testCase2(); 63 | testCase3(); 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /TinySTL/Test/UniquePtrTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _UNIQUEPTR_TEST_H_ 2 | #define _UNIQUEPTR_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../Memory.h" 7 | 8 | #include 9 | 10 | namespace TinySTL{ 11 | namespace UniquePtrTest{ 12 | void testCase1(); 13 | void testCase2(); 14 | void testCase3(); 15 | 16 | void testAllCases(); 17 | } 18 | } 19 | 20 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/Unordered_setTest.cpp: -------------------------------------------------------------------------------- 1 | #include "Unordered_setTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace Unordered_setTest{ 5 | template 6 | bool container_equal(Container1& con1, Container2& con2){ 7 | std::vector vec1, vec2; 8 | for (auto& item : con1){ 9 | vec1.push_back(item); 10 | } 11 | for (auto& item : con2){ 12 | vec2.push_back(item); 13 | } 14 | std::sort(vec1.begin(), vec1.end()); 15 | std::sort(vec2.begin(), vec2.end()); 16 | return vec1 == vec2; 17 | } 18 | void testCase1(){ 19 | stdUst ust1(10); 20 | tsUst ust2(10); 21 | assert(container_equal(ust1, ust2)); 22 | 23 | int arr[] = { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 24 | stdUst ust3(std::begin(arr), std::end(arr)); 25 | tsUst ust4(std::begin(arr), std::end(arr)); 26 | assert(container_equal(ust3, ust4)); 27 | 28 | auto ust5(ust3); 29 | auto ust6(ust4); 30 | assert(container_equal(ust5, ust6)); 31 | 32 | auto ust7 = ust3; 33 | auto ust8 = ust4; 34 | assert(container_equal(ust7, ust8)); 35 | } 36 | void testCase2(){ 37 | tsUst ust1(10); 38 | assert(ust1.empty()); 39 | assert(ust1.size() == 0); 40 | 41 | int arr[] = { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 42 | tsUst ust2(std::begin(arr), std::end(arr)); 43 | assert(!ust2.empty()); 44 | assert(ust2.size() == 10); 45 | } 46 | void testCase3(){ 47 | tsUst ust1(10); 48 | stdUst ust2(10); 49 | std::random_device rd; 50 | for (auto i = 0; i != 100; ++i){ 51 | auto n = std::to_string(rd() % 65536); 52 | ust1.insert(n); 53 | ust2.insert(n); 54 | } 55 | assert(container_equal(ust1, ust2)); 56 | 57 | tsUst ust3(10); 58 | stdUst ust4(10); 59 | std::vector v(200); 60 | std::generate(v.begin(), v.end(), [&rd](){return rd() % 65536; }); 61 | ust3.insert(v.begin(), v.end()); 62 | ust4.insert(v.begin(), v.end()); 63 | assert(container_equal(ust3, ust4)); 64 | } 65 | void testCase4(){ 66 | int arr[] = { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 67 | stdUst ust1(std::begin(arr), std::end(arr)); 68 | tsUst ust2(std::begin(arr), std::end(arr)); 69 | 70 | ust1.erase(9); 71 | auto n = ust2.erase(9); 72 | assert(n == 1); 73 | ust1.erase(7); 74 | auto it = ust2.find(7); 75 | it = ust2.erase(it); 76 | assert(it != ust2.end()); 77 | assert(container_equal(ust1, ust2)); 78 | } 79 | void testCase5(){ 80 | int arr[] = { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9 }; 81 | tsUst ust(std::begin(arr), std::end(arr)); 82 | 83 | assert(ust.find(0) != ust.end()); 84 | assert(ust.count(10) == 0); 85 | } 86 | 87 | void testAllCases(){ 88 | testCase1(); 89 | testCase2(); 90 | testCase3(); 91 | testCase4(); 92 | testCase5(); 93 | } 94 | } 95 | } -------------------------------------------------------------------------------- /TinySTL/Test/Unordered_setTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _UNORDERED_SET_TEST_H_ 2 | #define _UNORDERED_SET_TEST_H_ 3 | 4 | #include "TestUtil.h" 5 | 6 | #include "../Unordered_set.h" 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | namespace TinySTL{ 16 | namespace Unordered_setTest{ 17 | template 18 | using stdUst = std::unordered_set < T > ; 19 | template 20 | using tsUst = TinySTL::Unordered_set < T > ; 21 | 22 | void testCase1(); 23 | void testCase2(); 24 | void testCase3(); 25 | void testCase4(); 26 | void testCase5(); 27 | 28 | void testAllCases(); 29 | } 30 | } 31 | 32 | #endif -------------------------------------------------------------------------------- /TinySTL/Test/VectorTest.cpp: -------------------------------------------------------------------------------- 1 | #include "VectorTest.h" 2 | 3 | namespace TinySTL{ 4 | namespace VectorTest{ 5 | 6 | void testCase1(){ 7 | stdVec v1(10, "zxh"); 8 | tsVec v2(10, "zxh"); 9 | assert(TinySTL::Test::container_equal(v1, v2)); 10 | 11 | stdVec v3(10); 12 | tsVec v4(10); 13 | assert(TinySTL::Test::container_equal(v3, v4)); 14 | 15 | std::array arr = { "abc", "def", "ghi" }; 16 | stdVec v5(std::begin(arr), std::end(arr)); 17 | tsVec v6(std::begin(arr), std::end(arr)); 18 | assert(TinySTL::Test::container_equal(v5, v6)); 19 | } 20 | void testCase2(){ 21 | stdVec temp1(10, 0); 22 | tsVec temp2(10, 0); 23 | 24 | auto v1(temp1); 25 | auto v2(temp2); 26 | assert(TinySTL::Test::container_equal(v1, v2)); 27 | 28 | auto v3(std::move(temp1)); 29 | auto v4(std::move(temp2)); 30 | assert(TinySTL::Test::container_equal(v3, v4)); 31 | 32 | auto v5 = v1; 33 | auto v6 = v2; 34 | assert(TinySTL::Test::container_equal(v5, v6)); 35 | 36 | auto v7 = std::move(v3); 37 | auto v8 = std::move(v4); 38 | assert(TinySTL::Test::container_equal(v7, v8)); 39 | } 40 | void testCase3(){ 41 | tsVec v1, v2; 42 | for (int i = 0; i != 100; ++i){ 43 | v1.push_back(i); 44 | v2.push_back(i); 45 | } 46 | 47 | assert(v1 == v2); 48 | assert(!(v1 != v2)); 49 | } 50 | void testCase4(){ 51 | tsVec myvector; 52 | for (int i = 1; i <= 5; i++) myvector.push_back(i); 53 | 54 | auto i = 1; 55 | for (tsVec::iterator it = myvector.begin(); it != myvector.end(); ++it, ++i){ 56 | assert(*it == i); 57 | } 58 | 59 | i = 1; 60 | for (tsVec::const_iterator it = myvector.cbegin(); it != myvector.cend(); ++it, ++i){ 61 | assert(*it == i); 62 | } 63 | } 64 | void testCase5(){ 65 | tsVec myvector(5); // 5 default-constructed ints 66 | int i = 0; 67 | tsVec::reverse_iterator rit = myvector.rbegin(); 68 | for (; rit != myvector.rend(); ++rit) 69 | *rit = ++i; 70 | 71 | i = 5; 72 | for (tsVec::iterator it = myvector.begin(); it != myvector.end(); ++it, --i){ 73 | assert(*it == i); 74 | } 75 | 76 | i = 1; 77 | for (tsVec::reverse_iterator it = myvector.rbegin(); it != myvector.rend(); ++it, ++i){ 78 | assert(*it == i); 79 | } 80 | } 81 | void testCase6(){ 82 | tsVec v(11, 0); 83 | assert(v.size() == 11); 84 | 85 | v.resize(5); 86 | assert(v.size() == 5); 87 | 88 | v.resize(20); 89 | assert(v.size() == 20); 90 | } 91 | void testCase7(){ 92 | tsVec v; 93 | v.reserve(20); 94 | assert(v.capacity() == 20); 95 | } 96 | void testCase8(){ 97 | stdVec v1(10); 98 | tsVec v2(10); 99 | for (unsigned i = 0; i < 10; i++){ 100 | v1[i] = i; 101 | v2[i] = i; 102 | } 103 | assert(TinySTL::Test::container_equal(v1, v2)); 104 | 105 | v1.front() = 99; 106 | v2.front() = 99; 107 | v1.back() = 100; 108 | v2.back() = 100; 109 | 110 | assert(TinySTL::Test::container_equal(v1, v2)); 111 | } 112 | void testCase9(){ 113 | stdVec v1(5); 114 | tsVec v2(5); 115 | 116 | auto p1 = v1.data(); 117 | auto p2 = v2.data(); 118 | *p1 = 10; ++p1; *p1 = 20; p1[2] = 100; 119 | *p2 = 10; ++p2; *p2 = 20; p2[2] = 100; 120 | 121 | assert(TinySTL::Test::container_equal(v1, v2)); 122 | } 123 | void testCase10(){ 124 | tsVec foo(3, 100); // three ints with a value of 100 125 | tsVec bar(2, 200); // five ints with a value of 200 126 | 127 | assert(TinySTL::Test::container_equal(foo, stdVec < int > { 100, 100, 100 })); 128 | assert(TinySTL::Test::container_equal(bar, stdVec < int > {200, 200})); 129 | 130 | foo.swap(bar); 131 | assert(TinySTL::Test::container_equal(bar, stdVec < int > { 100, 100, 100 })); 132 | assert(TinySTL::Test::container_equal(foo, stdVec < int > {200, 200})); 133 | } 134 | void testCase11(){ 135 | stdVec v1; 136 | tsVec v2; 137 | 138 | v1.push_back("hello "); v1.push_back("world"); 139 | v2.push_back("hello "); v2.push_back("world"); 140 | assert(TinySTL::Test::container_equal(v1, v2)); 141 | 142 | v1.pop_back(); 143 | v2.pop_back(); 144 | assert(TinySTL::Test::container_equal(v1, v2)); 145 | } 146 | void testCase12(){ 147 | stdVec v1; 148 | tsVec v2; 149 | 150 | v1.insert(v1.begin(), 0); 151 | v2.insert(v2.begin(), 0); 152 | assert(TinySTL::Test::container_equal(v1, v2)); 153 | 154 | v1.insert(v1.end(), 1); 155 | v2.insert(v2.end(), 1); 156 | assert(TinySTL::Test::container_equal(v1, v2)); 157 | 158 | v1.insert(v1.begin() + v1.size() / 2, 10, 0); 159 | v2.insert(v2.begin() + v2.size() / 2, 10, 0); 160 | assert(TinySTL::Test::container_equal(v1, v2)); 161 | 162 | int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 163 | v1.insert(v1.end(), std::begin(arr), std::end(arr)); 164 | v2.insert(v2.end(), std::begin(arr), std::end(arr)); 165 | assert(TinySTL::Test::container_equal(v1, v2)); 166 | } 167 | void testCase13(){ 168 | stdVec v1; 169 | tsVec v2; 170 | for (int i = 1; i <= 10; i++) { 171 | v1.push_back(i); 172 | v2.push_back(i); 173 | } 174 | v1.erase(v1.begin() + 5); 175 | v2.erase(v2.begin() + 5); 176 | assert(TinySTL::Test::container_equal(v1, v2)); 177 | 178 | v1.erase(v1.begin(), v1.begin() + 3); 179 | v2.erase(v2.begin(), v2.begin() + 3); 180 | assert(TinySTL::Test::container_equal(v1, v2)); 181 | } 182 | void testCase14(){ 183 | tsVec foo(3, 100); 184 | tsVec bar(2, 200); 185 | 186 | assert(!(foo == bar)); 187 | assert(foo != bar); 188 | } 189 | 190 | class TestItem 191 | { 192 | public: 193 | TestItem() 194 | { 195 | ++count; 196 | } 197 | TestItem(const TestItem & other) 198 | { 199 | ++count; 200 | } 201 | 202 | virtual ~TestItem() 203 | { 204 | --count; 205 | } 206 | 207 | static int getCount() 208 | { 209 | return count; 210 | } 211 | private: 212 | static int count; 213 | }; 214 | int TestItem::count = 0; 215 | 216 | void testCase15() 217 | { 218 | assert(TestItem::getCount() == 0); 219 | { 220 | typedef TinySTL::vector TVector; 221 | TVector t(10); 222 | t.push_back(TestItem()); 223 | t.push_back(TestItem()); 224 | t.push_back(TestItem()); 225 | t.insert(t.begin(), t.begin(), t.begin() + 1); 226 | 227 | } 228 | assert(TestItem::getCount() == 0); 229 | 230 | } 231 | 232 | void testAllCases(){ 233 | testCase1(); 234 | testCase2(); 235 | testCase3(); 236 | testCase4(); 237 | testCase5(); 238 | testCase6(); 239 | testCase7(); 240 | testCase8(); 241 | testCase9(); 242 | testCase10(); 243 | testCase11(); 244 | testCase12(); 245 | testCase13(); 246 | testCase14(); 247 | testCase15(); 248 | } 249 | } 250 | } -------------------------------------------------------------------------------- /TinySTL/Test/VectorTest.h: -------------------------------------------------------------------------------- 1 | #ifndef _VECTOR_TEST_H_ 2 | #define _VECTOR_TEST_H_ 3 | 4 | #include "../Vector.h" 5 | #include "TestUtil.h" 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | namespace TinySTL{ 16 | namespace VectorTest{ 17 | template 18 | using stdVec = std::vector < T >; 19 | 20 | template 21 | using tsVec = TinySTL::vector < T >; 22 | 23 | void testCase1(); 24 | void testCase2(); 25 | void testCase3(); 26 | void testCase4(); 27 | void testCase5(); 28 | void testCase6(); 29 | void testCase7(); 30 | void testCase8(); 31 | void testCase9(); 32 | void testCase10(); 33 | void testCase11(); 34 | void testCase12(); 35 | void testCase13(); 36 | void testCase14(); 37 | 38 | void testAllCases(); 39 | } 40 | } 41 | 42 | #endif -------------------------------------------------------------------------------- /TinySTL/TestData/string_test.txt: -------------------------------------------------------------------------------- 1 | zouxiaohang -------------------------------------------------------------------------------- /TinySTL/TinySTL.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {8B728502-8B31-4983-B9C3-13D8CCA52181} 15 | Win32Proj 16 | TinySTL 17 | 18 | 19 | 20 | Application 21 | true 22 | v140 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v140 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | C:\Users\zxh\Desktop\boost_1_56_0;$(IncludePath) 45 | C:\Users\zxh\Desktop\boost_1_56_0\libs;$(LibraryPath) 46 | 47 | 48 | false 49 | C:\Users\zxh\Desktop\boost_1_56_0;$(IncludePath) 50 | 51 | 52 | 53 | 54 | 55 | Level3 56 | Disabled 57 | WIN32;_DEBUG;_CONSOLE;_LIB;NOMINMAX;%(PreprocessorDefinitions) 58 | 59 | 60 | Console 61 | true 62 | 63 | 64 | 65 | 66 | Level3 67 | 68 | 69 | MaxSpeed 70 | true 71 | true 72 | WIN32;NDEBUG;_CONSOLE;_LIB;NOMINMAX;%(PreprocessorDefinitions) 73 | 74 | 75 | Console 76 | true 77 | true 78 | true 79 | 0x10000000 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 | -------------------------------------------------------------------------------- /TinySTL/TinySTL.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | {092c2875-2b56-404b-977e-a9b4aa67c134} 18 | 19 | 20 | {102ada31-2e23-4fa3-b913-81e7e40d12a7} 21 | 22 | 23 | {f20d9032-1c1d-424a-a8aa-f39c87789d3b} 24 | 25 | 26 | {7dd5c5f0-33b6-44c6-b2b9-d4c0ec1d4c13} 27 | 28 | 29 | {676114e9-f556-46bf-9f51-65d820758de2} 30 | 31 | 32 | 33 | 34 | 头文件\Profiler 35 | 36 | 37 | Test 38 | 39 | 40 | 源文件 41 | 42 | 43 | Test 44 | 45 | 46 | Test 47 | 48 | 49 | Test 50 | 51 | 52 | Test 53 | 54 | 55 | Test 56 | 57 | 58 | Test 59 | 60 | 61 | Test 62 | 63 | 64 | Test 65 | 66 | 67 | Test 68 | 69 | 70 | Test 71 | 72 | 73 | Test 74 | 75 | 76 | Detail 77 | 78 | 79 | Detail 80 | 81 | 82 | Test 83 | 84 | 85 | Test 86 | 87 | 88 | Test 89 | 90 | 91 | Test 92 | 93 | 94 | Test 95 | 96 | 97 | Test 98 | 99 | 100 | Test 101 | 102 | 103 | Test 104 | 105 | 106 | Detail 107 | 108 | 109 | Test 110 | 111 | 112 | Test 113 | 114 | 115 | Test 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 | 头文件\Profiler 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 | Test 184 | 185 | 186 | Test 187 | 188 | 189 | Test 190 | 191 | 192 | Test 193 | 194 | 195 | Test 196 | 197 | 198 | Test 199 | 200 | 201 | 头文件 202 | 203 | 204 | Test 205 | 206 | 207 | Test 208 | 209 | 210 | Test 211 | 212 | 213 | Test 214 | 215 | 216 | Test 217 | 218 | 219 | Test 220 | 221 | 222 | Test 223 | 224 | 225 | Detail 226 | 227 | 228 | Detail 229 | 230 | 231 | Detail 232 | 233 | 234 | Detail 235 | 236 | 237 | Detail 238 | 239 | 240 | Test 241 | 242 | 243 | Detail 244 | 245 | 246 | Test 247 | 248 | 249 | Detail 250 | 251 | 252 | 头文件 253 | 254 | 255 | Detail 256 | 257 | 258 | Test 259 | 260 | 261 | 头文件 262 | 263 | 264 | Detail 265 | 266 | 267 | Test 268 | 269 | 270 | 头文件 271 | 272 | 273 | Test 274 | 275 | 276 | 头文件 277 | 278 | 279 | Test 280 | 281 | 282 | Detail 283 | 284 | 285 | Test 286 | 287 | 288 | Test 289 | 290 | 291 | 头文件 292 | 293 | 294 | Detail 295 | 296 | 297 | Test 298 | 299 | 300 | Test 301 | 302 | 303 | 头文件 304 | 305 | 306 | Test 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | ScreenShots 315 | 316 | 317 | ScreenShots 318 | 319 | 320 | ScreenShots 321 | 322 | 323 | ScreenShots 324 | 325 | 326 | ScreenShots 327 | 328 | 329 | ScreenShots 330 | 331 | 332 | 333 | 334 | TestData 335 | 336 | 337 | TestData 338 | 339 | 340 | -------------------------------------------------------------------------------- /TinySTL/TrieTree.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRIE_TREE_H_ 2 | #define _TRIE_TREE_H_ 3 | 4 | #include "String.h" 5 | #include "Vector.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace TinySTL{ 13 | class trie_tree{ 14 | private: 15 | struct trie_node{ 16 | char data; 17 | bool is_a_word; 18 | std::map> map_childs; 19 | trie_node() :data('\0'), is_a_word(false){} 20 | trie_node(char ch, bool is) :data(ch), is_a_word(is){} 21 | }; 22 | typedef std::unique_ptr node_ptr; 23 | public: 24 | typedef string value_type; 25 | typedef size_t size_type; 26 | private: 27 | trie_node *root_; 28 | size_type size_; 29 | public: 30 | trie_tree(); 31 | ~trie_tree(); 32 | trie_tree(const trie_tree&) = delete; 33 | trie_tree& operator = (const trie_tree&) = delete; 34 | 35 | bool empty()const; 36 | size_type size()const; 37 | 38 | vector get_word_by_prefix(const string& prefix)const; 39 | void print_tree(std::ostream& os = std::cout)const; 40 | bool insert(const string& word); 41 | bool is_existed(const string& word)const; 42 | private: 43 | node_ptr make_node(char ch, bool is_a_word); 44 | inline trie_node* get_root()const{ return root_; } 45 | void _get_word_by_prefix(const string& prefix, const node_ptr& up, const string& real_prefix, vector& words)const; 46 | void __get_word_by_prefix(const node_ptr& up, string& word, const string& prefix, vector& words)const; 47 | void _print_tree(std::ostream& os, const node_ptr& up, string word)const; 48 | bool _insert(const string& word, const node_ptr& up); 49 | bool _is_existed(const string& word, const node_ptr& up)const; 50 | };// end of trie_tree 51 | } 52 | 53 | #endif -------------------------------------------------------------------------------- /TinySTL/TypeTraits.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/TypeTraits.h -------------------------------------------------------------------------------- /TinySTL/UFSet.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/UFSet.h -------------------------------------------------------------------------------- /TinySTL/UninitializedFunctions.h: -------------------------------------------------------------------------------- 1 | #ifndef _UNINITIALIZED_FUNCTIONS_H_ 2 | #define _UNINITIALIZED_FUNCTIONS_H_ 3 | 4 | #include "Algorithm.h" 5 | #include "Construct.h" 6 | #include "Iterator.h" 7 | #include "TypeTraits.h" 8 | 9 | namespace TinySTL{ 10 | 11 | /***************************************************************************/ 12 | template 13 | ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, 14 | ForwardIterator result, _true_type); 15 | template 16 | ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, 17 | ForwardIterator result, _false_type); 18 | 19 | template 20 | ForwardIterator uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result){ 21 | typedef typename _type_traits::value_type>::is_POD_type isPODType; 22 | return _uninitialized_copy_aux(first, last, result, isPODType()); 23 | } 24 | template 25 | ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, 26 | ForwardIterator result, _true_type){ 27 | memcpy(result, first, (last - first) * sizeof(*first)); 28 | return result + (last - first); 29 | } 30 | template 31 | ForwardIterator _uninitialized_copy_aux(InputIterator first, InputIterator last, 32 | ForwardIterator result, _false_type){ 33 | int i = 0; 34 | for (; first != last; ++first, ++i){ 35 | construct((result + i), *first); 36 | } 37 | return (result + i); 38 | } 39 | 40 | /***************************************************************************/ 41 | template 42 | void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, 43 | const T& value, _true_type); 44 | template 45 | void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, 46 | const T& value, _false_type); 47 | 48 | template 49 | void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& value){ 50 | typedef typename _type_traits::is_POD_type isPODType; 51 | _uninitialized_fill_aux(first, last, value, isPODType()); 52 | } 53 | template 54 | void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, 55 | const T& value, _true_type){ 56 | fill(first, last, value); 57 | } 58 | template 59 | void _uninitialized_fill_aux(ForwardIterator first, ForwardIterator last, 60 | const T& value, _false_type){ 61 | for (; first != last; ++first){ 62 | construct(first, value); 63 | } 64 | } 65 | 66 | /***************************************************************************/ 67 | template 68 | ForwardIterator _uninitialized_fill_n_aux(ForwardIterator first, 69 | Size n, const T& x, _true_type); 70 | template 71 | ForwardIterator _uninitialized_fill_n_aux(ForwardIterator first, 72 | Size n, const T& x, _false_type); 73 | 74 | template 75 | inline ForwardIterator uninitialized_fill_n(ForwardIterator first, 76 | Size n, const T& x){ 77 | typedef typename _type_traits::is_POD_type isPODType; 78 | return _uninitialized_fill_n_aux(first, n, x, isPODType()); 79 | } 80 | template 81 | ForwardIterator _uninitialized_fill_n_aux(ForwardIterator first, 82 | Size n, const T& x, _true_type){ 83 | return fill_n(first, n, x); 84 | } 85 | template 86 | ForwardIterator _uninitialized_fill_n_aux(ForwardIterator first, 87 | Size n, const T& x, _false_type){ 88 | int i = 0; 89 | for (; i != n; ++i){ 90 | construct((T*)(first + i), x); 91 | } 92 | return (first + i); 93 | } 94 | } 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /TinySTL/Unordered_set.h: -------------------------------------------------------------------------------- 1 | #ifndef _UNORDERED_SET_H_ 2 | #define _UNORDERED_SET_H_ 3 | 4 | #include "Allocator.h" 5 | #include "Algorithm.h" 6 | #include "Functional.h" 7 | #include "Iterator.h" 8 | #include "List.h" 9 | #include "Vector.h" 10 | 11 | namespace TinySTL{ 12 | template 13 | class Unordered_set; 14 | namespace Detail{ 15 | template, 16 | class KeyEqual = TinySTL::equal_to, class Allocator = TinySTL::allocator < Key >> 17 | class ust_iterator : public iterator{ 18 | private: 19 | template 20 | friend class Unordered_set; 21 | private: 22 | typedef Unordered_set* cntrPtr; 23 | size_t bucket_index_; 24 | ListIterator iterator_; 25 | cntrPtr container_; 26 | public: 27 | ust_iterator(size_t index, ListIterator it, cntrPtr ptr); 28 | ust_iterator& operator ++(); 29 | ust_iterator operator ++(int); 30 | Key& operator*(){ return *iterator_; } 31 | Key* operator->(){ return &(operator*()); } 32 | private: 33 | template 34 | friend bool operator ==(const ust_iterator& lhs, 35 | const ust_iterator& rhs); 36 | template 37 | friend bool operator !=(const ust_iterator& lhs, 38 | const ust_iterator& rhs); 39 | }; 40 | }//end of namespace Detail 41 | 42 | 43 | template, 44 | class KeyEqual = TinySTL::equal_to, class Allocator = TinySTL::allocator < Key >> 45 | class Unordered_set{ 46 | private: 47 | template 48 | friend class Detail::ust_iterator; 49 | public: 50 | typedef Key key_type; 51 | typedef Key value_type; 52 | typedef size_t size_type; 53 | typedef Hash haser; 54 | typedef KeyEqual key_equal; 55 | typedef Allocator allocator_type; 56 | typedef value_type& reference; 57 | typedef const value_type& const_reference; 58 | typedef typename TinySTL::list::iterator local_iterator; 59 | typedef Detail::ust_iterator::iterator, Hash, KeyEqual, Allocator> iterator; 60 | private: 61 | TinySTL::vector> buckets_; 62 | size_type size_; 63 | float max_load_factor_; 64 | #define PRIME_LIST_SIZE 28 65 | static size_t prime_list_[PRIME_LIST_SIZE]; 66 | public: 67 | explicit Unordered_set(size_t bucket_count); 68 | template 69 | Unordered_set(InputIterator first, InputIterator last); 70 | Unordered_set(const Unordered_set& ust); 71 | Unordered_set& operator = (const Unordered_set& ust); 72 | 73 | size_type size()const; 74 | bool empty()const; 75 | size_type bucket_count()const; 76 | size_type bucket_size(size_type i)const; 77 | size_type bucket(const key_type& key)const; 78 | float load_factor()const; 79 | float max_load_factor()const; 80 | void max_load_factor(float z); 81 | void rehash(size_type n); 82 | 83 | iterator begin(); 84 | iterator end(); 85 | local_iterator begin(size_type i); 86 | local_iterator end(size_type i); 87 | 88 | iterator find(const key_type& key); 89 | size_type count(const key_type& key); 90 | 91 | TinySTL::pair insert(const value_type& val); 92 | template 93 | void insert(InputIterator first, InputIterator last); 94 | iterator erase(iterator position); 95 | size_type erase(const key_type& key); 96 | 97 | haser hash_function()const; 98 | key_equal key_eq()const; 99 | allocator_type get_allocator()const; 100 | private: 101 | size_type next_prime(size_type n)const; 102 | size_type bucket_index(const key_type& key)const; 103 | bool has_key(const key_type& key); 104 | public: 105 | template 106 | friend void swap(Unordered_set& lhs, 107 | Unordered_set& rhs); 108 | }; 109 | 110 | }//end of namespace TinySTL 111 | 112 | #include "Detail\Unordered_set.impl.h" 113 | #endif -------------------------------------------------------------------------------- /TinySTL/Utility.h: -------------------------------------------------------------------------------- 1 | #ifndef _UTILITY_H_ 2 | #define _UTILITY_H_ 3 | 4 | //#include "Algorithm.h" 5 | 6 | namespace TinySTL{ 7 | //************ [swap] *************** 8 | template 9 | void swap(T& a, T& b){ 10 | T temp = a; 11 | a = b; 12 | b = temp; 13 | } 14 | //*********** [pair] **************** 15 | template 16 | struct pair{ 17 | public: 18 | typedef T1 first_type; 19 | typedef T2 second_type; 20 | public: 21 | T1 first; 22 | T2 second; 23 | public: 24 | pair(){} 25 | template 26 | pair(const pair& pr); 27 | pair(const first_type& a, const second_type& b); 28 | pair& operator= (const pair& pr); 29 | void swap(pair& pr); 30 | public: 31 | template 32 | friend bool operator== (const pair& lhs, const pair& rhs); 33 | template 34 | friend bool operator!= (const pair& lhs, const pair& rhs); 35 | template 36 | friend bool operator< (const pair& lhs, const pair& rhs); 37 | template 38 | friend bool operator<= (const pair& lhs, const pair& rhs); 39 | template 40 | friend bool operator> (const pair& lhs, const pair& rhs); 41 | template 42 | friend bool operator>= (const pair& lhs, const pair& rhs); 43 | template 44 | friend void swap(pair& x, pair& y); 45 | 46 | }; 47 | template 48 | template 49 | pair::pair(const pair& pr) :first(pr.first), second(pr.second){} 50 | template 51 | pair::pair(const first_type& a, const second_type& b) : first(a), second(b){} 52 | template 53 | pair& pair::operator =(const pair& pr){ 54 | if (this != &pr){ 55 | first = pr.first; 56 | second = pr.second; 57 | } 58 | return *this; 59 | } 60 | template 61 | void pair::swap(pair& pr){ 62 | TinySTL::swap(first, pr.first); 63 | TinySTL::swap(second, pr.second); 64 | } 65 | 66 | template 67 | bool operator== (const pair& lhs, const pair& rhs){ 68 | return lhs.first == rhs.first && lhs.second == rhs.second; 69 | } 70 | template 71 | bool operator!= (const pair& lhs, const pair& rhs){ 72 | return !(lhs == rhs); 73 | } 74 | template 75 | bool operator< (const pair& lhs, const pair& rhs){ 76 | return lhs.first 79 | bool operator<= (const pair& lhs, const pair& rhs){ 80 | return !(rhs 83 | bool operator>(const pair& lhs, const pair& rhs){ 84 | return rhs 87 | bool operator>= (const pair& lhs, const pair& rhs){ 88 | return !(lhs 91 | void swap(pair& x, pair& y){ 92 | x.swap(y); 93 | } 94 | // ******* [make_pair] ************ 95 | template 96 | pair make_pair(const U& u, const V& v){ 97 | return pair(u, v); 98 | } 99 | } 100 | #endif -------------------------------------------------------------------------------- /TinySTL/Vector.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zouxiaohang/TinySTL/b6bde62f4539241a2ddc7577d39ba61925a132fa/TinySTL/Vector.h -------------------------------------------------------------------------------- /TinySTL/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Algorithm.h" 4 | #include "Profiler\Profiler.h" 5 | 6 | #include "Test\AlgorithmTest.h" 7 | #include "Test\AVLTreeTest.h" 8 | #include "Test\BitmapTest.h" 9 | #include "Test\BinarySearchTreeTest.h" 10 | #include "Test\CircularBufferTest.h" 11 | #include "Test\COWPtrTest.h" 12 | #include "Test\DequeTest.h" 13 | #include "Test\GraphTest.h" 14 | #include "Test\ListTest.h" 15 | #include "Test\PairTest.h" 16 | #include "Test\PriorityQueueTest.h" 17 | #include "Test\QueueTest.h" 18 | #include "Test\RefTest.h" 19 | #include "Test\SharedPtrTest.h" 20 | #include "Test\StackTest.h" 21 | #include "Test\StringTest.h" 22 | #include "Test\SuffixArrayTest.h" 23 | #include "Test\TrieTreeTest.h" 24 | #include "Test\TypeTraitsTest.h" 25 | #include "Test\UFSetTest.h" 26 | #include "Test\UniquePtrTest.h" 27 | #include "Test\Unordered_setTest.h" 28 | #include "Test\VectorTest.h" 29 | 30 | using namespace TinySTL::Profiler; 31 | 32 | int main(){ 33 | TinySTL::AlgorithmTest::testAllCases(); 34 | TinySTL::AVLTreeTest::testAllCases(); 35 | TinySTL::BitmapTest::testAllCases(); 36 | TinySTL::BinarySearchTreeTest::testAllCases(); 37 | TinySTL::CircularBufferTest::testAllCases(); 38 | TinySTL::COWPtrTest::testAllCases(); 39 | TinySTL::DequeTest::testAllCases(); 40 | TinySTL::ListTest::testAllCases(); 41 | TinySTL::GraphTest::testAllCases(); 42 | TinySTL::PairTest::testAllCases(); 43 | TinySTL::PriorityQueueTest::testAllCases(); 44 | TinySTL::QueueTest::testAllCases(); 45 | TinySTL::RefTest::testAllCases(); 46 | TinySTL::SharedPtrTest::testAllCases(); 47 | TinySTL::StackTest::testAllCases(); 48 | TinySTL::StringTest::testAllCases(); 49 | TinySTL::SuffixArrayTest::testAllCases(); 50 | TinySTL::TrieTreeTest::testAllCases(); 51 | TinySTL::TypeTraitsTest::testAllCases(); 52 | TinySTL::UFSetTest::testAllCases(); 53 | TinySTL::UniquePtrTest::testAllCases(); 54 | TinySTL::Unordered_setTest::testAllCases(); 55 | TinySTL::VectorTest::testAllCases(); 56 | 57 | system("pause"); 58 | return 0; 59 | } --------------------------------------------------------------------------------