fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / squirrel / squirrel / sqtable.h
1 /*      see copyright notice in squirrel.h */
2 #ifndef _SQTABLE_H_
3 #define _SQTABLE_H_
4 /*
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
8 */
9
10 #include "sqstring.h"
11
12
13 #define hashptr(p)  ((SQHash)(((SQInteger)p) >> 3))
14
15 inline SQHash HashObj(const SQObjectPtr &key)
16 {
17         switch(type(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);
22         }
23 }
24
25 struct SQTable : public SQDelegable
26 {
27 private:
28         struct _HashNode
29         {
30                 _HashNode() { next = NULL; }
31                 SQObjectPtr val;
32                 SQObjectPtr key;
33                 _HashNode *next;
34         };
35         _HashNode *_firstfree;
36         _HashNode *_nodes;
37         SQInteger _numofnodes;
38         SQInteger _usednodes;
39
40 ///////////////////////////
41         void AllocNodes(SQInteger nSize);
42         void Rehash(bool force);
43         SQTable(SQSharedState *ss, SQInteger nInitialSize);
44 public:
45         static SQTable* Create(SQSharedState *ss,SQInteger nInitialSize)
46         {
47                 SQTable *newtable = (SQTable*)SQ_MALLOC(sizeof(SQTable));
48                 new (newtable) SQTable(ss, nInitialSize);
49                 newtable->_delegate = NULL;
50                 return newtable;
51         }
52         void Finalize();
53         SQTable *Clone();
54         ~SQTable()
55         {
56                 SetDelegate(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));
60         }
61 #ifndef NO_GARBAGE_COLLECTOR
62         void Mark(SQCollectable **chain);
63 #endif
64         inline _HashNode *_Get(const SQObjectPtr &key,SQHash hash)
65         {
66                 _HashNode *n = &_nodes[hash];
67                 do{
68                         if(_rawval(n->key) == _rawval(key) && type(n->key) == type(key)){
69                                 return n;
70                         }
71                 }while((n = n->next));
72                 return NULL;
73         }
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);
80
81         SQInteger CountUsed(){ return _usednodes;}
82         void Release()
83         {
84                 sq_delete(this, SQTable);
85         }
86
87 };
88
89 #endif //_SQTABLE_H_