00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
00046
00047 class EventWheel
00048 {
00049
00050 protected:
00051 typedef EventPtr ListNode;
00052 typedef EventList ListType;
00053
00054
00055 std::vector<ListType> m_event;
00056
00057
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
00092 class EventWheel
00093 {
00094
00095 protected:
00096 typedef EventPtr ListNode;
00097 typedef EventList ListType;
00098
00099
00100 std::vector<ListType*> m_event;
00101
00102
00103 int m_size;
00104
00105
00106 class EventFreeList
00107 {
00108
00109 public:
00110 pool_vector<ListType*> m_eventFreeList;
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
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
00206
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
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
00231 class EventWheel
00232 {
00233
00234 protected:
00235 typedef EventPtr ListNode;
00236 typedef EventList ListType;
00237
00238
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
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
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
00277
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
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