TList.LinkAtIndex() ?

BlitzMax Forums/BlitzMax Programming/TList.LinkAtIndex() ?

ima747(Posted 2010) [#1]
I can get an object at a specific point using ValueAtIndex on a TList, but I need to insert a new object at that position, so I'm using InsertBeforeLink, but I need to get the Link, which I do with FindLink... which is fine, unless there's 2 of the same object in my list... then it only find the first one...

So. Is there a way to get a link at a position, OR, just insert an object at a specific position without a link besides the following...

get FirstLink() then manually follow it's NextLink() and counting until you reach the position you want...


Arowx(Posted 2010) [#2]
ValueAtIndex is already doing that (cycling down to the index value) what you need is a LinkAtIndex Method you could easily add this to your own linkedlist.bmx or you could extend the TList and add this function to your own list?

If you explain what you are using the list for and why you need to insert items it might help with other possible solution.


ima747(Posted 2010) [#3]
I'm using the list to order a bunch of objects that need to be added to, removed from, inserted into, and object are allowed to appear multiple times.

I'm tempted to modify my linkdlist.bmx but I know I'll forget I made changes by the next update and then be SOL when my project no longer works, so for now I'm just going to write a function to do the digging.

With that in mind, perhaps a feature request for future versions of BMax to include a LinkAtIndex() function, which should be pretty trivial since it essentially already exists in the ValueAtIndex() function, it just needs 1 less step (don't resolve the link into an object)... anyone else want to see it?


ima747(Posted 2010) [#4]
For those that might find such a feature useful, here's what I've got
Function LinkAtIndex:TLink(list:TList, index:int)
	Assert index>=0 Else "Object index must be positive"
	Local link:TLink=list._head._succ
	While link<>list._head
		If Not index Return link
		link=link._succ
		index:-1
	Wend
	RuntimeError "List index out of range"
End Function


just a modified version of the ValueAtIndex() method. All may use, and enjoy. Just remove the references to list to make it a method if you prefer.


Arowx(Posted 2010) [#5]
The best interim option would be to ...

1. Create a new Type that extends TList
2. Just copy the method ValueAtIndex and rename it to LinkAtIndex.
3. Change it's return value to the TLink of the desired entry.

Test it and then anywhere where you need a TList with this feature just use your modiefied list Type ;o)

A brl update to ValueAtIndex could add a TLink Var to the call that would return the link of the value at that index, or just a LinkAtIndex method!

EDIT Or you can use the above, sometimes I forget that a there is no private scope in BlitzMax ;o)


ima747(Posted 2010) [#6]
I was going to use a modified TList type, but I'm not sure how many of my lists I would need to go back and change eventually, was just easier to implement it as a function in my current project. I would just add it to the bmax source but like I said I'd forget and then 6 months from now bad things would happen ;0)


Czar Flavius(Posted 2010) [#7]
Can't you use TLink.PrevLink()?