mirror of
https://github.com/openmoh/openmohaa.git
synced 2025-04-28 21:57:57 +03:00
Reworked con_set and con_arrayset so that they have their own Entry class
This commit is contained in:
parent
03143d1341
commit
b1753df1ed
8 changed files with 459 additions and 185 deletions
|
@ -348,7 +348,7 @@ void con_set< key, value >::Archive
|
|||
Archiver& arc
|
||||
)
|
||||
{
|
||||
Entry< key, value > *e;
|
||||
Entry *e;
|
||||
int hash;
|
||||
int i;
|
||||
|
||||
|
@ -361,18 +361,17 @@ void con_set< key, value >::Archive
|
|||
{
|
||||
if( tableLength != 1 )
|
||||
{
|
||||
table = new Entry< key, value > *[ tableLength ]();
|
||||
memset( table, 0, tableLength * sizeof( Entry< key, value > * ) );
|
||||
table = new Entry *[ tableLength ]();
|
||||
memset( table, 0, tableLength * sizeof( Entry * ) );
|
||||
}
|
||||
|
||||
for( i = 0; i < count; i++ )
|
||||
{
|
||||
e = new Entry< key, value >;
|
||||
e = new Entry;
|
||||
e->Archive( arc );
|
||||
|
||||
hash = HashCode< key >( e->key ) % tableLength;
|
||||
|
||||
e->index = i;
|
||||
e->next = table[ hash ];
|
||||
table[ hash ] = e;
|
||||
}
|
||||
|
|
|
@ -238,7 +238,8 @@ random()
|
|||
inline float G_Random(float n)
|
||||
|
||||
{
|
||||
return G_Random() * n;
|
||||
//return G_Random() * n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -254,7 +255,8 @@ crandom()
|
|||
inline float G_CRandom(void)
|
||||
|
||||
{
|
||||
return G_Random(2) - 1;
|
||||
//return G_Random(2) - 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -163,7 +163,7 @@ StateScript::StateScript()
|
|||
}
|
||||
|
||||
template<>
|
||||
void Entry< const_str, script_label_t >::Archive( Archiver& arc )
|
||||
void con_set< const_str, script_label_t >::Entry::Archive( Archiver& arc )
|
||||
{
|
||||
unsigned int offset;
|
||||
|
||||
|
@ -439,7 +439,7 @@ __exec:
|
|||
}
|
||||
|
||||
template<>
|
||||
void Entry < unsigned char *, sourceinfo_t >::Archive( Archiver& arc )
|
||||
void con_set< unsigned char *, sourceinfo_t >::Entry::Archive( Archiver& arc )
|
||||
{
|
||||
unsigned int offset;
|
||||
|
||||
|
|
|
@ -1144,7 +1144,7 @@ void ScriptMaster::PrintThread(int iThreadNum)
|
|||
status += "(none)\n";
|
||||
} else {
|
||||
con_set_enum<const_str, ConList> en = *vm->m_Thread->m_WaitForList;
|
||||
Entry<const_str, ConList> *entry;
|
||||
con_set<const_str, ConList>::Entry *entry;
|
||||
int i = 0;
|
||||
|
||||
for (entry = en.NextElement(); entry != NULL; entry = en.NextElement()) {
|
||||
|
|
|
@ -22,24 +22,67 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
|
||||
// con_arrayset.h: con_set with an index table
|
||||
|
||||
#ifndef __CON_ARRAYSET_H__
|
||||
#define __CON_ARRAYSET_H__
|
||||
#pragma once
|
||||
|
||||
#include "mem_blockalloc.h"
|
||||
|
||||
template< typename key, typename value >
|
||||
class con_arrayset_enum;
|
||||
|
||||
template< typename k, typename v >
|
||||
class con_arrayset : public con_set< k, v >
|
||||
{
|
||||
friend class con_arrayset_enum < k, v >;
|
||||
|
||||
public:
|
||||
class Entry
|
||||
{
|
||||
public:
|
||||
k key;
|
||||
v value;
|
||||
unsigned int index;
|
||||
|
||||
Entry* next;
|
||||
|
||||
public:
|
||||
void* operator new(size_t size);
|
||||
void operator delete(void* ptr);
|
||||
|
||||
Entry();
|
||||
|
||||
#ifdef ARCHIVE_SUPPORTED
|
||||
void Archive(Archiver& arc);
|
||||
#endif
|
||||
};
|
||||
|
||||
public:
|
||||
static MEM_BlockAlloc<Entry> Entry_allocator;
|
||||
|
||||
private:
|
||||
Entry< k, v > **reverseTable; // the index table
|
||||
Entry **table; // hashtable
|
||||
unsigned int tableLength;
|
||||
unsigned int threshold;
|
||||
unsigned int count; // num of entries
|
||||
short unsigned int tableLengthIndex;
|
||||
Entry *defaultEntry;
|
||||
Entry **reverseTable; // the index table
|
||||
|
||||
protected:
|
||||
virtual Entry< k, v > *addNewKeyEntry( const k& key );
|
||||
Entry *findKeyEntry(const k& key) const;
|
||||
Entry *addKeyEntry(const k& key);
|
||||
Entry *addNewKeyEntry( const k& key );
|
||||
|
||||
public:
|
||||
con_arrayset();
|
||||
~con_arrayset();
|
||||
|
||||
virtual void clear();
|
||||
virtual void resize( int count );
|
||||
|
||||
#ifdef ARCHIVE_SUPPORTED
|
||||
void Archive(Archiver& arc);
|
||||
#endif
|
||||
|
||||
void clear();
|
||||
void resize(int count = 0);
|
||||
|
||||
unsigned int findKeyIndex( const k& key );
|
||||
unsigned int addKeyIndex( const k& key );
|
||||
|
@ -49,29 +92,106 @@ public:
|
|||
v& operator[]( unsigned int index );
|
||||
};
|
||||
|
||||
template< typename k, typename v >
|
||||
MEM_BlockAlloc<typename con_arrayset<k, v>::Entry> con_arrayset< k, v >::Entry_allocator;
|
||||
|
||||
template< typename k, typename v >
|
||||
void *con_arrayset<k, v>::Entry::operator new( size_t size )
|
||||
{
|
||||
return con_arrayset< k, v >::Entry_allocator.Alloc();
|
||||
}
|
||||
|
||||
template< typename k, typename v >
|
||||
void con_arrayset<k, v>::Entry::operator delete( void *ptr )
|
||||
{
|
||||
con_arrayset< k, v >::Entry_allocator.Free( ptr );
|
||||
}
|
||||
|
||||
template< typename k, typename v >
|
||||
con_arrayset<k, v>::Entry::Entry()
|
||||
{
|
||||
this->key = k();
|
||||
this->value = v();
|
||||
|
||||
index = 0;
|
||||
next = NULL;
|
||||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
con_arrayset<key, value>::con_arrayset()
|
||||
{
|
||||
tableLength = 1;
|
||||
table = &defaultEntry;
|
||||
|
||||
threshold = 1;
|
||||
count = 0;
|
||||
tableLengthIndex = 0;
|
||||
|
||||
defaultEntry = NULL;
|
||||
reverseTable = ( &this->defaultEntry ) - 1;
|
||||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
con_arrayset<key, value>::~con_arrayset()
|
||||
{
|
||||
|
||||
clear();
|
||||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
void con_arrayset<key, value>::resize( int count )
|
||||
{
|
||||
Entry< key, value > **oldReverseTable = reverseTable;
|
||||
unsigned int oldTableLength = this->tableLength;
|
||||
unsigned int i;
|
||||
Entry **oldReverseTable = reverseTable;
|
||||
Entry **oldTable = table;
|
||||
Entry *e, *old;
|
||||
unsigned int index;
|
||||
unsigned int oldTableLength = tableLength;
|
||||
unsigned int i;
|
||||
|
||||
con_set< key, value >::resize( count );
|
||||
if( count > 0 )
|
||||
{
|
||||
tableLength += count;
|
||||
threshold = tableLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
//threshold = ( unsigned int )( ( float )tableLength * 0.75f );
|
||||
threshold = ( unsigned int )( ( float )tableLength * 0.75 );
|
||||
if( threshold < 1 )
|
||||
{
|
||||
threshold = 1;
|
||||
}
|
||||
|
||||
tableLength += threshold;
|
||||
}
|
||||
|
||||
// allocate a new table
|
||||
table = new Entry *[ tableLength ]();
|
||||
memset( table, 0, tableLength * sizeof( Entry * ) );
|
||||
|
||||
// rehash the table
|
||||
for( i = oldTableLength; i > 0; i-- )
|
||||
{
|
||||
// rehash all entries from the old table
|
||||
for( e = oldTable[ i - 1 ]; e != NULL; e = old )
|
||||
{
|
||||
old = e->next;
|
||||
|
||||
// insert the old entry to the table hashindex
|
||||
index = HashCode< key >( e->key ) % tableLength;
|
||||
|
||||
e->next = table[ index ];
|
||||
table[ index ] = e;
|
||||
}
|
||||
}
|
||||
|
||||
if( oldTableLength > 1 )
|
||||
{
|
||||
// delete the previous table
|
||||
delete[] oldTable;
|
||||
}
|
||||
|
||||
// allocate a bigger reverse table
|
||||
reverseTable = ( new Entry< key, value > *[ this->tableLength ]() ) - 1;
|
||||
reverseTable = ( new Entry *[ this->tableLength ]() ) - 1;
|
||||
|
||||
for( i = 1; i <= oldTableLength; i++ )
|
||||
{
|
||||
|
@ -88,6 +208,10 @@ void con_arrayset<key, value>::resize( int count )
|
|||
template< typename key, typename value >
|
||||
void con_arrayset<key, value>::clear()
|
||||
{
|
||||
Entry* entry = NULL;
|
||||
Entry* next = NULL;
|
||||
unsigned int i;
|
||||
|
||||
if( this->tableLength > 1 )
|
||||
{
|
||||
reverseTable++;
|
||||
|
@ -95,18 +219,94 @@ void con_arrayset<key, value>::clear()
|
|||
reverseTable = ( &this->defaultEntry ) - 1;
|
||||
}
|
||||
|
||||
con_set< key, value >::clear();
|
||||
for( i = 0; i < tableLength; i++ )
|
||||
{
|
||||
for( entry = table[ i ]; entry != NULL; entry = next )
|
||||
{
|
||||
next = entry->next;
|
||||
delete entry;
|
||||
}
|
||||
}
|
||||
|
||||
if( tableLength > 1 )
|
||||
{
|
||||
delete[] table;
|
||||
}
|
||||
|
||||
tableLength = 1;
|
||||
table = &defaultEntry;
|
||||
|
||||
threshold = 1;
|
||||
count = 0;
|
||||
tableLengthIndex = 0;
|
||||
|
||||
defaultEntry = NULL;
|
||||
}
|
||||
|
||||
template< typename k, typename v >
|
||||
typename con_arrayset< k, v >::Entry* con_arrayset< k, v >::findKeyEntry(const k& key) const
|
||||
{
|
||||
Entry* entry;
|
||||
|
||||
entry = table[HashCode< k >(key) % tableLength];
|
||||
|
||||
for (; entry != NULL; entry = entry->next)
|
||||
{
|
||||
if (entry->key == key) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
template< typename k, typename v >
|
||||
Entry< k, v > *con_arrayset< k, v >::addNewKeyEntry( const k& key )
|
||||
typename con_arrayset< k, v >::Entry* con_arrayset< k, v >::addKeyEntry(const k& key)
|
||||
{
|
||||
Entry< k, v > *entry = con_set< k, v >::addNewKeyEntry( key );
|
||||
Entry* entry;
|
||||
|
||||
entry->index = this->count;
|
||||
entry = findKeyEntry(key);
|
||||
|
||||
reverseTable[ this->count ] = entry;
|
||||
if (entry != NULL) {
|
||||
return entry;
|
||||
}
|
||||
else {
|
||||
return addNewKeyEntry(key);
|
||||
}
|
||||
}
|
||||
|
||||
template< typename k, typename v >
|
||||
typename con_arrayset<k, v>::Entry *con_arrayset< k, v >::addNewKeyEntry( const k& key )
|
||||
{
|
||||
Entry* entry;
|
||||
int index;
|
||||
|
||||
if (count >= threshold)
|
||||
{
|
||||
resize();
|
||||
}
|
||||
|
||||
index = HashCode< k >(key) % tableLength;
|
||||
|
||||
count++;
|
||||
|
||||
entry = new Entry;
|
||||
|
||||
if (defaultEntry == NULL)
|
||||
{
|
||||
defaultEntry = entry;
|
||||
entry->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
entry->next = table[index];
|
||||
}
|
||||
|
||||
entry->key = key;
|
||||
entry->index = count;
|
||||
|
||||
table[index] = entry;
|
||||
reverseTable[ count ] = entry;
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
@ -114,7 +314,7 @@ Entry< k, v > *con_arrayset< k, v >::addNewKeyEntry( const k& key )
|
|||
template< typename k, typename v >
|
||||
unsigned int con_arrayset< k, v >::addKeyIndex( const k& key )
|
||||
{
|
||||
Entry< k, v > *entry = this->addKeyEntry( key );
|
||||
Entry *entry = this->addKeyEntry( key );
|
||||
|
||||
return entry->index;
|
||||
}
|
||||
|
@ -122,7 +322,7 @@ unsigned int con_arrayset< k, v >::addKeyIndex( const k& key )
|
|||
template< typename k, typename v >
|
||||
unsigned int con_arrayset< k, v >::addNewKeyIndex( const k& key )
|
||||
{
|
||||
Entry< k, v > *entry = this->addNewKeyEntry( key );
|
||||
Entry *entry = this->addNewKeyEntry( key );
|
||||
|
||||
return entry->index;
|
||||
}
|
||||
|
@ -130,7 +330,7 @@ unsigned int con_arrayset< k, v >::addNewKeyIndex( const k& key )
|
|||
template< typename k, typename v >
|
||||
unsigned int con_arrayset< k, v >::findKeyIndex( const k& key )
|
||||
{
|
||||
Entry< k, v > *entry = this->findKeyEntry( key );
|
||||
Entry *entry = this->findKeyEntry( key );
|
||||
|
||||
if( entry != NULL ) {
|
||||
return entry->index;
|
||||
|
@ -162,4 +362,91 @@ value& con_arrayset< key, value >::operator[]( unsigned int index )
|
|||
return reverseTable[ index ]->key;
|
||||
}
|
||||
|
||||
#endif /* __CON_ARRAYSET_H__ */
|
||||
template<typename key, typename value >
|
||||
class con_arrayset_enum
|
||||
{
|
||||
friend class con_map_enum < key, value >;
|
||||
|
||||
public:
|
||||
using Entry = typename con_arrayset<key, value>::Entry;
|
||||
|
||||
protected:
|
||||
con_arrayset< key, value >* m_Set;
|
||||
unsigned int m_Index;
|
||||
Entry* m_CurrentEntry;
|
||||
Entry* m_NextEntry;
|
||||
|
||||
public:
|
||||
|
||||
con_arrayset_enum();
|
||||
con_arrayset_enum(con_arrayset< key, value >& set );
|
||||
|
||||
bool operator=(con_arrayset< key, value >& set);
|
||||
|
||||
Entry *NextElement( void );
|
||||
Entry *CurrentElement( void );
|
||||
};
|
||||
|
||||
template< typename key, typename value >
|
||||
con_arrayset_enum< key, value >::con_arrayset_enum()
|
||||
{
|
||||
m_Set = NULL;
|
||||
m_Index = 0;
|
||||
m_CurrentEntry = NULL;
|
||||
m_NextEntry = NULL;
|
||||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
con_arrayset_enum< key, value >::con_arrayset_enum( con_arrayset< key, value > &set )
|
||||
{
|
||||
*this = set;
|
||||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
bool con_arrayset_enum< key, value >::operator=(con_arrayset< key, value > &set )
|
||||
{
|
||||
m_Set = &set;
|
||||
m_Index = m_Set->tableLength;
|
||||
m_CurrentEntry = NULL;
|
||||
m_NextEntry = NULL;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
typename con_arrayset_enum< key, value >::Entry* con_arrayset_enum< key, value >::CurrentElement( void )
|
||||
{
|
||||
return m_CurrentEntry;
|
||||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
typename con_arrayset_enum< key, value >::Entry* con_arrayset_enum< key, value >::NextElement( void )
|
||||
{
|
||||
if( !m_NextEntry )
|
||||
{
|
||||
while( 1 )
|
||||
{
|
||||
if( !m_Index ) {
|
||||
break;
|
||||
}
|
||||
|
||||
m_Index--;
|
||||
m_NextEntry = m_Set->table[ m_Index ];
|
||||
|
||||
if( m_NextEntry ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( !m_NextEntry )
|
||||
{
|
||||
m_CurrentEntry = NULL;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
m_CurrentEntry = m_NextEntry;
|
||||
m_NextEntry = m_NextEntry->next;
|
||||
|
||||
return m_CurrentEntry;
|
||||
}
|
||||
|
|
|
@ -22,8 +22,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
|
||||
// con_set.h: C++ map/set classes. Contains an hash table to improve the speed of finding a key
|
||||
|
||||
#ifndef __CON_SET_H__
|
||||
#define __CON_SET_H__
|
||||
#pragma once
|
||||
|
||||
#include "mem_blockalloc.h"
|
||||
|
||||
|
@ -42,47 +41,46 @@ struct con_set_is_pointer { static const bool con_value = false; };
|
|||
template<typename T>
|
||||
struct con_set_is_pointer<T*> { static const bool con_value = true; };
|
||||
|
||||
template< typename k, typename v >
|
||||
class Entry
|
||||
{
|
||||
public:
|
||||
k key;
|
||||
v value;
|
||||
unsigned int index;
|
||||
|
||||
Entry *next;
|
||||
|
||||
public:
|
||||
//void *operator new( size_t size );
|
||||
//void operator delete( void *ptr );
|
||||
|
||||
Entry();
|
||||
|
||||
#ifdef ARCHIVE_SUPPORTED
|
||||
void Archive( Archiver& arc );
|
||||
#endif
|
||||
};
|
||||
|
||||
template< typename k, typename v >
|
||||
class con_set
|
||||
{
|
||||
friend class con_set_enum < k, v > ;
|
||||
|
||||
public:
|
||||
static MEM_BlockAlloc<Entry<k, v >> Entry_allocator;
|
||||
class Entry
|
||||
{
|
||||
public:
|
||||
k key;
|
||||
v value;
|
||||
|
||||
Entry* next;
|
||||
|
||||
public:
|
||||
void* operator new(size_t size);
|
||||
void operator delete(void* ptr);
|
||||
|
||||
Entry();
|
||||
|
||||
#ifdef ARCHIVE_SUPPORTED
|
||||
void Archive(Archiver& arc);
|
||||
#endif
|
||||
};
|
||||
|
||||
public:
|
||||
static MEM_BlockAlloc<Entry> Entry_allocator;
|
||||
|
||||
protected:
|
||||
Entry< k, v > **table; // hashtable
|
||||
Entry **table; // hashtable
|
||||
unsigned int tableLength;
|
||||
unsigned int threshold;
|
||||
unsigned int count; // num of entries
|
||||
short unsigned int tableLengthIndex;
|
||||
Entry< k, v > *defaultEntry;
|
||||
Entry *defaultEntry;
|
||||
|
||||
protected:
|
||||
Entry< k, v > *findKeyEntry( const k& key ) const;
|
||||
Entry< k, v > *addKeyEntry( const k& key );
|
||||
virtual Entry< k, v > *addNewKeyEntry( const k& key );
|
||||
Entry *findKeyEntry( const k& key ) const;
|
||||
Entry *addKeyEntry( const k& key );
|
||||
Entry *addNewKeyEntry( const k& key );
|
||||
|
||||
public:
|
||||
con_set();
|
||||
|
@ -92,8 +90,8 @@ public:
|
|||
void Archive( Archiver& arc );
|
||||
#endif
|
||||
|
||||
virtual void clear();
|
||||
virtual void resize( int count = 0 );
|
||||
void clear();
|
||||
void resize( int count = 0 );
|
||||
|
||||
v *findKeyValue( const k& key ) const;
|
||||
k *firstKeyValue();
|
||||
|
@ -108,111 +106,30 @@ public:
|
|||
unsigned int size();
|
||||
};
|
||||
|
||||
template< typename key, typename value >
|
||||
class con_set_enum
|
||||
{
|
||||
friend class con_map_enum < key, value >;
|
||||
|
||||
protected:
|
||||
con_set< key, value > *m_Set;
|
||||
unsigned int m_Index;
|
||||
Entry< key, value > *m_CurrentEntry;
|
||||
Entry< key, value > *m_NextEntry;
|
||||
|
||||
public:
|
||||
|
||||
con_set_enum();
|
||||
con_set_enum( con_set< key, value >& set );
|
||||
|
||||
bool operator=( con_set< key, value >& set );
|
||||
|
||||
Entry< key, value > *NextElement( void );
|
||||
Entry< key, value > *CurrentElement( void );
|
||||
};
|
||||
|
||||
template< typename key, typename value >
|
||||
class con_map {
|
||||
friend class con_map_enum < key, value >;
|
||||
|
||||
private:
|
||||
con_set< key, value > m_con_set;
|
||||
|
||||
public:
|
||||
#ifdef ARCHIVE_SUPPORTED
|
||||
void Archive( Archiver& arc );
|
||||
#endif
|
||||
|
||||
void clear();
|
||||
virtual void resize( int count = 0 );
|
||||
|
||||
value& operator[]( const key& index );
|
||||
|
||||
value* find( const key& index );
|
||||
bool remove( const key& index );
|
||||
|
||||
unsigned int size();
|
||||
};
|
||||
|
||||
template< typename key, typename value >
|
||||
class con_map_enum
|
||||
{
|
||||
private:
|
||||
con_set_enum< key, value > m_Set_Enum;
|
||||
|
||||
public:
|
||||
|
||||
con_map_enum();
|
||||
con_map_enum( con_map< key, value >& map );
|
||||
|
||||
bool operator=( con_map< key, value >& map );
|
||||
|
||||
key *NextKey( void );
|
||||
value *NextValue( void );
|
||||
key *CurrentKey( void );
|
||||
value *CurrentValue( void );
|
||||
};
|
||||
/*
|
||||
int HashCode( const char& key );
|
||||
int HashCode( const unsigned char& key );
|
||||
int HashCode( unsigned char * const& key );
|
||||
int HashCode( const short& key );
|
||||
int HashCode( const unsigned short& key );
|
||||
int HashCode( const short3& key );
|
||||
int HashCode( const unsigned_short3& key );
|
||||
int HashCode( const int& key );
|
||||
int HashCode( const unsigned int& key );
|
||||
int HashCode( const float& key );
|
||||
int HashCode( const char *key );
|
||||
int HashCode( const str& key );
|
||||
*/
|
||||
|
||||
template< typename k, typename v >
|
||||
MEM_BlockAlloc<Entry< k, v >> con_set< k, v >::Entry_allocator;
|
||||
MEM_BlockAlloc<typename con_set<k, v>::Entry> con_set< k, v >::Entry_allocator;
|
||||
|
||||
template< typename k >
|
||||
int HashCode( const k& key );
|
||||
|
||||
/*
|
||||
template< typename k, typename v >
|
||||
void *Entry< k, v >::operator new( size_t size )
|
||||
void *con_set<k, v>::Entry::operator new( size_t size )
|
||||
{
|
||||
return con_set< k, v >::Entry_allocator.Alloc();
|
||||
}
|
||||
|
||||
template< typename k, typename v >
|
||||
void Entry< k, v >::operator delete( void *ptr )
|
||||
void con_set<k, v>::Entry::operator delete( void *ptr )
|
||||
{
|
||||
con_set< k, v >::Entry_allocator.Free( ptr );
|
||||
}
|
||||
*/
|
||||
|
||||
template< typename k, typename v >
|
||||
Entry< k, v >::Entry()
|
||||
con_set<k, v>::Entry::Entry()
|
||||
{
|
||||
this->key = k();
|
||||
this->value = v();
|
||||
|
||||
index = 0;
|
||||
next = NULL;
|
||||
}
|
||||
|
||||
|
@ -238,8 +155,8 @@ con_set<key, value>::~con_set()
|
|||
template< typename key, typename value >
|
||||
void con_set< key, value >::clear()
|
||||
{
|
||||
Entry< key, value > *entry = NULL;
|
||||
Entry< key, value > *next = NULL;
|
||||
Entry* entry = NULL;
|
||||
Entry* next = NULL;
|
||||
unsigned int i;
|
||||
|
||||
for( i = 0; i < tableLength; i++ )
|
||||
|
@ -269,10 +186,11 @@ void con_set< key, value >::clear()
|
|||
template< typename key, typename value >
|
||||
void con_set< key, value >::resize( int count )
|
||||
{
|
||||
Entry< key, value > **oldTable = table;
|
||||
Entry< key, value > *e, *old;
|
||||
int oldTableLength = tableLength;
|
||||
int i, index;
|
||||
Entry **oldTable = table;
|
||||
Entry *e, *old;
|
||||
unsigned int oldTableLength = tableLength;
|
||||
unsigned int i;
|
||||
unsigned int index;
|
||||
|
||||
if( count > 0 )
|
||||
{
|
||||
|
@ -292,14 +210,14 @@ void con_set< key, value >::resize( int count )
|
|||
}
|
||||
|
||||
// allocate a new table
|
||||
table = new Entry< key, value > *[ tableLength ]();
|
||||
memset( table, 0, tableLength * sizeof( Entry< key, value > * ) );
|
||||
table = new Entry *[ tableLength ]();
|
||||
memset( table, 0, tableLength * sizeof( Entry* ) );
|
||||
|
||||
// rehash the table
|
||||
for( i = oldTableLength - 1; i >= 0; i-- )
|
||||
for( i = oldTableLength; i > 0; i-- )
|
||||
{
|
||||
// rehash all entries from the old table
|
||||
for( e = oldTable[ i ]; e != NULL; e = old )
|
||||
for( e = oldTable[ i - 1 ]; e != NULL; e = old )
|
||||
{
|
||||
old = e->next;
|
||||
|
||||
|
@ -319,9 +237,9 @@ void con_set< key, value >::resize( int count )
|
|||
}
|
||||
|
||||
template< typename k, typename v >
|
||||
Entry< k, v > *con_set< k, v >::findKeyEntry( const k& key ) const
|
||||
typename con_set< k, v >::Entry *con_set< k, v >::findKeyEntry( const k& key ) const
|
||||
{
|
||||
Entry< k, v > *entry;
|
||||
Entry *entry;
|
||||
|
||||
entry = table[ HashCode< k >( key ) % tableLength ];
|
||||
|
||||
|
@ -336,9 +254,9 @@ Entry< k, v > *con_set< k, v >::findKeyEntry( const k& key ) const
|
|||
}
|
||||
|
||||
template< typename k, typename v >
|
||||
Entry< k, v > *con_set< k, v >::addKeyEntry( const k& key )
|
||||
typename con_set< k, v >::Entry*con_set< k, v >::addKeyEntry( const k& key )
|
||||
{
|
||||
Entry< k, v > *entry;
|
||||
Entry *entry;
|
||||
|
||||
entry = findKeyEntry( key );
|
||||
|
||||
|
@ -350,9 +268,9 @@ Entry< k, v > *con_set< k, v >::addKeyEntry( const k& key )
|
|||
}
|
||||
|
||||
template< typename k, typename v >
|
||||
Entry< k, v > *con_set< k, v >::addNewKeyEntry( const k& key )
|
||||
typename con_set< k, v >::Entry *con_set< k, v >::addNewKeyEntry( const k& key )
|
||||
{
|
||||
Entry< k, v > *entry;
|
||||
Entry *entry;
|
||||
int index;
|
||||
|
||||
if( count >= threshold )
|
||||
|
@ -364,7 +282,7 @@ Entry< k, v > *con_set< k, v >::addNewKeyEntry( const k& key )
|
|||
|
||||
count++;
|
||||
|
||||
entry = new Entry < k, v >;
|
||||
entry = new Entry;
|
||||
|
||||
if( defaultEntry == NULL )
|
||||
{
|
||||
|
@ -393,8 +311,8 @@ template< typename k, typename v >
|
|||
bool con_set< k, v >::remove( const k& key )
|
||||
{
|
||||
int index = HashCode< k >( key ) % tableLength;
|
||||
Entry< k, v > *prev = NULL;
|
||||
Entry< k, v > *entry;
|
||||
Entry *prev = NULL;
|
||||
Entry *entry;
|
||||
|
||||
for( entry = table[ index ]; entry != NULL; entry = entry->next )
|
||||
{
|
||||
|
@ -431,7 +349,7 @@ bool con_set< k, v >::remove( const k& key )
|
|||
template< typename k, typename v >
|
||||
v *con_set< k, v >::findKeyValue( const k& key ) const
|
||||
{
|
||||
Entry< k, v > *entry = findKeyEntry( key );
|
||||
Entry *entry = findKeyEntry( key );
|
||||
|
||||
if( entry != NULL ) {
|
||||
return &entry->value;
|
||||
|
@ -456,7 +374,7 @@ key *con_set< key, value >::firstKeyValue( void )
|
|||
template< typename k, typename v >
|
||||
v& con_set< k, v >::addKeyValue( const k& key )
|
||||
{
|
||||
Entry< k, v > *entry = addKeyEntry( key );
|
||||
Entry *entry = addKeyEntry( key );
|
||||
|
||||
return entry->value;
|
||||
}
|
||||
|
@ -464,7 +382,7 @@ v& con_set< k, v >::addKeyValue( const k& key )
|
|||
template< typename k, typename v >
|
||||
v& con_set< k, v >::addNewKeyValue( const k& key )
|
||||
{
|
||||
Entry< k, v > *entry = addNewKeyEntry( key );
|
||||
Entry *entry = addNewKeyEntry( key );
|
||||
|
||||
return entry->value;
|
||||
}
|
||||
|
@ -472,7 +390,7 @@ v& con_set< k, v >::addNewKeyValue( const k& key )
|
|||
template< typename k, typename v >
|
||||
bool con_set< k, v >::keyExists( const k& key )
|
||||
{
|
||||
Entry< k, v > *entry;
|
||||
Entry *entry;
|
||||
|
||||
for( entry = table; entry != NULL; entry = entry->next )
|
||||
{
|
||||
|
@ -490,6 +408,31 @@ unsigned int con_set< key, value >::size()
|
|||
return count;
|
||||
}
|
||||
|
||||
template<typename key, typename value >
|
||||
class con_set_enum
|
||||
{
|
||||
friend class con_map_enum < key, value >;
|
||||
|
||||
public:
|
||||
using Entry = typename con_set<key, value>::Entry;
|
||||
|
||||
protected:
|
||||
con_set< key, value >* m_Set;
|
||||
unsigned int m_Index;
|
||||
Entry* m_CurrentEntry;
|
||||
Entry* m_NextEntry;
|
||||
|
||||
public:
|
||||
|
||||
con_set_enum();
|
||||
con_set_enum(con_set< key, value >& set );
|
||||
|
||||
bool operator=(con_set< key, value >& set );
|
||||
|
||||
Entry *NextElement( void );
|
||||
Entry *CurrentElement( void );
|
||||
};
|
||||
|
||||
template< typename key, typename value >
|
||||
con_set_enum< key, value >::con_set_enum()
|
||||
{
|
||||
|
@ -517,13 +460,13 @@ bool con_set_enum< key, value >::operator=( con_set< key, value > &set )
|
|||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
Entry< key, value > *con_set_enum< key, value >::CurrentElement( void )
|
||||
typename con_set_enum< key, value >::Entry* con_set_enum< key, value >::CurrentElement( void )
|
||||
{
|
||||
return m_CurrentEntry;
|
||||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
Entry< key, value > *con_set_enum< key, value >::NextElement( void )
|
||||
typename con_set_enum< key, value >::Entry* con_set_enum< key, value >::NextElement( void )
|
||||
{
|
||||
if( !m_NextEntry )
|
||||
{
|
||||
|
@ -554,6 +497,29 @@ Entry< key, value > *con_set_enum< key, value >::NextElement( void )
|
|||
return m_CurrentEntry;
|
||||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
class con_map {
|
||||
friend class con_map_enum < key, value >;
|
||||
|
||||
private:
|
||||
con_set< key, value > m_con_set;
|
||||
|
||||
public:
|
||||
#ifdef ARCHIVE_SUPPORTED
|
||||
void Archive( Archiver& arc );
|
||||
#endif
|
||||
|
||||
void clear();
|
||||
virtual void resize( int count = 0 );
|
||||
|
||||
value& operator[]( const key& index );
|
||||
|
||||
value* find( const key& index );
|
||||
bool remove( const key& index );
|
||||
|
||||
unsigned int size();
|
||||
};
|
||||
|
||||
template< typename key, typename value >
|
||||
void con_map< key, value >::clear()
|
||||
{
|
||||
|
@ -590,6 +556,28 @@ unsigned int con_map< key, value >::size( void )
|
|||
return m_con_set.size();
|
||||
}
|
||||
|
||||
template< typename key, typename value >
|
||||
class con_map_enum
|
||||
{
|
||||
public:
|
||||
using Entry = typename con_set_enum<key, value>::Entry;
|
||||
|
||||
private:
|
||||
con_set_enum< key, value > m_Set_Enum;
|
||||
|
||||
public:
|
||||
|
||||
con_map_enum();
|
||||
con_map_enum( con_map< key, value >& map );
|
||||
|
||||
bool operator=( con_map< key, value >& map );
|
||||
|
||||
key *NextKey( void );
|
||||
value *NextValue( void );
|
||||
key *CurrentKey( void );
|
||||
value *CurrentValue( void );
|
||||
};
|
||||
|
||||
template< typename key, typename value >
|
||||
con_map_enum< key, value >::con_map_enum()
|
||||
{
|
||||
|
@ -616,7 +604,7 @@ bool con_map_enum< key, value >::operator=( con_map< key, value >& map )
|
|||
template< typename key, typename value >
|
||||
key *con_map_enum< key, value >::CurrentKey( void )
|
||||
{
|
||||
Entry< key, value > *entry = m_Set_Enum.CurrentElement();
|
||||
Entry *entry = m_Set_Enum.CurrentElement();
|
||||
|
||||
if( entry )
|
||||
{
|
||||
|
@ -631,7 +619,7 @@ key *con_map_enum< key, value >::CurrentKey( void )
|
|||
template< typename key, typename value >
|
||||
value *con_map_enum< key, value >::CurrentValue( void )
|
||||
{
|
||||
Entry< key, value > *entry = m_Set_Enum.CurrentElement();
|
||||
Entry *entry = m_Set_Enum.CurrentElement();
|
||||
|
||||
if( entry )
|
||||
{
|
||||
|
@ -646,7 +634,7 @@ value *con_map_enum< key, value >::CurrentValue( void )
|
|||
template< typename key, typename value >
|
||||
key *con_map_enum< key, value >::NextKey( void )
|
||||
{
|
||||
Entry< key, value > *entry = m_Set_Enum.NextElement();
|
||||
Entry *entry = m_Set_Enum.NextElement();
|
||||
|
||||
if( entry )
|
||||
{
|
||||
|
@ -661,7 +649,7 @@ key *con_map_enum< key, value >::NextKey( void )
|
|||
template< typename key, typename value >
|
||||
value *con_map_enum< key, value >::NextValue( void )
|
||||
{
|
||||
Entry< key, value > *entry = m_Set_Enum.NextElement();
|
||||
Entry *entry = m_Set_Enum.NextElement();
|
||||
|
||||
if( entry )
|
||||
{
|
||||
|
@ -672,5 +660,3 @@ value *con_map_enum< key, value >::NextValue( void )
|
|||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // __CON_SET_H__
|
||||
|
|
|
@ -388,7 +388,7 @@ void ConList::Archive
|
|||
}
|
||||
|
||||
template<>
|
||||
void Entry< const_str, ConList >::Archive
|
||||
void con_set< const_str, ConList >::Entry::Archive
|
||||
(
|
||||
Archiver& arc
|
||||
)
|
||||
|
@ -1742,8 +1742,8 @@ void Event::LoadEvents()
|
|||
int index;
|
||||
command_t c;
|
||||
command_t *i;
|
||||
Entry< command_t, command_t > *entry;
|
||||
con_set_enum< command_t, command_t > en;
|
||||
con_arrayset<command_t, command_t>::Entry *entry;
|
||||
con_arrayset_enum< command_t, command_t > en;
|
||||
|
||||
while( evi )
|
||||
{
|
||||
|
@ -3413,7 +3413,7 @@ void Listener::CancelWaitingAll()
|
|||
}
|
||||
|
||||
con_set_enum< const_str, ConList > en = *m_WaitForList;
|
||||
Entry< const_str, ConList > *e;
|
||||
con_set< const_str, ConList >::Entry *e;
|
||||
ConList stoppedListeners;
|
||||
|
||||
for( e = en.NextElement(); e != NULL; e = en.NextElement() )
|
||||
|
@ -3749,7 +3749,7 @@ void Listener::UnregisterAll( void )
|
|||
}
|
||||
|
||||
con_set_enum < const_str, ConList > en = *m_NotifyList;
|
||||
Entry< const_str, ConList > *e;
|
||||
con_set< const_str, ConList >::Entry *e;
|
||||
ConList stoppedListeners;
|
||||
Container< const_str > stoppedNames;
|
||||
|
||||
|
|
|
@ -71,14 +71,14 @@ int HashCode<ScriptVariable>(const ScriptVariable& key)
|
|||
|
||||
#if defined(ARCHIVE_SUPPORTED)
|
||||
template<>
|
||||
void Entry<ScriptVariable, ScriptVariable>::Archive(Archiver& arc)
|
||||
void con_set<ScriptVariable, ScriptVariable>::Entry::Archive(Archiver& arc)
|
||||
{
|
||||
key.ArchiveInternal(arc);
|
||||
value.ArchiveInternal(arc);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Entry<short3, ScriptVariable>::Archive(Archiver& arc)
|
||||
void con_set<short3, ScriptVariable>::Entry::Archive(Archiver& arc)
|
||||
{
|
||||
if (arc.Loading()) {
|
||||
value.Archive(arc);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue