I've seen people talk about FreeObject() functions for finding unused object numbers. The idea often seems to be to search through object numbers from 1 to whatever the first nonexistent object is. The speed of such a function is proportional to the lowest free object number, and i think quite slow for a large number of objects.
This set of functions i made i think are much more efficient. The attached code illustrates making new objects and deleting objects randomly. The downside is that you have to call functions when creating or destroying objects. Can be used alongside other objects not using this system. It uses bitshifting and XOR logic and stuff which i'm not sure DBC has.
I could write a better explanation and comments if anyone is interested. This is probably best explained with aid of a diagram, but maybe you can suss it out already. Hope this is of some use to someone.
Rem Project: freeobj binary function
Rem Created: 02/06/2008 22:42:44
Rem ***** Main Source File *****
`want to make a "free object" routine that's more elegant than conventional "start from 1 and keep going until object
`doesn't exist".
`thought about linked list of used and unused object numbers, but would use a lot of memory.
`this is (i think) a cool system where binary "brackets" have a bit for every bracket.
`say possible 32768 objects (more than that might seem a bit ludicrous!)
`seems limit of object numbers is a bit strange! i get about 22000000 but guessing may depend on computer.
`so using smaller set might seem like better idea.
`also the speed of functions goes as ~log(n). Maybe slight advantage to using smaller set of possible object numbers-
`for this demo 4096 would be fine. functions should then take 12/15=0.8 of the time. no big deal.
#constant binaryobjnum0 1 `arbitrary start number - number of start db objnum. Must be >0
`could set this higher if want to reserve a few low numbers for stuff with
`"hard coded" object numbers
`dim binary_freeobj_spacefull(8191) as boolean `2^12 positions- so need size 2^13 (-1)
dim binary_freeobj_spacefull(65535) as boolean `2^15 positions- so need size 2^16 (-1)
sync on:sync rate 50:hide mouse
backdrop on:color backdrop rgb(200,200,200):ink 0,0
autocam off:position camera 50,100,50:point camera 50,0,50 `above area cubes will appear.
global objs_created:objs_created=0 `not necessary for functions.
do
`delete some objects at random
for trial=1 to 1000
trydeleteobj=rnd(32767)
`can see if it exists-
if binary_freeobj_spacefull(32768+trydeleteobj)
dbObjnum=binaryobjnum0+trydeleteobj
delete object dbObjnum
switch_off_binary_freeobj(trydeleteobj)
dec objs_created `not required for functionality
endif
next trial
`create some objects...
for create=1 to 40
if binary_freeobj_spacefull(1)=0 `some space in whole set
createobjnum=find_first_binary_freeobj()
dbObjnum=binaryobjnum0+createobjnum
make object cube dbObjnum,0.9
position object dbObjnum,rnd(100),0,rnd(100)
switch_on_binary_freeobj(createobjnum)
inc objs_created `not required for functionality
endif
next create
`above: 1000/32768 of objects deleted on average.
`40 are created.
`so equilibrium is 32768*40/1000 = 1310.73 objects.
text 10,0,"SCREEN FPS = "+str$(screen fps())
text 10,10,"LOWEST FREE OBJ NUMBER= "+str$(find_first_binary_freeobj())
text 10,20,"CURRENT NUMBER OF OBJS ="+str$(objs_created)
sync
loop
end
function find_first_binary_freeobj()
psn2=0
for power=0 to 15 `was 12 with 4096=2^12 objs. now with 32768=2^15..
psn2=psn2<<1
psn1=(%1<<power) `2^power - 2^12=4096 etc
inc psn2,binary_freeobj_spacefull(psn2+psn1)
next power
endfunction psn2
function switch_off_binary_freeobj(n) `make it so that all brackets containing it are switched to off (means some space available)
for power=15 to 0 step -1
psn=%1<<power `2^power - 2^12=4096 etc
binary_freeobj_spacefull(n+psn)=0
n=n>>1 `n=n/2 - should work out the same...
next power
endfunction
function switch_on_binary_freeobj(n) `harder, since need to know what other space there is- could fill final space or not..
spacefull=1
for power=15 to 0 step -1
psn=(%1<<power) `2^power - 2^12=4096 etc
binary_freeobj_spacefull(psn+n)=spacefull
neighbour_position=n~~%1 `switch last bit
n=n>>1 `n=n/2 `- should work out the same...
spacefull=spacefull*binary_freeobj_spacefull(psn+neighbour_position) `product occupied with occupied neighbouring bracket..
next power `could do until occupied =1 - no point continuing further.. - but for next faster, also more consistent
endfunction