123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- #ifndef _LIBSTL_HPP
- #define _LIBSTL_HPP
- #include <vcl.h>
- #include <math.h>
- #include <map>
- #include <list>
- #include <vector>
- #include <algorithm>
- //#include <functional>
- #ifndef FOR_STL
- #define FOR_STL(_class, _pointer, _list) for(_class _pointer = (_list).GetHead(); !(_list).IsEnd(); _pointer = (_list).GetNext())
- #endif
- #ifndef REV_STL
- #define REV_STL(_class, _pointer, _list) for(_class _pointer = (_list).GetTail(); !(_list).IsBegin(); _pointer = (_list).GetPrev())
- #endif
- #define DB_INIT 0
- #define DB_NEW 1
- #define DB_DELETE 2
- #define DB_UPDATE 3
- class TLockObj
- {
- private:
- CRITICAL_SECTION CS;
- public:
- TLockObj(CRITICAL_SECTION &ACS)
- {
- EnterCriticalSection(&CS);
- }
- ~TLockObj()
- {
- LeaveCriticalSection(&CS);
- }
- };
- namespace LibSTL
- {
- template<class T>
- class LibStrMap
- {
- protected:
- typedef std::map<AnsiString, T*> MapObject;
- typedef std::map<AnsiString, T*>::iterator MapPosition;
- typedef std::map<AnsiString, T*>::reverse_iterator MapRevPosition;
- MapPosition FMapPosition;
- MapRevPosition FMapRevPosition;
- //TCriticalSection *FCS;
- //TRTLCriticalSection CS;
- CRITICAL_SECTION CS;
- public:
- MapObject FMapObject;
- LibStrMap()
- {
- //FCS = new TCriticalSection();
- FMapObject.clear();
- InitializeCriticalSection(&CS);
- };
- virtual ~LibStrMap()
- {
- RemoveAll();
- //if (FCS) delete FCS;
- DeleteCriticalSection(&CS);
- };
- public:
- inline bool Lock()
- {
- EnterCriticalSection(&CS);
- //FCS->Acquire();
- return true;
- };
- inline void UnLock()
- {
- LeaveCriticalSection(&CS);
- //FCS->Release();
- };
- inline bool Push(AnsiString AKey, T* AData)
- {
- //std::pair<map<AnsiString, T*>::iterator, bool> pairTwo;
- std::pair<MapPosition, bool> pairTwo;
- pairTwo = FMapObject.insert(std::make_pair<AnsiString, T*>(AKey, AData));
- // if (pairTwo->second == false) //데이터가 중복으로 들어간것임.
- return pairTwo.second;
- };
- inline T* Find(AnsiString AKey)
- {
- MapPosition it;
- it = FMapObject.find(AKey);
- if (it == FMapObject.end())
- return (T*)NULL;
- return it->second;
- }
- inline void RemoveAll()
- {
- MapPosition it;
- for (it = FMapObject.begin(); it != FMapObject.end(); ++it)
- {
- T* pData = it->second;
- if (pData)
- delete pData;
- }
- FMapObject.clear();
- }
- inline void Clear()
- {
- FMapObject.clear();
- }
- inline bool Remove(AnsiString AKey)
- {
- MapPosition it;
- it = FMapObject.find(AKey);
- if (it != FMapObject.end())
- {
- T* pData = it->second;
- if (pData)
- delete pData;
-
- FMapObject.erase(AKey);
- return true;
- }
- return false;
- }
- inline bool Erase(AnsiString AKey)
- {
- MapPosition it;
- it = FMapObject.find(AKey);
- if (it != FMapObject.end())
- {
- FMapObject.erase(AKey);
- return true;
- }
- return false;
- }
-
- inline int Size()
- {
- return(int)FMapObject.size();
- }
- inline bool IsExist(AnsiString AKey)
- {
- MapPosition it;
- it = FMapObject.find(AKey);
- if (it == FMapObject.end())
- return false;
- return true;
- }
- inline T* GetTail()
- {
- FMapRevPosition = FMapObject.rbegin();
- if (FMapRevPosition == FMapObject.rend())
- return (T*)NULL;
- return FMapRevPosition->second;
- }
- inline T* GetPrev()
- {
- if (FMapRevPosition != FMapObject.rend()) {
- FMapRevPosition++;
- if (FMapRevPosition != FMapObject.rend())
- return FMapRevPosition->second;
- }
- return(T*)NULL;
- }
- inline bool IsBegin()
- {
- return (FMapRevPosition == FMapObject.rend());
- }
- inline T* GetHead()
- {
- FMapPosition = FMapObject.begin();
- if (FMapPosition == FMapObject.end())
- return (T*)NULL;
- return FMapPosition->second;
- }
- inline T* GetNext()
- {
- if (FMapPosition != FMapObject.end()) {
- FMapPosition++;
- if (FMapPosition != FMapObject.end())
- return FMapPosition->second;
- }
- return(T*)NULL;
- }
- inline bool IsEnd()
- {
- return (FMapPosition == FMapObject.end());
- }
- };
- template<class T>
- class LibIntMap
- {
- protected:
- typedef std::map<int, T*> MapObject;
- typedef std::map<int, T*>::iterator MapPosition;
- typedef std::map<int, T*>::reverse_iterator MapRevPosition;
- MapObject FMapObject;
- MapPosition FMapPosition;
- MapRevPosition FMapRevPosition;
- CRITICAL_SECTION CS;
- public:
- LibIntMap()
- {
- FMapObject.clear();
- InitializeCriticalSection(&CS);
- };
- virtual ~LibIntMap()
- {
- RemoveAll();
- DeleteCriticalSection(&CS);
- };
- public:
- inline bool Lock()
- {
- EnterCriticalSection(&CS);
- return true;
- };
- inline void UnLock()
- {
- LeaveCriticalSection(&CS);
- };
- inline bool Push(int AKey, T* AData)
- {
- std::pair<MapPosition, bool> pairTwo;
- pairTwo = FMapObject.insert(std::make_pair<int, T*>(AKey, AData));
- return pairTwo.second;
- };
- inline T* Find(int AKey)
- {
- MapPosition it;
- it = FMapObject.find(AKey);
- if (it == FMapObject.end())
- return (T*)NULL;
- return it->second;
- }
- inline void RemoveAll()
- {
- MapPosition it;
- for (it = FMapObject.begin(); it != FMapObject.end(); ++it)
- {
- T* pData = it->second;
- if (pData)
- delete pData;
- }
- FMapObject.clear();
- }
- inline void Clear()
- {
- FMapObject.clear();
- }
- inline bool Remove(int AKey)
- {
- MapPosition it;
- it = FMapObject.find(AKey);
- if (it != FMapObject.end())
- {
- T* pData = it->second;
- if (pData)
- delete pData;
- FMapObject.erase(AKey);
- return true;
- }
- return false;
- }
- inline bool Erase(int AKey)
- {
- MapPosition it;
- it = FMapObject.find(AKey);
- if (it != FMapObject.end())
- {
- FMapObject.erase(AKey);
- return true;
- }
- return false;
- }
- inline int Size()
- {
- return(int)FMapObject.size();
- }
- inline bool IsExist(int AKey)
- {
- MapPosition it;
- it = FMapObject.find(AKey);
- if (it == FMapObject.end())
- return false;
- return true;
- }
- inline T* GetTail()
- {
- FMapRevPosition = FMapObject.rbegin();
- if (FMapRevPosition == FMapObject.rend())
- return (T*)NULL;
- return FMapRevPosition->second;
- }
- inline T* GetPrev()
- {
- if (FMapRevPosition != FMapObject.rend()) {
- FMapRevPosition++;
- if (FMapRevPosition != FMapObject.rend())
- return FMapRevPosition->second;
- }
- return(T*)NULL;
- }
- inline bool IsBegin()
- {
- return (FMapRevPosition == FMapObject.rend());
- }
- inline T* GetHead()
- {
- FMapPosition = FMapObject.begin();
- if (FMapPosition == FMapObject.end())
- return (T*)NULL;
- return FMapPosition->second;
- }
- inline T* GetNext()
- {
- if (FMapPosition != FMapObject.end()) {
- FMapPosition++;
- if (FMapPosition != FMapObject.end())
- return FMapPosition->second;
- }
- return(T*)NULL;
- }
- inline bool IsEnd()
- {
- return (FMapPosition == FMapObject.end());
- }
- };
- } // namespace
- #define StrMap LibSTL::LibStrMap
- #define IntMap LibSTL::LibIntMap
- #define ListMap LibSTL::LibStrMap
- #endif //_LIBSTL_HPP
|