00001 #ifndef DOTYPETABLE_HPP
00002 #define DOTYPETABLE_HPP
00003
00004
00005
00006
00007
00008 #include "DoType.h"
00009
00010 template<class T, int N>
00011 struct DynObjTypeTableBase {
00012 DynObjTypeTableBase() { IterReset(); }
00013 enum { TableSize=1<<N };
00014
00015 ExpArrP<T*,short> _types[TableSize];
00016
00017 void Insert( T* pdt ){
00018 int ix = Hash(GetTypeLookupName(pdt)) & (TableSize-1);
00019 _types[ix].Push(pdt);
00020 }
00021
00022 T* Lookup( const char* type ){
00023 int ix = Hash(type) & (TableSize-1);
00024 ExpArrP<T*,short>& slot = _types[ix];
00025 for( int jx=slot.Size()-1; jx>=0; jx-- ){
00026 if( !strcmp(GetTypeLookupName(slot[jx]),type) )
00027 return slot[jx];
00028 }
00029 return NULL;
00030 }
00031
00032 T* Remove( const char* type ){
00033 int ix = Hash(type) & (TableSize-1);
00034 int n_remove = 0;
00035 ExpArrP<T*,short>& slot = _types[ix];
00036 for( int jx=slot.Size()-1; jx>=0; jx-- ){
00037 if( !strcmp(GetTypeLookupName(slot[jx]),type) ){
00038 T * pdt = slot.RemoveIndex(jx);
00039 n_remove++;
00040 return pdt;
00041 }
00042 }
00043 return NULL;
00044
00045 }
00046
00047
00048
00049 short m_it_line, m_it_pos;
00050 int m_it_ix;
00051
00052 void IterReset( ){
00053 m_it_line = 0;
00054 m_it_pos = -1;
00055 m_it_ix = -1;
00056 }
00057
00058 bool IterStep( ){
00059 if( m_it_line>=TableSize ) return false;
00060 while( ++m_it_pos>=_types[m_it_line].Size() ){
00061 m_it_pos = -1;
00062 if( ++m_it_line>=TableSize )
00063 return false;
00064 }
00065 m_it_ix++;
00066 return true;
00067 }
00068
00069 T* operator [] (int ix ){
00070 if( ix<m_it_ix )
00071 IterReset();
00072 while( m_it_ix<ix )
00073 if( !IterStep() )
00074 return NULL;
00075 return _types[m_it_line][m_it_pos];
00076 }
00077
00078 static int Hash( const char *ps ){
00079 if( !ps ) return 0;
00080 int sum = 0;
00081 int sl = (int)strlen(ps);
00082 int p0 = 4*(sl>>2);
00083 const char* pe = ps + p0;
00084 while( ps<pe ){
00085 sum += *(int*)ps;
00086 ps += 4;
00087 }
00088 while( sl&3 ){
00089 sum += *ps++;
00090 sl--;
00091 }
00092 return sum;
00093 }
00094
00095
00096 const char* GetTypeLookupName( DynObjType *pdt ){
00097
00098 return pdt->_type;
00099 }
00100
00101
00102 const char* GetTypeLookupName( DoBaseNode* pbn ){
00103
00104 DO_ASSERT_MSG(pbn && pbn->Unresolved(), "DynObjBaseTable::GetTypeLookupName - Should have unresolved DoBaseNode");
00105 return pbn->_base_type;
00106 }
00107
00108 };
00109
00110 template <int N>
00111 struct DynObjTypeTable : public DynObjTypeTableBase<DynObjType,N> {
00112
00113 };
00114
00115 template <int N>
00116 struct DynObjBaseTable : public DynObjTypeTableBase<DoBaseNode,N> {
00117
00118 };
00119
00120 #endif // DOTYPETABLE_HPP