src/Sim/Foundation/Event/EventList/EventWheel.h

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 //
00033 // Event wheel
00034 //
00035 
00036 #ifndef SIM_FOUNDATION_EVENT_EVENT_WHEEL_H
00037 #define SIM_FOUNDATION_EVENT_EVENT_WHEEL_H
00038 
00039 #include "Sim/Foundation/Event/EventList/EventList.h"
00040 
00041 namespace Onikiri
00042 {
00043 #if 1
00044 
00045     // iC[uタ
00046     // ListType z
00047     class EventWheel
00048     {
00049 
00050     protected:
00051         typedef EventPtr  ListNode;
00052         typedef EventList ListType;
00053 
00054         // eTCNpCxg
00055         std::vector<ListType> m_event;
00056 
00057         // mTCY
00058         int m_size;
00059 
00060     public:
00061 
00062         EventWheel()
00063         {
00064             m_size = 0;
00065         }
00066 
00067         void Resize( int size )
00068         {
00069             m_size = size;
00070             m_event.resize( m_size, ListType() );
00071         }
00072 
00073         INLINE ListType* PeekEventList( int index )
00074         {
00075             return &m_event[index];
00076         }
00077 
00078         INLINE ListType* GetEventList( int index )
00079         {
00080             return &m_event[index];
00081         }
00082 
00083         INLINE void ReleaseEventList( ListType* list, int index )
00084         {
00085         }
00086 
00087     };
00088 
00089 #elif 1
00090 
00091     // t[Xgpe
00092     class EventWheel
00093     {
00094 
00095     protected:
00096         typedef EventPtr  ListNode;
00097         typedef EventList ListType;
00098 
00099         // eTCNpCxg
00100         std::vector<ListType*> m_event;
00101 
00102         // mTCY
00103         int m_size;
00104 
00105         // t[Xg
00106         class EventFreeList
00107         {
00108 
00109         public:
00110             pool_vector<ListType*> m_eventFreeList; // |C^t[XgiX^bNj
00111 
00112 
00113             EventFreeList()
00114             {
00115                 m_clean = true;
00116             }
00117 
00118             ~EventFreeList()
00119             {
00120                 for( size_t i = 0; i < m_eventFreeList.size(); ++i ){
00121                     delete m_eventFreeList[i];
00122                 }
00123                 m_eventFreeList.clear();
00124             }
00125 
00126             EventFreeList( const EventFreeList& ref )
00127             {
00128                 ASSERT( 0 );
00129             }
00130 
00131             EventFreeList& operator=( const EventFreeList &ref )
00132             {
00133                 ASSERT( 0 );
00134                 return(*this);
00135             }
00136 
00137             INLINE ListType* Allocate()
00138             {
00139                 m_clean = false;
00140                 if( m_eventFreeList.size() == 0 ){
00141                     return new ListType();
00142                 }
00143 
00144                 ListType* eventList = m_eventFreeList.back();
00145                 m_eventFreeList.pop_back();
00146                 return eventList;
00147             }
00148 
00149             INLINE void Free( ListType* eventList )
00150             {
00151                 m_eventFreeList.push_back( eventList );
00152             }
00153 
00154         protected:
00155             bool m_clean;
00156         };
00157 
00158         EventFreeList m_eventFreeList;
00159         EventFreeList& FreeList()
00160         {
00161             return m_eventFreeList;
00162         }
00163         
00164     public:
00165 
00166         EventWheel( const EventWheel& ref )
00167         {
00168             ASSERT( 0 );
00169         }
00170 
00171         EventWheel& operator=( const EventWheel &ref )
00172         {
00173             ASSERT( 0 );
00174             return(*this);
00175         }
00176 
00177         EventWheel()
00178         {
00179             m_size = 0;
00180         }
00181 
00182         ~EventWheel()
00183         {
00184             for( size_t i = 0; i < m_event.size(); ++i ){
00185                 if( m_event[i] ){
00186                     FreeList().Free( m_event[i] );
00187                     m_event[i] = NULL;
00188                 }
00189             }
00190         }
00191 
00192         void Resize( int size )
00193         {
00194             m_size = size;
00195             m_event.resize( m_size, NULL );
00196         }
00197 
00198         // |C^Cms
00199         INLINE ListType* PeekEventList( int index ) const
00200         {
00201             ASSERT( index < m_size, "The passed index is out of range." );
00202             return m_event[index];
00203         }
00204 
00205         // Xg
00206         // mCXgm
00207         INLINE ListType* GetEventList( int index )
00208         {
00209             ASSERT( index < m_size, "The passed index is out of range." );
00210             ListType* eventList = m_event[index];
00211             if( !eventList ){
00212                 eventList = FreeList().Allocate();
00213                 m_event[index] = eventList;
00214             }
00215             return eventList;
00216         }
00217 
00218         // Xgp
00219         INLINE void ReleaseEventList( ListType* list, int index )
00220         {
00221             ASSERT( m_event[index], "A not allocated list is released." );
00222             m_event[index] = NULL;
00223             FreeList().Free( list );
00224         }
00225 
00226     };
00227 
00228 #elif 1
00229 
00230     // nbVgp
00231     class EventWheel
00232     {
00233 
00234     protected:
00235         typedef EventPtr  ListNode;
00236         typedef EventList ListType;
00237 
00238         // eTCNpCxg
00239         class EventHash
00240         {
00241         public:
00242             size_t operator()(const int value) const
00243             {
00244                 return (size_t)(value ^ (value >> 8));
00245             }
00246         };
00247 
00248         pool_unordered_map<int, ListType, EventHash> m_event;
00249 
00250         // mTCY
00251         int m_size;
00252 
00253     public:
00254 
00255         EventWheel()
00256         {
00257             m_size = 0;
00258         }
00259 
00260         ~EventWheel()
00261         {
00262         }
00263         void Resize( int size )
00264         {
00265             m_size = size;
00266         }
00267 
00268         // |C^Cms
00269         INLINE ListType* PeekEventList( int index ) 
00270         {
00271             ASSERT( index < m_size, "The passed index is out of range." );
00272             pool_unordered_map<int, ListType, EventHash>::iterator i = m_event.find(index);
00273             return i == m_event.end() ? NULL : &i->second;
00274         }
00275 
00276         // Xg
00277         // mCXgm
00278         INLINE ListType* GetEventList( int index )
00279         {
00280             ASSERT( index < m_size, "The passed index is out of range." );
00281             pool_unordered_map<int, ListType, EventHash>::iterator i = m_event.find(index);
00282             if( i == m_event.end() ){
00283                 m_event[index] = EventList();
00284                 return &m_event[index];
00285             }
00286             return &i->second;
00287         }
00288 
00289         // Xgp
00290         INLINE void ReleaseEventList( ListType* list, int index )
00291         {
00292             m_event.erase(index);
00293         }
00294 
00295     };
00296 #endif
00297 }
00298 
00299 
00300 #endif
00301 

Onikiri2に対してTue Jun 18 14:34:23 2013に生成されました。  doxygen 1.4.7