STLHelper.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "hpsocket/GlobalDef.h"
  25. #include "Singleton.h"
  26. #include "StringT.h"
  27. #include <math.h>
  28. #include <functional>
  29. #include <algorithm>
  30. #include <random>
  31. #include <vector>
  32. #include <deque>
  33. #include <queue>
  34. #include <stack>
  35. #include <list>
  36. #include <set>
  37. #include <map>
  38. #include <unordered_set>
  39. #include <unordered_map>
  40. #include <type_traits>
  41. #define hash_set unordered_set
  42. #define hash_map unordered_map
  43. #define hash_multimap unordered_multimap
  44. using namespace std;
  45. typedef list<short> short_list;
  46. typedef list<int> int_list;
  47. typedef list<long> long_list;
  48. typedef list<int64_t> int64_list;
  49. typedef list<unsigned short> ushort_list;
  50. typedef list<unsigned int> uint_list;
  51. typedef list<unsigned long> ulong_list;
  52. typedef list<uint64_t> uint64_list;
  53. typedef list<float> float_list;
  54. typedef list<double> double_list;
  55. typedef stack<short> short_stack;
  56. typedef stack<int> int_stack;
  57. typedef stack<long> long_stack;
  58. typedef stack<int64_t> int64_stack;
  59. typedef stack<unsigned short> ushort_stack;
  60. typedef stack<unsigned int> uint_stack;
  61. typedef stack<unsigned long> ulong_stack;
  62. typedef stack<uint64_t> uint64_stack;
  63. typedef stack<float> float_stack;
  64. typedef stack<double> double_stack;
  65. typedef queue<short> short_queue;
  66. typedef queue<int> int_queue;
  67. typedef queue<long> long_queue;
  68. typedef queue<int64_t> int64_queue;
  69. typedef queue<unsigned short> ushort_queue;
  70. typedef queue<unsigned int> uint_queue;
  71. typedef queue<unsigned long> ulong_queue;
  72. typedef queue<uint64_t> uint64_queue;
  73. typedef queue<float> float_queue;
  74. typedef queue<double> double_queue;
  75. typedef deque<short> short_deque;
  76. typedef deque<int> int_deque;
  77. typedef deque<long> long_deque;
  78. typedef deque<int64_t> int64_deque;
  79. typedef deque<unsigned short> ushort_deque;
  80. typedef deque<unsigned int> uint_deque;
  81. typedef deque<unsigned long> ulong_deque;
  82. typedef deque<uint64_t> uint64_deque;
  83. typedef deque<float> float_deque;
  84. typedef deque<double> double_deque;
  85. typedef vector<short> short_vector;
  86. typedef vector<int> int_vector;
  87. typedef vector<long> long_vector;
  88. typedef vector<int64_t> int64_vector;
  89. typedef vector<unsigned short> ushort_vector;
  90. typedef vector<unsigned int> uint_vector;
  91. typedef vector<unsigned long> ulong_vector;
  92. typedef vector<uint64_t> uint64_vector;
  93. typedef vector<float> float_vector;
  94. typedef vector<double> double_vector;
  95. typedef set<short> short_set;
  96. typedef set<int> int_set;
  97. typedef set<long> long_set;
  98. typedef set<int64_t> int64_set;
  99. typedef set<unsigned short> ushort_set;
  100. typedef set<unsigned int> uint_set;
  101. typedef set<unsigned long> ulong_set;
  102. typedef set<uint64_t> uint64_set;
  103. typedef set<float> float_set;
  104. typedef set<double> double_set;
  105. typedef hash_set<short> short_hash_set;
  106. typedef hash_set<int> int_hash_set;
  107. typedef hash_set<long> long_hash_set;
  108. typedef hash_set<int64_t> int64_hash_set;
  109. typedef hash_set<unsigned short> ushort_hash_set;
  110. typedef hash_set<unsigned int> uint_hash_set;
  111. typedef hash_set<unsigned long> ulong_hash_set;
  112. typedef hash_set<uint64_t> uint64_hash_set;
  113. typedef hash_set<float> float_hash_set;
  114. typedef hash_set<double> double_hash_set;
  115. typedef unordered_set<short> short_unordered_set;
  116. typedef unordered_set<int> int_unordered_set;
  117. typedef unordered_set<long> long_unordered_set;
  118. typedef unordered_set<int64_t> int64_unordered_set;
  119. typedef unordered_set<unsigned short> ushort_unordered_set;
  120. typedef unordered_set<unsigned int> uint_unordered_set;
  121. typedef unordered_set<unsigned long> ulong_unordered_set;
  122. typedef unordered_set<uint64_t> uint64_unordered_set;
  123. typedef unordered_set<float> float_unordered_set;
  124. typedef unordered_set<double> double_unordered_set;
  125. typedef list<INT_PTR> int_ptr_list;
  126. typedef list<LONG_PTR> long_ptr_list;
  127. typedef list<UINT_PTR> uint_ptr_list;
  128. typedef list<ULONG_PTR> ulong_ptr_list;
  129. typedef stack<INT_PTR> int_ptr_stack;
  130. typedef stack<LONG_PTR> long_ptr_stack;
  131. typedef stack<UINT_PTR> uint_ptr_stack;
  132. typedef stack<ULONG_PTR> ulong_ptr_stack;
  133. typedef queue<INT_PTR> int_ptr_queue;
  134. typedef queue<LONG_PTR> long_ptr_queue;
  135. typedef queue<UINT_PTR> uint_ptr_queue;
  136. typedef queue<ULONG_PTR> ulong_ptr_queue;
  137. typedef deque<INT_PTR> int_ptr_deque;
  138. typedef deque<LONG_PTR> long_ptr_deque;
  139. typedef deque<UINT_PTR> uint_ptr_deque;
  140. typedef deque<ULONG_PTR> ulong_ptr_deque;
  141. typedef vector<INT_PTR> int_ptr_vector;
  142. typedef vector<LONG_PTR> long_ptr_vector;
  143. typedef vector<UINT_PTR> uint_ptr_vector;
  144. typedef vector<ULONG_PTR> ulong_ptr_vector;
  145. typedef set<INT_PTR> int_ptr_set;
  146. typedef set<LONG_PTR> long_ptr_set;
  147. typedef set<UINT_PTR> uint_ptr_set;
  148. typedef set<ULONG_PTR> ulong_ptr_set;
  149. typedef hash_set<INT_PTR> int_ptr_hash_set;
  150. typedef hash_set<LONG_PTR> long_ptr_hash_set;
  151. typedef hash_set<UINT_PTR> uint_ptr_hash_set;
  152. typedef hash_set<ULONG_PTR> ulong_ptr_hash_set;
  153. typedef unordered_set<INT_PTR> int_ptr_unordered_set;
  154. typedef unordered_set<LONG_PTR> long_ptr_unordered_set;
  155. typedef unordered_set<UINT_PTR> uint_ptr_unordered_set;
  156. typedef unordered_set<ULONG_PTR> ulong_ptr_unordered_set;
  157. /*****************************************************************************/
  158. /******************************** 容器操作函数 *******************************/
  159. /**********************************
  160. 描述: 清除普通集合 , 适用于 vector<Object> / list<Object>
  161. 参数:
  162. v : vector / list / set
  163. 返回值:
  164. **********************************/
  165. template<class Set> void ClearSet(Set& v)
  166. {
  167. v.clear();
  168. }
  169. template<class Set> struct Set_Cleaner
  170. {
  171. static void Clear(Set& v) {ClearSet(v);}
  172. };
  173. /**********************************
  174. 描述: 清除指针集合 (清除前先释放指针), 适用于 vector<Object*> / list<Object*>
  175. 参数:
  176. v : vector / list / set
  177. 返回值:
  178. **********************************/
  179. template<class PtrSet> void ClearPtrSet(PtrSet& v)
  180. {
  181. for(auto it = v.begin(),
  182. end = v.end();
  183. it != end;
  184. ++it)
  185. delete (*it);
  186. v.clear();
  187. }
  188. template<class PtrSet> struct PtrSet_Cleaner
  189. {
  190. static void Clear(PtrSet& v) {ClearPtrSet(v);}
  191. };
  192. /**********************************
  193. 描述: 清除指针集合 (指针同时又指向数组), 适用于 vector<Object*[]> / list<Object*[]>
  194. 参数:
  195. v : vector / list / set
  196. 返回值:
  197. **********************************/
  198. template<class PtrArraySet> void ClearPtrArraySet(PtrArraySet& v)
  199. {
  200. for(auto it = v.begin(),
  201. end = v.end();
  202. it != end;
  203. ++it)
  204. delete[] (*it);
  205. v.clear();
  206. }
  207. template<class PtrArraySet> struct PtrArraySet_Cleaner
  208. {
  209. static void Clear(PtrArraySet& v) {ClearPtrArraySet(v);}
  210. };
  211. /**********************************
  212. 描述: 清除普通影射 , 适用于 map<key, value>
  213. 参数:
  214. v : map
  215. 返回值:
  216. **********************************/
  217. template<class Map> void ClearMap(Map& v)
  218. {
  219. v.clear();
  220. }
  221. template<class Map> struct Map_Cleaner
  222. {
  223. static void Clear(Map& v) {ClearMap(v);}
  224. };
  225. /**********************************
  226. 描述: 清除指针影射 (清除前先释放指针), 适用于 map<key, Object*>
  227. 参数:
  228. v : map
  229. 返回值:
  230. **********************************/
  231. template<class PtrMap> void ClearPtrMap(PtrMap& v)
  232. {
  233. for(auto it = v.begin(),
  234. end = v.end();
  235. it != end;
  236. ++it)
  237. delete it->second;
  238. v.clear();
  239. }
  240. template<class PtrMap> struct PtrMap_Cleaner
  241. {
  242. static void Clear(PtrMap& v) {ClearPtrMap(v);}
  243. };
  244. /**********************************
  245. 描述: 清除指针影射 (指针同时又指向数组), 适用于 map<key, Object*[]>
  246. 参数:
  247. v : map
  248. 返回值:
  249. **********************************/
  250. template<class PtrArrayMap> void ClearPtrArrayMap(PtrArrayMap& v)
  251. {
  252. for(auto it = v.begin(),
  253. end = v.end();
  254. it != end;
  255. ++it)
  256. delete[] it->second;
  257. v.clear();
  258. }
  259. template<class PtrArrayMap> struct PtrArrayMap_Cleaner
  260. {
  261. static void Clear(PtrArrayMap& v) {ClearPtrArrayMap(v);}
  262. };
  263. /**********************************
  264. 描述: 清除集合-集合 (清除前先清除内部集合), 适用于 set<vector<Object>*>
  265. 参数:
  266. v : vector / list / set
  267. 返回值:
  268. **********************************/
  269. template<class SetSet> void ClearSetSet(SetSet& v)
  270. {
  271. for(auto it = v.begin(),
  272. end = v.end();
  273. it != end;
  274. ++it)
  275. {
  276. (*it)->clear();
  277. delete (*it);
  278. }
  279. v.clear();
  280. }
  281. template<class SetSet> struct SetSet_Cleaner
  282. {
  283. static void Clear(SetSet& v) {ClearSetSet(v);}
  284. };
  285. /**********************************
  286. 描述: 清除指针集合-集合 (清除前先清除内部指针集合), 适用于 set<vector<Object*>*>
  287. 参数:
  288. v : vector / list / set
  289. 返回值:
  290. **********************************/
  291. template<class PtrSetSet> void ClearPtrSetSet(PtrSetSet& v)
  292. {
  293. for(auto it = v.begin(),
  294. end = v.end();
  295. it != end;
  296. ++it)
  297. {
  298. ClearPtrSet(**it);
  299. delete (*it);
  300. }
  301. v.clear();
  302. }
  303. template<class PtrSetSet> struct PtrSetSet_Cleaner
  304. {
  305. static void Clear(PtrSetSet& v) {ClearPtrSetSet(v);}
  306. };
  307. /**********************************
  308. 描述: 清除指针数组集合影射 (清除前先清除指针数组集合), 适用于 map<vector<Object*[]>*>
  309. 参数:
  310. v : vector / list / set
  311. 返回值:
  312. **********************************/
  313. template<class PtrArraySetSet> void ClearPtrArraySetSet(PtrArraySetSet& v)
  314. {
  315. for(auto it = v.begin(),
  316. end = v.end();
  317. it != end;
  318. ++it)
  319. {
  320. ClearPtrArraySet(**it);
  321. delete (*it);
  322. }
  323. v.clear();
  324. }
  325. template<class PtrArraySetSet> struct PtrArraySetSet_Cleaner
  326. {
  327. static void Clear(PtrArraySetSet& v) {ClearPtrArraySetSet(v);}
  328. };
  329. /**********************************
  330. 描述: 清除集合影射 (清除前先清除集合), 适用于 map<key, vector<Object>*>
  331. 参数:
  332. v : map
  333. 返回值:
  334. **********************************/
  335. template<class SetMap> void ClearSetMap(SetMap& v)
  336. {
  337. for(auto it = v.begin(),
  338. end = v.end();
  339. it != end;
  340. ++it)
  341. {
  342. it->second->clear();
  343. delete it->second;
  344. }
  345. v.clear();
  346. }
  347. template<class SetMap> struct SetMap_Cleaner
  348. {
  349. static void Clear(SetMap& v) {ClearSetMap(v);}
  350. };
  351. /**********************************
  352. 描述: 清除指针集合影射 (清除前先清除指针集合), 适用于 map<key, vector<Object*>*>
  353. 参数:
  354. v : map
  355. 返回值:
  356. **********************************/
  357. template<class PtrSetMap> void ClearPtrSetMap(PtrSetMap& v)
  358. {
  359. for(auto it = v.begin(),
  360. end = v.end();
  361. it != end;
  362. ++it)
  363. {
  364. ClearPtrSet(*(it->second));
  365. delete it->second;
  366. }
  367. v.clear();
  368. }
  369. template<class PtrSetMap> struct PtrSetMap_Cleaner
  370. {
  371. static void Clear(PtrSetMap& v) {ClearPtrSetMap(v);}
  372. };
  373. /**********************************
  374. 描述: 清除指针数组集合影射 (清除前先清除指针数组集合), 适用于 map<key, vector<Object*[]>*>
  375. 参数:
  376. v : map
  377. 返回值:
  378. **********************************/
  379. template<class PtrArraySetMap> void ClearPtrArraySetMap(PtrArraySetMap& v)
  380. {
  381. for(auto it = v.begin(),
  382. end = v.end();
  383. it != end;
  384. ++it)
  385. {
  386. ClearPtrArraySet(*(it->second));
  387. delete it->second;
  388. }
  389. v.clear();
  390. }
  391. template<class PtrArraySetMap> struct PtrArraySetMap_Cleaner
  392. {
  393. static void Clear(PtrArraySetMap& v) {ClearPtrArraySetMap(v);}
  394. };
  395. /**********************************
  396. 描述: 清除映射-影射 (清除前先清除内部映射), 适用于 map<key, map<key2, Object>*>
  397. 参数:
  398. v : map
  399. 返回值:
  400. **********************************/
  401. template<class MapMap> void ClearMapMap(MapMap& v)
  402. {
  403. for(auto it = v.begin(),
  404. end = v.end();
  405. it != end;
  406. ++it)
  407. {
  408. it->second->clear();
  409. delete it->second;
  410. }
  411. v.clear();
  412. }
  413. template<class MapMap> struct MapMap_Cleaner
  414. {
  415. static void Clear(MapMap& v) {ClearMapMap(v);}
  416. };
  417. /**********************************
  418. 描述: 清除指针映射-影射 (清除前先清除指针内部映射), 适用于 map<key, map<key2, Object*>*>
  419. 参数:
  420. v : map
  421. 返回值:
  422. **********************************/
  423. template<class PtrMapMap> void ClearPtrMapMap(PtrMapMap& v)
  424. {
  425. for(auto it = v.begin(),
  426. end = v.end();
  427. it != end;
  428. ++it)
  429. {
  430. ClearPtrMap(*(it->second));
  431. delete it->second;
  432. }
  433. v.clear();
  434. }
  435. template<class PtrMapMap> struct PtrMapMap_Cleaner
  436. {
  437. static void Clear(PtrMapMap& v) {ClearPtrMapMap(v);}
  438. };
  439. /**********************************
  440. 描述: 清除指针映射-影射 (清除前先清除指针数组内部映射), 适用于 map<key, map<key2, Object*[]>*>
  441. 参数:
  442. v : map
  443. 返回值:
  444. **********************************/
  445. template<class PtrArrayMapMap> void ClearPtrArrayMapMap(PtrArrayMapMap& v)
  446. {
  447. for(auto it = v.begin(),
  448. end = v.end();
  449. it != end;
  450. ++it)
  451. {
  452. ClearPtrArrayMap(*(it->second));
  453. delete it->second;
  454. }
  455. v.clear();
  456. }
  457. template<class PtrArrayMapMap> struct PtrArrayMapMap_Cleaner
  458. {
  459. static void Clear(PtrArrayMapMap& v) {ClearPtrArrayMapMap(v);}
  460. };
  461. /************************************************************************/
  462. /* 指针集合容器 */
  463. /************************************************************************/
  464. template<class Set, class Cleaner> struct SetWrapper
  465. {
  466. typedef typename Set::iterator iterator;
  467. typedef typename Set::const_iterator const_iterator;
  468. typedef typename Set::value_type value_type;
  469. typedef typename Set::reference reference;
  470. typedef typename Set::const_reference const_reference;
  471. typedef typename Set::pointer pointer;
  472. typedef typename Set::const_pointer const_pointer;
  473. typedef typename Set::size_type size_type;
  474. typedef typename Set::difference_type difference_type;
  475. SetWrapper()
  476. {
  477. }
  478. virtual ~SetWrapper()
  479. {
  480. Clear();
  481. }
  482. void Clear()
  483. {
  484. if(!IsEmpty())
  485. {
  486. Cleaner::Clear(m_set);
  487. }
  488. }
  489. Set& operator * () {return m_set;}
  490. const Set& operator * () const {return m_set;}
  491. Set* operator -> () {return &m_set;}
  492. const Set* operator -> () const {return &m_set;}
  493. Set& Get () {return m_set;}
  494. operator Set& () {return m_set;}
  495. bool IsEmpty () const {return m_set.empty();}
  496. size_t Size () const {return m_set.size();}
  497. protected:
  498. Set m_set;
  499. DECLARE_NO_COPY_CLASS(SetWrapper)
  500. };
  501. template<class Set, class Cleaner> struct VectorWrapper : public SetWrapper<Set, Cleaner>
  502. {
  503. typedef SetWrapper<Set, Cleaner> __super;
  504. typedef typename __super::reference reference;
  505. typedef typename __super::const_reference const_reference;
  506. typedef typename __super::size_type size_type;
  507. VectorWrapper()
  508. {
  509. }
  510. reference operator [] (size_type i) {return __super::m_set[i];}
  511. const_reference operator [] (size_type i) const {return __super::m_set[i];}
  512. DECLARE_NO_COPY_CLASS(VectorWrapper)
  513. };
  514. /************************************************************************/
  515. /* 指针数组集合容器 */
  516. /************************************************************************/
  517. /************************************************************************/
  518. /* 指针映射容器 */
  519. /************************************************************************/
  520. template<class Map, class Cleaner> struct MapWrapper
  521. {
  522. typedef typename Map::iterator iterator;
  523. typedef typename Map::const_iterator const_iterator;
  524. typedef typename Map::key_type key_type;
  525. typedef typename Map::mapped_type mapped_type;
  526. typedef typename Map::value_type value_type;
  527. typedef typename Map::reference reference;
  528. typedef typename Map::const_reference const_reference;
  529. typedef typename Map::pointer pointer;
  530. typedef typename Map::size_type size_type;
  531. typedef typename Map::difference_type difference_type;
  532. MapWrapper()
  533. {
  534. }
  535. ~MapWrapper()
  536. {
  537. Clear();
  538. }
  539. void Clear()
  540. {
  541. if(!IsEmpty())
  542. {
  543. Cleaner::Clear(m_map);
  544. }
  545. }
  546. Map& operator * () {return m_map;}
  547. const Map& operator * () const {return m_map;}
  548. Map* operator -> () {return &m_map;}
  549. const Map* operator -> () const {return &m_map;}
  550. mapped_type& operator [] (const key_type& key) {return m_map[key];}
  551. const mapped_type& operator [] (const key_type& key) const {return m_map[key];}
  552. Map& Get () {return m_map;}
  553. operator Map& () {return m_map;}
  554. bool IsEmpty () const {return m_map.empty();}
  555. size_t Size () const {return m_map.size();}
  556. private:
  557. Map m_map;
  558. DECLARE_NO_COPY_CLASS(MapWrapper)
  559. };
  560. /************************************************************************/
  561. /* 比较仿函数 */
  562. /************************************************************************/
  563. template<class T> struct char_comparator
  564. {
  565. typedef T row_type;
  566. static row_type row_type_value(const T& v) {return (row_type)v;}
  567. static bool equal_to(const T& v1, const T& v2) {return strcmp(v1, v2) == 0;}
  568. };
  569. template<class T> struct char_nc_comparator
  570. {
  571. typedef T row_type;
  572. static row_type row_type_value(const T& v) {return (row_type)v;}
  573. static bool equal_to(const T& v1, const T& v2) {return stricmp(v1, v2) == 0;}
  574. };
  575. template<class T> struct wchar_comparator
  576. {
  577. typedef T row_type;
  578. static row_type row_type_value(const T& v) {return (row_type)v;}
  579. static bool equal_to(const T& v1, const T& v2) {return wcscmp(v1, v2) == 0;}
  580. };
  581. template<class T> struct wchar_nc_comparator
  582. {
  583. typedef T row_type;
  584. static row_type row_type_value(const T& v) {return (row_type)v;}
  585. static bool equal_to(const T& v1, const T& v2) {return wcsicmp(v1, v2) == 0;}
  586. };
  587. template<class T> struct cstring_comparator
  588. {
  589. typedef typename T::PCXSTR row_type;
  590. static row_type row_type_value(const T& v) {return (row_type)v;}
  591. static bool equal_to(const T& v1, const T& v2) {return v1.Compare(v2) == 0;}
  592. };
  593. template<class T> struct cstring_nc_comparator
  594. {
  595. typedef typename T::PCXSTR row_type;
  596. static row_type row_type_value(const T& v) {return (row_type)v;}
  597. static bool equal_to(const T& v1, const T& v2) {return v1.CompareNoCase(v2) == 0;}
  598. };
  599. // char/wchar_t/CStringX hash function
  600. template<class T, class H> struct str_hash_func_t
  601. {
  602. struct hash
  603. {
  604. size_t operator() (const T& t) const
  605. {
  606. return hash_value(H::row_type_value(t));
  607. }
  608. };
  609. struct equal_to
  610. {
  611. bool operator() (const T& t1, const T& t2) const
  612. {
  613. return H::equal_to(t1, t2);
  614. }
  615. };
  616. };
  617. // char/wchar_t/CStringX hash function (no case)
  618. template<class T, class H, class B> struct str_nc_hash_func_t
  619. {
  620. struct hash
  621. {
  622. size_t operator() (const T& t) const
  623. {
  624. size_t _Val = 2166136261U;
  625. typename H::row_type lpsz = H::row_type_value(t);
  626. B c;
  627. while((c = *lpsz++) != 0)
  628. {
  629. if(c >= 'A' && c <= 'Z')
  630. c = (char)(c + 32);
  631. _Val = 16777619U * _Val ^ c;
  632. }
  633. return _Val;
  634. }
  635. };
  636. struct equal_to
  637. {
  638. bool operator() (const T& t1, const T& t2) const
  639. {
  640. return H::equal_to(t1, t2);
  641. }
  642. };
  643. };
  644. typedef str_hash_func_t<LPCSTR, char_comparator<LPCSTR>> str_hash_func;
  645. typedef str_hash_func_t<LPCWSTR, wchar_comparator<LPCWSTR>> wstr_hash_func;
  646. typedef str_hash_func_t<CStringA, cstring_comparator<CStringA>> cstringa_hash_func;
  647. typedef str_hash_func_t<CStringW, cstring_comparator<CStringW>> cstringw_hash_func;
  648. typedef str_nc_hash_func_t<LPCSTR, char_nc_comparator<LPCSTR>, char> str_nc_hash_func;
  649. typedef str_nc_hash_func_t<LPCWSTR, wchar_nc_comparator<LPCWSTR>, wchar_t> wstr_nc_hash_func;
  650. typedef str_nc_hash_func_t<CStringA, cstring_nc_comparator<CStringA>, char> cstringa_nc_hash_func;
  651. typedef str_nc_hash_func_t<CStringW, cstring_nc_comparator<CStringW>, wchar_t> cstringw_nc_hash_func;
  652. #ifdef _UNICODE
  653. typedef cstringw_hash_func cstring_hash_func;
  654. typedef cstringw_nc_hash_func cstring_nc_hash_func;
  655. #else
  656. typedef cstringa_hash_func cstring_hash_func;
  657. typedef cstringa_nc_hash_func cstring_nc_hash_func;
  658. #endif
  659. struct bool_comp_func
  660. {
  661. bool operator() (bool v1, bool v2) const
  662. {
  663. if(!v1)
  664. return false;
  665. if(v1 == v2)
  666. return false;
  667. return true;
  668. }
  669. };
  670. template<class T>
  671. // T -> (signed / unsigned) short / int / long / int64_t
  672. struct integer_comp_func
  673. {
  674. bool operator() (T v1, T v2) const
  675. {
  676. return v1 < v2;
  677. }
  678. };
  679. typedef integer_comp_func<short> short_comp_func;
  680. typedef integer_comp_func<int> int_comp_func;
  681. typedef integer_comp_func<long> long_comp_func;
  682. typedef integer_comp_func<int64_t> int64_comp_func;
  683. typedef integer_comp_func<unsigned short> ushort_comp_func;
  684. typedef integer_comp_func<unsigned int> uint_comp_func;
  685. typedef integer_comp_func<unsigned long> ulong_comp_func;
  686. typedef integer_comp_func<uint64_t> uint64_comp_func;
  687. struct float_comp_func
  688. {
  689. bool operator() (float v1, float v2) const
  690. {
  691. float disc = v1 - v2;
  692. if(fabsf(disc) < 1E-5)
  693. return false;
  694. return disc < 0;
  695. }
  696. };
  697. struct double_comp_func
  698. {
  699. bool operator() (double v1, double v2) const
  700. {
  701. double disc = v1 - v2;
  702. if(fabs(disc) < 1E-8)
  703. return false;
  704. return disc < 0;
  705. }
  706. };
  707. template<class T, bool CASE = false>
  708. // T -> (unsigned) char / wchar_t
  709. struct character_comp_func
  710. {
  711. bool operator() (T v1, T v2) const
  712. {
  713. if(!CASE)
  714. {
  715. if(v1 >= 'A' && v1 <= 'Z') v1 += 32;
  716. if(v2 >= 'A' && v2 <= 'Z') v2 += 32;
  717. }
  718. return v1 < v2;
  719. }
  720. };
  721. typedef character_comp_func<char, true> char_case_comp_func;
  722. typedef character_comp_func<unsigned char, true> uchar_case_comp_func;
  723. typedef character_comp_func<wchar_t, true> wchar_case_comp_func;
  724. typedef character_comp_func<char, false> char_ucase_comp_func;
  725. typedef character_comp_func<unsigned char, false> uchar_ucase_comp_func;
  726. typedef character_comp_func<wchar_t, false> wchar_ucase_comp_func;
  727. template<class T, bool CASE = false>
  728. // T -> TCHAR* / CString
  729. struct str_comp_func
  730. {
  731. //比较函数。
  732. bool operator() (const T &A, const T &B) const
  733. {
  734. if(!CASE)
  735. return lstricmp((LPCTSTR)A, (LPCTSTR)B) < 0;
  736. else
  737. return lstrcmp((LPCTSTR)A, (LPCTSTR)B) < 0;
  738. }
  739. };
  740. typedef str_comp_func<LPCTSTR, true> case_tchar_comp_func;
  741. typedef str_comp_func<LPCTSTR, false> uncase_tchar_comp_func;
  742. typedef str_comp_func<CString, true> case_string_comp_func;
  743. typedef str_comp_func<CString, false> uncase_string_comp_func;
  744. typedef case_tchar_comp_func tchar_ptr_case_comp_func;
  745. typedef uncase_tchar_comp_func tchar_ptr_ucase_comp_func;
  746. typedef case_string_comp_func string_case_comp_func;
  747. typedef uncase_string_comp_func string_ucase_comp_func;
  748. /************************************************************************/
  749. /* 排序仿函数 */
  750. /************************************************************************/
  751. template<bool ASC = true>
  752. struct bool_sort_func
  753. {
  754. bool operator() (bool v1, bool v2) const
  755. {
  756. if(v1 == v2)
  757. return false;
  758. bool result = !v1;
  759. return ASC ? result : !result;
  760. }
  761. };
  762. typedef bool_sort_func<true> bool_asc_sort_func;
  763. typedef bool_sort_func<false> bool_desc_sort_func;
  764. template<class T, bool ASC = true>
  765. // T -> (signed / unsigned) short / int / long / int64_t
  766. struct integer_sort_func
  767. {
  768. bool operator() (T v1, T v2) const
  769. {
  770. if(v1 == v2)
  771. return false;
  772. bool result = v1 < v2;
  773. return ASC ? result : !result;
  774. }
  775. };
  776. typedef integer_sort_func<short, true> short_asc_sort_func;
  777. typedef integer_sort_func<unsigned short, true> ushort_asc_sort_func;
  778. typedef integer_sort_func<int, true> int_asc_sort_func;
  779. typedef integer_sort_func<unsigned int, true> uint_asc_sort_func;
  780. typedef integer_sort_func<long, true> long_asc_sort_func;
  781. typedef integer_sort_func<unsigned long, true> ulong_asc_sort_func;
  782. typedef integer_sort_func<int64_t, true> int64_asc_sort_func;
  783. typedef integer_sort_func<uint64_t, true> uint64_asc_sort_func;
  784. typedef integer_sort_func<short, false> short_desc_sort_func;
  785. typedef integer_sort_func<unsigned short, false> ushort_desc_sort_func;
  786. typedef integer_sort_func<int, false> int_desc_sort_func;
  787. typedef integer_sort_func<unsigned int, false> uint_desc_sort_func;
  788. typedef integer_sort_func<long, false> long_desc_sort_func;
  789. typedef integer_sort_func<unsigned long, false> ulong_desc_sort_func;
  790. typedef integer_sort_func<int64_t, false> int64_desc_sort_func;
  791. typedef integer_sort_func<uint64_t, false> uint64_desc_sort_func;
  792. template<bool ASC = true>
  793. struct float_sort_func
  794. {
  795. bool operator() (float v1, float v2) const
  796. {
  797. float disc = v1 - v2;
  798. if(fabsf(disc) < 1E-5)
  799. return false;
  800. bool result = disc < 0;
  801. return ASC ? result : !result;
  802. }
  803. };
  804. typedef float_sort_func<true> float_asc_sort_func;
  805. typedef float_sort_func<false> float_desc_sort_func;
  806. template<bool ASC = true>
  807. struct double_sort_func
  808. {
  809. bool operator() (double v1, double v2) const
  810. {
  811. double disc = v1 - v2;
  812. if(fabs(disc) < 1E-8)
  813. return false;
  814. bool result = disc < 0;
  815. return ASC ? result : !result;
  816. }
  817. };
  818. typedef double_sort_func<true> double_asc_sort_func;
  819. typedef double_sort_func<false> double_desc_sort_func;
  820. template<class T, bool ASC = true, bool CASE = false>
  821. // T -> (unsigned) char / wchar_t
  822. struct character_sort_func
  823. {
  824. bool operator() (T v1, T v2) const
  825. {
  826. if(!CASE)
  827. {
  828. if(v1 >= 'A' && v1 <= 'Z') v1 += 32;
  829. if(v2 >= 'A' && v2 <= 'Z') v2 += 32;
  830. }
  831. if(v1 == v2)
  832. return false;
  833. bool result = v1 < v2;
  834. return ASC ? result : !result;
  835. }
  836. };
  837. typedef character_sort_func<char, true, true> char_asc_case_sort_func;
  838. typedef character_sort_func<unsigned char, true, true> uchar_asc_case_sort_func;
  839. typedef character_sort_func<wchar_t, true, true> wchar_asc_case_sort_func;
  840. typedef character_sort_func<char, true, false> char_asc_ucase_sort_func;
  841. typedef character_sort_func<unsigned char, true, false> uchar_asc_ucase_sort_func;
  842. typedef character_sort_func<wchar_t, true, false> wchar_asc_ucase_sort_func;
  843. typedef character_sort_func<char, false, true> char_desc_case_sort_func;
  844. typedef character_sort_func<unsigned char, false, true> uchar_desc_case_sort_func;
  845. typedef character_sort_func<wchar_t, false, true> wchar_desc_case_sort_func;
  846. typedef character_sort_func<char, false, false> char_desc_ucase_sort_func;
  847. typedef character_sort_func<unsigned char, false, false> uchar_desc_ucase_sort_func;
  848. typedef character_sort_func<wchar_t, false, false> wchar_desc_ucase_sort_func;
  849. template<class T, bool ASC = true, bool CASE = false>
  850. // T -> TCHAR* / CString
  851. struct str_sort_func
  852. {
  853. bool operator() (const T& v1, const T& v2) const
  854. {
  855. bool result;
  856. if(CASE)
  857. {
  858. int v = lstrcmp((LPCTSTR)v1, (LPCTSTR)v2);
  859. if(v == 0)
  860. result = false;
  861. else
  862. result = v < 0;
  863. }
  864. else
  865. {
  866. int v = tstricmp((LPCTSTR)v1, (LPCTSTR)v2);
  867. if(v == 0)
  868. result = false;
  869. else
  870. result = v < 0;
  871. }
  872. return ASC ? result : !result;
  873. }
  874. };
  875. typedef str_sort_func<TCHAR*, true, true> tchar_ptr_asc_case_sort_func;
  876. typedef str_sort_func<CString, true, true> string_asc_case_sort_func;
  877. typedef str_sort_func<TCHAR*, true, false> tchar_ptr_asc_ucase_sort_func;
  878. typedef str_sort_func<CString, true, false> string_asc_ucase_sort_func;
  879. typedef str_sort_func<TCHAR*, false, true> tchar_ptr_desc_case_sort_func;
  880. typedef str_sort_func<CString, false, true> string_desc_case_sort_func;
  881. typedef str_sort_func<TCHAR*, false, false> tchar_ptr_desc_ucase_sort_func;
  882. typedef str_sort_func<CString, false, false> string_desc_ucase_sort_func;
  883. template<typename _IntType = int, typename = enable_if_t<is_integral<_IntType>::value>>
  884. class CRandomIntegralT
  885. {
  886. public:
  887. _IntType Generate() {return dist(gen);}
  888. _IntType operator()() {return Generate();}
  889. public:
  890. CRandomIntegralT(_IntType from, _IntType to) : gen(rd()), dist(from, to)
  891. {
  892. }
  893. private:
  894. random_device rd;
  895. mt19937 gen;
  896. uniform_int_distribution<_IntType> dist;
  897. };
  898. template<typename _RealType = double, typename = enable_if_t<is_floating_point<_RealType>::value>>
  899. class CRandomRealTypeT
  900. {
  901. public:
  902. _RealType Generate() {return dist(gen);}
  903. _RealType operator()() {return Generate();}
  904. public:
  905. CRandomRealTypeT(_RealType from, _RealType to) : gen(rd()), dist(from, to)
  906. {
  907. }
  908. private:
  909. random_device rd;
  910. mt19937 gen;
  911. uniform_real_distribution<_RealType> dist;
  912. };
  913. typedef CRandomIntegralT<int> CRandomInt;
  914. typedef CRandomIntegralT<long> CRandomLong;
  915. typedef CRandomIntegralT<unsigned int> CRandomUint;
  916. typedef CRandomIntegralT<unsigned long> CRandomUlong;
  917. typedef CRandomIntegralT<int64_t> CRandomInt64;
  918. typedef CRandomIntegralT<uint64_t> CRandomUint64;
  919. typedef CRandomRealTypeT<float> CRandomFloat;
  920. typedef CRandomRealTypeT<double> CRandomDouble;