STLHelper.h 32 KB

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