[bb] Linked Type Lists (Updated) by Miracle [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : Linked Type Lists (Updated)
Author : Miracle
Posted : 1+ years ago

Description : This method allows a program quickly to search a large number of similar types by creating a linked list through which they can be indexed together. This is MUCH faster than the "needle in a haystack" method of searching large numbers of types with a For/Each loop. Clever programmers can use this algorithm to link types together in all sorts of funky ways.

UPDATE: Expanded method allows bidirectional searches and an easier way to insert and delete types in the middle of a list.


Code :
Code (blitzbasic) Select
Type ship
Field x
Field y
Field pv.ship ; This will point to the previous ship in the linked list
Field nx.ship ; And this points to the next one
End Type

Type destroyer
Field lastship.ship ; Points to the last ship in the string ...
Field firstship.ship ; ... and the first one.
End Type

Type fighter
Field lastship.ship
Field firstship.ship
End Type

Global destroyer.destroyer = New destroyer
Global fighter.fighter = New fighter

; Let's make some destroyers
For x = 1 To 100
d.ship = New ship
dpv = destroyerlastship
If destroyerlastship <> Null Then destroyerlastship
x = d Else destroyerfirstship = d
destroyerlastship = d
Next

; Now we need fighters
For x = 1 To 250
f.ship = New ship
fpv = fighterlastship
If fighterlastship <> Null Then fighterlastship
x = f Else fighterfirstship = f
fighterlastship = f
Next

; Move all the fighters one pixel to the left, in reverse order
scratch.ship = fighterlastship
Repeat
scratchx = scratchx - 1
scratch = scratchpv
Until scratch = Null

; Draw all the destroyers, in forward order
scratch.ship = destroyerfirstship
Repeat
DrawImage destroyerimage,scratchx,scratchy
scratch = scratch
x
Until scratch = Null

; Insert a fighter after the first one in the list
f.ship = New ship
f
x = fighterfirstship
x
fpv = fighterfirstship
fighterfirstship
x = f

; Add a new destroyer to the end of the list
d.ship = New ship
dpv = destroyerlastship
destroyerlastship
x = d
destroyerlastship = d

; Delete the 15th destroyer
scratch.ship = destroyerfirstship
For x = 1 To 14
scratch = scratch
x
Next
scratch
xpv = scratchpv
scratchpv
x = scratch
x
Delete scratch

; Move all ships three pixels down
For scratch.ship = Each ship
scratchy = scratchy + 3
Next


Comments : none...