back to index
Pool
- a buffered non-linear array of homogenous objects
- assign name-IDs (integers) to allocated objects
- very fast alloc/free methods, no linear search required
- also see PointerArray, ObjectArray and StringArray
Inheritance
Object -> Pool
Properties
numElements - - number of used elements
maxElements - - number of available elements
template - - template object, e.g. p.template=String;
Methods
int alloc (int _num)
empty ()
free ()
int getIDByObject (Object _o)
int getNumElements ()
int getMaxElements ()
Object getTemplate ()
setTemplate (Object _t)
int qAlloc ()
qFree (int _id)
qFreeByObject (Object _o)
Example
//
module Main;
class MyPoolEntry {
String name;
}
function main() {
Pool pool;
int i;
int id;
MyPoolEntry ce;
String s;
pool.template=MyPoolEntry;
pool.alloc(32);
loop(10)
{
id=pool.qAlloc();
ce<=pool[id];
ce.name="poolentry_"+id;
}
i=0;
foreach id in pool
{
trace("foreach qFree i=="+i);
if(++i<5)
{
pool.qFree(id); // detach entry, does not affect iterated list
}
else
id=-1; // end foreach
}
foreach id in pool
{
ce<=pool[id];
s<=ce.name;
trace("foreach entry \""+s+"\"\n");
}
pool.free();
}
(pool.tks)
back to index