#pragma once //======================================================================================================================= // Group Counter /** @author Cho Jae Sik < jscho@webzen.co.kr > @since 2007. 8. 15( ±¤º¹Àý... T.T ) @remarks - ±×·ì(Ű) °ªº°·Î ÀÚµ¿À¸·Î Ä«¿îÆ® À¯Áö ¹× °ü¸® @ToDo - CountType¿¡´Â ÀÏ¹Ý ÀÚ·áÇü¸¸ ¿Ã¼ö ÀÖ´Ù. ³»ºÎÀûÀ¸·Î óÀ½»ç¿ë½Ã ÃʱâÈ­ ½Ãų¶§ 0À¸·Î ÃʱâÈ­ ½ÃŲ´Ù. - GetCount() ÇÔ¼ö´Â ÇØ´ç ۰ªÀÌ Á¸ÀçÇÏÁö ¾ÊÀ¸¸é 0¸¦ ¸®ÅÏÇÑ´Ù. @example - GroupCounter< DWORD, DWORD > Counter; Counter.Increse( 1 ); @History - 2007³â 8¿ù 15ÀÏ ÃÖÃÊ ¸±¸®Áî.. */ //======================================================================================================================= #include template< class KeyType, class CountType > class GroupCounter { public: GroupCounter(); ~GroupCounter(); public: void Increse( KeyType Key ); void Decrese( KeyType Key ); CountType GetCount( KeyType Key ); private: CountType* Find( KeyType Key ); void Release(); private: std::map< KeyType, CountType* > m_mapGroupCounter; }; template< class KeyType, class CountType > GroupCounter< KeyType, CountType >::GroupCounter() { m_mapGroupCounter.clear(); } template< class KeyType, class CountType > GroupCounter< KeyType, CountType >::~GroupCounter() { Release(); } template< class KeyType, class CountType > void GroupCounter< KeyType, CountType >::Release() { std::map< KeyType, CountType* >::iterator iter; for( iter = m_mapGroupCounter.begin(); iter != m_mapGroupCounter.end(); ++iter) { CountType* pCount = iter->second; if( pCount ) delete pCount; } m_mapGroupCounter.clear(); } template< class KeyType, class CountType > CountType* GroupCounter< KeyType, CountType >::Find( KeyType Key ) { std::map< KeyType, CountType* >::iterator iter; iter = m_mapGroupCounter.find( Key ); if( iter != m_mapGroupCounter.end() ) return iter->second; return NULL; } template< class KeyType, class CountType > void GroupCounter< KeyType, CountType >::Increse( KeyType Key ) { CountType* pCount = Find( Key ); if( pCount ) { (*pCount)++; return; } pCount = new CountType; (*pCount) = 0; (*pCount)++; m_mapGroupCounter.insert( std::make_pair( Key, pCount ) ); return; } template< class KeyType, class CountType > void GroupCounter< KeyType, CountType >::Decrese( KeyType Key ) { CountType* pCount = Find( Key ); if( pCount ) { if( (*pCount) == 0 ) //Ä«¿îÅͰ¡ 0ÀÌ¸é °¨¼Ò½Ãų¼ö ¾ø´Ù. ¼Ò¸ê½ÃÄÑ¾ß Çϴ°¡? return; (*pCount)--; return; } return; } template< class KeyType, class CountType > CountType GroupCounter< KeyType, CountType >::GetCount( KeyType Key ) { CountType* pCount = Find( Key ); if( pCount ) return (*pCount); return 0; }