type e linked lists!!!
Blitz3D Forums/Blitz3D Beginners Area/type e linked lists!!!
| ||
Hi boys! I would like to ask a question to all the experts of BB3D! Here is the source example: ; ------------------------------------------------------------------------ Type tHuman Field Name$ End Type Global Good.tHuman Global Bad.tHuman Good.tHuman = New tHuman Good\Name = "Gianluca" Good.tHuman = New tHuman Good\Name = "Cinzia" Bad.tHuman = New tHuman Bad\Name = "Ciccio" Bad.tHuman = New tHuman Bad\Name = "Peppino" Print "------ nice guys " For Good.tHuman = Each tHuman Print Good\Name Next Print "------ Now bad guys " For Bad.tHuman = Each tHuman Print Bad\Name Next While Not KeyHit(1) Wend ; ------------------------------------------------------------------------ And this is what it stamps to video: ------ Nice Guys Gianluca Cinzia Ciccio Peppino ------ Now Bad Guys Gianluca Cinzia Ciccio Peppino The question is this: it’s possible in BBD3 to do so that the var “Good” hold only "Gianluca, cinzia" and the var Bad "ciccio and peppino?" if yes…. how? It seems that blitz basic is not able' to manage two lists separate of the same type…. Don't tell me that is so!!! Hi and thanks Gianluca! ------------------ Ciao Ciao! [GregBUG] |
| ||
you could change your THuman type:Type THuman Field Name$ Field Alignment% ;1 = good, -1 = bad End Type hum.tHuman = New tHuman hum\Name = "Gianluca" hum\Alignment = 1 hum.tHuman = New tHuman hum\Name = "Cinzia" hum\Alignment = 1 hum.tHuman = New tHuman hum\Name = "Ciccio" hum\Alignment = -1 hum.tHuman = New tHuman hum\Name = "Peppino" hum\Alignment = -1 Print "------ nice guys " For hum.tHuman = Each tHuman If hum\alignment = 1 Then Print hum\Name Next Print "------ Now bad guys " For hum.tHuman = Each tHuman If hum\alignment = -1 Then Print hum\Name Next While Not KeyHit(1) Wend |
| ||
Yes, the solutions are to either have two different Type definitions, to add a "flag" field like Perturbatio suggests, or to manage your own sets of linked lists. It will probably get more flexible with inheritance in Blitzmax (though it's not that big of an issue now). |
| ||
I thought that it could be done as in C or Delphi… with user defined type…. I’m disappointed … Ok… > or to manage your own sets of linked lists. how? i wish manage different linked lists with the same type of records... |
| ||
> or to manage your own sets of linked lists. how? Well, just like in any other language. If the "free" linked lists that come with custom types don't suit you, just create your own. You don't have to use the built-in stuff. You know, create a node with a forward and backward pointer along with the data, have a head node and a tail node, That's really the hardest way, though. I would definitely recommend one of the other implementations. Another thought, though, is to use arrays. For example: Given a list of 4 THuman objects... h1 (Gianluca, good) h2 (Cinzia, good) h3 (Ciccio, bad) h4 (Peppino, bad) ...you can assign the type pointers to their own good/bad arrays Dim GoodHuman.THuman(2) Dim BadHuman.THuman(2) GoodHuman(0)=h1 GoodHuman(1)=h2 BadHuman(0)=h3 BadHuman(1)=h4 That way you still have all your humans (good and bad) in a type list, but you can separate them. (Note that you don't have to have separate references to each one -- you can use a For..Each loop with a single h.THuman iteration variable, or you can use the Before and After commands, or you can just put them in the arrays upon creation.) I still would recommend Perturbatio's solution (that is, if they must all be in the same list). You're essentially emulating a sort of inheritance (which isn't built in to Blitz ATM). |
| ||
thank you! Soja! i like the array solution because i need to split the list in a series of chunks. Thanks also to Perturbatio! CIAO |
| ||
here's a linked list solution that I came up with... http://www.blitzbasic.com/codearcs/codearcs.php?code=1117 with it, you would write the above like this: Type tHuman Field Name$ End Type Global Good.BList = New BList Global Bad.BList = New BList For i = 0 To 1 ptr.tHuman = Object.tHuman(AddFront(Good,Handle(New tHuman))) Read ptr\Name Next Data "Gianluca","Cinzia" For i = 0 To 1 ptr.tHuman = Object.tHuman(AddFront(Bad,Handle(New tHuman))) Read ptr\Name Next Data "Ciccio","Peppino" Print "------ nice guys " ResetList(Good) While NextItem(Good) ptr.tHuman = Object.tHuman(CurrentItem(Good)) Print ptr\Name Wend Print "------ Now bad guys " ResetList(Bad) While NextItem(Bad) ptr.tHuman = Object.tHuman(CurrentItem(Bad)) Print ptr\Name Wend WaitKey the current library (include file) supports the following procedures userTypePtr% = AddFront%( list.Blist, userTypePtr% ) userTypePtr% = AddBack%( list.Blist, userTypePtr% ) trueFalse% = ResetList%( list.BList ) userTypePtr% = NextItem%( list.BList ) userTypePtr% = FirstItem%( list.BList ) userTypePtr% = LastItem%( list.BList ) userTypePtr% = CurrentItem%( list.BList ) userTypePtr% = MoveItem%( fromList.BList, toList.BList ) userTypePtr% = KillItem%( list.BList, node.BNode ) node.BNode = CurrentNode.BNode( list.Blist ) totalItems% = TotalItems%( list.BList ) -dmaz |
| ||
wow! how about performances with the linked listlib and array solution? thanks! |
| ||
The lib itself is fast, I doubt you'll have any problems using it. I did use this lib with a single surface particle system I wrote. Although, I modified it for one user Type which means I then don't use Handle and Object. Handle and Object are not the fastest of commands, for what reason I don't know. But, don't get me wrong, they are still fast. How many items do you plan to go through? which linked list lib are you refering to? If all you're looking for is speed then the fastest solution is to use 2 types and then use for each, like so. type tGood field Name$ end type type tBad field Name$ end type good.tGood = new tGood ... bad.tBad = new tBad ... for good.tGood = each tGood print good\name next for bad.tBad = each tBad print bad\name next this would be the fastest, faster than arrays, but it can get very confusing and messy since could have very many Types all over the place. |