December 04, 2020, 11:23:25 AM

Author Topic: [bb] Linked Type Lists (Updated) by Miracle [ 1+ years ago ]  (Read 379 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
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
  1. Type ship
  2.         Field x
  3.         Field y
  4.         Field pv.ship                   ; This will point to the previous ship in the linked list
  5.         Field nx.ship                   ; And this points to the next one
  6. End Type
  7.  
  8. Type destroyer
  9.         Field lastship.ship             ; Points to the last ship in the string ...
  10.         Field firstship.ship    ; ... and the first one.
  11. End Type
  12.  
  13. Type fighter
  14.         Field lastship.ship
  15.         Field firstship.ship
  16. End Type
  17.  
  18. Global destroyer.destroyer = New destroyer
  19. Global fighter.fighter = New fighter
  20.  
  21. ; Let's make some destroyers
  22. For x = 1 To 100
  23.         d.ship = New ship
  24.         dpv = destroyerlastship
  25.         If destroyerlastship <> Null Then destroyerlastship
  26. x = d Else destroyerfirstship = d
  27.         destroyerlastship = d
  28. Next
  29.  
  30. ; Now we need fighters
  31. For x = 1 To 250
  32.         f.ship = New ship
  33.         fpv = fighterlastship
  34.         If fighterlastship <> Null Then fighterlastship
  35. x = f Else fighterfirstship = f
  36.         fighterlastship = f
  37. Next
  38.  
  39. ; Move all the fighters one pixel to the left, in reverse order
  40. scratch.ship = fighterlastship
  41. Repeat
  42.         scratchx = scratchx - 1
  43.         scratch = scratchpv
  44. Until scratch = Null
  45.  
  46. ; Draw all the destroyers, in forward order
  47. scratch.ship = destroyerfirstship
  48. Repeat
  49.         DrawImage destroyerimage,scratchx,scratchy
  50.         scratch = scratch
  51. x
  52. Until scratch = Null
  53.  
  54. ; Insert a fighter after the first one in the list
  55. f.ship = New ship
  56. f
  57. x = fighterfirstship
  58. x
  59. fpv = fighterfirstship
  60. fighterfirstship
  61. x = f
  62.  
  63. ; Add a new destroyer to the end of the list
  64. d.ship = New ship
  65. dpv = destroyerlastship
  66. destroyerlastship
  67. x = d
  68. destroyerlastship = d
  69.  
  70. ; Delete the 15th destroyer
  71. scratch.ship = destroyerfirstship
  72. For x = 1 To 14
  73.         scratch = scratch
  74. x
  75. Next
  76. scratch
  77. xpv = scratchpv
  78. scratchpv
  79. x = scratch
  80. x
  81. Delete scratch
  82.  
  83. ; Move all ships three pixels down
  84. For scratch.ship = Each ship
  85.         scratchy = scratchy + 3
  86. Next


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal