dumb way to remove stuff, seeking smart way

BlitzMax Forums/BlitzMax Programming/dumb way to remove stuff, seeking smart way

Jeroen(Posted 2005) [#1]
The following functions are part of a Type. When I create an object, I also give it an ID (Field). When I remove an object from the list, I use this ID: I loop through the whole list, until I find the ID, and then I remove it.

But, is this really neccessary? It seems a dumb way of doing it. Can't I just directly remove list item 4 or 6 for example? Does a list come with an "ID" internally?

Thanks.

	Function removeByID(id)
		For Local p:plaatje = EachIn plaatje.plaatjesList
			If p.id = id 
				p.removeObject() 
				Return
			End If
		Next

		Print "Could not remove object with id "+id+". Not found."
		End

	End Function

	Method removeObject()
		ListRemove plaatjesList,Self
	End Method
	



Perturbatio(Posted 2005) [#2]
if ListContains(yourvar) then List.Remove(yourvar)


Jeroen(Posted 2005) [#3]
Huh? Does listContains look in the entire object?

e.g contents of item in list:

bike.size=400
bike.brand="super bike"
bike.color="yellow"

List.Remove(400); clears reference to object with size=400?
Or should I type: List.Remove(bike.size=400))?

I don't get it :-D


ziggy(Posted 2005) [#4]
you should write:
List.Remove(bike)


Jeroen(Posted 2005) [#5]
Okay, let me try to explain a bit better :-)

Let's say I have created 100 bikes. (Types with methods/functions)
I keep track of the bikes in a tList so that I can do a forEach.

Now I want to delete bike 20.

How can I directly reference to bike 20 without looping through all the bikes and find bike 20?


ImaginaryHuman(Posted 2005) [#6]
Sounds like you also need an array which you use as a `lookup` or map of what's in the linked list?

You should also maintain a counter of how many items are in the list, and add any new items at that counter position in the array. When you remove an item you should copy the item from counter position -1, to the location you are removing, and delete the item at counter position -1, then decrease counter position by 1. This automatically de-fragments the array and keeps it compact.

Since linked list items can be anywhere in memory, you would otherwise have to step through them all till you get to the one you want. That's the price you pay for having it be dynamic. Unless anyone has a better algorithm.


N(Posted 2005) [#7]
I assume you want something like this...

Type Item
     Global List:TList
     Field Link:TLink
     
     Method New( )
          Link = List.AddLast( Self )
     End Method

     Function RemoveIndex( idx:Int )
          Item( List.ValueAtIndex( idx ) ).Link.Remove( )
     End Function
End Type
Item.List = New TList



Jeroen(Posted 2005) [#8]
Thanks Noel! That was it!

Btw List.AddLast doesnt work here, I use
ListAddLast listname,object


N(Posted 2005) [#9]
All I can say is that it should work, since it all builds fine on my end.


Jeroen(Posted 2005) [#10]
hmmmm it works here now too. had some computer problems.
Weird enough though, AddLast is not syntax highlighted!


N(Posted 2005) [#11]
That's because it's a method and the BlitzMax IDE is piss.


Jeroen(Posted 2005) [#12]
well it's a lot better than B3d's, but yes, with syntax highlighting, sometimes random letters are colored yellow and such. weird.


Tom(Posted 2005) [#13]
Charming Noel :S