1 /* see copyright notice in squirrel.h */
5 * The following code is based on Lua 4.0 (Copyright 1994-2002 Tecgraf, PUC-Rio.)
6 * http://www.lua.org/copyright.html#4
7 * http://www.lua.org/source/4.0.1/src_ltable.c.html
13 #define hashptr(p) ((SQHash)(((SQInteger)p) >> 3))
15 inline SQHash HashObj(const SQObjectPtr &key)
18 case OT_STRING: return _string(key)->_hash;
19 case OT_FLOAT: return (SQHash)((SQInteger)_float(key));
20 case OT_BOOL: case OT_INTEGER: return (SQHash)((SQInteger)_integer(key));
21 default: return hashptr(key._unVal.pRefCounted);
25 struct SQTable : public SQDelegable
30 _HashNode() { next = NULL; }
35 _HashNode *_firstfree;
37 SQInteger _numofnodes;
40 ///////////////////////////
41 void AllocNodes(SQInteger nSize);
42 void Rehash(bool force);
43 SQTable(SQSharedState *ss, SQInteger nInitialSize);
46 static SQTable* Create(SQSharedState *ss,SQInteger nInitialSize)
48 SQTable *newtable = (SQTable*)SQ_MALLOC(sizeof(SQTable));
49 new (newtable) SQTable(ss, nInitialSize);
50 newtable->_delegate = NULL;
58 REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
59 for (SQInteger i = 0; i < _numofnodes; i++) _nodes[i].~_HashNode();
60 SQ_FREE(_nodes, _numofnodes * sizeof(_HashNode));
62 #ifndef NO_GARBAGE_COLLECTOR
63 void Mark(SQCollectable **chain);
65 inline _HashNode *_Get(const SQObjectPtr &key,SQHash hash)
67 _HashNode *n = &_nodes[hash];
69 if(_rawval(n->key) == _rawval(key) && type(n->key) == type(key)){
72 }while((n = n->next));
75 bool Get(const SQObjectPtr &key,SQObjectPtr &val);
76 void Remove(const SQObjectPtr &key);
77 bool Set(const SQObjectPtr &key, const SQObjectPtr &val);
78 //returns true if a new slot has been created false if it was already present
79 bool NewSlot(const SQObjectPtr &key,const SQObjectPtr &val);
80 SQInteger Next(bool getweakrefs,const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
82 SQInteger CountUsed(){ return _usednodes;}
86 sq_delete(this, SQTable);