X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fsquirrel%2Fsquirrel%2Fsqtable.h;h=099cfb75aa39b8f219787d8e881b1d016e994fd5;hb=c81e5d255303f56a3fc33bef1fd0124c4b62cfcf;hp=b27b7d37cdb3e4eaf42906df6d31199c534d9375;hpb=fedb7355c2a369399cc781e907ac961dfa11fdd9;p=supertux.git diff --git a/src/squirrel/squirrel/sqtable.h b/src/squirrel/squirrel/sqtable.h index b27b7d37c..099cfb75a 100644 --- a/src/squirrel/squirrel/sqtable.h +++ b/src/squirrel/squirrel/sqtable.h @@ -1,88 +1,88 @@ -/* see copyright notice in squirrel.h */ -#ifndef _SQTABLE_H_ -#define _SQTABLE_H_ -/* -* The following code is based on Lua 4.0 (Copyright 1994-2002 Tecgraf, PUC-Rio.) -* http://www.lua.org/copyright.html#4 -* http://www.lua.org/source/4.0.1/src_ltable.c.html -*/ - -#include "sqstring.h" - -#define hashptr(p) (((unsigned long)(p)) >> 3) - -struct SQTable : public SQDelegable -{ -private: - struct _HashNode - { - SQObjectPtr val; - SQObjectPtr key; - _HashNode *next; - }; - _HashNode *_firstfree; - _HashNode *_nodes; - int _numofnodes; - int _usednodes; - -/////////////////////////// - void AllocNodes(int nSize); - void Rehash(bool force); - SQTable(SQSharedState *ss, int nInitialSize); -public: - static SQTable* Create(SQSharedState *ss,int nInitialSize) - { - SQTable *newtable = (SQTable*)SQ_MALLOC(sizeof(SQTable)); - new (newtable) SQTable(ss, nInitialSize); - newtable->_delegate = NULL; - return newtable; - } - void Finalize(); - SQTable *Clone(); - ~SQTable() - { - SetDelegate(NULL); - REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this); - for (int i = 0; i < _numofnodes; i++) _nodes[i].~_HashNode(); - SQ_FREE(_nodes, _numofnodes * sizeof(_HashNode)); - } -#ifndef NO_GARBAGE_COLLECTOR - void Mark(SQCollectable **chain); -#endif - inline unsigned long HashKey(const SQObjectPtr &key) - { - switch(type(key)){ - case OT_STRING: return _string(key)->_hash; - case OT_FLOAT: return (unsigned long)((long)_float(key)); - case OT_INTEGER: return (unsigned long)((long)_integer(key)); - default: return hashptr(key._unVal.pRefCounted); - } - } - _HashNode *_Get(const SQObjectPtr &key,unsigned long hash) - { - _HashNode *n = &_nodes[hash]; - do{ - if(_rawval(n->key) == _rawval(key) && type(n->key) == type(key)){ - return n; - } - }while(n = n->next); - return NULL; - } - bool Get(const SQObjectPtr &key,SQObjectPtr &val); - void Remove(const SQObjectPtr &key); - bool Set(const SQObjectPtr &key, const SQObjectPtr &val); - //returns true if a new slot has been created false if it was already present - bool NewSlot(const SQObjectPtr &key,const SQObjectPtr &val); - int Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval); - - int CountUsed(); - void Release() - { - sq_delete(this, SQTable); - } - bool SetDelegate(SQTable *mt); - - -}; - -#endif //_SQTABLE_H_ +/* see copyright notice in squirrel.h */ +#ifndef _SQTABLE_H_ +#define _SQTABLE_H_ +/* +* The following code is based on Lua 4.0 (Copyright 1994-2002 Tecgraf, PUC-Rio.) +* http://www.lua.org/copyright.html#4 +* http://www.lua.org/source/4.0.1/src_ltable.c.html +*/ + +#include "sqstring.h" + +#define hashptr(p) (((unsigned long)(p)) >> 3) + +struct SQTable : public SQDelegable +{ +private: + struct _HashNode + { + SQObjectPtr val; + SQObjectPtr key; + _HashNode *next; + }; + _HashNode *_firstfree; + _HashNode *_nodes; + int _numofnodes; + int _usednodes; + +/////////////////////////// + void AllocNodes(int nSize); + void Rehash(bool force); + SQTable(SQSharedState *ss, int nInitialSize); +public: + static SQTable* Create(SQSharedState *ss,int nInitialSize) + { + SQTable *newtable = (SQTable*)SQ_MALLOC(sizeof(SQTable)); + new (newtable) SQTable(ss, nInitialSize); + newtable->_delegate = NULL; + return newtable; + } + void Finalize(); + SQTable *Clone(); + ~SQTable() + { + SetDelegate(NULL); + REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this); + for (int i = 0; i < _numofnodes; i++) _nodes[i].~_HashNode(); + SQ_FREE(_nodes, _numofnodes * sizeof(_HashNode)); + } +#ifndef NO_GARBAGE_COLLECTOR + void Mark(SQCollectable **chain); +#endif + inline unsigned long HashKey(const SQObjectPtr &key) + { + switch(type(key)){ + case OT_STRING: return _string(key)->_hash; + case OT_FLOAT: return (unsigned long)((long)_float(key)); + case OT_INTEGER: return (unsigned long)((long)_integer(key)); + default: return hashptr(key._unVal.pRefCounted); + } + } + _HashNode *_Get(const SQObjectPtr &key,unsigned long hash) + { + _HashNode *n = &_nodes[hash]; + do{ + if(_rawval(n->key) == _rawval(key) && type(n->key) == type(key)){ + return n; + } + }while( (n = n->next) ); + return NULL; + } + bool Get(const SQObjectPtr &key,SQObjectPtr &val); + void Remove(const SQObjectPtr &key); + bool Set(const SQObjectPtr &key, const SQObjectPtr &val); + //returns true if a new slot has been created false if it was already present + bool NewSlot(const SQObjectPtr &key,const SQObjectPtr &val); + int Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval); + + int CountUsed(); + void Release() + { + sq_delete(this, SQTable); + } + bool SetDelegate(SQTable *mt); + + +}; + +#endif //_SQTABLE_H_