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);
45 static SQTable* Create(SQSharedState *ss,SQInteger nInitialSize)
47 SQTable *newtable = (SQTable*)SQ_MALLOC(sizeof(SQTable));
48 new (newtable) SQTable(ss, nInitialSize);
49 newtable->_delegate = NULL;
57 REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
58 for (SQInteger i = 0; i < _numofnodes; i++) _nodes[i].~_HashNode();
59 SQ_FREE(_nodes, _numofnodes * sizeof(_HashNode));
61 #ifndef NO_GARBAGE_COLLECTOR
62 void Mark(SQCollectable **chain);
64 inline _HashNode *_Get(const SQObjectPtr &key,SQHash hash)
66 _HashNode *n = &_nodes[hash];
68 if(_rawval(n->key) == _rawval(key) && type(n->key) == type(key)){
71 }while((n = n->next));
74 bool Get(const SQObjectPtr &key,SQObjectPtr &val);
75 void Remove(const SQObjectPtr &key);
76 bool Set(const SQObjectPtr &key, const SQObjectPtr &val);
77 //returns true if a new slot has been created false if it was already present
78 bool NewSlot(const SQObjectPtr &key,const SQObjectPtr &val);
79 SQInteger Next(bool getweakrefs,const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
81 SQInteger CountUsed(){ return _usednodes;}
84 sq_delete(this, SQTable);