summaryrefslogtreecommitdiffstats
path: root/squirrel_3_0_1_stable/squirrel/sqtable.h
diff options
context:
space:
mode:
authorfaketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6>2011-11-08 02:25:01 +0100
committerfaketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6>2011-11-08 02:25:01 +0100
commit8285a11a26d78784f26b76e6bcdfa479f6c1a345 (patch)
tree6d567d22754b2d65258b1557e18ea8b590c2c709 /squirrel_3_0_1_stable/squirrel/sqtable.h
parentFixed bug in cChunk.cpp not calculating RedstoneCircuits at the correct positions. Also, forgot to mention you can now place colored wool. (diff)
downloadcuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar
cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar.gz
cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar.bz2
cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar.lz
cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar.xz
cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.tar.zst
cuberite-8285a11a26d78784f26b76e6bcdfa479f6c1a345.zip
Diffstat (limited to 'squirrel_3_0_1_stable/squirrel/sqtable.h')
-rw-r--r--squirrel_3_0_1_stable/squirrel/sqtable.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/squirrel_3_0_1_stable/squirrel/sqtable.h b/squirrel_3_0_1_stable/squirrel/sqtable.h
new file mode 100644
index 000000000..2f6487023
--- /dev/null
+++ b/squirrel_3_0_1_stable/squirrel/sqtable.h
@@ -0,0 +1,92 @@
+/* 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) ((SQHash)(((SQInteger)p) >> 3))
+
+inline SQHash HashObj(const SQObjectPtr &key)
+{
+ switch(type(key)) {
+ case OT_STRING: return _string(key)->_hash;
+ case OT_FLOAT: return (SQHash)((SQInteger)_float(key));
+ case OT_BOOL: case OT_INTEGER: return (SQHash)((SQInteger)_integer(key));
+ default: return hashptr(key._unVal.pRefCounted);
+ }
+}
+
+struct SQTable : public SQDelegable
+{
+private:
+ struct _HashNode
+ {
+ _HashNode() { next = NULL; }
+ SQObjectPtr val;
+ SQObjectPtr key;
+ _HashNode *next;
+ };
+ _HashNode *_firstfree;
+ _HashNode *_nodes;
+ SQInteger _numofnodes;
+ SQInteger _usednodes;
+
+///////////////////////////
+ void AllocNodes(SQInteger nSize);
+ void Rehash(bool force);
+ SQTable(SQSharedState *ss, SQInteger nInitialSize);
+ void _ClearNodes();
+public:
+ static SQTable* Create(SQSharedState *ss,SQInteger 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 (SQInteger i = 0; i < _numofnodes; i++) _nodes[i].~_HashNode();
+ SQ_FREE(_nodes, _numofnodes * sizeof(_HashNode));
+ }
+#ifndef NO_GARBAGE_COLLECTOR
+ void Mark(SQCollectable **chain);
+ SQObjectType GetType() {return OT_TABLE;}
+#endif
+ inline _HashNode *_Get(const SQObjectPtr &key,SQHash 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);
+ SQInteger Next(bool getweakrefs,const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
+
+ SQInteger CountUsed(){ return _usednodes;}
+ void Clear();
+ void Release()
+ {
+ sq_delete(this, SQTable);
+ }
+
+};
+
+#endif //_SQTABLE_H_