The Squirrel interpreter. See http://www.squirrel-lang.org/

Dependents:   Squirrel

Committer:
jhnwkmn
Date:
Tue Dec 16 10:20:34 2014 +0000
Revision:
0:97a4f8cc534c
Initial import of Squirrel.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhnwkmn 0:97a4f8cc534c 1 /* see copyright notice in squirrel.h */
jhnwkmn 0:97a4f8cc534c 2 #ifndef _SQTABLE_H_
jhnwkmn 0:97a4f8cc534c 3 #define _SQTABLE_H_
jhnwkmn 0:97a4f8cc534c 4 /*
jhnwkmn 0:97a4f8cc534c 5 * The following code is based on Lua 4.0 (Copyright 1994-2002 Tecgraf, PUC-Rio.)
jhnwkmn 0:97a4f8cc534c 6 * http://www.lua.org/copyright.html#4
jhnwkmn 0:97a4f8cc534c 7 * http://www.lua.org/source/4.0.1/src_ltable.c.html
jhnwkmn 0:97a4f8cc534c 8 */
jhnwkmn 0:97a4f8cc534c 9
jhnwkmn 0:97a4f8cc534c 10 #include "sqstring.h"
jhnwkmn 0:97a4f8cc534c 11
jhnwkmn 0:97a4f8cc534c 12
jhnwkmn 0:97a4f8cc534c 13 #define hashptr(p) ((SQHash)(((SQInteger)p) >> 3))
jhnwkmn 0:97a4f8cc534c 14
jhnwkmn 0:97a4f8cc534c 15 inline SQHash HashObj(const SQObjectPtr &key)
jhnwkmn 0:97a4f8cc534c 16 {
jhnwkmn 0:97a4f8cc534c 17 switch(type(key)) {
jhnwkmn 0:97a4f8cc534c 18 case OT_STRING: return _string(key)->_hash;
jhnwkmn 0:97a4f8cc534c 19 case OT_FLOAT: return (SQHash)((SQInteger)_float(key));
jhnwkmn 0:97a4f8cc534c 20 case OT_BOOL: case OT_INTEGER: return (SQHash)((SQInteger)_integer(key));
jhnwkmn 0:97a4f8cc534c 21 default: return hashptr(key._unVal.pRefCounted);
jhnwkmn 0:97a4f8cc534c 22 }
jhnwkmn 0:97a4f8cc534c 23 }
jhnwkmn 0:97a4f8cc534c 24
jhnwkmn 0:97a4f8cc534c 25 struct SQTable : public SQDelegable
jhnwkmn 0:97a4f8cc534c 26 {
jhnwkmn 0:97a4f8cc534c 27 private:
jhnwkmn 0:97a4f8cc534c 28 struct _HashNode
jhnwkmn 0:97a4f8cc534c 29 {
jhnwkmn 0:97a4f8cc534c 30 _HashNode() { next = NULL; }
jhnwkmn 0:97a4f8cc534c 31 SQObjectPtr val;
jhnwkmn 0:97a4f8cc534c 32 SQObjectPtr key;
jhnwkmn 0:97a4f8cc534c 33 _HashNode *next;
jhnwkmn 0:97a4f8cc534c 34 };
jhnwkmn 0:97a4f8cc534c 35 _HashNode *_firstfree;
jhnwkmn 0:97a4f8cc534c 36 _HashNode *_nodes;
jhnwkmn 0:97a4f8cc534c 37 SQInteger _numofnodes;
jhnwkmn 0:97a4f8cc534c 38 SQInteger _usednodes;
jhnwkmn 0:97a4f8cc534c 39
jhnwkmn 0:97a4f8cc534c 40 ///////////////////////////
jhnwkmn 0:97a4f8cc534c 41 void AllocNodes(SQInteger nSize);
jhnwkmn 0:97a4f8cc534c 42 void Rehash(bool force);
jhnwkmn 0:97a4f8cc534c 43 SQTable(SQSharedState *ss, SQInteger nInitialSize);
jhnwkmn 0:97a4f8cc534c 44 void _ClearNodes();
jhnwkmn 0:97a4f8cc534c 45 public:
jhnwkmn 0:97a4f8cc534c 46 static SQTable* Create(SQSharedState *ss,SQInteger nInitialSize)
jhnwkmn 0:97a4f8cc534c 47 {
jhnwkmn 0:97a4f8cc534c 48 SQTable *newtable = (SQTable*)SQ_MALLOC(sizeof(SQTable));
jhnwkmn 0:97a4f8cc534c 49 new (newtable) SQTable(ss, nInitialSize);
jhnwkmn 0:97a4f8cc534c 50 newtable->_delegate = NULL;
jhnwkmn 0:97a4f8cc534c 51 return newtable;
jhnwkmn 0:97a4f8cc534c 52 }
jhnwkmn 0:97a4f8cc534c 53 void Finalize();
jhnwkmn 0:97a4f8cc534c 54 SQTable *Clone();
jhnwkmn 0:97a4f8cc534c 55 ~SQTable()
jhnwkmn 0:97a4f8cc534c 56 {
jhnwkmn 0:97a4f8cc534c 57 SetDelegate(NULL);
jhnwkmn 0:97a4f8cc534c 58 REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
jhnwkmn 0:97a4f8cc534c 59 for (SQInteger i = 0; i < _numofnodes; i++) _nodes[i].~_HashNode();
jhnwkmn 0:97a4f8cc534c 60 SQ_FREE(_nodes, _numofnodes * sizeof(_HashNode));
jhnwkmn 0:97a4f8cc534c 61 }
jhnwkmn 0:97a4f8cc534c 62 #ifndef NO_GARBAGE_COLLECTOR
jhnwkmn 0:97a4f8cc534c 63 void Mark(SQCollectable **chain);
jhnwkmn 0:97a4f8cc534c 64 SQObjectType GetType() {return OT_TABLE;}
jhnwkmn 0:97a4f8cc534c 65 #endif
jhnwkmn 0:97a4f8cc534c 66 inline _HashNode *_Get(const SQObjectPtr &key,SQHash hash)
jhnwkmn 0:97a4f8cc534c 67 {
jhnwkmn 0:97a4f8cc534c 68 _HashNode *n = &_nodes[hash];
jhnwkmn 0:97a4f8cc534c 69 do{
jhnwkmn 0:97a4f8cc534c 70 if(_rawval(n->key) == _rawval(key) && type(n->key) == type(key)){
jhnwkmn 0:97a4f8cc534c 71 return n;
jhnwkmn 0:97a4f8cc534c 72 }
jhnwkmn 0:97a4f8cc534c 73 }while((n = n->next));
jhnwkmn 0:97a4f8cc534c 74 return NULL;
jhnwkmn 0:97a4f8cc534c 75 }
jhnwkmn 0:97a4f8cc534c 76 bool Get(const SQObjectPtr &key,SQObjectPtr &val);
jhnwkmn 0:97a4f8cc534c 77 void Remove(const SQObjectPtr &key);
jhnwkmn 0:97a4f8cc534c 78 bool Set(const SQObjectPtr &key, const SQObjectPtr &val);
jhnwkmn 0:97a4f8cc534c 79 //returns true if a new slot has been created false if it was already present
jhnwkmn 0:97a4f8cc534c 80 bool NewSlot(const SQObjectPtr &key,const SQObjectPtr &val);
jhnwkmn 0:97a4f8cc534c 81 SQInteger Next(bool getweakrefs,const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
jhnwkmn 0:97a4f8cc534c 82
jhnwkmn 0:97a4f8cc534c 83 SQInteger CountUsed(){ return _usednodes;}
jhnwkmn 0:97a4f8cc534c 84 void Clear();
jhnwkmn 0:97a4f8cc534c 85 void Release()
jhnwkmn 0:97a4f8cc534c 86 {
jhnwkmn 0:97a4f8cc534c 87 sq_delete(this, SQTable);
jhnwkmn 0:97a4f8cc534c 88 }
jhnwkmn 0:97a4f8cc534c 89
jhnwkmn 0:97a4f8cc534c 90 };
jhnwkmn 0:97a4f8cc534c 91
jhnwkmn 0:97a4f8cc534c 92 #endif //_SQTABLE_H_