TLink work-around?

Monkey Forums/Monkey Programming/TLink work-around?

GfK(Posted 2011) [#1]
I have a List object and want to get the second-to-last element in the list.

Previously in Blitzmax I used TLinks for this, which aren't supported in Monkey. I just got to the end of rewriting the code to get around that and found that TList.ValueAtIndex() isn't there any more either!

Ideas? I'm currently doing this until I learn of a better way:
			count = Self.visiblePages.Count()
			If count > 1
				'get second-to-last item in list
				count = 0
				For t = EachIn Self.mylist.Backwards()
					count+=1
					If count = 2
						Exit
					EndIf
				Next
                        EndIf



Goodlookinguy(Posted 2011) [#2]
If you use a stack instead of a list, you can use Stack.Get(index) to retrieve an object from the index. Alternatively, look at list.monkey in the Monkey module folder and you should see a way to modify it to do what you want to do. I'd write it up for you, but I have to leave right now.


Uncle(Posted 2011) [#3]
Perhaps you could use an IntMap. Just set the key int to a sequential index number, and then you can just Get the value by using Get(index).

I too miss not having valueAtIndex, but Im working around it using IntMaps.


GfK(Posted 2011) [#4]
Would Stack.Get() be slower than the current method I'm using? I.e. does it have to iterate through all objects to get to the requested index?


Jesse(Posted 2011) [#5]
tlink's are supported in monkey as Nodes but they are not equally implemented in monkey lists.


[edited]
confused between bmax and monkey.

that's what I get by going back and forth in both languages.
I could have sworn I used them in Monkey.

next time I comment, I will make sure to verify.


Samah(Posted 2011) [#6]
If you use Diddy's ArrayList you can do this fairly easily. It's array indexed, so it's O(1).
Local mylist:ArrayList<myClass> = New ArrayList<myClass>
...add elements here...
Local myitem:myClass = mylist.Get(mylist.Size-2)

Edit: That should be Size, not Length.


AdamRedwoods(Posted 2011) [#7]
Monkey Nodes have prev() and next() methods?
I can't find them in v44.

I created my own module for prev() and next() when dealing with nodes, since I was surprised that Monkey did not have them built-in to Lists.


Jesse(Posted 2011) [#8]
was sure I used them in my project. Maybe I confused them. sorry.
now I feel stupid.


AdamRedwoods(Posted 2011) [#9]
No worries, I thought Mark may have put them in on some version that I missed.

But, he should! I don't know why it's not in the List class.


Foppy(Posted 2011) [#10]
You could create a custom linked list.

1) Add "fNext" and "fPrevious" fields to the class;
2) Add global "gFirst" and "gLast" pointers to the class;
3) Add a function to the class for adding an object to the list.

Now you can say:
Local secondToLast:MyClass = Null

If (MyClass.gLast <> null) Then
  secondToLast = MyClass.gLast.fPrevious
End if