list remove object

BlitzMax Forums/BlitzMax Programming/list remove object

Jur(Posted 2009) [#1]
Has anyone have encountered problems with command list.Remove(o:object)? Like that it removes a wrong object? In my code I am using this command and it keeps removing the first object from the list instead the wanted one. I cant reproduce this in a simple sample code. The FindLink and RemoveLink approach also does not work, but if I get the link through list looping than RemoveLink works.


Brucey(Posted 2009) [#2]
No. It always works as expected.

I would suggest it is probably a bug in your code.


GfK(Posted 2009) [#3]
Same, always worked fine, for me.


ziggy(Posted 2009) [#4]
Don't get me wrong but, if I remember correctly, the only changes that have been made to lists in the last years is a new sort algorithm and the inclussion of exceptions when adding null objects. None of them affect the find object method that has been there for about 4 years, so all in all it seems like there's a bug in your code rather than in tlist. Just an impression, no offense intended!


Gabriel(Posted 2009) [#5]
Have you overriden the object's Compare method in order to customize how the list is sorted?


Otus(Posted 2009) [#6]
Maybe a bug in your Compare method?

Edit: Gabriel beat me.


Jur(Posted 2009) [#7]
Yes, I have overriden the compare method and that is the cause of the problem indeed. If I comment it out then the problem goes away! Still, I have no idea what is actually wrong with my compare method (it do the job of comparing) and why it influence remove object at all. I am trying to reproduce the problem in simple code but so far without success.
Ok, here is my compare method:

Method Compare(otherObject:Object)
	Local OtherProfile:TPlayerProfile = TPlayerProfile (otherObject)
	If Not OtherProfile Then Return 1		
	Return scoreReached - OtherProfile.scoreReached
End Method	


I am using two lists "Profiles" and "HiScores", both filled with objects of "TPlayerProfile" type, which contains the Compare method. I am sorting only "HiScores" list. The problems occurs when I am removing an object from the "Profiles" list. I am using the references to these "TPlayerProfile" objects also in array somewhere else in the code, and one is always stored in a pointer variable.

Jure


Otus(Posted 2009) [#8]
Try this:
Method Compare%(otherObject:Object)
	Local OtherProfile:TPlayerProfile = TPlayerProfile (otherObject)
	If Not OtherProfile Then Return Super.Compare(otherObject)
	If scoreReached = OtherProfile.scoreReached Then Return Super.Compare(otherObject)
	Return scoreReached - OtherProfile.scoreReached
End Method

(Adding multiple types of objects may still mess sorting.)

Edit: The problem is that FindLink uses Compare - if it returns 0, the objects are considered the same. You might want to use something like player names instead of Super.Compare when scores are equal, though.


Jur(Posted 2009) [#9]
The problem disappeared with the above method! Thank you Otus! But I am not sure if this would not influence hiscore sorting. To be on the safe side, I just made a custom sorting function as an argument for Sort method.


Kurator(Posted 2009) [#10]
The Problem is in TList Compare is used for Sorting AND for determining Equality.

With only implementing sorting logic in your compare you will get this uncontrolled behaviour of TList that it removes entries from your compare method says "Oh, they are equal!" and returning 0.

But mostly you want to remove a specific instance of an object in your list. TList uses internally also the compare method to find the "right" link to delete...

Imho it is only sure to return 0 when it is the same instance (Self = Other for the above example) and return only -1 and 1 when you compare values / fields of your instance. It is a very small trade off if you return 1 on equal values - it doesn't affect the sorting, but it keeps you on track with deleting elements from list.

Personally I would prefer if BMax would use some other kind of determining equalitiy