STLHelper.h 27 KB

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