help with a list of numbers.

BlitzMax Forums/BlitzMax Programming/help with a list of numbers.

ckob(Posted 2008) [#1]
I'm not real familiar with Tlist so im wondering if thats the best way to do this.

Anyway I have a listbox which is filled with names of various model files, I want to remove from this list and move anything higher up in the list down to take the place of the removed number so for example.
pseudo code.
a[100]
if delete selectedgadgetitem()
   remove selected item from list
   move all items above select item down
endif


This is what I am using right now:

selectedprops:Int = SelectedGadgetItem(proplist)
RemoveGadgetItem(proplist,selectedprops)
FreeEntity props_Entity[selectedprops + 1]
selectedprops = selectedprops + 1
For i = selectedprops To PropsCount 
DebugLog "removing: " + selectedprops
DebugLog "Copying: " + i
props_Entity[i] = CopyMesh(props_Entity[i+1])	
propsX[i] = propsX[i+1]
propsY[i] = propsY[i+1]
propsZ[i] = propsZ[i+1]
Next
PropsCount = PropsCount -1




again im sure Tlist is probebly the best way to handle this variable but again im unfamiliar with it.


ImaginaryHuman(Posted 2008) [#2]
TList is just a linked list. A link list boils down to a `previous link` and `next link` pointer. You can embed a custom type object into each link in the list and remove them as you're intending. The only trick then is finding which link contains the item you want to remove, since linked lists usually do not have index numbers for each link. You could maintain an array of indexes if that helps, or search through the links until you find the one you want to remove.

You can add linked-list functionality to any custom type by just adding a Previous and Next field to the type. But TList is a nice linked list implementation for most purposes. You just make a list by adding objects onto the end (`add last` I think), then it's just a matter of finding the object you want to remove.


Arowx(Posted 2008) [#3]
Think of a linked list as a chain, and when you remove a link the chain rejoins around the missing link's position.

In effect the list items above the removed items are moved down, or you could say the items below are moved up! ;0)

This happens automatically, that the TList is doing in the background is something like this.

Say A - B - C are linked list items and you remove B.

Each list stores a link to the previous and next link's.

As B is removed A's next link is no longer valid so it now uses B's next link to C
And the same goes for C as it's previous link is invalid so it now uses's B's previous link to A.

Hope this helps.

Linked Lists are great for adding and storing an unknown quantity of items.

However if you need to access them via an index and not just loop through them then you are better off with a Map which allows you to have a key or index for each item and find the indexed item quickly.


Czar Flavius(Posted 2008) [#4]
I like to think of lists like a pile of paper. You can start at the top and move your way down the pile, you can easily add or remove sheets anywhere in the pile. The only thing you can't do, is jump to a specific sheet in the middle of the pile, because you don't know where it is. You have to search through the sheets from the top until you reach the sheet you want.


ckob(Posted 2008) [#5]
all these helpful explanations but no examples, I get how they work but need to see them visually.


tonyg(Posted 2008) [#6]
This is good and contains info on tlist.
Then there is this example and this one and info for tlink


Czar Flavius(Posted 2008) [#7]
Is this any help?

SuperStrict
SeedRnd MilliSecs()

Type TThing
	Global list:TList = New TList 'the list can be stored in the type
	Function make(newinfo:Int) 'makes a new thing with some arbitary info in it, and adds it to the list
		Local temp:TThing = New TThing
		temp.info = newinfo
		list.addLast(temp)
	End Function
	
	Field info:Int
	Method deleteme()
		list.remove(Self)
	End Method	
End Type

For Local i:Int = 1 To 10 'make 10 things with a random info and put them in the list
	TThing.make(Rand(0,3))
Next

For Local temp:TThing = EachIn TThing.list 'print them out to see visually your collection
	Print temp.info
Next

Print ""

For Local temp:TThing = EachIn TThing.list 'if the info is 0 then remove that thing from the list. notice how you can pluck them from the middle of the list no problem
	If temp.info = 0
		temp.deleteme()
	EndIf
Next

For Local temp:TThing = EachIn TThing.list 'print the new list with some removed, unless by chance no things were given 0 hehe
	Print temp.info
Next
'you can add new things or remove things in the middle of the list no problem, unlike an array. but you cannot grab a specific one you want easily. you must use for loop to go through list and find it



ckob(Posted 2008) [#8]
thanks for all the info ill look it over and try to get it working as soon as possible!