STLHelper.h 32 KB

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