A Tlist extension
BlitzMax Forums/BlitzMax Programming/A Tlist extension
| ||
I am trying to split a list from given object to one end or the other. going through the tlist and link is giving me a headache( not too bright) and the fact that I have little experience with link lists make it harder to understand. so far I have this code:SuperStrict Type TsuperList Extends TList Method ExtractToEnd:TsuperList(O:Object) Local newList:TsuperList = New TsuperList Local link:TLink = findlink(o) newList._head._succ = link newList._head._pred = _head._pred _head._pred = link._pred _head._pred._succ = _head Return newList End Method Method ExtractToStart(O:Object) End Method Method AppendToStart(list:TsuperList) End Method Method AppendToEnd(list:TsuperList) End Method End Type Local mylist:TSuperList = New TSuperList mylist.addlast("me") myList.addlast("you") Local name:String = "her" mylist.addlast(name) mylist.addlast("she") mylist.addlast("him") mylist.addlast("i") Local newList:TsuperList = mylist.ExtractToend(name) Print "new list four:" For name:String = EachIn newlist Print name Delay(10) Next Print Print "______________" Print "new list first :" + String(newlist.first()) Print "new list last :" + String(newList.last()) Print "new list count :" + newlist.count() Print "______________" Print Print "remaining from mylist first :" + String(mylist.first()) Print "remaining from mylist last :" + String(mylist.last()) Print "remaining from mylist count :" + mylist.count() Print "______________" Print "left on original list " For name:String = EachIn mylist Print name Delay(10) Next but it breaks when it has to display the newList.count(). if I remove this line everything works fine. obviously I am doing something wrong but I can't figure what. help please. [edit] the delay is used to prevent the program from locking in case it creates a cyclic redundancy. |
| ||
What exactly are you doing? |
| ||
I did a clone of spider solitaire, but I bult my own link list from scratch. it works fine as it is. Now I am trying to use bmax version but there is that one thing, it is not working yet and I can't figure out the problem: I save the individual card stacks in lists. as you must know in the game you can grab parts of the card stacks and move/add them to other stacks. bmax lists doesn't have that and therefore I am trying to implement it. I just want to know what I am doing wrong in the ExtractToEnd method. and yes, I know I can grab one at a time and move them to a new list but I kind of figure it is kind of slow that way therefore I am trying to do it more efficiently. |
| ||
You don't appear to be changing the _succ of the original link's _pred |
| ||
You don't appear to be changing the _succ of the original link's _pred I am tryed to follow you but failed. the original list is working correct(I think). if you comment the newList.count() line out everything is displayed correctly. the problem is with the newList. |
| ||
Assuming you're splitting the list on O, this is probably closer to what you're wanting.SuperStrict Type TsuperList Extends TList Method ExtractToEnd:TsuperList(O:Object) If _head._succ = _head Then Return New TsuperList EndIf Local fl:TLink = FindLink(O) If fl = Null Then Throw "Link not found in list" EndIf Local newList:TsuperList = New TsuperList If _head._succ._succ = _head Then Local t:TLink = newList._head newList._head = _head _head = t Return newList EndIf newList._head._succ = fl newList._head._pred = _head._pred _head._pred._succ = newList._head _head._pred = fl._pred _head._pred._succ = _head fl._pred = newList._head Return newList End Method Method ExtractToStart(O:Object) End Method Method AppendToStart(list:TsuperList) End Method Method AppendToEnd(list:TsuperList) End Method End Type Local mylist:TSuperList = New TSuperList mylist.addlast("me") myList.addlast("you") Local name:String = "her" mylist.addlast(name) mylist.addlast("she") mylist.addlast("him") mylist.addlast("i") Local newList:TsuperList = mylist.ExtractToend(name) Print "new list last four:" For name:String = EachIn newlist Print name Delay(10) Next Print Print "______________" Print "new list first :" + String(newlist.first()) Print "new list last :" + String(newList.last()) Print "new list count :" + newlist.count() Print "______________" Print Print "remaining from mylist first :" + String(mylist.first()) Print "remaining from mylist last :" + String(mylist.last()) Print "remaining from mylist count :" + mylist.count() Print "______________" Print "left on original list " For name:String = EachIn mylist Print name Delay(10) Next |
| ||
great thanks! now lets see if I can figure out how yo did it so I can figure out the rest. I will probably be back with more problems. |
| ||
@Nilium you know it crashes when grabbing all of the objects from the original list and trying to display the original mylist.count() Funny, it fails in 1.30 but works in 1.32. if you want to try it: |
| ||
I checked the structure in the debugger and I'm not seeing anything unusual about it, so I don't know what the issue is. |
| ||
Nilium sorry, I was in a hurry when I posted earlier and therefor I jumped into conclusion. It is working as it should. It bugs out when I try to get the first item simply because there are no items in the list. It seems to be normal behavior for previous versions to 1.32 or at least for 1.30. I didn't want to switch to 1.32 until it was more stable or at least bugs report slowed down so for now I am just going to work around it. Thank you again. |
| ||
yesterday I stayed working on this all afternoon until I figured it out completely (I know lame) and finally completed it. Here it is working, as it should, I think: if anybody is interested, use at your own risk. note: the merge list is cleared to prevent corruption of the "merged" list. thanks again for your help Nilium. |