LibSTLF.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #ifndef _LIBSTL_HPP
  2. #define _LIBSTL_HPP
  3. #include <vcl.h>
  4. #include <math.h>
  5. #include <map>
  6. #include <list>
  7. #include <vector>
  8. #include <algorithm>
  9. //#include <functional>
  10. #ifndef FOR_STL
  11. #define FOR_STL(_class, _pointer, _list) for(_class _pointer = (_list).GetHead(); !(_list).IsEnd(); _pointer = (_list).GetNext())
  12. #endif
  13. #ifndef REV_STL
  14. #define REV_STL(_class, _pointer, _list) for(_class _pointer = (_list).GetTail(); !(_list).IsBegin(); _pointer = (_list).GetPrev())
  15. #endif
  16. #define DB_INIT 0
  17. #define DB_NEW 1
  18. #define DB_DELETE 2
  19. #define DB_UPDATE 3
  20. class TLockObj
  21. {
  22. private:
  23. CRITICAL_SECTION CS;
  24. public:
  25. TLockObj(CRITICAL_SECTION &ACS)
  26. {
  27. EnterCriticalSection(&CS);
  28. }
  29. ~TLockObj()
  30. {
  31. LeaveCriticalSection(&CS);
  32. }
  33. };
  34. namespace LibSTL
  35. {
  36. template<class T>
  37. class LibStrMap
  38. {
  39. protected:
  40. typedef std::map<AnsiString, T*> MapObject;
  41. typedef std::map<AnsiString, T*>::iterator MapPosition;
  42. typedef std::map<AnsiString, T*>::reverse_iterator MapRevPosition;
  43. MapPosition FMapPosition;
  44. MapRevPosition FMapRevPosition;
  45. //TCriticalSection *FCS;
  46. //TRTLCriticalSection CS;
  47. CRITICAL_SECTION CS;
  48. public:
  49. MapObject FMapObject;
  50. LibStrMap()
  51. {
  52. //FCS = new TCriticalSection();
  53. FMapObject.clear();
  54. InitializeCriticalSection(&CS);
  55. };
  56. virtual ~LibStrMap()
  57. {
  58. RemoveAll();
  59. //if (FCS) delete FCS;
  60. DeleteCriticalSection(&CS);
  61. };
  62. public:
  63. inline bool Lock()
  64. {
  65. EnterCriticalSection(&CS);
  66. //FCS->Acquire();
  67. return true;
  68. };
  69. inline void UnLock()
  70. {
  71. LeaveCriticalSection(&CS);
  72. //FCS->Release();
  73. };
  74. inline bool Push(AnsiString AKey, T* AData)
  75. {
  76. //std::pair<map<AnsiString, T*>::iterator, bool> pairTwo;
  77. std::pair<MapPosition, bool> pairTwo;
  78. pairTwo = FMapObject.insert(std::make_pair<AnsiString, T*>(AKey, AData));
  79. // if (pairTwo->second == false) //데이터가 중복으로 들어간것임.
  80. return pairTwo.second;
  81. };
  82. inline T* Find(AnsiString AKey)
  83. {
  84. MapPosition it;
  85. it = FMapObject.find(AKey);
  86. if (it == FMapObject.end())
  87. return (T*)NULL;
  88. return it->second;
  89. }
  90. inline void RemoveAll()
  91. {
  92. MapPosition it;
  93. for (it = FMapObject.begin(); it != FMapObject.end(); ++it)
  94. {
  95. T* pData = it->second;
  96. if (pData)
  97. delete pData;
  98. }
  99. FMapObject.clear();
  100. }
  101. inline void Clear()
  102. {
  103. FMapObject.clear();
  104. }
  105. inline bool Remove(AnsiString AKey)
  106. {
  107. MapPosition it;
  108. it = FMapObject.find(AKey);
  109. if (it != FMapObject.end())
  110. {
  111. T* pData = it->second;
  112. if (pData)
  113. delete pData;
  114. FMapObject.erase(AKey);
  115. return true;
  116. }
  117. return false;
  118. }
  119. inline bool Erase(AnsiString AKey)
  120. {
  121. MapPosition it;
  122. it = FMapObject.find(AKey);
  123. if (it != FMapObject.end())
  124. {
  125. FMapObject.erase(AKey);
  126. return true;
  127. }
  128. return false;
  129. }
  130. inline int Size()
  131. {
  132. return(int)FMapObject.size();
  133. }
  134. inline bool IsExist(AnsiString AKey)
  135. {
  136. MapPosition it;
  137. it = FMapObject.find(AKey);
  138. if (it == FMapObject.end())
  139. return false;
  140. return true;
  141. }
  142. inline T* GetTail()
  143. {
  144. FMapRevPosition = FMapObject.rbegin();
  145. if (FMapRevPosition == FMapObject.rend())
  146. return (T*)NULL;
  147. return FMapRevPosition->second;
  148. }
  149. inline T* GetPrev()
  150. {
  151. if (FMapRevPosition != FMapObject.rend()) {
  152. FMapRevPosition++;
  153. if (FMapRevPosition != FMapObject.rend())
  154. return FMapRevPosition->second;
  155. }
  156. return(T*)NULL;
  157. }
  158. inline bool IsBegin()
  159. {
  160. return (FMapRevPosition == FMapObject.rend());
  161. }
  162. inline T* GetHead()
  163. {
  164. FMapPosition = FMapObject.begin();
  165. if (FMapPosition == FMapObject.end())
  166. return (T*)NULL;
  167. return FMapPosition->second;
  168. }
  169. inline T* GetNext()
  170. {
  171. if (FMapPosition != FMapObject.end()) {
  172. FMapPosition++;
  173. if (FMapPosition != FMapObject.end())
  174. return FMapPosition->second;
  175. }
  176. return(T*)NULL;
  177. }
  178. inline bool IsEnd()
  179. {
  180. return (FMapPosition == FMapObject.end());
  181. }
  182. };
  183. template<class T>
  184. class LibIntMap
  185. {
  186. protected:
  187. typedef std::map<int, T*> MapObject;
  188. typedef std::map<int, T*>::iterator MapPosition;
  189. typedef std::map<int, T*>::reverse_iterator MapRevPosition;
  190. MapObject FMapObject;
  191. MapPosition FMapPosition;
  192. MapRevPosition FMapRevPosition;
  193. CRITICAL_SECTION CS;
  194. public:
  195. LibIntMap()
  196. {
  197. FMapObject.clear();
  198. InitializeCriticalSection(&CS);
  199. };
  200. virtual ~LibIntMap()
  201. {
  202. RemoveAll();
  203. DeleteCriticalSection(&CS);
  204. };
  205. public:
  206. inline bool Lock()
  207. {
  208. EnterCriticalSection(&CS);
  209. return true;
  210. };
  211. inline void UnLock()
  212. {
  213. LeaveCriticalSection(&CS);
  214. };
  215. inline bool Push(int AKey, T* AData)
  216. {
  217. std::pair<MapPosition, bool> pairTwo;
  218. pairTwo = FMapObject.insert(std::make_pair<int, T*>(AKey, AData));
  219. return pairTwo.second;
  220. };
  221. inline T* Find(int AKey)
  222. {
  223. MapPosition it;
  224. it = FMapObject.find(AKey);
  225. if (it == FMapObject.end())
  226. return (T*)NULL;
  227. return it->second;
  228. }
  229. inline void RemoveAll()
  230. {
  231. MapPosition it;
  232. for (it = FMapObject.begin(); it != FMapObject.end(); ++it)
  233. {
  234. T* pData = it->second;
  235. if (pData)
  236. delete pData;
  237. }
  238. FMapObject.clear();
  239. }
  240. inline void Clear()
  241. {
  242. FMapObject.clear();
  243. }
  244. inline bool Remove(int AKey)
  245. {
  246. MapPosition it;
  247. it = FMapObject.find(AKey);
  248. if (it != FMapObject.end())
  249. {
  250. T* pData = it->second;
  251. if (pData)
  252. delete pData;
  253. FMapObject.erase(AKey);
  254. return true;
  255. }
  256. return false;
  257. }
  258. inline bool Erase(int AKey)
  259. {
  260. MapPosition it;
  261. it = FMapObject.find(AKey);
  262. if (it != FMapObject.end())
  263. {
  264. FMapObject.erase(AKey);
  265. return true;
  266. }
  267. return false;
  268. }
  269. inline int Size()
  270. {
  271. return(int)FMapObject.size();
  272. }
  273. inline bool IsExist(int AKey)
  274. {
  275. MapPosition it;
  276. it = FMapObject.find(AKey);
  277. if (it == FMapObject.end())
  278. return false;
  279. return true;
  280. }
  281. inline T* GetTail()
  282. {
  283. FMapRevPosition = FMapObject.rbegin();
  284. if (FMapRevPosition == FMapObject.rend())
  285. return (T*)NULL;
  286. return FMapRevPosition->second;
  287. }
  288. inline T* GetPrev()
  289. {
  290. if (FMapRevPosition != FMapObject.rend()) {
  291. FMapRevPosition++;
  292. if (FMapRevPosition != FMapObject.rend())
  293. return FMapRevPosition->second;
  294. }
  295. return(T*)NULL;
  296. }
  297. inline bool IsBegin()
  298. {
  299. return (FMapRevPosition == FMapObject.rend());
  300. }
  301. inline T* GetHead()
  302. {
  303. FMapPosition = FMapObject.begin();
  304. if (FMapPosition == FMapObject.end())
  305. return (T*)NULL;
  306. return FMapPosition->second;
  307. }
  308. inline T* GetNext()
  309. {
  310. if (FMapPosition != FMapObject.end()) {
  311. FMapPosition++;
  312. if (FMapPosition != FMapObject.end())
  313. return FMapPosition->second;
  314. }
  315. return(T*)NULL;
  316. }
  317. inline bool IsEnd()
  318. {
  319. return (FMapPosition == FMapObject.end());
  320. }
  321. };
  322. } // namespace
  323. #define StrMap LibSTL::LibStrMap
  324. #define IntMap LibSTL::LibIntMap
  325. #define ListMap LibSTL::LibStrMap
  326. #endif //_LIBSTL_HPP